blob: 1c365b48bc1ef355b0000c51df4dfbbc9c3862c4 [file] [log] [blame]
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +05301/*
Michal Simek2a47faa2023-04-14 08:43:51 +02002 * Copyright (c) 2018-2021, Arm Limited and Contributors. All rights reserved.
Devanshi Chauhan Alpeshbhaiee5a5d62025-03-26 01:50:27 -07003 * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +05304 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
Tejas Patel54d13192019-02-27 18:44:55 +05308#include <assert.h>
Prasad Kummari536e1102023-06-22 10:50:02 +05309
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <common/debug.h>
11#include <lib/mmio.h>
12#include <lib/psci/psci.h>
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -080013#include <plat/arm/common/plat_arm.h>
Prasad Kummari536e1102023-06-22 10:50:02 +053014#include <plat/common/platform.h>
15#include <plat_arm.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -070017#include "drivers/delay_timer.h"
Prasad Kummari536e1102023-06-22 10:50:02 +053018#include <plat_private.h>
Tejas Patel61717112019-02-27 18:44:57 +053019#include "pm_api_sys.h"
20#include "pm_client.h"
Prasad Kummari536e1102023-06-22 10:50:02 +053021#include <pm_common.h>
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -070022#include "pm_ipi.h"
23#include "pm_svc_main.h"
Tejas Patel61717112019-02-27 18:44:57 +053024
Devanshi Chauhan Alpeshbhaiee5a5d62025-03-26 01:50:27 -070025#define SEC_ENTRY_ADDRESS_MASK 0xFFFFFFFFUL
26#define RESUME_ADDR_SET 0x1UL
27
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053028static uintptr_t versal_sec_entry;
29
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053030static int32_t versal_pwr_domain_on(u_register_t mpidr)
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053031{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053032 int32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
Tejas Patel61717112019-02-27 18:44:57 +053033 const struct pm_proc *proc;
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +000034 int32_t ret = PSCI_E_INTERN_FAIL;
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053035
36 VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr);
37
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070038 if (cpu_id == -1) {
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +000039 goto exit_label;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070040 }
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053041
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053042 proc = pm_get_proc((uint32_t)cpu_id);
Ronak Jain807f41b2024-05-08 02:41:13 -070043 if (proc == NULL) {
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +000044 goto exit_label;
Michal Simekb8eca3b2024-04-19 12:16:46 +020045 }
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053046
Tejas Patel61717112019-02-27 18:44:57 +053047 /* Send request to PMC to wake up selected ACPU core */
Devanshi Chauhan Alpeshbhaiee5a5d62025-03-26 01:50:27 -070048 (void)pm_req_wakeup(proc->node_id,
49 (uint32_t)((versal_sec_entry & SEC_ENTRY_ADDRESS_MASK) |
50 RESUME_ADDR_SET), versal_sec_entry >> 32, 0, SECURE_FLAG);
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053051
Tejas Patel61717112019-02-27 18:44:57 +053052 /* Clear power down request */
53 pm_client_wakeup(proc);
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053054
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +000055 ret = PSCI_E_SUCCESS;
56
57exit_label:
58 return ret;
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053059}
60
Tejas Patel54d13192019-02-27 18:44:55 +053061/**
62 * versal_pwr_domain_suspend() - This function sends request to PMC to suspend
Prasad Kummari7d0623a2023-06-09 14:32:00 +053063 * core.
64 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +053065 *
Tejas Patel54d13192019-02-27 18:44:55 +053066 */
67static void versal_pwr_domain_suspend(const psci_power_state_t *target_state)
68{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053069 uint32_t state;
70 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +053071 const struct pm_proc *proc = pm_get_proc(cpu_id);
72
Ronak Jain807f41b2024-05-08 02:41:13 -070073 if (proc == NULL) {
Michal Simekb8eca3b2024-04-19 12:16:46 +020074 return;
75 }
76
Abhyuday Godhasara589afa52021-08-11 06:15:13 -070077 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +053078 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
79 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070080 }
Tejas Patel54d13192019-02-27 18:44:55 +053081
82 plat_versal_gic_cpuif_disable();
83
Ravi Pateleafc8782019-06-21 05:00:49 -070084 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
85 plat_versal_gic_save();
86 }
Tejas Patel54d13192019-02-27 18:44:55 +053087
Maheedhar Bollapallicc64a792024-10-14 04:16:03 +000088 state = (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) ?
Tejas Patel54d13192019-02-27 18:44:55 +053089 PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE;
90
91 /* Send request to PMC to suspend this core */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -070092 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_sec_entry,
93 SECURE_FLAG);
Tejas Patel54d13192019-02-27 18:44:55 +053094
95 /* APU is to be turned off */
96 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
97 /* disable coherency */
98 plat_arm_interconnect_exit_coherency();
99 }
100}
101
102/**
103 * versal_pwr_domain_suspend_finish() - This function performs actions to finish
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530104 * suspend procedure.
105 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +0530106 *
Tejas Patel54d13192019-02-27 18:44:55 +0530107 */
108static void versal_pwr_domain_suspend_finish(
109 const psci_power_state_t *target_state)
110{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530111 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +0530112 const struct pm_proc *proc = pm_get_proc(cpu_id);
113
Ronak Jain807f41b2024-05-08 02:41:13 -0700114 if (proc == NULL) {
Michal Simekb8eca3b2024-04-19 12:16:46 +0200115 return;
116 }
117
Abhyuday Godhasara589afa52021-08-11 06:15:13 -0700118 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +0530119 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
120 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700121 }
Tejas Patel54d13192019-02-27 18:44:55 +0530122
123 /* Clear the APU power control register for this cpu */
124 pm_client_wakeup(proc);
125
126 /* enable coherency */
127 plat_arm_interconnect_enter_coherency();
128
129 /* APU was turned off, so restore GIC context */
130 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
131 plat_versal_gic_resume();
Tejas Patel54d13192019-02-27 18:44:55 +0530132 }
Ravi Pateleafc8782019-06-21 05:00:49 -0700133
134 plat_versal_gic_cpuif_enable();
Tejas Patel54d13192019-02-27 18:44:55 +0530135}
136
Maheedhar Bollapalli86d6d702024-10-07 09:27:58 +0000137static void versal_pwr_domain_on_finish(const psci_power_state_t *target_state)
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530138{
Devanshi Chauhan Alpeshbhai0fac09c2025-03-23 23:47:26 -0700139 /*
140 * Typecasting to void to intentionally retain the variable and avoid
141 * MISRA violation for unused parameters. This may be used in the
142 * future if specific action is required based on CPU power state.
143 */
144 (void)target_state;
145
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530146 /* Enable the gic cpu interface */
147 plat_versal_gic_pcpu_init();
148
149 /* Program the gic per-cpu distributor or re-distributor interface */
150 plat_versal_gic_cpuif_enable();
151}
152
Tejas Patel54d13192019-02-27 18:44:55 +0530153/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530154 * versal_system_off() - This function sends the system off request to firmware.
155 * This function does not return.
156 *
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800157 */
158static void __dead2 versal_system_off(void)
159{
160 /* Send the power down request to the PMC */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -0700161 (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN,
162 pm_get_shutdown_scope(), SECURE_FLAG);
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800163
Maheedhar Bollapallie897f0a2024-10-14 06:45:13 +0000164 while (true) {
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800165 wfi();
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700166 }
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800167}
168
169/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530170 * versal_system_reset() - This function sends the reset request to firmware
171 * for the system to reset. This function does not
172 * return.
173 *
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800174 */
175static void __dead2 versal_system_reset(void)
176{
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -0700177 uint32_t ret, timeout = 10000U;
178
179 request_cpu_pwrdwn();
180
181 /*
182 * Send the system reset request to the firmware if power down request
183 * is not received from firmware.
184 */
185 if (!pwrdwn_req_received) {
186 (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET,
187 pm_get_shutdown_scope(), SECURE_FLAG);
188
189 /*
190 * Wait for system shutdown request completed and idle callback
191 * not received.
192 */
193 do {
Devanshi Chauhan Alpeshbhaib3d4c9f2025-03-26 02:08:58 -0700194 ret = ipi_mb_enquire_status(primary_proc->ipi->local_ipi_id,
195 primary_proc->ipi->remote_ipi_id);
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -0700196 udelay(100);
197 timeout--;
198 } while ((ret != IPI_MB_STATUS_RECV_PENDING) && (timeout > 0U));
199 }
200
201 (void)psci_cpu_off();
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800202
Maheedhar Bollapallie897f0a2024-10-14 06:45:13 +0000203 while (true) {
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800204 wfi();
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700205 }
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800206}
207
Maheedhar Bollapalli5d6cf5b2025-02-17 15:52:24 +0530208static int32_t versal_validate_ns_entrypoint(uint64_t ns_entrypoint)
209{
210 int32_t ret = PSCI_E_SUCCESS;
211
212 if (((ns_entrypoint >= PLAT_DDR_LOWMEM_MAX) && (ns_entrypoint <= PLAT_DDR_HIGHMEM_MAX)) ||
213 ((ns_entrypoint >= BL31_BASE) && (ns_entrypoint <= BL31_LIMIT))) {
214 ret = PSCI_E_INVALID_ADDRESS;
215 }
216
217 return ret;
218}
219
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800220/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530221 * versal_pwr_domain_off() - This function performs actions to turn off core.
222 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +0530223 *
Tejas Patel54d13192019-02-27 18:44:55 +0530224 */
225static void versal_pwr_domain_off(const psci_power_state_t *target_state)
226{
Maheedhar Bollapalli493c7a22024-10-08 05:42:28 +0000227 uint32_t ret, fw_api_version, version_type[RET_PAYLOAD_ARG_CNT] = {0U};
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530228 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +0530229 const struct pm_proc *proc = pm_get_proc(cpu_id);
230
Ronak Jain807f41b2024-05-08 02:41:13 -0700231 if (proc == NULL) {
Michal Simekb8eca3b2024-04-19 12:16:46 +0200232 return;
233 }
234
Abhyuday Godhasara589afa52021-08-11 06:15:13 -0700235 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +0530236 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
237 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700238 }
Tejas Patel54d13192019-02-27 18:44:55 +0530239
240 /* Prevent interrupts from spuriously waking up this cpu */
241 plat_versal_gic_cpuif_disable();
242
243 /*
244 * Send request to PMC to power down the appropriate APU CPU
245 * core.
246 * According to PSCI specification, CPU_off function does not
247 * have resume address and CPU core can only be woken up
248 * invoking CPU_on function, during which resume address will
249 * be set.
250 */
Devanshi Chauhan Alpeshbhaiee5a5d62025-03-26 01:50:27 -0700251 ret = (uint32_t)pm_feature_check((uint32_t)PM_SELF_SUSPEND,
252 &version_type[0], SECURE_FLAG);
Maheedhar Bollapallib3c92e62024-10-21 05:23:53 +0000253 if (ret == (uint32_t)PM_RET_SUCCESS) {
Maheedhar Bollapalli493c7a22024-10-08 05:42:28 +0000254 fw_api_version = version_type[0] & 0xFFFFU;
Jay Buddhabhatti31488a32023-09-11 23:50:06 -0700255 if (fw_api_version >= 3U) {
256 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_OFF, 0,
257 SECURE_FLAG);
258 } else {
259 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0,
260 SECURE_FLAG);
261 }
262 }
Tejas Patel54d13192019-02-27 18:44:55 +0530263}
264
265/**
266 * versal_validate_power_state() - This function ensures that the power state
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530267 * parameter in request is valid.
268 * @power_state: Power state of core.
269 * @req_state: Requested state.
Tejas Patel54d13192019-02-27 18:44:55 +0530270 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530271 * Return: Returns status, either success or reason.
Tejas Patel54d13192019-02-27 18:44:55 +0530272 *
Tejas Patel54d13192019-02-27 18:44:55 +0530273 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530274static int32_t versal_validate_power_state(uint32_t power_state,
Tejas Patel54d13192019-02-27 18:44:55 +0530275 psci_power_state_t *req_state)
276{
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +0000277 int32_t ret = PSCI_E_SUCCESS;
Tejas Patel54d13192019-02-27 18:44:55 +0530278 VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
279
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530280 uint32_t pstate = psci_get_pstate_type(power_state);
Tejas Patel54d13192019-02-27 18:44:55 +0530281
Maheedhar Bollapalli15fa5b52024-09-27 05:52:21 +0000282 assert(req_state != NULL);
Tejas Patel54d13192019-02-27 18:44:55 +0530283
284 /* Sanity check the requested state */
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700285 if (pstate == PSTATE_TYPE_STANDBY) {
Tejas Patel54d13192019-02-27 18:44:55 +0530286 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700287 } else {
Tejas Patel54d13192019-02-27 18:44:55 +0530288 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700289 }
Tejas Patel54d13192019-02-27 18:44:55 +0530290
291 /* We expect the 'state id' to be zero */
Abhyuday Godhasarabacbdee2021-08-20 00:27:03 -0700292 if (psci_get_pstate_id(power_state) != 0U) {
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +0000293 ret = PSCI_E_INVALID_PARAMS;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700294 }
Tejas Patel54d13192019-02-27 18:44:55 +0530295
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +0000296 return ret;
Tejas Patel54d13192019-02-27 18:44:55 +0530297}
298
299/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530300 * versal_get_sys_suspend_power_state() - Get power state for system suspend.
301 * @req_state: Requested state.
Tejas Patel54d13192019-02-27 18:44:55 +0530302 *
Tejas Patel54d13192019-02-27 18:44:55 +0530303 */
304static void versal_get_sys_suspend_power_state(psci_power_state_t *req_state)
305{
306 req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE;
307 req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE;
308}
309
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530310static const struct plat_psci_ops versal_nopmc_psci_ops = {
Tejas Patel61717112019-02-27 18:44:57 +0530311 .pwr_domain_on = versal_pwr_domain_on,
Tejas Patel54d13192019-02-27 18:44:55 +0530312 .pwr_domain_off = versal_pwr_domain_off,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530313 .pwr_domain_on_finish = versal_pwr_domain_on_finish,
Tejas Patel54d13192019-02-27 18:44:55 +0530314 .pwr_domain_suspend = versal_pwr_domain_suspend,
315 .pwr_domain_suspend_finish = versal_pwr_domain_suspend_finish,
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800316 .system_off = versal_system_off,
317 .system_reset = versal_system_reset,
Maheedhar Bollapalli5d6cf5b2025-02-17 15:52:24 +0530318 .validate_ns_entrypoint = versal_validate_ns_entrypoint,
Tejas Patel54d13192019-02-27 18:44:55 +0530319 .validate_power_state = versal_validate_power_state,
320 .get_sys_suspend_power_state = versal_get_sys_suspend_power_state,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530321};
322
323/*******************************************************************************
324 * Export the platform specific power ops.
325 ******************************************************************************/
Devanshi Chauhan Alpeshbhaibbde4eb2025-03-18 02:44:12 -0700326int plat_setup_psci_ops(uintptr_t sec_entrypoint,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530327 const struct plat_psci_ops **psci_ops)
328{
329 versal_sec_entry = sec_entrypoint;
330
331 *psci_ops = &versal_nopmc_psci_ops;
332
333 return 0;
334}