plat_psci_pm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2022, Xilinx, Inc. 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. #include "versal_net_def.h"
  22. static uintptr_t versal_net_sec_entry;
  23. static int32_t versal_net_pwr_domain_on(u_register_t mpidr)
  24. {
  25. uint32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
  26. const struct pm_proc *proc;
  27. VERBOSE("%s: mpidr: 0x%lx, cpuid: %x\n",
  28. __func__, mpidr, cpu_id);
  29. if (cpu_id == -1) {
  30. return PSCI_E_INTERN_FAIL;
  31. }
  32. proc = pm_get_proc(cpu_id);
  33. if (proc == NULL) {
  34. return PSCI_E_INTERN_FAIL;
  35. }
  36. (void)pm_req_wakeup(proc->node_id, (versal_net_sec_entry & 0xFFFFFFFFU) | 0x1U,
  37. versal_net_sec_entry >> 32, 0, 0);
  38. /* Clear power down request */
  39. pm_client_wakeup(proc);
  40. return PSCI_E_SUCCESS;
  41. }
  42. /**
  43. * versal_net_pwr_domain_off() - This function performs actions to turn off
  44. * core.
  45. * @target_state: Targeted state.
  46. *
  47. */
  48. static void versal_net_pwr_domain_off(const psci_power_state_t *target_state)
  49. {
  50. uint32_t ret, fw_api_version, version_type[RET_PAYLOAD_ARG_CNT] = {0U};
  51. uint32_t cpu_id = plat_my_core_pos();
  52. const struct pm_proc *proc = pm_get_proc(cpu_id);
  53. if (proc == NULL) {
  54. return;
  55. }
  56. for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) {
  57. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  58. __func__, i, target_state->pwr_domain_state[i]);
  59. }
  60. /* Prevent interrupts from spuriously waking up this cpu */
  61. plat_arm_gic_cpuif_disable();
  62. /*
  63. * Send request to PMC to power down the appropriate APU CPU
  64. * core.
  65. * According to PSCI specification, CPU_off function does not
  66. * have resume address and CPU core can only be woken up
  67. * invoking CPU_on function, during which resume address will
  68. * be set.
  69. */
  70. ret = pm_feature_check((uint32_t)PM_SELF_SUSPEND, &version_type[0], SECURE_FLAG);
  71. if (ret == PM_RET_SUCCESS) {
  72. fw_api_version = version_type[0] & 0xFFFFU;
  73. if (fw_api_version >= 3U) {
  74. (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_OFF, 0,
  75. SECURE_FLAG);
  76. } else {
  77. (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0,
  78. SECURE_FLAG);
  79. }
  80. }
  81. }
  82. /**
  83. * versal_net_system_reset() - This function sends the reset request to firmware
  84. * for the system to reset. This function does not
  85. * return.
  86. *
  87. */
  88. static void __dead2 versal_net_system_reset(void)
  89. {
  90. uint32_t ret, timeout = 10000U;
  91. request_cpu_pwrdwn();
  92. /*
  93. * Send the system reset request to the firmware if power down request
  94. * is not received from firmware.
  95. */
  96. if (!pwrdwn_req_received) {
  97. (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET,
  98. pm_get_shutdown_scope(), SECURE_FLAG);
  99. /*
  100. * Wait for system shutdown request completed and idle callback
  101. * not received.
  102. */
  103. do {
  104. ret = ipi_mb_enquire_status(primary_proc->ipi->local_ipi_id,
  105. primary_proc->ipi->remote_ipi_id);
  106. udelay(100);
  107. timeout--;
  108. } while ((ret != IPI_MB_STATUS_RECV_PENDING) && (timeout > 0U));
  109. }
  110. (void)psci_cpu_off();
  111. while (true) {
  112. wfi();
  113. }
  114. }
  115. /**
  116. * versal_net_pwr_domain_suspend() - This function sends request to PMC to suspend
  117. * core.
  118. * @target_state: Targeted state.
  119. *
  120. */
  121. static void versal_net_pwr_domain_suspend(const psci_power_state_t *target_state)
  122. {
  123. uint32_t state;
  124. uint32_t cpu_id = plat_my_core_pos();
  125. const struct pm_proc *proc = pm_get_proc(cpu_id);
  126. if (proc == NULL) {
  127. return;
  128. }
  129. for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) {
  130. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  131. __func__, i, target_state->pwr_domain_state[i]);
  132. }
  133. plat_arm_gic_cpuif_disable();
  134. if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
  135. plat_arm_gic_save();
  136. }
  137. state = (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) ?
  138. PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE;
  139. /* Send request to PMC to suspend this core */
  140. (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_net_sec_entry,
  141. SECURE_FLAG);
  142. /* TODO: disable coherency */
  143. }
  144. static void versal_net_pwr_domain_on_finish(const psci_power_state_t *target_state)
  145. {
  146. (void)target_state;
  147. /* Enable the gic cpu interface */
  148. plat_arm_gic_pcpu_init();
  149. /* Program the gic per-cpu distributor or re-distributor interface */
  150. plat_arm_gic_cpuif_enable();
  151. }
  152. /**
  153. * versal_net_pwr_domain_suspend_finish() - This function performs actions to finish
  154. * suspend procedure.
  155. * @target_state: Targeted state.
  156. *
  157. */
  158. static void versal_net_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  159. {
  160. uint32_t cpu_id = plat_my_core_pos();
  161. const struct pm_proc *proc = pm_get_proc(cpu_id);
  162. if (proc == NULL) {
  163. return;
  164. }
  165. for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++)
  166. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  167. __func__, i, target_state->pwr_domain_state[i]);
  168. /* Clear the APU power control register for this cpu */
  169. pm_client_wakeup(proc);
  170. /* TODO: enable coherency */
  171. /* APU was turned off, so restore GIC context */
  172. if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
  173. plat_arm_gic_resume();
  174. }
  175. plat_arm_gic_cpuif_enable();
  176. }
  177. /**
  178. * versal_net_system_off() - This function sends the system off request
  179. * to firmware. This function does not return.
  180. *
  181. */
  182. static void __dead2 versal_net_system_off(void)
  183. {
  184. /* Send the power down request to the PMC */
  185. (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN,
  186. pm_get_shutdown_scope(), SECURE_FLAG);
  187. while (true) {
  188. wfi();
  189. }
  190. }
  191. /**
  192. * versal_net_validate_power_state() - This function ensures that the power state
  193. * parameter in request is valid.
  194. * @power_state: Power state of core.
  195. * @req_state: Requested state.
  196. *
  197. * Return: Returns status, either PSCI_E_SUCCESS or reason.
  198. *
  199. */
  200. static int32_t versal_net_validate_power_state(unsigned int power_state,
  201. psci_power_state_t *req_state)
  202. {
  203. VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
  204. int32_t pstate = psci_get_pstate_type(power_state);
  205. assert(req_state != NULL);
  206. /* Sanity check the requested state */
  207. if (pstate == PSTATE_TYPE_STANDBY) {
  208. req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE;
  209. } else {
  210. req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE;
  211. }
  212. /* We expect the 'state id' to be zero */
  213. if (psci_get_pstate_id(power_state) != 0U) {
  214. return PSCI_E_INVALID_PARAMS;
  215. }
  216. return PSCI_E_SUCCESS;
  217. }
  218. /**
  219. * versal_net_get_sys_suspend_power_state() - Get power state for system
  220. * suspend.
  221. * @req_state: Requested state.
  222. *
  223. */
  224. static void versal_net_get_sys_suspend_power_state(psci_power_state_t *req_state)
  225. {
  226. uint64_t i;
  227. for (i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
  228. req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
  229. }
  230. static const struct plat_psci_ops versal_net_nopmc_psci_ops = {
  231. .pwr_domain_on = versal_net_pwr_domain_on,
  232. .pwr_domain_off = versal_net_pwr_domain_off,
  233. .pwr_domain_on_finish = versal_net_pwr_domain_on_finish,
  234. .pwr_domain_suspend = versal_net_pwr_domain_suspend,
  235. .pwr_domain_suspend_finish = versal_net_pwr_domain_suspend_finish,
  236. .system_off = versal_net_system_off,
  237. .system_reset = versal_net_system_reset,
  238. .validate_power_state = versal_net_validate_power_state,
  239. .get_sys_suspend_power_state = versal_net_get_sys_suspend_power_state,
  240. };
  241. /*******************************************************************************
  242. * Export the platform specific power ops.
  243. ******************************************************************************/
  244. int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint,
  245. const struct plat_psci_ops **psci_ops)
  246. {
  247. versal_net_sec_entry = sec_entrypoint;
  248. VERBOSE("Setting up entry point %lx\n", versal_net_sec_entry);
  249. *psci_ops = &versal_net_nopmc_psci_ops;
  250. return 0;
  251. }
  252. int32_t sip_svc_setup_init(void)
  253. {
  254. return pm_setup();
  255. }
  256. uint64_t smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4,
  257. void *cookie, void *handle, uint64_t flags)
  258. {
  259. return pm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags);
  260. }