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/cpuidle.h>
12 #include <linux/cpumask.h>
13 #include <linux/cpu_pm.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/psci.h>
19 #include <linux/slab.h>
21 #include <asm/cpuidle.h>
23 #include "cpuidle-psci.h"
24 #include "dt_idle_states.h"
26 struct psci_cpuidle_data {
31 static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
32 static DEFINE_PER_CPU(u32, domain_state);
34 static inline void psci_set_domain_state(u32 state)
36 __this_cpu_write(domain_state, state);
39 static inline u32 psci_get_domain_state(void)
41 return __this_cpu_read(domain_state);
44 static inline int psci_enter_state(int idx, u32 state)
46 return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, idx, state);
49 static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
50 struct cpuidle_driver *drv, int idx)
52 struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
53 u32 *states = data->psci_states;
54 u32 state = psci_get_domain_state();
60 ret = psci_enter_state(idx, state);
62 /* Clear the domain state to start fresh when back from idle. */
63 psci_set_domain_state(0);
67 static int psci_enter_idle_state(struct cpuidle_device *dev,
68 struct cpuidle_driver *drv, int idx)
70 u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
72 return psci_enter_state(idx, state[idx]);
75 static struct cpuidle_driver psci_idle_driver __initdata = {
79 * PSCI idle states relies on architectural WFI to
80 * be represented as state index 0.
83 .enter = psci_enter_idle_state,
85 .target_residency = 1,
86 .power_usage = UINT_MAX,
92 static const struct of_device_id psci_idle_state_match[] __initconst = {
93 { .compatible = "arm,idle-state",
94 .data = psci_enter_idle_state },
98 static int __init psci_dt_parse_state_node(struct device_node *np, u32 *state)
100 int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
103 pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
107 if (!psci_power_state_is_valid(*state)) {
108 pr_warn("Invalid PSCI power state %#x\n", *state);
115 static int __init psci_dt_cpu_init_idle(struct cpuidle_driver *drv,
116 struct device_node *cpu_node,
117 unsigned int state_count, int cpu)
121 struct device_node *state_node;
122 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
124 state_count++; /* Add WFI state too */
125 psci_states = kcalloc(state_count, sizeof(*psci_states), GFP_KERNEL);
129 for (i = 1; i < state_count; i++) {
130 state_node = of_get_cpu_state_node(cpu_node, i - 1);
134 ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
135 of_node_put(state_node);
140 pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
143 if (i != state_count) {
148 /* Currently limit the hierarchical topology to be used in OSI mode. */
149 if (psci_has_osi_support()) {
150 data->dev = psci_dt_attach_cpu(cpu);
151 if (IS_ERR(data->dev)) {
152 ret = PTR_ERR(data->dev);
157 * Using the deepest state for the CPU to trigger a potential
158 * selection of a shared state for the domain, assumes the
159 * domain states are all deeper states.
162 drv->states[state_count - 1].enter =
163 psci_enter_domain_idle_state;
166 /* Idle states parsed correctly, store them in the per-cpu struct. */
167 data->psci_states = psci_states;
175 static __init int psci_cpu_init_idle(struct cpuidle_driver *drv,
176 unsigned int cpu, unsigned int state_count)
178 struct device_node *cpu_node;
182 * If the PSCI cpu_suspend function hook has not been initialized
183 * idle states must not be enabled, so bail out
185 if (!psci_ops.cpu_suspend)
188 cpu_node = of_cpu_device_node_get(cpu);
192 ret = psci_dt_cpu_init_idle(drv, cpu_node, state_count, cpu);
194 of_node_put(cpu_node);
199 static int __init psci_idle_init_cpu(int cpu)
201 struct cpuidle_driver *drv;
202 struct device_node *cpu_node;
203 const char *enable_method;
206 cpu_node = of_cpu_device_node_get(cpu);
211 * Check whether the enable-method for the cpu is PSCI, fail
214 enable_method = of_get_property(cpu_node, "enable-method", NULL);
215 if (!enable_method || (strcmp(enable_method, "psci")))
218 of_node_put(cpu_node);
222 drv = kmemdup(&psci_idle_driver, sizeof(*drv), GFP_KERNEL);
226 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
229 * Initialize idle states data, starting at index 1, since
230 * by default idle state 0 is the quiescent state reached
231 * by the cpu by executing the wfi instruction.
233 * If no DT idle states are detected (ret == 0) let the driver
234 * initialization fail accordingly since there is no reason to
235 * initialize the idle driver if only wfi is supported, the
236 * default archictectural back-end already executes wfi
239 ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
241 ret = ret ? : -ENODEV;
246 * Initialize PSCI idle states.
248 ret = psci_cpu_init_idle(drv, cpu, ret);
250 pr_err("CPU %d failed to PSCI idle\n", cpu);
254 ret = cpuidle_register(drv, NULL);
266 * psci_idle_init - Initializes PSCI cpuidle driver
268 * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
269 * to register cpuidle driver then rollback to cancel all CPUs
272 static int __init psci_idle_init(void)
275 struct cpuidle_driver *drv;
276 struct cpuidle_device *dev;
278 for_each_possible_cpu(cpu) {
279 ret = psci_idle_init_cpu(cpu);
288 dev = per_cpu(cpuidle_devices, cpu);
289 drv = cpuidle_get_cpu_driver(dev);
290 cpuidle_unregister(drv);
296 device_initcall(psci_idle_init);