qemu_pm.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <platform_def.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <lib/psci/psci.h>
  11. #include <lib/semihosting.h>
  12. #include <plat/common/platform.h>
  13. #include <drivers/gpio.h>
  14. #include "qemu_private.h"
  15. #define ADP_STOPPED_APPLICATION_EXIT 0x20026
  16. /*
  17. * The secure entry point to be used on warm reset.
  18. */
  19. static unsigned long secure_entrypoint;
  20. /* Make composite power state parameter till power level 0 */
  21. #if PSCI_EXTENDED_STATE_ID
  22. #define qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
  23. (((lvl0_state) << PSTATE_ID_SHIFT) | \
  24. ((type) << PSTATE_TYPE_SHIFT))
  25. #else
  26. #define qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
  27. (((lvl0_state) << PSTATE_ID_SHIFT) | \
  28. ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
  29. ((type) << PSTATE_TYPE_SHIFT))
  30. #endif /* PSCI_EXTENDED_STATE_ID */
  31. #define qemu_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
  32. (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \
  33. qemu_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
  34. /*
  35. * The table storing the valid idle power states. Ensure that the
  36. * array entries are populated in ascending order of state-id to
  37. * enable us to use binary search during power state validation.
  38. * The table must be terminated by a NULL entry.
  39. */
  40. static const unsigned int qemu_pm_idle_states[] = {
  41. /* State-id - 0x01 */
  42. qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET,
  43. MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
  44. /* State-id - 0x02 */
  45. qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF,
  46. MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
  47. /* State-id - 0x22 */
  48. qemu_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF,
  49. MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN),
  50. 0,
  51. };
  52. /*******************************************************************************
  53. * Platform handler called to check the validity of the power state
  54. * parameter. The power state parameter has to be a composite power state.
  55. ******************************************************************************/
  56. static int qemu_validate_power_state(unsigned int power_state,
  57. psci_power_state_t *req_state)
  58. {
  59. unsigned int state_id;
  60. int i;
  61. assert(req_state);
  62. /*
  63. * Currently we are using a linear search for finding the matching
  64. * entry in the idle power state array. This can be made a binary
  65. * search if the number of entries justify the additional complexity.
  66. */
  67. for (i = 0; !!qemu_pm_idle_states[i]; i++) {
  68. if (power_state == qemu_pm_idle_states[i])
  69. break;
  70. }
  71. /* Return error if entry not found in the idle state array */
  72. if (!qemu_pm_idle_states[i])
  73. return PSCI_E_INVALID_PARAMS;
  74. i = 0;
  75. state_id = psci_get_pstate_id(power_state);
  76. /* Parse the State ID and populate the state info parameter */
  77. while (state_id) {
  78. req_state->pwr_domain_state[i++] = state_id &
  79. PLAT_LOCAL_PSTATE_MASK;
  80. state_id >>= PLAT_LOCAL_PSTATE_WIDTH;
  81. }
  82. return PSCI_E_SUCCESS;
  83. }
  84. /*******************************************************************************
  85. * Platform handler called when a CPU is about to enter standby.
  86. ******************************************************************************/
  87. static void qemu_cpu_standby(plat_local_state_t cpu_state)
  88. {
  89. assert(cpu_state == PLAT_LOCAL_STATE_RET);
  90. /*
  91. * Enter standby state
  92. * dsb is good practice before using wfi to enter low power states
  93. */
  94. dsb();
  95. wfi();
  96. }
  97. /*******************************************************************************
  98. * Platform handler called when a power domain is about to be turned on. The
  99. * mpidr determines the CPU to be turned on.
  100. ******************************************************************************/
  101. static int qemu_pwr_domain_on(u_register_t mpidr)
  102. {
  103. int rc = PSCI_E_SUCCESS;
  104. unsigned pos = plat_core_pos_by_mpidr(mpidr);
  105. uint64_t *hold_base = (uint64_t *)PLAT_QEMU_HOLD_BASE;
  106. hold_base[pos] = PLAT_QEMU_HOLD_STATE_GO;
  107. sev();
  108. return rc;
  109. }
  110. /*******************************************************************************
  111. * Platform handler called when a power domain is about to be turned off. The
  112. * target_state encodes the power state that each level should transition to.
  113. ******************************************************************************/
  114. static void qemu_pwr_domain_off(const psci_power_state_t *target_state)
  115. {
  116. qemu_pwr_gic_off();
  117. }
  118. void __dead2 plat_secondary_cold_boot_setup(void);
  119. static void __dead2
  120. qemu_pwr_domain_pwr_down_wfi(const psci_power_state_t *target_state)
  121. {
  122. disable_mmu_el3();
  123. plat_secondary_cold_boot_setup();
  124. }
  125. /*******************************************************************************
  126. * Platform handler called when a power domain is about to be suspended. The
  127. * target_state encodes the power state that each level should transition to.
  128. ******************************************************************************/
  129. void qemu_pwr_domain_suspend(const psci_power_state_t *target_state)
  130. {
  131. assert(0);
  132. }
  133. /*******************************************************************************
  134. * Platform handler called when a power domain has just been powered on after
  135. * being turned off earlier. The target_state encodes the low power state that
  136. * each level has woken up from.
  137. ******************************************************************************/
  138. void qemu_pwr_domain_on_finish(const psci_power_state_t *target_state)
  139. {
  140. assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
  141. PLAT_LOCAL_STATE_OFF);
  142. qemu_pwr_gic_on_finish();
  143. }
  144. /*******************************************************************************
  145. * Platform handler called when a power domain has just been powered on after
  146. * having been suspended earlier. The target_state encodes the low power state
  147. * that each level has woken up from.
  148. ******************************************************************************/
  149. void qemu_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  150. {
  151. assert(0);
  152. }
  153. /*******************************************************************************
  154. * Platform handlers to shutdown/reboot the system
  155. ******************************************************************************/
  156. static void __dead2 qemu_system_off(void)
  157. {
  158. #ifdef SECURE_GPIO_BASE
  159. ERROR("QEMU System Power off: with GPIO.\n");
  160. gpio_set_direction(SECURE_GPIO_POWEROFF, GPIO_DIR_OUT);
  161. gpio_set_value(SECURE_GPIO_POWEROFF, GPIO_LEVEL_LOW);
  162. gpio_set_value(SECURE_GPIO_POWEROFF, GPIO_LEVEL_HIGH);
  163. #else
  164. semihosting_exit(ADP_STOPPED_APPLICATION_EXIT, 0);
  165. ERROR("QEMU System Off: semihosting call unexpectedly returned.\n");
  166. #endif
  167. panic();
  168. }
  169. static void __dead2 qemu_system_reset(void)
  170. {
  171. ERROR("QEMU System Reset: with GPIO.\n");
  172. #ifdef SECURE_GPIO_BASE
  173. gpio_set_direction(SECURE_GPIO_RESET, GPIO_DIR_OUT);
  174. gpio_set_value(SECURE_GPIO_RESET, GPIO_LEVEL_LOW);
  175. gpio_set_value(SECURE_GPIO_RESET, GPIO_LEVEL_HIGH);
  176. #else
  177. ERROR("QEMU System Reset: operation not handled.\n");
  178. #endif
  179. panic();
  180. }
  181. static const plat_psci_ops_t plat_qemu_psci_pm_ops = {
  182. .cpu_standby = qemu_cpu_standby,
  183. .pwr_domain_on = qemu_pwr_domain_on,
  184. .pwr_domain_off = qemu_pwr_domain_off,
  185. .pwr_domain_pwr_down_wfi = qemu_pwr_domain_pwr_down_wfi,
  186. .pwr_domain_suspend = qemu_pwr_domain_suspend,
  187. .pwr_domain_on_finish = qemu_pwr_domain_on_finish,
  188. .pwr_domain_suspend_finish = qemu_pwr_domain_suspend_finish,
  189. .system_off = qemu_system_off,
  190. .system_reset = qemu_system_reset,
  191. .validate_power_state = qemu_validate_power_state,
  192. };
  193. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  194. const plat_psci_ops_t **psci_ops)
  195. {
  196. uintptr_t *mailbox = (void *) PLAT_QEMU_TRUSTED_MAILBOX_BASE;
  197. *mailbox = sec_entrypoint;
  198. secure_entrypoint = (unsigned long) sec_entrypoint;
  199. *psci_ops = &plat_qemu_psci_pm_ops;
  200. return 0;
  201. }