blob: 31b1d1cf3ecd635f02aafc5f1f79930ef8b8be76 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Jayanth Dodderi Chidanandb41b0822022-08-22 23:46:10 +01002 * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00007#include <assert.h>
8#include <string.h>
9
Dan Handley97043ac2014-04-09 13:14:54 +010010#include <arch.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010011#include <arch_helpers.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000012#include <common/debug.h>
13#include <lib/pmf/pmf.h>
14#include <lib/runtime_instr.h>
15#include <lib/smccc.h>
16#include <plat/common/platform.h>
17#include <services/arm_arch_svc.h>
18
Dan Handley35e98e52014-04-09 13:13:04 +010019#include "psci_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010020
21/*******************************************************************************
22 * PSCI frontend api for servicing SMCs. Described in the PSCI spec.
23 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +010024int psci_cpu_on(u_register_t target_cpu,
25 uintptr_t entrypoint,
26 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +010027
28{
29 int rc;
Soby Mathew78879b92015-01-06 15:36:38 +000030 entry_point_info_t ep;
Achin Gupta4f6ad662013-10-25 09:08:21 +010031
Manish Pandey256a5482023-10-27 11:45:44 +010032 /* Validate the target CPU */
33 if (!is_valid_mpidr(target_cpu))
Soby Mathew539dced2014-10-02 16:56:51 +010034 return PSCI_E_INVALID_PARAMS;
Soby Mathew539dced2014-10-02 16:56:51 +010035
Soby Mathew617540d2015-07-15 12:13:26 +010036 /* Validate the entry point and get the entry_point_info */
37 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathew78879b92015-01-06 15:36:38 +000038 if (rc != PSCI_E_SUCCESS)
39 return rc;
40
Soby Mathew78879b92015-01-06 15:36:38 +000041 /*
Soby Mathew67487842015-07-13 14:10:57 +010042 * To turn this cpu on, specify which power
Achin Gupta0959db52013-12-02 17:33:04 +000043 * levels need to be turned on
44 */
Sandrine Bailleux22b09c12016-04-25 09:28:43 +010045 return psci_cpu_on_start(target_cpu, &ep);
Achin Gupta4f6ad662013-10-25 09:08:21 +010046}
47
48unsigned int psci_version(void)
49{
50 return PSCI_MAJOR_VER | PSCI_MINOR_VER;
51}
52
53int psci_cpu_suspend(unsigned int power_state,
Soby Mathew9d070b92015-07-29 17:05:03 +010054 uintptr_t entrypoint,
55 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +010056{
57 int rc;
Soby Mathew67487842015-07-13 14:10:57 +010058 unsigned int target_pwrlvl, is_power_down_state;
Soby Mathew78879b92015-01-06 15:36:38 +000059 entry_point_info_t ep;
Soby Mathew67487842015-07-13 14:10:57 +010060 psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
61 plat_local_state_t cpu_pd_state;
Achin Gupta4f6ad662013-10-25 09:08:21 +010062
Soby Mathew67487842015-07-13 14:10:57 +010063 /* Validate the power_state parameter */
64 rc = psci_validate_power_state(power_state, &state_info);
65 if (rc != PSCI_E_SUCCESS) {
66 assert(rc == PSCI_E_INVALID_PARAMS);
67 return rc;
Soby Mathew539dced2014-10-02 16:56:51 +010068 }
69
Achin Gupta317ba092014-05-09 19:32:25 +010070 /*
Soby Mathew67487842015-07-13 14:10:57 +010071 * Get the value of the state type bit from the power state parameter.
Achin Gupta317ba092014-05-09 19:32:25 +010072 */
Soby Mathew67487842015-07-13 14:10:57 +010073 is_power_down_state = psci_get_pstate_type(power_state);
74
75 /* Sanity check the requested suspend levels */
Soby Mathewda554d72016-05-03 17:11:42 +010076 assert(psci_validate_suspend_req(&state_info, is_power_down_state)
Soby Mathew67487842015-07-13 14:10:57 +010077 == PSCI_E_SUCCESS);
78
79 target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
Sandrine Bailleuxa1c3faa2016-06-22 16:35:01 +010080 if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
81 ERROR("Invalid target power level for suspend operation\n");
82 panic();
83 }
Soby Mathew67487842015-07-13 14:10:57 +010084
85 /* Fast path for CPU standby.*/
Antonio Nino Diaz362030b2018-08-01 16:42:10 +010086 if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +010087 if (psci_plat_pm_ops->cpu_standby == NULL)
Vikram Kanigirid118f9f2014-03-21 11:57:10 +000088 return PSCI_E_INVALID_PARAMS;
Achin Gupta317ba092014-05-09 19:32:25 +010089
Soby Mathew67487842015-07-13 14:10:57 +010090 /*
91 * Set the state of the CPU power domain to the platform
92 * specific retention state and enter the standby state.
93 */
94 cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
95 psci_set_cpu_local_state(cpu_pd_state);
Yatharth Kochar170fb932016-05-09 18:26:35 +010096
97#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +000098 plat_psci_stat_accounting_start(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +010099#endif
100
dp-arm872be882016-09-19 11:18:44 +0100101#if ENABLE_RUNTIME_INSTRUMENTATION
102 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
103 RT_INSTR_ENTER_HW_LOW_PWR,
104 PMF_NO_CACHE_MAINT);
105#endif
106
Soby Mathew67487842015-07-13 14:10:57 +0100107 psci_plat_pm_ops->cpu_standby(cpu_pd_state);
108
109 /* Upon exit from standby, set the state back to RUN. */
110 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
111
dp-arm872be882016-09-19 11:18:44 +0100112#if ENABLE_RUNTIME_INSTRUMENTATION
113 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
114 RT_INSTR_EXIT_HW_LOW_PWR,
115 PMF_NO_CACHE_MAINT);
116#endif
117
Yatharth Kochar170fb932016-05-09 18:26:35 +0100118#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000119 plat_psci_stat_accounting_stop(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100120
121 /* Update PSCI stats */
dp-arm04c1db12017-01-31 13:01:04 +0000122 psci_stats_update_pwr_up(PSCI_CPU_PWR_LVL, &state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100123#endif
124
Soby Mathew539dced2014-10-02 16:56:51 +0100125 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100126 }
127
Achin Gupta317ba092014-05-09 19:32:25 +0100128 /*
Soby Mathew67487842015-07-13 14:10:57 +0100129 * If a power down state has been requested, we need to verify entry
130 * point and program entry information.
Soby Mathew78879b92015-01-06 15:36:38 +0000131 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100132 if (is_power_down_state != 0U) {
Soby Mathew617540d2015-07-15 12:13:26 +0100133 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathew67487842015-07-13 14:10:57 +0100134 if (rc != PSCI_E_SUCCESS)
135 return rc;
136 }
Soby Mathew31244d72014-09-30 11:19:51 +0100137
Soby Mathew78879b92015-01-06 15:36:38 +0000138 /*
Achin Gupta317ba092014-05-09 19:32:25 +0100139 * Do what is needed to enter the power down state. Upon success,
Soby Mathew67487842015-07-13 14:10:57 +0100140 * enter the final wfi which will power down this CPU. This function
141 * might return if the power down was abandoned for any reason, e.g.
142 * arrival of an interrupt
Achin Gupta317ba092014-05-09 19:32:25 +0100143 */
Soby Mathew67487842015-07-13 14:10:57 +0100144 psci_cpu_suspend_start(&ep,
145 target_pwrlvl,
146 &state_info,
147 is_power_down_state);
Soby Mathew539dced2014-10-02 16:56:51 +0100148
Soby Mathew539dced2014-10-02 16:56:51 +0100149 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100150}
151
Soby Mathew9d070b92015-07-29 17:05:03 +0100152
153int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000154{
155 int rc;
Soby Mathew67487842015-07-13 14:10:57 +0100156 psci_power_state_t state_info;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000157 entry_point_info_t ep;
158
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000159 /* Check if the current CPU is the last ON CPU in the system */
Jayanth Dodderi Chidanandb41b0822022-08-22 23:46:10 +0100160 if (!psci_is_last_on_cpu())
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000161 return PSCI_E_DENIED;
162
Soby Mathew617540d2015-07-15 12:13:26 +0100163 /* Validate the entry point and get the entry_point_info */
164 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000165 if (rc != PSCI_E_SUCCESS)
166 return rc;
167
Soby Mathew67487842015-07-13 14:10:57 +0100168 /* Query the psci_power_state for system suspend */
169 psci_query_sys_suspend_pwrstate(&state_info);
170
ldtsa4065ab2018-10-11 08:40:32 +0200171 /*
172 * Check if platform allows suspend to Highest power level
173 * (System level)
174 */
175 if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL)
176 return PSCI_E_DENIED;
177
Soby Mathew67487842015-07-13 14:10:57 +0100178 /* Ensure that the psci_power_state makes sense */
Soby Mathew67487842015-07-13 14:10:57 +0100179 assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
180 == PSCI_E_SUCCESS);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100181 assert(is_local_state_off(
182 state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000183
184 /*
Soby Mathew67487842015-07-13 14:10:57 +0100185 * Do what is needed to enter the system suspend state. This function
186 * might return if the power down was abandoned for any reason, e.g.
187 * arrival of an interrupt
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000188 */
Soby Mathew67487842015-07-13 14:10:57 +0100189 psci_cpu_suspend_start(&ep,
190 PLAT_MAX_PWR_LVL,
191 &state_info,
192 PSTATE_TYPE_POWERDOWN);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000193
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000194 return PSCI_E_SUCCESS;
195}
196
Achin Gupta4f6ad662013-10-25 09:08:21 +0100197int psci_cpu_off(void)
198{
199 int rc;
Soby Mathew9d070b92015-07-29 17:05:03 +0100200 unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100201
Achin Gupta4f6ad662013-10-25 09:08:21 +0100202 /*
Soby Mathew67487842015-07-13 14:10:57 +0100203 * Do what is needed to power off this CPU and possible higher power
204 * levels if it able to do so. Upon success, enter the final wfi
205 * which will power down this CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100206 */
Soby Mathew67487842015-07-13 14:10:57 +0100207 rc = psci_do_cpu_off(target_pwrlvl);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100208
Achin Gupta3140a9e2013-12-02 16:23:12 +0000209 /*
210 * The only error cpu_off can return is E_DENIED. So check if that's
211 * indeed the case.
212 */
Soby Mathewda554d72016-05-03 17:11:42 +0100213 assert(rc == PSCI_E_DENIED);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100214
215 return rc;
216}
217
Soby Mathew9d070b92015-07-29 17:05:03 +0100218int psci_affinity_info(u_register_t target_affinity,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100219 unsigned int lowest_affinity_level)
220{
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600221 unsigned int target_idx;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100222
Manish Pandey256a5482023-10-27 11:45:44 +0100223 /* Validate the target affinity */
224 if (!is_valid_mpidr(target_affinity))
225 return PSCI_E_INVALID_PARAMS;
226
Soby Mathew67487842015-07-13 14:10:57 +0100227 /* We dont support level higher than PSCI_CPU_PWR_LVL */
228 if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
229 return PSCI_E_INVALID_PARAMS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100230
Soby Mathew67487842015-07-13 14:10:57 +0100231 /* Calculate the cpu index of the target */
Manish Pandey256a5482023-10-27 11:45:44 +0100232 target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
Achin Gupta75f73672013-12-05 16:33:10 +0000233
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000234 /*
235 * Generic management:
236 * Perform cache maintanence ahead of reading the target CPU state to
237 * ensure that the data is not stale.
238 * There is a theoretical edge case where the cache may contain stale
239 * data for the target CPU data - this can occur under the following
240 * conditions:
241 * - the target CPU is in another cluster from the current
242 * - the target CPU was the last CPU to shutdown on its cluster
243 * - the cluster was removed from coherency as part of the CPU shutdown
244 *
245 * In this case the cache maintenace that was performed as part of the
246 * target CPUs shutdown was not seen by the current CPU's cluster. And
247 * so the cache may contain stale data for the target CPU.
248 */
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600249 flush_cpu_data_by_index(target_idx,
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100250 psci_svc_cpu_data.aff_info_state);
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000251
Soby Mathew67487842015-07-13 14:10:57 +0100252 return psci_get_aff_info_state_by_idx(target_idx);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100253}
254
Soby Mathew9d070b92015-07-29 17:05:03 +0100255int psci_migrate(u_register_t target_cpu)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100256{
Soby Mathew8991eed2014-10-23 10:35:34 +0100257 int rc;
Soby Mathew9d070b92015-07-29 17:05:03 +0100258 u_register_t resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100259
Manish Pandey256a5482023-10-27 11:45:44 +0100260 /* Validate the target cpu */
261 if (!is_valid_mpidr(target_cpu))
262 return PSCI_E_INVALID_PARAMS;
263
Soby Mathew8991eed2014-10-23 10:35:34 +0100264 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
265 if (rc != PSCI_TOS_UP_MIG_CAP)
266 return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
267 PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100268
Achin Gupta4f6ad662013-10-25 09:08:21 +0100269 /*
Soby Mathew8991eed2014-10-23 10:35:34 +0100270 * Migrate should only be invoked on the CPU where
271 * the Secure OS is resident.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100272 */
Soby Mathew8991eed2014-10-23 10:35:34 +0100273 if (resident_cpu_mpidr != read_mpidr_el1())
274 return PSCI_E_NOT_PRESENT;
275
276 /* Check the validity of the specified target cpu */
Manish Pandey256a5482023-10-27 11:45:44 +0100277 if (!is_valid_mpidr(target_cpu))
Soby Mathew8991eed2014-10-23 10:35:34 +0100278 return PSCI_E_INVALID_PARAMS;
279
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100280 assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100281
282 rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100283 assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100284
285 return rc;
286}
287
288int psci_migrate_info_type(void)
289{
Soby Mathew9d070b92015-07-29 17:05:03 +0100290 u_register_t resident_cpu_mpidr;
Soby Mathew8991eed2014-10-23 10:35:34 +0100291
292 return psci_spd_migrate_info(&resident_cpu_mpidr);
293}
294
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100295u_register_t psci_migrate_info_up_cpu(void)
Soby Mathew8991eed2014-10-23 10:35:34 +0100296{
Soby Mathew9d070b92015-07-29 17:05:03 +0100297 u_register_t resident_cpu_mpidr;
Soby Mathew8991eed2014-10-23 10:35:34 +0100298 int rc;
299
300 /*
301 * Return value of this depends upon what
302 * psci_spd_migrate_info() returns.
303 */
304 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100305 if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
306 return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
Soby Mathew8991eed2014-10-23 10:35:34 +0100307
308 return resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100309}
310
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100311int psci_node_hw_state(u_register_t target_cpu,
312 unsigned int power_level)
313{
314 int rc;
315
316 /* Validate target_cpu */
Manish Pandey256a5482023-10-27 11:45:44 +0100317 if (!is_valid_mpidr(target_cpu))
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100318 return PSCI_E_INVALID_PARAMS;
319
320 /* Validate power_level against PLAT_MAX_PWR_LVL */
321 if (power_level > PLAT_MAX_PWR_LVL)
322 return PSCI_E_INVALID_PARAMS;
323
324 /*
325 * Dispatch this call to platform to query power controller, and pass on
326 * to the caller what it returns
327 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100328 assert(psci_plat_pm_ops->get_node_hw_state != NULL);
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100329 rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100330 assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
331 || (rc == PSCI_E_NOT_SUPPORTED)
332 || (rc == PSCI_E_INVALID_PARAMS));
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100333 return rc;
334}
335
Soby Mathew90e82582015-01-07 11:10:22 +0000336int psci_features(unsigned int psci_fid)
337{
Soby Mathew9d070b92015-07-29 17:05:03 +0100338 unsigned int local_caps = psci_caps;
Soby Mathew90e82582015-01-07 11:10:22 +0000339
Dimitris Papastamos6eabbb02018-01-22 12:58:52 +0000340 if (psci_fid == SMCCC_VERSION)
341 return PSCI_E_SUCCESS;
342
Soby Mathew90e82582015-01-07 11:10:22 +0000343 /* Check if it is a 64 bit function */
344 if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64)
345 local_caps &= PSCI_CAP_64BIT_MASK;
346
347 /* Check for invalid fid */
348 if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
349 && is_psci_fid(psci_fid)))
350 return PSCI_E_NOT_SUPPORTED;
351
352
353 /* Check if the psci fid is supported or not */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100354 if ((local_caps & define_psci_cap(psci_fid)) == 0U)
Soby Mathew90e82582015-01-07 11:10:22 +0000355 return PSCI_E_NOT_SUPPORTED;
356
357 /* Format the feature flags */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100358 if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
359 (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
Soby Mathew90e82582015-01-07 11:10:22 +0000360 /*
Soby Mathew67487842015-07-13 14:10:57 +0100361 * The trusted firmware does not support OS Initiated Mode.
Soby Mathew90e82582015-01-07 11:10:22 +0000362 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100363 unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
364 (((FF_SUPPORTS_OS_INIT_MODE == 1U) ? 0U : 1U)
365 << FF_MODE_SUPPORT_SHIFT));
366 return (int) ret;
Soby Mathew90e82582015-01-07 11:10:22 +0000367 }
368
369 /* Return 0 for all other fid's */
370 return PSCI_E_SUCCESS;
371}
372
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000373/*******************************************************************************
374 * PSCI top level handler for servicing SMCs.
375 ******************************************************************************/
Soby Mathewcf0b1492016-04-29 19:01:30 +0100376u_register_t psci_smc_handler(uint32_t smc_fid,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100377 u_register_t x1,
378 u_register_t x2,
379 u_register_t x3,
380 u_register_t x4,
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000381 void *cookie,
382 void *handle,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100383 u_register_t flags)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000384{
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100385 u_register_t ret;
386
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100387 if (is_caller_secure(flags))
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100388 return (u_register_t)SMC_UNK;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000389
Soby Mathewb234b2c2015-01-15 11:49:49 +0000390 /* Check the fid against the capabilities */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100391 if ((psci_caps & define_psci_cap(smc_fid)) == 0U)
392 return (u_register_t)SMC_UNK;
Soby Mathewb234b2c2015-01-15 11:49:49 +0000393
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100394 if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
395 /* 32-bit PSCI function, clear top parameter bits */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000396
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100397 uint32_t r1 = (uint32_t)x1;
398 uint32_t r2 = (uint32_t)x2;
399 uint32_t r3 = (uint32_t)x3;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000400
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100401 switch (smc_fid) {
402 case PSCI_VERSION:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100403 ret = (u_register_t)psci_version();
404 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000405
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100406 case PSCI_CPU_OFF:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100407 ret = (u_register_t)psci_cpu_off();
408 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000409
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100410 case PSCI_CPU_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100411 ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
412 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000413
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100414 case PSCI_CPU_ON_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100415 ret = (u_register_t)psci_cpu_on(r1, r2, r3);
416 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000417
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100418 case PSCI_AFFINITY_INFO_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100419 ret = (u_register_t)psci_affinity_info(r1, r2);
420 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000421
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100422 case PSCI_MIG_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100423 ret = (u_register_t)psci_migrate(r1);
424 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000425
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100426 case PSCI_MIG_INFO_TYPE:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100427 ret = (u_register_t)psci_migrate_info_type();
428 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100429
430 case PSCI_MIG_INFO_UP_CPU_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100431 ret = psci_migrate_info_up_cpu();
432 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100433
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100434 case PSCI_NODE_HW_STATE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100435 ret = (u_register_t)psci_node_hw_state(r1, r2);
436 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100437
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000438 case PSCI_SYSTEM_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100439 ret = (u_register_t)psci_system_suspend(r1, r2);
440 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000441
Juan Castillod5f13092014-08-12 11:17:06 +0100442 case PSCI_SYSTEM_OFF:
443 psci_system_off();
444 /* We should never return from psci_system_off() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000445 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100446
447 case PSCI_SYSTEM_RESET:
448 psci_system_reset();
449 /* We should never return from psci_system_reset() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000450 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100451
Soby Mathew90e82582015-01-07 11:10:22 +0000452 case PSCI_FEATURES:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100453 ret = (u_register_t)psci_features(r1);
454 break;
Soby Mathew90e82582015-01-07 11:10:22 +0000455
Yatharth Kochar170fb932016-05-09 18:26:35 +0100456#if ENABLE_PSCI_STAT
457 case PSCI_STAT_RESIDENCY_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100458 ret = psci_stat_residency(r1, r2);
459 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100460
461 case PSCI_STAT_COUNT_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100462 ret = psci_stat_count(r1, r2);
463 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100464#endif
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100465 case PSCI_MEM_PROTECT:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100466 ret = psci_mem_protect(r1);
467 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100468
469 case PSCI_MEM_CHK_RANGE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100470 ret = psci_mem_chk_range(r1, r2);
471 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100472
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100473 case PSCI_SYSTEM_RESET2_AARCH32:
474 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100475 ret = psci_system_reset2(r1, r2);
476 break;
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100477
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100478 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100479 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
480 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100481 break;
482 }
483 } else {
484 /* 64-bit PSCI function */
485
486 switch (smc_fid) {
487 case PSCI_CPU_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100488 ret = (u_register_t)
489 psci_cpu_suspend((unsigned int)x1, x2, x3);
490 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100491
492 case PSCI_CPU_ON_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100493 ret = (u_register_t)psci_cpu_on(x1, x2, x3);
494 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100495
496 case PSCI_AFFINITY_INFO_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100497 ret = (u_register_t)
498 psci_affinity_info(x1, (unsigned int)x2);
499 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100500
501 case PSCI_MIG_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100502 ret = (u_register_t)psci_migrate(x1);
503 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100504
505 case PSCI_MIG_INFO_UP_CPU_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100506 ret = psci_migrate_info_up_cpu();
507 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100508
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100509 case PSCI_NODE_HW_STATE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100510 ret = (u_register_t)psci_node_hw_state(
511 x1, (unsigned int) x2);
512 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100513
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000514 case PSCI_SYSTEM_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100515 ret = (u_register_t)psci_system_suspend(x1, x2);
516 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000517
Yatharth Kochar170fb932016-05-09 18:26:35 +0100518#if ENABLE_PSCI_STAT
519 case PSCI_STAT_RESIDENCY_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100520 ret = psci_stat_residency(x1, (unsigned int) x2);
521 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100522
523 case PSCI_STAT_COUNT_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100524 ret = psci_stat_count(x1, (unsigned int) x2);
525 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100526#endif
527
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100528 case PSCI_MEM_CHK_RANGE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100529 ret = psci_mem_chk_range(x1, x2);
530 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100531
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100532 case PSCI_SYSTEM_RESET2_AARCH64:
533 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100534 ret = psci_system_reset2((uint32_t) x1, x2);
535 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100536
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100537 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100538 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
539 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100540 break;
541 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000542 }
543
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100544 return ret;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000545}