blob: f6211a2d92c52d344afaf4d4a4982e28fcc1e370 [file] [log] [blame]
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 Broadcom.
4 */
5#include <common.h>
Hou Zhiqiang295b42d2021-03-05 15:02:35 +08006#include <cpu_func.h>
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +05307#include <dm.h>
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -08008#include <asm/gic.h>
9#include <asm/gic-v3.h>
10#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Wasim Khan1fc54802020-02-14 11:00:52 +053012#include <linux/sizes.h>
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080013
14static 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 Kokatanurb417ca12020-07-26 22:37:32 +053021 * gic_v3_its_priv - gic details
22 *
23 * @gicd_base: gicd base address
24 * @gicr_base: gicr base address
25 */
26struct gic_v3_its_priv {
27 ulong gicd_base;
28 ulong gicr_base;
29};
30
31static 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 Glass65130cd2020-12-28 20:34:56 -070038 DM_DRIVER_GET(arm_gic_v3_its), &dev);
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053039 if (ret) {
40 pr_err("%s: failed to get %s irq device\n", __func__,
Simon Glass65130cd2020-12-28 20:34:56 -070041 DM_DRIVER_GET(arm_gic_v3_its)->name);
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053042 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 Gooty436efc02019-12-16 09:09:43 -080063 * Program the GIC LPI configuration tables for all
64 * the re-distributors and enable the LPI table
Michael Walle29b1d332021-10-27 18:54:54 +020065 * base: Configuration table address
66 * num_redist: number of redistributors
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080067 */
Michael Walle29b1d332021-10-27 18:54:54 +020068int gic_lpi_tables_init(u64 base, u32 num_redist)
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080069{
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053070 struct gic_v3_its_priv priv;
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080071 u32 gicd_typer;
72 u64 val;
73 u64 tmp;
74 int i;
75 u64 redist_lpi_base;
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053076 u64 pend_base;
Michael Walle29b1d332021-10-27 18:54:54 +020077 ulong pend_tab_total_sz = num_redist * LPI_PENDBASE_SZ;
Hou Zhiqiang295b42d2021-03-05 15:02:35 +080078 void *pend_tab_va;
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080079
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053080 if (gic_v3_its_get_gic_addr(&priv))
81 return -EINVAL;
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080082
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053083 gicd_typer = readl((uintptr_t)(priv.gicd_base + GICD_TYPER));
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080084 /* 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 Walle29b1d332021-10-27 18:54:54 +020095 for (i = 0; i < num_redist; i++) {
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080096 u32 offset = i * GIC_REDISTRIBUTOR_OFFSET;
97
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +053098 if ((readl((uintptr_t)(priv.gicr_base + offset))) &
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -080099 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 Walle29b1d332021-10-27 18:54:54 +0200111 val = (base |
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800112 GICR_PROPBASER_INNERSHAREABLE |
113 GICR_PROPBASER_RAWAWB |
114 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
115
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530116 writeq(val, (uintptr_t)(priv.gicr_base + GICR_PROPBASER));
117 tmp = readl((uintptr_t)(priv.gicr_base + GICR_PROPBASER));
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800118 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 Kokatanurb417ca12020-07-26 22:37:32 +0530123 writeq(val,
124 (uintptr_t)(priv.gicr_base + GICR_PROPBASER));
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800125 }
126 }
127
Michael Walle29b1d332021-10-27 18:54:54 +0200128 redist_lpi_base = base + LPI_PROPBASE_SZ;
Hou Zhiqiang295b42d2021-03-05 15:02:35 +0800129 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 Gooty436efc02019-12-16 09:09:43 -0800134
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530135 pend_base = priv.gicr_base + GICR_PENDBASER;
Michael Walle29b1d332021-10-27 18:54:54 +0200136 for (i = 0; i < num_redist; i++) {
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800137 u32 offset = i * GIC_REDISTRIBUTOR_OFFSET;
138
139 val = ((redist_lpi_base + (i * LPI_PENDBASE_SZ)) |
140 GICR_PENDBASER_INNERSHAREABLE |
Hou Zhiqiang295b42d2021-03-05 15:02:35 +0800141 GICR_PENDBASER_RAWAWB |
142 GICR_PENDBASER_PTZ);
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800143
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 Kokatanurb417ca12020-07-26 22:37:32 +0530154 writel(GICR_CTLR_ENABLE_LPIS,
155 (uintptr_t)(priv.gicr_base + offset));
Bharat Kumar Reddy Gooty436efc02019-12-16 09:09:43 -0800156 }
157
158 return 0;
159}
160
Rayagonda Kokatanurb417ca12020-07-26 22:37:32 +0530161static const struct udevice_id gic_v3_its_ids[] = {
162 { .compatible = "arm,gic-v3" },
163 {}
164};
165
166U_BOOT_DRIVER(arm_gic_v3_its) = {
167 .name = "gic-v3",
168 .id = UCLASS_IRQ,
169 .of_match = gic_v3_its_ids,
170};