shmem.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * For transport using shared mem structure.
  4. *
  5. * Copyright (C) 2019 ARM Ltd.
  6. */
  7. #include <linux/ktime.h>
  8. #include <linux/io.h>
  9. #include <linux/processor.h>
  10. #include <linux/types.h>
  11. #include <asm-generic/bug.h>
  12. #include "common.h"
  13. /*
  14. * SCMI specification requires all parameters, message headers, return
  15. * arguments or any protocol data to be expressed in little endian
  16. * format only.
  17. */
  18. struct scmi_shared_mem {
  19. __le32 reserved;
  20. __le32 channel_status;
  21. #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1)
  22. #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0)
  23. __le32 reserved1[2];
  24. __le32 flags;
  25. #define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0)
  26. __le32 length;
  27. __le32 msg_header;
  28. u8 msg_payload[];
  29. };
  30. void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem,
  31. struct scmi_xfer *xfer, struct scmi_chan_info *cinfo)
  32. {
  33. ktime_t stop;
  34. /*
  35. * Ideally channel must be free by now unless OS timeout last
  36. * request and platform continued to process the same, wait
  37. * until it releases the shared memory, otherwise we may endup
  38. * overwriting its response with new message payload or vice-versa.
  39. * Giving up anyway after twice the expected channel timeout so as
  40. * not to bail-out on intermittent issues where the platform is
  41. * occasionally a bit slower to answer.
  42. *
  43. * Note that after a timeout is detected we bail-out and carry on but
  44. * the transport functionality is probably permanently compromised:
  45. * this is just to ease debugging and avoid complete hangs on boot
  46. * due to a misbehaving SCMI firmware.
  47. */
  48. stop = ktime_add_ms(ktime_get(), 2 * cinfo->rx_timeout_ms);
  49. spin_until_cond((ioread32(&shmem->channel_status) &
  50. SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE) ||
  51. ktime_after(ktime_get(), stop));
  52. if (!(ioread32(&shmem->channel_status) &
  53. SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
  54. WARN_ON_ONCE(1);
  55. dev_err(cinfo->dev,
  56. "Timeout waiting for a free TX channel !\n");
  57. return;
  58. }
  59. /* Mark channel busy + clear error */
  60. iowrite32(0x0, &shmem->channel_status);
  61. iowrite32(xfer->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
  62. &shmem->flags);
  63. iowrite32(sizeof(shmem->msg_header) + xfer->tx.len, &shmem->length);
  64. iowrite32(pack_scmi_header(&xfer->hdr), &shmem->msg_header);
  65. if (xfer->tx.buf)
  66. memcpy_toio(shmem->msg_payload, xfer->tx.buf, xfer->tx.len);
  67. }
  68. u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem)
  69. {
  70. return ioread32(&shmem->msg_header);
  71. }
  72. void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem,
  73. struct scmi_xfer *xfer)
  74. {
  75. size_t len = ioread32(&shmem->length);
  76. xfer->hdr.status = ioread32(shmem->msg_payload);
  77. /* Skip the length of header and status in shmem area i.e 8 bytes */
  78. xfer->rx.len = min_t(size_t, xfer->rx.len, len > 8 ? len - 8 : 0);
  79. /* Take a copy to the rx buffer.. */
  80. memcpy_fromio(xfer->rx.buf, shmem->msg_payload + 4, xfer->rx.len);
  81. }
  82. void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem,
  83. size_t max_len, struct scmi_xfer *xfer)
  84. {
  85. size_t len = ioread32(&shmem->length);
  86. /* Skip only the length of header in shmem area i.e 4 bytes */
  87. xfer->rx.len = min_t(size_t, max_len, len > 4 ? len - 4 : 0);
  88. /* Take a copy to the rx buffer.. */
  89. memcpy_fromio(xfer->rx.buf, shmem->msg_payload, xfer->rx.len);
  90. }
  91. void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem)
  92. {
  93. iowrite32(SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE, &shmem->channel_status);
  94. }
  95. bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem,
  96. struct scmi_xfer *xfer)
  97. {
  98. u16 xfer_id;
  99. xfer_id = MSG_XTRACT_TOKEN(ioread32(&shmem->msg_header));
  100. if (xfer->hdr.seq != xfer_id)
  101. return false;
  102. return ioread32(&shmem->channel_status) &
  103. (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
  104. SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
  105. }