Merge branch 'linux-linaro-lsk-v3.10' into linux-linaro-lsk-v3.10-android
[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 <uapi/linux/psci.h>
27
28 #include <asm/compiler.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 struct psci_power_state {
40         u16     id;
41         u8      type;
42         u8      affinity_level;
43 };
44
45 struct psci_operations {
46         int (*cpu_suspend)(struct psci_power_state state,
47                            unsigned long entry_point);
48         int (*cpu_off)(struct psci_power_state state);
49         int (*cpu_on)(unsigned long cpuid, unsigned long entry_point);
50         int (*migrate)(unsigned long cpuid);
51         int (*affinity_info)(unsigned long target_affinity,
52                         unsigned long lowest_affinity_level);
53         int (*migrate_info_type)(void);
54 };
55
56 static struct psci_operations psci_ops;
57
58 static int (*invoke_psci_fn)(u64, u64, u64, u64);
59 typedef int (*psci_initcall_t)(const struct device_node *);
60
61 asmlinkage int __invoke_psci_fn_hvc(u64, u64, u64, u64);
62 asmlinkage int __invoke_psci_fn_smc(u64, u64, u64, u64);
63
64 enum psci_function {
65         PSCI_FN_CPU_SUSPEND,
66         PSCI_FN_CPU_ON,
67         PSCI_FN_CPU_OFF,
68         PSCI_FN_MIGRATE,
69         PSCI_FN_AFFINITY_INFO,
70         PSCI_FN_MIGRATE_INFO_TYPE,
71         PSCI_FN_MAX,
72 };
73
74 static DEFINE_PER_CPU_READ_MOSTLY(struct psci_power_state *, psci_power_state);
75
76 static u32 psci_function_id[PSCI_FN_MAX];
77
78 static int psci_to_linux_errno(int errno)
79 {
80         switch (errno) {
81         case PSCI_RET_SUCCESS:
82                 return 0;
83         case PSCI_RET_NOT_SUPPORTED:
84                 return -EOPNOTSUPP;
85         case PSCI_RET_INVALID_PARAMS:
86                 return -EINVAL;
87         case PSCI_RET_DENIED:
88                 return -EPERM;
89         };
90
91         return -EINVAL;
92 }
93
94 static u32 psci_power_state_pack(struct psci_power_state state)
95 {
96         return ((state.id << PSCI_0_2_POWER_STATE_ID_SHIFT)
97                         & PSCI_0_2_POWER_STATE_ID_MASK) |
98                 ((state.type << PSCI_0_2_POWER_STATE_TYPE_SHIFT)
99                  & PSCI_0_2_POWER_STATE_TYPE_MASK) |
100                 ((state.affinity_level << PSCI_0_2_POWER_STATE_AFFL_SHIFT)
101                  & PSCI_0_2_POWER_STATE_AFFL_MASK);
102 }
103
104 static void psci_power_state_unpack(u32 power_state,
105                                     struct psci_power_state *state)
106 {
107         state->id = (power_state >> PSCI_0_2_POWER_STATE_ID_SHIFT)
108                         & PSCI_0_2_POWER_STATE_ID_MASK;
109         state->type = (power_state >> PSCI_0_2_POWER_STATE_TYPE_SHIFT)
110                         & PSCI_0_2_POWER_STATE_TYPE_MASK;
111         state->affinity_level = (power_state >> PSCI_0_2_POWER_STATE_AFFL_SHIFT)
112                         & PSCI_0_2_POWER_STATE_AFFL_MASK;
113 }
114
115 static int psci_get_version(void)
116 {
117         int err;
118
119         err = invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
120         return err;
121 }
122
123 static int psci_cpu_suspend(struct psci_power_state state,
124                             unsigned long entry_point)
125 {
126         int err;
127         u32 fn, power_state;
128
129         fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
130         power_state = psci_power_state_pack(state);
131         err = invoke_psci_fn(fn, power_state, entry_point, 0);
132         return psci_to_linux_errno(err);
133 }
134
135 static int psci_cpu_off(struct psci_power_state state)
136 {
137         int err;
138         u32 fn, power_state;
139
140         fn = psci_function_id[PSCI_FN_CPU_OFF];
141         power_state = psci_power_state_pack(state);
142         err = invoke_psci_fn(fn, power_state, 0, 0);
143         return psci_to_linux_errno(err);
144 }
145
146 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
147 {
148         int err;
149         u32 fn;
150
151         fn = psci_function_id[PSCI_FN_CPU_ON];
152         err = invoke_psci_fn(fn, cpuid, entry_point, 0);
153         return psci_to_linux_errno(err);
154 }
155
156 static int psci_migrate(unsigned long cpuid)
157 {
158         int err;
159         u32 fn;
160
161         fn = psci_function_id[PSCI_FN_MIGRATE];
162         err = invoke_psci_fn(fn, cpuid, 0, 0);
163         return psci_to_linux_errno(err);
164 }
165
166 static int psci_affinity_info(unsigned long target_affinity,
167                 unsigned long lowest_affinity_level)
168 {
169         int err;
170         u32 fn;
171
172         fn = psci_function_id[PSCI_FN_AFFINITY_INFO];
173         err = invoke_psci_fn(fn, target_affinity, lowest_affinity_level, 0);
174         return err;
175 }
176
177 static int psci_migrate_info_type(void)
178 {
179         int err;
180         u32 fn;
181
182         fn = psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE];
183         err = invoke_psci_fn(fn, 0, 0, 0);
184         return err;
185 }
186
187 static int get_set_conduit_method(struct device_node *np)
188 {
189         const char *method;
190
191         pr_info("probing for conduit method from DT.\n");
192
193         if (of_property_read_string(np, "method", &method)) {
194                 pr_warn("missing \"method\" property\n");
195                 return -ENXIO;
196         }
197
198         if (!strcmp("hvc", method)) {
199                 invoke_psci_fn = __invoke_psci_fn_hvc;
200         } else if (!strcmp("smc", method)) {
201                 invoke_psci_fn = __invoke_psci_fn_smc;
202         } else {
203                 pr_warn("invalid \"method\" property: %s\n", method);
204                 return -EINVAL;
205         }
206         return 0;
207 }
208
209 static void psci_sys_reset(char str, const char *cmd)
210 {
211         invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
212 }
213
214 static void psci_sys_poweroff(void)
215 {
216         invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
217 }
218
219 /*
220  * PSCI Function IDs for v0.2+ are well defined so use
221  * standard values.
222  */
223 static int __init psci_0_2_init(struct device_node *np)
224 {
225         int err, ver;
226
227         err = get_set_conduit_method(np);
228
229         if (err)
230                 goto out_put_node;
231
232         ver = psci_get_version();
233
234         if (ver == PSCI_RET_NOT_SUPPORTED) {
235                 /* PSCI v0.2 mandates implementation of PSCI_ID_VERSION. */
236                 pr_err("PSCI firmware does not comply with the v0.2 spec.\n");
237                 err = -EOPNOTSUPP;
238                 goto out_put_node;
239         } else {
240                 pr_info("PSCIv%d.%d detected in firmware.\n",
241                                 PSCI_VERSION_MAJOR(ver),
242                                 PSCI_VERSION_MINOR(ver));
243
244                 if (PSCI_VERSION_MAJOR(ver) == 0 &&
245                                 PSCI_VERSION_MINOR(ver) < 2) {
246                         err = -EINVAL;
247                         pr_err("Conflicting PSCI version detected.\n");
248                         goto out_put_node;
249                 }
250         }
251
252         pr_info("Using standard PSCI v0.2 function IDs\n");
253         psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN64_CPU_SUSPEND;
254         psci_ops.cpu_suspend = psci_cpu_suspend;
255
256         psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
257         psci_ops.cpu_off = psci_cpu_off;
258
259         psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN64_CPU_ON;
260         psci_ops.cpu_on = psci_cpu_on;
261
262         psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN64_MIGRATE;
263         psci_ops.migrate = psci_migrate;
264
265         psci_function_id[PSCI_FN_AFFINITY_INFO] = PSCI_0_2_FN64_AFFINITY_INFO;
266         psci_ops.affinity_info = psci_affinity_info;
267
268         psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE] =
269                 PSCI_0_2_FN_MIGRATE_INFO_TYPE;
270         psci_ops.migrate_info_type = psci_migrate_info_type;
271
272         arm_pm_restart = psci_sys_reset;
273
274         pm_power_off = psci_sys_poweroff;
275
276 out_put_node:
277         of_node_put(np);
278         return err;
279 }
280
281 /*
282  * PSCI < v0.2 get PSCI Function IDs via DT.
283  */
284 static int __init psci_0_1_init(struct device_node *np)
285 {
286         u32 id;
287         int err;
288
289         err = get_set_conduit_method(np);
290
291         if (err)
292                 goto out_put_node;
293
294         pr_info("Using PSCI v0.1 Function IDs from DT\n");
295
296         if (!of_property_read_u32(np, "cpu_suspend", &id)) {
297                 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
298                 psci_ops.cpu_suspend = psci_cpu_suspend;
299         }
300
301         if (!of_property_read_u32(np, "cpu_off", &id)) {
302                 psci_function_id[PSCI_FN_CPU_OFF] = id;
303                 psci_ops.cpu_off = psci_cpu_off;
304         }
305
306         if (!of_property_read_u32(np, "cpu_on", &id)) {
307                 psci_function_id[PSCI_FN_CPU_ON] = id;
308                 psci_ops.cpu_on = psci_cpu_on;
309         }
310
311         if (!of_property_read_u32(np, "migrate", &id)) {
312                 psci_function_id[PSCI_FN_MIGRATE] = id;
313                 psci_ops.migrate = psci_migrate;
314         }
315
316 out_put_node:
317         of_node_put(np);
318         return err;
319 }
320
321 static const struct of_device_id psci_of_match[] __initconst = {
322         { .compatible = "arm,psci",     .data = psci_0_1_init},
323         { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
324         {},
325 };
326
327 int __init psci_init(void)
328 {
329         struct device_node *np;
330         const struct of_device_id *matched_np;
331         psci_initcall_t init_fn;
332
333         np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
334
335         if (!np)
336                 return -ENODEV;
337
338         init_fn = (psci_initcall_t)matched_np->data;
339         return init_fn(np);
340 }
341
342 #ifdef CONFIG_SMP
343
344 static int __init cpu_psci_cpu_init(struct device_node *dn, unsigned int cpu)
345 {
346         return 0;
347 }
348
349 static int __init cpu_psci_cpu_prepare(unsigned int cpu)
350 {
351         if (!psci_ops.cpu_on) {
352                 pr_err("no cpu_on method, not booting CPU%d\n", cpu);
353                 return -ENODEV;
354         }
355
356         return 0;
357 }
358
359 static int cpu_psci_cpu_boot(unsigned int cpu)
360 {
361         int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
362         if (err)
363                 pr_err("failed to boot CPU%d (%d)\n", cpu, err);
364
365         return err;
366 }
367
368 #ifdef CONFIG_HOTPLUG_CPU
369 static int cpu_psci_cpu_disable(unsigned int cpu)
370 {
371         /* Fail early if we don't have CPU_OFF support */
372         if (!psci_ops.cpu_off)
373                 return -EOPNOTSUPP;
374         return 0;
375 }
376
377 static void cpu_psci_cpu_die(unsigned int cpu)
378 {
379         int ret;
380         /*
381          * There are no known implementations of PSCI actually using the
382          * power state field, pass a sensible default for now.
383          */
384         struct psci_power_state state = {
385                 .type = PSCI_POWER_STATE_TYPE_POWER_DOWN,
386         };
387
388         ret = psci_ops.cpu_off(state);
389
390         pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
391 }
392
393 static int cpu_psci_cpu_kill(unsigned int cpu)
394 {
395         int err, i;
396
397         if (!psci_ops.affinity_info)
398                 return 1;
399         /*
400          * cpu_kill could race with cpu_die and we can
401          * potentially end up declaring this cpu undead
402          * while it is dying. So, try again a few times.
403          */
404
405         for (i = 0; i < 10; i++) {
406                 err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
407                 if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
408                         pr_info("CPU%d killed.\n", cpu);
409                         return 1;
410                 }
411
412                 msleep(10);
413                 pr_info("Retrying again to check for CPU kill\n");
414         }
415
416         pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
417                         cpu, err);
418         /* Make op_cpu_kill() fail. */
419         return 0;
420 }
421 #endif
422 #endif
423
424 #ifdef CONFIG_ARM64_CPU_SUSPEND
425 static int cpu_psci_cpu_suspend(unsigned long index)
426 {
427         struct psci_power_state *state = __get_cpu_var(psci_power_state);
428
429         if (!state)
430                 return -EOPNOTSUPP;
431
432         return psci_ops.cpu_suspend(state[index], virt_to_phys(cpu_resume));
433 }
434 #endif
435
436 const struct cpu_operations cpu_psci_ops = {
437         .name           = "psci",
438 #ifdef CONFIG_SMP
439         .cpu_init       = cpu_psci_cpu_init,
440         .cpu_prepare    = cpu_psci_cpu_prepare,
441         .cpu_boot       = cpu_psci_cpu_boot,
442 #ifdef CONFIG_HOTPLUG_CPU
443         .cpu_disable    = cpu_psci_cpu_disable,
444         .cpu_die        = cpu_psci_cpu_die,
445         .cpu_kill       = cpu_psci_cpu_kill,
446 #endif
447 #endif
448 #ifdef CONFIG_ARM64_CPU_SUSPEND
449         .cpu_suspend    = cpu_psci_cpu_suspend,
450 #endif
451 };
452