xref: /rk3399_ARM-atf/plat/arm/board/fvp/fvp_pm.c (revision da305ec75dedca5e8e939790ab02fe7c0ba999d5)
13fc4124cSDan Handley /*
2777f1f68SJayanth Dodderi Chidanand  * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
33fc4124cSDan Handley  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
53fc4124cSDan Handley  */
63fc4124cSDan Handley 
73fc4124cSDan Handley #include <assert.h>
809d40e0eSAntonio Nino Diaz 
909d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1009d40e0eSAntonio Nino Diaz #include <common/debug.h>
1109d40e0eSAntonio Nino Diaz #include <drivers/arm/gicv3.h>
12560293bbSAntonio Nino Diaz #include <drivers/arm/fvp/fvp_pwrc.h>
1309d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1409d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
15bd9344f6SAntonio Nino Diaz #include <plat/arm/common/arm_config.h>
16bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
17234bc7f8SAntonio Nino Diaz #include <platform_def.h>
1809d40e0eSAntonio Nino Diaz 
193fc4124cSDan Handley #include "fvp_private.h"
20609e053cSAmbroise Vincent #include "../drivers/arm/gic/v3/gicv3_private.h"
213fc4124cSDan Handley 
223fc4124cSDan Handley 
232204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
242204afdeSSoby Mathew /*
252204afdeSSoby Mathew  *  The table storing the valid idle power states. Ensure that the
262204afdeSSoby Mathew  *  array entries are populated in ascending order of state-id to
272204afdeSSoby Mathew  *  enable us to use binary search during power state validation.
282204afdeSSoby Mathew  *  The table must be terminated by a NULL entry.
292204afdeSSoby Mathew  */
302204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = {
312204afdeSSoby Mathew 	/* State-id - 0x01 */
322204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RET,
332204afdeSSoby Mathew 			ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
342204afdeSSoby Mathew 	/* State-id - 0x02 */
352204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
362204afdeSSoby Mathew 			ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
372204afdeSSoby Mathew 	/* State-id - 0x22 */
382204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
392204afdeSSoby Mathew 			ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
40e35a3fb5SSoby Mathew 	/* State-id - 0x222 */
41e35a3fb5SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
42e35a3fb5SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
432204afdeSSoby Mathew 	0,
442204afdeSSoby Mathew };
452204afdeSSoby Mathew #endif
462204afdeSSoby Mathew 
473fc4124cSDan Handley /*******************************************************************************
483fc4124cSDan Handley  * Function which implements the common FVP specific operations to power down a
493fc4124cSDan Handley  * cluster in response to a CPU_OFF or CPU_SUSPEND request.
503fc4124cSDan Handley  ******************************************************************************/
513fc4124cSDan Handley static void fvp_cluster_pwrdwn_common(void)
523fc4124cSDan Handley {
533fc4124cSDan Handley 	uint64_t mpidr = read_mpidr_el1();
543fc4124cSDan Handley 
553fc4124cSDan Handley 	/* Disable coherency if this cluster is to be turned off */
566355f234SVikram Kanigiri 	fvp_interconnect_disable();
573fc4124cSDan Handley 
583fc4124cSDan Handley 	/* Program the power controller to turn the cluster off */
593fc4124cSDan Handley 	fvp_pwrc_write_pcoffr(mpidr);
603fc4124cSDan Handley }
613fc4124cSDan Handley 
62e35a3fb5SSoby Mathew /*
63e35a3fb5SSoby Mathew  * Empty implementation of these hooks avoid setting the GICR_WAKER.Sleep bit
64e35a3fb5SSoby Mathew  * on ARM GICv3 implementations on FVP. This is required, because FVP does not
65e35a3fb5SSoby Mathew  * support SYSTEM_SUSPEND and it is `faked` in firmware. Hence, for wake up
66e35a3fb5SSoby Mathew  * from `fake` system suspend the GIC must not be powered off.
67e35a3fb5SSoby Mathew  */
68dc6aad2eSRoberto Vargas void arm_gicv3_distif_pre_save(unsigned int rdist_proc_num)
69e35a3fb5SSoby Mathew {}
70e35a3fb5SSoby Mathew 
71dc6aad2eSRoberto Vargas void arm_gicv3_distif_post_restore(unsigned int rdist_proc_num)
72e35a3fb5SSoby Mathew {}
73e35a3fb5SSoby Mathew 
74f14d1886SSoby Mathew static void fvp_power_domain_on_finish_common(const psci_power_state_t *target_state)
75f14d1886SSoby Mathew {
76f14d1886SSoby Mathew 	unsigned long mpidr;
77f14d1886SSoby Mathew 
78f14d1886SSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
79f14d1886SSoby Mathew 					ARM_LOCAL_STATE_OFF);
80f14d1886SSoby Mathew 
81f14d1886SSoby Mathew 	/* Get the mpidr for this cpu */
82f14d1886SSoby Mathew 	mpidr = read_mpidr_el1();
83f14d1886SSoby Mathew 
84f14d1886SSoby Mathew 	/* Perform the common cluster specific operations */
85f14d1886SSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
86f14d1886SSoby Mathew 					ARM_LOCAL_STATE_OFF) {
87f14d1886SSoby Mathew 		/*
88f14d1886SSoby Mathew 		 * This CPU might have woken up whilst the cluster was
89f14d1886SSoby Mathew 		 * attempting to power down. In this case the FVP power
90f14d1886SSoby Mathew 		 * controller will have a pending cluster power off request
91f14d1886SSoby Mathew 		 * which needs to be cleared by writing to the PPONR register.
92f14d1886SSoby Mathew 		 * This prevents the power controller from interpreting a
93f14d1886SSoby Mathew 		 * subsequent entry of this cpu into a simple wfi as a power
94f14d1886SSoby Mathew 		 * down request.
95f14d1886SSoby Mathew 		 */
96f14d1886SSoby Mathew 		fvp_pwrc_write_pponr(mpidr);
97f14d1886SSoby Mathew 
98f14d1886SSoby Mathew 		/* Enable coherency if this cluster was off */
996355f234SVikram Kanigiri 		fvp_interconnect_enable();
100f14d1886SSoby Mathew 	}
101e35a3fb5SSoby Mathew 	/* Perform the common system specific operations */
102e35a3fb5SSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL2] ==
103e35a3fb5SSoby Mathew 						ARM_LOCAL_STATE_OFF)
104e35a3fb5SSoby Mathew 		arm_system_pwr_domain_resume();
105f14d1886SSoby Mathew 
106f14d1886SSoby Mathew 	/*
107f14d1886SSoby Mathew 	 * Clear PWKUPR.WEN bit to ensure interrupts do not interfere
108f14d1886SSoby Mathew 	 * with a cpu power down unless the bit is set again
109f14d1886SSoby Mathew 	 */
110f14d1886SSoby Mathew 	fvp_pwrc_clr_wen(mpidr);
111f14d1886SSoby Mathew }
112f14d1886SSoby Mathew 
1133fc4124cSDan Handley /*******************************************************************************
11438dce70fSSoby Mathew  * FVP handler called when a CPU is about to enter standby.
1153fc4124cSDan Handley  ******************************************************************************/
1161af540efSRoberto Vargas static void fvp_cpu_standby(plat_local_state_t cpu_state)
1173fc4124cSDan Handley {
1183202ce8bSAlexei Fedorov 	u_register_t scr = read_scr_el3();
11938dce70fSSoby Mathew 
12038dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
12138dce70fSSoby Mathew 
1223fc4124cSDan Handley 	/*
1233202ce8bSAlexei Fedorov 	 * Enable the Non-secure interrupt to wake the CPU.
1243202ce8bSAlexei Fedorov 	 * In GICv3 affinity routing mode, the Non-secure Group 1 interrupts
1253202ce8bSAlexei Fedorov 	 * use Physical FIQ at EL3 whereas in GICv2, Physical IRQ is used.
1263202ce8bSAlexei Fedorov 	 * Enabling both the bits works for both GICv2 mode and GICv3 affinity
1273202ce8bSAlexei Fedorov 	 * routing mode.
1283202ce8bSAlexei Fedorov 	 */
1293202ce8bSAlexei Fedorov 	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
1303202ce8bSAlexei Fedorov 	isb();
1313202ce8bSAlexei Fedorov 
1323202ce8bSAlexei Fedorov 	/*
1333202ce8bSAlexei Fedorov 	 * Enter standby state.
1343202ce8bSAlexei Fedorov 	 * dsb is good practice before using wfi to enter low power states.
1353fc4124cSDan Handley 	 */
1363fc4124cSDan Handley 	dsb();
1373fc4124cSDan Handley 	wfi();
1383202ce8bSAlexei Fedorov 
1393202ce8bSAlexei Fedorov 	/*
1403202ce8bSAlexei Fedorov 	 * Restore SCR_EL3 to the original value, synchronisation of SCR_EL3
1413202ce8bSAlexei Fedorov 	 * is done by eret in el3_exit() to save some execution cycles.
1423202ce8bSAlexei Fedorov 	 */
1433202ce8bSAlexei Fedorov 	write_scr_el3(scr);
1443fc4124cSDan Handley }
1453fc4124cSDan Handley 
1463fc4124cSDan Handley /*******************************************************************************
14738dce70fSSoby Mathew  * FVP handler called when a power domain is about to be turned on. The
14838dce70fSSoby Mathew  * mpidr determines the CPU to be turned on.
1493fc4124cSDan Handley  ******************************************************************************/
1501af540efSRoberto Vargas static int fvp_pwr_domain_on(u_register_t mpidr)
1513fc4124cSDan Handley {
1523fc4124cSDan Handley 	int rc = PSCI_E_SUCCESS;
1533fc4124cSDan Handley 	unsigned int psysr;
1543fc4124cSDan Handley 
1553fc4124cSDan Handley 	/*
1560f09c8f7SSandrine Bailleux 	 * Ensure that we do not cancel an inflight power off request for the
1570f09c8f7SSandrine Bailleux 	 * target cpu. That would leave it in a zombie wfi. Wait for it to power
1580f09c8f7SSandrine Bailleux 	 * off and then program the power controller to turn that CPU on.
1593fc4124cSDan Handley 	 */
1603fc4124cSDan Handley 	do {
1613fc4124cSDan Handley 		psysr = fvp_pwrc_read_psysr(mpidr);
162e02f469fSSathees Balya 	} while ((psysr & PSYSR_AFF_L0) != 0U);
1633fc4124cSDan Handley 
1643fc4124cSDan Handley 	fvp_pwrc_write_pponr(mpidr);
1653fc4124cSDan Handley 	return rc;
1663fc4124cSDan Handley }
1673fc4124cSDan Handley 
1683fc4124cSDan Handley /*******************************************************************************
16938dce70fSSoby Mathew  * FVP handler called when a power domain is about to be turned off. The
17038dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
1713fc4124cSDan Handley  ******************************************************************************/
1721af540efSRoberto Vargas static void fvp_pwr_domain_off(const psci_power_state_t *target_state)
1733fc4124cSDan Handley {
17438dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
17538dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF);
1763fc4124cSDan Handley 
1773fc4124cSDan Handley 	/*
17838dce70fSSoby Mathew 	 * If execution reaches this stage then this power domain will be
17938dce70fSSoby Mathew 	 * suspended. Perform at least the cpu specific actions followed
18038dce70fSSoby Mathew 	 * by the cluster specific operations if applicable.
1813fc4124cSDan Handley 	 */
18274a9578cSJeenu Viswambharan 
18374a9578cSJeenu Viswambharan 	/* Prevent interrupts from spuriously waking up this cpu */
18474a9578cSJeenu Viswambharan 	plat_arm_gic_cpuif_disable();
18574a9578cSJeenu Viswambharan 
18674a9578cSJeenu Viswambharan 	/* Turn redistributor off */
18774a9578cSJeenu Viswambharan 	plat_arm_gic_redistif_off();
18874a9578cSJeenu Viswambharan 
18974a9578cSJeenu Viswambharan 	/* Program the power controller to power off this cpu. */
19074a9578cSJeenu Viswambharan 	fvp_pwrc_write_ppoffr(read_mpidr_el1());
1913fc4124cSDan Handley 
19238dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
19338dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF)
1943fc4124cSDan Handley 		fvp_cluster_pwrdwn_common();
1953fc4124cSDan Handley 
1963fc4124cSDan Handley }
1973fc4124cSDan Handley 
1983fc4124cSDan Handley /*******************************************************************************
19938dce70fSSoby Mathew  * FVP handler called when a power domain is about to be suspended. The
20038dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
2013fc4124cSDan Handley  ******************************************************************************/
2021af540efSRoberto Vargas static void fvp_pwr_domain_suspend(const psci_power_state_t *target_state)
2033fc4124cSDan Handley {
2043fc4124cSDan Handley 	unsigned long mpidr;
2053fc4124cSDan Handley 
20638dce70fSSoby Mathew 	/*
20738dce70fSSoby Mathew 	 * FVP has retention only at cpu level. Just return
20838dce70fSSoby Mathew 	 * as nothing is to be done for retention.
20938dce70fSSoby Mathew 	 */
21038dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
21138dce70fSSoby Mathew 					ARM_LOCAL_STATE_RET)
2123fc4124cSDan Handley 		return;
2133fc4124cSDan Handley 
21438dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
21538dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF);
21638dce70fSSoby Mathew 
2173fc4124cSDan Handley 	/* Get the mpidr for this cpu */
2183fc4124cSDan Handley 	mpidr = read_mpidr_el1();
2193fc4124cSDan Handley 
2203fc4124cSDan Handley 	/* Program the power controller to enable wakeup interrupts. */
2213fc4124cSDan Handley 	fvp_pwrc_set_wen(mpidr);
2223fc4124cSDan Handley 
22374a9578cSJeenu Viswambharan 	/* Prevent interrupts from spuriously waking up this cpu */
22474a9578cSJeenu Viswambharan 	plat_arm_gic_cpuif_disable();
22574a9578cSJeenu Viswambharan 
22674a9578cSJeenu Viswambharan 	/*
22774a9578cSJeenu Viswambharan 	 * The Redistributor is not powered off as it can potentially prevent
22874a9578cSJeenu Viswambharan 	 * wake up events reaching the CPUIF and/or might lead to losing
22974a9578cSJeenu Viswambharan 	 * register context.
23074a9578cSJeenu Viswambharan 	 */
23174a9578cSJeenu Viswambharan 
2323fc4124cSDan Handley 	/* Perform the common cluster specific operations */
23338dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
23438dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF)
2353fc4124cSDan Handley 		fvp_cluster_pwrdwn_common();
236e35a3fb5SSoby Mathew 
237e35a3fb5SSoby Mathew 	/* Perform the common system specific operations */
238e35a3fb5SSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL2] ==
239e35a3fb5SSoby Mathew 						ARM_LOCAL_STATE_OFF)
240e35a3fb5SSoby Mathew 		arm_system_pwr_domain_save();
241e35a3fb5SSoby Mathew 
242e35a3fb5SSoby Mathew 	/* Program the power controller to power off this cpu. */
243e35a3fb5SSoby Mathew 	fvp_pwrc_write_ppoffr(read_mpidr_el1());
244e75cc247SWing Li 
245e75cc247SWing Li 	return;
2463fc4124cSDan Handley }
2473fc4124cSDan Handley 
2483fc4124cSDan Handley /*******************************************************************************
24938dce70fSSoby Mathew  * FVP handler called when a power domain has just been powered on after
25038dce70fSSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
25138dce70fSSoby Mathew  * each level has woken up from.
2523fc4124cSDan Handley  ******************************************************************************/
2531af540efSRoberto Vargas static void fvp_pwr_domain_on_finish(const psci_power_state_t *target_state)
2543fc4124cSDan Handley {
255f14d1886SSoby Mathew 	fvp_power_domain_on_finish_common(target_state);
2563fc4124cSDan Handley 
2576806cd23SMadhukar Pappireddy }
2586806cd23SMadhukar Pappireddy 
2596806cd23SMadhukar Pappireddy /*******************************************************************************
2606806cd23SMadhukar Pappireddy  * FVP handler called when a power domain has just been powered on and the cpu
2616806cd23SMadhukar Pappireddy  * and its cluster are fully participating in coherent transaction on the
2626806cd23SMadhukar Pappireddy  * interconnect. Data cache must be enabled for CPU at this point.
2636806cd23SMadhukar Pappireddy  ******************************************************************************/
2646806cd23SMadhukar Pappireddy static void fvp_pwr_domain_on_finish_late(const psci_power_state_t *target_state)
2656806cd23SMadhukar Pappireddy {
2666806cd23SMadhukar Pappireddy 	/* Program GIC per-cpu distributor or re-distributor interface */
26727573c59SAchin Gupta 	plat_arm_gic_pcpu_init();
26827573c59SAchin Gupta 
2696806cd23SMadhukar Pappireddy 	/* Enable GIC CPU interface */
27027573c59SAchin Gupta 	plat_arm_gic_cpuif_enable();
2713fc4124cSDan Handley }
2723fc4124cSDan Handley 
2733fc4124cSDan Handley /*******************************************************************************
27438dce70fSSoby Mathew  * FVP handler called when a power domain has just been powered on after
27538dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
27638dce70fSSoby Mathew  * that each level has woken up from.
2773fc4124cSDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
2783fc4124cSDan Handley  * context. Need to implement a separate suspend finisher.
2793fc4124cSDan Handley  ******************************************************************************/
2801af540efSRoberto Vargas static void fvp_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
2813fc4124cSDan Handley {
28238dce70fSSoby Mathew 	/*
28338dce70fSSoby Mathew 	 * Nothing to be done on waking up from retention from CPU level.
28438dce70fSSoby Mathew 	 */
28538dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
28638dce70fSSoby Mathew 					ARM_LOCAL_STATE_RET)
28738dce70fSSoby Mathew 		return;
28838dce70fSSoby Mathew 
289f14d1886SSoby Mathew 	fvp_power_domain_on_finish_common(target_state);
290f14d1886SSoby Mathew 
2916806cd23SMadhukar Pappireddy 	/* Enable GIC CPU interface */
29227573c59SAchin Gupta 	plat_arm_gic_cpuif_enable();
2933fc4124cSDan Handley }
2943fc4124cSDan Handley 
2953fc4124cSDan Handley /*******************************************************************************
2963fc4124cSDan Handley  * FVP handlers to shutdown/reboot the system
2973fc4124cSDan Handley  ******************************************************************************/
298*da305ec7SBoyan Karatotev static void fvp_system_off(void)
2993fc4124cSDan Handley {
3003fc4124cSDan Handley 	/* Write the System Configuration Control Register */
3013fc4124cSDan Handley 	mmio_write_32(V2M_SYSREGS_BASE + V2M_SYS_CFGCTRL,
3023fc4124cSDan Handley 		V2M_CFGCTRL_START |
3033fc4124cSDan Handley 		V2M_CFGCTRL_RW |
3043fc4124cSDan Handley 		V2M_CFGCTRL_FUNC(V2M_FUNC_SHUTDOWN));
3053fc4124cSDan Handley }
3063fc4124cSDan Handley 
307*da305ec7SBoyan Karatotev static void fvp_system_reset(void)
3083fc4124cSDan Handley {
3093fc4124cSDan Handley 	/* Write the System Configuration Control Register */
3103fc4124cSDan Handley 	mmio_write_32(V2M_SYSREGS_BASE + V2M_SYS_CFGCTRL,
3113fc4124cSDan Handley 		V2M_CFGCTRL_START |
3123fc4124cSDan Handley 		V2M_CFGCTRL_RW |
3133fc4124cSDan Handley 		V2M_CFGCTRL_FUNC(V2M_FUNC_REBOOT));
3143fc4124cSDan Handley }
3153fc4124cSDan Handley 
3161298ae02SJeenu Viswambharan static int fvp_node_hw_state(u_register_t target_cpu,
3171298ae02SJeenu Viswambharan 			     unsigned int power_level)
3181298ae02SJeenu Viswambharan {
3191298ae02SJeenu Viswambharan 	unsigned int psysr;
320b9c3a8c0SGovindraj Raja 	int ret = 0;
3211298ae02SJeenu Viswambharan 
3221298ae02SJeenu Viswambharan 	/*
3231298ae02SJeenu Viswambharan 	 * The format of 'power_level' is implementation-defined, but 0 must
3241298ae02SJeenu Viswambharan 	 * mean a CPU. We also allow 1 to denote the cluster
3251298ae02SJeenu Viswambharan 	 */
326b9c3a8c0SGovindraj Raja 	if ((power_level < ARM_PWR_LVL0) || (power_level > ARM_PWR_LVL1))
3271298ae02SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
3281298ae02SJeenu Viswambharan 
3291298ae02SJeenu Viswambharan 	/*
3301298ae02SJeenu Viswambharan 	 * Read the status of the given MPDIR from FVP power controller. The
3311298ae02SJeenu Viswambharan 	 * power controller only gives us on/off status, so map that to expected
3321298ae02SJeenu Viswambharan 	 * return values of the PSCI call
3331298ae02SJeenu Viswambharan 	 */
3341298ae02SJeenu Viswambharan 	psysr = fvp_pwrc_read_psysr(target_cpu);
3351298ae02SJeenu Viswambharan 	if (psysr == PSYSR_INVALID)
3361298ae02SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
3371298ae02SJeenu Viswambharan 
338649c48f5SJonathan Wright 	if (power_level == ARM_PWR_LVL0) {
339e02f469fSSathees Balya 		ret = ((psysr & PSYSR_AFF_L0) != 0U) ? HW_ON : HW_OFF;
340b9c3a8c0SGovindraj Raja 	} else if (power_level == ARM_PWR_LVL1) {
341b9c3a8c0SGovindraj Raja 	/*
342b9c3a8c0SGovindraj Raja 	 * Use L1 affinity if MPIDR_EL1.MT bit is not set else use L2 affinity.
343b9c3a8c0SGovindraj Raja 	 */
344b9c3a8c0SGovindraj Raja 		if ((read_mpidr_el1() & MPIDR_MT_MASK) == 0U)
345e02f469fSSathees Balya 			ret = ((psysr & PSYSR_AFF_L1) != 0U) ? HW_ON : HW_OFF;
346b9c3a8c0SGovindraj Raja 		else
347b9c3a8c0SGovindraj Raja 			ret = ((psysr & PSYSR_AFF_L2) != 0U) ? HW_ON : HW_OFF;
3481298ae02SJeenu Viswambharan 	}
3491298ae02SJeenu Viswambharan 
3501298ae02SJeenu Viswambharan 	return ret;
3511298ae02SJeenu Viswambharan }
3521298ae02SJeenu Viswambharan 
353e35a3fb5SSoby Mathew /*
354e35a3fb5SSoby Mathew  * The FVP doesn't truly support power management at SYSTEM power domain. The
355e35a3fb5SSoby Mathew  * SYSTEM_SUSPEND will be down-graded to the cluster level within the platform
356e35a3fb5SSoby Mathew  * layer. The `fake` SYSTEM_SUSPEND allows us to validate some of the driver
357e35a3fb5SSoby Mathew  * save and restore sequences on FVP.
358e35a3fb5SSoby Mathew  */
3591af540efSRoberto Vargas #if !ARM_BL31_IN_DRAM
3601af540efSRoberto Vargas static void fvp_get_sys_suspend_power_state(psci_power_state_t *req_state)
361e35a3fb5SSoby Mathew {
362e35a3fb5SSoby Mathew 	unsigned int i;
363e35a3fb5SSoby Mathew 
364e35a3fb5SSoby Mathew 	for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
365e35a3fb5SSoby Mathew 		req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
366e0ef05bbSWing Li 
367e0ef05bbSWing Li #if PSCI_OS_INIT_MODE
368e0ef05bbSWing Li 	req_state->last_at_pwrlvl = PLAT_MAX_PWR_LVL;
369e0ef05bbSWing Li #endif
370e35a3fb5SSoby Mathew }
3711af540efSRoberto Vargas #endif
372e35a3fb5SSoby Mathew 
373e35a3fb5SSoby Mathew /*******************************************************************************
374e35a3fb5SSoby Mathew  * Handler to filter PSCI requests.
375e35a3fb5SSoby Mathew  ******************************************************************************/
376e35a3fb5SSoby Mathew /*
377e35a3fb5SSoby Mathew  * The system power domain suspend is only supported only via
378e35a3fb5SSoby Mathew  * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain
379e35a3fb5SSoby Mathew  * will be downgraded to the lower level.
380e35a3fb5SSoby Mathew  */
381e35a3fb5SSoby Mathew static int fvp_validate_power_state(unsigned int power_state,
382e35a3fb5SSoby Mathew 			    psci_power_state_t *req_state)
383e35a3fb5SSoby Mathew {
384e35a3fb5SSoby Mathew 	int rc;
385e35a3fb5SSoby Mathew 	rc = arm_validate_power_state(power_state, req_state);
386e35a3fb5SSoby Mathew 
387e35a3fb5SSoby Mathew 	/*
388e35a3fb5SSoby Mathew 	 * Ensure that the system power domain level is never suspended
389e35a3fb5SSoby Mathew 	 * via PSCI CPU SUSPEND API. Currently system suspend is only
390e35a3fb5SSoby Mathew 	 * supported via PSCI SYSTEM SUSPEND API.
391e35a3fb5SSoby Mathew 	 */
392e35a3fb5SSoby Mathew 	req_state->pwr_domain_state[ARM_PWR_LVL2] = ARM_LOCAL_STATE_RUN;
393e35a3fb5SSoby Mathew 	return rc;
394e35a3fb5SSoby Mathew }
395e35a3fb5SSoby Mathew 
396e35a3fb5SSoby Mathew /*
397e35a3fb5SSoby Mathew  * Custom `translate_power_state_by_mpidr` handler for FVP. Unlike in the
398e35a3fb5SSoby Mathew  * `fvp_validate_power_state`, we do not downgrade the system power
399e35a3fb5SSoby Mathew  * domain level request in `power_state` as it will be used to query the
400e35a3fb5SSoby Mathew  * PSCI_STAT_COUNT/RESIDENCY at the system power domain level.
401e35a3fb5SSoby Mathew  */
402e35a3fb5SSoby Mathew static int fvp_translate_power_state_by_mpidr(u_register_t mpidr,
403e35a3fb5SSoby Mathew 		unsigned int power_state,
404e35a3fb5SSoby Mathew 		psci_power_state_t *output_state)
405e35a3fb5SSoby Mathew {
406e35a3fb5SSoby Mathew 	return arm_validate_power_state(power_state, output_state);
407e35a3fb5SSoby Mathew }
408e35a3fb5SSoby Mathew 
4093fc4124cSDan Handley /*******************************************************************************
410785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
411785fb92bSSoby Mathew  * platform layer will take care of registering the handlers with PSCI.
4123fc4124cSDan Handley  ******************************************************************************/
4135486a965SSoby Mathew plat_psci_ops_t plat_arm_psci_pm_ops = {
41438dce70fSSoby Mathew 	.cpu_standby = fvp_cpu_standby,
41538dce70fSSoby Mathew 	.pwr_domain_on = fvp_pwr_domain_on,
41638dce70fSSoby Mathew 	.pwr_domain_off = fvp_pwr_domain_off,
41738dce70fSSoby Mathew 	.pwr_domain_suspend = fvp_pwr_domain_suspend,
41838dce70fSSoby Mathew 	.pwr_domain_on_finish = fvp_pwr_domain_on_finish,
4196806cd23SMadhukar Pappireddy 	.pwr_domain_on_finish_late = fvp_pwr_domain_on_finish_late,
42038dce70fSSoby Mathew 	.pwr_domain_suspend_finish = fvp_pwr_domain_suspend_finish,
4213fc4124cSDan Handley 	.system_off = fvp_system_off,
4223fc4124cSDan Handley 	.system_reset = fvp_system_reset,
423e35a3fb5SSoby Mathew 	.validate_power_state = fvp_validate_power_state,
42471e7a4e5SJeenu Viswambharan 	.validate_ns_entrypoint = arm_validate_psci_entrypoint,
425e35a3fb5SSoby Mathew 	.translate_power_state_by_mpidr = fvp_translate_power_state_by_mpidr,
426f145403cSRoberto Vargas 	.get_node_hw_state = fvp_node_hw_state,
4277d44ac1eSAntonio Nino Diaz #if !ARM_BL31_IN_DRAM
4287d44ac1eSAntonio Nino Diaz 	/*
4297d44ac1eSAntonio Nino Diaz 	 * The TrustZone Controller is set up during the warmboot sequence after
4307d44ac1eSAntonio Nino Diaz 	 * resuming the CPU from a SYSTEM_SUSPEND. If BL31 is located in SRAM
4317d44ac1eSAntonio Nino Diaz 	 * this is  not a problem but, if it is in TZC-secured DRAM, it tries to
4327d44ac1eSAntonio Nino Diaz 	 * reconfigure the same memory it is running on, causing an exception.
4337d44ac1eSAntonio Nino Diaz 	 */
434e35a3fb5SSoby Mathew 	.get_sys_suspend_power_state = fvp_get_sys_suspend_power_state,
4357d44ac1eSAntonio Nino Diaz #endif
436f145403cSRoberto Vargas 	.mem_protect_chk	= arm_psci_mem_protect_chk,
437f145403cSRoberto Vargas 	.read_mem_protect	= arm_psci_read_mem_protect,
438f145403cSRoberto Vargas 	.write_mem_protect	= arm_nor_psci_write_mem_protect,
4393fc4124cSDan Handley };
44089f2e589SChandni Cherukuri 
44189f2e589SChandni Cherukuri const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
44289f2e589SChandni Cherukuri {
44389f2e589SChandni Cherukuri 	return ops;
44489f2e589SChandni Cherukuri }
445