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