xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision c1bb8a0500149c58f59f241676751b6b87edbae6)
1b4315306SDan Handley /*
2b4315306SDan Handley  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3b4315306SDan Handley  *
4b4315306SDan Handley  * Redistribution and use in source and binary forms, with or without
5b4315306SDan Handley  * modification, are permitted provided that the following conditions are met:
6b4315306SDan Handley  *
7b4315306SDan Handley  * Redistributions of source code must retain the above copyright notice, this
8b4315306SDan Handley  * list of conditions and the following disclaimer.
9b4315306SDan Handley  *
10b4315306SDan Handley  * Redistributions in binary form must reproduce the above copyright notice,
11b4315306SDan Handley  * this list of conditions and the following disclaimer in the documentation
12b4315306SDan Handley  * and/or other materials provided with the distribution.
13b4315306SDan Handley  *
14b4315306SDan Handley  * Neither the name of ARM nor the names of its contributors may be used
15b4315306SDan Handley  * to endorse or promote products derived from this software without specific
16b4315306SDan Handley  * prior written permission.
17b4315306SDan Handley  *
18b4315306SDan Handley  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19b4315306SDan Handley  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b4315306SDan Handley  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b4315306SDan Handley  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22b4315306SDan Handley  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23b4315306SDan Handley  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24b4315306SDan Handley  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25b4315306SDan Handley  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26b4315306SDan Handley  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27b4315306SDan Handley  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28b4315306SDan Handley  * POSSIBILITY OF SUCH DAMAGE.
29b4315306SDan Handley  */
30b4315306SDan Handley 
31b4315306SDan Handley #include <arch_helpers.h>
32785fb92bSSoby Mathew #include <assert.h>
33b4315306SDan Handley #include <arm_gic.h>
34*c1bb8a05SSoby Mathew #include <cassert.h>
35b4315306SDan Handley #include <cci.h>
36785fb92bSSoby Mathew #include <css_pm.h>
37b4315306SDan Handley #include <debug.h>
38b4315306SDan Handley #include <errno.h>
39b4315306SDan Handley #include <plat_arm.h>
40b4315306SDan Handley #include <platform.h>
41b4315306SDan Handley #include <platform_def.h>
42b4315306SDan Handley #include "css_scpi.h"
43b4315306SDan Handley 
44785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
45785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops
4638dce70fSSoby Mathew 
472204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
482204afdeSSoby Mathew /*
492204afdeSSoby Mathew  *  The table storing the valid idle power states. Ensure that the
502204afdeSSoby Mathew  *  array entries are populated in ascending order of state-id to
512204afdeSSoby Mathew  *  enable us to use binary search during power state validation.
522204afdeSSoby Mathew  *  The table must be terminated by a NULL entry.
532204afdeSSoby Mathew  */
542204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = {
555f3a6030SSoby Mathew 	/* State-id - 0x001 */
565f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
575f3a6030SSoby Mathew 		ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
585f3a6030SSoby Mathew 	/* State-id - 0x002 */
595f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
605f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
615f3a6030SSoby Mathew 	/* State-id - 0x022 */
625f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
635f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
645f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
655f3a6030SSoby Mathew 	/* State-id - 0x222 */
665f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
675f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
685f3a6030SSoby Mathew #endif
692204afdeSSoby Mathew 	0,
702204afdeSSoby Mathew };
715f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
722204afdeSSoby Mathew 
73*c1bb8a05SSoby Mathew /*
74*c1bb8a05SSoby Mathew  * All the power management helpers in this file assume at least cluster power
75*c1bb8a05SSoby Mathew  * level is supported.
76*c1bb8a05SSoby Mathew  */
77*c1bb8a05SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1,
78*c1bb8a05SSoby Mathew 		assert_max_pwr_lvl_supported_mismatch);
79*c1bb8a05SSoby Mathew 
80b4315306SDan Handley /*******************************************************************************
8138dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
82b4315306SDan Handley  * level and mpidr determine the affinity instance.
83b4315306SDan Handley  ******************************************************************************/
8438dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
85b4315306SDan Handley {
86b4315306SDan Handley 	/*
8738dce70fSSoby Mathew 	 * SCP takes care of powering up parent power domains so we
88b4315306SDan Handley 	 * only need to care about level 0
89b4315306SDan Handley 	 */
90b4315306SDan Handley 	scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on,
91b4315306SDan Handley 				 scpi_power_on);
92b4315306SDan Handley 
93b4315306SDan Handley 	return PSCI_E_SUCCESS;
94b4315306SDan Handley }
95b4315306SDan Handley 
96b4315306SDan Handley /*******************************************************************************
9738dce70fSSoby Mathew  * Handler called when a power level has just been powered on after
9838dce70fSSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
9938dce70fSSoby Mathew  * each level has woken up from.
100b4315306SDan Handley  ******************************************************************************/
10138dce70fSSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
102b4315306SDan Handley {
10338dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
10438dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
105b4315306SDan Handley 
106*c1bb8a05SSoby Mathew 	if (PLAT_MAX_PWR_LVL > ARM_PWR_LVL1) {
107*c1bb8a05SSoby Mathew 		/*
108*c1bb8a05SSoby Mathew 		 * Perform system initialization if woken up from system
109*c1bb8a05SSoby Mathew 		 * suspend.
110*c1bb8a05SSoby Mathew 		 */
111*c1bb8a05SSoby Mathew 		if (target_state->pwr_domain_state[ARM_PWR_LVL2] ==
112*c1bb8a05SSoby Mathew 							ARM_LOCAL_STATE_OFF)
113*c1bb8a05SSoby Mathew 			arm_system_pwr_domain_resume();
114*c1bb8a05SSoby Mathew 	}
115*c1bb8a05SSoby Mathew 
116b4315306SDan Handley 	/*
117b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
118b4315306SDan Handley 	 * if this cluster was off.
119b4315306SDan Handley 	 */
12038dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
12138dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF)
12238dce70fSSoby Mathew 		cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1()));
123b4315306SDan Handley 
124*c1bb8a05SSoby Mathew 
125*c1bb8a05SSoby Mathew 	if (PLAT_MAX_PWR_LVL > ARM_PWR_LVL1) {
126*c1bb8a05SSoby Mathew 		/*
127*c1bb8a05SSoby Mathew 		 * Skip GIC CPU interface and per-CPU Distributor interface
128*c1bb8a05SSoby Mathew 		 * setups if woken up from system suspend as it is done as
129*c1bb8a05SSoby Mathew 		 * part of css_system_pwr_domain_resume().
130*c1bb8a05SSoby Mathew 		 */
131*c1bb8a05SSoby Mathew 		if (target_state->pwr_domain_state[ARM_PWR_LVL2] ==
132*c1bb8a05SSoby Mathew 							ARM_LOCAL_STATE_OFF)
133*c1bb8a05SSoby Mathew 			return;
134*c1bb8a05SSoby Mathew 	}
135*c1bb8a05SSoby Mathew 
136b4315306SDan Handley 	/* Enable the gic cpu interface */
137b4315306SDan Handley 	arm_gic_cpuif_setup();
138b4315306SDan Handley 
139b4315306SDan Handley 	/* todo: Is this setup only needed after a cold boot? */
140b4315306SDan Handley 	arm_gic_pcpu_distif_setup();
141b4315306SDan Handley }
142b4315306SDan Handley 
143b4315306SDan Handley /*******************************************************************************
144b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
145b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
14638dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
14738dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
148b4315306SDan Handley  ******************************************************************************/
14938dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
150b4315306SDan Handley {
151b4315306SDan Handley 	uint32_t cluster_state = scpi_power_on;
152*c1bb8a05SSoby Mathew 	uint32_t system_state = scpi_power_on;
153b4315306SDan Handley 
154b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
155b4315306SDan Handley 	arm_gic_cpuif_deactivate();
156b4315306SDan Handley 
157*c1bb8a05SSoby Mathew 	if (PLAT_MAX_PWR_LVL > ARM_PWR_LVL1) {
158*c1bb8a05SSoby Mathew 		/*
159*c1bb8a05SSoby Mathew 		 * Check if power down at system power domain level is
160*c1bb8a05SSoby Mathew 		 * requested.
161*c1bb8a05SSoby Mathew 		 */
162*c1bb8a05SSoby Mathew 		if (target_state->pwr_domain_state[ARM_PWR_LVL2] ==
163*c1bb8a05SSoby Mathew 							ARM_LOCAL_STATE_OFF)
164*c1bb8a05SSoby Mathew 			system_state = scpi_power_retention;
165*c1bb8a05SSoby Mathew 	}
166*c1bb8a05SSoby Mathew 
167b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
16838dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
16938dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF) {
170b4315306SDan Handley 		cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr()));
171b4315306SDan Handley 		cluster_state = scpi_power_off;
172b4315306SDan Handley 	}
173b4315306SDan Handley 
174b4315306SDan Handley 	/*
175b4315306SDan Handley 	 * Ask the SCP to power down the appropriate components depending upon
176b4315306SDan Handley 	 * their state.
177b4315306SDan Handley 	 */
178b4315306SDan Handley 	scpi_set_css_power_state(read_mpidr_el1(),
179b4315306SDan Handley 				 scpi_power_off,
180b4315306SDan Handley 				 cluster_state,
181*c1bb8a05SSoby Mathew 				 system_state);
182b4315306SDan Handley }
183b4315306SDan Handley 
184b4315306SDan Handley /*******************************************************************************
18538dce70fSSoby Mathew  * Handler called when a power domain is about to be turned off. The
18638dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
187b4315306SDan Handley  ******************************************************************************/
188785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state)
189b4315306SDan Handley {
19038dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
19138dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
192b4315306SDan Handley 
19338dce70fSSoby Mathew 	css_power_down_common(target_state);
194b4315306SDan Handley }
195b4315306SDan Handley 
196b4315306SDan Handley /*******************************************************************************
19738dce70fSSoby Mathew  * Handler called when a power domain is about to be suspended. The
19838dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
199b4315306SDan Handley  ******************************************************************************/
200785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state)
201b4315306SDan Handley {
20238dce70fSSoby Mathew 	/*
20338dce70fSSoby Mathew 	 * Juno has retention only at cpu level. Just return
20438dce70fSSoby Mathew 	 * as nothing is to be done for retention.
20538dce70fSSoby Mathew 	 */
20638dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
20738dce70fSSoby Mathew 						ARM_LOCAL_STATE_RET)
208b4315306SDan Handley 		return;
209b4315306SDan Handley 
21038dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
21138dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
21238dce70fSSoby Mathew 
21338dce70fSSoby Mathew 	css_power_down_common(target_state);
214b4315306SDan Handley }
215b4315306SDan Handley 
216b4315306SDan Handley /*******************************************************************************
21738dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
21838dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
21938dce70fSSoby Mathew  * that each level has woken up from.
220b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
221b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
222b4315306SDan Handley  ******************************************************************************/
223785fb92bSSoby Mathew void css_pwr_domain_suspend_finish(
22438dce70fSSoby Mathew 				const psci_power_state_t *target_state)
225b4315306SDan Handley {
22638dce70fSSoby Mathew 	/*
22738dce70fSSoby Mathew 	 * Return as nothing is to be done on waking up from retention.
22838dce70fSSoby Mathew 	 */
22938dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
23038dce70fSSoby Mathew 						ARM_LOCAL_STATE_RET)
23138dce70fSSoby Mathew 		return;
23238dce70fSSoby Mathew 
23338dce70fSSoby Mathew 	css_pwr_domain_on_finish(target_state);
234b4315306SDan Handley }
235b4315306SDan Handley 
236b4315306SDan Handley /*******************************************************************************
237b4315306SDan Handley  * Handlers to shutdown/reboot the system
238b4315306SDan Handley  ******************************************************************************/
239785fb92bSSoby Mathew void __dead2 css_system_off(void)
240b4315306SDan Handley {
241b4315306SDan Handley 	uint32_t response;
242b4315306SDan Handley 
243b4315306SDan Handley 	/* Send the power down request to the SCP */
244b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_shutdown);
245b4315306SDan Handley 
246b4315306SDan Handley 	if (response != SCP_OK) {
247b4315306SDan Handley 		ERROR("CSS System Off: SCP error %u.\n", response);
248b4315306SDan Handley 		panic();
249b4315306SDan Handley 	}
250b4315306SDan Handley 	wfi();
251b4315306SDan Handley 	ERROR("CSS System Off: operation not handled.\n");
252b4315306SDan Handley 	panic();
253b4315306SDan Handley }
254b4315306SDan Handley 
255785fb92bSSoby Mathew void __dead2 css_system_reset(void)
256b4315306SDan Handley {
257b4315306SDan Handley 	uint32_t response;
258b4315306SDan Handley 
259b4315306SDan Handley 	/* Send the system reset request to the SCP */
260b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_reboot);
261b4315306SDan Handley 
262b4315306SDan Handley 	if (response != SCP_OK) {
263b4315306SDan Handley 		ERROR("CSS System Reset: SCP error %u.\n", response);
264b4315306SDan Handley 		panic();
265b4315306SDan Handley 	}
266b4315306SDan Handley 	wfi();
267b4315306SDan Handley 	ERROR("CSS System Reset: operation not handled.\n");
268b4315306SDan Handley 	panic();
269b4315306SDan Handley }
270b4315306SDan Handley 
271b4315306SDan Handley /*******************************************************************************
27238dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
273b4315306SDan Handley  ******************************************************************************/
27438dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
275b4315306SDan Handley {
276b4315306SDan Handley 	unsigned int scr;
277b4315306SDan Handley 
27838dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
27938dce70fSSoby Mathew 
280b4315306SDan Handley 	scr = read_scr_el3();
281b4315306SDan Handley 	/* Enable PhysicalIRQ bit for NS world to wake the CPU */
282b4315306SDan Handley 	write_scr_el3(scr | SCR_IRQ_BIT);
283b4315306SDan Handley 	isb();
284b4315306SDan Handley 	dsb();
285b4315306SDan Handley 	wfi();
286b4315306SDan Handley 
287b4315306SDan Handley 	/*
288b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
289b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
290b4315306SDan Handley 	 */
291b4315306SDan Handley 	write_scr_el3(scr);
292b4315306SDan Handley }
293b4315306SDan Handley 
294b4315306SDan Handley /*******************************************************************************
295*c1bb8a05SSoby Mathew  * Handler called to return the 'req_state' for system suspend.
296*c1bb8a05SSoby Mathew  ******************************************************************************/
297*c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
298*c1bb8a05SSoby Mathew {
299*c1bb8a05SSoby Mathew 	unsigned int i;
300*c1bb8a05SSoby Mathew 
301*c1bb8a05SSoby Mathew 	/*
302*c1bb8a05SSoby Mathew 	 * System Suspend is supported only if the system power domain node
303*c1bb8a05SSoby Mathew 	 * is implemented.
304*c1bb8a05SSoby Mathew 	 */
305*c1bb8a05SSoby Mathew 	assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
306*c1bb8a05SSoby Mathew 
307*c1bb8a05SSoby Mathew 	for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
308*c1bb8a05SSoby Mathew 		req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
309*c1bb8a05SSoby Mathew }
310*c1bb8a05SSoby Mathew 
311*c1bb8a05SSoby Mathew /*******************************************************************************
312785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
313785fb92bSSoby Mathew  * platform will take care of registering the handlers with PSCI.
314b4315306SDan Handley  ******************************************************************************/
315785fb92bSSoby Mathew const plat_psci_ops_t plat_arm_psci_pm_ops = {
31638dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
31738dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
31838dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
31938dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
32038dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
32138dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
322b4315306SDan Handley 	.system_off		= css_system_off,
323b4315306SDan Handley 	.system_reset		= css_system_reset,
324f9e858b1SSoby Mathew 	.validate_power_state	= arm_validate_power_state,
325f9e858b1SSoby Mathew 	.validate_ns_entrypoint = arm_validate_ns_entrypoint
326b4315306SDan Handley };
327