xref: /rk3399_ARM-atf/lib/psci/psci_main.c (revision 45c7328c0b94d043745b4a44c2e14e1a77f5c347)
1532ed618SSoby Mathew /*
2*45c7328cSBoyan Karatotev  * Copyright (c) 2013-2024, 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>
11*45c7328cSBoyan Karatotev #include <arch_features.h>
12532ed618SSoby Mathew #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
1409d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h>
1509d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h>
1609d40e0eSAntonio Nino Diaz #include <lib/smccc.h>
1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1809d40e0eSAntonio Nino Diaz #include <services/arm_arch_svc.h>
1909d40e0eSAntonio Nino Diaz 
20532ed618SSoby Mathew #include "psci_private.h"
21532ed618SSoby Mathew 
22532ed618SSoby Mathew /*******************************************************************************
23532ed618SSoby Mathew  * PSCI frontend api for servicing SMCs. Described in the PSCI spec.
24532ed618SSoby Mathew  ******************************************************************************/
25532ed618SSoby Mathew int psci_cpu_on(u_register_t target_cpu,
26532ed618SSoby Mathew 		uintptr_t entrypoint,
27532ed618SSoby Mathew 		u_register_t context_id)
28532ed618SSoby Mathew 
29532ed618SSoby Mathew {
30532ed618SSoby Mathew 	int rc;
31532ed618SSoby Mathew 	entry_point_info_t ep;
32532ed618SSoby Mathew 
33e60c1847SManish Pandey 	/* Validate the target CPU */
34e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
35532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
36532ed618SSoby Mathew 
37532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
38532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
39532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
40532ed618SSoby Mathew 		return rc;
41532ed618SSoby Mathew 
42532ed618SSoby Mathew 	/*
43532ed618SSoby Mathew 	 * To turn this cpu on, specify which power
44532ed618SSoby Mathew 	 * levels need to be turned on
45532ed618SSoby Mathew 	 */
46532ed618SSoby Mathew 	return psci_cpu_on_start(target_cpu, &ep);
47532ed618SSoby Mathew }
48532ed618SSoby Mathew 
49532ed618SSoby Mathew unsigned int psci_version(void)
50532ed618SSoby Mathew {
51532ed618SSoby Mathew 	return PSCI_MAJOR_VER | PSCI_MINOR_VER;
52532ed618SSoby Mathew }
53532ed618SSoby Mathew 
54532ed618SSoby Mathew int psci_cpu_suspend(unsigned int power_state,
55532ed618SSoby Mathew 		     uintptr_t entrypoint,
56532ed618SSoby Mathew 		     u_register_t context_id)
57532ed618SSoby Mathew {
58532ed618SSoby Mathew 	int rc;
59532ed618SSoby Mathew 	unsigned int target_pwrlvl, is_power_down_state;
60532ed618SSoby Mathew 	entry_point_info_t ep;
61532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
62532ed618SSoby Mathew 	plat_local_state_t cpu_pd_state;
63606b7430SWing Li 	unsigned int cpu_idx = plat_my_core_pos();
643b802105SBoyan Karatotev #if PSCI_OS_INIT_MODE
65606b7430SWing Li 	plat_local_state_t prev[PLAT_MAX_PWR_LVL];
66606b7430SWing Li #endif
67532ed618SSoby Mathew 
68*45c7328cSBoyan Karatotev #if ERRATA_SME_POWER_DOWN
69*45c7328cSBoyan Karatotev 	/*
70*45c7328cSBoyan Karatotev 	 * If SME isn't off, attempting a real power down will only end up being
71*45c7328cSBoyan Karatotev 	 * rejected. If we got called with SME on, fall back to a normal
72*45c7328cSBoyan Karatotev 	 * suspend. We can't force SME off as in the event the power down is
73*45c7328cSBoyan Karatotev 	 * rejected for another reason (eg GIC) we'd lose the SME context.
74*45c7328cSBoyan Karatotev 	 */
75*45c7328cSBoyan Karatotev 	if (is_feat_sme_supported() && read_svcr() != 0) {
76*45c7328cSBoyan Karatotev 		power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT);
77*45c7328cSBoyan Karatotev 		power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT);
78*45c7328cSBoyan Karatotev 	}
79*45c7328cSBoyan Karatotev #endif /* ERRATA_SME_POWER_DOWN */
80*45c7328cSBoyan 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 
103532ed618SSoby Mathew 	/* Fast path for CPU standby.*/
104362030bfSAntonio Nino Diaz 	if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
1056b7b0f36SAntonio Nino Diaz 		if  (psci_plat_pm_ops->cpu_standby == NULL)
106532ed618SSoby Mathew 			return PSCI_E_INVALID_PARAMS;
107532ed618SSoby Mathew 
108532ed618SSoby Mathew 		/*
109532ed618SSoby Mathew 		 * Set the state of the CPU power domain to the platform
110532ed618SSoby Mathew 		 * specific retention state and enter the standby state.
111532ed618SSoby Mathew 		 */
112532ed618SSoby Mathew 		cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
113532ed618SSoby Mathew 		psci_set_cpu_local_state(cpu_pd_state);
114532ed618SSoby Mathew 
115606b7430SWing Li #if PSCI_OS_INIT_MODE
116606b7430SWing Li 		/*
117606b7430SWing Li 		 * If in OS-initiated mode, save a copy of the previous
118606b7430SWing Li 		 * requested local power states and update the new requested
119606b7430SWing Li 		 * local power states for this CPU.
120606b7430SWing Li 		 */
121606b7430SWing Li 		if (psci_suspend_mode == OS_INIT) {
122606b7430SWing Li 			psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx,
123606b7430SWing Li 							 &state_info, prev);
124606b7430SWing Li 		}
125606b7430SWing Li #endif
126606b7430SWing Li 
127532ed618SSoby Mathew #if ENABLE_PSCI_STAT
12804c1db1eSdp-arm 		plat_psci_stat_accounting_start(&state_info);
129532ed618SSoby Mathew #endif
130532ed618SSoby Mathew 
131872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
132872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
133872be88aSdp-arm 		    RT_INSTR_ENTER_HW_LOW_PWR,
134872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
135872be88aSdp-arm #endif
136872be88aSdp-arm 
137532ed618SSoby Mathew 		psci_plat_pm_ops->cpu_standby(cpu_pd_state);
138532ed618SSoby Mathew 
139532ed618SSoby Mathew 		/* Upon exit from standby, set the state back to RUN. */
140532ed618SSoby Mathew 		psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
141532ed618SSoby Mathew 
142606b7430SWing Li #if PSCI_OS_INIT_MODE
143606b7430SWing Li 		/*
144606b7430SWing Li 		 * If in OS-initiated mode, restore the previous requested
145606b7430SWing Li 		 * local power states for this CPU.
146606b7430SWing Li 		 */
147606b7430SWing Li 		if (psci_suspend_mode == OS_INIT) {
148606b7430SWing Li 			psci_restore_req_local_pwr_states(cpu_idx, prev);
149606b7430SWing Li 		}
150606b7430SWing Li #endif
151606b7430SWing Li 
152872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
153872be88aSdp-arm 		PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
154872be88aSdp-arm 		    RT_INSTR_EXIT_HW_LOW_PWR,
155872be88aSdp-arm 		    PMF_NO_CACHE_MAINT);
156872be88aSdp-arm #endif
157872be88aSdp-arm 
158532ed618SSoby Mathew #if ENABLE_PSCI_STAT
15904c1db1eSdp-arm 		plat_psci_stat_accounting_stop(&state_info);
160532ed618SSoby Mathew 
161532ed618SSoby Mathew 		/* Update PSCI stats */
1623b802105SBoyan Karatotev 		psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
163532ed618SSoby Mathew #endif
164532ed618SSoby Mathew 
165532ed618SSoby Mathew 		return PSCI_E_SUCCESS;
166532ed618SSoby Mathew 	}
167532ed618SSoby Mathew 
168532ed618SSoby Mathew 	/*
169532ed618SSoby Mathew 	 * If a power down state has been requested, we need to verify entry
170532ed618SSoby Mathew 	 * point and program entry information.
171532ed618SSoby Mathew 	 */
1726b7b0f36SAntonio Nino Diaz 	if (is_power_down_state != 0U) {
173532ed618SSoby Mathew 		rc = psci_validate_entry_point(&ep, entrypoint, context_id);
174532ed618SSoby Mathew 		if (rc != PSCI_E_SUCCESS)
175532ed618SSoby Mathew 			return rc;
176532ed618SSoby Mathew 	}
177532ed618SSoby Mathew 
178532ed618SSoby Mathew 	/*
179532ed618SSoby Mathew 	 * Do what is needed to enter the power down state. Upon success,
180532ed618SSoby Mathew 	 * enter the final wfi which will power down this CPU. This function
181532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
182532ed618SSoby Mathew 	 * arrival of an interrupt
183532ed618SSoby Mathew 	 */
1843b802105SBoyan Karatotev 	rc = psci_cpu_suspend_start(cpu_idx,
1853b802105SBoyan Karatotev 				    &ep,
186532ed618SSoby Mathew 				    target_pwrlvl,
187532ed618SSoby Mathew 				    &state_info,
188532ed618SSoby Mathew 				    is_power_down_state);
189532ed618SSoby Mathew 
190606b7430SWing Li 	return rc;
191532ed618SSoby Mathew }
192532ed618SSoby Mathew 
193532ed618SSoby Mathew 
194532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
195532ed618SSoby Mathew {
196532ed618SSoby Mathew 	int rc;
197532ed618SSoby Mathew 	psci_power_state_t state_info;
198532ed618SSoby Mathew 	entry_point_info_t ep;
1993b802105SBoyan Karatotev 	unsigned int cpu_idx = plat_my_core_pos();
200532ed618SSoby Mathew 
201532ed618SSoby Mathew 	/* Check if the current CPU is the last ON CPU in the system */
2023b802105SBoyan Karatotev 	if (!psci_is_last_on_cpu(cpu_idx))
203532ed618SSoby Mathew 		return PSCI_E_DENIED;
204532ed618SSoby Mathew 
205532ed618SSoby Mathew 	/* Validate the entry point and get the entry_point_info */
206532ed618SSoby Mathew 	rc = psci_validate_entry_point(&ep, entrypoint, context_id);
207532ed618SSoby Mathew 	if (rc != PSCI_E_SUCCESS)
208532ed618SSoby Mathew 		return rc;
209532ed618SSoby Mathew 
210532ed618SSoby Mathew 	/* Query the psci_power_state for system suspend */
211532ed618SSoby Mathew 	psci_query_sys_suspend_pwrstate(&state_info);
212532ed618SSoby Mathew 
213a4065abdSldts 	/*
214a4065abdSldts 	 * Check if platform allows suspend to Highest power level
215a4065abdSldts 	 * (System level)
216a4065abdSldts 	 */
217a4065abdSldts 	if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL)
218a4065abdSldts 		return PSCI_E_DENIED;
219a4065abdSldts 
220532ed618SSoby Mathew 	/* Ensure that the psci_power_state makes sense */
221532ed618SSoby Mathew 	assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
222532ed618SSoby Mathew 						== PSCI_E_SUCCESS);
2236b7b0f36SAntonio Nino Diaz 	assert(is_local_state_off(
2246b7b0f36SAntonio Nino Diaz 			state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
225532ed618SSoby Mathew 
226532ed618SSoby Mathew 	/*
227532ed618SSoby Mathew 	 * Do what is needed to enter the system suspend state. This function
228532ed618SSoby Mathew 	 * might return if the power down was abandoned for any reason, e.g.
229532ed618SSoby Mathew 	 * arrival of an interrupt
230532ed618SSoby Mathew 	 */
2313b802105SBoyan Karatotev 	rc = psci_cpu_suspend_start(cpu_idx,
2323b802105SBoyan Karatotev 				    &ep,
233532ed618SSoby Mathew 				    PLAT_MAX_PWR_LVL,
234532ed618SSoby Mathew 				    &state_info,
235532ed618SSoby Mathew 				    PSTATE_TYPE_POWERDOWN);
236532ed618SSoby Mathew 
237606b7430SWing Li 	return rc;
238532ed618SSoby Mathew }
239532ed618SSoby Mathew 
240532ed618SSoby Mathew int psci_cpu_off(void)
241532ed618SSoby Mathew {
242532ed618SSoby Mathew 	int rc;
243532ed618SSoby Mathew 	unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
244532ed618SSoby Mathew 
245532ed618SSoby Mathew 	/*
246532ed618SSoby Mathew 	 * Do what is needed to power off this CPU and possible higher power
247532ed618SSoby Mathew 	 * levels if it able to do so. Upon success, enter the final wfi
248532ed618SSoby Mathew 	 * which will power down this CPU.
249532ed618SSoby Mathew 	 */
250532ed618SSoby Mathew 	rc = psci_do_cpu_off(target_pwrlvl);
251532ed618SSoby Mathew 
252532ed618SSoby Mathew 	/*
253532ed618SSoby Mathew 	 * The only error cpu_off can return is E_DENIED. So check if that's
254532ed618SSoby Mathew 	 * indeed the case.
255532ed618SSoby Mathew 	 */
256532ed618SSoby Mathew 	assert(rc == PSCI_E_DENIED);
257532ed618SSoby Mathew 
258532ed618SSoby Mathew 	return rc;
259532ed618SSoby Mathew }
260532ed618SSoby Mathew 
261532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity,
262532ed618SSoby Mathew 		       unsigned int lowest_affinity_level)
263532ed618SSoby Mathew {
2645b33ad17SDeepika Bhavnani 	unsigned int target_idx;
265532ed618SSoby Mathew 
266e60c1847SManish Pandey 	/* Validate the target affinity */
267e60c1847SManish Pandey 	if (!is_valid_mpidr(target_affinity))
268e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
269e60c1847SManish Pandey 
270532ed618SSoby Mathew 	/* We dont support level higher than PSCI_CPU_PWR_LVL */
271532ed618SSoby Mathew 	if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
272532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
273532ed618SSoby Mathew 
274532ed618SSoby Mathew 	/* Calculate the cpu index of the target */
275e60c1847SManish Pandey 	target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
276532ed618SSoby Mathew 
2778fd307ffSRoberto Vargas 	/*
2788fd307ffSRoberto Vargas 	 * Generic management:
2798fd307ffSRoberto Vargas 	 * Perform cache maintanence ahead of reading the target CPU state to
2808fd307ffSRoberto Vargas 	 * ensure that the data is not stale.
2818fd307ffSRoberto Vargas 	 * There is a theoretical edge case where the cache may contain stale
2828fd307ffSRoberto Vargas 	 * data for the target CPU data - this can occur under the following
2838fd307ffSRoberto Vargas 	 * conditions:
2848fd307ffSRoberto Vargas 	 * - the target CPU is in another cluster from the current
2858fd307ffSRoberto Vargas 	 * - the target CPU was the last CPU to shutdown on its cluster
2868fd307ffSRoberto Vargas 	 * - the cluster was removed from coherency as part of the CPU shutdown
2878fd307ffSRoberto Vargas 	 *
2888fd307ffSRoberto Vargas 	 * In this case the cache maintenace that was performed as part of the
2898fd307ffSRoberto Vargas 	 * target CPUs shutdown was not seen by the current CPU's cluster. And
2908fd307ffSRoberto Vargas 	 * so the cache may contain stale data for the target CPU.
2918fd307ffSRoberto Vargas 	 */
2925b33ad17SDeepika Bhavnani 	flush_cpu_data_by_index(target_idx,
2936b7b0f36SAntonio Nino Diaz 				psci_svc_cpu_data.aff_info_state);
2948fd307ffSRoberto Vargas 
295532ed618SSoby Mathew 	return psci_get_aff_info_state_by_idx(target_idx);
296532ed618SSoby Mathew }
297532ed618SSoby Mathew 
298532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu)
299532ed618SSoby Mathew {
300532ed618SSoby Mathew 	int rc;
301532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
302532ed618SSoby Mathew 
303e60c1847SManish Pandey 	/* Validate the target cpu */
304e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
305e60c1847SManish Pandey 		return PSCI_E_INVALID_PARAMS;
306e60c1847SManish Pandey 
307532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
308532ed618SSoby Mathew 	if (rc != PSCI_TOS_UP_MIG_CAP)
309532ed618SSoby Mathew 		return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
310532ed618SSoby Mathew 			  PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
311532ed618SSoby Mathew 
312532ed618SSoby Mathew 	/*
313532ed618SSoby Mathew 	 * Migrate should only be invoked on the CPU where
314532ed618SSoby Mathew 	 * the Secure OS is resident.
315532ed618SSoby Mathew 	 */
316532ed618SSoby Mathew 	if (resident_cpu_mpidr != read_mpidr_el1())
317532ed618SSoby Mathew 		return PSCI_E_NOT_PRESENT;
318532ed618SSoby Mathew 
319532ed618SSoby Mathew 	/* Check the validity of the specified target cpu */
320e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
321532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
322532ed618SSoby Mathew 
3236b7b0f36SAntonio Nino Diaz 	assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
324532ed618SSoby Mathew 
325532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
3266b7b0f36SAntonio Nino Diaz 	assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
327532ed618SSoby Mathew 
328532ed618SSoby Mathew 	return rc;
329532ed618SSoby Mathew }
330532ed618SSoby Mathew 
331532ed618SSoby Mathew int psci_migrate_info_type(void)
332532ed618SSoby Mathew {
333532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
334532ed618SSoby Mathew 
335532ed618SSoby Mathew 	return psci_spd_migrate_info(&resident_cpu_mpidr);
336532ed618SSoby Mathew }
337532ed618SSoby Mathew 
3386b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void)
339532ed618SSoby Mathew {
340532ed618SSoby Mathew 	u_register_t resident_cpu_mpidr;
341532ed618SSoby Mathew 	int rc;
342532ed618SSoby Mathew 
343532ed618SSoby Mathew 	/*
344532ed618SSoby Mathew 	 * Return value of this depends upon what
345532ed618SSoby Mathew 	 * psci_spd_migrate_info() returns.
346532ed618SSoby Mathew 	 */
347532ed618SSoby Mathew 	rc = psci_spd_migrate_info(&resident_cpu_mpidr);
3486b7b0f36SAntonio Nino Diaz 	if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
3496b7b0f36SAntonio Nino Diaz 		return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
350532ed618SSoby Mathew 
351532ed618SSoby Mathew 	return resident_cpu_mpidr;
352532ed618SSoby Mathew }
353532ed618SSoby Mathew 
35428d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu,
35528d3d614SJeenu Viswambharan 		       unsigned int power_level)
35628d3d614SJeenu Viswambharan {
35728d3d614SJeenu Viswambharan 	int rc;
35828d3d614SJeenu Viswambharan 
35928d3d614SJeenu Viswambharan 	/* Validate target_cpu */
360e60c1847SManish Pandey 	if (!is_valid_mpidr(target_cpu))
36128d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
36228d3d614SJeenu Viswambharan 
36328d3d614SJeenu Viswambharan 	/* Validate power_level against PLAT_MAX_PWR_LVL */
36428d3d614SJeenu Viswambharan 	if (power_level > PLAT_MAX_PWR_LVL)
36528d3d614SJeenu Viswambharan 		return PSCI_E_INVALID_PARAMS;
36628d3d614SJeenu Viswambharan 
36728d3d614SJeenu Viswambharan 	/*
36828d3d614SJeenu Viswambharan 	 * Dispatch this call to platform to query power controller, and pass on
36928d3d614SJeenu Viswambharan 	 * to the caller what it returns
37028d3d614SJeenu Viswambharan 	 */
3716b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->get_node_hw_state != NULL);
37228d3d614SJeenu Viswambharan 	rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
3736b7b0f36SAntonio Nino Diaz 	assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
3746b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_NOT_SUPPORTED)
3756b7b0f36SAntonio Nino Diaz 		|| (rc == PSCI_E_INVALID_PARAMS));
37628d3d614SJeenu Viswambharan 	return rc;
37728d3d614SJeenu Viswambharan }
37828d3d614SJeenu Viswambharan 
379532ed618SSoby Mathew int psci_features(unsigned int psci_fid)
380532ed618SSoby Mathew {
381532ed618SSoby Mathew 	unsigned int local_caps = psci_caps;
382532ed618SSoby Mathew 
3836eabbb07SDimitris Papastamos 	if (psci_fid == SMCCC_VERSION)
3846eabbb07SDimitris Papastamos 		return PSCI_E_SUCCESS;
3856eabbb07SDimitris Papastamos 
386532ed618SSoby Mathew 	/* Check if it is a 64 bit function */
387532ed618SSoby Mathew 	if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64)
388532ed618SSoby Mathew 		local_caps &= PSCI_CAP_64BIT_MASK;
389532ed618SSoby Mathew 
390532ed618SSoby Mathew 	/* Check for invalid fid */
391532ed618SSoby Mathew 	if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
392532ed618SSoby Mathew 			&& is_psci_fid(psci_fid)))
393532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
394532ed618SSoby Mathew 
395532ed618SSoby Mathew 
396532ed618SSoby Mathew 	/* Check if the psci fid is supported or not */
3976b7b0f36SAntonio Nino Diaz 	if ((local_caps & define_psci_cap(psci_fid)) == 0U)
398532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
399532ed618SSoby Mathew 
400532ed618SSoby Mathew 	/* Format the feature flags */
4016b7b0f36SAntonio Nino Diaz 	if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
4026b7b0f36SAntonio Nino Diaz 	    (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
4036b7b0f36SAntonio Nino Diaz 		unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
4049a70e69eSWing Li 			(FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
4056b7b0f36SAntonio Nino Diaz 		return (int)ret;
406532ed618SSoby Mathew 	}
407532ed618SSoby Mathew 
408532ed618SSoby Mathew 	/* Return 0 for all other fid's */
409532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
410532ed618SSoby Mathew }
411532ed618SSoby Mathew 
412b88a4416SWing Li #if PSCI_OS_INIT_MODE
413b88a4416SWing Li int psci_set_suspend_mode(unsigned int mode)
414b88a4416SWing Li {
415b88a4416SWing Li 	if (psci_suspend_mode == mode) {
416b88a4416SWing Li 		return PSCI_E_SUCCESS;
417b88a4416SWing Li 	}
418b88a4416SWing Li 
4193b802105SBoyan Karatotev 	unsigned int this_core = plat_my_core_pos();
4203b802105SBoyan Karatotev 
421b88a4416SWing Li 	if (mode == PLAT_COORD) {
422b88a4416SWing Li 		/* Check if the current CPU is the last ON CPU in the system */
4233b802105SBoyan Karatotev 		if (!psci_is_last_on_cpu_safe(this_core)) {
424b88a4416SWing Li 			return PSCI_E_DENIED;
425b88a4416SWing Li 		}
426b88a4416SWing Li 	}
427b88a4416SWing Li 
428b88a4416SWing Li 	if (mode == OS_INIT) {
429b88a4416SWing Li 		/*
430b88a4416SWing Li 		 * Check if all CPUs in the system are ON or if the current
431b88a4416SWing Li 		 * CPU is the last ON CPU in the system.
432b88a4416SWing Li 		 */
4333b802105SBoyan Karatotev 		if (!(psci_are_all_cpus_on_safe(this_core) ||
4343b802105SBoyan Karatotev 		      psci_is_last_on_cpu_safe(this_core))) {
435b88a4416SWing Li 			return PSCI_E_DENIED;
436b88a4416SWing Li 		}
437b88a4416SWing Li 	}
438b88a4416SWing Li 
439b88a4416SWing Li 	psci_suspend_mode = mode;
440b88a4416SWing Li 	psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
441b88a4416SWing Li 				sizeof(psci_suspend_mode));
442b88a4416SWing Li 
443b88a4416SWing Li 	return PSCI_E_SUCCESS;
444b88a4416SWing Li }
445b88a4416SWing Li #endif
446b88a4416SWing Li 
447532ed618SSoby Mathew /*******************************************************************************
448532ed618SSoby Mathew  * PSCI top level handler for servicing SMCs.
449532ed618SSoby Mathew  ******************************************************************************/
450cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid,
451532ed618SSoby Mathew 			  u_register_t x1,
452532ed618SSoby Mathew 			  u_register_t x2,
453532ed618SSoby Mathew 			  u_register_t x3,
454532ed618SSoby Mathew 			  u_register_t x4,
455532ed618SSoby Mathew 			  void *cookie,
456532ed618SSoby Mathew 			  void *handle,
457532ed618SSoby Mathew 			  u_register_t flags)
458532ed618SSoby Mathew {
4596b7b0f36SAntonio Nino Diaz 	u_register_t ret;
4606b7b0f36SAntonio Nino Diaz 
461532ed618SSoby Mathew 	if (is_caller_secure(flags))
4626b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
463532ed618SSoby Mathew 
464532ed618SSoby Mathew 	/* Check the fid against the capabilities */
4656b7b0f36SAntonio Nino Diaz 	if ((psci_caps & define_psci_cap(smc_fid)) == 0U)
4666b7b0f36SAntonio Nino Diaz 		return (u_register_t)SMC_UNK;
467532ed618SSoby Mathew 
468532ed618SSoby Mathew 	if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
469532ed618SSoby Mathew 		/* 32-bit PSCI function, clear top parameter bits */
470532ed618SSoby Mathew 
4716b7b0f36SAntonio Nino Diaz 		uint32_t r1 = (uint32_t)x1;
4726b7b0f36SAntonio Nino Diaz 		uint32_t r2 = (uint32_t)x2;
4736b7b0f36SAntonio Nino Diaz 		uint32_t r3 = (uint32_t)x3;
474532ed618SSoby Mathew 
475532ed618SSoby Mathew 		switch (smc_fid) {
476532ed618SSoby Mathew 		case PSCI_VERSION:
4776b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_version();
4786b7b0f36SAntonio Nino Diaz 			break;
479532ed618SSoby Mathew 
480532ed618SSoby Mathew 		case PSCI_CPU_OFF:
4816b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_off();
4826b7b0f36SAntonio Nino Diaz 			break;
483532ed618SSoby Mathew 
484532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH32:
4856b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
4866b7b0f36SAntonio Nino Diaz 			break;
487532ed618SSoby Mathew 
488532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH32:
4896b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(r1, r2, r3);
4906b7b0f36SAntonio Nino Diaz 			break;
491532ed618SSoby Mathew 
492532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH32:
4936b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_affinity_info(r1, r2);
4946b7b0f36SAntonio Nino Diaz 			break;
495532ed618SSoby Mathew 
496532ed618SSoby Mathew 		case PSCI_MIG_AARCH32:
4976b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(r1);
4986b7b0f36SAntonio Nino Diaz 			break;
499532ed618SSoby Mathew 
500532ed618SSoby Mathew 		case PSCI_MIG_INFO_TYPE:
5016b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate_info_type();
5026b7b0f36SAntonio Nino Diaz 			break;
503532ed618SSoby Mathew 
504532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH32:
5056b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
5066b7b0f36SAntonio Nino Diaz 			break;
507532ed618SSoby Mathew 
50828d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH32:
5096b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(r1, r2);
5106b7b0f36SAntonio Nino Diaz 			break;
51128d3d614SJeenu Viswambharan 
512532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH32:
5136b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(r1, r2);
5146b7b0f36SAntonio Nino Diaz 			break;
515532ed618SSoby Mathew 
516532ed618SSoby Mathew 		case PSCI_SYSTEM_OFF:
517532ed618SSoby Mathew 			psci_system_off();
518532ed618SSoby Mathew 			/* We should never return from psci_system_off() */
5193eacacc0SJonathan Wright 			break;
520532ed618SSoby Mathew 
521532ed618SSoby Mathew 		case PSCI_SYSTEM_RESET:
522532ed618SSoby Mathew 			psci_system_reset();
523532ed618SSoby Mathew 			/* We should never return from psci_system_reset() */
5243eacacc0SJonathan Wright 			break;
525532ed618SSoby Mathew 
526532ed618SSoby Mathew 		case PSCI_FEATURES:
5276b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_features(r1);
5286b7b0f36SAntonio Nino Diaz 			break;
529532ed618SSoby Mathew 
530b88a4416SWing Li #if PSCI_OS_INIT_MODE
531b88a4416SWing Li 		case PSCI_SET_SUSPEND_MODE:
532b88a4416SWing Li 			ret = (u_register_t)psci_set_suspend_mode(r1);
533b88a4416SWing Li 			break;
534b88a4416SWing Li #endif
535b88a4416SWing Li 
536532ed618SSoby Mathew #if ENABLE_PSCI_STAT
537532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH32:
5386b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(r1, r2);
5396b7b0f36SAntonio Nino Diaz 			break;
540532ed618SSoby Mathew 
541532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH32:
5426b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(r1, r2);
5436b7b0f36SAntonio Nino Diaz 			break;
544532ed618SSoby Mathew #endif
545d4c596beSRoberto Vargas 		case PSCI_MEM_PROTECT:
5466b7b0f36SAntonio Nino Diaz 			ret = psci_mem_protect(r1);
5476b7b0f36SAntonio Nino Diaz 			break;
548d4c596beSRoberto Vargas 
549d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH32:
5506b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(r1, r2);
5516b7b0f36SAntonio Nino Diaz 			break;
552532ed618SSoby Mathew 
55336a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH32:
55436a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
5556b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2(r1, r2);
5566b7b0f36SAntonio Nino Diaz 			break;
55736a8f8fdSRoberto Vargas 
558532ed618SSoby Mathew 		default:
5596b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
5606b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
561532ed618SSoby Mathew 			break;
562532ed618SSoby Mathew 		}
563532ed618SSoby Mathew 	} else {
564532ed618SSoby Mathew 		/* 64-bit PSCI function */
565532ed618SSoby Mathew 
566532ed618SSoby Mathew 		switch (smc_fid) {
567532ed618SSoby Mathew 		case PSCI_CPU_SUSPEND_AARCH64:
5686b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5696b7b0f36SAntonio Nino Diaz 				psci_cpu_suspend((unsigned int)x1, x2, x3);
5706b7b0f36SAntonio Nino Diaz 			break;
571532ed618SSoby Mathew 
572532ed618SSoby Mathew 		case PSCI_CPU_ON_AARCH64:
5736b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_cpu_on(x1, x2, x3);
5746b7b0f36SAntonio Nino Diaz 			break;
575532ed618SSoby Mathew 
576532ed618SSoby Mathew 		case PSCI_AFFINITY_INFO_AARCH64:
5776b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)
5786b7b0f36SAntonio Nino Diaz 				psci_affinity_info(x1, (unsigned int)x2);
5796b7b0f36SAntonio Nino Diaz 			break;
580532ed618SSoby Mathew 
581532ed618SSoby Mathew 		case PSCI_MIG_AARCH64:
5826b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_migrate(x1);
5836b7b0f36SAntonio Nino Diaz 			break;
584532ed618SSoby Mathew 
585532ed618SSoby Mathew 		case PSCI_MIG_INFO_UP_CPU_AARCH64:
5866b7b0f36SAntonio Nino Diaz 			ret = psci_migrate_info_up_cpu();
5876b7b0f36SAntonio Nino Diaz 			break;
588532ed618SSoby Mathew 
58928d3d614SJeenu Viswambharan 		case PSCI_NODE_HW_STATE_AARCH64:
5906b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_node_hw_state(
5916b7b0f36SAntonio Nino Diaz 					x1, (unsigned int) x2);
5926b7b0f36SAntonio Nino Diaz 			break;
59328d3d614SJeenu Viswambharan 
594532ed618SSoby Mathew 		case PSCI_SYSTEM_SUSPEND_AARCH64:
5956b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)psci_system_suspend(x1, x2);
5966b7b0f36SAntonio Nino Diaz 			break;
597532ed618SSoby Mathew 
598532ed618SSoby Mathew #if ENABLE_PSCI_STAT
599532ed618SSoby Mathew 		case PSCI_STAT_RESIDENCY_AARCH64:
6006b7b0f36SAntonio Nino Diaz 			ret = psci_stat_residency(x1, (unsigned int) x2);
6016b7b0f36SAntonio Nino Diaz 			break;
602532ed618SSoby Mathew 
603532ed618SSoby Mathew 		case PSCI_STAT_COUNT_AARCH64:
6046b7b0f36SAntonio Nino Diaz 			ret = psci_stat_count(x1, (unsigned int) x2);
6056b7b0f36SAntonio Nino Diaz 			break;
606532ed618SSoby Mathew #endif
607532ed618SSoby Mathew 
608d4c596beSRoberto Vargas 		case PSCI_MEM_CHK_RANGE_AARCH64:
6096b7b0f36SAntonio Nino Diaz 			ret = psci_mem_chk_range(x1, x2);
6106b7b0f36SAntonio Nino Diaz 			break;
611d4c596beSRoberto Vargas 
61236a8f8fdSRoberto Vargas 		case PSCI_SYSTEM_RESET2_AARCH64:
61336a8f8fdSRoberto Vargas 			/* We should never return from psci_system_reset2() */
6146b7b0f36SAntonio Nino Diaz 			ret = psci_system_reset2((uint32_t) x1, x2);
6156b7b0f36SAntonio Nino Diaz 			break;
616d4c596beSRoberto Vargas 
617532ed618SSoby Mathew 		default:
6186b7b0f36SAntonio Nino Diaz 			WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
6196b7b0f36SAntonio Nino Diaz 			ret = (u_register_t)SMC_UNK;
620532ed618SSoby Mathew 			break;
621532ed618SSoby Mathew 		}
622532ed618SSoby Mathew 	}
623532ed618SSoby Mathew 
6246b7b0f36SAntonio Nino Diaz 	return ret;
625532ed618SSoby Mathew }
626