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