qcom_scm-64.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* Copyright (c) 2015, The Linux Foundation. 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. #include <linux/io.h>
  13. #include <linux/errno.h>
  14. #include <linux/delay.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/qcom_scm.h>
  19. #include <linux/arm-smccc.h>
  20. #include <linux/dma-mapping.h>
  21. #include "qcom_scm.h"
  22. #define QCOM_SCM_FNID(s, c) ((((s) & 0xFF) << 8) | ((c) & 0xFF))
  23. #define MAX_QCOM_SCM_ARGS 10
  24. #define MAX_QCOM_SCM_RETS 3
  25. enum qcom_scm_arg_types {
  26. QCOM_SCM_VAL,
  27. QCOM_SCM_RO,
  28. QCOM_SCM_RW,
  29. QCOM_SCM_BUFVAL,
  30. };
  31. #define QCOM_SCM_ARGS_IMPL(num, a, b, c, d, e, f, g, h, i, j, ...) (\
  32. (((a) & 0x3) << 4) | \
  33. (((b) & 0x3) << 6) | \
  34. (((c) & 0x3) << 8) | \
  35. (((d) & 0x3) << 10) | \
  36. (((e) & 0x3) << 12) | \
  37. (((f) & 0x3) << 14) | \
  38. (((g) & 0x3) << 16) | \
  39. (((h) & 0x3) << 18) | \
  40. (((i) & 0x3) << 20) | \
  41. (((j) & 0x3) << 22) | \
  42. ((num) & 0xf))
  43. #define QCOM_SCM_ARGS(...) QCOM_SCM_ARGS_IMPL(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  44. /**
  45. * struct qcom_scm_desc
  46. * @arginfo: Metadata describing the arguments in args[]
  47. * @args: The array of arguments for the secure syscall
  48. * @res: The values returned by the secure syscall
  49. */
  50. struct qcom_scm_desc {
  51. u32 arginfo;
  52. u64 args[MAX_QCOM_SCM_ARGS];
  53. };
  54. static u64 qcom_smccc_convention = -1;
  55. static DEFINE_MUTEX(qcom_scm_lock);
  56. #define QCOM_SCM_EBUSY_WAIT_MS 30
  57. #define QCOM_SCM_EBUSY_MAX_RETRY 20
  58. #define N_EXT_QCOM_SCM_ARGS 7
  59. #define FIRST_EXT_ARG_IDX 3
  60. #define N_REGISTER_ARGS (MAX_QCOM_SCM_ARGS - N_EXT_QCOM_SCM_ARGS + 1)
  61. /**
  62. * qcom_scm_call() - Invoke a syscall in the secure world
  63. * @dev: device
  64. * @svc_id: service identifier
  65. * @cmd_id: command identifier
  66. * @desc: Descriptor structure containing arguments and return values
  67. *
  68. * Sends a command to the SCM and waits for the command to finish processing.
  69. * This should *only* be called in pre-emptible context.
  70. */
  71. static int qcom_scm_call(struct device *dev, u32 svc_id, u32 cmd_id,
  72. const struct qcom_scm_desc *desc,
  73. struct arm_smccc_res *res)
  74. {
  75. int arglen = desc->arginfo & 0xf;
  76. int retry_count = 0, i;
  77. u32 fn_id = QCOM_SCM_FNID(svc_id, cmd_id);
  78. u64 cmd, x5 = desc->args[FIRST_EXT_ARG_IDX];
  79. dma_addr_t args_phys = 0;
  80. void *args_virt = NULL;
  81. size_t alloc_len;
  82. if (unlikely(arglen > N_REGISTER_ARGS)) {
  83. alloc_len = N_EXT_QCOM_SCM_ARGS * sizeof(u64);
  84. args_virt = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL);
  85. if (!args_virt)
  86. return -ENOMEM;
  87. if (qcom_smccc_convention == ARM_SMCCC_SMC_32) {
  88. __le32 *args = args_virt;
  89. for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
  90. args[i] = cpu_to_le32(desc->args[i +
  91. FIRST_EXT_ARG_IDX]);
  92. } else {
  93. __le64 *args = args_virt;
  94. for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
  95. args[i] = cpu_to_le64(desc->args[i +
  96. FIRST_EXT_ARG_IDX]);
  97. }
  98. args_phys = dma_map_single(dev, args_virt, alloc_len,
  99. DMA_TO_DEVICE);
  100. if (dma_mapping_error(dev, args_phys)) {
  101. kfree(args_virt);
  102. return -ENOMEM;
  103. }
  104. x5 = args_phys;
  105. }
  106. do {
  107. mutex_lock(&qcom_scm_lock);
  108. cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL,
  109. qcom_smccc_convention,
  110. ARM_SMCCC_OWNER_SIP, fn_id);
  111. do {
  112. arm_smccc_smc(cmd, desc->arginfo, desc->args[0],
  113. desc->args[1], desc->args[2], x5, 0, 0,
  114. res);
  115. } while (res->a0 == QCOM_SCM_INTERRUPTED);
  116. mutex_unlock(&qcom_scm_lock);
  117. if (res->a0 == QCOM_SCM_V2_EBUSY) {
  118. if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
  119. break;
  120. msleep(QCOM_SCM_EBUSY_WAIT_MS);
  121. }
  122. } while (res->a0 == QCOM_SCM_V2_EBUSY);
  123. if (args_virt) {
  124. dma_unmap_single(dev, args_phys, alloc_len, DMA_TO_DEVICE);
  125. kfree(args_virt);
  126. }
  127. if (res->a0 < 0)
  128. return qcom_scm_remap_error(res->a0);
  129. return 0;
  130. }
  131. /**
  132. * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
  133. * @entry: Entry point function for the cpus
  134. * @cpus: The cpumask of cpus that will use the entry point
  135. *
  136. * Set the cold boot address of the cpus. Any cpu outside the supported
  137. * range would be removed from the cpu present mask.
  138. */
  139. int __qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
  140. {
  141. return -ENOTSUPP;
  142. }
  143. /**
  144. * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
  145. * @dev: Device pointer
  146. * @entry: Entry point function for the cpus
  147. * @cpus: The cpumask of cpus that will use the entry point
  148. *
  149. * Set the Linux entry point for the SCM to transfer control to when coming
  150. * out of a power down. CPU power down may be executed on cpuidle or hotplug.
  151. */
  152. int __qcom_scm_set_warm_boot_addr(struct device *dev, void *entry,
  153. const cpumask_t *cpus)
  154. {
  155. return -ENOTSUPP;
  156. }
  157. /**
  158. * qcom_scm_cpu_power_down() - Power down the cpu
  159. * @flags - Flags to flush cache
  160. *
  161. * This is an end point to power down cpu. If there was a pending interrupt,
  162. * the control would return from this function, otherwise, the cpu jumps to the
  163. * warm boot entry point set for this cpu upon reset.
  164. */
  165. void __qcom_scm_cpu_power_down(u32 flags)
  166. {
  167. }
  168. int __qcom_scm_is_call_available(struct device *dev, u32 svc_id, u32 cmd_id)
  169. {
  170. int ret;
  171. struct qcom_scm_desc desc = {0};
  172. struct arm_smccc_res res;
  173. desc.arginfo = QCOM_SCM_ARGS(1);
  174. desc.args[0] = QCOM_SCM_FNID(svc_id, cmd_id) |
  175. (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
  176. ret = qcom_scm_call(dev, QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD,
  177. &desc, &res);
  178. return ret ? : res.a1;
  179. }
  180. int __qcom_scm_hdcp_req(struct device *dev, struct qcom_scm_hdcp_req *req,
  181. u32 req_cnt, u32 *resp)
  182. {
  183. int ret;
  184. struct qcom_scm_desc desc = {0};
  185. struct arm_smccc_res res;
  186. if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
  187. return -ERANGE;
  188. desc.args[0] = req[0].addr;
  189. desc.args[1] = req[0].val;
  190. desc.args[2] = req[1].addr;
  191. desc.args[3] = req[1].val;
  192. desc.args[4] = req[2].addr;
  193. desc.args[5] = req[2].val;
  194. desc.args[6] = req[3].addr;
  195. desc.args[7] = req[3].val;
  196. desc.args[8] = req[4].addr;
  197. desc.args[9] = req[4].val;
  198. desc.arginfo = QCOM_SCM_ARGS(10);
  199. ret = qcom_scm_call(dev, QCOM_SCM_SVC_HDCP, QCOM_SCM_CMD_HDCP, &desc,
  200. &res);
  201. *resp = res.a1;
  202. return ret;
  203. }
  204. void __qcom_scm_init(void)
  205. {
  206. u64 cmd;
  207. struct arm_smccc_res res;
  208. u32 function = QCOM_SCM_FNID(QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD);
  209. /* First try a SMC64 call */
  210. cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
  211. ARM_SMCCC_OWNER_SIP, function);
  212. arm_smccc_smc(cmd, QCOM_SCM_ARGS(1), cmd & (~BIT(ARM_SMCCC_TYPE_SHIFT)),
  213. 0, 0, 0, 0, 0, &res);
  214. if (!res.a0 && res.a1)
  215. qcom_smccc_convention = ARM_SMCCC_SMC_64;
  216. else
  217. qcom_smccc_convention = ARM_SMCCC_SMC_32;
  218. }
  219. bool __qcom_scm_pas_supported(struct device *dev, u32 peripheral)
  220. {
  221. int ret;
  222. struct qcom_scm_desc desc = {0};
  223. struct arm_smccc_res res;
  224. desc.args[0] = peripheral;
  225. desc.arginfo = QCOM_SCM_ARGS(1);
  226. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
  227. QCOM_SCM_PAS_IS_SUPPORTED_CMD,
  228. &desc, &res);
  229. return ret ? false : !!res.a1;
  230. }
  231. int __qcom_scm_pas_init_image(struct device *dev, u32 peripheral,
  232. dma_addr_t metadata_phys)
  233. {
  234. int ret;
  235. struct qcom_scm_desc desc = {0};
  236. struct arm_smccc_res res;
  237. desc.args[0] = peripheral;
  238. desc.args[1] = metadata_phys;
  239. desc.arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW);
  240. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_INIT_IMAGE_CMD,
  241. &desc, &res);
  242. return ret ? : res.a1;
  243. }
  244. int __qcom_scm_pas_mem_setup(struct device *dev, u32 peripheral,
  245. phys_addr_t addr, phys_addr_t size)
  246. {
  247. int ret;
  248. struct qcom_scm_desc desc = {0};
  249. struct arm_smccc_res res;
  250. desc.args[0] = peripheral;
  251. desc.args[1] = addr;
  252. desc.args[2] = size;
  253. desc.arginfo = QCOM_SCM_ARGS(3);
  254. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_MEM_SETUP_CMD,
  255. &desc, &res);
  256. return ret ? : res.a1;
  257. }
  258. int __qcom_scm_pas_auth_and_reset(struct device *dev, u32 peripheral)
  259. {
  260. int ret;
  261. struct qcom_scm_desc desc = {0};
  262. struct arm_smccc_res res;
  263. desc.args[0] = peripheral;
  264. desc.arginfo = QCOM_SCM_ARGS(1);
  265. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
  266. QCOM_SCM_PAS_AUTH_AND_RESET_CMD,
  267. &desc, &res);
  268. return ret ? : res.a1;
  269. }
  270. int __qcom_scm_pas_shutdown(struct device *dev, u32 peripheral)
  271. {
  272. int ret;
  273. struct qcom_scm_desc desc = {0};
  274. struct arm_smccc_res res;
  275. desc.args[0] = peripheral;
  276. desc.arginfo = QCOM_SCM_ARGS(1);
  277. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_SHUTDOWN_CMD,
  278. &desc, &res);
  279. return ret ? : res.a1;
  280. }
  281. int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
  282. {
  283. struct qcom_scm_desc desc = {0};
  284. struct arm_smccc_res res;
  285. int ret;
  286. desc.args[0] = reset;
  287. desc.args[1] = 0;
  288. desc.arginfo = QCOM_SCM_ARGS(2);
  289. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_MSS_RESET, &desc,
  290. &res);
  291. return ret ? : res.a1;
  292. }