xref: /rk3399_ARM-atf/lib/psci/psci_main.c (revision 3a9a70382702bdf266a517820b3b6d62b9157715)
1532ed618SSoby Mathew /*
2382ba743SBoyan 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  ******************************************************************************/
psci_cpu_on(u_register_t target_cpu,uintptr_t entrypoint,u_register_t context_id)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;
31a9eb44d4SSigned-off-by: Maheedhar Bollapalli 	entry_point_info_t *ep = NULL;
32ef738d19SManish Pandey 	unsigned int target_idx = (unsigned int)plat_core_pos_by_mpidr(target_cpu);
33532ed618SSoby Mathew 
34e60c1847SManish Pandey 	/* Validate the target CPU */
35c7b0a28dSMaheedhar Bollapalli 	if (!is_valid_mpidr(target_cpu)) {
36532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
37c7b0a28dSMaheedhar Bollapalli 	}
38532ed618SSoby Mathew 
39ef738d19SManish Pandey 	ep = get_cpu_data_by_index(target_idx, warmboot_ep_info);
40ef738d19SManish Pandey 	/* Validate the lower EL entry point and put it in the entry_point_info */
41ef738d19SManish Pandey 	rc = psci_validate_entry_point(ep, entrypoint, context_id);
42c7b0a28dSMaheedhar Bollapalli 	if (rc != PSCI_E_SUCCESS) {
43532ed618SSoby Mathew 		return rc;
44c7b0a28dSMaheedhar Bollapalli 	}
45532ed618SSoby Mathew 
46532ed618SSoby Mathew 	/*
47532ed618SSoby Mathew 	 * To turn this cpu on, specify which power
48532ed618SSoby Mathew 	 * levels need to be turned on
49532ed618SSoby Mathew 	 */
50ef738d19SManish Pandey 	return psci_cpu_on_start(target_cpu, ep);
51532ed618SSoby Mathew }
52532ed618SSoby Mathew 
psci_version(void)53532ed618SSoby Mathew unsigned int psci_version(void)
54532ed618SSoby Mathew {
55532ed618SSoby Mathew 	return PSCI_MAJOR_VER | PSCI_MINOR_VER;
56532ed618SSoby Mathew }
57532ed618SSoby Mathew 
psci_cpu_suspend(unsigned int power_state,uintptr_t entrypoint,u_register_t context_id)58532ed618SSoby Mathew int psci_cpu_suspend(unsigned int power_state,
59532ed618SSoby Mathew 		     uintptr_t entrypoint,
60532ed618SSoby Mathew 		     u_register_t context_id)
61532ed618SSoby Mathew {
62532ed618SSoby Mathew 	int rc;
63532ed618SSoby Mathew 	unsigned int target_pwrlvl, is_power_down_state;
64532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
65532ed618SSoby Mathew 	plat_local_state_t cpu_pd_state;
66606b7430SWing Li 	unsigned int cpu_idx = plat_my_core_pos();
67532ed618SSoby Mathew 
6845c7328cSBoyan Karatotev #if ERRATA_SME_POWER_DOWN
6945c7328cSBoyan Karatotev 	/*
7045c7328cSBoyan Karatotev 	 * If SME isn't off, attempting a real power down will only end up being
7145c7328cSBoyan Karatotev 	 * rejected. If we got called with SME on, fall back to a normal
7245c7328cSBoyan Karatotev 	 * suspend. We can't force SME off as in the event the power down is
7345c7328cSBoyan Karatotev 	 * rejected for another reason (eg GIC) we'd lose the SME context.
7445c7328cSBoyan Karatotev 	 */
7545c7328cSBoyan Karatotev 	if (is_feat_sme_supported() && read_svcr() != 0) {
7645c7328cSBoyan Karatotev 		power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT);
7745c7328cSBoyan Karatotev 		power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT);
7845c7328cSBoyan Karatotev 	}
7945c7328cSBoyan Karatotev #endif /* ERRATA_SME_POWER_DOWN */
8045c7328cSBoyan Karatotev 
81532ed618SSoby Mathew 	/* Validate the power_state parameter */
82532ed618SSoby Mathew 	rc = psci_validate_power_state(power_state, &state_info);
83532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS) {
84532ed618SSoby Mathew 		assert(rc == PSCI_E_INVALID_PARAMS);
85532ed618SSoby Mathew 		return rc;
86532ed618SSoby Mathew 	}
87532ed618SSoby Mathew 
88532ed618SSoby Mathew 	/*
89532ed618SSoby Mathew 	 * Get the value of the state type bit from the power state parameter.
90532ed618SSoby Mathew 	 */
91532ed618SSoby Mathew 	is_power_down_state = psci_get_pstate_type(power_state);
92532ed618SSoby Mathew 
93532ed618SSoby Mathew 	/* Sanity check the requested suspend levels */
94532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, is_power_down_state)
95532ed618SSoby Mathew 			== PSCI_E_SUCCESS);
96532ed618SSoby Mathew 
97532ed618SSoby Mathew 	target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
98a1c3faa6SSandrine Bailleux 	if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
99a1c3faa6SSandrine Bailleux 		ERROR("Invalid target power level for suspend operation\n");
100a1c3faa6SSandrine Bailleux 		panic();
101a1c3faa6SSandrine Bailleux 	}
102532ed618SSoby Mathew 
103b34be5dfSBoyan Karatotev 	/* Fast path for local CPU standby, won't interact with higher power levels. */
104362030bfSAntonio Nino Diaz 	if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
105c7b0a28dSMaheedhar Bollapalli 		if  (psci_plat_pm_ops->cpu_standby == NULL) {
106532ed618SSoby Mathew 			return PSCI_E_INVALID_PARAMS;
107c7b0a28dSMaheedhar Bollapalli 		}
108532ed618SSoby Mathew 
109532ed618SSoby Mathew 		/*
110532ed618SSoby Mathew 		 * Set the state of the CPU power domain to the platform
111532ed618SSoby Mathew 		 * specific retention state and enter the standby state.
112532ed618SSoby Mathew 		 */
113532ed618SSoby Mathew 		cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
114532ed618SSoby Mathew 		psci_set_cpu_local_state(cpu_pd_state);
115532ed618SSoby Mathew 
116532ed618SSoby Mathew #if ENABLE_PSCI_STAT
11704c1db1eSdp-arm 		plat_psci_stat_accounting_start(&state_info);
118532ed618SSoby Mathew #endif
119532ed618SSoby Mathew 
120872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
121872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
122872be88aSdp-arm 		    RT_INSTR_ENTER_HW_LOW_PWR,
123872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
124872be88aSdp-arm #endif
125872be88aSdp-arm 
126532ed618SSoby Mathew 		psci_plat_pm_ops->cpu_standby(cpu_pd_state);
127532ed618SSoby Mathew 
128532ed618SSoby Mathew 		/* Upon exit from standby, set the state back to RUN. */
129532ed618SSoby Mathew 		psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
130532ed618SSoby Mathew 
131872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
132872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
133872be88aSdp-arm 		    RT_INSTR_EXIT_HW_LOW_PWR,
134872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
135872be88aSdp-arm #endif
136872be88aSdp-arm 
137532ed618SSoby Mathew #if ENABLE_PSCI_STAT
13804c1db1eSdp-arm 		plat_psci_stat_accounting_stop(&state_info);
139532ed618SSoby Mathew 
140532ed618SSoby Mathew 		/* Update PSCI stats */
1413b802105SBoyan Karatotev 		psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
142532ed618SSoby Mathew #endif
143532ed618SSoby Mathew 
144532ed618SSoby Mathew 		return PSCI_E_SUCCESS;
145532ed618SSoby Mathew 	}
146532ed618SSoby Mathew 
147532ed618SSoby Mathew 	/*
148532ed618SSoby Mathew 	 * If a power down state has been requested, we need to verify entry
149532ed618SSoby Mathew 	 * point and program entry information.
150532ed618SSoby Mathew 	 */
1516b7b0f36SAntonio Nino Diaz 	if (is_power_down_state != 0U) {
152ef738d19SManish Pandey 		entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info);
153ef738d19SManish Pandey 
154ef738d19SManish Pandey 		rc = psci_validate_entry_point(ep, entrypoint, context_id);
155c7b0a28dSMaheedhar Bollapalli 		if (rc != PSCI_E_SUCCESS) {
156532ed618SSoby Mathew 			return rc;
157532ed618SSoby Mathew 		}
158c7b0a28dSMaheedhar Bollapalli 	}
159532ed618SSoby Mathew 
160532ed618SSoby Mathew 	/*
161532ed618SSoby Mathew 	 * Do what is needed to enter the power down state. Upon success,
162532ed618SSoby Mathew 	 * enter the final wfi which will power down this CPU. This function
163532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
164532ed618SSoby Mathew 	 * arrival of an interrupt
165532ed618SSoby Mathew 	 */
1663b802105SBoyan Karatotev 	rc = psci_cpu_suspend_start(cpu_idx,
167532ed618SSoby Mathew 				    target_pwrlvl,
168532ed618SSoby Mathew 				    &state_info,
169532ed618SSoby Mathew 				    is_power_down_state);
170532ed618SSoby Mathew 
171606b7430SWing Li 	return rc;
172532ed618SSoby Mathew }
173532ed618SSoby Mathew 
174532ed618SSoby Mathew 
psci_system_suspend(uintptr_t entrypoint,u_register_t context_id)175532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
176532ed618SSoby Mathew {
177532ed618SSoby Mathew 	int rc;
178532ed618SSoby Mathew 	psci_power_state_t state_info;
1793b802105SBoyan Karatotev 	unsigned int cpu_idx = plat_my_core_pos();
180ef738d19SManish Pandey 	entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info);
181532ed618SSoby Mathew 
182532ed618SSoby Mathew 	/* Check if the current CPU is the last ON CPU in the system */
183c7b0a28dSMaheedhar Bollapalli 	if (!psci_is_last_on_cpu(cpu_idx)) {
184532ed618SSoby Mathew 		return PSCI_E_DENIED;
185c7b0a28dSMaheedhar Bollapalli 	}
186532ed618SSoby Mathew 
187532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
188ef738d19SManish Pandey 	rc = psci_validate_entry_point(ep, entrypoint, context_id);
189c7b0a28dSMaheedhar Bollapalli 	if (rc != PSCI_E_SUCCESS) {
190532ed618SSoby Mathew 		return rc;
191c7b0a28dSMaheedhar Bollapalli 	}
192532ed618SSoby Mathew 
193532ed618SSoby Mathew 	/* Query the psci_power_state for system suspend */
194532ed618SSoby Mathew 	psci_query_sys_suspend_pwrstate(&state_info);
195532ed618SSoby Mathew 
196a4065abdSldts 	/*
197a4065abdSldts 	 * Check if platform allows suspend to Highest power level
198a4065abdSldts 	 * (System level)
199a4065abdSldts 	 */
200c7b0a28dSMaheedhar Bollapalli 	if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) {
201a4065abdSldts 		return PSCI_E_DENIED;
202c7b0a28dSMaheedhar Bollapalli 	}
203532ed618SSoby Mathew 	/* Ensure that the psci_power_state makes sense */
204532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
205532ed618SSoby Mathew 						== PSCI_E_SUCCESS);
2066b7b0f36SAntonio Nino Diaz 	assert(is_local_state_off(
2076b7b0f36SAntonio Nino Diaz 			state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
208532ed618SSoby Mathew 
209532ed618SSoby Mathew 	/*
210532ed618SSoby Mathew 	 * Do what is needed to enter the system suspend state. This function
211532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
212532ed618SSoby Mathew 	 * arrival of an interrupt
213532ed618SSoby Mathew 	 */
2143b802105SBoyan Karatotev 	rc = psci_cpu_suspend_start(cpu_idx,
215532ed618SSoby Mathew 				    PLAT_MAX_PWR_LVL,
216532ed618SSoby Mathew 				    &state_info,
217532ed618SSoby Mathew 				    PSTATE_TYPE_POWERDOWN);
218532ed618SSoby Mathew 
219606b7430SWing Li 	return rc;
220532ed618SSoby Mathew }
221532ed618SSoby Mathew 
psci_cpu_off(void)222532ed618SSoby Mathew int psci_cpu_off(void)
223532ed618SSoby Mathew {
224532ed618SSoby Mathew 	int rc;
225532ed618SSoby Mathew 	unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
226532ed618SSoby Mathew 
227532ed618SSoby Mathew 	/*
228532ed618SSoby Mathew 	 * Do what is needed to power off this CPU and possible higher power
229532ed618SSoby Mathew 	 * levels if it able to do so. Upon success, enter the final wfi
230532ed618SSoby Mathew 	 * which will power down this CPU.
231532ed618SSoby Mathew 	 */
232532ed618SSoby Mathew 	rc = psci_do_cpu_off(target_pwrlvl);
233532ed618SSoby Mathew 
234532ed618SSoby Mathew 	/*
235532ed618SSoby Mathew 	 * The only error cpu_off can return is E_DENIED. So check if that's
236532ed618SSoby Mathew 	 * indeed the case.
237532ed618SSoby Mathew 	 */
238532ed618SSoby Mathew 	assert(rc == PSCI_E_DENIED);
239532ed618SSoby Mathew 
240532ed618SSoby Mathew 	return rc;
241532ed618SSoby Mathew }
242532ed618SSoby Mathew 
psci_affinity_info(u_register_t target_affinity,unsigned int lowest_affinity_level)243532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity,
244532ed618SSoby Mathew 		       unsigned int lowest_affinity_level)
245532ed618SSoby Mathew {
2465b33ad17SDeepika Bhavnani 	unsigned int target_idx;
247532ed618SSoby Mathew 
248e60c1847SManish Pandey 	/* Validate the target affinity */
249c7b0a28dSMaheedhar Bollapalli 	if (!is_valid_mpidr(target_affinity)) {
250e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
251c7b0a28dSMaheedhar Bollapalli 	}
252e60c1847SManish Pandey 
253532ed618SSoby Mathew 	/* We dont support level higher than PSCI_CPU_PWR_LVL */
254c7b0a28dSMaheedhar Bollapalli 	if (lowest_affinity_level > PSCI_CPU_PWR_LVL) {
255532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
256c7b0a28dSMaheedhar Bollapalli 	}
257532ed618SSoby Mathew 	/* Calculate the cpu index of the target */
258e60c1847SManish Pandey 	target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
259532ed618SSoby Mathew 
2608fd307ffSRoberto Vargas 	/*
2618fd307ffSRoberto Vargas 	 * Generic management:
2628fd307ffSRoberto Vargas 	 * Perform cache maintanence ahead of reading the target CPU state to
2638fd307ffSRoberto Vargas 	 * ensure that the data is not stale.
2648fd307ffSRoberto Vargas 	 * There is a theoretical edge case where the cache may contain stale
2658fd307ffSRoberto Vargas 	 * data for the target CPU data - this can occur under the following
2668fd307ffSRoberto Vargas 	 * conditions:
2678fd307ffSRoberto Vargas 	 * - the target CPU is in another cluster from the current
2688fd307ffSRoberto Vargas 	 * - the target CPU was the last CPU to shutdown on its cluster
2698fd307ffSRoberto Vargas 	 * - the cluster was removed from coherency as part of the CPU shutdown
2708fd307ffSRoberto Vargas 	 *
2718fd307ffSRoberto Vargas 	 * In this case the cache maintenace that was performed as part of the
2728fd307ffSRoberto Vargas 	 * target CPUs shutdown was not seen by the current CPU's cluster. And
2738fd307ffSRoberto Vargas 	 * so the cache may contain stale data for the target CPU.
2748fd307ffSRoberto Vargas 	 */
2755b33ad17SDeepika Bhavnani 	flush_cpu_data_by_index(target_idx,
2766b7b0f36SAntonio Nino Diaz 				psci_svc_cpu_data.aff_info_state);
2778fd307ffSRoberto Vargas 
278f6166f7fSMaheedhar Bollapalli 	return (int)psci_get_aff_info_state_by_idx(target_idx);
279532ed618SSoby Mathew }
280532ed618SSoby Mathew 
psci_migrate(u_register_t target_cpu)281532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu)
282532ed618SSoby Mathew {
283532ed618SSoby Mathew 	int rc;
284382ba743SBoyan Karatotev 	u_register_t resident_cpu_mpidr = 0;
285532ed618SSoby Mathew 
286e60c1847SManish Pandey 	/* Validate the target cpu */
287bac32cc4SSaivardhan Thatikonda 	if (!is_valid_mpidr(target_cpu)) {
288e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
289bac32cc4SSaivardhan Thatikonda 	}
290e60c1847SManish Pandey 
291532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
292c7b0a28dSMaheedhar Bollapalli 	if (rc != PSCI_TOS_UP_MIG_CAP) {
293532ed618SSoby Mathew 		return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
294532ed618SSoby Mathew 			  PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
295c7b0a28dSMaheedhar Bollapalli 	}
296532ed618SSoby Mathew 
297532ed618SSoby Mathew 	/*
298532ed618SSoby Mathew 	 * Migrate should only be invoked on the CPU where
299532ed618SSoby Mathew 	 * the Secure OS is resident.
300532ed618SSoby Mathew 	 */
301c7b0a28dSMaheedhar Bollapalli 	if (resident_cpu_mpidr != read_mpidr_el1()) {
302532ed618SSoby Mathew 		return PSCI_E_NOT_PRESENT;
303c7b0a28dSMaheedhar Bollapalli 	}
304532ed618SSoby Mathew 
305532ed618SSoby Mathew 	/* Check the validity of the specified target cpu */
306c7b0a28dSMaheedhar Bollapalli 	if (!is_valid_mpidr(target_cpu)) {
307532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
308c7b0a28dSMaheedhar Bollapalli 	}
309532ed618SSoby Mathew 
3106b7b0f36SAntonio Nino Diaz 	assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
311532ed618SSoby Mathew 
312532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
3136b7b0f36SAntonio Nino Diaz 	assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
314532ed618SSoby Mathew 
315532ed618SSoby Mathew 	return rc;
316532ed618SSoby Mathew }
317532ed618SSoby Mathew 
psci_migrate_info_type(void)318532ed618SSoby Mathew int psci_migrate_info_type(void)
319532ed618SSoby Mathew {
320e8c3fddbSPrasad Kummari 	u_register_t resident_cpu_mpidr = 0;
321532ed618SSoby Mathew 
322532ed618SSoby Mathew 	return psci_spd_migrate_info(&resident_cpu_mpidr);
323532ed618SSoby Mathew }
324532ed618SSoby Mathew 
psci_migrate_info_up_cpu(void)3256b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void)
326532ed618SSoby Mathew {
327382ba743SBoyan Karatotev 	u_register_t resident_cpu_mpidr = 0;
328532ed618SSoby Mathew 	int rc;
329532ed618SSoby Mathew 
330532ed618SSoby Mathew 	/*
331532ed618SSoby Mathew 	 * Return value of this depends upon what
332532ed618SSoby Mathew 	 * psci_spd_migrate_info() returns.
333532ed618SSoby Mathew 	 */
334532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
335f3d9e22aSPrasad Kummari 	if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP)) {
3366b7b0f36SAntonio Nino Diaz 		return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
337f3d9e22aSPrasad Kummari 	}
338532ed618SSoby Mathew 
339532ed618SSoby Mathew 	return resident_cpu_mpidr;
340532ed618SSoby Mathew }
341532ed618SSoby Mathew 
psci_node_hw_state(u_register_t target_cpu,unsigned int power_level)34228d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu,
34328d3d614SJeenu Viswambharan 		       unsigned int power_level)
34428d3d614SJeenu Viswambharan {
34528d3d614SJeenu Viswambharan 	int rc;
34628d3d614SJeenu Viswambharan 
34728d3d614SJeenu Viswambharan 	/* Validate target_cpu */
348f3d9e22aSPrasad Kummari 	if (!is_valid_mpidr(target_cpu)) {
34928d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
350f3d9e22aSPrasad Kummari 	}
35128d3d614SJeenu Viswambharan 
35228d3d614SJeenu Viswambharan 	/* Validate power_level against PLAT_MAX_PWR_LVL */
353f3d9e22aSPrasad Kummari 	if (power_level > PLAT_MAX_PWR_LVL) {
35428d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
355f3d9e22aSPrasad Kummari 	}
35628d3d614SJeenu Viswambharan 
35728d3d614SJeenu Viswambharan 	/*
35828d3d614SJeenu Viswambharan 	 * Dispatch this call to platform to query power controller, and pass on
35928d3d614SJeenu Viswambharan 	 * to the caller what it returns
36028d3d614SJeenu Viswambharan 	 */
3616b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->get_node_hw_state != NULL);
36228d3d614SJeenu Viswambharan 	rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
3636b7b0f36SAntonio Nino Diaz 	assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
3646b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_NOT_SUPPORTED)
3656b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_INVALID_PARAMS));
36628d3d614SJeenu Viswambharan 	return rc;
36728d3d614SJeenu Viswambharan }
36828d3d614SJeenu Viswambharan 
psci_features(unsigned int psci_fid)369532ed618SSoby Mathew int psci_features(unsigned int psci_fid)
370532ed618SSoby Mathew {
371532ed618SSoby Mathew 	unsigned int local_caps = psci_caps;
372532ed618SSoby Mathew 
373c7b0a28dSMaheedhar Bollapalli 	if (psci_fid == SMCCC_VERSION) {
3746eabbb07SDimitris Papastamos 		return PSCI_E_SUCCESS;
375c7b0a28dSMaheedhar Bollapalli 	}
376532ed618SSoby Mathew 	/* Check if it is a 64 bit function */
377c7b0a28dSMaheedhar Bollapalli 	if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) {
378532ed618SSoby Mathew 		local_caps &= PSCI_CAP_64BIT_MASK;
379c7b0a28dSMaheedhar Bollapalli 	}
380532ed618SSoby Mathew 	/* Check for invalid fid */
381532ed618SSoby Mathew 	if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
382c7b0a28dSMaheedhar Bollapalli 			&& is_psci_fid(psci_fid))) {
383532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
384c7b0a28dSMaheedhar Bollapalli 	}
385532ed618SSoby Mathew 
386532ed618SSoby Mathew 	/* Check if the psci fid is supported or not */
387c7b0a28dSMaheedhar Bollapalli 	if ((local_caps & define_psci_cap(psci_fid)) == 0U) {
388532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
389c7b0a28dSMaheedhar Bollapalli 	}
390532ed618SSoby Mathew 	/* Format the feature flags */
3916b7b0f36SAntonio Nino Diaz 	if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
3926b7b0f36SAntonio Nino Diaz 	    (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
3936b7b0f36SAntonio Nino Diaz 		unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
3949a70e69eSWing Li 			(FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
3956b7b0f36SAntonio Nino Diaz 		return (int)ret;
396532ed618SSoby Mathew 	}
397532ed618SSoby Mathew 
398532ed618SSoby Mathew 	/* Return 0 for all other fid's */
399532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
400532ed618SSoby Mathew }
401532ed618SSoby Mathew 
402b88a4416SWing Li #if PSCI_OS_INIT_MODE
psci_set_suspend_mode(unsigned int mode)403b88a4416SWing Li int psci_set_suspend_mode(unsigned int mode)
404b88a4416SWing Li {
405b88a4416SWing Li 	if (psci_suspend_mode == mode) {
406b88a4416SWing Li 		return PSCI_E_SUCCESS;
407b88a4416SWing Li 	}
408b88a4416SWing Li 
4093b802105SBoyan Karatotev 	unsigned int this_core = plat_my_core_pos();
4103b802105SBoyan Karatotev 
411b88a4416SWing Li 	if (mode == PLAT_COORD) {
412b88a4416SWing Li 		/* Check if the current CPU is the last ON CPU in the system */
4133b802105SBoyan Karatotev 		if (!psci_is_last_on_cpu_safe(this_core)) {
414b88a4416SWing Li 			return PSCI_E_DENIED;
415b88a4416SWing Li 		}
416b88a4416SWing Li 	}
417b88a4416SWing Li 
418b88a4416SWing Li 	if (mode == OS_INIT) {
419b88a4416SWing Li 		/*
420b88a4416SWing Li 		 * Check if all CPUs in the system are ON or if the current
421b88a4416SWing Li 		 * CPU is the last ON CPU in the system.
422b88a4416SWing Li 		 */
4233b802105SBoyan Karatotev 		if (!(psci_are_all_cpus_on_safe(this_core) ||
4243b802105SBoyan Karatotev 		      psci_is_last_on_cpu_safe(this_core))) {
425b88a4416SWing Li 			return PSCI_E_DENIED;
426b88a4416SWing Li 		}
427b88a4416SWing Li 	}
428b88a4416SWing Li 
429b88a4416SWing Li 	psci_suspend_mode = mode;
430b88a4416SWing Li 	psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
431b88a4416SWing Li 				sizeof(psci_suspend_mode));
432b88a4416SWing Li 
433b88a4416SWing Li 	return PSCI_E_SUCCESS;
434b88a4416SWing Li }
435b88a4416SWing Li #endif
436b88a4416SWing Li 
437532ed618SSoby Mathew /*******************************************************************************
438532ed618SSoby Mathew  * PSCI top level handler for servicing SMCs.
439532ed618SSoby Mathew  ******************************************************************************/
psci_smc_handler(uint32_t smc_fid,u_register_t x1,u_register_t x2,u_register_t x3,u_register_t x4,void * cookie,void * handle,u_register_t flags)440cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid,
441532ed618SSoby Mathew 			  u_register_t x1,
442532ed618SSoby Mathew 			  u_register_t x2,
443532ed618SSoby Mathew 			  u_register_t x3,
444532ed618SSoby Mathew 			  u_register_t x4,
445532ed618SSoby Mathew 			  void *cookie,
446532ed618SSoby Mathew 			  void *handle,
447532ed618SSoby Mathew 			  u_register_t flags)
448532ed618SSoby Mathew {
449ccec2b98SSigned-off-by: Maheedhar Bollapalli 	(void)x4;
450ccec2b98SSigned-off-by: Maheedhar Bollapalli 	(void)cookie;
451ccec2b98SSigned-off-by: Maheedhar Bollapalli 	(void)handle;
4526b7b0f36SAntonio Nino Diaz 	u_register_t ret;
4536b7b0f36SAntonio Nino Diaz 
454*4e4a8c58SArvind Ram Prakash 	if (!is_caller_non_secure(flags)) {
4556b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
456c7b0a28dSMaheedhar Bollapalli 	}
457532ed618SSoby Mathew 
458532ed618SSoby Mathew 	/* Check the fid against the capabilities */
459c7b0a28dSMaheedhar Bollapalli 	if ((psci_caps & define_psci_cap(smc_fid)) == 0U) {
4606b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
461c7b0a28dSMaheedhar Bollapalli 	}
462532ed618SSoby Mathew 
463532ed618SSoby Mathew 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
464532ed618SSoby Mathew 		/* 32-bit PSCI function, clear top parameter bits */
465532ed618SSoby Mathew 
4666b7b0f36SAntonio Nino Diaz 		uint32_t r1 = (uint32_t)x1;
4676b7b0f36SAntonio Nino Diaz 		uint32_t r2 = (uint32_t)x2;
4686b7b0f36SAntonio Nino Diaz 		uint32_t r3 = (uint32_t)x3;
469532ed618SSoby Mathew 
470532ed618SSoby Mathew 		switch (smc_fid) {
471532ed618SSoby Mathew 		case PSCI_VERSION:
4726b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_version();
4736b7b0f36SAntonio Nino Diaz 			break;
474532ed618SSoby Mathew 
475532ed618SSoby Mathew 		case PSCI_CPU_OFF:
4766b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_off();
4776b7b0f36SAntonio Nino Diaz 			break;
478532ed618SSoby Mathew 
479532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH32:
4806b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
4816b7b0f36SAntonio Nino Diaz 			break;
482532ed618SSoby Mathew 
483532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH32:
4846b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(r1, r2, r3);
4856b7b0f36SAntonio Nino Diaz 			break;
486532ed618SSoby Mathew 
487532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH32:
4886b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_affinity_info(r1, r2);
4896b7b0f36SAntonio Nino Diaz 			break;
490532ed618SSoby Mathew 
491532ed618SSoby Mathew 		case PSCI_MIG_AARCH32:
4926b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(r1);
4936b7b0f36SAntonio Nino Diaz 			break;
494532ed618SSoby Mathew 
495532ed618SSoby Mathew 		case PSCI_MIG_INFO_TYPE:
4966b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate_info_type();
4976b7b0f36SAntonio Nino Diaz 			break;
498532ed618SSoby Mathew 
499532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH32:
5006b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
5016b7b0f36SAntonio Nino Diaz 			break;
502532ed618SSoby Mathew 
50328d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH32:
5046b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(r1, r2);
5056b7b0f36SAntonio Nino Diaz 			break;
50628d3d614SJeenu Viswambharan 
507532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH32:
5086b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(r1, r2);
5096b7b0f36SAntonio Nino Diaz 			break;
510532ed618SSoby Mathew 
511532ed618SSoby Mathew 		case PSCI_SYSTEM_OFF:
512532ed618SSoby Mathew 			psci_system_off();
513532ed618SSoby Mathew 			/* We should never return from psci_system_off() */
5143eacacc0SJonathan Wright 			break;
515532ed618SSoby Mathew 
516532ed618SSoby Mathew 		case PSCI_SYSTEM_RESET:
517532ed618SSoby Mathew 			psci_system_reset();
518532ed618SSoby Mathew 			/* We should never return from psci_system_reset() */
5193eacacc0SJonathan Wright 			break;
520532ed618SSoby Mathew 
521532ed618SSoby Mathew 		case PSCI_FEATURES:
5226b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_features(r1);
5236b7b0f36SAntonio Nino Diaz 			break;
524532ed618SSoby Mathew 
525b88a4416SWing Li #if PSCI_OS_INIT_MODE
526b88a4416SWing Li 		case PSCI_SET_SUSPEND_MODE:
527b88a4416SWing Li 			ret = (u_register_t)psci_set_suspend_mode(r1);
528b88a4416SWing Li 			break;
529b88a4416SWing Li #endif
530b88a4416SWing Li 
531532ed618SSoby Mathew #if ENABLE_PSCI_STAT
532532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH32:
5336b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(r1, r2);
5346b7b0f36SAntonio Nino Diaz 			break;
535532ed618SSoby Mathew 
536532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH32:
5376b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(r1, r2);
5386b7b0f36SAntonio Nino Diaz 			break;
539532ed618SSoby Mathew #endif
540d4c596beSRoberto Vargas 		case PSCI_MEM_PROTECT:
5416b7b0f36SAntonio Nino Diaz 			ret = psci_mem_protect(r1);
5426b7b0f36SAntonio Nino Diaz 			break;
543d4c596beSRoberto Vargas 
544d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH32:
5456b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(r1, r2);
5466b7b0f36SAntonio Nino Diaz 			break;
547532ed618SSoby Mathew 
54836a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH32:
54936a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
5506b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2(r1, r2);
5516b7b0f36SAntonio Nino Diaz 			break;
55236a8f8fdSRoberto Vargas 
553532ed618SSoby Mathew 		default:
5546b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
5556b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
556532ed618SSoby Mathew 			break;
557532ed618SSoby Mathew 		}
558532ed618SSoby Mathew 	} else {
559532ed618SSoby Mathew 		/* 64-bit PSCI function */
560532ed618SSoby Mathew 
561532ed618SSoby Mathew 		switch (smc_fid) {
562532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH64:
5636b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5646b7b0f36SAntonio Nino Diaz 				psci_cpu_suspend((unsigned int)x1, x2, x3);
5656b7b0f36SAntonio Nino Diaz 			break;
566532ed618SSoby Mathew 
567532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH64:
5686b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(x1, x2, x3);
5696b7b0f36SAntonio Nino Diaz 			break;
570532ed618SSoby Mathew 
571532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH64:
5726b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5736b7b0f36SAntonio Nino Diaz 				psci_affinity_info(x1, (unsigned int)x2);
5746b7b0f36SAntonio Nino Diaz 			break;
575532ed618SSoby Mathew 
576532ed618SSoby Mathew 		case PSCI_MIG_AARCH64:
5776b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(x1);
5786b7b0f36SAntonio Nino Diaz 			break;
579532ed618SSoby Mathew 
580532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH64:
5816b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
5826b7b0f36SAntonio Nino Diaz 			break;
583532ed618SSoby Mathew 
58428d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH64:
5856b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(
5866b7b0f36SAntonio Nino Diaz 					x1, (unsigned int) x2);
5876b7b0f36SAntonio Nino Diaz 			break;
58828d3d614SJeenu Viswambharan 
589532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH64:
5906b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(x1, x2);
5916b7b0f36SAntonio Nino Diaz 			break;
592532ed618SSoby Mathew 
593532ed618SSoby Mathew #if ENABLE_PSCI_STAT
594532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH64:
5956b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(x1, (unsigned int) x2);
5966b7b0f36SAntonio Nino Diaz 			break;
597532ed618SSoby Mathew 
598532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH64:
5996b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(x1, (unsigned int) x2);
6006b7b0f36SAntonio Nino Diaz 			break;
601532ed618SSoby Mathew #endif
602532ed618SSoby Mathew 
603d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH64:
6046b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(x1, x2);
6056b7b0f36SAntonio Nino Diaz 			break;
606d4c596beSRoberto Vargas 
60736a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH64:
60836a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
6096b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2((uint32_t) x1, x2);
6106b7b0f36SAntonio Nino Diaz 			break;
611d4c596beSRoberto Vargas 
612532ed618SSoby Mathew 		default:
6136b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
6146b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
615532ed618SSoby Mathew 			break;
616532ed618SSoby Mathew 		}
617532ed618SSoby Mathew 	}
618532ed618SSoby Mathew 
6196b7b0f36SAntonio Nino Diaz 	return ret;
620532ed618SSoby Mathew }
621