qcom_scm-64.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. struct arm_smccc_quirk quirk = {.id = ARM_SMCCC_QUIRK_QCOM_A6};
  83. if (unlikely(arglen > N_REGISTER_ARGS)) {
  84. alloc_len = N_EXT_QCOM_SCM_ARGS * sizeof(u64);
  85. args_virt = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL);
  86. if (!args_virt)
  87. return -ENOMEM;
  88. if (qcom_smccc_convention == ARM_SMCCC_SMC_32) {
  89. __le32 *args = args_virt;
  90. for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
  91. args[i] = cpu_to_le32(desc->args[i +
  92. FIRST_EXT_ARG_IDX]);
  93. } else {
  94. __le64 *args = args_virt;
  95. for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
  96. args[i] = cpu_to_le64(desc->args[i +
  97. FIRST_EXT_ARG_IDX]);
  98. }
  99. args_phys = dma_map_single(dev, args_virt, alloc_len,
  100. DMA_TO_DEVICE);
  101. if (dma_mapping_error(dev, args_phys)) {
  102. kfree(args_virt);
  103. return -ENOMEM;
  104. }
  105. x5 = args_phys;
  106. }
  107. do {
  108. mutex_lock(&qcom_scm_lock);
  109. cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL,
  110. qcom_smccc_convention,
  111. ARM_SMCCC_OWNER_SIP, fn_id);
  112. quirk.state.a6 = 0;
  113. do {
  114. arm_smccc_smc_quirk(cmd, desc->arginfo, desc->args[0],
  115. desc->args[1], desc->args[2], x5,
  116. quirk.state.a6, 0, res, &quirk);
  117. if (res->a0 == QCOM_SCM_INTERRUPTED)
  118. cmd = res->a0;
  119. } while (res->a0 == QCOM_SCM_INTERRUPTED);
  120. mutex_unlock(&qcom_scm_lock);
  121. if (res->a0 == QCOM_SCM_V2_EBUSY) {
  122. if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
  123. break;
  124. msleep(QCOM_SCM_EBUSY_WAIT_MS);
  125. }
  126. } while (res->a0 == QCOM_SCM_V2_EBUSY);
  127. if (args_virt) {
  128. dma_unmap_single(dev, args_phys, alloc_len, DMA_TO_DEVICE);
  129. kfree(args_virt);
  130. }
  131. if (res->a0 < 0)
  132. return qcom_scm_remap_error(res->a0);
  133. return 0;
  134. }
  135. /**
  136. * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
  137. * @entry: Entry point function for the cpus
  138. * @cpus: The cpumask of cpus that will use the entry point
  139. *
  140. * Set the cold boot address of the cpus. Any cpu outside the supported
  141. * range would be removed from the cpu present mask.
  142. */
  143. int __qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
  144. {
  145. return -ENOTSUPP;
  146. }
  147. /**
  148. * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
  149. * @dev: Device pointer
  150. * @entry: Entry point function for the cpus
  151. * @cpus: The cpumask of cpus that will use the entry point
  152. *
  153. * Set the Linux entry point for the SCM to transfer control to when coming
  154. * out of a power down. CPU power down may be executed on cpuidle or hotplug.
  155. */
  156. int __qcom_scm_set_warm_boot_addr(struct device *dev, void *entry,
  157. const cpumask_t *cpus)
  158. {
  159. return -ENOTSUPP;
  160. }
  161. /**
  162. * qcom_scm_cpu_power_down() - Power down the cpu
  163. * @flags - Flags to flush cache
  164. *
  165. * This is an end point to power down cpu. If there was a pending interrupt,
  166. * the control would return from this function, otherwise, the cpu jumps to the
  167. * warm boot entry point set for this cpu upon reset.
  168. */
  169. void __qcom_scm_cpu_power_down(u32 flags)
  170. {
  171. }
  172. int __qcom_scm_is_call_available(struct device *dev, u32 svc_id, u32 cmd_id)
  173. {
  174. int ret;
  175. struct qcom_scm_desc desc = {0};
  176. struct arm_smccc_res res;
  177. desc.arginfo = QCOM_SCM_ARGS(1);
  178. desc.args[0] = QCOM_SCM_FNID(svc_id, cmd_id) |
  179. (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
  180. ret = qcom_scm_call(dev, QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD,
  181. &desc, &res);
  182. return ret ? : res.a1;
  183. }
  184. int __qcom_scm_hdcp_req(struct device *dev, struct qcom_scm_hdcp_req *req,
  185. u32 req_cnt, u32 *resp)
  186. {
  187. int ret;
  188. struct qcom_scm_desc desc = {0};
  189. struct arm_smccc_res res;
  190. if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
  191. return -ERANGE;
  192. desc.args[0] = req[0].addr;
  193. desc.args[1] = req[0].val;
  194. desc.args[2] = req[1].addr;
  195. desc.args[3] = req[1].val;
  196. desc.args[4] = req[2].addr;
  197. desc.args[5] = req[2].val;
  198. desc.args[6] = req[3].addr;
  199. desc.args[7] = req[3].val;
  200. desc.args[8] = req[4].addr;
  201. desc.args[9] = req[4].val;
  202. desc.arginfo = QCOM_SCM_ARGS(10);
  203. ret = qcom_scm_call(dev, QCOM_SCM_SVC_HDCP, QCOM_SCM_CMD_HDCP, &desc,
  204. &res);
  205. *resp = res.a1;
  206. return ret;
  207. }
  208. void __qcom_scm_init(void)
  209. {
  210. u64 cmd;
  211. struct arm_smccc_res res;
  212. u32 function = QCOM_SCM_FNID(QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD);
  213. /* First try a SMC64 call */
  214. cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
  215. ARM_SMCCC_OWNER_SIP, function);
  216. arm_smccc_smc(cmd, QCOM_SCM_ARGS(1), cmd & (~BIT(ARM_SMCCC_TYPE_SHIFT)),
  217. 0, 0, 0, 0, 0, &res);
  218. if (!res.a0 && res.a1)
  219. qcom_smccc_convention = ARM_SMCCC_SMC_64;
  220. else
  221. qcom_smccc_convention = ARM_SMCCC_SMC_32;
  222. }
  223. bool __qcom_scm_pas_supported(struct device *dev, u32 peripheral)
  224. {
  225. int ret;
  226. struct qcom_scm_desc desc = {0};
  227. struct arm_smccc_res res;
  228. desc.args[0] = peripheral;
  229. desc.arginfo = QCOM_SCM_ARGS(1);
  230. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
  231. QCOM_SCM_PAS_IS_SUPPORTED_CMD,
  232. &desc, &res);
  233. return ret ? false : !!res.a1;
  234. }
  235. int __qcom_scm_pas_init_image(struct device *dev, u32 peripheral,
  236. dma_addr_t metadata_phys)
  237. {
  238. int ret;
  239. struct qcom_scm_desc desc = {0};
  240. struct arm_smccc_res res;
  241. desc.args[0] = peripheral;
  242. desc.args[1] = metadata_phys;
  243. desc.arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW);
  244. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_INIT_IMAGE_CMD,
  245. &desc, &res);
  246. return ret ? : res.a1;
  247. }
  248. int __qcom_scm_pas_mem_setup(struct device *dev, u32 peripheral,
  249. phys_addr_t addr, phys_addr_t size)
  250. {
  251. int ret;
  252. struct qcom_scm_desc desc = {0};
  253. struct arm_smccc_res res;
  254. desc.args[0] = peripheral;
  255. desc.args[1] = addr;
  256. desc.args[2] = size;
  257. desc.arginfo = QCOM_SCM_ARGS(3);
  258. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_MEM_SETUP_CMD,
  259. &desc, &res);
  260. return ret ? : res.a1;
  261. }
  262. int __qcom_scm_pas_auth_and_reset(struct device *dev, u32 peripheral)
  263. {
  264. int ret;
  265. struct qcom_scm_desc desc = {0};
  266. struct arm_smccc_res res;
  267. desc.args[0] = peripheral;
  268. desc.arginfo = QCOM_SCM_ARGS(1);
  269. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
  270. QCOM_SCM_PAS_AUTH_AND_RESET_CMD,
  271. &desc, &res);
  272. return ret ? : res.a1;
  273. }
  274. int __qcom_scm_pas_shutdown(struct device *dev, u32 peripheral)
  275. {
  276. int ret;
  277. struct qcom_scm_desc desc = {0};
  278. struct arm_smccc_res res;
  279. desc.args[0] = peripheral;
  280. desc.arginfo = QCOM_SCM_ARGS(1);
  281. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_SHUTDOWN_CMD,
  282. &desc, &res);
  283. return ret ? : res.a1;
  284. }
  285. int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
  286. {
  287. struct qcom_scm_desc desc = {0};
  288. struct arm_smccc_res res;
  289. int ret;
  290. desc.args[0] = reset;
  291. desc.args[1] = 0;
  292. desc.arginfo = QCOM_SCM_ARGS(2);
  293. ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_MSS_RESET, &desc,
  294. &res);
  295. return ret ? : res.a1;
  296. }