2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/pirq_routing.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static struct irq_router irq_router;
20 static struct irq_routing_table *pirq_routing_table;
22 bool pirq_check_irq_routed(int link, u8 irq)
25 int base = irq_router.link_base;
27 if (irq_router.config == PIRQ_VIA_PCI)
28 pirq = x86_pci_read_config8(irq_router.bdf,
29 LINK_N2V(link, base));
31 pirq = readb(irq_router.ibase + LINK_N2V(link, base));
35 /* IRQ# 0/1/2/8/13 are reserved */
36 if (pirq < 3 || pirq == 8 || pirq == 13)
39 return pirq == irq ? true : false;
42 int pirq_translate_link(int link)
44 return LINK_V2N(link, irq_router.link_base);
47 void pirq_assign_irq(int link, u8 irq)
49 int base = irq_router.link_base;
51 /* IRQ# 0/1/2/8/13 are reserved */
52 if (irq < 3 || irq == 8 || irq == 13)
55 if (irq_router.config == PIRQ_VIA_PCI)
56 x86_pci_write_config8(irq_router.bdf,
57 LINK_N2V(link, base), irq);
59 writeb(irq, irq_router.ibase + LINK_N2V(link, base));
62 static struct irq_info *check_dup_entry(struct irq_info *slot_base,
63 int entry_num, int bus, int device)
65 struct irq_info *slot = slot_base;
68 for (i = 0; i < entry_num; i++) {
69 if (slot->bus == bus && slot->devfn == (device << 3))
74 return (i == entry_num) ? NULL : slot;
77 static inline void fill_irq_info(struct irq_info *slot, int bus, int device,
81 slot->devfn = (device << 3) | 0;
82 slot->irq[pin - 1].link = LINK_N2V(pirq, irq_router.link_base);
83 slot->irq[pin - 1].bitmap = irq_router.irq_mask;
86 __weak void cpu_irq_init(void)
91 static int create_pirq_routing_table(struct udevice *dev)
93 const void *blob = gd->fdt_blob;
94 struct fdt_pci_addr addr;
98 struct irq_routing_table *rt;
99 struct irq_info *slot, *slot_base;
105 node = dev->of_offset;
106 parent = dev->parent->of_offset;
107 ret = fdtdec_get_pci_addr(blob, parent, FDT_PCI_SPACE_CONFIG,
112 /* extract the bdf from fdt_pci_addr */
113 irq_router.bdf = addr.phys_hi & 0xffff00;
115 ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
117 irq_router.config = PIRQ_VIA_PCI;
119 ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
121 irq_router.config = PIRQ_VIA_IBASE;
126 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
129 irq_router.link_base = ret;
131 irq_router.irq_mask = fdtdec_get_int(blob, node,
132 "intel,pirq-mask", PIRQ_BITMAP);
134 if (irq_router.config == PIRQ_VIA_IBASE) {
137 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
142 * Here we assume that the IBASE register has already been
143 * properly configured by U-Boot before.
145 * By 'valid' we mean:
146 * 1) a valid memory space carved within system memory space
147 * assigned to IBASE register block.
148 * 2) memory range decoding is enabled.
149 * Hence we don't do any santify test here.
151 irq_router.ibase = x86_pci_read_config32(irq_router.bdf,
153 irq_router.ibase &= ~0xf;
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(irq_router.bdf);
169 rt->rtr_devfn = (PCI_DEV(irq_router.bdf) << 3) |
170 PCI_FUNC(irq_router.bdf);
171 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
172 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
174 slot_base = rt->slots;
176 /* Now fill in the irq_info entries in the PIRQ table */
177 for (i = 0; i < count;
178 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
179 struct pirq_routing pr;
181 pr.bdf = fdt_addr_to_cpu(cell[0]);
182 pr.pin = fdt_addr_to_cpu(cell[1]);
183 pr.pirq = fdt_addr_to_cpu(cell[2]);
185 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
186 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
187 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
190 slot = check_dup_entry(slot_base, irq_entries,
191 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
193 debug("found entry for bus %d device %d, ",
194 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
196 if (slot->irq[pr.pin - 1].link) {
200 * Sanity test on the routed PIRQ pin
202 * If they don't match, show a warning to tell
203 * there might be something wrong with the PIRQ
204 * routing information in the device tree.
206 if (slot->irq[pr.pin - 1].link !=
207 LINK_N2V(pr.pirq, irq_router.link_base))
208 debug("WARNING: Inconsistent PIRQ routing information\n");
212 slot = slot_base + irq_entries++;
214 debug("writing INT%c\n", 'A' + pr.pin - 1);
215 fill_irq_info(slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf), pr.pin,
219 rt->size = irq_entries * sizeof(struct irq_info) + 32;
221 pirq_routing_table = rt;
226 int irq_router_common_init(struct udevice *dev)
232 ret = create_pirq_routing_table(dev);
234 debug("Failed to create pirq routing table\n");
238 pirq_route_irqs(pirq_routing_table->slots,
239 get_irq_slot_count(pirq_routing_table));
244 int irq_router_probe(struct udevice *dev)
246 return irq_router_common_init(dev);
249 u32 write_pirq_routing_table(u32 addr)
251 if (!pirq_routing_table)
254 return copy_pirq_routing_table(addr, pirq_routing_table);
257 static const struct udevice_id irq_router_ids[] = {
258 { .compatible = "intel,irq-router" },
262 U_BOOT_DRIVER(irq_router_drv) = {
265 .of_match = irq_router_ids,
266 .probe = irq_router_probe,
269 UCLASS_DRIVER(irq) = {