qcom_scm-32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* Copyright (c) 2010,2015, The Linux Foundation. All rights reserved.
  2. * Copyright (C) 2015 Linaro Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA.
  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. #include <linux/qcom_scm.h>
  25. #include <asm/cacheflush.h>
  26. #include "qcom_scm.h"
  27. #define QCOM_SCM_FLAG_COLDBOOT_CPU0 0x00
  28. #define QCOM_SCM_FLAG_COLDBOOT_CPU1 0x01
  29. #define QCOM_SCM_FLAG_COLDBOOT_CPU2 0x08
  30. #define QCOM_SCM_FLAG_COLDBOOT_CPU3 0x20
  31. #define QCOM_SCM_FLAG_WARMBOOT_CPU0 0x04
  32. #define QCOM_SCM_FLAG_WARMBOOT_CPU1 0x02
  33. #define QCOM_SCM_FLAG_WARMBOOT_CPU2 0x10
  34. #define QCOM_SCM_FLAG_WARMBOOT_CPU3 0x40
  35. struct qcom_scm_entry {
  36. int flag;
  37. void *entry;
  38. };
  39. static struct qcom_scm_entry qcom_scm_wb[] = {
  40. { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU0 },
  41. { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU1 },
  42. { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU2 },
  43. { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU3 },
  44. };
  45. static DEFINE_MUTEX(qcom_scm_lock);
  46. /**
  47. * struct qcom_scm_command - one SCM command buffer
  48. * @len: total available memory for command and response
  49. * @buf_offset: start of command buffer
  50. * @resp_hdr_offset: start of response buffer
  51. * @id: command to be executed
  52. * @buf: buffer returned from qcom_scm_get_command_buffer()
  53. *
  54. * An SCM command is laid out in memory as follows:
  55. *
  56. * ------------------- <--- struct qcom_scm_command
  57. * | command header |
  58. * ------------------- <--- qcom_scm_get_command_buffer()
  59. * | command buffer |
  60. * ------------------- <--- struct qcom_scm_response and
  61. * | response header | qcom_scm_command_to_response()
  62. * ------------------- <--- qcom_scm_get_response_buffer()
  63. * | response buffer |
  64. * -------------------
  65. *
  66. * There can be arbitrary padding between the headers and buffers so
  67. * you should always use the appropriate qcom_scm_get_*_buffer() routines
  68. * to access the buffers in a safe manner.
  69. */
  70. struct qcom_scm_command {
  71. __le32 len;
  72. __le32 buf_offset;
  73. __le32 resp_hdr_offset;
  74. __le32 id;
  75. __le32 buf[0];
  76. };
  77. /**
  78. * struct qcom_scm_response - one SCM response buffer
  79. * @len: total available memory for response
  80. * @buf_offset: start of response data relative to start of qcom_scm_response
  81. * @is_complete: indicates if the command has finished processing
  82. */
  83. struct qcom_scm_response {
  84. __le32 len;
  85. __le32 buf_offset;
  86. __le32 is_complete;
  87. };
  88. /**
  89. * alloc_qcom_scm_command() - Allocate an SCM command
  90. * @cmd_size: size of the command buffer
  91. * @resp_size: size of the response buffer
  92. *
  93. * Allocate an SCM command, including enough room for the command
  94. * and response headers as well as the command and response buffers.
  95. *
  96. * Returns a valid &qcom_scm_command on success or %NULL if the allocation fails.
  97. */
  98. static struct qcom_scm_command *alloc_qcom_scm_command(size_t cmd_size, size_t resp_size)
  99. {
  100. struct qcom_scm_command *cmd;
  101. size_t len = sizeof(*cmd) + sizeof(struct qcom_scm_response) + cmd_size +
  102. resp_size;
  103. u32 offset;
  104. cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
  105. if (cmd) {
  106. cmd->len = cpu_to_le32(len);
  107. offset = offsetof(struct qcom_scm_command, buf);
  108. cmd->buf_offset = cpu_to_le32(offset);
  109. cmd->resp_hdr_offset = cpu_to_le32(offset + cmd_size);
  110. }
  111. return cmd;
  112. }
  113. /**
  114. * free_qcom_scm_command() - Free an SCM command
  115. * @cmd: command to free
  116. *
  117. * Free an SCM command.
  118. */
  119. static inline void free_qcom_scm_command(struct qcom_scm_command *cmd)
  120. {
  121. kfree(cmd);
  122. }
  123. /**
  124. * qcom_scm_command_to_response() - Get a pointer to a qcom_scm_response
  125. * @cmd: command
  126. *
  127. * Returns a pointer to a response for a command.
  128. */
  129. static inline struct qcom_scm_response *qcom_scm_command_to_response(
  130. const struct qcom_scm_command *cmd)
  131. {
  132. return (void *)cmd + le32_to_cpu(cmd->resp_hdr_offset);
  133. }
  134. /**
  135. * qcom_scm_get_command_buffer() - Get a pointer to a command buffer
  136. * @cmd: command
  137. *
  138. * Returns a pointer to the command buffer of a command.
  139. */
  140. static inline void *qcom_scm_get_command_buffer(const struct qcom_scm_command *cmd)
  141. {
  142. return (void *)cmd->buf;
  143. }
  144. /**
  145. * qcom_scm_get_response_buffer() - Get a pointer to a response buffer
  146. * @rsp: response
  147. *
  148. * Returns a pointer to a response buffer of a response.
  149. */
  150. static inline void *qcom_scm_get_response_buffer(const struct qcom_scm_response *rsp)
  151. {
  152. return (void *)rsp + le32_to_cpu(rsp->buf_offset);
  153. }
  154. static int qcom_scm_remap_error(int err)
  155. {
  156. pr_err("qcom_scm_call failed with error code %d\n", err);
  157. switch (err) {
  158. case QCOM_SCM_ERROR:
  159. return -EIO;
  160. case QCOM_SCM_EINVAL_ADDR:
  161. case QCOM_SCM_EINVAL_ARG:
  162. return -EINVAL;
  163. case QCOM_SCM_EOPNOTSUPP:
  164. return -EOPNOTSUPP;
  165. case QCOM_SCM_ENOMEM:
  166. return -ENOMEM;
  167. }
  168. return -EINVAL;
  169. }
  170. static u32 smc(u32 cmd_addr)
  171. {
  172. int context_id;
  173. register u32 r0 asm("r0") = 1;
  174. register u32 r1 asm("r1") = (u32)&context_id;
  175. register u32 r2 asm("r2") = cmd_addr;
  176. do {
  177. asm volatile(
  178. __asmeq("%0", "r0")
  179. __asmeq("%1", "r0")
  180. __asmeq("%2", "r1")
  181. __asmeq("%3", "r2")
  182. #ifdef REQUIRES_SEC
  183. ".arch_extension sec\n"
  184. #endif
  185. "smc #0 @ switch to secure world\n"
  186. : "=r" (r0)
  187. : "r" (r0), "r" (r1), "r" (r2)
  188. : "r3");
  189. } while (r0 == QCOM_SCM_INTERRUPTED);
  190. return r0;
  191. }
  192. static int __qcom_scm_call(const struct qcom_scm_command *cmd)
  193. {
  194. int ret;
  195. u32 cmd_addr = virt_to_phys(cmd);
  196. /*
  197. * Flush the command buffer so that the secure world sees
  198. * the correct data.
  199. */
  200. secure_flush_area(cmd, cmd->len);
  201. ret = smc(cmd_addr);
  202. if (ret < 0)
  203. ret = qcom_scm_remap_error(ret);
  204. return ret;
  205. }
  206. static void qcom_scm_inv_range(unsigned long start, unsigned long end)
  207. {
  208. u32 cacheline_size, ctr;
  209. asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
  210. cacheline_size = 4 << ((ctr >> 16) & 0xf);
  211. start = round_down(start, cacheline_size);
  212. end = round_up(end, cacheline_size);
  213. outer_inv_range(start, end);
  214. while (start < end) {
  215. asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
  216. : "memory");
  217. start += cacheline_size;
  218. }
  219. dsb();
  220. isb();
  221. }
  222. /**
  223. * qcom_scm_call() - Send an SCM command
  224. * @svc_id: service identifier
  225. * @cmd_id: command identifier
  226. * @cmd_buf: command buffer
  227. * @cmd_len: length of the command buffer
  228. * @resp_buf: response buffer
  229. * @resp_len: length of the response buffer
  230. *
  231. * Sends a command to the SCM and waits for the command to finish processing.
  232. *
  233. * A note on cache maintenance:
  234. * Note that any buffers that are expected to be accessed by the secure world
  235. * must be flushed before invoking qcom_scm_call and invalidated in the cache
  236. * immediately after qcom_scm_call returns. Cache maintenance on the command
  237. * and response buffers is taken care of by qcom_scm_call; however, callers are
  238. * responsible for any other cached buffers passed over to the secure world.
  239. */
  240. static int qcom_scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf,
  241. size_t cmd_len, void *resp_buf, size_t resp_len)
  242. {
  243. int ret;
  244. struct qcom_scm_command *cmd;
  245. struct qcom_scm_response *rsp;
  246. unsigned long start, end;
  247. cmd = alloc_qcom_scm_command(cmd_len, resp_len);
  248. if (!cmd)
  249. return -ENOMEM;
  250. cmd->id = cpu_to_le32((svc_id << 10) | cmd_id);
  251. if (cmd_buf)
  252. memcpy(qcom_scm_get_command_buffer(cmd), cmd_buf, cmd_len);
  253. mutex_lock(&qcom_scm_lock);
  254. ret = __qcom_scm_call(cmd);
  255. mutex_unlock(&qcom_scm_lock);
  256. if (ret)
  257. goto out;
  258. rsp = qcom_scm_command_to_response(cmd);
  259. start = (unsigned long)rsp;
  260. do {
  261. qcom_scm_inv_range(start, start + sizeof(*rsp));
  262. } while (!rsp->is_complete);
  263. end = (unsigned long)qcom_scm_get_response_buffer(rsp) + resp_len;
  264. qcom_scm_inv_range(start, end);
  265. if (resp_buf)
  266. memcpy(resp_buf, qcom_scm_get_response_buffer(rsp), resp_len);
  267. out:
  268. free_qcom_scm_command(cmd);
  269. return ret;
  270. }
  271. #define SCM_CLASS_REGISTER (0x2 << 8)
  272. #define SCM_MASK_IRQS BIT(5)
  273. #define SCM_ATOMIC(svc, cmd, n) (((((svc) << 10)|((cmd) & 0x3ff)) << 12) | \
  274. SCM_CLASS_REGISTER | \
  275. SCM_MASK_IRQS | \
  276. (n & 0xf))
  277. /**
  278. * qcom_scm_call_atomic1() - Send an atomic SCM command with one argument
  279. * @svc_id: service identifier
  280. * @cmd_id: command identifier
  281. * @arg1: first argument
  282. *
  283. * This shall only be used with commands that are guaranteed to be
  284. * uninterruptable, atomic and SMP safe.
  285. */
  286. static s32 qcom_scm_call_atomic1(u32 svc, u32 cmd, u32 arg1)
  287. {
  288. int context_id;
  289. register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 1);
  290. register u32 r1 asm("r1") = (u32)&context_id;
  291. register u32 r2 asm("r2") = arg1;
  292. asm volatile(
  293. __asmeq("%0", "r0")
  294. __asmeq("%1", "r0")
  295. __asmeq("%2", "r1")
  296. __asmeq("%3", "r2")
  297. #ifdef REQUIRES_SEC
  298. ".arch_extension sec\n"
  299. #endif
  300. "smc #0 @ switch to secure world\n"
  301. : "=r" (r0)
  302. : "r" (r0), "r" (r1), "r" (r2)
  303. : "r3");
  304. return r0;
  305. }
  306. u32 qcom_scm_get_version(void)
  307. {
  308. int context_id;
  309. static u32 version = -1;
  310. register u32 r0 asm("r0");
  311. register u32 r1 asm("r1");
  312. if (version != -1)
  313. return version;
  314. mutex_lock(&qcom_scm_lock);
  315. r0 = 0x1 << 8;
  316. r1 = (u32)&context_id;
  317. do {
  318. asm volatile(
  319. __asmeq("%0", "r0")
  320. __asmeq("%1", "r1")
  321. __asmeq("%2", "r0")
  322. __asmeq("%3", "r1")
  323. #ifdef REQUIRES_SEC
  324. ".arch_extension sec\n"
  325. #endif
  326. "smc #0 @ switch to secure world\n"
  327. : "=r" (r0), "=r" (r1)
  328. : "r" (r0), "r" (r1)
  329. : "r2", "r3");
  330. } while (r0 == QCOM_SCM_INTERRUPTED);
  331. version = r1;
  332. mutex_unlock(&qcom_scm_lock);
  333. return version;
  334. }
  335. EXPORT_SYMBOL(qcom_scm_get_version);
  336. /*
  337. * Set the cold/warm boot address for one of the CPU cores.
  338. */
  339. static int qcom_scm_set_boot_addr(u32 addr, int flags)
  340. {
  341. struct {
  342. __le32 flags;
  343. __le32 addr;
  344. } cmd;
  345. cmd.addr = cpu_to_le32(addr);
  346. cmd.flags = cpu_to_le32(flags);
  347. return qcom_scm_call(QCOM_SCM_SVC_BOOT, QCOM_SCM_BOOT_ADDR,
  348. &cmd, sizeof(cmd), NULL, 0);
  349. }
  350. /**
  351. * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
  352. * @entry: Entry point function for the cpus
  353. * @cpus: The cpumask of cpus that will use the entry point
  354. *
  355. * Set the cold boot address of the cpus. Any cpu outside the supported
  356. * range would be removed from the cpu present mask.
  357. */
  358. int __qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
  359. {
  360. int flags = 0;
  361. int cpu;
  362. int scm_cb_flags[] = {
  363. QCOM_SCM_FLAG_COLDBOOT_CPU0,
  364. QCOM_SCM_FLAG_COLDBOOT_CPU1,
  365. QCOM_SCM_FLAG_COLDBOOT_CPU2,
  366. QCOM_SCM_FLAG_COLDBOOT_CPU3,
  367. };
  368. if (!cpus || (cpus && cpumask_empty(cpus)))
  369. return -EINVAL;
  370. for_each_cpu(cpu, cpus) {
  371. if (cpu < ARRAY_SIZE(scm_cb_flags))
  372. flags |= scm_cb_flags[cpu];
  373. else
  374. set_cpu_present(cpu, false);
  375. }
  376. return qcom_scm_set_boot_addr(virt_to_phys(entry), flags);
  377. }
  378. /**
  379. * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
  380. * @entry: Entry point function for the cpus
  381. * @cpus: The cpumask of cpus that will use the entry point
  382. *
  383. * Set the Linux entry point for the SCM to transfer control to when coming
  384. * out of a power down. CPU power down may be executed on cpuidle or hotplug.
  385. */
  386. int __qcom_scm_set_warm_boot_addr(void *entry, const cpumask_t *cpus)
  387. {
  388. int ret;
  389. int flags = 0;
  390. int cpu;
  391. /*
  392. * Reassign only if we are switching from hotplug entry point
  393. * to cpuidle entry point or vice versa.
  394. */
  395. for_each_cpu(cpu, cpus) {
  396. if (entry == qcom_scm_wb[cpu].entry)
  397. continue;
  398. flags |= qcom_scm_wb[cpu].flag;
  399. }
  400. /* No change in entry function */
  401. if (!flags)
  402. return 0;
  403. ret = qcom_scm_set_boot_addr(virt_to_phys(entry), flags);
  404. if (!ret) {
  405. for_each_cpu(cpu, cpus)
  406. qcom_scm_wb[cpu].entry = entry;
  407. }
  408. return ret;
  409. }
  410. /**
  411. * qcom_scm_cpu_power_down() - Power down the cpu
  412. * @flags - Flags to flush cache
  413. *
  414. * This is an end point to power down cpu. If there was a pending interrupt,
  415. * the control would return from this function, otherwise, the cpu jumps to the
  416. * warm boot entry point set for this cpu upon reset.
  417. */
  418. void __qcom_scm_cpu_power_down(u32 flags)
  419. {
  420. qcom_scm_call_atomic1(QCOM_SCM_SVC_BOOT, QCOM_SCM_CMD_TERMINATE_PC,
  421. flags & QCOM_SCM_FLUSH_FLAG_MASK);
  422. }
  423. int __qcom_scm_is_call_available(u32 svc_id, u32 cmd_id)
  424. {
  425. int ret;
  426. __le32 svc_cmd = cpu_to_le32((svc_id << 10) | cmd_id);
  427. __le32 ret_val = 0;
  428. ret = qcom_scm_call(QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD, &svc_cmd,
  429. sizeof(svc_cmd), &ret_val, sizeof(ret_val));
  430. if (ret)
  431. return ret;
  432. return le32_to_cpu(ret_val);
  433. }
  434. int __qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
  435. {
  436. if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
  437. return -ERANGE;
  438. return qcom_scm_call(QCOM_SCM_SVC_HDCP, QCOM_SCM_CMD_HDCP,
  439. req, req_cnt * sizeof(*req), resp, sizeof(*resp));
  440. }