2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * Copyright (C) 2013 ARM Limited
13 * Author: Will Deacon <will.deacon@arm.com>
16 #define pr_fmt(fmt) "psci: " fmt
18 #include <linux/cpuidle.h>
19 #include <linux/init.h>
21 #include <linux/smp.h>
22 #include <linux/slab.h>
23 #include <linux/reboot.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <uapi/linux/psci.h>
29 #include <asm/compiler.h>
30 #include <asm/cpu_ops.h>
31 #include <asm/errno.h>
33 #include <asm/smp_plat.h>
34 #include <asm/suspend.h>
35 #include <asm/system_misc.h>
37 #define PSCI_POWER_STATE_TYPE_STANDBY 0
38 #define PSCI_POWER_STATE_TYPE_POWER_DOWN 1
40 struct psci_power_state {
46 struct psci_operations {
47 int (*cpu_suspend)(struct psci_power_state state,
48 unsigned long entry_point);
49 int (*cpu_off)(struct psci_power_state state);
50 int (*cpu_on)(unsigned long cpuid, unsigned long entry_point);
51 int (*migrate)(unsigned long cpuid);
52 int (*affinity_info)(unsigned long target_affinity,
53 unsigned long lowest_affinity_level);
54 int (*migrate_info_type)(void);
57 static struct psci_operations psci_ops;
59 static int (*invoke_psci_fn)(u64, u64, u64, u64);
60 typedef int (*psci_initcall_t)(const struct device_node *);
67 PSCI_FN_AFFINITY_INFO,
68 PSCI_FN_MIGRATE_INFO_TYPE,
72 static DEFINE_PER_CPU_READ_MOSTLY(struct psci_power_state *, psci_power_state);
74 static u32 psci_function_id[PSCI_FN_MAX];
76 static int psci_to_linux_errno(int errno)
79 case PSCI_RET_SUCCESS:
81 case PSCI_RET_NOT_SUPPORTED:
83 case PSCI_RET_INVALID_PARAMS:
92 static u32 psci_power_state_pack(struct psci_power_state state)
94 return ((state.id << PSCI_0_2_POWER_STATE_ID_SHIFT)
95 & PSCI_0_2_POWER_STATE_ID_MASK) |
96 ((state.type << PSCI_0_2_POWER_STATE_TYPE_SHIFT)
97 & PSCI_0_2_POWER_STATE_TYPE_MASK) |
98 ((state.affinity_level << PSCI_0_2_POWER_STATE_AFFL_SHIFT)
99 & PSCI_0_2_POWER_STATE_AFFL_MASK);
102 static void psci_power_state_unpack(u32 power_state,
103 struct psci_power_state *state)
105 state->id = (power_state & PSCI_0_2_POWER_STATE_ID_MASK) >>
106 PSCI_0_2_POWER_STATE_ID_SHIFT;
107 state->type = (power_state & PSCI_0_2_POWER_STATE_TYPE_MASK) >>
108 PSCI_0_2_POWER_STATE_TYPE_SHIFT;
109 state->affinity_level =
110 (power_state & PSCI_0_2_POWER_STATE_AFFL_MASK) >>
111 PSCI_0_2_POWER_STATE_AFFL_SHIFT;
115 * The following two functions are invoked via the invoke_psci_fn pointer
116 * and will not be inlined, allowing us to piggyback on the AAPCS.
118 static noinline int __invoke_psci_fn_hvc(u64 function_id, u64 arg0, u64 arg1,
128 : "r" (arg0), "r" (arg1), "r" (arg2));
133 static noinline int __invoke_psci_fn_smc(u64 function_id, u64 arg0, u64 arg1,
143 : "r" (arg0), "r" (arg1), "r" (arg2));
148 static int psci_get_version(void)
152 err = invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
156 static int psci_cpu_suspend(struct psci_power_state state,
157 unsigned long entry_point)
162 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
163 power_state = psci_power_state_pack(state);
164 err = invoke_psci_fn(fn, power_state, entry_point, 0);
165 return psci_to_linux_errno(err);
168 static int psci_cpu_off(struct psci_power_state state)
173 fn = psci_function_id[PSCI_FN_CPU_OFF];
174 power_state = psci_power_state_pack(state);
175 err = invoke_psci_fn(fn, power_state, 0, 0);
176 return psci_to_linux_errno(err);
179 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
184 fn = psci_function_id[PSCI_FN_CPU_ON];
185 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
186 return psci_to_linux_errno(err);
189 static int psci_migrate(unsigned long cpuid)
194 fn = psci_function_id[PSCI_FN_MIGRATE];
195 err = invoke_psci_fn(fn, cpuid, 0, 0);
196 return psci_to_linux_errno(err);
199 static int psci_affinity_info(unsigned long target_affinity,
200 unsigned long lowest_affinity_level)
205 fn = psci_function_id[PSCI_FN_AFFINITY_INFO];
206 err = invoke_psci_fn(fn, target_affinity, lowest_affinity_level, 0);
210 static int psci_migrate_info_type(void)
215 fn = psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE];
216 err = invoke_psci_fn(fn, 0, 0, 0);
220 static int __maybe_unused cpu_psci_cpu_init_idle(struct device_node *cpu_node,
223 int i, ret, count = 0;
224 struct psci_power_state *psci_states;
225 struct device_node *state_node;
228 * If the PSCI cpu_suspend function hook has not been initialized
229 * idle states must not be enabled, so bail out
231 if (!psci_ops.cpu_suspend)
234 /* Count idle states */
235 while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
238 of_node_put(state_node);
244 psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
248 for (i = 0; i < count; i++) {
249 u32 psci_power_state;
251 state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
253 ret = of_property_read_u32(state_node,
254 "arm,psci-suspend-param",
257 pr_warn(" * %s missing arm,psci-suspend-param property\n",
258 state_node->full_name);
259 of_node_put(state_node);
263 of_node_put(state_node);
264 pr_debug("psci-power-state %#x index %d\n", psci_power_state,
266 psci_power_state_unpack(psci_power_state, &psci_states[i]);
268 /* Idle states parsed correctly, initialize per-cpu pointer */
269 per_cpu(psci_power_state, cpu) = psci_states;
277 static int get_set_conduit_method(struct device_node *np)
281 pr_info("probing for conduit method from DT.\n");
283 if (of_property_read_string(np, "method", &method)) {
284 pr_warn("missing \"method\" property\n");
288 if (!strcmp("hvc", method)) {
289 invoke_psci_fn = __invoke_psci_fn_hvc;
290 } else if (!strcmp("smc", method)) {
291 invoke_psci_fn = __invoke_psci_fn_smc;
293 pr_warn("invalid \"method\" property: %s\n", method);
299 static void psci_sys_reset(char str, const char *cmd)
301 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
304 static void psci_sys_poweroff(void)
306 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
310 * PSCI Function IDs for v0.2+ are well defined so use
313 static int __init psci_0_2_init(struct device_node *np)
317 err = get_set_conduit_method(np);
322 ver = psci_get_version();
324 if (ver == PSCI_RET_NOT_SUPPORTED) {
325 /* PSCI v0.2 mandates implementation of PSCI_ID_VERSION. */
326 pr_err("PSCI firmware does not comply with the v0.2 spec.\n");
330 pr_info("PSCIv%d.%d detected in firmware.\n",
331 PSCI_VERSION_MAJOR(ver),
332 PSCI_VERSION_MINOR(ver));
334 if (PSCI_VERSION_MAJOR(ver) == 0 &&
335 PSCI_VERSION_MINOR(ver) < 2) {
337 pr_err("Conflicting PSCI version detected.\n");
342 pr_info("Using standard PSCI v0.2 function IDs\n");
343 psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN64_CPU_SUSPEND;
344 psci_ops.cpu_suspend = psci_cpu_suspend;
346 psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
347 psci_ops.cpu_off = psci_cpu_off;
349 psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN64_CPU_ON;
350 psci_ops.cpu_on = psci_cpu_on;
352 psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN64_MIGRATE;
353 psci_ops.migrate = psci_migrate;
355 psci_function_id[PSCI_FN_AFFINITY_INFO] = PSCI_0_2_FN64_AFFINITY_INFO;
356 psci_ops.affinity_info = psci_affinity_info;
358 psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE] =
359 PSCI_0_2_FN_MIGRATE_INFO_TYPE;
360 psci_ops.migrate_info_type = psci_migrate_info_type;
362 arm_pm_restart = psci_sys_reset;
364 pm_power_off = psci_sys_poweroff;
372 * PSCI < v0.2 get PSCI Function IDs via DT.
374 static int __init psci_0_1_init(struct device_node *np)
379 err = get_set_conduit_method(np);
384 pr_info("Using PSCI v0.1 Function IDs from DT\n");
386 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
387 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
388 psci_ops.cpu_suspend = psci_cpu_suspend;
391 if (!of_property_read_u32(np, "cpu_off", &id)) {
392 psci_function_id[PSCI_FN_CPU_OFF] = id;
393 psci_ops.cpu_off = psci_cpu_off;
396 if (!of_property_read_u32(np, "cpu_on", &id)) {
397 psci_function_id[PSCI_FN_CPU_ON] = id;
398 psci_ops.cpu_on = psci_cpu_on;
401 if (!of_property_read_u32(np, "migrate", &id)) {
402 psci_function_id[PSCI_FN_MIGRATE] = id;
403 psci_ops.migrate = psci_migrate;
411 static const struct of_device_id psci_of_match[] __initconst = {
412 { .compatible = "arm,psci", .data = psci_0_1_init},
413 { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
417 int __init psci_init(void)
419 struct device_node *np;
420 const struct of_device_id *matched_np;
421 psci_initcall_t init_fn;
423 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
428 init_fn = (psci_initcall_t)matched_np->data;
434 static int __init cpu_psci_cpu_init(struct device_node *dn, unsigned int cpu)
439 static int __init cpu_psci_cpu_prepare(unsigned int cpu)
441 if (!psci_ops.cpu_on) {
442 pr_err("no cpu_on method, not booting CPU%d\n", cpu);
449 static int cpu_psci_cpu_boot(unsigned int cpu)
451 int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
453 pr_err("failed to boot CPU%d (%d)\n", cpu, err);
458 #ifdef CONFIG_HOTPLUG_CPU
459 static int cpu_psci_cpu_disable(unsigned int cpu)
461 /* Fail early if we don't have CPU_OFF support */
462 if (!psci_ops.cpu_off)
467 static void cpu_psci_cpu_die(unsigned int cpu)
471 * There are no known implementations of PSCI actually using the
472 * power state field, pass a sensible default for now.
474 struct psci_power_state state = {
475 .type = PSCI_POWER_STATE_TYPE_POWER_DOWN,
478 ret = psci_ops.cpu_off(state);
480 pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
483 static int cpu_psci_cpu_kill(unsigned int cpu)
487 if (!psci_ops.affinity_info)
490 * cpu_kill could race with cpu_die and we can
491 * potentially end up declaring this cpu undead
492 * while it is dying. So, try again a few times.
495 for (i = 0; i < 10; i++) {
496 err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
497 if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
498 pr_info("CPU%d killed.\n", cpu);
503 pr_info("Retrying again to check for CPU kill\n");
506 pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
508 /* Make op_cpu_kill() fail. */
514 #ifdef CONFIG_ARM64_CPU_SUSPEND
515 static int psci_suspend_finisher(unsigned long index)
517 struct psci_power_state *state = __get_cpu_var(psci_power_state);
519 return psci_ops.cpu_suspend(state[index - 1],
520 virt_to_phys(cpu_resume));
523 static int __maybe_unused cpu_psci_cpu_suspend(unsigned long index)
526 struct psci_power_state *state = __get_cpu_var(psci_power_state);
528 * idle state index 0 corresponds to wfi, should never be called
529 * from the cpu_suspend operations
531 if (WARN_ON_ONCE(!index))
536 if (state[index - 1].type == PSCI_POWER_STATE_TYPE_STANDBY)
537 ret = psci_ops.cpu_suspend(state[index - 1], 0);
539 ret = __cpu_suspend(index, psci_suspend_finisher);
545 const struct cpu_operations cpu_psci_ops = {
547 #ifdef CONFIG_CPU_IDLE
548 .cpu_init_idle = cpu_psci_cpu_init_idle,
551 .cpu_init = cpu_psci_cpu_init,
552 .cpu_prepare = cpu_psci_cpu_prepare,
553 .cpu_boot = cpu_psci_cpu_boot,
554 #ifdef CONFIG_HOTPLUG_CPU
555 .cpu_disable = cpu_psci_cpu_disable,
556 .cpu_die = cpu_psci_cpu_die,
557 .cpu_kill = cpu_psci_cpu_kill,
560 #ifdef CONFIG_ARM64_CPU_SUSPEND
561 .cpu_suspend = cpu_psci_cpu_suspend,