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>
30 /* Cache line size for msm8x60 */
31 #define CACHELINESIZE 32
34 #define SCM_EOPNOTSUPP -4
35 #define SCM_EINVAL_ADDR -3
36 #define SCM_EINVAL_ARG -2
38 #define SCM_INTERRUPTED 1
40 static DEFINE_MUTEX(scm_lock);
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()
50 * An SCM command is laid out in memory as follows:
52 * ------------------- <--- struct scm_command
54 * ------------------- <--- scm_get_command_buffer()
56 * ------------------- <--- struct scm_response and
57 * | response header | scm_command_to_response()
58 * ------------------- <--- scm_get_response_buffer()
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.
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
87 * alloc_scm_command() - Allocate an SCM command
88 * @cmd_size: size of the command buffer
89 * @resp_size: size of the response buffer
91 * Allocate an SCM command, including enough room for the command
92 * and response headers as well as the command and response buffers.
94 * Returns a valid &scm_command on success or %NULL if the allocation fails.
96 static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
98 struct scm_command *cmd;
99 size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
102 cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
105 cmd->buf_offset = offsetof(struct scm_command, buf);
106 cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
112 * free_scm_command() - Free an SCM command
113 * @cmd: command to free
115 * Free an SCM command.
117 static inline void free_scm_command(struct scm_command *cmd)
123 * scm_command_to_response() - Get a pointer to a scm_response
126 * Returns a pointer to a response for a command.
128 static inline struct scm_response *scm_command_to_response(
129 const struct scm_command *cmd)
131 return (void *)cmd + cmd->resp_hdr_offset;
135 * scm_get_command_buffer() - Get a pointer to a command buffer
138 * Returns a pointer to the command buffer of a command.
140 static inline void *scm_get_command_buffer(const struct scm_command *cmd)
142 return (void *)cmd->buf;
146 * scm_get_response_buffer() - Get a pointer to a response buffer
149 * Returns a pointer to a response buffer of a response.
151 static inline void *scm_get_response_buffer(const struct scm_response *rsp)
153 return (void *)rsp + rsp->buf_offset;
156 static int scm_remap_error(int 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 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.
210 ret = scm_remap_error(ret);
215 static void scm_inv_range(unsigned long start, unsigned long end)
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)
223 start += CACHELINESIZE;
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
238 * Sends a command to the SCM and waits for the command to finish processing.
240 int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
241 void *resp_buf, size_t resp_len)
244 struct scm_command *cmd;
245 struct scm_response *rsp;
246 unsigned long start, end;
248 cmd = alloc_scm_command(cmd_len, resp_len);
252 cmd->id = (svc_id << 10) | cmd_id;
254 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
256 mutex_lock(&scm_lock);
257 ret = __scm_call(cmd);
258 mutex_unlock(&scm_lock);
262 rsp = scm_command_to_response(cmd);
263 start = (unsigned long)rsp;
266 scm_inv_range(start, start + sizeof(*rsp));
267 } while (!rsp->is_complete);
269 end = (unsigned long)scm_get_response_buffer(rsp) + resp_len;
270 scm_inv_range(start, end);
273 memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
275 free_scm_command(cmd);
278 EXPORT_SYMBOL(scm_call);
280 u32 scm_get_version(void)
283 static u32 version = -1;
284 register u32 r0 asm("r0");
285 register u32 r1 asm("r1");
290 mutex_lock(&scm_lock);
293 r1 = (u32)&context_id;
301 ".arch_extension sec\n"
303 "smc #0 @ switch to secure world\n"
304 : "=r" (r0), "=r" (r1)
307 } while (r0 == SCM_INTERRUPTED);
310 mutex_unlock(&scm_lock);
314 EXPORT_SYMBOL(scm_get_version);