1 // SPDX-License-Identifier: GPL-2.0-only
3 * PSCI CPU idle driver.
5 * Copyright (C) 2019 ARM Ltd.
6 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
9 #define pr_fmt(fmt) "CPUidle PSCI: " fmt
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>
19 #include <linux/platform_device.h>
20 #include <linux/psci.h>
21 #include <linux/pm_domain.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/syscore_ops.h>
27 #include <asm/cpuidle.h>
29 #include "cpuidle-psci.h"
30 #include "dt_idle_states.h"
31 #include "dt_idle_genpd.h"
33 struct psci_cpuidle_data
{
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_syscore
;
41 static bool psci_cpuidle_use_cpuhp
;
43 void psci_set_domain_state(u32 state
)
45 __this_cpu_write(domain_state
, state
);
48 static inline u32
psci_get_domain_state(void)
50 return __this_cpu_read(domain_state
);
53 static __cpuidle
int __psci_enter_domain_idle_state(struct cpuidle_device
*dev
,
54 struct cpuidle_driver
*drv
, int idx
,
57 struct psci_cpuidle_data
*data
= this_cpu_ptr(&psci_cpuidle_data
);
58 u32
*states
= data
->psci_states
;
59 struct device
*pd_dev
= data
->dev
;
67 /* Do runtime PM to manage a hierarchical CPU toplogy. */
69 dev_pm_genpd_suspend(pd_dev
);
71 pm_runtime_put_sync_suspend(pd_dev
);
73 state
= psci_get_domain_state();
77 ret
= psci_cpu_suspend_enter(state
) ? -1 : idx
;
80 dev_pm_genpd_resume(pd_dev
);
82 pm_runtime_get_sync(pd_dev
);
86 /* Clear the domain state to start fresh when back from idle. */
87 psci_set_domain_state(0);
91 static int psci_enter_domain_idle_state(struct cpuidle_device
*dev
,
92 struct cpuidle_driver
*drv
, int idx
)
94 return __psci_enter_domain_idle_state(dev
, drv
, idx
, false);
97 static int psci_enter_s2idle_domain_idle_state(struct cpuidle_device
*dev
,
98 struct cpuidle_driver
*drv
,
101 return __psci_enter_domain_idle_state(dev
, drv
, idx
, true);
104 static int psci_idle_cpuhp_up(unsigned int cpu
)
106 struct device
*pd_dev
= __this_cpu_read(psci_cpuidle_data
.dev
);
109 pm_runtime_get_sync(pd_dev
);
114 static int psci_idle_cpuhp_down(unsigned int cpu
)
116 struct device
*pd_dev
= __this_cpu_read(psci_cpuidle_data
.dev
);
119 pm_runtime_put_sync(pd_dev
);
120 /* Clear domain state to start fresh at next online. */
121 psci_set_domain_state(0);
127 static void psci_idle_syscore_switch(bool suspend
)
129 bool cleared
= false;
133 for_each_possible_cpu(cpu
) {
134 dev
= per_cpu_ptr(&psci_cpuidle_data
, cpu
)->dev
;
136 if (dev
&& suspend
) {
137 dev_pm_genpd_suspend(dev
);
139 dev_pm_genpd_resume(dev
);
141 /* Account for userspace having offlined a CPU. */
142 if (pm_runtime_status_suspended(dev
))
143 pm_runtime_set_active(dev
);
145 /* Clear domain state to re-start fresh. */
147 psci_set_domain_state(0);
154 static int psci_idle_syscore_suspend(void)
156 psci_idle_syscore_switch(true);
160 static void psci_idle_syscore_resume(void)
162 psci_idle_syscore_switch(false);
165 static struct syscore_ops psci_idle_syscore_ops
= {
166 .suspend
= psci_idle_syscore_suspend
,
167 .resume
= psci_idle_syscore_resume
,
170 static void psci_idle_init_syscore(void)
172 if (psci_cpuidle_use_syscore
)
173 register_syscore_ops(&psci_idle_syscore_ops
);
176 static void psci_idle_init_cpuhp(void)
180 if (!psci_cpuidle_use_cpuhp
)
183 err
= cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING
,
184 "cpuidle/psci:online",
186 psci_idle_cpuhp_down
);
188 pr_warn("Failed %d while setup cpuhp state\n", err
);
191 static __cpuidle
int psci_enter_idle_state(struct cpuidle_device
*dev
,
192 struct cpuidle_driver
*drv
, int idx
)
194 u32
*state
= __this_cpu_read(psci_cpuidle_data
.psci_states
);
196 return CPU_PM_CPU_IDLE_ENTER_PARAM_RCU(psci_cpu_suspend_enter
, idx
, state
[idx
]);
199 static const struct of_device_id psci_idle_state_match
[] = {
200 { .compatible
= "arm,idle-state",
201 .data
= psci_enter_idle_state
},
205 int psci_dt_parse_state_node(struct device_node
*np
, u32
*state
)
207 int err
= of_property_read_u32(np
, "arm,psci-suspend-param", state
);
210 pr_warn("%pOF missing arm,psci-suspend-param property\n", np
);
214 if (!psci_power_state_is_valid(*state
)) {
215 pr_warn("Invalid PSCI power state %#x\n", *state
);
222 static int psci_dt_cpu_init_topology(struct cpuidle_driver
*drv
,
223 struct psci_cpuidle_data
*data
,
224 unsigned int state_count
, int cpu
)
226 /* Currently limit the hierarchical topology to be used in OSI mode. */
227 if (!psci_has_osi_support())
230 data
->dev
= dt_idle_attach_cpu(cpu
, "psci");
231 if (IS_ERR_OR_NULL(data
->dev
))
232 return PTR_ERR_OR_ZERO(data
->dev
);
234 psci_cpuidle_use_syscore
= true;
237 * Using the deepest state for the CPU to trigger a potential selection
238 * of a shared state for the domain, assumes the domain states are all
239 * deeper states. On PREEMPT_RT the hierarchical topology is limited to
242 drv
->states
[state_count
- 1].enter_s2idle
= psci_enter_s2idle_domain_idle_state
;
243 if (!IS_ENABLED(CONFIG_PREEMPT_RT
)) {
244 drv
->states
[state_count
- 1].enter
= psci_enter_domain_idle_state
;
245 psci_cpuidle_use_cpuhp
= true;
251 static int psci_dt_cpu_init_idle(struct device
*dev
, struct cpuidle_driver
*drv
,
252 struct device_node
*cpu_node
,
253 unsigned int state_count
, int cpu
)
257 struct device_node
*state_node
;
258 struct psci_cpuidle_data
*data
= per_cpu_ptr(&psci_cpuidle_data
, cpu
);
260 state_count
++; /* Add WFI state too */
261 psci_states
= devm_kcalloc(dev
, state_count
, sizeof(*psci_states
),
266 for (i
= 1; i
< state_count
; i
++) {
267 state_node
= of_get_cpu_state_node(cpu_node
, i
- 1);
271 ret
= psci_dt_parse_state_node(state_node
, &psci_states
[i
]);
272 of_node_put(state_node
);
277 pr_debug("psci-power-state %#x index %d\n", psci_states
[i
], i
);
280 if (i
!= state_count
)
283 /* Initialize optional data, used for the hierarchical topology. */
284 ret
= psci_dt_cpu_init_topology(drv
, data
, state_count
, cpu
);
288 /* Idle states parsed correctly, store them in the per-cpu struct. */
289 data
->psci_states
= psci_states
;
293 static int psci_cpu_init_idle(struct device
*dev
, struct cpuidle_driver
*drv
,
294 unsigned int cpu
, unsigned int state_count
)
296 struct device_node
*cpu_node
;
300 * If the PSCI cpu_suspend function hook has not been initialized
301 * idle states must not be enabled, so bail out
303 if (!psci_ops
.cpu_suspend
)
306 cpu_node
= of_cpu_device_node_get(cpu
);
310 ret
= psci_dt_cpu_init_idle(dev
, drv
, cpu_node
, state_count
, cpu
);
312 of_node_put(cpu_node
);
317 static void psci_cpu_deinit_idle(int cpu
)
319 struct psci_cpuidle_data
*data
= per_cpu_ptr(&psci_cpuidle_data
, cpu
);
321 dt_idle_detach_cpu(data
->dev
);
322 psci_cpuidle_use_syscore
= false;
323 psci_cpuidle_use_cpuhp
= false;
326 static int psci_idle_init_cpu(struct device
*dev
, int cpu
)
328 struct cpuidle_driver
*drv
;
329 struct device_node
*cpu_node
;
330 const char *enable_method
;
333 cpu_node
= of_cpu_device_node_get(cpu
);
338 * Check whether the enable-method for the cpu is PSCI, fail
341 enable_method
= of_get_property(cpu_node
, "enable-method", NULL
);
342 if (!enable_method
|| (strcmp(enable_method
, "psci")))
345 of_node_put(cpu_node
);
349 drv
= devm_kzalloc(dev
, sizeof(*drv
), GFP_KERNEL
);
353 drv
->name
= "psci_idle";
354 drv
->owner
= THIS_MODULE
;
355 drv
->cpumask
= (struct cpumask
*)cpumask_of(cpu
);
358 * PSCI idle states relies on architectural WFI to be represented as
361 drv
->states
[0].enter
= psci_enter_idle_state
;
362 drv
->states
[0].exit_latency
= 1;
363 drv
->states
[0].target_residency
= 1;
364 drv
->states
[0].power_usage
= UINT_MAX
;
365 strcpy(drv
->states
[0].name
, "WFI");
366 strcpy(drv
->states
[0].desc
, "ARM WFI");
369 * If no DT idle states are detected (ret == 0) let the driver
370 * initialization fail accordingly since there is no reason to
371 * initialize the idle driver if only wfi is supported, the
372 * default archictectural back-end already executes wfi
375 ret
= dt_init_idle_driver(drv
, psci_idle_state_match
, 1);
377 return ret
? : -ENODEV
;
380 * Initialize PSCI idle states.
382 ret
= psci_cpu_init_idle(dev
, drv
, cpu
, ret
);
384 pr_err("CPU %d failed to PSCI idle\n", cpu
);
388 ret
= cpuidle_register(drv
, NULL
);
392 cpuidle_cooling_register(drv
);
396 psci_cpu_deinit_idle(cpu
);
401 * psci_idle_probe - Initializes PSCI cpuidle driver
403 * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
404 * to register cpuidle driver then rollback to cancel all CPUs
407 static int psci_cpuidle_probe(struct platform_device
*pdev
)
410 struct cpuidle_driver
*drv
;
411 struct cpuidle_device
*dev
;
413 for_each_possible_cpu(cpu
) {
414 ret
= psci_idle_init_cpu(&pdev
->dev
, cpu
);
419 psci_idle_init_syscore();
420 psci_idle_init_cpuhp();
425 dev
= per_cpu(cpuidle_devices
, cpu
);
426 drv
= cpuidle_get_cpu_driver(dev
);
427 cpuidle_unregister(drv
);
428 psci_cpu_deinit_idle(cpu
);
434 static struct platform_driver psci_cpuidle_driver
= {
435 .probe
= psci_cpuidle_probe
,
437 .name
= "psci-cpuidle",
441 static int __init
psci_idle_init(void)
443 struct platform_device
*pdev
;
446 ret
= platform_driver_register(&psci_cpuidle_driver
);
450 pdev
= platform_device_register_simple("psci-cpuidle", -1, NULL
, 0);
452 platform_driver_unregister(&psci_cpuidle_driver
);
453 return PTR_ERR(pdev
);
458 device_initcall(psci_idle_init
);