blob: eb926eec6109daa45cd93610b1afe50cfa241b6f [file] [log] [blame]
Jay Buddhabhattic6daff02022-09-05 02:56:32 -07001/*
2 * Copyright (c) 2022, Xilinx, Inc. All rights reserved.
Ronak Jain807f41b2024-05-08 02:41:13 -07003 * Copyright (c) 2022-2024, Advanced Micro Devices, Inc. All rights reserved.
Jay Buddhabhattic6daff02022-09-05 02:56:32 -07004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#include <assert.h>
9
10#include <common/debug.h>
11#include <lib/mmio.h>
12#include <lib/psci/psci.h>
13#include <plat/arm/common/plat_arm.h>
14#include <plat/common/platform.h>
15#include <plat_arm.h>
16
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -070017#include <drivers/delay_timer.h>
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070018#include <plat_private.h>
19#include "pm_api_sys.h"
20#include "pm_client.h"
21#include <pm_common.h>
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -070022#include "pm_ipi.h"
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070023#include "pm_svc_main.h"
24#include "versal_net_def.h"
25
26static uintptr_t versal_net_sec_entry;
27
28static int32_t versal_net_pwr_domain_on(u_register_t mpidr)
29{
Maheedhar Bollapalli60837112024-10-21 05:41:14 +000030 int32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070031 const struct pm_proc *proc;
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +000032 int32_t ret = PSCI_E_INTERN_FAIL;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070033
34 VERBOSE("%s: mpidr: 0x%lx, cpuid: %x\n",
35 __func__, mpidr, cpu_id);
36
37 if (cpu_id == -1) {
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +000038 goto exit_label;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070039 }
40
41 proc = pm_get_proc(cpu_id);
Ronak Jain807f41b2024-05-08 02:41:13 -070042 if (proc == NULL) {
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +000043 goto exit_label;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070044 }
45
Maheedhar Bollapalliad1553d2024-10-14 11:15:53 +000046 (void)pm_req_wakeup(proc->node_id, (versal_net_sec_entry & 0xFFFFFFFFU) | 0x1U,
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070047 versal_net_sec_entry >> 32, 0, 0);
48
49 /* Clear power down request */
50 pm_client_wakeup(proc);
51
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +000052 ret = PSCI_E_SUCCESS;
53
54exit_label:
55 return ret;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070056}
57
58/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +053059 * versal_net_pwr_domain_off() - This function performs actions to turn off
60 * core.
61 * @target_state: Targeted state.
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070062 *
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070063 */
64static void versal_net_pwr_domain_off(const psci_power_state_t *target_state)
65{
Maheedhar Bollapalli828a07a2024-10-08 05:50:05 +000066 uint32_t ret, fw_api_version, version_type[RET_PAYLOAD_ARG_CNT] = {0U};
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070067 uint32_t cpu_id = plat_my_core_pos();
68 const struct pm_proc *proc = pm_get_proc(cpu_id);
69
Ronak Jain807f41b2024-05-08 02:41:13 -070070 if (proc == NULL) {
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +000071 goto exit_label;
Michal Simekb8eca3b2024-04-19 12:16:46 +020072 }
73
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070074 for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) {
75 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
76 __func__, i, target_state->pwr_domain_state[i]);
77 }
78
79 /* Prevent interrupts from spuriously waking up this cpu */
Jay Buddhabhattib7bb1ed2023-10-05 21:55:28 -070080 plat_arm_gic_cpuif_disable();
Jay Buddhabhattic6daff02022-09-05 02:56:32 -070081
82 /*
83 * Send request to PMC to power down the appropriate APU CPU
84 * core.
85 * According to PSCI specification, CPU_off function does not
86 * have resume address and CPU core can only be woken up
87 * invoking CPU_on function, during which resume address will
88 * be set.
89 */
Maheedhar Bollapalli828a07a2024-10-08 05:50:05 +000090 ret = pm_feature_check((uint32_t)PM_SELF_SUSPEND, &version_type[0], SECURE_FLAG);
Maheedhar Bollapalli60837112024-10-21 05:41:14 +000091 if (ret == (uint32_t)PM_RET_SUCCESS) {
Maheedhar Bollapalli828a07a2024-10-08 05:50:05 +000092 fw_api_version = version_type[0] & 0xFFFFU;
Jay Buddhabhatti31488a32023-09-11 23:50:06 -070093 if (fw_api_version >= 3U) {
94 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_OFF, 0,
95 SECURE_FLAG);
96 } else {
97 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0,
98 SECURE_FLAG);
99 }
100 }
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +0000101
102exit_label:
103 return;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700104}
105
Maheedhar Bollapallif9617f92025-02-17 15:52:05 +0530106static int32_t versal_net_validate_ns_entrypoint(uint64_t ns_entrypoint)
107{
108 int32_t ret = PSCI_E_SUCCESS;
109
110 if (((ns_entrypoint >= PLAT_DDR_LOWMEM_MAX) && (ns_entrypoint <= PLAT_DDR_HIGHMEM_MAX)) ||
111 ((ns_entrypoint >= BL31_BASE) && (ns_entrypoint <= BL31_LIMIT))) {
112 ret = PSCI_E_INVALID_ADDRESS;
113 }
114
115 return ret;
116}
117
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700118/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530119 * versal_net_system_reset() - This function sends the reset request to firmware
120 * for the system to reset. This function does not
121 * return.
122 *
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700123 */
124static void __dead2 versal_net_system_reset(void)
125{
Jay Buddhabhatti10e71e42023-06-19 05:08:54 -0700126 uint32_t ret, timeout = 10000U;
127
128 request_cpu_pwrdwn();
129
130 /*
131 * Send the system reset request to the firmware if power down request
132 * is not received from firmware.
133 */
134 if (!pwrdwn_req_received) {
135 (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET,
136 pm_get_shutdown_scope(), SECURE_FLAG);
137
138 /*
139 * Wait for system shutdown request completed and idle callback
140 * not received.
141 */
142 do {
143 ret = ipi_mb_enquire_status(primary_proc->ipi->local_ipi_id,
144 primary_proc->ipi->remote_ipi_id);
145 udelay(100);
146 timeout--;
147 } while ((ret != IPI_MB_STATUS_RECV_PENDING) && (timeout > 0U));
148 }
149
150 (void)psci_cpu_off();
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700151
Maheedhar Bollapalli2093b252024-10-14 06:48:43 +0000152 while (true) {
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700153 wfi();
154 }
155}
156
157/**
158 * versal_net_pwr_domain_suspend() - This function sends request to PMC to suspend
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530159 * core.
160 * @target_state: Targeted state.
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700161 *
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700162 */
163static void versal_net_pwr_domain_suspend(const psci_power_state_t *target_state)
164{
165 uint32_t state;
166 uint32_t cpu_id = plat_my_core_pos();
167 const struct pm_proc *proc = pm_get_proc(cpu_id);
168
Ronak Jain807f41b2024-05-08 02:41:13 -0700169 if (proc == NULL) {
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +0000170 goto exit_label;
Michal Simekb8eca3b2024-04-19 12:16:46 +0200171 }
172
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700173 for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) {
174 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
175 __func__, i, target_state->pwr_domain_state[i]);
176 }
177
Jay Buddhabhattib7bb1ed2023-10-05 21:55:28 -0700178 plat_arm_gic_cpuif_disable();
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700179
180 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
Jay Buddhabhattib7bb1ed2023-10-05 21:55:28 -0700181 plat_arm_gic_save();
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700182 }
183
Maheedhar Bollapalli71d8e2b2024-10-14 04:19:45 +0000184 state = (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) ?
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700185 PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE;
186
187 /* Send request to PMC to suspend this core */
Maheedhar Bollapalliad1553d2024-10-14 11:15:53 +0000188 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_net_sec_entry,
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700189 SECURE_FLAG);
190
191 /* TODO: disable coherency */
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +0000192
193exit_label:
194 return;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700195}
196
197static void versal_net_pwr_domain_on_finish(const psci_power_state_t *target_state)
198{
199 (void)target_state;
200
201 /* Enable the gic cpu interface */
Jay Buddhabhattib7bb1ed2023-10-05 21:55:28 -0700202 plat_arm_gic_pcpu_init();
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700203
204 /* Program the gic per-cpu distributor or re-distributor interface */
Jay Buddhabhattib7bb1ed2023-10-05 21:55:28 -0700205 plat_arm_gic_cpuif_enable();
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700206}
207
208/**
209 * versal_net_pwr_domain_suspend_finish() - This function performs actions to finish
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530210 * suspend procedure.
211 * @target_state: Targeted state.
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700212 *
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700213 */
214static void versal_net_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
215{
216 uint32_t cpu_id = plat_my_core_pos();
217 const struct pm_proc *proc = pm_get_proc(cpu_id);
218
Ronak Jain807f41b2024-05-08 02:41:13 -0700219 if (proc == NULL) {
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +0000220 goto exit_label;
Michal Simekb8eca3b2024-04-19 12:16:46 +0200221 }
222
Maheedhar Bollapallic59b1df2024-10-24 06:02:48 +0000223 for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) {
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700224 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
225 __func__, i, target_state->pwr_domain_state[i]);
Maheedhar Bollapallic59b1df2024-10-24 06:02:48 +0000226 }
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700227
228 /* Clear the APU power control register for this cpu */
229 pm_client_wakeup(proc);
230
231 /* TODO: enable coherency */
232
233 /* APU was turned off, so restore GIC context */
234 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
Jay Buddhabhattib7bb1ed2023-10-05 21:55:28 -0700235 plat_arm_gic_resume();
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700236 }
237
Jay Buddhabhattib7bb1ed2023-10-05 21:55:28 -0700238 plat_arm_gic_cpuif_enable();
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +0000239
240exit_label:
241 return;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700242}
243
244/**
245 * versal_net_system_off() - This function sends the system off request
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530246 * to firmware. This function does not return.
247 *
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700248 */
249static void __dead2 versal_net_system_off(void)
250{
251 /* Send the power down request to the PMC */
Maheedhar Bollapalliad1553d2024-10-14 11:15:53 +0000252 (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN,
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700253 pm_get_shutdown_scope(), SECURE_FLAG);
254
Maheedhar Bollapalli2093b252024-10-14 06:48:43 +0000255 while (true) {
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700256 wfi();
257 }
258}
259
260/**
261 * versal_net_validate_power_state() - This function ensures that the power state
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530262 * parameter in request is valid.
263 * @power_state: Power state of core.
264 * @req_state: Requested state.
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700265 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530266 * Return: Returns status, either PSCI_E_SUCCESS or reason.
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700267 *
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700268 */
269static int32_t versal_net_validate_power_state(unsigned int power_state,
270 psci_power_state_t *req_state)
271{
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +0000272 int32_t ret = PSCI_E_INVALID_PARAMS;
273
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700274 VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
275
Maheedhar Bollapalli1e1d1b02024-10-22 06:53:27 +0000276 uint32_t pstate = psci_get_pstate_type(power_state);
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700277
Maheedhar Bollapalli311dce72024-09-27 05:55:04 +0000278 assert(req_state != NULL);
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700279
280 /* Sanity check the requested state */
281 if (pstate == PSTATE_TYPE_STANDBY) {
282 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE;
283 } else {
Jay Buddhabhatti6cd94be2023-03-22 22:44:16 -0700284 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700285 }
286
287 /* We expect the 'state id' to be zero */
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +0000288 if (psci_get_pstate_id(power_state) == 0U) {
289 ret = PSCI_E_SUCCESS;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700290 }
291
Maheedhar Bollapallid27a5162024-10-29 03:34:49 +0000292 return ret;
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700293}
294
295/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530296 * versal_net_get_sys_suspend_power_state() - Get power state for system
297 * suspend.
298 * @req_state: Requested state.
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700299 *
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700300 */
301static void versal_net_get_sys_suspend_power_state(psci_power_state_t *req_state)
302{
Jay Buddhabhatti7c5adef2022-12-29 21:58:35 -0800303 uint64_t i;
304
Maheedhar Bollapallic59b1df2024-10-24 06:02:48 +0000305 for (i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) {
Jay Buddhabhatti7c5adef2022-12-29 21:58:35 -0800306 req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
Maheedhar Bollapallic59b1df2024-10-24 06:02:48 +0000307 }
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700308}
309
310static const struct plat_psci_ops versal_net_nopmc_psci_ops = {
311 .pwr_domain_on = versal_net_pwr_domain_on,
312 .pwr_domain_off = versal_net_pwr_domain_off,
313 .pwr_domain_on_finish = versal_net_pwr_domain_on_finish,
314 .pwr_domain_suspend = versal_net_pwr_domain_suspend,
315 .pwr_domain_suspend_finish = versal_net_pwr_domain_suspend_finish,
316 .system_off = versal_net_system_off,
317 .system_reset = versal_net_system_reset,
Maheedhar Bollapallif9617f92025-02-17 15:52:05 +0530318 .validate_ns_entrypoint = versal_net_validate_ns_entrypoint,
Jay Buddhabhattic6daff02022-09-05 02:56:32 -0700319 .validate_power_state = versal_net_validate_power_state,
320 .get_sys_suspend_power_state = versal_net_get_sys_suspend_power_state,
321};
322
323/*******************************************************************************
324 * Export the platform specific power ops.
325 ******************************************************************************/
326int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint,
327 const struct plat_psci_ops **psci_ops)
328{
329 versal_net_sec_entry = sec_entrypoint;
330
331 VERBOSE("Setting up entry point %lx\n", versal_net_sec_entry);
332
333 *psci_ops = &versal_net_nopmc_psci_ops;
334
335 return 0;
336}
337
338int32_t sip_svc_setup_init(void)
339{
340 return pm_setup();
341}
342
343uint64_t smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4,
344 void *cookie, void *handle, uint64_t flags)
345{
346 return pm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags);
347}