scm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 laid 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. do {
  162. asm volatile(
  163. __asmeq("%0", "r0")
  164. __asmeq("%1", "r0")
  165. __asmeq("%2", "r1")
  166. __asmeq("%3", "r2")
  167. "smc #0 @ switch to secure world\n"
  168. : "=r" (r0)
  169. : "r" (r0), "r" (r1), "r" (r2)
  170. : "r3");
  171. } while (r0 == SCM_INTERRUPTED);
  172. return r0;
  173. }
  174. static int __scm_call(const struct scm_command *cmd)
  175. {
  176. int ret;
  177. u32 cmd_addr = virt_to_phys(cmd);
  178. /*
  179. * Flush the entire cache here so callers don't have to remember
  180. * to flush the cache when passing physical addresses to the secure
  181. * side in the buffer.
  182. */
  183. flush_cache_all();
  184. ret = smc(cmd_addr);
  185. if (ret < 0)
  186. ret = scm_remap_error(ret);
  187. return ret;
  188. }
  189. /**
  190. * scm_call() - Send an SCM command
  191. * @svc_id: service identifier
  192. * @cmd_id: command identifier
  193. * @cmd_buf: command buffer
  194. * @cmd_len: length of the command buffer
  195. * @resp_buf: response buffer
  196. * @resp_len: length of the response buffer
  197. *
  198. * Sends a command to the SCM and waits for the command to finish processing.
  199. */
  200. int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len,
  201. void *resp_buf, size_t resp_len)
  202. {
  203. int ret;
  204. struct scm_command *cmd;
  205. struct scm_response *rsp;
  206. cmd = alloc_scm_command(cmd_len, resp_len);
  207. if (!cmd)
  208. return -ENOMEM;
  209. cmd->id = (svc_id << 10) | cmd_id;
  210. if (cmd_buf)
  211. memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
  212. mutex_lock(&scm_lock);
  213. ret = __scm_call(cmd);
  214. mutex_unlock(&scm_lock);
  215. if (ret)
  216. goto out;
  217. rsp = scm_command_to_response(cmd);
  218. do {
  219. u32 start = (u32)rsp;
  220. u32 end = (u32)scm_get_response_buffer(rsp) + resp_len;
  221. start &= ~(CACHELINESIZE - 1);
  222. while (start < end) {
  223. asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
  224. : "memory");
  225. start += CACHELINESIZE;
  226. }
  227. } while (!rsp->is_complete);
  228. if (resp_buf)
  229. memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len);
  230. out:
  231. free_scm_command(cmd);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL(scm_call);
  235. u32 scm_get_version(void)
  236. {
  237. int context_id;
  238. static u32 version = -1;
  239. register u32 r0 asm("r0");
  240. register u32 r1 asm("r1");
  241. if (version != -1)
  242. return version;
  243. mutex_lock(&scm_lock);
  244. r0 = 0x1 << 8;
  245. r1 = (u32)&context_id;
  246. do {
  247. asm volatile(
  248. __asmeq("%0", "r0")
  249. __asmeq("%1", "r1")
  250. __asmeq("%2", "r0")
  251. __asmeq("%3", "r1")
  252. "smc #0 @ switch to secure world\n"
  253. : "=r" (r0), "=r" (r1)
  254. : "r" (r0), "r" (r1)
  255. : "r2", "r3");
  256. } while (r0 == SCM_INTERRUPTED);
  257. version = r1;
  258. mutex_unlock(&scm_lock);
  259. return version;
  260. }
  261. EXPORT_SYMBOL(scm_get_version);