xref: /rk3399_ARM-atf/plat/arm/board/fvp/fvp_pm.c (revision 2204afded5cf9557ef1bb934fd15a74b9fb42244)
13fc4124cSDan Handley /*
23fc4124cSDan Handley  * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
33fc4124cSDan Handley  *
43fc4124cSDan Handley  * Redistribution and use in source and binary forms, with or without
53fc4124cSDan Handley  * modification, are permitted provided that the following conditions are met:
63fc4124cSDan Handley  *
73fc4124cSDan Handley  * Redistributions of source code must retain the above copyright notice, this
83fc4124cSDan Handley  * list of conditions and the following disclaimer.
93fc4124cSDan Handley  *
103fc4124cSDan Handley  * Redistributions in binary form must reproduce the above copyright notice,
113fc4124cSDan Handley  * this list of conditions and the following disclaimer in the documentation
123fc4124cSDan Handley  * and/or other materials provided with the distribution.
133fc4124cSDan Handley  *
143fc4124cSDan Handley  * Neither the name of ARM nor the names of its contributors may be used
153fc4124cSDan Handley  * to endorse or promote products derived from this software without specific
163fc4124cSDan Handley  * prior written permission.
173fc4124cSDan Handley  *
183fc4124cSDan Handley  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
193fc4124cSDan Handley  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
203fc4124cSDan Handley  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
213fc4124cSDan Handley  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
223fc4124cSDan Handley  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
233fc4124cSDan Handley  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
243fc4124cSDan Handley  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
253fc4124cSDan Handley  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
263fc4124cSDan Handley  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
273fc4124cSDan Handley  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
283fc4124cSDan Handley  * POSSIBILITY OF SUCH DAMAGE.
293fc4124cSDan Handley  */
303fc4124cSDan Handley 
313fc4124cSDan Handley #include <arch_helpers.h>
323fc4124cSDan Handley #include <arm_config.h>
333fc4124cSDan Handley #include <arm_gic.h>
343fc4124cSDan Handley #include <assert.h>
353fc4124cSDan Handley #include <debug.h>
363fc4124cSDan Handley #include <errno.h>
373fc4124cSDan Handley #include <mmio.h>
383fc4124cSDan Handley #include <platform.h>
393fc4124cSDan Handley #include <plat_arm.h>
403fc4124cSDan Handley #include <psci.h>
413fc4124cSDan Handley #include <v2m_def.h>
423fc4124cSDan Handley #include "drivers/pwrc/fvp_pwrc.h"
433fc4124cSDan Handley #include "fvp_def.h"
443fc4124cSDan Handley #include "fvp_private.h"
453fc4124cSDan Handley 
4638dce70fSSoby Mathew unsigned long wakeup_address;
473fc4124cSDan Handley 
483fc4124cSDan Handley typedef volatile struct mailbox {
493fc4124cSDan Handley 	unsigned long value __aligned(CACHE_WRITEBACK_GRANULE);
503fc4124cSDan Handley } mailbox_t;
513fc4124cSDan Handley 
52*2204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
53*2204afdeSSoby Mathew /*
54*2204afdeSSoby Mathew  *  The table storing the valid idle power states. Ensure that the
55*2204afdeSSoby Mathew  *  array entries are populated in ascending order of state-id to
56*2204afdeSSoby Mathew  *  enable us to use binary search during power state validation.
57*2204afdeSSoby Mathew  *  The table must be terminated by a NULL entry.
58*2204afdeSSoby Mathew  */
59*2204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = {
60*2204afdeSSoby Mathew 	/* State-id - 0x01 */
61*2204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RET,
62*2204afdeSSoby Mathew 			ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
63*2204afdeSSoby Mathew 	/* State-id - 0x02 */
64*2204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
65*2204afdeSSoby Mathew 			ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
66*2204afdeSSoby Mathew 	/* State-id - 0x22 */
67*2204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
68*2204afdeSSoby Mathew 			ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
69*2204afdeSSoby Mathew 	0,
70*2204afdeSSoby Mathew };
71*2204afdeSSoby Mathew #endif
72*2204afdeSSoby Mathew 
733fc4124cSDan Handley /*******************************************************************************
743fc4124cSDan Handley  * Private FVP function to program the mailbox for a cpu before it is released
753fc4124cSDan Handley  * from reset.
763fc4124cSDan Handley  ******************************************************************************/
773fc4124cSDan Handley static void fvp_program_mailbox(uint64_t mpidr, uint64_t address)
783fc4124cSDan Handley {
793fc4124cSDan Handley 	uint64_t linear_id;
803fc4124cSDan Handley 	mailbox_t *fvp_mboxes;
813fc4124cSDan Handley 
8238dce70fSSoby Mathew 	linear_id = plat_arm_calc_core_pos(mpidr);
833fc4124cSDan Handley 	fvp_mboxes = (mailbox_t *)MBOX_BASE;
843fc4124cSDan Handley 	fvp_mboxes[linear_id].value = address;
853fc4124cSDan Handley 	flush_dcache_range((unsigned long) &fvp_mboxes[linear_id],
863fc4124cSDan Handley 			   sizeof(unsigned long));
873fc4124cSDan Handley }
883fc4124cSDan Handley 
893fc4124cSDan Handley /*******************************************************************************
903fc4124cSDan Handley  * Function which implements the common FVP specific operations to power down a
913fc4124cSDan Handley  * cpu in response to a CPU_OFF or CPU_SUSPEND request.
923fc4124cSDan Handley  ******************************************************************************/
933fc4124cSDan Handley static void fvp_cpu_pwrdwn_common(void)
943fc4124cSDan Handley {
953fc4124cSDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
963fc4124cSDan Handley 	arm_gic_cpuif_deactivate();
973fc4124cSDan Handley 
983fc4124cSDan Handley 	/* Program the power controller to power off this cpu. */
993fc4124cSDan Handley 	fvp_pwrc_write_ppoffr(read_mpidr_el1());
1003fc4124cSDan Handley }
1013fc4124cSDan Handley 
1023fc4124cSDan Handley /*******************************************************************************
1033fc4124cSDan Handley  * Function which implements the common FVP specific operations to power down a
1043fc4124cSDan Handley  * cluster in response to a CPU_OFF or CPU_SUSPEND request.
1053fc4124cSDan Handley  ******************************************************************************/
1063fc4124cSDan Handley static void fvp_cluster_pwrdwn_common(void)
1073fc4124cSDan Handley {
1083fc4124cSDan Handley 	uint64_t mpidr = read_mpidr_el1();
1093fc4124cSDan Handley 
1103fc4124cSDan Handley 	/* Disable coherency if this cluster is to be turned off */
1113fc4124cSDan Handley 	fvp_cci_disable();
1123fc4124cSDan Handley 
1133fc4124cSDan Handley 	/* Program the power controller to turn the cluster off */
1143fc4124cSDan Handley 	fvp_pwrc_write_pcoffr(mpidr);
1153fc4124cSDan Handley }
1163fc4124cSDan Handley 
1173fc4124cSDan Handley /*******************************************************************************
11838dce70fSSoby Mathew  * FVP handler called when a CPU is about to enter standby.
1193fc4124cSDan Handley  ******************************************************************************/
12038dce70fSSoby Mathew void fvp_cpu_standby(plat_local_state_t cpu_state)
1213fc4124cSDan Handley {
12238dce70fSSoby Mathew 
12338dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
12438dce70fSSoby Mathew 
1253fc4124cSDan Handley 	/*
1263fc4124cSDan Handley 	 * Enter standby state
1273fc4124cSDan Handley 	 * dsb is good practice before using wfi to enter low power states
1283fc4124cSDan Handley 	 */
1293fc4124cSDan Handley 	dsb();
1303fc4124cSDan Handley 	wfi();
1313fc4124cSDan Handley }
1323fc4124cSDan Handley 
1333fc4124cSDan Handley /*******************************************************************************
13438dce70fSSoby Mathew  * FVP handler called when a power domain is about to be turned on. The
13538dce70fSSoby Mathew  * mpidr determines the CPU to be turned on.
1363fc4124cSDan Handley  ******************************************************************************/
13738dce70fSSoby Mathew int fvp_pwr_domain_on(u_register_t mpidr)
1383fc4124cSDan Handley {
1393fc4124cSDan Handley 	int rc = PSCI_E_SUCCESS;
1403fc4124cSDan Handley 	unsigned int psysr;
1413fc4124cSDan Handley 
1423fc4124cSDan Handley 	/*
1433fc4124cSDan Handley 	 * Ensure that we do not cancel an inflight power off request
1443fc4124cSDan Handley 	 * for the target cpu. That would leave it in a zombie wfi.
1453fc4124cSDan Handley 	 * Wait for it to power off, program the jump address for the
1463fc4124cSDan Handley 	 * target cpu and then program the power controller to turn
1473fc4124cSDan Handley 	 * that cpu on
1483fc4124cSDan Handley 	 */
1493fc4124cSDan Handley 	do {
1503fc4124cSDan Handley 		psysr = fvp_pwrc_read_psysr(mpidr);
1513fc4124cSDan Handley 	} while (psysr & PSYSR_AFF_L0);
1523fc4124cSDan Handley 
15338dce70fSSoby Mathew 	fvp_program_mailbox(mpidr, wakeup_address);
1543fc4124cSDan Handley 	fvp_pwrc_write_pponr(mpidr);
1553fc4124cSDan Handley 
1563fc4124cSDan Handley 	return rc;
1573fc4124cSDan Handley }
1583fc4124cSDan Handley 
1593fc4124cSDan Handley /*******************************************************************************
16038dce70fSSoby Mathew  * FVP handler called when a power domain is about to be turned off. The
16138dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
1623fc4124cSDan Handley  ******************************************************************************/
16338dce70fSSoby Mathew void fvp_pwr_domain_off(const psci_power_state_t *target_state)
1643fc4124cSDan Handley {
16538dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
16638dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF);
1673fc4124cSDan Handley 
1683fc4124cSDan Handley 	/*
16938dce70fSSoby Mathew 	 * If execution reaches this stage then this power domain will be
17038dce70fSSoby Mathew 	 * suspended. Perform at least the cpu specific actions followed
17138dce70fSSoby Mathew 	 * by the cluster specific operations if applicable.
1723fc4124cSDan Handley 	 */
1733fc4124cSDan Handley 	fvp_cpu_pwrdwn_common();
1743fc4124cSDan Handley 
17538dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
17638dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF)
1773fc4124cSDan Handley 		fvp_cluster_pwrdwn_common();
1783fc4124cSDan Handley 
1793fc4124cSDan Handley }
1803fc4124cSDan Handley 
1813fc4124cSDan Handley /*******************************************************************************
18238dce70fSSoby Mathew  * FVP handler called when a power domain is about to be suspended. The
18338dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
1843fc4124cSDan Handley  ******************************************************************************/
18538dce70fSSoby Mathew void fvp_pwr_domain_suspend(const psci_power_state_t *target_state)
1863fc4124cSDan Handley {
1873fc4124cSDan Handley 	unsigned long mpidr;
1883fc4124cSDan Handley 
18938dce70fSSoby Mathew 	/*
19038dce70fSSoby Mathew 	 * FVP has retention only at cpu level. Just return
19138dce70fSSoby Mathew 	 * as nothing is to be done for retention.
19238dce70fSSoby Mathew 	 */
19338dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
19438dce70fSSoby Mathew 					ARM_LOCAL_STATE_RET)
1953fc4124cSDan Handley 		return;
1963fc4124cSDan Handley 
19738dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
19838dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF);
19938dce70fSSoby Mathew 
2003fc4124cSDan Handley 	/* Get the mpidr for this cpu */
2013fc4124cSDan Handley 	mpidr = read_mpidr_el1();
2023fc4124cSDan Handley 
2033fc4124cSDan Handley 	/* Program the jump address for the this cpu */
20438dce70fSSoby Mathew 	fvp_program_mailbox(mpidr, wakeup_address);
2053fc4124cSDan Handley 
2063fc4124cSDan Handley 	/* Program the power controller to enable wakeup interrupts. */
2073fc4124cSDan Handley 	fvp_pwrc_set_wen(mpidr);
2083fc4124cSDan Handley 
2093fc4124cSDan Handley 	/* Perform the common cpu specific operations */
2103fc4124cSDan Handley 	fvp_cpu_pwrdwn_common();
2113fc4124cSDan Handley 
2123fc4124cSDan Handley 	/* Perform the common cluster specific operations */
21338dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
21438dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF)
2153fc4124cSDan Handley 		fvp_cluster_pwrdwn_common();
2163fc4124cSDan Handley }
2173fc4124cSDan Handley 
2183fc4124cSDan Handley /*******************************************************************************
21938dce70fSSoby Mathew  * FVP handler called when a power domain has just been powered on after
22038dce70fSSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
22138dce70fSSoby Mathew  * each level has woken up from.
2223fc4124cSDan Handley  ******************************************************************************/
22338dce70fSSoby Mathew void fvp_pwr_domain_on_finish(const psci_power_state_t *target_state)
2243fc4124cSDan Handley {
2253fc4124cSDan Handley 	unsigned long mpidr;
2263fc4124cSDan Handley 
22738dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
22838dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF);
2293fc4124cSDan Handley 
2303fc4124cSDan Handley 	/* Get the mpidr for this cpu */
2313fc4124cSDan Handley 	mpidr = read_mpidr_el1();
2323fc4124cSDan Handley 
2333fc4124cSDan Handley 	/* Perform the common cluster specific operations */
23438dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
23538dce70fSSoby Mathew 					ARM_LOCAL_STATE_OFF) {
2363fc4124cSDan Handley 		/*
2373fc4124cSDan Handley 		 * This CPU might have woken up whilst the cluster was
2383fc4124cSDan Handley 		 * attempting to power down. In this case the FVP power
2393fc4124cSDan Handley 		 * controller will have a pending cluster power off request
2403fc4124cSDan Handley 		 * which needs to be cleared by writing to the PPONR register.
2413fc4124cSDan Handley 		 * This prevents the power controller from interpreting a
2423fc4124cSDan Handley 		 * subsequent entry of this cpu into a simple wfi as a power
2433fc4124cSDan Handley 		 * down request.
2443fc4124cSDan Handley 		 */
2453fc4124cSDan Handley 		fvp_pwrc_write_pponr(mpidr);
2463fc4124cSDan Handley 
2473fc4124cSDan Handley 		/* Enable coherency if this cluster was off */
2483fc4124cSDan Handley 		fvp_cci_enable();
2493fc4124cSDan Handley 	}
2503fc4124cSDan Handley 
2513fc4124cSDan Handley 	/*
2523fc4124cSDan Handley 	 * Clear PWKUPR.WEN bit to ensure interrupts do not interfere
2533fc4124cSDan Handley 	 * with a cpu power down unless the bit is set again
2543fc4124cSDan Handley 	 */
2553fc4124cSDan Handley 	fvp_pwrc_clr_wen(mpidr);
2563fc4124cSDan Handley 
2573fc4124cSDan Handley 	/* Zero the jump address in the mailbox for this cpu */
2583fc4124cSDan Handley 	fvp_program_mailbox(mpidr, 0);
2593fc4124cSDan Handley 
2603fc4124cSDan Handley 	/* Enable the gic cpu interface */
2613fc4124cSDan Handley 	arm_gic_cpuif_setup();
2623fc4124cSDan Handley 
2633fc4124cSDan Handley 	/* TODO: This setup is needed only after a cold boot */
2643fc4124cSDan Handley 	arm_gic_pcpu_distif_setup();
2653fc4124cSDan Handley }
2663fc4124cSDan Handley 
2673fc4124cSDan Handley /*******************************************************************************
26838dce70fSSoby Mathew  * FVP handler called when a power domain has just been powered on after
26938dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
27038dce70fSSoby Mathew  * that each level has woken up from.
2713fc4124cSDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
2723fc4124cSDan Handley  * context. Need to implement a separate suspend finisher.
2733fc4124cSDan Handley  ******************************************************************************/
27438dce70fSSoby Mathew void fvp_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
2753fc4124cSDan Handley {
27638dce70fSSoby Mathew 	/*
27738dce70fSSoby Mathew 	 * Nothing to be done on waking up from retention from CPU level.
27838dce70fSSoby Mathew 	 */
27938dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
28038dce70fSSoby Mathew 					ARM_LOCAL_STATE_RET)
28138dce70fSSoby Mathew 		return;
28238dce70fSSoby Mathew 
28338dce70fSSoby Mathew 	fvp_pwr_domain_on_finish(target_state);
2843fc4124cSDan Handley }
2853fc4124cSDan Handley 
2863fc4124cSDan Handley /*******************************************************************************
2873fc4124cSDan Handley  * FVP handlers to shutdown/reboot the system
2883fc4124cSDan Handley  ******************************************************************************/
2893fc4124cSDan Handley static void __dead2 fvp_system_off(void)
2903fc4124cSDan Handley {
2913fc4124cSDan Handley 	/* Write the System Configuration Control Register */
2923fc4124cSDan Handley 	mmio_write_32(V2M_SYSREGS_BASE + V2M_SYS_CFGCTRL,
2933fc4124cSDan Handley 		V2M_CFGCTRL_START |
2943fc4124cSDan Handley 		V2M_CFGCTRL_RW |
2953fc4124cSDan Handley 		V2M_CFGCTRL_FUNC(V2M_FUNC_SHUTDOWN));
2963fc4124cSDan Handley 	wfi();
2973fc4124cSDan Handley 	ERROR("FVP System Off: operation not handled.\n");
2983fc4124cSDan Handley 	panic();
2993fc4124cSDan Handley }
3003fc4124cSDan Handley 
3013fc4124cSDan Handley static void __dead2 fvp_system_reset(void)
3023fc4124cSDan Handley {
3033fc4124cSDan Handley 	/* Write the System Configuration Control Register */
3043fc4124cSDan Handley 	mmio_write_32(V2M_SYSREGS_BASE + V2M_SYS_CFGCTRL,
3053fc4124cSDan Handley 		V2M_CFGCTRL_START |
3063fc4124cSDan Handley 		V2M_CFGCTRL_RW |
3073fc4124cSDan Handley 		V2M_CFGCTRL_FUNC(V2M_FUNC_REBOOT));
3083fc4124cSDan Handley 	wfi();
3093fc4124cSDan Handley 	ERROR("FVP System Reset: operation not handled.\n");
3103fc4124cSDan Handley 	panic();
3113fc4124cSDan Handley }
3123fc4124cSDan Handley 
3133fc4124cSDan Handley /*******************************************************************************
3143fc4124cSDan Handley  * Export the platform handlers to enable psci to invoke them
3153fc4124cSDan Handley  ******************************************************************************/
31638dce70fSSoby Mathew static const plat_psci_ops_t fvp_plat_psci_ops = {
31738dce70fSSoby Mathew 	.cpu_standby = fvp_cpu_standby,
31838dce70fSSoby Mathew 	.pwr_domain_on = fvp_pwr_domain_on,
31938dce70fSSoby Mathew 	.pwr_domain_off = fvp_pwr_domain_off,
32038dce70fSSoby Mathew 	.pwr_domain_suspend = fvp_pwr_domain_suspend,
32138dce70fSSoby Mathew 	.pwr_domain_on_finish = fvp_pwr_domain_on_finish,
32238dce70fSSoby Mathew 	.pwr_domain_suspend_finish = fvp_pwr_domain_suspend_finish,
3233fc4124cSDan Handley 	.system_off = fvp_system_off,
3243fc4124cSDan Handley 	.system_reset = fvp_system_reset,
3253fc4124cSDan Handley 	.validate_power_state = arm_validate_power_state
3263fc4124cSDan Handley };
3273fc4124cSDan Handley 
3283fc4124cSDan Handley /*******************************************************************************
32938dce70fSSoby Mathew  * Export the platform specific psci ops & initialize the fvp power controller
3303fc4124cSDan Handley  ******************************************************************************/
33138dce70fSSoby Mathew int plat_setup_psci_ops(uintptr_t sec_entrypoint,
33238dce70fSSoby Mathew 				const plat_psci_ops_t **psci_ops)
3333fc4124cSDan Handley {
33438dce70fSSoby Mathew 	*psci_ops = &fvp_plat_psci_ops;
33538dce70fSSoby Mathew 	wakeup_address = sec_entrypoint;
33638dce70fSSoby Mathew 
33738dce70fSSoby Mathew 	flush_dcache_range((unsigned long)&wakeup_address,
33838dce70fSSoby Mathew 				sizeof(wakeup_address));
3393fc4124cSDan Handley 	return 0;
3403fc4124cSDan Handley }
341