OSDN Git Service

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