scmi_common.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <arch_helpers.h>
  8. #include <common/debug.h>
  9. #include <drivers/arm/css/scmi.h>
  10. #include <drivers/delay_timer.h>
  11. #include "scmi_private.h"
  12. #if HW_ASSISTED_COHERENCY
  13. #define scmi_lock_init(lock)
  14. #define scmi_lock_get(lock) spin_lock(lock)
  15. #define scmi_lock_release(lock) spin_unlock(lock)
  16. #else
  17. #define scmi_lock_init(lock) bakery_lock_init(lock)
  18. #define scmi_lock_get(lock) bakery_lock_get(lock)
  19. #define scmi_lock_release(lock) bakery_lock_release(lock)
  20. #endif
  21. /*
  22. * Private helper function to get exclusive access to SCMI channel.
  23. */
  24. void scmi_get_channel(scmi_channel_t *ch)
  25. {
  26. assert(ch->lock);
  27. scmi_lock_get(ch->lock);
  28. /* Make sure any previous command has finished */
  29. assert(SCMI_IS_CHANNEL_FREE(
  30. ((mailbox_mem_t *)(ch->info->scmi_mbx_mem))->status));
  31. }
  32. /*
  33. * Private helper function to transfer ownership of channel from AP to SCP.
  34. */
  35. void scmi_send_sync_command(scmi_channel_t *ch)
  36. {
  37. mailbox_mem_t *mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
  38. SCMI_MARK_CHANNEL_BUSY(mbx_mem->status);
  39. /*
  40. * Ensure that any write to the SCMI payload area is seen by SCP before
  41. * we write to the doorbell register. If these 2 writes were reordered
  42. * by the CPU then SCP would read stale payload data
  43. */
  44. dmbst();
  45. ch->info->ring_doorbell(ch->info);
  46. /*
  47. * Ensure that the write to the doorbell register is ordered prior to
  48. * checking whether the channel is free.
  49. */
  50. dmbsy();
  51. /* Wait for channel to be free */
  52. while (!SCMI_IS_CHANNEL_FREE(mbx_mem->status)) {
  53. if (ch->info->delay != 0)
  54. udelay(ch->info->delay);
  55. }
  56. /*
  57. * Ensure that any read to the SCMI payload area is done after reading
  58. * mailbox status. If these 2 reads were reordered then the CPU would
  59. * read invalid payload data
  60. */
  61. dmbld();
  62. }
  63. /*
  64. * Private helper function to release exclusive access to SCMI channel.
  65. */
  66. void scmi_put_channel(scmi_channel_t *ch)
  67. {
  68. /* Make sure any previous command has finished */
  69. assert(SCMI_IS_CHANNEL_FREE(
  70. ((mailbox_mem_t *)(ch->info->scmi_mbx_mem))->status));
  71. assert(ch->lock);
  72. scmi_lock_release(ch->lock);
  73. }
  74. /*
  75. * API to query the SCMI protocol version.
  76. */
  77. int scmi_proto_version(void *p, uint32_t proto_id, uint32_t *version)
  78. {
  79. mailbox_mem_t *mbx_mem;
  80. unsigned int token = 0;
  81. int ret;
  82. scmi_channel_t *ch = (scmi_channel_t *)p;
  83. validate_scmi_channel(ch);
  84. scmi_get_channel(ch);
  85. mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
  86. mbx_mem->msg_header = SCMI_MSG_CREATE(proto_id, SCMI_PROTO_VERSION_MSG,
  87. token);
  88. mbx_mem->len = SCMI_PROTO_VERSION_MSG_LEN;
  89. mbx_mem->flags = SCMI_FLAG_RESP_POLL;
  90. scmi_send_sync_command(ch);
  91. /* Get the return values */
  92. SCMI_PAYLOAD_RET_VAL2(mbx_mem->payload, ret, *version);
  93. assert(mbx_mem->len == SCMI_PROTO_VERSION_RESP_LEN);
  94. assert(token == SCMI_MSG_GET_TOKEN(mbx_mem->msg_header));
  95. scmi_put_channel(ch);
  96. return ret;
  97. }
  98. /*
  99. * API to query the protocol message attributes for a SCMI protocol.
  100. */
  101. int scmi_proto_msg_attr(void *p, uint32_t proto_id,
  102. uint32_t command_id, uint32_t *attr)
  103. {
  104. mailbox_mem_t *mbx_mem;
  105. unsigned int token = 0;
  106. int ret;
  107. scmi_channel_t *ch = (scmi_channel_t *)p;
  108. validate_scmi_channel(ch);
  109. scmi_get_channel(ch);
  110. mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
  111. mbx_mem->msg_header = SCMI_MSG_CREATE(proto_id,
  112. SCMI_PROTO_MSG_ATTR_MSG, token);
  113. mbx_mem->len = SCMI_PROTO_MSG_ATTR_MSG_LEN;
  114. mbx_mem->flags = SCMI_FLAG_RESP_POLL;
  115. SCMI_PAYLOAD_ARG1(mbx_mem->payload, command_id);
  116. scmi_send_sync_command(ch);
  117. /* Get the return values */
  118. SCMI_PAYLOAD_RET_VAL2(mbx_mem->payload, ret, *attr);
  119. assert(mbx_mem->len == SCMI_PROTO_MSG_ATTR_RESP_LEN);
  120. assert(token == SCMI_MSG_GET_TOKEN(mbx_mem->msg_header));
  121. scmi_put_channel(ch);
  122. return ret;
  123. }
  124. /*
  125. * SCMI Driver initialization API. Returns initialized channel on success
  126. * or NULL on error. The return type is an opaque void pointer.
  127. */
  128. void *scmi_init(scmi_channel_t *ch)
  129. {
  130. uint32_t version;
  131. int ret;
  132. assert(ch && ch->info);
  133. assert(ch->info->db_reg_addr);
  134. assert(ch->info->db_modify_mask);
  135. assert(ch->info->db_preserve_mask);
  136. assert(ch->info->ring_doorbell != NULL);
  137. assert(ch->lock);
  138. scmi_lock_init(ch->lock);
  139. ch->is_initialized = 1;
  140. ret = scmi_proto_version(ch, SCMI_PWR_DMN_PROTO_ID, &version);
  141. if (ret != SCMI_E_SUCCESS) {
  142. WARN("SCMI power domain protocol version message failed\n");
  143. goto error;
  144. }
  145. if (!is_scmi_version_compatible(SCMI_PWR_DMN_PROTO_VER, version)) {
  146. WARN("SCMI power domain protocol version 0x%x incompatible with driver version 0x%x\n",
  147. version, SCMI_PWR_DMN_PROTO_VER);
  148. goto error;
  149. }
  150. VERBOSE("SCMI power domain protocol version 0x%x detected\n", version);
  151. ret = scmi_proto_version(ch, SCMI_SYS_PWR_PROTO_ID, &version);
  152. if ((ret != SCMI_E_SUCCESS)) {
  153. WARN("SCMI system power protocol version message failed\n");
  154. goto error;
  155. }
  156. if (!is_scmi_version_compatible(SCMI_SYS_PWR_PROTO_VER, version)) {
  157. WARN("SCMI system power management protocol version 0x%x incompatible with driver version 0x%x\n",
  158. version, SCMI_SYS_PWR_PROTO_VER);
  159. goto error;
  160. }
  161. VERBOSE("SCMI system power management protocol version 0x%x detected\n",
  162. version);
  163. INFO("SCMI driver initialized\n");
  164. return (void *)ch;
  165. error:
  166. ch->is_initialized = 0;
  167. return NULL;
  168. }