xref: /rk3399_ARM-atf/lib/psci/psci_main.c (revision 6b7b0f368680ce657390d0a046871d880d704b2d)
1532ed618SSoby Mathew /*
26eabbb07SDimitris Papastamos  * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7532ed618SSoby Mathew #include <arch.h>
8532ed618SSoby Mathew #include <arch_helpers.h>
96eabbb07SDimitris Papastamos #include <arm_arch_svc.h>
10532ed618SSoby Mathew #include <assert.h>
11532ed618SSoby Mathew #include <debug.h>
12532ed618SSoby Mathew #include <platform.h>
13872be88aSdp-arm #include <pmf.h>
14872be88aSdp-arm #include <runtime_instr.h>
15085e80ecSAntonio Nino Diaz #include <smccc.h>
16532ed618SSoby Mathew #include <string.h>
17532ed618SSoby Mathew #include "psci_private.h"
18532ed618SSoby Mathew 
19532ed618SSoby Mathew /*******************************************************************************
20532ed618SSoby Mathew  * PSCI frontend api for servicing SMCs. Described in the PSCI spec.
21532ed618SSoby Mathew  ******************************************************************************/
22532ed618SSoby Mathew int psci_cpu_on(u_register_t target_cpu,
23532ed618SSoby Mathew 		uintptr_t entrypoint,
24532ed618SSoby Mathew 		u_register_t context_id)
25532ed618SSoby Mathew 
26532ed618SSoby Mathew {
27532ed618SSoby Mathew 	int rc;
28532ed618SSoby Mathew 	entry_point_info_t ep;
29532ed618SSoby Mathew 
30532ed618SSoby Mathew 	/* Determine if the cpu exists of not */
31532ed618SSoby Mathew 	rc = psci_validate_mpidr(target_cpu);
32532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
33532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
34532ed618SSoby Mathew 
35532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
36532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
37532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
38532ed618SSoby Mathew 		return rc;
39532ed618SSoby Mathew 
40532ed618SSoby Mathew 	/*
41532ed618SSoby Mathew 	 * To turn this cpu on, specify which power
42532ed618SSoby Mathew 	 * levels need to be turned on
43532ed618SSoby Mathew 	 */
44532ed618SSoby Mathew 	return psci_cpu_on_start(target_cpu, &ep);
45532ed618SSoby Mathew }
46532ed618SSoby Mathew 
47532ed618SSoby Mathew unsigned int psci_version(void)
48532ed618SSoby Mathew {
49532ed618SSoby Mathew 	return PSCI_MAJOR_VER | PSCI_MINOR_VER;
50532ed618SSoby Mathew }
51532ed618SSoby Mathew 
52532ed618SSoby Mathew int psci_cpu_suspend(unsigned int power_state,
53532ed618SSoby Mathew 		     uintptr_t entrypoint,
54532ed618SSoby Mathew 		     u_register_t context_id)
55532ed618SSoby Mathew {
56532ed618SSoby Mathew 	int rc;
57532ed618SSoby Mathew 	unsigned int target_pwrlvl, is_power_down_state;
58532ed618SSoby Mathew 	entry_point_info_t ep;
59532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
60532ed618SSoby Mathew 	plat_local_state_t cpu_pd_state;
61532ed618SSoby Mathew 
62532ed618SSoby Mathew 	/* Validate the power_state parameter */
63532ed618SSoby Mathew 	rc = psci_validate_power_state(power_state, &state_info);
64532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS) {
65532ed618SSoby Mathew 		assert(rc == PSCI_E_INVALID_PARAMS);
66532ed618SSoby Mathew 		return rc;
67532ed618SSoby Mathew 	}
68532ed618SSoby Mathew 
69532ed618SSoby Mathew 	/*
70532ed618SSoby Mathew 	 * Get the value of the state type bit from the power state parameter.
71532ed618SSoby Mathew 	 */
72532ed618SSoby Mathew 	is_power_down_state = psci_get_pstate_type(power_state);
73532ed618SSoby Mathew 
74532ed618SSoby Mathew 	/* Sanity check the requested suspend levels */
75532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, is_power_down_state)
76532ed618SSoby Mathew 			== PSCI_E_SUCCESS);
77532ed618SSoby Mathew 
78532ed618SSoby Mathew 	target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
79a1c3faa6SSandrine Bailleux 	if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
80a1c3faa6SSandrine Bailleux 		ERROR("Invalid target power level for suspend operation\n");
81a1c3faa6SSandrine Bailleux 		panic();
82a1c3faa6SSandrine Bailleux 	}
83532ed618SSoby Mathew 
84532ed618SSoby Mathew 	/* Fast path for CPU standby.*/
85*6b7b0f36SAntonio Nino Diaz 	if (is_cpu_standby_req(is_power_down_state, target_pwrlvl) != 0) {
86*6b7b0f36SAntonio Nino Diaz 		if  (psci_plat_pm_ops->cpu_standby == NULL)
87532ed618SSoby Mathew 			return PSCI_E_INVALID_PARAMS;
88532ed618SSoby Mathew 
89532ed618SSoby Mathew 		/*
90532ed618SSoby Mathew 		 * Set the state of the CPU power domain to the platform
91532ed618SSoby Mathew 		 * specific retention state and enter the standby state.
92532ed618SSoby Mathew 		 */
93532ed618SSoby Mathew 		cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
94532ed618SSoby Mathew 		psci_set_cpu_local_state(cpu_pd_state);
95532ed618SSoby Mathew 
96532ed618SSoby Mathew #if ENABLE_PSCI_STAT
9704c1db1eSdp-arm 		plat_psci_stat_accounting_start(&state_info);
98532ed618SSoby Mathew #endif
99532ed618SSoby Mathew 
100872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
101872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
102872be88aSdp-arm 		    RT_INSTR_ENTER_HW_LOW_PWR,
103872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
104872be88aSdp-arm #endif
105872be88aSdp-arm 
106532ed618SSoby Mathew 		psci_plat_pm_ops->cpu_standby(cpu_pd_state);
107532ed618SSoby Mathew 
108532ed618SSoby Mathew 		/* Upon exit from standby, set the state back to RUN. */
109532ed618SSoby Mathew 		psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
110532ed618SSoby Mathew 
111872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
112872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
113872be88aSdp-arm 		    RT_INSTR_EXIT_HW_LOW_PWR,
114872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
115872be88aSdp-arm #endif
116872be88aSdp-arm 
117532ed618SSoby Mathew #if ENABLE_PSCI_STAT
11804c1db1eSdp-arm 		plat_psci_stat_accounting_stop(&state_info);
119532ed618SSoby Mathew 
120532ed618SSoby Mathew 		/* Update PSCI stats */
12104c1db1eSdp-arm 		psci_stats_update_pwr_up(PSCI_CPU_PWR_LVL, &state_info);
122532ed618SSoby Mathew #endif
123532ed618SSoby Mathew 
124532ed618SSoby Mathew 		return PSCI_E_SUCCESS;
125532ed618SSoby Mathew 	}
126532ed618SSoby Mathew 
127532ed618SSoby Mathew 	/*
128532ed618SSoby Mathew 	 * If a power down state has been requested, we need to verify entry
129532ed618SSoby Mathew 	 * point and program entry information.
130532ed618SSoby Mathew 	 */
131*6b7b0f36SAntonio Nino Diaz 	if (is_power_down_state != 0U) {
132532ed618SSoby Mathew 		rc = psci_validate_entry_point(&ep, entrypoint, context_id);
133532ed618SSoby Mathew 		if (rc != PSCI_E_SUCCESS)
134532ed618SSoby Mathew 			return rc;
135532ed618SSoby Mathew 	}
136532ed618SSoby Mathew 
137532ed618SSoby Mathew 	/*
138532ed618SSoby Mathew 	 * Do what is needed to enter the power down state. Upon success,
139532ed618SSoby Mathew 	 * enter the final wfi which will power down this CPU. This function
140532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
141532ed618SSoby Mathew 	 * arrival of an interrupt
142532ed618SSoby Mathew 	 */
143532ed618SSoby Mathew 	psci_cpu_suspend_start(&ep,
144532ed618SSoby Mathew 			    target_pwrlvl,
145532ed618SSoby Mathew 			    &state_info,
146532ed618SSoby Mathew 			    is_power_down_state);
147532ed618SSoby Mathew 
148532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
149532ed618SSoby Mathew }
150532ed618SSoby Mathew 
151532ed618SSoby Mathew 
152532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
153532ed618SSoby Mathew {
154532ed618SSoby Mathew 	int rc;
155532ed618SSoby Mathew 	psci_power_state_t state_info;
156532ed618SSoby Mathew 	entry_point_info_t ep;
157532ed618SSoby Mathew 
158532ed618SSoby Mathew 	/* Check if the current CPU is the last ON CPU in the system */
159*6b7b0f36SAntonio Nino Diaz 	if (psci_is_last_on_cpu() == 0U)
160532ed618SSoby Mathew 		return PSCI_E_DENIED;
161532ed618SSoby Mathew 
162532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
163532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
164532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
165532ed618SSoby Mathew 		return rc;
166532ed618SSoby Mathew 
167532ed618SSoby Mathew 	/* Query the psci_power_state for system suspend */
168532ed618SSoby Mathew 	psci_query_sys_suspend_pwrstate(&state_info);
169532ed618SSoby Mathew 
170532ed618SSoby Mathew 	/* Ensure that the psci_power_state makes sense */
171532ed618SSoby Mathew 	assert(psci_find_target_suspend_lvl(&state_info) == PLAT_MAX_PWR_LVL);
172532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
173532ed618SSoby Mathew 						== PSCI_E_SUCCESS);
174*6b7b0f36SAntonio Nino Diaz 	assert(is_local_state_off(
175*6b7b0f36SAntonio Nino Diaz 			state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
176532ed618SSoby Mathew 
177532ed618SSoby Mathew 	/*
178532ed618SSoby Mathew 	 * Do what is needed to enter the system suspend state. This function
179532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
180532ed618SSoby Mathew 	 * arrival of an interrupt
181532ed618SSoby Mathew 	 */
182532ed618SSoby Mathew 	psci_cpu_suspend_start(&ep,
183532ed618SSoby Mathew 			    PLAT_MAX_PWR_LVL,
184532ed618SSoby Mathew 			    &state_info,
185532ed618SSoby Mathew 			    PSTATE_TYPE_POWERDOWN);
186532ed618SSoby Mathew 
187532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
188532ed618SSoby Mathew }
189532ed618SSoby Mathew 
190532ed618SSoby Mathew int psci_cpu_off(void)
191532ed618SSoby Mathew {
192532ed618SSoby Mathew 	int rc;
193532ed618SSoby Mathew 	unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
194532ed618SSoby Mathew 
195532ed618SSoby Mathew 	/*
196532ed618SSoby Mathew 	 * Do what is needed to power off this CPU and possible higher power
197532ed618SSoby Mathew 	 * levels if it able to do so. Upon success, enter the final wfi
198532ed618SSoby Mathew 	 * which will power down this CPU.
199532ed618SSoby Mathew 	 */
200532ed618SSoby Mathew 	rc = psci_do_cpu_off(target_pwrlvl);
201532ed618SSoby Mathew 
202532ed618SSoby Mathew 	/*
203532ed618SSoby Mathew 	 * The only error cpu_off can return is E_DENIED. So check if that's
204532ed618SSoby Mathew 	 * indeed the case.
205532ed618SSoby Mathew 	 */
206532ed618SSoby Mathew 	assert(rc == PSCI_E_DENIED);
207532ed618SSoby Mathew 
208532ed618SSoby Mathew 	return rc;
209532ed618SSoby Mathew }
210532ed618SSoby Mathew 
211532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity,
212532ed618SSoby Mathew 		       unsigned int lowest_affinity_level)
213532ed618SSoby Mathew {
2146311f63dSVarun Wadekar 	int target_idx;
215532ed618SSoby Mathew 
216532ed618SSoby Mathew 	/* We dont support level higher than PSCI_CPU_PWR_LVL */
217532ed618SSoby Mathew 	if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
218532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
219532ed618SSoby Mathew 
220532ed618SSoby Mathew 	/* Calculate the cpu index of the target */
221532ed618SSoby Mathew 	target_idx = plat_core_pos_by_mpidr(target_affinity);
222532ed618SSoby Mathew 	if (target_idx == -1)
223532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
224532ed618SSoby Mathew 
2258fd307ffSRoberto Vargas 	/*
2268fd307ffSRoberto Vargas 	 * Generic management:
2278fd307ffSRoberto Vargas 	 * Perform cache maintanence ahead of reading the target CPU state to
2288fd307ffSRoberto Vargas 	 * ensure that the data is not stale.
2298fd307ffSRoberto Vargas 	 * There is a theoretical edge case where the cache may contain stale
2308fd307ffSRoberto Vargas 	 * data for the target CPU data - this can occur under the following
2318fd307ffSRoberto Vargas 	 * conditions:
2328fd307ffSRoberto Vargas 	 * - the target CPU is in another cluster from the current
2338fd307ffSRoberto Vargas 	 * - the target CPU was the last CPU to shutdown on its cluster
2348fd307ffSRoberto Vargas 	 * - the cluster was removed from coherency as part of the CPU shutdown
2358fd307ffSRoberto Vargas 	 *
2368fd307ffSRoberto Vargas 	 * In this case the cache maintenace that was performed as part of the
2378fd307ffSRoberto Vargas 	 * target CPUs shutdown was not seen by the current CPU's cluster. And
2388fd307ffSRoberto Vargas 	 * so the cache may contain stale data for the target CPU.
2398fd307ffSRoberto Vargas 	 */
240*6b7b0f36SAntonio Nino Diaz 	flush_cpu_data_by_index((unsigned int)target_idx,
241*6b7b0f36SAntonio Nino Diaz 				psci_svc_cpu_data.aff_info_state);
2428fd307ffSRoberto Vargas 
243532ed618SSoby Mathew 	return psci_get_aff_info_state_by_idx(target_idx);
244532ed618SSoby Mathew }
245532ed618SSoby Mathew 
246532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu)
247532ed618SSoby Mathew {
248532ed618SSoby Mathew 	int rc;
249532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
250532ed618SSoby Mathew 
251532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
252532ed618SSoby Mathew 	if (rc != PSCI_TOS_UP_MIG_CAP)
253532ed618SSoby Mathew 		return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
254532ed618SSoby Mathew 			  PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
255532ed618SSoby Mathew 
256532ed618SSoby Mathew 	/*
257532ed618SSoby Mathew 	 * Migrate should only be invoked on the CPU where
258532ed618SSoby Mathew 	 * the Secure OS is resident.
259532ed618SSoby Mathew 	 */
260532ed618SSoby Mathew 	if (resident_cpu_mpidr != read_mpidr_el1())
261532ed618SSoby Mathew 		return PSCI_E_NOT_PRESENT;
262532ed618SSoby Mathew 
263532ed618SSoby Mathew 	/* Check the validity of the specified target cpu */
264532ed618SSoby Mathew 	rc = psci_validate_mpidr(target_cpu);
265532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
266532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
267532ed618SSoby Mathew 
268*6b7b0f36SAntonio Nino Diaz 	assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
269532ed618SSoby Mathew 
270532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
271*6b7b0f36SAntonio Nino Diaz 	assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
272532ed618SSoby Mathew 
273532ed618SSoby Mathew 	return rc;
274532ed618SSoby Mathew }
275532ed618SSoby Mathew 
276532ed618SSoby Mathew int psci_migrate_info_type(void)
277532ed618SSoby Mathew {
278532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
279532ed618SSoby Mathew 
280532ed618SSoby Mathew 	return psci_spd_migrate_info(&resident_cpu_mpidr);
281532ed618SSoby Mathew }
282532ed618SSoby Mathew 
283*6b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void)
284532ed618SSoby Mathew {
285532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
286532ed618SSoby Mathew 	int rc;
287532ed618SSoby Mathew 
288532ed618SSoby Mathew 	/*
289532ed618SSoby Mathew 	 * Return value of this depends upon what
290532ed618SSoby Mathew 	 * psci_spd_migrate_info() returns.
291532ed618SSoby Mathew 	 */
292532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
293*6b7b0f36SAntonio Nino Diaz 	if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
294*6b7b0f36SAntonio Nino Diaz 		return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
295532ed618SSoby Mathew 
296532ed618SSoby Mathew 	return resident_cpu_mpidr;
297532ed618SSoby Mathew }
298532ed618SSoby Mathew 
29928d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu,
30028d3d614SJeenu Viswambharan 		       unsigned int power_level)
30128d3d614SJeenu Viswambharan {
30228d3d614SJeenu Viswambharan 	int rc;
30328d3d614SJeenu Viswambharan 
30428d3d614SJeenu Viswambharan 	/* Validate target_cpu */
30528d3d614SJeenu Viswambharan 	rc = psci_validate_mpidr(target_cpu);
30628d3d614SJeenu Viswambharan 	if (rc != PSCI_E_SUCCESS)
30728d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
30828d3d614SJeenu Viswambharan 
30928d3d614SJeenu Viswambharan 	/* Validate power_level against PLAT_MAX_PWR_LVL */
31028d3d614SJeenu Viswambharan 	if (power_level > PLAT_MAX_PWR_LVL)
31128d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
31228d3d614SJeenu Viswambharan 
31328d3d614SJeenu Viswambharan 	/*
31428d3d614SJeenu Viswambharan 	 * Dispatch this call to platform to query power controller, and pass on
31528d3d614SJeenu Viswambharan 	 * to the caller what it returns
31628d3d614SJeenu Viswambharan 	 */
317*6b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->get_node_hw_state != NULL);
31828d3d614SJeenu Viswambharan 	rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
319*6b7b0f36SAntonio Nino Diaz 	assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
320*6b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_NOT_SUPPORTED)
321*6b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_INVALID_PARAMS));
32228d3d614SJeenu Viswambharan 	return rc;
32328d3d614SJeenu Viswambharan }
32428d3d614SJeenu Viswambharan 
325532ed618SSoby Mathew int psci_features(unsigned int psci_fid)
326532ed618SSoby Mathew {
327532ed618SSoby Mathew 	unsigned int local_caps = psci_caps;
328532ed618SSoby Mathew 
3296eabbb07SDimitris Papastamos 	if (psci_fid == SMCCC_VERSION)
3306eabbb07SDimitris Papastamos 		return PSCI_E_SUCCESS;
3316eabbb07SDimitris Papastamos 
332532ed618SSoby Mathew 	/* Check if it is a 64 bit function */
333532ed618SSoby Mathew 	if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64)
334532ed618SSoby Mathew 		local_caps &= PSCI_CAP_64BIT_MASK;
335532ed618SSoby Mathew 
336532ed618SSoby Mathew 	/* Check for invalid fid */
337532ed618SSoby Mathew 	if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
338532ed618SSoby Mathew 			&& is_psci_fid(psci_fid)))
339532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
340532ed618SSoby Mathew 
341532ed618SSoby Mathew 
342532ed618SSoby Mathew 	/* Check if the psci fid is supported or not */
343*6b7b0f36SAntonio Nino Diaz 	if ((local_caps & define_psci_cap(psci_fid)) == 0U)
344532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
345532ed618SSoby Mathew 
346532ed618SSoby Mathew 	/* Format the feature flags */
347*6b7b0f36SAntonio Nino Diaz 	if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
348*6b7b0f36SAntonio Nino Diaz 	    (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
349532ed618SSoby Mathew 		/*
350532ed618SSoby Mathew 		 * The trusted firmware does not support OS Initiated Mode.
351532ed618SSoby Mathew 		 */
352*6b7b0f36SAntonio Nino Diaz 		unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
353*6b7b0f36SAntonio Nino Diaz 			(((FF_SUPPORTS_OS_INIT_MODE == 1U) ? 0U : 1U)
354*6b7b0f36SAntonio Nino Diaz 				<< FF_MODE_SUPPORT_SHIFT));
355*6b7b0f36SAntonio Nino Diaz 		return (int) ret;
356532ed618SSoby Mathew 	}
357532ed618SSoby Mathew 
358532ed618SSoby Mathew 	/* Return 0 for all other fid's */
359532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
360532ed618SSoby Mathew }
361532ed618SSoby Mathew 
362532ed618SSoby Mathew /*******************************************************************************
363532ed618SSoby Mathew  * PSCI top level handler for servicing SMCs.
364532ed618SSoby Mathew  ******************************************************************************/
365cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid,
366532ed618SSoby Mathew 			  u_register_t x1,
367532ed618SSoby Mathew 			  u_register_t x2,
368532ed618SSoby Mathew 			  u_register_t x3,
369532ed618SSoby Mathew 			  u_register_t x4,
370532ed618SSoby Mathew 			  void *cookie,
371532ed618SSoby Mathew 			  void *handle,
372532ed618SSoby Mathew 			  u_register_t flags)
373532ed618SSoby Mathew {
374*6b7b0f36SAntonio Nino Diaz 	u_register_t ret;
375*6b7b0f36SAntonio Nino Diaz 
376532ed618SSoby Mathew 	if (is_caller_secure(flags))
377*6b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
378532ed618SSoby Mathew 
379532ed618SSoby Mathew 	/* Check the fid against the capabilities */
380*6b7b0f36SAntonio Nino Diaz 	if ((psci_caps & define_psci_cap(smc_fid)) == 0U)
381*6b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
382532ed618SSoby Mathew 
383532ed618SSoby Mathew 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
384532ed618SSoby Mathew 		/* 32-bit PSCI function, clear top parameter bits */
385532ed618SSoby Mathew 
386*6b7b0f36SAntonio Nino Diaz 		uint32_t r1 = (uint32_t)x1;
387*6b7b0f36SAntonio Nino Diaz 		uint32_t r2 = (uint32_t)x2;
388*6b7b0f36SAntonio Nino Diaz 		uint32_t r3 = (uint32_t)x3;
389532ed618SSoby Mathew 
390532ed618SSoby Mathew 		switch (smc_fid) {
391532ed618SSoby Mathew 		case PSCI_VERSION:
392*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_version();
393*6b7b0f36SAntonio Nino Diaz 			break;
394532ed618SSoby Mathew 
395532ed618SSoby Mathew 		case PSCI_CPU_OFF:
396*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_off();
397*6b7b0f36SAntonio Nino Diaz 			break;
398532ed618SSoby Mathew 
399532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH32:
400*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
401*6b7b0f36SAntonio Nino Diaz 			break;
402532ed618SSoby Mathew 
403532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH32:
404*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(r1, r2, r3);
405*6b7b0f36SAntonio Nino Diaz 			break;
406532ed618SSoby Mathew 
407532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH32:
408*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_affinity_info(r1, r2);
409*6b7b0f36SAntonio Nino Diaz 			break;
410532ed618SSoby Mathew 
411532ed618SSoby Mathew 		case PSCI_MIG_AARCH32:
412*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(r1);
413*6b7b0f36SAntonio Nino Diaz 			break;
414532ed618SSoby Mathew 
415532ed618SSoby Mathew 		case PSCI_MIG_INFO_TYPE:
416*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate_info_type();
417*6b7b0f36SAntonio Nino Diaz 			break;
418532ed618SSoby Mathew 
419532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH32:
420*6b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
421*6b7b0f36SAntonio Nino Diaz 			break;
422532ed618SSoby Mathew 
42328d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH32:
424*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(r1, r2);
425*6b7b0f36SAntonio Nino Diaz 			break;
42628d3d614SJeenu Viswambharan 
427532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH32:
428*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(r1, r2);
429*6b7b0f36SAntonio Nino Diaz 			break;
430532ed618SSoby Mathew 
431532ed618SSoby Mathew 		case PSCI_SYSTEM_OFF:
432532ed618SSoby Mathew 			psci_system_off();
433532ed618SSoby Mathew 			/* We should never return from psci_system_off() */
4343eacacc0SJonathan Wright 			break;
435532ed618SSoby Mathew 
436532ed618SSoby Mathew 		case PSCI_SYSTEM_RESET:
437532ed618SSoby Mathew 			psci_system_reset();
438532ed618SSoby Mathew 			/* We should never return from psci_system_reset() */
4393eacacc0SJonathan Wright 			break;
440532ed618SSoby Mathew 
441532ed618SSoby Mathew 		case PSCI_FEATURES:
442*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_features(r1);
443*6b7b0f36SAntonio Nino Diaz 			break;
444532ed618SSoby Mathew 
445532ed618SSoby Mathew #if ENABLE_PSCI_STAT
446532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH32:
447*6b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(r1, r2);
448*6b7b0f36SAntonio Nino Diaz 			break;
449532ed618SSoby Mathew 
450532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH32:
451*6b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(r1, r2);
452*6b7b0f36SAntonio Nino Diaz 			break;
453532ed618SSoby Mathew #endif
454d4c596beSRoberto Vargas 		case PSCI_MEM_PROTECT:
455*6b7b0f36SAntonio Nino Diaz 			ret = psci_mem_protect(r1);
456*6b7b0f36SAntonio Nino Diaz 			break;
457d4c596beSRoberto Vargas 
458d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH32:
459*6b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(r1, r2);
460*6b7b0f36SAntonio Nino Diaz 			break;
461532ed618SSoby Mathew 
46236a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH32:
46336a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
464*6b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2(r1, r2);
465*6b7b0f36SAntonio Nino Diaz 			break;
46636a8f8fdSRoberto Vargas 
467532ed618SSoby Mathew 		default:
468*6b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
469*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
470532ed618SSoby Mathew 			break;
471532ed618SSoby Mathew 		}
472532ed618SSoby Mathew 	} else {
473532ed618SSoby Mathew 		/* 64-bit PSCI function */
474532ed618SSoby Mathew 
475532ed618SSoby Mathew 		switch (smc_fid) {
476532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH64:
477*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
478*6b7b0f36SAntonio Nino Diaz 				psci_cpu_suspend((unsigned int)x1, x2, x3);
479*6b7b0f36SAntonio Nino Diaz 			break;
480532ed618SSoby Mathew 
481532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH64:
482*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(x1, x2, x3);
483*6b7b0f36SAntonio Nino Diaz 			break;
484532ed618SSoby Mathew 
485532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH64:
486*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
487*6b7b0f36SAntonio Nino Diaz 				psci_affinity_info(x1, (unsigned int)x2);
488*6b7b0f36SAntonio Nino Diaz 			break;
489532ed618SSoby Mathew 
490532ed618SSoby Mathew 		case PSCI_MIG_AARCH64:
491*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(x1);
492*6b7b0f36SAntonio Nino Diaz 			break;
493532ed618SSoby Mathew 
494532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH64:
495*6b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
496*6b7b0f36SAntonio Nino Diaz 			break;
497532ed618SSoby Mathew 
49828d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH64:
499*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(
500*6b7b0f36SAntonio Nino Diaz 					x1, (unsigned int) x2);
501*6b7b0f36SAntonio Nino Diaz 			break;
50228d3d614SJeenu Viswambharan 
503532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH64:
504*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(x1, x2);
505*6b7b0f36SAntonio Nino Diaz 			break;
506532ed618SSoby Mathew 
507532ed618SSoby Mathew #if ENABLE_PSCI_STAT
508532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH64:
509*6b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(x1, (unsigned int) x2);
510*6b7b0f36SAntonio Nino Diaz 			break;
511532ed618SSoby Mathew 
512532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH64:
513*6b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(x1, (unsigned int) x2);
514*6b7b0f36SAntonio Nino Diaz 			break;
515532ed618SSoby Mathew #endif
516532ed618SSoby Mathew 
517d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH64:
518*6b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(x1, x2);
519*6b7b0f36SAntonio Nino Diaz 			break;
520d4c596beSRoberto Vargas 
52136a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH64:
52236a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
523*6b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2((uint32_t) x1, x2);
524*6b7b0f36SAntonio Nino Diaz 			break;
525d4c596beSRoberto Vargas 
526532ed618SSoby Mathew 		default:
527*6b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
528*6b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
529532ed618SSoby Mathew 			break;
530532ed618SSoby Mathew 		}
531532ed618SSoby Mathew 	}
532532ed618SSoby Mathew 
533*6b7b0f36SAntonio Nino Diaz 	return ret;
534532ed618SSoby Mathew }
535