blob: edd7a54ef0d30d63439f64605b4d4027718f628b [file] [log] [blame]
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +01001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * PSCI CPU idle driver.
4 *
5 * Copyright (C) 2019 ARM Ltd.
6 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
7 */
8
9#define pr_fmt(fmt) "CPUidle PSCI: " fmt
10
Ulf Hansson9c6ceec2019-10-10 12:01:48 +020011#include <linux/cpuhotplug.h>
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +010012#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>
17#include <linux/of.h>
18#include <linux/of_device.h>
19#include <linux/psci.h>
Ulf Hanssonce85aef2019-10-10 12:01:48 +020020#include <linux/pm_runtime.h>
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +010021#include <linux/slab.h>
22
23#include <asm/cpuidle.h>
24
Ulf Hansson85549512019-10-10 12:01:48 +020025#include "cpuidle-psci.h"
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +010026#include "dt_idle_states.h"
27
Ulf Hansson85549512019-10-10 12:01:48 +020028struct psci_cpuidle_data {
29 u32 *psci_states;
30 struct device *dev;
31};
32
33static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
Ulf Hanssona0cf3192019-10-10 12:01:48 +020034static DEFINE_PER_CPU(u32, domain_state);
Ulf Hansson9c6ceec2019-10-10 12:01:48 +020035static bool psci_cpuidle_use_cpuhp __initdata;
Ulf Hanssona0cf3192019-10-10 12:01:48 +020036
Ulf Hanssona65a3972019-10-10 12:01:48 +020037void psci_set_domain_state(u32 state)
Ulf Hanssona0cf3192019-10-10 12:01:48 +020038{
39 __this_cpu_write(domain_state, state);
40}
41
42static inline u32 psci_get_domain_state(void)
43{
44 return __this_cpu_read(domain_state);
45}
46
47static inline int psci_enter_state(int idx, u32 state)
48{
49 return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, idx, state);
50}
51
52static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
53 struct cpuidle_driver *drv, int idx)
54{
55 struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
56 u32 *states = data->psci_states;
Ulf Hanssonce85aef2019-10-10 12:01:48 +020057 struct device *pd_dev = data->dev;
58 u32 state;
Ulf Hanssona0cf3192019-10-10 12:01:48 +020059 int ret;
60
Ulf Hanssonce85aef2019-10-10 12:01:48 +020061 /* Do runtime PM to manage a hierarchical CPU toplogy. */
62 pm_runtime_put_sync_suspend(pd_dev);
63
64 state = psci_get_domain_state();
Ulf Hanssona0cf3192019-10-10 12:01:48 +020065 if (!state)
66 state = states[idx];
67
68 ret = psci_enter_state(idx, state);
69
Ulf Hanssonce85aef2019-10-10 12:01:48 +020070 pm_runtime_get_sync(pd_dev);
71
Ulf Hanssona0cf3192019-10-10 12:01:48 +020072 /* Clear the domain state to start fresh when back from idle. */
73 psci_set_domain_state(0);
74 return ret;
75}
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +010076
Ulf Hansson9c6ceec2019-10-10 12:01:48 +020077static int psci_idle_cpuhp_up(unsigned int cpu)
78{
79 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
80
81 if (pd_dev)
82 pm_runtime_get_sync(pd_dev);
83
84 return 0;
85}
86
87static int psci_idle_cpuhp_down(unsigned int cpu)
88{
89 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
90
91 if (pd_dev) {
92 pm_runtime_put_sync(pd_dev);
93 /* Clear domain state to start fresh at next online. */
94 psci_set_domain_state(0);
95 }
96
97 return 0;
98}
99
100static void __init psci_idle_init_cpuhp(void)
101{
102 int err;
103
104 if (!psci_cpuidle_use_cpuhp)
105 return;
106
107 err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
108 "cpuidle/psci:online",
109 psci_idle_cpuhp_up,
110 psci_idle_cpuhp_down);
111 if (err)
112 pr_warn("Failed %d while setup cpuhp state\n", err);
113}
114
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100115static int psci_enter_idle_state(struct cpuidle_device *dev,
116 struct cpuidle_driver *drv, int idx)
117{
Ulf Hansson85549512019-10-10 12:01:48 +0200118 u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100119
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200120 return psci_enter_state(idx, state[idx]);
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100121}
122
123static struct cpuidle_driver psci_idle_driver __initdata = {
124 .name = "psci_idle",
125 .owner = THIS_MODULE,
126 /*
127 * PSCI idle states relies on architectural WFI to
128 * be represented as state index 0.
129 */
130 .states[0] = {
131 .enter = psci_enter_idle_state,
132 .exit_latency = 1,
133 .target_residency = 1,
134 .power_usage = UINT_MAX,
135 .name = "WFI",
136 .desc = "ARM WFI",
137 }
138};
139
140static const struct of_device_id psci_idle_state_match[] __initconst = {
141 { .compatible = "arm,idle-state",
142 .data = psci_enter_idle_state },
143 { },
144};
145
Ulf Hanssona65a3972019-10-10 12:01:48 +0200146int __init psci_dt_parse_state_node(struct device_node *np, u32 *state)
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100147{
148 int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
149
150 if (err) {
151 pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
152 return err;
153 }
154
155 if (!psci_power_state_is_valid(*state)) {
156 pr_warn("Invalid PSCI power state %#x\n", *state);
157 return -EINVAL;
158 }
159
160 return 0;
161}
162
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200163static int __init psci_dt_cpu_init_idle(struct cpuidle_driver *drv,
164 struct device_node *cpu_node,
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200165 unsigned int state_count, int cpu)
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100166{
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200167 int i, ret = 0;
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100168 u32 *psci_states;
169 struct device_node *state_node;
Ulf Hansson85549512019-10-10 12:01:48 +0200170 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100171
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200172 state_count++; /* Add WFI state too */
173 psci_states = kcalloc(state_count, sizeof(*psci_states), GFP_KERNEL);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100174 if (!psci_states)
175 return -ENOMEM;
176
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200177 for (i = 1; i < state_count; i++) {
Ulf Hanssonf08cfbf2019-10-10 12:01:48 +0200178 state_node = of_get_cpu_state_node(cpu_node, i - 1);
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200179 if (!state_node)
180 break;
181
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100182 ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
183 of_node_put(state_node);
184
185 if (ret)
186 goto free_mem;
187
188 pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
189 }
190
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200191 if (i != state_count) {
192 ret = -ENODEV;
193 goto free_mem;
194 }
195
Ulf Hansson85549512019-10-10 12:01:48 +0200196 /* 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);
201 goto free_mem;
202 }
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200203
204 /*
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.
208 */
Ulf Hansson9c6ceec2019-10-10 12:01:48 +0200209 if (data->dev) {
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200210 drv->states[state_count - 1].enter =
211 psci_enter_domain_idle_state;
Ulf Hansson9c6ceec2019-10-10 12:01:48 +0200212 psci_cpuidle_use_cpuhp = true;
213 }
Ulf Hansson85549512019-10-10 12:01:48 +0200214 }
215
216 /* Idle states parsed correctly, store them in the per-cpu struct. */
217 data->psci_states = psci_states;
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100218 return 0;
219
220free_mem:
221 kfree(psci_states);
222 return ret;
223}
224
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200225static __init int psci_cpu_init_idle(struct cpuidle_driver *drv,
226 unsigned int cpu, unsigned int state_count)
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100227{
228 struct device_node *cpu_node;
229 int ret;
230
231 /*
232 * If the PSCI cpu_suspend function hook has not been initialized
233 * idle states must not be enabled, so bail out
234 */
235 if (!psci_ops.cpu_suspend)
236 return -EOPNOTSUPP;
237
238 cpu_node = of_cpu_device_node_get(cpu);
239 if (!cpu_node)
240 return -ENODEV;
241
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200242 ret = psci_dt_cpu_init_idle(drv, cpu_node, state_count, cpu);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100243
244 of_node_put(cpu_node);
245
246 return ret;
247}
248
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100249static int __init psci_idle_init_cpu(int cpu)
250{
251 struct cpuidle_driver *drv;
252 struct device_node *cpu_node;
253 const char *enable_method;
254 int ret = 0;
255
256 cpu_node = of_cpu_device_node_get(cpu);
257 if (!cpu_node)
258 return -ENODEV;
259
260 /*
261 * Check whether the enable-method for the cpu is PSCI, fail
262 * if it is not.
263 */
264 enable_method = of_get_property(cpu_node, "enable-method", NULL);
265 if (!enable_method || (strcmp(enable_method, "psci")))
266 ret = -ENODEV;
267
268 of_node_put(cpu_node);
269 if (ret)
270 return ret;
271
272 drv = kmemdup(&psci_idle_driver, sizeof(*drv), GFP_KERNEL);
273 if (!drv)
274 return -ENOMEM;
275
276 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
277
278 /*
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.
282 *
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
287 * on idle entry.
288 */
289 ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
290 if (ret <= 0) {
291 ret = ret ? : -ENODEV;
292 goto out_kfree_drv;
293 }
294
295 /*
296 * Initialize PSCI idle states.
297 */
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200298 ret = psci_cpu_init_idle(drv, cpu, ret);
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100299 if (ret) {
300 pr_err("CPU %d failed to PSCI idle\n", cpu);
301 goto out_kfree_drv;
302 }
303
304 ret = cpuidle_register(drv, NULL);
305 if (ret)
306 goto out_kfree_drv;
307
308 return 0;
309
310out_kfree_drv:
311 kfree(drv);
312 return ret;
313}
314
315/*
316 * psci_idle_init - Initializes PSCI cpuidle driver
317 *
318 * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
319 * to register cpuidle driver then rollback to cancel all CPUs
320 * registration.
321 */
322static int __init psci_idle_init(void)
323{
324 int cpu, ret;
325 struct cpuidle_driver *drv;
326 struct cpuidle_device *dev;
327
328 for_each_possible_cpu(cpu) {
329 ret = psci_idle_init_cpu(cpu);
330 if (ret)
331 goto out_fail;
332 }
333
Ulf Hansson9c6ceec2019-10-10 12:01:48 +0200334 psci_idle_init_cpuhp();
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100335 return 0;
336
337out_fail:
338 while (--cpu >= 0) {
339 dev = per_cpu(cpuidle_devices, cpu);
340 drv = cpuidle_get_cpu_driver(dev);
341 cpuidle_unregister(drv);
342 kfree(drv);
343 }
344
345 return ret;
346}
347device_initcall(psci_idle_init);