blob: 820c72165e19ba3fd8460d45f2f8f421e1dce719 [file] [log] [blame]
Stephen Boyd2a1eb582010-08-27 10:01:23 -07001/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2 *
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.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#include <linux/slab.h>
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/mutex.h>
22#include <linux/errno.h>
23#include <linux/err.h>
24
Stephen Boydf76c6912014-08-04 18:31:43 -070025#include <asm/outercache.h>
Stephen Boyd2a1eb582010-08-27 10:01:23 -070026#include <asm/cacheflush.h>
27
28#include "scm.h"
29
30/* Cache line size for msm8x60 */
31#define CACHELINESIZE 32
32
33#define SCM_ENOMEM -5
34#define SCM_EOPNOTSUPP -4
35#define SCM_EINVAL_ADDR -3
36#define SCM_EINVAL_ARG -2
37#define SCM_ERROR -1
38#define SCM_INTERRUPTED 1
39
40static DEFINE_MUTEX(scm_lock);
41
42/**
43 * struct scm_command - one SCM command buffer
44 * @len: total available memory for command and response
45 * @buf_offset: start of command buffer
46 * @resp_hdr_offset: start of response buffer
47 * @id: command to be executed
48 * @buf: buffer returned from scm_get_command_buffer()
49 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030050 * An SCM command is laid out in memory as follows:
Stephen Boyd2a1eb582010-08-27 10:01:23 -070051 *
52 * ------------------- <--- struct scm_command
53 * | command header |
54 * ------------------- <--- scm_get_command_buffer()
55 * | command buffer |
56 * ------------------- <--- struct scm_response and
57 * | response header | scm_command_to_response()
58 * ------------------- <--- scm_get_response_buffer()
59 * | response buffer |
60 * -------------------
61 *
62 * There can be arbitrary padding between the headers and buffers so
63 * you should always use the appropriate scm_get_*_buffer() routines
64 * to access the buffers in a safe manner.
65 */
66struct scm_command {
67 u32 len;
68 u32 buf_offset;
69 u32 resp_hdr_offset;
70 u32 id;
71 u32 buf[0];
72};
73
74/**
75 * struct scm_response - one SCM response buffer
76 * @len: total available memory for response
77 * @buf_offset: start of response data relative to start of scm_response
78 * @is_complete: indicates if the command has finished processing
79 */
80struct scm_response {
81 u32 len;
82 u32 buf_offset;
83 u32 is_complete;
84};
85
86/**
87 * alloc_scm_command() - Allocate an SCM command
88 * @cmd_size: size of the command buffer
89 * @resp_size: size of the response buffer
90 *
91 * Allocate an SCM command, including enough room for the command
92 * and response headers as well as the command and response buffers.
93 *
94 * Returns a valid &scm_command on success or %NULL if the allocation fails.
95 */
96static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
97{
98 struct scm_command *cmd;
99 size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
100 resp_size;
101
102 cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
103 if (cmd) {
104 cmd->len = len;
105 cmd->buf_offset = offsetof(struct scm_command, buf);
106 cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
107 }
108 return cmd;
109}
110
111/**
112 * free_scm_command() - Free an SCM command
113 * @cmd: command to free
114 *
115 * Free an SCM command.
116 */
117static inline void free_scm_command(struct scm_command *cmd)
118{
119 kfree(cmd);
120}
121
122/**
123 * scm_command_to_response() - Get a pointer to a scm_response
124 * @cmd: command
125 *
126 * Returns a pointer to a response for a command.
127 */
128static inline struct scm_response *scm_command_to_response(
129 const struct scm_command *cmd)
130{
131 return (void *)cmd + cmd->resp_hdr_offset;
132}
133
134/**
135 * scm_get_command_buffer() - Get a pointer to a command buffer
136 * @cmd: command
137 *
138 * Returns a pointer to the command buffer of a command.
139 */
140static inline void *scm_get_command_buffer(const struct scm_command *cmd)
141{
142 return (void *)cmd->buf;
143}
144
145/**
146 * scm_get_response_buffer() - Get a pointer to a response buffer
147 * @rsp: response
148 *
149 * Returns a pointer to a response buffer of a response.
150 */
151static inline void *scm_get_response_buffer(const struct scm_response *rsp)
152{
153 return (void *)rsp + rsp->buf_offset;
154}
155
156static int scm_remap_error(int err)
157{
158 switch (err) {
159 case SCM_ERROR:
160 return -EIO;
161 case SCM_EINVAL_ADDR:
162 case SCM_EINVAL_ARG:
163 return -EINVAL;
164 case SCM_EOPNOTSUPP:
165 return -EOPNOTSUPP;
166 case SCM_ENOMEM:
167 return -ENOMEM;
168 }
169 return -EINVAL;
170}
171
172static u32 smc(u32 cmd_addr)
173{
174 int context_id;
175 register u32 r0 asm("r0") = 1;
176 register u32 r1 asm("r1") = (u32)&context_id;
177 register u32 r2 asm("r2") = cmd_addr;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800178 do {
179 asm volatile(
180 __asmeq("%0", "r0")
181 __asmeq("%1", "r0")
182 __asmeq("%2", "r1")
183 __asmeq("%3", "r2")
Marc Zyngiereca55f42011-11-08 13:07:36 +0000184#ifdef REQUIRES_SEC
185 ".arch_extension sec\n"
186#endif
Stephen Boyd8e76a802011-02-24 10:44:44 -0800187 "smc #0 @ switch to secure world\n"
188 : "=r" (r0)
189 : "r" (r0), "r" (r1), "r" (r2)
190 : "r3");
191 } while (r0 == SCM_INTERRUPTED);
192
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700193 return r0;
194}
195
196static int __scm_call(const struct scm_command *cmd)
197{
198 int ret;
199 u32 cmd_addr = virt_to_phys(cmd);
200
201 /*
202 * Flush the entire cache here so callers don't have to remember
203 * to flush the cache when passing physical addresses to the secure
204 * side in the buffer.
205 */
206 flush_cache_all();
Stephen Boydf76c6912014-08-04 18:31:43 -0700207 outer_flush_all();
Stephen Boyd8e76a802011-02-24 10:44:44 -0800208 ret = smc(cmd_addr);
209 if (ret < 0)
210 ret = scm_remap_error(ret);
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700211
212 return ret;
213}
214
Stephen Boydf76c6912014-08-04 18:31:43 -0700215static void scm_inv_range(unsigned long start, unsigned long end)
216{
217 start = round_down(start, CACHELINESIZE);
218 end = round_up(end, CACHELINESIZE);
219 outer_inv_range(start, end);
220 while (start < end) {
221 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
222 : "memory");
223 start += CACHELINESIZE;
224 }
225 dsb();
226 isb();
227}
228
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700229/**
230 * scm_call() - Send an SCM command
231 * @svc_id: service identifier
232 * @cmd_id: command identifier
233 * @cmd_buf: command buffer
234 * @cmd_len: length of the command buffer
235 * @resp_buf: response buffer
236 * @resp_len: length of the response buffer
237 *
238 * Sends a command to the SCM and waits for the command to finish processing.
239 */
240int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
241 void *resp_buf, size_t resp_len)
242{
243 int ret;
244 struct scm_command *cmd;
245 struct scm_response *rsp;
Stephen Boydf76c6912014-08-04 18:31:43 -0700246 unsigned long start, end;
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700247
248 cmd = alloc_scm_command(cmd_len, resp_len);
249 if (!cmd)
250 return -ENOMEM;
251
252 cmd->id = (svc_id << 10) | cmd_id;
253 if (cmd_buf)
254 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
255
256 mutex_lock(&scm_lock);
257 ret = __scm_call(cmd);
258 mutex_unlock(&scm_lock);
259 if (ret)
260 goto out;
261
262 rsp = scm_command_to_response(cmd);
Stephen Boydf76c6912014-08-04 18:31:43 -0700263 start = (unsigned long)rsp;
264
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700265 do {
Stephen Boydf76c6912014-08-04 18:31:43 -0700266 scm_inv_range(start, start + sizeof(*rsp));
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700267 } while (!rsp->is_complete);
268
Stephen Boydf76c6912014-08-04 18:31:43 -0700269 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
270 scm_inv_range(start, end);
271
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700272 if (resp_buf)
273 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
274out:
275 free_scm_command(cmd);
276 return ret;
277}
278EXPORT_SYMBOL(scm_call);
279
280u32 scm_get_version(void)
281{
282 int context_id;
283 static u32 version = -1;
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800284 register u32 r0 asm("r0");
285 register u32 r1 asm("r1");
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700286
287 if (version != -1)
288 return version;
289
290 mutex_lock(&scm_lock);
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800291
292 r0 = 0x1 << 8;
293 r1 = (u32)&context_id;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800294 do {
295 asm volatile(
296 __asmeq("%0", "r0")
297 __asmeq("%1", "r1")
298 __asmeq("%2", "r0")
299 __asmeq("%3", "r1")
Stephen Boyd26e87b12012-04-30 19:17:20 -0700300#ifdef REQUIRES_SEC
301 ".arch_extension sec\n"
302#endif
Stephen Boyd8e76a802011-02-24 10:44:44 -0800303 "smc #0 @ switch to secure world\n"
304 : "=r" (r0), "=r" (r1)
305 : "r" (r0), "r" (r1)
306 : "r2", "r3");
307 } while (r0 == SCM_INTERRUPTED);
308
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700309 version = r1;
310 mutex_unlock(&scm_lock);
311
312 return version;
313}
314EXPORT_SYMBOL(scm_get_version);