1
2
3
4
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
32
33
34
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
44
45
46
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
60typedef unsigned long (psci_fn)(unsigned long, unsigned long,
61 unsigned long, unsigned long);
62static psci_fn *invoke_psci_fn;
63
64enum psci_function {
65 PSCI_FN_CPU_SUSPEND,
66 PSCI_FN_CPU_ON,
67 PSCI_FN_CPU_OFF,
68 PSCI_FN_MIGRATE,
69 PSCI_FN_MAX,
70};
71
72static u32 psci_function_id[PSCI_FN_MAX];
73
74#define PSCI_0_2_POWER_STATE_MASK \
75 (PSCI_0_2_POWER_STATE_ID_MASK | \
76 PSCI_0_2_POWER_STATE_TYPE_MASK | \
77 PSCI_0_2_POWER_STATE_AFFL_MASK)
78
79#define PSCI_1_0_EXT_POWER_STATE_MASK \
80 (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
81 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
82
83static u32 psci_cpu_suspend_feature;
84static bool psci_system_reset2_supported;
85
86static inline bool psci_has_ext_power_state(void)
87{
88 return psci_cpu_suspend_feature &
89 PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
90}
91
92static inline bool psci_has_osi_support(void)
93{
94 return psci_cpu_suspend_feature & PSCI_1_0_OS_INITIATED;
95}
96
97static inline bool psci_power_state_loses_context(u32 state)
98{
99 const u32 mask = psci_has_ext_power_state() ?
100 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
101 PSCI_0_2_POWER_STATE_TYPE_MASK;
102
103 return state & mask;
104}
105
106static inline bool psci_power_state_is_valid(u32 state)
107{
108 const u32 valid_mask = psci_has_ext_power_state() ?
109 PSCI_1_0_EXT_POWER_STATE_MASK :
110 PSCI_0_2_POWER_STATE_MASK;
111
112 return !(state & ~valid_mask);
113}
114
115static unsigned long __invoke_psci_fn_hvc(unsigned long function_id,
116 unsigned long arg0, unsigned long arg1,
117 unsigned long arg2)
118{
119 struct arm_smccc_res res;
120
121 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
122 return res.a0;
123}
124
125static unsigned long __invoke_psci_fn_smc(unsigned long function_id,
126 unsigned long arg0, unsigned long arg1,
127 unsigned long arg2)
128{
129 struct arm_smccc_res res;
130
131 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
132 return res.a0;
133}
134
135static int psci_to_linux_errno(int errno)
136{
137 switch (errno) {
138 case PSCI_RET_SUCCESS:
139 return 0;
140 case PSCI_RET_NOT_SUPPORTED:
141 return -EOPNOTSUPP;
142 case PSCI_RET_INVALID_PARAMS:
143 case PSCI_RET_INVALID_ADDRESS:
144 return -EINVAL;
145 case PSCI_RET_DENIED:
146 return -EPERM;
147 };
148
149 return -EINVAL;
150}
151
152static u32 psci_get_version(void)
153{
154 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
155}
156
157static int psci_cpu_suspend(u32 state, unsigned long entry_point)
158{
159 int err;
160 u32 fn;
161
162 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
163 err = invoke_psci_fn(fn, state, entry_point, 0);
164 return psci_to_linux_errno(err);
165}
166
167static int psci_cpu_off(u32 state)
168{
169 int err;
170 u32 fn;
171
172 fn = psci_function_id[PSCI_FN_CPU_OFF];
173 err = invoke_psci_fn(fn, state, 0, 0);
174 return psci_to_linux_errno(err);
175}
176
177static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
178{
179 int err;
180 u32 fn;
181
182 fn = psci_function_id[PSCI_FN_CPU_ON];
183 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
184 return psci_to_linux_errno(err);
185}
186
187static int psci_migrate(unsigned long cpuid)
188{
189 int err;
190 u32 fn;
191
192 fn = psci_function_id[PSCI_FN_MIGRATE];
193 err = invoke_psci_fn(fn, cpuid, 0, 0);
194 return psci_to_linux_errno(err);
195}
196
197static int psci_affinity_info(unsigned long target_affinity,
198 unsigned long lowest_affinity_level)
199{
200 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
201 target_affinity, lowest_affinity_level, 0);
202}
203
204static int psci_migrate_info_type(void)
205{
206 return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
207}
208
209static unsigned long psci_migrate_info_up_cpu(void)
210{
211 return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
212 0, 0, 0);
213}
214
215static void set_conduit(enum psci_conduit conduit)
216{
217 switch (conduit) {
218 case PSCI_CONDUIT_HVC:
219 invoke_psci_fn = __invoke_psci_fn_hvc;
220 break;
221 case PSCI_CONDUIT_SMC:
222 invoke_psci_fn = __invoke_psci_fn_smc;
223 break;
224 default:
225 WARN(1, "Unexpected PSCI conduit %d\n", conduit);
226 }
227
228 psci_ops.conduit = conduit;
229}
230
231static int get_set_conduit_method(struct device_node *np)
232{
233 const char *method;
234
235 pr_info("probing for conduit method from DT.\n");
236
237 if (of_property_read_string(np, "method", &method)) {
238 pr_warn("missing \"method\" property\n");
239 return -ENXIO;
240 }
241
242 if (!strcmp("hvc", method)) {
243 set_conduit(PSCI_CONDUIT_HVC);
244 } else if (!strcmp("smc", method)) {
245 set_conduit(PSCI_CONDUIT_SMC);
246 } else {
247 pr_warn("invalid \"method\" property: %s\n", method);
248 return -EINVAL;
249 }
250 return 0;
251}
252
253static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
254{
255 if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) &&
256 psci_system_reset2_supported) {
257
258
259
260
261
262 invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
263 } else {
264 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
265 }
266}
267
268static void psci_sys_poweroff(void)
269{
270 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
271}
272
273static int __init psci_features(u32 psci_func_id)
274{
275 return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
276 psci_func_id, 0, 0);
277}
278
279#ifdef CONFIG_CPU_IDLE
280static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state);
281
282static int psci_dt_parse_state_node(struct device_node *np, u32 *state)
283{
284 int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
285
286 if (err) {
287 pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
288 return err;
289 }
290
291 if (!psci_power_state_is_valid(*state)) {
292 pr_warn("Invalid PSCI power state %#x\n", *state);
293 return -EINVAL;
294 }
295
296 return 0;
297}
298
299static int psci_dt_cpu_init_idle(struct device_node *cpu_node, int cpu)
300{
301 int i, ret = 0, count = 0;
302 u32 *psci_states;
303 struct device_node *state_node;
304
305
306 while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
307 count))) {
308 count++;
309 of_node_put(state_node);
310 }
311
312 if (!count)
313 return -ENODEV;
314
315 psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
316 if (!psci_states)
317 return -ENOMEM;
318
319 for (i = 0; i < count; i++) {
320 state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
321 ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
322 of_node_put(state_node);
323
324 if (ret)
325 goto free_mem;
326
327 pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
328 }
329
330
331 per_cpu(psci_power_state, cpu) = psci_states;
332 return 0;
333
334free_mem:
335 kfree(psci_states);
336 return ret;
337}
338
339#ifdef CONFIG_ACPI
340#include <acpi/processor.h>
341
342static int __maybe_unused psci_acpi_cpu_init_idle(unsigned int cpu)
343{
344 int i, count;
345 u32 *psci_states;
346 struct acpi_lpi_state *lpi;
347 struct acpi_processor *pr = per_cpu(processors, cpu);
348
349 if (unlikely(!pr || !pr->flags.has_lpi))
350 return -EINVAL;
351
352 count = pr->power.count - 1;
353 if (count <= 0)
354 return -ENODEV;
355
356 psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
357 if (!psci_states)
358 return -ENOMEM;
359
360 for (i = 0; i < count; i++) {
361 u32 state;
362
363 lpi = &pr->power.lpi_states[i + 1];
364
365
366
367
368 state = lpi->address;
369 if (!psci_power_state_is_valid(state)) {
370 pr_warn("Invalid PSCI power state %#x\n", state);
371 kfree(psci_states);
372 return -EINVAL;
373 }
374 psci_states[i] = state;
375 }
376
377 per_cpu(psci_power_state, cpu) = psci_states;
378 return 0;
379}
380#else
381static int __maybe_unused psci_acpi_cpu_init_idle(unsigned int cpu)
382{
383 return -EINVAL;
384}
385#endif
386
387int psci_cpu_init_idle(unsigned int cpu)
388{
389 struct device_node *cpu_node;
390 int ret;
391
392
393
394
395
396 if (!psci_ops.cpu_suspend)
397 return -EOPNOTSUPP;
398
399 if (!acpi_disabled)
400 return psci_acpi_cpu_init_idle(cpu);
401
402 cpu_node = of_get_cpu_node(cpu, NULL);
403 if (!cpu_node)
404 return -ENODEV;
405
406 ret = psci_dt_cpu_init_idle(cpu_node, cpu);
407
408 of_node_put(cpu_node);
409
410 return ret;
411}
412
413static int psci_suspend_finisher(unsigned long index)
414{
415 u32 *state = __this_cpu_read(psci_power_state);
416
417 return psci_ops.cpu_suspend(state[index - 1],
418 __pa_symbol(cpu_resume));
419}
420
421int psci_cpu_suspend_enter(unsigned long index)
422{
423 int ret;
424 u32 *state = __this_cpu_read(psci_power_state);
425
426
427
428
429 if (WARN_ON_ONCE(!index))
430 return -EINVAL;
431
432 if (!psci_power_state_loses_context(state[index - 1]))
433 ret = psci_ops.cpu_suspend(state[index - 1], 0);
434 else
435 ret = cpu_suspend(index, psci_suspend_finisher);
436
437 return ret;
438}
439
440
441#ifdef CONFIG_ARM
442static const struct cpuidle_ops psci_cpuidle_ops __initconst = {
443 .suspend = psci_cpu_suspend_enter,
444 .init = psci_dt_cpu_init_idle,
445};
446
447CPUIDLE_METHOD_OF_DECLARE(psci, "psci", &psci_cpuidle_ops);
448#endif
449#endif
450
451static int psci_system_suspend(unsigned long unused)
452{
453 return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
454 __pa_symbol(cpu_resume), 0, 0);
455}
456
457static int psci_system_suspend_enter(suspend_state_t state)
458{
459 return cpu_suspend(0, psci_system_suspend);
460}
461
462static const struct platform_suspend_ops psci_suspend_ops = {
463 .valid = suspend_valid_only_mem,
464 .enter = psci_system_suspend_enter,
465};
466
467static void __init psci_init_system_reset2(void)
468{
469 int ret;
470
471 ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2));
472
473 if (ret != PSCI_RET_NOT_SUPPORTED)
474 psci_system_reset2_supported = true;
475}
476
477static void __init psci_init_system_suspend(void)
478{
479 int ret;
480
481 if (!IS_ENABLED(CONFIG_SUSPEND))
482 return;
483
484 ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
485
486 if (ret != PSCI_RET_NOT_SUPPORTED)
487 suspend_set_ops(&psci_suspend_ops);
488}
489
490static void __init psci_init_cpu_suspend(void)
491{
492 int feature = psci_features(psci_function_id[PSCI_FN_CPU_SUSPEND]);
493
494 if (feature != PSCI_RET_NOT_SUPPORTED)
495 psci_cpu_suspend_feature = feature;
496}
497
498
499
500
501
502static void __init psci_init_migrate(void)
503{
504 unsigned long cpuid;
505 int type, cpu = -1;
506
507 type = psci_ops.migrate_info_type();
508
509 if (type == PSCI_0_2_TOS_MP) {
510 pr_info("Trusted OS migration not required\n");
511 return;
512 }
513
514 if (type == PSCI_RET_NOT_SUPPORTED) {
515 pr_info("MIGRATE_INFO_TYPE not supported.\n");
516 return;
517 }
518
519 if (type != PSCI_0_2_TOS_UP_MIGRATE &&
520 type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
521 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
522 return;
523 }
524
525 cpuid = psci_migrate_info_up_cpu();
526 if (cpuid & ~MPIDR_HWID_BITMASK) {
527 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
528 cpuid);
529 return;
530 }
531
532 cpu = get_logical_index(cpuid);
533 resident_cpu = cpu >= 0 ? cpu : -1;
534
535 pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
536}
537
538static void __init psci_init_smccc(void)
539{
540 u32 ver = ARM_SMCCC_VERSION_1_0;
541 int feature;
542
543 feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
544
545 if (feature != PSCI_RET_NOT_SUPPORTED) {
546 u32 ret;
547 ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
548 if (ret == ARM_SMCCC_VERSION_1_1) {
549 psci_ops.smccc_version = SMCCC_VERSION_1_1;
550 ver = ret;
551 }
552 }
553
554
555
556
557
558 pr_info("SMC Calling Convention v%d.%d\n",
559 PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
560
561}
562
563static void __init psci_0_2_set_functions(void)
564{
565 pr_info("Using standard PSCI v0.2 function IDs\n");
566 psci_ops.get_version = psci_get_version;
567
568 psci_function_id[PSCI_FN_CPU_SUSPEND] =
569 PSCI_FN_NATIVE(0_2, CPU_SUSPEND);
570 psci_ops.cpu_suspend = psci_cpu_suspend;
571
572 psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
573 psci_ops.cpu_off = psci_cpu_off;
574
575 psci_function_id[PSCI_FN_CPU_ON] = PSCI_FN_NATIVE(0_2, CPU_ON);
576 psci_ops.cpu_on = psci_cpu_on;
577
578 psci_function_id[PSCI_FN_MIGRATE] = PSCI_FN_NATIVE(0_2, MIGRATE);
579 psci_ops.migrate = psci_migrate;
580
581 psci_ops.affinity_info = psci_affinity_info;
582
583 psci_ops.migrate_info_type = psci_migrate_info_type;
584
585 arm_pm_restart = psci_sys_reset;
586
587 pm_power_off = psci_sys_poweroff;
588}
589
590
591
592
593static int __init psci_probe(void)
594{
595 u32 ver = psci_get_version();
596
597 pr_info("PSCIv%d.%d detected in firmware.\n",
598 PSCI_VERSION_MAJOR(ver),
599 PSCI_VERSION_MINOR(ver));
600
601 if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
602 pr_err("Conflicting PSCI version detected.\n");
603 return -EINVAL;
604 }
605
606 psci_0_2_set_functions();
607
608 psci_init_migrate();
609
610 if (PSCI_VERSION_MAJOR(ver) >= 1) {
611 psci_init_smccc();
612 psci_init_cpu_suspend();
613 psci_init_system_suspend();
614 psci_init_system_reset2();
615 }
616
617 return 0;
618}
619
620typedef int (*psci_initcall_t)(const struct device_node *);
621
622
623
624
625
626
627static int __init psci_0_2_init(struct device_node *np)
628{
629 int err;
630
631 err = get_set_conduit_method(np);
632 if (err)
633 return err;
634
635
636
637
638
639
640
641
642 return psci_probe();
643}
644
645
646
647
648static int __init psci_0_1_init(struct device_node *np)
649{
650 u32 id;
651 int err;
652
653 err = get_set_conduit_method(np);
654 if (err)
655 return err;
656
657 pr_info("Using PSCI v0.1 Function IDs from DT\n");
658
659 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
660 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
661 psci_ops.cpu_suspend = psci_cpu_suspend;
662 }
663
664 if (!of_property_read_u32(np, "cpu_off", &id)) {
665 psci_function_id[PSCI_FN_CPU_OFF] = id;
666 psci_ops.cpu_off = psci_cpu_off;
667 }
668
669 if (!of_property_read_u32(np, "cpu_on", &id)) {
670 psci_function_id[PSCI_FN_CPU_ON] = id;
671 psci_ops.cpu_on = psci_cpu_on;
672 }
673
674 if (!of_property_read_u32(np, "migrate", &id)) {
675 psci_function_id[PSCI_FN_MIGRATE] = id;
676 psci_ops.migrate = psci_migrate;
677 }
678
679 return 0;
680}
681
682static int __init psci_1_0_init(struct device_node *np)
683{
684 int err;
685
686 err = psci_0_2_init(np);
687 if (err)
688 return err;
689
690 if (psci_has_osi_support())
691 pr_info("OSI mode supported.\n");
692
693 return 0;
694}
695
696static const struct of_device_id psci_of_match[] __initconst = {
697 { .compatible = "arm,psci", .data = psci_0_1_init},
698 { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
699 { .compatible = "arm,psci-1.0", .data = psci_1_0_init},
700 {},
701};
702
703int __init psci_dt_init(void)
704{
705 struct device_node *np;
706 const struct of_device_id *matched_np;
707 psci_initcall_t init_fn;
708 int ret;
709
710 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
711
712 if (!np || !of_device_is_available(np))
713 return -ENODEV;
714
715 init_fn = (psci_initcall_t)matched_np->data;
716 ret = init_fn(np);
717
718 of_node_put(np);
719 return ret;
720}
721
722#ifdef CONFIG_ACPI
723
724
725
726
727int __init psci_acpi_init(void)
728{
729 if (!acpi_psci_present()) {
730 pr_info("is not implemented in ACPI.\n");
731 return -EOPNOTSUPP;
732 }
733
734 pr_info("probing for conduit method from ACPI.\n");
735
736 if (acpi_psci_use_hvc())
737 set_conduit(PSCI_CONDUIT_HVC);
738 else
739 set_conduit(PSCI_CONDUIT_SMC);
740
741 return psci_probe();
742}
743#endif
744