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