1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2019 Broadcom.
9 #include <asm/gic-v3.h>
11 #include <linux/bitops.h>
12 #include <linux/sizes.h>
14 static u32 lpi_id_bits;
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)
21 * gic_v3_its_priv - gic details
23 * @gicd_base: gicd base address
24 * @gicr_base: gicr base address
26 struct gic_v3_its_priv {
31 static int gic_v3_its_get_gic_addr(struct gic_v3_its_priv *priv)
37 ret = uclass_get_device_by_driver(UCLASS_IRQ,
38 DM_DRIVER_GET(arm_gic_v3_its), &dev);
40 pr_err("%s: failed to get %s irq device\n", __func__,
41 DM_DRIVER_GET(arm_gic_v3_its)->name);
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__);
50 priv->gicd_base = addr;
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__);
57 priv->gicr_base = addr;
63 * Program the GIC LPI configuration tables for all
64 * the re-distributors and enable the LPI table
65 * base: Configuration table address
66 * num_redist: number of redistributors
68 int gic_lpi_tables_init(u64 base, u32 num_redist)
70 struct gic_v3_its_priv priv;
77 ulong pend_tab_total_sz = num_redist * LPI_PENDBASE_SZ;
80 if (gic_v3_its_get_gic_addr(&priv))
83 gicd_typer = readl((uintptr_t)(priv.gicd_base + GICD_TYPER));
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");
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.
95 for (i = 0; i < num_redist; i++) {
96 u32 offset = i * GIC_REDISTRIBUTOR_OFFSET;
98 if ((readl((uintptr_t)(priv.gicr_base + offset))) &
99 GICR_CTLR_ENABLE_LPIS) {
100 pr_err("Re-Distributor %d LPI is already enabled\n",
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),
112 GICR_PROPBASER_INNERSHAREABLE |
113 GICR_PROPBASER_RAWAWB |
114 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
116 writeq(val, (uintptr_t)(priv.gicr_base + GICR_PROPBASER));
117 tmp = readl((uintptr_t)(priv.gicr_base + GICR_PROPBASER));
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;
124 (uintptr_t)(priv.gicr_base + GICR_PROPBASER));
128 redist_lpi_base = base + LPI_PROPBASE_SZ;
129 pend_tab_va = map_physmem(redist_lpi_base, pend_tab_total_sz,
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);
135 pend_base = priv.gicr_base + GICR_PENDBASER;
136 for (i = 0; i < num_redist; i++) {
137 u32 offset = i * GIC_REDISTRIBUTOR_OFFSET;
139 val = ((redist_lpi_base + (i * LPI_PENDBASE_SZ)) |
140 GICR_PENDBASER_INNERSHAREABLE |
141 GICR_PENDBASER_RAWAWB |
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));
153 /* Enable LPI for the redistributor */
154 writel(GICR_CTLR_ENABLE_LPIS,
155 (uintptr_t)(priv.gicr_base + offset));
161 static const struct udevice_id gic_v3_its_ids[] = {
162 { .compatible = "arm,gic-v3" },
166 U_BOOT_DRIVER(arm_gic_v3_its) = {
169 .of_match = gic_v3_its_ids,