plat_psci.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2018-2021, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2022-2024, Advanced Micro Devices, Inc. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <common/debug.h>
  9. #include <lib/mmio.h>
  10. #include <lib/psci/psci.h>
  11. #include <plat/arm/common/plat_arm.h>
  12. #include <plat/common/platform.h>
  13. #include <plat_arm.h>
  14. #include "drivers/delay_timer.h"
  15. #include <plat_private.h>
  16. #include "pm_api_sys.h"
  17. #include "pm_client.h"
  18. #include <pm_common.h>
  19. #include "pm_ipi.h"
  20. #include "pm_svc_main.h"
  21. static uintptr_t versal_sec_entry;
  22. static int32_t versal_pwr_domain_on(u_register_t mpidr)
  23. {
  24. int32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
  25. const struct pm_proc *proc;
  26. VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr);
  27. if (cpu_id == -1) {
  28. return PSCI_E_INTERN_FAIL;
  29. }
  30. proc = pm_get_proc((uint32_t)cpu_id);
  31. if (proc == NULL) {
  32. return PSCI_E_INTERN_FAIL;
  33. }
  34. /* Send request to PMC to wake up selected ACPU core */
  35. (void)pm_req_wakeup(proc->node_id, (versal_sec_entry & 0xFFFFFFFFU) | 0x1U,
  36. versal_sec_entry >> 32, 0, SECURE_FLAG);
  37. /* Clear power down request */
  38. pm_client_wakeup(proc);
  39. return PSCI_E_SUCCESS;
  40. }
  41. /**
  42. * versal_pwr_domain_suspend() - This function sends request to PMC to suspend
  43. * core.
  44. * @target_state: Targated state.
  45. *
  46. */
  47. static void versal_pwr_domain_suspend(const psci_power_state_t *target_state)
  48. {
  49. uint32_t state;
  50. uint32_t cpu_id = plat_my_core_pos();
  51. const struct pm_proc *proc = pm_get_proc(cpu_id);
  52. if (proc == NULL) {
  53. return;
  54. }
  55. for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
  56. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  57. __func__, i, target_state->pwr_domain_state[i]);
  58. }
  59. plat_versal_gic_cpuif_disable();
  60. if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
  61. plat_versal_gic_save();
  62. }
  63. state = (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) ?
  64. PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE;
  65. /* Send request to PMC to suspend this core */
  66. (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_sec_entry,
  67. SECURE_FLAG);
  68. /* APU is to be turned off */
  69. if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
  70. /* disable coherency */
  71. plat_arm_interconnect_exit_coherency();
  72. }
  73. }
  74. /**
  75. * versal_pwr_domain_suspend_finish() - This function performs actions to finish
  76. * suspend procedure.
  77. * @target_state: Targated state.
  78. *
  79. */
  80. static void versal_pwr_domain_suspend_finish(
  81. const psci_power_state_t *target_state)
  82. {
  83. uint32_t cpu_id = plat_my_core_pos();
  84. const struct pm_proc *proc = pm_get_proc(cpu_id);
  85. if (proc == NULL) {
  86. return;
  87. }
  88. for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
  89. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  90. __func__, i, target_state->pwr_domain_state[i]);
  91. }
  92. /* Clear the APU power control register for this cpu */
  93. pm_client_wakeup(proc);
  94. /* enable coherency */
  95. plat_arm_interconnect_enter_coherency();
  96. /* APU was turned off, so restore GIC context */
  97. if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
  98. plat_versal_gic_resume();
  99. }
  100. plat_versal_gic_cpuif_enable();
  101. }
  102. static void versal_pwr_domain_on_finish(const psci_power_state_t *target_state)
  103. {
  104. /* Enable the gic cpu interface */
  105. plat_versal_gic_pcpu_init();
  106. /* Program the gic per-cpu distributor or re-distributor interface */
  107. plat_versal_gic_cpuif_enable();
  108. }
  109. /**
  110. * versal_system_off() - This function sends the system off request to firmware.
  111. * This function does not return.
  112. *
  113. */
  114. static void __dead2 versal_system_off(void)
  115. {
  116. /* Send the power down request to the PMC */
  117. (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN,
  118. pm_get_shutdown_scope(), SECURE_FLAG);
  119. while (true) {
  120. wfi();
  121. }
  122. }
  123. /**
  124. * versal_system_reset() - This function sends the reset request to firmware
  125. * for the system to reset. This function does not
  126. * return.
  127. *
  128. */
  129. static void __dead2 versal_system_reset(void)
  130. {
  131. uint32_t ret, timeout = 10000U;
  132. request_cpu_pwrdwn();
  133. /*
  134. * Send the system reset request to the firmware if power down request
  135. * is not received from firmware.
  136. */
  137. if (!pwrdwn_req_received) {
  138. (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET,
  139. pm_get_shutdown_scope(), SECURE_FLAG);
  140. /*
  141. * Wait for system shutdown request completed and idle callback
  142. * not received.
  143. */
  144. do {
  145. ret = ipi_mb_enquire_status(primary_proc->ipi->local_ipi_id,
  146. primary_proc->ipi->remote_ipi_id);
  147. udelay(100);
  148. timeout--;
  149. } while ((ret != IPI_MB_STATUS_RECV_PENDING) && (timeout > 0U));
  150. }
  151. (void)psci_cpu_off();
  152. while (true) {
  153. wfi();
  154. }
  155. }
  156. /**
  157. * versal_pwr_domain_off() - This function performs actions to turn off core.
  158. * @target_state: Targated state.
  159. *
  160. */
  161. static void versal_pwr_domain_off(const psci_power_state_t *target_state)
  162. {
  163. uint32_t ret, fw_api_version, version_type[RET_PAYLOAD_ARG_CNT] = {0U};
  164. uint32_t cpu_id = plat_my_core_pos();
  165. const struct pm_proc *proc = pm_get_proc(cpu_id);
  166. if (proc == NULL) {
  167. return;
  168. }
  169. for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
  170. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  171. __func__, i, target_state->pwr_domain_state[i]);
  172. }
  173. /* Prevent interrupts from spuriously waking up this cpu */
  174. plat_versal_gic_cpuif_disable();
  175. /*
  176. * Send request to PMC to power down the appropriate APU CPU
  177. * core.
  178. * According to PSCI specification, CPU_off function does not
  179. * have resume address and CPU core can only be woken up
  180. * invoking CPU_on function, during which resume address will
  181. * be set.
  182. */
  183. ret = pm_feature_check((uint32_t)PM_SELF_SUSPEND, &version_type[0], SECURE_FLAG);
  184. if (ret == PM_RET_SUCCESS) {
  185. fw_api_version = version_type[0] & 0xFFFFU;
  186. if (fw_api_version >= 3U) {
  187. (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_OFF, 0,
  188. SECURE_FLAG);
  189. } else {
  190. (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0,
  191. SECURE_FLAG);
  192. }
  193. }
  194. }
  195. /**
  196. * versal_validate_power_state() - This function ensures that the power state
  197. * parameter in request is valid.
  198. * @power_state: Power state of core.
  199. * @req_state: Requested state.
  200. *
  201. * Return: Returns status, either success or reason.
  202. *
  203. */
  204. static int32_t versal_validate_power_state(uint32_t power_state,
  205. psci_power_state_t *req_state)
  206. {
  207. VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
  208. uint32_t pstate = psci_get_pstate_type(power_state);
  209. assert(req_state != NULL);
  210. /* Sanity check the requested state */
  211. if (pstate == PSTATE_TYPE_STANDBY) {
  212. req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE;
  213. } else {
  214. req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE;
  215. }
  216. /* We expect the 'state id' to be zero */
  217. if (psci_get_pstate_id(power_state) != 0U) {
  218. return PSCI_E_INVALID_PARAMS;
  219. }
  220. return PSCI_E_SUCCESS;
  221. }
  222. /**
  223. * versal_get_sys_suspend_power_state() - Get power state for system suspend.
  224. * @req_state: Requested state.
  225. *
  226. */
  227. static void versal_get_sys_suspend_power_state(psci_power_state_t *req_state)
  228. {
  229. req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE;
  230. req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE;
  231. }
  232. static const struct plat_psci_ops versal_nopmc_psci_ops = {
  233. .pwr_domain_on = versal_pwr_domain_on,
  234. .pwr_domain_off = versal_pwr_domain_off,
  235. .pwr_domain_on_finish = versal_pwr_domain_on_finish,
  236. .pwr_domain_suspend = versal_pwr_domain_suspend,
  237. .pwr_domain_suspend_finish = versal_pwr_domain_suspend_finish,
  238. .system_off = versal_system_off,
  239. .system_reset = versal_system_reset,
  240. .validate_power_state = versal_validate_power_state,
  241. .get_sys_suspend_power_state = versal_get_sys_suspend_power_state,
  242. };
  243. /*******************************************************************************
  244. * Export the platform specific power ops.
  245. ******************************************************************************/
  246. int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint,
  247. const struct plat_psci_ops **psci_ops)
  248. {
  249. versal_sec_entry = sec_entrypoint;
  250. *psci_ops = &versal_nopmc_psci_ops;
  251. return 0;
  252. }