xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision 785fb92b8a021369348da2316b99b7e143c2806f)
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>
32*785fb92bSSoby Mathew #include <assert.h>
33b4315306SDan Handley #include <arm_gic.h>
34b4315306SDan Handley #include <cci.h>
35*785fb92bSSoby 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 
43*785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
44*785fb92bSSoby 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[] = {
542204afdeSSoby Mathew 	/* State-id - 0x01 */
552204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RET,
562204afdeSSoby Mathew 			ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
572204afdeSSoby Mathew 	/* State-id - 0x02 */
582204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
592204afdeSSoby Mathew 			ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
602204afdeSSoby Mathew 	/* State-id - 0x22 */
612204afdeSSoby Mathew 	arm_make_pwrstate_lvl1(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
622204afdeSSoby Mathew 			ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
632204afdeSSoby Mathew 	0,
642204afdeSSoby Mathew };
652204afdeSSoby Mathew #endif
662204afdeSSoby Mathew 
67b4315306SDan Handley /*******************************************************************************
6838dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
69b4315306SDan Handley  * level and mpidr determine the affinity instance.
70b4315306SDan Handley  ******************************************************************************/
7138dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
72b4315306SDan Handley {
73b4315306SDan Handley 	/*
7438dce70fSSoby Mathew 	 * SCP takes care of powering up parent power domains so we
75b4315306SDan Handley 	 * only need to care about level 0
76b4315306SDan Handley 	 */
77b4315306SDan Handley 	scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on,
78b4315306SDan Handley 				 scpi_power_on);
79b4315306SDan Handley 
80b4315306SDan Handley 	return PSCI_E_SUCCESS;
81b4315306SDan Handley }
82b4315306SDan Handley 
83b4315306SDan Handley /*******************************************************************************
8438dce70fSSoby Mathew  * Handler called when a power level has just been powered on after
8538dce70fSSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
8638dce70fSSoby Mathew  * each level has woken up from.
87b4315306SDan Handley  ******************************************************************************/
8838dce70fSSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
89b4315306SDan Handley {
9038dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
9138dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
92b4315306SDan Handley 
93b4315306SDan Handley 	/*
94b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
95b4315306SDan Handley 	 * if this cluster was off.
96b4315306SDan Handley 	 */
9738dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
9838dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF)
9938dce70fSSoby Mathew 		cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1()));
100b4315306SDan Handley 
101b4315306SDan Handley 	/* Enable the gic cpu interface */
102b4315306SDan Handley 	arm_gic_cpuif_setup();
103b4315306SDan Handley 
104b4315306SDan Handley 	/* todo: Is this setup only needed after a cold boot? */
105b4315306SDan Handley 	arm_gic_pcpu_distif_setup();
106b4315306SDan Handley }
107b4315306SDan Handley 
108b4315306SDan Handley /*******************************************************************************
109b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
110b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
11138dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
11238dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
113b4315306SDan Handley  ******************************************************************************/
11438dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
115b4315306SDan Handley {
116b4315306SDan Handley 	uint32_t cluster_state = scpi_power_on;
117b4315306SDan Handley 
118b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
119b4315306SDan Handley 	arm_gic_cpuif_deactivate();
120b4315306SDan Handley 
121b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
12238dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL1] ==
12338dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF) {
124b4315306SDan Handley 		cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr()));
125b4315306SDan Handley 		cluster_state = scpi_power_off;
126b4315306SDan Handley 	}
127b4315306SDan Handley 
128b4315306SDan Handley 	/*
129b4315306SDan Handley 	 * Ask the SCP to power down the appropriate components depending upon
130b4315306SDan Handley 	 * their state.
131b4315306SDan Handley 	 */
132b4315306SDan Handley 	scpi_set_css_power_state(read_mpidr_el1(),
133b4315306SDan Handley 				 scpi_power_off,
134b4315306SDan Handley 				 cluster_state,
135b4315306SDan Handley 				 scpi_power_on);
136b4315306SDan Handley }
137b4315306SDan Handley 
138b4315306SDan Handley /*******************************************************************************
13938dce70fSSoby Mathew  * Handler called when a power domain is about to be turned off. The
14038dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
141b4315306SDan Handley  ******************************************************************************/
142*785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state)
143b4315306SDan Handley {
14438dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
14538dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
146b4315306SDan Handley 
14738dce70fSSoby Mathew 	css_power_down_common(target_state);
148b4315306SDan Handley }
149b4315306SDan Handley 
150b4315306SDan Handley /*******************************************************************************
15138dce70fSSoby Mathew  * Handler called when a power domain is about to be suspended. The
15238dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
153b4315306SDan Handley  ******************************************************************************/
154*785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state)
155b4315306SDan Handley {
15638dce70fSSoby Mathew 	/*
15738dce70fSSoby Mathew 	 * Juno has retention only at cpu level. Just return
15838dce70fSSoby Mathew 	 * as nothing is to be done for retention.
15938dce70fSSoby Mathew 	 */
16038dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
16138dce70fSSoby Mathew 						ARM_LOCAL_STATE_RET)
162b4315306SDan Handley 		return;
163b4315306SDan Handley 
16438dce70fSSoby Mathew 	assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
16538dce70fSSoby Mathew 						ARM_LOCAL_STATE_OFF);
16638dce70fSSoby Mathew 
16738dce70fSSoby Mathew 	css_power_down_common(target_state);
168b4315306SDan Handley }
169b4315306SDan Handley 
170b4315306SDan Handley /*******************************************************************************
17138dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
17238dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
17338dce70fSSoby Mathew  * that each level has woken up from.
174b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
175b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
176b4315306SDan Handley  ******************************************************************************/
177*785fb92bSSoby Mathew void css_pwr_domain_suspend_finish(
17838dce70fSSoby Mathew 				const psci_power_state_t *target_state)
179b4315306SDan Handley {
18038dce70fSSoby Mathew 	/*
18138dce70fSSoby Mathew 	 * Return as nothing is to be done on waking up from retention.
18238dce70fSSoby Mathew 	 */
18338dce70fSSoby Mathew 	if (target_state->pwr_domain_state[ARM_PWR_LVL0] ==
18438dce70fSSoby Mathew 						ARM_LOCAL_STATE_RET)
18538dce70fSSoby Mathew 		return;
18638dce70fSSoby Mathew 
18738dce70fSSoby Mathew 	css_pwr_domain_on_finish(target_state);
188b4315306SDan Handley }
189b4315306SDan Handley 
190b4315306SDan Handley /*******************************************************************************
191b4315306SDan Handley  * Handlers to shutdown/reboot the system
192b4315306SDan Handley  ******************************************************************************/
193*785fb92bSSoby Mathew void __dead2 css_system_off(void)
194b4315306SDan Handley {
195b4315306SDan Handley 	uint32_t response;
196b4315306SDan Handley 
197b4315306SDan Handley 	/* Send the power down request to the SCP */
198b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_shutdown);
199b4315306SDan Handley 
200b4315306SDan Handley 	if (response != SCP_OK) {
201b4315306SDan Handley 		ERROR("CSS System Off: SCP error %u.\n", response);
202b4315306SDan Handley 		panic();
203b4315306SDan Handley 	}
204b4315306SDan Handley 	wfi();
205b4315306SDan Handley 	ERROR("CSS System Off: operation not handled.\n");
206b4315306SDan Handley 	panic();
207b4315306SDan Handley }
208b4315306SDan Handley 
209*785fb92bSSoby Mathew void __dead2 css_system_reset(void)
210b4315306SDan Handley {
211b4315306SDan Handley 	uint32_t response;
212b4315306SDan Handley 
213b4315306SDan Handley 	/* Send the system reset request to the SCP */
214b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_reboot);
215b4315306SDan Handley 
216b4315306SDan Handley 	if (response != SCP_OK) {
217b4315306SDan Handley 		ERROR("CSS System Reset: SCP error %u.\n", response);
218b4315306SDan Handley 		panic();
219b4315306SDan Handley 	}
220b4315306SDan Handley 	wfi();
221b4315306SDan Handley 	ERROR("CSS System Reset: operation not handled.\n");
222b4315306SDan Handley 	panic();
223b4315306SDan Handley }
224b4315306SDan Handley 
225b4315306SDan Handley /*******************************************************************************
22638dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
227b4315306SDan Handley  ******************************************************************************/
22838dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
229b4315306SDan Handley {
230b4315306SDan Handley 	unsigned int scr;
231b4315306SDan Handley 
23238dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
23338dce70fSSoby Mathew 
234b4315306SDan Handley 	scr = read_scr_el3();
235b4315306SDan Handley 	/* Enable PhysicalIRQ bit for NS world to wake the CPU */
236b4315306SDan Handley 	write_scr_el3(scr | SCR_IRQ_BIT);
237b4315306SDan Handley 	isb();
238b4315306SDan Handley 	dsb();
239b4315306SDan Handley 	wfi();
240b4315306SDan Handley 
241b4315306SDan Handley 	/*
242b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
243b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
244b4315306SDan Handley 	 */
245b4315306SDan Handley 	write_scr_el3(scr);
246b4315306SDan Handley }
247b4315306SDan Handley 
248b4315306SDan Handley /*******************************************************************************
249*785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
250*785fb92bSSoby Mathew  * platform will take care of registering the handlers with PSCI.
251b4315306SDan Handley  ******************************************************************************/
252*785fb92bSSoby Mathew const plat_psci_ops_t plat_arm_psci_pm_ops = {
25338dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
25438dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
25538dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
25638dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
25738dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
25838dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
259b4315306SDan Handley 	.system_off		= css_system_off,
260b4315306SDan Handley 	.system_reset		= css_system_reset,
261f9e858b1SSoby Mathew 	.validate_power_state	= arm_validate_power_state,
262f9e858b1SSoby Mathew 	.validate_ns_entrypoint = arm_validate_ns_entrypoint
263b4315306SDan Handley };
264