sq_scpi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <platform_def.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <sq_common.h>
  12. #include "sq_mhu.h"
  13. #include "sq_scpi.h"
  14. #define SCPI_SHARED_MEM_SCP_TO_AP PLAT_SQ_SCP_COM_SHARED_MEM_BASE
  15. #define SCPI_SHARED_MEM_AP_TO_SCP (PLAT_SQ_SCP_COM_SHARED_MEM_BASE \
  16. + 0x100)
  17. #define SCPI_CMD_HEADER_AP_TO_SCP \
  18. ((scpi_cmd_t *) SCPI_SHARED_MEM_AP_TO_SCP)
  19. #define SCPI_CMD_PAYLOAD_AP_TO_SCP \
  20. ((void *) (SCPI_SHARED_MEM_AP_TO_SCP + sizeof(scpi_cmd_t)))
  21. /* ID of the MHU slot used for the SCPI protocol */
  22. #define SCPI_MHU_SLOT_ID 0
  23. static void scpi_secure_message_start(void)
  24. {
  25. mhu_secure_message_start(SCPI_MHU_SLOT_ID);
  26. }
  27. static void scpi_secure_message_send(size_t payload_size)
  28. {
  29. /*
  30. * Ensure that any write to the SCPI payload area is seen by SCP before
  31. * we write to the MHU register. If these 2 writes were reordered by
  32. * the CPU then SCP would read stale payload data
  33. */
  34. dmbst();
  35. mhu_secure_message_send(SCPI_MHU_SLOT_ID);
  36. }
  37. static void scpi_secure_message_receive(scpi_cmd_t *cmd)
  38. {
  39. uint32_t mhu_status;
  40. assert(cmd != NULL);
  41. mhu_status = mhu_secure_message_wait();
  42. /* Expect an SCPI message, reject any other protocol */
  43. if (mhu_status != (1 << SCPI_MHU_SLOT_ID)) {
  44. ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n",
  45. mhu_status);
  46. panic();
  47. }
  48. /*
  49. * Ensure that any read to the SCPI payload area is done after reading
  50. * the MHU register. If these 2 reads were reordered then the CPU would
  51. * read invalid payload data
  52. */
  53. dmbld();
  54. memcpy(cmd, (void *) SCPI_SHARED_MEM_SCP_TO_AP, sizeof(*cmd));
  55. }
  56. static void scpi_secure_message_end(void)
  57. {
  58. mhu_secure_message_end(SCPI_MHU_SLOT_ID);
  59. }
  60. int scpi_wait_ready(void)
  61. {
  62. scpi_cmd_t scpi_cmd;
  63. scpi_status_t status = SCP_OK;
  64. VERBOSE("Waiting for SCP_READY command...\n");
  65. /* Get a message from the SCP */
  66. scpi_secure_message_start();
  67. scpi_secure_message_receive(&scpi_cmd);
  68. scpi_secure_message_end();
  69. /* We are expecting 'SCP Ready', produce correct error if it's not */
  70. if (scpi_cmd.id != SCPI_CMD_SCP_READY) {
  71. ERROR("Unexpected SCP command: expected command #%u,"
  72. "got command #%u\n", SCPI_CMD_SCP_READY, scpi_cmd.id);
  73. status = SCP_E_SUPPORT;
  74. } else if (scpi_cmd.size != 0) {
  75. ERROR("SCP_READY command has incorrect size: expected 0,"
  76. "got %u\n", scpi_cmd.size);
  77. status = SCP_E_SIZE;
  78. }
  79. VERBOSE("Sending response for SCP_READY command\n");
  80. /*
  81. * Send our response back to SCP.
  82. * We are using the same SCPI header, just update the status field.
  83. */
  84. scpi_cmd.status = status;
  85. scpi_secure_message_start();
  86. memcpy((void *) SCPI_SHARED_MEM_AP_TO_SCP, &scpi_cmd, sizeof(scpi_cmd));
  87. scpi_secure_message_send(0);
  88. scpi_secure_message_end();
  89. return status == SCP_OK ? 0 : -1;
  90. }
  91. void scpi_set_sq_power_state(unsigned int mpidr, scpi_power_state_t cpu_state,
  92. scpi_power_state_t cluster_state, scpi_power_state_t sq_state)
  93. {
  94. scpi_cmd_t *cmd;
  95. uint32_t state = 0;
  96. uint32_t *payload_addr;
  97. state |= mpidr & 0x0f; /* CPU ID */
  98. state |= (mpidr & 0xf00) >> 4; /* Cluster ID */
  99. state |= cpu_state << 8;
  100. state |= cluster_state << 12;
  101. state |= sq_state << 16;
  102. scpi_secure_message_start();
  103. /* Populate the command header */
  104. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  105. cmd->id = SCPI_CMD_SET_POWER_STATE;
  106. cmd->set = SCPI_SET_NORMAL;
  107. cmd->sender = 0;
  108. cmd->size = sizeof(state);
  109. /* Populate the command payload */
  110. payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
  111. *payload_addr = state;
  112. scpi_secure_message_send(sizeof(state));
  113. /*
  114. * SCP does not reply to this command in order to avoid MHU interrupts
  115. * from the sender, which could interfere with its power state request.
  116. */
  117. scpi_secure_message_end();
  118. }
  119. uint32_t scpi_sys_power_state(scpi_system_state_t system_state)
  120. {
  121. scpi_cmd_t *cmd;
  122. uint8_t *payload_addr;
  123. scpi_cmd_t response;
  124. scpi_secure_message_start();
  125. /* Populate the command header */
  126. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  127. cmd->id = SCPI_CMD_SYS_POWER_STATE;
  128. cmd->set = 0;
  129. cmd->sender = 0;
  130. cmd->size = sizeof(*payload_addr);
  131. /* Populate the command payload */
  132. payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
  133. *payload_addr = system_state & 0xff;
  134. scpi_secure_message_send(sizeof(*payload_addr));
  135. scpi_secure_message_receive(&response);
  136. scpi_secure_message_end();
  137. return response.status;
  138. }
  139. uint32_t scpi_get_draminfo(struct draminfo *info)
  140. {
  141. scpi_cmd_t *cmd;
  142. struct {
  143. scpi_cmd_t cmd;
  144. struct draminfo info;
  145. } response;
  146. uint32_t mhu_status;
  147. scpi_secure_message_start();
  148. /* Populate the command header */
  149. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  150. cmd->id = SCPI_CMD_GET_DRAMINFO;
  151. cmd->set = SCPI_SET_EXTENDED;
  152. cmd->sender = 0;
  153. cmd->size = 0;
  154. scpi_secure_message_send(0);
  155. mhu_status = mhu_secure_message_wait();
  156. /* Expect an SCPI message, reject any other protocol */
  157. if (mhu_status != (1 << SCPI_MHU_SLOT_ID)) {
  158. ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n",
  159. mhu_status);
  160. panic();
  161. }
  162. /*
  163. * Ensure that any read to the SCPI payload area is done after reading
  164. * the MHU register. If these 2 reads were reordered then the CPU would
  165. * read invalid payload data
  166. */
  167. dmbld();
  168. memcpy(&response, (void *)SCPI_SHARED_MEM_SCP_TO_AP, sizeof(response));
  169. scpi_secure_message_end();
  170. if (response.cmd.status == SCP_OK)
  171. *info = response.info;
  172. return response.cmd.status;
  173. }