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