xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision b12a2b493e4dc162ab8bf8fc2353cf6a403a19b3)
1b4315306SDan Handley /*
26355f234SVikram 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>
40*b12a2b49SSoby Mathew #include "../drivers/scp/css_scp.h"
41f14d1886SSoby Mathew 
42785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
43785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops
4438dce70fSSoby Mathew 
452204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
462204afdeSSoby Mathew /*
472204afdeSSoby Mathew  *  The table storing the valid idle power states. Ensure that the
482204afdeSSoby Mathew  *  array entries are populated in ascending order of state-id to
492204afdeSSoby Mathew  *  enable us to use binary search during power state validation.
502204afdeSSoby Mathew  *  The table must be terminated by a NULL entry.
512204afdeSSoby Mathew  */
522204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = {
535f3a6030SSoby Mathew 	/* State-id - 0x001 */
545f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
555f3a6030SSoby Mathew 		ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
565f3a6030SSoby Mathew 	/* State-id - 0x002 */
575f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
585f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
595f3a6030SSoby Mathew 	/* State-id - 0x022 */
605f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
615f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
625f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
635f3a6030SSoby Mathew 	/* State-id - 0x222 */
645f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
655f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
665f3a6030SSoby Mathew #endif
672204afdeSSoby Mathew 	0,
682204afdeSSoby Mathew };
695f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
702204afdeSSoby Mathew 
71c1bb8a05SSoby Mathew /*
72c1bb8a05SSoby Mathew  * All the power management helpers in this file assume at least cluster power
73c1bb8a05SSoby Mathew  * level is supported.
74c1bb8a05SSoby Mathew  */
75c1bb8a05SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1,
76c1bb8a05SSoby Mathew 		assert_max_pwr_lvl_supported_mismatch);
77c1bb8a05SSoby Mathew 
78b4315306SDan Handley /*******************************************************************************
7938dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
80b4315306SDan Handley  * level and mpidr determine the affinity instance.
81b4315306SDan Handley  ******************************************************************************/
8238dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
83b4315306SDan Handley {
84*b12a2b49SSoby Mathew 	css_scp_on(mpidr);
85b4315306SDan Handley 
86b4315306SDan Handley 	return PSCI_E_SUCCESS;
87b4315306SDan Handley }
88b4315306SDan Handley 
89f14d1886SSoby Mathew static void css_pwr_domain_on_finisher_common(
90f14d1886SSoby Mathew 		const psci_power_state_t *target_state)
91b4315306SDan Handley {
92f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
93c1bb8a05SSoby Mathew 
94b4315306SDan Handley 	/*
95b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
96b4315306SDan Handley 	 * if this cluster was off.
97b4315306SDan Handley 	 */
98f14d1886SSoby Mathew 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
996355f234SVikram Kanigiri 		plat_arm_interconnect_enter_coherency();
100c1bb8a05SSoby Mathew }
101c1bb8a05SSoby Mathew 
102f14d1886SSoby Mathew /*******************************************************************************
103f14d1886SSoby Mathew  * Handler called when a power level has just been powered on after
104f14d1886SSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
105f14d1886SSoby Mathew  * each level has woken up from. This handler would never be invoked with
106f14d1886SSoby Mathew  * the system power domain uninitialized as either the primary would have taken
107f14d1886SSoby Mathew  * care of it as part of cold boot or the first core awakened from system
108f14d1886SSoby Mathew  * suspend would have already initialized it.
109f14d1886SSoby Mathew  ******************************************************************************/
110f14d1886SSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
111f14d1886SSoby Mathew {
112f14d1886SSoby Mathew 	/* Assert that the system power domain need not be initialized */
113f14d1886SSoby Mathew 	assert(CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_RUN);
114f14d1886SSoby Mathew 
115f14d1886SSoby Mathew 	css_pwr_domain_on_finisher_common(target_state);
116f14d1886SSoby Mathew 
11727573c59SAchin Gupta 	/* Program the gic per-cpu distributor or re-distributor interface */
11827573c59SAchin Gupta 	plat_arm_gic_pcpu_init();
11927573c59SAchin Gupta 
120b4315306SDan Handley 	/* Enable the gic cpu interface */
12127573c59SAchin Gupta 	plat_arm_gic_cpuif_enable();
122b4315306SDan Handley }
123b4315306SDan Handley 
124b4315306SDan Handley /*******************************************************************************
125b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
126b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
12738dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
12838dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
129b4315306SDan Handley  ******************************************************************************/
13038dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
131b4315306SDan Handley {
132b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
13327573c59SAchin Gupta 	plat_arm_gic_cpuif_disable();
134b4315306SDan Handley 
135b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
136*b12a2b49SSoby Mathew 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
1376355f234SVikram Kanigiri 		plat_arm_interconnect_exit_coherency();
138b4315306SDan Handley }
139b4315306SDan Handley 
140b4315306SDan Handley /*******************************************************************************
14138dce70fSSoby Mathew  * Handler called when a power domain is about to be turned off. The
14238dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
143b4315306SDan Handley  ******************************************************************************/
144785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state)
145b4315306SDan Handley {
146f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
14738dce70fSSoby Mathew 	css_power_down_common(target_state);
148*b12a2b49SSoby Mathew 	css_scp_off(target_state);
149b4315306SDan Handley }
150b4315306SDan Handley 
151b4315306SDan Handley /*******************************************************************************
15238dce70fSSoby Mathew  * Handler called when a power domain is about to be suspended. The
15338dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
154b4315306SDan Handley  ******************************************************************************/
155785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state)
156b4315306SDan Handley {
15738dce70fSSoby Mathew 	/*
158f14d1886SSoby Mathew 	 * CSS currently supports retention only at cpu level. Just return
15938dce70fSSoby Mathew 	 * as nothing is to be done for retention.
16038dce70fSSoby Mathew 	 */
161f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
162b4315306SDan Handley 		return;
163b4315306SDan Handley 
164f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
16538dce70fSSoby Mathew 	css_power_down_common(target_state);
166*b12a2b49SSoby Mathew 	css_scp_suspend(target_state);
167b4315306SDan Handley }
168b4315306SDan Handley 
169b4315306SDan Handley /*******************************************************************************
17038dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
17138dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
17238dce70fSSoby Mathew  * that each level has woken up from.
173b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
174b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
175b4315306SDan Handley  ******************************************************************************/
176785fb92bSSoby Mathew void css_pwr_domain_suspend_finish(
17738dce70fSSoby Mathew 				const psci_power_state_t *target_state)
178b4315306SDan Handley {
179f14d1886SSoby Mathew 	/* Return as nothing is to be done on waking up from retention. */
180f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
18138dce70fSSoby Mathew 		return;
18238dce70fSSoby Mathew 
183f14d1886SSoby Mathew 	/* Perform system domain restore if woken up from system suspend */
184f14d1886SSoby Mathew 	if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
185f14d1886SSoby Mathew 		arm_system_pwr_domain_resume();
186f14d1886SSoby Mathew 	else
187f14d1886SSoby Mathew 		/* Enable the gic cpu interface */
18827573c59SAchin Gupta 		plat_arm_gic_cpuif_enable();
189f14d1886SSoby Mathew 
190f14d1886SSoby Mathew 	css_pwr_domain_on_finisher_common(target_state);
191b4315306SDan Handley }
192b4315306SDan Handley 
193b4315306SDan Handley /*******************************************************************************
194b4315306SDan Handley  * Handlers to shutdown/reboot the system
195b4315306SDan Handley  ******************************************************************************/
196785fb92bSSoby Mathew void __dead2 css_system_off(void)
197b4315306SDan Handley {
198*b12a2b49SSoby Mathew 	css_scp_sys_shutdown();
199b4315306SDan Handley }
200b4315306SDan Handley 
201785fb92bSSoby Mathew void __dead2 css_system_reset(void)
202b4315306SDan Handley {
203*b12a2b49SSoby Mathew 	css_scp_sys_reboot();
204b4315306SDan Handley }
205b4315306SDan Handley 
206b4315306SDan Handley /*******************************************************************************
20738dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
208b4315306SDan Handley  ******************************************************************************/
20938dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
210b4315306SDan Handley {
211b4315306SDan Handley 	unsigned int scr;
212b4315306SDan Handley 
21338dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
21438dce70fSSoby Mathew 
215b4315306SDan Handley 	scr = read_scr_el3();
21668b105aeSDavid Wang 	/*
21768b105aeSDavid Wang 	 * Enable the Non secure interrupt to wake the CPU.
21868b105aeSDavid Wang 	 * In GICv3 affinity routing mode, the non secure group1 interrupts use
21968b105aeSDavid Wang 	 * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ.
22068b105aeSDavid Wang 	 * Enabling both the bits works for both GICv2 mode and GICv3 affinity
22168b105aeSDavid Wang 	 * routing mode.
22268b105aeSDavid Wang 	 */
22368b105aeSDavid Wang 	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
224b4315306SDan Handley 	isb();
225b4315306SDan Handley 	dsb();
226b4315306SDan Handley 	wfi();
227b4315306SDan Handley 
228b4315306SDan Handley 	/*
229b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
230b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
231b4315306SDan Handley 	 */
232b4315306SDan Handley 	write_scr_el3(scr);
233b4315306SDan Handley }
234b4315306SDan Handley 
235b4315306SDan Handley /*******************************************************************************
236c1bb8a05SSoby Mathew  * Handler called to return the 'req_state' for system suspend.
237c1bb8a05SSoby Mathew  ******************************************************************************/
238c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
239c1bb8a05SSoby Mathew {
240c1bb8a05SSoby Mathew 	unsigned int i;
241c1bb8a05SSoby Mathew 
242c1bb8a05SSoby Mathew 	/*
243c1bb8a05SSoby Mathew 	 * System Suspend is supported only if the system power domain node
244c1bb8a05SSoby Mathew 	 * is implemented.
245c1bb8a05SSoby Mathew 	 */
246c1bb8a05SSoby Mathew 	assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
247c1bb8a05SSoby Mathew 
248c1bb8a05SSoby Mathew 	for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
249c1bb8a05SSoby Mathew 		req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
250c1bb8a05SSoby Mathew }
251c1bb8a05SSoby Mathew 
252c1bb8a05SSoby Mathew /*******************************************************************************
2533cc17aaeSJeenu Viswambharan  * Handler to query CPU/cluster power states from SCP
2543cc17aaeSJeenu Viswambharan  ******************************************************************************/
2553cc17aaeSJeenu Viswambharan int css_node_hw_state(u_register_t mpidr, unsigned int power_level)
2563cc17aaeSJeenu Viswambharan {
257*b12a2b49SSoby Mathew 	return css_scp_get_power_state(mpidr, power_level);
2583cc17aaeSJeenu Viswambharan }
2593cc17aaeSJeenu Viswambharan 
2603cc17aaeSJeenu Viswambharan /*******************************************************************************
261785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
262785fb92bSSoby Mathew  * platform will take care of registering the handlers with PSCI.
263b4315306SDan Handley  ******************************************************************************/
264785fb92bSSoby Mathew const plat_psci_ops_t plat_arm_psci_pm_ops = {
26538dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
26638dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
26738dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
26838dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
26938dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
27038dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
271b4315306SDan Handley 	.system_off		= css_system_off,
272b4315306SDan Handley 	.system_reset		= css_system_reset,
273f9e858b1SSoby Mathew 	.validate_power_state	= arm_validate_power_state,
2743cc17aaeSJeenu Viswambharan 	.validate_ns_entrypoint = arm_validate_ns_entrypoint,
2753cc17aaeSJeenu Viswambharan 	.get_node_hw_state	= css_node_hw_state
276b4315306SDan Handley };
277