Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2019 Broadcom. |
| 4 | */ |
Hou Zhiqiang | 295b42d | 2021-03-05 15:02:35 +0800 | [diff] [blame] | 5 | #include <cpu_func.h> |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 6 | #include <dm.h> |
Patrick Rudolph | ae457ed | 2024-10-23 15:20:03 +0200 | [diff] [blame] | 7 | #include <irq.h> |
Patrick Rudolph | 613f1ed | 2024-10-23 15:20:06 +0200 | [diff] [blame] | 8 | #include <asm/acpi_table.h> |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 9 | #include <asm/gic.h> |
| 10 | #include <asm/gic-v3.h> |
| 11 | #include <asm/io.h> |
Patrick Rudolph | 613f1ed | 2024-10-23 15:20:06 +0200 | [diff] [blame] | 12 | #include <dm/acpi.h> |
Patrick Rudolph | ae457ed | 2024-10-23 15:20:03 +0200 | [diff] [blame] | 13 | #include <dt-bindings/interrupt-controller/arm-gic.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 15 | #include <linux/printk.h> |
Wasim Khan | 1fc5480 | 2020-02-14 11:00:52 +0530 | [diff] [blame] | 16 | #include <linux/sizes.h> |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 17 | |
| 18 | static u32 lpi_id_bits; |
| 19 | |
| 20 | #define LPI_NRBITS lpi_id_bits |
| 21 | #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K) |
| 22 | #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K) |
| 23 | |
| 24 | /* |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 25 | * gic_v3_its_priv - gic details |
| 26 | * |
| 27 | * @gicd_base: gicd base address |
| 28 | * @gicr_base: gicr base address |
| 29 | */ |
| 30 | struct gic_v3_its_priv { |
| 31 | ulong gicd_base; |
| 32 | ulong gicr_base; |
Patrick Rudolph | 613f1ed | 2024-10-23 15:20:06 +0200 | [diff] [blame] | 33 | ulong gicr_length; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static int gic_v3_its_get_gic_addr(struct gic_v3_its_priv *priv) |
| 37 | { |
| 38 | struct udevice *dev; |
| 39 | fdt_addr_t addr; |
Patrick Rudolph | 613f1ed | 2024-10-23 15:20:06 +0200 | [diff] [blame] | 40 | fdt_size_t size; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 41 | int ret; |
| 42 | |
| 43 | ret = uclass_get_device_by_driver(UCLASS_IRQ, |
Patrick Rudolph | b73bd2f | 2024-10-23 15:20:02 +0200 | [diff] [blame] | 44 | DM_DRIVER_GET(arm_gic_v3), &dev); |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 45 | if (ret) { |
| 46 | pr_err("%s: failed to get %s irq device\n", __func__, |
Patrick Rudolph | b73bd2f | 2024-10-23 15:20:02 +0200 | [diff] [blame] | 47 | DM_DRIVER_GET(arm_gic_v3)->name); |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | addr = dev_read_addr_index(dev, 0); |
| 52 | if (addr == FDT_ADDR_T_NONE) { |
| 53 | pr_err("%s: failed to get GICD address\n", __func__); |
| 54 | return -EINVAL; |
| 55 | } |
| 56 | priv->gicd_base = addr; |
| 57 | |
Patrick Rudolph | 613f1ed | 2024-10-23 15:20:06 +0200 | [diff] [blame] | 58 | addr = dev_read_addr_size_index(dev, 1, &size); |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 59 | if (addr == FDT_ADDR_T_NONE) { |
| 60 | pr_err("%s: failed to get GICR address\n", __func__); |
| 61 | return -EINVAL; |
| 62 | } |
| 63 | priv->gicr_base = addr; |
Patrick Rudolph | 613f1ed | 2024-10-23 15:20:06 +0200 | [diff] [blame] | 64 | priv->gicr_length = size; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 70 | * Program the GIC LPI configuration tables for all |
| 71 | * the re-distributors and enable the LPI table |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 72 | * base: Configuration table address |
| 73 | * num_redist: number of redistributors |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 74 | */ |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 75 | int gic_lpi_tables_init(u64 base, u32 num_redist) |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 76 | { |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 77 | struct gic_v3_its_priv priv; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 78 | u32 gicd_typer; |
| 79 | u64 val; |
| 80 | u64 tmp; |
| 81 | int i; |
| 82 | u64 redist_lpi_base; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 83 | u64 pend_base; |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 84 | ulong pend_tab_total_sz = num_redist * LPI_PENDBASE_SZ; |
Hou Zhiqiang | 295b42d | 2021-03-05 15:02:35 +0800 | [diff] [blame] | 85 | void *pend_tab_va; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 86 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 87 | if (gic_v3_its_get_gic_addr(&priv)) |
| 88 | return -EINVAL; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 89 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 90 | gicd_typer = readl((uintptr_t)(priv.gicd_base + GICD_TYPER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 91 | /* GIC support for Locality specific peripheral interrupts (LPI's) */ |
| 92 | if (!(gicd_typer & GICD_TYPER_LPIS)) { |
| 93 | pr_err("GIC implementation does not support LPI's\n"); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Check for LPI is disabled for all the redistributors. |
| 99 | * Once the LPI table is enabled, can not program the |
| 100 | * LPI configuration tables again, unless the GIC is reset. |
| 101 | */ |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 102 | for (i = 0; i < num_redist; i++) { |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 103 | u32 offset = i * GIC_REDISTRIBUTOR_OFFSET; |
| 104 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 105 | if ((readl((uintptr_t)(priv.gicr_base + offset))) & |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 106 | GICR_CTLR_ENABLE_LPIS) { |
| 107 | pr_err("Re-Distributor %d LPI is already enabled\n", |
| 108 | i); |
| 109 | return -EINVAL; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* lpi_id_bits to get LPI_PENDBASE_SZ and LPi_PROPBASE_SZ */ |
| 114 | lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gicd_typer), |
| 115 | ITS_MAX_LPI_NRBITS); |
| 116 | |
| 117 | /* Set PropBase */ |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 118 | val = (base | |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 119 | GICR_PROPBASER_INNERSHAREABLE | |
| 120 | GICR_PROPBASER_RAWAWB | |
| 121 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); |
| 122 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 123 | writeq(val, (uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
| 124 | tmp = readl((uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 125 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { |
| 126 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
| 127 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | |
| 128 | GICR_PROPBASER_CACHEABILITY_MASK); |
| 129 | val |= GICR_PROPBASER_NC; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 130 | writeq(val, |
| 131 | (uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 135 | redist_lpi_base = base + LPI_PROPBASE_SZ; |
Hou Zhiqiang | 295b42d | 2021-03-05 15:02:35 +0800 | [diff] [blame] | 136 | pend_tab_va = map_physmem(redist_lpi_base, pend_tab_total_sz, |
| 137 | MAP_NOCACHE); |
| 138 | memset(pend_tab_va, 0, pend_tab_total_sz); |
| 139 | flush_cache((ulong)pend_tab_va, pend_tab_total_sz); |
| 140 | unmap_physmem(pend_tab_va, MAP_NOCACHE); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 141 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 142 | pend_base = priv.gicr_base + GICR_PENDBASER; |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 143 | for (i = 0; i < num_redist; i++) { |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 144 | u32 offset = i * GIC_REDISTRIBUTOR_OFFSET; |
| 145 | |
| 146 | val = ((redist_lpi_base + (i * LPI_PENDBASE_SZ)) | |
| 147 | GICR_PENDBASER_INNERSHAREABLE | |
Hou Zhiqiang | 295b42d | 2021-03-05 15:02:35 +0800 | [diff] [blame] | 148 | GICR_PENDBASER_RAWAWB | |
| 149 | GICR_PENDBASER_PTZ); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 150 | |
| 151 | writeq(val, (uintptr_t)(pend_base + offset)); |
| 152 | tmp = readq((uintptr_t)(pend_base + offset)); |
| 153 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { |
| 154 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | |
| 155 | GICR_PENDBASER_CACHEABILITY_MASK); |
| 156 | val |= GICR_PENDBASER_NC; |
| 157 | writeq(val, (uintptr_t)(pend_base + offset)); |
| 158 | } |
| 159 | |
| 160 | /* Enable LPI for the redistributor */ |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 161 | writel(GICR_CTLR_ENABLE_LPIS, |
| 162 | (uintptr_t)(priv.gicr_base + offset)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Patrick Rudolph | 613f1ed | 2024-10-23 15:20:06 +0200 | [diff] [blame] | 168 | #ifdef CONFIG_ACPIGEN |
| 169 | /** |
| 170 | * acpi_gicv3_fill_madt() - Fill out the body of the MADT |
| 171 | * |
| 172 | * Write GICD and GICR tables based on collected devicetree data. |
| 173 | * |
| 174 | * @dev: Device to write ACPI tables for |
| 175 | * @ctx: ACPI context to write MADT sub-tables to |
| 176 | * Return: 0 if OK |
| 177 | */ |
| 178 | static int acpi_gicv3_fill_madt(const struct udevice *dev, struct acpi_ctx *ctx) |
| 179 | { |
| 180 | struct acpi_madt_gicd *gicd; |
| 181 | struct acpi_madt_gicr *gicr; |
| 182 | |
| 183 | struct gic_v3_its_priv priv; |
| 184 | |
| 185 | if (gic_v3_its_get_gic_addr(&priv)) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | gicd = ctx->current; |
| 189 | acpi_write_madt_gicd(gicd, dev_seq(dev), priv.gicd_base, 3); |
| 190 | acpi_inc(ctx, gicd->length); |
| 191 | |
| 192 | gicr = ctx->current; |
| 193 | acpi_write_madt_gicr(gicr, priv.gicr_base, priv.gicr_length); |
| 194 | acpi_inc(ctx, gicr->length); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | struct acpi_ops gic_v3_acpi_ops = { |
| 200 | .fill_madt = acpi_gicv3_fill_madt, |
| 201 | }; |
| 202 | #endif |
| 203 | |
Patrick Rudolph | b73bd2f | 2024-10-23 15:20:02 +0200 | [diff] [blame] | 204 | static const struct udevice_id gic_v3_ids[] = { |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 205 | { .compatible = "arm,gic-v3" }, |
| 206 | {} |
| 207 | }; |
| 208 | |
Patrick Rudolph | ae457ed | 2024-10-23 15:20:03 +0200 | [diff] [blame] | 209 | static int arm_gic_v3_of_xlate(struct irq *irq, struct ofnode_phandle_args *args) |
| 210 | { |
| 211 | if (args->args_count < 3) { |
| 212 | log_debug("Invalid args_count: %d\n", args->args_count); |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | |
| 216 | if (args->args[0] == GIC_SPI) |
| 217 | irq->id = args->args[1] + 32; |
| 218 | else |
| 219 | irq->id = args->args[1] + 16; |
| 220 | |
| 221 | irq->flags = args->args[2]; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static const struct irq_ops arm_gic_v3_ops = { |
| 227 | .of_xlate = arm_gic_v3_of_xlate, |
| 228 | }; |
| 229 | |
Patrick Rudolph | b73bd2f | 2024-10-23 15:20:02 +0200 | [diff] [blame] | 230 | U_BOOT_DRIVER(arm_gic_v3) = { |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 231 | .name = "gic-v3", |
| 232 | .id = UCLASS_IRQ, |
Patrick Rudolph | b73bd2f | 2024-10-23 15:20:02 +0200 | [diff] [blame] | 233 | .of_match = gic_v3_ids, |
Patrick Rudolph | ae457ed | 2024-10-23 15:20:03 +0200 | [diff] [blame] | 234 | .ops = &arm_gic_v3_ops, |
Patrick Rudolph | 613f1ed | 2024-10-23 15:20:06 +0200 | [diff] [blame] | 235 | ACPI_OPS_PTR(&gic_v3_acpi_ops) |
| 236 | }; |
| 237 | |
| 238 | #ifdef CONFIG_ACPIGEN |
| 239 | /** |
| 240 | * acpi_gic_its_fill_madt() - Fill out the body of the MADT |
| 241 | * |
| 242 | * Write ITS tables based on collected devicetree data. |
| 243 | * |
| 244 | * @dev: Device to write ACPI tables for |
| 245 | * @ctx: ACPI context to write MADT sub-tables to |
| 246 | * Return: 0 if OK |
| 247 | */ |
| 248 | static int acpi_gic_its_fill_madt(const struct udevice *dev, struct acpi_ctx *ctx) |
| 249 | { |
| 250 | struct acpi_madt_its *its; |
| 251 | fdt_addr_t addr; |
| 252 | |
| 253 | addr = dev_read_addr_index(dev, 0); |
| 254 | if (addr == FDT_ADDR_T_NONE) { |
| 255 | pr_err("%s: failed to get GIC ITS address\n", __func__); |
| 256 | return -EINVAL; |
| 257 | } |
| 258 | |
| 259 | its = ctx->current; |
| 260 | acpi_write_madt_its(its, dev_seq(dev), addr); |
| 261 | acpi_inc(ctx, its->length); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | struct acpi_ops gic_v3_its_acpi_ops = { |
| 267 | .fill_madt = acpi_gic_its_fill_madt, |
| 268 | }; |
| 269 | #endif |
| 270 | |
| 271 | static const struct udevice_id gic_v3_its_ids[] = { |
| 272 | { .compatible = "arm,gic-v3-its" }, |
| 273 | {} |
| 274 | }; |
| 275 | |
| 276 | U_BOOT_DRIVER(arm_gic_v3_its) = { |
| 277 | .name = "gic-v3-its", |
| 278 | .id = UCLASS_IRQ, |
| 279 | .of_match = gic_v3_its_ids, |
| 280 | ACPI_OPS_PTR(&gic_v3_its_acpi_ops) |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 281 | }; |