qcom_scm-smc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015,2019 The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/io.h>
  5. #include <linux/errno.h>
  6. #include <linux/delay.h>
  7. #include <linux/mutex.h>
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include <linux/qcom_scm.h>
  11. #include <linux/arm-smccc.h>
  12. #include <linux/dma-mapping.h>
  13. #include "qcom_scm.h"
  14. /**
  15. * struct arm_smccc_args
  16. * @args: The array of values used in registers in smc instruction
  17. */
  18. struct arm_smccc_args {
  19. unsigned long args[8];
  20. };
  21. static DEFINE_MUTEX(qcom_scm_lock);
  22. #define QCOM_SCM_EBUSY_WAIT_MS 30
  23. #define QCOM_SCM_EBUSY_MAX_RETRY 20
  24. #define SCM_SMC_N_REG_ARGS 4
  25. #define SCM_SMC_FIRST_EXT_IDX (SCM_SMC_N_REG_ARGS - 1)
  26. #define SCM_SMC_N_EXT_ARGS (MAX_QCOM_SCM_ARGS - SCM_SMC_N_REG_ARGS + 1)
  27. #define SCM_SMC_FIRST_REG_IDX 2
  28. #define SCM_SMC_LAST_REG_IDX (SCM_SMC_FIRST_REG_IDX + SCM_SMC_N_REG_ARGS - 1)
  29. static void __scm_smc_do_quirk(const struct arm_smccc_args *smc,
  30. struct arm_smccc_res *res)
  31. {
  32. unsigned long a0 = smc->args[0];
  33. struct arm_smccc_quirk quirk = { .id = ARM_SMCCC_QUIRK_QCOM_A6 };
  34. quirk.state.a6 = 0;
  35. do {
  36. arm_smccc_smc_quirk(a0, smc->args[1], smc->args[2],
  37. smc->args[3], smc->args[4], smc->args[5],
  38. quirk.state.a6, smc->args[7], res, &quirk);
  39. if (res->a0 == QCOM_SCM_INTERRUPTED)
  40. a0 = res->a0;
  41. } while (res->a0 == QCOM_SCM_INTERRUPTED);
  42. }
  43. static void __scm_smc_do(const struct arm_smccc_args *smc,
  44. struct arm_smccc_res *res, bool atomic)
  45. {
  46. int retry_count = 0;
  47. if (atomic) {
  48. __scm_smc_do_quirk(smc, res);
  49. return;
  50. }
  51. do {
  52. mutex_lock(&qcom_scm_lock);
  53. __scm_smc_do_quirk(smc, res);
  54. mutex_unlock(&qcom_scm_lock);
  55. if (res->a0 == QCOM_SCM_V2_EBUSY) {
  56. if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
  57. break;
  58. msleep(QCOM_SCM_EBUSY_WAIT_MS);
  59. }
  60. } while (res->a0 == QCOM_SCM_V2_EBUSY);
  61. }
  62. int __scm_smc_call(struct device *dev, const struct qcom_scm_desc *desc,
  63. enum qcom_scm_convention qcom_convention,
  64. struct qcom_scm_res *res, bool atomic)
  65. {
  66. int arglen = desc->arginfo & 0xf;
  67. int i;
  68. dma_addr_t args_phys = 0;
  69. void *args_virt = NULL;
  70. size_t alloc_len;
  71. gfp_t flag = atomic ? GFP_ATOMIC : GFP_KERNEL;
  72. u32 smccc_call_type = atomic ? ARM_SMCCC_FAST_CALL : ARM_SMCCC_STD_CALL;
  73. u32 qcom_smccc_convention = (qcom_convention == SMC_CONVENTION_ARM_32) ?
  74. ARM_SMCCC_SMC_32 : ARM_SMCCC_SMC_64;
  75. struct arm_smccc_res smc_res;
  76. struct arm_smccc_args smc = {0};
  77. smc.args[0] = ARM_SMCCC_CALL_VAL(
  78. smccc_call_type,
  79. qcom_smccc_convention,
  80. desc->owner,
  81. SCM_SMC_FNID(desc->svc, desc->cmd));
  82. smc.args[1] = desc->arginfo;
  83. for (i = 0; i < SCM_SMC_N_REG_ARGS; i++)
  84. smc.args[i + SCM_SMC_FIRST_REG_IDX] = desc->args[i];
  85. if (unlikely(arglen > SCM_SMC_N_REG_ARGS)) {
  86. alloc_len = SCM_SMC_N_EXT_ARGS * sizeof(u64);
  87. args_virt = kzalloc(PAGE_ALIGN(alloc_len), flag);
  88. if (!args_virt)
  89. return -ENOMEM;
  90. if (qcom_smccc_convention == ARM_SMCCC_SMC_32) {
  91. __le32 *args = args_virt;
  92. for (i = 0; i < SCM_SMC_N_EXT_ARGS; i++)
  93. args[i] = cpu_to_le32(desc->args[i +
  94. SCM_SMC_FIRST_EXT_IDX]);
  95. } else {
  96. __le64 *args = args_virt;
  97. for (i = 0; i < SCM_SMC_N_EXT_ARGS; i++)
  98. args[i] = cpu_to_le64(desc->args[i +
  99. SCM_SMC_FIRST_EXT_IDX]);
  100. }
  101. args_phys = dma_map_single(dev, args_virt, alloc_len,
  102. DMA_TO_DEVICE);
  103. if (dma_mapping_error(dev, args_phys)) {
  104. kfree(args_virt);
  105. return -ENOMEM;
  106. }
  107. smc.args[SCM_SMC_LAST_REG_IDX] = args_phys;
  108. }
  109. __scm_smc_do(&smc, &smc_res, atomic);
  110. if (args_virt) {
  111. dma_unmap_single(dev, args_phys, alloc_len, DMA_TO_DEVICE);
  112. kfree(args_virt);
  113. }
  114. if (res) {
  115. res->result[0] = smc_res.a1;
  116. res->result[1] = smc_res.a2;
  117. res->result[2] = smc_res.a3;
  118. }
  119. return (long)smc_res.a0 ? qcom_scm_remap_error(smc_res.a0) : 0;
  120. }