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/cpuidle.h>
13 #include <linux/cpumask.h>
14 #include <linux/cpu_pm.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/psci.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
23 #include <asm/cpuidle.h>
25 #include "cpuidle-psci.h"
26 #include "dt_idle_states.h"
28 struct psci_cpuidle_data {
33 static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
34 static DEFINE_PER_CPU(u32, domain_state);
35 static bool psci_cpuidle_use_cpuhp __initdata;
37 void psci_set_domain_state(u32 state)
39 __this_cpu_write(domain_state, state);
42 static inline u32 psci_get_domain_state(void)
44 return __this_cpu_read(domain_state);
47 static inline int psci_enter_state(int idx, u32 state)
49 return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, idx, state);
52 static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
53 struct cpuidle_driver *drv, int idx)
55 struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
56 u32 *states = data->psci_states;
57 struct device *pd_dev = data->dev;
61 /* Do runtime PM to manage a hierarchical CPU toplogy. */
62 pm_runtime_put_sync_suspend(pd_dev);
64 state = psci_get_domain_state();
68 ret = psci_enter_state(idx, state);
70 pm_runtime_get_sync(pd_dev);
72 /* Clear the domain state to start fresh when back from idle. */
73 psci_set_domain_state(0);
77 static int psci_idle_cpuhp_up(unsigned int cpu)
79 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
82 pm_runtime_get_sync(pd_dev);
87 static int psci_idle_cpuhp_down(unsigned int cpu)
89 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
92 pm_runtime_put_sync(pd_dev);
93 /* Clear domain state to start fresh at next online. */
94 psci_set_domain_state(0);
100 static void __init psci_idle_init_cpuhp(void)
104 if (!psci_cpuidle_use_cpuhp)
107 err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
108 "cpuidle/psci:online",
110 psci_idle_cpuhp_down);
112 pr_warn("Failed %d while setup cpuhp state\n", err);
115 static int psci_enter_idle_state(struct cpuidle_device *dev,
116 struct cpuidle_driver *drv, int idx)
118 u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
120 return psci_enter_state(idx, state[idx]);
123 static struct cpuidle_driver psci_idle_driver __initdata = {
125 .owner = THIS_MODULE,
127 * PSCI idle states relies on architectural WFI to
128 * be represented as state index 0.
131 .enter = psci_enter_idle_state,
133 .target_residency = 1,
134 .power_usage = UINT_MAX,
140 static const struct of_device_id psci_idle_state_match[] __initconst = {
141 { .compatible = "arm,idle-state",
142 .data = psci_enter_idle_state },
146 int __init psci_dt_parse_state_node(struct device_node *np, u32 *state)
148 int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
151 pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
155 if (!psci_power_state_is_valid(*state)) {
156 pr_warn("Invalid PSCI power state %#x\n", *state);
163 static int __init psci_dt_cpu_init_idle(struct cpuidle_driver *drv,
164 struct device_node *cpu_node,
165 unsigned int state_count, int cpu)
169 struct device_node *state_node;
170 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
172 state_count++; /* Add WFI state too */
173 psci_states = kcalloc(state_count, sizeof(*psci_states), GFP_KERNEL);
177 for (i = 1; i < state_count; i++) {
178 state_node = of_get_cpu_state_node(cpu_node, i - 1);
182 ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
183 of_node_put(state_node);
188 pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
191 if (i != state_count) {
196 /* Currently limit the hierarchical topology to be used in OSI mode. */
197 if (psci_has_osi_support()) {
198 data->dev = psci_dt_attach_cpu(cpu);
199 if (IS_ERR(data->dev)) {
200 ret = PTR_ERR(data->dev);
205 * Using the deepest state for the CPU to trigger a potential
206 * selection of a shared state for the domain, assumes the
207 * domain states are all deeper states.
210 drv->states[state_count - 1].enter =
211 psci_enter_domain_idle_state;
212 psci_cpuidle_use_cpuhp = true;
216 /* Idle states parsed correctly, store them in the per-cpu struct. */
217 data->psci_states = psci_states;
225 static __init int psci_cpu_init_idle(struct cpuidle_driver *drv,
226 unsigned int cpu, unsigned int state_count)
228 struct device_node *cpu_node;
232 * If the PSCI cpu_suspend function hook has not been initialized
233 * idle states must not be enabled, so bail out
235 if (!psci_ops.cpu_suspend)
238 cpu_node = of_cpu_device_node_get(cpu);
242 ret = psci_dt_cpu_init_idle(drv, cpu_node, state_count, cpu);
244 of_node_put(cpu_node);
249 static int __init psci_idle_init_cpu(int cpu)
251 struct cpuidle_driver *drv;
252 struct device_node *cpu_node;
253 const char *enable_method;
256 cpu_node = of_cpu_device_node_get(cpu);
261 * Check whether the enable-method for the cpu is PSCI, fail
264 enable_method = of_get_property(cpu_node, "enable-method", NULL);
265 if (!enable_method || (strcmp(enable_method, "psci")))
268 of_node_put(cpu_node);
272 drv = kmemdup(&psci_idle_driver, sizeof(*drv), GFP_KERNEL);
276 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
279 * Initialize idle states data, starting at index 1, since
280 * by default idle state 0 is the quiescent state reached
281 * by the cpu by executing the wfi instruction.
283 * If no DT idle states are detected (ret == 0) let the driver
284 * initialization fail accordingly since there is no reason to
285 * initialize the idle driver if only wfi is supported, the
286 * default archictectural back-end already executes wfi
289 ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
291 ret = ret ? : -ENODEV;
296 * Initialize PSCI idle states.
298 ret = psci_cpu_init_idle(drv, cpu, ret);
300 pr_err("CPU %d failed to PSCI idle\n", cpu);
304 ret = cpuidle_register(drv, NULL);
316 * psci_idle_init - Initializes PSCI cpuidle driver
318 * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
319 * to register cpuidle driver then rollback to cancel all CPUs
322 static int __init psci_idle_init(void)
325 struct cpuidle_driver *drv;
326 struct cpuidle_device *dev;
328 for_each_possible_cpu(cpu) {
329 ret = psci_idle_init_cpu(cpu);
334 psci_idle_init_cpuhp();
339 dev = per_cpu(cpuidle_devices, cpu);
340 drv = cpuidle_get_cpu_driver(dev);
341 cpuidle_unregister(drv);
347 device_initcall(psci_idle_init);