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 /drivers/firmware/psci.c | |
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>
Diffstat (limited to 'drivers/firmware/psci.c')
-rw-r--r-- | drivers/firmware/psci.c | 369 |
1 files changed, 369 insertions, 0 deletions
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 | ||