xref: /rk3399_ARM-atf/lib/psci/psci_off.c (revision 7138e6591fc5c4b3d26e92b927bf3ddc95526c6a)
1532ed618SSoby Mathew /*
25d893410SBoyan Karatotev  * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved.
36cf4ae97SVarun Wadekar  * Copyright (c) 2023, NVIDIA Corporation. All rights reserved.
4532ed618SSoby Mathew  *
582cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
6532ed618SSoby Mathew  */
7532ed618SSoby Mathew 
809d40e0eSAntonio Nino Diaz #include <assert.h>
909d40e0eSAntonio Nino Diaz #include <string.h>
1009d40e0eSAntonio Nino Diaz 
11532ed618SSoby Mathew #include <arch.h>
12532ed618SSoby Mathew #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
145d893410SBoyan Karatotev #include <drivers/arm/gic.h>
1509d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h>
1609d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h>
1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1809d40e0eSAntonio Nino Diaz 
19532ed618SSoby Mathew #include "psci_private.h"
20532ed618SSoby Mathew 
21532ed618SSoby Mathew /******************************************************************************
22532ed618SSoby Mathew  * Construct the psci_power_state to request power OFF at all power levels.
23532ed618SSoby Mathew  ******************************************************************************/
psci_set_power_off_state(psci_power_state_t * state_info)24532ed618SSoby Mathew static void psci_set_power_off_state(psci_power_state_t *state_info)
25532ed618SSoby Mathew {
266311f63dSVarun Wadekar 	unsigned int lvl;
27532ed618SSoby Mathew 
28*bac32cc4SSaivardhan Thatikonda 	for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
29532ed618SSoby Mathew 		state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE;
30532ed618SSoby Mathew 	}
31*bac32cc4SSaivardhan Thatikonda }
32532ed618SSoby Mathew 
33532ed618SSoby Mathew /******************************************************************************
34532ed618SSoby Mathew  * Top level handler which is called when a cpu wants to power itself down.
35532ed618SSoby Mathew  * It's assumed that along with turning the cpu power domain off, power
36532ed618SSoby Mathew  * domains at higher levels will be turned off as far as possible. It finds
37532ed618SSoby Mathew  * the highest level where a domain has to be powered off by traversing the
38532ed618SSoby Mathew  * node information and then performs generic, architectural, platform setup
39532ed618SSoby Mathew  * and state management required to turn OFF that power domain and domains
40532ed618SSoby Mathew  * below it. e.g. For a cpu that's to be powered OFF, it could mean programming
41532ed618SSoby Mathew  * the power controller whereas for a cluster that's to be powered off, it will
42532ed618SSoby Mathew  * call the platform specific code which will disable coherency at the
43532ed618SSoby Mathew  * interconnect level if the cpu is the last in the cluster and also the
44532ed618SSoby Mathew  * program the power controller.
45532ed618SSoby Mathew  ******************************************************************************/
psci_do_cpu_off(unsigned int end_pwrlvl)46532ed618SSoby Mathew int psci_do_cpu_off(unsigned int end_pwrlvl)
47532ed618SSoby Mathew {
48621d64f8SAntonio Nino Diaz 	int rc = PSCI_E_SUCCESS;
495b33ad17SDeepika Bhavnani 	unsigned int idx = plat_my_core_pos();
50532ed618SSoby Mathew 	psci_power_state_t state_info;
5174d27d00SAndrew F. Davis 	unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
52532ed618SSoby Mathew 
53532ed618SSoby Mathew 	/*
54532ed618SSoby Mathew 	 * This function must only be called on platforms where the
55532ed618SSoby Mathew 	 * CPU_OFF platform hooks have been implemented.
56532ed618SSoby Mathew 	 */
57621d64f8SAntonio Nino Diaz 	assert(psci_plat_pm_ops->pwr_domain_off != NULL);
58532ed618SSoby Mathew 
59216e58a3SRoberto Vargas 	/* Construct the psci_power_state for CPU_OFF */
60216e58a3SRoberto Vargas 	psci_set_power_off_state(&state_info);
61216e58a3SRoberto Vargas 
62532ed618SSoby Mathew 	/*
636cf4ae97SVarun Wadekar 	 * Call the platform provided early CPU_OFF handler to allow
646cf4ae97SVarun Wadekar 	 * platforms to perform any housekeeping activities before
656cf4ae97SVarun Wadekar 	 * actually powering the CPU off. PSCI_E_DENIED indicates that
666cf4ae97SVarun Wadekar 	 * the CPU off sequence should be aborted at this time.
676cf4ae97SVarun Wadekar 	 */
686cf4ae97SVarun Wadekar 	if (psci_plat_pm_ops->pwr_domain_off_early) {
696cf4ae97SVarun Wadekar 		rc = psci_plat_pm_ops->pwr_domain_off_early(&state_info);
706cf4ae97SVarun Wadekar 		if (rc == PSCI_E_DENIED) {
716cf4ae97SVarun Wadekar 			return rc;
726cf4ae97SVarun Wadekar 		}
736cf4ae97SVarun Wadekar 	}
746cf4ae97SVarun Wadekar 
756cf4ae97SVarun Wadekar 	/*
7674d27d00SAndrew F. Davis 	 * Get the parent nodes here, this is important to do before we
7774d27d00SAndrew F. Davis 	 * initiate the power down sequence as after that point the core may
7874d27d00SAndrew F. Davis 	 * have exited coherency and its cache may be disabled, any access to
7974d27d00SAndrew F. Davis 	 * shared memory after that (such as the parent node lookup in
8074d27d00SAndrew F. Davis 	 * psci_cpu_pd_nodes) can cause coherency issues on some platforms.
8174d27d00SAndrew F. Davis 	 */
8274d27d00SAndrew F. Davis 	psci_get_parent_pwr_domain_nodes(idx, end_pwrlvl, parent_nodes);
8374d27d00SAndrew F. Davis 
8474d27d00SAndrew F. Davis 	/*
85532ed618SSoby Mathew 	 * This function acquires the lock corresponding to each power
86532ed618SSoby Mathew 	 * level so that by the time all locks are taken, the system topology
87532ed618SSoby Mathew 	 * is snapshot and state management can be done safely.
88532ed618SSoby Mathew 	 */
8974d27d00SAndrew F. Davis 	psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
90532ed618SSoby Mathew 
91532ed618SSoby Mathew 	/*
92532ed618SSoby Mathew 	 * Call the cpu off handler registered by the Secure Payload Dispatcher
93532ed618SSoby Mathew 	 * to let it do any bookkeeping. Assume that the SPD always reports an
94532ed618SSoby Mathew 	 * E_DENIED error if SP refuse to power down
95532ed618SSoby Mathew 	 */
96621d64f8SAntonio Nino Diaz 	if ((psci_spd_pm != NULL) && (psci_spd_pm->svc_off != NULL)) {
97532ed618SSoby Mathew 		rc = psci_spd_pm->svc_off(0);
98*bac32cc4SSaivardhan Thatikonda 		if (rc != PSCI_E_SUCCESS) {
990839cfc9SMaheedhar Bollapalli 			goto off_exit;
100532ed618SSoby Mathew 		}
101*bac32cc4SSaivardhan Thatikonda 	}
102532ed618SSoby Mathew 
103532ed618SSoby Mathew 	/*
104532ed618SSoby Mathew 	 * This function is passed the requested state info and
105532ed618SSoby Mathew 	 * it returns the negotiated state info for each power level upto
106532ed618SSoby Mathew 	 * the end level specified.
107532ed618SSoby Mathew 	 */
1083b802105SBoyan Karatotev 	psci_do_state_coordination(idx, end_pwrlvl, &state_info);
109532ed618SSoby Mathew 
110d3488614SWing Li 	/* Update the target state in the power domain nodes */
1113b802105SBoyan Karatotev 	psci_set_target_local_pwr_states(idx, end_pwrlvl, &state_info);
112d3488614SWing Li 
113532ed618SSoby Mathew #if ENABLE_PSCI_STAT
114532ed618SSoby Mathew 	/* Update the last cpu for each level till end_pwrlvl */
1153b802105SBoyan Karatotev 	psci_stats_update_pwr_down(idx, end_pwrlvl, &state_info);
116532ed618SSoby Mathew #endif
117532ed618SSoby Mathew 
118532ed618SSoby Mathew 	/*
119b0408e87SJeenu Viswambharan 	 * Arch. management. Initiate power down sequence.
120532ed618SSoby Mathew 	 */
1212b5e00d4SBoyan Karatotev 	psci_pwrdown_cpu_start(psci_find_max_off_lvl(&state_info));
122532ed618SSoby Mathew 
1235d893410SBoyan Karatotev #if USE_GIC_DRIVER
1245d893410SBoyan Karatotev 	/* turn the GIC off before we hand off to the platform */
1255d893410SBoyan Karatotev 	gic_cpuif_disable(idx);
1265d893410SBoyan Karatotev 	/* we don't want any wakeups until explicitly turned on */
1275d893410SBoyan Karatotev 	gic_pcpu_off(idx);
1285d893410SBoyan Karatotev #endif /* USE_GIC_DRIVER */
1295d893410SBoyan Karatotev 
130532ed618SSoby Mathew 	/*
131532ed618SSoby Mathew 	 * Plat. management: Perform platform specific actions to turn this
132532ed618SSoby Mathew 	 * cpu off e.g. exit cpu coherency, program the power controller etc.
133532ed618SSoby Mathew 	 */
134532ed618SSoby Mathew 	psci_plat_pm_ops->pwr_domain_off(&state_info);
135532ed618SSoby Mathew 
136532ed618SSoby Mathew #if ENABLE_PSCI_STAT
13704c1db1eSdp-arm 	plat_psci_stat_accounting_start(&state_info);
138532ed618SSoby Mathew #endif
139532ed618SSoby Mathew 
1400839cfc9SMaheedhar Bollapalli off_exit:
141532ed618SSoby Mathew 	/*
142532ed618SSoby Mathew 	 * Release the locks corresponding to each power level in the
143532ed618SSoby Mathew 	 * reverse order to which they were acquired.
144532ed618SSoby Mathew 	 */
14574d27d00SAndrew F. Davis 	psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
146532ed618SSoby Mathew 
147532ed618SSoby Mathew 	/*
148532ed618SSoby Mathew 	 * Check if all actions needed to safely power down this cpu have
149532ed618SSoby Mathew 	 * successfully completed.
150532ed618SSoby Mathew 	 */
151532ed618SSoby Mathew 	if (rc == PSCI_E_SUCCESS) {
152532ed618SSoby Mathew 		/*
153a10d3632SJeenu Viswambharan 		 * Set the affinity info state to OFF. When caches are disabled,
154a10d3632SJeenu Viswambharan 		 * this writes directly to main memory, so cache maintenance is
155532ed618SSoby Mathew 		 * required to ensure that later cached reads of aff_info_state
156532ed618SSoby Mathew 		 * return AFF_STATE_OFF. A dsbish() ensures ordering of the
157532ed618SSoby Mathew 		 * update to the affinity info state prior to cache line
158532ed618SSoby Mathew 		 * invalidation.
159532ed618SSoby Mathew 		 */
160a10d3632SJeenu Viswambharan 		psci_flush_cpu_data(psci_svc_cpu_data.aff_info_state);
161532ed618SSoby Mathew 		psci_set_aff_info_state(AFF_STATE_OFF);
162a10d3632SJeenu Viswambharan 		psci_dsbish();
163a10d3632SJeenu Viswambharan 		psci_inv_cpu_data(psci_svc_cpu_data.aff_info_state);
164532ed618SSoby Mathew 
165872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
166872be88aSdp-arm 		/*
167872be88aSdp-arm 		 * Update the timestamp with cache off.  We assume this
168872be88aSdp-arm 		 * timestamp can only be read from the current CPU and the
169872be88aSdp-arm 		 * timestamp cache line will be flushed before return to
170872be88aSdp-arm 		 * normal world on wakeup.
171872be88aSdp-arm 		 */
172872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
173872be88aSdp-arm 		    RT_INSTR_ENTER_HW_LOW_PWR,
174872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
175872be88aSdp-arm #endif
176db5fe4f4SBoyan Karatotev 		if (psci_plat_pm_ops->pwr_domain_pwr_down != NULL) {
1772b5e00d4SBoyan Karatotev 			/* This function may not return */
178db5fe4f4SBoyan Karatotev 			psci_plat_pm_ops->pwr_domain_pwr_down(&state_info);
179532ed618SSoby Mathew 		}
1802b5e00d4SBoyan Karatotev 
1812b5e00d4SBoyan Karatotev 		psci_pwrdown_cpu_end_terminal();
182532ed618SSoby Mathew 	}
183532ed618SSoby Mathew 
184532ed618SSoby Mathew 	return rc;
185532ed618SSoby Mathew }
186