Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 1 | /* |
Tanmay Shah | b145e12 | 2022-03-23 12:43:45 -0700 | [diff] [blame] | 2 | * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved. |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * APU specific definition of processors in the subsystem as well as functions |
| 9 | * for getting information about and changing state of the APU. |
| 10 | */ |
| 11 | |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 12 | #include <assert.h> |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 13 | #include <plat_ipi.h> |
| 14 | #include <platform_def.h> |
| 15 | #include <versal_def.h> |
| 16 | #include <lib/bakery_lock.h> |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 17 | #include <lib/mmio.h> |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 18 | #include <lib/utils.h> |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 19 | #include <drivers/arm/gicv3.h> |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 20 | #include <drivers/arm/gic_common.h> |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 21 | #include <plat/common/platform.h> |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 22 | #include "pm_api_sys.h" |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 23 | #include "pm_client.h" |
Tanmay Shah | b145e12 | 2022-03-23 12:43:45 -0700 | [diff] [blame] | 24 | #include "pm_defs.h" |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 25 | |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 26 | #define UNDEFINED_CPUID (~0) |
Abhyuday Godhasara | edc38ae | 2021-08-04 23:58:46 -0700 | [diff] [blame] | 27 | #define IRQ_MAX 142U |
| 28 | #define NUM_GICD_ISENABLER ((IRQ_MAX >> 5U) + 1U) |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 29 | |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 30 | DEFINE_BAKERY_LOCK(pm_client_secure_lock); |
| 31 | |
| 32 | static const struct pm_ipi apu_ipi = { |
| 33 | .local_ipi_id = IPI_ID_APU, |
| 34 | .remote_ipi_id = IPI_ID_PMC, |
| 35 | .buffer_base = IPI_BUFFER_APU_BASE, |
| 36 | }; |
| 37 | |
| 38 | /* Order in pm_procs_all array must match cpu ids */ |
| 39 | static const struct pm_proc pm_procs_all[] = { |
| 40 | { |
| 41 | .node_id = XPM_DEVID_ACPU_0, |
| 42 | .ipi = &apu_ipi, |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 43 | .pwrdn_mask = APU_0_PWRCTL_CPUPWRDWNREQ_MASK, |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 44 | }, |
| 45 | { |
| 46 | .node_id = XPM_DEVID_ACPU_1, |
| 47 | .ipi = &apu_ipi, |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 48 | .pwrdn_mask = APU_1_PWRCTL_CPUPWRDWNREQ_MASK, |
Tejas Patel | 354fe57 | 2018-12-14 00:55:37 -0800 | [diff] [blame] | 49 | } |
| 50 | }; |
| 51 | |
| 52 | const struct pm_proc *primary_proc = &pm_procs_all[0]; |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 53 | |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 54 | /* Interrupt to PM node index map */ |
| 55 | static enum pm_device_node_idx irq_node_map[IRQ_MAX + 1] = { |
| 56 | [13] = XPM_NODEIDX_DEV_GPIO, |
| 57 | [14] = XPM_NODEIDX_DEV_I2C_0, |
| 58 | [15] = XPM_NODEIDX_DEV_I2C_1, |
| 59 | [16] = XPM_NODEIDX_DEV_SPI_0, |
| 60 | [17] = XPM_NODEIDX_DEV_SPI_1, |
| 61 | [18] = XPM_NODEIDX_DEV_UART_0, |
| 62 | [19] = XPM_NODEIDX_DEV_UART_1, |
| 63 | [20] = XPM_NODEIDX_DEV_CAN_FD_0, |
| 64 | [21] = XPM_NODEIDX_DEV_CAN_FD_1, |
| 65 | [22] = XPM_NODEIDX_DEV_USB_0, |
| 66 | [23] = XPM_NODEIDX_DEV_USB_0, |
| 67 | [24] = XPM_NODEIDX_DEV_USB_0, |
| 68 | [25] = XPM_NODEIDX_DEV_USB_0, |
| 69 | [26] = XPM_NODEIDX_DEV_USB_0, |
| 70 | [37] = XPM_NODEIDX_DEV_TTC_0, |
| 71 | [38] = XPM_NODEIDX_DEV_TTC_0, |
| 72 | [39] = XPM_NODEIDX_DEV_TTC_0, |
| 73 | [40] = XPM_NODEIDX_DEV_TTC_1, |
| 74 | [41] = XPM_NODEIDX_DEV_TTC_1, |
| 75 | [42] = XPM_NODEIDX_DEV_TTC_1, |
| 76 | [43] = XPM_NODEIDX_DEV_TTC_2, |
| 77 | [44] = XPM_NODEIDX_DEV_TTC_2, |
| 78 | [45] = XPM_NODEIDX_DEV_TTC_2, |
| 79 | [46] = XPM_NODEIDX_DEV_TTC_3, |
| 80 | [47] = XPM_NODEIDX_DEV_TTC_3, |
| 81 | [48] = XPM_NODEIDX_DEV_TTC_3, |
| 82 | [56] = XPM_NODEIDX_DEV_GEM_0, |
| 83 | [57] = XPM_NODEIDX_DEV_GEM_0, |
| 84 | [58] = XPM_NODEIDX_DEV_GEM_1, |
| 85 | [59] = XPM_NODEIDX_DEV_GEM_1, |
| 86 | [60] = XPM_NODEIDX_DEV_ADMA_0, |
| 87 | [61] = XPM_NODEIDX_DEV_ADMA_1, |
| 88 | [62] = XPM_NODEIDX_DEV_ADMA_2, |
| 89 | [63] = XPM_NODEIDX_DEV_ADMA_3, |
| 90 | [64] = XPM_NODEIDX_DEV_ADMA_4, |
| 91 | [65] = XPM_NODEIDX_DEV_ADMA_5, |
| 92 | [66] = XPM_NODEIDX_DEV_ADMA_6, |
| 93 | [67] = XPM_NODEIDX_DEV_ADMA_7, |
| 94 | [74] = XPM_NODEIDX_DEV_USB_0, |
| 95 | [126] = XPM_NODEIDX_DEV_SDIO_0, |
| 96 | [127] = XPM_NODEIDX_DEV_SDIO_0, |
| 97 | [128] = XPM_NODEIDX_DEV_SDIO_1, |
| 98 | [129] = XPM_NODEIDX_DEV_SDIO_1, |
| 99 | [142] = XPM_NODEIDX_DEV_RTC, |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * irq_to_pm_node_idx - Get PM node index corresponding to the interrupt number |
| 104 | * @irq: Interrupt number |
| 105 | * |
| 106 | * Return: PM node index corresponding to the specified interrupt |
| 107 | */ |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 108 | static enum pm_device_node_idx irq_to_pm_node_idx(uint32_t irq) |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 109 | { |
| 110 | assert(irq <= IRQ_MAX); |
| 111 | return irq_node_map[irq]; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * pm_client_set_wakeup_sources - Set all devices with enabled interrupts as |
| 116 | * wake sources in the LibPM. |
Ravi Patel | 023899d | 2019-07-30 04:10:07 -0700 | [diff] [blame] | 117 | * @node_id: Node id of processor |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 118 | */ |
Ravi Patel | 023899d | 2019-07-30 04:10:07 -0700 | [diff] [blame] | 119 | static void pm_client_set_wakeup_sources(uint32_t node_id) |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 120 | { |
| 121 | uint32_t reg_num; |
| 122 | uint32_t device_id; |
Naman Patel | b8434b1 | 2022-11-16 01:54:23 -0800 | [diff] [blame^] | 123 | uint8_t pm_wakeup_nodes_set[XPM_NODEIDX_DEV_MAX] = { 0U }; |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 124 | uintptr_t isenabler1 = PLAT_VERSAL_GICD_BASE + GICD_ISENABLER + 4; |
| 125 | |
Abhyuday Godhasara | edc38ae | 2021-08-04 23:58:46 -0700 | [diff] [blame] | 126 | for (reg_num = 0U; reg_num < NUM_GICD_ISENABLER; reg_num++) { |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 127 | uint32_t base_irq = reg_num << ISENABLER_SHIFT; |
| 128 | uint32_t reg = mmio_read_32(isenabler1 + (reg_num << 2)); |
| 129 | |
Abhyuday Godhasara | 4487794 | 2021-08-05 02:59:26 -0700 | [diff] [blame] | 130 | if (reg == 0U) { |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 131 | continue; |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 132 | } |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 133 | |
Abhyuday Godhasara | 4487794 | 2021-08-05 02:59:26 -0700 | [diff] [blame] | 134 | while (reg != 0U) { |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 135 | enum pm_device_node_idx node_idx; |
Abhyuday Godhasara | 5c67ec1 | 2021-08-05 03:05:22 -0700 | [diff] [blame] | 136 | uint32_t idx, irq, lowest_set = reg & (-reg); |
| 137 | enum pm_ret_status ret; |
Michal Simek | 08341b7 | 2022-03-09 08:53:20 +0100 | [diff] [blame] | 138 | |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 139 | idx = __builtin_ctz(lowest_set); |
| 140 | irq = base_irq + idx; |
| 141 | |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 142 | if (irq > IRQ_MAX) { |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 143 | break; |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 144 | } |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 145 | |
| 146 | node_idx = irq_to_pm_node_idx(irq); |
| 147 | reg &= ~lowest_set; |
| 148 | |
Tanmay Shah | b145e12 | 2022-03-23 12:43:45 -0700 | [diff] [blame] | 149 | if (node_idx > XPM_NODEIDX_DEV_MIN && node_idx < XPM_NODEIDX_DEV_MAX) { |
| 150 | if (pm_wakeup_nodes_set[node_idx] == 0U) { |
| 151 | /* Get device ID from node index */ |
| 152 | device_id = PERIPH_DEVID(node_idx); |
| 153 | ret = pm_set_wakeup_source(node_id, |
| 154 | device_id, 1, |
| 155 | SECURE_FLAG); |
| 156 | pm_wakeup_nodes_set[node_idx] = (ret == PM_RET_SUCCESS) ? |
| 157 | 1 : 0; |
| 158 | } |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 164 | /** |
| 165 | * pm_client_suspend() - Client-specific suspend actions |
| 166 | * |
| 167 | * This function should contain any PU-specific actions |
| 168 | * required prior to sending suspend request to PMU |
| 169 | * Actions taken depend on the state system is suspending to. |
| 170 | */ |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 171 | void pm_client_suspend(const struct pm_proc *proc, uint32_t state) |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 172 | { |
| 173 | bakery_lock_get(&pm_client_secure_lock); |
| 174 | |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 175 | if (state == PM_STATE_SUSPEND_TO_RAM) { |
Abhyuday Godhasara | edc38ae | 2021-08-04 23:58:46 -0700 | [diff] [blame] | 176 | pm_client_set_wakeup_sources((uint32_t)proc->node_id); |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 177 | } |
Tejas Patel | fcb7c16 | 2019-02-27 18:44:56 +0530 | [diff] [blame] | 178 | |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 179 | /* Set powerdown request */ |
| 180 | mmio_write_32(FPD_APU_PWRCTL, mmio_read_32(FPD_APU_PWRCTL) | |
Abhyuday Godhasara | 4487794 | 2021-08-05 02:59:26 -0700 | [diff] [blame] | 181 | (uint32_t)proc->pwrdn_mask); |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 182 | |
| 183 | bakery_lock_release(&pm_client_secure_lock); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * pm_client_abort_suspend() - Client-specific abort-suspend actions |
| 188 | * |
| 189 | * This function should contain any PU-specific actions |
| 190 | * required for aborting a prior suspend request |
| 191 | */ |
| 192 | void pm_client_abort_suspend(void) |
| 193 | { |
| 194 | /* Enable interrupts at processor level (for current cpu) */ |
| 195 | gicv3_cpuif_enable(plat_my_core_pos()); |
| 196 | |
| 197 | bakery_lock_get(&pm_client_secure_lock); |
| 198 | |
| 199 | /* Clear powerdown request */ |
| 200 | mmio_write_32(FPD_APU_PWRCTL, mmio_read_32(FPD_APU_PWRCTL) & |
Abhyuday Godhasara | 4487794 | 2021-08-05 02:59:26 -0700 | [diff] [blame] | 201 | ~((uint32_t)primary_proc->pwrdn_mask)); |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 202 | |
| 203 | bakery_lock_release(&pm_client_secure_lock); |
| 204 | } |
| 205 | |
| 206 | /** |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 207 | * pm_get_cpuid() - get the local cpu ID for a global node ID |
| 208 | * @nid: node id of the processor |
| 209 | * |
| 210 | * Return: the cpu ID (starting from 0) for the subsystem |
| 211 | */ |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 212 | static uint32_t pm_get_cpuid(uint32_t nid) |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 213 | { |
Abhyuday Godhasara | edc38ae | 2021-08-04 23:58:46 -0700 | [diff] [blame] | 214 | for (size_t i = 0U; i < ARRAY_SIZE(pm_procs_all); i++) { |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 215 | if (pm_procs_all[i].node_id == nid) { |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 216 | return i; |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 217 | } |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 218 | } |
| 219 | return UNDEFINED_CPUID; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * pm_client_wakeup() - Client-specific wakeup actions |
| 224 | * |
| 225 | * This function should contain any PU-specific actions |
| 226 | * required for waking up another APU core |
| 227 | */ |
| 228 | void pm_client_wakeup(const struct pm_proc *proc) |
| 229 | { |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 230 | uint32_t cpuid = pm_get_cpuid(proc->node_id); |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 231 | |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 232 | if (cpuid == UNDEFINED_CPUID) { |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 233 | return; |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 234 | } |
Tejas Patel | 7029dd8 | 2019-02-27 18:44:54 +0530 | [diff] [blame] | 235 | |
| 236 | bakery_lock_get(&pm_client_secure_lock); |
| 237 | |
| 238 | /* clear powerdown bit for affected cpu */ |
| 239 | uint32_t val = mmio_read_32(FPD_APU_PWRCTL); |
| 240 | val &= ~(proc->pwrdn_mask); |
| 241 | mmio_write_32(FPD_APU_PWRCTL, val); |
| 242 | |
| 243 | bakery_lock_release(&pm_client_secure_lock); |
| 244 | } |
| 245 | |
| 246 | /** |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 247 | * pm_get_proc() - returns pointer to the proc structure |
| 248 | * @cpuid: id of the cpu whose proc struct pointer should be returned |
| 249 | * |
| 250 | * Return: pointer to a proc structure if proc is found, otherwise NULL |
| 251 | */ |
Venkatesh Yadav Abbarapu | bde8759 | 2022-05-24 11:11:12 +0530 | [diff] [blame] | 252 | const struct pm_proc *pm_get_proc(uint32_t cpuid) |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 253 | { |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 254 | if (cpuid < ARRAY_SIZE(pm_procs_all)) { |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 255 | return &pm_procs_all[cpuid]; |
Abhyuday Godhasara | ddc4662 | 2021-08-05 03:14:17 -0700 | [diff] [blame] | 256 | } |
Tejas Patel | fe0e10a | 2019-12-08 23:29:44 -0800 | [diff] [blame] | 257 | |
| 258 | return NULL; |
| 259 | } |