brcm_scpi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2019-2020, 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 <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <lib/utils.h>
  11. #include <plat/common/platform.h>
  12. #include <brcm_mhu.h>
  13. #include <brcm_scpi.h>
  14. #include <platform_def.h>
  15. #define SCPI_SHARED_MEM_SCP_TO_AP (PLAT_SCP_COM_SHARED_MEM_BASE)
  16. #define SCPI_SHARED_MEM_AP_TO_SCP (PLAT_SCP_COM_SHARED_MEM_BASE \
  17. + 0x100)
  18. /* Header and payload addresses for commands from AP to SCP */
  19. #define SCPI_CMD_HEADER_AP_TO_SCP \
  20. ((scpi_cmd_t *) SCPI_SHARED_MEM_AP_TO_SCP)
  21. #define SCPI_CMD_PAYLOAD_AP_TO_SCP \
  22. ((void *) (SCPI_SHARED_MEM_AP_TO_SCP + sizeof(scpi_cmd_t)))
  23. /* Header and payload addresses for responses from SCP to AP */
  24. #define SCPI_RES_HEADER_SCP_TO_AP \
  25. ((scpi_cmd_t *) SCPI_SHARED_MEM_SCP_TO_AP)
  26. #define SCPI_RES_PAYLOAD_SCP_TO_AP \
  27. ((void *) (SCPI_SHARED_MEM_SCP_TO_AP + sizeof(scpi_cmd_t)))
  28. /* ID of the MHU slot used for the SCPI protocol */
  29. #define SCPI_MHU_SLOT_ID 0
  30. static void scpi_secure_message_start(void)
  31. {
  32. mhu_secure_message_start(SCPI_MHU_SLOT_ID);
  33. }
  34. static void scpi_secure_message_send(size_t payload_size)
  35. {
  36. /*
  37. * Ensure that any write to the SCPI payload area is seen by SCP before
  38. * we write to the MHU register. If these 2 writes were reordered by
  39. * the CPU then SCP would read stale payload data
  40. */
  41. dmbst();
  42. mhu_secure_message_send(SCPI_MHU_SLOT_ID);
  43. }
  44. static void scpi_secure_message_receive(scpi_cmd_t *cmd)
  45. {
  46. uint32_t mhu_status;
  47. assert(cmd != NULL);
  48. mhu_status = mhu_secure_message_wait();
  49. /* Expect an SCPI message, reject any other protocol */
  50. if (mhu_status != (1 << SCPI_MHU_SLOT_ID)) {
  51. ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n",
  52. mhu_status);
  53. panic();
  54. }
  55. /*
  56. * Ensure that any read to the SCPI payload area is done after reading
  57. * the MHU register. If these 2 reads were reordered then the CPU would
  58. * read invalid payload data
  59. */
  60. dmbld();
  61. memcpy(cmd, (void *) SCPI_SHARED_MEM_SCP_TO_AP, sizeof(*cmd));
  62. }
  63. static void scpi_secure_message_end(void)
  64. {
  65. mhu_secure_message_end(SCPI_MHU_SLOT_ID);
  66. }
  67. int scpi_wait_ready(void)
  68. {
  69. scpi_cmd_t scpi_cmd;
  70. VERBOSE("Waiting for SCP_READY command...\n");
  71. /* Get a message from the SCP */
  72. scpi_secure_message_start();
  73. scpi_secure_message_receive(&scpi_cmd);
  74. scpi_secure_message_end();
  75. /* We are expecting 'SCP Ready', produce correct error if it's not */
  76. scpi_status_t status = SCP_OK;
  77. if (scpi_cmd.id != SCPI_CMD_SCP_READY) {
  78. ERROR("Unexpected SCP command: expected #%u, received #%u\n",
  79. SCPI_CMD_SCP_READY, scpi_cmd.id);
  80. status = SCP_E_SUPPORT;
  81. } else if (scpi_cmd.size != 0) {
  82. ERROR("SCP_READY cmd has incorrect size: expected 0, got %u\n",
  83. scpi_cmd.size);
  84. status = SCP_E_SIZE;
  85. }
  86. VERBOSE("Sending response for SCP_READY command\n");
  87. /*
  88. * Send our response back to SCP.
  89. * We are using the same SCPI header, just update the status field.
  90. */
  91. scpi_cmd.status = status;
  92. scpi_secure_message_start();
  93. memcpy((void *) SCPI_SHARED_MEM_AP_TO_SCP, &scpi_cmd, sizeof(scpi_cmd));
  94. scpi_secure_message_send(0);
  95. scpi_secure_message_end();
  96. return status == SCP_OK ? 0 : -1;
  97. }
  98. void scpi_set_brcm_power_state(unsigned int mpidr,
  99. scpi_power_state_t cpu_state, scpi_power_state_t cluster_state,
  100. scpi_power_state_t brcm_state)
  101. {
  102. scpi_cmd_t *cmd;
  103. uint32_t state = 0;
  104. uint32_t *payload_addr;
  105. #if ARM_PLAT_MT
  106. /*
  107. * The current SCPI driver only caters for single-threaded platforms.
  108. * Hence we ignore the thread ID (which is always 0) for such platforms.
  109. */
  110. state |= (mpidr >> MPIDR_AFF1_SHIFT) & 0x0f; /* CPU ID */
  111. state |= ((mpidr >> MPIDR_AFF2_SHIFT) & 0x0f) << 4; /* Cluster ID */
  112. #else
  113. state |= mpidr & 0x0f; /* CPU ID */
  114. state |= (mpidr & 0xf00) >> 4; /* Cluster ID */
  115. #endif /* ARM_PLAT_MT */
  116. state |= cpu_state << 8;
  117. state |= cluster_state << 12;
  118. state |= brcm_state << 16;
  119. scpi_secure_message_start();
  120. /* Populate the command header */
  121. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  122. cmd->id = SCPI_CMD_SET_POWER_STATE;
  123. cmd->set = SCPI_SET_NORMAL;
  124. cmd->sender = 0;
  125. cmd->size = sizeof(state);
  126. /* Populate the command payload */
  127. payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
  128. *payload_addr = state;
  129. scpi_secure_message_send(sizeof(state));
  130. /*
  131. * SCP does not reply to this command in order to avoid MHU interrupts
  132. * from the sender, which could interfere with its power state request.
  133. */
  134. scpi_secure_message_end();
  135. }
  136. /*
  137. * Query and obtain power state from SCP.
  138. *
  139. * In response to the query, SCP returns power states of all CPUs in all
  140. * clusters of the system. The returned response is then filtered based on the
  141. * supplied MPIDR. Power states of requested cluster and CPUs within are updated
  142. * via. supplied non-NULL pointer arguments.
  143. *
  144. * Returns 0 on success, or -1 on errors.
  145. */
  146. int scpi_get_brcm_power_state(unsigned int mpidr, unsigned int *cpu_state_p,
  147. unsigned int *cluster_state_p)
  148. {
  149. scpi_cmd_t *cmd;
  150. scpi_cmd_t response;
  151. int power_state, cpu, cluster, rc = -1;
  152. /*
  153. * Extract CPU and cluster membership of the given MPIDR. SCPI caters
  154. * for only up to 0xf clusters, and 8 CPUs per cluster
  155. */
  156. cpu = mpidr & MPIDR_AFFLVL_MASK;
  157. cluster = (mpidr >> MPIDR_AFF1_SHIFT) & MPIDR_AFFLVL_MASK;
  158. if (cpu >= 8 || cluster >= 0xf)
  159. return -1;
  160. scpi_secure_message_start();
  161. /* Populate request headers */
  162. zeromem(SCPI_CMD_HEADER_AP_TO_SCP, sizeof(*cmd));
  163. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  164. cmd->id = SCPI_CMD_GET_POWER_STATE;
  165. /*
  166. * Send message and wait for SCP's response
  167. */
  168. scpi_secure_message_send(0);
  169. scpi_secure_message_receive(&response);
  170. if (response.status != SCP_OK)
  171. goto exit;
  172. /* Validate SCP response */
  173. if (!CHECK_RESPONSE(response, cluster))
  174. goto exit;
  175. /* Extract power states for required cluster */
  176. power_state = *(((uint16_t *) SCPI_RES_PAYLOAD_SCP_TO_AP) + cluster);
  177. if (CLUSTER_ID(power_state) != cluster)
  178. goto exit;
  179. /* Update power state via. pointers */
  180. if (cluster_state_p)
  181. *cluster_state_p = CLUSTER_POWER_STATE(power_state);
  182. if (cpu_state_p)
  183. *cpu_state_p = CPU_POWER_STATE(power_state);
  184. rc = 0;
  185. exit:
  186. scpi_secure_message_end();
  187. return rc;
  188. }
  189. uint32_t scpi_sys_power_state(scpi_system_state_t system_state)
  190. {
  191. scpi_cmd_t *cmd;
  192. uint8_t *payload_addr;
  193. scpi_secure_message_start();
  194. /* Populate the command header */
  195. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  196. cmd->id = SCPI_CMD_SYS_POWER_STATE;
  197. cmd->set = 0;
  198. cmd->sender = 0;
  199. cmd->size = sizeof(*payload_addr);
  200. /* Populate the command payload */
  201. payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
  202. *payload_addr = system_state & 0xff;
  203. scpi_secure_message_send(sizeof(*payload_addr));
  204. scpi_secure_message_end();
  205. return SCP_OK;
  206. }