gxl_pm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2018-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 gxl_sec_entrypoint;
  23. static volatile uint32_t gxl_cpu0_go;
  24. static void gxl_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 gxl_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 gxl_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. gxl_pm_reset(mpidr);
  52. wfi();
  53. ERROR("BL31: PSCI_SYSTEM_RESET: Operation not handled\n");
  54. panic();
  55. }
  56. static void __dead2 gxl_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. gxl_pm_set_reset_addr(mpidr, 0);
  67. gxl_pm_reset(mpidr);
  68. wfi();
  69. ERROR("BL31: PSCI_SYSTEM_OFF: Operation not handled\n");
  70. panic();
  71. }
  72. static int32_t gxl_pwr_domain_on(u_register_t mpidr)
  73. {
  74. unsigned int core = plat_calc_core_pos(mpidr);
  75. /* CPU0 can't be turned OFF, emulate it with a WFE loop */
  76. if (core == AML_PRIMARY_CPU) {
  77. VERBOSE("BL31: Releasing CPU0 from wait loop...\n");
  78. gxl_cpu0_go = 1;
  79. flush_dcache_range((uintptr_t)&gxl_cpu0_go,
  80. sizeof(gxl_cpu0_go));
  81. dsb();
  82. isb();
  83. sev();
  84. return PSCI_E_SUCCESS;
  85. }
  86. gxl_pm_set_reset_addr(mpidr, gxl_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 gxl_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. gxl_cpu0_go = 0;
  100. flush_dcache_range((uintptr_t)&gxl_cpu0_go,
  101. sizeof(gxl_cpu0_go));
  102. dsb();
  103. isb();
  104. }
  105. gicv2_pcpu_distif_init();
  106. gicv2_cpuif_enable();
  107. }
  108. static void gxl_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, emulate it with a WFE loop */
  114. if (core == AML_PRIMARY_CPU)
  115. return;
  116. aml_scpi_set_css_power_state(mpidr,
  117. SCPI_POWER_OFF, SCPI_POWER_ON, SCPI_POWER_ON);
  118. }
  119. static void __dead2 gxl_pwr_domain_pwr_down_wfi(const psci_power_state_t
  120. *target_state)
  121. {
  122. u_register_t mpidr = read_mpidr_el1();
  123. unsigned int core = plat_calc_core_pos(mpidr);
  124. /* CPU0 can't be turned OFF, emulate it with a WFE loop */
  125. if (core == AML_PRIMARY_CPU) {
  126. VERBOSE("BL31: CPU0 entering wait loop...\n");
  127. while (gxl_cpu0_go == 0)
  128. wfe();
  129. VERBOSE("BL31: CPU0 resumed.\n");
  130. /*
  131. * Because setting CPU0's warm reset entrypoint through PSCI
  132. * mailbox and/or mmio mapped RVBAR (0xda834650) does not seem
  133. * to work, jump to it manually.
  134. * In order to avoid an assert, mmu has to be disabled.
  135. */
  136. disable_mmu_el3();
  137. ((void(*)(void))gxl_sec_entrypoint)();
  138. }
  139. dsbsy();
  140. gxl_pm_set_reset_addr(mpidr, 0);
  141. gxl_pm_reset(mpidr);
  142. for (;;)
  143. wfi();
  144. }
  145. /*******************************************************************************
  146. * Platform handlers and setup function.
  147. ******************************************************************************/
  148. static const plat_psci_ops_t gxl_ops = {
  149. .pwr_domain_on = gxl_pwr_domain_on,
  150. .pwr_domain_on_finish = gxl_pwr_domain_on_finish,
  151. .pwr_domain_off = gxl_pwr_domain_off,
  152. .pwr_domain_pwr_down_wfi = gxl_pwr_domain_pwr_down_wfi,
  153. .system_off = gxl_system_off,
  154. .system_reset = gxl_system_reset,
  155. };
  156. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  157. const plat_psci_ops_t **psci_ops)
  158. {
  159. gxl_sec_entrypoint = sec_entrypoint;
  160. *psci_ops = &gxl_ops;
  161. gxl_cpu0_go = 0;
  162. return 0;
  163. }