]>
Commit | Line | Data |
---|---|---|
bff60792 MR |
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 | ||
5211df00 MR |
30 | /* |
31 | * While a 64-bit OS can make calls with SMC32 calling conventions, for some | |
32 | * calls it is necessary to use SMC64 to pass or return 64-bit values. For such | |
33 | * calls PSCI_0_2_FN_NATIVE(x) will choose the appropriate (native-width) | |
34 | * function ID. | |
35 | */ | |
36 | #ifdef CONFIG_64BIT | |
37 | #define PSCI_0_2_FN_NATIVE(name) PSCI_0_2_FN64_##name | |
38 | #else | |
39 | #define PSCI_0_2_FN_NATIVE(name) PSCI_0_2_FN_##name | |
40 | #endif | |
41 | ||
bff60792 MR |
42 | /* |
43 | * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF | |
44 | * calls to its resident CPU, so we must avoid issuing those. We never migrate | |
45 | * a Trusted OS even if it claims to be capable of migration -- doing so will | |
46 | * require cooperation with a Trusted OS driver. | |
47 | */ | |
48 | static int resident_cpu = -1; | |
49 | ||
50 | bool psci_tos_resident_on(int cpu) | |
51 | { | |
52 | return cpu == resident_cpu; | |
53 | } | |
54 | ||
55 | struct psci_operations psci_ops; | |
56 | ||
57 | typedef unsigned long (psci_fn)(unsigned long, unsigned long, | |
58 | unsigned long, unsigned long); | |
59 | asmlinkage psci_fn __invoke_psci_fn_hvc; | |
60 | asmlinkage psci_fn __invoke_psci_fn_smc; | |
61 | static psci_fn *invoke_psci_fn; | |
62 | ||
63 | enum psci_function { | |
64 | PSCI_FN_CPU_SUSPEND, | |
65 | PSCI_FN_CPU_ON, | |
66 | PSCI_FN_CPU_OFF, | |
67 | PSCI_FN_MIGRATE, | |
68 | PSCI_FN_MAX, | |
69 | }; | |
70 | ||
71 | static u32 psci_function_id[PSCI_FN_MAX]; | |
72 | ||
73 | static int psci_to_linux_errno(int errno) | |
74 | { | |
75 | switch (errno) { | |
76 | case PSCI_RET_SUCCESS: | |
77 | return 0; | |
78 | case PSCI_RET_NOT_SUPPORTED: | |
79 | return -EOPNOTSUPP; | |
80 | case PSCI_RET_INVALID_PARAMS: | |
2217d7c6 | 81 | case PSCI_RET_INVALID_ADDRESS: |
bff60792 MR |
82 | return -EINVAL; |
83 | case PSCI_RET_DENIED: | |
84 | return -EPERM; | |
85 | }; | |
86 | ||
87 | return -EINVAL; | |
88 | } | |
89 | ||
90 | static u32 psci_get_version(void) | |
91 | { | |
92 | return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0); | |
93 | } | |
94 | ||
95 | static int psci_cpu_suspend(u32 state, unsigned long entry_point) | |
96 | { | |
97 | int err; | |
98 | u32 fn; | |
99 | ||
100 | fn = psci_function_id[PSCI_FN_CPU_SUSPEND]; | |
101 | err = invoke_psci_fn(fn, state, entry_point, 0); | |
102 | return psci_to_linux_errno(err); | |
103 | } | |
104 | ||
105 | static int psci_cpu_off(u32 state) | |
106 | { | |
107 | int err; | |
108 | u32 fn; | |
109 | ||
110 | fn = psci_function_id[PSCI_FN_CPU_OFF]; | |
111 | err = invoke_psci_fn(fn, state, 0, 0); | |
112 | return psci_to_linux_errno(err); | |
113 | } | |
114 | ||
115 | static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point) | |
116 | { | |
117 | int err; | |
118 | u32 fn; | |
119 | ||
120 | fn = psci_function_id[PSCI_FN_CPU_ON]; | |
121 | err = invoke_psci_fn(fn, cpuid, entry_point, 0); | |
122 | return psci_to_linux_errno(err); | |
123 | } | |
124 | ||
125 | static int psci_migrate(unsigned long cpuid) | |
126 | { | |
127 | int err; | |
128 | u32 fn; | |
129 | ||
130 | fn = psci_function_id[PSCI_FN_MIGRATE]; | |
131 | err = invoke_psci_fn(fn, cpuid, 0, 0); | |
132 | return psci_to_linux_errno(err); | |
133 | } | |
134 | ||
135 | static int psci_affinity_info(unsigned long target_affinity, | |
136 | unsigned long lowest_affinity_level) | |
137 | { | |
5211df00 MR |
138 | return invoke_psci_fn(PSCI_0_2_FN_NATIVE(AFFINITY_INFO), |
139 | target_affinity, lowest_affinity_level, 0); | |
bff60792 MR |
140 | } |
141 | ||
142 | static int psci_migrate_info_type(void) | |
143 | { | |
144 | return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0); | |
145 | } | |
146 | ||
147 | static unsigned long psci_migrate_info_up_cpu(void) | |
148 | { | |
5211df00 MR |
149 | return invoke_psci_fn(PSCI_0_2_FN_NATIVE(MIGRATE_INFO_UP_CPU), |
150 | 0, 0, 0); | |
bff60792 MR |
151 | } |
152 | ||
153 | static int get_set_conduit_method(struct device_node *np) | |
154 | { | |
155 | const char *method; | |
156 | ||
157 | pr_info("probing for conduit method from DT.\n"); | |
158 | ||
159 | if (of_property_read_string(np, "method", &method)) { | |
160 | pr_warn("missing \"method\" property\n"); | |
161 | return -ENXIO; | |
162 | } | |
163 | ||
164 | if (!strcmp("hvc", method)) { | |
165 | invoke_psci_fn = __invoke_psci_fn_hvc; | |
166 | } else if (!strcmp("smc", method)) { | |
167 | invoke_psci_fn = __invoke_psci_fn_smc; | |
168 | } else { | |
169 | pr_warn("invalid \"method\" property: %s\n", method); | |
170 | return -EINVAL; | |
171 | } | |
172 | return 0; | |
173 | } | |
174 | ||
175 | static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd) | |
176 | { | |
177 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); | |
178 | } | |
179 | ||
180 | static void psci_sys_poweroff(void) | |
181 | { | |
182 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); | |
183 | } | |
184 | ||
185 | /* | |
186 | * Detect the presence of a resident Trusted OS which may cause CPU_OFF to | |
187 | * return DENIED (which would be fatal). | |
188 | */ | |
189 | static void __init psci_init_migrate(void) | |
190 | { | |
191 | unsigned long cpuid; | |
192 | int type, cpu = -1; | |
193 | ||
194 | type = psci_ops.migrate_info_type(); | |
195 | ||
196 | if (type == PSCI_0_2_TOS_MP) { | |
197 | pr_info("Trusted OS migration not required\n"); | |
198 | return; | |
199 | } | |
200 | ||
201 | if (type == PSCI_RET_NOT_SUPPORTED) { | |
202 | pr_info("MIGRATE_INFO_TYPE not supported.\n"); | |
203 | return; | |
204 | } | |
205 | ||
206 | if (type != PSCI_0_2_TOS_UP_MIGRATE && | |
207 | type != PSCI_0_2_TOS_UP_NO_MIGRATE) { | |
208 | pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type); | |
209 | return; | |
210 | } | |
211 | ||
212 | cpuid = psci_migrate_info_up_cpu(); | |
213 | if (cpuid & ~MPIDR_HWID_BITMASK) { | |
214 | pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n", | |
215 | cpuid); | |
216 | return; | |
217 | } | |
218 | ||
219 | cpu = get_logical_index(cpuid); | |
220 | resident_cpu = cpu >= 0 ? cpu : -1; | |
221 | ||
222 | pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid); | |
223 | } | |
224 | ||
225 | static void __init psci_0_2_set_functions(void) | |
226 | { | |
227 | pr_info("Using standard PSCI v0.2 function IDs\n"); | |
5211df00 | 228 | psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN_NATIVE(CPU_SUSPEND); |
bff60792 MR |
229 | psci_ops.cpu_suspend = psci_cpu_suspend; |
230 | ||
231 | psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF; | |
232 | psci_ops.cpu_off = psci_cpu_off; | |
233 | ||
5211df00 | 234 | psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN_NATIVE(CPU_ON); |
bff60792 MR |
235 | psci_ops.cpu_on = psci_cpu_on; |
236 | ||
5211df00 | 237 | psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN_NATIVE(MIGRATE); |
bff60792 MR |
238 | psci_ops.migrate = psci_migrate; |
239 | ||
240 | psci_ops.affinity_info = psci_affinity_info; | |
241 | ||
242 | psci_ops.migrate_info_type = psci_migrate_info_type; | |
243 | ||
244 | arm_pm_restart = psci_sys_reset; | |
245 | ||
246 | pm_power_off = psci_sys_poweroff; | |
247 | } | |
248 | ||
249 | /* | |
250 | * Probe function for PSCI firmware versions >= 0.2 | |
251 | */ | |
252 | static int __init psci_probe(void) | |
253 | { | |
254 | u32 ver = psci_get_version(); | |
255 | ||
256 | pr_info("PSCIv%d.%d detected in firmware.\n", | |
257 | PSCI_VERSION_MAJOR(ver), | |
258 | PSCI_VERSION_MINOR(ver)); | |
259 | ||
260 | if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) { | |
261 | pr_err("Conflicting PSCI version detected.\n"); | |
262 | return -EINVAL; | |
263 | } | |
264 | ||
265 | psci_0_2_set_functions(); | |
266 | ||
267 | psci_init_migrate(); | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
272 | typedef int (*psci_initcall_t)(const struct device_node *); | |
273 | ||
274 | /* | |
275 | * PSCI init function for PSCI versions >=0.2 | |
276 | * | |
277 | * Probe based on PSCI PSCI_VERSION function | |
278 | */ | |
279 | static int __init psci_0_2_init(struct device_node *np) | |
280 | { | |
281 | int err; | |
282 | ||
283 | err = get_set_conduit_method(np); | |
284 | ||
285 | if (err) | |
286 | goto out_put_node; | |
287 | /* | |
288 | * Starting with v0.2, the PSCI specification introduced a call | |
289 | * (PSCI_VERSION) that allows probing the firmware version, so | |
290 | * that PSCI function IDs and version specific initialization | |
291 | * can be carried out according to the specific version reported | |
292 | * by firmware | |
293 | */ | |
294 | err = psci_probe(); | |
295 | ||
296 | out_put_node: | |
297 | of_node_put(np); | |
298 | return err; | |
299 | } | |
300 | ||
301 | /* | |
302 | * PSCI < v0.2 get PSCI Function IDs via DT. | |
303 | */ | |
304 | static int __init psci_0_1_init(struct device_node *np) | |
305 | { | |
306 | u32 id; | |
307 | int err; | |
308 | ||
309 | err = get_set_conduit_method(np); | |
310 | ||
311 | if (err) | |
312 | goto out_put_node; | |
313 | ||
314 | pr_info("Using PSCI v0.1 Function IDs from DT\n"); | |
315 | ||
316 | if (!of_property_read_u32(np, "cpu_suspend", &id)) { | |
317 | psci_function_id[PSCI_FN_CPU_SUSPEND] = id; | |
318 | psci_ops.cpu_suspend = psci_cpu_suspend; | |
319 | } | |
320 | ||
321 | if (!of_property_read_u32(np, "cpu_off", &id)) { | |
322 | psci_function_id[PSCI_FN_CPU_OFF] = id; | |
323 | psci_ops.cpu_off = psci_cpu_off; | |
324 | } | |
325 | ||
326 | if (!of_property_read_u32(np, "cpu_on", &id)) { | |
327 | psci_function_id[PSCI_FN_CPU_ON] = id; | |
328 | psci_ops.cpu_on = psci_cpu_on; | |
329 | } | |
330 | ||
331 | if (!of_property_read_u32(np, "migrate", &id)) { | |
332 | psci_function_id[PSCI_FN_MIGRATE] = id; | |
333 | psci_ops.migrate = psci_migrate; | |
334 | } | |
335 | ||
336 | out_put_node: | |
337 | of_node_put(np); | |
338 | return err; | |
339 | } | |
340 | ||
c706c7eb | 341 | static const struct of_device_id const psci_of_match[] __initconst = { |
bff60792 MR |
342 | { .compatible = "arm,psci", .data = psci_0_1_init}, |
343 | { .compatible = "arm,psci-0.2", .data = psci_0_2_init}, | |
344 | {}, | |
345 | }; | |
346 | ||
347 | int __init psci_dt_init(void) | |
348 | { | |
349 | struct device_node *np; | |
350 | const struct of_device_id *matched_np; | |
351 | psci_initcall_t init_fn; | |
352 | ||
353 | np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np); | |
354 | ||
355 | if (!np) | |
356 | return -ENODEV; | |
357 | ||
358 | init_fn = (psci_initcall_t)matched_np->data; | |
359 | return init_fn(np); | |
360 | } | |
361 | ||
362 | #ifdef CONFIG_ACPI | |
363 | /* | |
364 | * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's | |
365 | * explicitly clarified in SBBR | |
366 | */ | |
367 | int __init psci_acpi_init(void) | |
368 | { | |
369 | if (!acpi_psci_present()) { | |
370 | pr_info("is not implemented in ACPI.\n"); | |
371 | return -EOPNOTSUPP; | |
372 | } | |
373 | ||
374 | pr_info("probing for conduit method from ACPI.\n"); | |
375 | ||
376 | if (acpi_psci_use_hvc()) | |
377 | invoke_psci_fn = __invoke_psci_fn_hvc; | |
378 | else | |
379 | invoke_psci_fn = __invoke_psci_fn_smc; | |
380 | ||
381 | return psci_probe(); | |
382 | } | |
383 | #endif |