xref: /rk3399_ARM-atf/lib/psci/psci_main.c (revision 3b8021058a7537d1cf316aa387a553c515f35437)
1532ed618SSoby Mathew /*
2b41b0824SJayanth Dodderi Chidanand  * Copyright (c) 2013-2022, 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>
11532ed618SSoby Mathew #include <arch_helpers.h>
1209d40e0eSAntonio Nino Diaz #include <common/debug.h>
1309d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h>
1409d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h>
1509d40e0eSAntonio Nino Diaz #include <lib/smccc.h>
1609d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1709d40e0eSAntonio Nino Diaz #include <services/arm_arch_svc.h>
1809d40e0eSAntonio Nino Diaz 
19532ed618SSoby Mathew #include "psci_private.h"
20532ed618SSoby Mathew 
21532ed618SSoby Mathew /*******************************************************************************
22532ed618SSoby Mathew  * PSCI frontend api for servicing SMCs. Described in the PSCI spec.
23532ed618SSoby Mathew  ******************************************************************************/
24532ed618SSoby Mathew int psci_cpu_on(u_register_t target_cpu,
25532ed618SSoby Mathew 		uintptr_t entrypoint,
26532ed618SSoby Mathew 		u_register_t context_id)
27532ed618SSoby Mathew 
28532ed618SSoby Mathew {
29532ed618SSoby Mathew 	int rc;
30532ed618SSoby Mathew 	entry_point_info_t ep;
31532ed618SSoby Mathew 
32e60c1847SManish Pandey 	/* Validate the target CPU */
33e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
34532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
35532ed618SSoby Mathew 
36532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
37532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
38532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
39532ed618SSoby Mathew 		return rc;
40532ed618SSoby Mathew 
41532ed618SSoby Mathew 	/*
42532ed618SSoby Mathew 	 * To turn this cpu on, specify which power
43532ed618SSoby Mathew 	 * levels need to be turned on
44532ed618SSoby Mathew 	 */
45532ed618SSoby Mathew 	return psci_cpu_on_start(target_cpu, &ep);
46532ed618SSoby Mathew }
47532ed618SSoby Mathew 
48532ed618SSoby Mathew unsigned int psci_version(void)
49532ed618SSoby Mathew {
50532ed618SSoby Mathew 	return PSCI_MAJOR_VER | PSCI_MINOR_VER;
51532ed618SSoby Mathew }
52532ed618SSoby Mathew 
53532ed618SSoby Mathew int psci_cpu_suspend(unsigned int power_state,
54532ed618SSoby Mathew 		     uintptr_t entrypoint,
55532ed618SSoby Mathew 		     u_register_t context_id)
56532ed618SSoby Mathew {
57532ed618SSoby Mathew 	int rc;
58532ed618SSoby Mathew 	unsigned int target_pwrlvl, is_power_down_state;
59532ed618SSoby Mathew 	entry_point_info_t ep;
60532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
61532ed618SSoby Mathew 	plat_local_state_t cpu_pd_state;
62606b7430SWing Li 	unsigned int cpu_idx = plat_my_core_pos();
63*3b802105SBoyan Karatotev #if PSCI_OS_INIT_MODE
64606b7430SWing Li 	plat_local_state_t prev[PLAT_MAX_PWR_LVL];
65606b7430SWing Li #endif
66532ed618SSoby Mathew 
67532ed618SSoby Mathew 	/* Validate the power_state parameter */
68532ed618SSoby Mathew 	rc = psci_validate_power_state(power_state, &state_info);
69532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS) {
70532ed618SSoby Mathew 		assert(rc == PSCI_E_INVALID_PARAMS);
71532ed618SSoby Mathew 		return rc;
72532ed618SSoby Mathew 	}
73532ed618SSoby Mathew 
74532ed618SSoby Mathew 	/*
75532ed618SSoby Mathew 	 * Get the value of the state type bit from the power state parameter.
76532ed618SSoby Mathew 	 */
77532ed618SSoby Mathew 	is_power_down_state = psci_get_pstate_type(power_state);
78532ed618SSoby Mathew 
79532ed618SSoby Mathew 	/* Sanity check the requested suspend levels */
80532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, is_power_down_state)
81532ed618SSoby Mathew 			== PSCI_E_SUCCESS);
82532ed618SSoby Mathew 
83532ed618SSoby Mathew 	target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
84a1c3faa6SSandrine Bailleux 	if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
85a1c3faa6SSandrine Bailleux 		ERROR("Invalid target power level for suspend operation\n");
86a1c3faa6SSandrine Bailleux 		panic();
87a1c3faa6SSandrine Bailleux 	}
88532ed618SSoby Mathew 
89532ed618SSoby Mathew 	/* Fast path for CPU standby.*/
90362030bfSAntonio Nino Diaz 	if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
916b7b0f36SAntonio Nino Diaz 		if  (psci_plat_pm_ops->cpu_standby == NULL)
92532ed618SSoby Mathew 			return PSCI_E_INVALID_PARAMS;
93532ed618SSoby Mathew 
94532ed618SSoby Mathew 		/*
95532ed618SSoby Mathew 		 * Set the state of the CPU power domain to the platform
96532ed618SSoby Mathew 		 * specific retention state and enter the standby state.
97532ed618SSoby Mathew 		 */
98532ed618SSoby Mathew 		cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
99532ed618SSoby Mathew 		psci_set_cpu_local_state(cpu_pd_state);
100532ed618SSoby Mathew 
101606b7430SWing Li #if PSCI_OS_INIT_MODE
102606b7430SWing Li 		/*
103606b7430SWing Li 		 * If in OS-initiated mode, save a copy of the previous
104606b7430SWing Li 		 * requested local power states and update the new requested
105606b7430SWing Li 		 * local power states for this CPU.
106606b7430SWing Li 		 */
107606b7430SWing Li 		if (psci_suspend_mode == OS_INIT) {
108606b7430SWing Li 			psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx,
109606b7430SWing Li 							 &state_info, prev);
110606b7430SWing Li 		}
111606b7430SWing Li #endif
112606b7430SWing Li 
113532ed618SSoby Mathew #if ENABLE_PSCI_STAT
11404c1db1eSdp-arm 		plat_psci_stat_accounting_start(&state_info);
115532ed618SSoby Mathew #endif
116532ed618SSoby Mathew 
117872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
118872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
119872be88aSdp-arm 		    RT_INSTR_ENTER_HW_LOW_PWR,
120872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
121872be88aSdp-arm #endif
122872be88aSdp-arm 
123532ed618SSoby Mathew 		psci_plat_pm_ops->cpu_standby(cpu_pd_state);
124532ed618SSoby Mathew 
125532ed618SSoby Mathew 		/* Upon exit from standby, set the state back to RUN. */
126532ed618SSoby Mathew 		psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
127532ed618SSoby Mathew 
128606b7430SWing Li #if PSCI_OS_INIT_MODE
129606b7430SWing Li 		/*
130606b7430SWing Li 		 * If in OS-initiated mode, restore the previous requested
131606b7430SWing Li 		 * local power states for this CPU.
132606b7430SWing Li 		 */
133606b7430SWing Li 		if (psci_suspend_mode == OS_INIT) {
134606b7430SWing Li 			psci_restore_req_local_pwr_states(cpu_idx, prev);
135606b7430SWing Li 		}
136606b7430SWing Li #endif
137606b7430SWing Li 
138872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
139872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
140872be88aSdp-arm 		    RT_INSTR_EXIT_HW_LOW_PWR,
141872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
142872be88aSdp-arm #endif
143872be88aSdp-arm 
144532ed618SSoby Mathew #if ENABLE_PSCI_STAT
14504c1db1eSdp-arm 		plat_psci_stat_accounting_stop(&state_info);
146532ed618SSoby Mathew 
147532ed618SSoby Mathew 		/* Update PSCI stats */
148*3b802105SBoyan Karatotev 		psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
149532ed618SSoby Mathew #endif
150532ed618SSoby Mathew 
151532ed618SSoby Mathew 		return PSCI_E_SUCCESS;
152532ed618SSoby Mathew 	}
153532ed618SSoby Mathew 
154532ed618SSoby Mathew 	/*
155532ed618SSoby Mathew 	 * If a power down state has been requested, we need to verify entry
156532ed618SSoby Mathew 	 * point and program entry information.
157532ed618SSoby Mathew 	 */
1586b7b0f36SAntonio Nino Diaz 	if (is_power_down_state != 0U) {
159532ed618SSoby Mathew 		rc = psci_validate_entry_point(&ep, entrypoint, context_id);
160532ed618SSoby Mathew 		if (rc != PSCI_E_SUCCESS)
161532ed618SSoby Mathew 			return rc;
162532ed618SSoby Mathew 	}
163532ed618SSoby Mathew 
164532ed618SSoby Mathew 	/*
165532ed618SSoby Mathew 	 * Do what is needed to enter the power down state. Upon success,
166532ed618SSoby Mathew 	 * enter the final wfi which will power down this CPU. This function
167532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
168532ed618SSoby Mathew 	 * arrival of an interrupt
169532ed618SSoby Mathew 	 */
170*3b802105SBoyan Karatotev 	rc = psci_cpu_suspend_start(cpu_idx,
171*3b802105SBoyan Karatotev 				    &ep,
172532ed618SSoby Mathew 				    target_pwrlvl,
173532ed618SSoby Mathew 				    &state_info,
174532ed618SSoby Mathew 				    is_power_down_state);
175532ed618SSoby Mathew 
176606b7430SWing Li 	return rc;
177532ed618SSoby Mathew }
178532ed618SSoby Mathew 
179532ed618SSoby Mathew 
180532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
181532ed618SSoby Mathew {
182532ed618SSoby Mathew 	int rc;
183532ed618SSoby Mathew 	psci_power_state_t state_info;
184532ed618SSoby Mathew 	entry_point_info_t ep;
185*3b802105SBoyan Karatotev 	unsigned int cpu_idx = plat_my_core_pos();
186532ed618SSoby Mathew 
187532ed618SSoby Mathew 	/* Check if the current CPU is the last ON CPU in the system */
188*3b802105SBoyan Karatotev 	if (!psci_is_last_on_cpu(cpu_idx))
189532ed618SSoby Mathew 		return PSCI_E_DENIED;
190532ed618SSoby Mathew 
191532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
192532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
193532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
194532ed618SSoby Mathew 		return rc;
195532ed618SSoby Mathew 
196532ed618SSoby Mathew 	/* Query the psci_power_state for system suspend */
197532ed618SSoby Mathew 	psci_query_sys_suspend_pwrstate(&state_info);
198532ed618SSoby Mathew 
199a4065abdSldts 	/*
200a4065abdSldts 	 * Check if platform allows suspend to Highest power level
201a4065abdSldts 	 * (System level)
202a4065abdSldts 	 */
203a4065abdSldts 	if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL)
204a4065abdSldts 		return PSCI_E_DENIED;
205a4065abdSldts 
206532ed618SSoby Mathew 	/* Ensure that the psci_power_state makes sense */
207532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
208532ed618SSoby Mathew 						== PSCI_E_SUCCESS);
2096b7b0f36SAntonio Nino Diaz 	assert(is_local_state_off(
2106b7b0f36SAntonio Nino Diaz 			state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
211532ed618SSoby Mathew 
212532ed618SSoby Mathew 	/*
213532ed618SSoby Mathew 	 * Do what is needed to enter the system suspend state. This function
214532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
215532ed618SSoby Mathew 	 * arrival of an interrupt
216532ed618SSoby Mathew 	 */
217*3b802105SBoyan Karatotev 	rc = psci_cpu_suspend_start(cpu_idx,
218*3b802105SBoyan Karatotev 				    &ep,
219532ed618SSoby Mathew 				    PLAT_MAX_PWR_LVL,
220532ed618SSoby Mathew 				    &state_info,
221532ed618SSoby Mathew 				    PSTATE_TYPE_POWERDOWN);
222532ed618SSoby Mathew 
223606b7430SWing Li 	return rc;
224532ed618SSoby Mathew }
225532ed618SSoby Mathew 
226532ed618SSoby Mathew int psci_cpu_off(void)
227532ed618SSoby Mathew {
228532ed618SSoby Mathew 	int rc;
229532ed618SSoby Mathew 	unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
230532ed618SSoby Mathew 
231532ed618SSoby Mathew 	/*
232532ed618SSoby Mathew 	 * Do what is needed to power off this CPU and possible higher power
233532ed618SSoby Mathew 	 * levels if it able to do so. Upon success, enter the final wfi
234532ed618SSoby Mathew 	 * which will power down this CPU.
235532ed618SSoby Mathew 	 */
236532ed618SSoby Mathew 	rc = psci_do_cpu_off(target_pwrlvl);
237532ed618SSoby Mathew 
238532ed618SSoby Mathew 	/*
239532ed618SSoby Mathew 	 * The only error cpu_off can return is E_DENIED. So check if that's
240532ed618SSoby Mathew 	 * indeed the case.
241532ed618SSoby Mathew 	 */
242532ed618SSoby Mathew 	assert(rc == PSCI_E_DENIED);
243532ed618SSoby Mathew 
244532ed618SSoby Mathew 	return rc;
245532ed618SSoby Mathew }
246532ed618SSoby Mathew 
247532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity,
248532ed618SSoby Mathew 		       unsigned int lowest_affinity_level)
249532ed618SSoby Mathew {
2505b33ad17SDeepika Bhavnani 	unsigned int target_idx;
251532ed618SSoby Mathew 
252e60c1847SManish Pandey 	/* Validate the target affinity */
253e60c1847SManish Pandey 	if (!is_valid_mpidr(target_affinity))
254e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
255e60c1847SManish Pandey 
256532ed618SSoby Mathew 	/* We dont support level higher than PSCI_CPU_PWR_LVL */
257532ed618SSoby Mathew 	if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
258532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
259532ed618SSoby Mathew 
260532ed618SSoby Mathew 	/* Calculate the cpu index of the target */
261e60c1847SManish Pandey 	target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
262532ed618SSoby Mathew 
2638fd307ffSRoberto Vargas 	/*
2648fd307ffSRoberto Vargas 	 * Generic management:
2658fd307ffSRoberto Vargas 	 * Perform cache maintanence ahead of reading the target CPU state to
2668fd307ffSRoberto Vargas 	 * ensure that the data is not stale.
2678fd307ffSRoberto Vargas 	 * There is a theoretical edge case where the cache may contain stale
2688fd307ffSRoberto Vargas 	 * data for the target CPU data - this can occur under the following
2698fd307ffSRoberto Vargas 	 * conditions:
2708fd307ffSRoberto Vargas 	 * - the target CPU is in another cluster from the current
2718fd307ffSRoberto Vargas 	 * - the target CPU was the last CPU to shutdown on its cluster
2728fd307ffSRoberto Vargas 	 * - the cluster was removed from coherency as part of the CPU shutdown
2738fd307ffSRoberto Vargas 	 *
2748fd307ffSRoberto Vargas 	 * In this case the cache maintenace that was performed as part of the
2758fd307ffSRoberto Vargas 	 * target CPUs shutdown was not seen by the current CPU's cluster. And
2768fd307ffSRoberto Vargas 	 * so the cache may contain stale data for the target CPU.
2778fd307ffSRoberto Vargas 	 */
2785b33ad17SDeepika Bhavnani 	flush_cpu_data_by_index(target_idx,
2796b7b0f36SAntonio Nino Diaz 				psci_svc_cpu_data.aff_info_state);
2808fd307ffSRoberto Vargas 
281532ed618SSoby Mathew 	return psci_get_aff_info_state_by_idx(target_idx);
282532ed618SSoby Mathew }
283532ed618SSoby Mathew 
284532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu)
285532ed618SSoby Mathew {
286532ed618SSoby Mathew 	int rc;
287532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
288532ed618SSoby Mathew 
289e60c1847SManish Pandey 	/* Validate the target cpu */
290e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
291e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
292e60c1847SManish Pandey 
293532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
294532ed618SSoby Mathew 	if (rc != PSCI_TOS_UP_MIG_CAP)
295532ed618SSoby Mathew 		return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
296532ed618SSoby Mathew 			  PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
297532ed618SSoby Mathew 
298532ed618SSoby Mathew 	/*
299532ed618SSoby Mathew 	 * Migrate should only be invoked on the CPU where
300532ed618SSoby Mathew 	 * the Secure OS is resident.
301532ed618SSoby Mathew 	 */
302532ed618SSoby Mathew 	if (resident_cpu_mpidr != read_mpidr_el1())
303532ed618SSoby Mathew 		return PSCI_E_NOT_PRESENT;
304532ed618SSoby Mathew 
305532ed618SSoby Mathew 	/* Check the validity of the specified target cpu */
306e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
307532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
308532ed618SSoby Mathew 
3096b7b0f36SAntonio Nino Diaz 	assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
310532ed618SSoby Mathew 
311532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
3126b7b0f36SAntonio Nino Diaz 	assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
313532ed618SSoby Mathew 
314532ed618SSoby Mathew 	return rc;
315532ed618SSoby Mathew }
316532ed618SSoby Mathew 
317532ed618SSoby Mathew int psci_migrate_info_type(void)
318532ed618SSoby Mathew {
319532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
320532ed618SSoby Mathew 
321532ed618SSoby Mathew 	return psci_spd_migrate_info(&resident_cpu_mpidr);
322532ed618SSoby Mathew }
323532ed618SSoby Mathew 
3246b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void)
325532ed618SSoby Mathew {
326532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
327532ed618SSoby Mathew 	int rc;
328532ed618SSoby Mathew 
329532ed618SSoby Mathew 	/*
330532ed618SSoby Mathew 	 * Return value of this depends upon what
331532ed618SSoby Mathew 	 * psci_spd_migrate_info() returns.
332532ed618SSoby Mathew 	 */
333532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
3346b7b0f36SAntonio Nino Diaz 	if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
3356b7b0f36SAntonio Nino Diaz 		return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
336532ed618SSoby Mathew 
337532ed618SSoby Mathew 	return resident_cpu_mpidr;
338532ed618SSoby Mathew }
339532ed618SSoby Mathew 
34028d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu,
34128d3d614SJeenu Viswambharan 		       unsigned int power_level)
34228d3d614SJeenu Viswambharan {
34328d3d614SJeenu Viswambharan 	int rc;
34428d3d614SJeenu Viswambharan 
34528d3d614SJeenu Viswambharan 	/* Validate target_cpu */
346e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
34728d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
34828d3d614SJeenu Viswambharan 
34928d3d614SJeenu Viswambharan 	/* Validate power_level against PLAT_MAX_PWR_LVL */
35028d3d614SJeenu Viswambharan 	if (power_level > PLAT_MAX_PWR_LVL)
35128d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
35228d3d614SJeenu Viswambharan 
35328d3d614SJeenu Viswambharan 	/*
35428d3d614SJeenu Viswambharan 	 * Dispatch this call to platform to query power controller, and pass on
35528d3d614SJeenu Viswambharan 	 * to the caller what it returns
35628d3d614SJeenu Viswambharan 	 */
3576b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->get_node_hw_state != NULL);
35828d3d614SJeenu Viswambharan 	rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
3596b7b0f36SAntonio Nino Diaz 	assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
3606b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_NOT_SUPPORTED)
3616b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_INVALID_PARAMS));
36228d3d614SJeenu Viswambharan 	return rc;
36328d3d614SJeenu Viswambharan }
36428d3d614SJeenu Viswambharan 
365532ed618SSoby Mathew int psci_features(unsigned int psci_fid)
366532ed618SSoby Mathew {
367532ed618SSoby Mathew 	unsigned int local_caps = psci_caps;
368532ed618SSoby Mathew 
3696eabbb07SDimitris Papastamos 	if (psci_fid == SMCCC_VERSION)
3706eabbb07SDimitris Papastamos 		return PSCI_E_SUCCESS;
3716eabbb07SDimitris Papastamos 
372532ed618SSoby Mathew 	/* Check if it is a 64 bit function */
373532ed618SSoby Mathew 	if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64)
374532ed618SSoby Mathew 		local_caps &= PSCI_CAP_64BIT_MASK;
375532ed618SSoby Mathew 
376532ed618SSoby Mathew 	/* Check for invalid fid */
377532ed618SSoby Mathew 	if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
378532ed618SSoby Mathew 			&& is_psci_fid(psci_fid)))
379532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
380532ed618SSoby Mathew 
381532ed618SSoby Mathew 
382532ed618SSoby Mathew 	/* Check if the psci fid is supported or not */
3836b7b0f36SAntonio Nino Diaz 	if ((local_caps & define_psci_cap(psci_fid)) == 0U)
384532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
385532ed618SSoby Mathew 
386532ed618SSoby Mathew 	/* Format the feature flags */
3876b7b0f36SAntonio Nino Diaz 	if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
3886b7b0f36SAntonio Nino Diaz 	    (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
3896b7b0f36SAntonio Nino Diaz 		unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
3909a70e69eSWing Li 			(FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
3916b7b0f36SAntonio Nino Diaz 		return (int)ret;
392532ed618SSoby Mathew 	}
393532ed618SSoby Mathew 
394532ed618SSoby Mathew 	/* Return 0 for all other fid's */
395532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
396532ed618SSoby Mathew }
397532ed618SSoby Mathew 
398b88a4416SWing Li #if PSCI_OS_INIT_MODE
399b88a4416SWing Li int psci_set_suspend_mode(unsigned int mode)
400b88a4416SWing Li {
401b88a4416SWing Li 	if (psci_suspend_mode == mode) {
402b88a4416SWing Li 		return PSCI_E_SUCCESS;
403b88a4416SWing Li 	}
404b88a4416SWing Li 
405*3b802105SBoyan Karatotev 	unsigned int this_core = plat_my_core_pos();
406*3b802105SBoyan Karatotev 
407b88a4416SWing Li 	if (mode == PLAT_COORD) {
408b88a4416SWing Li 		/* Check if the current CPU is the last ON CPU in the system */
409*3b802105SBoyan Karatotev 		if (!psci_is_last_on_cpu_safe(this_core)) {
410b88a4416SWing Li 			return PSCI_E_DENIED;
411b88a4416SWing Li 		}
412b88a4416SWing Li 	}
413b88a4416SWing Li 
414b88a4416SWing Li 	if (mode == OS_INIT) {
415b88a4416SWing Li 		/*
416b88a4416SWing Li 		 * Check if all CPUs in the system are ON or if the current
417b88a4416SWing Li 		 * CPU is the last ON CPU in the system.
418b88a4416SWing Li 		 */
419*3b802105SBoyan Karatotev 		if (!(psci_are_all_cpus_on_safe(this_core) ||
420*3b802105SBoyan Karatotev 		      psci_is_last_on_cpu_safe(this_core))) {
421b88a4416SWing Li 			return PSCI_E_DENIED;
422b88a4416SWing Li 		}
423b88a4416SWing Li 	}
424b88a4416SWing Li 
425b88a4416SWing Li 	psci_suspend_mode = mode;
426b88a4416SWing Li 	psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
427b88a4416SWing Li 				sizeof(psci_suspend_mode));
428b88a4416SWing Li 
429b88a4416SWing Li 	return PSCI_E_SUCCESS;
430b88a4416SWing Li }
431b88a4416SWing Li #endif
432b88a4416SWing Li 
433532ed618SSoby Mathew /*******************************************************************************
434532ed618SSoby Mathew  * PSCI top level handler for servicing SMCs.
435532ed618SSoby Mathew  ******************************************************************************/
436cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid,
437532ed618SSoby Mathew 			  u_register_t x1,
438532ed618SSoby Mathew 			  u_register_t x2,
439532ed618SSoby Mathew 			  u_register_t x3,
440532ed618SSoby Mathew 			  u_register_t x4,
441532ed618SSoby Mathew 			  void *cookie,
442532ed618SSoby Mathew 			  void *handle,
443532ed618SSoby Mathew 			  u_register_t flags)
444532ed618SSoby Mathew {
4456b7b0f36SAntonio Nino Diaz 	u_register_t ret;
4466b7b0f36SAntonio Nino Diaz 
447532ed618SSoby Mathew 	if (is_caller_secure(flags))
4486b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
449532ed618SSoby Mathew 
450532ed618SSoby Mathew 	/* Check the fid against the capabilities */
4516b7b0f36SAntonio Nino Diaz 	if ((psci_caps & define_psci_cap(smc_fid)) == 0U)
4526b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
453532ed618SSoby Mathew 
454532ed618SSoby Mathew 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
455532ed618SSoby Mathew 		/* 32-bit PSCI function, clear top parameter bits */
456532ed618SSoby Mathew 
4576b7b0f36SAntonio Nino Diaz 		uint32_t r1 = (uint32_t)x1;
4586b7b0f36SAntonio Nino Diaz 		uint32_t r2 = (uint32_t)x2;
4596b7b0f36SAntonio Nino Diaz 		uint32_t r3 = (uint32_t)x3;
460532ed618SSoby Mathew 
461532ed618SSoby Mathew 		switch (smc_fid) {
462532ed618SSoby Mathew 		case PSCI_VERSION:
4636b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_version();
4646b7b0f36SAntonio Nino Diaz 			break;
465532ed618SSoby Mathew 
466532ed618SSoby Mathew 		case PSCI_CPU_OFF:
4676b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_off();
4686b7b0f36SAntonio Nino Diaz 			break;
469532ed618SSoby Mathew 
470532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH32:
4716b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
4726b7b0f36SAntonio Nino Diaz 			break;
473532ed618SSoby Mathew 
474532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH32:
4756b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(r1, r2, r3);
4766b7b0f36SAntonio Nino Diaz 			break;
477532ed618SSoby Mathew 
478532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH32:
4796b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_affinity_info(r1, r2);
4806b7b0f36SAntonio Nino Diaz 			break;
481532ed618SSoby Mathew 
482532ed618SSoby Mathew 		case PSCI_MIG_AARCH32:
4836b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(r1);
4846b7b0f36SAntonio Nino Diaz 			break;
485532ed618SSoby Mathew 
486532ed618SSoby Mathew 		case PSCI_MIG_INFO_TYPE:
4876b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate_info_type();
4886b7b0f36SAntonio Nino Diaz 			break;
489532ed618SSoby Mathew 
490532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH32:
4916b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
4926b7b0f36SAntonio Nino Diaz 			break;
493532ed618SSoby Mathew 
49428d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH32:
4956b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(r1, r2);
4966b7b0f36SAntonio Nino Diaz 			break;
49728d3d614SJeenu Viswambharan 
498532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH32:
4996b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(r1, r2);
5006b7b0f36SAntonio Nino Diaz 			break;
501532ed618SSoby Mathew 
502532ed618SSoby Mathew 		case PSCI_SYSTEM_OFF:
503532ed618SSoby Mathew 			psci_system_off();
504532ed618SSoby Mathew 			/* We should never return from psci_system_off() */
5053eacacc0SJonathan Wright 			break;
506532ed618SSoby Mathew 
507532ed618SSoby Mathew 		case PSCI_SYSTEM_RESET:
508532ed618SSoby Mathew 			psci_system_reset();
509532ed618SSoby Mathew 			/* We should never return from psci_system_reset() */
5103eacacc0SJonathan Wright 			break;
511532ed618SSoby Mathew 
512532ed618SSoby Mathew 		case PSCI_FEATURES:
5136b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_features(r1);
5146b7b0f36SAntonio Nino Diaz 			break;
515532ed618SSoby Mathew 
516b88a4416SWing Li #if PSCI_OS_INIT_MODE
517b88a4416SWing Li 		case PSCI_SET_SUSPEND_MODE:
518b88a4416SWing Li 			ret = (u_register_t)psci_set_suspend_mode(r1);
519b88a4416SWing Li 			break;
520b88a4416SWing Li #endif
521b88a4416SWing Li 
522532ed618SSoby Mathew #if ENABLE_PSCI_STAT
523532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH32:
5246b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(r1, r2);
5256b7b0f36SAntonio Nino Diaz 			break;
526532ed618SSoby Mathew 
527532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH32:
5286b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(r1, r2);
5296b7b0f36SAntonio Nino Diaz 			break;
530532ed618SSoby Mathew #endif
531d4c596beSRoberto Vargas 		case PSCI_MEM_PROTECT:
5326b7b0f36SAntonio Nino Diaz 			ret = psci_mem_protect(r1);
5336b7b0f36SAntonio Nino Diaz 			break;
534d4c596beSRoberto Vargas 
535d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH32:
5366b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(r1, r2);
5376b7b0f36SAntonio Nino Diaz 			break;
538532ed618SSoby Mathew 
53936a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH32:
54036a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
5416b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2(r1, r2);
5426b7b0f36SAntonio Nino Diaz 			break;
54336a8f8fdSRoberto Vargas 
544532ed618SSoby Mathew 		default:
5456b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
5466b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
547532ed618SSoby Mathew 			break;
548532ed618SSoby Mathew 		}
549532ed618SSoby Mathew 	} else {
550532ed618SSoby Mathew 		/* 64-bit PSCI function */
551532ed618SSoby Mathew 
552532ed618SSoby Mathew 		switch (smc_fid) {
553532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH64:
5546b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5556b7b0f36SAntonio Nino Diaz 				psci_cpu_suspend((unsigned int)x1, x2, x3);
5566b7b0f36SAntonio Nino Diaz 			break;
557532ed618SSoby Mathew 
558532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH64:
5596b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(x1, x2, x3);
5606b7b0f36SAntonio Nino Diaz 			break;
561532ed618SSoby Mathew 
562532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH64:
5636b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5646b7b0f36SAntonio Nino Diaz 				psci_affinity_info(x1, (unsigned int)x2);
5656b7b0f36SAntonio Nino Diaz 			break;
566532ed618SSoby Mathew 
567532ed618SSoby Mathew 		case PSCI_MIG_AARCH64:
5686b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(x1);
5696b7b0f36SAntonio Nino Diaz 			break;
570532ed618SSoby Mathew 
571532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH64:
5726b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
5736b7b0f36SAntonio Nino Diaz 			break;
574532ed618SSoby Mathew 
57528d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH64:
5766b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(
5776b7b0f36SAntonio Nino Diaz 					x1, (unsigned int) x2);
5786b7b0f36SAntonio Nino Diaz 			break;
57928d3d614SJeenu Viswambharan 
580532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH64:
5816b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(x1, x2);
5826b7b0f36SAntonio Nino Diaz 			break;
583532ed618SSoby Mathew 
584532ed618SSoby Mathew #if ENABLE_PSCI_STAT
585532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH64:
5866b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(x1, (unsigned int) x2);
5876b7b0f36SAntonio Nino Diaz 			break;
588532ed618SSoby Mathew 
589532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH64:
5906b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(x1, (unsigned int) x2);
5916b7b0f36SAntonio Nino Diaz 			break;
592532ed618SSoby Mathew #endif
593532ed618SSoby Mathew 
594d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH64:
5956b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(x1, x2);
5966b7b0f36SAntonio Nino Diaz 			break;
597d4c596beSRoberto Vargas 
59836a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH64:
59936a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
6006b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2((uint32_t) x1, x2);
6016b7b0f36SAntonio Nino Diaz 			break;
602d4c596beSRoberto Vargas 
603532ed618SSoby Mathew 		default:
6046b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
6056b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
606532ed618SSoby Mathew 			break;
607532ed618SSoby Mathew 		}
608532ed618SSoby Mathew 	}
609532ed618SSoby Mathew 
6106b7b0f36SAntonio Nino Diaz 	return ret;
611532ed618SSoby Mathew }
612