xref: /rk3399_ARM-atf/lib/psci/psci_main.c (revision 382ba743a81c9b61d2ab794a22e37af0ac6128fe)
1532ed618SSoby Mathew /*
2*382ba743SBoyan 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 <string.h>
909d40e0eSAntonio Nino Diaz 
10532ed618SSoby Mathew #include <arch.h>
1145c7328cSBoyan Karatotev #include <arch_features.h>
12532ed618SSoby Mathew #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
1409d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h>
1509d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h>
1609d40e0eSAntonio Nino Diaz #include <lib/smccc.h>
1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1809d40e0eSAntonio Nino Diaz #include <services/arm_arch_svc.h>
1909d40e0eSAntonio Nino Diaz 
20532ed618SSoby Mathew #include "psci_private.h"
21532ed618SSoby Mathew 
22532ed618SSoby Mathew /*******************************************************************************
23532ed618SSoby Mathew  * PSCI frontend api for servicing SMCs. Described in the PSCI spec.
24532ed618SSoby Mathew  ******************************************************************************/
25532ed618SSoby Mathew int psci_cpu_on(u_register_t target_cpu,
26532ed618SSoby Mathew 		uintptr_t entrypoint,
27532ed618SSoby Mathew 		u_register_t context_id)
28532ed618SSoby Mathew 
29532ed618SSoby Mathew {
30532ed618SSoby Mathew 	int rc;
31532ed618SSoby Mathew 	entry_point_info_t ep;
32532ed618SSoby Mathew 
33e60c1847SManish Pandey 	/* Validate the target CPU */
34c7b0a28dSMaheedhar Bollapalli 	if (!is_valid_mpidr(target_cpu)) {
35532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
36c7b0a28dSMaheedhar Bollapalli 	}
37532ed618SSoby Mathew 
38532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
39532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
40c7b0a28dSMaheedhar Bollapalli 	if (rc != PSCI_E_SUCCESS) {
41532ed618SSoby Mathew 		return rc;
42c7b0a28dSMaheedhar Bollapalli 	}
43532ed618SSoby Mathew 
44532ed618SSoby Mathew 	/*
45532ed618SSoby Mathew 	 * To turn this cpu on, specify which power
46532ed618SSoby Mathew 	 * levels need to be turned on
47532ed618SSoby Mathew 	 */
48532ed618SSoby Mathew 	return psci_cpu_on_start(target_cpu, &ep);
49532ed618SSoby Mathew }
50532ed618SSoby Mathew 
51532ed618SSoby Mathew unsigned int psci_version(void)
52532ed618SSoby Mathew {
53532ed618SSoby Mathew 	return PSCI_MAJOR_VER | PSCI_MINOR_VER;
54532ed618SSoby Mathew }
55532ed618SSoby Mathew 
56532ed618SSoby Mathew int psci_cpu_suspend(unsigned int power_state,
57532ed618SSoby Mathew 		     uintptr_t entrypoint,
58532ed618SSoby Mathew 		     u_register_t context_id)
59532ed618SSoby Mathew {
60532ed618SSoby Mathew 	int rc;
61532ed618SSoby Mathew 	unsigned int target_pwrlvl, is_power_down_state;
62532ed618SSoby Mathew 	entry_point_info_t ep;
63532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
64532ed618SSoby Mathew 	plat_local_state_t cpu_pd_state;
65606b7430SWing Li 	unsigned int cpu_idx = plat_my_core_pos();
663b802105SBoyan Karatotev #if PSCI_OS_INIT_MODE
67606b7430SWing Li 	plat_local_state_t prev[PLAT_MAX_PWR_LVL];
68606b7430SWing Li #endif
69532ed618SSoby Mathew 
7045c7328cSBoyan Karatotev #if ERRATA_SME_POWER_DOWN
7145c7328cSBoyan Karatotev 	/*
7245c7328cSBoyan Karatotev 	 * If SME isn't off, attempting a real power down will only end up being
7345c7328cSBoyan Karatotev 	 * rejected. If we got called with SME on, fall back to a normal
7445c7328cSBoyan Karatotev 	 * suspend. We can't force SME off as in the event the power down is
7545c7328cSBoyan Karatotev 	 * rejected for another reason (eg GIC) we'd lose the SME context.
7645c7328cSBoyan Karatotev 	 */
7745c7328cSBoyan Karatotev 	if (is_feat_sme_supported() && read_svcr() != 0) {
7845c7328cSBoyan Karatotev 		power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT);
7945c7328cSBoyan Karatotev 		power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT);
8045c7328cSBoyan Karatotev 	}
8145c7328cSBoyan Karatotev #endif /* ERRATA_SME_POWER_DOWN */
8245c7328cSBoyan Karatotev 
83532ed618SSoby Mathew 	/* Validate the power_state parameter */
84532ed618SSoby Mathew 	rc = psci_validate_power_state(power_state, &state_info);
85532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS) {
86532ed618SSoby Mathew 		assert(rc == PSCI_E_INVALID_PARAMS);
87532ed618SSoby Mathew 		return rc;
88532ed618SSoby Mathew 	}
89532ed618SSoby Mathew 
90532ed618SSoby Mathew 	/*
91532ed618SSoby Mathew 	 * Get the value of the state type bit from the power state parameter.
92532ed618SSoby Mathew 	 */
93532ed618SSoby Mathew 	is_power_down_state = psci_get_pstate_type(power_state);
94532ed618SSoby Mathew 
95532ed618SSoby Mathew 	/* Sanity check the requested suspend levels */
96532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, is_power_down_state)
97532ed618SSoby Mathew 			== PSCI_E_SUCCESS);
98532ed618SSoby Mathew 
99532ed618SSoby Mathew 	target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
100a1c3faa6SSandrine Bailleux 	if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
101a1c3faa6SSandrine Bailleux 		ERROR("Invalid target power level for suspend operation\n");
102a1c3faa6SSandrine Bailleux 		panic();
103a1c3faa6SSandrine Bailleux 	}
104532ed618SSoby Mathew 
105532ed618SSoby Mathew 	/* Fast path for CPU standby.*/
106362030bfSAntonio Nino Diaz 	if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
107c7b0a28dSMaheedhar Bollapalli 		if  (psci_plat_pm_ops->cpu_standby == NULL) {
108532ed618SSoby Mathew 			return PSCI_E_INVALID_PARAMS;
109c7b0a28dSMaheedhar Bollapalli 		}
110532ed618SSoby Mathew 
111532ed618SSoby Mathew 		/*
112532ed618SSoby Mathew 		 * Set the state of the CPU power domain to the platform
113532ed618SSoby Mathew 		 * specific retention state and enter the standby state.
114532ed618SSoby Mathew 		 */
115532ed618SSoby Mathew 		cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
116532ed618SSoby Mathew 		psci_set_cpu_local_state(cpu_pd_state);
117532ed618SSoby Mathew 
118606b7430SWing Li #if PSCI_OS_INIT_MODE
119606b7430SWing Li 		/*
120606b7430SWing Li 		 * If in OS-initiated mode, save a copy of the previous
121606b7430SWing Li 		 * requested local power states and update the new requested
122606b7430SWing Li 		 * local power states for this CPU.
123606b7430SWing Li 		 */
124606b7430SWing Li 		if (psci_suspend_mode == OS_INIT) {
125606b7430SWing Li 			psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx,
126606b7430SWing Li 							 &state_info, prev);
127606b7430SWing Li 		}
128606b7430SWing Li #endif
129606b7430SWing Li 
130532ed618SSoby Mathew #if ENABLE_PSCI_STAT
13104c1db1eSdp-arm 		plat_psci_stat_accounting_start(&state_info);
132532ed618SSoby Mathew #endif
133532ed618SSoby Mathew 
134872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
135872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
136872be88aSdp-arm 		    RT_INSTR_ENTER_HW_LOW_PWR,
137872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
138872be88aSdp-arm #endif
139872be88aSdp-arm 
140532ed618SSoby Mathew 		psci_plat_pm_ops->cpu_standby(cpu_pd_state);
141532ed618SSoby Mathew 
142532ed618SSoby Mathew 		/* Upon exit from standby, set the state back to RUN. */
143532ed618SSoby Mathew 		psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
144532ed618SSoby Mathew 
145606b7430SWing Li #if PSCI_OS_INIT_MODE
146606b7430SWing Li 		/*
147606b7430SWing Li 		 * If in OS-initiated mode, restore the previous requested
148606b7430SWing Li 		 * local power states for this CPU.
149606b7430SWing Li 		 */
150606b7430SWing Li 		if (psci_suspend_mode == OS_INIT) {
151606b7430SWing Li 			psci_restore_req_local_pwr_states(cpu_idx, prev);
152606b7430SWing Li 		}
153606b7430SWing Li #endif
154606b7430SWing Li 
155872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
156872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
157872be88aSdp-arm 		    RT_INSTR_EXIT_HW_LOW_PWR,
158872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
159872be88aSdp-arm #endif
160872be88aSdp-arm 
161532ed618SSoby Mathew #if ENABLE_PSCI_STAT
16204c1db1eSdp-arm 		plat_psci_stat_accounting_stop(&state_info);
163532ed618SSoby Mathew 
164532ed618SSoby Mathew 		/* Update PSCI stats */
1653b802105SBoyan Karatotev 		psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
166532ed618SSoby Mathew #endif
167532ed618SSoby Mathew 
168532ed618SSoby Mathew 		return PSCI_E_SUCCESS;
169532ed618SSoby Mathew 	}
170532ed618SSoby Mathew 
171532ed618SSoby Mathew 	/*
172532ed618SSoby Mathew 	 * If a power down state has been requested, we need to verify entry
173532ed618SSoby Mathew 	 * point and program entry information.
174532ed618SSoby Mathew 	 */
1756b7b0f36SAntonio Nino Diaz 	if (is_power_down_state != 0U) {
176532ed618SSoby Mathew 		rc = psci_validate_entry_point(&ep, entrypoint, context_id);
177c7b0a28dSMaheedhar Bollapalli 		if (rc != PSCI_E_SUCCESS) {
178532ed618SSoby Mathew 			return rc;
179532ed618SSoby Mathew 		}
180c7b0a28dSMaheedhar Bollapalli 	}
181532ed618SSoby Mathew 
182532ed618SSoby Mathew 	/*
183532ed618SSoby Mathew 	 * Do what is needed to enter the power down state. Upon success,
184532ed618SSoby Mathew 	 * enter the final wfi which will power down this CPU. This function
185532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
186532ed618SSoby Mathew 	 * arrival of an interrupt
187532ed618SSoby Mathew 	 */
1883b802105SBoyan Karatotev 	rc = psci_cpu_suspend_start(cpu_idx,
1893b802105SBoyan Karatotev 				    &ep,
190532ed618SSoby Mathew 				    target_pwrlvl,
191532ed618SSoby Mathew 				    &state_info,
192532ed618SSoby Mathew 				    is_power_down_state);
193532ed618SSoby Mathew 
194606b7430SWing Li 	return rc;
195532ed618SSoby Mathew }
196532ed618SSoby Mathew 
197532ed618SSoby Mathew 
198532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
199532ed618SSoby Mathew {
200532ed618SSoby Mathew 	int rc;
201532ed618SSoby Mathew 	psci_power_state_t state_info;
202532ed618SSoby Mathew 	entry_point_info_t ep;
2033b802105SBoyan Karatotev 	unsigned int cpu_idx = plat_my_core_pos();
204532ed618SSoby Mathew 
205532ed618SSoby Mathew 	/* Check if the current CPU is the last ON CPU in the system */
206c7b0a28dSMaheedhar Bollapalli 	if (!psci_is_last_on_cpu(cpu_idx)) {
207532ed618SSoby Mathew 		return PSCI_E_DENIED;
208c7b0a28dSMaheedhar Bollapalli 	}
209532ed618SSoby Mathew 
210532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
211532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
212c7b0a28dSMaheedhar Bollapalli 	if (rc != PSCI_E_SUCCESS) {
213532ed618SSoby Mathew 		return rc;
214c7b0a28dSMaheedhar Bollapalli 	}
215532ed618SSoby Mathew 
216532ed618SSoby Mathew 	/* Query the psci_power_state for system suspend */
217532ed618SSoby Mathew 	psci_query_sys_suspend_pwrstate(&state_info);
218532ed618SSoby Mathew 
219a4065abdSldts 	/*
220a4065abdSldts 	 * Check if platform allows suspend to Highest power level
221a4065abdSldts 	 * (System level)
222a4065abdSldts 	 */
223c7b0a28dSMaheedhar Bollapalli 	if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) {
224a4065abdSldts 		return PSCI_E_DENIED;
225c7b0a28dSMaheedhar Bollapalli 	}
226532ed618SSoby Mathew 	/* Ensure that the psci_power_state makes sense */
227532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
228532ed618SSoby Mathew 						== PSCI_E_SUCCESS);
2296b7b0f36SAntonio Nino Diaz 	assert(is_local_state_off(
2306b7b0f36SAntonio Nino Diaz 			state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
231532ed618SSoby Mathew 
232532ed618SSoby Mathew 	/*
233532ed618SSoby Mathew 	 * Do what is needed to enter the system suspend state. This function
234532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
235532ed618SSoby Mathew 	 * arrival of an interrupt
236532ed618SSoby Mathew 	 */
2373b802105SBoyan Karatotev 	rc = psci_cpu_suspend_start(cpu_idx,
2383b802105SBoyan Karatotev 				    &ep,
239532ed618SSoby Mathew 				    PLAT_MAX_PWR_LVL,
240532ed618SSoby Mathew 				    &state_info,
241532ed618SSoby Mathew 				    PSTATE_TYPE_POWERDOWN);
242532ed618SSoby Mathew 
243606b7430SWing Li 	return rc;
244532ed618SSoby Mathew }
245532ed618SSoby Mathew 
246532ed618SSoby Mathew int psci_cpu_off(void)
247532ed618SSoby Mathew {
248532ed618SSoby Mathew 	int rc;
249532ed618SSoby Mathew 	unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
250532ed618SSoby Mathew 
251532ed618SSoby Mathew 	/*
252532ed618SSoby Mathew 	 * Do what is needed to power off this CPU and possible higher power
253532ed618SSoby Mathew 	 * levels if it able to do so. Upon success, enter the final wfi
254532ed618SSoby Mathew 	 * which will power down this CPU.
255532ed618SSoby Mathew 	 */
256532ed618SSoby Mathew 	rc = psci_do_cpu_off(target_pwrlvl);
257532ed618SSoby Mathew 
258532ed618SSoby Mathew 	/*
259532ed618SSoby Mathew 	 * The only error cpu_off can return is E_DENIED. So check if that's
260532ed618SSoby Mathew 	 * indeed the case.
261532ed618SSoby Mathew 	 */
262532ed618SSoby Mathew 	assert(rc == PSCI_E_DENIED);
263532ed618SSoby Mathew 
264532ed618SSoby Mathew 	return rc;
265532ed618SSoby Mathew }
266532ed618SSoby Mathew 
267532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity,
268532ed618SSoby Mathew 		       unsigned int lowest_affinity_level)
269532ed618SSoby Mathew {
2705b33ad17SDeepika Bhavnani 	unsigned int target_idx;
271532ed618SSoby Mathew 
272e60c1847SManish Pandey 	/* Validate the target affinity */
273c7b0a28dSMaheedhar Bollapalli 	if (!is_valid_mpidr(target_affinity)) {
274e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
275c7b0a28dSMaheedhar Bollapalli 	}
276e60c1847SManish Pandey 
277532ed618SSoby Mathew 	/* We dont support level higher than PSCI_CPU_PWR_LVL */
278c7b0a28dSMaheedhar Bollapalli 	if (lowest_affinity_level > PSCI_CPU_PWR_LVL) {
279532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
280c7b0a28dSMaheedhar Bollapalli 	}
281532ed618SSoby Mathew 	/* Calculate the cpu index of the target */
282e60c1847SManish Pandey 	target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
283532ed618SSoby Mathew 
2848fd307ffSRoberto Vargas 	/*
2858fd307ffSRoberto Vargas 	 * Generic management:
2868fd307ffSRoberto Vargas 	 * Perform cache maintanence ahead of reading the target CPU state to
2878fd307ffSRoberto Vargas 	 * ensure that the data is not stale.
2888fd307ffSRoberto Vargas 	 * There is a theoretical edge case where the cache may contain stale
2898fd307ffSRoberto Vargas 	 * data for the target CPU data - this can occur under the following
2908fd307ffSRoberto Vargas 	 * conditions:
2918fd307ffSRoberto Vargas 	 * - the target CPU is in another cluster from the current
2928fd307ffSRoberto Vargas 	 * - the target CPU was the last CPU to shutdown on its cluster
2938fd307ffSRoberto Vargas 	 * - the cluster was removed from coherency as part of the CPU shutdown
2948fd307ffSRoberto Vargas 	 *
2958fd307ffSRoberto Vargas 	 * In this case the cache maintenace that was performed as part of the
2968fd307ffSRoberto Vargas 	 * target CPUs shutdown was not seen by the current CPU's cluster. And
2978fd307ffSRoberto Vargas 	 * so the cache may contain stale data for the target CPU.
2988fd307ffSRoberto Vargas 	 */
2995b33ad17SDeepika Bhavnani 	flush_cpu_data_by_index(target_idx,
3006b7b0f36SAntonio Nino Diaz 				psci_svc_cpu_data.aff_info_state);
3018fd307ffSRoberto Vargas 
302532ed618SSoby Mathew 	return psci_get_aff_info_state_by_idx(target_idx);
303532ed618SSoby Mathew }
304532ed618SSoby Mathew 
305532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu)
306532ed618SSoby Mathew {
307532ed618SSoby Mathew 	int rc;
308*382ba743SBoyan Karatotev 	u_register_t resident_cpu_mpidr = 0;
309532ed618SSoby Mathew 
310e60c1847SManish Pandey 	/* Validate the target cpu */
311e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
312e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
313e60c1847SManish Pandey 
314532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
315c7b0a28dSMaheedhar Bollapalli 	if (rc != PSCI_TOS_UP_MIG_CAP) {
316532ed618SSoby Mathew 		return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
317532ed618SSoby Mathew 			  PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
318c7b0a28dSMaheedhar Bollapalli 	}
319532ed618SSoby Mathew 
320532ed618SSoby Mathew 	/*
321532ed618SSoby Mathew 	 * Migrate should only be invoked on the CPU where
322532ed618SSoby Mathew 	 * the Secure OS is resident.
323532ed618SSoby Mathew 	 */
324c7b0a28dSMaheedhar Bollapalli 	if (resident_cpu_mpidr != read_mpidr_el1()) {
325532ed618SSoby Mathew 		return PSCI_E_NOT_PRESENT;
326c7b0a28dSMaheedhar Bollapalli 	}
327532ed618SSoby Mathew 
328532ed618SSoby Mathew 	/* Check the validity of the specified target cpu */
329c7b0a28dSMaheedhar Bollapalli 	if (!is_valid_mpidr(target_cpu)) {
330532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
331c7b0a28dSMaheedhar Bollapalli 	}
332532ed618SSoby Mathew 
3336b7b0f36SAntonio Nino Diaz 	assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
334532ed618SSoby Mathew 
335532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
3366b7b0f36SAntonio Nino Diaz 	assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
337532ed618SSoby Mathew 
338532ed618SSoby Mathew 	return rc;
339532ed618SSoby Mathew }
340532ed618SSoby Mathew 
341532ed618SSoby Mathew int psci_migrate_info_type(void)
342532ed618SSoby Mathew {
343532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
344532ed618SSoby Mathew 
345532ed618SSoby Mathew 	return psci_spd_migrate_info(&resident_cpu_mpidr);
346532ed618SSoby Mathew }
347532ed618SSoby Mathew 
3486b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void)
349532ed618SSoby Mathew {
350*382ba743SBoyan Karatotev 	u_register_t resident_cpu_mpidr = 0;
351532ed618SSoby Mathew 	int rc;
352532ed618SSoby Mathew 
353532ed618SSoby Mathew 	/*
354532ed618SSoby Mathew 	 * Return value of this depends upon what
355532ed618SSoby Mathew 	 * psci_spd_migrate_info() returns.
356532ed618SSoby Mathew 	 */
357532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
3586b7b0f36SAntonio Nino Diaz 	if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
3596b7b0f36SAntonio Nino Diaz 		return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
360532ed618SSoby Mathew 
361532ed618SSoby Mathew 	return resident_cpu_mpidr;
362532ed618SSoby Mathew }
363532ed618SSoby Mathew 
36428d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu,
36528d3d614SJeenu Viswambharan 		       unsigned int power_level)
36628d3d614SJeenu Viswambharan {
36728d3d614SJeenu Viswambharan 	int rc;
36828d3d614SJeenu Viswambharan 
36928d3d614SJeenu Viswambharan 	/* Validate target_cpu */
370e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
37128d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
37228d3d614SJeenu Viswambharan 
37328d3d614SJeenu Viswambharan 	/* Validate power_level against PLAT_MAX_PWR_LVL */
37428d3d614SJeenu Viswambharan 	if (power_level > PLAT_MAX_PWR_LVL)
37528d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
37628d3d614SJeenu Viswambharan 
37728d3d614SJeenu Viswambharan 	/*
37828d3d614SJeenu Viswambharan 	 * Dispatch this call to platform to query power controller, and pass on
37928d3d614SJeenu Viswambharan 	 * to the caller what it returns
38028d3d614SJeenu Viswambharan 	 */
3816b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->get_node_hw_state != NULL);
38228d3d614SJeenu Viswambharan 	rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
3836b7b0f36SAntonio Nino Diaz 	assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
3846b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_NOT_SUPPORTED)
3856b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_INVALID_PARAMS));
38628d3d614SJeenu Viswambharan 	return rc;
38728d3d614SJeenu Viswambharan }
38828d3d614SJeenu Viswambharan 
389532ed618SSoby Mathew int psci_features(unsigned int psci_fid)
390532ed618SSoby Mathew {
391532ed618SSoby Mathew 	unsigned int local_caps = psci_caps;
392532ed618SSoby Mathew 
393c7b0a28dSMaheedhar Bollapalli 	if (psci_fid == SMCCC_VERSION) {
3946eabbb07SDimitris Papastamos 		return PSCI_E_SUCCESS;
395c7b0a28dSMaheedhar Bollapalli 	}
396532ed618SSoby Mathew 	/* Check if it is a 64 bit function */
397c7b0a28dSMaheedhar Bollapalli 	if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) {
398532ed618SSoby Mathew 		local_caps &= PSCI_CAP_64BIT_MASK;
399c7b0a28dSMaheedhar Bollapalli 	}
400532ed618SSoby Mathew 	/* Check for invalid fid */
401532ed618SSoby Mathew 	if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
402c7b0a28dSMaheedhar Bollapalli 			&& is_psci_fid(psci_fid))) {
403532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
404c7b0a28dSMaheedhar Bollapalli 	}
405532ed618SSoby Mathew 
406532ed618SSoby Mathew 	/* Check if the psci fid is supported or not */
407c7b0a28dSMaheedhar Bollapalli 	if ((local_caps & define_psci_cap(psci_fid)) == 0U) {
408532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
409c7b0a28dSMaheedhar Bollapalli 	}
410532ed618SSoby Mathew 	/* Format the feature flags */
4116b7b0f36SAntonio Nino Diaz 	if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
4126b7b0f36SAntonio Nino Diaz 	    (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
4136b7b0f36SAntonio Nino Diaz 		unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
4149a70e69eSWing Li 			(FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
4156b7b0f36SAntonio Nino Diaz 		return (int)ret;
416532ed618SSoby Mathew 	}
417532ed618SSoby Mathew 
418532ed618SSoby Mathew 	/* Return 0 for all other fid's */
419532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
420532ed618SSoby Mathew }
421532ed618SSoby Mathew 
422b88a4416SWing Li #if PSCI_OS_INIT_MODE
423b88a4416SWing Li int psci_set_suspend_mode(unsigned int mode)
424b88a4416SWing Li {
425b88a4416SWing Li 	if (psci_suspend_mode == mode) {
426b88a4416SWing Li 		return PSCI_E_SUCCESS;
427b88a4416SWing Li 	}
428b88a4416SWing Li 
4293b802105SBoyan Karatotev 	unsigned int this_core = plat_my_core_pos();
4303b802105SBoyan Karatotev 
431b88a4416SWing Li 	if (mode == PLAT_COORD) {
432b88a4416SWing Li 		/* Check if the current CPU is the last ON CPU in the system */
4333b802105SBoyan Karatotev 		if (!psci_is_last_on_cpu_safe(this_core)) {
434b88a4416SWing Li 			return PSCI_E_DENIED;
435b88a4416SWing Li 		}
436b88a4416SWing Li 	}
437b88a4416SWing Li 
438b88a4416SWing Li 	if (mode == OS_INIT) {
439b88a4416SWing Li 		/*
440b88a4416SWing Li 		 * Check if all CPUs in the system are ON or if the current
441b88a4416SWing Li 		 * CPU is the last ON CPU in the system.
442b88a4416SWing Li 		 */
4433b802105SBoyan Karatotev 		if (!(psci_are_all_cpus_on_safe(this_core) ||
4443b802105SBoyan Karatotev 		      psci_is_last_on_cpu_safe(this_core))) {
445b88a4416SWing Li 			return PSCI_E_DENIED;
446b88a4416SWing Li 		}
447b88a4416SWing Li 	}
448b88a4416SWing Li 
449b88a4416SWing Li 	psci_suspend_mode = mode;
450b88a4416SWing Li 	psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
451b88a4416SWing Li 				sizeof(psci_suspend_mode));
452b88a4416SWing Li 
453b88a4416SWing Li 	return PSCI_E_SUCCESS;
454b88a4416SWing Li }
455b88a4416SWing Li #endif
456b88a4416SWing Li 
457532ed618SSoby Mathew /*******************************************************************************
458532ed618SSoby Mathew  * PSCI top level handler for servicing SMCs.
459532ed618SSoby Mathew  ******************************************************************************/
460cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid,
461532ed618SSoby Mathew 			  u_register_t x1,
462532ed618SSoby Mathew 			  u_register_t x2,
463532ed618SSoby Mathew 			  u_register_t x3,
464532ed618SSoby Mathew 			  u_register_t x4,
465532ed618SSoby Mathew 			  void *cookie,
466532ed618SSoby Mathew 			  void *handle,
467532ed618SSoby Mathew 			  u_register_t flags)
468532ed618SSoby Mathew {
4696b7b0f36SAntonio Nino Diaz 	u_register_t ret;
4706b7b0f36SAntonio Nino Diaz 
471c7b0a28dSMaheedhar Bollapalli 	if (is_caller_secure(flags)) {
4726b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
473c7b0a28dSMaheedhar Bollapalli 	}
474532ed618SSoby Mathew 
475532ed618SSoby Mathew 	/* Check the fid against the capabilities */
476c7b0a28dSMaheedhar Bollapalli 	if ((psci_caps & define_psci_cap(smc_fid)) == 0U) {
4776b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
478c7b0a28dSMaheedhar Bollapalli 	}
479532ed618SSoby Mathew 
480532ed618SSoby Mathew 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
481532ed618SSoby Mathew 		/* 32-bit PSCI function, clear top parameter bits */
482532ed618SSoby Mathew 
4836b7b0f36SAntonio Nino Diaz 		uint32_t r1 = (uint32_t)x1;
4846b7b0f36SAntonio Nino Diaz 		uint32_t r2 = (uint32_t)x2;
4856b7b0f36SAntonio Nino Diaz 		uint32_t r3 = (uint32_t)x3;
486532ed618SSoby Mathew 
487532ed618SSoby Mathew 		switch (smc_fid) {
488532ed618SSoby Mathew 		case PSCI_VERSION:
4896b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_version();
4906b7b0f36SAntonio Nino Diaz 			break;
491532ed618SSoby Mathew 
492532ed618SSoby Mathew 		case PSCI_CPU_OFF:
4936b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_off();
4946b7b0f36SAntonio Nino Diaz 			break;
495532ed618SSoby Mathew 
496532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH32:
4976b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
4986b7b0f36SAntonio Nino Diaz 			break;
499532ed618SSoby Mathew 
500532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH32:
5016b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(r1, r2, r3);
5026b7b0f36SAntonio Nino Diaz 			break;
503532ed618SSoby Mathew 
504532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH32:
5056b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_affinity_info(r1, r2);
5066b7b0f36SAntonio Nino Diaz 			break;
507532ed618SSoby Mathew 
508532ed618SSoby Mathew 		case PSCI_MIG_AARCH32:
5096b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(r1);
5106b7b0f36SAntonio Nino Diaz 			break;
511532ed618SSoby Mathew 
512532ed618SSoby Mathew 		case PSCI_MIG_INFO_TYPE:
5136b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate_info_type();
5146b7b0f36SAntonio Nino Diaz 			break;
515532ed618SSoby Mathew 
516532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH32:
5176b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
5186b7b0f36SAntonio Nino Diaz 			break;
519532ed618SSoby Mathew 
52028d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH32:
5216b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(r1, r2);
5226b7b0f36SAntonio Nino Diaz 			break;
52328d3d614SJeenu Viswambharan 
524532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH32:
5256b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(r1, r2);
5266b7b0f36SAntonio Nino Diaz 			break;
527532ed618SSoby Mathew 
528532ed618SSoby Mathew 		case PSCI_SYSTEM_OFF:
529532ed618SSoby Mathew 			psci_system_off();
530532ed618SSoby Mathew 			/* We should never return from psci_system_off() */
5313eacacc0SJonathan Wright 			break;
532532ed618SSoby Mathew 
533532ed618SSoby Mathew 		case PSCI_SYSTEM_RESET:
534532ed618SSoby Mathew 			psci_system_reset();
535532ed618SSoby Mathew 			/* We should never return from psci_system_reset() */
5363eacacc0SJonathan Wright 			break;
537532ed618SSoby Mathew 
538532ed618SSoby Mathew 		case PSCI_FEATURES:
5396b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_features(r1);
5406b7b0f36SAntonio Nino Diaz 			break;
541532ed618SSoby Mathew 
542b88a4416SWing Li #if PSCI_OS_INIT_MODE
543b88a4416SWing Li 		case PSCI_SET_SUSPEND_MODE:
544b88a4416SWing Li 			ret = (u_register_t)psci_set_suspend_mode(r1);
545b88a4416SWing Li 			break;
546b88a4416SWing Li #endif
547b88a4416SWing Li 
548532ed618SSoby Mathew #if ENABLE_PSCI_STAT
549532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH32:
5506b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(r1, r2);
5516b7b0f36SAntonio Nino Diaz 			break;
552532ed618SSoby Mathew 
553532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH32:
5546b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(r1, r2);
5556b7b0f36SAntonio Nino Diaz 			break;
556532ed618SSoby Mathew #endif
557d4c596beSRoberto Vargas 		case PSCI_MEM_PROTECT:
5586b7b0f36SAntonio Nino Diaz 			ret = psci_mem_protect(r1);
5596b7b0f36SAntonio Nino Diaz 			break;
560d4c596beSRoberto Vargas 
561d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH32:
5626b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(r1, r2);
5636b7b0f36SAntonio Nino Diaz 			break;
564532ed618SSoby Mathew 
56536a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH32:
56636a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
5676b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2(r1, r2);
5686b7b0f36SAntonio Nino Diaz 			break;
56936a8f8fdSRoberto Vargas 
570532ed618SSoby Mathew 		default:
5716b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
5726b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
573532ed618SSoby Mathew 			break;
574532ed618SSoby Mathew 		}
575532ed618SSoby Mathew 	} else {
576532ed618SSoby Mathew 		/* 64-bit PSCI function */
577532ed618SSoby Mathew 
578532ed618SSoby Mathew 		switch (smc_fid) {
579532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH64:
5806b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5816b7b0f36SAntonio Nino Diaz 				psci_cpu_suspend((unsigned int)x1, x2, x3);
5826b7b0f36SAntonio Nino Diaz 			break;
583532ed618SSoby Mathew 
584532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH64:
5856b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(x1, x2, x3);
5866b7b0f36SAntonio Nino Diaz 			break;
587532ed618SSoby Mathew 
588532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH64:
5896b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5906b7b0f36SAntonio Nino Diaz 				psci_affinity_info(x1, (unsigned int)x2);
5916b7b0f36SAntonio Nino Diaz 			break;
592532ed618SSoby Mathew 
593532ed618SSoby Mathew 		case PSCI_MIG_AARCH64:
5946b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(x1);
5956b7b0f36SAntonio Nino Diaz 			break;
596532ed618SSoby Mathew 
597532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH64:
5986b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
5996b7b0f36SAntonio Nino Diaz 			break;
600532ed618SSoby Mathew 
60128d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH64:
6026b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(
6036b7b0f36SAntonio Nino Diaz 					x1, (unsigned int) x2);
6046b7b0f36SAntonio Nino Diaz 			break;
60528d3d614SJeenu Viswambharan 
606532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH64:
6076b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(x1, x2);
6086b7b0f36SAntonio Nino Diaz 			break;
609532ed618SSoby Mathew 
610532ed618SSoby Mathew #if ENABLE_PSCI_STAT
611532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH64:
6126b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(x1, (unsigned int) x2);
6136b7b0f36SAntonio Nino Diaz 			break;
614532ed618SSoby Mathew 
615532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH64:
6166b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(x1, (unsigned int) x2);
6176b7b0f36SAntonio Nino Diaz 			break;
618532ed618SSoby Mathew #endif
619532ed618SSoby Mathew 
620d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH64:
6216b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(x1, x2);
6226b7b0f36SAntonio Nino Diaz 			break;
623d4c596beSRoberto Vargas 
62436a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH64:
62536a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
6266b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2((uint32_t) x1, x2);
6276b7b0f36SAntonio Nino Diaz 			break;
628d4c596beSRoberto Vargas 
629532ed618SSoby Mathew 		default:
6306b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
6316b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
632532ed618SSoby Mathew 			break;
633532ed618SSoby Mathew 		}
634532ed618SSoby Mathew 	}
635532ed618SSoby Mathew 
6366b7b0f36SAntonio Nino Diaz 	return ret;
637532ed618SSoby Mathew }
638