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