imx8qm_psci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdbool.h>
  7. #include <arch.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <drivers/arm/cci.h>
  11. #include <drivers/arm/gicv3.h>
  12. #include <lib/mmio.h>
  13. #include <lib/psci/psci.h>
  14. #include <plat_imx8.h>
  15. #include <sci/sci.h>
  16. #include "../../common/sci/imx8_mu.h"
  17. #define CORE_PWR_STATE(state) \
  18. ((state)->pwr_domain_state[MPIDR_AFFLVL0])
  19. #define CLUSTER_PWR_STATE(state) \
  20. ((state)->pwr_domain_state[MPIDR_AFFLVL1])
  21. #define SYSTEM_PWR_STATE(state) \
  22. ((state)->pwr_domain_state[PLAT_MAX_PWR_LVL])
  23. static const int ap_core_index[PLATFORM_CORE_COUNT] = {
  24. SC_R_A53_0, SC_R_A53_1, SC_R_A53_2,
  25. SC_R_A53_3, SC_R_A72_0, SC_R_A72_1,
  26. };
  27. /* save gic dist/redist context when GIC is poewr down */
  28. static struct plat_gic_ctx imx_gicv3_ctx;
  29. static unsigned int gpt_lpcg, gpt_reg[2];
  30. static void imx_enable_irqstr_wakeup(void)
  31. {
  32. uint32_t irq_mask;
  33. gicv3_dist_ctx_t *dist_ctx = &imx_gicv3_ctx.dist_ctx;
  34. /* put IRQSTR into ON mode */
  35. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_ON);
  36. /* enable the irqsteer to handle wakeup irq */
  37. mmio_write_32(IMX_WUP_IRQSTR_BASE, 0x1);
  38. for (int i = 0; i < 15; i++) {
  39. irq_mask = dist_ctx->gicd_isenabler[i];
  40. mmio_write_32(IMX_WUP_IRQSTR_BASE + 0x3c - 0x4 * i, irq_mask);
  41. }
  42. /* set IRQSTR low power mode */
  43. if (imx_is_wakeup_src_irqsteer())
  44. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_STBY);
  45. else
  46. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_OFF);
  47. }
  48. static void imx_disable_irqstr_wakeup(void)
  49. {
  50. /* put IRQSTR into ON from STBY mode */
  51. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_ON);
  52. /* disable the irqsteer */
  53. mmio_write_32(IMX_WUP_IRQSTR_BASE, 0x0);
  54. for (int i = 0; i < 16; i++)
  55. mmio_write_32(IMX_WUP_IRQSTR_BASE + 0x4 + 0x4 * i, 0x0);
  56. /* put IRQSTR into OFF mode */
  57. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_OFF);
  58. }
  59. int imx_pwr_domain_on(u_register_t mpidr)
  60. {
  61. int ret = PSCI_E_SUCCESS;
  62. unsigned int cluster_id = MPIDR_AFFLVL1_VAL(mpidr);
  63. unsigned int cpu_id = MPIDR_AFFLVL0_VAL(mpidr);
  64. sc_pm_set_resource_power_mode(ipc_handle, cluster_id == 0 ?
  65. SC_R_A53 : SC_R_A72, SC_PM_PW_MODE_ON);
  66. if (cluster_id == 1)
  67. sc_pm_req_low_power_mode(ipc_handle, SC_R_A72, SC_PM_PW_MODE_ON);
  68. if (sc_pm_set_resource_power_mode(ipc_handle,
  69. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  70. SC_PM_PW_MODE_ON) != SC_ERR_NONE) {
  71. ERROR("core %d power on failed!\n", cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id);
  72. ret = PSCI_E_INTERN_FAIL;
  73. }
  74. if (sc_pm_cpu_start(ipc_handle,
  75. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  76. true, BL31_BASE) != SC_ERR_NONE) {
  77. ERROR("boot core %d failed!\n", cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id);
  78. ret = PSCI_E_INTERN_FAIL;
  79. }
  80. return ret;
  81. }
  82. void imx_pwr_domain_on_finish(const psci_power_state_t *target_state)
  83. {
  84. uint64_t mpidr = read_mpidr_el1();
  85. if (CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE)
  86. cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(mpidr));
  87. plat_gic_pcpu_init();
  88. plat_gic_cpuif_enable();
  89. }
  90. void imx_pwr_domain_off(const psci_power_state_t *target_state)
  91. {
  92. u_register_t mpidr = read_mpidr_el1();
  93. unsigned int cluster_id = MPIDR_AFFLVL1_VAL(mpidr);
  94. unsigned int cpu_id = MPIDR_AFFLVL0_VAL(mpidr);
  95. plat_gic_cpuif_disable();
  96. sc_pm_req_cpu_low_power_mode(ipc_handle,
  97. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  98. SC_PM_PW_MODE_OFF, SC_PM_WAKE_SRC_NONE);
  99. if (is_local_state_off(CLUSTER_PWR_STATE(target_state))) {
  100. cci_disable_snoop_dvm_reqs(cluster_id);
  101. if (cluster_id == 1)
  102. sc_pm_req_low_power_mode(ipc_handle, SC_R_A72, SC_PM_PW_MODE_OFF);
  103. }
  104. printf("turn off cluster:%d core:%d\n", cluster_id, cpu_id);
  105. }
  106. void imx_domain_suspend(const psci_power_state_t *target_state)
  107. {
  108. u_register_t mpidr = read_mpidr_el1();
  109. unsigned int cluster_id = MPIDR_AFFLVL1_VAL(mpidr);
  110. unsigned int cpu_id = MPIDR_AFFLVL0_VAL(mpidr);
  111. if (is_local_state_off(CORE_PWR_STATE(target_state))) {
  112. plat_gic_cpuif_disable();
  113. sc_pm_set_cpu_resume(ipc_handle,
  114. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  115. true, BL31_BASE);
  116. sc_pm_req_cpu_low_power_mode(ipc_handle,
  117. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  118. SC_PM_PW_MODE_OFF, SC_PM_WAKE_SRC_GIC);
  119. } else {
  120. dsb();
  121. write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
  122. isb();
  123. }
  124. if (is_local_state_off(CLUSTER_PWR_STATE(target_state))) {
  125. cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(mpidr));
  126. if (cluster_id == 1)
  127. sc_pm_req_low_power_mode(ipc_handle, SC_R_A72, SC_PM_PW_MODE_OFF);
  128. }
  129. if (is_local_state_retn(SYSTEM_PWR_STATE(target_state))) {
  130. plat_gic_cpuif_disable();
  131. /* save gic context */
  132. plat_gic_save(cpu_id, &imx_gicv3_ctx);
  133. /* enable the irqsteer for wakeup */
  134. imx_enable_irqstr_wakeup();
  135. cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(mpidr));
  136. /* Put GIC in LP mode. */
  137. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GIC, SC_PM_PW_MODE_OFF);
  138. /* Save GPT clock and registers, then turn off its power */
  139. gpt_lpcg = mmio_read_32(IMX_GPT_LPCG_BASE);
  140. gpt_reg[0] = mmio_read_32(IMX_GPT_BASE);
  141. gpt_reg[1] = mmio_read_32(IMX_GPT_BASE + 0x4);
  142. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GPT_0, SC_PM_PW_MODE_OFF);
  143. sc_pm_req_low_power_mode(ipc_handle, SC_R_A53, SC_PM_PW_MODE_OFF);
  144. sc_pm_req_low_power_mode(ipc_handle, SC_R_A72, SC_PM_PW_MODE_OFF);
  145. sc_pm_req_low_power_mode(ipc_handle, SC_R_CCI, SC_PM_PW_MODE_OFF);
  146. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_DDR,
  147. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  148. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_DDR,
  149. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  150. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_MU,
  151. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  152. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_MU,
  153. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  154. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_INTERCONNECT,
  155. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  156. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_INTERCONNECT,
  157. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  158. sc_pm_req_low_power_mode(ipc_handle, SC_R_CCI, SC_PM_PW_MODE_OFF);
  159. sc_pm_set_cpu_resume(ipc_handle,
  160. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  161. true, BL31_BASE);
  162. if (imx_is_wakeup_src_irqsteer())
  163. sc_pm_req_cpu_low_power_mode(ipc_handle,
  164. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  165. SC_PM_PW_MODE_OFF, SC_PM_WAKE_SRC_IRQSTEER);
  166. else
  167. sc_pm_req_cpu_low_power_mode(ipc_handle,
  168. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  169. SC_PM_PW_MODE_OFF, SC_PM_WAKE_SRC_SCU);
  170. }
  171. }
  172. void imx_domain_suspend_finish(const psci_power_state_t *target_state)
  173. {
  174. u_register_t mpidr = read_mpidr_el1();
  175. unsigned int cluster_id = MPIDR_AFFLVL1_VAL(mpidr);
  176. unsigned int cpu_id = MPIDR_AFFLVL0_VAL(mpidr);
  177. /* check the system level status */
  178. if (is_local_state_retn(SYSTEM_PWR_STATE(target_state))) {
  179. MU_Resume(SC_IPC_BASE);
  180. sc_pm_req_cpu_low_power_mode(ipc_handle,
  181. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  182. SC_PM_PW_MODE_ON, SC_PM_WAKE_SRC_GIC);
  183. /* Put GIC/IRQSTR back to high power mode. */
  184. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GIC, SC_PM_PW_MODE_ON);
  185. /* Turn GPT power and restore its clock and registers */
  186. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GPT_0, SC_PM_PW_MODE_ON);
  187. sc_pm_clock_enable(ipc_handle, SC_R_GPT_0, SC_PM_CLK_PER, true, 0);
  188. mmio_write_32(IMX_GPT_BASE, gpt_reg[0]);
  189. mmio_write_32(IMX_GPT_BASE + 0x4, gpt_reg[1]);
  190. mmio_write_32(IMX_GPT_LPCG_BASE, gpt_lpcg);
  191. sc_pm_req_low_power_mode(ipc_handle, SC_R_A53, SC_PM_PW_MODE_ON);
  192. sc_pm_req_low_power_mode(ipc_handle, SC_R_A72, SC_PM_PW_MODE_ON);
  193. sc_pm_req_low_power_mode(ipc_handle, SC_R_CCI, SC_PM_PW_MODE_ON);
  194. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_DDR,
  195. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  196. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_DDR,
  197. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  198. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_MU,
  199. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  200. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_MU,
  201. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  202. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_INTERCONNECT,
  203. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  204. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_INTERCONNECT,
  205. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  206. sc_pm_req_low_power_mode(ipc_handle, SC_R_CCI, SC_PM_PW_MODE_ON);
  207. cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(mpidr));
  208. /* restore gic context */
  209. plat_gic_restore(cpu_id, &imx_gicv3_ctx);
  210. /* disable the irqsteer wakeup */
  211. imx_disable_irqstr_wakeup();
  212. plat_gic_cpuif_enable();
  213. }
  214. /* check the cluster level power status */
  215. if (is_local_state_off(CLUSTER_PWR_STATE(target_state))) {
  216. cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(mpidr));
  217. if (cluster_id == 1)
  218. sc_pm_req_low_power_mode(ipc_handle, SC_R_A72, SC_PM_PW_MODE_ON);
  219. }
  220. /* check the core level power status */
  221. if (is_local_state_off(CORE_PWR_STATE(target_state))) {
  222. sc_pm_set_cpu_resume(ipc_handle,
  223. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  224. false, BL31_BASE);
  225. sc_pm_req_cpu_low_power_mode(ipc_handle,
  226. ap_core_index[cpu_id + PLATFORM_CLUSTER0_CORE_COUNT * cluster_id],
  227. SC_PM_PW_MODE_ON, SC_PM_WAKE_SRC_GIC);
  228. plat_gic_cpuif_enable();
  229. } else {
  230. write_scr_el3(read_scr_el3() & (~SCR_FIQ_BIT));
  231. isb();
  232. }
  233. }
  234. int imx_validate_ns_entrypoint(uintptr_t ns_entrypoint)
  235. {
  236. return PSCI_E_SUCCESS;
  237. }
  238. static const plat_psci_ops_t imx_plat_psci_ops = {
  239. .pwr_domain_on = imx_pwr_domain_on,
  240. .pwr_domain_on_finish = imx_pwr_domain_on_finish,
  241. .pwr_domain_off = imx_pwr_domain_off,
  242. .pwr_domain_suspend = imx_domain_suspend,
  243. .pwr_domain_suspend_finish = imx_domain_suspend_finish,
  244. .get_sys_suspend_power_state = imx_get_sys_suspend_power_state,
  245. .validate_power_state = imx_validate_power_state,
  246. .validate_ns_entrypoint = imx_validate_ns_entrypoint,
  247. .system_off = imx_system_off,
  248. .system_reset = imx_system_reset,
  249. };
  250. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  251. const plat_psci_ops_t **psci_ops)
  252. {
  253. imx_mailbox_init(sec_entrypoint);
  254. *psci_ops = &imx_plat_psci_ops;
  255. /* make sure system sources power ON in low power mode by default */
  256. sc_pm_req_low_power_mode(ipc_handle, SC_R_A53, SC_PM_PW_MODE_ON);
  257. sc_pm_req_low_power_mode(ipc_handle, SC_R_A72, SC_PM_PW_MODE_ON);
  258. sc_pm_req_low_power_mode(ipc_handle, SC_R_CCI, SC_PM_PW_MODE_ON);
  259. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_DDR,
  260. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  261. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_DDR,
  262. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  263. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_MU,
  264. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  265. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_MU,
  266. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  267. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A53, SC_PM_SYS_IF_INTERCONNECT,
  268. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  269. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A72, SC_PM_SYS_IF_INTERCONNECT,
  270. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  271. return 0;
  272. }