shmem.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * For transport using shared mem structure.
  4. *
  5. * Copyright (C) 2019-2024 ARM Ltd.
  6. */
  7. #include <linux/ktime.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/processor.h>
  12. #include <linux/types.h>
  13. #include <linux/bug.h>
  14. #include "common.h"
  15. #define SCMI_SHMEM_LAYOUT_OVERHEAD 24
  16. /*
  17. * SCMI specification requires all parameters, message headers, return
  18. * arguments or any protocol data to be expressed in little endian
  19. * format only.
  20. */
  21. struct scmi_shared_mem {
  22. __le32 reserved;
  23. __le32 channel_status;
  24. #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1)
  25. #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0)
  26. __le32 reserved1[2];
  27. __le32 flags;
  28. #define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0)
  29. __le32 length;
  30. __le32 msg_header;
  31. u8 msg_payload[];
  32. };
  33. static inline void shmem_memcpy_fromio32(void *to,
  34. const void __iomem *from,
  35. size_t count)
  36. {
  37. WARN_ON(!IS_ALIGNED((unsigned long)from, 4) ||
  38. !IS_ALIGNED((unsigned long)to, 4) ||
  39. count % 4);
  40. __ioread32_copy(to, from, count / 4);
  41. }
  42. static inline void shmem_memcpy_toio32(void __iomem *to,
  43. const void *from,
  44. size_t count)
  45. {
  46. WARN_ON(!IS_ALIGNED((unsigned long)to, 4) ||
  47. !IS_ALIGNED((unsigned long)from, 4) ||
  48. count % 4);
  49. __iowrite32_copy(to, from, count / 4);
  50. }
  51. static struct scmi_shmem_io_ops shmem_io_ops32 = {
  52. .fromio = shmem_memcpy_fromio32,
  53. .toio = shmem_memcpy_toio32,
  54. };
  55. /* Wrappers are needed for proper memcpy_{from,to}_io expansion by the
  56. * pre-processor.
  57. */
  58. static inline void shmem_memcpy_fromio(void *to,
  59. const void __iomem *from,
  60. size_t count)
  61. {
  62. memcpy_fromio(to, from, count);
  63. }
  64. static inline void shmem_memcpy_toio(void __iomem *to,
  65. const void *from,
  66. size_t count)
  67. {
  68. memcpy_toio(to, from, count);
  69. }
  70. static struct scmi_shmem_io_ops shmem_io_ops_default = {
  71. .fromio = shmem_memcpy_fromio,
  72. .toio = shmem_memcpy_toio,
  73. };
  74. static void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem,
  75. struct scmi_xfer *xfer,
  76. struct scmi_chan_info *cinfo,
  77. shmem_copy_toio_t copy_toio)
  78. {
  79. ktime_t stop;
  80. /*
  81. * Ideally channel must be free by now unless OS timeout last
  82. * request and platform continued to process the same, wait
  83. * until it releases the shared memory, otherwise we may endup
  84. * overwriting its response with new message payload or vice-versa.
  85. * Giving up anyway after twice the expected channel timeout so as
  86. * not to bail-out on intermittent issues where the platform is
  87. * occasionally a bit slower to answer.
  88. *
  89. * Note that after a timeout is detected we bail-out and carry on but
  90. * the transport functionality is probably permanently compromised:
  91. * this is just to ease debugging and avoid complete hangs on boot
  92. * due to a misbehaving SCMI firmware.
  93. */
  94. stop = ktime_add_ms(ktime_get(), 2 * cinfo->rx_timeout_ms);
  95. spin_until_cond((ioread32(&shmem->channel_status) &
  96. SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE) ||
  97. ktime_after(ktime_get(), stop));
  98. if (!(ioread32(&shmem->channel_status) &
  99. SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
  100. WARN_ON_ONCE(1);
  101. dev_err(cinfo->dev,
  102. "Timeout waiting for a free TX channel !\n");
  103. return;
  104. }
  105. /* Mark channel busy + clear error */
  106. iowrite32(0x0, &shmem->channel_status);
  107. iowrite32(xfer->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
  108. &shmem->flags);
  109. iowrite32(sizeof(shmem->msg_header) + xfer->tx.len, &shmem->length);
  110. iowrite32(pack_scmi_header(&xfer->hdr), &shmem->msg_header);
  111. if (xfer->tx.buf)
  112. copy_toio(shmem->msg_payload, xfer->tx.buf, xfer->tx.len);
  113. }
  114. static u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem)
  115. {
  116. return ioread32(&shmem->msg_header);
  117. }
  118. static void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem,
  119. struct scmi_xfer *xfer,
  120. shmem_copy_fromio_t copy_fromio)
  121. {
  122. size_t len = ioread32(&shmem->length);
  123. xfer->hdr.status = ioread32(shmem->msg_payload);
  124. /* Skip the length of header and status in shmem area i.e 8 bytes */
  125. xfer->rx.len = min_t(size_t, xfer->rx.len, len > 8 ? len - 8 : 0);
  126. /* Take a copy to the rx buffer.. */
  127. copy_fromio(xfer->rx.buf, shmem->msg_payload + 4, xfer->rx.len);
  128. }
  129. static void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem,
  130. size_t max_len, struct scmi_xfer *xfer,
  131. shmem_copy_fromio_t copy_fromio)
  132. {
  133. size_t len = ioread32(&shmem->length);
  134. /* Skip only the length of header in shmem area i.e 4 bytes */
  135. xfer->rx.len = min_t(size_t, max_len, len > 4 ? len - 4 : 0);
  136. /* Take a copy to the rx buffer.. */
  137. copy_fromio(xfer->rx.buf, shmem->msg_payload, xfer->rx.len);
  138. }
  139. static void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem)
  140. {
  141. iowrite32(SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE, &shmem->channel_status);
  142. }
  143. static bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem,
  144. struct scmi_xfer *xfer)
  145. {
  146. u16 xfer_id;
  147. xfer_id = MSG_XTRACT_TOKEN(ioread32(&shmem->msg_header));
  148. if (xfer->hdr.seq != xfer_id)
  149. return false;
  150. return ioread32(&shmem->channel_status) &
  151. (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
  152. SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
  153. }
  154. static bool shmem_channel_free(struct scmi_shared_mem __iomem *shmem)
  155. {
  156. return (ioread32(&shmem->channel_status) &
  157. SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
  158. }
  159. static bool shmem_channel_intr_enabled(struct scmi_shared_mem __iomem *shmem)
  160. {
  161. return ioread32(&shmem->flags) & SCMI_SHMEM_FLAG_INTR_ENABLED;
  162. }
  163. static void __iomem *shmem_setup_iomap(struct scmi_chan_info *cinfo,
  164. struct device *dev, bool tx,
  165. struct resource *res,
  166. struct scmi_shmem_io_ops **ops)
  167. {
  168. const char *desc = tx ? "Tx" : "Rx";
  169. int ret, idx = tx ? 0 : 1;
  170. struct device *cdev = cinfo->dev;
  171. struct resource lres = {};
  172. resource_size_t size;
  173. void __iomem *addr;
  174. u32 reg_io_width;
  175. struct device_node *shmem __free(device_node) = of_parse_phandle(cdev->of_node,
  176. "shmem", idx);
  177. if (!shmem)
  178. return IOMEM_ERR_PTR(-ENODEV);
  179. if (!of_device_is_compatible(shmem, "arm,scmi-shmem"))
  180. return IOMEM_ERR_PTR(-ENXIO);
  181. /* Use a local on-stack as a working area when not provided */
  182. if (!res)
  183. res = &lres;
  184. ret = of_address_to_resource(shmem, 0, res);
  185. if (ret) {
  186. dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
  187. return IOMEM_ERR_PTR(ret);
  188. }
  189. size = resource_size(res);
  190. if (cinfo->max_msg_size + SCMI_SHMEM_LAYOUT_OVERHEAD > size) {
  191. dev_err(dev, "misconfigured SCMI shared memory\n");
  192. return IOMEM_ERR_PTR(-ENOSPC);
  193. }
  194. addr = devm_ioremap(dev, res->start, size);
  195. if (!addr) {
  196. dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
  197. return IOMEM_ERR_PTR(-EADDRNOTAVAIL);
  198. }
  199. of_property_read_u32(shmem, "reg-io-width", &reg_io_width);
  200. switch (reg_io_width) {
  201. case 4:
  202. *ops = &shmem_io_ops32;
  203. break;
  204. default:
  205. *ops = &shmem_io_ops_default;
  206. break;
  207. }
  208. return addr;
  209. }
  210. static const struct scmi_shared_mem_operations scmi_shmem_ops = {
  211. .tx_prepare = shmem_tx_prepare,
  212. .read_header = shmem_read_header,
  213. .fetch_response = shmem_fetch_response,
  214. .fetch_notification = shmem_fetch_notification,
  215. .clear_channel = shmem_clear_channel,
  216. .poll_done = shmem_poll_done,
  217. .channel_free = shmem_channel_free,
  218. .channel_intr_enabled = shmem_channel_intr_enabled,
  219. .setup_iomap = shmem_setup_iomap,
  220. };
  221. const struct scmi_shared_mem_operations *scmi_shared_mem_operations_get(void)
  222. {
  223. return &scmi_shmem_ops;
  224. }