xref: /rk3399_ARM-atf/lib/psci/psci_suspend.c (revision 83ec7e452c182af1148af0abaf8f24da2585102a)
1532ed618SSoby Mathew /*
244ee7714SBoyan Karatotev  * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
709d40e0eSAntonio Nino Diaz #include <assert.h>
809d40e0eSAntonio Nino Diaz #include <stddef.h>
909d40e0eSAntonio Nino Diaz 
10532ed618SSoby Mathew #include <arch.h>
11532ed618SSoby Mathew #include <arch_helpers.h>
1209d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
14532ed618SSoby Mathew #include <context.h>
1509d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
1609d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h>
1709d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/pubsub_events.h>
1809d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h>
1909d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h>
2009d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2109d40e0eSAntonio Nino Diaz 
22532ed618SSoby Mathew #include "psci_private.h"
23532ed618SSoby Mathew 
24532ed618SSoby Mathew /*******************************************************************************
25532ed618SSoby Mathew  * This function does generic and platform specific operations after a wake-up
26532ed618SSoby Mathew  * from standby/retention states at multiple power levels.
27532ed618SSoby Mathew  ******************************************************************************/
282b5e00d4SBoyan Karatotev static void psci_cpu_suspend_to_standby_finish(unsigned int end_pwrlvl,
2944ee7714SBoyan Karatotev 					     psci_power_state_t *state_info)
30532ed618SSoby Mathew {
3161eae524SAchin Gupta 	/*
32532ed618SSoby Mathew 	 * Plat. management: Allow the platform to do operations
33532ed618SSoby Mathew 	 * on waking up from retention.
34532ed618SSoby Mathew 	 */
3544ee7714SBoyan Karatotev 	psci_plat_pm_ops->pwr_domain_suspend_finish(state_info);
36532ed618SSoby Mathew 
370c836554SBoyan Karatotev 	/* This loses its meaning when not suspending, reset so it's correct for OFF */
380c836554SBoyan Karatotev 	psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL);
39532ed618SSoby Mathew }
40532ed618SSoby Mathew 
41532ed618SSoby Mathew /*******************************************************************************
42532ed618SSoby Mathew  * This function does generic and platform specific suspend to power down
43532ed618SSoby Mathew  * operations.
44532ed618SSoby Mathew  ******************************************************************************/
45*83ec7e45SBoyan Karatotev static void psci_suspend_to_pwrdown_start(unsigned int idx,
46*83ec7e45SBoyan Karatotev 					  unsigned int end_pwrlvl,
472b5e00d4SBoyan Karatotev 					  unsigned int max_off_lvl,
48621d64f8SAntonio Nino Diaz 					  const entry_point_info_t *ep,
49621d64f8SAntonio Nino Diaz 					  const psci_power_state_t *state_info)
50532ed618SSoby Mathew {
51*83ec7e45SBoyan Karatotev 	PUBLISH_EVENT_ARG(psci_suspend_pwrdown_start, &idx);
527593252cSDimitris Papastamos 
53606b7430SWing Li #if PSCI_OS_INIT_MODE
54606b7430SWing Li #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
55606b7430SWing Li 	end_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
56606b7430SWing Li #else
57606b7430SWing Li 	end_pwrlvl = PLAT_MAX_PWR_LVL;
58606b7430SWing Li #endif
59606b7430SWing Li #endif
60606b7430SWing Li 
61532ed618SSoby Mathew 	/* Save PSCI target power level for the suspend finisher handler */
62532ed618SSoby Mathew 	psci_set_suspend_pwrlvl(end_pwrlvl);
63532ed618SSoby Mathew 
64532ed618SSoby Mathew 	/*
65a10d3632SJeenu Viswambharan 	 * Flush the target power level as it might be accessed on power up with
66532ed618SSoby Mathew 	 * Data cache disabled.
67532ed618SSoby Mathew 	 */
68a10d3632SJeenu Viswambharan 	psci_flush_cpu_data(psci_svc_cpu_data.target_pwrlvl);
69532ed618SSoby Mathew 
70532ed618SSoby Mathew 	/*
71532ed618SSoby Mathew 	 * Call the cpu suspend handler registered by the Secure Payload
72532ed618SSoby Mathew 	 * Dispatcher to let it do any book-keeping. If the handler encounters an
73532ed618SSoby Mathew 	 * error, it's expected to assert within
74532ed618SSoby Mathew 	 */
75621d64f8SAntonio Nino Diaz 	if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend != NULL))
76532ed618SSoby Mathew 		psci_spd_pm->svc_suspend(max_off_lvl);
77532ed618SSoby Mathew 
781862d620SVarun Wadekar #if !HW_ASSISTED_COHERENCY
791862d620SVarun Wadekar 	/*
801862d620SVarun Wadekar 	 * Plat. management: Allow the platform to perform any early
811862d620SVarun Wadekar 	 * actions required to power down the CPU. This might be useful for
821862d620SVarun Wadekar 	 * HW_ASSISTED_COHERENCY = 0 platforms that can safely perform these
831862d620SVarun Wadekar 	 * actions with data caches enabled.
841862d620SVarun Wadekar 	 */
85621d64f8SAntonio Nino Diaz 	if (psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early != NULL)
861862d620SVarun Wadekar 		psci_plat_pm_ops->pwr_domain_suspend_pwrdown_early(state_info);
871862d620SVarun Wadekar #endif
881862d620SVarun Wadekar 
89532ed618SSoby Mathew 	/*
90532ed618SSoby Mathew 	 * Store the re-entry information for the non-secure world.
91532ed618SSoby Mathew 	 */
92532ed618SSoby Mathew 	cm_init_my_context(ep);
93532ed618SSoby Mathew 
94532ed618SSoby Mathew 	/*
95b0408e87SJeenu Viswambharan 	 * Arch. management. Initiate power down sequence.
96532ed618SSoby Mathew 	 */
972b5e00d4SBoyan Karatotev 	psci_pwrdown_cpu_start(max_off_lvl);
98532ed618SSoby Mathew }
99532ed618SSoby Mathew 
100532ed618SSoby Mathew /*******************************************************************************
101532ed618SSoby Mathew  * Top level handler which is called when a cpu wants to suspend its execution.
102532ed618SSoby Mathew  * It is assumed that along with suspending the cpu power domain, power domains
103532ed618SSoby Mathew  * at higher levels until the target power level will be suspended as well. It
104532ed618SSoby Mathew  * coordinates with the platform to negotiate the target state for each of
105532ed618SSoby Mathew  * the power domain level till the target power domain level. It then performs
106532ed618SSoby Mathew  * generic, architectural, platform setup and state management required to
107532ed618SSoby Mathew  * suspend that power domain level and power domain levels below it.
108532ed618SSoby Mathew  * e.g. For a cpu that's to be suspended, it could mean programming the
109532ed618SSoby Mathew  * power controller whereas for a cluster that's to be suspended, it will call
110532ed618SSoby Mathew  * the platform specific code which will disable coherency at the interconnect
111532ed618SSoby Mathew  * level if the cpu is the last in the cluster and also the program the power
112532ed618SSoby Mathew  * controller.
113532ed618SSoby Mathew  *
114532ed618SSoby Mathew  * All the required parameter checks are performed at the beginning and after
115532ed618SSoby Mathew  * the state transition has been done, no further error is expected and it is
116532ed618SSoby Mathew  * not possible to undo any of the actions taken beyond that point.
117532ed618SSoby Mathew  ******************************************************************************/
1183b802105SBoyan Karatotev int psci_cpu_suspend_start(unsigned int idx,
1193b802105SBoyan Karatotev 			   const entry_point_info_t *ep,
120532ed618SSoby Mathew 			   unsigned int end_pwrlvl,
121532ed618SSoby Mathew 			   psci_power_state_t *state_info,
122532ed618SSoby Mathew 			   unsigned int is_power_down_state)
123532ed618SSoby Mathew {
124606b7430SWing Li 	int rc = PSCI_E_SUCCESS;
12574d27d00SAndrew F. Davis 	unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1262b5e00d4SBoyan Karatotev 	unsigned int max_off_lvl = 0;
1272b5e00d4SBoyan Karatotev #if FEAT_PABANDON
1282b5e00d4SBoyan Karatotev 	cpu_context_t *ctx = cm_get_context(NON_SECURE);
1292b5e00d4SBoyan Karatotev 	cpu_context_t old_ctx;
1302b5e00d4SBoyan Karatotev #endif
131532ed618SSoby Mathew 
132532ed618SSoby Mathew 	/*
133532ed618SSoby Mathew 	 * This function must only be called on platforms where the
134532ed618SSoby Mathew 	 * CPU_SUSPEND platform hooks have been implemented.
135532ed618SSoby Mathew 	 */
136621d64f8SAntonio Nino Diaz 	assert((psci_plat_pm_ops->pwr_domain_suspend != NULL) &&
137621d64f8SAntonio Nino Diaz 	       (psci_plat_pm_ops->pwr_domain_suspend_finish != NULL));
138532ed618SSoby Mathew 
13974d27d00SAndrew F. Davis 	/* Get the parent nodes */
14074d27d00SAndrew F. Davis 	psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes);
14174d27d00SAndrew F. Davis 
142532ed618SSoby Mathew 	/*
143532ed618SSoby Mathew 	 * This function acquires the lock corresponding to each power
144532ed618SSoby Mathew 	 * level so that by the time all locks are taken, the system topology
145532ed618SSoby Mathew 	 * is snapshot and state management can be done safely.
146532ed618SSoby Mathew 	 */
14774d27d00SAndrew F. Davis 	psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
148532ed618SSoby Mathew 
149532ed618SSoby Mathew 	/*
150532ed618SSoby Mathew 	 * We check if there are any pending interrupts after the delay
151532ed618SSoby Mathew 	 * introduced by lock contention to increase the chances of early
152532ed618SSoby Mathew 	 * detection that a wake-up interrupt has fired.
153532ed618SSoby Mathew 	 */
154621d64f8SAntonio Nino Diaz 	if (read_isr_el1() != 0U) {
155532ed618SSoby Mathew 		goto exit;
156532ed618SSoby Mathew 	}
157532ed618SSoby Mathew 
158606b7430SWing Li #if PSCI_OS_INIT_MODE
159606b7430SWing Li 	if (psci_suspend_mode == OS_INIT) {
160606b7430SWing Li 		/*
161606b7430SWing Li 		 * This function validates the requested state info for
162606b7430SWing Li 		 * OS-initiated mode.
163606b7430SWing Li 		 */
1643b802105SBoyan Karatotev 		rc = psci_validate_state_coordination(idx, end_pwrlvl, state_info);
165606b7430SWing Li 		if (rc != PSCI_E_SUCCESS) {
166606b7430SWing Li 			goto exit;
167606b7430SWing Li 		}
168606b7430SWing Li 	} else {
169606b7430SWing Li #endif
170532ed618SSoby Mathew 		/*
171532ed618SSoby Mathew 		 * This function is passed the requested state info and
172532ed618SSoby Mathew 		 * it returns the negotiated state info for each power level upto
173532ed618SSoby Mathew 		 * the end level specified.
174532ed618SSoby Mathew 		 */
1753b802105SBoyan Karatotev 		psci_do_state_coordination(idx, end_pwrlvl, state_info);
176606b7430SWing Li #if PSCI_OS_INIT_MODE
177606b7430SWing Li 	}
178606b7430SWing Li #endif
179532ed618SSoby Mathew 
180d3488614SWing Li #if PSCI_OS_INIT_MODE
181d3488614SWing Li 	if (psci_plat_pm_ops->pwr_domain_validate_suspend != NULL) {
182d3488614SWing Li 		rc = psci_plat_pm_ops->pwr_domain_validate_suspend(state_info);
183d3488614SWing Li 		if (rc != PSCI_E_SUCCESS) {
184d3488614SWing Li 			goto exit;
185d3488614SWing Li 		}
186d3488614SWing Li 	}
187d3488614SWing Li #endif
188d3488614SWing Li 
189d3488614SWing Li 	/* Update the target state in the power domain nodes */
1903b802105SBoyan Karatotev 	psci_set_target_local_pwr_states(idx, end_pwrlvl, state_info);
191d3488614SWing Li 
192532ed618SSoby Mathew #if ENABLE_PSCI_STAT
193532ed618SSoby Mathew 	/* Update the last cpu for each level till end_pwrlvl */
1943b802105SBoyan Karatotev 	psci_stats_update_pwr_down(idx, end_pwrlvl, state_info);
195532ed618SSoby Mathew #endif
196532ed618SSoby Mathew 
1972b5e00d4SBoyan Karatotev 	if (is_power_down_state != 0U) {
1982b5e00d4SBoyan Karatotev 		/*
1992b5e00d4SBoyan Karatotev 		 * WHen CTX_INCLUDE_EL2_REGS is usnet, we're probably runnig
2002b5e00d4SBoyan Karatotev 		 * with some SPD that assumes the core is going off so it
2012b5e00d4SBoyan Karatotev 		 * doesn't bother saving NS's context. Do that here until we
2022b5e00d4SBoyan Karatotev 		 * figure out a way to make this coherent.
2032b5e00d4SBoyan Karatotev 		 */
2042b5e00d4SBoyan Karatotev #if FEAT_PABANDON
2052b5e00d4SBoyan Karatotev #if !CTX_INCLUDE_EL2_REGS
2062b5e00d4SBoyan Karatotev 		cm_el1_sysregs_context_save(NON_SECURE);
2072b5e00d4SBoyan Karatotev #endif
2082b5e00d4SBoyan Karatotev 		/*
2092b5e00d4SBoyan Karatotev 		 * when the core wakes it expects its context to already be in
2102b5e00d4SBoyan Karatotev 		 * place so we must overwrite it before powerdown. But if
2112b5e00d4SBoyan Karatotev 		 * powerdown never happens we want the old context. Save it in
2122b5e00d4SBoyan Karatotev 		 * case we wake up. EL2/El1 will not be touched by PSCI so don't
2132b5e00d4SBoyan Karatotev 		 * copy */
2142b5e00d4SBoyan Karatotev 		memcpy(&ctx->gpregs_ctx, &old_ctx.gpregs_ctx, sizeof(gp_regs_t));
2152b5e00d4SBoyan Karatotev 		memcpy(&ctx->el3state_ctx, &old_ctx.el3state_ctx, sizeof(el3_state_t));
2162b5e00d4SBoyan Karatotev #if DYNAMIC_WORKAROUND_CVE_2018_3639
2172b5e00d4SBoyan Karatotev 		memcpy(&ctx->cve_2018_3639_ctx, &old_ctx.cve_2018_3639_ctx, sizeof(cve_2018_3639_t));
2182b5e00d4SBoyan Karatotev #endif
2192b5e00d4SBoyan Karatotev #if ERRATA_SPECULATIVE_AT
2202b5e00d4SBoyan Karatotev 		memcpy(&ctx->errata_speculative_at_ctx, &old_ctx.errata_speculative_at_ctx, sizeof(errata_speculative_at_t));
2212b5e00d4SBoyan Karatotev #endif
2222b5e00d4SBoyan Karatotev #if CTX_INCLUDE_PAUTH_REGS
2232b5e00d4SBoyan Karatotev 		memcpy(&ctx->pauth_ctx, &old_ctx.pauth_ctx, sizeof(pauth_t));
2242b5e00d4SBoyan Karatotev #endif
2252b5e00d4SBoyan Karatotev #endif
2262b5e00d4SBoyan Karatotev 		max_off_lvl = psci_find_max_off_lvl(state_info);
227*83ec7e45SBoyan Karatotev 		psci_suspend_to_pwrdown_start(idx, end_pwrlvl, end_pwrlvl, ep, state_info);
2282b5e00d4SBoyan Karatotev 	}
229532ed618SSoby Mathew 
230532ed618SSoby Mathew 	/*
231532ed618SSoby Mathew 	 * Plat. management: Allow the platform to perform the
232532ed618SSoby Mathew 	 * necessary actions to turn off this cpu e.g. set the
233532ed618SSoby Mathew 	 * platform defined mailbox with the psci entrypoint,
234532ed618SSoby Mathew 	 * program the power controller etc.
235532ed618SSoby Mathew 	 */
236606b7430SWing Li 
237532ed618SSoby Mathew 	psci_plat_pm_ops->pwr_domain_suspend(state_info);
238532ed618SSoby Mathew 
239532ed618SSoby Mathew #if ENABLE_PSCI_STAT
24004c1db1eSdp-arm 	plat_psci_stat_accounting_start(state_info);
241532ed618SSoby Mathew #endif
242532ed618SSoby Mathew 
243532ed618SSoby Mathew 	/*
244532ed618SSoby Mathew 	 * Release the locks corresponding to each power level in the
245532ed618SSoby Mathew 	 * reverse order to which they were acquired.
246532ed618SSoby Mathew 	 */
24774d27d00SAndrew F. Davis 	psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
24874d27d00SAndrew F. Davis 
249872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
250872be88aSdp-arm 	/*
251872be88aSdp-arm 	 * Update the timestamp with cache off. We assume this
252872be88aSdp-arm 	 * timestamp can only be read from the current CPU and the
253872be88aSdp-arm 	 * timestamp cache line will be flushed before return to
254872be88aSdp-arm 	 * normal world on wakeup.
255872be88aSdp-arm 	 */
256872be88aSdp-arm 	PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
257872be88aSdp-arm 	    RT_INSTR_ENTER_HW_LOW_PWR,
258872be88aSdp-arm 	    PMF_NO_CACHE_MAINT);
259872be88aSdp-arm #endif
260872be88aSdp-arm 
2612b5e00d4SBoyan Karatotev 	if (is_power_down_state != 0U) {
262db5fe4f4SBoyan Karatotev 		if (psci_plat_pm_ops->pwr_domain_pwr_down != NULL) {
2632b5e00d4SBoyan Karatotev 			/* This function may not return */
264db5fe4f4SBoyan Karatotev 			psci_plat_pm_ops->pwr_domain_pwr_down(state_info);
265532ed618SSoby Mathew 		}
266532ed618SSoby Mathew 
2672b5e00d4SBoyan Karatotev 		psci_pwrdown_cpu_end_wakeup(max_off_lvl);
2682b5e00d4SBoyan Karatotev 	} else {
269532ed618SSoby Mathew 		/*
270532ed618SSoby Mathew 		 * We will reach here if only retention/standby states have been
271532ed618SSoby Mathew 		 * requested at multiple power levels. This means that the cpu
272532ed618SSoby Mathew 		 * context will be preserved.
273532ed618SSoby Mathew 		 */
274532ed618SSoby Mathew 		wfi();
2752b5e00d4SBoyan Karatotev 	}
276532ed618SSoby Mathew 
277872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
278872be88aSdp-arm 	PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
279872be88aSdp-arm 	    RT_INSTR_EXIT_HW_LOW_PWR,
280872be88aSdp-arm 	    PMF_NO_CACHE_MAINT);
281872be88aSdp-arm #endif
282872be88aSdp-arm 
28344ee7714SBoyan Karatotev 	psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
28444ee7714SBoyan Karatotev 	/*
28544ee7714SBoyan Karatotev 	 * Find out which retention states this CPU has exited from until the
28644ee7714SBoyan Karatotev 	 * 'end_pwrlvl'. The exit retention state could be deeper than the entry
28744ee7714SBoyan Karatotev 	 * state as a result of state coordination amongst other CPUs post wfi.
28844ee7714SBoyan Karatotev 	 */
2893b802105SBoyan Karatotev 	psci_get_target_local_pwr_states(idx, end_pwrlvl, state_info);
29044ee7714SBoyan Karatotev 
29144ee7714SBoyan Karatotev #if ENABLE_PSCI_STAT
29244ee7714SBoyan Karatotev 	plat_psci_stat_accounting_stop(state_info);
2933b802105SBoyan Karatotev 	psci_stats_update_pwr_up(idx, end_pwrlvl, state_info);
29444ee7714SBoyan Karatotev #endif
29544ee7714SBoyan Karatotev 
296532ed618SSoby Mathew 	/*
2972b5e00d4SBoyan Karatotev 	 * Waking up means we've retained all context. Call the finishers to put
2982b5e00d4SBoyan Karatotev 	 * the system back to a usable state.
299532ed618SSoby Mathew 	 */
3002b5e00d4SBoyan Karatotev 	if (is_power_down_state != 0U) {
3012b5e00d4SBoyan Karatotev #if FEAT_PABANDON
3022b5e00d4SBoyan Karatotev 		psci_cpu_suspend_to_powerdown_finish(idx, max_off_lvl, state_info);
3032b5e00d4SBoyan Karatotev 
3042b5e00d4SBoyan Karatotev 		/* we overwrote context ourselves, put it back */
3052b5e00d4SBoyan Karatotev 		memcpy(&ctx->gpregs_ctx, &old_ctx.gpregs_ctx, sizeof(gp_regs_t));
3062b5e00d4SBoyan Karatotev 		memcpy(&ctx->el3state_ctx, &old_ctx.el3state_ctx, sizeof(el3_state_t));
3072b5e00d4SBoyan Karatotev #if DYNAMIC_WORKAROUND_CVE_2018_3639
3082b5e00d4SBoyan Karatotev 		memcpy(&ctx->cve_2018_3639_ctx, &old_ctx.cve_2018_3639_ctx, sizeof(cve_2018_3639_t));
3092b5e00d4SBoyan Karatotev #endif
3102b5e00d4SBoyan Karatotev #if ERRATA_SPECULATIVE_AT
3112b5e00d4SBoyan Karatotev 		memcpy(&ctx->errata_speculative_at_ctx, &old_ctx.errata_speculative_at_ctx, sizeof(errata_speculative_at_t));
3122b5e00d4SBoyan Karatotev #endif
3132b5e00d4SBoyan Karatotev #if CTX_INCLUDE_PAUTH_REGS
3142b5e00d4SBoyan Karatotev 		memcpy(&ctx->pauth_ctx, &old_ctx.pauth_ctx, sizeof(pauth_t));
3152b5e00d4SBoyan Karatotev #endif
3162b5e00d4SBoyan Karatotev #if !CTX_INCLUDE_EL2_REGS
3172b5e00d4SBoyan Karatotev 		cm_el1_sysregs_context_restore(NON_SECURE);
3182b5e00d4SBoyan Karatotev #endif
3192b5e00d4SBoyan Karatotev #endif
3202b5e00d4SBoyan Karatotev 	} else {
3212b5e00d4SBoyan Karatotev 		psci_cpu_suspend_to_standby_finish(end_pwrlvl, state_info);
3222b5e00d4SBoyan Karatotev 	}
32344ee7714SBoyan Karatotev 
32444ee7714SBoyan Karatotev 	/*
32544ee7714SBoyan Karatotev 	 * Set the requested and target state of this CPU and all the higher
32644ee7714SBoyan Karatotev 	 * power domain levels for this CPU to run.
32744ee7714SBoyan Karatotev 	 */
3283b802105SBoyan Karatotev 	psci_set_pwr_domains_to_run(idx, end_pwrlvl);
32944ee7714SBoyan Karatotev 
330dc0bf486SBoyan Karatotev exit:
33144ee7714SBoyan Karatotev 	psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
332606b7430SWing Li 
333606b7430SWing Li 	return rc;
334532ed618SSoby Mathew }
335532ed618SSoby Mathew 
336532ed618SSoby Mathew /*******************************************************************************
337532ed618SSoby Mathew  * The following functions finish an earlier suspend request. They
338532ed618SSoby Mathew  * are called by the common finisher routine in psci_common.c. The `state_info`
339532ed618SSoby Mathew  * is the psci_power_state from which this CPU has woken up from.
340532ed618SSoby Mathew  ******************************************************************************/
3412b5e00d4SBoyan Karatotev void psci_cpu_suspend_to_powerdown_finish(unsigned int cpu_idx, unsigned int max_off_lvl, const psci_power_state_t *state_info)
342532ed618SSoby Mathew {
343532ed618SSoby Mathew 	unsigned int counter_freq;
344532ed618SSoby Mathew 
345532ed618SSoby Mathew 	/* Ensure we have been woken up from a suspended state */
346621d64f8SAntonio Nino Diaz 	assert((psci_get_aff_info_state() == AFF_STATE_ON) &&
347621d64f8SAntonio Nino Diaz 		(is_local_state_off(
348621d64f8SAntonio Nino Diaz 			state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]) != 0));
349532ed618SSoby Mathew 
350532ed618SSoby Mathew 	/*
351532ed618SSoby Mathew 	 * Plat. management: Perform the platform specific actions
352532ed618SSoby Mathew 	 * before we change the state of the cpu e.g. enabling the
353532ed618SSoby Mathew 	 * gic or zeroing the mailbox register. If anything goes
354532ed618SSoby Mathew 	 * wrong then assert as there is no way to recover from this
355532ed618SSoby Mathew 	 * situation.
356532ed618SSoby Mathew 	 */
357532ed618SSoby Mathew 	psci_plat_pm_ops->pwr_domain_suspend_finish(state_info);
358532ed618SSoby Mathew 
359bcc3c49cSSoby Mathew #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
360b0408e87SJeenu Viswambharan 	/* Arch. management: Enable the data cache, stack memory maintenance. */
361532ed618SSoby Mathew 	psci_do_pwrup_cache_maintenance();
362b0408e87SJeenu Viswambharan #endif
363532ed618SSoby Mathew 
364532ed618SSoby Mathew 	/* Re-init the cntfrq_el0 register */
365532ed618SSoby Mathew 	counter_freq = plat_get_syscnt_freq2();
366532ed618SSoby Mathew 	write_cntfrq_el0(counter_freq);
367532ed618SSoby Mathew 
368ed108b56SAlexei Fedorov #if ENABLE_PAUTH
369ed108b56SAlexei Fedorov 	/* Store APIAKey_EL1 key */
370ed108b56SAlexei Fedorov 	set_cpu_data(apiakey[0], read_apiakeylo_el1());
371ed108b56SAlexei Fedorov 	set_cpu_data(apiakey[1], read_apiakeyhi_el1());
372ed108b56SAlexei Fedorov #endif /* ENABLE_PAUTH */
373ed108b56SAlexei Fedorov 
374532ed618SSoby Mathew 	/*
375532ed618SSoby Mathew 	 * Call the cpu suspend finish handler registered by the Secure Payload
376532ed618SSoby Mathew 	 * Dispatcher to let it do any bookeeping. If the handler encounters an
377532ed618SSoby Mathew 	 * error, it's expected to assert within
378532ed618SSoby Mathew 	 */
379621d64f8SAntonio Nino Diaz 	if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_suspend_finish != NULL)) {
380532ed618SSoby Mathew 		psci_spd_pm->svc_suspend_finish(max_off_lvl);
381532ed618SSoby Mathew 	}
382532ed618SSoby Mathew 
3830c836554SBoyan Karatotev 	/* This loses its meaning when not suspending, reset so it's correct for OFF */
3840c836554SBoyan Karatotev 	psci_set_suspend_pwrlvl(PLAT_MAX_PWR_LVL);
385532ed618SSoby Mathew 
386*83ec7e45SBoyan Karatotev 	PUBLISH_EVENT_ARG(psci_suspend_pwrdown_finish, &cpu_idx);
387532ed618SSoby Mathew }
388