blob: 326f125b6360fcfc46445c4aa463031a88df2864 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Jayanth Dodderi Chidanand70763502022-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-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
Dan Handley2bd4ef22014-04-09 13:14:54 +01007#include <assert.h>
Soby Mathew981487a2015-07-13 14:10:57 +01008#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
10#include <arch.h>
11#include <arch_helpers.h>
12#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 Handley714a0d22014-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 Mathew011ca182015-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 Mathew8595b872015-01-06 15:36:38 +000030 entry_point_info_t ep;
Achin Gupta4f6ad662013-10-25 09:08:21 +010031
32 /* Determine if the cpu exists of not */
Soby Mathew981487a2015-07-13 14:10:57 +010033 rc = psci_validate_mpidr(target_cpu);
34 if (rc != PSCI_E_SUCCESS)
Soby Mathew74e52a72014-10-02 16:56:51 +010035 return PSCI_E_INVALID_PARAMS;
Soby Mathew74e52a72014-10-02 16:56:51 +010036
Soby Mathewf1f97a12015-07-15 12:13:26 +010037 /* Validate the entry point and get the entry_point_info */
38 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathew8595b872015-01-06 15:36:38 +000039 if (rc != PSCI_E_SUCCESS)
40 return rc;
41
Soby Mathew8595b872015-01-06 15:36:38 +000042 /*
Soby Mathew981487a2015-07-13 14:10:57 +010043 * To turn this cpu on, specify which power
Achin Gupta0959db52013-12-02 17:33:04 +000044 * levels need to be turned on
45 */
Sandrine Bailleux7497bff2016-04-25 09:28:43 +010046 return psci_cpu_on_start(target_cpu, &ep);
Achin Gupta4f6ad662013-10-25 09:08:21 +010047}
48
49unsigned int psci_version(void)
50{
51 return PSCI_MAJOR_VER | PSCI_MINOR_VER;
52}
53
54int psci_cpu_suspend(unsigned int power_state,
Soby Mathew011ca182015-07-29 17:05:03 +010055 uintptr_t entrypoint,
56 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +010057{
58 int rc;
Soby Mathew981487a2015-07-13 14:10:57 +010059 unsigned int target_pwrlvl, is_power_down_state;
Soby Mathew8595b872015-01-06 15:36:38 +000060 entry_point_info_t ep;
Soby Mathew981487a2015-07-13 14:10:57 +010061 psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
62 plat_local_state_t cpu_pd_state;
Wing Li2c556f32022-09-14 13:18:17 -070063#if PSCI_OS_INIT_MODE
64 unsigned int cpu_idx = plat_my_core_pos();
65 plat_local_state_t prev[PLAT_MAX_PWR_LVL];
66#endif
Achin Gupta4f6ad662013-10-25 09:08:21 +010067
Soby Mathew981487a2015-07-13 14:10:57 +010068 /* Validate the power_state parameter */
69 rc = psci_validate_power_state(power_state, &state_info);
70 if (rc != PSCI_E_SUCCESS) {
71 assert(rc == PSCI_E_INVALID_PARAMS);
72 return rc;
73 }
Vikram Kanigirif100f412014-04-01 19:26:26 +010074
Soby Mathew981487a2015-07-13 14:10:57 +010075 /*
76 * Get the value of the state type bit from the power state parameter.
77 */
78 is_power_down_state = psci_get_pstate_type(power_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +010079
Soby Mathew981487a2015-07-13 14:10:57 +010080 /* Sanity check the requested suspend levels */
Soby Mathew24ab34f2016-05-03 17:11:42 +010081 assert(psci_validate_suspend_req(&state_info, is_power_down_state)
Soby Mathew981487a2015-07-13 14:10:57 +010082 == PSCI_E_SUCCESS);
Soby Mathew74e52a72014-10-02 16:56:51 +010083
Soby Mathew981487a2015-07-13 14:10:57 +010084 target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
Sandrine Bailleuxf9f3bbf2016-06-22 16:35:01 +010085 if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
86 ERROR("Invalid target power level for suspend operation\n");
87 panic();
88 }
Soby Mathew981487a2015-07-13 14:10:57 +010089
90 /* Fast path for CPU standby.*/
Antonio Nino Diazde11a5b2018-08-01 16:42:10 +010091 if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +010092 if (psci_plat_pm_ops->cpu_standby == NULL)
Soby Mathew74e52a72014-10-02 16:56:51 +010093 return PSCI_E_INVALID_PARAMS;
Soby Mathew74e52a72014-10-02 16:56:51 +010094
Soby Mathew981487a2015-07-13 14:10:57 +010095 /*
96 * Set the state of the CPU power domain to the platform
97 * specific retention state and enter the standby state.
98 */
99 cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
100 psci_set_cpu_local_state(cpu_pd_state);
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100101
Wing Li2c556f32022-09-14 13:18:17 -0700102#if PSCI_OS_INIT_MODE
103 /*
104 * If in OS-initiated mode, save a copy of the previous
105 * requested local power states and update the new requested
106 * local power states for this CPU.
107 */
108 if (psci_suspend_mode == OS_INIT) {
109 psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx,
110 &state_info, prev);
111 }
112#endif
113
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100114#if ENABLE_PSCI_STAT
dp-arm66abfbe2017-01-31 13:01:04 +0000115 plat_psci_stat_accounting_start(&state_info);
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100116#endif
117
dp-arm3cac7862016-09-19 11:18:44 +0100118#if ENABLE_RUNTIME_INSTRUMENTATION
119 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
120 RT_INSTR_ENTER_HW_LOW_PWR,
121 PMF_NO_CACHE_MAINT);
122#endif
123
Soby Mathew981487a2015-07-13 14:10:57 +0100124 psci_plat_pm_ops->cpu_standby(cpu_pd_state);
Achin Gupta42c52802014-05-09 19:32:25 +0100125
Soby Mathew981487a2015-07-13 14:10:57 +0100126 /* Upon exit from standby, set the state back to RUN. */
127 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
Achin Gupta42c52802014-05-09 19:32:25 +0100128
Wing Li2c556f32022-09-14 13:18:17 -0700129#if PSCI_OS_INIT_MODE
130 /*
131 * If in OS-initiated mode, restore the previous requested
132 * local power states for this CPU.
133 */
134 if (psci_suspend_mode == OS_INIT) {
135 psci_restore_req_local_pwr_states(cpu_idx, prev);
136 }
137#endif
138
dp-arm3cac7862016-09-19 11:18:44 +0100139#if ENABLE_RUNTIME_INSTRUMENTATION
140 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
141 RT_INSTR_EXIT_HW_LOW_PWR,
142 PMF_NO_CACHE_MAINT);
143#endif
144
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100145#if ENABLE_PSCI_STAT
dp-arm66abfbe2017-01-31 13:01:04 +0000146 plat_psci_stat_accounting_stop(&state_info);
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100147
148 /* Update PSCI stats */
dp-arm66abfbe2017-01-31 13:01:04 +0000149 psci_stats_update_pwr_up(PSCI_CPU_PWR_LVL, &state_info);
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100150#endif
151
Soby Mathew74e52a72014-10-02 16:56:51 +0100152 return PSCI_E_SUCCESS;
Vikram Kanigiri3b7c59b2014-03-21 11:57:10 +0000153 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100154
Achin Gupta42c52802014-05-09 19:32:25 +0100155 /*
Soby Mathew981487a2015-07-13 14:10:57 +0100156 * If a power down state has been requested, we need to verify entry
157 * point and program entry information.
Soby Mathew8595b872015-01-06 15:36:38 +0000158 */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100159 if (is_power_down_state != 0U) {
Soby Mathewf1f97a12015-07-15 12:13:26 +0100160 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathew981487a2015-07-13 14:10:57 +0100161 if (rc != PSCI_E_SUCCESS)
162 return rc;
163 }
Soby Mathewf5121572014-09-30 11:19:51 +0100164
Soby Mathew8595b872015-01-06 15:36:38 +0000165 /*
Achin Gupta42c52802014-05-09 19:32:25 +0100166 * Do what is needed to enter the power down state. Upon success,
Soby Mathew981487a2015-07-13 14:10:57 +0100167 * enter the final wfi which will power down this CPU. This function
168 * might return if the power down was abandoned for any reason, e.g.
169 * arrival of an interrupt
Achin Gupta42c52802014-05-09 19:32:25 +0100170 */
Wing Li2c556f32022-09-14 13:18:17 -0700171 rc = psci_cpu_suspend_start(&ep,
172 target_pwrlvl,
173 &state_info,
174 is_power_down_state);
Soby Mathew74e52a72014-10-02 16:56:51 +0100175
Wing Li2c556f32022-09-14 13:18:17 -0700176 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100177}
178
Soby Mathew011ca182015-07-29 17:05:03 +0100179
180int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
Soby Mathew96168382014-12-17 14:47:57 +0000181{
182 int rc;
Soby Mathew981487a2015-07-13 14:10:57 +0100183 psci_power_state_t state_info;
Soby Mathew96168382014-12-17 14:47:57 +0000184 entry_point_info_t ep;
185
Soby Mathew96168382014-12-17 14:47:57 +0000186 /* Check if the current CPU is the last ON CPU in the system */
Jayanth Dodderi Chidanand70763502022-08-22 23:46:10 +0100187 if (!psci_is_last_on_cpu())
Soby Mathew96168382014-12-17 14:47:57 +0000188 return PSCI_E_DENIED;
189
Soby Mathewf1f97a12015-07-15 12:13:26 +0100190 /* Validate the entry point and get the entry_point_info */
191 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathew96168382014-12-17 14:47:57 +0000192 if (rc != PSCI_E_SUCCESS)
193 return rc;
194
Soby Mathew981487a2015-07-13 14:10:57 +0100195 /* Query the psci_power_state for system suspend */
196 psci_query_sys_suspend_pwrstate(&state_info);
Soby Mathew96168382014-12-17 14:47:57 +0000197
ldts1821db22018-10-11 08:40:32 +0200198 /*
199 * Check if platform allows suspend to Highest power level
200 * (System level)
201 */
202 if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL)
203 return PSCI_E_DENIED;
204
Soby Mathew981487a2015-07-13 14:10:57 +0100205 /* Ensure that the psci_power_state makes sense */
Soby Mathew981487a2015-07-13 14:10:57 +0100206 assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
207 == PSCI_E_SUCCESS);
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100208 assert(is_local_state_off(
209 state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
Soby Mathew96168382014-12-17 14:47:57 +0000210
211 /*
Soby Mathew981487a2015-07-13 14:10:57 +0100212 * Do what is needed to enter the system suspend state. This function
213 * might return if the power down was abandoned for any reason, e.g.
214 * arrival of an interrupt
Soby Mathew96168382014-12-17 14:47:57 +0000215 */
Wing Li2c556f32022-09-14 13:18:17 -0700216 rc = psci_cpu_suspend_start(&ep,
217 PLAT_MAX_PWR_LVL,
218 &state_info,
219 PSTATE_TYPE_POWERDOWN);
Soby Mathew96168382014-12-17 14:47:57 +0000220
Wing Li2c556f32022-09-14 13:18:17 -0700221 return rc;
Soby Mathew96168382014-12-17 14:47:57 +0000222}
223
Achin Gupta4f6ad662013-10-25 09:08:21 +0100224int psci_cpu_off(void)
225{
226 int rc;
Soby Mathew011ca182015-07-29 17:05:03 +0100227 unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100228
Achin Gupta4f6ad662013-10-25 09:08:21 +0100229 /*
Soby Mathew981487a2015-07-13 14:10:57 +0100230 * Do what is needed to power off this CPU and possible higher power
231 * levels if it able to do so. Upon success, enter the final wfi
232 * which will power down this CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100233 */
Soby Mathew981487a2015-07-13 14:10:57 +0100234 rc = psci_do_cpu_off(target_pwrlvl);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100235
Achin Gupta3140a9e2013-12-02 16:23:12 +0000236 /*
237 * The only error cpu_off can return is E_DENIED. So check if that's
238 * indeed the case.
239 */
Soby Mathew24ab34f2016-05-03 17:11:42 +0100240 assert(rc == PSCI_E_DENIED);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100241
242 return rc;
243}
244
Soby Mathew011ca182015-07-29 17:05:03 +0100245int psci_affinity_info(u_register_t target_affinity,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100246 unsigned int lowest_affinity_level)
247{
Deepika Bhavnani4287c0c2019-12-13 10:23:18 -0600248 int ret;
249 unsigned int target_idx;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100250
Soby Mathew981487a2015-07-13 14:10:57 +0100251 /* We dont support level higher than PSCI_CPU_PWR_LVL */
252 if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
253 return PSCI_E_INVALID_PARAMS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100254
Soby Mathew981487a2015-07-13 14:10:57 +0100255 /* Calculate the cpu index of the target */
Deepika Bhavnani4287c0c2019-12-13 10:23:18 -0600256 ret = plat_core_pos_by_mpidr(target_affinity);
257 if (ret == -1) {
Soby Mathew981487a2015-07-13 14:10:57 +0100258 return PSCI_E_INVALID_PARAMS;
Deepika Bhavnani4287c0c2019-12-13 10:23:18 -0600259 }
260 target_idx = (unsigned int)ret;
Achin Gupta75f73672013-12-05 16:33:10 +0000261
Roberto Vargas6dc82142017-11-13 08:24:07 +0000262 /*
263 * Generic management:
264 * Perform cache maintanence ahead of reading the target CPU state to
265 * ensure that the data is not stale.
266 * There is a theoretical edge case where the cache may contain stale
267 * data for the target CPU data - this can occur under the following
268 * conditions:
269 * - the target CPU is in another cluster from the current
270 * - the target CPU was the last CPU to shutdown on its cluster
271 * - the cluster was removed from coherency as part of the CPU shutdown
272 *
273 * In this case the cache maintenace that was performed as part of the
274 * target CPUs shutdown was not seen by the current CPU's cluster. And
275 * so the cache may contain stale data for the target CPU.
276 */
Deepika Bhavnani4287c0c2019-12-13 10:23:18 -0600277 flush_cpu_data_by_index(target_idx,
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100278 psci_svc_cpu_data.aff_info_state);
Roberto Vargas6dc82142017-11-13 08:24:07 +0000279
Soby Mathew981487a2015-07-13 14:10:57 +0100280 return psci_get_aff_info_state_by_idx(target_idx);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100281}
282
Soby Mathew011ca182015-07-29 17:05:03 +0100283int psci_migrate(u_register_t target_cpu)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100284{
Soby Mathew110fe362014-10-23 10:35:34 +0100285 int rc;
Soby Mathew011ca182015-07-29 17:05:03 +0100286 u_register_t resident_cpu_mpidr;
Soby Mathew110fe362014-10-23 10:35:34 +0100287
288 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
289 if (rc != PSCI_TOS_UP_MIG_CAP)
290 return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
291 PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
292
293 /*
294 * Migrate should only be invoked on the CPU where
295 * the Secure OS is resident.
296 */
297 if (resident_cpu_mpidr != read_mpidr_el1())
298 return PSCI_E_NOT_PRESENT;
299
300 /* Check the validity of the specified target cpu */
Soby Mathew981487a2015-07-13 14:10:57 +0100301 rc = psci_validate_mpidr(target_cpu);
Soby Mathew110fe362014-10-23 10:35:34 +0100302 if (rc != PSCI_E_SUCCESS)
303 return PSCI_E_INVALID_PARAMS;
304
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100305 assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
Soby Mathew110fe362014-10-23 10:35:34 +0100306
307 rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100308 assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
Soby Mathew110fe362014-10-23 10:35:34 +0100309
310 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100311}
312
Soby Mathew110fe362014-10-23 10:35:34 +0100313int psci_migrate_info_type(void)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100314{
Soby Mathew011ca182015-07-29 17:05:03 +0100315 u_register_t resident_cpu_mpidr;
Soby Mathew110fe362014-10-23 10:35:34 +0100316
317 return psci_spd_migrate_info(&resident_cpu_mpidr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100318}
319
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100320u_register_t psci_migrate_info_up_cpu(void)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100321{
Soby Mathew011ca182015-07-29 17:05:03 +0100322 u_register_t resident_cpu_mpidr;
Soby Mathew110fe362014-10-23 10:35:34 +0100323 int rc;
324
Achin Gupta4f6ad662013-10-25 09:08:21 +0100325 /*
Soby Mathew110fe362014-10-23 10:35:34 +0100326 * Return value of this depends upon what
327 * psci_spd_migrate_info() returns.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100328 */
Soby Mathew110fe362014-10-23 10:35:34 +0100329 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100330 if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
331 return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
Soby Mathew110fe362014-10-23 10:35:34 +0100332
333 return resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100334}
335
Jeenu Viswambharan7f03e9d92016-08-03 15:54:50 +0100336int psci_node_hw_state(u_register_t target_cpu,
337 unsigned int power_level)
338{
339 int rc;
340
341 /* Validate target_cpu */
342 rc = psci_validate_mpidr(target_cpu);
343 if (rc != PSCI_E_SUCCESS)
344 return PSCI_E_INVALID_PARAMS;
345
346 /* Validate power_level against PLAT_MAX_PWR_LVL */
347 if (power_level > PLAT_MAX_PWR_LVL)
348 return PSCI_E_INVALID_PARAMS;
349
350 /*
351 * Dispatch this call to platform to query power controller, and pass on
352 * to the caller what it returns
353 */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100354 assert(psci_plat_pm_ops->get_node_hw_state != NULL);
Jeenu Viswambharan7f03e9d92016-08-03 15:54:50 +0100355 rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100356 assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
357 || (rc == PSCI_E_NOT_SUPPORTED)
358 || (rc == PSCI_E_INVALID_PARAMS));
Jeenu Viswambharan7f03e9d92016-08-03 15:54:50 +0100359 return rc;
360}
361
Soby Mathew6cdddaf2015-01-07 11:10:22 +0000362int psci_features(unsigned int psci_fid)
363{
Soby Mathew011ca182015-07-29 17:05:03 +0100364 unsigned int local_caps = psci_caps;
Soby Mathew6cdddaf2015-01-07 11:10:22 +0000365
Dimitris Papastamosa65841a2018-01-22 12:58:52 +0000366 if (psci_fid == SMCCC_VERSION)
367 return PSCI_E_SUCCESS;
368
Soby Mathew6cdddaf2015-01-07 11:10:22 +0000369 /* Check if it is a 64 bit function */
370 if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64)
371 local_caps &= PSCI_CAP_64BIT_MASK;
372
373 /* Check for invalid fid */
374 if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
375 && is_psci_fid(psci_fid)))
376 return PSCI_E_NOT_SUPPORTED;
377
378
379 /* Check if the psci fid is supported or not */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100380 if ((local_caps & define_psci_cap(psci_fid)) == 0U)
Soby Mathew6cdddaf2015-01-07 11:10:22 +0000381 return PSCI_E_NOT_SUPPORTED;
382
383 /* Format the feature flags */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100384 if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
385 (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100386 unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
Wing Lid28393a2022-09-14 13:18:19 -0700387 (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
388 return (int)ret;
Soby Mathew6cdddaf2015-01-07 11:10:22 +0000389 }
390
391 /* Return 0 for all other fid's */
392 return PSCI_E_SUCCESS;
393}
394
Wing Li71f69df2022-09-14 13:18:15 -0700395#if PSCI_OS_INIT_MODE
396int psci_set_suspend_mode(unsigned int mode)
397{
398 if (psci_suspend_mode == mode) {
399 return PSCI_E_SUCCESS;
400 }
401
402 if (mode == PLAT_COORD) {
403 /* Check if the current CPU is the last ON CPU in the system */
404 if (!psci_is_last_on_cpu_safe()) {
405 return PSCI_E_DENIED;
406 }
407 }
408
409 if (mode == OS_INIT) {
410 /*
411 * Check if all CPUs in the system are ON or if the current
412 * CPU is the last ON CPU in the system.
413 */
414 if (!(psci_are_all_cpus_on_safe() ||
415 psci_is_last_on_cpu_safe())) {
416 return PSCI_E_DENIED;
417 }
418 }
419
420 psci_suspend_mode = mode;
421 psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
422 sizeof(psci_suspend_mode));
423
424 return PSCI_E_SUCCESS;
425}
426#endif
427
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000428/*******************************************************************************
429 * PSCI top level handler for servicing SMCs.
430 ******************************************************************************/
Soby Mathewd0194872016-04-29 19:01:30 +0100431u_register_t psci_smc_handler(uint32_t smc_fid,
Soby Mathewa0fedc42016-06-16 14:52:04 +0100432 u_register_t x1,
433 u_register_t x2,
434 u_register_t x3,
435 u_register_t x4,
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000436 void *cookie,
437 void *handle,
Soby Mathewa0fedc42016-06-16 14:52:04 +0100438 u_register_t flags)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000439{
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100440 u_register_t ret;
441
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100442 if (is_caller_secure(flags))
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100443 return (u_register_t)SMC_UNK;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000444
Soby Mathew61e615b2015-01-15 11:49:49 +0000445 /* Check the fid against the capabilities */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100446 if ((psci_caps & define_psci_cap(smc_fid)) == 0U)
447 return (u_register_t)SMC_UNK;
Soby Mathew61e615b2015-01-15 11:49:49 +0000448
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100449 if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
450 /* 32-bit PSCI function, clear top parameter bits */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000451
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100452 uint32_t r1 = (uint32_t)x1;
453 uint32_t r2 = (uint32_t)x2;
454 uint32_t r3 = (uint32_t)x3;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000455
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100456 switch (smc_fid) {
457 case PSCI_VERSION:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100458 ret = (u_register_t)psci_version();
459 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000460
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100461 case PSCI_CPU_OFF:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100462 ret = (u_register_t)psci_cpu_off();
463 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000464
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100465 case PSCI_CPU_SUSPEND_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100466 ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
467 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000468
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100469 case PSCI_CPU_ON_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100470 ret = (u_register_t)psci_cpu_on(r1, r2, r3);
471 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000472
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100473 case PSCI_AFFINITY_INFO_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100474 ret = (u_register_t)psci_affinity_info(r1, r2);
475 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000476
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100477 case PSCI_MIG_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100478 ret = (u_register_t)psci_migrate(r1);
479 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000480
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100481 case PSCI_MIG_INFO_TYPE:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100482 ret = (u_register_t)psci_migrate_info_type();
483 break;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100484
485 case PSCI_MIG_INFO_UP_CPU_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100486 ret = psci_migrate_info_up_cpu();
487 break;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100488
Jeenu Viswambharan7f03e9d92016-08-03 15:54:50 +0100489 case PSCI_NODE_HW_STATE_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100490 ret = (u_register_t)psci_node_hw_state(r1, r2);
491 break;
Jeenu Viswambharan7f03e9d92016-08-03 15:54:50 +0100492
Soby Mathew96168382014-12-17 14:47:57 +0000493 case PSCI_SYSTEM_SUSPEND_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100494 ret = (u_register_t)psci_system_suspend(r1, r2);
495 break;
Soby Mathew96168382014-12-17 14:47:57 +0000496
Juan Castillo4dc4a472014-08-12 11:17:06 +0100497 case PSCI_SYSTEM_OFF:
498 psci_system_off();
499 /* We should never return from psci_system_off() */
Jonathan Wrightde701832018-03-13 17:45:42 +0000500 break;
Juan Castillo4dc4a472014-08-12 11:17:06 +0100501
502 case PSCI_SYSTEM_RESET:
503 psci_system_reset();
504 /* We should never return from psci_system_reset() */
Jonathan Wrightde701832018-03-13 17:45:42 +0000505 break;
Juan Castillo4dc4a472014-08-12 11:17:06 +0100506
Soby Mathew6cdddaf2015-01-07 11:10:22 +0000507 case PSCI_FEATURES:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100508 ret = (u_register_t)psci_features(r1);
509 break;
Wing Li71f69df2022-09-14 13:18:15 -0700510
511#if PSCI_OS_INIT_MODE
512 case PSCI_SET_SUSPEND_MODE:
513 ret = (u_register_t)psci_set_suspend_mode(r1);
514 break;
515#endif
Soby Mathew6cdddaf2015-01-07 11:10:22 +0000516
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100517#if ENABLE_PSCI_STAT
518 case PSCI_STAT_RESIDENCY_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100519 ret = psci_stat_residency(r1, r2);
520 break;
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100521
522 case PSCI_STAT_COUNT_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100523 ret = psci_stat_count(r1, r2);
524 break;
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100525#endif
Roberto Vargas0a4c2612017-08-03 08:16:16 +0100526 case PSCI_MEM_PROTECT:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100527 ret = psci_mem_protect(r1);
528 break;
Roberto Vargas0a4c2612017-08-03 08:16:16 +0100529
530 case PSCI_MEM_CHK_RANGE_AARCH32:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100531 ret = psci_mem_chk_range(r1, r2);
532 break;
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100533
Roberto Vargasb820ad02017-07-26 09:23:09 +0100534 case PSCI_SYSTEM_RESET2_AARCH32:
535 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100536 ret = psci_system_reset2(r1, r2);
537 break;
Roberto Vargasb820ad02017-07-26 09:23:09 +0100538
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100539 default:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100540 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
541 ret = (u_register_t)SMC_UNK;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100542 break;
543 }
544 } else {
545 /* 64-bit PSCI function */
546
547 switch (smc_fid) {
548 case PSCI_CPU_SUSPEND_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100549 ret = (u_register_t)
550 psci_cpu_suspend((unsigned int)x1, x2, x3);
551 break;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100552
553 case PSCI_CPU_ON_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100554 ret = (u_register_t)psci_cpu_on(x1, x2, x3);
555 break;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100556
557 case PSCI_AFFINITY_INFO_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100558 ret = (u_register_t)
559 psci_affinity_info(x1, (unsigned int)x2);
560 break;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100561
562 case PSCI_MIG_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100563 ret = (u_register_t)psci_migrate(x1);
564 break;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100565
566 case PSCI_MIG_INFO_UP_CPU_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100567 ret = psci_migrate_info_up_cpu();
568 break;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100569
Jeenu Viswambharan7f03e9d92016-08-03 15:54:50 +0100570 case PSCI_NODE_HW_STATE_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100571 ret = (u_register_t)psci_node_hw_state(
572 x1, (unsigned int) x2);
573 break;
Jeenu Viswambharan7f03e9d92016-08-03 15:54:50 +0100574
Soby Mathew96168382014-12-17 14:47:57 +0000575 case PSCI_SYSTEM_SUSPEND_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100576 ret = (u_register_t)psci_system_suspend(x1, x2);
577 break;
Soby Mathew96168382014-12-17 14:47:57 +0000578
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100579#if ENABLE_PSCI_STAT
580 case PSCI_STAT_RESIDENCY_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100581 ret = psci_stat_residency(x1, (unsigned int) x2);
582 break;
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100583
584 case PSCI_STAT_COUNT_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100585 ret = psci_stat_count(x1, (unsigned int) x2);
586 break;
Yatharth Kochar241ec6c2016-05-09 18:26:35 +0100587#endif
588
Roberto Vargas0a4c2612017-08-03 08:16:16 +0100589 case PSCI_MEM_CHK_RANGE_AARCH64:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100590 ret = psci_mem_chk_range(x1, x2);
591 break;
Roberto Vargas0a4c2612017-08-03 08:16:16 +0100592
Roberto Vargasb820ad02017-07-26 09:23:09 +0100593 case PSCI_SYSTEM_RESET2_AARCH64:
594 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100595 ret = psci_system_reset2((uint32_t) x1, x2);
596 break;
Roberto Vargas0a4c2612017-08-03 08:16:16 +0100597
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100598 default:
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100599 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
600 ret = (u_register_t)SMC_UNK;
Andrew Thoelke89a3c842014-06-10 16:37:37 +0100601 break;
602 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000603 }
604
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100605 return ret;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000606}