blob: 19f7290c62e1300cf8b2f61b4de1a44ac8b869a8 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, 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")
176 "smc #0 @ switch to secure world\n"
177 : "=r" (r0)
178 : "r" (r0), "r" (r1), "r" (r2)
179 : "r3");
180 } while (r0 == SCM_INTERRUPTED);
181
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700182 return r0;
183}
184
185static int __scm_call(const struct scm_command *cmd)
186{
187 int ret;
188 u32 cmd_addr = virt_to_phys(cmd);
189
190 /*
191 * Flush the entire cache here so callers don't have to remember
192 * to flush the cache when passing physical addresses to the secure
193 * side in the buffer.
194 */
195 flush_cache_all();
Stephen Boyd8e76a802011-02-24 10:44:44 -0800196 ret = smc(cmd_addr);
197 if (ret < 0)
198 ret = scm_remap_error(ret);
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700199
200 return ret;
201}
202
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203static u32 cacheline_size;
204
205static void scm_inv_range(unsigned long start, unsigned long end)
206{
207 start = round_down(start, cacheline_size);
208 end = round_up(end, cacheline_size);
209 while (start < end) {
210 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
211 : "memory");
212 start += cacheline_size;
213 }
214 dsb();
215 isb();
216}
217
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700218/**
219 * scm_call() - Send an SCM command
220 * @svc_id: service identifier
221 * @cmd_id: command identifier
222 * @cmd_buf: command buffer
223 * @cmd_len: length of the command buffer
224 * @resp_buf: response buffer
225 * @resp_len: length of the response buffer
226 *
227 * Sends a command to the SCM and waits for the command to finish processing.
228 */
229int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
230 void *resp_buf, size_t resp_len)
231{
232 int ret;
233 struct scm_command *cmd;
234 struct scm_response *rsp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 unsigned long start, end;
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700236
237 cmd = alloc_scm_command(cmd_len, resp_len);
238 if (!cmd)
239 return -ENOMEM;
240
241 cmd->id = (svc_id << 10) | cmd_id;
242 if (cmd_buf)
243 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
244
245 mutex_lock(&scm_lock);
246 ret = __scm_call(cmd);
247 mutex_unlock(&scm_lock);
248 if (ret)
249 goto out;
250
251 rsp = scm_command_to_response(cmd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 start = (unsigned long)rsp;
253
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700254 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 scm_inv_range(start, start + sizeof(*rsp));
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700256 } while (!rsp->is_complete);
257
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
259 scm_inv_range(start, end);
260
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700261 if (resp_buf)
262 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
263out:
264 free_scm_command(cmd);
265 return ret;
266}
267EXPORT_SYMBOL(scm_call);
268
Stephen Boyd551cd962011-07-18 10:33:45 -0700269#define SCM_CLASS_REGISTER (0x2 << 8)
270#define SCM_MASK_IRQS BIT(5)
271#define SCM_ATOMIC(svc, cmd, n) (((((svc) << 10)|((cmd) & 0x3ff)) << 12) | \
272 SCM_CLASS_REGISTER | \
273 SCM_MASK_IRQS | \
274 (n & 0xf))
275
276/**
277 * scm_call_atomic1() - Send an atomic SCM command with one argument
278 * @svc_id: service identifier
279 * @cmd_id: command identifier
280 * @arg1: first argument
281 *
282 * This shall only be used with commands that are guaranteed to be
283 * uninterruptable, atomic and SMP safe.
284 */
285u32 scm_call_atomic1(u32 svc, u32 cmd, u32 arg1)
286{
287 int context_id;
288 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 1);
289 register u32 r1 asm("r1") = (u32)&context_id;
290 register u32 r2 asm("r2") = arg1;
291
292 asm volatile(
293 __asmeq("%0", "r0")
294 __asmeq("%1", "r0")
295 __asmeq("%2", "r1")
296 __asmeq("%3", "r2")
297 "smc #0 @ switch to secure world\n"
298 : "=r" (r0)
299 : "r" (r0), "r" (r1), "r" (r2)
300 : "r3");
301 return r0;
302}
303EXPORT_SYMBOL(scm_call_atomic1);
304
305/**
306 * scm_call_atomic2() - Send an atomic SCM command with two arguments
307 * @svc_id: service identifier
308 * @cmd_id: command identifier
309 * @arg1: first argument
310 * @arg2: second argument
311 *
312 * This shall only be used with commands that are guaranteed to be
313 * uninterruptable, atomic and SMP safe.
314 */
315u32 scm_call_atomic2(u32 svc, u32 cmd, u32 arg1, u32 arg2)
316{
317 int context_id;
318 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 2);
319 register u32 r1 asm("r1") = (u32)&context_id;
320 register u32 r2 asm("r2") = arg1;
321 register u32 r3 asm("r3") = arg2;
322
323 asm volatile(
324 __asmeq("%0", "r0")
325 __asmeq("%1", "r0")
326 __asmeq("%2", "r1")
327 __asmeq("%3", "r2")
328 __asmeq("%4", "r3")
329 "smc #0 @ switch to secure world\n"
330 : "=r" (r0)
331 : "r" (r0), "r" (r1), "r" (r2), "r" (r3));
332 return r0;
333}
334EXPORT_SYMBOL(scm_call_atomic2);
335
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700336u32 scm_get_version(void)
337{
338 int context_id;
339 static u32 version = -1;
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800340 register u32 r0 asm("r0");
341 register u32 r1 asm("r1");
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700342
343 if (version != -1)
344 return version;
345
346 mutex_lock(&scm_lock);
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800347
348 r0 = 0x1 << 8;
349 r1 = (u32)&context_id;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800350 do {
351 asm volatile(
352 __asmeq("%0", "r0")
353 __asmeq("%1", "r1")
354 __asmeq("%2", "r0")
355 __asmeq("%3", "r1")
356 "smc #0 @ switch to secure world\n"
357 : "=r" (r0), "=r" (r1)
358 : "r" (r0), "r" (r1)
359 : "r2", "r3");
360 } while (r0 == SCM_INTERRUPTED);
361
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700362 version = r1;
363 mutex_unlock(&scm_lock);
364
365 return version;
366}
367EXPORT_SYMBOL(scm_get_version);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368
Saravana Kannan988feaf2011-09-07 19:57:06 -0700369#define IS_CALL_AVAIL_CMD 1
370int scm_is_call_available(u32 svc_id, u32 cmd_id)
371{
372 int ret;
373 u32 svc_cmd = (svc_id << 10) | cmd_id;
374 u32 ret_val = 0;
375
376 ret = scm_call(SCM_SVC_INFO, IS_CALL_AVAIL_CMD, &svc_cmd,
377 sizeof(svc_cmd), &ret_val, sizeof(ret_val));
378 if (ret)
379 return ret;
380
381 return ret_val;
382}
383EXPORT_SYMBOL(scm_is_call_available);
384
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385static int scm_init(void)
386{
387 u32 ctr;
388
389 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
390 cacheline_size = 4 << ((ctr >> 16) & 0xf);
391
392 return 0;
393}
394early_initcall(scm_init);