g12a_pm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <assert.h>
  8. #include <common/debug.h>
  9. #include <drivers/arm/gicv2.h>
  10. #include <drivers/console.h>
  11. #include <errno.h>
  12. #include <lib/mmio.h>
  13. #include <lib/psci/psci.h>
  14. #include <plat/common/platform.h>
  15. #include <platform_def.h>
  16. #include "aml_private.h"
  17. #define SCPI_POWER_ON 0
  18. #define SCPI_POWER_RETENTION 1
  19. #define SCPI_POWER_OFF 3
  20. #define SCPI_SYSTEM_SHUTDOWN 0
  21. #define SCPI_SYSTEM_REBOOT 1
  22. static uintptr_t g12a_sec_entrypoint;
  23. static volatile uint32_t g12a_cpu0_go;
  24. static void g12a_pm_set_reset_addr(u_register_t mpidr, uint64_t value)
  25. {
  26. unsigned int core = plat_calc_core_pos(mpidr);
  27. uintptr_t cpu_mailbox_addr = AML_PSCI_MAILBOX_BASE + (core << 4);
  28. mmio_write_64(cpu_mailbox_addr, value);
  29. }
  30. static void g12a_pm_reset(u_register_t mpidr)
  31. {
  32. unsigned int core = plat_calc_core_pos(mpidr);
  33. uintptr_t cpu_mailbox_addr = AML_PSCI_MAILBOX_BASE + (core << 4) + 8;
  34. mmio_write_32(cpu_mailbox_addr, 0);
  35. }
  36. static void __dead2 g12a_system_reset(void)
  37. {
  38. INFO("BL31: PSCI_SYSTEM_RESET\n");
  39. u_register_t mpidr = read_mpidr_el1();
  40. uint32_t status = mmio_read_32(AML_AO_RTI_STATUS_REG3);
  41. int ret;
  42. NOTICE("BL31: Reboot reason: 0x%x\n", status);
  43. status &= 0xFFFF0FF0;
  44. console_flush();
  45. mmio_write_32(AML_AO_RTI_STATUS_REG3, status);
  46. ret = aml_scpi_sys_power_state(SCPI_SYSTEM_REBOOT);
  47. if (ret != 0) {
  48. ERROR("BL31: PSCI_SYSTEM_RESET: SCP error: %i\n", ret);
  49. panic();
  50. }
  51. g12a_pm_reset(mpidr);
  52. wfi();
  53. ERROR("BL31: PSCI_SYSTEM_RESET: Operation not handled\n");
  54. panic();
  55. }
  56. static void __dead2 g12a_system_off(void)
  57. {
  58. INFO("BL31: PSCI_SYSTEM_OFF\n");
  59. u_register_t mpidr = read_mpidr_el1();
  60. int ret;
  61. ret = aml_scpi_sys_power_state(SCPI_SYSTEM_SHUTDOWN);
  62. if (ret != 0) {
  63. ERROR("BL31: PSCI_SYSTEM_OFF: SCP error %i\n", ret);
  64. panic();
  65. }
  66. g12a_pm_set_reset_addr(mpidr, 0);
  67. g12a_pm_reset(mpidr);
  68. wfi();
  69. ERROR("BL31: PSCI_SYSTEM_OFF: Operation not handled\n");
  70. panic();
  71. }
  72. static int32_t g12a_pwr_domain_on(u_register_t mpidr)
  73. {
  74. unsigned int core = plat_calc_core_pos(mpidr);
  75. /* CPU0 can't be turned OFF */
  76. if (core == AML_PRIMARY_CPU) {
  77. VERBOSE("BL31: Releasing CPU0 from wait loop...\n");
  78. g12a_cpu0_go = 1;
  79. flush_dcache_range((uintptr_t)&g12a_cpu0_go,
  80. sizeof(g12a_cpu0_go));
  81. dsb();
  82. isb();
  83. sev();
  84. return PSCI_E_SUCCESS;
  85. }
  86. g12a_pm_set_reset_addr(mpidr, g12a_sec_entrypoint);
  87. aml_scpi_set_css_power_state(mpidr,
  88. SCPI_POWER_ON, SCPI_POWER_ON, SCPI_POWER_ON);
  89. dmbsy();
  90. sev();
  91. return PSCI_E_SUCCESS;
  92. }
  93. static void g12a_pwr_domain_on_finish(const psci_power_state_t *target_state)
  94. {
  95. unsigned int core = plat_calc_core_pos(read_mpidr_el1());
  96. assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
  97. PLAT_LOCAL_STATE_OFF);
  98. if (core == AML_PRIMARY_CPU) {
  99. g12a_cpu0_go = 0;
  100. flush_dcache_range((uintptr_t)&g12a_cpu0_go,
  101. sizeof(g12a_cpu0_go));
  102. dsb();
  103. isb();
  104. }
  105. gicv2_pcpu_distif_init();
  106. gicv2_cpuif_enable();
  107. }
  108. static void g12a_pwr_domain_off(const psci_power_state_t *target_state)
  109. {
  110. u_register_t mpidr = read_mpidr_el1();
  111. unsigned int core = plat_calc_core_pos(mpidr);
  112. gicv2_cpuif_disable();
  113. /* CPU0 can't be turned OFF */
  114. if (core == AML_PRIMARY_CPU)
  115. return;
  116. aml_scpi_set_css_power_state(mpidr,
  117. SCPI_POWER_OFF, SCPI_POWER_ON,
  118. SCPI_POWER_ON);
  119. }
  120. static void __dead2 g12a_pwr_domain_pwr_down_wfi(const psci_power_state_t
  121. *target_state)
  122. {
  123. u_register_t mpidr = read_mpidr_el1();
  124. unsigned int core = plat_calc_core_pos(mpidr);
  125. /* CPU0 can't be turned OFF, emulate it with a WFE loop */
  126. if (core == AML_PRIMARY_CPU) {
  127. VERBOSE("BL31: CPU0 entering wait loop...\n");
  128. while (g12a_cpu0_go == 0)
  129. wfe();
  130. VERBOSE("BL31: CPU0 resumed.\n");
  131. /*
  132. * Because setting CPU0's warm reset entrypoint through PSCI
  133. * mailbox and/or mmio mapped RVBAR (0xda834650) does not seem
  134. * to work, jump to it manually.
  135. * In order to avoid an assert, MMU has to be disabled.
  136. */
  137. disable_mmu_el3();
  138. ((void(*)(void))g12a_sec_entrypoint)();
  139. }
  140. dsbsy();
  141. g12a_pm_set_reset_addr(mpidr, 0);
  142. g12a_pm_reset(mpidr);
  143. for (;;)
  144. wfi();
  145. }
  146. /*******************************************************************************
  147. * Platform handlers and setup function.
  148. ******************************************************************************/
  149. static const plat_psci_ops_t g12a_ops = {
  150. .pwr_domain_on = g12a_pwr_domain_on,
  151. .pwr_domain_on_finish = g12a_pwr_domain_on_finish,
  152. .pwr_domain_off = g12a_pwr_domain_off,
  153. .pwr_domain_pwr_down_wfi = g12a_pwr_domain_pwr_down_wfi,
  154. .system_off = g12a_system_off,
  155. .system_reset = g12a_system_reset
  156. };
  157. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  158. const plat_psci_ops_t **psci_ops)
  159. {
  160. g12a_sec_entrypoint = sec_entrypoint;
  161. *psci_ops = &g12a_ops;
  162. g12a_cpu0_go = 0;
  163. return 0;
  164. }