Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 1 | /* |
Michal Simek | 2a47faa | 2023-04-14 08:43:51 +0200 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, Arm Limited and Contributors. All rights reserved. |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 3 | * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved. |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 8 | #include <assert.h> |
| 9 | #include <plat_arm.h> |
Tejas Patel | 6940996 | 2018-12-14 00:55:29 -0800 | [diff] [blame] | 10 | #include <plat_private.h> |
Tejas Patel | 6171711 | 2019-02-27 18:44:57 +0530 | [diff] [blame] | 11 | #include <pm_common.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <common/debug.h> |
| 13 | #include <lib/mmio.h> |
| 14 | #include <lib/psci/psci.h> |
| 15 | #include <plat/common/platform.h> |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 16 | #include <plat/arm/common/plat_arm.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | |
Tejas Patel | 6171711 | 2019-02-27 18:44:57 +0530 | [diff] [blame] | 18 | #include "pm_api_sys.h" |
| 19 | #include "pm_client.h" |
| 20 | |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 21 | static uintptr_t versal_sec_entry; |
| 22 | |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 23 | static int32_t versal_pwr_domain_on(u_register_t mpidr) |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 24 | { |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 25 | int32_t cpu_id = plat_core_pos_by_mpidr(mpidr); |
Tejas Patel | 6171711 | 2019-02-27 18:44:57 +0530 | [diff] [blame] | 26 | const struct pm_proc *proc; |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 27 | |
| 28 | VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr); |
| 29 | |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 30 | if (cpu_id == -1) { |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 31 | return PSCI_E_INTERN_FAIL; |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 32 | } |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 33 | |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 34 | proc = pm_get_proc((uint32_t)cpu_id); |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 35 | |
Tejas Patel | 6171711 | 2019-02-27 18:44:57 +0530 | [diff] [blame] | 36 | /* Send request to PMC to wake up selected ACPU core */ |
Abhyuday Godhasara | f435a14 | 2021-08-20 00:04:33 -0700 | [diff] [blame] | 37 | (void)pm_req_wakeup(proc->node_id, (versal_sec_entry & 0xFFFFFFFFU) | 0x1U, |
| 38 | versal_sec_entry >> 32, 0, SECURE_FLAG); |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 39 | |
Tejas Patel | 6171711 | 2019-02-27 18:44:57 +0530 | [diff] [blame] | 40 | /* Clear power down request */ |
| 41 | pm_client_wakeup(proc); |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 42 | |
| 43 | return PSCI_E_SUCCESS; |
| 44 | } |
| 45 | |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 46 | /** |
| 47 | * versal_pwr_domain_suspend() - This function sends request to PMC to suspend |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 48 | * core. |
| 49 | * @target_state: Targated state. |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 50 | * |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 51 | */ |
| 52 | static void versal_pwr_domain_suspend(const psci_power_state_t *target_state) |
| 53 | { |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 54 | uint32_t state; |
| 55 | uint32_t cpu_id = plat_my_core_pos(); |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 56 | const struct pm_proc *proc = pm_get_proc(cpu_id); |
| 57 | |
Abhyuday Godhasara | 589afa5 | 2021-08-11 06:15:13 -0700 | [diff] [blame] | 58 | for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) { |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 59 | VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", |
| 60 | __func__, i, target_state->pwr_domain_state[i]); |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 61 | } |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 62 | |
| 63 | plat_versal_gic_cpuif_disable(); |
| 64 | |
Ravi Patel | eafc878 | 2019-06-21 05:00:49 -0700 | [diff] [blame] | 65 | if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) { |
| 66 | plat_versal_gic_save(); |
| 67 | } |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 68 | |
| 69 | state = target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE ? |
| 70 | PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE; |
| 71 | |
| 72 | /* Send request to PMC to suspend this core */ |
Abhyuday Godhasara | f435a14 | 2021-08-20 00:04:33 -0700 | [diff] [blame] | 73 | (void)pm_self_suspend(proc->node_id, MAX_LATENCY, state, versal_sec_entry, |
| 74 | SECURE_FLAG); |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 75 | |
| 76 | /* APU is to be turned off */ |
| 77 | if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) { |
| 78 | /* disable coherency */ |
| 79 | plat_arm_interconnect_exit_coherency(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * versal_pwr_domain_suspend_finish() - This function performs actions to finish |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 85 | * suspend procedure. |
| 86 | * @target_state: Targated state. |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 87 | * |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 88 | */ |
| 89 | static void versal_pwr_domain_suspend_finish( |
| 90 | const psci_power_state_t *target_state) |
| 91 | { |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 92 | uint32_t cpu_id = plat_my_core_pos(); |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 93 | const struct pm_proc *proc = pm_get_proc(cpu_id); |
| 94 | |
Abhyuday Godhasara | 589afa5 | 2021-08-11 06:15:13 -0700 | [diff] [blame] | 95 | for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) { |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 96 | VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", |
| 97 | __func__, i, target_state->pwr_domain_state[i]); |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 98 | } |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 99 | |
| 100 | /* Clear the APU power control register for this cpu */ |
| 101 | pm_client_wakeup(proc); |
| 102 | |
| 103 | /* enable coherency */ |
| 104 | plat_arm_interconnect_enter_coherency(); |
| 105 | |
| 106 | /* APU was turned off, so restore GIC context */ |
| 107 | if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) { |
| 108 | plat_versal_gic_resume(); |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 109 | } |
Ravi Patel | eafc878 | 2019-06-21 05:00:49 -0700 | [diff] [blame] | 110 | |
| 111 | plat_versal_gic_cpuif_enable(); |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 112 | } |
| 113 | |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 114 | void versal_pwr_domain_on_finish(const psci_power_state_t *target_state) |
| 115 | { |
| 116 | /* Enable the gic cpu interface */ |
| 117 | plat_versal_gic_pcpu_init(); |
| 118 | |
| 119 | /* Program the gic per-cpu distributor or re-distributor interface */ |
| 120 | plat_versal_gic_cpuif_enable(); |
| 121 | } |
| 122 | |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 123 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 124 | * versal_system_off() - This function sends the system off request to firmware. |
| 125 | * This function does not return. |
| 126 | * |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 127 | */ |
| 128 | static void __dead2 versal_system_off(void) |
| 129 | { |
| 130 | /* Send the power down request to the PMC */ |
Abhyuday Godhasara | f435a14 | 2021-08-20 00:04:33 -0700 | [diff] [blame] | 131 | (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_SHUTDOWN, |
| 132 | pm_get_shutdown_scope(), SECURE_FLAG); |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 133 | |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 134 | while (1) { |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 135 | wfi(); |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 136 | } |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 140 | * versal_system_reset() - This function sends the reset request to firmware |
| 141 | * for the system to reset. This function does not |
| 142 | * return. |
| 143 | * |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 144 | */ |
| 145 | static void __dead2 versal_system_reset(void) |
| 146 | { |
| 147 | /* Send the system reset request to the PMC */ |
Abhyuday Godhasara | f435a14 | 2021-08-20 00:04:33 -0700 | [diff] [blame] | 148 | (void)pm_system_shutdown(XPM_SHUTDOWN_TYPE_RESET, |
| 149 | pm_get_shutdown_scope(), SECURE_FLAG); |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 150 | |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 151 | while (1) { |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 152 | wfi(); |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 153 | } |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 157 | * versal_pwr_domain_off() - This function performs actions to turn off core. |
| 158 | * @target_state: Targated state. |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 159 | * |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 160 | */ |
| 161 | static void versal_pwr_domain_off(const psci_power_state_t *target_state) |
| 162 | { |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 163 | uint32_t cpu_id = plat_my_core_pos(); |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 164 | const struct pm_proc *proc = pm_get_proc(cpu_id); |
| 165 | |
Abhyuday Godhasara | 589afa5 | 2021-08-11 06:15:13 -0700 | [diff] [blame] | 166 | for (size_t i = 0U; i <= PLAT_MAX_PWR_LVL; i++) { |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 167 | VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", |
| 168 | __func__, i, target_state->pwr_domain_state[i]); |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 169 | } |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 170 | |
| 171 | /* Prevent interrupts from spuriously waking up this cpu */ |
| 172 | plat_versal_gic_cpuif_disable(); |
| 173 | |
| 174 | /* |
| 175 | * Send request to PMC to power down the appropriate APU CPU |
| 176 | * core. |
| 177 | * According to PSCI specification, CPU_off function does not |
| 178 | * have resume address and CPU core can only be woken up |
| 179 | * invoking CPU_on function, during which resume address will |
| 180 | * be set. |
| 181 | */ |
Abhyuday Godhasara | f435a14 | 2021-08-20 00:04:33 -0700 | [diff] [blame] | 182 | (void)pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0, |
| 183 | SECURE_FLAG); |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * versal_validate_power_state() - This function ensures that the power state |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 188 | * parameter in request is valid. |
| 189 | * @power_state: Power state of core. |
| 190 | * @req_state: Requested state. |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 191 | * |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 192 | * Return: Returns status, either success or reason. |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 193 | * |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 194 | */ |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 195 | static int32_t versal_validate_power_state(uint32_t power_state, |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 196 | psci_power_state_t *req_state) |
| 197 | { |
| 198 | VERBOSE("%s: power_state: 0x%x\n", __func__, power_state); |
| 199 | |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 200 | uint32_t pstate = psci_get_pstate_type(power_state); |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 201 | |
| 202 | assert(req_state); |
| 203 | |
| 204 | /* Sanity check the requested state */ |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 205 | if (pstate == PSTATE_TYPE_STANDBY) { |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 206 | req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE; |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 207 | } else { |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 208 | req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE; |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 209 | } |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 210 | |
| 211 | /* We expect the 'state id' to be zero */ |
Abhyuday Godhasara | bacbdee | 2021-08-20 00:27:03 -0700 | [diff] [blame] | 212 | if (psci_get_pstate_id(power_state) != 0U) { |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 213 | return PSCI_E_INVALID_PARAMS; |
Abhyuday Godhasara | f55a5cd | 2021-08-09 08:15:13 -0700 | [diff] [blame] | 214 | } |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 215 | |
| 216 | return PSCI_E_SUCCESS; |
| 217 | } |
| 218 | |
| 219 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 220 | * versal_get_sys_suspend_power_state() - Get power state for system suspend. |
| 221 | * @req_state: Requested state. |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 222 | * |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 223 | */ |
| 224 | static void versal_get_sys_suspend_power_state(psci_power_state_t *req_state) |
| 225 | { |
| 226 | req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE; |
| 227 | req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE; |
| 228 | } |
| 229 | |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 230 | static const struct plat_psci_ops versal_nopmc_psci_ops = { |
Tejas Patel | 6171711 | 2019-02-27 18:44:57 +0530 | [diff] [blame] | 231 | .pwr_domain_on = versal_pwr_domain_on, |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 232 | .pwr_domain_off = versal_pwr_domain_off, |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 233 | .pwr_domain_on_finish = versal_pwr_domain_on_finish, |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 234 | .pwr_domain_suspend = versal_pwr_domain_suspend, |
| 235 | .pwr_domain_suspend_finish = versal_pwr_domain_suspend_finish, |
Saeed Nowshadi | c5a1bda | 2019-12-08 23:35:35 -0800 | [diff] [blame] | 236 | .system_off = versal_system_off, |
| 237 | .system_reset = versal_system_reset, |
Tejas Patel | 54d1319 | 2019-02-27 18:44:55 +0530 | [diff] [blame] | 238 | .validate_power_state = versal_validate_power_state, |
| 239 | .get_sys_suspend_power_state = versal_get_sys_suspend_power_state, |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | /******************************************************************************* |
| 243 | * Export the platform specific power ops. |
| 244 | ******************************************************************************/ |
Venkatesh Yadav Abbarapu | 2cefbcd | 2022-07-31 14:05:40 +0530 | [diff] [blame] | 245 | int32_t plat_setup_psci_ops(uintptr_t sec_entrypoint, |
Siva Durga Prasad Paladugu | fe4af66 | 2018-09-25 18:44:58 +0530 | [diff] [blame] | 246 | const struct plat_psci_ops **psci_ops) |
| 247 | { |
| 248 | versal_sec_entry = sec_entrypoint; |
| 249 | |
| 250 | *psci_ops = &versal_nopmc_psci_ops; |
| 251 | |
| 252 | return 0; |
| 253 | } |