xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision 5f3a60301ef7a455f1c74e71e286b89cb0c97f7d)
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>
34b4315306SDan Handley #include <cci.h>
35785fb92bSSoby Mathew #include <css_pm.h>
36b4315306SDan Handley #include <debug.h>
37b4315306SDan Handley #include <errno.h>
38b4315306SDan Handley #include <plat_arm.h>
39b4315306SDan Handley #include <platform.h>
40b4315306SDan Handley #include <platform_def.h>
41b4315306SDan Handley #include "css_scpi.h"
42b4315306SDan Handley 
43785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
44785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops
4538dce70fSSoby Mathew 
462204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
472204afdeSSoby Mathew /*
482204afdeSSoby Mathew  *  The table storing the valid idle power states. Ensure that the
492204afdeSSoby Mathew  *  array entries are populated in ascending order of state-id to
502204afdeSSoby Mathew  *  enable us to use binary search during power state validation.
512204afdeSSoby Mathew  *  The table must be terminated by a NULL entry.
522204afdeSSoby Mathew  */
532204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = {
54*5f3a6030SSoby Mathew 	/* State-id - 0x001 */
55*5f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
56*5f3a6030SSoby Mathew 		ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
57*5f3a6030SSoby Mathew 	/* State-id - 0x002 */
58*5f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
59*5f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
60*5f3a6030SSoby Mathew 	/* State-id - 0x022 */
61*5f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
62*5f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
63*5f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
64*5f3a6030SSoby Mathew 	/* State-id - 0x222 */
65*5f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
66*5f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
67*5f3a6030SSoby Mathew #endif
682204afdeSSoby Mathew 	0,
692204afdeSSoby Mathew };
70*5f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
712204afdeSSoby Mathew 
72b4315306SDan Handley /*******************************************************************************
7338dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
74b4315306SDan Handley  * level and mpidr determine the affinity instance.
75b4315306SDan Handley  ******************************************************************************/
7638dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
77b4315306SDan Handley {
78b4315306SDan Handley 	/*
7938dce70fSSoby Mathew 	 * SCP takes care of powering up parent power domains so we
80b4315306SDan Handley 	 * only need to care about level 0
81b4315306SDan Handley 	 */
82b4315306SDan Handley 	scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on,
83b4315306SDan Handley 				 scpi_power_on);
84b4315306SDan Handley 
85b4315306SDan Handley 	return PSCI_E_SUCCESS;
86b4315306SDan Handley }
87b4315306SDan Handley 
88b4315306SDan Handley /*******************************************************************************
8938dce70fSSoby Mathew  * Handler called when a power level has just been powered on after
9038dce70fSSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
9138dce70fSSoby Mathew  * each level has woken up from.
92b4315306SDan Handley  ******************************************************************************/
9338dce70fSSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
94b4315306SDan Handley {
9538dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
9638dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
97b4315306SDan Handley 
98b4315306SDan Handley 	/*
99b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
100b4315306SDan Handley 	 * if this cluster was off.
101b4315306SDan Handley 	 */
10238dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
10338dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF)
10438dce70fSSoby Mathew 		cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1()));
105b4315306SDan Handley 
106b4315306SDan Handley 	/* Enable the gic cpu interface */
107b4315306SDan Handley 	arm_gic_cpuif_setup();
108b4315306SDan Handley 
109b4315306SDan Handley 	/* todo: Is this setup only needed after a cold boot? */
110b4315306SDan Handley 	arm_gic_pcpu_distif_setup();
111b4315306SDan Handley }
112b4315306SDan Handley 
113b4315306SDan Handley /*******************************************************************************
114b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
115b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
11638dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
11738dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
118b4315306SDan Handley  ******************************************************************************/
11938dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
120b4315306SDan Handley {
121b4315306SDan Handley 	uint32_t cluster_state = scpi_power_on;
122b4315306SDan Handley 
123b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
124b4315306SDan Handley 	arm_gic_cpuif_deactivate();
125b4315306SDan Handley 
126b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
12738dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
12838dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF) {
129b4315306SDan Handley 		cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr()));
130b4315306SDan Handley 		cluster_state = scpi_power_off;
131b4315306SDan Handley 	}
132b4315306SDan Handley 
133b4315306SDan Handley 	/*
134b4315306SDan Handley 	 * Ask the SCP to power down the appropriate components depending upon
135b4315306SDan Handley 	 * their state.
136b4315306SDan Handley 	 */
137b4315306SDan Handley 	scpi_set_css_power_state(read_mpidr_el1(),
138b4315306SDan Handley 				 scpi_power_off,
139b4315306SDan Handley 				 cluster_state,
140b4315306SDan Handley 				 scpi_power_on);
141b4315306SDan Handley }
142b4315306SDan Handley 
143b4315306SDan Handley /*******************************************************************************
14438dce70fSSoby Mathew  * Handler called when a power domain is about to be turned off. The
14538dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
146b4315306SDan Handley  ******************************************************************************/
147785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state)
148b4315306SDan Handley {
14938dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
15038dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
151b4315306SDan Handley 
15238dce70fSSoby Mathew 	css_power_down_common(target_state);
153b4315306SDan Handley }
154b4315306SDan Handley 
155b4315306SDan Handley /*******************************************************************************
15638dce70fSSoby Mathew  * Handler called when a power domain is about to be suspended. The
15738dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
158b4315306SDan Handley  ******************************************************************************/
159785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state)
160b4315306SDan Handley {
16138dce70fSSoby Mathew 	/*
16238dce70fSSoby Mathew 	 * Juno has retention only at cpu level. Just return
16338dce70fSSoby Mathew 	 * as nothing is to be done for retention.
16438dce70fSSoby Mathew 	 */
16538dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
16638dce70fSSoby Mathew 						ARM_LOCAL_STATE_RET)
167b4315306SDan Handley 		return;
168b4315306SDan Handley 
16938dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
17038dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
17138dce70fSSoby Mathew 
17238dce70fSSoby Mathew 	css_power_down_common(target_state);
173b4315306SDan Handley }
174b4315306SDan Handley 
175b4315306SDan Handley /*******************************************************************************
17638dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
17738dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
17838dce70fSSoby Mathew  * that each level has woken up from.
179b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
180b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
181b4315306SDan Handley  ******************************************************************************/
182785fb92bSSoby Mathew void css_pwr_domain_suspend_finish(
18338dce70fSSoby Mathew 				const psci_power_state_t *target_state)
184b4315306SDan Handley {
18538dce70fSSoby Mathew 	/*
18638dce70fSSoby Mathew 	 * Return as nothing is to be done on waking up from retention.
18738dce70fSSoby Mathew 	 */
18838dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
18938dce70fSSoby Mathew 						ARM_LOCAL_STATE_RET)
19038dce70fSSoby Mathew 		return;
19138dce70fSSoby Mathew 
19238dce70fSSoby Mathew 	css_pwr_domain_on_finish(target_state);
193b4315306SDan Handley }
194b4315306SDan Handley 
195b4315306SDan Handley /*******************************************************************************
196b4315306SDan Handley  * Handlers to shutdown/reboot the system
197b4315306SDan Handley  ******************************************************************************/
198785fb92bSSoby Mathew void __dead2 css_system_off(void)
199b4315306SDan Handley {
200b4315306SDan Handley 	uint32_t response;
201b4315306SDan Handley 
202b4315306SDan Handley 	/* Send the power down request to the SCP */
203b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_shutdown);
204b4315306SDan Handley 
205b4315306SDan Handley 	if (response != SCP_OK) {
206b4315306SDan Handley 		ERROR("CSS System Off: SCP error %u.\n", response);
207b4315306SDan Handley 		panic();
208b4315306SDan Handley 	}
209b4315306SDan Handley 	wfi();
210b4315306SDan Handley 	ERROR("CSS System Off: operation not handled.\n");
211b4315306SDan Handley 	panic();
212b4315306SDan Handley }
213b4315306SDan Handley 
214785fb92bSSoby Mathew void __dead2 css_system_reset(void)
215b4315306SDan Handley {
216b4315306SDan Handley 	uint32_t response;
217b4315306SDan Handley 
218b4315306SDan Handley 	/* Send the system reset request to the SCP */
219b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_reboot);
220b4315306SDan Handley 
221b4315306SDan Handley 	if (response != SCP_OK) {
222b4315306SDan Handley 		ERROR("CSS System Reset: SCP error %u.\n", response);
223b4315306SDan Handley 		panic();
224b4315306SDan Handley 	}
225b4315306SDan Handley 	wfi();
226b4315306SDan Handley 	ERROR("CSS System Reset: operation not handled.\n");
227b4315306SDan Handley 	panic();
228b4315306SDan Handley }
229b4315306SDan Handley 
230b4315306SDan Handley /*******************************************************************************
23138dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
232b4315306SDan Handley  ******************************************************************************/
23338dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
234b4315306SDan Handley {
235b4315306SDan Handley 	unsigned int scr;
236b4315306SDan Handley 
23738dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
23838dce70fSSoby Mathew 
239b4315306SDan Handley 	scr = read_scr_el3();
240b4315306SDan Handley 	/* Enable PhysicalIRQ bit for NS world to wake the CPU */
241b4315306SDan Handley 	write_scr_el3(scr | SCR_IRQ_BIT);
242b4315306SDan Handley 	isb();
243b4315306SDan Handley 	dsb();
244b4315306SDan Handley 	wfi();
245b4315306SDan Handley 
246b4315306SDan Handley 	/*
247b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
248b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
249b4315306SDan Handley 	 */
250b4315306SDan Handley 	write_scr_el3(scr);
251b4315306SDan Handley }
252b4315306SDan Handley 
253b4315306SDan Handley /*******************************************************************************
254785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
255785fb92bSSoby Mathew  * platform will take care of registering the handlers with PSCI.
256b4315306SDan Handley  ******************************************************************************/
257785fb92bSSoby Mathew const plat_psci_ops_t plat_arm_psci_pm_ops = {
25838dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
25938dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
26038dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
26138dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
26238dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
26338dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
264b4315306SDan Handley 	.system_off		= css_system_off,
265b4315306SDan Handley 	.system_reset		= css_system_reset,
266f9e858b1SSoby Mathew 	.validate_power_state	= arm_validate_power_state,
267f9e858b1SSoby Mathew 	.validate_ns_entrypoint = arm_validate_ns_entrypoint
268b4315306SDan Handley };
269