plat_psci.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2013-2022, 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 <errno.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <drivers/arm/gicv2.h>
  12. #include <lib/mmio.h>
  13. #include <lib/psci/psci.h>
  14. #include <plat/arm/common/plat_arm.h>
  15. #include <plat/common/platform.h>
  16. #include <plat_private.h>
  17. #include "pm_client.h"
  18. #include "zynqmp_pm_api_sys.h"
  19. static uintptr_t zynqmp_sec_entry;
  20. static void zynqmp_cpu_standby(plat_local_state_t cpu_state)
  21. {
  22. VERBOSE("%s: cpu_state: 0x%x\n", __func__, cpu_state);
  23. dsb();
  24. wfi();
  25. }
  26. static int32_t zynqmp_pwr_domain_on(u_register_t mpidr)
  27. {
  28. uint32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
  29. const struct pm_proc *proc;
  30. uint32_t buff[3];
  31. enum pm_ret_status ret;
  32. VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr);
  33. if (cpu_id == -1) {
  34. return PSCI_E_INTERN_FAIL;
  35. }
  36. proc = pm_get_proc(cpu_id);
  37. if (proc == NULL) {
  38. return PSCI_E_INTERN_FAIL;
  39. }
  40. /* Check the APU proc status before wakeup */
  41. ret = pm_get_node_status(proc->node_id, buff);
  42. if ((ret != PM_RET_SUCCESS) || (buff[0] == PM_PROC_STATE_SUSPENDING)) {
  43. return PSCI_E_INTERN_FAIL;
  44. }
  45. /* Clear power down request */
  46. pm_client_wakeup(proc);
  47. /* Send request to PMU to wake up selected APU CPU core */
  48. (void)pm_req_wakeup(proc->node_id, 1, zynqmp_sec_entry, REQ_ACK_BLOCKING);
  49. return PSCI_E_SUCCESS;
  50. }
  51. static void zynqmp_pwr_domain_off(const psci_power_state_t *target_state)
  52. {
  53. uint32_t cpu_id = plat_my_core_pos();
  54. const struct pm_proc *proc = pm_get_proc(cpu_id);
  55. if (proc == NULL) {
  56. return;
  57. }
  58. for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) {
  59. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  60. __func__, i, target_state->pwr_domain_state[i]);
  61. }
  62. /* Prevent interrupts from spuriously waking up this cpu */
  63. gicv2_cpuif_disable();
  64. /*
  65. * Send request to PMU to power down the appropriate APU CPU
  66. * core.
  67. * According to PSCI specification, CPU_off function does not
  68. * have resume address and CPU core can only be woken up
  69. * invoking CPU_on function, during which resume address will
  70. * be set.
  71. */
  72. (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0);
  73. }
  74. static void zynqmp_pwr_domain_suspend(const psci_power_state_t *target_state)
  75. {
  76. uint32_t state;
  77. uint32_t cpu_id = plat_my_core_pos();
  78. const struct pm_proc *proc = pm_get_proc(cpu_id);
  79. if (proc == NULL) {
  80. return;
  81. }
  82. for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++)
  83. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  84. __func__, i, target_state->pwr_domain_state[i]);
  85. state = (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) ?
  86. PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE;
  87. /* Send request to PMU to suspend this core */
  88. (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, zynqmp_sec_entry);
  89. /* APU is to be turned off */
  90. if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
  91. /* disable coherency */
  92. plat_arm_interconnect_exit_coherency();
  93. }
  94. }
  95. static void zynqmp_pwr_domain_on_finish(const psci_power_state_t *target_state)
  96. {
  97. for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) {
  98. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  99. __func__, i, target_state->pwr_domain_state[i]);
  100. }
  101. plat_arm_gic_pcpu_init();
  102. gicv2_cpuif_enable();
  103. }
  104. static void zynqmp_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  105. {
  106. uint32_t cpu_id = plat_my_core_pos();
  107. const struct pm_proc *proc = pm_get_proc(cpu_id);
  108. if (proc == NULL) {
  109. return;
  110. }
  111. for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) {
  112. VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
  113. __func__, i, target_state->pwr_domain_state[i]);
  114. }
  115. /* Clear the APU power control register for this cpu */
  116. pm_client_wakeup(proc);
  117. /* enable coherency */
  118. plat_arm_interconnect_enter_coherency();
  119. /* APU was turned off */
  120. if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
  121. plat_arm_gic_init();
  122. } else {
  123. gicv2_cpuif_enable();
  124. gicv2_pcpu_distif_init();
  125. }
  126. }
  127. /*******************************************************************************
  128. * ZynqMP handlers to shutdown/reboot the system
  129. ******************************************************************************/
  130. static void __dead2 zynqmp_system_off(void)
  131. {
  132. /* disable coherency */
  133. plat_arm_interconnect_exit_coherency();
  134. /* Send the power down request to the PMU */
  135. (void)pm_system_shutdown((uint32_t)PMF_SHUTDOWN_TYPE_SHUTDOWN,
  136. pm_get_shutdown_scope());
  137. while (true) {
  138. wfi();
  139. }
  140. }
  141. static void __dead2 zynqmp_system_reset(void)
  142. {
  143. /* disable coherency */
  144. plat_arm_interconnect_exit_coherency();
  145. /* Send the system reset request to the PMU */
  146. (void)pm_system_shutdown((uint32_t)PMF_SHUTDOWN_TYPE_RESET,
  147. pm_get_shutdown_scope());
  148. while (true) {
  149. wfi();
  150. }
  151. }
  152. static int32_t zynqmp_validate_power_state(uint32_t power_state,
  153. psci_power_state_t *req_state)
  154. {
  155. VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
  156. uint32_t pstate = psci_get_pstate_type(power_state);
  157. assert(req_state);
  158. /* Sanity check the requested state */
  159. if (pstate == PSTATE_TYPE_STANDBY) {
  160. req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE;
  161. } else {
  162. req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE;
  163. }
  164. /* We expect the 'state id' to be zero */
  165. if (psci_get_pstate_id(power_state) != 0U) {
  166. return PSCI_E_INVALID_PARAMS;
  167. }
  168. return PSCI_E_SUCCESS;
  169. }
  170. static void zynqmp_get_sys_suspend_power_state(psci_power_state_t *req_state)
  171. {
  172. req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE;
  173. req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE;
  174. }
  175. /*******************************************************************************
  176. * Export the platform handlers to enable psci to invoke them
  177. ******************************************************************************/
  178. static const struct plat_psci_ops zynqmp_psci_ops = {
  179. .cpu_standby = zynqmp_cpu_standby,
  180. .pwr_domain_on = zynqmp_pwr_domain_on,
  181. .pwr_domain_off = zynqmp_pwr_domain_off,
  182. .pwr_domain_suspend = zynqmp_pwr_domain_suspend,
  183. .pwr_domain_on_finish = zynqmp_pwr_domain_on_finish,
  184. .pwr_domain_suspend_finish = zynqmp_pwr_domain_suspend_finish,
  185. .system_off = zynqmp_system_off,
  186. .system_reset = zynqmp_system_reset,
  187. .validate_power_state = zynqmp_validate_power_state,
  188. .get_sys_suspend_power_state = zynqmp_get_sys_suspend_power_state,
  189. };
  190. /*******************************************************************************
  191. * Export the platform specific power ops.
  192. ******************************************************************************/
  193. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  194. const struct plat_psci_ops **psci_ops)
  195. {
  196. zynqmp_sec_entry = sec_entrypoint;
  197. *psci_ops = &zynqmp_psci_ops;
  198. return 0;
  199. }