qcom_scm-64.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #define QCOM_SCM_ARGS_IMPL(num, a, b, c, d, e, f, g, h, i, j, ...) (\
  26. (((a) & 0x3) << 4) | \
  27. (((b) & 0x3) << 6) | \
  28. (((c) & 0x3) << 8) | \
  29. (((d) & 0x3) << 10) | \
  30. (((e) & 0x3) << 12) | \
  31. (((f) & 0x3) << 14) | \
  32. (((g) & 0x3) << 16) | \
  33. (((h) & 0x3) << 18) | \
  34. (((i) & 0x3) << 20) | \
  35. (((j) & 0x3) << 22) | \
  36. ((num) & 0xf))
  37. #define QCOM_SCM_ARGS(...) QCOM_SCM_ARGS_IMPL(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  38. /**
  39. * struct qcom_scm_desc
  40. * @arginfo: Metadata describing the arguments in args[]
  41. * @args: The array of arguments for the secure syscall
  42. * @res: The values returned by the secure syscall
  43. */
  44. struct qcom_scm_desc {
  45. u32 arginfo;
  46. u64 args[MAX_QCOM_SCM_ARGS];
  47. };
  48. static u64 qcom_smccc_convention = -1;
  49. static DEFINE_MUTEX(qcom_scm_lock);
  50. #define QCOM_SCM_EBUSY_WAIT_MS 30
  51. #define QCOM_SCM_EBUSY_MAX_RETRY 20
  52. #define N_EXT_QCOM_SCM_ARGS 7
  53. #define FIRST_EXT_ARG_IDX 3
  54. #define N_REGISTER_ARGS (MAX_QCOM_SCM_ARGS - N_EXT_QCOM_SCM_ARGS + 1)
  55. /**
  56. * qcom_scm_call() - Invoke a syscall in the secure world
  57. * @dev: device
  58. * @svc_id: service identifier
  59. * @cmd_id: command identifier
  60. * @desc: Descriptor structure containing arguments and return values
  61. *
  62. * Sends a command to the SCM and waits for the command to finish processing.
  63. * This should *only* be called in pre-emptible context.
  64. */
  65. static int qcom_scm_call(struct device *dev, u32 svc_id, u32 cmd_id,
  66. const struct qcom_scm_desc *desc,
  67. struct arm_smccc_res *res)
  68. {
  69. int arglen = desc->arginfo & 0xf;
  70. int retry_count = 0, i;
  71. u32 fn_id = QCOM_SCM_FNID(svc_id, cmd_id);
  72. u64 cmd, x5 = desc->args[FIRST_EXT_ARG_IDX];
  73. dma_addr_t args_phys = 0;
  74. void *args_virt = NULL;
  75. size_t alloc_len;
  76. if (unlikely(arglen > N_REGISTER_ARGS)) {
  77. alloc_len = N_EXT_QCOM_SCM_ARGS * sizeof(u64);
  78. args_virt = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL);
  79. if (!args_virt)
  80. return -ENOMEM;
  81. if (qcom_smccc_convention == ARM_SMCCC_SMC_32) {
  82. __le32 *args = args_virt;
  83. for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
  84. args[i] = cpu_to_le32(desc->args[i +
  85. FIRST_EXT_ARG_IDX]);
  86. } else {
  87. __le64 *args = args_virt;
  88. for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
  89. args[i] = cpu_to_le64(desc->args[i +
  90. FIRST_EXT_ARG_IDX]);
  91. }
  92. args_phys = dma_map_single(dev, args_virt, alloc_len,
  93. DMA_TO_DEVICE);
  94. if (dma_mapping_error(dev, args_phys)) {
  95. kfree(args_virt);
  96. return -ENOMEM;
  97. }
  98. x5 = args_phys;
  99. }
  100. do {
  101. mutex_lock(&qcom_scm_lock);
  102. cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL,
  103. qcom_smccc_convention,
  104. ARM_SMCCC_OWNER_SIP, fn_id);
  105. do {
  106. arm_smccc_smc(cmd, desc->arginfo, desc->args[0],
  107. desc->args[1], desc->args[2], x5, 0, 0,
  108. res);
  109. } while (res->a0 == QCOM_SCM_INTERRUPTED);
  110. mutex_unlock(&qcom_scm_lock);
  111. if (res->a0 == QCOM_SCM_V2_EBUSY) {
  112. if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
  113. break;
  114. msleep(QCOM_SCM_EBUSY_WAIT_MS);
  115. }
  116. } while (res->a0 == QCOM_SCM_V2_EBUSY);
  117. if (args_virt) {
  118. dma_unmap_single(dev, args_phys, alloc_len, DMA_TO_DEVICE);
  119. kfree(args_virt);
  120. }
  121. if (res->a0 < 0)
  122. return qcom_scm_remap_error(res->a0);
  123. return 0;
  124. }
  125. /**
  126. * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
  127. * @entry: Entry point function for the cpus
  128. * @cpus: The cpumask of cpus that will use the entry point
  129. *
  130. * Set the cold boot address of the cpus. Any cpu outside the supported
  131. * range would be removed from the cpu present mask.
  132. */
  133. int __qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
  134. {
  135. return -ENOTSUPP;
  136. }
  137. /**
  138. * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
  139. * @dev: Device pointer
  140. * @entry: Entry point function for the cpus
  141. * @cpus: The cpumask of cpus that will use the entry point
  142. *
  143. * Set the Linux entry point for the SCM to transfer control to when coming
  144. * out of a power down. CPU power down may be executed on cpuidle or hotplug.
  145. */
  146. int __qcom_scm_set_warm_boot_addr(struct device *dev, void *entry,
  147. const cpumask_t *cpus)
  148. {
  149. return -ENOTSUPP;
  150. }
  151. /**
  152. * qcom_scm_cpu_power_down() - Power down the cpu
  153. * @flags - Flags to flush cache
  154. *
  155. * This is an end point to power down cpu. If there was a pending interrupt,
  156. * the control would return from this function, otherwise, the cpu jumps to the
  157. * warm boot entry point set for this cpu upon reset.
  158. */
  159. void __qcom_scm_cpu_power_down(u32 flags)
  160. {
  161. }
  162. int __qcom_scm_is_call_available(struct device *dev, u32 svc_id, u32 cmd_id)
  163. {
  164. int ret;
  165. struct qcom_scm_desc desc = {0};
  166. struct arm_smccc_res res;
  167. desc.arginfo = QCOM_SCM_ARGS(1);
  168. desc.args[0] = QCOM_SCM_FNID(svc_id, cmd_id) |
  169. (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
  170. ret = qcom_scm_call(dev, QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD,
  171. &desc, &res);
  172. return ret ? : res.a1;
  173. }
  174. int __qcom_scm_hdcp_req(struct device *dev, struct qcom_scm_hdcp_req *req,
  175. u32 req_cnt, u32 *resp)
  176. {
  177. int ret;
  178. struct qcom_scm_desc desc = {0};
  179. struct arm_smccc_res res;
  180. if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
  181. return -ERANGE;
  182. desc.args[0] = req[0].addr;
  183. desc.args[1] = req[0].val;
  184. desc.args[2] = req[1].addr;
  185. desc.args[3] = req[1].val;
  186. desc.args[4] = req[2].addr;
  187. desc.args[5] = req[2].val;
  188. desc.args[6] = req[3].addr;
  189. desc.args[7] = req[3].val;
  190. desc.args[8] = req[4].addr;
  191. desc.args[9] = req[4].val;
  192. desc.arginfo = QCOM_SCM_ARGS(10);
  193. ret = qcom_scm_call(dev, QCOM_SCM_SVC_HDCP, QCOM_SCM_CMD_HDCP, &desc,
  194. &res);
  195. *resp = res.a1;
  196. return ret;
  197. }
  198. void __qcom_scm_init(void)
  199. {
  200. u64 cmd;
  201. struct arm_smccc_res res;
  202. u32 function = QCOM_SCM_FNID(QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD);
  203. /* First try a SMC64 call */
  204. cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
  205. ARM_SMCCC_OWNER_SIP, function);
  206. arm_smccc_smc(cmd, QCOM_SCM_ARGS(1), cmd & (~BIT(ARM_SMCCC_TYPE_SHIFT)),
  207. 0, 0, 0, 0, 0, &res);
  208. if (!res.a0 && res.a1)
  209. qcom_smccc_convention = ARM_SMCCC_SMC_64;
  210. else
  211. qcom_smccc_convention = ARM_SMCCC_SMC_32;
  212. }