mailbox.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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);
  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));
  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_setup(struct scmi_chan_info *cinfo, struct device *dev,
  46. bool tx)
  47. {
  48. const char *desc = tx ? "Tx" : "Rx";
  49. struct device *cdev = cinfo->dev;
  50. struct scmi_mailbox *smbox;
  51. struct device_node *shmem;
  52. int ret, idx = tx ? 0 : 1;
  53. struct mbox_client *cl;
  54. resource_size_t size;
  55. struct resource res;
  56. smbox = devm_kzalloc(dev, sizeof(*smbox), GFP_KERNEL);
  57. if (!smbox)
  58. return -ENOMEM;
  59. shmem = of_parse_phandle(cdev->of_node, "shmem", idx);
  60. ret = of_address_to_resource(shmem, 0, &res);
  61. of_node_put(shmem);
  62. if (ret) {
  63. dev_err(cdev, "failed to get SCMI %s shared memory\n", desc);
  64. return ret;
  65. }
  66. size = resource_size(&res);
  67. smbox->shmem = devm_ioremap(dev, res.start, size);
  68. if (!smbox->shmem) {
  69. dev_err(dev, "failed to ioremap SCMI %s shared memory\n", desc);
  70. return -EADDRNOTAVAIL;
  71. }
  72. cl = &smbox->cl;
  73. cl->dev = cdev;
  74. cl->tx_prepare = tx ? tx_prepare : NULL;
  75. cl->rx_callback = rx_callback;
  76. cl->tx_block = false;
  77. cl->knows_txdone = tx;
  78. smbox->chan = mbox_request_channel(cl, tx ? 0 : 1);
  79. if (IS_ERR(smbox->chan)) {
  80. ret = PTR_ERR(smbox->chan);
  81. if (ret != -EPROBE_DEFER)
  82. dev_err(cdev, "failed to request SCMI %s mailbox\n",
  83. tx ? "Tx" : "Rx");
  84. return ret;
  85. }
  86. cinfo->transport_info = smbox;
  87. smbox->cinfo = cinfo;
  88. return 0;
  89. }
  90. static int mailbox_chan_free(int id, void *p, void *data)
  91. {
  92. struct scmi_chan_info *cinfo = p;
  93. struct scmi_mailbox *smbox = cinfo->transport_info;
  94. if (smbox && !IS_ERR(smbox->chan)) {
  95. mbox_free_channel(smbox->chan);
  96. cinfo->transport_info = NULL;
  97. smbox->chan = NULL;
  98. smbox->cinfo = NULL;
  99. }
  100. scmi_free_channel(cinfo, data, id);
  101. return 0;
  102. }
  103. static int mailbox_send_message(struct scmi_chan_info *cinfo,
  104. struct scmi_xfer *xfer)
  105. {
  106. struct scmi_mailbox *smbox = cinfo->transport_info;
  107. int ret;
  108. ret = mbox_send_message(smbox->chan, xfer);
  109. /* mbox_send_message returns non-negative value on success, so reset */
  110. if (ret > 0)
  111. ret = 0;
  112. return ret;
  113. }
  114. static void mailbox_mark_txdone(struct scmi_chan_info *cinfo, int ret)
  115. {
  116. struct scmi_mailbox *smbox = cinfo->transport_info;
  117. /*
  118. * NOTE: we might prefer not to need the mailbox ticker to manage the
  119. * transfer queueing since the protocol layer queues things by itself.
  120. * Unfortunately, we have to kick the mailbox framework after we have
  121. * received our message.
  122. */
  123. mbox_client_txdone(smbox->chan, ret);
  124. }
  125. static void mailbox_fetch_response(struct scmi_chan_info *cinfo,
  126. struct scmi_xfer *xfer)
  127. {
  128. struct scmi_mailbox *smbox = cinfo->transport_info;
  129. shmem_fetch_response(smbox->shmem, xfer);
  130. }
  131. static void mailbox_fetch_notification(struct scmi_chan_info *cinfo,
  132. size_t max_len, struct scmi_xfer *xfer)
  133. {
  134. struct scmi_mailbox *smbox = cinfo->transport_info;
  135. shmem_fetch_notification(smbox->shmem, max_len, xfer);
  136. }
  137. static void mailbox_clear_channel(struct scmi_chan_info *cinfo)
  138. {
  139. struct scmi_mailbox *smbox = cinfo->transport_info;
  140. shmem_clear_channel(smbox->shmem);
  141. }
  142. static bool
  143. mailbox_poll_done(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
  144. {
  145. struct scmi_mailbox *smbox = cinfo->transport_info;
  146. return shmem_poll_done(smbox->shmem, xfer);
  147. }
  148. static const struct scmi_transport_ops scmi_mailbox_ops = {
  149. .chan_available = mailbox_chan_available,
  150. .chan_setup = mailbox_chan_setup,
  151. .chan_free = mailbox_chan_free,
  152. .send_message = mailbox_send_message,
  153. .mark_txdone = mailbox_mark_txdone,
  154. .fetch_response = mailbox_fetch_response,
  155. .fetch_notification = mailbox_fetch_notification,
  156. .clear_channel = mailbox_clear_channel,
  157. .poll_done = mailbox_poll_done,
  158. };
  159. const struct scmi_desc scmi_mailbox_desc = {
  160. .ops = &scmi_mailbox_ops,
  161. .max_rx_timeout_ms = 30, /* We may increase this if required */
  162. .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
  163. .max_msg_size = 128,
  164. };