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/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>
27 #include <asm/cpuidle.h>
29 #include "cpuidle-psci.h"
30 #include "dt_idle_states.h"
32 struct psci_cpuidle_data
{
37 static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data
, psci_cpuidle_data
);
38 static DEFINE_PER_CPU(u32
, domain_state
);
39 static bool psci_cpuidle_use_cpuhp
;
41 void psci_set_domain_state(u32 state
)
43 __this_cpu_write(domain_state
, state
);
46 static inline u32
psci_get_domain_state(void)
48 return __this_cpu_read(domain_state
);
51 static inline int psci_enter_state(int idx
, u32 state
)
53 return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter
, idx
, state
);
56 static int __psci_enter_domain_idle_state(struct cpuidle_device
*dev
,
57 struct cpuidle_driver
*drv
, int idx
,
60 struct psci_cpuidle_data
*data
= this_cpu_ptr(&psci_cpuidle_data
);
61 u32
*states
= data
->psci_states
;
62 struct device
*pd_dev
= data
->dev
;
70 /* Do runtime PM to manage a hierarchical CPU toplogy. */
71 rcu_irq_enter_irqson();
73 dev_pm_genpd_suspend(pd_dev
);
75 pm_runtime_put_sync_suspend(pd_dev
);
76 rcu_irq_exit_irqson();
78 state
= psci_get_domain_state();
82 ret
= psci_cpu_suspend_enter(state
) ? -1 : idx
;
84 rcu_irq_enter_irqson();
86 dev_pm_genpd_resume(pd_dev
);
88 pm_runtime_get_sync(pd_dev
);
89 rcu_irq_exit_irqson();
93 /* Clear the domain state to start fresh when back from idle. */
94 psci_set_domain_state(0);
98 static int psci_enter_domain_idle_state(struct cpuidle_device
*dev
,
99 struct cpuidle_driver
*drv
, int idx
)
101 return __psci_enter_domain_idle_state(dev
, drv
, idx
, false);
104 static int psci_enter_s2idle_domain_idle_state(struct cpuidle_device
*dev
,
105 struct cpuidle_driver
*drv
,
108 return __psci_enter_domain_idle_state(dev
, drv
, idx
, true);
111 static int psci_idle_cpuhp_up(unsigned int cpu
)
113 struct device
*pd_dev
= __this_cpu_read(psci_cpuidle_data
.dev
);
116 pm_runtime_get_sync(pd_dev
);
121 static int psci_idle_cpuhp_down(unsigned int cpu
)
123 struct device
*pd_dev
= __this_cpu_read(psci_cpuidle_data
.dev
);
126 pm_runtime_put_sync(pd_dev
);
127 /* Clear domain state to start fresh at next online. */
128 psci_set_domain_state(0);
134 static void psci_idle_init_cpuhp(void)
138 if (!psci_cpuidle_use_cpuhp
)
141 err
= cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING
,
142 "cpuidle/psci:online",
144 psci_idle_cpuhp_down
);
146 pr_warn("Failed %d while setup cpuhp state\n", err
);
149 static int psci_enter_idle_state(struct cpuidle_device
*dev
,
150 struct cpuidle_driver
*drv
, int idx
)
152 u32
*state
= __this_cpu_read(psci_cpuidle_data
.psci_states
);
154 return psci_enter_state(idx
, state
[idx
]);
157 static const struct of_device_id psci_idle_state_match
[] = {
158 { .compatible
= "arm,idle-state",
159 .data
= psci_enter_idle_state
},
163 int psci_dt_parse_state_node(struct device_node
*np
, u32
*state
)
165 int err
= of_property_read_u32(np
, "arm,psci-suspend-param", state
);
168 pr_warn("%pOF missing arm,psci-suspend-param property\n", np
);
172 if (!psci_power_state_is_valid(*state
)) {
173 pr_warn("Invalid PSCI power state %#x\n", *state
);
180 static int psci_dt_cpu_init_topology(struct cpuidle_driver
*drv
,
181 struct psci_cpuidle_data
*data
,
182 unsigned int state_count
, int cpu
)
184 /* Currently limit the hierarchical topology to be used in OSI mode. */
185 if (!psci_has_osi_support())
188 data
->dev
= psci_dt_attach_cpu(cpu
);
189 if (IS_ERR_OR_NULL(data
->dev
))
190 return PTR_ERR_OR_ZERO(data
->dev
);
193 * Using the deepest state for the CPU to trigger a potential selection
194 * of a shared state for the domain, assumes the domain states are all
197 drv
->states
[state_count
- 1].enter
= psci_enter_domain_idle_state
;
198 drv
->states
[state_count
- 1].enter_s2idle
= psci_enter_s2idle_domain_idle_state
;
199 psci_cpuidle_use_cpuhp
= true;
204 static int psci_dt_cpu_init_idle(struct device
*dev
, struct cpuidle_driver
*drv
,
205 struct device_node
*cpu_node
,
206 unsigned int state_count
, int cpu
)
210 struct device_node
*state_node
;
211 struct psci_cpuidle_data
*data
= per_cpu_ptr(&psci_cpuidle_data
, cpu
);
213 state_count
++; /* Add WFI state too */
214 psci_states
= devm_kcalloc(dev
, state_count
, sizeof(*psci_states
),
219 for (i
= 1; i
< state_count
; i
++) {
220 state_node
= of_get_cpu_state_node(cpu_node
, i
- 1);
224 ret
= psci_dt_parse_state_node(state_node
, &psci_states
[i
]);
225 of_node_put(state_node
);
230 pr_debug("psci-power-state %#x index %d\n", psci_states
[i
], i
);
233 if (i
!= state_count
)
236 /* Initialize optional data, used for the hierarchical topology. */
237 ret
= psci_dt_cpu_init_topology(drv
, data
, state_count
, cpu
);
241 /* Idle states parsed correctly, store them in the per-cpu struct. */
242 data
->psci_states
= psci_states
;
246 static int psci_cpu_init_idle(struct device
*dev
, struct cpuidle_driver
*drv
,
247 unsigned int cpu
, unsigned int state_count
)
249 struct device_node
*cpu_node
;
253 * If the PSCI cpu_suspend function hook has not been initialized
254 * idle states must not be enabled, so bail out
256 if (!psci_ops
.cpu_suspend
)
259 cpu_node
= of_cpu_device_node_get(cpu
);
263 ret
= psci_dt_cpu_init_idle(dev
, drv
, cpu_node
, state_count
, cpu
);
265 of_node_put(cpu_node
);
270 static void psci_cpu_deinit_idle(int cpu
)
272 struct psci_cpuidle_data
*data
= per_cpu_ptr(&psci_cpuidle_data
, cpu
);
274 psci_dt_detach_cpu(data
->dev
);
275 psci_cpuidle_use_cpuhp
= false;
278 static int psci_idle_init_cpu(struct device
*dev
, int cpu
)
280 struct cpuidle_driver
*drv
;
281 struct device_node
*cpu_node
;
282 const char *enable_method
;
285 cpu_node
= of_cpu_device_node_get(cpu
);
290 * Check whether the enable-method for the cpu is PSCI, fail
293 enable_method
= of_get_property(cpu_node
, "enable-method", NULL
);
294 if (!enable_method
|| (strcmp(enable_method
, "psci")))
297 of_node_put(cpu_node
);
301 drv
= devm_kzalloc(dev
, sizeof(*drv
), GFP_KERNEL
);
305 drv
->name
= "psci_idle";
306 drv
->owner
= THIS_MODULE
;
307 drv
->cpumask
= (struct cpumask
*)cpumask_of(cpu
);
310 * PSCI idle states relies on architectural WFI to be represented as
313 drv
->states
[0].enter
= psci_enter_idle_state
;
314 drv
->states
[0].exit_latency
= 1;
315 drv
->states
[0].target_residency
= 1;
316 drv
->states
[0].power_usage
= UINT_MAX
;
317 strcpy(drv
->states
[0].name
, "WFI");
318 strcpy(drv
->states
[0].desc
, "ARM WFI");
321 * If no DT idle states are detected (ret == 0) let the driver
322 * initialization fail accordingly since there is no reason to
323 * initialize the idle driver if only wfi is supported, the
324 * default archictectural back-end already executes wfi
327 ret
= dt_init_idle_driver(drv
, psci_idle_state_match
, 1);
329 return ret
? : -ENODEV
;
332 * Initialize PSCI idle states.
334 ret
= psci_cpu_init_idle(dev
, drv
, cpu
, ret
);
336 pr_err("CPU %d failed to PSCI idle\n", cpu
);
340 ret
= cpuidle_register(drv
, NULL
);
344 cpuidle_cooling_register(drv
);
348 psci_cpu_deinit_idle(cpu
);
353 * psci_idle_probe - Initializes PSCI cpuidle driver
355 * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
356 * to register cpuidle driver then rollback to cancel all CPUs
359 static int psci_cpuidle_probe(struct platform_device
*pdev
)
362 struct cpuidle_driver
*drv
;
363 struct cpuidle_device
*dev
;
365 for_each_possible_cpu(cpu
) {
366 ret
= psci_idle_init_cpu(&pdev
->dev
, cpu
);
371 psci_idle_init_cpuhp();
376 dev
= per_cpu(cpuidle_devices
, cpu
);
377 drv
= cpuidle_get_cpu_driver(dev
);
378 cpuidle_unregister(drv
);
379 psci_cpu_deinit_idle(cpu
);
385 static struct platform_driver psci_cpuidle_driver
= {
386 .probe
= psci_cpuidle_probe
,
388 .name
= "psci-cpuidle",
392 static int __init
psci_idle_init(void)
394 struct platform_device
*pdev
;
397 ret
= platform_driver_register(&psci_cpuidle_driver
);
401 pdev
= platform_device_register_simple("psci-cpuidle", -1, NULL
, 0);
403 platform_driver_unregister(&psci_cpuidle_driver
);
404 return PTR_ERR(pdev
);
409 device_initcall(psci_idle_init
);