blob: d0fb585073c69221d567ebcfa53f41642dbfefcc [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 Hansson8b7ce5e2020-05-11 15:33:46 +020061 ret = cpu_pm_enter();
62 if (ret)
63 return -1;
64
Ulf Hanssonce85aef2019-10-10 12:01:48 +020065 /* Do runtime PM to manage a hierarchical CPU toplogy. */
66 pm_runtime_put_sync_suspend(pd_dev);
67
68 state = psci_get_domain_state();
Ulf Hanssona0cf3192019-10-10 12:01:48 +020069 if (!state)
70 state = states[idx];
71
Ulf Hansson8b7ce5e2020-05-11 15:33:46 +020072 ret = psci_cpu_suspend_enter(state) ? -1 : idx;
Ulf Hanssona0cf3192019-10-10 12:01:48 +020073
Ulf Hanssonce85aef2019-10-10 12:01:48 +020074 pm_runtime_get_sync(pd_dev);
75
Ulf Hansson8b7ce5e2020-05-11 15:33:46 +020076 cpu_pm_exit();
77
Ulf Hanssona0cf3192019-10-10 12:01:48 +020078 /* Clear the domain state to start fresh when back from idle. */
79 psci_set_domain_state(0);
80 return ret;
81}
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +010082
Ulf Hansson9c6ceec2019-10-10 12:01:48 +020083static int psci_idle_cpuhp_up(unsigned int cpu)
84{
85 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
86
87 if (pd_dev)
88 pm_runtime_get_sync(pd_dev);
89
90 return 0;
91}
92
93static int psci_idle_cpuhp_down(unsigned int cpu)
94{
95 struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
96
97 if (pd_dev) {
98 pm_runtime_put_sync(pd_dev);
99 /* Clear domain state to start fresh at next online. */
100 psci_set_domain_state(0);
101 }
102
103 return 0;
104}
105
106static void __init psci_idle_init_cpuhp(void)
107{
108 int err;
109
110 if (!psci_cpuidle_use_cpuhp)
111 return;
112
113 err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
114 "cpuidle/psci:online",
115 psci_idle_cpuhp_up,
116 psci_idle_cpuhp_down);
117 if (err)
118 pr_warn("Failed %d while setup cpuhp state\n", err);
119}
120
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100121static int psci_enter_idle_state(struct cpuidle_device *dev,
122 struct cpuidle_driver *drv, int idx)
123{
Ulf Hansson85549512019-10-10 12:01:48 +0200124 u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100125
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200126 return psci_enter_state(idx, state[idx]);
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100127}
128
129static struct cpuidle_driver psci_idle_driver __initdata = {
130 .name = "psci_idle",
131 .owner = THIS_MODULE,
132 /*
133 * PSCI idle states relies on architectural WFI to
134 * be represented as state index 0.
135 */
136 .states[0] = {
137 .enter = psci_enter_idle_state,
138 .exit_latency = 1,
139 .target_residency = 1,
140 .power_usage = UINT_MAX,
141 .name = "WFI",
142 .desc = "ARM WFI",
143 }
144};
145
146static const struct of_device_id psci_idle_state_match[] __initconst = {
147 { .compatible = "arm,idle-state",
148 .data = psci_enter_idle_state },
149 { },
150};
151
Ulf Hanssona65a3972019-10-10 12:01:48 +0200152int __init psci_dt_parse_state_node(struct device_node *np, u32 *state)
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100153{
154 int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
155
156 if (err) {
157 pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
158 return err;
159 }
160
161 if (!psci_power_state_is_valid(*state)) {
162 pr_warn("Invalid PSCI power state %#x\n", *state);
163 return -EINVAL;
164 }
165
166 return 0;
167}
168
Ulf Hansson7fbee482020-03-10 11:40:39 +0100169static int __init psci_dt_cpu_init_topology(struct cpuidle_driver *drv,
170 struct psci_cpuidle_data *data,
171 unsigned int state_count, int cpu)
172{
173 /* Currently limit the hierarchical topology to be used in OSI mode. */
174 if (!psci_has_osi_support())
175 return 0;
176
177 data->dev = psci_dt_attach_cpu(cpu);
178 if (IS_ERR_OR_NULL(data->dev))
179 return PTR_ERR_OR_ZERO(data->dev);
180
181 /*
182 * Using the deepest state for the CPU to trigger a potential selection
183 * of a shared state for the domain, assumes the domain states are all
184 * deeper states.
185 */
186 drv->states[state_count - 1].enter = psci_enter_domain_idle_state;
187 psci_cpuidle_use_cpuhp = true;
188
189 return 0;
190}
191
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200192static int __init psci_dt_cpu_init_idle(struct cpuidle_driver *drv,
193 struct device_node *cpu_node,
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200194 unsigned int state_count, int cpu)
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100195{
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200196 int i, ret = 0;
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100197 u32 *psci_states;
198 struct device_node *state_node;
Ulf Hansson85549512019-10-10 12:01:48 +0200199 struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100200
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200201 state_count++; /* Add WFI state too */
202 psci_states = kcalloc(state_count, sizeof(*psci_states), GFP_KERNEL);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100203 if (!psci_states)
204 return -ENOMEM;
205
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200206 for (i = 1; i < state_count; i++) {
Ulf Hanssonf08cfbf2019-10-10 12:01:48 +0200207 state_node = of_get_cpu_state_node(cpu_node, i - 1);
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200208 if (!state_node)
209 break;
210
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100211 ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
212 of_node_put(state_node);
213
214 if (ret)
215 goto free_mem;
216
217 pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
218 }
219
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200220 if (i != state_count) {
221 ret = -ENODEV;
222 goto free_mem;
223 }
224
Ulf Hansson7fbee482020-03-10 11:40:39 +0100225 /* Initialize optional data, used for the hierarchical topology. */
226 ret = psci_dt_cpu_init_topology(drv, data, state_count, cpu);
227 if (ret < 0)
228 goto free_mem;
Ulf Hansson85549512019-10-10 12:01:48 +0200229
230 /* Idle states parsed correctly, store them in the per-cpu struct. */
231 data->psci_states = psci_states;
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100232 return 0;
233
234free_mem:
235 kfree(psci_states);
236 return ret;
237}
238
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200239static __init int psci_cpu_init_idle(struct cpuidle_driver *drv,
240 unsigned int cpu, unsigned int state_count)
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100241{
242 struct device_node *cpu_node;
243 int ret;
244
245 /*
246 * If the PSCI cpu_suspend function hook has not been initialized
247 * idle states must not be enabled, so bail out
248 */
249 if (!psci_ops.cpu_suspend)
250 return -EOPNOTSUPP;
251
252 cpu_node = of_cpu_device_node_get(cpu);
253 if (!cpu_node)
254 return -ENODEV;
255
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200256 ret = psci_dt_cpu_init_idle(drv, cpu_node, state_count, cpu);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100257
258 of_node_put(cpu_node);
259
260 return ret;
261}
262
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100263static int __init psci_idle_init_cpu(int cpu)
264{
265 struct cpuidle_driver *drv;
266 struct device_node *cpu_node;
267 const char *enable_method;
268 int ret = 0;
269
270 cpu_node = of_cpu_device_node_get(cpu);
271 if (!cpu_node)
272 return -ENODEV;
273
274 /*
275 * Check whether the enable-method for the cpu is PSCI, fail
276 * if it is not.
277 */
278 enable_method = of_get_property(cpu_node, "enable-method", NULL);
279 if (!enable_method || (strcmp(enable_method, "psci")))
280 ret = -ENODEV;
281
282 of_node_put(cpu_node);
283 if (ret)
284 return ret;
285
286 drv = kmemdup(&psci_idle_driver, sizeof(*drv), GFP_KERNEL);
287 if (!drv)
288 return -ENOMEM;
289
290 drv->cpumask = (struct cpumask *)cpumask_of(cpu);
291
292 /*
293 * Initialize idle states data, starting at index 1, since
294 * by default idle state 0 is the quiescent state reached
295 * by the cpu by executing the wfi instruction.
296 *
297 * If no DT idle states are detected (ret == 0) let the driver
298 * initialization fail accordingly since there is no reason to
299 * initialize the idle driver if only wfi is supported, the
300 * default archictectural back-end already executes wfi
301 * on idle entry.
302 */
303 ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
304 if (ret <= 0) {
305 ret = ret ? : -ENODEV;
306 goto out_kfree_drv;
307 }
308
309 /*
310 * Initialize PSCI idle states.
311 */
Ulf Hanssona0cf3192019-10-10 12:01:48 +0200312 ret = psci_cpu_init_idle(drv, cpu, ret);
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100313 if (ret) {
314 pr_err("CPU %d failed to PSCI idle\n", cpu);
315 goto out_kfree_drv;
316 }
317
318 ret = cpuidle_register(drv, NULL);
319 if (ret)
320 goto out_kfree_drv;
321
322 return 0;
323
324out_kfree_drv:
325 kfree(drv);
326 return ret;
327}
328
329/*
330 * psci_idle_init - Initializes PSCI cpuidle driver
331 *
332 * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
333 * to register cpuidle driver then rollback to cancel all CPUs
334 * registration.
335 */
336static int __init psci_idle_init(void)
337{
338 int cpu, ret;
339 struct cpuidle_driver *drv;
340 struct cpuidle_device *dev;
341
342 for_each_possible_cpu(cpu) {
343 ret = psci_idle_init_cpu(cpu);
344 if (ret)
345 goto out_fail;
346 }
347
Ulf Hansson9c6ceec2019-10-10 12:01:48 +0200348 psci_idle_init_cpuhp();
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100349 return 0;
350
351out_fail:
352 while (--cpu >= 0) {
353 dev = per_cpu(cpuidle_devices, cpu);
354 drv = cpuidle_get_cpu_driver(dev);
355 cpuidle_unregister(drv);
356 kfree(drv);
357 }
358
359 return ret;
360}
361device_initcall(psci_idle_init);