blob: 6013efc47b16bee396836f31d9dd994b1a52bdc8 [file] [log] [blame]
Stephen Boydcb053de2011-12-15 13:32:43 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Stephen Boyd2a1eb582010-08-27 10:01:23 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
Stephen Boyd2a1eb582010-08-27 10:01:23 -070011 */
12
13#include <linux/slab.h>
14#include <linux/io.h>
15#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/errno.h>
18#include <linux/err.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <linux/init.h>
Stephen Boyd2a1eb582010-08-27 10:01:23 -070020
21#include <asm/cacheflush.h>
22
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <mach/scm.h>
Stephen Boyd2a1eb582010-08-27 10:01:23 -070024
25#define SCM_ENOMEM -5
26#define SCM_EOPNOTSUPP -4
27#define SCM_EINVAL_ADDR -3
28#define SCM_EINVAL_ARG -2
29#define SCM_ERROR -1
30#define SCM_INTERRUPTED 1
31
32static DEFINE_MUTEX(scm_lock);
33
34/**
35 * struct scm_command - one SCM command buffer
36 * @len: total available memory for command and response
37 * @buf_offset: start of command buffer
38 * @resp_hdr_offset: start of response buffer
39 * @id: command to be executed
40 * @buf: buffer returned from scm_get_command_buffer()
41 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030042 * An SCM command is laid out in memory as follows:
Stephen Boyd2a1eb582010-08-27 10:01:23 -070043 *
44 * ------------------- <--- struct scm_command
45 * | command header |
46 * ------------------- <--- scm_get_command_buffer()
47 * | command buffer |
48 * ------------------- <--- struct scm_response and
49 * | response header | scm_command_to_response()
50 * ------------------- <--- scm_get_response_buffer()
51 * | response buffer |
52 * -------------------
53 *
54 * There can be arbitrary padding between the headers and buffers so
55 * you should always use the appropriate scm_get_*_buffer() routines
56 * to access the buffers in a safe manner.
57 */
58struct scm_command {
59 u32 len;
60 u32 buf_offset;
61 u32 resp_hdr_offset;
62 u32 id;
63 u32 buf[0];
64};
65
66/**
67 * struct scm_response - one SCM response buffer
68 * @len: total available memory for response
69 * @buf_offset: start of response data relative to start of scm_response
70 * @is_complete: indicates if the command has finished processing
71 */
72struct scm_response {
73 u32 len;
74 u32 buf_offset;
75 u32 is_complete;
76};
77
78/**
79 * alloc_scm_command() - Allocate an SCM command
80 * @cmd_size: size of the command buffer
81 * @resp_size: size of the response buffer
82 *
83 * Allocate an SCM command, including enough room for the command
84 * and response headers as well as the command and response buffers.
85 *
86 * Returns a valid &scm_command on success or %NULL if the allocation fails.
87 */
88static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
89{
90 struct scm_command *cmd;
91 size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
92 resp_size;
93
94 cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
95 if (cmd) {
96 cmd->len = len;
97 cmd->buf_offset = offsetof(struct scm_command, buf);
98 cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
99 }
100 return cmd;
101}
102
103/**
104 * free_scm_command() - Free an SCM command
105 * @cmd: command to free
106 *
107 * Free an SCM command.
108 */
109static inline void free_scm_command(struct scm_command *cmd)
110{
111 kfree(cmd);
112}
113
114/**
115 * scm_command_to_response() - Get a pointer to a scm_response
116 * @cmd: command
117 *
118 * Returns a pointer to a response for a command.
119 */
120static inline struct scm_response *scm_command_to_response(
121 const struct scm_command *cmd)
122{
123 return (void *)cmd + cmd->resp_hdr_offset;
124}
125
126/**
127 * scm_get_command_buffer() - Get a pointer to a command buffer
128 * @cmd: command
129 *
130 * Returns a pointer to the command buffer of a command.
131 */
132static inline void *scm_get_command_buffer(const struct scm_command *cmd)
133{
134 return (void *)cmd->buf;
135}
136
137/**
138 * scm_get_response_buffer() - Get a pointer to a response buffer
139 * @rsp: response
140 *
141 * Returns a pointer to a response buffer of a response.
142 */
143static inline void *scm_get_response_buffer(const struct scm_response *rsp)
144{
145 return (void *)rsp + rsp->buf_offset;
146}
147
148static int scm_remap_error(int err)
149{
150 switch (err) {
151 case SCM_ERROR:
152 return -EIO;
153 case SCM_EINVAL_ADDR:
154 case SCM_EINVAL_ARG:
155 return -EINVAL;
156 case SCM_EOPNOTSUPP:
157 return -EOPNOTSUPP;
158 case SCM_ENOMEM:
159 return -ENOMEM;
160 }
161 return -EINVAL;
162}
163
164static u32 smc(u32 cmd_addr)
165{
166 int context_id;
167 register u32 r0 asm("r0") = 1;
168 register u32 r1 asm("r1") = (u32)&context_id;
169 register u32 r2 asm("r2") = cmd_addr;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800170 do {
171 asm volatile(
172 __asmeq("%0", "r0")
173 __asmeq("%1", "r0")
174 __asmeq("%2", "r1")
175 __asmeq("%3", "r2")
Marc Zyngiereca55f42011-11-08 13:07:36 +0000176#ifdef REQUIRES_SEC
177 ".arch_extension sec\n"
178#endif
Stephen Boyd8e76a802011-02-24 10:44:44 -0800179 "smc #0 @ switch to secure world\n"
180 : "=r" (r0)
181 : "r" (r0), "r" (r1), "r" (r2)
182 : "r3");
183 } while (r0 == SCM_INTERRUPTED);
184
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700185 return r0;
186}
187
188static int __scm_call(const struct scm_command *cmd)
189{
190 int ret;
191 u32 cmd_addr = virt_to_phys(cmd);
192
193 /*
194 * Flush the entire cache here so callers don't have to remember
195 * to flush the cache when passing physical addresses to the secure
196 * side in the buffer.
197 */
198 flush_cache_all();
Stephen Boyda968b282012-10-17 17:43:19 -0700199 outer_flush_all();
Stephen Boyd8e76a802011-02-24 10:44:44 -0800200 ret = smc(cmd_addr);
201 if (ret < 0)
202 ret = scm_remap_error(ret);
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700203
204 return ret;
205}
206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207static u32 cacheline_size;
208
209static void scm_inv_range(unsigned long start, unsigned long end)
210{
211 start = round_down(start, cacheline_size);
212 end = round_up(end, cacheline_size);
Stephen Boyda968b282012-10-17 17:43:19 -0700213 outer_inv_range(start, end);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 while (start < end) {
215 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
216 : "memory");
217 start += cacheline_size;
218 }
219 dsb();
220 isb();
221}
222
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700223/**
224 * scm_call() - Send an SCM command
225 * @svc_id: service identifier
226 * @cmd_id: command identifier
227 * @cmd_buf: command buffer
228 * @cmd_len: length of the command buffer
229 * @resp_buf: response buffer
230 * @resp_len: length of the response buffer
231 *
232 * Sends a command to the SCM and waits for the command to finish processing.
233 */
234int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
235 void *resp_buf, size_t resp_len)
236{
237 int ret;
238 struct scm_command *cmd;
239 struct scm_response *rsp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 unsigned long start, end;
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700241
242 cmd = alloc_scm_command(cmd_len, resp_len);
243 if (!cmd)
244 return -ENOMEM;
245
246 cmd->id = (svc_id << 10) | cmd_id;
247 if (cmd_buf)
248 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
249
250 mutex_lock(&scm_lock);
251 ret = __scm_call(cmd);
252 mutex_unlock(&scm_lock);
253 if (ret)
254 goto out;
255
256 rsp = scm_command_to_response(cmd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 start = (unsigned long)rsp;
258
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700259 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 scm_inv_range(start, start + sizeof(*rsp));
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700261 } while (!rsp->is_complete);
262
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
264 scm_inv_range(start, end);
265
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700266 if (resp_buf)
267 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
268out:
269 free_scm_command(cmd);
270 return ret;
271}
272EXPORT_SYMBOL(scm_call);
273
Stephen Boyd551cd962011-07-18 10:33:45 -0700274#define SCM_CLASS_REGISTER (0x2 << 8)
275#define SCM_MASK_IRQS BIT(5)
276#define SCM_ATOMIC(svc, cmd, n) (((((svc) << 10)|((cmd) & 0x3ff)) << 12) | \
277 SCM_CLASS_REGISTER | \
278 SCM_MASK_IRQS | \
279 (n & 0xf))
280
281/**
282 * scm_call_atomic1() - Send an atomic SCM command with one argument
283 * @svc_id: service identifier
284 * @cmd_id: command identifier
285 * @arg1: first argument
286 *
287 * This shall only be used with commands that are guaranteed to be
288 * uninterruptable, atomic and SMP safe.
289 */
Stephen Boydc2a77182011-12-12 13:52:17 -0800290s32 scm_call_atomic1(u32 svc, u32 cmd, u32 arg1)
Stephen Boyd551cd962011-07-18 10:33:45 -0700291{
292 int context_id;
293 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 1);
294 register u32 r1 asm("r1") = (u32)&context_id;
295 register u32 r2 asm("r2") = arg1;
296
297 asm volatile(
298 __asmeq("%0", "r0")
299 __asmeq("%1", "r0")
300 __asmeq("%2", "r1")
301 __asmeq("%3", "r2")
Steve Muckle6dff9572012-06-25 17:37:50 -0700302#ifdef REQUIRES_SEC
303 ".arch_extension sec\n"
304#endif
Stephen Boyd551cd962011-07-18 10:33:45 -0700305 "smc #0 @ switch to secure world\n"
306 : "=r" (r0)
307 : "r" (r0), "r" (r1), "r" (r2)
308 : "r3");
309 return r0;
310}
311EXPORT_SYMBOL(scm_call_atomic1);
312
313/**
314 * scm_call_atomic2() - Send an atomic SCM command with two arguments
315 * @svc_id: service identifier
316 * @cmd_id: command identifier
317 * @arg1: first argument
318 * @arg2: second argument
319 *
320 * This shall only be used with commands that are guaranteed to be
321 * uninterruptable, atomic and SMP safe.
322 */
Stephen Boydc2a77182011-12-12 13:52:17 -0800323s32 scm_call_atomic2(u32 svc, u32 cmd, u32 arg1, u32 arg2)
Stephen Boyd551cd962011-07-18 10:33:45 -0700324{
325 int context_id;
326 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 2);
327 register u32 r1 asm("r1") = (u32)&context_id;
328 register u32 r2 asm("r2") = arg1;
329 register u32 r3 asm("r3") = arg2;
330
331 asm volatile(
332 __asmeq("%0", "r0")
333 __asmeq("%1", "r0")
334 __asmeq("%2", "r1")
335 __asmeq("%3", "r2")
336 __asmeq("%4", "r3")
Steve Muckle6dff9572012-06-25 17:37:50 -0700337#ifdef REQUIRES_SEC
338 ".arch_extension sec\n"
339#endif
Stephen Boyd551cd962011-07-18 10:33:45 -0700340 "smc #0 @ switch to secure world\n"
341 : "=r" (r0)
342 : "r" (r0), "r" (r1), "r" (r2), "r" (r3));
343 return r0;
344}
345EXPORT_SYMBOL(scm_call_atomic2);
346
Stephen Boydcb053de2011-12-15 13:32:43 -0800347s32 scm_call_atomic4_3(u32 svc, u32 cmd, u32 arg1, u32 arg2,
348 u32 arg3, u32 arg4, u32 *ret1, u32 *ret2)
349{
350 int ret;
351 int context_id;
352 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 4);
353 register u32 r1 asm("r1") = (u32)&context_id;
354 register u32 r2 asm("r2") = arg1;
355 register u32 r3 asm("r3") = arg2;
356 register u32 r4 asm("r4") = arg3;
357 register u32 r5 asm("r5") = arg4;
358
359 asm volatile(
360 __asmeq("%0", "r0")
361 __asmeq("%1", "r1")
362 __asmeq("%2", "r2")
363 __asmeq("%3", "r0")
364 __asmeq("%4", "r1")
365 __asmeq("%5", "r2")
366 __asmeq("%6", "r3")
Steve Muckle6dff9572012-06-25 17:37:50 -0700367#ifdef REQUIRES_SEC
368 ".arch_extension sec\n"
369#endif
Stephen Boydcb053de2011-12-15 13:32:43 -0800370 "smc #0 @ switch to secure world\n"
371 : "=r" (r0), "=r" (r1), "=r" (r2)
372 : "r" (r0), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5));
373 ret = r0;
374 if (ret1)
375 *ret1 = r1;
376 if (ret2)
377 *ret2 = r2;
378 return r0;
379}
380EXPORT_SYMBOL(scm_call_atomic4_3);
381
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700382u32 scm_get_version(void)
383{
384 int context_id;
385 static u32 version = -1;
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800386 register u32 r0 asm("r0");
387 register u32 r1 asm("r1");
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700388
389 if (version != -1)
390 return version;
391
392 mutex_lock(&scm_lock);
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800393
394 r0 = 0x1 << 8;
395 r1 = (u32)&context_id;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800396 do {
397 asm volatile(
398 __asmeq("%0", "r0")
399 __asmeq("%1", "r1")
400 __asmeq("%2", "r0")
401 __asmeq("%3", "r1")
Steve Muckle6dff9572012-06-25 17:37:50 -0700402#ifdef REQUIRES_SEC
403 ".arch_extension sec\n"
404#endif
Stephen Boyd8e76a802011-02-24 10:44:44 -0800405 "smc #0 @ switch to secure world\n"
406 : "=r" (r0), "=r" (r1)
407 : "r" (r0), "r" (r1)
408 : "r2", "r3");
409 } while (r0 == SCM_INTERRUPTED);
410
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700411 version = r1;
412 mutex_unlock(&scm_lock);
413
414 return version;
415}
416EXPORT_SYMBOL(scm_get_version);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417
Saravana Kannan988feaf2011-09-07 19:57:06 -0700418#define IS_CALL_AVAIL_CMD 1
419int scm_is_call_available(u32 svc_id, u32 cmd_id)
420{
421 int ret;
422 u32 svc_cmd = (svc_id << 10) | cmd_id;
423 u32 ret_val = 0;
424
425 ret = scm_call(SCM_SVC_INFO, IS_CALL_AVAIL_CMD, &svc_cmd,
426 sizeof(svc_cmd), &ret_val, sizeof(ret_val));
427 if (ret)
428 return ret;
429
430 return ret_val;
431}
432EXPORT_SYMBOL(scm_is_call_available);
433
Stephen Boydc085f9b2012-02-29 17:52:52 -0800434#define GET_FEAT_VERSION_CMD 3
435int scm_get_feat_version(u32 feat)
436{
437 if (scm_is_call_available(SCM_SVC_INFO, GET_FEAT_VERSION_CMD)) {
438 u32 version;
439 if (!scm_call(SCM_SVC_INFO, GET_FEAT_VERSION_CMD, &feat,
440 sizeof(feat), &version, sizeof(version)))
441 return version;
442 }
443 return 0;
444}
445EXPORT_SYMBOL(scm_get_feat_version);
446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447static int scm_init(void)
448{
449 u32 ctr;
450
451 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
452 cacheline_size = 4 << ((ctr >> 16) & 0xf);
453
454 return 0;
455}
456early_initcall(scm_init);