1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
14 #include <asm/pirq_routing.h>
15 #include <asm/tables.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
21 struct irq_router *priv = dev_get_priv(dev);
23 int base = priv->link_base;
25 if (priv->config == PIRQ_VIA_PCI)
26 dm_pci_read_config8(dev->parent, LINK_N2V(link, base), &pirq);
28 pirq = readb((uintptr_t)priv->ibase + LINK_N2V(link, base));
32 /* IRQ# 0/1/2/8/13 are reserved */
33 if (pirq < 3 || pirq == 8 || pirq == 13)
36 return pirq == irq ? true : false;
39 int pirq_translate_link(struct udevice *dev, int link)
41 struct irq_router *priv = dev_get_priv(dev);
43 return LINK_V2N(link, priv->link_base);
46 void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
48 struct irq_router *priv = dev_get_priv(dev);
49 int base = priv->link_base;
51 /* IRQ# 0/1/2/8/13 are reserved */
52 if (irq < 3 || irq == 8 || irq == 13)
55 if (priv->config == PIRQ_VIA_PCI)
56 dm_pci_write_config8(dev->parent, LINK_N2V(link, base), irq);
58 writeb(irq, (uintptr_t)priv->ibase + LINK_N2V(link, base));
61 static struct irq_info *check_dup_entry(struct irq_info *slot_base,
62 int entry_num, int bus, int device)
64 struct irq_info *slot = slot_base;
67 for (i = 0; i < entry_num; i++) {
68 if (slot->bus == bus && slot->devfn == (device << 3))
73 return (i == entry_num) ? NULL : slot;
76 static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
77 int bus, int device, int pin, int pirq)
80 slot->devfn = (device << 3) | 0;
81 slot->irq[pin - 1].link = LINK_N2V(pirq, priv->link_base);
82 slot->irq[pin - 1].bitmap = priv->irq_mask;
85 static int create_pirq_routing_table(struct udevice *dev)
87 struct irq_router *priv = dev_get_priv(dev);
88 const void *blob = gd->fdt_blob;
92 struct irq_routing_table *rt;
93 struct irq_info *slot, *slot_base;
98 node = dev_of_offset(dev);
100 /* extract the bdf from fdt_pci_addr */
101 priv->bdf = dm_pci_get_bdf(dev->parent);
103 ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
105 priv->config = PIRQ_VIA_PCI;
107 ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
110 priv->config = PIRQ_VIA_IBASE;
115 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
118 priv->link_base = ret;
120 priv->irq_mask = fdtdec_get_int(blob, node,
121 "intel,pirq-mask", PIRQ_BITMAP);
123 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
124 /* Reserve IRQ9 for SCI */
125 priv->irq_mask &= ~(1 << 9);
128 if (priv->config == PIRQ_VIA_IBASE) {
131 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
136 * Here we assume that the IBASE register has already been
137 * properly configured by U-Boot before.
139 * By 'valid' we mean:
140 * 1) a valid memory space carved within system memory space
141 * assigned to IBASE register block.
142 * 2) memory range decoding is enabled.
143 * Hence we don't do any santify test here.
145 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
149 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
150 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
152 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
153 if (!cell || len % sizeof(struct pirq_routing))
155 count = len / sizeof(struct pirq_routing);
157 rt = calloc(1, sizeof(struct irq_routing_table));
161 /* Populate the PIRQ table fields */
162 rt->signature = PIRQ_SIGNATURE;
163 rt->version = PIRQ_VERSION;
164 rt->rtr_bus = PCI_BUS(priv->bdf);
165 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
166 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
167 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
169 slot_base = rt->slots;
171 /* Now fill in the irq_info entries in the PIRQ table */
172 for (i = 0; i < count;
173 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
174 struct pirq_routing pr;
176 pr.bdf = fdt_addr_to_cpu(cell[0]);
177 pr.pin = fdt_addr_to_cpu(cell[1]);
178 pr.pirq = fdt_addr_to_cpu(cell[2]);
180 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
181 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
182 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
185 slot = check_dup_entry(slot_base, irq_entries,
186 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
188 debug("found entry for bus %d device %d, ",
189 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
191 if (slot->irq[pr.pin - 1].link) {
195 * Sanity test on the routed PIRQ pin
197 * If they don't match, show a warning to tell
198 * there might be something wrong with the PIRQ
199 * routing information in the device tree.
201 if (slot->irq[pr.pin - 1].link !=
202 LINK_N2V(pr.pirq, priv->link_base))
203 debug("WARNING: Inconsistent PIRQ routing information\n");
207 slot = slot_base + irq_entries++;
209 debug("writing INT%c\n", 'A' + pr.pin - 1);
210 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
214 rt->size = irq_entries * sizeof(struct irq_info) + 32;
216 /* Fix up the table checksum */
217 rt->checksum = table_compute_checksum(rt, rt->size);
219 gd->arch.pirq_routing_table = rt;
224 static void irq_enable_sci(struct udevice *dev)
226 struct irq_router *priv = dev_get_priv(dev);
228 if (priv->actl_8bit) {
229 /* Bit7 must be turned on to enable ACPI */
230 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
232 /* Write 0 to enable SCI on IRQ9 */
233 if (priv->config == PIRQ_VIA_PCI)
234 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
236 writel(0, (uintptr_t)priv->ibase + priv->actl_addr);
240 int irq_router_probe(struct udevice *dev)
244 ret = create_pirq_routing_table(dev);
246 debug("Failed to create pirq routing table\n");
250 pirq_route_irqs(dev, gd->arch.pirq_routing_table->slots,
251 get_irq_slot_count(gd->arch.pirq_routing_table));
253 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
259 ulong write_pirq_routing_table(ulong addr)
261 if (!gd->arch.pirq_routing_table)
264 return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table);
267 static const struct udevice_id irq_router_ids[] = {
268 { .compatible = "intel,irq-router" },
272 U_BOOT_DRIVER(irq_router_drv) = {
275 .of_match = irq_router_ids,
276 .probe = irq_router_probe,
277 .priv_auto_alloc_size = sizeof(struct irq_router),
280 UCLASS_DRIVER(irq) = {