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,
27 pirq_linkno_to_reg(link, base), &pirq);
29 pirq = readb((uintptr_t)priv->ibase +
30 pirq_linkno_to_reg(link, base));
34 /* IRQ# 0/1/2/8/13 are reserved */
35 if (pirq < 3 || pirq == 8 || pirq == 13)
38 return pirq == irq ? true : false;
41 int pirq_translate_link(struct udevice *dev, int link)
43 struct irq_router *priv = dev_get_priv(dev);
45 return pirq_reg_to_linkno(link, priv->link_base);
48 void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
50 struct irq_router *priv = dev_get_priv(dev);
51 int base = priv->link_base;
53 /* IRQ# 0/1/2/8/13 are reserved */
54 if (irq < 3 || irq == 8 || irq == 13)
57 if (priv->config == PIRQ_VIA_PCI)
58 dm_pci_write_config8(dev->parent,
59 pirq_linkno_to_reg(link, base), irq);
61 writeb(irq, (uintptr_t)priv->ibase +
62 pirq_linkno_to_reg(link, base));
65 static struct irq_info *check_dup_entry(struct irq_info *slot_base,
66 int entry_num, int bus, int device)
68 struct irq_info *slot = slot_base;
71 for (i = 0; i < entry_num; i++) {
72 if (slot->bus == bus && slot->devfn == (device << 3))
77 return (i == entry_num) ? NULL : slot;
80 static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
81 int bus, int device, int pin, int pirq)
84 slot->devfn = (device << 3) | 0;
85 slot->irq[pin - 1].link = pirq_linkno_to_reg(pirq, priv->link_base);
86 slot->irq[pin - 1].bitmap = priv->irq_mask;
89 static int create_pirq_routing_table(struct udevice *dev)
91 struct irq_router *priv = dev_get_priv(dev);
92 const void *blob = gd->fdt_blob;
96 struct irq_routing_table *rt;
97 struct irq_info *slot, *slot_base;
102 node = dev_of_offset(dev);
104 /* extract the bdf from fdt_pci_addr */
105 priv->bdf = dm_pci_get_bdf(dev->parent);
107 ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
109 priv->config = PIRQ_VIA_PCI;
111 ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
114 priv->config = PIRQ_VIA_IBASE;
119 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
122 priv->link_base = ret;
124 priv->irq_mask = fdtdec_get_int(blob, node,
125 "intel,pirq-mask", PIRQ_BITMAP);
127 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
128 /* Reserve IRQ9 for SCI */
129 priv->irq_mask &= ~(1 << 9);
132 if (priv->config == PIRQ_VIA_IBASE) {
135 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
140 * Here we assume that the IBASE register has already been
141 * properly configured by U-Boot before.
143 * By 'valid' we mean:
144 * 1) a valid memory space carved within system memory space
145 * assigned to IBASE register block.
146 * 2) memory range decoding is enabled.
147 * Hence we don't do any santify test here.
149 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
153 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
154 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
156 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
157 if (!cell || len % sizeof(struct pirq_routing))
159 count = len / sizeof(struct pirq_routing);
161 rt = calloc(1, sizeof(struct irq_routing_table));
165 /* Populate the PIRQ table fields */
166 rt->signature = PIRQ_SIGNATURE;
167 rt->version = PIRQ_VERSION;
168 rt->rtr_bus = PCI_BUS(priv->bdf);
169 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
170 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
171 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
173 slot_base = rt->slots;
175 /* Now fill in the irq_info entries in the PIRQ table */
176 for (i = 0; i < count;
177 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
178 struct pirq_routing pr;
180 pr.bdf = fdt_addr_to_cpu(cell[0]);
181 pr.pin = fdt_addr_to_cpu(cell[1]);
182 pr.pirq = fdt_addr_to_cpu(cell[2]);
184 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
185 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
186 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
189 slot = check_dup_entry(slot_base, irq_entries,
190 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
192 debug("found entry for bus %d device %d, ",
193 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
195 if (slot->irq[pr.pin - 1].link) {
199 * Sanity test on the routed PIRQ pin
201 * If they don't match, show a warning to tell
202 * there might be something wrong with the PIRQ
203 * routing information in the device tree.
205 if (slot->irq[pr.pin - 1].link !=
206 pirq_linkno_to_reg(pr.pirq, priv->link_base))
207 debug("WARNING: Inconsistent PIRQ routing information\n");
211 slot = slot_base + irq_entries++;
213 debug("writing INT%c\n", 'A' + pr.pin - 1);
214 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
218 rt->size = irq_entries * sizeof(struct irq_info) + 32;
220 /* Fix up the table checksum */
221 rt->checksum = table_compute_checksum(rt, rt->size);
223 gd->arch.pirq_routing_table = rt;
228 static void irq_enable_sci(struct udevice *dev)
230 struct irq_router *priv = dev_get_priv(dev);
232 if (priv->actl_8bit) {
233 /* Bit7 must be turned on to enable ACPI */
234 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
236 /* Write 0 to enable SCI on IRQ9 */
237 if (priv->config == PIRQ_VIA_PCI)
238 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
240 writel(0, (uintptr_t)priv->ibase + priv->actl_addr);
244 int irq_router_probe(struct udevice *dev)
248 ret = create_pirq_routing_table(dev);
250 debug("Failed to create pirq routing table\n");
254 pirq_route_irqs(dev, gd->arch.pirq_routing_table->slots,
255 get_irq_slot_count(gd->arch.pirq_routing_table));
257 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
263 ulong write_pirq_routing_table(ulong addr)
265 if (!gd->arch.pirq_routing_table)
268 return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table);
271 static const struct udevice_id irq_router_ids[] = {
272 { .compatible = "intel,irq-router" },
276 U_BOOT_DRIVER(irq_router_drv) = {
279 .of_match = irq_router_ids,
280 .probe = irq_router_probe,
281 .priv_auto_alloc_size = sizeof(struct irq_router),
284 UCLASS_DRIVER(irq) = {