Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
scm.c
Go to the documentation of this file.
1 /* 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 
25 #include <asm/cacheflush.h>
26 
27 #include "scm.h"
28 
29 /* Cache line size for msm8x60 */
30 #define CACHELINESIZE 32
31 
32 #define SCM_ENOMEM -5
33 #define SCM_EOPNOTSUPP -4
34 #define SCM_EINVAL_ADDR -3
35 #define SCM_EINVAL_ARG -2
36 #define SCM_ERROR -1
37 #define SCM_INTERRUPTED 1
38 
39 static DEFINE_MUTEX(scm_lock);
40 
65 struct scm_command {
70  u32 buf[0];
71 };
72 
79 struct scm_response {
83 };
84 
95 static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
96 {
97  struct scm_command *cmd;
98  size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
99  resp_size;
100 
101  cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
102  if (cmd) {
103  cmd->len = len;
104  cmd->buf_offset = offsetof(struct scm_command, buf);
105  cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
106  }
107  return cmd;
108 }
109 
116 static inline void free_scm_command(struct scm_command *cmd)
117 {
118  kfree(cmd);
119 }
120 
127 static inline struct scm_response *scm_command_to_response(
128  const struct scm_command *cmd)
129 {
130  return (void *)cmd + cmd->resp_hdr_offset;
131 }
132 
139 static inline void *scm_get_command_buffer(const struct scm_command *cmd)
140 {
141  return (void *)cmd->buf;
142 }
143 
150 static inline void *scm_get_response_buffer(const struct scm_response *rsp)
151 {
152  return (void *)rsp + rsp->buf_offset;
153 }
154 
155 static int scm_remap_error(int err)
156 {
157  switch (err) {
158  case SCM_ERROR:
159  return -EIO;
160  case SCM_EINVAL_ADDR:
161  case SCM_EINVAL_ARG:
162  return -EINVAL;
163  case SCM_EOPNOTSUPP:
164  return -EOPNOTSUPP;
165  case SCM_ENOMEM:
166  return -ENOMEM;
167  }
168  return -EINVAL;
169 }
170 
171 static u32 smc(u32 cmd_addr)
172 {
173  int context_id;
174  register u32 r0 asm("r0") = 1;
175  register u32 r1 asm("r1") = (u32)&context_id;
176  register u32 r2 asm("r2") = cmd_addr;
177  do {
178  asm volatile(
179  __asmeq("%0", "r0")
180  __asmeq("%1", "r0")
181  __asmeq("%2", "r1")
182  __asmeq("%3", "r2")
183 #ifdef REQUIRES_SEC
184  ".arch_extension sec\n"
185 #endif
186  "smc #0 @ switch to secure world\n"
187  : "=r" (r0)
188  : "r" (r0), "r" (r1), "r" (r2)
189  : "r3");
190  } while (r0 == SCM_INTERRUPTED);
191 
192  return r0;
193 }
194 
195 static int __scm_call(const struct scm_command *cmd)
196 {
197  int ret;
198  u32 cmd_addr = virt_to_phys(cmd);
199 
200  /*
201  * Flush the entire cache here so callers don't have to remember
202  * to flush the cache when passing physical addresses to the secure
203  * side in the buffer.
204  */
205  flush_cache_all();
206  ret = smc(cmd_addr);
207  if (ret < 0)
208  ret = scm_remap_error(ret);
209 
210  return ret;
211 }
212 
224 int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
225  void *resp_buf, size_t resp_len)
226 {
227  int ret;
228  struct scm_command *cmd;
229  struct scm_response *rsp;
230 
231  cmd = alloc_scm_command(cmd_len, resp_len);
232  if (!cmd)
233  return -ENOMEM;
234 
235  cmd->id = (svc_id << 10) | cmd_id;
236  if (cmd_buf)
237  memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
238 
239  mutex_lock(&scm_lock);
240  ret = __scm_call(cmd);
241  mutex_unlock(&scm_lock);
242  if (ret)
243  goto out;
244 
245  rsp = scm_command_to_response(cmd);
246  do {
247  u32 start = (u32)rsp;
248  u32 end = (u32)scm_get_response_buffer(rsp) + resp_len;
249  start &= ~(CACHELINESIZE - 1);
250  while (start < end) {
251  asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
252  : "memory");
253  start += CACHELINESIZE;
254  }
255  } while (!rsp->is_complete);
256 
257  if (resp_buf)
258  memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
259 out:
260  free_scm_command(cmd);
261  return ret;
262 }
264 
266 {
267  int context_id;
268  static u32 version = -1;
269  register u32 r0 asm("r0");
270  register u32 r1 asm("r1");
271 
272  if (version != -1)
273  return version;
274 
275  mutex_lock(&scm_lock);
276 
277  r0 = 0x1 << 8;
278  r1 = (u32)&context_id;
279  do {
280  asm volatile(
281  __asmeq("%0", "r0")
282  __asmeq("%1", "r1")
283  __asmeq("%2", "r0")
284  __asmeq("%3", "r1")
285 #ifdef REQUIRES_SEC
286  ".arch_extension sec\n"
287 #endif
288  "smc #0 @ switch to secure world\n"
289  : "=r" (r0), "=r" (r1)
290  : "r" (r0), "r" (r1)
291  : "r2", "r3");
292  } while (r0 == SCM_INTERRUPTED);
293 
294  version = r1;
295  mutex_unlock(&scm_lock);
296 
297  return version;
298 }