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