blob: 1fa7284fb21d62bf6b49cdc9f4aaea5eb93510a2 [file] [log] [blame]
Mark Rutlandbff60792015-07-31 15:46:16 +01001/*
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.
5 *
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.
10 *
11 * Copyright (C) 2015 ARM Limited
12 */
13
14#define pr_fmt(fmt) "psci: " fmt
15
16#include <linux/errno.h>
17#include <linux/linkage.h>
18#include <linux/of.h>
19#include <linux/pm.h>
20#include <linux/printk.h>
21#include <linux/psci.h>
22#include <linux/reboot.h>
23
24#include <uapi/linux/psci.h>
25
26#include <asm/cputype.h>
27#include <asm/system_misc.h>
28#include <asm/smp_plat.h>
29
30/*
Mark Rutland5211df02015-07-31 15:46:17 +010031 * While a 64-bit OS can make calls with SMC32 calling conventions, for some
Sudeep Holla029180b2015-06-18 15:41:33 +010032 * calls it is necessary to use SMC64 to pass or return 64-bit values.
33 * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
34 * (native-width) function ID.
Mark Rutland5211df02015-07-31 15:46:17 +010035 */
36#ifdef CONFIG_64BIT
Sudeep Holla029180b2015-06-18 15:41:33 +010037#define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
Mark Rutland5211df02015-07-31 15:46:17 +010038#else
Sudeep Holla029180b2015-06-18 15:41:33 +010039#define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
Mark Rutland5211df02015-07-31 15:46:17 +010040#endif
41
42/*
Mark Rutlandbff60792015-07-31 15:46:16 +010043 * 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.
47 */
48static int resident_cpu = -1;
49
50bool psci_tos_resident_on(int cpu)
51{
52 return cpu == resident_cpu;
53}
54
55struct psci_operations psci_ops;
56
57typedef unsigned long (psci_fn)(unsigned long, unsigned long,
58 unsigned long, unsigned long);
59asmlinkage psci_fn __invoke_psci_fn_hvc;
60asmlinkage psci_fn __invoke_psci_fn_smc;
61static psci_fn *invoke_psci_fn;
62
63enum psci_function {
64 PSCI_FN_CPU_SUSPEND,
65 PSCI_FN_CPU_ON,
66 PSCI_FN_CPU_OFF,
67 PSCI_FN_MIGRATE,
68 PSCI_FN_MAX,
69};
70
71static u32 psci_function_id[PSCI_FN_MAX];
72
Lorenzo Pieralisi068654c2015-05-26 16:49:01 +010073#define PSCI_0_2_POWER_STATE_MASK \
74 (PSCI_0_2_POWER_STATE_ID_MASK | \
75 PSCI_0_2_POWER_STATE_TYPE_MASK | \
76 PSCI_0_2_POWER_STATE_AFFL_MASK)
77
Lorenzo Pieralisia5c00bb2015-05-26 17:10:32 +010078#define PSCI_1_0_EXT_POWER_STATE_MASK \
79 (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
80 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
81
82static u32 psci_cpu_suspend_feature;
83
84static inline bool psci_has_ext_power_state(void)
85{
86 return psci_cpu_suspend_feature &
87 PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
88}
89
Lorenzo Pieralisi068654c2015-05-26 16:49:01 +010090bool psci_power_state_loses_context(u32 state)
91{
Lorenzo Pieralisia5c00bb2015-05-26 17:10:32 +010092 const u32 mask = psci_has_ext_power_state() ?
93 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
94 PSCI_0_2_POWER_STATE_TYPE_MASK;
95
96 return state & mask;
Lorenzo Pieralisi068654c2015-05-26 16:49:01 +010097}
98
99bool psci_power_state_is_valid(u32 state)
100{
Lorenzo Pieralisia5c00bb2015-05-26 17:10:32 +0100101 const u32 valid_mask = psci_has_ext_power_state() ?
102 PSCI_1_0_EXT_POWER_STATE_MASK :
103 PSCI_0_2_POWER_STATE_MASK;
104
105 return !(state & ~valid_mask);
Lorenzo Pieralisi068654c2015-05-26 16:49:01 +0100106}
107
Mark Rutlandbff60792015-07-31 15:46:16 +0100108static int psci_to_linux_errno(int errno)
109{
110 switch (errno) {
111 case PSCI_RET_SUCCESS:
112 return 0;
113 case PSCI_RET_NOT_SUPPORTED:
114 return -EOPNOTSUPP;
115 case PSCI_RET_INVALID_PARAMS:
Lorenzo Pieralisi2217d7c2015-05-22 14:31:37 +0100116 case PSCI_RET_INVALID_ADDRESS:
Mark Rutlandbff60792015-07-31 15:46:16 +0100117 return -EINVAL;
118 case PSCI_RET_DENIED:
119 return -EPERM;
120 };
121
122 return -EINVAL;
123}
124
125static u32 psci_get_version(void)
126{
127 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
128}
129
130static int psci_cpu_suspend(u32 state, unsigned long entry_point)
131{
132 int err;
133 u32 fn;
134
135 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
136 err = invoke_psci_fn(fn, state, entry_point, 0);
137 return psci_to_linux_errno(err);
138}
139
140static int psci_cpu_off(u32 state)
141{
142 int err;
143 u32 fn;
144
145 fn = psci_function_id[PSCI_FN_CPU_OFF];
146 err = invoke_psci_fn(fn, state, 0, 0);
147 return psci_to_linux_errno(err);
148}
149
150static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
151{
152 int err;
153 u32 fn;
154
155 fn = psci_function_id[PSCI_FN_CPU_ON];
156 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
157 return psci_to_linux_errno(err);
158}
159
160static int psci_migrate(unsigned long cpuid)
161{
162 int err;
163 u32 fn;
164
165 fn = psci_function_id[PSCI_FN_MIGRATE];
166 err = invoke_psci_fn(fn, cpuid, 0, 0);
167 return psci_to_linux_errno(err);
168}
169
170static int psci_affinity_info(unsigned long target_affinity,
171 unsigned long lowest_affinity_level)
172{
Sudeep Holla029180b2015-06-18 15:41:33 +0100173 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
Mark Rutland5211df02015-07-31 15:46:17 +0100174 target_affinity, lowest_affinity_level, 0);
Mark Rutlandbff60792015-07-31 15:46:16 +0100175}
176
177static int psci_migrate_info_type(void)
178{
179 return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
180}
181
182static unsigned long psci_migrate_info_up_cpu(void)
183{
Sudeep Holla029180b2015-06-18 15:41:33 +0100184 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
Mark Rutland5211df02015-07-31 15:46:17 +0100185 0, 0, 0);
Mark Rutlandbff60792015-07-31 15:46:16 +0100186}
187
188static int get_set_conduit_method(struct device_node *np)
189{
190 const char *method;
191
192 pr_info("probing for conduit method from DT.\n");
193
194 if (of_property_read_string(np, "method", &method)) {
195 pr_warn("missing \"method\" property\n");
196 return -ENXIO;
197 }
198
199 if (!strcmp("hvc", method)) {
200 invoke_psci_fn = __invoke_psci_fn_hvc;
201 } else if (!strcmp("smc", method)) {
202 invoke_psci_fn = __invoke_psci_fn_smc;
203 } else {
204 pr_warn("invalid \"method\" property: %s\n", method);
205 return -EINVAL;
206 }
207 return 0;
208}
209
210static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
211{
212 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
213}
214
215static void psci_sys_poweroff(void)
216{
217 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
218}
219
Lorenzo Pieralisi5f004e02015-05-26 17:06:21 +0100220static int __init psci_features(u32 psci_func_id)
221{
222 return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
223 psci_func_id, 0, 0);
224}
225
Lorenzo Pieralisia5c00bb2015-05-26 17:10:32 +0100226static void __init psci_init_cpu_suspend(void)
227{
228 int feature = psci_features(psci_function_id[PSCI_FN_CPU_SUSPEND]);
229
230 if (feature != PSCI_RET_NOT_SUPPORTED)
231 psci_cpu_suspend_feature = feature;
232}
233
Mark Rutlandbff60792015-07-31 15:46:16 +0100234/*
235 * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
236 * return DENIED (which would be fatal).
237 */
238static void __init psci_init_migrate(void)
239{
240 unsigned long cpuid;
241 int type, cpu = -1;
242
243 type = psci_ops.migrate_info_type();
244
245 if (type == PSCI_0_2_TOS_MP) {
246 pr_info("Trusted OS migration not required\n");
247 return;
248 }
249
250 if (type == PSCI_RET_NOT_SUPPORTED) {
251 pr_info("MIGRATE_INFO_TYPE not supported.\n");
252 return;
253 }
254
255 if (type != PSCI_0_2_TOS_UP_MIGRATE &&
256 type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
257 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
258 return;
259 }
260
261 cpuid = psci_migrate_info_up_cpu();
262 if (cpuid & ~MPIDR_HWID_BITMASK) {
263 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
264 cpuid);
265 return;
266 }
267
268 cpu = get_logical_index(cpuid);
269 resident_cpu = cpu >= 0 ? cpu : -1;
270
271 pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
272}
273
274static void __init psci_0_2_set_functions(void)
275{
276 pr_info("Using standard PSCI v0.2 function IDs\n");
Sudeep Holla029180b2015-06-18 15:41:33 +0100277 psci_function_id[PSCI_FN_CPU_SUSPEND] =
278 PSCI_FN_NATIVE(0_2, CPU_SUSPEND);
Mark Rutlandbff60792015-07-31 15:46:16 +0100279 psci_ops.cpu_suspend = psci_cpu_suspend;
280
281 psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
282 psci_ops.cpu_off = psci_cpu_off;
283
Sudeep Holla029180b2015-06-18 15:41:33 +0100284 psci_function_id[PSCI_FN_CPU_ON] = PSCI_FN_NATIVE(0_2, CPU_ON);
Mark Rutlandbff60792015-07-31 15:46:16 +0100285 psci_ops.cpu_on = psci_cpu_on;
286
Sudeep Holla029180b2015-06-18 15:41:33 +0100287 psci_function_id[PSCI_FN_MIGRATE] = PSCI_FN_NATIVE(0_2, MIGRATE);
Mark Rutlandbff60792015-07-31 15:46:16 +0100288 psci_ops.migrate = psci_migrate;
289
290 psci_ops.affinity_info = psci_affinity_info;
291
292 psci_ops.migrate_info_type = psci_migrate_info_type;
293
294 arm_pm_restart = psci_sys_reset;
295
296 pm_power_off = psci_sys_poweroff;
297}
298
299/*
300 * Probe function for PSCI firmware versions >= 0.2
301 */
302static int __init psci_probe(void)
303{
304 u32 ver = psci_get_version();
305
306 pr_info("PSCIv%d.%d detected in firmware.\n",
307 PSCI_VERSION_MAJOR(ver),
308 PSCI_VERSION_MINOR(ver));
309
310 if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
311 pr_err("Conflicting PSCI version detected.\n");
312 return -EINVAL;
313 }
314
315 psci_0_2_set_functions();
316
317 psci_init_migrate();
318
Lorenzo Pieralisia5c00bb2015-05-26 17:10:32 +0100319 psci_init_cpu_suspend();
320
Mark Rutlandbff60792015-07-31 15:46:16 +0100321 return 0;
322}
323
324typedef int (*psci_initcall_t)(const struct device_node *);
325
326/*
327 * PSCI init function for PSCI versions >=0.2
328 *
329 * Probe based on PSCI PSCI_VERSION function
330 */
331static int __init psci_0_2_init(struct device_node *np)
332{
333 int err;
334
335 err = get_set_conduit_method(np);
336
337 if (err)
338 goto out_put_node;
339 /*
340 * Starting with v0.2, the PSCI specification introduced a call
341 * (PSCI_VERSION) that allows probing the firmware version, so
342 * that PSCI function IDs and version specific initialization
343 * can be carried out according to the specific version reported
344 * by firmware
345 */
346 err = psci_probe();
347
348out_put_node:
349 of_node_put(np);
350 return err;
351}
352
353/*
354 * PSCI < v0.2 get PSCI Function IDs via DT.
355 */
356static int __init psci_0_1_init(struct device_node *np)
357{
358 u32 id;
359 int err;
360
361 err = get_set_conduit_method(np);
362
363 if (err)
364 goto out_put_node;
365
366 pr_info("Using PSCI v0.1 Function IDs from DT\n");
367
368 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
369 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
370 psci_ops.cpu_suspend = psci_cpu_suspend;
371 }
372
373 if (!of_property_read_u32(np, "cpu_off", &id)) {
374 psci_function_id[PSCI_FN_CPU_OFF] = id;
375 psci_ops.cpu_off = psci_cpu_off;
376 }
377
378 if (!of_property_read_u32(np, "cpu_on", &id)) {
379 psci_function_id[PSCI_FN_CPU_ON] = id;
380 psci_ops.cpu_on = psci_cpu_on;
381 }
382
383 if (!of_property_read_u32(np, "migrate", &id)) {
384 psci_function_id[PSCI_FN_MIGRATE] = id;
385 psci_ops.migrate = psci_migrate;
386 }
387
388out_put_node:
389 of_node_put(np);
390 return err;
391}
392
Linus Torvaldsc706c7e2015-09-03 16:27:01 -0700393static const struct of_device_id const psci_of_match[] __initconst = {
Mark Rutlandbff60792015-07-31 15:46:16 +0100394 { .compatible = "arm,psci", .data = psci_0_1_init},
395 { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
Lorenzo Pieralisi0fc197c2015-05-26 13:06:57 +0100396 { .compatible = "arm,psci-1.0", .data = psci_0_2_init},
Mark Rutlandbff60792015-07-31 15:46:16 +0100397 {},
398};
399
400int __init psci_dt_init(void)
401{
402 struct device_node *np;
403 const struct of_device_id *matched_np;
404 psci_initcall_t init_fn;
405
406 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
407
408 if (!np)
409 return -ENODEV;
410
411 init_fn = (psci_initcall_t)matched_np->data;
412 return init_fn(np);
413}
414
415#ifdef CONFIG_ACPI
416/*
417 * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
418 * explicitly clarified in SBBR
419 */
420int __init psci_acpi_init(void)
421{
422 if (!acpi_psci_present()) {
423 pr_info("is not implemented in ACPI.\n");
424 return -EOPNOTSUPP;
425 }
426
427 pr_info("probing for conduit method from ACPI.\n");
428
429 if (acpi_psci_use_hvc())
430 invoke_psci_fn = __invoke_psci_fn_hvc;
431 else
432 invoke_psci_fn = __invoke_psci_fn_smc;
433
434 return psci_probe();
435}
436#endif