smt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
  4. * Copyright (C) 2019-2020 Linaro Limited.
  5. */
  6. #define LOG_CATEGORY UCLASS_SCMI_AGENT
  7. #include <common.h>
  8. #include <cpu_func.h>
  9. #include <dm.h>
  10. #include <dm/device_compat.h>
  11. #include <errno.h>
  12. #include <scmi_agent.h>
  13. #include <asm/cache.h>
  14. #include <asm/system.h>
  15. #include <dm/ofnode.h>
  16. #include <linux/compat.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include "smt.h"
  20. /**
  21. * Get shared memory configuration defined by the referred DT phandle
  22. * Return with a errno compliant value.
  23. */
  24. int scmi_dt_get_smt_buffer(struct udevice *dev, struct scmi_smt *smt)
  25. {
  26. int ret;
  27. struct ofnode_phandle_args args;
  28. struct resource resource;
  29. ret = dev_read_phandle_with_args(dev, "shmem", NULL, 0, 0, &args);
  30. if (ret)
  31. return ret;
  32. ret = ofnode_read_resource(args.node, 0, &resource);
  33. if (ret)
  34. return ret;
  35. smt->size = resource_size(&resource);
  36. if (smt->size < sizeof(struct scmi_smt_header)) {
  37. dev_err(dev, "Shared memory buffer too small\n");
  38. return -EINVAL;
  39. }
  40. smt->buf = devm_ioremap(dev, resource.start, smt->size);
  41. if (!smt->buf)
  42. return -ENOMEM;
  43. #ifdef CONFIG_ARM
  44. if (dcache_status())
  45. mmu_set_region_dcache_behaviour(ALIGN_DOWN((uintptr_t)smt->buf, MMU_SECTION_SIZE),
  46. ALIGN(smt->size, MMU_SECTION_SIZE),
  47. DCACHE_OFF);
  48. #endif
  49. return 0;
  50. }
  51. /**
  52. * Write SCMI message @msg into a SMT shared buffer @smt.
  53. * Return 0 on success and with a negative errno in case of error.
  54. */
  55. int scmi_write_msg_to_smt(struct udevice *dev, struct scmi_smt *smt,
  56. struct scmi_msg *msg)
  57. {
  58. struct scmi_smt_header *hdr = (void *)smt->buf;
  59. if ((!msg->in_msg && msg->in_msg_sz) ||
  60. (!msg->out_msg && msg->out_msg_sz))
  61. return -EINVAL;
  62. if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
  63. dev_dbg(dev, "Channel busy\n");
  64. return -EBUSY;
  65. }
  66. if (smt->size < (sizeof(*hdr) + msg->in_msg_sz) ||
  67. smt->size < (sizeof(*hdr) + msg->out_msg_sz)) {
  68. dev_dbg(dev, "Buffer too small\n");
  69. return -ETOOSMALL;
  70. }
  71. /* Load message in shared memory */
  72. hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE;
  73. hdr->length = msg->in_msg_sz + sizeof(hdr->msg_header);
  74. hdr->msg_header = SMT_HEADER_TOKEN(0) |
  75. SMT_HEADER_MESSAGE_TYPE(0) |
  76. SMT_HEADER_PROTOCOL_ID(msg->protocol_id) |
  77. SMT_HEADER_MESSAGE_ID(msg->message_id);
  78. memcpy_toio(hdr->msg_payload, msg->in_msg, msg->in_msg_sz);
  79. return 0;
  80. }
  81. /**
  82. * Read SCMI message from a SMT shared buffer @smt and copy it into @msg.
  83. * Return 0 on success and with a negative errno in case of error.
  84. */
  85. int scmi_read_resp_from_smt(struct udevice *dev, struct scmi_smt *smt,
  86. struct scmi_msg *msg)
  87. {
  88. struct scmi_smt_header *hdr = (void *)smt->buf;
  89. if (!(hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE)) {
  90. dev_err(dev, "Channel unexpectedly busy\n");
  91. return -EBUSY;
  92. }
  93. if (hdr->channel_status & SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR) {
  94. dev_err(dev, "Channel error reported, reset channel\n");
  95. return -ECOMM;
  96. }
  97. if (hdr->length > msg->out_msg_sz + sizeof(hdr->msg_header)) {
  98. dev_err(dev, "Buffer to small\n");
  99. return -ETOOSMALL;
  100. }
  101. /* Get the data */
  102. msg->out_msg_sz = hdr->length - sizeof(hdr->msg_header);
  103. memcpy_fromio(msg->out_msg, hdr->msg_payload, msg->out_msg_sz);
  104. return 0;
  105. }
  106. /**
  107. * Clear SMT flags in shared buffer to allow further message exchange
  108. */
  109. void scmi_clear_smt_channel(struct scmi_smt *smt)
  110. {
  111. struct scmi_smt_header *hdr = (void *)smt->buf;
  112. hdr->channel_status &= ~SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR;
  113. }