gxbb_pm.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 gxbb_sec_entrypoint;
  23. static volatile uint32_t gxbb_cpu0_go;
  24. static void gxbb_program_mailbox(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. flush_dcache_range(cpu_mailbox_addr, sizeof(uint64_t));
  30. }
  31. static void __dead2 gxbb_system_reset(void)
  32. {
  33. INFO("BL31: PSCI_SYSTEM_RESET\n");
  34. uint32_t status = mmio_read_32(AML_AO_RTI_STATUS_REG3);
  35. NOTICE("BL31: Reboot reason: 0x%x\n", status);
  36. status &= 0xFFFF0FF0;
  37. console_flush();
  38. mmio_write_32(AML_AO_RTI_STATUS_REG3, status);
  39. int ret = aml_scpi_sys_power_state(SCPI_SYSTEM_REBOOT);
  40. if (ret != 0) {
  41. ERROR("BL31: PSCI_SYSTEM_RESET: SCP error: %u\n", ret);
  42. panic();
  43. }
  44. wfi();
  45. ERROR("BL31: PSCI_SYSTEM_RESET: Operation not handled\n");
  46. panic();
  47. }
  48. static void __dead2 gxbb_system_off(void)
  49. {
  50. INFO("BL31: PSCI_SYSTEM_OFF\n");
  51. unsigned int ret = aml_scpi_sys_power_state(SCPI_SYSTEM_SHUTDOWN);
  52. if (ret != 0) {
  53. ERROR("BL31: PSCI_SYSTEM_OFF: SCP error %u\n", ret);
  54. panic();
  55. }
  56. gxbb_program_mailbox(read_mpidr_el1(), 0);
  57. wfi();
  58. ERROR("BL31: PSCI_SYSTEM_OFF: Operation not handled\n");
  59. panic();
  60. }
  61. static int32_t gxbb_pwr_domain_on(u_register_t mpidr)
  62. {
  63. unsigned int core = plat_calc_core_pos(mpidr);
  64. /* CPU0 can't be turned OFF, emulate it with a WFE loop */
  65. if (core == AML_PRIMARY_CPU) {
  66. VERBOSE("BL31: Releasing CPU0 from wait loop...\n");
  67. gxbb_cpu0_go = 1;
  68. flush_dcache_range((uintptr_t)&gxbb_cpu0_go, sizeof(gxbb_cpu0_go));
  69. dsb();
  70. isb();
  71. sev();
  72. return PSCI_E_SUCCESS;
  73. }
  74. gxbb_program_mailbox(mpidr, gxbb_sec_entrypoint);
  75. aml_scpi_set_css_power_state(mpidr,
  76. SCPI_POWER_ON, SCPI_POWER_ON, SCPI_POWER_ON);
  77. dmbsy();
  78. sev();
  79. return PSCI_E_SUCCESS;
  80. }
  81. static void gxbb_pwr_domain_on_finish(const psci_power_state_t *target_state)
  82. {
  83. unsigned int core = plat_calc_core_pos(read_mpidr_el1());
  84. assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
  85. PLAT_LOCAL_STATE_OFF);
  86. if (core == AML_PRIMARY_CPU) {
  87. gxbb_cpu0_go = 0;
  88. flush_dcache_range((uintptr_t)&gxbb_cpu0_go, sizeof(gxbb_cpu0_go));
  89. dsb();
  90. isb();
  91. }
  92. gicv2_pcpu_distif_init();
  93. gicv2_cpuif_enable();
  94. }
  95. static void gxbb_pwr_domain_off(const psci_power_state_t *target_state)
  96. {
  97. u_register_t mpidr = read_mpidr_el1();
  98. unsigned int core = plat_calc_core_pos(mpidr);
  99. uintptr_t addr = AML_PSCI_MAILBOX_BASE + 8 + (core << 4);
  100. mmio_write_32(addr, 0xFFFFFFFF);
  101. flush_dcache_range(addr, sizeof(uint32_t));
  102. gicv2_cpuif_disable();
  103. /* CPU0 can't be turned OFF, emulate it with a WFE loop */
  104. if (core == AML_PRIMARY_CPU)
  105. return;
  106. aml_scpi_set_css_power_state(mpidr,
  107. SCPI_POWER_OFF, SCPI_POWER_ON, SCPI_POWER_ON);
  108. }
  109. static void __dead2 gxbb_pwr_domain_pwr_down_wfi(const psci_power_state_t
  110. *target_state)
  111. {
  112. unsigned int core = plat_calc_core_pos(read_mpidr_el1());
  113. /* CPU0 can't be turned OFF, emulate it with a WFE loop */
  114. if (core == AML_PRIMARY_CPU) {
  115. VERBOSE("BL31: CPU0 entering wait loop...\n");
  116. while (gxbb_cpu0_go == 0)
  117. wfe();
  118. VERBOSE("BL31: CPU0 resumed.\n");
  119. write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT);
  120. }
  121. dsbsy();
  122. for (;;)
  123. wfi();
  124. }
  125. /*******************************************************************************
  126. * Platform handlers and setup function.
  127. ******************************************************************************/
  128. static const plat_psci_ops_t gxbb_ops = {
  129. .pwr_domain_on = gxbb_pwr_domain_on,
  130. .pwr_domain_on_finish = gxbb_pwr_domain_on_finish,
  131. .pwr_domain_off = gxbb_pwr_domain_off,
  132. .pwr_domain_pwr_down_wfi = gxbb_pwr_domain_pwr_down_wfi,
  133. .system_off = gxbb_system_off,
  134. .system_reset = gxbb_system_reset,
  135. };
  136. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  137. const plat_psci_ops_t **psci_ops)
  138. {
  139. gxbb_sec_entrypoint = sec_entrypoint;
  140. *psci_ops = &gxbb_ops;
  141. gxbb_cpu0_go = 0;
  142. return 0;
  143. }