smt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
  4. * Copyright (c) 2019-2020, Linaro Limited
  5. */
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <drivers/scmi-msg.h>
  11. #include <drivers/scmi.h>
  12. #include <lib/cassert.h>
  13. #include <lib/mmio.h>
  14. #include <lib/spinlock.h>
  15. #include <lib/utils.h>
  16. #include <plat/common/platform.h>
  17. #include "common.h"
  18. /* Legacy SMT/SCMI messages are 128 bytes at most including SMT header */
  19. #define SCMI_PLAYLOAD_MAX 92U
  20. #define SCMI_PLAYLOAD_U32_MAX (SCMI_PLAYLOAD_MAX / sizeof(uint32_t))
  21. /**
  22. * struct smt_header - SMT formatted header for SMT base shared memory transfer
  23. *
  24. * @status: Bit flags, see SMT_STATUS_*
  25. * @flags: Bit flags, see SMT_FLAG_*
  26. * @length: Byte size of message payload (variable) + ::message_header (32bit)
  27. * payload: SCMI message payload data
  28. */
  29. struct smt_header {
  30. uint32_t reserved0;
  31. uint32_t status;
  32. uint64_t reserved1;
  33. uint32_t flags;
  34. uint32_t length; /* message_header + payload */
  35. uint32_t message_header;
  36. uint32_t payload[];
  37. };
  38. CASSERT(SCMI_PLAYLOAD_MAX + sizeof(struct smt_header) <= SMT_BUF_SLOT_SIZE,
  39. assert_scmi_message_max_length_fits_in_smt_buffer_slot);
  40. /* Flag set in smt_header::status when SMT does not contain pending message */
  41. #define SMT_STATUS_FREE BIT_32(0)
  42. /* Flag set in smt_header::status when SMT reports an error */
  43. #define SMT_STATUS_ERROR BIT_32(1)
  44. /* Flag set in smt_header::flags when SMT uses interrupts */
  45. #define SMT_FLAG_INTR_ENABLED BIT_32(1)
  46. /* Bit fields packed in smt_header::message_header */
  47. #define SMT_MSG_ID_MASK GENMASK_32(7, 0)
  48. #define SMT_HDR_MSG_ID(_hdr) ((_hdr) & SMT_MSG_ID_MASK)
  49. #define SMT_MSG_TYPE_MASK GENMASK_32(9, 8)
  50. #define SMT_HDR_TYPE_ID(_hdr) (((_hdr) & SMT_MSG_TYPE_MASK) >> 8)
  51. #define SMT_MSG_PROT_ID_MASK GENMASK_32(17, 10)
  52. #define SMT_HDR_PROT_ID(_hdr) (((_hdr) & SMT_MSG_PROT_ID_MASK) >> 10)
  53. /*
  54. * Provision input message payload buffers for fastcall SMC context entries
  55. * and for interrupt context execution entries.
  56. */
  57. static uint32_t fast_smc_payload[PLATFORM_CORE_COUNT][SCMI_PLAYLOAD_U32_MAX];
  58. static uint32_t interrupt_payload[PLATFORM_CORE_COUNT][SCMI_PLAYLOAD_U32_MAX];
  59. /* SMP protection on channel access */
  60. static struct spinlock smt_channels_lock;
  61. /* If channel is not busy, set busy and return true, otherwise return false */
  62. static bool channel_set_busy(struct scmi_msg_channel *chan)
  63. {
  64. bool channel_is_busy;
  65. spin_lock(&smt_channels_lock);
  66. channel_is_busy = chan->busy;
  67. if (!channel_is_busy) {
  68. chan->busy = true;
  69. }
  70. spin_unlock(&smt_channels_lock);
  71. return !channel_is_busy;
  72. }
  73. static void channel_release_busy(struct scmi_msg_channel *chan)
  74. {
  75. chan->busy = false;
  76. }
  77. static struct smt_header *channel_to_smt_hdr(struct scmi_msg_channel *chan)
  78. {
  79. return (struct smt_header *)chan->shm_addr;
  80. }
  81. /*
  82. * Creates a SCMI message instance in secure memory and pushes it in the SCMI
  83. * message drivers. Message structure contains SCMI protocol meta-data and
  84. * references to input payload in secure memory and output message buffer
  85. * in shared memory.
  86. */
  87. static void scmi_proccess_smt(unsigned int agent_id, uint32_t *payload_buf)
  88. {
  89. struct scmi_msg_channel *chan;
  90. struct smt_header *smt_hdr;
  91. size_t in_payload_size;
  92. uint32_t smt_status;
  93. struct scmi_msg msg;
  94. bool error = true;
  95. chan = plat_scmi_get_channel(agent_id);
  96. if (chan == NULL) {
  97. return;
  98. }
  99. smt_hdr = channel_to_smt_hdr(chan);
  100. assert(smt_hdr);
  101. smt_status = __atomic_load_n(&smt_hdr->status, __ATOMIC_RELAXED);
  102. if (!channel_set_busy(chan)) {
  103. VERBOSE("SCMI channel %u busy", agent_id);
  104. goto out;
  105. }
  106. in_payload_size = __atomic_load_n(&smt_hdr->length, __ATOMIC_RELAXED) -
  107. sizeof(smt_hdr->message_header);
  108. if (in_payload_size > SCMI_PLAYLOAD_MAX) {
  109. VERBOSE("SCMI payload too big %zu", in_payload_size);
  110. goto out;
  111. }
  112. if ((smt_status & (SMT_STATUS_ERROR | SMT_STATUS_FREE)) != 0U) {
  113. VERBOSE("SCMI channel bad status 0x%x",
  114. smt_hdr->status & (SMT_STATUS_ERROR | SMT_STATUS_FREE));
  115. goto out;
  116. }
  117. /* Fill message */
  118. zeromem(&msg, sizeof(msg));
  119. msg.in = (char *)payload_buf;
  120. msg.in_size = in_payload_size;
  121. msg.out = (char *)smt_hdr->payload;
  122. msg.out_size = chan->shm_size - sizeof(*smt_hdr);
  123. assert((msg.out != NULL) && (msg.out_size >= sizeof(int32_t)));
  124. /* Here the payload is copied in secure memory */
  125. memcpy(msg.in, smt_hdr->payload, in_payload_size);
  126. msg.protocol_id = SMT_HDR_PROT_ID(smt_hdr->message_header);
  127. msg.message_id = SMT_HDR_MSG_ID(smt_hdr->message_header);
  128. msg.agent_id = agent_id;
  129. scmi_process_message(&msg);
  130. /* Update message length with the length of the response message */
  131. smt_hdr->length = msg.out_size_out + sizeof(smt_hdr->message_header);
  132. channel_release_busy(chan);
  133. error = false;
  134. out:
  135. if (error) {
  136. VERBOSE("SCMI error");
  137. smt_hdr->status |= SMT_STATUS_ERROR | SMT_STATUS_FREE;
  138. } else {
  139. smt_hdr->status |= SMT_STATUS_FREE;
  140. }
  141. }
  142. void scmi_smt_fastcall_smc_entry(unsigned int agent_id)
  143. {
  144. scmi_proccess_smt(agent_id,
  145. fast_smc_payload[plat_my_core_pos()]);
  146. }
  147. void scmi_smt_interrupt_entry(unsigned int agent_id)
  148. {
  149. scmi_proccess_smt(agent_id,
  150. interrupt_payload[plat_my_core_pos()]);
  151. }
  152. /* Init a SMT header for a shared memory buffer: state it a free/no-error */
  153. void scmi_smt_init_agent_channel(struct scmi_msg_channel *chan)
  154. {
  155. if (chan != NULL) {
  156. struct smt_header *smt_header = channel_to_smt_hdr(chan);
  157. if (smt_header != NULL) {
  158. memset(smt_header, 0, sizeof(*smt_header));
  159. smt_header->status = SMT_STATUS_FREE;
  160. return;
  161. }
  162. }
  163. panic();
  164. }