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 | */ |
| 5 | #include <common.h> |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 6 | #include <dm.h> |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 7 | #include <regmap.h> |
| 8 | #include <syscon.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> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Wasim Khan | 1fc5480 | 2020-02-14 11:00:52 +0530 | [diff] [blame] | 13 | #include <linux/sizes.h> |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 14 | |
| 15 | static u32 lpi_id_bits; |
| 16 | |
| 17 | #define LPI_NRBITS lpi_id_bits |
| 18 | #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K) |
| 19 | #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K) |
| 20 | |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 21 | /* Number of GIC re-distributors */ |
| 22 | #define MAX_GIC_REDISTRIBUTORS 8 |
| 23 | |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 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 |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 29 | * @lpi_base: gic lpi base address |
| 30 | * @num_redist: number of gic re-distributors |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 31 | */ |
| 32 | struct gic_v3_its_priv { |
| 33 | ulong gicd_base; |
| 34 | ulong gicr_base; |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 35 | ulong lpi_base; |
| 36 | u32 num_redist; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static int gic_v3_its_get_gic_addr(struct gic_v3_its_priv *priv) |
| 40 | { |
| 41 | struct udevice *dev; |
| 42 | fdt_addr_t addr; |
| 43 | int ret; |
| 44 | |
| 45 | ret = uclass_get_device_by_driver(UCLASS_IRQ, |
| 46 | DM_GET_DRIVER(arm_gic_v3_its), &dev); |
| 47 | if (ret) { |
| 48 | pr_err("%s: failed to get %s irq device\n", __func__, |
| 49 | DM_GET_DRIVER(arm_gic_v3_its)->name); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | addr = dev_read_addr_index(dev, 0); |
| 54 | if (addr == FDT_ADDR_T_NONE) { |
| 55 | pr_err("%s: failed to get GICD address\n", __func__); |
| 56 | return -EINVAL; |
| 57 | } |
| 58 | priv->gicd_base = addr; |
| 59 | |
| 60 | addr = dev_read_addr_index(dev, 1); |
| 61 | if (addr == FDT_ADDR_T_NONE) { |
| 62 | pr_err("%s: failed to get GICR address\n", __func__); |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | priv->gicr_base = addr; |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 70 | static int gic_v3_its_get_gic_lpi_addr(struct gic_v3_its_priv *priv) |
| 71 | { |
| 72 | struct regmap *regmap; |
| 73 | struct udevice *dev; |
| 74 | int ret; |
| 75 | |
| 76 | ret = uclass_get_device_by_driver(UCLASS_SYSCON, |
| 77 | DM_GET_DRIVER(gic_lpi_syscon), &dev); |
| 78 | if (ret) { |
| 79 | pr_err("%s: failed to get %s syscon device\n", __func__, |
| 80 | DM_GET_DRIVER(gic_lpi_syscon)->name); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | regmap = syscon_get_regmap(dev); |
| 85 | if (!regmap) { |
| 86 | pr_err("%s: failed to regmap for %s syscon device\n", __func__, |
| 87 | DM_GET_DRIVER(gic_lpi_syscon)->name); |
| 88 | return -ENODEV; |
| 89 | } |
| 90 | priv->lpi_base = regmap->ranges[0].start; |
| 91 | |
| 92 | priv->num_redist = dev_read_u32_default(dev, "max-gic-redistributors", |
| 93 | MAX_GIC_REDISTRIBUTORS); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 98 | /* |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 99 | * Program the GIC LPI configuration tables for all |
| 100 | * the re-distributors and enable the LPI table |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 101 | */ |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 102 | int gic_lpi_tables_init(void) |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 103 | { |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 104 | struct gic_v3_its_priv priv; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 105 | u32 gicd_typer; |
| 106 | u64 val; |
| 107 | u64 tmp; |
| 108 | int i; |
| 109 | u64 redist_lpi_base; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 110 | u64 pend_base; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 111 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 112 | if (gic_v3_its_get_gic_addr(&priv)) |
| 113 | return -EINVAL; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 114 | |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 115 | if (gic_v3_its_get_gic_lpi_addr(&priv)) |
| 116 | return -EINVAL; |
| 117 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 118 | gicd_typer = readl((uintptr_t)(priv.gicd_base + GICD_TYPER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 119 | /* GIC support for Locality specific peripheral interrupts (LPI's) */ |
| 120 | if (!(gicd_typer & GICD_TYPER_LPIS)) { |
| 121 | pr_err("GIC implementation does not support LPI's\n"); |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Check for LPI is disabled for all the redistributors. |
| 127 | * Once the LPI table is enabled, can not program the |
| 128 | * LPI configuration tables again, unless the GIC is reset. |
| 129 | */ |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 130 | for (i = 0; i < priv.num_redist; i++) { |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 131 | u32 offset = i * GIC_REDISTRIBUTOR_OFFSET; |
| 132 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 133 | if ((readl((uintptr_t)(priv.gicr_base + offset))) & |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 134 | GICR_CTLR_ENABLE_LPIS) { |
| 135 | pr_err("Re-Distributor %d LPI is already enabled\n", |
| 136 | i); |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* lpi_id_bits to get LPI_PENDBASE_SZ and LPi_PROPBASE_SZ */ |
| 142 | lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gicd_typer), |
| 143 | ITS_MAX_LPI_NRBITS); |
| 144 | |
| 145 | /* Set PropBase */ |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 146 | val = (priv.lpi_base | |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 147 | GICR_PROPBASER_INNERSHAREABLE | |
| 148 | GICR_PROPBASER_RAWAWB | |
| 149 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); |
| 150 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 151 | writeq(val, (uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
| 152 | tmp = readl((uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 153 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { |
| 154 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
| 155 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | |
| 156 | GICR_PROPBASER_CACHEABILITY_MASK); |
| 157 | val |= GICR_PROPBASER_NC; |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 158 | writeq(val, |
| 159 | (uintptr_t)(priv.gicr_base + GICR_PROPBASER)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 163 | redist_lpi_base = priv.lpi_base + LPI_PROPBASE_SZ; |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 164 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 165 | pend_base = priv.gicr_base + GICR_PENDBASER; |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 166 | for (i = 0; i < priv.num_redist; i++) { |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 167 | u32 offset = i * GIC_REDISTRIBUTOR_OFFSET; |
| 168 | |
| 169 | val = ((redist_lpi_base + (i * LPI_PENDBASE_SZ)) | |
| 170 | GICR_PENDBASER_INNERSHAREABLE | |
| 171 | GICR_PENDBASER_RAWAWB); |
| 172 | |
| 173 | writeq(val, (uintptr_t)(pend_base + offset)); |
| 174 | tmp = readq((uintptr_t)(pend_base + offset)); |
| 175 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { |
| 176 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | |
| 177 | GICR_PENDBASER_CACHEABILITY_MASK); |
| 178 | val |= GICR_PENDBASER_NC; |
| 179 | writeq(val, (uintptr_t)(pend_base + offset)); |
| 180 | } |
| 181 | |
| 182 | /* Enable LPI for the redistributor */ |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 183 | writel(GICR_CTLR_ENABLE_LPIS, |
| 184 | (uintptr_t)(priv.gicr_base + offset)); |
Bharat Kumar Reddy Gooty | 436efc0 | 2019-12-16 09:09:43 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Rayagonda Kokatanur | b417ca1 | 2020-07-26 22:37:32 +0530 | [diff] [blame] | 190 | static const struct udevice_id gic_v3_its_ids[] = { |
| 191 | { .compatible = "arm,gic-v3" }, |
| 192 | {} |
| 193 | }; |
| 194 | |
| 195 | U_BOOT_DRIVER(arm_gic_v3_its) = { |
| 196 | .name = "gic-v3", |
| 197 | .id = UCLASS_IRQ, |
| 198 | .of_match = gic_v3_its_ids, |
| 199 | }; |
Rayagonda Kokatanur | 158c16e | 2020-07-26 22:37:33 +0530 | [diff] [blame] | 200 | |
| 201 | static const struct udevice_id gic_lpi_syscon_ids[] = { |
| 202 | { .compatible = "gic-lpi-base" }, |
| 203 | {} |
| 204 | }; |
| 205 | |
| 206 | U_BOOT_DRIVER(gic_lpi_syscon) = { |
| 207 | .name = "gic-lpi-base", |
| 208 | .id = UCLASS_SYSCON, |
| 209 | .of_match = gic_lpi_syscon_ids, |
| 210 | }; |