qcom_scm-32.c 13 KB

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