blob: 56d98f79e217f80864c7dbdf1e586972ac7842c8 [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.
Prasad Kummari7d0623a2023-06-09 14:32:00 +05303 * Copyright (c) 2022-2023, 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
Prasad Kummari536e1102023-06-22 10:50:02 +053017#include <plat_private.h>
Tejas Patel61717112019-02-27 18:44:57 +053018#include "pm_api_sys.h"
19#include "pm_client.h"
Prasad Kummari536e1102023-06-22 10:50:02 +053020#include <pm_common.h>
Tejas Patel61717112019-02-27 18:44:57 +053021
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053022static uintptr_t versal_sec_entry;
23
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053024static int32_t versal_pwr_domain_on(u_register_t mpidr)
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053025{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053026 int32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
Tejas Patel61717112019-02-27 18:44:57 +053027 const struct pm_proc *proc;
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053028
29 VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr);
30
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070031 if (cpu_id == -1) {
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053032 return PSCI_E_INTERN_FAIL;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070033 }
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053034
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053035 proc = pm_get_proc((uint32_t)cpu_id);
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053036
Tejas Patel61717112019-02-27 18:44:57 +053037 /* Send request to PMC to wake up selected ACPU core */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -070038 (void)pm_req_wakeup(proc->node_id, (versal_sec_entry & 0xFFFFFFFFU) | 0x1U,
39 versal_sec_entry >> 32, 0, SECURE_FLAG);
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053040
Tejas Patel61717112019-02-27 18:44:57 +053041 /* Clear power down request */
42 pm_client_wakeup(proc);
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +053043
44 return PSCI_E_SUCCESS;
45}
46
Tejas Patel54d13192019-02-27 18:44:55 +053047/**
48 * versal_pwr_domain_suspend() - This function sends request to PMC to suspend
Prasad Kummari7d0623a2023-06-09 14:32:00 +053049 * core.
50 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +053051 *
Tejas Patel54d13192019-02-27 18:44:55 +053052 */
53static void versal_pwr_domain_suspend(const psci_power_state_t *target_state)
54{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053055 uint32_t state;
56 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +053057 const struct pm_proc *proc = pm_get_proc(cpu_id);
58
Abhyuday Godhasara589afa52021-08-11 06:15:13 -070059 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +053060 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
61 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070062 }
Tejas Patel54d13192019-02-27 18:44:55 +053063
64 plat_versal_gic_cpuif_disable();
65
Ravi Pateleafc8782019-06-21 05:00:49 -070066 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
67 plat_versal_gic_save();
68 }
Tejas Patel54d13192019-02-27 18:44:55 +053069
70 state = target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE ?
71 PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE;
72
73 /* Send request to PMC to suspend this core */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -070074 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_sec_entry,
75 SECURE_FLAG);
Tejas Patel54d13192019-02-27 18:44:55 +053076
77 /* APU is to be turned off */
78 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
79 /* disable coherency */
80 plat_arm_interconnect_exit_coherency();
81 }
82}
83
84/**
85 * versal_pwr_domain_suspend_finish() - This function performs actions to finish
Prasad Kummari7d0623a2023-06-09 14:32:00 +053086 * suspend procedure.
87 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +053088 *
Tejas Patel54d13192019-02-27 18:44:55 +053089 */
90static void versal_pwr_domain_suspend_finish(
91 const psci_power_state_t *target_state)
92{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +053093 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +053094 const struct pm_proc *proc = pm_get_proc(cpu_id);
95
Abhyuday Godhasara589afa52021-08-11 06:15:13 -070096 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +053097 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
98 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -070099 }
Tejas Patel54d13192019-02-27 18:44:55 +0530100
101 /* Clear the APU power control register for this cpu */
102 pm_client_wakeup(proc);
103
104 /* enable coherency */
105 plat_arm_interconnect_enter_coherency();
106
107 /* APU was turned off, so restore GIC context */
108 if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) {
109 plat_versal_gic_resume();
Tejas Patel54d13192019-02-27 18:44:55 +0530110 }
Ravi Pateleafc8782019-06-21 05:00:49 -0700111
112 plat_versal_gic_cpuif_enable();
Tejas Patel54d13192019-02-27 18:44:55 +0530113}
114
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530115void versal_pwr_domain_on_finish(const psci_power_state_t *target_state)
116{
117 /* Enable the gic cpu interface */
118 plat_versal_gic_pcpu_init();
119
120 /* Program the gic per-cpu distributor or re-distributor interface */
121 plat_versal_gic_cpuif_enable();
122}
123
Tejas Patel54d13192019-02-27 18:44:55 +0530124/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530125 * versal_system_off() - This function sends the system off request to firmware.
126 * This function does not return.
127 *
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800128 */
129static void __dead2 versal_system_off(void)
130{
131 /* Send the power down request to the PMC */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -0700132 (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN,
133 pm_get_shutdown_scope(), SECURE_FLAG);
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800134
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700135 while (1) {
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800136 wfi();
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700137 }
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800138}
139
140/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530141 * versal_system_reset() - This function sends the reset request to firmware
142 * for the system to reset. This function does not
143 * return.
144 *
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800145 */
146static void __dead2 versal_system_reset(void)
147{
148 /* Send the system reset request to the PMC */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -0700149 (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET,
150 pm_get_shutdown_scope(), SECURE_FLAG);
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800151
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700152 while (1) {
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800153 wfi();
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700154 }
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800155}
156
157/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530158 * versal_pwr_domain_off() - This function performs actions to turn off core.
159 * @target_state: Targated state.
Tejas Patel54d13192019-02-27 18:44:55 +0530160 *
Tejas Patel54d13192019-02-27 18:44:55 +0530161 */
162static void versal_pwr_domain_off(const psci_power_state_t *target_state)
163{
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530164 uint32_t cpu_id = plat_my_core_pos();
Tejas Patel54d13192019-02-27 18:44:55 +0530165 const struct pm_proc *proc = pm_get_proc(cpu_id);
166
Abhyuday Godhasara589afa52021-08-11 06:15:13 -0700167 for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) {
Tejas Patel54d13192019-02-27 18:44:55 +0530168 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
169 __func__, i, target_state->pwr_domain_state[i]);
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700170 }
Tejas Patel54d13192019-02-27 18:44:55 +0530171
172 /* Prevent interrupts from spuriously waking up this cpu */
173 plat_versal_gic_cpuif_disable();
174
175 /*
176 * Send request to PMC to power down the appropriate APU CPU
177 * core.
178 * According to PSCI specification, CPU_off function does not
179 * have resume address and CPU core can only be woken up
180 * invoking CPU_on function, during which resume address will
181 * be set.
182 */
Abhyuday Godhasaraf435a142021-08-20 00:04:33 -0700183 (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0,
184 SECURE_FLAG);
Tejas Patel54d13192019-02-27 18:44:55 +0530185}
186
187/**
188 * versal_validate_power_state() - This function ensures that the power state
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530189 * parameter in request is valid.
190 * @power_state: Power state of core.
191 * @req_state: Requested state.
Tejas Patel54d13192019-02-27 18:44:55 +0530192 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530193 * Return: Returns status, either success or reason.
Tejas Patel54d13192019-02-27 18:44:55 +0530194 *
Tejas Patel54d13192019-02-27 18:44:55 +0530195 */
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530196static int32_t versal_validate_power_state(uint32_t power_state,
Tejas Patel54d13192019-02-27 18:44:55 +0530197 psci_power_state_t *req_state)
198{
199 VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
200
Venkatesh Yadav Abbarapubde87592022-05-24 11:11:12 +0530201 uint32_t pstate = psci_get_pstate_type(power_state);
Tejas Patel54d13192019-02-27 18:44:55 +0530202
203 assert(req_state);
204
205 /* Sanity check the requested state */
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700206 if (pstate == PSTATE_TYPE_STANDBY) {
Tejas Patel54d13192019-02-27 18:44:55 +0530207 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700208 } else {
Tejas Patel54d13192019-02-27 18:44:55 +0530209 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700210 }
Tejas Patel54d13192019-02-27 18:44:55 +0530211
212 /* We expect the 'state id' to be zero */
Abhyuday Godhasarabacbdee2021-08-20 00:27:03 -0700213 if (psci_get_pstate_id(power_state) != 0U) {
Tejas Patel54d13192019-02-27 18:44:55 +0530214 return PSCI_E_INVALID_PARAMS;
Abhyuday Godhasaraf55a5cd2021-08-09 08:15:13 -0700215 }
Tejas Patel54d13192019-02-27 18:44:55 +0530216
217 return PSCI_E_SUCCESS;
218}
219
220/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530221 * versal_get_sys_suspend_power_state() - Get power state for system suspend.
222 * @req_state: Requested state.
Tejas Patel54d13192019-02-27 18:44:55 +0530223 *
Tejas Patel54d13192019-02-27 18:44:55 +0530224 */
225static void versal_get_sys_suspend_power_state(psci_power_state_t *req_state)
226{
227 req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE;
228 req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE;
229}
230
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530231static const struct plat_psci_ops versal_nopmc_psci_ops = {
Tejas Patel61717112019-02-27 18:44:57 +0530232 .pwr_domain_on = versal_pwr_domain_on,
Tejas Patel54d13192019-02-27 18:44:55 +0530233 .pwr_domain_off = versal_pwr_domain_off,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530234 .pwr_domain_on_finish = versal_pwr_domain_on_finish,
Tejas Patel54d13192019-02-27 18:44:55 +0530235 .pwr_domain_suspend = versal_pwr_domain_suspend,
236 .pwr_domain_suspend_finish = versal_pwr_domain_suspend_finish,
Saeed Nowshadic5a1bda2019-12-08 23:35:35 -0800237 .system_off = versal_system_off,
238 .system_reset = versal_system_reset,
Tejas Patel54d13192019-02-27 18:44:55 +0530239 .validate_power_state = versal_validate_power_state,
240 .get_sys_suspend_power_state = versal_get_sys_suspend_power_state,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530241};
242
243/*******************************************************************************
244 * Export the platform specific power ops.
245 ******************************************************************************/
Venkatesh Yadav Abbarapu2cefbcd2022-07-31 14:05:40 +0530246int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint,
Siva Durga Prasad Paladugufe4af662018-09-25 18:44:58 +0530247 const struct plat_psci_ops **psci_ops)
248{
249 versal_sec_entry = sec_entrypoint;
250
251 *psci_ops = &versal_nopmc_psci_ops;
252
253 return 0;
254}