blob: c08786ebf116724bd27a7a6848e1b215e947075d [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
Stephen Boyd2a1eb582010-08-27 10:01:23 -070030#define SCM_ENOMEM -5
31#define SCM_EOPNOTSUPP -4
32#define SCM_EINVAL_ADDR -3
33#define SCM_EINVAL_ARG -2
34#define SCM_ERROR -1
35#define SCM_INTERRUPTED 1
36
37static DEFINE_MUTEX(scm_lock);
38
39/**
40 * struct scm_command - one SCM command buffer
41 * @len: total available memory for command and response
42 * @buf_offset: start of command buffer
43 * @resp_hdr_offset: start of response buffer
44 * @id: command to be executed
45 * @buf: buffer returned from scm_get_command_buffer()
46 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030047 * An SCM command is laid out in memory as follows:
Stephen Boyd2a1eb582010-08-27 10:01:23 -070048 *
49 * ------------------- <--- struct scm_command
50 * | command header |
51 * ------------------- <--- scm_get_command_buffer()
52 * | command buffer |
53 * ------------------- <--- struct scm_response and
54 * | response header | scm_command_to_response()
55 * ------------------- <--- scm_get_response_buffer()
56 * | response buffer |
57 * -------------------
58 *
59 * There can be arbitrary padding between the headers and buffers so
60 * you should always use the appropriate scm_get_*_buffer() routines
61 * to access the buffers in a safe manner.
62 */
63struct scm_command {
64 u32 len;
65 u32 buf_offset;
66 u32 resp_hdr_offset;
67 u32 id;
68 u32 buf[0];
69};
70
71/**
72 * struct scm_response - one SCM response buffer
73 * @len: total available memory for response
74 * @buf_offset: start of response data relative to start of scm_response
75 * @is_complete: indicates if the command has finished processing
76 */
77struct scm_response {
78 u32 len;
79 u32 buf_offset;
80 u32 is_complete;
81};
82
83/**
84 * alloc_scm_command() - Allocate an SCM command
85 * @cmd_size: size of the command buffer
86 * @resp_size: size of the response buffer
87 *
88 * Allocate an SCM command, including enough room for the command
89 * and response headers as well as the command and response buffers.
90 *
91 * Returns a valid &scm_command on success or %NULL if the allocation fails.
92 */
93static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
94{
95 struct scm_command *cmd;
96 size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
97 resp_size;
98
99 cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
100 if (cmd) {
101 cmd->len = len;
102 cmd->buf_offset = offsetof(struct scm_command, buf);
103 cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
104 }
105 return cmd;
106}
107
108/**
109 * free_scm_command() - Free an SCM command
110 * @cmd: command to free
111 *
112 * Free an SCM command.
113 */
114static inline void free_scm_command(struct scm_command *cmd)
115{
116 kfree(cmd);
117}
118
119/**
120 * scm_command_to_response() - Get a pointer to a scm_response
121 * @cmd: command
122 *
123 * Returns a pointer to a response for a command.
124 */
125static inline struct scm_response *scm_command_to_response(
126 const struct scm_command *cmd)
127{
128 return (void *)cmd + cmd->resp_hdr_offset;
129}
130
131/**
132 * scm_get_command_buffer() - Get a pointer to a command buffer
133 * @cmd: command
134 *
135 * Returns a pointer to the command buffer of a command.
136 */
137static inline void *scm_get_command_buffer(const struct scm_command *cmd)
138{
139 return (void *)cmd->buf;
140}
141
142/**
143 * scm_get_response_buffer() - Get a pointer to a response buffer
144 * @rsp: response
145 *
146 * Returns a pointer to a response buffer of a response.
147 */
148static inline void *scm_get_response_buffer(const struct scm_response *rsp)
149{
150 return (void *)rsp + rsp->buf_offset;
151}
152
153static int scm_remap_error(int err)
154{
155 switch (err) {
156 case SCM_ERROR:
157 return -EIO;
158 case SCM_EINVAL_ADDR:
159 case SCM_EINVAL_ARG:
160 return -EINVAL;
161 case SCM_EOPNOTSUPP:
162 return -EOPNOTSUPP;
163 case SCM_ENOMEM:
164 return -ENOMEM;
165 }
166 return -EINVAL;
167}
168
169static u32 smc(u32 cmd_addr)
170{
171 int context_id;
172 register u32 r0 asm("r0") = 1;
173 register u32 r1 asm("r1") = (u32)&context_id;
174 register u32 r2 asm("r2") = cmd_addr;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800175 do {
176 asm volatile(
177 __asmeq("%0", "r0")
178 __asmeq("%1", "r0")
179 __asmeq("%2", "r1")
180 __asmeq("%3", "r2")
Marc Zyngiereca55f42011-11-08 13:07:36 +0000181#ifdef REQUIRES_SEC
182 ".arch_extension sec\n"
183#endif
Stephen Boyd8e76a802011-02-24 10:44:44 -0800184 "smc #0 @ switch to secure world\n"
185 : "=r" (r0)
186 : "r" (r0), "r" (r1), "r" (r2)
187 : "r3");
188 } while (r0 == SCM_INTERRUPTED);
189
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700190 return r0;
191}
192
193static int __scm_call(const struct scm_command *cmd)
194{
195 int ret;
196 u32 cmd_addr = virt_to_phys(cmd);
197
198 /*
199 * Flush the entire cache here so callers don't have to remember
200 * to flush the cache when passing physical addresses to the secure
201 * side in the buffer.
202 */
203 flush_cache_all();
Stephen Boydf76c6912014-08-04 18:31:43 -0700204 outer_flush_all();
Stephen Boyd8e76a802011-02-24 10:44:44 -0800205 ret = smc(cmd_addr);
206 if (ret < 0)
207 ret = scm_remap_error(ret);
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700208
209 return ret;
210}
211
Stephen Boydf76c6912014-08-04 18:31:43 -0700212static void scm_inv_range(unsigned long start, unsigned long end)
213{
Stephen Boyd30cbb0c2014-08-04 18:31:44 -0700214 u32 cacheline_size, ctr;
215
216 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
217 cacheline_size = 4 << ((ctr >> 16) & 0xf);
218
219 start = round_down(start, cacheline_size);
220 end = round_up(end, cacheline_size);
Stephen Boydf76c6912014-08-04 18:31:43 -0700221 outer_inv_range(start, end);
222 while (start < end) {
223 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
224 : "memory");
Stephen Boyd30cbb0c2014-08-04 18:31:44 -0700225 start += cacheline_size;
Stephen Boydf76c6912014-08-04 18:31:43 -0700226 }
227 dsb();
228 isb();
229}
230
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700231/**
232 * scm_call() - Send an SCM command
233 * @svc_id: service identifier
234 * @cmd_id: command identifier
235 * @cmd_buf: command buffer
236 * @cmd_len: length of the command buffer
237 * @resp_buf: response buffer
238 * @resp_len: length of the response buffer
239 *
240 * Sends a command to the SCM and waits for the command to finish processing.
241 */
242int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
243 void *resp_buf, size_t resp_len)
244{
245 int ret;
246 struct scm_command *cmd;
247 struct scm_response *rsp;
Stephen Boydf76c6912014-08-04 18:31:43 -0700248 unsigned long start, end;
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700249
250 cmd = alloc_scm_command(cmd_len, resp_len);
251 if (!cmd)
252 return -ENOMEM;
253
254 cmd->id = (svc_id << 10) | cmd_id;
255 if (cmd_buf)
256 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
257
258 mutex_lock(&scm_lock);
259 ret = __scm_call(cmd);
260 mutex_unlock(&scm_lock);
261 if (ret)
262 goto out;
263
264 rsp = scm_command_to_response(cmd);
Stephen Boydf76c6912014-08-04 18:31:43 -0700265 start = (unsigned long)rsp;
266
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700267 do {
Stephen Boydf76c6912014-08-04 18:31:43 -0700268 scm_inv_range(start, start + sizeof(*rsp));
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700269 } while (!rsp->is_complete);
270
Stephen Boydf76c6912014-08-04 18:31:43 -0700271 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
272 scm_inv_range(start, end);
273
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700274 if (resp_buf)
275 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
276out:
277 free_scm_command(cmd);
278 return ret;
279}
280EXPORT_SYMBOL(scm_call);
281
282u32 scm_get_version(void)
283{
284 int context_id;
285 static u32 version = -1;
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800286 register u32 r0 asm("r0");
287 register u32 r1 asm("r1");
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700288
289 if (version != -1)
290 return version;
291
292 mutex_lock(&scm_lock);
Stephen Boyd98d4ded2011-02-24 10:44:43 -0800293
294 r0 = 0x1 << 8;
295 r1 = (u32)&context_id;
Stephen Boyd8e76a802011-02-24 10:44:44 -0800296 do {
297 asm volatile(
298 __asmeq("%0", "r0")
299 __asmeq("%1", "r1")
300 __asmeq("%2", "r0")
301 __asmeq("%3", "r1")
Stephen Boyd26e87b12012-04-30 19:17:20 -0700302#ifdef REQUIRES_SEC
303 ".arch_extension sec\n"
304#endif
Stephen Boyd8e76a802011-02-24 10:44:44 -0800305 "smc #0 @ switch to secure world\n"
306 : "=r" (r0), "=r" (r1)
307 : "r" (r0), "r" (r1)
308 : "r2", "r3");
309 } while (r0 == SCM_INTERRUPTED);
310
Stephen Boyd2a1eb582010-08-27 10:01:23 -0700311 version = r1;
312 mutex_unlock(&scm_lock);
313
314 return version;
315}
316EXPORT_SYMBOL(scm_get_version);