qcom_scm-legacy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2010,2015,2019 The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2015 Linaro Ltd.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <linux/errno.h>
  11. #include <linux/err.h>
  12. #include <linux/qcom_scm.h>
  13. #include <linux/arm-smccc.h>
  14. #include <linux/dma-mapping.h>
  15. #include "qcom_scm.h"
  16. /**
  17. * struct scm_legacy_command - one SCM command buffer
  18. * @len: total available memory for command and response
  19. * @buf_offset: start of command buffer
  20. * @resp_hdr_offset: start of response buffer
  21. * @id: command to be executed
  22. * @buf: buffer returned from scm_legacy_get_command_buffer()
  23. *
  24. * An SCM command is laid out in memory as follows:
  25. *
  26. * ------------------- <--- struct scm_legacy_command
  27. * | command header |
  28. * ------------------- <--- scm_legacy_get_command_buffer()
  29. * | command buffer |
  30. * ------------------- <--- struct scm_legacy_response and
  31. * | response header | scm_legacy_command_to_response()
  32. * ------------------- <--- scm_legacy_get_response_buffer()
  33. * | response buffer |
  34. * -------------------
  35. *
  36. * There can be arbitrary padding between the headers and buffers so
  37. * you should always use the appropriate scm_legacy_get_*_buffer() routines
  38. * to access the buffers in a safe manner.
  39. */
  40. struct scm_legacy_command {
  41. __le32 len;
  42. __le32 buf_offset;
  43. __le32 resp_hdr_offset;
  44. __le32 id;
  45. __le32 buf[];
  46. };
  47. /**
  48. * struct scm_legacy_response - one SCM response buffer
  49. * @len: total available memory for response
  50. * @buf_offset: start of response data relative to start of scm_legacy_response
  51. * @is_complete: indicates if the command has finished processing
  52. */
  53. struct scm_legacy_response {
  54. __le32 len;
  55. __le32 buf_offset;
  56. __le32 is_complete;
  57. };
  58. /**
  59. * scm_legacy_command_to_response() - Get a pointer to a scm_legacy_response
  60. * @cmd: command
  61. *
  62. * Returns a pointer to a response for a command.
  63. */
  64. static inline struct scm_legacy_response *scm_legacy_command_to_response(
  65. const struct scm_legacy_command *cmd)
  66. {
  67. return (void *)cmd + le32_to_cpu(cmd->resp_hdr_offset);
  68. }
  69. /**
  70. * scm_legacy_get_command_buffer() - Get a pointer to a command buffer
  71. * @cmd: command
  72. *
  73. * Returns a pointer to the command buffer of a command.
  74. */
  75. static inline void *scm_legacy_get_command_buffer(
  76. const struct scm_legacy_command *cmd)
  77. {
  78. return (void *)cmd->buf;
  79. }
  80. /**
  81. * scm_legacy_get_response_buffer() - Get a pointer to a response buffer
  82. * @rsp: response
  83. *
  84. * Returns a pointer to a response buffer of a response.
  85. */
  86. static inline void *scm_legacy_get_response_buffer(
  87. const struct scm_legacy_response *rsp)
  88. {
  89. return (void *)rsp + le32_to_cpu(rsp->buf_offset);
  90. }
  91. static void __scm_legacy_do(const struct arm_smccc_args *smc,
  92. struct arm_smccc_res *res)
  93. {
  94. do {
  95. arm_smccc_smc(smc->args[0], smc->args[1], smc->args[2],
  96. smc->args[3], smc->args[4], smc->args[5],
  97. smc->args[6], smc->args[7], res);
  98. } while (res->a0 == QCOM_SCM_INTERRUPTED);
  99. }
  100. /**
  101. * scm_legacy_call() - Sends a command to the SCM and waits for the command to
  102. * finish processing.
  103. * @dev: device
  104. * @desc: descriptor structure containing arguments and return values
  105. * @res: results from SMC call
  106. *
  107. * A note on cache maintenance:
  108. * Note that any buffers that are expected to be accessed by the secure world
  109. * must be flushed before invoking qcom_scm_call and invalidated in the cache
  110. * immediately after qcom_scm_call returns. Cache maintenance on the command
  111. * and response buffers is taken care of by qcom_scm_call; however, callers are
  112. * responsible for any other cached buffers passed over to the secure world.
  113. */
  114. int scm_legacy_call(struct device *dev, const struct qcom_scm_desc *desc,
  115. struct qcom_scm_res *res)
  116. {
  117. u8 arglen = desc->arginfo & 0xf;
  118. int ret = 0, context_id;
  119. unsigned int i;
  120. struct scm_legacy_command *cmd;
  121. struct scm_legacy_response *rsp;
  122. struct arm_smccc_args smc = {0};
  123. struct arm_smccc_res smc_res;
  124. const size_t cmd_len = arglen * sizeof(__le32);
  125. const size_t resp_len = MAX_QCOM_SCM_RETS * sizeof(__le32);
  126. size_t alloc_len = sizeof(*cmd) + cmd_len + sizeof(*rsp) + resp_len;
  127. dma_addr_t cmd_phys;
  128. __le32 *arg_buf;
  129. const __le32 *res_buf;
  130. cmd = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL);
  131. if (!cmd)
  132. return -ENOMEM;
  133. cmd->len = cpu_to_le32(alloc_len);
  134. cmd->buf_offset = cpu_to_le32(sizeof(*cmd));
  135. cmd->resp_hdr_offset = cpu_to_le32(sizeof(*cmd) + cmd_len);
  136. cmd->id = cpu_to_le32(SCM_LEGACY_FNID(desc->svc, desc->cmd));
  137. arg_buf = scm_legacy_get_command_buffer(cmd);
  138. for (i = 0; i < arglen; i++)
  139. arg_buf[i] = cpu_to_le32(desc->args[i]);
  140. rsp = scm_legacy_command_to_response(cmd);
  141. cmd_phys = dma_map_single(dev, cmd, alloc_len, DMA_TO_DEVICE);
  142. if (dma_mapping_error(dev, cmd_phys)) {
  143. kfree(cmd);
  144. return -ENOMEM;
  145. }
  146. smc.args[0] = 1;
  147. smc.args[1] = (unsigned long)&context_id;
  148. smc.args[2] = cmd_phys;
  149. down(&qcom_scm_sem_lock);
  150. __scm_legacy_do(&smc, &smc_res);
  151. if (smc_res.a0)
  152. ret = qcom_scm_remap_error(smc_res.a0);
  153. up(&qcom_scm_sem_lock);
  154. if (ret)
  155. goto out;
  156. do {
  157. dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len,
  158. sizeof(*rsp), DMA_FROM_DEVICE);
  159. } while (!rsp->is_complete);
  160. dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len +
  161. le32_to_cpu(rsp->buf_offset),
  162. resp_len, DMA_FROM_DEVICE);
  163. if (res) {
  164. res_buf = scm_legacy_get_response_buffer(rsp);
  165. for (i = 0; i < MAX_QCOM_SCM_RETS; i++)
  166. res->result[i] = le32_to_cpu(res_buf[i]);
  167. }
  168. out:
  169. dma_unmap_single(dev, cmd_phys, alloc_len, DMA_TO_DEVICE);
  170. kfree(cmd);
  171. return ret;
  172. }
  173. #define SCM_LEGACY_ATOMIC_N_REG_ARGS 5
  174. #define SCM_LEGACY_ATOMIC_FIRST_REG_IDX 2
  175. #define SCM_LEGACY_CLASS_REGISTER (0x2 << 8)
  176. #define SCM_LEGACY_MASK_IRQS BIT(5)
  177. #define SCM_LEGACY_ATOMIC_ID(svc, cmd, n) \
  178. ((SCM_LEGACY_FNID(svc, cmd) << 12) | \
  179. SCM_LEGACY_CLASS_REGISTER | \
  180. SCM_LEGACY_MASK_IRQS | \
  181. (n & 0xf))
  182. /**
  183. * scm_legacy_call_atomic() - Send an atomic SCM command with up to 5 arguments
  184. * and 3 return values
  185. * @unused: device, legacy argument, not used, can be NULL
  186. * @desc: SCM call descriptor containing arguments
  187. * @res: SCM call return values
  188. *
  189. * This shall only be used with commands that are guaranteed to be
  190. * uninterruptable, atomic and SMP safe.
  191. */
  192. int scm_legacy_call_atomic(struct device *unused,
  193. const struct qcom_scm_desc *desc,
  194. struct qcom_scm_res *res)
  195. {
  196. int context_id;
  197. struct arm_smccc_res smc_res;
  198. size_t arglen = desc->arginfo & 0xf;
  199. BUG_ON(arglen > SCM_LEGACY_ATOMIC_N_REG_ARGS);
  200. arm_smccc_smc(SCM_LEGACY_ATOMIC_ID(desc->svc, desc->cmd, arglen),
  201. (unsigned long)&context_id,
  202. desc->args[0], desc->args[1], desc->args[2],
  203. desc->args[3], desc->args[4], 0, &smc_res);
  204. if (res) {
  205. res->result[0] = smc_res.a1;
  206. res->result[1] = smc_res.a2;
  207. res->result[2] = smc_res.a3;
  208. }
  209. return smc_res.a0;
  210. }