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> |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 7 | #include <asm/gic.h> |
| 8 | #include <asm/gic-v3.h> |
| 9 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 11 | #include <linux/printk.h> |
Wasim Khan | 1fc5480 | 2020-02-14 11:00:52 +0530 | [diff] [blame] | 12 | #include <linux/sizes.h> |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 13 | |
| 14 | static u32 lpi_id_bits; |
| 15 | |
| 16 | #define LPI_NRBITS lpi_id_bits |
| 17 | #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K) |
| 18 | #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K) |
| 19 | |
| 20 | /* |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 21 | * gic_v3_its_priv - gic details |
| 22 | * |
| 23 | * @gicd_base: gicd base address |
| 24 | * @gicr_base: gicr base address |
| 25 | */ |
| 26 | struct gic_v3_its_priv { |
| 27 | ulong gicd_base; |
| 28 | ulong gicr_base; |
| 29 | }; |
| 30 | |
| 31 | static int gic_v3_its_get_gic_addr(struct gic_v3_its_priv *priv) |
| 32 | { |
| 33 | struct udevice *dev; |
| 34 | fdt_addr_t addr; |
| 35 | int ret; |
| 36 | |
| 37 | ret = uclass_get_device_by_driver(UCLASS_IRQ, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 38 | DM_DRIVER_GET(arm_gic_v3_its), &dev); |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 39 | if (ret) { |
| 40 | pr_err("%s: failed to get %s irq device\n", __func__, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 41 | DM_DRIVER_GET(arm_gic_v3_its)->name); |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | addr = dev_read_addr_index(dev, 0); |
| 46 | if (addr == FDT_ADDR_T_NONE) { |
| 47 | pr_err("%s: failed to get GICD address\n", __func__); |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | priv->gicd_base = addr; |
| 51 | |
| 52 | addr = dev_read_addr_index(dev, 1); |
| 53 | if (addr == FDT_ADDR_T_NONE) { |
| 54 | pr_err("%s: failed to get GICR address\n", __func__); |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | priv->gicr_base = addr; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 63 | * Program the GIC LPI configuration tables for all |
| 64 | * the re-distributors and enable the LPI table |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 65 | * base: Configuration table address |
| 66 | * num_redist: number of redistributors |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 67 | */ |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 68 | int gic_lpi_tables_init(u64 base, u32 num_redist) |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 69 | { |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 70 | struct gic_v3_its_priv priv; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 71 | u32 gicd_typer; |
| 72 | u64 val; |
| 73 | u64 tmp; |
| 74 | int i; |
| 75 | u64 redist_lpi_base; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 76 | u64 pend_base; |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 77 | ulong pend_tab_total_sz = num_redist * LPI_PENDBASE_SZ; |
Hou Zhiqiang | 295b42d | 2021-03-05 15:02:35 +0800 | [diff] [blame] | 78 | void *pend_tab_va; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 79 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 80 | if (gic_v3_its_get_gic_addr(&priv)) |
| 81 | return -EINVAL; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 82 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 83 | gicd_typer = readl((uintptr_t)(priv.gicd_base + GICD_TYPER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 84 | /* GIC support for Locality specific peripheral interrupts (LPI's) */ |
| 85 | if (!(gicd_typer & GICD_TYPER_LPIS)) { |
| 86 | pr_err("GIC implementation does not support LPI's\n"); |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Check for LPI is disabled for all the redistributors. |
| 92 | * Once the LPI table is enabled, can not program the |
| 93 | * LPI configuration tables again, unless the GIC is reset. |
| 94 | */ |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 95 | for (i = 0; i < num_redist; i++) { |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 96 | u32 offset = i * GIC_REDISTRIBUTOR_OFFSET; |
| 97 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 98 | if ((readl((uintptr_t)(priv.gicr_base + offset))) & |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 99 | GICR_CTLR_ENABLE_LPIS) { |
| 100 | pr_err("Re-Distributor %d LPI is already enabled\n", |
| 101 | i); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* lpi_id_bits to get LPI_PENDBASE_SZ and LPi_PROPBASE_SZ */ |
| 107 | lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gicd_typer), |
| 108 | ITS_MAX_LPI_NRBITS); |
| 109 | |
| 110 | /* Set PropBase */ |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 111 | val = (base | |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 112 | GICR_PROPBASER_INNERSHAREABLE | |
| 113 | GICR_PROPBASER_RAWAWB | |
| 114 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); |
| 115 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 116 | writeq(val, (uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
| 117 | tmp = readl((uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 118 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { |
| 119 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
| 120 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | |
| 121 | GICR_PROPBASER_CACHEABILITY_MASK); |
| 122 | val |= GICR_PROPBASER_NC; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 123 | writeq(val, |
| 124 | (uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 128 | redist_lpi_base = base + LPI_PROPBASE_SZ; |
Hou Zhiqiang | 295b42d | 2021-03-05 15:02:35 +0800 | [diff] [blame] | 129 | pend_tab_va = map_physmem(redist_lpi_base, pend_tab_total_sz, |
| 130 | MAP_NOCACHE); |
| 131 | memset(pend_tab_va, 0, pend_tab_total_sz); |
| 132 | flush_cache((ulong)pend_tab_va, pend_tab_total_sz); |
| 133 | unmap_physmem(pend_tab_va, MAP_NOCACHE); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 134 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 135 | pend_base = priv.gicr_base + GICR_PENDBASER; |
Michael Walle | 29b1d33 | 2021-10-27 18:54:54 +0200 | [diff] [blame] | 136 | for (i = 0; i < num_redist; i++) { |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 137 | u32 offset = i * GIC_REDISTRIBUTOR_OFFSET; |
| 138 | |
| 139 | val = ((redist_lpi_base + (i * LPI_PENDBASE_SZ)) | |
| 140 | GICR_PENDBASER_INNERSHAREABLE | |
Hou Zhiqiang | 295b42d | 2021-03-05 15:02:35 +0800 | [diff] [blame] | 141 | GICR_PENDBASER_RAWAWB | |
| 142 | GICR_PENDBASER_PTZ); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 143 | |
| 144 | writeq(val, (uintptr_t)(pend_base + offset)); |
| 145 | tmp = readq((uintptr_t)(pend_base + offset)); |
| 146 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { |
| 147 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | |
| 148 | GICR_PENDBASER_CACHEABILITY_MASK); |
| 149 | val |= GICR_PENDBASER_NC; |
| 150 | writeq(val, (uintptr_t)(pend_base + offset)); |
| 151 | } |
| 152 | |
| 153 | /* Enable LPI for the redistributor */ |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 154 | writel(GICR_CTLR_ENABLE_LPIS, |
| 155 | (uintptr_t)(priv.gicr_base + offset)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 161 | static const struct udevice_id gic_v3_its_ids[] = { |
| 162 | { .compatible = "arm,gic-v3" }, |
| 163 | {} |
| 164 | }; |
| 165 | |
| 166 | U_BOOT_DRIVER(arm_gic_v3_its) = { |
| 167 | .name = "gic-v3", |
| 168 | .id = UCLASS_IRQ, |
| 169 | .of_match = gic_v3_its_ids, |
| 170 | }; |