1 // SPDX-License-Identifier: GPL-2.0-only
3 * PSCI CPU idle driver.
5 * Copyright (C) 2019 ARM Ltd.
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/psci.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/slab.h>
24 #include <asm/cpuidle.h>
26 #include "cpuidle-psci.h"
27 #include "dt_idle_states.h"
29 struct psci_cpuidle_data {
34 static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
35 static DEFINE_PER_CPU(u32, domain_state);
36 static bool psci_cpuidle_use_cpuhp __initdata;
38 void psci_set_domain_state(u32 state)
40 __this_cpu_write(domain_state, state);
43 static inline u32 psci_get_domain_state(void)
45 return __this_cpu_read(domain_state);
48 static inline int psci_enter_state(int idx, u32 state)
50 return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, idx, state);
53 static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
54 struct cpuidle_driver *drv, int idx)
56 struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
57 u32 *states = data->psci_states;
58 struct device *pd_dev = data->dev;
66 /* Do runtime PM to manage a hierarchical CPU toplogy. */
67 pm_runtime_put_sync_suspend(pd_dev);
69 state = psci_get_domain_state();
73 ret = psci_cpu_suspend_enter(state) ? -1 : idx;
75 pm_runtime_get_sync(pd_dev);
79 /* Clear the domain state to start fresh when back from idle. */
80 psci_set_domain_state(0);
84 static int psci_idle_cpuhp_up(unsigned int cpu)
86 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
89 pm_runtime_get_sync(pd_dev);
94 static int psci_idle_cpuhp_down(unsigned int cpu)
96 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
99 pm_runtime_put_sync(pd_dev);
100 /* Clear domain state to start fresh at next online. */
101 psci_set_domain_state(0);
107 static void __init psci_idle_init_cpuhp(void)
111 if (!psci_cpuidle_use_cpuhp)
114 err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
115 "cpuidle/psci:online",
117 psci_idle_cpuhp_down);
119 pr_warn("Failed %d while setup cpuhp state\n", err);
122 static int psci_enter_idle_state(struct cpuidle_device *dev,
123 struct cpuidle_driver *drv, int idx)
125 u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
127 return psci_enter_state(idx, state[idx]);
130 static struct cpuidle_driver psci_idle_driver __initdata = {
132 .owner = THIS_MODULE,
134 * PSCI idle states relies on architectural WFI to
135 * be represented as state index 0.
138 .enter = psci_enter_idle_state,
140 .target_residency = 1,
141 .power_usage = UINT_MAX,
147 static const struct of_device_id psci_idle_state_match[] __initconst = {
148 { .compatible = "arm,idle-state",
149 .data = psci_enter_idle_state },
153 int __init psci_dt_parse_state_node(struct device_node *np, u32 *state)
155 int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
158 pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
162 if (!psci_power_state_is_valid(*state)) {
163 pr_warn("Invalid PSCI power state %#x\n", *state);
170 static int __init psci_dt_cpu_init_topology(struct cpuidle_driver *drv,
171 struct psci_cpuidle_data *data,
172 unsigned int state_count, int cpu)
174 /* Currently limit the hierarchical topology to be used in OSI mode. */
175 if (!psci_has_osi_support())
178 data->dev = psci_dt_attach_cpu(cpu);
179 if (IS_ERR_OR_NULL(data->dev))
180 return PTR_ERR_OR_ZERO(data->dev);
183 * Using the deepest state for the CPU to trigger a potential selection
184 * of a shared state for the domain, assumes the domain states are all
187 drv->states[state_count - 1].enter = psci_enter_domain_idle_state;
188 psci_cpuidle_use_cpuhp = true;
193 static int __init psci_dt_cpu_init_idle(struct cpuidle_driver *drv,
194 struct device_node *cpu_node,
195 unsigned int state_count, int cpu)
199 struct device_node *state_node;
200 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
202 state_count++; /* Add WFI state too */
203 psci_states = kcalloc(state_count, sizeof(*psci_states), GFP_KERNEL);
207 for (i = 1; i < state_count; i++) {
208 state_node = of_get_cpu_state_node(cpu_node, i - 1);
212 ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
213 of_node_put(state_node);
218 pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
221 if (i != state_count) {
226 /* Initialize optional data, used for the hierarchical topology. */
227 ret = psci_dt_cpu_init_topology(drv, data, state_count, cpu);
231 /* Idle states parsed correctly, store them in the per-cpu struct. */
232 data->psci_states = psci_states;
240 static __init int psci_cpu_init_idle(struct cpuidle_driver *drv,
241 unsigned int cpu, unsigned int state_count)
243 struct device_node *cpu_node;
247 * If the PSCI cpu_suspend function hook has not been initialized
248 * idle states must not be enabled, so bail out
250 if (!psci_ops.cpu_suspend)
253 cpu_node = of_cpu_device_node_get(cpu);
257 ret = psci_dt_cpu_init_idle(drv, cpu_node, state_count, cpu);
259 of_node_put(cpu_node);
264 static int __init psci_idle_init_cpu(int cpu)
266 struct cpuidle_driver *drv;
267 struct device_node *cpu_node;
268 const char *enable_method;
271 cpu_node = of_cpu_device_node_get(cpu);
276 * Check whether the enable-method for the cpu is PSCI, fail
279 enable_method = of_get_property(cpu_node, "enable-method", NULL);
280 if (!enable_method || (strcmp(enable_method, "psci")))
283 of_node_put(cpu_node);
287 drv = kmemdup(&psci_idle_driver, sizeof(*drv), GFP_KERNEL);
291 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
294 * Initialize idle states data, starting at index 1, since
295 * by default idle state 0 is the quiescent state reached
296 * by the cpu by executing the wfi instruction.
298 * If no DT idle states are detected (ret == 0) let the driver
299 * initialization fail accordingly since there is no reason to
300 * initialize the idle driver if only wfi is supported, the
301 * default archictectural back-end already executes wfi
304 ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
306 ret = ret ? : -ENODEV;
311 * Initialize PSCI idle states.
313 ret = psci_cpu_init_idle(drv, cpu, ret);
315 pr_err("CPU %d failed to PSCI idle\n", cpu);
319 ret = cpuidle_register(drv, NULL);
323 cpuidle_cooling_register(drv);
333 * psci_idle_init - Initializes PSCI cpuidle driver
335 * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
336 * to register cpuidle driver then rollback to cancel all CPUs
339 static int __init psci_idle_init(void)
342 struct cpuidle_driver *drv;
343 struct cpuidle_device *dev;
345 for_each_possible_cpu(cpu) {
346 ret = psci_idle_init_cpu(cpu);
351 psci_idle_init_cpuhp();
356 dev = per_cpu(cpuidle_devices, cpu);
357 drv = cpuidle_get_cpu_driver(dev);
358 cpuidle_unregister(drv);
364 device_initcall(psci_idle_init);