css_pm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (c) 2015-2022, 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 <bl31/interrupt_mgmt.h>
  10. #include <common/debug.h>
  11. #include <drivers/arm/css/css_scp.h>
  12. #include <drivers/arm/css/dsu.h>
  13. #include <lib/cassert.h>
  14. #include <plat/arm/common/plat_arm.h>
  15. #include <plat/common/platform.h>
  16. #include <plat/arm/css/common/css_pm.h>
  17. /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
  18. #pragma weak plat_arm_psci_pm_ops
  19. #if ARM_RECOM_STATE_ID_ENC
  20. /*
  21. * The table storing the valid idle power states. Ensure that the
  22. * array entries are populated in ascending order of state-id to
  23. * enable us to use binary search during power state validation.
  24. * The table must be terminated by a NULL entry.
  25. */
  26. const unsigned int arm_pm_idle_states[] = {
  27. /* State-id - 0x001 */
  28. arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
  29. ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
  30. /* State-id - 0x002 */
  31. arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
  32. ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
  33. /* State-id - 0x022 */
  34. arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
  35. ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
  36. #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
  37. /* State-id - 0x222 */
  38. arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
  39. ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
  40. #endif
  41. 0,
  42. };
  43. #endif /* __ARM_RECOM_STATE_ID_ENC__ */
  44. /*
  45. * All the power management helpers in this file assume at least cluster power
  46. * level is supported.
  47. */
  48. CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1,
  49. assert_max_pwr_lvl_supported_mismatch);
  50. /*
  51. * Ensure that the PLAT_MAX_PWR_LVL is not greater than CSS_SYSTEM_PWR_DMN_LVL
  52. * assumed by the CSS layer.
  53. */
  54. CASSERT(PLAT_MAX_PWR_LVL <= CSS_SYSTEM_PWR_DMN_LVL,
  55. assert_max_pwr_lvl_higher_than_css_sys_lvl);
  56. /*******************************************************************************
  57. * Handler called when a power domain is about to be turned on. The
  58. * level and mpidr determine the affinity instance.
  59. ******************************************************************************/
  60. int css_pwr_domain_on(u_register_t mpidr)
  61. {
  62. css_scp_on(mpidr);
  63. return PSCI_E_SUCCESS;
  64. }
  65. static void css_pwr_domain_on_finisher_common(
  66. const psci_power_state_t *target_state)
  67. {
  68. assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
  69. /*
  70. * Perform the common cluster specific operations i.e enable coherency
  71. * if this cluster was off.
  72. */
  73. if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) {
  74. #if PRESERVE_DSU_PMU_REGS
  75. cluster_on_dsu_pmu_context_restore();
  76. #endif
  77. plat_arm_interconnect_enter_coherency();
  78. }
  79. }
  80. /*******************************************************************************
  81. * Handler called when a power level has just been powered on after
  82. * being turned off earlier. The target_state encodes the low power state that
  83. * each level has woken up from. This handler would never be invoked with
  84. * the system power domain uninitialized as either the primary would have taken
  85. * care of it as part of cold boot or the first core awakened from system
  86. * suspend would have already initialized it.
  87. ******************************************************************************/
  88. void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
  89. {
  90. /* Assert that the system power domain need not be initialized */
  91. assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
  92. css_pwr_domain_on_finisher_common(target_state);
  93. }
  94. /*******************************************************************************
  95. * Handler called when a power domain has just been powered on and the cpu
  96. * and its cluster are fully participating in coherent transaction on the
  97. * interconnect. Data cache must be enabled for CPU at this point.
  98. ******************************************************************************/
  99. void css_pwr_domain_on_finish_late(const psci_power_state_t *target_state)
  100. {
  101. /* Program the gic per-cpu distributor or re-distributor interface */
  102. plat_arm_gic_pcpu_init();
  103. /* Enable the gic cpu interface */
  104. plat_arm_gic_cpuif_enable();
  105. /* Setup the CPU power down request interrupt for secondary core(s) */
  106. css_setup_cpu_pwr_down_intr();
  107. }
  108. /*******************************************************************************
  109. * Common function called while turning a cpu off or suspending it. It is called
  110. * from css_off() or css_suspend() when these functions in turn are called for
  111. * power domain at the highest power level which will be powered down. It
  112. * performs the actions common to the OFF and SUSPEND calls.
  113. ******************************************************************************/
  114. static void css_power_down_common(const psci_power_state_t *target_state)
  115. {
  116. /* Prevent interrupts from spuriously waking up this cpu */
  117. plat_arm_gic_cpuif_disable();
  118. /* Cluster is to be turned off, so disable coherency */
  119. if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) {
  120. #if PRESERVE_DSU_PMU_REGS
  121. cluster_off_dsu_pmu_context_save();
  122. #endif
  123. plat_arm_interconnect_exit_coherency();
  124. }
  125. }
  126. /*******************************************************************************
  127. * Handler called when a power domain is about to be turned off. The
  128. * target_state encodes the power state that each level should transition to.
  129. ******************************************************************************/
  130. void css_pwr_domain_off(const psci_power_state_t *target_state)
  131. {
  132. assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
  133. css_power_down_common(target_state);
  134. css_scp_off(target_state);
  135. }
  136. /*******************************************************************************
  137. * Handler called when a power domain is about to be suspended. The
  138. * target_state encodes the power state that each level should transition to.
  139. ******************************************************************************/
  140. void css_pwr_domain_suspend(const psci_power_state_t *target_state)
  141. {
  142. /*
  143. * CSS currently supports retention only at cpu level. Just return
  144. * as nothing is to be done for retention.
  145. */
  146. if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
  147. return;
  148. assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
  149. css_power_down_common(target_state);
  150. /* Perform system domain state saving if issuing system suspend */
  151. if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) {
  152. arm_system_pwr_domain_save();
  153. /* Power off the Redistributor after having saved its context */
  154. plat_arm_gic_redistif_off();
  155. }
  156. css_scp_suspend(target_state);
  157. }
  158. /*******************************************************************************
  159. * Handler called when a power domain has just been powered on after
  160. * having been suspended earlier. The target_state encodes the low power state
  161. * that each level has woken up from.
  162. * TODO: At the moment we reuse the on finisher and reinitialize the secure
  163. * context. Need to implement a separate suspend finisher.
  164. ******************************************************************************/
  165. void css_pwr_domain_suspend_finish(
  166. const psci_power_state_t *target_state)
  167. {
  168. /* Return as nothing is to be done on waking up from retention. */
  169. if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
  170. return;
  171. /* Perform system domain restore if woken up from system suspend */
  172. if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF)
  173. /*
  174. * At this point, the Distributor must be powered on to be ready
  175. * to have its state restored. The Redistributor will be powered
  176. * on as part of gicv3_rdistif_init_restore.
  177. */
  178. arm_system_pwr_domain_resume();
  179. css_pwr_domain_on_finisher_common(target_state);
  180. /* Enable the gic cpu interface */
  181. plat_arm_gic_cpuif_enable();
  182. }
  183. /*******************************************************************************
  184. * Handlers to shutdown/reboot the system
  185. ******************************************************************************/
  186. void __dead2 css_system_off(void)
  187. {
  188. css_scp_sys_shutdown();
  189. }
  190. void __dead2 css_system_reset(void)
  191. {
  192. css_scp_sys_reboot();
  193. }
  194. /*******************************************************************************
  195. * Handler called when the CPU power domain is about to enter standby.
  196. ******************************************************************************/
  197. void css_cpu_standby(plat_local_state_t cpu_state)
  198. {
  199. unsigned int scr;
  200. assert(cpu_state == ARM_LOCAL_STATE_RET);
  201. scr = read_scr_el3();
  202. /*
  203. * Enable the Non secure interrupt to wake the CPU.
  204. * In GICv3 affinity routing mode, the non secure group1 interrupts use
  205. * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ.
  206. * Enabling both the bits works for both GICv2 mode and GICv3 affinity
  207. * routing mode.
  208. */
  209. write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
  210. isb();
  211. dsb();
  212. wfi();
  213. /*
  214. * Restore SCR to the original value, synchronisation of scr_el3 is
  215. * done by eret while el3_exit to save some execution cycles.
  216. */
  217. write_scr_el3(scr);
  218. }
  219. /*******************************************************************************
  220. * Handler called to return the 'req_state' for system suspend.
  221. ******************************************************************************/
  222. void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
  223. {
  224. unsigned int i;
  225. /*
  226. * System Suspend is supported only if the system power domain node
  227. * is implemented.
  228. */
  229. assert(PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL);
  230. for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
  231. req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
  232. }
  233. /*******************************************************************************
  234. * Handler to query CPU/cluster power states from SCP
  235. ******************************************************************************/
  236. int css_node_hw_state(u_register_t mpidr, unsigned int power_level)
  237. {
  238. return css_scp_get_power_state(mpidr, power_level);
  239. }
  240. /*
  241. * The system power domain suspend is only supported only via
  242. * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain
  243. * will be downgraded to the lower level.
  244. */
  245. static int css_validate_power_state(unsigned int power_state,
  246. psci_power_state_t *req_state)
  247. {
  248. int rc;
  249. rc = arm_validate_power_state(power_state, req_state);
  250. /*
  251. * Ensure that we don't overrun the pwr_domain_state array in the case
  252. * where the platform supported max power level is less than the system
  253. * power level
  254. */
  255. #if (PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL)
  256. /*
  257. * Ensure that the system power domain level is never suspended
  258. * via PSCI CPU SUSPEND API. Currently system suspend is only
  259. * supported via PSCI SYSTEM SUSPEND API.
  260. */
  261. req_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] =
  262. ARM_LOCAL_STATE_RUN;
  263. #endif
  264. return rc;
  265. }
  266. /*
  267. * Custom `translate_power_state_by_mpidr` handler for CSS. Unlike in the
  268. * `css_validate_power_state`, we do not downgrade the system power
  269. * domain level request in `power_state` as it will be used to query the
  270. * PSCI_STAT_COUNT/RESIDENCY at the system power domain level.
  271. */
  272. static int css_translate_power_state_by_mpidr(u_register_t mpidr,
  273. unsigned int power_state,
  274. psci_power_state_t *output_state)
  275. {
  276. return arm_validate_power_state(power_state, output_state);
  277. }
  278. /*
  279. * Setup the SGI interrupt that will be used trigger the execution of power
  280. * down sequence for all the secondary cores. This interrupt is setup to be
  281. * handled in EL3 context at a priority defined by the platform.
  282. */
  283. void css_setup_cpu_pwr_down_intr(void)
  284. {
  285. #if CSS_SYSTEM_GRACEFUL_RESET
  286. plat_ic_set_interrupt_type(CSS_CPU_PWR_DOWN_REQ_INTR, INTR_TYPE_EL3);
  287. plat_ic_set_interrupt_priority(CSS_CPU_PWR_DOWN_REQ_INTR,
  288. PLAT_REBOOT_PRI);
  289. plat_ic_enable_interrupt(CSS_CPU_PWR_DOWN_REQ_INTR);
  290. #endif
  291. }
  292. /*
  293. * For a graceful shutdown/reboot, each CPU in the system should do their power
  294. * down sequence. On a PSCI shutdown/reboot request, only one CPU gets an
  295. * opportunity to do the powerdown sequence. To achieve graceful reset, of all
  296. * cores in the system, the CPU gets the opportunity raise warm reboot SGI to
  297. * rest of the CPUs which are online. Add handler for the reboot SGI where the
  298. * rest of the CPU execute the powerdown sequence.
  299. */
  300. int css_reboot_interrupt_handler(uint32_t intr_raw, uint32_t flags,
  301. void *handle, void *cookie)
  302. {
  303. assert(intr_raw == CSS_CPU_PWR_DOWN_REQ_INTR);
  304. /* Deactivate warm reboot SGI */
  305. plat_ic_end_of_interrupt(CSS_CPU_PWR_DOWN_REQ_INTR);
  306. /*
  307. * Disable GIC CPU interface to prevent pending interrupt from waking
  308. * up the AP from WFI.
  309. */
  310. plat_arm_gic_cpuif_disable();
  311. plat_arm_gic_redistif_off();
  312. psci_pwrdown_cpu(PLAT_MAX_PWR_LVL);
  313. dmbsy();
  314. wfi();
  315. return 0;
  316. }
  317. /*******************************************************************************
  318. * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
  319. * platform will take care of registering the handlers with PSCI.
  320. ******************************************************************************/
  321. plat_psci_ops_t plat_arm_psci_pm_ops = {
  322. .pwr_domain_on = css_pwr_domain_on,
  323. .pwr_domain_on_finish = css_pwr_domain_on_finish,
  324. .pwr_domain_on_finish_late = css_pwr_domain_on_finish_late,
  325. .pwr_domain_off = css_pwr_domain_off,
  326. .cpu_standby = css_cpu_standby,
  327. .pwr_domain_suspend = css_pwr_domain_suspend,
  328. .pwr_domain_suspend_finish = css_pwr_domain_suspend_finish,
  329. .system_off = css_system_off,
  330. .system_reset = css_system_reset,
  331. .validate_power_state = css_validate_power_state,
  332. .validate_ns_entrypoint = arm_validate_psci_entrypoint,
  333. .translate_power_state_by_mpidr = css_translate_power_state_by_mpidr,
  334. .get_node_hw_state = css_node_hw_state,
  335. .get_sys_suspend_power_state = css_get_sys_suspend_power_state,
  336. #if defined(PLAT_ARM_MEM_PROT_ADDR)
  337. .mem_protect_chk = arm_psci_mem_protect_chk,
  338. .read_mem_protect = arm_psci_read_mem_protect,
  339. .write_mem_protect = arm_nor_psci_write_mem_protect,
  340. #endif
  341. #if CSS_USE_SCMI_SDS_DRIVER
  342. .system_reset2 = css_system_reset2,
  343. #endif
  344. };