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