blob: 605291881114f604e1682fced8056ce795a1e179 [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 Boyd8e76a802011-02-24 10:44:44 -0800199 ret = smc(cmd_addr);
200 if (ret < 0)
201 ret = scm_remap_error(ret);
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700202
203 return ret;
204}
205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206static u32 cacheline_size;
207
208static void scm_inv_range(unsigned long start, unsigned long end)
209{
210 start = round_down(start, cacheline_size);
211 end = round_up(end, cacheline_size);
212 while (start < end) {
213 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
214 : "memory");
215 start += cacheline_size;
216 }
217 dsb();
218 isb();
219}
220
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700221/**
222 * scm_call() - Send an SCM command
223 * @svc_id: service identifier
224 * @cmd_id: command identifier
225 * @cmd_buf: command buffer
226 * @cmd_len: length of the command buffer
227 * @resp_buf: response buffer
228 * @resp_len: length of the response buffer
229 *
230 * Sends a command to the SCM and waits for the command to finish processing.
231 */
232int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
233 void *resp_buf, size_t resp_len)
234{
235 int ret;
236 struct scm_command *cmd;
237 struct scm_response *rsp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 unsigned long start, end;
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700239
240 cmd = alloc_scm_command(cmd_len, resp_len);
241 if (!cmd)
242 return -ENOMEM;
243
244 cmd->id = (svc_id << 10) | cmd_id;
245 if (cmd_buf)
246 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
247
248 mutex_lock(&scm_lock);
249 ret = __scm_call(cmd);
250 mutex_unlock(&scm_lock);
251 if (ret)
252 goto out;
253
254 rsp = scm_command_to_response(cmd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 start = (unsigned long)rsp;
256
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700257 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 scm_inv_range(start, start + sizeof(*rsp));
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700259 } while (!rsp->is_complete);
260
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
262 scm_inv_range(start, end);
263
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700264 if (resp_buf)
265 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
266out:
267 free_scm_command(cmd);
268 return ret;
269}
270EXPORT_SYMBOL(scm_call);
271
Stephen Boyd551cd962011-07-18 10:33:45 -0700272#define SCM_CLASS_REGISTER (0x2 << 8)
273#define SCM_MASK_IRQS BIT(5)
274#define SCM_ATOMIC(svc, cmd, n) (((((svc) << 10)|((cmd) & 0x3ff)) << 12) | \
275 SCM_CLASS_REGISTER | \
276 SCM_MASK_IRQS | \
277 (n & 0xf))
278
279/**
280 * scm_call_atomic1() - Send an atomic SCM command with one argument
281 * @svc_id: service identifier
282 * @cmd_id: command identifier
283 * @arg1: first argument
284 *
285 * This shall only be used with commands that are guaranteed to be
286 * uninterruptable, atomic and SMP safe.
287 */
Stephen Boydc2a77182011-12-12 13:52:17 -0800288s32 scm_call_atomic1(u32 svc, u32 cmd, u32 arg1)
Stephen Boyd551cd962011-07-18 10:33:45 -0700289{
290 int context_id;
291 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 1);
292 register u32 r1 asm("r1") = (u32)&context_id;
293 register u32 r2 asm("r2") = arg1;
294
295 asm volatile(
296 __asmeq("%0", "r0")
297 __asmeq("%1", "r0")
298 __asmeq("%2", "r1")
299 __asmeq("%3", "r2")
Steve Muckle6dff9572012-06-25 17:37:50 -0700300#ifdef REQUIRES_SEC
301 ".arch_extension sec\n"
302#endif
Stephen Boyd551cd962011-07-18 10:33:45 -0700303 "smc #0 @ switch to secure world\n"
304 : "=r" (r0)
305 : "r" (r0), "r" (r1), "r" (r2)
306 : "r3");
307 return r0;
308}
309EXPORT_SYMBOL(scm_call_atomic1);
310
311/**
312 * scm_call_atomic2() - Send an atomic SCM command with two arguments
313 * @svc_id: service identifier
314 * @cmd_id: command identifier
315 * @arg1: first argument
316 * @arg2: second argument
317 *
318 * This shall only be used with commands that are guaranteed to be
319 * uninterruptable, atomic and SMP safe.
320 */
Stephen Boydc2a77182011-12-12 13:52:17 -0800321s32 scm_call_atomic2(u32 svc, u32 cmd, u32 arg1, u32 arg2)
Stephen Boyd551cd962011-07-18 10:33:45 -0700322{
323 int context_id;
324 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 2);
325 register u32 r1 asm("r1") = (u32)&context_id;
326 register u32 r2 asm("r2") = arg1;
327 register u32 r3 asm("r3") = arg2;
328
329 asm volatile(
330 __asmeq("%0", "r0")
331 __asmeq("%1", "r0")
332 __asmeq("%2", "r1")
333 __asmeq("%3", "r2")
334 __asmeq("%4", "r3")
Steve Muckle6dff9572012-06-25 17:37:50 -0700335#ifdef REQUIRES_SEC
336 ".arch_extension sec\n"
337#endif
Stephen Boyd551cd962011-07-18 10:33:45 -0700338 "smc #0 @ switch to secure world\n"
339 : "=r" (r0)
340 : "r" (r0), "r" (r1), "r" (r2), "r" (r3));
341 return r0;
342}
343EXPORT_SYMBOL(scm_call_atomic2);
344
Stephen Boydcb053de2011-12-15 13:32:43 -0800345s32 scm_call_atomic4_3(u32 svc, u32 cmd, u32 arg1, u32 arg2,
346 u32 arg3, u32 arg4, u32 *ret1, u32 *ret2)
347{
348 int ret;
349 int context_id;
350 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 4);
351 register u32 r1 asm("r1") = (u32)&context_id;
352 register u32 r2 asm("r2") = arg1;
353 register u32 r3 asm("r3") = arg2;
354 register u32 r4 asm("r4") = arg3;
355 register u32 r5 asm("r5") = arg4;
356
357 asm volatile(
358 __asmeq("%0", "r0")
359 __asmeq("%1", "r1")
360 __asmeq("%2", "r2")
361 __asmeq("%3", "r0")
362 __asmeq("%4", "r1")
363 __asmeq("%5", "r2")
364 __asmeq("%6", "r3")
Steve Muckle6dff9572012-06-25 17:37:50 -0700365#ifdef REQUIRES_SEC
366 ".arch_extension sec\n"
367#endif
Stephen Boydcb053de2011-12-15 13:32:43 -0800368 "smc #0 @ switch to secure world\n"
369 : "=r" (r0), "=r" (r1), "=r" (r2)
370 : "r" (r0), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5));
371 ret = r0;
372 if (ret1)
373 *ret1 = r1;
374 if (ret2)
375 *ret2 = r2;
376 return r0;
377}
378EXPORT_SYMBOL(scm_call_atomic4_3);
379
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700380u32 scm_get_version(void)
381{
382 int context_id;
383 static u32 version = -1;
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800384 register u32 r0 asm("r0");
385 register u32 r1 asm("r1");
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700386
387 if (version != -1)
388 return version;
389
390 mutex_lock(&scm_lock);
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800391
392 r0 = 0x1 << 8;
393 r1 = (u32)&context_id;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800394 do {
395 asm volatile(
396 __asmeq("%0", "r0")
397 __asmeq("%1", "r1")
398 __asmeq("%2", "r0")
399 __asmeq("%3", "r1")
Steve Muckle6dff9572012-06-25 17:37:50 -0700400#ifdef REQUIRES_SEC
401 ".arch_extension sec\n"
402#endif
Stephen Boyd8e76a802011-02-24 10:44:44 -0800403 "smc #0 @ switch to secure world\n"
404 : "=r" (r0), "=r" (r1)
405 : "r" (r0), "r" (r1)
406 : "r2", "r3");
407 } while (r0 == SCM_INTERRUPTED);
408
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700409 version = r1;
410 mutex_unlock(&scm_lock);
411
412 return version;
413}
414EXPORT_SYMBOL(scm_get_version);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415
Saravana Kannan988feaf2011-09-07 19:57:06 -0700416#define IS_CALL_AVAIL_CMD 1
417int scm_is_call_available(u32 svc_id, u32 cmd_id)
418{
419 int ret;
420 u32 svc_cmd = (svc_id << 10) | cmd_id;
421 u32 ret_val = 0;
422
423 ret = scm_call(SCM_SVC_INFO, IS_CALL_AVAIL_CMD, &svc_cmd,
424 sizeof(svc_cmd), &ret_val, sizeof(ret_val));
425 if (ret)
426 return ret;
427
428 return ret_val;
429}
430EXPORT_SYMBOL(scm_is_call_available);
431
Stephen Boydc085f9b2012-02-29 17:52:52 -0800432#define GET_FEAT_VERSION_CMD 3
433int scm_get_feat_version(u32 feat)
434{
435 if (scm_is_call_available(SCM_SVC_INFO, GET_FEAT_VERSION_CMD)) {
436 u32 version;
437 if (!scm_call(SCM_SVC_INFO, GET_FEAT_VERSION_CMD, &feat,
438 sizeof(feat), &version, sizeof(version)))
439 return version;
440 }
441 return 0;
442}
443EXPORT_SYMBOL(scm_get_feat_version);
444
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445static int scm_init(void)
446{
447 u32 ctr;
448
449 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
450 cacheline_size = 4 << ((ctr >> 16) & 0xf);
451
452 return 0;
453}
454early_initcall(scm_init);