1 /* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
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.
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.
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
18 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/errno.h>
23 #include <linux/err.h>
25 #include <asm/outercache.h>
26 #include <asm/cacheflush.h>
31 #define SCM_EOPNOTSUPP -4
32 #define SCM_EINVAL_ADDR -3
33 #define SCM_EINVAL_ARG -2
35 #define SCM_INTERRUPTED 1
37 static DEFINE_MUTEX(scm_lock);
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()
47 * An SCM command is laid out in memory as follows:
49 * ------------------- <--- struct scm_command
51 * ------------------- <--- scm_get_command_buffer()
53 * ------------------- <--- struct scm_response and
54 * | response header | scm_command_to_response()
55 * ------------------- <--- scm_get_response_buffer()
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.
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
84 * alloc_scm_command() - Allocate an SCM command
85 * @cmd_size: size of the command buffer
86 * @resp_size: size of the response buffer
88 * Allocate an SCM command, including enough room for the command
89 * and response headers as well as the command and response buffers.
91 * Returns a valid &scm_command on success or %NULL if the allocation fails.
93 static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
95 struct scm_command *cmd;
96 size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
99 cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
102 cmd->buf_offset = offsetof(struct scm_command, buf);
103 cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
109 * free_scm_command() - Free an SCM command
110 * @cmd: command to free
112 * Free an SCM command.
114 static inline void free_scm_command(struct scm_command *cmd)
120 * scm_command_to_response() - Get a pointer to a scm_response
123 * Returns a pointer to a response for a command.
125 static inline struct scm_response *scm_command_to_response(
126 const struct scm_command *cmd)
128 return (void *)cmd + cmd->resp_hdr_offset;
132 * scm_get_command_buffer() - Get a pointer to a command buffer
135 * Returns a pointer to the command buffer of a command.
137 static inline void *scm_get_command_buffer(const struct scm_command *cmd)
139 return (void *)cmd->buf;
143 * scm_get_response_buffer() - Get a pointer to a response buffer
146 * Returns a pointer to a response buffer of a response.
148 static inline void *scm_get_response_buffer(const struct scm_response *rsp)
150 return (void *)rsp + rsp->buf_offset;
153 static int scm_remap_error(int err)
158 case SCM_EINVAL_ADDR:
169 static u32 smc(u32 cmd_addr)
172 register u32 r0 asm("r0") = 1;
173 register u32 r1 asm("r1") = (u32)&context_id;
174 register u32 r2 asm("r2") = cmd_addr;
182 ".arch_extension sec\n"
184 "smc #0 @ switch to secure world\n"
186 : "r" (r0), "r" (r1), "r" (r2)
188 } while (r0 == SCM_INTERRUPTED);
193 static int __scm_call(const struct scm_command *cmd)
196 u32 cmd_addr = virt_to_phys(cmd);
199 * Flush the command buffer so that the secure world sees
202 __cpuc_flush_dcache_area((void *)cmd, cmd->len);
203 outer_flush_range(cmd_addr, cmd_addr + cmd->len);
207 ret = scm_remap_error(ret);
212 static void scm_inv_range(unsigned long start, unsigned long end)
214 u32 cacheline_size, ctr;
216 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
217 cacheline_size = 4 << ((ctr >> 16) & 0xf);
219 start = round_down(start, cacheline_size);
220 end = round_up(end, cacheline_size);
221 outer_inv_range(start, end);
222 while (start < end) {
223 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
225 start += cacheline_size;
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
240 * Sends a command to the SCM and waits for the command to finish processing.
242 * A note on cache maintenance:
243 * Note that any buffers that are expected to be accessed by the secure world
244 * must be flushed before invoking scm_call and invalidated in the cache
245 * immediately after scm_call returns. Cache maintenance on the command and
246 * response buffers is taken care of by scm_call; however, callers are
247 * responsible for any other cached buffers passed over to the secure world.
249 int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
250 void *resp_buf, size_t resp_len)
253 struct scm_command *cmd;
254 struct scm_response *rsp;
255 unsigned long start, end;
257 cmd = alloc_scm_command(cmd_len, resp_len);
261 cmd->id = (svc_id << 10) | cmd_id;
263 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
265 mutex_lock(&scm_lock);
266 ret = __scm_call(cmd);
267 mutex_unlock(&scm_lock);
271 rsp = scm_command_to_response(cmd);
272 start = (unsigned long)rsp;
275 scm_inv_range(start, start + sizeof(*rsp));
276 } while (!rsp->is_complete);
278 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
279 scm_inv_range(start, end);
282 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
284 free_scm_command(cmd);
287 EXPORT_SYMBOL(scm_call);
289 u32 scm_get_version(void)
292 static u32 version = -1;
293 register u32 r0 asm("r0");
294 register u32 r1 asm("r1");
299 mutex_lock(&scm_lock);
302 r1 = (u32)&context_id;
310 ".arch_extension sec\n"
312 "smc #0 @ switch to secure world\n"
313 : "=r" (r0), "=r" (r1)
316 } while (r0 == SCM_INTERRUPTED);
319 mutex_unlock(&scm_lock);
323 EXPORT_SYMBOL(scm_get_version);