xref: /rk3399_ARM-atf/plat/arm/css/common/css_pm.c (revision f1fe1440db197d514b5484e780cfb90f504c62b9)
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>
1509d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
16bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
1709d40e0eSAntonio Nino Diaz 
18158ed580SPranav Madhu #include <plat/common/platform.h>
19158ed580SPranav Madhu 
20*f1fe1440SPranav Madhu #include <plat/arm/css/common/css_pm.h>
21*f1fe1440SPranav Madhu 
22785fb92bSSoby Mathew /* Allow CSS platforms to override `plat_arm_psci_pm_ops` */
23785fb92bSSoby Mathew #pragma weak plat_arm_psci_pm_ops
2438dce70fSSoby Mathew 
252204afdeSSoby Mathew #if ARM_RECOM_STATE_ID_ENC
262204afdeSSoby Mathew /*
272204afdeSSoby Mathew  *  The table storing the valid idle power states. Ensure that the
282204afdeSSoby Mathew  *  array entries are populated in ascending order of state-id to
292204afdeSSoby Mathew  *  enable us to use binary search during power state validation.
302204afdeSSoby Mathew  *  The table must be terminated by a NULL entry.
312204afdeSSoby Mathew  */
322204afdeSSoby Mathew const unsigned int arm_pm_idle_states[] = {
335f3a6030SSoby Mathew 	/* State-id - 0x001 */
345f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
355f3a6030SSoby Mathew 		ARM_LOCAL_STATE_RET, ARM_PWR_LVL0, PSTATE_TYPE_STANDBY),
365f3a6030SSoby Mathew 	/* State-id - 0x002 */
375f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_RUN,
385f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL0, PSTATE_TYPE_POWERDOWN),
395f3a6030SSoby Mathew 	/* State-id - 0x022 */
405f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_RUN, ARM_LOCAL_STATE_OFF,
415f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL1, PSTATE_TYPE_POWERDOWN),
425f3a6030SSoby Mathew #if PLAT_MAX_PWR_LVL > ARM_PWR_LVL1
435f3a6030SSoby Mathew 	/* State-id - 0x222 */
445f3a6030SSoby Mathew 	arm_make_pwrstate_lvl2(ARM_LOCAL_STATE_OFF, ARM_LOCAL_STATE_OFF,
455f3a6030SSoby Mathew 		ARM_LOCAL_STATE_OFF, ARM_PWR_LVL2, PSTATE_TYPE_POWERDOWN),
465f3a6030SSoby Mathew #endif
472204afdeSSoby Mathew 	0,
482204afdeSSoby Mathew };
495f3a6030SSoby Mathew #endif /* __ARM_RECOM_STATE_ID_ENC__ */
502204afdeSSoby Mathew 
51c1bb8a05SSoby Mathew /*
52c1bb8a05SSoby Mathew  * All the power management helpers in this file assume at least cluster power
53c1bb8a05SSoby Mathew  * level is supported.
54c1bb8a05SSoby Mathew  */
55c1bb8a05SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL1,
56c1bb8a05SSoby Mathew 		assert_max_pwr_lvl_supported_mismatch);
57c1bb8a05SSoby Mathew 
58abd2aba9SSoby Mathew /*
59abd2aba9SSoby Mathew  * Ensure that the PLAT_MAX_PWR_LVL is not greater than CSS_SYSTEM_PWR_DMN_LVL
60abd2aba9SSoby Mathew  * assumed by the CSS layer.
61abd2aba9SSoby Mathew  */
62abd2aba9SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL <= CSS_SYSTEM_PWR_DMN_LVL,
63abd2aba9SSoby Mathew 		assert_max_pwr_lvl_higher_than_css_sys_lvl);
64abd2aba9SSoby Mathew 
65b4315306SDan Handley /*******************************************************************************
6638dce70fSSoby Mathew  * Handler called when a power domain is about to be turned on. The
67b4315306SDan Handley  * level and mpidr determine the affinity instance.
68b4315306SDan Handley  ******************************************************************************/
6938dce70fSSoby Mathew int css_pwr_domain_on(u_register_t mpidr)
70b4315306SDan Handley {
71b12a2b49SSoby Mathew 	css_scp_on(mpidr);
72b4315306SDan Handley 
73b4315306SDan Handley 	return PSCI_E_SUCCESS;
74b4315306SDan Handley }
75b4315306SDan Handley 
76f14d1886SSoby Mathew static void css_pwr_domain_on_finisher_common(
77f14d1886SSoby Mathew 		const psci_power_state_t *target_state)
78b4315306SDan Handley {
79f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
80c1bb8a05SSoby Mathew 
81b4315306SDan Handley 	/*
82b4315306SDan Handley 	 * Perform the common cluster specific operations i.e enable coherency
83b4315306SDan Handley 	 * if this cluster was off.
84b4315306SDan Handley 	 */
85f14d1886SSoby Mathew 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF)
866355f234SVikram Kanigiri 		plat_arm_interconnect_enter_coherency();
87c1bb8a05SSoby Mathew }
88c1bb8a05SSoby Mathew 
89f14d1886SSoby Mathew /*******************************************************************************
90f14d1886SSoby Mathew  * Handler called when a power level has just been powered on after
91f14d1886SSoby Mathew  * being turned off earlier. The target_state encodes the low power state that
92f14d1886SSoby Mathew  * each level has woken up from. This handler would never be invoked with
93f14d1886SSoby Mathew  * the system power domain uninitialized as either the primary would have taken
94f14d1886SSoby Mathew  * care of it as part of cold boot or the first core awakened from system
95f14d1886SSoby Mathew  * suspend would have already initialized it.
96f14d1886SSoby Mathew  ******************************************************************************/
97f14d1886SSoby Mathew void css_pwr_domain_on_finish(const psci_power_state_t *target_state)
98f14d1886SSoby Mathew {
99f14d1886SSoby Mathew 	/* Assert that the system power domain need not be initialized */
1009b4c611cSNariman Poushin 	assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
101f14d1886SSoby Mathew 
1026806cd23SMadhukar Pappireddy 	css_pwr_domain_on_finisher_common(target_state);
1036806cd23SMadhukar Pappireddy }
1046806cd23SMadhukar Pappireddy 
1056806cd23SMadhukar Pappireddy /*******************************************************************************
1066806cd23SMadhukar Pappireddy  * Handler called when a power domain has just been powered on and the cpu
1076806cd23SMadhukar Pappireddy  * and its cluster are fully participating in coherent transaction on the
1086806cd23SMadhukar Pappireddy  * interconnect. Data cache must be enabled for CPU at this point.
1096806cd23SMadhukar Pappireddy  ******************************************************************************/
1106806cd23SMadhukar Pappireddy void css_pwr_domain_on_finish_late(const psci_power_state_t *target_state)
1116806cd23SMadhukar Pappireddy {
11227573c59SAchin Gupta 	/* Program the gic per-cpu distributor or re-distributor interface */
11327573c59SAchin Gupta 	plat_arm_gic_pcpu_init();
11427573c59SAchin Gupta 
1156806cd23SMadhukar Pappireddy 	/* Enable the gic cpu interface */
1166806cd23SMadhukar Pappireddy 	plat_arm_gic_cpuif_enable();
117158ed580SPranav Madhu 
118158ed580SPranav Madhu 	/* Setup the CPU power down request interrupt for secondary core(s) */
119158ed580SPranav Madhu 	css_setup_cpu_pwr_down_intr();
120b4315306SDan Handley }
121b4315306SDan Handley 
122b4315306SDan Handley /*******************************************************************************
123b4315306SDan Handley  * Common function called while turning a cpu off or suspending it. It is called
124b4315306SDan Handley  * from css_off() or css_suspend() when these functions in turn are called for
12538dce70fSSoby Mathew  * power domain at the highest power level which will be powered down. It
12638dce70fSSoby Mathew  * performs the actions common to the OFF and SUSPEND calls.
127b4315306SDan Handley  ******************************************************************************/
12838dce70fSSoby Mathew static void css_power_down_common(const psci_power_state_t *target_state)
129b4315306SDan Handley {
130b4315306SDan Handley 	/* Prevent interrupts from spuriously waking up this cpu */
13127573c59SAchin Gupta 	plat_arm_gic_cpuif_disable();
132b4315306SDan Handley 
1334d8c1819SJagadeesh Ujja 	/* Turn redistributor off */
1344d8c1819SJagadeesh Ujja 	plat_arm_gic_redistif_off();
1354d8c1819SJagadeesh Ujja 
136b4315306SDan Handley 	/* Cluster is to be turned off, so disable coherency */
1379cf7f355SMadhukar Pappireddy 	if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) {
1386355f234SVikram Kanigiri 		plat_arm_interconnect_exit_coherency();
1399cf7f355SMadhukar Pappireddy 
1409cf7f355SMadhukar Pappireddy #if HW_ASSISTED_COHERENCY
1419cf7f355SMadhukar Pappireddy 		uint32_t reg;
1429cf7f355SMadhukar Pappireddy 
1439cf7f355SMadhukar Pappireddy 		/*
1449cf7f355SMadhukar Pappireddy 		 * If we have determined this core to be the last man standing and we
1459cf7f355SMadhukar Pappireddy 		 * intend to power down the cluster proactively, we provide a hint to
1469cf7f355SMadhukar Pappireddy 		 * the power controller that cluster power is not required when all
1479cf7f355SMadhukar Pappireddy 		 * cores are powered down.
1489cf7f355SMadhukar Pappireddy 		 * Note that this is only an advisory to power controller and is supported
1499cf7f355SMadhukar Pappireddy 		 * by SoCs with DynamIQ Shared Units only.
1509cf7f355SMadhukar Pappireddy 		 */
1519cf7f355SMadhukar Pappireddy 		reg = read_clusterpwrdn();
1529cf7f355SMadhukar Pappireddy 
1539cf7f355SMadhukar Pappireddy 		/* Clear and set bit 0 : Cluster power not required */
1549cf7f355SMadhukar Pappireddy 		reg &= ~DSU_CLUSTER_PWR_MASK;
1559cf7f355SMadhukar Pappireddy 		reg |= DSU_CLUSTER_PWR_OFF;
1569cf7f355SMadhukar Pappireddy 		write_clusterpwrdn(reg);
1579cf7f355SMadhukar Pappireddy #endif
1589cf7f355SMadhukar Pappireddy 	}
159b4315306SDan Handley }
160b4315306SDan Handley 
161b4315306SDan Handley /*******************************************************************************
16238dce70fSSoby Mathew  * Handler called when a power domain is about to be turned off. The
16338dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
164b4315306SDan Handley  ******************************************************************************/
165785fb92bSSoby Mathew void css_pwr_domain_off(const psci_power_state_t *target_state)
166b4315306SDan Handley {
167f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
16838dce70fSSoby Mathew 	css_power_down_common(target_state);
169b12a2b49SSoby Mathew 	css_scp_off(target_state);
170b4315306SDan Handley }
171b4315306SDan Handley 
172b4315306SDan Handley /*******************************************************************************
17338dce70fSSoby Mathew  * Handler called when a power domain is about to be suspended. The
17438dce70fSSoby Mathew  * target_state encodes the power state that each level should transition to.
175b4315306SDan Handley  ******************************************************************************/
176785fb92bSSoby Mathew void css_pwr_domain_suspend(const psci_power_state_t *target_state)
177b4315306SDan Handley {
17838dce70fSSoby Mathew 	/*
179f14d1886SSoby Mathew 	 * CSS currently supports retention only at cpu level. Just return
18038dce70fSSoby Mathew 	 * as nothing is to be done for retention.
18138dce70fSSoby Mathew 	 */
182f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
183b4315306SDan Handley 		return;
184b4315306SDan Handley 
185e35a3fb5SSoby Mathew 
186f14d1886SSoby Mathew 	assert(CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF);
18738dce70fSSoby Mathew 	css_power_down_common(target_state);
188e35a3fb5SSoby Mathew 
189e35a3fb5SSoby Mathew 	/* Perform system domain state saving if issuing system suspend */
1909b4c611cSNariman Poushin 	if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) {
191e35a3fb5SSoby Mathew 		arm_system_pwr_domain_save();
192e35a3fb5SSoby Mathew 
193e35a3fb5SSoby Mathew 		/* Power off the Redistributor after having saved its context */
194e35a3fb5SSoby Mathew 		plat_arm_gic_redistif_off();
195e35a3fb5SSoby Mathew 	}
196e35a3fb5SSoby Mathew 
197b12a2b49SSoby Mathew 	css_scp_suspend(target_state);
198b4315306SDan Handley }
199b4315306SDan Handley 
200b4315306SDan Handley /*******************************************************************************
20138dce70fSSoby Mathew  * Handler called when a power domain has just been powered on after
20238dce70fSSoby Mathew  * having been suspended earlier. The target_state encodes the low power state
20338dce70fSSoby Mathew  * that each level has woken up from.
204b4315306SDan Handley  * TODO: At the moment we reuse the on finisher and reinitialize the secure
205b4315306SDan Handley  * context. Need to implement a separate suspend finisher.
206b4315306SDan Handley  ******************************************************************************/
207785fb92bSSoby Mathew void css_pwr_domain_suspend_finish(
20838dce70fSSoby Mathew 				const psci_power_state_t *target_state)
209b4315306SDan Handley {
210f14d1886SSoby Mathew 	/* Return as nothing is to be done on waking up from retention. */
211f14d1886SSoby Mathew 	if (CSS_CORE_PWR_STATE(target_state) == ARM_LOCAL_STATE_RET)
21238dce70fSSoby Mathew 		return;
21338dce70fSSoby Mathew 
214f14d1886SSoby Mathew 	/* Perform system domain restore if woken up from system suspend */
2159b4c611cSNariman Poushin 	if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF)
216e35a3fb5SSoby Mathew 		/*
217e35a3fb5SSoby Mathew 		 * At this point, the Distributor must be powered on to be ready
218e35a3fb5SSoby Mathew 		 * to have its state restored. The Redistributor will be powered
219e35a3fb5SSoby Mathew 		 * on as part of gicv3_rdistif_init_restore.
220e35a3fb5SSoby Mathew 		 */
221f14d1886SSoby Mathew 		arm_system_pwr_domain_resume();
222f14d1886SSoby Mathew 
223f14d1886SSoby Mathew 	css_pwr_domain_on_finisher_common(target_state);
2246806cd23SMadhukar Pappireddy 
2256806cd23SMadhukar Pappireddy 	/* Enable the gic cpu interface */
2266806cd23SMadhukar Pappireddy 	plat_arm_gic_cpuif_enable();
227b4315306SDan Handley }
228b4315306SDan Handley 
229b4315306SDan Handley /*******************************************************************************
230b4315306SDan Handley  * Handlers to shutdown/reboot the system
231b4315306SDan Handley  ******************************************************************************/
232785fb92bSSoby Mathew void __dead2 css_system_off(void)
233b4315306SDan Handley {
234b12a2b49SSoby Mathew 	css_scp_sys_shutdown();
235b4315306SDan Handley }
236b4315306SDan Handley 
237785fb92bSSoby Mathew void __dead2 css_system_reset(void)
238b4315306SDan Handley {
239b12a2b49SSoby Mathew 	css_scp_sys_reboot();
240b4315306SDan Handley }
241b4315306SDan Handley 
242b4315306SDan Handley /*******************************************************************************
24338dce70fSSoby Mathew  * Handler called when the CPU power domain is about to enter standby.
244b4315306SDan Handley  ******************************************************************************/
24538dce70fSSoby Mathew void css_cpu_standby(plat_local_state_t cpu_state)
246b4315306SDan Handley {
247b4315306SDan Handley 	unsigned int scr;
248b4315306SDan Handley 
24938dce70fSSoby Mathew 	assert(cpu_state == ARM_LOCAL_STATE_RET);
25038dce70fSSoby Mathew 
251b4315306SDan Handley 	scr = read_scr_el3();
25268b105aeSDavid Wang 	/*
25368b105aeSDavid Wang 	 * Enable the Non secure interrupt to wake the CPU.
25468b105aeSDavid Wang 	 * In GICv3 affinity routing mode, the non secure group1 interrupts use
25568b105aeSDavid Wang 	 * the PhysicalFIQ at EL3 whereas in GICv2, it uses the PhysicalIRQ.
25668b105aeSDavid Wang 	 * Enabling both the bits works for both GICv2 mode and GICv3 affinity
25768b105aeSDavid Wang 	 * routing mode.
25868b105aeSDavid Wang 	 */
25968b105aeSDavid Wang 	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
260b4315306SDan Handley 	isb();
261b4315306SDan Handley 	dsb();
262b4315306SDan Handley 	wfi();
263b4315306SDan Handley 
264b4315306SDan Handley 	/*
265b4315306SDan Handley 	 * Restore SCR to the original value, synchronisation of scr_el3 is
266b4315306SDan Handley 	 * done by eret while el3_exit to save some execution cycles.
267b4315306SDan Handley 	 */
268b4315306SDan Handley 	write_scr_el3(scr);
269b4315306SDan Handley }
270b4315306SDan Handley 
271b4315306SDan Handley /*******************************************************************************
272c1bb8a05SSoby Mathew  * Handler called to return the 'req_state' for system suspend.
273c1bb8a05SSoby Mathew  ******************************************************************************/
274c1bb8a05SSoby Mathew void css_get_sys_suspend_power_state(psci_power_state_t *req_state)
275c1bb8a05SSoby Mathew {
276c1bb8a05SSoby Mathew 	unsigned int i;
277c1bb8a05SSoby Mathew 
278c1bb8a05SSoby Mathew 	/*
279c1bb8a05SSoby Mathew 	 * System Suspend is supported only if the system power domain node
280c1bb8a05SSoby Mathew 	 * is implemented.
281c1bb8a05SSoby Mathew 	 */
282abd2aba9SSoby Mathew 	assert(PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL);
283c1bb8a05SSoby Mathew 
284c1bb8a05SSoby Mathew 	for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
285c1bb8a05SSoby Mathew 		req_state->pwr_domain_state[i] = ARM_LOCAL_STATE_OFF;
286c1bb8a05SSoby Mathew }
287c1bb8a05SSoby Mathew 
288c1bb8a05SSoby Mathew /*******************************************************************************
2893cc17aaeSJeenu Viswambharan  * Handler to query CPU/cluster power states from SCP
2903cc17aaeSJeenu Viswambharan  ******************************************************************************/
2913cc17aaeSJeenu Viswambharan int css_node_hw_state(u_register_t mpidr, unsigned int power_level)
2923cc17aaeSJeenu Viswambharan {
293b12a2b49SSoby Mathew 	return css_scp_get_power_state(mpidr, power_level);
2943cc17aaeSJeenu Viswambharan }
2953cc17aaeSJeenu Viswambharan 
296abd2aba9SSoby Mathew /*
297abd2aba9SSoby Mathew  * The system power domain suspend is only supported only via
298abd2aba9SSoby Mathew  * PSCI SYSTEM_SUSPEND API. PSCI CPU_SUSPEND request to system power domain
299abd2aba9SSoby Mathew  * will be downgraded to the lower level.
300abd2aba9SSoby Mathew  */
301abd2aba9SSoby Mathew static int css_validate_power_state(unsigned int power_state,
302abd2aba9SSoby Mathew 			    psci_power_state_t *req_state)
303abd2aba9SSoby Mathew {
304abd2aba9SSoby Mathew 	int rc;
305abd2aba9SSoby Mathew 	rc = arm_validate_power_state(power_state, req_state);
306abd2aba9SSoby Mathew 
307abd2aba9SSoby Mathew 	/*
3088e26307dSNariman Poushin 	 * Ensure that we don't overrun the pwr_domain_state array in the case
3098e26307dSNariman Poushin 	 * where the platform supported max power level is less than the system
3108e26307dSNariman Poushin 	 * power level
3118e26307dSNariman Poushin 	 */
3128e26307dSNariman Poushin 
3138e26307dSNariman Poushin #if (PLAT_MAX_PWR_LVL == CSS_SYSTEM_PWR_DMN_LVL)
3148e26307dSNariman Poushin 
3158e26307dSNariman Poushin 	/*
316abd2aba9SSoby Mathew 	 * Ensure that the system power domain level is never suspended
317abd2aba9SSoby Mathew 	 * via PSCI CPU SUSPEND API. Currently system suspend is only
318abd2aba9SSoby Mathew 	 * supported via PSCI SYSTEM SUSPEND API.
319abd2aba9SSoby Mathew 	 */
3208e26307dSNariman Poushin 
3218e26307dSNariman Poushin 	req_state->pwr_domain_state[CSS_SYSTEM_PWR_DMN_LVL] =
3228e26307dSNariman Poushin 							ARM_LOCAL_STATE_RUN;
3238e26307dSNariman Poushin #endif
3248e26307dSNariman Poushin 
325abd2aba9SSoby Mathew 	return rc;
326abd2aba9SSoby Mathew }
327abd2aba9SSoby Mathew 
328abd2aba9SSoby Mathew /*
329abd2aba9SSoby Mathew  * Custom `translate_power_state_by_mpidr` handler for CSS. Unlike in the
330abd2aba9SSoby Mathew  * `css_validate_power_state`, we do not downgrade the system power
331abd2aba9SSoby Mathew  * domain level request in `power_state` as it will be used to query the
332abd2aba9SSoby Mathew  * PSCI_STAT_COUNT/RESIDENCY at the system power domain level.
333abd2aba9SSoby Mathew  */
334abd2aba9SSoby Mathew static int css_translate_power_state_by_mpidr(u_register_t mpidr,
335abd2aba9SSoby Mathew 		unsigned int power_state,
336abd2aba9SSoby Mathew 		psci_power_state_t *output_state)
337abd2aba9SSoby Mathew {
338abd2aba9SSoby Mathew 	return arm_validate_power_state(power_state, output_state);
339abd2aba9SSoby Mathew }
340abd2aba9SSoby Mathew 
341158ed580SPranav Madhu /*
342158ed580SPranav Madhu  * Setup the SGI interrupt that will be used trigger the execution of power
343158ed580SPranav Madhu  * down sequence for all the secondary cores. This interrupt is setup to be
344158ed580SPranav Madhu  * handled in EL3 context at a priority defined by the platform.
345158ed580SPranav Madhu  */
346158ed580SPranav Madhu void css_setup_cpu_pwr_down_intr(void)
347158ed580SPranav Madhu {
348158ed580SPranav Madhu #if CSS_SYSTEM_GRACEFUL_RESET
349158ed580SPranav Madhu 	plat_ic_set_interrupt_type(CSS_CPU_PWR_DOWN_REQ_INTR, INTR_TYPE_EL3);
350158ed580SPranav Madhu 	plat_ic_set_interrupt_priority(CSS_CPU_PWR_DOWN_REQ_INTR,
351158ed580SPranav Madhu 			PLAT_REBOOT_PRI);
352158ed580SPranav Madhu 	plat_ic_enable_interrupt(CSS_CPU_PWR_DOWN_REQ_INTR);
353158ed580SPranav Madhu #endif
354158ed580SPranav Madhu }
355158ed580SPranav Madhu 
356*f1fe1440SPranav Madhu /*
357*f1fe1440SPranav Madhu  * For a graceful shutdown/reboot, each CPU in the system should do their power
358*f1fe1440SPranav Madhu  * down sequence. On a PSCI shutdown/reboot request, only one CPU gets an
359*f1fe1440SPranav Madhu  * opportunity to do the powerdown sequence. To achieve graceful reset, of all
360*f1fe1440SPranav Madhu  * cores in the system, the CPU gets the opportunity raise warm reboot SGI to
361*f1fe1440SPranav Madhu  * rest of the CPUs which are online. Add handler for the reboot SGI where the
362*f1fe1440SPranav Madhu  * rest of the CPU execute the powerdown sequence.
363*f1fe1440SPranav Madhu  */
364*f1fe1440SPranav Madhu int css_reboot_interrupt_handler(uint32_t intr_raw, uint32_t flags,
365*f1fe1440SPranav Madhu 		void *handle, void *cookie)
366*f1fe1440SPranav Madhu {
367*f1fe1440SPranav Madhu 	assert(intr_raw == CSS_CPU_PWR_DOWN_REQ_INTR);
368*f1fe1440SPranav Madhu 
369*f1fe1440SPranav Madhu 	/* Deactivate warm reboot SGI */
370*f1fe1440SPranav Madhu 	plat_ic_end_of_interrupt(CSS_CPU_PWR_DOWN_REQ_INTR);
371*f1fe1440SPranav Madhu 
372*f1fe1440SPranav Madhu 	/*
373*f1fe1440SPranav Madhu 	 * Disable GIC CPU interface to prevent pending interrupt from waking
374*f1fe1440SPranav Madhu 	 * up the AP from WFI.
375*f1fe1440SPranav Madhu 	 */
376*f1fe1440SPranav Madhu 	plat_arm_gic_cpuif_disable();
377*f1fe1440SPranav Madhu 	plat_arm_gic_redistif_off();
378*f1fe1440SPranav Madhu 
379*f1fe1440SPranav Madhu 	psci_pwrdown_cpu(PLAT_MAX_PWR_LVL);
380*f1fe1440SPranav Madhu 
381*f1fe1440SPranav Madhu 	dmbsy();
382*f1fe1440SPranav Madhu 
383*f1fe1440SPranav Madhu 	wfi();
384*f1fe1440SPranav Madhu 	return 0;
385*f1fe1440SPranav Madhu }
386*f1fe1440SPranav Madhu 
3873cc17aaeSJeenu Viswambharan /*******************************************************************************
388785fb92bSSoby Mathew  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
389785fb92bSSoby Mathew  * platform will take care of registering the handlers with PSCI.
390b4315306SDan Handley  ******************************************************************************/
3915486a965SSoby Mathew plat_psci_ops_t plat_arm_psci_pm_ops = {
39238dce70fSSoby Mathew 	.pwr_domain_on		= css_pwr_domain_on,
39338dce70fSSoby Mathew 	.pwr_domain_on_finish	= css_pwr_domain_on_finish,
3946806cd23SMadhukar Pappireddy 	.pwr_domain_on_finish_late = css_pwr_domain_on_finish_late,
39538dce70fSSoby Mathew 	.pwr_domain_off		= css_pwr_domain_off,
39638dce70fSSoby Mathew 	.cpu_standby		= css_cpu_standby,
39738dce70fSSoby Mathew 	.pwr_domain_suspend	= css_pwr_domain_suspend,
39838dce70fSSoby Mathew 	.pwr_domain_suspend_finish	= css_pwr_domain_suspend_finish,
399b4315306SDan Handley 	.system_off		= css_system_off,
400b4315306SDan Handley 	.system_reset		= css_system_reset,
401abd2aba9SSoby Mathew 	.validate_power_state	= css_validate_power_state,
40271e7a4e5SJeenu Viswambharan 	.validate_ns_entrypoint = arm_validate_psci_entrypoint,
403abd2aba9SSoby Mathew 	.translate_power_state_by_mpidr = css_translate_power_state_by_mpidr,
404abd2aba9SSoby Mathew 	.get_node_hw_state	= css_node_hw_state,
405f145403cSRoberto Vargas 	.get_sys_suspend_power_state = css_get_sys_suspend_power_state,
406638b034cSRoberto Vargas 
407638b034cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR)
408f145403cSRoberto Vargas 	.mem_protect_chk	= arm_psci_mem_protect_chk,
409f145403cSRoberto Vargas 	.read_mem_protect	= arm_psci_read_mem_protect,
410f145403cSRoberto Vargas 	.write_mem_protect	= arm_nor_psci_write_mem_protect,
411f145403cSRoberto Vargas #endif
412b48ae263SRoberto Vargas #if CSS_USE_SCMI_SDS_DRIVER
413b48ae263SRoberto Vargas 	.system_reset2		= css_system_reset2,
414b48ae263SRoberto Vargas #endif
415b4315306SDan Handley };
416