1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PM domains for CPUs via genpd - managed by cpuidle-psci.
4  *
5  * Copyright (C) 2019 Linaro Ltd.
6  * Author: Ulf Hansson <[email protected]>
7  *
8  */
9 
10 #define pr_fmt(fmt) "CPUidle PSCI: " fmt
11 
12 #include <linux/cpu.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_domain.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/psci.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 
22 #include "cpuidle-psci.h"
23 #include "dt_idle_genpd.h"
24 
25 struct psci_pd_provider {
26 	struct list_head link;
27 	struct device_node *node;
28 };
29 
30 static LIST_HEAD(psci_pd_providers);
31 static bool psci_pd_allow_domain_state;
32 
psci_pd_power_off(struct generic_pm_domain * pd)33 static int psci_pd_power_off(struct generic_pm_domain *pd)
34 {
35 	struct genpd_power_state *state = &pd->states[pd->state_idx];
36 	u32 *pd_state;
37 
38 	if (!state->data)
39 		return 0;
40 
41 	if (!psci_pd_allow_domain_state)
42 		return -EBUSY;
43 
44 	/* OSI mode is enabled, set the corresponding domain state. */
45 	pd_state = state->data;
46 	psci_set_domain_state(*pd_state);
47 
48 	return 0;
49 }
50 
psci_pd_init(struct device_node * np,bool use_osi)51 static int psci_pd_init(struct device_node *np, bool use_osi)
52 {
53 	struct generic_pm_domain *pd;
54 	struct psci_pd_provider *pd_provider;
55 	struct dev_power_governor *pd_gov;
56 	int ret = -ENOMEM;
57 
58 	pd = dt_idle_pd_alloc(np, psci_dt_parse_state_node);
59 	if (!pd)
60 		goto out;
61 
62 	pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
63 	if (!pd_provider)
64 		goto free_pd;
65 
66 	pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
67 
68 	/*
69 	 * Allow power off when OSI has been successfully enabled.
70 	 * On a PREEMPT_RT based configuration the domain idle states are
71 	 * supported, but only during system-wide suspend.
72 	 */
73 	if (use_osi) {
74 		pd->power_off = psci_pd_power_off;
75 		pd->flags |= GENPD_FLAG_ACTIVE_WAKEUP;
76 		if (IS_ENABLED(CONFIG_PREEMPT_RT))
77 			pd->flags |= GENPD_FLAG_RPM_ALWAYS_ON;
78 	} else {
79 		pd->flags |= GENPD_FLAG_ALWAYS_ON;
80 	}
81 
82 	/* Use governor for CPU PM domains if it has some states to manage. */
83 	pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
84 
85 	ret = pm_genpd_init(pd, pd_gov, false);
86 	if (ret)
87 		goto free_pd_prov;
88 
89 	ret = of_genpd_add_provider_simple(np, pd);
90 	if (ret)
91 		goto remove_pd;
92 
93 	pd_provider->node = of_node_get(np);
94 	list_add(&pd_provider->link, &psci_pd_providers);
95 
96 	pr_debug("init PM domain %s\n", pd->name);
97 	return 0;
98 
99 remove_pd:
100 	pm_genpd_remove(pd);
101 free_pd_prov:
102 	kfree(pd_provider);
103 free_pd:
104 	dt_idle_pd_free(pd);
105 out:
106 	pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
107 	return ret;
108 }
109 
psci_pd_remove(void)110 static void psci_pd_remove(void)
111 {
112 	struct psci_pd_provider *pd_provider, *it;
113 	struct generic_pm_domain *genpd;
114 
115 	list_for_each_entry_safe_reverse(pd_provider, it,
116 					 &psci_pd_providers, link) {
117 		of_genpd_del_provider(pd_provider->node);
118 
119 		genpd = of_genpd_remove_last(pd_provider->node);
120 		if (!IS_ERR(genpd))
121 			kfree(genpd);
122 
123 		of_node_put(pd_provider->node);
124 		list_del(&pd_provider->link);
125 		kfree(pd_provider);
126 	}
127 }
128 
psci_cpuidle_domain_sync_state(struct device * dev)129 static void psci_cpuidle_domain_sync_state(struct device *dev)
130 {
131 	/*
132 	 * All devices have now been attached/probed to the PM domain topology,
133 	 * hence it's fine to allow domain states to be picked.
134 	 */
135 	psci_pd_allow_domain_state = true;
136 }
137 
138 static const struct of_device_id psci_of_match[] = {
139 	{ .compatible = "arm,psci-1.0" },
140 	{}
141 };
142 
psci_cpuidle_domain_probe(struct platform_device * pdev)143 static int psci_cpuidle_domain_probe(struct platform_device *pdev)
144 {
145 	struct device_node *np = pdev->dev.of_node;
146 	bool use_osi = psci_has_osi_support();
147 	int ret = 0, pd_count = 0;
148 
149 	if (!np)
150 		return -ENODEV;
151 
152 	/*
153 	 * Parse child nodes for the "#power-domain-cells" property and
154 	 * initialize a genpd/genpd-of-provider pair when it's found.
155 	 */
156 	for_each_child_of_node_scoped(np, node) {
157 		if (!of_property_present(node, "#power-domain-cells"))
158 			continue;
159 
160 		ret = psci_pd_init(node, use_osi);
161 		if (ret)
162 			goto exit;
163 
164 		pd_count++;
165 	}
166 
167 	/* Bail out if not using the hierarchical CPU topology. */
168 	if (!pd_count)
169 		return 0;
170 
171 	/* Link genpd masters/subdomains to model the CPU topology. */
172 	ret = dt_idle_pd_init_topology(np);
173 	if (ret)
174 		goto remove_pd;
175 
176 	/* let's try to enable OSI. */
177 	ret = psci_set_osi_mode(use_osi);
178 	if (ret)
179 		goto remove_pd;
180 
181 	pr_info("Initialized CPU PM domain topology using %s mode\n",
182 		use_osi ? "OSI" : "PC");
183 	return 0;
184 
185 remove_pd:
186 	dt_idle_pd_remove_topology(np);
187 	psci_pd_remove();
188 exit:
189 	pr_err("failed to create CPU PM domains ret=%d\n", ret);
190 	return ret;
191 }
192 
193 static struct platform_driver psci_cpuidle_domain_driver = {
194 	.probe  = psci_cpuidle_domain_probe,
195 	.driver = {
196 		.name = "psci-cpuidle-domain",
197 		.of_match_table = psci_of_match,
198 		.sync_state = psci_cpuidle_domain_sync_state,
199 	},
200 };
201 
psci_idle_init_domains(void)202 static int __init psci_idle_init_domains(void)
203 {
204 	return platform_driver_register(&psci_cpuidle_domain_driver);
205 }
206 core_initcall(psci_idle_init_domains);
207