agent-smc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. */
  10. #include <rthw.h>
  11. #include <mmu.h>
  12. #include <smccc.h>
  13. #include <cpuport.h>
  14. #include <dt-bindings/size.h>
  15. #define DBG_TAG "scmi.agent.smc"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include "agent.h"
  19. struct scmi_agent_smc
  20. {
  21. int irq;
  22. rt_uint32_t func_id;
  23. #define SHMEM_SIZE (4 * SIZE_KB)
  24. #define SHMEM_SHIFT 12
  25. #define SHMEM_PAGE(x) (((rt_ubase_t)(x) >> SHMEM_SHIFT))
  26. #define SHMEM_OFFSET(x) ((x) & (SHMEM_SIZE - 1))
  27. rt_uint32_t param_page;
  28. rt_uint32_t param_offset;
  29. rt_ubase_t cap_id;
  30. rt_bool_t done;
  31. struct rt_spinlock lock;
  32. struct scmi_shared_mem *shmem;
  33. };
  34. static void scmi_agent_smc_isr(int irqno, void *param)
  35. {
  36. struct scmi_agent_smc *asmc = param;
  37. HWREG32(&asmc->done) = RT_TRUE;
  38. rt_hw_dmb();
  39. }
  40. static rt_err_t scmi_agent_smc_setup(struct scmi_agent *agent,
  41. struct rt_device *dev)
  42. {
  43. rt_err_t err;
  44. rt_uint64_t shm_addr, shm_size;
  45. struct rt_ofw_node *np = dev->ofw_node, *shmem_np;
  46. struct scmi_agent_smc *asmc = rt_calloc(1, sizeof(*asmc));
  47. if (!asmc)
  48. {
  49. return -RT_ENOMEM;
  50. }
  51. if ((err = rt_ofw_prop_read_u32(np, "arm,smc-id", &asmc->func_id)))
  52. {
  53. goto _fail;
  54. }
  55. shmem_np = rt_ofw_parse_phandle(np, "shmem", 0);
  56. if (!rt_ofw_node_is_compatible(shmem_np, "arm,scmi-shmem"))
  57. {
  58. err = -RT_EINVAL;
  59. rt_ofw_node_put(shmem_np);
  60. goto _fail;
  61. }
  62. if ((err = rt_ofw_get_address(shmem_np, 0, &shm_addr, &shm_size)))
  63. {
  64. rt_ofw_node_put(shmem_np);
  65. goto _fail;
  66. }
  67. rt_ofw_node_put(shmem_np);
  68. asmc->shmem = rt_ioremap((void *)shm_addr, shm_size);
  69. if (!asmc->shmem)
  70. {
  71. err = -RT_EIO;
  72. goto _fail;
  73. }
  74. if (rt_ofw_node_is_compatible(np, "qcom,scmi-smc"))
  75. {
  76. void *cap_id_map = (void *)asmc->shmem + (shm_size - 8);
  77. rt_memcpy(&asmc->cap_id, cap_id_map, sizeof(asmc->cap_id));
  78. }
  79. else
  80. {
  81. asmc->cap_id = ~0UL;
  82. }
  83. if (rt_ofw_node_is_compatible(np, "arm,scmi-smc-param"))
  84. {
  85. rt_ubase_t base = (rt_ubase_t)rt_kmem_v2p(asmc->shmem);
  86. asmc->param_page = SHMEM_PAGE(base);
  87. asmc->param_offset = SHMEM_OFFSET(base);
  88. }
  89. asmc->irq = rt_ofw_get_irq_by_name(np, "a2p");
  90. if (asmc->irq >= 0)
  91. {
  92. rt_hw_interrupt_install(asmc->irq, scmi_agent_smc_isr, asmc, "scmi");
  93. rt_hw_interrupt_umask(asmc->irq);
  94. }
  95. rt_spin_lock_init(&asmc->lock);
  96. agent->priv = asmc;
  97. return RT_EOK;
  98. _fail:
  99. if (asmc->shmem)
  100. {
  101. rt_iounmap(asmc->shmem);
  102. }
  103. rt_free(asmc);
  104. return err;
  105. }
  106. static rt_err_t scmi_agent_smc_process_msg(struct scmi_agent *agent,
  107. struct rt_scmi_msg *msg)
  108. {
  109. rt_err_t err;
  110. struct arm_smccc_res_t res;
  111. struct scmi_shared_mem *shmem;
  112. struct scmi_agent_smc *asmc = agent->priv;
  113. rt_spin_lock(&asmc->lock);
  114. if (asmc->irq >= 0)
  115. {
  116. while (HWREG32(&asmc->done))
  117. {
  118. rt_hw_cpu_relax();
  119. }
  120. }
  121. shmem = asmc->shmem;
  122. if ((err = scmi_shmem_msg_write(shmem, msg)))
  123. {
  124. goto _out_lock;
  125. }
  126. if (asmc->irq >= 0)
  127. {
  128. HWREG32(&asmc->done) = RT_FALSE;
  129. }
  130. if (asmc->cap_id == ~0UL)
  131. {
  132. arm_smccc_smc(asmc->func_id, asmc->param_page, asmc->param_offset,
  133. 0, 0, 0, 0, 0, &res, RT_NULL);
  134. }
  135. else
  136. {
  137. arm_smccc_smc(asmc->func_id, asmc->cap_id,
  138. 0, 0, 0, 0, 0, 0, &res, RT_NULL);
  139. }
  140. if (res.a0)
  141. {
  142. err = -RT_EIO;
  143. }
  144. else
  145. {
  146. err = scmi_shmem_msg_read(shmem, msg);
  147. }
  148. scmi_shmem_clear_channel(shmem);
  149. _out_lock:
  150. rt_spin_unlock(&asmc->lock);
  151. return err;
  152. }
  153. struct scmi_agent_ops scmi_agent_smc_ops =
  154. {
  155. .name = "smc",
  156. .setup = scmi_agent_smc_setup,
  157. .process_msg = scmi_agent_smc_process_msg,
  158. };