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/init.h>
20 #include <linux/smp.h>
21 #include <linux/reboot.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <uapi/linux/psci.h>
27 #include <asm/compiler.h>
28 #include <asm/cputype.h>
29 #include <asm/cpu_ops.h>
30 #include <asm/errno.h>
32 #include <asm/smp_plat.h>
33 #include <asm/suspend.h>
34 #include <asm/system_misc.h>
36 #define PSCI_POWER_STATE_TYPE_STANDBY 0
37 #define PSCI_POWER_STATE_TYPE_POWER_DOWN 1
39 static bool psci_power_state_loses_context(u32 state
)
41 return state
& PSCI_0_2_POWER_STATE_TYPE_MASK
;
44 static bool psci_power_state_is_valid(u32 state
)
46 const u32 valid_mask
= PSCI_0_2_POWER_STATE_ID_MASK
|
47 PSCI_0_2_POWER_STATE_TYPE_MASK
|
48 PSCI_0_2_POWER_STATE_AFFL_MASK
;
50 return !(state
& ~valid_mask
);
54 * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
55 * calls to its resident CPU, so we must avoid issuing those. We never migrate
56 * a Trusted OS even if it claims to be capable of migration -- doing so will
57 * require cooperation with a Trusted OS driver.
59 static int resident_cpu
= -1;
61 struct psci_operations
{
62 int (*cpu_suspend
)(u32 state
, unsigned long entry_point
);
63 int (*cpu_off
)(u32 state
);
64 int (*cpu_on
)(unsigned long cpuid
, unsigned long entry_point
);
65 int (*migrate
)(unsigned long cpuid
);
66 int (*affinity_info
)(unsigned long target_affinity
,
67 unsigned long lowest_affinity_level
);
68 int (*migrate_info_type
)(void);
71 static struct psci_operations psci_ops
;
73 typedef unsigned long (psci_fn
)(unsigned long, unsigned long,
74 unsigned long, unsigned long);
75 asmlinkage psci_fn __invoke_psci_fn_hvc
;
76 asmlinkage psci_fn __invoke_psci_fn_smc
;
77 static psci_fn
*invoke_psci_fn
;
87 static DEFINE_PER_CPU_READ_MOSTLY(u32
*, psci_power_state
);
89 static u32 psci_function_id
[PSCI_FN_MAX
];
91 static int psci_to_linux_errno(int errno
)
94 case PSCI_RET_SUCCESS
:
96 case PSCI_RET_NOT_SUPPORTED
:
98 case PSCI_RET_INVALID_PARAMS
:
100 case PSCI_RET_DENIED
:
107 static u32
psci_get_version(void)
109 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION
, 0, 0, 0);
112 static int psci_cpu_suspend(u32 state
, unsigned long entry_point
)
117 fn
= psci_function_id
[PSCI_FN_CPU_SUSPEND
];
118 err
= invoke_psci_fn(fn
, state
, entry_point
, 0);
119 return psci_to_linux_errno(err
);
122 static int psci_cpu_off(u32 state
)
127 fn
= psci_function_id
[PSCI_FN_CPU_OFF
];
128 err
= invoke_psci_fn(fn
, state
, 0, 0);
129 return psci_to_linux_errno(err
);
132 static int psci_cpu_on(unsigned long cpuid
, unsigned long entry_point
)
137 fn
= psci_function_id
[PSCI_FN_CPU_ON
];
138 err
= invoke_psci_fn(fn
, cpuid
, entry_point
, 0);
139 return psci_to_linux_errno(err
);
142 static int psci_migrate(unsigned long cpuid
)
147 fn
= psci_function_id
[PSCI_FN_MIGRATE
];
148 err
= invoke_psci_fn(fn
, cpuid
, 0, 0);
149 return psci_to_linux_errno(err
);
152 static int psci_affinity_info(unsigned long target_affinity
,
153 unsigned long lowest_affinity_level
)
155 return invoke_psci_fn(PSCI_0_2_FN64_AFFINITY_INFO
, target_affinity
,
156 lowest_affinity_level
, 0);
159 static int psci_migrate_info_type(void)
161 return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE
, 0, 0, 0);
164 static unsigned long psci_migrate_info_up_cpu(void)
166 return invoke_psci_fn(PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU
, 0, 0, 0);
169 static int __maybe_unused
cpu_psci_cpu_init_idle(unsigned int cpu
)
171 int i
, ret
, count
= 0;
173 struct device_node
*state_node
, *cpu_node
;
175 cpu_node
= of_get_cpu_node(cpu
, NULL
);
180 * If the PSCI cpu_suspend function hook has not been initialized
181 * idle states must not be enabled, so bail out
183 if (!psci_ops
.cpu_suspend
)
186 /* Count idle states */
187 while ((state_node
= of_parse_phandle(cpu_node
, "cpu-idle-states",
190 of_node_put(state_node
);
196 psci_states
= kcalloc(count
, sizeof(*psci_states
), GFP_KERNEL
);
200 for (i
= 0; i
< count
; i
++) {
203 state_node
= of_parse_phandle(cpu_node
, "cpu-idle-states", i
);
205 ret
= of_property_read_u32(state_node
,
206 "arm,psci-suspend-param",
209 pr_warn(" * %s missing arm,psci-suspend-param property\n",
210 state_node
->full_name
);
211 of_node_put(state_node
);
215 of_node_put(state_node
);
216 pr_debug("psci-power-state %#x index %d\n", state
, i
);
217 if (!psci_power_state_is_valid(state
)) {
218 pr_warn("Invalid PSCI power state %#x\n", state
);
222 psci_states
[i
] = state
;
224 /* Idle states parsed correctly, initialize per-cpu pointer */
225 per_cpu(psci_power_state
, cpu
) = psci_states
;
233 static int get_set_conduit_method(struct device_node
*np
)
237 pr_info("probing for conduit method from DT.\n");
239 if (of_property_read_string(np
, "method", &method
)) {
240 pr_warn("missing \"method\" property\n");
244 if (!strcmp("hvc", method
)) {
245 invoke_psci_fn
= __invoke_psci_fn_hvc
;
246 } else if (!strcmp("smc", method
)) {
247 invoke_psci_fn
= __invoke_psci_fn_smc
;
249 pr_warn("invalid \"method\" property: %s\n", method
);
255 static void psci_sys_reset(enum reboot_mode reboot_mode
, const char *cmd
)
257 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET
, 0, 0, 0);
260 static void psci_sys_poweroff(void)
262 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF
, 0, 0, 0);
266 * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
267 * return DENIED (which would be fatal).
269 static void __init
psci_init_migrate(void)
274 type
= psci_ops
.migrate_info_type();
276 if (type
== PSCI_0_2_TOS_MP
) {
277 pr_info("Trusted OS migration not required\n");
281 if (type
== PSCI_RET_NOT_SUPPORTED
) {
282 pr_info("MIGRATE_INFO_TYPE not supported.\n");
286 if (type
!= PSCI_0_2_TOS_UP_MIGRATE
&&
287 type
!= PSCI_0_2_TOS_UP_NO_MIGRATE
) {
288 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type
);
292 cpuid
= psci_migrate_info_up_cpu();
293 if (cpuid
& ~MPIDR_HWID_BITMASK
) {
294 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
299 cpu
= get_logical_index(cpuid
);
300 resident_cpu
= cpu
>= 0 ? cpu
: -1;
302 pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid
);
305 static void __init
psci_0_2_set_functions(void)
307 pr_info("Using standard PSCI v0.2 function IDs\n");
308 psci_function_id
[PSCI_FN_CPU_SUSPEND
] = PSCI_0_2_FN64_CPU_SUSPEND
;
309 psci_ops
.cpu_suspend
= psci_cpu_suspend
;
311 psci_function_id
[PSCI_FN_CPU_OFF
] = PSCI_0_2_FN_CPU_OFF
;
312 psci_ops
.cpu_off
= psci_cpu_off
;
314 psci_function_id
[PSCI_FN_CPU_ON
] = PSCI_0_2_FN64_CPU_ON
;
315 psci_ops
.cpu_on
= psci_cpu_on
;
317 psci_function_id
[PSCI_FN_MIGRATE
] = PSCI_0_2_FN64_MIGRATE
;
318 psci_ops
.migrate
= psci_migrate
;
320 psci_ops
.affinity_info
= psci_affinity_info
;
322 psci_ops
.migrate_info_type
= psci_migrate_info_type
;
324 arm_pm_restart
= psci_sys_reset
;
326 pm_power_off
= psci_sys_poweroff
;
330 * Probe function for PSCI firmware versions >= 0.2
332 static int __init
psci_probe(void)
334 u32 ver
= psci_get_version();
336 pr_info("PSCIv%d.%d detected in firmware.\n",
337 PSCI_VERSION_MAJOR(ver
),
338 PSCI_VERSION_MINOR(ver
));
340 if (PSCI_VERSION_MAJOR(ver
) == 0 && PSCI_VERSION_MINOR(ver
) < 2) {
341 pr_err("Conflicting PSCI version detected.\n");
345 psci_0_2_set_functions();
352 typedef int (*psci_initcall_t
)(const struct device_node
*);
355 * PSCI init function for PSCI versions >=0.2
357 * Probe based on PSCI PSCI_VERSION function
359 static int __init
psci_0_2_init(struct device_node
*np
)
363 err
= get_set_conduit_method(np
);
368 * Starting with v0.2, the PSCI specification introduced a call
369 * (PSCI_VERSION) that allows probing the firmware version, so
370 * that PSCI function IDs and version specific initialization
371 * can be carried out according to the specific version reported
382 * PSCI < v0.2 get PSCI Function IDs via DT.
384 static int __init
psci_0_1_init(struct device_node
*np
)
389 err
= get_set_conduit_method(np
);
394 pr_info("Using PSCI v0.1 Function IDs from DT\n");
396 if (!of_property_read_u32(np
, "cpu_suspend", &id
)) {
397 psci_function_id
[PSCI_FN_CPU_SUSPEND
] = id
;
398 psci_ops
.cpu_suspend
= psci_cpu_suspend
;
401 if (!of_property_read_u32(np
, "cpu_off", &id
)) {
402 psci_function_id
[PSCI_FN_CPU_OFF
] = id
;
403 psci_ops
.cpu_off
= psci_cpu_off
;
406 if (!of_property_read_u32(np
, "cpu_on", &id
)) {
407 psci_function_id
[PSCI_FN_CPU_ON
] = id
;
408 psci_ops
.cpu_on
= psci_cpu_on
;
411 if (!of_property_read_u32(np
, "migrate", &id
)) {
412 psci_function_id
[PSCI_FN_MIGRATE
] = id
;
413 psci_ops
.migrate
= psci_migrate
;
421 static const struct of_device_id psci_of_match
[] __initconst
= {
422 { .compatible
= "arm,psci", .data
= psci_0_1_init
},
423 { .compatible
= "arm,psci-0.2", .data
= psci_0_2_init
},
427 int __init
psci_dt_init(void)
429 struct device_node
*np
;
430 const struct of_device_id
*matched_np
;
431 psci_initcall_t init_fn
;
433 np
= of_find_matching_node_and_match(NULL
, psci_of_match
, &matched_np
);
438 init_fn
= (psci_initcall_t
)matched_np
->data
;
444 * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
445 * explicitly clarified in SBBR
447 int __init
psci_acpi_init(void)
449 if (!acpi_psci_present()) {
450 pr_info("is not implemented in ACPI.\n");
454 pr_info("probing for conduit method from ACPI.\n");
456 if (acpi_psci_use_hvc())
457 invoke_psci_fn
= __invoke_psci_fn_hvc
;
459 invoke_psci_fn
= __invoke_psci_fn_smc
;
467 static int __init
cpu_psci_cpu_init(unsigned int cpu
)
472 static int __init
cpu_psci_cpu_prepare(unsigned int cpu
)
474 if (!psci_ops
.cpu_on
) {
475 pr_err("no cpu_on method, not booting CPU%d\n", cpu
);
482 static int cpu_psci_cpu_boot(unsigned int cpu
)
484 int err
= psci_ops
.cpu_on(cpu_logical_map(cpu
), __pa(secondary_entry
));
486 pr_err("failed to boot CPU%d (%d)\n", cpu
, err
);
491 #ifdef CONFIG_HOTPLUG_CPU
492 static bool psci_tos_resident_on(int cpu
)
494 return cpu
== resident_cpu
;
497 static int cpu_psci_cpu_disable(unsigned int cpu
)
499 /* Fail early if we don't have CPU_OFF support */
500 if (!psci_ops
.cpu_off
)
503 /* Trusted OS will deny CPU_OFF */
504 if (psci_tos_resident_on(cpu
))
510 static void cpu_psci_cpu_die(unsigned int cpu
)
514 * There are no known implementations of PSCI actually using the
515 * power state field, pass a sensible default for now.
517 u32 state
= PSCI_POWER_STATE_TYPE_POWER_DOWN
<<
518 PSCI_0_2_POWER_STATE_TYPE_SHIFT
;
520 ret
= psci_ops
.cpu_off(state
);
522 pr_crit("unable to power off CPU%u (%d)\n", cpu
, ret
);
525 static int cpu_psci_cpu_kill(unsigned int cpu
)
529 if (!psci_ops
.affinity_info
)
532 * cpu_kill could race with cpu_die and we can
533 * potentially end up declaring this cpu undead
534 * while it is dying. So, try again a few times.
537 for (i
= 0; i
< 10; i
++) {
538 err
= psci_ops
.affinity_info(cpu_logical_map(cpu
), 0);
539 if (err
== PSCI_0_2_AFFINITY_LEVEL_OFF
) {
540 pr_info("CPU%d killed.\n", cpu
);
545 pr_info("Retrying again to check for CPU kill\n");
548 pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
555 static int psci_suspend_finisher(unsigned long index
)
557 u32
*state
= __this_cpu_read(psci_power_state
);
559 return psci_ops
.cpu_suspend(state
[index
- 1],
560 virt_to_phys(cpu_resume
));
563 static int __maybe_unused
cpu_psci_cpu_suspend(unsigned long index
)
566 u32
*state
= __this_cpu_read(psci_power_state
);
568 * idle state index 0 corresponds to wfi, should never be called
569 * from the cpu_suspend operations
571 if (WARN_ON_ONCE(!index
))
574 if (!psci_power_state_loses_context(state
[index
- 1]))
575 ret
= psci_ops
.cpu_suspend(state
[index
- 1], 0);
577 ret
= cpu_suspend(index
, psci_suspend_finisher
);
582 const struct cpu_operations cpu_psci_ops
= {
584 #ifdef CONFIG_CPU_IDLE
585 .cpu_init_idle
= cpu_psci_cpu_init_idle
,
586 .cpu_suspend
= cpu_psci_cpu_suspend
,
589 .cpu_init
= cpu_psci_cpu_init
,
590 .cpu_prepare
= cpu_psci_cpu_prepare
,
591 .cpu_boot
= cpu_psci_cpu_boot
,
592 #ifdef CONFIG_HOTPLUG_CPU
593 .cpu_disable
= cpu_psci_cpu_disable
,
594 .cpu_die
= cpu_psci_cpu_die
,
595 .cpu_kill
= cpu_psci_cpu_kill
,