1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 *
4 * Copyright (C) 2015 ARM Limited
5 */
6
7 #define pr_fmt(fmt) "psci: " fmt
8
9 #include <linux/acpi.h>
10 #include <linux/arm-smccc.h>
11 #include <linux/cpuidle.h>
12 #include <linux/errno.h>
13 #include <linux/linkage.h>
14 #include <linux/of.h>
15 #include <linux/pm.h>
16 #include <linux/printk.h>
17 #include <linux/psci.h>
18 #include <linux/reboot.h>
19 #include <linux/slab.h>
20 #include <linux/suspend.h>
21
22 #include <uapi/linux/psci.h>
23
24 #include <asm/cpuidle.h>
25 #include <asm/cputype.h>
26 #include <asm/hypervisor.h>
27 #include <asm/system_misc.h>
28 #include <asm/smp_plat.h>
29 #include <asm/suspend.h>
30 #include <trace/hooks/psci.h>
31
32 /*
33 * While a 64-bit OS can make calls with SMC32 calling conventions, for some
34 * calls it is necessary to use SMC64 to pass or return 64-bit values.
35 * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
36 * (native-width) function ID.
37 */
38 #ifdef CONFIG_64BIT
39 #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
40 #else
41 #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
42 #endif
43
44 /*
45 * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
46 * calls to its resident CPU, so we must avoid issuing those. We never migrate
47 * a Trusted OS even if it claims to be capable of migration -- doing so will
48 * require cooperation with a Trusted OS driver.
49 */
50 static int resident_cpu = -1;
51 struct psci_operations psci_ops;
52 static enum arm_smccc_conduit psci_conduit = SMCCC_CONDUIT_NONE;
53
psci_tos_resident_on(int cpu)54 bool psci_tos_resident_on(int cpu)
55 {
56 bool resident = false;
57
58 trace_android_vh_psci_tos_resident_on(cpu, &resident);
59 if (resident)
60 return resident;
61
62 return cpu == resident_cpu;
63 }
64
65 typedef unsigned long (psci_fn)(unsigned long, unsigned long,
66 unsigned long, unsigned long);
67 static psci_fn *invoke_psci_fn;
68
69 static struct psci_0_1_function_ids psci_0_1_function_ids;
70
get_psci_0_1_function_ids(void)71 struct psci_0_1_function_ids get_psci_0_1_function_ids(void)
72 {
73 return psci_0_1_function_ids;
74 }
75
76 #define PSCI_0_2_POWER_STATE_MASK \
77 (PSCI_0_2_POWER_STATE_ID_MASK | \
78 PSCI_0_2_POWER_STATE_TYPE_MASK | \
79 PSCI_0_2_POWER_STATE_AFFL_MASK)
80
81 #define PSCI_1_0_EXT_POWER_STATE_MASK \
82 (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
83 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
84
85 static u32 psci_cpu_suspend_feature;
86 static bool psci_system_reset2_supported;
87
psci_has_ext_power_state(void)88 static inline bool psci_has_ext_power_state(void)
89 {
90 return psci_cpu_suspend_feature &
91 PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
92 }
93
psci_has_osi_support(void)94 bool psci_has_osi_support(void)
95 {
96 return psci_cpu_suspend_feature & PSCI_1_0_OS_INITIATED;
97 }
98
psci_power_state_loses_context(u32 state)99 static inline bool psci_power_state_loses_context(u32 state)
100 {
101 const u32 mask = psci_has_ext_power_state() ?
102 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
103 PSCI_0_2_POWER_STATE_TYPE_MASK;
104
105 return state & mask;
106 }
107
psci_power_state_is_valid(u32 state)108 bool psci_power_state_is_valid(u32 state)
109 {
110 const u32 valid_mask = psci_has_ext_power_state() ?
111 PSCI_1_0_EXT_POWER_STATE_MASK :
112 PSCI_0_2_POWER_STATE_MASK;
113
114 return !(state & ~valid_mask);
115 }
116
__invoke_psci_fn_hvc(unsigned long function_id,unsigned long arg0,unsigned long arg1,unsigned long arg2)117 static unsigned long __invoke_psci_fn_hvc(unsigned long function_id,
118 unsigned long arg0, unsigned long arg1,
119 unsigned long arg2)
120 {
121 struct arm_smccc_res res;
122
123 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
124 return res.a0;
125 }
126
__invoke_psci_fn_smc(unsigned long function_id,unsigned long arg0,unsigned long arg1,unsigned long arg2)127 static unsigned long __invoke_psci_fn_smc(unsigned long function_id,
128 unsigned long arg0, unsigned long arg1,
129 unsigned long arg2)
130 {
131 struct arm_smccc_res res;
132
133 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
134 return res.a0;
135 }
136
psci_to_linux_errno(int errno)137 static int psci_to_linux_errno(int errno)
138 {
139 switch (errno) {
140 case PSCI_RET_SUCCESS:
141 return 0;
142 case PSCI_RET_NOT_SUPPORTED:
143 return -EOPNOTSUPP;
144 case PSCI_RET_INVALID_PARAMS:
145 case PSCI_RET_INVALID_ADDRESS:
146 return -EINVAL;
147 case PSCI_RET_DENIED:
148 return -EPERM;
149 }
150
151 return -EINVAL;
152 }
153
psci_0_1_get_version(void)154 static u32 psci_0_1_get_version(void)
155 {
156 return PSCI_VERSION(0, 1);
157 }
158
psci_0_2_get_version(void)159 static u32 psci_0_2_get_version(void)
160 {
161 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
162 }
163
psci_set_osi_mode(bool enable)164 int psci_set_osi_mode(bool enable)
165 {
166 unsigned long suspend_mode;
167 int err;
168
169 suspend_mode = enable ? PSCI_1_0_SUSPEND_MODE_OSI :
170 PSCI_1_0_SUSPEND_MODE_PC;
171
172 err = invoke_psci_fn(PSCI_1_0_FN_SET_SUSPEND_MODE, suspend_mode, 0, 0);
173 return psci_to_linux_errno(err);
174 }
175
__psci_cpu_suspend(u32 fn,u32 state,unsigned long entry_point)176 static int __psci_cpu_suspend(u32 fn, u32 state, unsigned long entry_point)
177 {
178 int err;
179 bool deny = false;
180
181 trace_android_vh_psci_cpu_suspend(state, &deny);
182 if (deny)
183 return -EPERM;
184
185 err = invoke_psci_fn(fn, state, entry_point, 0);
186 return psci_to_linux_errno(err);
187 }
188
psci_0_1_cpu_suspend(u32 state,unsigned long entry_point)189 static int psci_0_1_cpu_suspend(u32 state, unsigned long entry_point)
190 {
191 return __psci_cpu_suspend(psci_0_1_function_ids.cpu_suspend,
192 state, entry_point);
193 }
194
psci_0_2_cpu_suspend(u32 state,unsigned long entry_point)195 static int psci_0_2_cpu_suspend(u32 state, unsigned long entry_point)
196 {
197 return __psci_cpu_suspend(PSCI_FN_NATIVE(0_2, CPU_SUSPEND),
198 state, entry_point);
199 }
200
__psci_cpu_off(u32 fn,u32 state)201 static int __psci_cpu_off(u32 fn, u32 state)
202 {
203 int err;
204
205 err = invoke_psci_fn(fn, state, 0, 0);
206 return psci_to_linux_errno(err);
207 }
208
psci_0_1_cpu_off(u32 state)209 static int psci_0_1_cpu_off(u32 state)
210 {
211 return __psci_cpu_off(psci_0_1_function_ids.cpu_off, state);
212 }
213
psci_0_2_cpu_off(u32 state)214 static int psci_0_2_cpu_off(u32 state)
215 {
216 return __psci_cpu_off(PSCI_0_2_FN_CPU_OFF, state);
217 }
218
__psci_cpu_on(u32 fn,unsigned long cpuid,unsigned long entry_point)219 static int __psci_cpu_on(u32 fn, unsigned long cpuid, unsigned long entry_point)
220 {
221 int err;
222
223 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
224 return psci_to_linux_errno(err);
225 }
226
psci_0_1_cpu_on(unsigned long cpuid,unsigned long entry_point)227 static int psci_0_1_cpu_on(unsigned long cpuid, unsigned long entry_point)
228 {
229 return __psci_cpu_on(psci_0_1_function_ids.cpu_on, cpuid, entry_point);
230 }
231
psci_0_2_cpu_on(unsigned long cpuid,unsigned long entry_point)232 static int psci_0_2_cpu_on(unsigned long cpuid, unsigned long entry_point)
233 {
234 return __psci_cpu_on(PSCI_FN_NATIVE(0_2, CPU_ON), cpuid, entry_point);
235 }
236
__psci_migrate(u32 fn,unsigned long cpuid)237 static int __psci_migrate(u32 fn, unsigned long cpuid)
238 {
239 int err;
240
241 err = invoke_psci_fn(fn, cpuid, 0, 0);
242 return psci_to_linux_errno(err);
243 }
244
psci_0_1_migrate(unsigned long cpuid)245 static int psci_0_1_migrate(unsigned long cpuid)
246 {
247 return __psci_migrate(psci_0_1_function_ids.migrate, cpuid);
248 }
249
psci_0_2_migrate(unsigned long cpuid)250 static int psci_0_2_migrate(unsigned long cpuid)
251 {
252 return __psci_migrate(PSCI_FN_NATIVE(0_2, MIGRATE), cpuid);
253 }
254
psci_affinity_info(unsigned long target_affinity,unsigned long lowest_affinity_level)255 static int psci_affinity_info(unsigned long target_affinity,
256 unsigned long lowest_affinity_level)
257 {
258 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
259 target_affinity, lowest_affinity_level, 0);
260 }
261
psci_migrate_info_type(void)262 static int psci_migrate_info_type(void)
263 {
264 return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
265 }
266
psci_migrate_info_up_cpu(void)267 static unsigned long psci_migrate_info_up_cpu(void)
268 {
269 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
270 0, 0, 0);
271 }
272
set_conduit(enum arm_smccc_conduit conduit)273 static void set_conduit(enum arm_smccc_conduit conduit)
274 {
275 switch (conduit) {
276 case SMCCC_CONDUIT_HVC:
277 invoke_psci_fn = __invoke_psci_fn_hvc;
278 break;
279 case SMCCC_CONDUIT_SMC:
280 invoke_psci_fn = __invoke_psci_fn_smc;
281 break;
282 default:
283 WARN(1, "Unexpected PSCI conduit %d\n", conduit);
284 }
285
286 psci_conduit = conduit;
287 }
288
get_set_conduit_method(struct device_node * np)289 static int get_set_conduit_method(struct device_node *np)
290 {
291 const char *method;
292
293 pr_info("probing for conduit method from DT.\n");
294
295 if (of_property_read_string(np, "method", &method)) {
296 pr_warn("missing \"method\" property\n");
297 return -ENXIO;
298 }
299
300 if (!strcmp("hvc", method)) {
301 set_conduit(SMCCC_CONDUIT_HVC);
302 } else if (!strcmp("smc", method)) {
303 set_conduit(SMCCC_CONDUIT_SMC);
304 } else {
305 pr_warn("invalid \"method\" property: %s\n", method);
306 return -EINVAL;
307 }
308 return 0;
309 }
310
psci_sys_reset(struct notifier_block * nb,unsigned long action,void * data)311 static int psci_sys_reset(struct notifier_block *nb, unsigned long action,
312 void *data)
313 {
314 if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) &&
315 psci_system_reset2_supported) {
316 /*
317 * reset_type[31] = 0 (architectural)
318 * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
319 * cookie = 0 (ignored by the implementation)
320 */
321 invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
322 } else {
323 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
324 }
325
326 return NOTIFY_DONE;
327 }
328
329 static struct notifier_block psci_sys_reset_nb = {
330 .notifier_call = psci_sys_reset,
331 .priority = 129,
332 };
333
psci_sys_poweroff(void)334 static void psci_sys_poweroff(void)
335 {
336 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
337 }
338
psci_features(u32 psci_func_id)339 static int __init psci_features(u32 psci_func_id)
340 {
341 return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
342 psci_func_id, 0, 0);
343 }
344
345 #ifdef CONFIG_CPU_IDLE
psci_suspend_finisher(unsigned long state)346 static int psci_suspend_finisher(unsigned long state)
347 {
348 u32 power_state = state;
349 phys_addr_t pa_cpu_resume = __pa_symbol(function_nocfi(cpu_resume));
350
351 return psci_ops.cpu_suspend(power_state, pa_cpu_resume);
352 }
353
psci_cpu_suspend_enter(u32 state)354 int psci_cpu_suspend_enter(u32 state)
355 {
356 int ret;
357
358 if (!psci_power_state_loses_context(state)) {
359 struct arm_cpuidle_irq_context context;
360
361 arm_cpuidle_save_irq_context(&context);
362 ret = psci_ops.cpu_suspend(state, 0);
363 arm_cpuidle_restore_irq_context(&context);
364 } else {
365 ret = cpu_suspend(state, psci_suspend_finisher);
366 }
367
368 return ret;
369 }
370 #endif
371
psci_system_suspend(unsigned long unused)372 static int psci_system_suspend(unsigned long unused)
373 {
374 phys_addr_t pa_cpu_resume = __pa_symbol(function_nocfi(cpu_resume));
375
376 return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
377 pa_cpu_resume, 0, 0);
378 }
379
psci_system_suspend_enter(suspend_state_t state)380 static int psci_system_suspend_enter(suspend_state_t state)
381 {
382 return cpu_suspend(0, psci_system_suspend);
383 }
384
385 static const struct platform_suspend_ops psci_suspend_ops = {
386 .valid = suspend_valid_only_mem,
387 .enter = psci_system_suspend_enter,
388 };
389
psci_init_system_reset2(void)390 static void __init psci_init_system_reset2(void)
391 {
392 int ret;
393
394 ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2));
395
396 if (ret != PSCI_RET_NOT_SUPPORTED)
397 psci_system_reset2_supported = true;
398 }
399
psci_init_system_suspend(void)400 static void __init psci_init_system_suspend(void)
401 {
402 int ret;
403
404 if (!IS_ENABLED(CONFIG_SUSPEND))
405 return;
406
407 ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
408
409 if (ret != PSCI_RET_NOT_SUPPORTED)
410 suspend_set_ops(&psci_suspend_ops);
411 }
412
psci_init_cpu_suspend(void)413 static void __init psci_init_cpu_suspend(void)
414 {
415 int feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
416
417 if (feature != PSCI_RET_NOT_SUPPORTED)
418 psci_cpu_suspend_feature = feature;
419 }
420
421 /*
422 * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
423 * return DENIED (which would be fatal).
424 */
psci_init_migrate(void)425 static void __init psci_init_migrate(void)
426 {
427 unsigned long cpuid;
428 int type, cpu = -1;
429
430 type = psci_ops.migrate_info_type();
431
432 if (type == PSCI_0_2_TOS_MP) {
433 pr_info("Trusted OS migration not required\n");
434 return;
435 }
436
437 if (type == PSCI_RET_NOT_SUPPORTED) {
438 pr_info("MIGRATE_INFO_TYPE not supported.\n");
439 return;
440 }
441
442 if (type != PSCI_0_2_TOS_UP_MIGRATE &&
443 type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
444 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
445 return;
446 }
447
448 cpuid = psci_migrate_info_up_cpu();
449 if (cpuid & ~MPIDR_HWID_BITMASK) {
450 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
451 cpuid);
452 return;
453 }
454
455 cpu = get_logical_index(cpuid);
456 resident_cpu = cpu >= 0 ? cpu : -1;
457
458 pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
459 }
460
psci_init_smccc(void)461 static void __init psci_init_smccc(void)
462 {
463 u32 ver = ARM_SMCCC_VERSION_1_0;
464 int feature;
465
466 feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
467
468 if (feature != PSCI_RET_NOT_SUPPORTED) {
469 u32 ret;
470 ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
471 if (ret >= ARM_SMCCC_VERSION_1_1) {
472 arm_smccc_version_init(ret, psci_conduit);
473 ver = ret;
474 }
475 }
476
477 /*
478 * Conveniently, the SMCCC and PSCI versions are encoded the
479 * same way. No, this isn't accidental.
480 */
481 pr_info("SMC Calling Convention v%d.%d\n",
482 PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
483
484 }
485
psci_0_2_set_functions(void)486 static void __init psci_0_2_set_functions(void)
487 {
488 pr_info("Using standard PSCI v0.2 function IDs\n");
489
490 psci_ops = (struct psci_operations){
491 .get_version = psci_0_2_get_version,
492 .cpu_suspend = psci_0_2_cpu_suspend,
493 .cpu_off = psci_0_2_cpu_off,
494 .cpu_on = psci_0_2_cpu_on,
495 .migrate = psci_0_2_migrate,
496 .affinity_info = psci_affinity_info,
497 .migrate_info_type = psci_migrate_info_type,
498 };
499
500 register_restart_handler(&psci_sys_reset_nb);
501
502 pm_power_off = psci_sys_poweroff;
503 }
504
505 /*
506 * Probe function for PSCI firmware versions >= 0.2
507 */
psci_probe(void)508 static int __init psci_probe(void)
509 {
510 u32 ver = psci_0_2_get_version();
511
512 pr_info("PSCIv%d.%d detected in firmware.\n",
513 PSCI_VERSION_MAJOR(ver),
514 PSCI_VERSION_MINOR(ver));
515
516 if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
517 pr_err("Conflicting PSCI version detected.\n");
518 return -EINVAL;
519 }
520
521 psci_0_2_set_functions();
522
523 psci_init_migrate();
524
525 if (PSCI_VERSION_MAJOR(ver) >= 1) {
526 psci_init_smccc();
527 psci_init_cpu_suspend();
528 psci_init_system_suspend();
529 psci_init_system_reset2();
530 kvm_init_hyp_services();
531 }
532
533 return 0;
534 }
535
536 typedef int (*psci_initcall_t)(const struct device_node *);
537
538 /*
539 * PSCI init function for PSCI versions >=0.2
540 *
541 * Probe based on PSCI PSCI_VERSION function
542 */
psci_0_2_init(struct device_node * np)543 static int __init psci_0_2_init(struct device_node *np)
544 {
545 int err;
546
547 err = get_set_conduit_method(np);
548 if (err)
549 return err;
550
551 /*
552 * Starting with v0.2, the PSCI specification introduced a call
553 * (PSCI_VERSION) that allows probing the firmware version, so
554 * that PSCI function IDs and version specific initialization
555 * can be carried out according to the specific version reported
556 * by firmware
557 */
558 return psci_probe();
559 }
560
561 /*
562 * PSCI < v0.2 get PSCI Function IDs via DT.
563 */
psci_0_1_init(struct device_node * np)564 static int __init psci_0_1_init(struct device_node *np)
565 {
566 u32 id;
567 int err;
568
569 err = get_set_conduit_method(np);
570 if (err)
571 return err;
572
573 pr_info("Using PSCI v0.1 Function IDs from DT\n");
574
575 psci_ops.get_version = psci_0_1_get_version;
576
577 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
578 psci_0_1_function_ids.cpu_suspend = id;
579 psci_ops.cpu_suspend = psci_0_1_cpu_suspend;
580 }
581
582 if (!of_property_read_u32(np, "cpu_off", &id)) {
583 psci_0_1_function_ids.cpu_off = id;
584 psci_ops.cpu_off = psci_0_1_cpu_off;
585 }
586
587 if (!of_property_read_u32(np, "cpu_on", &id)) {
588 psci_0_1_function_ids.cpu_on = id;
589 psci_ops.cpu_on = psci_0_1_cpu_on;
590 }
591
592 if (!of_property_read_u32(np, "migrate", &id)) {
593 psci_0_1_function_ids.migrate = id;
594 psci_ops.migrate = psci_0_1_migrate;
595 }
596
597 return 0;
598 }
599
psci_1_0_init(struct device_node * np)600 static int __init psci_1_0_init(struct device_node *np)
601 {
602 int err;
603
604 err = psci_0_2_init(np);
605 if (err)
606 return err;
607
608 if (psci_has_osi_support()) {
609 pr_info("OSI mode supported.\n");
610
611 /* Default to PC mode. */
612 psci_set_osi_mode(false);
613 }
614
615 return 0;
616 }
617
618 static const struct of_device_id psci_of_match[] __initconst = {
619 { .compatible = "arm,psci", .data = psci_0_1_init},
620 { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
621 { .compatible = "arm,psci-1.0", .data = psci_1_0_init},
622 {},
623 };
624
psci_dt_init(void)625 int __init psci_dt_init(void)
626 {
627 struct device_node *np;
628 const struct of_device_id *matched_np;
629 psci_initcall_t init_fn;
630 int ret;
631
632 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
633
634 if (!np || !of_device_is_available(np))
635 return -ENODEV;
636
637 init_fn = (psci_initcall_t)matched_np->data;
638 ret = init_fn(np);
639
640 of_node_put(np);
641 return ret;
642 }
643
644 #ifdef CONFIG_ACPI
645 /*
646 * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
647 * explicitly clarified in SBBR
648 */
psci_acpi_init(void)649 int __init psci_acpi_init(void)
650 {
651 if (!acpi_psci_present()) {
652 pr_info("is not implemented in ACPI.\n");
653 return -EOPNOTSUPP;
654 }
655
656 pr_info("probing for conduit method from ACPI.\n");
657
658 if (acpi_psci_use_hvc())
659 set_conduit(SMCCC_CONDUIT_HVC);
660 else
661 set_conduit(SMCCC_CONDUIT_SMC);
662
663 return psci_probe();
664 }
665 #endif
666