blob: 396d7c725a086da4689e499ddb1d86515fb85285 [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.
Michal Simekb8eca3b2024-04-19 12:16:46 +02003 * Copyright (c) 2022-2024, 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
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053025static uintptr_t versal_sec_entry;
26
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053027static int32_t versal_pwr_domain_on(u_register_t mpidr)
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053028{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053029 int32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
Tejas Patel61717112019-02-27 18:44:57 +053030 const struct pm_proc *proc;
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +000031 int32_t ret = PSCI_E_INTERN_FAIL;
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053032
33 VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr);
34
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070035 if (cpu_id == -1) {
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +000036 goto exit_label;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070037 }
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053038
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053039 proc = pm_get_proc((uint32_t)cpu_id);
Ronak Jain807f41b2024-05-08 02:41:13 -070040 if (proc == NULL) {
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +000041 goto exit_label;
Michal Simekb8eca3b2024-04-19 12:16:46 +020042 }
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053043
Tejas Patel61717112019-02-27 18:44:57 +053044 /* Send request to PMC to wake up selected ACPU core */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -070045 (void)pm_req_wakeup(proc->node_id, (versal_sec_entry & 0xFFFFFFFFU) | 0x1U,
46 versal_sec_entry >> 32, 0, SECURE_FLAG);
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053047
Tejas Patel61717112019-02-27 18:44:57 +053048 /* Clear power down request */
49 pm_client_wakeup(proc);
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053050
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +000051 ret = PSCI_E_SUCCESS;
52
53exit_label:
54 return ret;
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053055}
56
Tejas Patel54d13192019-02-27 18:44:55 +053057/**
58 * versal_pwr_domain_suspend() - This function sends request to PMC to suspend
Prasad Kummari7d0623a2023-06-09 14:32:00 +053059 * core.
60 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +053061 *
Tejas Patel54d13192019-02-27 18:44:55 +053062 */
63static void versal_pwr_domain_suspend(const psci_power_state_t *target_state)
64{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053065 uint32_t state;
66 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +053067 const struct pm_proc *proc = pm_get_proc(cpu_id);
68
Ronak Jain807f41b2024-05-08 02:41:13 -070069 if (proc == NULL) {
Michal Simekb8eca3b2024-04-19 12:16:46 +020070 return;
71 }
72
Abhyuday Godhasara589afa52021-08-11 06:15:13 -070073 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +053074 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
75 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070076 }
Tejas Patel54d13192019-02-27 18:44:55 +053077
78 plat_versal_gic_cpuif_disable();
79
Ravi Pateleafc8782019-06-21 05:00:49 -070080 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
81 plat_versal_gic_save();
82 }
Tejas Patel54d13192019-02-27 18:44:55 +053083
Maheedhar Bollapallicc64a792024-10-14 04:16:03 +000084 state = (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) ?
Tejas Patel54d13192019-02-27 18:44:55 +053085 PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE;
86
87 /* Send request to PMC to suspend this core */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -070088 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_sec_entry,
89 SECURE_FLAG);
Tejas Patel54d13192019-02-27 18:44:55 +053090
91 /* APU is to be turned off */
92 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
93 /* disable coherency */
94 plat_arm_interconnect_exit_coherency();
95 }
96}
97
98/**
99 * versal_pwr_domain_suspend_finish() - This function performs actions to finish
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530100 * suspend procedure.
101 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +0530102 *
Tejas Patel54d13192019-02-27 18:44:55 +0530103 */
104static void versal_pwr_domain_suspend_finish(
105 const psci_power_state_t *target_state)
106{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530107 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +0530108 const struct pm_proc *proc = pm_get_proc(cpu_id);
109
Ronak Jain807f41b2024-05-08 02:41:13 -0700110 if (proc == NULL) {
Michal Simekb8eca3b2024-04-19 12:16:46 +0200111 return;
112 }
113
Abhyuday Godhasara589afa52021-08-11 06:15:13 -0700114 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +0530115 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
116 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700117 }
Tejas Patel54d13192019-02-27 18:44:55 +0530118
119 /* Clear the APU power control register for this cpu */
120 pm_client_wakeup(proc);
121
122 /* enable coherency */
123 plat_arm_interconnect_enter_coherency();
124
125 /* APU was turned off, so restore GIC context */
126 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
127 plat_versal_gic_resume();
Tejas Patel54d13192019-02-27 18:44:55 +0530128 }
Ravi Pateleafc8782019-06-21 05:00:49 -0700129
130 plat_versal_gic_cpuif_enable();
Tejas Patel54d13192019-02-27 18:44:55 +0530131}
132
Maheedhar Bollapalli86d6d702024-10-07 09:27:58 +0000133static void versal_pwr_domain_on_finish(const psci_power_state_t *target_state)
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530134{
135 /* Enable the gic cpu interface */
136 plat_versal_gic_pcpu_init();
137
138 /* Program the gic per-cpu distributor or re-distributor interface */
139 plat_versal_gic_cpuif_enable();
140}
141
Tejas Patel54d13192019-02-27 18:44:55 +0530142/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530143 * versal_system_off() - This function sends the system off request to firmware.
144 * This function does not return.
145 *
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800146 */
147static void __dead2 versal_system_off(void)
148{
149 /* Send the power down request to the PMC */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -0700150 (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN,
151 pm_get_shutdown_scope(), SECURE_FLAG);
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800152
Maheedhar Bollapallie897f0a2024-10-14 06:45:13 +0000153 while (true) {
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800154 wfi();
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700155 }
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800156}
157
158/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530159 * versal_system_reset() - This function sends the reset request to firmware
160 * for the system to reset. This function does not
161 * return.
162 *
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800163 */
164static void __dead2 versal_system_reset(void)
165{
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -0700166 uint32_t ret, timeout = 10000U;
167
168 request_cpu_pwrdwn();
169
170 /*
171 * Send the system reset request to the firmware if power down request
172 * is not received from firmware.
173 */
174 if (!pwrdwn_req_received) {
175 (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET,
176 pm_get_shutdown_scope(), SECURE_FLAG);
177
178 /*
179 * Wait for system shutdown request completed and idle callback
180 * not received.
181 */
182 do {
183 ret = ipi_mb_enquire_status(primary_proc->ipi->local_ipi_id,
184 primary_proc->ipi->remote_ipi_id);
185 udelay(100);
186 timeout--;
187 } while ((ret != IPI_MB_STATUS_RECV_PENDING) && (timeout > 0U));
188 }
189
190 (void)psci_cpu_off();
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800191
Maheedhar Bollapallie897f0a2024-10-14 06:45:13 +0000192 while (true) {
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800193 wfi();
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700194 }
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800195}
196
Maheedhar Bollapalli5d6cf5b2025-02-17 15:52:24 +0530197static int32_t versal_validate_ns_entrypoint(uint64_t ns_entrypoint)
198{
199 int32_t ret = PSCI_E_SUCCESS;
200
201 if (((ns_entrypoint >= PLAT_DDR_LOWMEM_MAX) && (ns_entrypoint <= PLAT_DDR_HIGHMEM_MAX)) ||
202 ((ns_entrypoint >= BL31_BASE) && (ns_entrypoint <= BL31_LIMIT))) {
203 ret = PSCI_E_INVALID_ADDRESS;
204 }
205
206 return ret;
207}
208
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800209/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530210 * versal_pwr_domain_off() - This function performs actions to turn off core.
211 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +0530212 *
Tejas Patel54d13192019-02-27 18:44:55 +0530213 */
214static void versal_pwr_domain_off(const psci_power_state_t *target_state)
215{
Maheedhar Bollapalli493c7a22024-10-08 05:42:28 +0000216 uint32_t ret, fw_api_version, version_type[RET_PAYLOAD_ARG_CNT] = {0U};
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530217 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +0530218 const struct pm_proc *proc = pm_get_proc(cpu_id);
219
Ronak Jain807f41b2024-05-08 02:41:13 -0700220 if (proc == NULL) {
Michal Simekb8eca3b2024-04-19 12:16:46 +0200221 return;
222 }
223
Abhyuday Godhasara589afa52021-08-11 06:15:13 -0700224 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +0530225 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
226 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700227 }
Tejas Patel54d13192019-02-27 18:44:55 +0530228
229 /* Prevent interrupts from spuriously waking up this cpu */
230 plat_versal_gic_cpuif_disable();
231
232 /*
233 * Send request to PMC to power down the appropriate APU CPU
234 * core.
235 * According to PSCI specification, CPU_off function does not
236 * have resume address and CPU core can only be woken up
237 * invoking CPU_on function, during which resume address will
238 * be set.
239 */
Maheedhar Bollapalli493c7a22024-10-08 05:42:28 +0000240 ret = pm_feature_check((uint32_t)PM_SELF_SUSPEND, &version_type[0], SECURE_FLAG);
Maheedhar Bollapallib3c92e62024-10-21 05:23:53 +0000241 if (ret == (uint32_t)PM_RET_SUCCESS) {
Maheedhar Bollapalli493c7a22024-10-08 05:42:28 +0000242 fw_api_version = version_type[0] & 0xFFFFU;
Jay Buddhabhatti31488a32023-09-11 23:50:06 -0700243 if (fw_api_version >= 3U) {
244 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_OFF, 0,
245 SECURE_FLAG);
246 } else {
247 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0,
248 SECURE_FLAG);
249 }
250 }
Tejas Patel54d13192019-02-27 18:44:55 +0530251}
252
253/**
254 * versal_validate_power_state() - This function ensures that the power state
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530255 * parameter in request is valid.
256 * @power_state: Power state of core.
257 * @req_state: Requested state.
Tejas Patel54d13192019-02-27 18:44:55 +0530258 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530259 * Return: Returns status, either success or reason.
Tejas Patel54d13192019-02-27 18:44:55 +0530260 *
Tejas Patel54d13192019-02-27 18:44:55 +0530261 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530262static int32_t versal_validate_power_state(uint32_t power_state,
Tejas Patel54d13192019-02-27 18:44:55 +0530263 psci_power_state_t *req_state)
264{
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +0000265 int32_t ret = PSCI_E_SUCCESS;
Tejas Patel54d13192019-02-27 18:44:55 +0530266 VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
267
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530268 uint32_t pstate = psci_get_pstate_type(power_state);
Tejas Patel54d13192019-02-27 18:44:55 +0530269
Maheedhar Bollapalli15fa5b52024-09-27 05:52:21 +0000270 assert(req_state != NULL);
Tejas Patel54d13192019-02-27 18:44:55 +0530271
272 /* Sanity check the requested state */
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700273 if (pstate == PSTATE_TYPE_STANDBY) {
Tejas Patel54d13192019-02-27 18:44:55 +0530274 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700275 } else {
Tejas Patel54d13192019-02-27 18:44:55 +0530276 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700277 }
Tejas Patel54d13192019-02-27 18:44:55 +0530278
279 /* We expect the 'state id' to be zero */
Abhyuday Godhasarabacbdee2021-08-20 00:27:03 -0700280 if (psci_get_pstate_id(power_state) != 0U) {
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +0000281 ret = PSCI_E_INVALID_PARAMS;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700282 }
Tejas Patel54d13192019-02-27 18:44:55 +0530283
Maheedhar Bollapalli0449b672024-10-29 00:09:08 +0000284 return ret;
Tejas Patel54d13192019-02-27 18:44:55 +0530285}
286
287/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530288 * versal_get_sys_suspend_power_state() - Get power state for system suspend.
289 * @req_state: Requested state.
Tejas Patel54d13192019-02-27 18:44:55 +0530290 *
Tejas Patel54d13192019-02-27 18:44:55 +0530291 */
292static void versal_get_sys_suspend_power_state(psci_power_state_t *req_state)
293{
294 req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE;
295 req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE;
296}
297
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530298static const struct plat_psci_ops versal_nopmc_psci_ops = {
Tejas Patel61717112019-02-27 18:44:57 +0530299 .pwr_domain_on = versal_pwr_domain_on,
Tejas Patel54d13192019-02-27 18:44:55 +0530300 .pwr_domain_off = versal_pwr_domain_off,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530301 .pwr_domain_on_finish = versal_pwr_domain_on_finish,
Tejas Patel54d13192019-02-27 18:44:55 +0530302 .pwr_domain_suspend = versal_pwr_domain_suspend,
303 .pwr_domain_suspend_finish = versal_pwr_domain_suspend_finish,
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800304 .system_off = versal_system_off,
305 .system_reset = versal_system_reset,
Maheedhar Bollapalli5d6cf5b2025-02-17 15:52:24 +0530306 .validate_ns_entrypoint = versal_validate_ns_entrypoint,
Tejas Patel54d13192019-02-27 18:44:55 +0530307 .validate_power_state = versal_validate_power_state,
308 .get_sys_suspend_power_state = versal_get_sys_suspend_power_state,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530309};
310
311/*******************************************************************************
312 * Export the platform specific power ops.
313 ******************************************************************************/
Venkatesh Yadav Abbarapu2cefbcd2022-07-31 14:05:40 +0530314int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530315 const struct plat_psci_ops **psci_ops)
316{
317 versal_sec_entry = sec_entrypoint;
318
319 *psci_ops = &versal_nopmc_psci_ops;
320
321 return 0;
322}