1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * PSCI CPU idle driver.
4 *
5 * Copyright (C) 2019 ARM Ltd.
6 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
7 */
8
9 #define pr_fmt(fmt) "CPUidle PSCI: " fmt
10
11 #include <linux/cpuhotplug.h>
12 #include <linux/cpu_cooling.h>
13 #include <linux/cpuidle.h>
14 #include <linux/cpumask.h>
15 #include <linux/cpu_pm.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/psci.h>
22 #include <linux/pm_domain.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26
27 #include <asm/cpuidle.h>
28 #include <trace/hooks/cpuidle_psci.h>
29
30 #include "cpuidle-psci.h"
31 #include "dt_idle_states.h"
32
33 struct psci_cpuidle_data {
34 u32 *psci_states;
35 struct device *dev;
36 };
37
38 static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
39 static DEFINE_PER_CPU(u32, domain_state);
40 static bool psci_cpuidle_use_cpuhp;
41
psci_set_domain_state(u32 state)42 void psci_set_domain_state(u32 state)
43 {
44 __this_cpu_write(domain_state, state);
45 }
46
psci_get_domain_state(void)47 static inline u32 psci_get_domain_state(void)
48 {
49 return __this_cpu_read(domain_state);
50 }
51
psci_enter_state(int idx,u32 state)52 static inline int psci_enter_state(int idx, u32 state)
53 {
54 return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, idx, state);
55 }
56
__psci_enter_domain_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx,bool s2idle)57 static int __psci_enter_domain_idle_state(struct cpuidle_device *dev,
58 struct cpuidle_driver *drv, int idx,
59 bool s2idle)
60 {
61 struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
62 u32 *states = data->psci_states;
63 struct device *pd_dev = data->dev;
64 u32 state;
65 int ret;
66
67 ret = cpu_pm_enter();
68 if (ret)
69 return -1;
70
71
72 /* Do runtime PM to manage a hierarchical CPU toplogy. */
73 rcu_irq_enter_irqson();
74
75 trace_android_vh_cpuidle_psci_enter(dev, s2idle);
76
77 if (s2idle)
78 dev_pm_genpd_suspend(pd_dev);
79 else
80 pm_runtime_put_sync_suspend(pd_dev);
81 rcu_irq_exit_irqson();
82
83 state = psci_get_domain_state();
84 if (!state)
85 state = states[idx];
86
87 ret = psci_cpu_suspend_enter(state) ? -1 : idx;
88
89 rcu_irq_enter_irqson();
90 if (s2idle)
91 dev_pm_genpd_resume(pd_dev);
92 else
93 pm_runtime_get_sync(pd_dev);
94
95 trace_android_vh_cpuidle_psci_exit(dev, s2idle);
96
97 rcu_irq_exit_irqson();
98
99 cpu_pm_exit();
100
101 /* Clear the domain state to start fresh when back from idle. */
102 psci_set_domain_state(0);
103 return ret;
104 }
105
psci_enter_domain_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx)106 static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
107 struct cpuidle_driver *drv, int idx)
108 {
109 return __psci_enter_domain_idle_state(dev, drv, idx, false);
110 }
111
psci_enter_s2idle_domain_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx)112 static int psci_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
113 struct cpuidle_driver *drv,
114 int idx)
115 {
116 return __psci_enter_domain_idle_state(dev, drv, idx, true);
117 }
118
psci_idle_cpuhp_up(unsigned int cpu)119 static int psci_idle_cpuhp_up(unsigned int cpu)
120 {
121 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
122
123 if (pd_dev)
124 pm_runtime_get_sync(pd_dev);
125
126 return 0;
127 }
128
psci_idle_cpuhp_down(unsigned int cpu)129 static int psci_idle_cpuhp_down(unsigned int cpu)
130 {
131 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
132
133 if (pd_dev) {
134 pm_runtime_put_sync(pd_dev);
135 /* Clear domain state to start fresh at next online. */
136 psci_set_domain_state(0);
137 }
138
139 return 0;
140 }
141
psci_idle_init_cpuhp(void)142 static void psci_idle_init_cpuhp(void)
143 {
144 int err;
145
146 if (!psci_cpuidle_use_cpuhp)
147 return;
148
149 err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
150 "cpuidle/psci:online",
151 psci_idle_cpuhp_up,
152 psci_idle_cpuhp_down);
153 if (err)
154 pr_warn("Failed %d while setup cpuhp state\n", err);
155 }
156
psci_enter_idle_state(struct cpuidle_device * dev,struct cpuidle_driver * drv,int idx)157 static int psci_enter_idle_state(struct cpuidle_device *dev,
158 struct cpuidle_driver *drv, int idx)
159 {
160 u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
161
162 return psci_enter_state(idx, state[idx]);
163 }
164
165 static const struct of_device_id psci_idle_state_match[] = {
166 { .compatible = "arm,idle-state",
167 .data = psci_enter_idle_state },
168 { },
169 };
170
psci_dt_parse_state_node(struct device_node * np,u32 * state)171 int psci_dt_parse_state_node(struct device_node *np, u32 *state)
172 {
173 int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
174
175 if (err) {
176 pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
177 return err;
178 }
179
180 if (!psci_power_state_is_valid(*state)) {
181 pr_warn("Invalid PSCI power state %#x\n", *state);
182 return -EINVAL;
183 }
184
185 return 0;
186 }
187
psci_dt_cpu_init_topology(struct cpuidle_driver * drv,struct psci_cpuidle_data * data,unsigned int state_count,int cpu)188 static int psci_dt_cpu_init_topology(struct cpuidle_driver *drv,
189 struct psci_cpuidle_data *data,
190 unsigned int state_count, int cpu)
191 {
192 /* Currently limit the hierarchical topology to be used in OSI mode. */
193 if (!psci_has_osi_support())
194 return 0;
195
196 data->dev = psci_dt_attach_cpu(cpu);
197 if (IS_ERR_OR_NULL(data->dev))
198 return PTR_ERR_OR_ZERO(data->dev);
199
200 /*
201 * Using the deepest state for the CPU to trigger a potential selection
202 * of a shared state for the domain, assumes the domain states are all
203 * deeper states.
204 */
205 drv->states[state_count - 1].enter = psci_enter_domain_idle_state;
206 drv->states[state_count - 1].enter_s2idle = psci_enter_s2idle_domain_idle_state;
207 psci_cpuidle_use_cpuhp = true;
208
209 return 0;
210 }
211
psci_dt_cpu_init_idle(struct device * dev,struct cpuidle_driver * drv,struct device_node * cpu_node,unsigned int state_count,int cpu)212 static int psci_dt_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
213 struct device_node *cpu_node,
214 unsigned int state_count, int cpu)
215 {
216 int i, ret = 0;
217 u32 *psci_states;
218 struct device_node *state_node;
219 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
220
221 state_count++; /* Add WFI state too */
222 psci_states = devm_kcalloc(dev, state_count, sizeof(*psci_states),
223 GFP_KERNEL);
224 if (!psci_states)
225 return -ENOMEM;
226
227 for (i = 1; i < state_count; i++) {
228 state_node = of_get_cpu_state_node(cpu_node, i - 1);
229 if (!state_node)
230 break;
231
232 ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
233 of_node_put(state_node);
234
235 if (ret)
236 return ret;
237
238 pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
239 }
240
241 if (i != state_count)
242 return -ENODEV;
243
244 /* Initialize optional data, used for the hierarchical topology. */
245 ret = psci_dt_cpu_init_topology(drv, data, state_count, cpu);
246 if (ret < 0)
247 return ret;
248
249 /* Idle states parsed correctly, store them in the per-cpu struct. */
250 data->psci_states = psci_states;
251 return 0;
252 }
253
psci_cpu_init_idle(struct device * dev,struct cpuidle_driver * drv,unsigned int cpu,unsigned int state_count)254 static int psci_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
255 unsigned int cpu, unsigned int state_count)
256 {
257 struct device_node *cpu_node;
258 int ret;
259
260 /*
261 * If the PSCI cpu_suspend function hook has not been initialized
262 * idle states must not be enabled, so bail out
263 */
264 if (!psci_ops.cpu_suspend)
265 return -EOPNOTSUPP;
266
267 cpu_node = of_cpu_device_node_get(cpu);
268 if (!cpu_node)
269 return -ENODEV;
270
271 ret = psci_dt_cpu_init_idle(dev, drv, cpu_node, state_count, cpu);
272
273 of_node_put(cpu_node);
274
275 return ret;
276 }
277
psci_cpu_deinit_idle(int cpu)278 static void psci_cpu_deinit_idle(int cpu)
279 {
280 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
281
282 psci_dt_detach_cpu(data->dev);
283 psci_cpuidle_use_cpuhp = false;
284 }
285
psci_idle_init_cpu(struct device * dev,int cpu)286 static int psci_idle_init_cpu(struct device *dev, int cpu)
287 {
288 struct cpuidle_driver *drv;
289 struct device_node *cpu_node;
290 const char *enable_method;
291 int ret = 0;
292
293 cpu_node = of_cpu_device_node_get(cpu);
294 if (!cpu_node)
295 return -ENODEV;
296
297 /*
298 * Check whether the enable-method for the cpu is PSCI, fail
299 * if it is not.
300 */
301 enable_method = of_get_property(cpu_node, "enable-method", NULL);
302 if (!enable_method || (strcmp(enable_method, "psci")))
303 ret = -ENODEV;
304
305 of_node_put(cpu_node);
306 if (ret)
307 return ret;
308
309 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
310 if (!drv)
311 return -ENOMEM;
312
313 drv->name = "psci_idle";
314 drv->owner = THIS_MODULE;
315 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
316
317 /*
318 * PSCI idle states relies on architectural WFI to be represented as
319 * state index 0.
320 */
321 drv->states[0].enter = psci_enter_idle_state;
322 drv->states[0].exit_latency = 1;
323 drv->states[0].target_residency = 1;
324 drv->states[0].power_usage = UINT_MAX;
325 strcpy(drv->states[0].name, "WFI");
326 strcpy(drv->states[0].desc, "ARM WFI");
327
328 /*
329 * If no DT idle states are detected (ret == 0) let the driver
330 * initialization fail accordingly since there is no reason to
331 * initialize the idle driver if only wfi is supported, the
332 * default archictectural back-end already executes wfi
333 * on idle entry.
334 */
335 ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
336 if (ret <= 0)
337 return ret ? : -ENODEV;
338
339 /*
340 * Initialize PSCI idle states.
341 */
342 ret = psci_cpu_init_idle(dev, drv, cpu, ret);
343 if (ret) {
344 pr_err("CPU %d failed to PSCI idle\n", cpu);
345 return ret;
346 }
347
348 ret = cpuidle_register(drv, NULL);
349 if (ret)
350 goto deinit;
351
352 cpuidle_cooling_register(drv);
353
354 return 0;
355 deinit:
356 psci_cpu_deinit_idle(cpu);
357 return ret;
358 }
359
360 /*
361 * psci_idle_probe - Initializes PSCI cpuidle driver
362 *
363 * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
364 * to register cpuidle driver then rollback to cancel all CPUs
365 * registration.
366 */
psci_cpuidle_probe(struct platform_device * pdev)367 static int psci_cpuidle_probe(struct platform_device *pdev)
368 {
369 int cpu, ret;
370 struct cpuidle_driver *drv;
371 struct cpuidle_device *dev;
372
373 for_each_possible_cpu(cpu) {
374 ret = psci_idle_init_cpu(&pdev->dev, cpu);
375 if (ret)
376 goto out_fail;
377 }
378
379 psci_idle_init_cpuhp();
380 return 0;
381
382 out_fail:
383 while (--cpu >= 0) {
384 dev = per_cpu(cpuidle_devices, cpu);
385 drv = cpuidle_get_cpu_driver(dev);
386 cpuidle_unregister(drv);
387 psci_cpu_deinit_idle(cpu);
388 }
389
390 return ret;
391 }
392
393 static struct platform_driver psci_cpuidle_driver = {
394 .probe = psci_cpuidle_probe,
395 .driver = {
396 .name = "psci-cpuidle",
397 },
398 };
399
psci_idle_init(void)400 static int __init psci_idle_init(void)
401 {
402 struct platform_device *pdev;
403 int ret;
404
405 ret = platform_driver_register(&psci_cpuidle_driver);
406 if (ret)
407 return ret;
408
409 pdev = platform_device_register_simple("psci-cpuidle", -1, NULL, 0);
410 if (IS_ERR(pdev)) {
411 platform_driver_unregister(&psci_cpuidle_driver);
412 return PTR_ERR(pdev);
413 }
414
415 return 0;
416 }
417 device_initcall(psci_idle_init);
418