css_scpi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2014-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 <drivers/arm/css/css_mhu.h>
  11. #include <drivers/arm/css/css_scpi.h>
  12. #include <lib/utils.h>
  13. #include <plat/common/platform.h>
  14. #include <platform_def.h>
  15. #define SCPI_SHARED_MEM_SCP_TO_AP PLAT_CSS_SCP_COM_SHARED_MEM_BASE
  16. #define SCPI_SHARED_MEM_AP_TO_SCP (PLAT_CSS_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 int 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. return -1;
  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. return 0;
  63. }
  64. static void scpi_secure_message_end(void)
  65. {
  66. mhu_secure_message_end(SCPI_MHU_SLOT_ID);
  67. }
  68. int scpi_wait_ready(void)
  69. {
  70. scpi_cmd_t scpi_cmd;
  71. int rc;
  72. VERBOSE("Waiting for SCP_READY command...\n");
  73. /* Get a message from the SCP */
  74. scpi_secure_message_start();
  75. rc = scpi_secure_message_receive(&scpi_cmd);
  76. scpi_secure_message_end();
  77. /* If no message was received, don't send a response */
  78. if (rc != 0)
  79. return rc;
  80. /* We are expecting 'SCP Ready', produce correct error if it's not */
  81. scpi_status_t status = SCP_OK;
  82. if (scpi_cmd.id != SCPI_CMD_SCP_READY) {
  83. ERROR("Unexpected SCP command: expected command #%u, got command #%u\n",
  84. SCPI_CMD_SCP_READY, scpi_cmd.id);
  85. status = SCP_E_SUPPORT;
  86. } else if (scpi_cmd.size != 0) {
  87. ERROR("SCP_READY command has incorrect size: expected 0, got %u\n",
  88. scpi_cmd.size);
  89. status = SCP_E_SIZE;
  90. }
  91. VERBOSE("Sending response for SCP_READY command\n");
  92. /*
  93. * Send our response back to SCP.
  94. * We are using the same SCPI header, just update the status field.
  95. */
  96. scpi_cmd.status = status;
  97. scpi_secure_message_start();
  98. memcpy((void *) SCPI_SHARED_MEM_AP_TO_SCP, &scpi_cmd, sizeof(scpi_cmd));
  99. scpi_secure_message_send(0);
  100. scpi_secure_message_end();
  101. return status == SCP_OK ? 0 : -1;
  102. }
  103. void scpi_set_css_power_state(unsigned int mpidr,
  104. scpi_power_state_t cpu_state, scpi_power_state_t cluster_state,
  105. scpi_power_state_t css_state)
  106. {
  107. scpi_cmd_t *cmd;
  108. uint32_t state = 0;
  109. uint32_t *payload_addr;
  110. #if ARM_PLAT_MT
  111. /*
  112. * The current SCPI driver only caters for single-threaded platforms.
  113. * Hence we ignore the thread ID (which is always 0) for such platforms.
  114. */
  115. state |= (mpidr >> MPIDR_AFF1_SHIFT) & 0x0f; /* CPU ID */
  116. state |= ((mpidr >> MPIDR_AFF2_SHIFT) & 0x0f) << 4; /* Cluster ID */
  117. #else
  118. state |= mpidr & 0x0f; /* CPU ID */
  119. state |= (mpidr & 0xf00) >> 4; /* Cluster ID */
  120. #endif /* ARM_PLAT_MT */
  121. state |= cpu_state << 8;
  122. state |= cluster_state << 12;
  123. state |= css_state << 16;
  124. scpi_secure_message_start();
  125. /* Populate the command header */
  126. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  127. cmd->id = SCPI_CMD_SET_CSS_POWER_STATE;
  128. cmd->set = SCPI_SET_NORMAL;
  129. cmd->sender = 0;
  130. cmd->size = sizeof(state);
  131. /* Populate the command payload */
  132. payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
  133. *payload_addr = state;
  134. scpi_secure_message_send(sizeof(state));
  135. /*
  136. * SCP does not reply to this command in order to avoid MHU interrupts
  137. * from the sender, which could interfere with its power state request.
  138. */
  139. scpi_secure_message_end();
  140. }
  141. /*
  142. * Query and obtain CSS power state from SCP.
  143. *
  144. * In response to the query, SCP returns power states of all CPUs in all
  145. * clusters of the system. The returned response is then filtered based on the
  146. * supplied MPIDR. Power states of requested cluster and CPUs within are updated
  147. * via supplied non-NULL pointer arguments.
  148. *
  149. * Returns 0 on success, or -1 on errors.
  150. */
  151. int scpi_get_css_power_state(unsigned int mpidr, unsigned int *cpu_state_p,
  152. unsigned int *cluster_state_p)
  153. {
  154. scpi_cmd_t *cmd;
  155. scpi_cmd_t response;
  156. int power_state, cpu, cluster, rc = -1;
  157. /*
  158. * Extract CPU and cluster membership of the given MPIDR. SCPI caters
  159. * for only up to 0xf clusters, and 8 CPUs per cluster
  160. */
  161. #if ARM_PLAT_MT
  162. /*
  163. * The current SCPI driver only caters for single-threaded platforms.
  164. * Hence we ignore the thread ID (which is always 0) for such platforms.
  165. */
  166. cpu = (mpidr >> MPIDR_AFF1_SHIFT) & MPIDR_AFFLVL_MASK;
  167. cluster = (mpidr >> MPIDR_AFF2_SHIFT) & MPIDR_AFFLVL_MASK;
  168. #else
  169. cpu = mpidr & MPIDR_AFFLVL_MASK;
  170. cluster = (mpidr >> MPIDR_AFF1_SHIFT) & MPIDR_AFFLVL_MASK;
  171. #endif /* ARM_PLAT_MT */
  172. if (cpu >= 8 || cluster >= 0xf)
  173. return -1;
  174. scpi_secure_message_start();
  175. /* Populate request headers */
  176. zeromem(SCPI_CMD_HEADER_AP_TO_SCP, sizeof(*cmd));
  177. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  178. cmd->id = SCPI_CMD_GET_CSS_POWER_STATE;
  179. /*
  180. * Send message and wait for SCP's response
  181. */
  182. scpi_secure_message_send(0);
  183. if (scpi_secure_message_receive(&response) != 0)
  184. goto exit;
  185. if (response.status != SCP_OK)
  186. goto exit;
  187. /* Validate SCP response */
  188. if (!CHECK_RESPONSE(response, cluster))
  189. goto exit;
  190. /* Extract power states for required cluster */
  191. power_state = *(((uint16_t *) SCPI_RES_PAYLOAD_SCP_TO_AP) + cluster);
  192. if (CLUSTER_ID(power_state) != cluster)
  193. goto exit;
  194. /* Update power state via pointers */
  195. if (cluster_state_p)
  196. *cluster_state_p = CLUSTER_POWER_STATE(power_state);
  197. if (cpu_state_p)
  198. *cpu_state_p = CPU_POWER_STATE(power_state);
  199. rc = 0;
  200. exit:
  201. scpi_secure_message_end();
  202. return rc;
  203. }
  204. uint32_t scpi_sys_power_state(scpi_system_state_t system_state)
  205. {
  206. scpi_cmd_t *cmd;
  207. uint8_t *payload_addr;
  208. scpi_cmd_t response;
  209. scpi_secure_message_start();
  210. /* Populate the command header */
  211. cmd = SCPI_CMD_HEADER_AP_TO_SCP;
  212. cmd->id = SCPI_CMD_SYS_POWER_STATE;
  213. cmd->set = 0;
  214. cmd->sender = 0;
  215. cmd->size = sizeof(*payload_addr);
  216. /* Populate the command payload */
  217. payload_addr = SCPI_CMD_PAYLOAD_AP_TO_SCP;
  218. *payload_addr = system_state & 0xff;
  219. scpi_secure_message_send(sizeof(*payload_addr));
  220. /* If no response is received, fill in an error status */
  221. if (scpi_secure_message_receive(&response) != 0)
  222. response.status = SCP_E_TIMEOUT;
  223. scpi_secure_message_end();
  224. return response.status;
  225. }