diff options
author | Mark Rutland <mark.rutland@arm.com> | 2015-07-31 10:46:16 -0400 |
---|---|---|
committer | Will Deacon <will.deacon@arm.com> | 2015-08-03 07:33:39 -0400 |
commit | bff60792f994a87324ab57e89e945b4572b1ef77 (patch) | |
tree | 6621042fdbf61a747dd7f7c094db8e8bfe17087a | |
parent | bc0195aad0daa2ad5b0d76cce22b167bc3435590 (diff) |
arm64: psci: factor invocation code to drivers
To enable sharing with arm, move the core PSCI framework code to
drivers/firmware. This results in a minor gain in lines of code, but
this will quickly be amortised by the removal of code currently
duplicated in arch/arm.
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Reviewed-by: Hanjun Guo <hanjun.guo@linaro.org>
Tested-by: Hanjun Guo <hanjun.guo@linaro.org>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
-rw-r--r-- | arch/arm64/Kconfig | 1 | ||||
-rw-r--r-- | arch/arm64/include/asm/acpi.h | 4 | ||||
-rw-r--r-- | arch/arm64/include/asm/psci.h | 28 | ||||
-rw-r--r-- | arch/arm64/kernel/psci.c | 361 | ||||
-rw-r--r-- | arch/arm64/kernel/setup.c | 2 | ||||
-rw-r--r-- | drivers/firmware/Kconfig | 3 | ||||
-rw-r--r-- | drivers/firmware/Makefile | 1 | ||||
-rw-r--r-- | drivers/firmware/psci.c | 369 | ||||
-rw-r--r-- | include/linux/psci.h | 52 |
9 files changed, 431 insertions, 390 deletions
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 318175f62c24..7c55a632c08b 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig | |||
@@ -20,6 +20,7 @@ config ARM64 | |||
20 | select ARM_GIC_V2M if PCI_MSI | 20 | select ARM_GIC_V2M if PCI_MSI |
21 | select ARM_GIC_V3 | 21 | select ARM_GIC_V3 |
22 | select ARM_GIC_V3_ITS if PCI_MSI | 22 | select ARM_GIC_V3_ITS if PCI_MSI |
23 | select ARM_PSCI_FW | ||
23 | select BUILDTIME_EXTABLE_SORT | 24 | select BUILDTIME_EXTABLE_SORT |
24 | select CLONE_BACKWARDS | 25 | select CLONE_BACKWARDS |
25 | select COMMON_CLK | 26 | select COMMON_CLK |
diff --git a/arch/arm64/include/asm/acpi.h b/arch/arm64/include/asm/acpi.h index 406485ed110a..208cec08a74f 100644 --- a/arch/arm64/include/asm/acpi.h +++ b/arch/arm64/include/asm/acpi.h | |||
@@ -12,11 +12,11 @@ | |||
12 | #ifndef _ASM_ACPI_H | 12 | #ifndef _ASM_ACPI_H |
13 | #define _ASM_ACPI_H | 13 | #define _ASM_ACPI_H |
14 | 14 | ||
15 | #include <linux/mm.h> | ||
16 | #include <linux/irqchip/arm-gic-acpi.h> | 15 | #include <linux/irqchip/arm-gic-acpi.h> |
16 | #include <linux/mm.h> | ||
17 | #include <linux/psci.h> | ||
17 | 18 | ||
18 | #include <asm/cputype.h> | 19 | #include <asm/cputype.h> |
19 | #include <asm/psci.h> | ||
20 | #include <asm/smp_plat.h> | 20 | #include <asm/smp_plat.h> |
21 | 21 | ||
22 | /* Macros for consistency checks of the GICC subtable of MADT */ | 22 | /* Macros for consistency checks of the GICC subtable of MADT */ |
diff --git a/arch/arm64/include/asm/psci.h b/arch/arm64/include/asm/psci.h deleted file mode 100644 index 49d7e1aaebdc..000000000000 --- a/arch/arm64/include/asm/psci.h +++ /dev/null | |||
@@ -1,28 +0,0 @@ | |||
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 | |||
14 | #ifndef __ASM_PSCI_H | ||
15 | #define __ASM_PSCI_H | ||
16 | |||
17 | int __init psci_dt_init(void); | ||
18 | |||
19 | #ifdef CONFIG_ACPI | ||
20 | int __init psci_acpi_init(void); | ||
21 | bool __init acpi_psci_present(void); | ||
22 | bool __init acpi_psci_use_hvc(void); | ||
23 | #else | ||
24 | static inline int psci_acpi_init(void) { return 0; } | ||
25 | static inline bool acpi_psci_present(void) { return false; } | ||
26 | #endif | ||
27 | |||
28 | #endif /* __ASM_PSCI_H */ | ||
diff --git a/arch/arm64/kernel/psci.c b/arch/arm64/kernel/psci.c index 869f202748e8..51fd15a16461 100644 --- a/arch/arm64/kernel/psci.c +++ b/arch/arm64/kernel/psci.c | |||
@@ -18,23 +18,17 @@ | |||
18 | #include <linux/init.h> | 18 | #include <linux/init.h> |
19 | #include <linux/of.h> | 19 | #include <linux/of.h> |
20 | #include <linux/smp.h> | 20 | #include <linux/smp.h> |
21 | #include <linux/reboot.h> | ||
22 | #include <linux/pm.h> | ||
23 | #include <linux/delay.h> | 21 | #include <linux/delay.h> |
22 | #include <linux/psci.h> | ||
24 | #include <linux/slab.h> | 23 | #include <linux/slab.h> |
24 | |||
25 | #include <uapi/linux/psci.h> | 25 | #include <uapi/linux/psci.h> |
26 | 26 | ||
27 | #include <asm/compiler.h> | 27 | #include <asm/compiler.h> |
28 | #include <asm/cputype.h> | ||
29 | #include <asm/cpu_ops.h> | 28 | #include <asm/cpu_ops.h> |
30 | #include <asm/errno.h> | 29 | #include <asm/errno.h> |
31 | #include <asm/psci.h> | ||
32 | #include <asm/smp_plat.h> | 30 | #include <asm/smp_plat.h> |
33 | #include <asm/suspend.h> | 31 | #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 | 32 | ||
39 | static bool psci_power_state_loses_context(u32 state) | 33 | static bool psci_power_state_loses_context(u32 state) |
40 | { | 34 | { |
@@ -50,122 +44,8 @@ static bool psci_power_state_is_valid(u32 state) | |||
50 | return !(state & ~valid_mask); | 44 | return !(state & ~valid_mask); |
51 | } | 45 | } |
52 | 46 | ||
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 | struct psci_operations { | ||
62 | int (*cpu_suspend)(u32 state, unsigned long entry_point); | ||
63 | int (*cpu_off)(u32 state); | ||
64 | int (*cpu_on)(unsigned long cpuid, unsigned long entry_point); | ||
65 | int (*migrate)(unsigned long cpuid); | ||
66 | int (*affinity_info)(unsigned long target_affinity, | ||
67 | unsigned long lowest_affinity_level); | ||
68 | int (*migrate_info_type)(void); | ||
69 | }; | ||
70 | |||
71 | static struct psci_operations psci_ops; | ||
72 | |||
73 | typedef unsigned long (psci_fn)(unsigned long, unsigned long, | ||
74 | unsigned long, unsigned long); | ||
75 | asmlinkage psci_fn __invoke_psci_fn_hvc; | ||
76 | asmlinkage psci_fn __invoke_psci_fn_smc; | ||
77 | static psci_fn *invoke_psci_fn; | ||
78 | |||
79 | enum psci_function { | ||
80 | PSCI_FN_CPU_SUSPEND, | ||
81 | PSCI_FN_CPU_ON, | ||
82 | PSCI_FN_CPU_OFF, | ||
83 | PSCI_FN_MIGRATE, | ||
84 | PSCI_FN_MAX, | ||
85 | }; | ||
86 | |||
87 | static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state); | 47 | static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state); |
88 | 48 | ||
89 | static u32 psci_function_id[PSCI_FN_MAX]; | ||
90 | |||
91 | static int psci_to_linux_errno(int errno) | ||
92 | { | ||
93 | switch (errno) { | ||
94 | case PSCI_RET_SUCCESS: | ||
95 | return 0; | ||
96 | case PSCI_RET_NOT_SUPPORTED: | ||
97 | return -EOPNOTSUPP; | ||
98 | case PSCI_RET_INVALID_PARAMS: | ||
99 | return -EINVAL; | ||
100 | case PSCI_RET_DENIED: | ||
101 | return -EPERM; | ||
102 | }; | ||
103 | |||
104 | return -EINVAL; | ||
105 | } | ||
106 | |||
107 | static u32 psci_get_version(void) | ||
108 | { | ||
109 | return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0); | ||
110 | } | ||
111 | |||
112 | static int psci_cpu_suspend(u32 state, unsigned long entry_point) | ||
113 | { | ||
114 | int err; | ||
115 | u32 fn; | ||
116 | |||
117 | fn = psci_function_id[PSCI_FN_CPU_SUSPEND]; | ||
118 | err = invoke_psci_fn(fn, state, entry_point, 0); | ||
119 | return psci_to_linux_errno(err); | ||
120 | } | ||
121 | |||
122 | static int psci_cpu_off(u32 state) | ||
123 | { | ||
124 | int err; | ||
125 | u32 fn; | ||
126 | |||
127 | fn = psci_function_id[PSCI_FN_CPU_OFF]; | ||
128 | err = invoke_psci_fn(fn, state, 0, 0); | ||
129 | return psci_to_linux_errno(err); | ||
130 | } | ||
131 | |||
132 | static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point) | ||
133 | { | ||
134 | int err; | ||
135 | u32 fn; | ||
136 | |||
137 | fn = psci_function_id[PSCI_FN_CPU_ON]; | ||
138 | err = invoke_psci_fn(fn, cpuid, entry_point, 0); | ||
139 | return psci_to_linux_errno(err); | ||
140 | } | ||
141 | |||
142 | static int psci_migrate(unsigned long cpuid) | ||
143 | { | ||
144 | int err; | ||
145 | u32 fn; | ||
146 | |||
147 | fn = psci_function_id[PSCI_FN_MIGRATE]; | ||
148 | err = invoke_psci_fn(fn, cpuid, 0, 0); | ||
149 | return psci_to_linux_errno(err); | ||
150 | } | ||
151 | |||
152 | static int psci_affinity_info(unsigned long target_affinity, | ||
153 | unsigned long lowest_affinity_level) | ||
154 | { | ||
155 | return invoke_psci_fn(PSCI_0_2_FN64_AFFINITY_INFO, target_affinity, | ||
156 | lowest_affinity_level, 0); | ||
157 | } | ||
158 | |||
159 | static int psci_migrate_info_type(void) | ||
160 | { | ||
161 | return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0); | ||
162 | } | ||
163 | |||
164 | static unsigned long psci_migrate_info_up_cpu(void) | ||
165 | { | ||
166 | return invoke_psci_fn(PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU, 0, 0, 0); | ||
167 | } | ||
168 | |||
169 | static int __maybe_unused cpu_psci_cpu_init_idle(unsigned int cpu) | 49 | static int __maybe_unused cpu_psci_cpu_init_idle(unsigned int cpu) |
170 | { | 50 | { |
171 | int i, ret, count = 0; | 51 | int i, ret, count = 0; |
@@ -230,238 +110,6 @@ free_mem: | |||
230 | return ret; | 110 | return ret; |
231 | } | 111 | } |
232 | 112 | ||
233 | static int get_set_conduit_method(struct device_node *np) | ||
234 | { | ||
235 | const char *method; | ||
236 | |||
237 | pr_info("probing for conduit method from DT.\n"); | ||
238 | |||
239 | if (of_property_read_string(np, "method", &method)) { | ||
240 | pr_warn("missing \"method\" property\n"); | ||
241 | return -ENXIO; | ||
242 | } | ||
243 | |||
244 | if (!strcmp("hvc", method)) { | ||
245 | invoke_psci_fn = __invoke_psci_fn_hvc; | ||
246 | } else if (!strcmp("smc", method)) { | ||
247 | invoke_psci_fn = __invoke_psci_fn_smc; | ||
248 | } else { | ||
249 | pr_warn("invalid \"method\" property: %s\n", method); | ||
250 | return -EINVAL; | ||
251 | } | ||
252 | return 0; | ||
253 | } | ||
254 | |||
255 | static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd) | ||
256 | { | ||
257 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); | ||
258 | } | ||
259 | |||
260 | static void psci_sys_poweroff(void) | ||
261 | { | ||
262 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); | ||
263 | } | ||
264 | |||
265 | /* | ||
266 | * Detect the presence of a resident Trusted OS which may cause CPU_OFF to | ||
267 | * return DENIED (which would be fatal). | ||
268 | */ | ||
269 | static void __init psci_init_migrate(void) | ||
270 | { | ||
271 | unsigned long cpuid; | ||
272 | int type, cpu; | ||
273 | |||
274 | type = psci_ops.migrate_info_type(); | ||
275 | |||
276 | if (type == PSCI_0_2_TOS_MP) { | ||
277 | pr_info("Trusted OS migration not required\n"); | ||
278 | return; | ||
279 | } | ||
280 | |||
281 | if (type == PSCI_RET_NOT_SUPPORTED) { | ||
282 | pr_info("MIGRATE_INFO_TYPE not supported.\n"); | ||
283 | return; | ||
284 | } | ||
285 | |||
286 | if (type != PSCI_0_2_TOS_UP_MIGRATE && | ||
287 | type != PSCI_0_2_TOS_UP_NO_MIGRATE) { | ||
288 | pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type); | ||
289 | return; | ||
290 | } | ||
291 | |||
292 | cpuid = psci_migrate_info_up_cpu(); | ||
293 | if (cpuid & ~MPIDR_HWID_BITMASK) { | ||
294 | pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n", | ||
295 | cpuid); | ||
296 | return; | ||
297 | } | ||
298 | |||
299 | cpu = get_logical_index(cpuid); | ||
300 | resident_cpu = cpu >= 0 ? cpu : -1; | ||
301 | |||
302 | pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid); | ||
303 | } | ||
304 | |||
305 | static void __init psci_0_2_set_functions(void) | ||
306 | { | ||
307 | pr_info("Using standard PSCI v0.2 function IDs\n"); | ||
308 | psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN64_CPU_SUSPEND; | ||
309 | psci_ops.cpu_suspend = psci_cpu_suspend; | ||
310 | |||
311 | psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF; | ||
312 | psci_ops.cpu_off = psci_cpu_off; | ||
313 | |||
314 | psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN64_CPU_ON; | ||
315 | psci_ops.cpu_on = psci_cpu_on; | ||
316 | |||
317 | psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN64_MIGRATE; | ||
318 | psci_ops.migrate = psci_migrate; | ||
319 | |||
320 | psci_ops.affinity_info = psci_affinity_info; | ||
321 | |||
322 | psci_ops.migrate_info_type = psci_migrate_info_type; | ||
323 | |||
324 | arm_pm_restart = psci_sys_reset; | ||
325 | |||
326 | pm_power_off = psci_sys_poweroff; | ||
327 | } | ||
328 | |||
329 | /* | ||
330 | * Probe function for PSCI firmware versions >= 0.2 | ||
331 | */ | ||
332 | static int __init psci_probe(void) | ||
333 | { | ||
334 | u32 ver = psci_get_version(); | ||
335 | |||
336 | pr_info("PSCIv%d.%d detected in firmware.\n", | ||
337 | PSCI_VERSION_MAJOR(ver), | ||
338 | PSCI_VERSION_MINOR(ver)); | ||
339 | |||
340 | if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) { | ||
341 | pr_err("Conflicting PSCI version detected.\n"); | ||
342 | return -EINVAL; | ||
343 | } | ||
344 | |||
345 | psci_0_2_set_functions(); | ||
346 | |||
347 | psci_init_migrate(); | ||
348 | |||
349 | return 0; | ||
350 | } | ||
351 | |||
352 | typedef int (*psci_initcall_t)(const struct device_node *); | ||
353 | |||
354 | /* | ||
355 | * PSCI init function for PSCI versions >=0.2 | ||
356 | * | ||
357 | * Probe based on PSCI PSCI_VERSION function | ||
358 | */ | ||
359 | static int __init psci_0_2_init(struct device_node *np) | ||
360 | { | ||
361 | int err; | ||
362 | |||
363 | err = get_set_conduit_method(np); | ||
364 | |||
365 | if (err) | ||
366 | goto out_put_node; | ||
367 | /* | ||
368 | * Starting with v0.2, the PSCI specification introduced a call | ||
369 | * (PSCI_VERSION) that allows probing the firmware version, so | ||
370 | * that PSCI function IDs and version specific initialization | ||
371 | * can be carried out according to the specific version reported | ||
372 | * by firmware | ||
373 | */ | ||
374 | err = psci_probe(); | ||
375 | |||
376 | out_put_node: | ||
377 | of_node_put(np); | ||
378 | return err; | ||
379 | } | ||
380 | |||
381 | /* | ||
382 | * PSCI < v0.2 get PSCI Function IDs via DT. | ||
383 | */ | ||
384 | static int __init psci_0_1_init(struct device_node *np) | ||
385 | { | ||
386 | u32 id; | ||
387 | int err; | ||
388 | |||
389 | err = get_set_conduit_method(np); | ||
390 | |||
391 | if (err) | ||
392 | goto out_put_node; | ||
393 | |||
394 | pr_info("Using PSCI v0.1 Function IDs from DT\n"); | ||
395 | |||
396 | if (!of_property_read_u32(np, "cpu_suspend", &id)) { | ||
397 | psci_function_id[PSCI_FN_CPU_SUSPEND] = id; | ||
398 | psci_ops.cpu_suspend = psci_cpu_suspend; | ||
399 | } | ||
400 | |||
401 | if (!of_property_read_u32(np, "cpu_off", &id)) { | ||
402 | psci_function_id[PSCI_FN_CPU_OFF] = id; | ||
403 | psci_ops.cpu_off = psci_cpu_off; | ||
404 | } | ||
405 | |||
406 | if (!of_property_read_u32(np, "cpu_on", &id)) { | ||
407 | psci_function_id[PSCI_FN_CPU_ON] = id; | ||
408 | psci_ops.cpu_on = psci_cpu_on; | ||
409 | } | ||
410 | |||
411 | if (!of_property_read_u32(np, "migrate", &id)) { | ||
412 | psci_function_id[PSCI_FN_MIGRATE] = id; | ||
413 | psci_ops.migrate = psci_migrate; | ||
414 | } | ||
415 | |||
416 | out_put_node: | ||
417 | of_node_put(np); | ||
418 | return err; | ||
419 | } | ||
420 | |||
421 | static const struct of_device_id psci_of_match[] __initconst = { | ||
422 | { .compatible = "arm,psci", .data = psci_0_1_init}, | ||
423 | { .compatible = "arm,psci-0.2", .data = psci_0_2_init}, | ||
424 | {}, | ||
425 | }; | ||
426 | |||
427 | int __init psci_dt_init(void) | ||
428 | { | ||
429 | struct device_node *np; | ||
430 | const struct of_device_id *matched_np; | ||
431 | psci_initcall_t init_fn; | ||
432 | |||
433 | np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np); | ||
434 | |||
435 | if (!np) | ||
436 | return -ENODEV; | ||
437 | |||
438 | init_fn = (psci_initcall_t)matched_np->data; | ||
439 | return init_fn(np); | ||
440 | } | ||
441 | |||
442 | #ifdef CONFIG_ACPI | ||
443 | /* | ||
444 | * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's | ||
445 | * explicitly clarified in SBBR | ||
446 | */ | ||
447 | int __init psci_acpi_init(void) | ||
448 | { | ||
449 | if (!acpi_psci_present()) { | ||
450 | pr_info("is not implemented in ACPI.\n"); | ||
451 | return -EOPNOTSUPP; | ||
452 | } | ||
453 | |||
454 | pr_info("probing for conduit method from ACPI.\n"); | ||
455 | |||
456 | if (acpi_psci_use_hvc()) | ||
457 | invoke_psci_fn = __invoke_psci_fn_hvc; | ||
458 | else | ||
459 | invoke_psci_fn = __invoke_psci_fn_smc; | ||
460 | |||
461 | return psci_probe(); | ||
462 | } | ||
463 | #endif | ||
464 | |||
465 | #ifdef CONFIG_SMP | 113 | #ifdef CONFIG_SMP |
466 | 114 | ||
467 | static int __init cpu_psci_cpu_init(unsigned int cpu) | 115 | static int __init cpu_psci_cpu_init(unsigned int cpu) |
@@ -489,11 +137,6 @@ static int cpu_psci_cpu_boot(unsigned int cpu) | |||
489 | } | 137 | } |
490 | 138 | ||
491 | #ifdef CONFIG_HOTPLUG_CPU | 139 | #ifdef CONFIG_HOTPLUG_CPU |
492 | static bool psci_tos_resident_on(int cpu) | ||
493 | { | ||
494 | return cpu == resident_cpu; | ||
495 | } | ||
496 | |||
497 | static int cpu_psci_cpu_disable(unsigned int cpu) | 140 | static int cpu_psci_cpu_disable(unsigned int cpu) |
498 | { | 141 | { |
499 | /* Fail early if we don't have CPU_OFF support */ | 142 | /* Fail early if we don't have CPU_OFF support */ |
diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index f3067d4d4e35..96ce26428f82 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c | |||
@@ -46,6 +46,7 @@ | |||
46 | #include <linux/of_platform.h> | 46 | #include <linux/of_platform.h> |
47 | #include <linux/efi.h> | 47 | #include <linux/efi.h> |
48 | #include <linux/personality.h> | 48 | #include <linux/personality.h> |
49 | #include <linux/psci.h> | ||
49 | 50 | ||
50 | #include <asm/acpi.h> | 51 | #include <asm/acpi.h> |
51 | #include <asm/fixmap.h> | 52 | #include <asm/fixmap.h> |
@@ -61,7 +62,6 @@ | |||
61 | #include <asm/tlbflush.h> | 62 | #include <asm/tlbflush.h> |
62 | #include <asm/traps.h> | 63 | #include <asm/traps.h> |
63 | #include <asm/memblock.h> | 64 | #include <asm/memblock.h> |
64 | #include <asm/psci.h> | ||
65 | #include <asm/efi.h> | 65 | #include <asm/efi.h> |
66 | #include <asm/virt.h> | 66 | #include <asm/virt.h> |
67 | #include <asm/xen/hypervisor.h> | 67 | #include <asm/xen/hypervisor.h> |
diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig index 99c69a3205c4..d8de6a8dd4de 100644 --- a/drivers/firmware/Kconfig +++ b/drivers/firmware/Kconfig | |||
@@ -5,6 +5,9 @@ | |||
5 | 5 | ||
6 | menu "Firmware Drivers" | 6 | menu "Firmware Drivers" |
7 | 7 | ||
8 | config ARM_PSCI_FW | ||
9 | bool | ||
10 | |||
8 | config EDD | 11 | config EDD |
9 | tristate "BIOS Enhanced Disk Drive calls determine boot disk" | 12 | tristate "BIOS Enhanced Disk Drive calls determine boot disk" |
10 | depends on X86 | 13 | depends on X86 |
diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile index 4a4b897f9314..000830fc6707 100644 --- a/drivers/firmware/Makefile +++ b/drivers/firmware/Makefile | |||
@@ -1,6 +1,7 @@ | |||
1 | # | 1 | # |
2 | # Makefile for the linux kernel. | 2 | # Makefile for the linux kernel. |
3 | # | 3 | # |
4 | obj-$(CONFIG_ARM_PSCI_FW) += psci.o | ||
4 | obj-$(CONFIG_DMI) += dmi_scan.o | 5 | obj-$(CONFIG_DMI) += dmi_scan.o |
5 | obj-$(CONFIG_DMI_SYSFS) += dmi-sysfs.o | 6 | obj-$(CONFIG_DMI_SYSFS) += dmi-sysfs.o |
6 | obj-$(CONFIG_EDD) += edd.o | 7 | obj-$(CONFIG_EDD) += edd.o |
diff --git a/drivers/firmware/psci.c b/drivers/firmware/psci.c new file mode 100644 index 000000000000..36e2cea3809b --- /dev/null +++ b/drivers/firmware/psci.c | |||
@@ -0,0 +1,369 @@ | |||
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/errno.h> | ||
17 | #include <linux/linkage.h> | ||
18 | #include <linux/of.h> | ||
19 | #include <linux/pm.h> | ||
20 | #include <linux/printk.h> | ||
21 | #include <linux/psci.h> | ||
22 | #include <linux/reboot.h> | ||
23 | |||
24 | #include <uapi/linux/psci.h> | ||
25 | |||
26 | #include <asm/cputype.h> | ||
27 | #include <asm/system_misc.h> | ||
28 | #include <asm/smp_plat.h> | ||
29 | |||
30 | /* | ||
31 | * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF | ||
32 | * calls to its resident CPU, so we must avoid issuing those. We never migrate | ||
33 | * a Trusted OS even if it claims to be capable of migration -- doing so will | ||
34 | * require cooperation with a Trusted OS driver. | ||
35 | */ | ||
36 | static int resident_cpu = -1; | ||
37 | |||
38 | bool psci_tos_resident_on(int cpu) | ||
39 | { | ||
40 | return cpu == resident_cpu; | ||
41 | } | ||
42 | |||
43 | struct psci_operations psci_ops; | ||
44 | |||
45 | typedef unsigned long (psci_fn)(unsigned long, unsigned long, | ||
46 | unsigned long, unsigned long); | ||
47 | asmlinkage psci_fn __invoke_psci_fn_hvc; | ||
48 | asmlinkage psci_fn __invoke_psci_fn_smc; | ||
49 | static psci_fn *invoke_psci_fn; | ||
50 | |||
51 | enum psci_function { | ||
52 | PSCI_FN_CPU_SUSPEND, | ||
53 | PSCI_FN_CPU_ON, | ||
54 | PSCI_FN_CPU_OFF, | ||
55 | PSCI_FN_MIGRATE, | ||
56 | PSCI_FN_MAX, | ||
57 | }; | ||
58 | |||
59 | static u32 psci_function_id[PSCI_FN_MAX]; | ||
60 | |||
61 | static int psci_to_linux_errno(int errno) | ||
62 | { | ||
63 | switch (errno) { | ||
64 | case PSCI_RET_SUCCESS: | ||
65 | return 0; | ||
66 | case PSCI_RET_NOT_SUPPORTED: | ||
67 | return -EOPNOTSUPP; | ||
68 | case PSCI_RET_INVALID_PARAMS: | ||
69 | return -EINVAL; | ||
70 | case PSCI_RET_DENIED: | ||
71 | return -EPERM; | ||
72 | }; | ||
73 | |||
74 | return -EINVAL; | ||
75 | } | ||
76 | |||
77 | static u32 psci_get_version(void) | ||
78 | { | ||
79 | return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0); | ||
80 | } | ||
81 | |||
82 | static int psci_cpu_suspend(u32 state, unsigned long entry_point) | ||
83 | { | ||
84 | int err; | ||
85 | u32 fn; | ||
86 | |||
87 | fn = psci_function_id[PSCI_FN_CPU_SUSPEND]; | ||
88 | err = invoke_psci_fn(fn, state, entry_point, 0); | ||
89 | return psci_to_linux_errno(err); | ||
90 | } | ||
91 | |||
92 | static int psci_cpu_off(u32 state) | ||
93 | { | ||
94 | int err; | ||
95 | u32 fn; | ||
96 | |||
97 | fn = psci_function_id[PSCI_FN_CPU_OFF]; | ||
98 | err = invoke_psci_fn(fn, state, 0, 0); | ||
99 | return psci_to_linux_errno(err); | ||
100 | } | ||
101 | |||
102 | static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point) | ||
103 | { | ||
104 | int err; | ||
105 | u32 fn; | ||
106 | |||
107 | fn = psci_function_id[PSCI_FN_CPU_ON]; | ||
108 | err = invoke_psci_fn(fn, cpuid, entry_point, 0); | ||
109 | return psci_to_linux_errno(err); | ||
110 | } | ||
111 | |||
112 | static int psci_migrate(unsigned long cpuid) | ||
113 | { | ||
114 | int err; | ||
115 | u32 fn; | ||
116 | |||
117 | fn = psci_function_id[PSCI_FN_MIGRATE]; | ||
118 | err = invoke_psci_fn(fn, cpuid, 0, 0); | ||
119 | return psci_to_linux_errno(err); | ||
120 | } | ||
121 | |||
122 | static int psci_affinity_info(unsigned long target_affinity, | ||
123 | unsigned long lowest_affinity_level) | ||
124 | { | ||
125 | return invoke_psci_fn(PSCI_0_2_FN64_AFFINITY_INFO, target_affinity, | ||
126 | lowest_affinity_level, 0); | ||
127 | } | ||
128 | |||
129 | static int psci_migrate_info_type(void) | ||
130 | { | ||
131 | return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0); | ||
132 | } | ||
133 | |||
134 | static unsigned long psci_migrate_info_up_cpu(void) | ||
135 | { | ||
136 | return invoke_psci_fn(PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU, 0, 0, 0); | ||
137 | } | ||
138 | |||
139 | static int get_set_conduit_method(struct device_node *np) | ||
140 | { | ||
141 | const char *method; | ||
142 | |||
143 | pr_info("probing for conduit method from DT.\n"); | ||
144 | |||
145 | if (of_property_read_string(np, "method", &method)) { | ||
146 | pr_warn("missing \"method\" property\n"); | ||
147 | return -ENXIO; | ||
148 | } | ||
149 | |||
150 | if (!strcmp("hvc", method)) { | ||
151 | invoke_psci_fn = __invoke_psci_fn_hvc; | ||
152 | } else if (!strcmp("smc", method)) { | ||
153 | invoke_psci_fn = __invoke_psci_fn_smc; | ||
154 | } else { | ||
155 | pr_warn("invalid \"method\" property: %s\n", method); | ||
156 | return -EINVAL; | ||
157 | } | ||
158 | return 0; | ||
159 | } | ||
160 | |||
161 | static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd) | ||
162 | { | ||
163 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); | ||
164 | } | ||
165 | |||
166 | static void psci_sys_poweroff(void) | ||
167 | { | ||
168 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); | ||
169 | } | ||
170 | |||
171 | /* | ||
172 | * Detect the presence of a resident Trusted OS which may cause CPU_OFF to | ||
173 | * return DENIED (which would be fatal). | ||
174 | */ | ||
175 | static void __init psci_init_migrate(void) | ||
176 | { | ||
177 | unsigned long cpuid; | ||
178 | int type, cpu = -1; | ||
179 | |||
180 | type = psci_ops.migrate_info_type(); | ||
181 | |||
182 | if (type == PSCI_0_2_TOS_MP) { | ||
183 | pr_info("Trusted OS migration not required\n"); | ||
184 | return; | ||
185 | } | ||
186 | |||
187 | if (type == PSCI_RET_NOT_SUPPORTED) { | ||
188 | pr_info("MIGRATE_INFO_TYPE not supported.\n"); | ||
189 | return; | ||
190 | } | ||
191 | |||
192 | if (type != PSCI_0_2_TOS_UP_MIGRATE && | ||
193 | type != PSCI_0_2_TOS_UP_NO_MIGRATE) { | ||
194 | pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type); | ||
195 | return; | ||
196 | } | ||
197 | |||
198 | cpuid = psci_migrate_info_up_cpu(); | ||
199 | if (cpuid & ~MPIDR_HWID_BITMASK) { | ||
200 | pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n", | ||
201 | cpuid); | ||
202 | return; | ||
203 | } | ||
204 | |||
205 | cpu = get_logical_index(cpuid); | ||
206 | resident_cpu = cpu >= 0 ? cpu : -1; | ||
207 | |||
208 | pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid); | ||
209 | } | ||
210 | |||
211 | static void __init psci_0_2_set_functions(void) | ||
212 | { | ||
213 | pr_info("Using standard PSCI v0.2 function IDs\n"); | ||
214 | psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN64_CPU_SUSPEND; | ||
215 | psci_ops.cpu_suspend = psci_cpu_suspend; | ||
216 | |||
217 | psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF; | ||
218 | psci_ops.cpu_off = psci_cpu_off; | ||
219 | |||
220 | psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN64_CPU_ON; | ||
221 | psci_ops.cpu_on = psci_cpu_on; | ||
222 | |||
223 | psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN64_MIGRATE; | ||
224 | psci_ops.migrate = psci_migrate; | ||
225 | |||
226 | psci_ops.affinity_info = psci_affinity_info; | ||
227 | |||
228 | psci_ops.migrate_info_type = psci_migrate_info_type; | ||
229 | |||
230 | arm_pm_restart = psci_sys_reset; | ||
231 | |||
232 | pm_power_off = psci_sys_poweroff; | ||
233 | } | ||
234 | |||
235 | /* | ||
236 | * Probe function for PSCI firmware versions >= 0.2 | ||
237 | */ | ||
238 | static int __init psci_probe(void) | ||
239 | { | ||
240 | u32 ver = psci_get_version(); | ||
241 | |||
242 | pr_info("PSCIv%d.%d detected in firmware.\n", | ||
243 | PSCI_VERSION_MAJOR(ver), | ||
244 | PSCI_VERSION_MINOR(ver)); | ||
245 | |||
246 | if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) { | ||
247 | pr_err("Conflicting PSCI version detected.\n"); | ||
248 | return -EINVAL; | ||
249 | } | ||
250 | |||
251 | psci_0_2_set_functions(); | ||
252 | |||
253 | psci_init_migrate(); | ||
254 | |||
255 | return 0; | ||
256 | } | ||
257 | |||
258 | typedef int (*psci_initcall_t)(const struct device_node *); | ||
259 | |||
260 | /* | ||
261 | * PSCI init function for PSCI versions >=0.2 | ||
262 | * | ||
263 | * Probe based on PSCI PSCI_VERSION function | ||
264 | */ | ||
265 | static int __init psci_0_2_init(struct device_node *np) | ||
266 | { | ||
267 | int err; | ||
268 | |||
269 | err = get_set_conduit_method(np); | ||
270 | |||
271 | if (err) | ||
272 | goto out_put_node; | ||
273 | /* | ||
274 | * Starting with v0.2, the PSCI specification introduced a call | ||
275 | * (PSCI_VERSION) that allows probing the firmware version, so | ||
276 | * that PSCI function IDs and version specific initialization | ||
277 | * can be carried out according to the specific version reported | ||
278 | * by firmware | ||
279 | */ | ||
280 | err = psci_probe(); | ||
281 | |||
282 | out_put_node: | ||
283 | of_node_put(np); | ||
284 | return err; | ||
285 | } | ||
286 | |||
287 | /* | ||
288 | * PSCI < v0.2 get PSCI Function IDs via DT. | ||
289 | */ | ||
290 | static int __init psci_0_1_init(struct device_node *np) | ||
291 | { | ||
292 | u32 id; | ||
293 | int err; | ||
294 | |||
295 | err = get_set_conduit_method(np); | ||
296 | |||
297 | if (err) | ||
298 | goto out_put_node; | ||
299 | |||
300 | pr_info("Using PSCI v0.1 Function IDs from DT\n"); | ||
301 | |||
302 | if (!of_property_read_u32(np, "cpu_suspend", &id)) { | ||
303 | psci_function_id[PSCI_FN_CPU_SUSPEND] = id; | ||
304 | psci_ops.cpu_suspend = psci_cpu_suspend; | ||
305 | } | ||
306 | |||
307 | if (!of_property_read_u32(np, "cpu_off", &id)) { | ||
308 | psci_function_id[PSCI_FN_CPU_OFF] = id; | ||
309 | psci_ops.cpu_off = psci_cpu_off; | ||
310 | } | ||
311 | |||
312 | if (!of_property_read_u32(np, "cpu_on", &id)) { | ||
313 | psci_function_id[PSCI_FN_CPU_ON] = id; | ||
314 | psci_ops.cpu_on = psci_cpu_on; | ||
315 | } | ||
316 | |||
317 | if (!of_property_read_u32(np, "migrate", &id)) { | ||
318 | psci_function_id[PSCI_FN_MIGRATE] = id; | ||
319 | psci_ops.migrate = psci_migrate; | ||
320 | } | ||
321 | |||
322 | out_put_node: | ||
323 | of_node_put(np); | ||
324 | return err; | ||
325 | } | ||
326 | |||
327 | static const struct of_device_id psci_of_match[] __initconst = { | ||
328 | { .compatible = "arm,psci", .data = psci_0_1_init}, | ||
329 | { .compatible = "arm,psci-0.2", .data = psci_0_2_init}, | ||
330 | {}, | ||
331 | }; | ||
332 | |||
333 | int __init psci_dt_init(void) | ||
334 | { | ||
335 | struct device_node *np; | ||
336 | const struct of_device_id *matched_np; | ||
337 | psci_initcall_t init_fn; | ||
338 | |||
339 | np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np); | ||
340 | |||
341 | if (!np) | ||
342 | return -ENODEV; | ||
343 | |||
344 | init_fn = (psci_initcall_t)matched_np->data; | ||
345 | return init_fn(np); | ||
346 | } | ||
347 | |||
348 | #ifdef CONFIG_ACPI | ||
349 | /* | ||
350 | * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's | ||
351 | * explicitly clarified in SBBR | ||
352 | */ | ||
353 | int __init psci_acpi_init(void) | ||
354 | { | ||
355 | if (!acpi_psci_present()) { | ||
356 | pr_info("is not implemented in ACPI.\n"); | ||
357 | return -EOPNOTSUPP; | ||
358 | } | ||
359 | |||
360 | pr_info("probing for conduit method from ACPI.\n"); | ||
361 | |||
362 | if (acpi_psci_use_hvc()) | ||
363 | invoke_psci_fn = __invoke_psci_fn_hvc; | ||
364 | else | ||
365 | invoke_psci_fn = __invoke_psci_fn_smc; | ||
366 | |||
367 | return psci_probe(); | ||
368 | } | ||
369 | #endif | ||
diff --git a/include/linux/psci.h b/include/linux/psci.h new file mode 100644 index 000000000000..a682fcc91c33 --- /dev/null +++ b/include/linux/psci.h | |||
@@ -0,0 +1,52 @@ | |||
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 | #ifndef __LINUX_PSCI_H | ||
15 | #define __LINUX_PSCI_H | ||
16 | |||
17 | #include <linux/init.h> | ||
18 | #include <linux/types.h> | ||
19 | |||
20 | #define PSCI_POWER_STATE_TYPE_STANDBY 0 | ||
21 | #define PSCI_POWER_STATE_TYPE_POWER_DOWN 1 | ||
22 | |||
23 | bool psci_tos_resident_on(int cpu); | ||
24 | |||
25 | struct psci_operations { | ||
26 | int (*cpu_suspend)(u32 state, unsigned long entry_point); | ||
27 | int (*cpu_off)(u32 state); | ||
28 | int (*cpu_on)(unsigned long cpuid, unsigned long entry_point); | ||
29 | int (*migrate)(unsigned long cpuid); | ||
30 | int (*affinity_info)(unsigned long target_affinity, | ||
31 | unsigned long lowest_affinity_level); | ||
32 | int (*migrate_info_type)(void); | ||
33 | }; | ||
34 | |||
35 | extern struct psci_operations psci_ops; | ||
36 | |||
37 | #if defined(CONFIG_ARM_PSCI_FW) | ||
38 | int __init psci_dt_init(void); | ||
39 | #else | ||
40 | static inline int psci_dt_init(void) { return 0; } | ||
41 | #endif | ||
42 | |||
43 | #if defined(CONFIG_ARM_PSCI_FW) && defined(CONFIG_ACPI) | ||
44 | int __init psci_acpi_init(void); | ||
45 | bool __init acpi_psci_present(void); | ||
46 | bool __init acpi_psci_use_hvc(void); | ||
47 | #else | ||
48 | static inline int psci_acpi_init(void) { return 0; } | ||
49 | static inline bool acpi_psci_present(void) { return false; } | ||
50 | #endif | ||
51 | |||
52 | #endif /* __LINUX_PSCI_H */ | ||