xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision 6355f2347aec8bf6ad74867c2b0c996e10546ad4)
1b4315306SDan Handley /*
2*6355f234SVikram Kanigiri  * Copyright (c) 2015-2016, 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>
33c1bb8a05SSoby Mathew #include <cassert.h>
34785fb92bSSoby Mathew #include <css_pm.h>
35b4315306SDan Handley #include <debug.h>
36b4315306SDan Handley #include <errno.h>
37b4315306SDan Handley #include <plat_arm.h>
38b4315306SDan Handley #include <platform.h>
39b4315306SDan Handley #include <platform_def.h>
40b4315306SDan Handley #include "css_scpi.h"
41b4315306SDan Handley 
42f14d1886SSoby Mathew /* Macros to read the CSS power domain state */
43f14d1886SSoby Mathew #define CSS_CORE_PWR_STATE(state)	(state)->pwr_domain_state[ARM_PWR_LVL0]
44f14d1886SSoby Mathew #define CSS_CLUSTER_PWR_STATE(state)	(state)->pwr_domain_state[ARM_PWR_LVL1]
45f14d1886SSoby Mathew #define CSS_SYSTEM_PWR_STATE(state)	((PLAT_MAX_PWR_LVL > ARM_PWR_LVL1) ?\
46f14d1886SSoby Mathew 				(state)->pwr_domain_state[ARM_PWR_LVL2] : 0)
47f14d1886SSoby Mathew 
48785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
49785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops
5038dce70fSSoby Mathew 
512204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
522204afdeSSoby Mathew /*
532204afdeSSoby Mathew  *  The table storing the valid idle power states. Ensure that the
542204afdeSSoby Mathew  *  array entries are populated in ascending order of state-id to
552204afdeSSoby Mathew  *  enable us to use binary search during power state validation.
562204afdeSSoby Mathew  *  The table must be terminated by a NULL entry.
572204afdeSSoby Mathew  */
582204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = {
595f3a6030SSoby Mathew 	/* State-id - 0x001 */
605f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
615f3a6030SSoby Mathew 		ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
625f3a6030SSoby Mathew 	/* State-id - 0x002 */
635f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
645f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
655f3a6030SSoby Mathew 	/* State-id - 0x022 */
665f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
675f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
685f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
695f3a6030SSoby Mathew 	/* State-id - 0x222 */
705f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
715f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
725f3a6030SSoby Mathew #endif
732204afdeSSoby Mathew 	0,
742204afdeSSoby Mathew };
755f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
762204afdeSSoby Mathew 
77c1bb8a05SSoby Mathew /*
78c1bb8a05SSoby Mathew  * All the power management helpers in this file assume at least cluster power
79c1bb8a05SSoby Mathew  * level is supported.
80c1bb8a05SSoby Mathew  */
81c1bb8a05SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1,
82c1bb8a05SSoby Mathew 		assert_max_pwr_lvl_supported_mismatch);
83c1bb8a05SSoby Mathew 
84b4315306SDan Handley /*******************************************************************************
8538dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
86b4315306SDan Handley  * level and mpidr determine the affinity instance.
87b4315306SDan Handley  ******************************************************************************/
8838dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
89b4315306SDan Handley {
90b4315306SDan Handley 	/*
9138dce70fSSoby Mathew 	 * SCP takes care of powering up parent power domains so we
92b4315306SDan Handley 	 * only need to care about level 0
93b4315306SDan Handley 	 */
94b4315306SDan Handley 	scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on,
95b4315306SDan Handley 				 scpi_power_on);
96b4315306SDan Handley 
97b4315306SDan Handley 	return PSCI_E_SUCCESS;
98b4315306SDan Handley }
99b4315306SDan Handley 
100f14d1886SSoby Mathew static void css_pwr_domain_on_finisher_common(
101f14d1886SSoby Mathew 		const psci_power_state_t *target_state)
102b4315306SDan Handley {
103f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
104c1bb8a05SSoby Mathew 
105b4315306SDan Handley 	/*
106b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
107b4315306SDan Handley 	 * if this cluster was off.
108b4315306SDan Handley 	 */
109f14d1886SSoby Mathew 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
110*6355f234SVikram Kanigiri 		plat_arm_interconnect_enter_coherency();
111c1bb8a05SSoby Mathew }
112c1bb8a05SSoby Mathew 
113f14d1886SSoby Mathew /*******************************************************************************
114f14d1886SSoby Mathew  * Handler called when a power level has just been powered on after
115f14d1886SSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
116f14d1886SSoby Mathew  * each level has woken up from. This handler would never be invoked with
117f14d1886SSoby Mathew  * the system power domain uninitialized as either the primary would have taken
118f14d1886SSoby Mathew  * care of it as part of cold boot or the first core awakened from system
119f14d1886SSoby Mathew  * suspend would have already initialized it.
120f14d1886SSoby Mathew  ******************************************************************************/
121f14d1886SSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
122f14d1886SSoby Mathew {
123f14d1886SSoby Mathew 	/* Assert that the system power domain need not be initialized */
124f14d1886SSoby Mathew 	assert(CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_RUN);
125f14d1886SSoby Mathew 
126f14d1886SSoby Mathew 	css_pwr_domain_on_finisher_common(target_state);
127f14d1886SSoby Mathew 
12827573c59SAchin Gupta 	/* Program the gic per-cpu distributor or re-distributor interface */
12927573c59SAchin Gupta 	plat_arm_gic_pcpu_init();
13027573c59SAchin Gupta 
131b4315306SDan Handley 	/* Enable the gic cpu interface */
13227573c59SAchin Gupta 	plat_arm_gic_cpuif_enable();
133b4315306SDan Handley }
134b4315306SDan Handley 
135b4315306SDan Handley /*******************************************************************************
136b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
137b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
13838dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
13938dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
140b4315306SDan Handley  ******************************************************************************/
14138dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
142b4315306SDan Handley {
143b4315306SDan Handley 	uint32_t cluster_state = scpi_power_on;
144c1bb8a05SSoby Mathew 	uint32_t system_state = scpi_power_on;
145b4315306SDan Handley 
146b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
14727573c59SAchin Gupta 	plat_arm_gic_cpuif_disable();
148b4315306SDan Handley 
149f14d1886SSoby Mathew 	/* Check if power down at system power domain level is requested */
150f14d1886SSoby Mathew 	if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
151c1bb8a05SSoby Mathew 			system_state = scpi_power_retention;
152c1bb8a05SSoby Mathew 
153b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
154f14d1886SSoby Mathew 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) {
155*6355f234SVikram Kanigiri 		plat_arm_interconnect_exit_coherency();
156b4315306SDan Handley 		cluster_state = scpi_power_off;
157b4315306SDan Handley 	}
158b4315306SDan Handley 
159b4315306SDan Handley 	/*
160b4315306SDan Handley 	 * Ask the SCP to power down the appropriate components depending upon
161b4315306SDan Handley 	 * their state.
162b4315306SDan Handley 	 */
163b4315306SDan Handley 	scpi_set_css_power_state(read_mpidr_el1(),
164b4315306SDan Handley 				 scpi_power_off,
165b4315306SDan Handley 				 cluster_state,
166c1bb8a05SSoby Mathew 				 system_state);
167b4315306SDan Handley }
168b4315306SDan Handley 
169b4315306SDan Handley /*******************************************************************************
17038dce70fSSoby Mathew  * Handler called when a power domain is about to be turned off. The
17138dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
172b4315306SDan Handley  ******************************************************************************/
173785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state)
174b4315306SDan Handley {
175f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
17638dce70fSSoby Mathew 	css_power_down_common(target_state);
177b4315306SDan Handley }
178b4315306SDan Handley 
179b4315306SDan Handley /*******************************************************************************
18038dce70fSSoby Mathew  * Handler called when a power domain is about to be suspended. The
18138dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
182b4315306SDan Handley  ******************************************************************************/
183785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state)
184b4315306SDan Handley {
18538dce70fSSoby Mathew 	/*
186f14d1886SSoby Mathew 	 * CSS currently supports retention only at cpu level. Just return
18738dce70fSSoby Mathew 	 * as nothing is to be done for retention.
18838dce70fSSoby Mathew 	 */
189f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
190b4315306SDan Handley 		return;
191b4315306SDan Handley 
192f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
19338dce70fSSoby Mathew 	css_power_down_common(target_state);
194b4315306SDan Handley }
195b4315306SDan Handley 
196b4315306SDan Handley /*******************************************************************************
19738dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
19838dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
19938dce70fSSoby Mathew  * that each level has woken up from.
200b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
201b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
202b4315306SDan Handley  ******************************************************************************/
203785fb92bSSoby Mathew void css_pwr_domain_suspend_finish(
20438dce70fSSoby Mathew 				const psci_power_state_t *target_state)
205b4315306SDan Handley {
206f14d1886SSoby Mathew 	/* Return as nothing is to be done on waking up from retention. */
207f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
20838dce70fSSoby Mathew 		return;
20938dce70fSSoby Mathew 
210f14d1886SSoby Mathew 	/* Perform system domain restore if woken up from system suspend */
211f14d1886SSoby Mathew 	if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
212f14d1886SSoby Mathew 		arm_system_pwr_domain_resume();
213f14d1886SSoby Mathew 	else
214f14d1886SSoby Mathew 		/* Enable the gic cpu interface */
21527573c59SAchin Gupta 		plat_arm_gic_cpuif_enable();
216f14d1886SSoby Mathew 
217f14d1886SSoby Mathew 	css_pwr_domain_on_finisher_common(target_state);
218b4315306SDan Handley }
219b4315306SDan Handley 
220b4315306SDan Handley /*******************************************************************************
221b4315306SDan Handley  * Handlers to shutdown/reboot the system
222b4315306SDan Handley  ******************************************************************************/
223785fb92bSSoby Mathew void __dead2 css_system_off(void)
224b4315306SDan Handley {
225b4315306SDan Handley 	uint32_t response;
226b4315306SDan Handley 
227b4315306SDan Handley 	/* Send the power down request to the SCP */
228b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_shutdown);
229b4315306SDan Handley 
230b4315306SDan Handley 	if (response != SCP_OK) {
231b4315306SDan Handley 		ERROR("CSS System Off: SCP error %u.\n", response);
232b4315306SDan Handley 		panic();
233b4315306SDan Handley 	}
234b4315306SDan Handley 	wfi();
235b4315306SDan Handley 	ERROR("CSS System Off: operation not handled.\n");
236b4315306SDan Handley 	panic();
237b4315306SDan Handley }
238b4315306SDan Handley 
239785fb92bSSoby Mathew void __dead2 css_system_reset(void)
240b4315306SDan Handley {
241b4315306SDan Handley 	uint32_t response;
242b4315306SDan Handley 
243b4315306SDan Handley 	/* Send the system reset request to the SCP */
244b4315306SDan Handley 	response = scpi_sys_power_state(scpi_system_reboot);
245b4315306SDan Handley 
246b4315306SDan Handley 	if (response != SCP_OK) {
247b4315306SDan Handley 		ERROR("CSS System Reset: SCP error %u.\n", response);
248b4315306SDan Handley 		panic();
249b4315306SDan Handley 	}
250b4315306SDan Handley 	wfi();
251b4315306SDan Handley 	ERROR("CSS System Reset: operation not handled.\n");
252b4315306SDan Handley 	panic();
253b4315306SDan Handley }
254b4315306SDan Handley 
255b4315306SDan Handley /*******************************************************************************
25638dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
257b4315306SDan Handley  ******************************************************************************/
25838dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
259b4315306SDan Handley {
260b4315306SDan Handley 	unsigned int scr;
261b4315306SDan Handley 
26238dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
26338dce70fSSoby Mathew 
264b4315306SDan Handley 	scr = read_scr_el3();
265b4315306SDan Handley 	/* Enable PhysicalIRQ bit for NS world to wake the CPU */
266b4315306SDan Handley 	write_scr_el3(scr | SCR_IRQ_BIT);
267b4315306SDan Handley 	isb();
268b4315306SDan Handley 	dsb();
269b4315306SDan Handley 	wfi();
270b4315306SDan Handley 
271b4315306SDan Handley 	/*
272b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
273b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
274b4315306SDan Handley 	 */
275b4315306SDan Handley 	write_scr_el3(scr);
276b4315306SDan Handley }
277b4315306SDan Handley 
278b4315306SDan Handley /*******************************************************************************
279c1bb8a05SSoby Mathew  * Handler called to return the 'req_state' for system suspend.
280c1bb8a05SSoby Mathew  ******************************************************************************/
281c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
282c1bb8a05SSoby Mathew {
283c1bb8a05SSoby Mathew 	unsigned int i;
284c1bb8a05SSoby Mathew 
285c1bb8a05SSoby Mathew 	/*
286c1bb8a05SSoby Mathew 	 * System Suspend is supported only if the system power domain node
287c1bb8a05SSoby Mathew 	 * is implemented.
288c1bb8a05SSoby Mathew 	 */
289c1bb8a05SSoby Mathew 	assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
290c1bb8a05SSoby Mathew 
291c1bb8a05SSoby Mathew 	for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
292c1bb8a05SSoby Mathew 		req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
293c1bb8a05SSoby Mathew }
294c1bb8a05SSoby Mathew 
295c1bb8a05SSoby Mathew /*******************************************************************************
296785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
297785fb92bSSoby Mathew  * platform will take care of registering the handlers with PSCI.
298b4315306SDan Handley  ******************************************************************************/
299785fb92bSSoby Mathew const plat_psci_ops_t plat_arm_psci_pm_ops = {
30038dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
30138dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
30238dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
30338dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
30438dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
30538dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
306b4315306SDan Handley 	.system_off		= css_system_off,
307b4315306SDan Handley 	.system_reset		= css_system_reset,
308f9e858b1SSoby Mathew 	.validate_power_state	= arm_validate_power_state,
309f9e858b1SSoby Mathew 	.validate_ns_entrypoint = arm_validate_ns_entrypoint
310b4315306SDan Handley };
311