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_warn("failed to set %s mode: %d\n", enable ? "OSI" : "PC", err);
178 return psci_to_linux_errno(err);
179 }
180
181 static __always_inline int
__psci_cpu_suspend(u32 fn,u32 state,unsigned long entry_point)182 __psci_cpu_suspend(u32 fn, u32 state, unsigned long entry_point)
183 {
184 int err;
185 bool deny = false;
186
187 trace_android_rvh_psci_cpu_suspend(state, &deny);
188 if (deny)
189 return -EPERM;
190
191 err = invoke_psci_fn(fn, state, entry_point, 0);
192 return psci_to_linux_errno(err);
193 }
194
195 static __always_inline int
psci_0_1_cpu_suspend(u32 state,unsigned long entry_point)196 psci_0_1_cpu_suspend(u32 state, unsigned long entry_point)
197 {
198 return __psci_cpu_suspend(psci_0_1_function_ids.cpu_suspend,
199 state, entry_point);
200 }
201
202 static __always_inline int
psci_0_2_cpu_suspend(u32 state,unsigned long entry_point)203 psci_0_2_cpu_suspend(u32 state, unsigned long entry_point)
204 {
205 return __psci_cpu_suspend(PSCI_FN_NATIVE(0_2, CPU_SUSPEND),
206 state, entry_point);
207 }
208
__psci_cpu_off(u32 fn,u32 state)209 static int __psci_cpu_off(u32 fn, u32 state)
210 {
211 int err;
212
213 err = invoke_psci_fn(fn, state, 0, 0);
214 return psci_to_linux_errno(err);
215 }
216
psci_0_1_cpu_off(u32 state)217 static int psci_0_1_cpu_off(u32 state)
218 {
219 return __psci_cpu_off(psci_0_1_function_ids.cpu_off, state);
220 }
221
psci_0_2_cpu_off(u32 state)222 static int psci_0_2_cpu_off(u32 state)
223 {
224 return __psci_cpu_off(PSCI_0_2_FN_CPU_OFF, state);
225 }
226
__psci_cpu_on(u32 fn,unsigned long cpuid,unsigned long entry_point)227 static int __psci_cpu_on(u32 fn, unsigned long cpuid, unsigned long entry_point)
228 {
229 int err;
230
231 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
232 return psci_to_linux_errno(err);
233 }
234
psci_0_1_cpu_on(unsigned long cpuid,unsigned long entry_point)235 static int psci_0_1_cpu_on(unsigned long cpuid, unsigned long entry_point)
236 {
237 return __psci_cpu_on(psci_0_1_function_ids.cpu_on, cpuid, entry_point);
238 }
239
psci_0_2_cpu_on(unsigned long cpuid,unsigned long entry_point)240 static int psci_0_2_cpu_on(unsigned long cpuid, unsigned long entry_point)
241 {
242 return __psci_cpu_on(PSCI_FN_NATIVE(0_2, CPU_ON), cpuid, entry_point);
243 }
244
__psci_migrate(u32 fn,unsigned long cpuid)245 static int __psci_migrate(u32 fn, unsigned long cpuid)
246 {
247 int err;
248
249 err = invoke_psci_fn(fn, cpuid, 0, 0);
250 return psci_to_linux_errno(err);
251 }
252
psci_0_1_migrate(unsigned long cpuid)253 static int psci_0_1_migrate(unsigned long cpuid)
254 {
255 return __psci_migrate(psci_0_1_function_ids.migrate, cpuid);
256 }
257
psci_0_2_migrate(unsigned long cpuid)258 static int psci_0_2_migrate(unsigned long cpuid)
259 {
260 return __psci_migrate(PSCI_FN_NATIVE(0_2, MIGRATE), cpuid);
261 }
262
psci_affinity_info(unsigned long target_affinity,unsigned long lowest_affinity_level)263 static int psci_affinity_info(unsigned long target_affinity,
264 unsigned long lowest_affinity_level)
265 {
266 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
267 target_affinity, lowest_affinity_level, 0);
268 }
269
psci_migrate_info_type(void)270 static int psci_migrate_info_type(void)
271 {
272 return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
273 }
274
psci_migrate_info_up_cpu(void)275 static unsigned long psci_migrate_info_up_cpu(void)
276 {
277 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
278 0, 0, 0);
279 }
280
set_conduit(enum arm_smccc_conduit conduit)281 static void set_conduit(enum arm_smccc_conduit conduit)
282 {
283 switch (conduit) {
284 case SMCCC_CONDUIT_HVC:
285 invoke_psci_fn = __invoke_psci_fn_hvc;
286 break;
287 case SMCCC_CONDUIT_SMC:
288 invoke_psci_fn = __invoke_psci_fn_smc;
289 break;
290 default:
291 WARN(1, "Unexpected PSCI conduit %d\n", conduit);
292 }
293
294 psci_conduit = conduit;
295 }
296
get_set_conduit_method(const struct device_node * np)297 static int get_set_conduit_method(const struct device_node *np)
298 {
299 const char *method;
300
301 pr_info("probing for conduit method from DT.\n");
302
303 if (of_property_read_string(np, "method", &method)) {
304 pr_warn("missing \"method\" property\n");
305 return -ENXIO;
306 }
307
308 if (!strcmp("hvc", method)) {
309 set_conduit(SMCCC_CONDUIT_HVC);
310 } else if (!strcmp("smc", method)) {
311 set_conduit(SMCCC_CONDUIT_SMC);
312 } else {
313 pr_warn("invalid \"method\" property: %s\n", method);
314 return -EINVAL;
315 }
316 return 0;
317 }
318
psci_sys_reset(struct notifier_block * nb,unsigned long action,void * data)319 static int psci_sys_reset(struct notifier_block *nb, unsigned long action,
320 void *data)
321 {
322 if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) &&
323 psci_system_reset2_supported) {
324 /*
325 * reset_type[31] = 0 (architectural)
326 * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
327 * cookie = 0 (ignored by the implementation)
328 */
329 invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
330 } else {
331 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
332 }
333
334 return NOTIFY_DONE;
335 }
336
337 static struct notifier_block psci_sys_reset_nb = {
338 .notifier_call = psci_sys_reset,
339 .priority = 129,
340 };
341
psci_sys_poweroff(void)342 static void psci_sys_poweroff(void)
343 {
344 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
345 }
346
psci_features(u32 psci_func_id)347 static int psci_features(u32 psci_func_id)
348 {
349 return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
350 psci_func_id, 0, 0);
351 }
352
353 #ifdef CONFIG_DEBUG_FS
354
355 #define PSCI_ID(ver, _name) \
356 { .fn = PSCI_##ver##_FN_##_name, .name = #_name, }
357 #define PSCI_ID_NATIVE(ver, _name) \
358 { .fn = PSCI_FN_NATIVE(ver, _name), .name = #_name, }
359
360 /* A table of all optional functions */
361 static const struct {
362 u32 fn;
363 const char *name;
364 } psci_fn_ids[] = {
365 PSCI_ID_NATIVE(0_2, MIGRATE),
366 PSCI_ID(0_2, MIGRATE_INFO_TYPE),
367 PSCI_ID_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
368 PSCI_ID(1_0, CPU_FREEZE),
369 PSCI_ID_NATIVE(1_0, CPU_DEFAULT_SUSPEND),
370 PSCI_ID_NATIVE(1_0, NODE_HW_STATE),
371 PSCI_ID_NATIVE(1_0, SYSTEM_SUSPEND),
372 PSCI_ID(1_0, SET_SUSPEND_MODE),
373 PSCI_ID_NATIVE(1_0, STAT_RESIDENCY),
374 PSCI_ID_NATIVE(1_0, STAT_COUNT),
375 PSCI_ID_NATIVE(1_1, SYSTEM_RESET2),
376 PSCI_ID(1_1, MEM_PROTECT),
377 PSCI_ID_NATIVE(1_1, MEM_PROTECT_CHECK_RANGE),
378 };
379
psci_debugfs_read(struct seq_file * s,void * data)380 static int psci_debugfs_read(struct seq_file *s, void *data)
381 {
382 int feature, type, i;
383 u32 ver;
384
385 ver = psci_ops.get_version();
386 seq_printf(s, "PSCIv%d.%d\n",
387 PSCI_VERSION_MAJOR(ver),
388 PSCI_VERSION_MINOR(ver));
389
390 /* PSCI_FEATURES is available only starting from 1.0 */
391 if (PSCI_VERSION_MAJOR(ver) < 1)
392 return 0;
393
394 feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
395 if (feature != PSCI_RET_NOT_SUPPORTED) {
396 ver = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
397 seq_printf(s, "SMC Calling Convention v%d.%d\n",
398 PSCI_VERSION_MAJOR(ver),
399 PSCI_VERSION_MINOR(ver));
400 } else {
401 seq_puts(s, "SMC Calling Convention v1.0 is assumed\n");
402 }
403
404 feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
405 if (feature < 0) {
406 seq_printf(s, "PSCI_FEATURES(CPU_SUSPEND) error (%d)\n", feature);
407 } else {
408 seq_printf(s, "OSI is %ssupported\n",
409 (feature & BIT(0)) ? "" : "not ");
410 seq_printf(s, "%s StateID format is used\n",
411 (feature & BIT(1)) ? "Extended" : "Original");
412 }
413
414 type = psci_ops.migrate_info_type();
415 if (type == PSCI_0_2_TOS_UP_MIGRATE ||
416 type == PSCI_0_2_TOS_UP_NO_MIGRATE) {
417 unsigned long cpuid;
418
419 seq_printf(s, "Trusted OS %smigrate capable\n",
420 type == PSCI_0_2_TOS_UP_NO_MIGRATE ? "not " : "");
421 cpuid = psci_migrate_info_up_cpu();
422 seq_printf(s, "Trusted OS resident on physical CPU 0x%lx (#%d)\n",
423 cpuid, resident_cpu);
424 } else if (type == PSCI_0_2_TOS_MP) {
425 seq_puts(s, "Trusted OS migration not required\n");
426 } else {
427 if (type != PSCI_RET_NOT_SUPPORTED)
428 seq_printf(s, "MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
429 }
430
431 for (i = 0; i < ARRAY_SIZE(psci_fn_ids); i++) {
432 feature = psci_features(psci_fn_ids[i].fn);
433 if (feature == PSCI_RET_NOT_SUPPORTED)
434 continue;
435 if (feature < 0)
436 seq_printf(s, "PSCI_FEATURES(%s) error (%d)\n",
437 psci_fn_ids[i].name, feature);
438 else
439 seq_printf(s, "%s is supported\n", psci_fn_ids[i].name);
440 }
441
442 return 0;
443 }
444
psci_debugfs_open(struct inode * inode,struct file * f)445 static int psci_debugfs_open(struct inode *inode, struct file *f)
446 {
447 return single_open(f, psci_debugfs_read, NULL);
448 }
449
450 static const struct file_operations psci_debugfs_ops = {
451 .owner = THIS_MODULE,
452 .open = psci_debugfs_open,
453 .release = single_release,
454 .read = seq_read,
455 .llseek = seq_lseek
456 };
457
psci_debugfs_init(void)458 static int __init psci_debugfs_init(void)
459 {
460 if (!invoke_psci_fn || !psci_ops.get_version)
461 return 0;
462
463 return PTR_ERR_OR_ZERO(debugfs_create_file("psci", 0444, NULL, NULL,
464 &psci_debugfs_ops));
465 }
late_initcall(psci_debugfs_init)466 late_initcall(psci_debugfs_init)
467 #endif
468
469 #ifdef CONFIG_CPU_IDLE
470 static noinstr int psci_suspend_finisher(unsigned long state)
471 {
472 u32 power_state = state;
473 phys_addr_t pa_cpu_resume;
474
475 pa_cpu_resume = __pa_symbol_nodebug((unsigned long)cpu_resume);
476
477 return psci_ops.cpu_suspend(power_state, pa_cpu_resume);
478 }
479
psci_cpu_suspend_enter(u32 state)480 int psci_cpu_suspend_enter(u32 state)
481 {
482 int ret;
483
484 if (!psci_power_state_loses_context(state)) {
485 struct arm_cpuidle_irq_context context;
486
487 arm_cpuidle_save_irq_context(&context);
488 ret = psci_ops.cpu_suspend(state, 0);
489 arm_cpuidle_restore_irq_context(&context);
490 } else {
491 ret = cpu_suspend(state, psci_suspend_finisher);
492 }
493
494 return ret;
495 }
496 #endif
497
psci_system_suspend(unsigned long unused)498 static int psci_system_suspend(unsigned long unused)
499 {
500 phys_addr_t pa_cpu_resume = __pa_symbol(cpu_resume);
501
502 return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
503 pa_cpu_resume, 0, 0);
504 }
505
psci_system_suspend_enter(suspend_state_t state)506 static int psci_system_suspend_enter(suspend_state_t state)
507 {
508 return cpu_suspend(0, psci_system_suspend);
509 }
510
511 static const struct platform_suspend_ops psci_suspend_ops = {
512 .valid = suspend_valid_only_mem,
513 .enter = psci_system_suspend_enter,
514 };
515
psci_init_system_reset2(void)516 static void __init psci_init_system_reset2(void)
517 {
518 int ret;
519
520 ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2));
521
522 if (ret != PSCI_RET_NOT_SUPPORTED)
523 psci_system_reset2_supported = true;
524 }
525
psci_init_system_suspend(void)526 static void __init psci_init_system_suspend(void)
527 {
528 int ret;
529
530 if (!IS_ENABLED(CONFIG_SUSPEND))
531 return;
532
533 ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
534
535 if (ret != PSCI_RET_NOT_SUPPORTED)
536 suspend_set_ops(&psci_suspend_ops);
537 }
538
psci_init_cpu_suspend(void)539 static void __init psci_init_cpu_suspend(void)
540 {
541 int feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
542
543 if (feature != PSCI_RET_NOT_SUPPORTED)
544 psci_cpu_suspend_feature = feature;
545 }
546
547 /*
548 * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
549 * return DENIED (which would be fatal).
550 */
psci_init_migrate(void)551 static void __init psci_init_migrate(void)
552 {
553 unsigned long cpuid;
554 int type, cpu = -1;
555
556 type = psci_ops.migrate_info_type();
557
558 if (type == PSCI_0_2_TOS_MP) {
559 pr_info("Trusted OS migration not required\n");
560 return;
561 }
562
563 if (type == PSCI_RET_NOT_SUPPORTED) {
564 pr_info("MIGRATE_INFO_TYPE not supported.\n");
565 return;
566 }
567
568 if (type != PSCI_0_2_TOS_UP_MIGRATE &&
569 type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
570 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
571 return;
572 }
573
574 cpuid = psci_migrate_info_up_cpu();
575 if (cpuid & ~MPIDR_HWID_BITMASK) {
576 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
577 cpuid);
578 return;
579 }
580
581 cpu = get_logical_index(cpuid);
582 resident_cpu = cpu >= 0 ? cpu : -1;
583
584 pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
585 }
586
psci_init_smccc(void)587 static void __init psci_init_smccc(void)
588 {
589 u32 ver = ARM_SMCCC_VERSION_1_0;
590 int feature;
591
592 feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
593
594 if (feature != PSCI_RET_NOT_SUPPORTED) {
595 u32 ret;
596 ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
597 if (ret >= ARM_SMCCC_VERSION_1_1) {
598 arm_smccc_version_init(ret, psci_conduit);
599 ver = ret;
600 }
601 }
602
603 /*
604 * Conveniently, the SMCCC and PSCI versions are encoded the
605 * same way. No, this isn't accidental.
606 */
607 pr_info("SMC Calling Convention v%d.%d\n",
608 PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
609
610 }
611
psci_0_2_set_functions(void)612 static void __init psci_0_2_set_functions(void)
613 {
614 pr_info("Using standard PSCI v0.2 function IDs\n");
615
616 psci_ops = (struct psci_operations){
617 .get_version = psci_0_2_get_version,
618 .cpu_suspend = psci_0_2_cpu_suspend,
619 .cpu_off = psci_0_2_cpu_off,
620 .cpu_on = psci_0_2_cpu_on,
621 .migrate = psci_0_2_migrate,
622 .affinity_info = psci_affinity_info,
623 .migrate_info_type = psci_migrate_info_type,
624 };
625
626 register_restart_handler(&psci_sys_reset_nb);
627
628 pm_power_off = psci_sys_poweroff;
629 }
630
631 /*
632 * Probe function for PSCI firmware versions >= 0.2
633 */
psci_probe(void)634 static int __init psci_probe(void)
635 {
636 u32 ver = psci_0_2_get_version();
637
638 pr_info("PSCIv%d.%d detected in firmware.\n",
639 PSCI_VERSION_MAJOR(ver),
640 PSCI_VERSION_MINOR(ver));
641
642 if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
643 pr_err("Conflicting PSCI version detected.\n");
644 return -EINVAL;
645 }
646
647 psci_0_2_set_functions();
648
649 psci_init_migrate();
650
651 if (PSCI_VERSION_MAJOR(ver) >= 1) {
652 psci_init_smccc();
653 psci_init_cpu_suspend();
654 psci_init_system_suspend();
655 psci_init_system_reset2();
656 kvm_init_hyp_services();
657 }
658
659 return 0;
660 }
661
662 typedef int (*psci_initcall_t)(const struct device_node *);
663
664 /*
665 * PSCI init function for PSCI versions >=0.2
666 *
667 * Probe based on PSCI PSCI_VERSION function
668 */
psci_0_2_init(const struct device_node * np)669 static int __init psci_0_2_init(const struct device_node *np)
670 {
671 int err;
672
673 err = get_set_conduit_method(np);
674 if (err)
675 return err;
676
677 /*
678 * Starting with v0.2, the PSCI specification introduced a call
679 * (PSCI_VERSION) that allows probing the firmware version, so
680 * that PSCI function IDs and version specific initialization
681 * can be carried out according to the specific version reported
682 * by firmware
683 */
684 return psci_probe();
685 }
686
687 /*
688 * PSCI < v0.2 get PSCI Function IDs via DT.
689 */
psci_0_1_init(const struct device_node * np)690 static int __init psci_0_1_init(const struct device_node *np)
691 {
692 u32 id;
693 int err;
694
695 err = get_set_conduit_method(np);
696 if (err)
697 return err;
698
699 pr_info("Using PSCI v0.1 Function IDs from DT\n");
700
701 psci_ops.get_version = psci_0_1_get_version;
702
703 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
704 psci_0_1_function_ids.cpu_suspend = id;
705 psci_ops.cpu_suspend = psci_0_1_cpu_suspend;
706 }
707
708 if (!of_property_read_u32(np, "cpu_off", &id)) {
709 psci_0_1_function_ids.cpu_off = id;
710 psci_ops.cpu_off = psci_0_1_cpu_off;
711 }
712
713 if (!of_property_read_u32(np, "cpu_on", &id)) {
714 psci_0_1_function_ids.cpu_on = id;
715 psci_ops.cpu_on = psci_0_1_cpu_on;
716 }
717
718 if (!of_property_read_u32(np, "migrate", &id)) {
719 psci_0_1_function_ids.migrate = id;
720 psci_ops.migrate = psci_0_1_migrate;
721 }
722
723 return 0;
724 }
725
psci_1_0_init(const struct device_node * np)726 static int __init psci_1_0_init(const struct device_node *np)
727 {
728 int err;
729
730 err = psci_0_2_init(np);
731 if (err)
732 return err;
733
734 if (psci_has_osi_support()) {
735 pr_info("OSI mode supported.\n");
736
737 /* Default to PC mode. */
738 psci_set_osi_mode(false);
739 }
740
741 return 0;
742 }
743
744 static const struct of_device_id psci_of_match[] __initconst = {
745 { .compatible = "arm,psci", .data = psci_0_1_init},
746 { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
747 { .compatible = "arm,psci-1.0", .data = psci_1_0_init},
748 {},
749 };
750
psci_dt_init(void)751 int __init psci_dt_init(void)
752 {
753 struct device_node *np;
754 const struct of_device_id *matched_np;
755 psci_initcall_t init_fn;
756 int ret;
757
758 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
759
760 if (!np || !of_device_is_available(np))
761 return -ENODEV;
762
763 init_fn = (psci_initcall_t)matched_np->data;
764 ret = init_fn(np);
765
766 of_node_put(np);
767 return ret;
768 }
769
770 #ifdef CONFIG_ACPI
771 /*
772 * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
773 * explicitly clarified in SBBR
774 */
psci_acpi_init(void)775 int __init psci_acpi_init(void)
776 {
777 if (!acpi_psci_present()) {
778 pr_info("is not implemented in ACPI.\n");
779 return -EOPNOTSUPP;
780 }
781
782 pr_info("probing for conduit method from ACPI.\n");
783
784 if (acpi_psci_use_hvc())
785 set_conduit(SMCCC_CONDUIT_HVC);
786 else
787 set_conduit(SMCCC_CONDUIT_SMC);
788
789 return psci_probe();
790 }
791 #endif
792