Merge tag 'v3.10.55' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / arm / kernel / psci.c
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) 2012 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>
20 #include <linux/reboot.h>
21 #include <linux/pm.h>
22 #include <uapi/linux/psci.h>
23
24 #include <asm/compiler.h>
25 #include <asm/errno.h>
26 #include <asm/opcodes-sec.h>
27 #include <asm/opcodes-virt.h>
28 #include <asm/psci.h>
29 #include <asm/system_misc.h>
30
31 struct psci_operations psci_ops;
32
33 static int (*invoke_psci_fn)(u32, u32, u32, u32);
34 typedef int (*psci_initcall_t)(const struct device_node *);
35
36 enum psci_function {
37 PSCI_FN_CPU_SUSPEND,
38 PSCI_FN_CPU_ON,
39 PSCI_FN_CPU_OFF,
40 PSCI_FN_MIGRATE,
41 PSCI_FN_AFFINITY_INFO,
42 PSCI_FN_MIGRATE_INFO_TYPE,
43 PSCI_FN_MAX,
44 };
45
46 static u32 psci_function_id[PSCI_FN_MAX];
47
48 static int psci_to_linux_errno(int errno)
49 {
50 switch (errno) {
51 case PSCI_RET_SUCCESS:
52 return 0;
53 case PSCI_RET_NOT_SUPPORTED:
54 return -EOPNOTSUPP;
55 case PSCI_RET_INVALID_PARAMS:
56 return -EINVAL;
57 case PSCI_RET_DENIED:
58 return -EPERM;
59 };
60
61 return -EINVAL;
62 }
63
64 static u32 psci_power_state_pack(struct psci_power_state state)
65 {
66 return ((state.id << PSCI_0_2_POWER_STATE_ID_SHIFT)
67 & PSCI_0_2_POWER_STATE_ID_MASK) |
68 ((state.type << PSCI_0_2_POWER_STATE_TYPE_SHIFT)
69 & PSCI_0_2_POWER_STATE_TYPE_MASK) |
70 ((state.affinity_level << PSCI_0_2_POWER_STATE_AFFL_SHIFT)
71 & PSCI_0_2_POWER_STATE_AFFL_MASK);
72 }
73
74 /*
75 * The following two functions are invoked via the invoke_psci_fn pointer
76 * and will not be inlined, allowing us to piggyback on the AAPCS.
77 */
78 static noinline int __invoke_psci_fn_hvc(u32 function_id, u32 arg0, u32 arg1,
79 u32 arg2)
80 {
81 asm volatile(
82 __asmeq("%0", "r0")
83 __asmeq("%1", "r1")
84 __asmeq("%2", "r2")
85 __asmeq("%3", "r3")
86 __HVC(0)
87 : "+r" (function_id)
88 : "r" (arg0), "r" (arg1), "r" (arg2));
89
90 return function_id;
91 }
92
93 static noinline int __invoke_psci_fn_smc(u32 function_id, u32 arg0, u32 arg1,
94 u32 arg2)
95 {
96 asm volatile(
97 __asmeq("%0", "r0")
98 __asmeq("%1", "r1")
99 __asmeq("%2", "r2")
100 __asmeq("%3", "r3")
101 __SMC(0)
102 : "+r" (function_id)
103 : "r" (arg0), "r" (arg1), "r" (arg2));
104
105 return function_id;
106 }
107
108 #define PSCI_VER_MAJOR_MASK 0xffff0000
109 #define PSCI_VER_MINOR_MASK 0x0000ffff
110 #define PSCI_VER_MAJOR_SHIFT 16
111 #define PSCI_VER_MAJOR(ver) \
112 ((ver & PSCI_VER_MAJOR_MASK) >> PSCI_VER_MAJOR_SHIFT)
113 #define PSCI_VER_MINOR(ver) (ver & PSCI_VER_MINOR_MASK)
114
115 static int psci_get_version(void)
116 {
117 int err;
118
119 err = invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
120 return err;
121 }
122
123 static int psci_cpu_suspend(struct psci_power_state state,
124 unsigned long entry_point)
125 {
126 int err;
127 u32 fn, power_state;
128
129 fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
130 power_state = psci_power_state_pack(state);
131 err = invoke_psci_fn(fn, power_state, entry_point, 0);
132 return psci_to_linux_errno(err);
133 }
134
135 static int psci_cpu_off(struct psci_power_state state)
136 {
137 int err;
138 u32 fn, power_state;
139
140 fn = psci_function_id[PSCI_FN_CPU_OFF];
141 power_state = psci_power_state_pack(state);
142 err = invoke_psci_fn(fn, power_state, 0, 0);
143 return psci_to_linux_errno(err);
144 }
145
146 static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
147 {
148 int err;
149 u32 fn;
150
151 fn = psci_function_id[PSCI_FN_CPU_ON];
152 err = invoke_psci_fn(fn, cpuid, entry_point, 0);
153 return psci_to_linux_errno(err);
154 }
155
156 static int psci_migrate(unsigned long cpuid)
157 {
158 int err;
159 u32 fn;
160
161 fn = psci_function_id[PSCI_FN_MIGRATE];
162 err = invoke_psci_fn(fn, cpuid, 0, 0);
163 return psci_to_linux_errno(err);
164 }
165
166 static int psci_affinity_info(unsigned long target_affinity,
167 unsigned long lowest_affinity_level)
168 {
169 int err;
170 u32 fn;
171
172 fn = psci_function_id[PSCI_FN_AFFINITY_INFO];
173 err = invoke_psci_fn(fn, target_affinity, lowest_affinity_level, 0);
174 return err;
175 }
176
177 static int psci_migrate_info_type(void)
178 {
179 int err;
180 u32 fn;
181
182 fn = psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE];
183 err = invoke_psci_fn(fn, 0, 0, 0);
184 return err;
185 }
186
187 static int get_set_conduit_method(struct device_node *np)
188 {
189 const char *method;
190
191 pr_info("probing for conduit method from DT.\n");
192
193 if (of_property_read_string(np, "method", &method)) {
194 pr_warn("missing \"method\" property\n");
195 return -ENXIO;
196 }
197
198 if (!strcmp("hvc", method)) {
199 invoke_psci_fn = __invoke_psci_fn_hvc;
200 } else if (!strcmp("smc", method)) {
201 invoke_psci_fn = __invoke_psci_fn_smc;
202 } else {
203 pr_warn("invalid \"method\" property: %s\n", method);
204 return -EINVAL;
205 }
206 return 0;
207 }
208
209 static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
210 {
211 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
212 }
213
214 static void psci_sys_poweroff(void)
215 {
216 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
217 }
218
219 /*
220 * PSCI Function IDs for v0.2+ are well defined so use
221 * standard values.
222 */
223 static int psci_0_2_init(struct device_node *np)
224 {
225 int err, ver;
226
227 err = get_set_conduit_method(np);
228
229 if (err)
230 goto out_put_node;
231
232 ver = psci_get_version();
233
234 if (ver == PSCI_RET_NOT_SUPPORTED) {
235 /* PSCI v0.2 mandates implementation of PSCI_ID_VERSION. */
236 pr_err("PSCI firmware does not comply with the v0.2 spec.\n");
237 err = -EOPNOTSUPP;
238 goto out_put_node;
239 } else {
240 pr_info("PSCIv%d.%d detected in firmware.\n",
241 PSCI_VERSION_MAJOR(ver),
242 PSCI_VERSION_MINOR(ver));
243
244 if (PSCI_VERSION_MAJOR(ver) == 0 &&
245 PSCI_VERSION_MINOR(ver) < 2) {
246 err = -EINVAL;
247 pr_err("Conflicting PSCI version detected.\n");
248 goto out_put_node;
249 }
250 }
251
252 pr_info("Using standard PSCI v0.2 function IDs\n");
253 psci_function_id[PSCI_FN_CPU_SUSPEND] = PSCI_0_2_FN_CPU_SUSPEND;
254 psci_ops.cpu_suspend = psci_cpu_suspend;
255
256 psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
257 psci_ops.cpu_off = psci_cpu_off;
258
259 psci_function_id[PSCI_FN_CPU_ON] = PSCI_0_2_FN_CPU_ON;
260 psci_ops.cpu_on = psci_cpu_on;
261
262 psci_function_id[PSCI_FN_MIGRATE] = PSCI_0_2_FN_MIGRATE;
263 psci_ops.migrate = psci_migrate;
264
265 psci_function_id[PSCI_FN_AFFINITY_INFO] = PSCI_0_2_FN_AFFINITY_INFO;
266 psci_ops.affinity_info = psci_affinity_info;
267
268 psci_function_id[PSCI_FN_MIGRATE_INFO_TYPE] =
269 PSCI_0_2_FN_MIGRATE_INFO_TYPE;
270 psci_ops.migrate_info_type = psci_migrate_info_type;
271
272 //TODO: verify how arm_pm_restart and pm_power_off works with atf
273 //arm_pm_restart = psci_sys_reset;
274
275 //pm_power_off = psci_sys_poweroff;
276
277 out_put_node:
278 of_node_put(np);
279 return err;
280 }
281
282 /*
283 * PSCI < v0.2 get PSCI Function IDs via DT.
284 */
285 static int psci_0_1_init(struct device_node *np)
286 {
287 u32 id;
288 int err;
289
290 err = get_set_conduit_method(np);
291
292 if (err)
293 goto out_put_node;
294
295 pr_info("Using PSCI v0.1 Function IDs from DT\n");
296
297 if (!of_property_read_u32(np, "cpu_suspend", &id)) {
298 psci_function_id[PSCI_FN_CPU_SUSPEND] = id;
299 psci_ops.cpu_suspend = psci_cpu_suspend;
300 }
301
302 if (!of_property_read_u32(np, "cpu_off", &id)) {
303 psci_function_id[PSCI_FN_CPU_OFF] = id;
304 psci_ops.cpu_off = psci_cpu_off;
305 }
306
307 if (!of_property_read_u32(np, "cpu_on", &id)) {
308 psci_function_id[PSCI_FN_CPU_ON] = id;
309 psci_ops.cpu_on = psci_cpu_on;
310 }
311
312 if (!of_property_read_u32(np, "migrate", &id)) {
313 psci_function_id[PSCI_FN_MIGRATE] = id;
314 psci_ops.migrate = psci_migrate;
315 }
316
317 out_put_node:
318 of_node_put(np);
319 return err;
320 }
321
322 static const struct of_device_id psci_of_match[] __initconst = {
323 { .compatible = "arm,psci", .data = psci_0_1_init},
324 { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
325 {},
326 };
327
328 int __init psci_init(void)
329 {
330 struct device_node *np;
331 const struct of_device_id *matched_np;
332 psci_initcall_t init_fn;
333
334 np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
335 if (!np)
336 return -ENODEV;
337
338 init_fn = (psci_initcall_t)matched_np->data;
339 return init_fn(np);
340 }