smc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Message SMC/HVC
  4. * Transport driver
  5. *
  6. * Copyright 2020 NXP
  7. */
  8. #include <linux/arm-smccc.h>
  9. #include <linux/atomic.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/mutex.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/processor.h>
  18. #include <linux/slab.h>
  19. #include "common.h"
  20. /**
  21. * struct scmi_smc - Structure representing a SCMI smc transport
  22. *
  23. * @irq: An optional IRQ for completion
  24. * @cinfo: SCMI channel info
  25. * @shmem: Transmit/Receive shared memory area
  26. * @shmem_lock: Lock to protect access to Tx/Rx shared memory area.
  27. * Used when NOT operating in atomic mode.
  28. * @inflight: Atomic flag to protect access to Tx/Rx shared memory area.
  29. * Used when operating in atomic mode.
  30. * @func_id: smc/hvc call function id
  31. */
  32. struct scmi_smc {
  33. int irq;
  34. struct scmi_chan_info *cinfo;
  35. struct scmi_shared_mem __iomem *shmem;
  36. /* Protect access to shmem area */
  37. struct mutex shmem_lock;
  38. #define INFLIGHT_NONE MSG_TOKEN_MAX
  39. atomic_t inflight;
  40. u32 func_id;
  41. };
  42. static irqreturn_t smc_msg_done_isr(int irq, void *data)
  43. {
  44. struct scmi_smc *scmi_info = data;
  45. scmi_rx_callback(scmi_info->cinfo,
  46. shmem_read_header(scmi_info->shmem), NULL);
  47. return IRQ_HANDLED;
  48. }
  49. static bool smc_chan_available(struct device *dev, int idx)
  50. {
  51. struct device_node *np = of_parse_phandle(dev->of_node, "shmem", 0);
  52. if (!np)
  53. return false;
  54. of_node_put(np);
  55. return true;
  56. }
  57. static inline void smc_channel_lock_init(struct scmi_smc *scmi_info)
  58. {
  59. if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE))
  60. atomic_set(&scmi_info->inflight, INFLIGHT_NONE);
  61. else
  62. mutex_init(&scmi_info->shmem_lock);
  63. }
  64. static bool smc_xfer_inflight(struct scmi_xfer *xfer, atomic_t *inflight)
  65. {
  66. int ret;
  67. ret = atomic_cmpxchg(inflight, INFLIGHT_NONE, xfer->hdr.seq);
  68. return ret == INFLIGHT_NONE;
  69. }
  70. static inline void
  71. smc_channel_lock_acquire(struct scmi_smc *scmi_info,
  72. struct scmi_xfer *xfer __maybe_unused)
  73. {
  74. if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE))
  75. spin_until_cond(smc_xfer_inflight(xfer, &scmi_info->inflight));
  76. else
  77. mutex_lock(&scmi_info->shmem_lock);
  78. }
  79. static inline void smc_channel_lock_release(struct scmi_smc *scmi_info)
  80. {
  81. if (IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE))
  82. atomic_set(&scmi_info->inflight, INFLIGHT_NONE);
  83. else
  84. mutex_unlock(&scmi_info->shmem_lock);
  85. }
  86. static int smc_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
  87. bool tx)
  88. {
  89. struct device *cdev = cinfo->dev;
  90. struct scmi_smc *scmi_info;
  91. resource_size_t size;
  92. struct resource res;
  93. struct device_node *np;
  94. u32 func_id;
  95. int ret;
  96. if (!tx)
  97. return -ENODEV;
  98. scmi_info = devm_kzalloc(dev, sizeof(*scmi_info), GFP_KERNEL);
  99. if (!scmi_info)
  100. return -ENOMEM;
  101. np = of_parse_phandle(cdev->of_node, "shmem", 0);
  102. if (!of_device_is_compatible(np, "arm,scmi-shmem")) {
  103. of_node_put(np);
  104. return -ENXIO;
  105. }
  106. ret = of_address_to_resource(np, 0, &res);
  107. of_node_put(np);
  108. if (ret) {
  109. dev_err(cdev, "failed to get SCMI Tx shared memory\n");
  110. return ret;
  111. }
  112. size = resource_size(&res);
  113. scmi_info->shmem = devm_ioremap(dev, res.start, size);
  114. if (!scmi_info->shmem) {
  115. dev_err(dev, "failed to ioremap SCMI Tx shared memory\n");
  116. return -EADDRNOTAVAIL;
  117. }
  118. ret = of_property_read_u32(dev->of_node, "arm,smc-id", &func_id);
  119. if (ret < 0)
  120. return ret;
  121. /*
  122. * If there is an interrupt named "a2p", then the service and
  123. * completion of a message is signaled by an interrupt rather than by
  124. * the return of the SMC call.
  125. */
  126. scmi_info->irq = of_irq_get_byname(cdev->of_node, "a2p");
  127. if (scmi_info->irq > 0) {
  128. ret = request_irq(scmi_info->irq, smc_msg_done_isr,
  129. IRQF_NO_SUSPEND, dev_name(dev), scmi_info);
  130. if (ret) {
  131. dev_err(dev, "failed to setup SCMI smc irq\n");
  132. return ret;
  133. }
  134. } else {
  135. cinfo->no_completion_irq = true;
  136. }
  137. scmi_info->func_id = func_id;
  138. scmi_info->cinfo = cinfo;
  139. smc_channel_lock_init(scmi_info);
  140. cinfo->transport_info = scmi_info;
  141. return 0;
  142. }
  143. static int smc_chan_free(int id, void *p, void *data)
  144. {
  145. struct scmi_chan_info *cinfo = p;
  146. struct scmi_smc *scmi_info = cinfo->transport_info;
  147. /* Ignore any possible further reception on the IRQ path */
  148. if (scmi_info->irq > 0)
  149. free_irq(scmi_info->irq, scmi_info);
  150. cinfo->transport_info = NULL;
  151. scmi_info->cinfo = NULL;
  152. scmi_free_channel(cinfo, data, id);
  153. return 0;
  154. }
  155. static int smc_send_message(struct scmi_chan_info *cinfo,
  156. struct scmi_xfer *xfer)
  157. {
  158. struct scmi_smc *scmi_info = cinfo->transport_info;
  159. struct arm_smccc_res res;
  160. /*
  161. * Channel will be released only once response has been
  162. * surely fully retrieved, so after .mark_txdone()
  163. */
  164. smc_channel_lock_acquire(scmi_info, xfer);
  165. shmem_tx_prepare(scmi_info->shmem, xfer, cinfo);
  166. arm_smccc_1_1_invoke(scmi_info->func_id, 0, 0, 0, 0, 0, 0, 0, &res);
  167. /* Only SMCCC_RET_NOT_SUPPORTED is valid error code */
  168. if (res.a0) {
  169. smc_channel_lock_release(scmi_info);
  170. return -EOPNOTSUPP;
  171. }
  172. return 0;
  173. }
  174. static void smc_fetch_response(struct scmi_chan_info *cinfo,
  175. struct scmi_xfer *xfer)
  176. {
  177. struct scmi_smc *scmi_info = cinfo->transport_info;
  178. shmem_fetch_response(scmi_info->shmem, xfer);
  179. }
  180. static void smc_mark_txdone(struct scmi_chan_info *cinfo, int ret,
  181. struct scmi_xfer *__unused)
  182. {
  183. struct scmi_smc *scmi_info = cinfo->transport_info;
  184. smc_channel_lock_release(scmi_info);
  185. }
  186. static const struct scmi_transport_ops scmi_smc_ops = {
  187. .chan_available = smc_chan_available,
  188. .chan_setup = smc_chan_setup,
  189. .chan_free = smc_chan_free,
  190. .send_message = smc_send_message,
  191. .mark_txdone = smc_mark_txdone,
  192. .fetch_response = smc_fetch_response,
  193. };
  194. const struct scmi_desc scmi_smc_desc = {
  195. .ops = &scmi_smc_ops,
  196. .max_rx_timeout_ms = 30,
  197. .max_msg = 20,
  198. .max_msg_size = 128,
  199. /*
  200. * Setting .sync_cmds_atomic_replies to true for SMC assumes that,
  201. * once the SMC instruction has completed successfully, the issued
  202. * SCMI command would have been already fully processed by the SCMI
  203. * platform firmware and so any possible response value expected
  204. * for the issued command will be immmediately ready to be fetched
  205. * from the shared memory area.
  206. */
  207. .sync_cmds_completed_on_ret = true,
  208. .atomic_enabled = IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_SMC_ATOMIC_ENABLE),
  209. };