scm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include <linux/slab.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/errno.h>
  22. #include <linux/err.h>
  23. #include <asm/cacheflush.h>
  24. #include "scm.h"
  25. /* Cache line size for msm8x60 */
  26. #define CACHELINESIZE 32
  27. #define SCM_ENOMEM -5
  28. #define SCM_EOPNOTSUPP -4
  29. #define SCM_EINVAL_ADDR -3
  30. #define SCM_EINVAL_ARG -2
  31. #define SCM_ERROR -1
  32. #define SCM_INTERRUPTED 1
  33. static DEFINE_MUTEX(scm_lock);
  34. /**
  35. * struct scm_command - one SCM command buffer
  36. * @len: total available memory for command and response
  37. * @buf_offset: start of command buffer
  38. * @resp_hdr_offset: start of response buffer
  39. * @id: command to be executed
  40. * @buf: buffer returned from scm_get_command_buffer()
  41. *
  42. * An SCM command is layed out in memory as follows:
  43. *
  44. * ------------------- <--- struct scm_command
  45. * | command header |
  46. * ------------------- <--- scm_get_command_buffer()
  47. * | command buffer |
  48. * ------------------- <--- struct scm_response and
  49. * | response header | scm_command_to_response()
  50. * ------------------- <--- scm_get_response_buffer()
  51. * | response buffer |
  52. * -------------------
  53. *
  54. * There can be arbitrary padding between the headers and buffers so
  55. * you should always use the appropriate scm_get_*_buffer() routines
  56. * to access the buffers in a safe manner.
  57. */
  58. struct scm_command {
  59. u32 len;
  60. u32 buf_offset;
  61. u32 resp_hdr_offset;
  62. u32 id;
  63. u32 buf[0];
  64. };
  65. /**
  66. * struct scm_response - one SCM response buffer
  67. * @len: total available memory for response
  68. * @buf_offset: start of response data relative to start of scm_response
  69. * @is_complete: indicates if the command has finished processing
  70. */
  71. struct scm_response {
  72. u32 len;
  73. u32 buf_offset;
  74. u32 is_complete;
  75. };
  76. /**
  77. * alloc_scm_command() - Allocate an SCM command
  78. * @cmd_size: size of the command buffer
  79. * @resp_size: size of the response buffer
  80. *
  81. * Allocate an SCM command, including enough room for the command
  82. * and response headers as well as the command and response buffers.
  83. *
  84. * Returns a valid &scm_command on success or %NULL if the allocation fails.
  85. */
  86. static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
  87. {
  88. struct scm_command *cmd;
  89. size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
  90. resp_size;
  91. cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
  92. if (cmd) {
  93. cmd->len = len;
  94. cmd->buf_offset = offsetof(struct scm_command, buf);
  95. cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
  96. }
  97. return cmd;
  98. }
  99. /**
  100. * free_scm_command() - Free an SCM command
  101. * @cmd: command to free
  102. *
  103. * Free an SCM command.
  104. */
  105. static inline void free_scm_command(struct scm_command *cmd)
  106. {
  107. kfree(cmd);
  108. }
  109. /**
  110. * scm_command_to_response() - Get a pointer to a scm_response
  111. * @cmd: command
  112. *
  113. * Returns a pointer to a response for a command.
  114. */
  115. static inline struct scm_response *scm_command_to_response(
  116. const struct scm_command *cmd)
  117. {
  118. return (void *)cmd + cmd->resp_hdr_offset;
  119. }
  120. /**
  121. * scm_get_command_buffer() - Get a pointer to a command buffer
  122. * @cmd: command
  123. *
  124. * Returns a pointer to the command buffer of a command.
  125. */
  126. static inline void *scm_get_command_buffer(const struct scm_command *cmd)
  127. {
  128. return (void *)cmd->buf;
  129. }
  130. /**
  131. * scm_get_response_buffer() - Get a pointer to a response buffer
  132. * @rsp: response
  133. *
  134. * Returns a pointer to a response buffer of a response.
  135. */
  136. static inline void *scm_get_response_buffer(const struct scm_response *rsp)
  137. {
  138. return (void *)rsp + rsp->buf_offset;
  139. }
  140. static int scm_remap_error(int err)
  141. {
  142. switch (err) {
  143. case SCM_ERROR:
  144. return -EIO;
  145. case SCM_EINVAL_ADDR:
  146. case SCM_EINVAL_ARG:
  147. return -EINVAL;
  148. case SCM_EOPNOTSUPP:
  149. return -EOPNOTSUPP;
  150. case SCM_ENOMEM:
  151. return -ENOMEM;
  152. }
  153. return -EINVAL;
  154. }
  155. static u32 smc(u32 cmd_addr)
  156. {
  157. int context_id;
  158. register u32 r0 asm("r0") = 1;
  159. register u32 r1 asm("r1") = (u32)&context_id;
  160. register u32 r2 asm("r2") = cmd_addr;
  161. asm(
  162. __asmeq("%0", "r0")
  163. __asmeq("%1", "r0")
  164. __asmeq("%2", "r1")
  165. __asmeq("%3", "r2")
  166. "smc #0 @ switch to secure world\n"
  167. : "=r" (r0)
  168. : "r" (r0), "r" (r1), "r" (r2)
  169. : "r3");
  170. return r0;
  171. }
  172. static int __scm_call(const struct scm_command *cmd)
  173. {
  174. int ret;
  175. u32 cmd_addr = virt_to_phys(cmd);
  176. /*
  177. * Flush the entire cache here so callers don't have to remember
  178. * to flush the cache when passing physical addresses to the secure
  179. * side in the buffer.
  180. */
  181. flush_cache_all();
  182. do {
  183. ret = smc(cmd_addr);
  184. if (ret < 0) {
  185. ret = scm_remap_error(ret);
  186. break;
  187. }
  188. } while (ret == SCM_INTERRUPTED);
  189. return ret;
  190. }
  191. /**
  192. * scm_call() - Send an SCM command
  193. * @svc_id: service identifier
  194. * @cmd_id: command identifier
  195. * @cmd_buf: command buffer
  196. * @cmd_len: length of the command buffer
  197. * @resp_buf: response buffer
  198. * @resp_len: length of the response buffer
  199. *
  200. * Sends a command to the SCM and waits for the command to finish processing.
  201. */
  202. int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
  203. void *resp_buf, size_t resp_len)
  204. {
  205. int ret;
  206. struct scm_command *cmd;
  207. struct scm_response *rsp;
  208. cmd = alloc_scm_command(cmd_len, resp_len);
  209. if (!cmd)
  210. return -ENOMEM;
  211. cmd->id = (svc_id << 10) | cmd_id;
  212. if (cmd_buf)
  213. memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
  214. mutex_lock(&scm_lock);
  215. ret = __scm_call(cmd);
  216. mutex_unlock(&scm_lock);
  217. if (ret)
  218. goto out;
  219. rsp = scm_command_to_response(cmd);
  220. do {
  221. u32 start = (u32)rsp;
  222. u32 end = (u32)scm_get_response_buffer(rsp) + resp_len;
  223. start &= ~(CACHELINESIZE - 1);
  224. while (start < end) {
  225. asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
  226. : "memory");
  227. start += CACHELINESIZE;
  228. }
  229. } while (!rsp->is_complete);
  230. if (resp_buf)
  231. memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
  232. out:
  233. free_scm_command(cmd);
  234. return ret;
  235. }
  236. EXPORT_SYMBOL(scm_call);
  237. u32 scm_get_version(void)
  238. {
  239. int context_id;
  240. static u32 version = -1;
  241. register u32 r0 asm("r0") = 0x1 << 8;
  242. register u32 r1 asm("r1") = (u32)&context_id;
  243. if (version != -1)
  244. return version;
  245. mutex_lock(&scm_lock);
  246. asm(
  247. __asmeq("%0", "r1")
  248. __asmeq("%1", "r0")
  249. __asmeq("%2", "r1")
  250. "smc #0 @ switch to secure world\n"
  251. : "=r" (r1)
  252. : "r" (r0), "r" (r1)
  253. : "r2", "r3");
  254. version = r1;
  255. mutex_unlock(&scm_lock);
  256. return version;
  257. }
  258. EXPORT_SYMBOL(scm_get_version);