xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision abd2aba99ef108e0d0bb5d71c0b6e9c47ca26377)
1b4315306SDan Handley /*
2*abd2aba9SSoby Mathew  * Copyright (c) 2015-2017, 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>
40b12a2b49SSoby 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 
78*abd2aba9SSoby Mathew /*
79*abd2aba9SSoby Mathew  * Ensure that the PLAT_MAX_PWR_LVL is not greater than CSS_SYSTEM_PWR_DMN_LVL
80*abd2aba9SSoby Mathew  * assumed by the CSS layer.
81*abd2aba9SSoby Mathew  */
82*abd2aba9SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL <= CSS_SYSTEM_PWR_DMN_LVL,
83*abd2aba9SSoby Mathew 		assert_max_pwr_lvl_higher_than_css_sys_lvl);
84*abd2aba9SSoby Mathew 
85b4315306SDan Handley /*******************************************************************************
8638dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
87b4315306SDan Handley  * level and mpidr determine the affinity instance.
88b4315306SDan Handley  ******************************************************************************/
8938dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
90b4315306SDan Handley {
91b12a2b49SSoby Mathew 	css_scp_on(mpidr);
92b4315306SDan Handley 
93b4315306SDan Handley 	return PSCI_E_SUCCESS;
94b4315306SDan Handley }
95b4315306SDan Handley 
96f14d1886SSoby Mathew static void css_pwr_domain_on_finisher_common(
97f14d1886SSoby Mathew 		const psci_power_state_t *target_state)
98b4315306SDan Handley {
99f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
100c1bb8a05SSoby Mathew 
101b4315306SDan Handley 	/*
102b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
103b4315306SDan Handley 	 * if this cluster was off.
104b4315306SDan Handley 	 */
105f14d1886SSoby Mathew 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
1066355f234SVikram Kanigiri 		plat_arm_interconnect_enter_coherency();
107c1bb8a05SSoby Mathew }
108c1bb8a05SSoby Mathew 
109f14d1886SSoby Mathew /*******************************************************************************
110f14d1886SSoby Mathew  * Handler called when a power level has just been powered on after
111f14d1886SSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
112f14d1886SSoby Mathew  * each level has woken up from. This handler would never be invoked with
113f14d1886SSoby Mathew  * the system power domain uninitialized as either the primary would have taken
114f14d1886SSoby Mathew  * care of it as part of cold boot or the first core awakened from system
115f14d1886SSoby Mathew  * suspend would have already initialized it.
116f14d1886SSoby Mathew  ******************************************************************************/
117f14d1886SSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
118f14d1886SSoby Mathew {
119f14d1886SSoby Mathew 	/* Assert that the system power domain need not be initialized */
120f14d1886SSoby Mathew 	assert(CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_RUN);
121f14d1886SSoby Mathew 
122f14d1886SSoby Mathew 	css_pwr_domain_on_finisher_common(target_state);
123f14d1886SSoby Mathew 
12427573c59SAchin Gupta 	/* Program the gic per-cpu distributor or re-distributor interface */
12527573c59SAchin Gupta 	plat_arm_gic_pcpu_init();
12627573c59SAchin Gupta 
127b4315306SDan Handley 	/* Enable the gic cpu interface */
12827573c59SAchin Gupta 	plat_arm_gic_cpuif_enable();
129b4315306SDan Handley }
130b4315306SDan Handley 
131b4315306SDan Handley /*******************************************************************************
132b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
133b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
13438dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
13538dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
136b4315306SDan Handley  ******************************************************************************/
13738dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
138b4315306SDan Handley {
139b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
14027573c59SAchin Gupta 	plat_arm_gic_cpuif_disable();
141b4315306SDan Handley 
142b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
143b12a2b49SSoby Mathew 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
1446355f234SVikram Kanigiri 		plat_arm_interconnect_exit_coherency();
145b4315306SDan Handley }
146b4315306SDan Handley 
147b4315306SDan Handley /*******************************************************************************
14838dce70fSSoby Mathew  * Handler called when a power domain is about to be turned off. The
14938dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
150b4315306SDan Handley  ******************************************************************************/
151785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state)
152b4315306SDan Handley {
153f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
15438dce70fSSoby Mathew 	css_power_down_common(target_state);
155b12a2b49SSoby Mathew 	css_scp_off(target_state);
156b4315306SDan Handley }
157b4315306SDan Handley 
158b4315306SDan Handley /*******************************************************************************
15938dce70fSSoby Mathew  * Handler called when a power domain is about to be suspended. The
16038dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
161b4315306SDan Handley  ******************************************************************************/
162785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state)
163b4315306SDan Handley {
16438dce70fSSoby Mathew 	/*
165f14d1886SSoby Mathew 	 * CSS currently supports retention only at cpu level. Just return
16638dce70fSSoby Mathew 	 * as nothing is to be done for retention.
16738dce70fSSoby Mathew 	 */
168f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
169b4315306SDan Handley 		return;
170b4315306SDan Handley 
171f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
17238dce70fSSoby Mathew 	css_power_down_common(target_state);
173b12a2b49SSoby Mathew 	css_scp_suspend(target_state);
174b4315306SDan Handley }
175b4315306SDan Handley 
176b4315306SDan Handley /*******************************************************************************
17738dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
17838dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
17938dce70fSSoby Mathew  * that each level has woken up from.
180b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
181b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
182b4315306SDan Handley  ******************************************************************************/
183785fb92bSSoby Mathew void css_pwr_domain_suspend_finish(
18438dce70fSSoby Mathew 				const psci_power_state_t *target_state)
185b4315306SDan Handley {
186f14d1886SSoby Mathew 	/* Return as nothing is to be done on waking up from retention. */
187f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
18838dce70fSSoby Mathew 		return;
18938dce70fSSoby Mathew 
190f14d1886SSoby Mathew 	/* Perform system domain restore if woken up from system suspend */
191f14d1886SSoby Mathew 	if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
192f14d1886SSoby Mathew 		arm_system_pwr_domain_resume();
193f14d1886SSoby Mathew 	else
194f14d1886SSoby Mathew 		/* Enable the gic cpu interface */
19527573c59SAchin Gupta 		plat_arm_gic_cpuif_enable();
196f14d1886SSoby Mathew 
197f14d1886SSoby Mathew 	css_pwr_domain_on_finisher_common(target_state);
198b4315306SDan Handley }
199b4315306SDan Handley 
200b4315306SDan Handley /*******************************************************************************
201b4315306SDan Handley  * Handlers to shutdown/reboot the system
202b4315306SDan Handley  ******************************************************************************/
203785fb92bSSoby Mathew void __dead2 css_system_off(void)
204b4315306SDan Handley {
205b12a2b49SSoby Mathew 	css_scp_sys_shutdown();
206b4315306SDan Handley }
207b4315306SDan Handley 
208785fb92bSSoby Mathew void __dead2 css_system_reset(void)
209b4315306SDan Handley {
210b12a2b49SSoby Mathew 	css_scp_sys_reboot();
211b4315306SDan Handley }
212b4315306SDan Handley 
213b4315306SDan Handley /*******************************************************************************
21438dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
215b4315306SDan Handley  ******************************************************************************/
21638dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
217b4315306SDan Handley {
218b4315306SDan Handley 	unsigned int scr;
219b4315306SDan Handley 
22038dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
22138dce70fSSoby Mathew 
222b4315306SDan Handley 	scr = read_scr_el3();
22368b105aeSDavid Wang 	/*
22468b105aeSDavid Wang 	 * Enable the Non secure interrupt to wake the CPU.
22568b105aeSDavid Wang 	 * In GICv3 affinity routing mode, the non secure group1 interrupts use
22668b105aeSDavid Wang 	 * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ.
22768b105aeSDavid Wang 	 * Enabling both the bits works for both GICv2 mode and GICv3 affinity
22868b105aeSDavid Wang 	 * routing mode.
22968b105aeSDavid Wang 	 */
23068b105aeSDavid Wang 	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
231b4315306SDan Handley 	isb();
232b4315306SDan Handley 	dsb();
233b4315306SDan Handley 	wfi();
234b4315306SDan Handley 
235b4315306SDan Handley 	/*
236b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
237b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
238b4315306SDan Handley 	 */
239b4315306SDan Handley 	write_scr_el3(scr);
240b4315306SDan Handley }
241b4315306SDan Handley 
242b4315306SDan Handley /*******************************************************************************
243c1bb8a05SSoby Mathew  * Handler called to return the 'req_state' for system suspend.
244c1bb8a05SSoby Mathew  ******************************************************************************/
245c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
246c1bb8a05SSoby Mathew {
247c1bb8a05SSoby Mathew 	unsigned int i;
248c1bb8a05SSoby Mathew 
249c1bb8a05SSoby Mathew 	/*
250c1bb8a05SSoby Mathew 	 * System Suspend is supported only if the system power domain node
251c1bb8a05SSoby Mathew 	 * is implemented.
252c1bb8a05SSoby Mathew 	 */
253*abd2aba9SSoby Mathew 	assert(PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL);
254c1bb8a05SSoby Mathew 
255c1bb8a05SSoby Mathew 	for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
256c1bb8a05SSoby Mathew 		req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
257c1bb8a05SSoby Mathew }
258c1bb8a05SSoby Mathew 
259c1bb8a05SSoby Mathew /*******************************************************************************
2603cc17aaeSJeenu Viswambharan  * Handler to query CPU/cluster power states from SCP
2613cc17aaeSJeenu Viswambharan  ******************************************************************************/
2623cc17aaeSJeenu Viswambharan int css_node_hw_state(u_register_t mpidr, unsigned int power_level)
2633cc17aaeSJeenu Viswambharan {
264b12a2b49SSoby Mathew 	return css_scp_get_power_state(mpidr, power_level);
2653cc17aaeSJeenu Viswambharan }
2663cc17aaeSJeenu Viswambharan 
267*abd2aba9SSoby Mathew /*
268*abd2aba9SSoby Mathew  * The system power domain suspend is only supported only via
269*abd2aba9SSoby Mathew  * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain
270*abd2aba9SSoby Mathew  * will be downgraded to the lower level.
271*abd2aba9SSoby Mathew  */
272*abd2aba9SSoby Mathew static int css_validate_power_state(unsigned int power_state,
273*abd2aba9SSoby Mathew 			    psci_power_state_t *req_state)
274*abd2aba9SSoby Mathew {
275*abd2aba9SSoby Mathew 	int rc;
276*abd2aba9SSoby Mathew 	rc = arm_validate_power_state(power_state, req_state);
277*abd2aba9SSoby Mathew 
278*abd2aba9SSoby Mathew 	/*
279*abd2aba9SSoby Mathew 	 * Ensure that the system power domain level is never suspended
280*abd2aba9SSoby Mathew 	 * via PSCI CPU SUSPEND API. Currently system suspend is only
281*abd2aba9SSoby Mathew 	 * supported via PSCI SYSTEM SUSPEND API.
282*abd2aba9SSoby Mathew 	 */
283*abd2aba9SSoby Mathew 	req_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] = ARM_LOCAL_STATE_RUN;
284*abd2aba9SSoby Mathew 	return rc;
285*abd2aba9SSoby Mathew }
286*abd2aba9SSoby Mathew 
287*abd2aba9SSoby Mathew /*
288*abd2aba9SSoby Mathew  * Custom `translate_power_state_by_mpidr` handler for CSS. Unlike in the
289*abd2aba9SSoby Mathew  * `css_validate_power_state`, we do not downgrade the system power
290*abd2aba9SSoby Mathew  * domain level request in `power_state` as it will be used to query the
291*abd2aba9SSoby Mathew  * PSCI_STAT_COUNT/RESIDENCY at the system power domain level.
292*abd2aba9SSoby Mathew  */
293*abd2aba9SSoby Mathew static int css_translate_power_state_by_mpidr(u_register_t mpidr,
294*abd2aba9SSoby Mathew 		unsigned int power_state,
295*abd2aba9SSoby Mathew 		psci_power_state_t *output_state)
296*abd2aba9SSoby Mathew {
297*abd2aba9SSoby Mathew 	return arm_validate_power_state(power_state, output_state);
298*abd2aba9SSoby Mathew }
299*abd2aba9SSoby Mathew 
3003cc17aaeSJeenu Viswambharan /*******************************************************************************
301785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
302785fb92bSSoby Mathew  * platform will take care of registering the handlers with PSCI.
303b4315306SDan Handley  ******************************************************************************/
3045486a965SSoby Mathew plat_psci_ops_t plat_arm_psci_pm_ops = {
30538dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
30638dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
30738dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
30838dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
30938dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
31038dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
311b4315306SDan Handley 	.system_off		= css_system_off,
312b4315306SDan Handley 	.system_reset		= css_system_reset,
313*abd2aba9SSoby Mathew 	.validate_power_state	= css_validate_power_state,
3143cc17aaeSJeenu Viswambharan 	.validate_ns_entrypoint = arm_validate_ns_entrypoint,
315*abd2aba9SSoby Mathew 	.translate_power_state_by_mpidr = css_translate_power_state_by_mpidr,
316*abd2aba9SSoby Mathew 	.get_node_hw_state	= css_node_hw_state,
317*abd2aba9SSoby Mathew 	.get_sys_suspend_power_state = css_get_sys_suspend_power_state
318b4315306SDan Handley };
319