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.
66 __le32 resp_hdr_offset;
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 +
100 cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
102 cmd->len = cpu_to_le32(len);
103 offset = offsetof(struct scm_command, buf);
104 cmd->buf_offset = cpu_to_le32(offset);
105 cmd->resp_hdr_offset = cpu_to_le32(offset + cmd_size);
111 * free_scm_command() - Free an SCM command
112 * @cmd: command to free
114 * Free an SCM command.
116 static inline void free_scm_command(struct scm_command *cmd)
122 * scm_command_to_response() - Get a pointer to a scm_response
125 * Returns a pointer to a response for a command.
127 static inline struct scm_response *scm_command_to_response(
128 const struct scm_command *cmd)
130 return (void *)cmd + le32_to_cpu(cmd->resp_hdr_offset);
134 * scm_get_command_buffer() - Get a pointer to a command buffer
137 * Returns a pointer to the command buffer of a command.
139 static inline void *scm_get_command_buffer(const struct scm_command *cmd)
141 return (void *)cmd->buf;
145 * scm_get_response_buffer() - Get a pointer to a response buffer
148 * Returns a pointer to a response buffer of a response.
150 static inline void *scm_get_response_buffer(const struct scm_response *rsp)
152 return (void *)rsp + le32_to_cpu(rsp->buf_offset);
155 static int scm_remap_error(int err)
157 pr_err("scm_call failed with error code %d\n", err);
161 case SCM_EINVAL_ADDR:
172 static u32 smc(u32 cmd_addr)
175 register u32 r0 asm("r0") = 1;
176 register u32 r1 asm("r1") = (u32)&context_id;
177 register u32 r2 asm("r2") = cmd_addr;
185 ".arch_extension sec\n"
187 "smc #0 @ switch to secure world\n"
189 : "r" (r0), "r" (r1), "r" (r2)
191 } while (r0 == SCM_INTERRUPTED);
196 static int __scm_call(const struct scm_command *cmd)
199 u32 cmd_addr = virt_to_phys(cmd);
202 * Flush the command buffer so that the secure world sees
205 __cpuc_flush_dcache_area((void *)cmd, cmd->len);
206 outer_flush_range(cmd_addr, cmd_addr + cmd->len);
210 ret = scm_remap_error(ret);
215 static void scm_inv_range(unsigned long start, unsigned long end)
217 u32 cacheline_size, ctr;
219 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
220 cacheline_size = 4 << ((ctr >> 16) & 0xf);
222 start = round_down(start, cacheline_size);
223 end = round_up(end, cacheline_size);
224 outer_inv_range(start, end);
225 while (start < end) {
226 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
228 start += cacheline_size;
235 * scm_call() - Send an SCM command
236 * @svc_id: service identifier
237 * @cmd_id: command identifier
238 * @cmd_buf: command buffer
239 * @cmd_len: length of the command buffer
240 * @resp_buf: response buffer
241 * @resp_len: length of the response buffer
243 * Sends a command to the SCM and waits for the command to finish processing.
245 * A note on cache maintenance:
246 * Note that any buffers that are expected to be accessed by the secure world
247 * must be flushed before invoking scm_call and invalidated in the cache
248 * immediately after scm_call returns. Cache maintenance on the command and
249 * response buffers is taken care of by scm_call; however, callers are
250 * responsible for any other cached buffers passed over to the secure world.
252 int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
253 void *resp_buf, size_t resp_len)
256 struct scm_command *cmd;
257 struct scm_response *rsp;
258 unsigned long start, end;
260 cmd = alloc_scm_command(cmd_len, resp_len);
264 cmd->id = cpu_to_le32((svc_id << 10) | cmd_id);
266 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
268 mutex_lock(&scm_lock);
269 ret = __scm_call(cmd);
270 mutex_unlock(&scm_lock);
274 rsp = scm_command_to_response(cmd);
275 start = (unsigned long)rsp;
278 scm_inv_range(start, start + sizeof(*rsp));
279 } while (!rsp->is_complete);
281 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
282 scm_inv_range(start, end);
285 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
287 free_scm_command(cmd);
290 EXPORT_SYMBOL(scm_call);
292 u32 scm_get_version(void)
295 static u32 version = -1;
296 register u32 r0 asm("r0");
297 register u32 r1 asm("r1");
302 mutex_lock(&scm_lock);
305 r1 = (u32)&context_id;
313 ".arch_extension sec\n"
315 "smc #0 @ switch to secure world\n"
316 : "=r" (r0), "=r" (r1)
319 } while (r0 == SCM_INTERRUPTED);
322 mutex_unlock(&scm_lock);
326 EXPORT_SYMBOL(scm_get_version);