mailbox.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Message Mailbox Transport
  4. * driver.
  5. *
  6. * Copyright (C) 2019 ARM Ltd.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/device.h>
  10. #include <linux/mailbox_client.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/slab.h>
  14. #include "common.h"
  15. /**
  16. * struct scmi_mailbox - Structure representing a SCMI mailbox transport
  17. *
  18. * @cl: Mailbox Client
  19. * @chan: Transmit/Receive mailbox channel
  20. * @cinfo: SCMI channel info
  21. * @shmem: Transmit/Receive shared memory area
  22. */
  23. struct scmi_mailbox {
  24. struct mbox_client cl;
  25. struct mbox_chan *chan;
  26. struct scmi_chan_info *cinfo;
  27. struct scmi_shared_mem __iomem *shmem;
  28. };
  29. #define client_to_scmi_mailbox(c) container_of(c, struct scmi_mailbox, cl)
  30. static void tx_prepare(struct mbox_client *cl, void *m)
  31. {
  32. struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
  33. shmem_tx_prepare(smbox->shmem, m, smbox->cinfo);
  34. }
  35. static void rx_callback(struct mbox_client *cl, void *m)
  36. {
  37. struct scmi_mailbox *smbox = client_to_scmi_mailbox(cl);
  38. scmi_rx_callback(smbox->cinfo, shmem_read_header(smbox->shmem), NULL);
  39. }
  40. static bool mailbox_chan_available(struct device *dev, int idx)
  41. {
  42. return !of_parse_phandle_with_args(dev->of_node, "mboxes",
  43. "#mbox-cells", idx, NULL);
  44. }
  45. static int mailbox_chan_validate(struct device *cdev)
  46. {
  47. int num_mb, num_sh, ret = 0;
  48. struct device_node *np = cdev->of_node;
  49. num_mb = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
  50. num_sh = of_count_phandle_with_args(np, "shmem", NULL);
  51. /* Bail out if mboxes and shmem descriptors are inconsistent */
  52. if (num_mb <= 0 || num_sh > 2 || num_mb != num_sh) {
  53. dev_warn(cdev, "Invalid channel descriptor for '%s'\n",
  54. of_node_full_name(np));
  55. return -EINVAL;
  56. }
  57. if (num_sh > 1) {
  58. struct device_node *np_tx, *np_rx;
  59. np_tx = of_parse_phandle(np, "shmem", 0);
  60. np_rx = of_parse_phandle(np, "shmem", 1);
  61. /* SCMI Tx and Rx shared mem areas have to be distinct */
  62. if (!np_tx || !np_rx || np_tx == np_rx) {
  63. dev_warn(cdev, "Invalid shmem descriptor for '%s'\n",
  64. of_node_full_name(np));
  65. ret = -EINVAL;
  66. }
  67. of_node_put(np_tx);
  68. of_node_put(np_rx);
  69. }
  70. return ret;
  71. }
  72. static int mailbox_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
  73. bool tx)
  74. {
  75. const char *desc = tx ? "Tx" : "Rx";
  76. struct device *cdev = cinfo->dev;
  77. struct scmi_mailbox *smbox;
  78. struct device_node *shmem;
  79. int ret, idx = tx ? 0 : 1;
  80. struct mbox_client *cl;
  81. resource_size_t size;
  82. struct resource res;
  83. ret = mailbox_chan_validate(cdev);
  84. if (ret)
  85. return ret;
  86. smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
  87. if (!smbox)
  88. return -ENOMEM;
  89. shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
  90. if (!of_device_is_compatible(shmem, "arm,scmi-shmem")) {
  91. of_node_put(shmem);
  92. return -ENXIO;
  93. }
  94. ret = of_address_to_resource(shmem, 0, &res);
  95. of_node_put(shmem);
  96. if (ret) {
  97. dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
  98. return ret;
  99. }
  100. size = resource_size(&res);
  101. smbox->shmem = devm_ioremap(dev, res.start, size);
  102. if (!smbox->shmem) {
  103. dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
  104. return -EADDRNOTAVAIL;
  105. }
  106. cl = &smbox->cl;
  107. cl->dev = cdev;
  108. cl->tx_prepare = tx ? tx_prepare : NULL;
  109. cl->rx_callback = rx_callback;
  110. cl->tx_block = false;
  111. cl->knows_txdone = tx;
  112. smbox->chan = mbox_request_channel(cl, tx ? 0 : 1);
  113. if (IS_ERR(smbox->chan)) {
  114. ret = PTR_ERR(smbox->chan);
  115. if (ret != -EPROBE_DEFER)
  116. dev_err(cdev, "failed to request SCMI %s mailbox\n",
  117. tx ? "Tx" : "Rx");
  118. return ret;
  119. }
  120. cinfo->transport_info = smbox;
  121. smbox->cinfo = cinfo;
  122. return 0;
  123. }
  124. static int mailbox_chan_free(int id, void *p, void *data)
  125. {
  126. struct scmi_chan_info *cinfo = p;
  127. struct scmi_mailbox *smbox = cinfo->transport_info;
  128. if (smbox && !IS_ERR(smbox->chan)) {
  129. mbox_free_channel(smbox->chan);
  130. cinfo->transport_info = NULL;
  131. smbox->chan = NULL;
  132. smbox->cinfo = NULL;
  133. }
  134. scmi_free_channel(cinfo, data, id);
  135. return 0;
  136. }
  137. static int mailbox_send_message(struct scmi_chan_info *cinfo,
  138. struct scmi_xfer *xfer)
  139. {
  140. struct scmi_mailbox *smbox = cinfo->transport_info;
  141. int ret;
  142. ret = mbox_send_message(smbox->chan, xfer);
  143. /* mbox_send_message returns non-negative value on success, so reset */
  144. if (ret > 0)
  145. ret = 0;
  146. return ret;
  147. }
  148. static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret,
  149. struct scmi_xfer *__unused)
  150. {
  151. struct scmi_mailbox *smbox = cinfo->transport_info;
  152. /*
  153. * NOTE: we might prefer not to need the mailbox ticker to manage the
  154. * transfer queueing since the protocol layer queues things by itself.
  155. * Unfortunately, we have to kick the mailbox framework after we have
  156. * received our message.
  157. */
  158. mbox_client_txdone(smbox->chan, ret);
  159. }
  160. static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
  161. struct scmi_xfer *xfer)
  162. {
  163. struct scmi_mailbox *smbox = cinfo->transport_info;
  164. shmem_fetch_response(smbox->shmem, xfer);
  165. }
  166. static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
  167. size_t max_len, struct scmi_xfer *xfer)
  168. {
  169. struct scmi_mailbox *smbox = cinfo->transport_info;
  170. shmem_fetch_notification(smbox->shmem, max_len, xfer);
  171. }
  172. static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
  173. {
  174. struct scmi_mailbox *smbox = cinfo->transport_info;
  175. shmem_clear_channel(smbox->shmem);
  176. }
  177. static bool
  178. mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
  179. {
  180. struct scmi_mailbox *smbox = cinfo->transport_info;
  181. return shmem_poll_done(smbox->shmem, xfer);
  182. }
  183. static const struct scmi_transport_ops scmi_mailbox_ops = {
  184. .chan_available = mailbox_chan_available,
  185. .chan_setup = mailbox_chan_setup,
  186. .chan_free = mailbox_chan_free,
  187. .send_message = mailbox_send_message,
  188. .mark_txdone = mailbox_mark_txdone,
  189. .fetch_response = mailbox_fetch_response,
  190. .fetch_notification = mailbox_fetch_notification,
  191. .clear_channel = mailbox_clear_channel,
  192. .poll_done = mailbox_poll_done,
  193. };
  194. const struct scmi_desc scmi_mailbox_desc = {
  195. .ops = &scmi_mailbox_ops,
  196. .max_rx_timeout_ms = 30, /* We may increase this if required */
  197. .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
  198. .max_msg_size = 128,
  199. };