diff options
Diffstat (limited to 'drivers/firmware')
-rw-r--r-- | drivers/firmware/psci.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/drivers/firmware/psci.c b/drivers/firmware/psci.c index f25cd79c8a79..11bfee8b79a9 100644 --- a/drivers/firmware/psci.c +++ b/drivers/firmware/psci.c | |||
@@ -14,6 +14,7 @@ | |||
14 | #define pr_fmt(fmt) "psci: " fmt | 14 | #define pr_fmt(fmt) "psci: " fmt |
15 | 15 | ||
16 | #include <linux/arm-smccc.h> | 16 | #include <linux/arm-smccc.h> |
17 | #include <linux/cpuidle.h> | ||
17 | #include <linux/errno.h> | 18 | #include <linux/errno.h> |
18 | #include <linux/linkage.h> | 19 | #include <linux/linkage.h> |
19 | #include <linux/of.h> | 20 | #include <linux/of.h> |
@@ -21,10 +22,12 @@ | |||
21 | #include <linux/printk.h> | 22 | #include <linux/printk.h> |
22 | #include <linux/psci.h> | 23 | #include <linux/psci.h> |
23 | #include <linux/reboot.h> | 24 | #include <linux/reboot.h> |
25 | #include <linux/slab.h> | ||
24 | #include <linux/suspend.h> | 26 | #include <linux/suspend.h> |
25 | 27 | ||
26 | #include <uapi/linux/psci.h> | 28 | #include <uapi/linux/psci.h> |
27 | 29 | ||
30 | #include <asm/cpuidle.h> | ||
28 | #include <asm/cputype.h> | 31 | #include <asm/cputype.h> |
29 | #include <asm/system_misc.h> | 32 | #include <asm/system_misc.h> |
30 | #include <asm/smp_plat.h> | 33 | #include <asm/smp_plat.h> |
@@ -244,6 +247,123 @@ static int __init psci_features(u32 psci_func_id) | |||
244 | psci_func_id, 0, 0); | 247 | psci_func_id, 0, 0); |
245 | } | 248 | } |
246 | 249 | ||
250 | #ifdef CONFIG_CPU_IDLE | ||
251 | static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state); | ||
252 | |||
253 | static int psci_dt_cpu_init_idle(struct device_node *cpu_node, int cpu) | ||
254 | { | ||
255 | int i, ret, count = 0; | ||
256 | u32 *psci_states; | ||
257 | struct device_node *state_node; | ||
258 | |||
259 | /* | ||
260 | * If the PSCI cpu_suspend function hook has not been initialized | ||
261 | * idle states must not be enabled, so bail out | ||
262 | */ | ||
263 | if (!psci_ops.cpu_suspend) | ||
264 | return -EOPNOTSUPP; | ||
265 | |||
266 | /* Count idle states */ | ||
267 | while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states", | ||
268 | count))) { | ||
269 | count++; | ||
270 | of_node_put(state_node); | ||
271 | } | ||
272 | |||
273 | if (!count) | ||
274 | return -ENODEV; | ||
275 | |||
276 | psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL); | ||
277 | if (!psci_states) | ||
278 | return -ENOMEM; | ||
279 | |||
280 | for (i = 0; i < count; i++) { | ||
281 | u32 state; | ||
282 | |||
283 | state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i); | ||
284 | |||
285 | ret = of_property_read_u32(state_node, | ||
286 | "arm,psci-suspend-param", | ||
287 | &state); | ||
288 | if (ret) { | ||
289 | pr_warn(" * %s missing arm,psci-suspend-param property\n", | ||
290 | state_node->full_name); | ||
291 | of_node_put(state_node); | ||
292 | goto free_mem; | ||
293 | } | ||
294 | |||
295 | of_node_put(state_node); | ||
296 | pr_debug("psci-power-state %#x index %d\n", state, i); | ||
297 | if (!psci_power_state_is_valid(state)) { | ||
298 | pr_warn("Invalid PSCI power state %#x\n", state); | ||
299 | ret = -EINVAL; | ||
300 | goto free_mem; | ||
301 | } | ||
302 | psci_states[i] = state; | ||
303 | } | ||
304 | /* Idle states parsed correctly, initialize per-cpu pointer */ | ||
305 | per_cpu(psci_power_state, cpu) = psci_states; | ||
306 | return 0; | ||
307 | |||
308 | free_mem: | ||
309 | kfree(psci_states); | ||
310 | return ret; | ||
311 | } | ||
312 | |||
313 | int psci_cpu_init_idle(unsigned int cpu) | ||
314 | { | ||
315 | struct device_node *cpu_node; | ||
316 | int ret; | ||
317 | |||
318 | cpu_node = of_get_cpu_node(cpu, NULL); | ||
319 | if (!cpu_node) | ||
320 | return -ENODEV; | ||
321 | |||
322 | ret = psci_dt_cpu_init_idle(cpu_node, cpu); | ||
323 | |||
324 | of_node_put(cpu_node); | ||
325 | |||
326 | return ret; | ||
327 | } | ||
328 | |||
329 | static int psci_suspend_finisher(unsigned long index) | ||
330 | { | ||
331 | u32 *state = __this_cpu_read(psci_power_state); | ||
332 | |||
333 | return psci_ops.cpu_suspend(state[index - 1], | ||
334 | virt_to_phys(cpu_resume)); | ||
335 | } | ||
336 | |||
337 | int psci_cpu_suspend_enter(unsigned long index) | ||
338 | { | ||
339 | int ret; | ||
340 | u32 *state = __this_cpu_read(psci_power_state); | ||
341 | /* | ||
342 | * idle state index 0 corresponds to wfi, should never be called | ||
343 | * from the cpu_suspend operations | ||
344 | */ | ||
345 | if (WARN_ON_ONCE(!index)) | ||
346 | return -EINVAL; | ||
347 | |||
348 | if (!psci_power_state_loses_context(state[index - 1])) | ||
349 | ret = psci_ops.cpu_suspend(state[index - 1], 0); | ||
350 | else | ||
351 | ret = cpu_suspend(index, psci_suspend_finisher); | ||
352 | |||
353 | return ret; | ||
354 | } | ||
355 | |||
356 | /* ARM specific CPU idle operations */ | ||
357 | #ifdef CONFIG_ARM | ||
358 | static struct cpuidle_ops psci_cpuidle_ops __initdata = { | ||
359 | .suspend = psci_cpu_suspend_enter, | ||
360 | .init = psci_dt_cpu_init_idle, | ||
361 | }; | ||
362 | |||
363 | CPUIDLE_METHOD_OF_DECLARE(psci, "arm,psci", &psci_cpuidle_ops); | ||
364 | #endif | ||
365 | #endif | ||
366 | |||
247 | static int psci_system_suspend(unsigned long unused) | 367 | static int psci_system_suspend(unsigned long unused) |
248 | { | 368 | { |
249 | return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND), | 369 | return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND), |