spmc_pm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2022-2023, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <lib/el3_runtime/context_mgmt.h>
  9. #include <lib/spinlock.h>
  10. #include <plat/common/common_def.h>
  11. #include <plat/common/platform.h>
  12. #include <services/ffa_svc.h>
  13. #include "spmc.h"
  14. #include <platform_def.h>
  15. /*******************************************************************************
  16. * spmc_build_pm_message
  17. *
  18. * Builds an SPMC to SP direct message request.
  19. ******************************************************************************/
  20. static void spmc_build_pm_message(gp_regs_t *gpregs,
  21. unsigned long long message,
  22. uint8_t pm_msg_type,
  23. uint16_t sp_id)
  24. {
  25. write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
  26. write_ctx_reg(gpregs, CTX_GPREG_X1,
  27. (FFA_SPMC_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
  28. sp_id);
  29. write_ctx_reg(gpregs, CTX_GPREG_X2, FFA_FWK_MSG_BIT |
  30. (pm_msg_type & FFA_FWK_MSG_MASK));
  31. write_ctx_reg(gpregs, CTX_GPREG_X3, message);
  32. }
  33. /*******************************************************************************
  34. * This CPU has been turned on. Enter the SP to initialise S-EL0 or S-EL1.
  35. ******************************************************************************/
  36. static void spmc_cpu_on_finish_handler(u_register_t unused)
  37. {
  38. struct secure_partition_desc *sp = spmc_get_current_sp_ctx();
  39. struct sp_exec_ctx *ec;
  40. unsigned int linear_id = plat_my_core_pos();
  41. entry_point_info_t sec_ec_ep_info = {0};
  42. uint64_t rc;
  43. /* Sanity check for a NULL pointer dereference. */
  44. assert(sp != NULL);
  45. /* Obtain a reference to the SP execution context */
  46. ec = &sp->ec[get_ec_index(sp)];
  47. /*
  48. * In case of a S-EL0 SP, only initialise the context data structure for
  49. * the secure world on this cpu and return.
  50. */
  51. if (sp->runtime_el == S_EL0) {
  52. /* Assign the context of the SP to this CPU */
  53. cm_set_context(&(ec->cpu_ctx), SECURE);
  54. return;
  55. }
  56. /* Initialize entry point information for the SP. */
  57. SET_PARAM_HEAD(&sec_ec_ep_info, PARAM_EP, VERSION_1,
  58. SECURE | EP_ST_ENABLE);
  59. /*
  60. * Check if the primary execution context registered an entry point else
  61. * bail out early.
  62. * TODO: Add support for boot reason in manifest to allow jumping to
  63. * entrypoint into the primary execution context.
  64. */
  65. if (sp->secondary_ep == 0) {
  66. WARN("%s: No secondary ep on core%u\n", __func__, linear_id);
  67. return;
  68. }
  69. sec_ec_ep_info.pc = sp->secondary_ep;
  70. /*
  71. * Setup and initialise the SP execution context on this physical cpu.
  72. */
  73. spmc_el1_sp_setup(sp, &sec_ec_ep_info);
  74. spmc_sp_common_ep_commit(sp, &sec_ec_ep_info);
  75. /* Obtain a reference to the SP execution context. */
  76. ec = spmc_get_sp_ec(sp);
  77. /*
  78. * TODO: Should we do some PM related state tracking of the SP execution
  79. * context here?
  80. */
  81. /* Update the runtime model and state of the partition. */
  82. ec->rt_model = RT_MODEL_INIT;
  83. ec->rt_state = RT_STATE_RUNNING;
  84. ec->dir_req_origin_id = INV_SP_ID;
  85. INFO("SP (0x%x) init start on core%u.\n", sp->sp_id, linear_id);
  86. rc = spmc_sp_synchronous_entry(ec);
  87. if (rc != 0ULL) {
  88. ERROR("%s failed (%lu) on CPU%u\n", __func__, rc, linear_id);
  89. }
  90. /* Update the runtime state of the partition. */
  91. ec->rt_state = RT_STATE_WAITING;
  92. VERBOSE("CPU %u on!\n", linear_id);
  93. }
  94. /*******************************************************************************
  95. * Helper function to send a FF-A power management message to an SP.
  96. ******************************************************************************/
  97. static int32_t spmc_send_pm_msg(uint8_t pm_msg_type,
  98. unsigned long long psci_event)
  99. {
  100. struct secure_partition_desc *sp = spmc_get_current_sp_ctx();
  101. struct sp_exec_ctx *ec;
  102. gp_regs_t *gpregs_ctx;
  103. unsigned int linear_id = plat_my_core_pos();
  104. u_register_t resp;
  105. uint64_t rc;
  106. /* Obtain a reference to the SP execution context. */
  107. ec = spmc_get_sp_ec(sp);
  108. /*
  109. * TODO: Should we do some PM related state tracking of the SP execution
  110. * context here?
  111. */
  112. /*
  113. * Build an SPMC to SP direct message request.
  114. * Note that x4-x6 should be populated with the original PSCI arguments.
  115. */
  116. spmc_build_pm_message(get_gpregs_ctx(&ec->cpu_ctx),
  117. psci_event,
  118. pm_msg_type,
  119. sp->sp_id);
  120. /* Sanity check partition state. */
  121. assert(ec->rt_state == RT_STATE_WAITING);
  122. /* Update the runtime model and state of the partition. */
  123. ec->rt_model = RT_MODEL_DIR_REQ;
  124. ec->rt_state = RT_STATE_RUNNING;
  125. ec->dir_req_origin_id = FFA_SPMC_ID;
  126. rc = spmc_sp_synchronous_entry(ec);
  127. if (rc != 0ULL) {
  128. ERROR("%s failed (%lu) on CPU%u.\n", __func__, rc, linear_id);
  129. assert(false);
  130. return -EINVAL;
  131. }
  132. /*
  133. * Validate we receive an expected response from the SP.
  134. * TODO: We don't currently support aborting an SP in the scenario
  135. * where it is misbehaving so assert these conditions are not
  136. * met for now.
  137. */
  138. gpregs_ctx = get_gpregs_ctx(&ec->cpu_ctx);
  139. /* Expect a direct message response from the SP. */
  140. resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X0);
  141. if (resp != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
  142. ERROR("%s invalid SP response (%lx).\n", __func__, resp);
  143. assert(false);
  144. return -EINVAL;
  145. }
  146. /* Ensure the sender and receiver are populated correctly. */
  147. resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X1);
  148. if (!(ffa_endpoint_source(resp) == sp->sp_id &&
  149. ffa_endpoint_destination(resp) == FFA_SPMC_ID)) {
  150. ERROR("%s invalid src/dst response (%lx).\n", __func__, resp);
  151. assert(false);
  152. return -EINVAL;
  153. }
  154. /* Expect a PM message response from the SP. */
  155. resp = read_ctx_reg(gpregs_ctx, CTX_GPREG_X2);
  156. if ((resp & FFA_FWK_MSG_BIT) == 0U ||
  157. ((resp & FFA_FWK_MSG_MASK) != FFA_PM_MSG_PM_RESP)) {
  158. ERROR("%s invalid PM response (%lx).\n", __func__, resp);
  159. assert(false);
  160. return -EINVAL;
  161. }
  162. /* Update the runtime state of the partition. */
  163. ec->rt_state = RT_STATE_WAITING;
  164. /* Return the status code returned by the SP */
  165. return read_ctx_reg(gpregs_ctx, CTX_GPREG_X3);
  166. }
  167. /*******************************************************************************
  168. * spmc_cpu_suspend_finish_handler
  169. ******************************************************************************/
  170. static void spmc_cpu_suspend_finish_handler(u_register_t unused)
  171. {
  172. struct secure_partition_desc *sp = spmc_get_current_sp_ctx();
  173. unsigned int linear_id = plat_my_core_pos();
  174. int32_t rc;
  175. /* Sanity check for a NULL pointer dereference. */
  176. assert(sp != NULL);
  177. /*
  178. * Check if the SP has subscribed for this power management message.
  179. * If not then we don't have anything else to do here.
  180. */
  181. if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_SUSPEND_RESUME) == 0U) {
  182. goto exit;
  183. }
  184. rc = spmc_send_pm_msg(FFA_PM_MSG_WB_REQ, FFA_WB_TYPE_NOTS2RAM);
  185. if (rc < 0) {
  186. ERROR("%s failed (%d) on CPU%u\n", __func__, rc, linear_id);
  187. return;
  188. }
  189. exit:
  190. VERBOSE("CPU %u resumed!\n", linear_id);
  191. }
  192. /*******************************************************************************
  193. * spmc_cpu_suspend_handler
  194. ******************************************************************************/
  195. static void spmc_cpu_suspend_handler(u_register_t unused)
  196. {
  197. struct secure_partition_desc *sp = spmc_get_current_sp_ctx();
  198. unsigned int linear_id = plat_my_core_pos();
  199. int32_t rc;
  200. /* Sanity check for a NULL pointer dereference. */
  201. assert(sp != NULL);
  202. /*
  203. * Check if the SP has subscribed for this power management message.
  204. * If not then we don't have anything else to do here.
  205. */
  206. if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_SUSPEND) == 0U) {
  207. goto exit;
  208. }
  209. rc = spmc_send_pm_msg(FFA_FWK_MSG_PSCI, PSCI_CPU_SUSPEND_AARCH64);
  210. if (rc < 0) {
  211. ERROR("%s failed (%d) on CPU%u\n", __func__, rc, linear_id);
  212. return;
  213. }
  214. exit:
  215. VERBOSE("CPU %u suspend!\n", linear_id);
  216. }
  217. /*******************************************************************************
  218. * spmc_cpu_off_handler
  219. ******************************************************************************/
  220. static int32_t spmc_cpu_off_handler(u_register_t unused)
  221. {
  222. struct secure_partition_desc *sp = spmc_get_current_sp_ctx();
  223. unsigned int linear_id = plat_my_core_pos();
  224. int32_t ret = 0;
  225. /* Sanity check for a NULL pointer dereference. */
  226. assert(sp != NULL);
  227. /*
  228. * Check if the SP has subscribed for this power management message.
  229. * If not then we don't have anything else to do here.
  230. */
  231. if ((sp->pwr_mgmt_msgs & FFA_PM_MSG_SUB_CPU_OFF) == 0U) {
  232. goto exit;
  233. }
  234. ret = spmc_send_pm_msg(FFA_FWK_MSG_PSCI, PSCI_CPU_OFF);
  235. if (ret < 0) {
  236. ERROR("%s failed (%d) on CPU%u\n", __func__, ret, linear_id);
  237. return ret;
  238. }
  239. exit:
  240. VERBOSE("CPU %u off!\n", linear_id);
  241. return ret;
  242. }
  243. /*******************************************************************************
  244. * Structure populated by the SPM Core to perform any bookkeeping before
  245. * PSCI executes a power mgmt. operation.
  246. ******************************************************************************/
  247. const spd_pm_ops_t spmc_pm = {
  248. .svc_on_finish = spmc_cpu_on_finish_handler,
  249. .svc_off = spmc_cpu_off_handler,
  250. .svc_suspend = spmc_cpu_suspend_handler,
  251. .svc_suspend_finish = spmc_cpu_suspend_finish_handler
  252. };