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) 2015 ARM Limited
14 #define pr_fmt(fmt) "psci: " fmt
16 #include <linux/errno.h>
17 #include <linux/linkage.h>
20 #include <linux/printk.h>
21 #include <linux/psci.h>
22 #include <linux/reboot.h>
24 #include <uapi/linux/psci.h>
26 #include <asm/cputype.h>
27 #include <asm/system_misc.h>
28 #include <asm/smp_plat.h>
31 * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
32 * calls to its resident CPU, so we must avoid issuing those. We never migrate
33 * a Trusted OS even if it claims to be capable of migration -- doing so will
34 * require cooperation with a Trusted OS driver.
36 static int resident_cpu = -1;
38 bool psci_tos_resident_on(int cpu)
40 return cpu == resident_cpu;
43 struct psci_operations psci_ops;
45 typedef unsigned long (psci_fn)(unsigned long, unsigned long,
46 unsigned long, unsigned long);
47 asmlinkage psci_fn __invoke_psci_fn_hvc;
48 asmlinkage psci_fn __invoke_psci_fn_smc;
49 static psci_fn *invoke_psci_fn;
59 static u32 psci_function_id[PSCI_FN_MAX];
61 static int psci_to_linux_errno(int errno)
64 case PSCI_RET_SUCCESS:
66 case PSCI_RET_NOT_SUPPORTED:
68 case PSCI_RET_INVALID_PARAMS:
77 static u32 psci_get_version(void)
79 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
82 static int psci_cpu_suspend(u32 state, unsigned long entry_point)
87 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
88 err = invoke_psci_fn(fn, state, entry_point, 0);
89 return psci_to_linux_errno(err);
92 static int psci_cpu_off(u32 state)
97 fn = psci_function_id[PSCI_FN_CPU_OFF];
98 err = invoke_psci_fn(fn, state, 0, 0);
99 return psci_to_linux_errno(err);
102 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
107 fn = psci_function_id[PSCI_FN_CPU_ON];
108 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
109 return psci_to_linux_errno(err);
112 static int psci_migrate(unsigned long cpuid)
117 fn = psci_function_id[PSCI_FN_MIGRATE];
118 err = invoke_psci_fn(fn, cpuid, 0, 0);
119 return psci_to_linux_errno(err);
122 static int psci_affinity_info(unsigned long target_affinity,
123 unsigned long lowest_affinity_level)
125 return invoke_psci_fn(PSCI_0_2_FN64_AFFINITY_INFO, target_affinity,
126 lowest_affinity_level, 0);
129 static int psci_migrate_info_type(void)
131 return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
134 static unsigned long psci_migrate_info_up_cpu(void)
136 return invoke_psci_fn(PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU, 0, 0, 0);
139 static int get_set_conduit_method(struct device_node *np)
143 pr_info("probing for conduit method from DT.\n");
145 if (of_property_read_string(np, "method", &method)) {
146 pr_warn("missing \"method\" property\n");
150 if (!strcmp("hvc", method)) {
151 invoke_psci_fn = __invoke_psci_fn_hvc;
152 } else if (!strcmp("smc", method)) {
153 invoke_psci_fn = __invoke_psci_fn_smc;
155 pr_warn("invalid \"method\" property: %s\n", method);
161 static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
163 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
166 static void psci_sys_poweroff(void)
168 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
172 * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
173 * return DENIED (which would be fatal).
175 static void __init psci_init_migrate(void)
180 type = psci_ops.migrate_info_type();
182 if (type == PSCI_0_2_TOS_MP) {
183 pr_info("Trusted OS migration not required\n");
187 if (type == PSCI_RET_NOT_SUPPORTED) {
188 pr_info("MIGRATE_INFO_TYPE not supported.\n");
192 if (type != PSCI_0_2_TOS_UP_MIGRATE &&
193 type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
194 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
198 cpuid = psci_migrate_info_up_cpu();
199 if (cpuid & ~MPIDR_HWID_BITMASK) {
200 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
205 cpu = get_logical_index(cpuid);
206 resident_cpu = cpu >= 0 ? cpu : -1;
208 pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
211 static void __init psci_0_2_set_functions(void)
213 pr_info("Using standard PSCI v0.2 function IDs\n");
214 psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN64_CPU_SUSPEND;
215 psci_ops.cpu_suspend = psci_cpu_suspend;
217 psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
218 psci_ops.cpu_off = psci_cpu_off;
220 psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN64_CPU_ON;
221 psci_ops.cpu_on = psci_cpu_on;
223 psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN64_MIGRATE;
224 psci_ops.migrate = psci_migrate;
226 psci_ops.affinity_info = psci_affinity_info;
228 psci_ops.migrate_info_type = psci_migrate_info_type;
230 arm_pm_restart = psci_sys_reset;
232 pm_power_off = psci_sys_poweroff;
236 * Probe function for PSCI firmware versions >= 0.2
238 static int __init psci_probe(void)
240 u32 ver = psci_get_version();
242 pr_info("PSCIv%d.%d detected in firmware.\n",
243 PSCI_VERSION_MAJOR(ver),
244 PSCI_VERSION_MINOR(ver));
246 if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
247 pr_err("Conflicting PSCI version detected.\n");
251 psci_0_2_set_functions();
258 typedef int (*psci_initcall_t)(const struct device_node *);
261 * PSCI init function for PSCI versions >=0.2
263 * Probe based on PSCI PSCI_VERSION function
265 static int __init psci_0_2_init(struct device_node *np)
269 err = get_set_conduit_method(np);
274 * Starting with v0.2, the PSCI specification introduced a call
275 * (PSCI_VERSION) that allows probing the firmware version, so
276 * that PSCI function IDs and version specific initialization
277 * can be carried out according to the specific version reported
288 * PSCI < v0.2 get PSCI Function IDs via DT.
290 static int __init psci_0_1_init(struct device_node *np)
295 err = get_set_conduit_method(np);
300 pr_info("Using PSCI v0.1 Function IDs from DT\n");
302 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
303 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
304 psci_ops.cpu_suspend = psci_cpu_suspend;
307 if (!of_property_read_u32(np, "cpu_off", &id)) {
308 psci_function_id[PSCI_FN_CPU_OFF] = id;
309 psci_ops.cpu_off = psci_cpu_off;
312 if (!of_property_read_u32(np, "cpu_on", &id)) {
313 psci_function_id[PSCI_FN_CPU_ON] = id;
314 psci_ops.cpu_on = psci_cpu_on;
317 if (!of_property_read_u32(np, "migrate", &id)) {
318 psci_function_id[PSCI_FN_MIGRATE] = id;
319 psci_ops.migrate = psci_migrate;
327 static const struct of_device_id psci_of_match[] __initconst = {
328 { .compatible = "arm,psci", .data = psci_0_1_init},
329 { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
333 int __init psci_dt_init(void)
335 struct device_node *np;
336 const struct of_device_id *matched_np;
337 psci_initcall_t init_fn;
339 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
344 init_fn = (psci_initcall_t)matched_np->data;
350 * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
351 * explicitly clarified in SBBR
353 int __init psci_acpi_init(void)
355 if (!acpi_psci_present()) {
356 pr_info("is not implemented in ACPI.\n");
360 pr_info("probing for conduit method from ACPI.\n");
362 if (acpi_psci_use_hvc())
363 invoke_psci_fn = __invoke_psci_fn_hvc;
365 invoke_psci_fn = __invoke_psci_fn_smc;