qemu_pm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 to check the validity of the non secure
  86. * entrypoint.
  87. ******************************************************************************/
  88. static int qemu_validate_ns_entrypoint(uintptr_t entrypoint)
  89. {
  90. /*
  91. * Check if the non secure entrypoint lies within the non
  92. * secure DRAM.
  93. */
  94. if ((entrypoint >= NS_DRAM0_BASE) &&
  95. (entrypoint < (NS_DRAM0_BASE + NS_DRAM0_SIZE)))
  96. return PSCI_E_SUCCESS;
  97. return PSCI_E_INVALID_ADDRESS;
  98. }
  99. /*******************************************************************************
  100. * Platform handler called when a CPU is about to enter standby.
  101. ******************************************************************************/
  102. static void qemu_cpu_standby(plat_local_state_t cpu_state)
  103. {
  104. assert(cpu_state == PLAT_LOCAL_STATE_RET);
  105. /*
  106. * Enter standby state
  107. * dsb is good practice before using wfi to enter low power states
  108. */
  109. dsb();
  110. wfi();
  111. }
  112. /*******************************************************************************
  113. * Platform handler called when a power domain is about to be turned on. The
  114. * mpidr determines the CPU to be turned on.
  115. ******************************************************************************/
  116. static int qemu_pwr_domain_on(u_register_t mpidr)
  117. {
  118. int rc = PSCI_E_SUCCESS;
  119. unsigned pos = plat_core_pos_by_mpidr(mpidr);
  120. uint64_t *hold_base = (uint64_t *)PLAT_QEMU_HOLD_BASE;
  121. hold_base[pos] = PLAT_QEMU_HOLD_STATE_GO;
  122. sev();
  123. return rc;
  124. }
  125. /*******************************************************************************
  126. * Platform handler called when a power domain is about to be turned off. The
  127. * target_state encodes the power state that each level should transition to.
  128. ******************************************************************************/
  129. static void qemu_pwr_domain_off(const psci_power_state_t *target_state)
  130. {
  131. qemu_pwr_gic_off();
  132. }
  133. void __dead2 plat_secondary_cold_boot_setup(void);
  134. static void __dead2
  135. qemu_pwr_domain_pwr_down_wfi(const psci_power_state_t *target_state)
  136. {
  137. disable_mmu_el3();
  138. plat_secondary_cold_boot_setup();
  139. }
  140. /*******************************************************************************
  141. * Platform handler called when a power domain is about to be suspended. The
  142. * target_state encodes the power state that each level should transition to.
  143. ******************************************************************************/
  144. void qemu_pwr_domain_suspend(const psci_power_state_t *target_state)
  145. {
  146. assert(0);
  147. }
  148. /*******************************************************************************
  149. * Platform handler called when a power domain has just been powered on after
  150. * being turned off earlier. The target_state encodes the low power state that
  151. * each level has woken up from.
  152. ******************************************************************************/
  153. void qemu_pwr_domain_on_finish(const psci_power_state_t *target_state)
  154. {
  155. assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
  156. PLAT_LOCAL_STATE_OFF);
  157. qemu_pwr_gic_on_finish();
  158. }
  159. /*******************************************************************************
  160. * Platform handler called when a power domain has just been powered on after
  161. * having been suspended earlier. The target_state encodes the low power state
  162. * that each level has woken up from.
  163. ******************************************************************************/
  164. void qemu_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  165. {
  166. assert(0);
  167. }
  168. /*******************************************************************************
  169. * Platform handlers to shutdown/reboot the system
  170. ******************************************************************************/
  171. static void __dead2 qemu_system_off(void)
  172. {
  173. #ifdef SECURE_GPIO_BASE
  174. ERROR("QEMU System Power off: with GPIO.\n");
  175. gpio_set_direction(SECURE_GPIO_POWEROFF, GPIO_DIR_OUT);
  176. gpio_set_value(SECURE_GPIO_POWEROFF, GPIO_LEVEL_LOW);
  177. gpio_set_value(SECURE_GPIO_POWEROFF, GPIO_LEVEL_HIGH);
  178. #else
  179. semihosting_exit(ADP_STOPPED_APPLICATION_EXIT, 0);
  180. ERROR("QEMU System Off: semihosting call unexpectedly returned.\n");
  181. #endif
  182. panic();
  183. }
  184. static void __dead2 qemu_system_reset(void)
  185. {
  186. ERROR("QEMU System Reset: with GPIO.\n");
  187. #ifdef SECURE_GPIO_BASE
  188. gpio_set_direction(SECURE_GPIO_RESET, GPIO_DIR_OUT);
  189. gpio_set_value(SECURE_GPIO_RESET, GPIO_LEVEL_LOW);
  190. gpio_set_value(SECURE_GPIO_RESET, GPIO_LEVEL_HIGH);
  191. #else
  192. ERROR("QEMU System Reset: operation not handled.\n");
  193. #endif
  194. panic();
  195. }
  196. static const plat_psci_ops_t plat_qemu_psci_pm_ops = {
  197. .cpu_standby = qemu_cpu_standby,
  198. .pwr_domain_on = qemu_pwr_domain_on,
  199. .pwr_domain_off = qemu_pwr_domain_off,
  200. .pwr_domain_pwr_down_wfi = qemu_pwr_domain_pwr_down_wfi,
  201. .pwr_domain_suspend = qemu_pwr_domain_suspend,
  202. .pwr_domain_on_finish = qemu_pwr_domain_on_finish,
  203. .pwr_domain_suspend_finish = qemu_pwr_domain_suspend_finish,
  204. .system_off = qemu_system_off,
  205. .system_reset = qemu_system_reset,
  206. .validate_power_state = qemu_validate_power_state,
  207. .validate_ns_entrypoint = qemu_validate_ns_entrypoint
  208. };
  209. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  210. const plat_psci_ops_t **psci_ops)
  211. {
  212. uintptr_t *mailbox = (void *) PLAT_QEMU_TRUSTED_MAILBOX_BASE;
  213. *mailbox = sec_entrypoint;
  214. secure_entrypoint = (unsigned long) sec_entrypoint;
  215. *psci_ops = &plat_qemu_psci_pm_ops;
  216. return 0;
  217. }