xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision b87d7ab13f4b03f872c3c4a3dd7c755baf3a38d3)
1b4315306SDan Handley /*
2158ed580SPranav Madhu  * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.
3b4315306SDan Handley  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5b4315306SDan Handley  */
6b4315306SDan Handley 
7785fb92bSSoby Mathew #include <assert.h>
809d40e0eSAntonio Nino Diaz 
9b4315306SDan Handley #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
12158ed580SPranav Madhu #include <bl31/interrupt_mgmt.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
142d4135e0SAntonio Nino Diaz #include <drivers/arm/css/css_scp.h>
15*b87d7ab1SArvind Ram Prakash #include <drivers/arm/css/dsu.h>
1609d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
17bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
1809d40e0eSAntonio Nino Diaz 
19158ed580SPranav Madhu #include <plat/common/platform.h>
20158ed580SPranav Madhu 
21f1fe1440SPranav Madhu #include <plat/arm/css/common/css_pm.h>
22f1fe1440SPranav Madhu 
23785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
24785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops
2538dce70fSSoby Mathew 
262204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
272204afdeSSoby Mathew /*
282204afdeSSoby Mathew  *  The table storing the valid idle power states. Ensure that the
292204afdeSSoby Mathew  *  array entries are populated in ascending order of state-id to
302204afdeSSoby Mathew  *  enable us to use binary search during power state validation.
312204afdeSSoby Mathew  *  The table must be terminated by a NULL entry.
322204afdeSSoby Mathew  */
332204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = {
345f3a6030SSoby Mathew 	/* State-id - 0x001 */
355f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
365f3a6030SSoby Mathew 		ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
375f3a6030SSoby Mathew 	/* State-id - 0x002 */
385f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
395f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
405f3a6030SSoby Mathew 	/* State-id - 0x022 */
415f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
425f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
435f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
445f3a6030SSoby Mathew 	/* State-id - 0x222 */
455f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
465f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
475f3a6030SSoby Mathew #endif
482204afdeSSoby Mathew 	0,
492204afdeSSoby Mathew };
505f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
512204afdeSSoby Mathew 
52c1bb8a05SSoby Mathew /*
53c1bb8a05SSoby Mathew  * All the power management helpers in this file assume at least cluster power
54c1bb8a05SSoby Mathew  * level is supported.
55c1bb8a05SSoby Mathew  */
56c1bb8a05SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1,
57c1bb8a05SSoby Mathew 		assert_max_pwr_lvl_supported_mismatch);
58c1bb8a05SSoby Mathew 
59abd2aba9SSoby Mathew /*
60abd2aba9SSoby Mathew  * Ensure that the PLAT_MAX_PWR_LVL is not greater than CSS_SYSTEM_PWR_DMN_LVL
61abd2aba9SSoby Mathew  * assumed by the CSS layer.
62abd2aba9SSoby Mathew  */
63abd2aba9SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL <= CSS_SYSTEM_PWR_DMN_LVL,
64abd2aba9SSoby Mathew 		assert_max_pwr_lvl_higher_than_css_sys_lvl);
65abd2aba9SSoby Mathew 
66b4315306SDan Handley /*******************************************************************************
6738dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
68b4315306SDan Handley  * level and mpidr determine the affinity instance.
69b4315306SDan Handley  ******************************************************************************/
7038dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
71b4315306SDan Handley {
72b12a2b49SSoby Mathew 	css_scp_on(mpidr);
73b4315306SDan Handley 
74b4315306SDan Handley 	return PSCI_E_SUCCESS;
75b4315306SDan Handley }
76b4315306SDan Handley 
77f14d1886SSoby Mathew static void css_pwr_domain_on_finisher_common(
78f14d1886SSoby Mathew 		const psci_power_state_t *target_state)
79b4315306SDan Handley {
80f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
81c1bb8a05SSoby Mathew 
82b4315306SDan Handley 	/*
83b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
84b4315306SDan Handley 	 * if this cluster was off.
85b4315306SDan Handley 	 */
86*b87d7ab1SArvind Ram Prakash 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) {
87*b87d7ab1SArvind Ram Prakash #if PRESERVE_DSU_PMU_REGS
88*b87d7ab1SArvind Ram Prakash 		cluster_on_dsu_pmu_context_restore();
89*b87d7ab1SArvind Ram Prakash #endif
906355f234SVikram Kanigiri 		plat_arm_interconnect_enter_coherency();
91c1bb8a05SSoby Mathew 	}
92*b87d7ab1SArvind Ram Prakash }
93c1bb8a05SSoby Mathew 
94f14d1886SSoby Mathew /*******************************************************************************
95f14d1886SSoby Mathew  * Handler called when a power level has just been powered on after
96f14d1886SSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
97f14d1886SSoby Mathew  * each level has woken up from. This handler would never be invoked with
98f14d1886SSoby Mathew  * the system power domain uninitialized as either the primary would have taken
99f14d1886SSoby Mathew  * care of it as part of cold boot or the first core awakened from system
100f14d1886SSoby Mathew  * suspend would have already initialized it.
101f14d1886SSoby Mathew  ******************************************************************************/
102f14d1886SSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
103f14d1886SSoby Mathew {
104f14d1886SSoby Mathew 	/* Assert that the system power domain need not be initialized */
1059b4c611cSNariman Poushin 	assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
106f14d1886SSoby Mathew 
1076806cd23SMadhukar Pappireddy 	css_pwr_domain_on_finisher_common(target_state);
1086806cd23SMadhukar Pappireddy }
1096806cd23SMadhukar Pappireddy 
1106806cd23SMadhukar Pappireddy /*******************************************************************************
1116806cd23SMadhukar Pappireddy  * Handler called when a power domain has just been powered on and the cpu
1126806cd23SMadhukar Pappireddy  * and its cluster are fully participating in coherent transaction on the
1136806cd23SMadhukar Pappireddy  * interconnect. Data cache must be enabled for CPU at this point.
1146806cd23SMadhukar Pappireddy  ******************************************************************************/
1156806cd23SMadhukar Pappireddy void css_pwr_domain_on_finish_late(const psci_power_state_t *target_state)
1166806cd23SMadhukar Pappireddy {
11727573c59SAchin Gupta 	/* Program the gic per-cpu distributor or re-distributor interface */
11827573c59SAchin Gupta 	plat_arm_gic_pcpu_init();
11927573c59SAchin Gupta 
1206806cd23SMadhukar Pappireddy 	/* Enable the gic cpu interface */
1216806cd23SMadhukar Pappireddy 	plat_arm_gic_cpuif_enable();
122158ed580SPranav Madhu 
123158ed580SPranav Madhu 	/* Setup the CPU power down request interrupt for secondary core(s) */
124158ed580SPranav Madhu 	css_setup_cpu_pwr_down_intr();
125b4315306SDan Handley }
126b4315306SDan Handley 
127b4315306SDan Handley /*******************************************************************************
128b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
129b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
13038dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
13138dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
132b4315306SDan Handley  ******************************************************************************/
13338dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
134b4315306SDan Handley {
135b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
13627573c59SAchin Gupta 	plat_arm_gic_cpuif_disable();
137b4315306SDan Handley 
138b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
139*b87d7ab1SArvind Ram Prakash 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) {
140*b87d7ab1SArvind Ram Prakash #if PRESERVE_DSU_PMU_REGS
141*b87d7ab1SArvind Ram Prakash 		cluster_off_dsu_pmu_context_save();
142*b87d7ab1SArvind Ram Prakash #endif
1436355f234SVikram Kanigiri 		plat_arm_interconnect_exit_coherency();
144b4315306SDan Handley 	}
145*b87d7ab1SArvind Ram Prakash }
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 
171e35a3fb5SSoby Mathew 
172f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
17338dce70fSSoby Mathew 	css_power_down_common(target_state);
174e35a3fb5SSoby Mathew 
175e35a3fb5SSoby Mathew 	/* Perform system domain state saving if issuing system suspend */
1769b4c611cSNariman Poushin 	if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) {
177e35a3fb5SSoby Mathew 		arm_system_pwr_domain_save();
178e35a3fb5SSoby Mathew 
179e35a3fb5SSoby Mathew 		/* Power off the Redistributor after having saved its context */
180e35a3fb5SSoby Mathew 		plat_arm_gic_redistif_off();
181e35a3fb5SSoby Mathew 	}
182e35a3fb5SSoby Mathew 
183b12a2b49SSoby Mathew 	css_scp_suspend(target_state);
184b4315306SDan Handley }
185b4315306SDan Handley 
186b4315306SDan Handley /*******************************************************************************
18738dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
18838dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
18938dce70fSSoby Mathew  * that each level has woken up from.
190b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
191b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
192b4315306SDan Handley  ******************************************************************************/
193785fb92bSSoby Mathew void css_pwr_domain_suspend_finish(
19438dce70fSSoby Mathew 				const psci_power_state_t *target_state)
195b4315306SDan Handley {
196f14d1886SSoby Mathew 	/* Return as nothing is to be done on waking up from retention. */
197f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
19838dce70fSSoby Mathew 		return;
19938dce70fSSoby Mathew 
200f14d1886SSoby Mathew 	/* Perform system domain restore if woken up from system suspend */
2019b4c611cSNariman Poushin 	if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF)
202e35a3fb5SSoby Mathew 		/*
203e35a3fb5SSoby Mathew 		 * At this point, the Distributor must be powered on to be ready
204e35a3fb5SSoby Mathew 		 * to have its state restored. The Redistributor will be powered
205e35a3fb5SSoby Mathew 		 * on as part of gicv3_rdistif_init_restore.
206e35a3fb5SSoby Mathew 		 */
207f14d1886SSoby Mathew 		arm_system_pwr_domain_resume();
208f14d1886SSoby Mathew 
209f14d1886SSoby Mathew 	css_pwr_domain_on_finisher_common(target_state);
2106806cd23SMadhukar Pappireddy 
2116806cd23SMadhukar Pappireddy 	/* Enable the gic cpu interface */
2126806cd23SMadhukar Pappireddy 	plat_arm_gic_cpuif_enable();
213b4315306SDan Handley }
214b4315306SDan Handley 
215b4315306SDan Handley /*******************************************************************************
216b4315306SDan Handley  * Handlers to shutdown/reboot the system
217b4315306SDan Handley  ******************************************************************************/
218785fb92bSSoby Mathew void __dead2 css_system_off(void)
219b4315306SDan Handley {
220b12a2b49SSoby Mathew 	css_scp_sys_shutdown();
221b4315306SDan Handley }
222b4315306SDan Handley 
223785fb92bSSoby Mathew void __dead2 css_system_reset(void)
224b4315306SDan Handley {
225b12a2b49SSoby Mathew 	css_scp_sys_reboot();
226b4315306SDan Handley }
227b4315306SDan Handley 
228b4315306SDan Handley /*******************************************************************************
22938dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
230b4315306SDan Handley  ******************************************************************************/
23138dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
232b4315306SDan Handley {
233b4315306SDan Handley 	unsigned int scr;
234b4315306SDan Handley 
23538dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
23638dce70fSSoby Mathew 
237b4315306SDan Handley 	scr = read_scr_el3();
23868b105aeSDavid Wang 	/*
23968b105aeSDavid Wang 	 * Enable the Non secure interrupt to wake the CPU.
24068b105aeSDavid Wang 	 * In GICv3 affinity routing mode, the non secure group1 interrupts use
24168b105aeSDavid Wang 	 * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ.
24268b105aeSDavid Wang 	 * Enabling both the bits works for both GICv2 mode and GICv3 affinity
24368b105aeSDavid Wang 	 * routing mode.
24468b105aeSDavid Wang 	 */
24568b105aeSDavid Wang 	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
246b4315306SDan Handley 	isb();
247b4315306SDan Handley 	dsb();
248b4315306SDan Handley 	wfi();
249b4315306SDan Handley 
250b4315306SDan Handley 	/*
251b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
252b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
253b4315306SDan Handley 	 */
254b4315306SDan Handley 	write_scr_el3(scr);
255b4315306SDan Handley }
256b4315306SDan Handley 
257b4315306SDan Handley /*******************************************************************************
258c1bb8a05SSoby Mathew  * Handler called to return the 'req_state' for system suspend.
259c1bb8a05SSoby Mathew  ******************************************************************************/
260c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
261c1bb8a05SSoby Mathew {
262c1bb8a05SSoby Mathew 	unsigned int i;
263c1bb8a05SSoby Mathew 
264c1bb8a05SSoby Mathew 	/*
265c1bb8a05SSoby Mathew 	 * System Suspend is supported only if the system power domain node
266c1bb8a05SSoby Mathew 	 * is implemented.
267c1bb8a05SSoby Mathew 	 */
268abd2aba9SSoby Mathew 	assert(PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL);
269c1bb8a05SSoby Mathew 
270c1bb8a05SSoby Mathew 	for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
271c1bb8a05SSoby Mathew 		req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
272c1bb8a05SSoby Mathew }
273c1bb8a05SSoby Mathew 
274c1bb8a05SSoby Mathew /*******************************************************************************
2753cc17aaeSJeenu Viswambharan  * Handler to query CPU/cluster power states from SCP
2763cc17aaeSJeenu Viswambharan  ******************************************************************************/
2773cc17aaeSJeenu Viswambharan int css_node_hw_state(u_register_t mpidr, unsigned int power_level)
2783cc17aaeSJeenu Viswambharan {
279b12a2b49SSoby Mathew 	return css_scp_get_power_state(mpidr, power_level);
2803cc17aaeSJeenu Viswambharan }
2813cc17aaeSJeenu Viswambharan 
282abd2aba9SSoby Mathew /*
283abd2aba9SSoby Mathew  * The system power domain suspend is only supported only via
284abd2aba9SSoby Mathew  * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain
285abd2aba9SSoby Mathew  * will be downgraded to the lower level.
286abd2aba9SSoby Mathew  */
287abd2aba9SSoby Mathew static int css_validate_power_state(unsigned int power_state,
288abd2aba9SSoby Mathew 			    psci_power_state_t *req_state)
289abd2aba9SSoby Mathew {
290abd2aba9SSoby Mathew 	int rc;
291abd2aba9SSoby Mathew 	rc = arm_validate_power_state(power_state, req_state);
292abd2aba9SSoby Mathew 
293abd2aba9SSoby Mathew 	/*
2948e26307dSNariman Poushin 	 * Ensure that we don't overrun the pwr_domain_state array in the case
2958e26307dSNariman Poushin 	 * where the platform supported max power level is less than the system
2968e26307dSNariman Poushin 	 * power level
2978e26307dSNariman Poushin 	 */
2988e26307dSNariman Poushin 
2998e26307dSNariman Poushin #if (PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL)
3008e26307dSNariman Poushin 
3018e26307dSNariman Poushin 	/*
302abd2aba9SSoby Mathew 	 * Ensure that the system power domain level is never suspended
303abd2aba9SSoby Mathew 	 * via PSCI CPU SUSPEND API. Currently system suspend is only
304abd2aba9SSoby Mathew 	 * supported via PSCI SYSTEM SUSPEND API.
305abd2aba9SSoby Mathew 	 */
3068e26307dSNariman Poushin 
3078e26307dSNariman Poushin 	req_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] =
3088e26307dSNariman Poushin 							ARM_LOCAL_STATE_RUN;
3098e26307dSNariman Poushin #endif
3108e26307dSNariman Poushin 
311abd2aba9SSoby Mathew 	return rc;
312abd2aba9SSoby Mathew }
313abd2aba9SSoby Mathew 
314abd2aba9SSoby Mathew /*
315abd2aba9SSoby Mathew  * Custom `translate_power_state_by_mpidr` handler for CSS. Unlike in the
316abd2aba9SSoby Mathew  * `css_validate_power_state`, we do not downgrade the system power
317abd2aba9SSoby Mathew  * domain level request in `power_state` as it will be used to query the
318abd2aba9SSoby Mathew  * PSCI_STAT_COUNT/RESIDENCY at the system power domain level.
319abd2aba9SSoby Mathew  */
320abd2aba9SSoby Mathew static int css_translate_power_state_by_mpidr(u_register_t mpidr,
321abd2aba9SSoby Mathew 		unsigned int power_state,
322abd2aba9SSoby Mathew 		psci_power_state_t *output_state)
323abd2aba9SSoby Mathew {
324abd2aba9SSoby Mathew 	return arm_validate_power_state(power_state, output_state);
325abd2aba9SSoby Mathew }
326abd2aba9SSoby Mathew 
327158ed580SPranav Madhu /*
328158ed580SPranav Madhu  * Setup the SGI interrupt that will be used trigger the execution of power
329158ed580SPranav Madhu  * down sequence for all the secondary cores. This interrupt is setup to be
330158ed580SPranav Madhu  * handled in EL3 context at a priority defined by the platform.
331158ed580SPranav Madhu  */
332158ed580SPranav Madhu void css_setup_cpu_pwr_down_intr(void)
333158ed580SPranav Madhu {
334158ed580SPranav Madhu #if CSS_SYSTEM_GRACEFUL_RESET
335158ed580SPranav Madhu 	plat_ic_set_interrupt_type(CSS_CPU_PWR_DOWN_REQ_INTR, INTR_TYPE_EL3);
336158ed580SPranav Madhu 	plat_ic_set_interrupt_priority(CSS_CPU_PWR_DOWN_REQ_INTR,
337158ed580SPranav Madhu 			PLAT_REBOOT_PRI);
338158ed580SPranav Madhu 	plat_ic_enable_interrupt(CSS_CPU_PWR_DOWN_REQ_INTR);
339158ed580SPranav Madhu #endif
340158ed580SPranav Madhu }
341158ed580SPranav Madhu 
342f1fe1440SPranav Madhu /*
343f1fe1440SPranav Madhu  * For a graceful shutdown/reboot, each CPU in the system should do their power
344f1fe1440SPranav Madhu  * down sequence. On a PSCI shutdown/reboot request, only one CPU gets an
345f1fe1440SPranav Madhu  * opportunity to do the powerdown sequence. To achieve graceful reset, of all
346f1fe1440SPranav Madhu  * cores in the system, the CPU gets the opportunity raise warm reboot SGI to
347f1fe1440SPranav Madhu  * rest of the CPUs which are online. Add handler for the reboot SGI where the
348f1fe1440SPranav Madhu  * rest of the CPU execute the powerdown sequence.
349f1fe1440SPranav Madhu  */
350f1fe1440SPranav Madhu int css_reboot_interrupt_handler(uint32_t intr_raw, uint32_t flags,
351f1fe1440SPranav Madhu 		void *handle, void *cookie)
352f1fe1440SPranav Madhu {
353f1fe1440SPranav Madhu 	assert(intr_raw == CSS_CPU_PWR_DOWN_REQ_INTR);
354f1fe1440SPranav Madhu 
355f1fe1440SPranav Madhu 	/* Deactivate warm reboot SGI */
356f1fe1440SPranav Madhu 	plat_ic_end_of_interrupt(CSS_CPU_PWR_DOWN_REQ_INTR);
357f1fe1440SPranav Madhu 
358f1fe1440SPranav Madhu 	/*
359f1fe1440SPranav Madhu 	 * Disable GIC CPU interface to prevent pending interrupt from waking
360f1fe1440SPranav Madhu 	 * up the AP from WFI.
361f1fe1440SPranav Madhu 	 */
362f1fe1440SPranav Madhu 	plat_arm_gic_cpuif_disable();
363f1fe1440SPranav Madhu 	plat_arm_gic_redistif_off();
364f1fe1440SPranav Madhu 
365f1fe1440SPranav Madhu 	psci_pwrdown_cpu(PLAT_MAX_PWR_LVL);
366f1fe1440SPranav Madhu 
367f1fe1440SPranav Madhu 	dmbsy();
368f1fe1440SPranav Madhu 
369f1fe1440SPranav Madhu 	wfi();
370f1fe1440SPranav Madhu 	return 0;
371f1fe1440SPranav Madhu }
372f1fe1440SPranav Madhu 
3733cc17aaeSJeenu Viswambharan /*******************************************************************************
374785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
375785fb92bSSoby Mathew  * platform will take care of registering the handlers with PSCI.
376b4315306SDan Handley  ******************************************************************************/
3775486a965SSoby Mathew plat_psci_ops_t plat_arm_psci_pm_ops = {
37838dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
37938dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
3806806cd23SMadhukar Pappireddy 	.pwr_domain_on_finish_late = css_pwr_domain_on_finish_late,
38138dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
38238dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
38338dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
38438dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
385b4315306SDan Handley 	.system_off		= css_system_off,
386b4315306SDan Handley 	.system_reset		= css_system_reset,
387abd2aba9SSoby Mathew 	.validate_power_state	= css_validate_power_state,
38871e7a4e5SJeenu Viswambharan 	.validate_ns_entrypoint = arm_validate_psci_entrypoint,
389abd2aba9SSoby Mathew 	.translate_power_state_by_mpidr = css_translate_power_state_by_mpidr,
390abd2aba9SSoby Mathew 	.get_node_hw_state	= css_node_hw_state,
391f145403cSRoberto Vargas 	.get_sys_suspend_power_state = css_get_sys_suspend_power_state,
392638b034cSRoberto Vargas 
393638b034cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
394f145403cSRoberto Vargas 	.mem_protect_chk	= arm_psci_mem_protect_chk,
395f145403cSRoberto Vargas 	.read_mem_protect	= arm_psci_read_mem_protect,
396f145403cSRoberto Vargas 	.write_mem_protect	= arm_nor_psci_write_mem_protect,
397f145403cSRoberto Vargas #endif
398b48ae263SRoberto Vargas #if CSS_USE_SCMI_SDS_DRIVER
399b48ae263SRoberto Vargas 	.system_reset2		= css_system_reset2,
400b48ae263SRoberto Vargas #endif
401b4315306SDan Handley };
402