arm64: psci: remove ACPI coupling
[linux-2.6-microblaze.git] / arch / arm64 / kernel / 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) 2013 ARM Limited
12  *
13  * Author: Will Deacon <will.deacon@arm.com>
14  */
15
16 #define pr_fmt(fmt) "psci: " fmt
17
18 #include <linux/init.h>
19 #include <linux/of.h>
20 #include <linux/smp.h>
21 #include <linux/reboot.h>
22 #include <linux/pm.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <uapi/linux/psci.h>
26
27 #include <asm/compiler.h>
28 #include <asm/cputype.h>
29 #include <asm/cpu_ops.h>
30 #include <asm/errno.h>
31 #include <asm/psci.h>
32 #include <asm/smp_plat.h>
33 #include <asm/suspend.h>
34 #include <asm/system_misc.h>
35
36 #define PSCI_POWER_STATE_TYPE_STANDBY           0
37 #define PSCI_POWER_STATE_TYPE_POWER_DOWN        1
38
39 static bool psci_power_state_loses_context(u32 state)
40 {
41         return state & PSCI_0_2_POWER_STATE_TYPE_MASK;
42 }
43
44 static bool psci_power_state_is_valid(u32 state)
45 {
46         const u32 valid_mask = PSCI_0_2_POWER_STATE_ID_MASK |
47                                PSCI_0_2_POWER_STATE_TYPE_MASK |
48                                PSCI_0_2_POWER_STATE_AFFL_MASK;
49
50         return !(state & ~valid_mask);
51 }
52
53 /*
54  * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
55  * calls to its resident CPU, so we must avoid issuing those. We never migrate
56  * a Trusted OS even if it claims to be capable of migration -- doing so will
57  * require cooperation with a Trusted OS driver.
58  */
59 static int resident_cpu = -1;
60
61 static bool psci_tos_resident_on(int cpu)
62 {
63         return cpu == resident_cpu;
64 }
65
66 struct psci_operations {
67         int (*cpu_suspend)(u32 state, unsigned long entry_point);
68         int (*cpu_off)(u32 state);
69         int (*cpu_on)(unsigned long cpuid, unsigned long entry_point);
70         int (*migrate)(unsigned long cpuid);
71         int (*affinity_info)(unsigned long target_affinity,
72                         unsigned long lowest_affinity_level);
73         int (*migrate_info_type)(void);
74 };
75
76 static struct psci_operations psci_ops;
77
78 typedef unsigned long (psci_fn)(unsigned long, unsigned long,
79                                 unsigned long, unsigned long);
80 asmlinkage psci_fn __invoke_psci_fn_hvc;
81 asmlinkage psci_fn __invoke_psci_fn_smc;
82 static psci_fn *invoke_psci_fn;
83
84 enum psci_function {
85         PSCI_FN_CPU_SUSPEND,
86         PSCI_FN_CPU_ON,
87         PSCI_FN_CPU_OFF,
88         PSCI_FN_MIGRATE,
89         PSCI_FN_MAX,
90 };
91
92 static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state);
93
94 static u32 psci_function_id[PSCI_FN_MAX];
95
96 static int psci_to_linux_errno(int errno)
97 {
98         switch (errno) {
99         case PSCI_RET_SUCCESS:
100                 return 0;
101         case PSCI_RET_NOT_SUPPORTED:
102                 return -EOPNOTSUPP;
103         case PSCI_RET_INVALID_PARAMS:
104                 return -EINVAL;
105         case PSCI_RET_DENIED:
106                 return -EPERM;
107         };
108
109         return -EINVAL;
110 }
111
112 static u32 psci_get_version(void)
113 {
114         return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
115 }
116
117 static int psci_cpu_suspend(u32 state, unsigned long entry_point)
118 {
119         int err;
120         u32 fn;
121
122         fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
123         err = invoke_psci_fn(fn, state, entry_point, 0);
124         return psci_to_linux_errno(err);
125 }
126
127 static int psci_cpu_off(u32 state)
128 {
129         int err;
130         u32 fn;
131
132         fn = psci_function_id[PSCI_FN_CPU_OFF];
133         err = invoke_psci_fn(fn, state, 0, 0);
134         return psci_to_linux_errno(err);
135 }
136
137 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
138 {
139         int err;
140         u32 fn;
141
142         fn = psci_function_id[PSCI_FN_CPU_ON];
143         err = invoke_psci_fn(fn, cpuid, entry_point, 0);
144         return psci_to_linux_errno(err);
145 }
146
147 static int psci_migrate(unsigned long cpuid)
148 {
149         int err;
150         u32 fn;
151
152         fn = psci_function_id[PSCI_FN_MIGRATE];
153         err = invoke_psci_fn(fn, cpuid, 0, 0);
154         return psci_to_linux_errno(err);
155 }
156
157 static int psci_affinity_info(unsigned long target_affinity,
158                 unsigned long lowest_affinity_level)
159 {
160         return invoke_psci_fn(PSCI_0_2_FN64_AFFINITY_INFO, target_affinity,
161                               lowest_affinity_level, 0);
162 }
163
164 static int psci_migrate_info_type(void)
165 {
166         return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
167 }
168
169 static unsigned long psci_migrate_info_up_cpu(void)
170 {
171         return invoke_psci_fn(PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU, 0, 0, 0);
172 }
173
174 static int __maybe_unused cpu_psci_cpu_init_idle(unsigned int cpu)
175 {
176         int i, ret, count = 0;
177         u32 *psci_states;
178         struct device_node *state_node, *cpu_node;
179
180         cpu_node = of_get_cpu_node(cpu, NULL);
181         if (!cpu_node)
182                 return -ENODEV;
183
184         /*
185          * If the PSCI cpu_suspend function hook has not been initialized
186          * idle states must not be enabled, so bail out
187          */
188         if (!psci_ops.cpu_suspend)
189                 return -EOPNOTSUPP;
190
191         /* Count idle states */
192         while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
193                                               count))) {
194                 count++;
195                 of_node_put(state_node);
196         }
197
198         if (!count)
199                 return -ENODEV;
200
201         psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
202         if (!psci_states)
203                 return -ENOMEM;
204
205         for (i = 0; i < count; i++) {
206                 u32 state;
207
208                 state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
209
210                 ret = of_property_read_u32(state_node,
211                                            "arm,psci-suspend-param",
212                                            &state);
213                 if (ret) {
214                         pr_warn(" * %s missing arm,psci-suspend-param property\n",
215                                 state_node->full_name);
216                         of_node_put(state_node);
217                         goto free_mem;
218                 }
219
220                 of_node_put(state_node);
221                 pr_debug("psci-power-state %#x index %d\n", state, i);
222                 if (!psci_power_state_is_valid(state)) {
223                         pr_warn("Invalid PSCI power state %#x\n", state);
224                         ret = -EINVAL;
225                         goto free_mem;
226                 }
227                 psci_states[i] = state;
228         }
229         /* Idle states parsed correctly, initialize per-cpu pointer */
230         per_cpu(psci_power_state, cpu) = psci_states;
231         return 0;
232
233 free_mem:
234         kfree(psci_states);
235         return ret;
236 }
237
238 static int get_set_conduit_method(struct device_node *np)
239 {
240         const char *method;
241
242         pr_info("probing for conduit method from DT.\n");
243
244         if (of_property_read_string(np, "method", &method)) {
245                 pr_warn("missing \"method\" property\n");
246                 return -ENXIO;
247         }
248
249         if (!strcmp("hvc", method)) {
250                 invoke_psci_fn = __invoke_psci_fn_hvc;
251         } else if (!strcmp("smc", method)) {
252                 invoke_psci_fn = __invoke_psci_fn_smc;
253         } else {
254                 pr_warn("invalid \"method\" property: %s\n", method);
255                 return -EINVAL;
256         }
257         return 0;
258 }
259
260 static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
261 {
262         invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
263 }
264
265 static void psci_sys_poweroff(void)
266 {
267         invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
268 }
269
270 /*
271  * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
272  * return DENIED (which would be fatal).
273  */
274 static void __init psci_init_migrate(void)
275 {
276         unsigned long cpuid;
277         int type, cpu;
278
279         type = psci_ops.migrate_info_type();
280
281         if (type == PSCI_0_2_TOS_MP) {
282                 pr_info("Trusted OS migration not required\n");
283                 return;
284         }
285
286         if (type == PSCI_RET_NOT_SUPPORTED) {
287                 pr_info("MIGRATE_INFO_TYPE not supported.\n");
288                 return;
289         }
290
291         if (type != PSCI_0_2_TOS_UP_MIGRATE &&
292             type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
293                 pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
294                 return;
295         }
296
297         cpuid = psci_migrate_info_up_cpu();
298         if (cpuid & ~MPIDR_HWID_BITMASK) {
299                 pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
300                         cpuid);
301                 return;
302         }
303
304         cpu = get_logical_index(cpuid);
305         resident_cpu = cpu >= 0 ? cpu : -1;
306
307         pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
308 }
309
310 static void __init psci_0_2_set_functions(void)
311 {
312         pr_info("Using standard PSCI v0.2 function IDs\n");
313         psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN64_CPU_SUSPEND;
314         psci_ops.cpu_suspend = psci_cpu_suspend;
315
316         psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
317         psci_ops.cpu_off = psci_cpu_off;
318
319         psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN64_CPU_ON;
320         psci_ops.cpu_on = psci_cpu_on;
321
322         psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN64_MIGRATE;
323         psci_ops.migrate = psci_migrate;
324
325         psci_ops.affinity_info = psci_affinity_info;
326
327         psci_ops.migrate_info_type = psci_migrate_info_type;
328
329         arm_pm_restart = psci_sys_reset;
330
331         pm_power_off = psci_sys_poweroff;
332 }
333
334 /*
335  * Probe function for PSCI firmware versions >= 0.2
336  */
337 static int __init psci_probe(void)
338 {
339         u32 ver = psci_get_version();
340
341         pr_info("PSCIv%d.%d detected in firmware.\n",
342                         PSCI_VERSION_MAJOR(ver),
343                         PSCI_VERSION_MINOR(ver));
344
345         if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
346                 pr_err("Conflicting PSCI version detected.\n");
347                 return -EINVAL;
348         }
349
350         psci_0_2_set_functions();
351
352         psci_init_migrate();
353
354         return 0;
355 }
356
357 typedef int (*psci_initcall_t)(const struct device_node *);
358
359 /*
360  * PSCI init function for PSCI versions >=0.2
361  *
362  * Probe based on PSCI PSCI_VERSION function
363  */
364 static int __init psci_0_2_init(struct device_node *np)
365 {
366         int err;
367
368         err = get_set_conduit_method(np);
369
370         if (err)
371                 goto out_put_node;
372         /*
373          * Starting with v0.2, the PSCI specification introduced a call
374          * (PSCI_VERSION) that allows probing the firmware version, so
375          * that PSCI function IDs and version specific initialization
376          * can be carried out according to the specific version reported
377          * by firmware
378          */
379         err = psci_probe();
380
381 out_put_node:
382         of_node_put(np);
383         return err;
384 }
385
386 /*
387  * PSCI < v0.2 get PSCI Function IDs via DT.
388  */
389 static int __init psci_0_1_init(struct device_node *np)
390 {
391         u32 id;
392         int err;
393
394         err = get_set_conduit_method(np);
395
396         if (err)
397                 goto out_put_node;
398
399         pr_info("Using PSCI v0.1 Function IDs from DT\n");
400
401         if (!of_property_read_u32(np, "cpu_suspend", &id)) {
402                 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
403                 psci_ops.cpu_suspend = psci_cpu_suspend;
404         }
405
406         if (!of_property_read_u32(np, "cpu_off", &id)) {
407                 psci_function_id[PSCI_FN_CPU_OFF] = id;
408                 psci_ops.cpu_off = psci_cpu_off;
409         }
410
411         if (!of_property_read_u32(np, "cpu_on", &id)) {
412                 psci_function_id[PSCI_FN_CPU_ON] = id;
413                 psci_ops.cpu_on = psci_cpu_on;
414         }
415
416         if (!of_property_read_u32(np, "migrate", &id)) {
417                 psci_function_id[PSCI_FN_MIGRATE] = id;
418                 psci_ops.migrate = psci_migrate;
419         }
420
421 out_put_node:
422         of_node_put(np);
423         return err;
424 }
425
426 static const struct of_device_id psci_of_match[] __initconst = {
427         { .compatible = "arm,psci",     .data = psci_0_1_init},
428         { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
429         {},
430 };
431
432 int __init psci_dt_init(void)
433 {
434         struct device_node *np;
435         const struct of_device_id *matched_np;
436         psci_initcall_t init_fn;
437
438         np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
439
440         if (!np)
441                 return -ENODEV;
442
443         init_fn = (psci_initcall_t)matched_np->data;
444         return init_fn(np);
445 }
446
447 #ifdef CONFIG_ACPI
448 /*
449  * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
450  * explicitly clarified in SBBR
451  */
452 int __init psci_acpi_init(void)
453 {
454         if (!acpi_psci_present()) {
455                 pr_info("is not implemented in ACPI.\n");
456                 return -EOPNOTSUPP;
457         }
458
459         pr_info("probing for conduit method from ACPI.\n");
460
461         if (acpi_psci_use_hvc())
462                 invoke_psci_fn = __invoke_psci_fn_hvc;
463         else
464                 invoke_psci_fn = __invoke_psci_fn_smc;
465
466         return psci_probe();
467 }
468 #endif
469
470 #ifdef CONFIG_SMP
471
472 static int __init cpu_psci_cpu_init(unsigned int cpu)
473 {
474         return 0;
475 }
476
477 static int __init cpu_psci_cpu_prepare(unsigned int cpu)
478 {
479         if (!psci_ops.cpu_on) {
480                 pr_err("no cpu_on method, not booting CPU%d\n", cpu);
481                 return -ENODEV;
482         }
483
484         return 0;
485 }
486
487 static int cpu_psci_cpu_boot(unsigned int cpu)
488 {
489         int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
490         if (err)
491                 pr_err("failed to boot CPU%d (%d)\n", cpu, err);
492
493         return err;
494 }
495
496 #ifdef CONFIG_HOTPLUG_CPU
497 static int cpu_psci_cpu_disable(unsigned int cpu)
498 {
499         /* Fail early if we don't have CPU_OFF support */
500         if (!psci_ops.cpu_off)
501                 return -EOPNOTSUPP;
502
503         /* Trusted OS will deny CPU_OFF */
504         if (psci_tos_resident_on(cpu))
505                 return -EPERM;
506
507         return 0;
508 }
509
510 static void cpu_psci_cpu_die(unsigned int cpu)
511 {
512         int ret;
513         /*
514          * There are no known implementations of PSCI actually using the
515          * power state field, pass a sensible default for now.
516          */
517         u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
518                     PSCI_0_2_POWER_STATE_TYPE_SHIFT;
519
520         ret = psci_ops.cpu_off(state);
521
522         pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
523 }
524
525 static int cpu_psci_cpu_kill(unsigned int cpu)
526 {
527         int err, i;
528
529         if (!psci_ops.affinity_info)
530                 return 0;
531         /*
532          * cpu_kill could race with cpu_die and we can
533          * potentially end up declaring this cpu undead
534          * while it is dying. So, try again a few times.
535          */
536
537         for (i = 0; i < 10; i++) {
538                 err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
539                 if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
540                         pr_info("CPU%d killed.\n", cpu);
541                         return 0;
542                 }
543
544                 msleep(10);
545                 pr_info("Retrying again to check for CPU kill\n");
546         }
547
548         pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
549                         cpu, err);
550         return -ETIMEDOUT;
551 }
552 #endif
553 #endif
554
555 static int psci_suspend_finisher(unsigned long index)
556 {
557         u32 *state = __this_cpu_read(psci_power_state);
558
559         return psci_ops.cpu_suspend(state[index - 1],
560                                     virt_to_phys(cpu_resume));
561 }
562
563 static int __maybe_unused cpu_psci_cpu_suspend(unsigned long index)
564 {
565         int ret;
566         u32 *state = __this_cpu_read(psci_power_state);
567         /*
568          * idle state index 0 corresponds to wfi, should never be called
569          * from the cpu_suspend operations
570          */
571         if (WARN_ON_ONCE(!index))
572                 return -EINVAL;
573
574         if (!psci_power_state_loses_context(state[index - 1]))
575                 ret = psci_ops.cpu_suspend(state[index - 1], 0);
576         else
577                 ret = __cpu_suspend(index, psci_suspend_finisher);
578
579         return ret;
580 }
581
582 const struct cpu_operations cpu_psci_ops = {
583         .name           = "psci",
584 #ifdef CONFIG_CPU_IDLE
585         .cpu_init_idle  = cpu_psci_cpu_init_idle,
586         .cpu_suspend    = cpu_psci_cpu_suspend,
587 #endif
588 #ifdef CONFIG_SMP
589         .cpu_init       = cpu_psci_cpu_init,
590         .cpu_prepare    = cpu_psci_cpu_prepare,
591         .cpu_boot       = cpu_psci_cpu_boot,
592 #ifdef CONFIG_HOTPLUG_CPU
593         .cpu_disable    = cpu_psci_cpu_disable,
594         .cpu_die        = cpu_psci_cpu_die,
595         .cpu_kill       = cpu_psci_cpu_kill,
596 #endif
597 #endif
598 };
599