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 * While a 64-bit OS can make calls with SMC32 calling conventions, for some
32 * calls it is necessary to use SMC64 to pass or return 64-bit values. For such
33 * calls PSCI_0_2_FN_NATIVE(x) will choose the appropriate (native-width)
37 #define PSCI_0_2_FN_NATIVE(name) PSCI_0_2_FN64_##name
39 #define PSCI_0_2_FN_NATIVE(name) PSCI_0_2_FN_##name
43 * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
44 * calls to its resident CPU, so we must avoid issuing those. We never migrate
45 * a Trusted OS even if it claims to be capable of migration -- doing so will
46 * require cooperation with a Trusted OS driver.
48 static int resident_cpu = -1;
50 bool psci_tos_resident_on(int cpu)
52 return cpu == resident_cpu;
55 struct psci_operations psci_ops;
57 typedef unsigned long (psci_fn)(unsigned long, unsigned long,
58 unsigned long, unsigned long);
59 asmlinkage psci_fn __invoke_psci_fn_hvc;
60 asmlinkage psci_fn __invoke_psci_fn_smc;
61 static psci_fn *invoke_psci_fn;
71 static u32 psci_function_id[PSCI_FN_MAX];
73 static int psci_to_linux_errno(int errno)
76 case PSCI_RET_SUCCESS:
78 case PSCI_RET_NOT_SUPPORTED:
80 case PSCI_RET_INVALID_PARAMS:
89 static u32 psci_get_version(void)
91 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
94 static int psci_cpu_suspend(u32 state, unsigned long entry_point)
99 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
100 err = invoke_psci_fn(fn, state, entry_point, 0);
101 return psci_to_linux_errno(err);
104 static int psci_cpu_off(u32 state)
109 fn = psci_function_id[PSCI_FN_CPU_OFF];
110 err = invoke_psci_fn(fn, state, 0, 0);
111 return psci_to_linux_errno(err);
114 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
119 fn = psci_function_id[PSCI_FN_CPU_ON];
120 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
121 return psci_to_linux_errno(err);
124 static int psci_migrate(unsigned long cpuid)
129 fn = psci_function_id[PSCI_FN_MIGRATE];
130 err = invoke_psci_fn(fn, cpuid, 0, 0);
131 return psci_to_linux_errno(err);
134 static int psci_affinity_info(unsigned long target_affinity,
135 unsigned long lowest_affinity_level)
137 return invoke_psci_fn(PSCI_0_2_FN_NATIVE(AFFINITY_INFO),
138 target_affinity, lowest_affinity_level, 0);
141 static int psci_migrate_info_type(void)
143 return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
146 static unsigned long psci_migrate_info_up_cpu(void)
148 return invoke_psci_fn(PSCI_0_2_FN_NATIVE(MIGRATE_INFO_UP_CPU),
152 static int get_set_conduit_method(struct device_node *np)
156 pr_info("probing for conduit method from DT.\n");
158 if (of_property_read_string(np, "method", &method)) {
159 pr_warn("missing \"method\" property\n");
163 if (!strcmp("hvc", method)) {
164 invoke_psci_fn = __invoke_psci_fn_hvc;
165 } else if (!strcmp("smc", method)) {
166 invoke_psci_fn = __invoke_psci_fn_smc;
168 pr_warn("invalid \"method\" property: %s\n", method);
174 static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
176 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
179 static void psci_sys_poweroff(void)
181 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
185 * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
186 * return DENIED (which would be fatal).
188 static void __init psci_init_migrate(void)
193 type = psci_ops.migrate_info_type();
195 if (type == PSCI_0_2_TOS_MP) {
196 pr_info("Trusted OS migration not required\n");
200 if (type == PSCI_RET_NOT_SUPPORTED) {
201 pr_info("MIGRATE_INFO_TYPE not supported.\n");
205 if (type != PSCI_0_2_TOS_UP_MIGRATE &&
206 type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
207 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
211 cpuid = psci_migrate_info_up_cpu();
212 if (cpuid & ~MPIDR_HWID_BITMASK) {
213 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
218 cpu = get_logical_index(cpuid);
219 resident_cpu = cpu >= 0 ? cpu : -1;
221 pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
224 static void __init psci_0_2_set_functions(void)
226 pr_info("Using standard PSCI v0.2 function IDs\n");
227 psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN_NATIVE(CPU_SUSPEND);
228 psci_ops.cpu_suspend = psci_cpu_suspend;
230 psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
231 psci_ops.cpu_off = psci_cpu_off;
233 psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN_NATIVE(CPU_ON);
234 psci_ops.cpu_on = psci_cpu_on;
236 psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN_NATIVE(MIGRATE);
237 psci_ops.migrate = psci_migrate;
239 psci_ops.affinity_info = psci_affinity_info;
241 psci_ops.migrate_info_type = psci_migrate_info_type;
243 arm_pm_restart = psci_sys_reset;
245 pm_power_off = psci_sys_poweroff;
249 * Probe function for PSCI firmware versions >= 0.2
251 static int __init psci_probe(void)
253 u32 ver = psci_get_version();
255 pr_info("PSCIv%d.%d detected in firmware.\n",
256 PSCI_VERSION_MAJOR(ver),
257 PSCI_VERSION_MINOR(ver));
259 if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
260 pr_err("Conflicting PSCI version detected.\n");
264 psci_0_2_set_functions();
271 typedef int (*psci_initcall_t)(const struct device_node *);
274 * PSCI init function for PSCI versions >=0.2
276 * Probe based on PSCI PSCI_VERSION function
278 static int __init psci_0_2_init(struct device_node *np)
282 err = get_set_conduit_method(np);
287 * Starting with v0.2, the PSCI specification introduced a call
288 * (PSCI_VERSION) that allows probing the firmware version, so
289 * that PSCI function IDs and version specific initialization
290 * can be carried out according to the specific version reported
301 * PSCI < v0.2 get PSCI Function IDs via DT.
303 static int __init psci_0_1_init(struct device_node *np)
308 err = get_set_conduit_method(np);
313 pr_info("Using PSCI v0.1 Function IDs from DT\n");
315 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
316 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
317 psci_ops.cpu_suspend = psci_cpu_suspend;
320 if (!of_property_read_u32(np, "cpu_off", &id)) {
321 psci_function_id[PSCI_FN_CPU_OFF] = id;
322 psci_ops.cpu_off = psci_cpu_off;
325 if (!of_property_read_u32(np, "cpu_on", &id)) {
326 psci_function_id[PSCI_FN_CPU_ON] = id;
327 psci_ops.cpu_on = psci_cpu_on;
330 if (!of_property_read_u32(np, "migrate", &id)) {
331 psci_function_id[PSCI_FN_MIGRATE] = id;
332 psci_ops.migrate = psci_migrate;
340 static const struct of_device_id const psci_of_match[] __initconst = {
341 { .compatible = "arm,psci", .data = psci_0_1_init},
342 { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
346 int __init psci_dt_init(void)
348 struct device_node *np;
349 const struct of_device_id *matched_np;
350 psci_initcall_t init_fn;
352 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
357 init_fn = (psci_initcall_t)matched_np->data;
363 * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
364 * explicitly clarified in SBBR
366 int __init psci_acpi_init(void)
368 if (!acpi_psci_present()) {
369 pr_info("is not implemented in ACPI.\n");
373 pr_info("probing for conduit method from ACPI.\n");
375 if (acpi_psci_use_hvc())
376 invoke_psci_fn = __invoke_psci_fn_hvc;
378 invoke_psci_fn = __invoke_psci_fn_smc;