blob: 6e0517720a9cfe19356ebf4a9cf489be0a8cafc2 [file] [log] [blame]
Stephen Boydcebbffe2013-04-09 10:57:54 -07001/* Copyright (c) 2010-2013, The Linux Foundation. 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 void scm_inv_range(unsigned long start, unsigned long end)
208{
Stepan Moskovchenko622c7702012-11-16 17:17:18 -0800209 u32 cacheline_size, ctr;
210
211 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
212 cacheline_size = 4 << ((ctr >> 16) & 0xf);
213
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 start = round_down(start, cacheline_size);
215 end = round_up(end, cacheline_size);
Stephen Boyda968b282012-10-17 17:43:19 -0700216 outer_inv_range(start, end);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 while (start < end) {
218 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
219 : "memory");
220 start += cacheline_size;
221 }
222 dsb();
223 isb();
224}
225
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700226/**
227 * scm_call() - Send an SCM command
228 * @svc_id: service identifier
229 * @cmd_id: command identifier
230 * @cmd_buf: command buffer
231 * @cmd_len: length of the command buffer
232 * @resp_buf: response buffer
233 * @resp_len: length of the response buffer
234 *
235 * Sends a command to the SCM and waits for the command to finish processing.
236 */
237int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
238 void *resp_buf, size_t resp_len)
239{
240 int ret;
241 struct scm_command *cmd;
242 struct scm_response *rsp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243 unsigned long start, end;
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700244
245 cmd = alloc_scm_command(cmd_len, resp_len);
246 if (!cmd)
247 return -ENOMEM;
248
249 cmd->id = (svc_id << 10) | cmd_id;
250 if (cmd_buf)
251 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
252
253 mutex_lock(&scm_lock);
254 ret = __scm_call(cmd);
255 mutex_unlock(&scm_lock);
256 if (ret)
257 goto out;
258
259 rsp = scm_command_to_response(cmd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 start = (unsigned long)rsp;
261
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700262 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 scm_inv_range(start, start + sizeof(*rsp));
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700264 } while (!rsp->is_complete);
265
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
267 scm_inv_range(start, end);
268
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700269 if (resp_buf)
270 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
271out:
272 free_scm_command(cmd);
273 return ret;
274}
275EXPORT_SYMBOL(scm_call);
276
Stephen Boyd551cd962011-07-18 10:33:45 -0700277#define SCM_CLASS_REGISTER (0x2 << 8)
278#define SCM_MASK_IRQS BIT(5)
279#define SCM_ATOMIC(svc, cmd, n) (((((svc) << 10)|((cmd) & 0x3ff)) << 12) | \
280 SCM_CLASS_REGISTER | \
281 SCM_MASK_IRQS | \
282 (n & 0xf))
283
284/**
285 * scm_call_atomic1() - Send an atomic SCM command with one argument
286 * @svc_id: service identifier
287 * @cmd_id: command identifier
288 * @arg1: first argument
289 *
290 * This shall only be used with commands that are guaranteed to be
291 * uninterruptable, atomic and SMP safe.
292 */
Stephen Boydc2a77182011-12-12 13:52:17 -0800293s32 scm_call_atomic1(u32 svc, u32 cmd, u32 arg1)
Stephen Boyd551cd962011-07-18 10:33:45 -0700294{
295 int context_id;
296 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 1);
297 register u32 r1 asm("r1") = (u32)&context_id;
298 register u32 r2 asm("r2") = arg1;
299
300 asm volatile(
301 __asmeq("%0", "r0")
302 __asmeq("%1", "r0")
303 __asmeq("%2", "r1")
304 __asmeq("%3", "r2")
Steve Muckle6dff9572012-06-25 17:37:50 -0700305#ifdef REQUIRES_SEC
306 ".arch_extension sec\n"
307#endif
Stephen Boyd551cd962011-07-18 10:33:45 -0700308 "smc #0 @ switch to secure world\n"
309 : "=r" (r0)
310 : "r" (r0), "r" (r1), "r" (r2)
311 : "r3");
312 return r0;
313}
314EXPORT_SYMBOL(scm_call_atomic1);
315
316/**
317 * scm_call_atomic2() - Send an atomic SCM command with two arguments
318 * @svc_id: service identifier
319 * @cmd_id: command identifier
320 * @arg1: first argument
321 * @arg2: second argument
322 *
323 * This shall only be used with commands that are guaranteed to be
324 * uninterruptable, atomic and SMP safe.
325 */
Stephen Boydc2a77182011-12-12 13:52:17 -0800326s32 scm_call_atomic2(u32 svc, u32 cmd, u32 arg1, u32 arg2)
Stephen Boyd551cd962011-07-18 10:33:45 -0700327{
328 int context_id;
329 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 2);
330 register u32 r1 asm("r1") = (u32)&context_id;
331 register u32 r2 asm("r2") = arg1;
332 register u32 r3 asm("r3") = arg2;
333
334 asm volatile(
335 __asmeq("%0", "r0")
336 __asmeq("%1", "r0")
337 __asmeq("%2", "r1")
338 __asmeq("%3", "r2")
339 __asmeq("%4", "r3")
Steve Muckle6dff9572012-06-25 17:37:50 -0700340#ifdef REQUIRES_SEC
341 ".arch_extension sec\n"
342#endif
Stephen Boyd551cd962011-07-18 10:33:45 -0700343 "smc #0 @ switch to secure world\n"
344 : "=r" (r0)
345 : "r" (r0), "r" (r1), "r" (r2), "r" (r3));
346 return r0;
347}
348EXPORT_SYMBOL(scm_call_atomic2);
349
Stephen Boydcebbffe2013-04-09 10:57:54 -0700350/**
351 * scm_call_atomic3() - Send an atomic SCM command with three arguments
352 * @svc_id: service identifier
353 * @cmd_id: command identifier
354 * @arg1: first argument
355 * @arg2: second argument
356 * @arg3: third argument
357 *
358 * This shall only be used with commands that are guaranteed to be
359 * uninterruptable, atomic and SMP safe.
360 */
361s32 scm_call_atomic3(u32 svc, u32 cmd, u32 arg1, u32 arg2, u32 arg3)
362{
363 int context_id;
364 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 3);
365 register u32 r1 asm("r1") = (u32)&context_id;
366 register u32 r2 asm("r2") = arg1;
367 register u32 r3 asm("r3") = arg2;
368 register u32 r4 asm("r4") = arg3;
369
370 asm volatile(
371 __asmeq("%0", "r0")
372 __asmeq("%1", "r0")
373 __asmeq("%2", "r1")
374 __asmeq("%3", "r2")
375 __asmeq("%4", "r3")
376 __asmeq("%5", "r4")
377#ifdef REQUIRES_SEC
378 ".arch_extension sec\n"
379#endif
380 "smc #0 @ switch to secure world\n"
381 : "=r" (r0)
382 : "r" (r0), "r" (r1), "r" (r2), "r" (r3), "r" (r4));
383 return r0;
384}
385EXPORT_SYMBOL(scm_call_atomic3);
386
Stephen Boydcb053de2011-12-15 13:32:43 -0800387s32 scm_call_atomic4_3(u32 svc, u32 cmd, u32 arg1, u32 arg2,
388 u32 arg3, u32 arg4, u32 *ret1, u32 *ret2)
389{
390 int ret;
391 int context_id;
392 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 4);
393 register u32 r1 asm("r1") = (u32)&context_id;
394 register u32 r2 asm("r2") = arg1;
395 register u32 r3 asm("r3") = arg2;
396 register u32 r4 asm("r4") = arg3;
397 register u32 r5 asm("r5") = arg4;
398
399 asm volatile(
400 __asmeq("%0", "r0")
401 __asmeq("%1", "r1")
402 __asmeq("%2", "r2")
403 __asmeq("%3", "r0")
404 __asmeq("%4", "r1")
405 __asmeq("%5", "r2")
406 __asmeq("%6", "r3")
Steve Muckle6dff9572012-06-25 17:37:50 -0700407#ifdef REQUIRES_SEC
408 ".arch_extension sec\n"
409#endif
Stephen Boydcb053de2011-12-15 13:32:43 -0800410 "smc #0 @ switch to secure world\n"
411 : "=r" (r0), "=r" (r1), "=r" (r2)
412 : "r" (r0), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5));
413 ret = r0;
414 if (ret1)
415 *ret1 = r1;
416 if (ret2)
417 *ret2 = r2;
418 return r0;
419}
420EXPORT_SYMBOL(scm_call_atomic4_3);
421
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700422u32 scm_get_version(void)
423{
424 int context_id;
425 static u32 version = -1;
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800426 register u32 r0 asm("r0");
427 register u32 r1 asm("r1");
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700428
429 if (version != -1)
430 return version;
431
432 mutex_lock(&scm_lock);
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800433
434 r0 = 0x1 << 8;
435 r1 = (u32)&context_id;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800436 do {
437 asm volatile(
438 __asmeq("%0", "r0")
439 __asmeq("%1", "r1")
440 __asmeq("%2", "r0")
441 __asmeq("%3", "r1")
Steve Muckle6dff9572012-06-25 17:37:50 -0700442#ifdef REQUIRES_SEC
443 ".arch_extension sec\n"
444#endif
Stephen Boyd8e76a802011-02-24 10:44:44 -0800445 "smc #0 @ switch to secure world\n"
446 : "=r" (r0), "=r" (r1)
447 : "r" (r0), "r" (r1)
448 : "r2", "r3");
449 } while (r0 == SCM_INTERRUPTED);
450
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700451 version = r1;
452 mutex_unlock(&scm_lock);
453
454 return version;
455}
456EXPORT_SYMBOL(scm_get_version);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457
Saravana Kannan988feaf2011-09-07 19:57:06 -0700458#define IS_CALL_AVAIL_CMD 1
459int scm_is_call_available(u32 svc_id, u32 cmd_id)
460{
461 int ret;
462 u32 svc_cmd = (svc_id << 10) | cmd_id;
463 u32 ret_val = 0;
464
465 ret = scm_call(SCM_SVC_INFO, IS_CALL_AVAIL_CMD, &svc_cmd,
466 sizeof(svc_cmd), &ret_val, sizeof(ret_val));
467 if (ret)
468 return ret;
469
470 return ret_val;
471}
472EXPORT_SYMBOL(scm_is_call_available);
473
Stephen Boydc085f9b2012-02-29 17:52:52 -0800474#define GET_FEAT_VERSION_CMD 3
475int scm_get_feat_version(u32 feat)
476{
477 if (scm_is_call_available(SCM_SVC_INFO, GET_FEAT_VERSION_CMD)) {
478 u32 version;
479 if (!scm_call(SCM_SVC_INFO, GET_FEAT_VERSION_CMD, &feat,
480 sizeof(feat), &version, sizeof(version)))
481 return version;
482 }
483 return 0;
484}
485EXPORT_SYMBOL(scm_get_feat_version);
486