Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
Michal Simek | 2a47faa | 2023-04-14 08:43:51 +0200 | [diff] [blame] | 2 | * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved. |
Jay Buddhabhatti | 26e138a | 2022-12-21 23:03:35 -0800 | [diff] [blame] | 3 | * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved. |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 4 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 5 | * SPDX-License-Identifier: BSD-3-Clause |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 8 | #include <assert.h> |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 9 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <arch_helpers.h> |
| 12 | #include <common/debug.h> |
| 13 | #include <drivers/arm/gicv2.h> |
| 14 | #include <lib/mmio.h> |
| 15 | #include <lib/psci/psci.h> |
Antonio Nino Diaz | bd7b740 | 2019-01-25 14:30:04 +0000 | [diff] [blame] | 16 | #include <plat/arm/common/plat_arm.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | #include <plat/common/platform.h> |
| 18 | |
Antonio Nino Diaz | bd7b740 | 2019-01-25 14:30:04 +0000 | [diff] [blame] | 19 | #include <plat_private.h> |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 20 | #include "pm_client.h" |
Jay Buddhabhatti | 26e138a | 2022-12-21 23:03:35 -0800 | [diff] [blame] | 21 | #include "zynqmp_pm_api_sys.h" |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 22 | |
Venkatesh Yadav Abbarapu | c950535 | 2022-05-16 17:29:04 +0530 | [diff] [blame] | 23 | static uintptr_t zynqmp_sec_entry; |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 24 | |
Venkatesh Yadav Abbarapu | c950535 | 2022-05-16 17:29:04 +0530 | [diff] [blame] | 25 | static void zynqmp_cpu_standby(plat_local_state_t cpu_state) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 26 | { |
| 27 | VERBOSE("%s: cpu_state: 0x%x\n", __func__, cpu_state); |
| 28 | |
| 29 | dsb(); |
| 30 | wfi(); |
| 31 | } |
| 32 | |
Venkatesh Yadav Abbarapu | e7c4538 | 2022-05-19 14:49:49 +0530 | [diff] [blame] | 33 | static int32_t zynqmp_pwr_domain_on(u_register_t mpidr) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 34 | { |
Venkatesh Yadav Abbarapu | e7c4538 | 2022-05-19 14:49:49 +0530 | [diff] [blame] | 35 | uint32_t cpu_id = plat_core_pos_by_mpidr(mpidr); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 36 | const struct pm_proc *proc; |
Ravi Patel | 2f34d36 | 2021-04-15 05:55:19 -0700 | [diff] [blame] | 37 | uint32_t buff[3]; |
| 38 | enum pm_ret_status ret; |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 39 | |
| 40 | VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr); |
| 41 | |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 42 | if (cpu_id == -1) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 43 | return PSCI_E_INTERN_FAIL; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 44 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 45 | proc = pm_get_proc(cpu_id); |
Ravi Patel | 2f34d36 | 2021-04-15 05:55:19 -0700 | [diff] [blame] | 46 | |
| 47 | /* Check the APU proc status before wakeup */ |
| 48 | ret = pm_get_node_status(proc->node_id, buff); |
| 49 | if ((ret != PM_RET_SUCCESS) || (buff[0] == PM_PROC_STATE_SUSPENDING)) { |
| 50 | return PSCI_E_INTERN_FAIL; |
| 51 | } |
| 52 | |
Filip Drazic | d7d62ce | 2017-02-07 12:03:56 +0100 | [diff] [blame] | 53 | /* Clear power down request */ |
| 54 | pm_client_wakeup(proc); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 55 | |
| 56 | /* Send request to PMU to wake up selected APU CPU core */ |
Soren Brinkmann | 17aea22 | 2016-05-19 07:20:14 -0700 | [diff] [blame] | 57 | pm_req_wakeup(proc->node_id, 1, zynqmp_sec_entry, REQ_ACK_BLOCKING); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 58 | |
| 59 | return PSCI_E_SUCCESS; |
| 60 | } |
| 61 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 62 | static void zynqmp_pwr_domain_off(const psci_power_state_t *target_state) |
| 63 | { |
Venkatesh Yadav Abbarapu | e7c4538 | 2022-05-19 14:49:49 +0530 | [diff] [blame] | 64 | uint32_t cpu_id = plat_my_core_pos(); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 65 | const struct pm_proc *proc = pm_get_proc(cpu_id); |
| 66 | |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 67 | for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 68 | VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", |
| 69 | __func__, i, target_state->pwr_domain_state[i]); |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 70 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 71 | |
| 72 | /* Prevent interrupts from spuriously waking up this cpu */ |
| 73 | gicv2_cpuif_disable(); |
| 74 | |
| 75 | /* |
| 76 | * Send request to PMU to power down the appropriate APU CPU |
| 77 | * core. |
| 78 | * According to PSCI specification, CPU_off function does not |
| 79 | * have resume address and CPU core can only be woken up |
| 80 | * invoking CPU_on function, during which resume address will |
| 81 | * be set. |
| 82 | */ |
Filip Drazic | 0bd9d0c | 2016-07-20 17:17:39 +0200 | [diff] [blame] | 83 | pm_self_suspend(proc->node_id, MAX_LATENCY, PM_STATE_CPU_IDLE, 0); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 86 | static void zynqmp_pwr_domain_suspend(const psci_power_state_t *target_state) |
| 87 | { |
Venkatesh Yadav Abbarapu | e7c4538 | 2022-05-19 14:49:49 +0530 | [diff] [blame] | 88 | uint32_t state; |
| 89 | uint32_t cpu_id = plat_my_core_pos(); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 90 | const struct pm_proc *proc = pm_get_proc(cpu_id); |
| 91 | |
| 92 | for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) |
| 93 | VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", |
| 94 | __func__, i, target_state->pwr_domain_state[i]); |
| 95 | |
Filip Drazic | 0bd9d0c | 2016-07-20 17:17:39 +0200 | [diff] [blame] | 96 | state = target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE ? |
| 97 | PM_STATE_SUSPEND_TO_RAM : PM_STATE_CPU_IDLE; |
| 98 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 99 | /* Send request to PMU to suspend this core */ |
Filip Drazic | 0bd9d0c | 2016-07-20 17:17:39 +0200 | [diff] [blame] | 100 | pm_self_suspend(proc->node_id, MAX_LATENCY, state, zynqmp_sec_entry); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 101 | |
| 102 | /* APU is to be turned off */ |
| 103 | if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 104 | /* disable coherency */ |
| 105 | plat_arm_interconnect_exit_coherency(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void zynqmp_pwr_domain_on_finish(const psci_power_state_t *target_state) |
| 110 | { |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 111 | for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 112 | VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", |
| 113 | __func__, i, target_state->pwr_domain_state[i]); |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 114 | } |
Siva Durga Prasad Paladugu | 60bfbc9 | 2018-09-24 22:51:49 -0700 | [diff] [blame] | 115 | plat_arm_gic_pcpu_init(); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 116 | gicv2_cpuif_enable(); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 119 | static void zynqmp_pwr_domain_suspend_finish(const psci_power_state_t *target_state) |
| 120 | { |
Venkatesh Yadav Abbarapu | e7c4538 | 2022-05-19 14:49:49 +0530 | [diff] [blame] | 121 | uint32_t cpu_id = plat_my_core_pos(); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 122 | const struct pm_proc *proc = pm_get_proc(cpu_id); |
| 123 | |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 124 | for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 125 | VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n", |
| 126 | __func__, i, target_state->pwr_domain_state[i]); |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 127 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 128 | |
| 129 | /* Clear the APU power control register for this cpu */ |
| 130 | pm_client_wakeup(proc); |
| 131 | |
| 132 | /* enable coherency */ |
| 133 | plat_arm_interconnect_enter_coherency(); |
Soren Brinkmann | 3b6ebcb | 2016-02-18 21:16:35 -0800 | [diff] [blame] | 134 | /* APU was turned off */ |
| 135 | if (target_state->pwr_domain_state[1] > PLAT_MAX_RET_STATE) { |
| 136 | plat_arm_gic_init(); |
| 137 | } else { |
| 138 | gicv2_cpuif_enable(); |
| 139 | gicv2_pcpu_distif_init(); |
| 140 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /******************************************************************************* |
| 144 | * ZynqMP handlers to shutdown/reboot the system |
| 145 | ******************************************************************************/ |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 146 | |
| 147 | static void __dead2 zynqmp_system_off(void) |
| 148 | { |
| 149 | /* disable coherency */ |
| 150 | plat_arm_interconnect_exit_coherency(); |
| 151 | |
| 152 | /* Send the power down request to the PMU */ |
Soren Brinkmann | 58fbb9b | 2016-09-02 09:50:54 -0700 | [diff] [blame] | 153 | pm_system_shutdown(PMF_SHUTDOWN_TYPE_SHUTDOWN, |
Siva Durga Prasad Paladugu | 1f80d3f | 2018-04-30 15:56:10 +0530 | [diff] [blame] | 154 | pm_get_shutdown_scope()); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 155 | |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 156 | while (1) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 157 | wfi(); |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 158 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 161 | static void __dead2 zynqmp_system_reset(void) |
| 162 | { |
| 163 | /* disable coherency */ |
| 164 | plat_arm_interconnect_exit_coherency(); |
| 165 | |
| 166 | /* Send the system reset request to the PMU */ |
Soren Brinkmann | 58fbb9b | 2016-09-02 09:50:54 -0700 | [diff] [blame] | 167 | pm_system_shutdown(PMF_SHUTDOWN_TYPE_RESET, |
Siva Durga Prasad Paladugu | 1f80d3f | 2018-04-30 15:56:10 +0530 | [diff] [blame] | 168 | pm_get_shutdown_scope()); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 169 | |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 170 | while (1) { |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 171 | wfi(); |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 172 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Venkatesh Yadav Abbarapu | e7c4538 | 2022-05-19 14:49:49 +0530 | [diff] [blame] | 175 | static int32_t zynqmp_validate_power_state(uint32_t power_state, |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 176 | psci_power_state_t *req_state) |
| 177 | { |
| 178 | VERBOSE("%s: power_state: 0x%x\n", __func__, power_state); |
| 179 | |
Venkatesh Yadav Abbarapu | a2ca35d | 2022-07-04 11:40:27 +0530 | [diff] [blame] | 180 | uint32_t pstate = psci_get_pstate_type(power_state); |
Stefan Krsmanovic | 3779c5c | 2016-05-09 18:00:47 +0200 | [diff] [blame] | 181 | |
| 182 | assert(req_state); |
| 183 | |
| 184 | /* Sanity check the requested state */ |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 185 | if (pstate == PSTATE_TYPE_STANDBY) { |
Stefan Krsmanovic | 3779c5c | 2016-05-09 18:00:47 +0200 | [diff] [blame] | 186 | req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_RET_STATE; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 187 | } else { |
Stefan Krsmanovic | 3779c5c | 2016-05-09 18:00:47 +0200 | [diff] [blame] | 188 | req_state->pwr_domain_state[MPIDR_AFFLVL0] = PLAT_MAX_OFF_STATE; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 189 | } |
Stefan Krsmanovic | 3779c5c | 2016-05-09 18:00:47 +0200 | [diff] [blame] | 190 | /* We expect the 'state id' to be zero */ |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 191 | if (psci_get_pstate_id(power_state)) { |
Stefan Krsmanovic | 3779c5c | 2016-05-09 18:00:47 +0200 | [diff] [blame] | 192 | return PSCI_E_INVALID_PARAMS; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 193 | } |
Stefan Krsmanovic | 3779c5c | 2016-05-09 18:00:47 +0200 | [diff] [blame] | 194 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 195 | return PSCI_E_SUCCESS; |
| 196 | } |
| 197 | |
Venkatesh Yadav Abbarapu | c950535 | 2022-05-16 17:29:04 +0530 | [diff] [blame] | 198 | static void zynqmp_get_sys_suspend_power_state(psci_power_state_t *req_state) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 199 | { |
| 200 | req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE; |
| 201 | req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE; |
| 202 | } |
| 203 | |
| 204 | /******************************************************************************* |
| 205 | * Export the platform handlers to enable psci to invoke them |
| 206 | ******************************************************************************/ |
| 207 | static const struct plat_psci_ops zynqmp_psci_ops = { |
| 208 | .cpu_standby = zynqmp_cpu_standby, |
| 209 | .pwr_domain_on = zynqmp_pwr_domain_on, |
| 210 | .pwr_domain_off = zynqmp_pwr_domain_off, |
| 211 | .pwr_domain_suspend = zynqmp_pwr_domain_suspend, |
| 212 | .pwr_domain_on_finish = zynqmp_pwr_domain_on_finish, |
| 213 | .pwr_domain_suspend_finish = zynqmp_pwr_domain_suspend_finish, |
| 214 | .system_off = zynqmp_system_off, |
| 215 | .system_reset = zynqmp_system_reset, |
| 216 | .validate_power_state = zynqmp_validate_power_state, |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 217 | .get_sys_suspend_power_state = zynqmp_get_sys_suspend_power_state, |
| 218 | }; |
| 219 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 220 | /******************************************************************************* |
| 221 | * Export the platform specific power ops. |
| 222 | ******************************************************************************/ |
| 223 | int plat_setup_psci_ops(uintptr_t sec_entrypoint, |
| 224 | const struct plat_psci_ops **psci_ops) |
| 225 | { |
| 226 | zynqmp_sec_entry = sec_entrypoint; |
| 227 | |
Siva Durga Prasad Paladugu | 40808bc | 2018-04-30 19:43:03 +0530 | [diff] [blame] | 228 | *psci_ops = &zynqmp_psci_ops; |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |