Merge tag 'probes-fixes-v6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / drivers / cpuidle / cpuidle-psci.c
CommitLineData
81d549e0
LP
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
9c6ceecb 11#include <linux/cpuhotplug.h>
fc7a3d9e 12#include <linux/cpu_cooling.h>
81d549e0
LP
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>
af5376a7 19#include <linux/device/faux.h>
81d549e0 20#include <linux/psci.h>
670c90de 21#include <linux/pm_domain.h>
ce85aef5 22#include <linux/pm_runtime.h>
81d549e0 23#include <linux/slab.h>
166bf835 24#include <linux/string.h>
171b66e2 25#include <linux/syscore_ops.h>
81d549e0
LP
26
27#include <asm/cpuidle.h>
7b764483 28#include <trace/events/power.h>
81d549e0 29
8554951a 30#include "cpuidle-psci.h"
81d549e0 31#include "dt_idle_states.h"
d72d7d6c 32#include "dt_idle_genpd.h"
81d549e0 33
8554951a
UH
34struct psci_cpuidle_data {
35 u32 *psci_states;
36 struct device *dev;
37};
38
3290e9f9 39struct psci_cpuidle_domain_state {
d0252ba8
UH
40 struct generic_pm_domain *pd;
41 unsigned int state_idx;
3290e9f9
UH
42 u32 state;
43};
44
8554951a 45static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
3290e9f9 46static DEFINE_PER_CPU(struct psci_cpuidle_domain_state, psci_domain_state);
4517b1c3 47static bool psci_cpuidle_use_syscore;
166bf835 48static bool psci_cpuidle_use_cpuhp;
a0cf3194 49
d0252ba8
UH
50void psci_set_domain_state(struct generic_pm_domain *pd, unsigned int state_idx,
51 u32 state)
a0cf3194 52{
d0252ba8 53 struct psci_cpuidle_domain_state *ds = this_cpu_ptr(&psci_domain_state);
a0cf3194 54
d0252ba8
UH
55 ds->pd = pd;
56 ds->state_idx = state_idx;
57 ds->state = state;
3290e9f9
UH
58}
59
60static inline void psci_clear_domain_state(void)
61{
62 __this_cpu_write(psci_domain_state.state, 0);
a0cf3194
UH
63}
64
69e26b4f
PZ
65static __cpuidle int __psci_enter_domain_idle_state(struct cpuidle_device *dev,
66 struct cpuidle_driver *drv, int idx,
67 bool s2idle)
a0cf3194
UH
68{
69 struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
70 u32 *states = data->psci_states;
ce85aef5 71 struct device *pd_dev = data->dev;
d0252ba8
UH
72 struct psci_cpuidle_domain_state *ds;
73 u32 state = states[idx];
a0cf3194
UH
74 int ret;
75
8b7ce5e4
UH
76 ret = cpu_pm_enter();
77 if (ret)
78 return -1;
79
ce85aef5 80 /* Do runtime PM to manage a hierarchical CPU toplogy. */
670c90de
UH
81 if (s2idle)
82 dev_pm_genpd_suspend(pd_dev);
83 else
84 pm_runtime_put_sync_suspend(pd_dev);
e038f7b8 85
d0252ba8
UH
86 ds = this_cpu_ptr(&psci_domain_state);
87 if (ds->state)
88 state = ds->state;
a0cf3194 89
7b764483 90 trace_psci_domain_idle_enter(dev->cpu, state, s2idle);
8b7ce5e4 91 ret = psci_cpu_suspend_enter(state) ? -1 : idx;
7b764483 92 trace_psci_domain_idle_exit(dev->cpu, state, s2idle);
a0cf3194 93
670c90de
UH
94 if (s2idle)
95 dev_pm_genpd_resume(pd_dev);
96 else
97 pm_runtime_get_sync(pd_dev);
ce85aef5 98
8b7ce5e4
UH
99 cpu_pm_exit();
100
d0252ba8
UH
101 /* Correct domain-idlestate statistics if we failed to enter. */
102 if (ret == -1 && ds->state)
103 pm_genpd_inc_rejected(ds->pd, ds->state_idx);
104
a0cf3194 105 /* Clear the domain state to start fresh when back from idle. */
3290e9f9 106 psci_clear_domain_state();
a0cf3194
UH
107 return ret;
108}
9ffeb6d0 109
670c90de
UH
110static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
111 struct cpuidle_driver *drv, int idx)
112{
113 return __psci_enter_domain_idle_state(dev, drv, idx, false);
114}
115
116static int psci_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
117 struct cpuidle_driver *drv,
118 int idx)
119{
120 return __psci_enter_domain_idle_state(dev, drv, idx, true);
121}
122
9c6ceecb
UH
123static int psci_idle_cpuhp_up(unsigned int cpu)
124{
125 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
126
127 if (pd_dev)
128 pm_runtime_get_sync(pd_dev);
129
130 return 0;
131}
132
133static int psci_idle_cpuhp_down(unsigned int cpu)
134{
135 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
136
137 if (pd_dev) {
138 pm_runtime_put_sync(pd_dev);
139 /* Clear domain state to start fresh at next online. */
3290e9f9 140 psci_clear_domain_state();
9c6ceecb
UH
141 }
142
143 return 0;
144}
145
171b66e2
UH
146static void psci_idle_syscore_switch(bool suspend)
147{
148 bool cleared = false;
149 struct device *dev;
150 int cpu;
151
152 for_each_possible_cpu(cpu) {
153 dev = per_cpu_ptr(&psci_cpuidle_data, cpu)->dev;
154
155 if (dev && suspend) {
156 dev_pm_genpd_suspend(dev);
157 } else if (dev) {
158 dev_pm_genpd_resume(dev);
159
160 /* Account for userspace having offlined a CPU. */
161 if (pm_runtime_status_suspended(dev))
162 pm_runtime_set_active(dev);
163
164 /* Clear domain state to re-start fresh. */
165 if (!cleared) {
3290e9f9 166 psci_clear_domain_state();
171b66e2
UH
167 cleared = true;
168 }
169 }
170 }
171}
172
173static int psci_idle_syscore_suspend(void)
174{
175 psci_idle_syscore_switch(true);
176 return 0;
177}
178
179static void psci_idle_syscore_resume(void)
180{
181 psci_idle_syscore_switch(false);
182}
183
184static struct syscore_ops psci_idle_syscore_ops = {
185 .suspend = psci_idle_syscore_suspend,
186 .resume = psci_idle_syscore_resume,
187};
188
4517b1c3
UH
189static void psci_idle_init_syscore(void)
190{
191 if (psci_cpuidle_use_syscore)
192 register_syscore_ops(&psci_idle_syscore_ops);
193}
194
166bf835 195static void psci_idle_init_cpuhp(void)
9c6ceecb
UH
196{
197 int err;
198
199 if (!psci_cpuidle_use_cpuhp)
200 return;
201
202 err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
203 "cpuidle/psci:online",
204 psci_idle_cpuhp_up,
205 psci_idle_cpuhp_down);
206 if (err)
207 pr_warn("Failed %d while setup cpuhp state\n", err);
208}
209
69e26b4f
PZ
210static __cpuidle int psci_enter_idle_state(struct cpuidle_device *dev,
211 struct cpuidle_driver *drv, int idx)
81d549e0 212{
8554951a 213 u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
9ffeb6d0 214
19235e47 215 return CPU_PM_CPU_IDLE_ENTER_PARAM_RCU(psci_cpu_suspend_enter, idx, state[idx]);
81d549e0
LP
216}
217
166bf835 218static const struct of_device_id psci_idle_state_match[] = {
81d549e0
LP
219 { .compatible = "arm,idle-state",
220 .data = psci_enter_idle_state },
221 { },
222};
223
166bf835 224int psci_dt_parse_state_node(struct device_node *np, u32 *state)
9ffeb6d0
LP
225{
226 int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
227
228 if (err) {
229 pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
230 return err;
231 }
232
233 if (!psci_power_state_is_valid(*state)) {
234 pr_warn("Invalid PSCI power state %#x\n", *state);
235 return -EINVAL;
236 }
237
238 return 0;
239}
240
166bf835
UH
241static int psci_dt_cpu_init_topology(struct cpuidle_driver *drv,
242 struct psci_cpuidle_data *data,
243 unsigned int state_count, int cpu)
7fbee48e
UH
244{
245 /* Currently limit the hierarchical topology to be used in OSI mode. */
246 if (!psci_has_osi_support())
247 return 0;
248
d72d7d6c 249 data->dev = dt_idle_attach_cpu(cpu, "psci");
7fbee48e
UH
250 if (IS_ERR_OR_NULL(data->dev))
251 return PTR_ERR_OR_ZERO(data->dev);
252
4517b1c3
UH
253 psci_cpuidle_use_syscore = true;
254
7fbee48e
UH
255 /*
256 * Using the deepest state for the CPU to trigger a potential selection
257 * of a shared state for the domain, assumes the domain states are all
1c4b2932
UH
258 * deeper states. On PREEMPT_RT the hierarchical topology is limited to
259 * s2ram and s2idle.
7fbee48e 260 */
670c90de 261 drv->states[state_count - 1].enter_s2idle = psci_enter_s2idle_domain_idle_state;
1c4b2932
UH
262 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
263 drv->states[state_count - 1].enter = psci_enter_domain_idle_state;
264 psci_cpuidle_use_cpuhp = true;
265 }
7fbee48e
UH
266
267 return 0;
268}
269
166bf835
UH
270static int psci_dt_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
271 struct device_node *cpu_node,
272 unsigned int state_count, int cpu)
9ffeb6d0 273{
1595e4b0 274 int i, ret = 0;
9ffeb6d0
LP
275 u32 *psci_states;
276 struct device_node *state_node;
8554951a 277 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
9ffeb6d0 278
1595e4b0 279 state_count++; /* Add WFI state too */
166bf835
UH
280 psci_states = devm_kcalloc(dev, state_count, sizeof(*psci_states),
281 GFP_KERNEL);
9ffeb6d0
LP
282 if (!psci_states)
283 return -ENOMEM;
284
1595e4b0 285 for (i = 1; i < state_count; i++) {
f08cfbfa 286 state_node = of_get_cpu_state_node(cpu_node, i - 1);
1595e4b0
UH
287 if (!state_node)
288 break;
289
9ffeb6d0
LP
290 ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
291 of_node_put(state_node);
292
293 if (ret)
166bf835 294 return ret;
9ffeb6d0
LP
295
296 pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
297 }
298
166bf835
UH
299 if (i != state_count)
300 return -ENODEV;
1595e4b0 301
7fbee48e
UH
302 /* Initialize optional data, used for the hierarchical topology. */
303 ret = psci_dt_cpu_init_topology(drv, data, state_count, cpu);
304 if (ret < 0)
166bf835 305 return ret;
8554951a
UH
306
307 /* Idle states parsed correctly, store them in the per-cpu struct. */
308 data->psci_states = psci_states;
9ffeb6d0 309 return 0;
9ffeb6d0
LP
310}
311
166bf835
UH
312static int psci_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
313 unsigned int cpu, unsigned int state_count)
9ffeb6d0
LP
314{
315 struct device_node *cpu_node;
316 int ret;
317
318 /*
319 * If the PSCI cpu_suspend function hook has not been initialized
320 * idle states must not be enabled, so bail out
321 */
322 if (!psci_ops.cpu_suspend)
323 return -EOPNOTSUPP;
324
325 cpu_node = of_cpu_device_node_get(cpu);
326 if (!cpu_node)
327 return -ENODEV;
328
166bf835 329 ret = psci_dt_cpu_init_idle(dev, drv, cpu_node, state_count, cpu);
9ffeb6d0
LP
330
331 of_node_put(cpu_node);
332
333 return ret;
334}
335
166bf835
UH
336static void psci_cpu_deinit_idle(int cpu)
337{
338 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
339
d72d7d6c 340 dt_idle_detach_cpu(data->dev);
4517b1c3 341 psci_cpuidle_use_syscore = false;
166bf835
UH
342 psci_cpuidle_use_cpuhp = false;
343}
344
345static int psci_idle_init_cpu(struct device *dev, int cpu)
81d549e0
LP
346{
347 struct cpuidle_driver *drv;
348 struct device_node *cpu_node;
349 const char *enable_method;
350 int ret = 0;
351
352 cpu_node = of_cpu_device_node_get(cpu);
353 if (!cpu_node)
354 return -ENODEV;
355
356 /*
357 * Check whether the enable-method for the cpu is PSCI, fail
358 * if it is not.
359 */
360 enable_method = of_get_property(cpu_node, "enable-method", NULL);
361 if (!enable_method || (strcmp(enable_method, "psci")))
362 ret = -ENODEV;
363
364 of_node_put(cpu_node);
365 if (ret)
366 return ret;
367
166bf835 368 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
81d549e0
LP
369 if (!drv)
370 return -ENOMEM;
371
166bf835
UH
372 drv->name = "psci_idle";
373 drv->owner = THIS_MODULE;
81d549e0
LP
374 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
375
376 /*
166bf835
UH
377 * PSCI idle states relies on architectural WFI to be represented as
378 * state index 0.
379 */
380 drv->states[0].enter = psci_enter_idle_state;
381 drv->states[0].exit_latency = 1;
382 drv->states[0].target_residency = 1;
383 drv->states[0].power_usage = UINT_MAX;
384 strcpy(drv->states[0].name, "WFI");
385 strcpy(drv->states[0].desc, "ARM WFI");
386
387 /*
81d549e0
LP
388 * If no DT idle states are detected (ret == 0) let the driver
389 * initialization fail accordingly since there is no reason to
390 * initialize the idle driver if only wfi is supported, the
391 * default archictectural back-end already executes wfi
392 * on idle entry.
393 */
394 ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
166bf835
UH
395 if (ret <= 0)
396 return ret ? : -ENODEV;
81d549e0
LP
397
398 /*
399 * Initialize PSCI idle states.
400 */
166bf835 401 ret = psci_cpu_init_idle(dev, drv, cpu, ret);
81d549e0
LP
402 if (ret) {
403 pr_err("CPU %d failed to PSCI idle\n", cpu);
166bf835 404 return ret;
81d549e0
LP
405 }
406
407 ret = cpuidle_register(drv, NULL);
408 if (ret)
166bf835 409 goto deinit;
81d549e0 410
fc7a3d9e
DL
411 cpuidle_cooling_register(drv);
412
81d549e0 413 return 0;
166bf835
UH
414deinit:
415 psci_cpu_deinit_idle(cpu);
81d549e0
LP
416 return ret;
417}
418
419/*
166bf835 420 * psci_idle_probe - Initializes PSCI cpuidle driver
81d549e0 421 *
68cb0139 422 * Initializes PSCI cpuidle driver for all present CPUs, if any CPU fails
81d549e0
LP
423 * to register cpuidle driver then rollback to cancel all CPUs
424 * registration.
425 */
af5376a7 426static int psci_cpuidle_probe(struct faux_device *fdev)
81d549e0
LP
427{
428 int cpu, ret;
429 struct cpuidle_driver *drv;
430 struct cpuidle_device *dev;
431
68cb0139 432 for_each_present_cpu(cpu) {
af5376a7 433 ret = psci_idle_init_cpu(&fdev->dev, cpu);
81d549e0
LP
434 if (ret)
435 goto out_fail;
436 }
437
4517b1c3 438 psci_idle_init_syscore();
9c6ceecb 439 psci_idle_init_cpuhp();
81d549e0
LP
440 return 0;
441
442out_fail:
443 while (--cpu >= 0) {
444 dev = per_cpu(cpuidle_devices, cpu);
445 drv = cpuidle_get_cpu_driver(dev);
446 cpuidle_unregister(drv);
166bf835 447 psci_cpu_deinit_idle(cpu);
81d549e0
LP
448 }
449
450 return ret;
451}
166bf835 452
af5376a7 453static struct faux_device_ops psci_cpuidle_ops = {
166bf835 454 .probe = psci_cpuidle_probe,
166bf835
UH
455};
456
5836ebeb
SH
457static bool __init dt_idle_state_present(void)
458{
39cdf87a
DC
459 struct device_node *cpu_node __free(device_node) =
460 of_cpu_device_node_get(cpumask_first(cpu_possible_mask));
5836ebeb
SH
461 if (!cpu_node)
462 return false;
463
39cdf87a
DC
464 struct device_node *state_node __free(device_node) =
465 of_get_cpu_state_node(cpu_node, 0);
5836ebeb
SH
466 if (!state_node)
467 return false;
468
469 return !!of_match_node(psci_idle_state_match, state_node);
470}
471
166bf835
UH
472static int __init psci_idle_init(void)
473{
af5376a7 474 struct faux_device *fdev;
166bf835 475
5836ebeb
SH
476 if (!dt_idle_state_present())
477 return 0;
166bf835 478
af5376a7
SH
479 fdev = faux_device_create("psci-cpuidle", NULL, &psci_cpuidle_ops);
480 if (!fdev) {
481 pr_err("Failed to create psci-cpuidle device\n");
482 return -ENODEV;
166bf835
UH
483 }
484
485 return 0;
486}
81d549e0 487device_initcall(psci_idle_init);