xref: /rk3399_ARM-atf/lib/psci/psci_main.c (revision e60c18471fc7488cc0bf1dc7eae3b43be77045a4)
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 
32*e60c1847SManish Pandey 	/* Validate the target CPU */
33*e60c1847SManish 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 #if PSCI_OS_INIT_MODE
63606b7430SWing Li 	unsigned int cpu_idx = plat_my_core_pos();
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 */
14804c1db1eSdp-arm 		psci_stats_update_pwr_up(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 	 */
170606b7430SWing Li 	rc = psci_cpu_suspend_start(&ep,
171532ed618SSoby Mathew 				    target_pwrlvl,
172532ed618SSoby Mathew 				    &state_info,
173532ed618SSoby Mathew 				    is_power_down_state);
174532ed618SSoby Mathew 
175606b7430SWing Li 	return rc;
176532ed618SSoby Mathew }
177532ed618SSoby Mathew 
178532ed618SSoby Mathew 
179532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
180532ed618SSoby Mathew {
181532ed618SSoby Mathew 	int rc;
182532ed618SSoby Mathew 	psci_power_state_t state_info;
183532ed618SSoby Mathew 	entry_point_info_t ep;
184532ed618SSoby Mathew 
185532ed618SSoby Mathew 	/* Check if the current CPU is the last ON CPU in the system */
186b41b0824SJayanth Dodderi Chidanand 	if (!psci_is_last_on_cpu())
187532ed618SSoby Mathew 		return PSCI_E_DENIED;
188532ed618SSoby Mathew 
189532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
190532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
191532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
192532ed618SSoby Mathew 		return rc;
193532ed618SSoby Mathew 
194532ed618SSoby Mathew 	/* Query the psci_power_state for system suspend */
195532ed618SSoby Mathew 	psci_query_sys_suspend_pwrstate(&state_info);
196532ed618SSoby Mathew 
197a4065abdSldts 	/*
198a4065abdSldts 	 * Check if platform allows suspend to Highest power level
199a4065abdSldts 	 * (System level)
200a4065abdSldts 	 */
201a4065abdSldts 	if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL)
202a4065abdSldts 		return PSCI_E_DENIED;
203a4065abdSldts 
204532ed618SSoby Mathew 	/* Ensure that the psci_power_state makes sense */
205532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
206532ed618SSoby Mathew 						== PSCI_E_SUCCESS);
2076b7b0f36SAntonio Nino Diaz 	assert(is_local_state_off(
2086b7b0f36SAntonio Nino Diaz 			state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
209532ed618SSoby Mathew 
210532ed618SSoby Mathew 	/*
211532ed618SSoby Mathew 	 * Do what is needed to enter the system suspend state. This function
212532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
213532ed618SSoby Mathew 	 * arrival of an interrupt
214532ed618SSoby Mathew 	 */
215606b7430SWing Li 	rc = psci_cpu_suspend_start(&ep,
216532ed618SSoby Mathew 				    PLAT_MAX_PWR_LVL,
217532ed618SSoby Mathew 				    &state_info,
218532ed618SSoby Mathew 				    PSTATE_TYPE_POWERDOWN);
219532ed618SSoby Mathew 
220606b7430SWing Li 	return rc;
221532ed618SSoby Mathew }
222532ed618SSoby Mathew 
223532ed618SSoby Mathew int psci_cpu_off(void)
224532ed618SSoby Mathew {
225532ed618SSoby Mathew 	int rc;
226532ed618SSoby Mathew 	unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
227532ed618SSoby Mathew 
228532ed618SSoby Mathew 	/*
229532ed618SSoby Mathew 	 * Do what is needed to power off this CPU and possible higher power
230532ed618SSoby Mathew 	 * levels if it able to do so. Upon success, enter the final wfi
231532ed618SSoby Mathew 	 * which will power down this CPU.
232532ed618SSoby Mathew 	 */
233532ed618SSoby Mathew 	rc = psci_do_cpu_off(target_pwrlvl);
234532ed618SSoby Mathew 
235532ed618SSoby Mathew 	/*
236532ed618SSoby Mathew 	 * The only error cpu_off can return is E_DENIED. So check if that's
237532ed618SSoby Mathew 	 * indeed the case.
238532ed618SSoby Mathew 	 */
239532ed618SSoby Mathew 	assert(rc == PSCI_E_DENIED);
240532ed618SSoby Mathew 
241532ed618SSoby Mathew 	return rc;
242532ed618SSoby Mathew }
243532ed618SSoby Mathew 
244532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity,
245532ed618SSoby Mathew 		       unsigned int lowest_affinity_level)
246532ed618SSoby Mathew {
2475b33ad17SDeepika Bhavnani 	unsigned int target_idx;
248532ed618SSoby Mathew 
249*e60c1847SManish Pandey 	/* Validate the target affinity */
250*e60c1847SManish Pandey 	if (!is_valid_mpidr(target_affinity))
251*e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
252*e60c1847SManish Pandey 
253532ed618SSoby Mathew 	/* We dont support level higher than PSCI_CPU_PWR_LVL */
254532ed618SSoby Mathew 	if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
255532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
256532ed618SSoby Mathew 
257532ed618SSoby Mathew 	/* Calculate the cpu index of the target */
258*e60c1847SManish 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 
278532ed618SSoby Mathew 	return psci_get_aff_info_state_by_idx(target_idx);
279532ed618SSoby Mathew }
280532ed618SSoby Mathew 
281532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu)
282532ed618SSoby Mathew {
283532ed618SSoby Mathew 	int rc;
284532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
285532ed618SSoby Mathew 
286*e60c1847SManish Pandey 	/* Validate the target cpu */
287*e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
288*e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
289*e60c1847SManish Pandey 
290532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
291532ed618SSoby Mathew 	if (rc != PSCI_TOS_UP_MIG_CAP)
292532ed618SSoby Mathew 		return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
293532ed618SSoby Mathew 			  PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
294532ed618SSoby Mathew 
295532ed618SSoby Mathew 	/*
296532ed618SSoby Mathew 	 * Migrate should only be invoked on the CPU where
297532ed618SSoby Mathew 	 * the Secure OS is resident.
298532ed618SSoby Mathew 	 */
299532ed618SSoby Mathew 	if (resident_cpu_mpidr != read_mpidr_el1())
300532ed618SSoby Mathew 		return PSCI_E_NOT_PRESENT;
301532ed618SSoby Mathew 
302532ed618SSoby Mathew 	/* Check the validity of the specified target cpu */
303*e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
304532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
305532ed618SSoby Mathew 
3066b7b0f36SAntonio Nino Diaz 	assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
307532ed618SSoby Mathew 
308532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
3096b7b0f36SAntonio Nino Diaz 	assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
310532ed618SSoby Mathew 
311532ed618SSoby Mathew 	return rc;
312532ed618SSoby Mathew }
313532ed618SSoby Mathew 
314532ed618SSoby Mathew int psci_migrate_info_type(void)
315532ed618SSoby Mathew {
316532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
317532ed618SSoby Mathew 
318532ed618SSoby Mathew 	return psci_spd_migrate_info(&resident_cpu_mpidr);
319532ed618SSoby Mathew }
320532ed618SSoby Mathew 
3216b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void)
322532ed618SSoby Mathew {
323532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
324532ed618SSoby Mathew 	int rc;
325532ed618SSoby Mathew 
326532ed618SSoby Mathew 	/*
327532ed618SSoby Mathew 	 * Return value of this depends upon what
328532ed618SSoby Mathew 	 * psci_spd_migrate_info() returns.
329532ed618SSoby Mathew 	 */
330532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
3316b7b0f36SAntonio Nino Diaz 	if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
3326b7b0f36SAntonio Nino Diaz 		return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
333532ed618SSoby Mathew 
334532ed618SSoby Mathew 	return resident_cpu_mpidr;
335532ed618SSoby Mathew }
336532ed618SSoby Mathew 
33728d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu,
33828d3d614SJeenu Viswambharan 		       unsigned int power_level)
33928d3d614SJeenu Viswambharan {
34028d3d614SJeenu Viswambharan 	int rc;
34128d3d614SJeenu Viswambharan 
34228d3d614SJeenu Viswambharan 	/* Validate target_cpu */
343*e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
34428d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
34528d3d614SJeenu Viswambharan 
34628d3d614SJeenu Viswambharan 	/* Validate power_level against PLAT_MAX_PWR_LVL */
34728d3d614SJeenu Viswambharan 	if (power_level > PLAT_MAX_PWR_LVL)
34828d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
34928d3d614SJeenu Viswambharan 
35028d3d614SJeenu Viswambharan 	/*
35128d3d614SJeenu Viswambharan 	 * Dispatch this call to platform to query power controller, and pass on
35228d3d614SJeenu Viswambharan 	 * to the caller what it returns
35328d3d614SJeenu Viswambharan 	 */
3546b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->get_node_hw_state != NULL);
35528d3d614SJeenu Viswambharan 	rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
3566b7b0f36SAntonio Nino Diaz 	assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
3576b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_NOT_SUPPORTED)
3586b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_INVALID_PARAMS));
35928d3d614SJeenu Viswambharan 	return rc;
36028d3d614SJeenu Viswambharan }
36128d3d614SJeenu Viswambharan 
362532ed618SSoby Mathew int psci_features(unsigned int psci_fid)
363532ed618SSoby Mathew {
364532ed618SSoby Mathew 	unsigned int local_caps = psci_caps;
365532ed618SSoby Mathew 
3666eabbb07SDimitris Papastamos 	if (psci_fid == SMCCC_VERSION)
3676eabbb07SDimitris Papastamos 		return PSCI_E_SUCCESS;
3686eabbb07SDimitris Papastamos 
369532ed618SSoby Mathew 	/* Check if it is a 64 bit function */
370532ed618SSoby Mathew 	if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64)
371532ed618SSoby Mathew 		local_caps &= PSCI_CAP_64BIT_MASK;
372532ed618SSoby Mathew 
373532ed618SSoby Mathew 	/* Check for invalid fid */
374532ed618SSoby Mathew 	if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
375532ed618SSoby Mathew 			&& is_psci_fid(psci_fid)))
376532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
377532ed618SSoby Mathew 
378532ed618SSoby Mathew 
379532ed618SSoby Mathew 	/* Check if the psci fid is supported or not */
3806b7b0f36SAntonio Nino Diaz 	if ((local_caps & define_psci_cap(psci_fid)) == 0U)
381532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
382532ed618SSoby Mathew 
383532ed618SSoby Mathew 	/* Format the feature flags */
3846b7b0f36SAntonio Nino Diaz 	if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
3856b7b0f36SAntonio Nino Diaz 	    (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
3866b7b0f36SAntonio Nino Diaz 		unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
3879a70e69eSWing Li 			(FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
3886b7b0f36SAntonio Nino Diaz 		return (int)ret;
389532ed618SSoby Mathew 	}
390532ed618SSoby Mathew 
391532ed618SSoby Mathew 	/* Return 0 for all other fid's */
392532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
393532ed618SSoby Mathew }
394532ed618SSoby Mathew 
395b88a4416SWing Li #if PSCI_OS_INIT_MODE
396b88a4416SWing Li int psci_set_suspend_mode(unsigned int mode)
397b88a4416SWing Li {
398b88a4416SWing Li 	if (psci_suspend_mode == mode) {
399b88a4416SWing Li 		return PSCI_E_SUCCESS;
400b88a4416SWing Li 	}
401b88a4416SWing Li 
402b88a4416SWing Li 	if (mode == PLAT_COORD) {
403b88a4416SWing Li 		/* Check if the current CPU is the last ON CPU in the system */
404b88a4416SWing Li 		if (!psci_is_last_on_cpu_safe()) {
405b88a4416SWing Li 			return PSCI_E_DENIED;
406b88a4416SWing Li 		}
407b88a4416SWing Li 	}
408b88a4416SWing Li 
409b88a4416SWing Li 	if (mode == OS_INIT) {
410b88a4416SWing Li 		/*
411b88a4416SWing Li 		 * Check if all CPUs in the system are ON or if the current
412b88a4416SWing Li 		 * CPU is the last ON CPU in the system.
413b88a4416SWing Li 		 */
414b88a4416SWing Li 		if (!(psci_are_all_cpus_on_safe() ||
415b88a4416SWing Li 		      psci_is_last_on_cpu_safe())) {
416b88a4416SWing Li 			return PSCI_E_DENIED;
417b88a4416SWing Li 		}
418b88a4416SWing Li 	}
419b88a4416SWing Li 
420b88a4416SWing Li 	psci_suspend_mode = mode;
421b88a4416SWing Li 	psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
422b88a4416SWing Li 				sizeof(psci_suspend_mode));
423b88a4416SWing Li 
424b88a4416SWing Li 	return PSCI_E_SUCCESS;
425b88a4416SWing Li }
426b88a4416SWing Li #endif
427b88a4416SWing Li 
428532ed618SSoby Mathew /*******************************************************************************
429532ed618SSoby Mathew  * PSCI top level handler for servicing SMCs.
430532ed618SSoby Mathew  ******************************************************************************/
431cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid,
432532ed618SSoby Mathew 			  u_register_t x1,
433532ed618SSoby Mathew 			  u_register_t x2,
434532ed618SSoby Mathew 			  u_register_t x3,
435532ed618SSoby Mathew 			  u_register_t x4,
436532ed618SSoby Mathew 			  void *cookie,
437532ed618SSoby Mathew 			  void *handle,
438532ed618SSoby Mathew 			  u_register_t flags)
439532ed618SSoby Mathew {
4406b7b0f36SAntonio Nino Diaz 	u_register_t ret;
4416b7b0f36SAntonio Nino Diaz 
442532ed618SSoby Mathew 	if (is_caller_secure(flags))
4436b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
444532ed618SSoby Mathew 
445532ed618SSoby Mathew 	/* Check the fid against the capabilities */
4466b7b0f36SAntonio Nino Diaz 	if ((psci_caps & define_psci_cap(smc_fid)) == 0U)
4476b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
448532ed618SSoby Mathew 
449532ed618SSoby Mathew 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
450532ed618SSoby Mathew 		/* 32-bit PSCI function, clear top parameter bits */
451532ed618SSoby Mathew 
4526b7b0f36SAntonio Nino Diaz 		uint32_t r1 = (uint32_t)x1;
4536b7b0f36SAntonio Nino Diaz 		uint32_t r2 = (uint32_t)x2;
4546b7b0f36SAntonio Nino Diaz 		uint32_t r3 = (uint32_t)x3;
455532ed618SSoby Mathew 
456532ed618SSoby Mathew 		switch (smc_fid) {
457532ed618SSoby Mathew 		case PSCI_VERSION:
4586b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_version();
4596b7b0f36SAntonio Nino Diaz 			break;
460532ed618SSoby Mathew 
461532ed618SSoby Mathew 		case PSCI_CPU_OFF:
4626b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_off();
4636b7b0f36SAntonio Nino Diaz 			break;
464532ed618SSoby Mathew 
465532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH32:
4666b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
4676b7b0f36SAntonio Nino Diaz 			break;
468532ed618SSoby Mathew 
469532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH32:
4706b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(r1, r2, r3);
4716b7b0f36SAntonio Nino Diaz 			break;
472532ed618SSoby Mathew 
473532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH32:
4746b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_affinity_info(r1, r2);
4756b7b0f36SAntonio Nino Diaz 			break;
476532ed618SSoby Mathew 
477532ed618SSoby Mathew 		case PSCI_MIG_AARCH32:
4786b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(r1);
4796b7b0f36SAntonio Nino Diaz 			break;
480532ed618SSoby Mathew 
481532ed618SSoby Mathew 		case PSCI_MIG_INFO_TYPE:
4826b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate_info_type();
4836b7b0f36SAntonio Nino Diaz 			break;
484532ed618SSoby Mathew 
485532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH32:
4866b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
4876b7b0f36SAntonio Nino Diaz 			break;
488532ed618SSoby Mathew 
48928d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH32:
4906b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(r1, r2);
4916b7b0f36SAntonio Nino Diaz 			break;
49228d3d614SJeenu Viswambharan 
493532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH32:
4946b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(r1, r2);
4956b7b0f36SAntonio Nino Diaz 			break;
496532ed618SSoby Mathew 
497532ed618SSoby Mathew 		case PSCI_SYSTEM_OFF:
498532ed618SSoby Mathew 			psci_system_off();
499532ed618SSoby Mathew 			/* We should never return from psci_system_off() */
5003eacacc0SJonathan Wright 			break;
501532ed618SSoby Mathew 
502532ed618SSoby Mathew 		case PSCI_SYSTEM_RESET:
503532ed618SSoby Mathew 			psci_system_reset();
504532ed618SSoby Mathew 			/* We should never return from psci_system_reset() */
5053eacacc0SJonathan Wright 			break;
506532ed618SSoby Mathew 
507532ed618SSoby Mathew 		case PSCI_FEATURES:
5086b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_features(r1);
5096b7b0f36SAntonio Nino Diaz 			break;
510532ed618SSoby Mathew 
511b88a4416SWing Li #if PSCI_OS_INIT_MODE
512b88a4416SWing Li 		case PSCI_SET_SUSPEND_MODE:
513b88a4416SWing Li 			ret = (u_register_t)psci_set_suspend_mode(r1);
514b88a4416SWing Li 			break;
515b88a4416SWing Li #endif
516b88a4416SWing Li 
517532ed618SSoby Mathew #if ENABLE_PSCI_STAT
518532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH32:
5196b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(r1, r2);
5206b7b0f36SAntonio Nino Diaz 			break;
521532ed618SSoby Mathew 
522532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH32:
5236b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(r1, r2);
5246b7b0f36SAntonio Nino Diaz 			break;
525532ed618SSoby Mathew #endif
526d4c596beSRoberto Vargas 		case PSCI_MEM_PROTECT:
5276b7b0f36SAntonio Nino Diaz 			ret = psci_mem_protect(r1);
5286b7b0f36SAntonio Nino Diaz 			break;
529d4c596beSRoberto Vargas 
530d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH32:
5316b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(r1, r2);
5326b7b0f36SAntonio Nino Diaz 			break;
533532ed618SSoby Mathew 
53436a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH32:
53536a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
5366b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2(r1, r2);
5376b7b0f36SAntonio Nino Diaz 			break;
53836a8f8fdSRoberto Vargas 
539532ed618SSoby Mathew 		default:
5406b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
5416b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
542532ed618SSoby Mathew 			break;
543532ed618SSoby Mathew 		}
544532ed618SSoby Mathew 	} else {
545532ed618SSoby Mathew 		/* 64-bit PSCI function */
546532ed618SSoby Mathew 
547532ed618SSoby Mathew 		switch (smc_fid) {
548532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH64:
5496b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5506b7b0f36SAntonio Nino Diaz 				psci_cpu_suspend((unsigned int)x1, x2, x3);
5516b7b0f36SAntonio Nino Diaz 			break;
552532ed618SSoby Mathew 
553532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH64:
5546b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(x1, x2, x3);
5556b7b0f36SAntonio Nino Diaz 			break;
556532ed618SSoby Mathew 
557532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH64:
5586b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5596b7b0f36SAntonio Nino Diaz 				psci_affinity_info(x1, (unsigned int)x2);
5606b7b0f36SAntonio Nino Diaz 			break;
561532ed618SSoby Mathew 
562532ed618SSoby Mathew 		case PSCI_MIG_AARCH64:
5636b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(x1);
5646b7b0f36SAntonio Nino Diaz 			break;
565532ed618SSoby Mathew 
566532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH64:
5676b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
5686b7b0f36SAntonio Nino Diaz 			break;
569532ed618SSoby Mathew 
57028d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH64:
5716b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(
5726b7b0f36SAntonio Nino Diaz 					x1, (unsigned int) x2);
5736b7b0f36SAntonio Nino Diaz 			break;
57428d3d614SJeenu Viswambharan 
575532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH64:
5766b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(x1, x2);
5776b7b0f36SAntonio Nino Diaz 			break;
578532ed618SSoby Mathew 
579532ed618SSoby Mathew #if ENABLE_PSCI_STAT
580532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH64:
5816b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(x1, (unsigned int) x2);
5826b7b0f36SAntonio Nino Diaz 			break;
583532ed618SSoby Mathew 
584532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH64:
5856b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(x1, (unsigned int) x2);
5866b7b0f36SAntonio Nino Diaz 			break;
587532ed618SSoby Mathew #endif
588532ed618SSoby Mathew 
589d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH64:
5906b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(x1, x2);
5916b7b0f36SAntonio Nino Diaz 			break;
592d4c596beSRoberto Vargas 
59336a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH64:
59436a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
5956b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2((uint32_t) x1, x2);
5966b7b0f36SAntonio Nino Diaz 			break;
597d4c596beSRoberto Vargas 
598532ed618SSoby Mathew 		default:
5996b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
6006b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
601532ed618SSoby Mathew 			break;
602532ed618SSoby Mathew 		}
603532ed618SSoby Mathew 	}
604532ed618SSoby Mathew 
6056b7b0f36SAntonio Nino Diaz 	return ret;
606532ed618SSoby Mathew }
607