2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/pirq_routing.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static struct irq_router irq_router;
19 static struct irq_routing_table *pirq_routing_table;
21 bool pirq_check_irq_routed(int link, u8 irq)
24 int base = irq_router.link_base;
26 if (irq_router.config == PIRQ_VIA_PCI)
27 pirq = x86_pci_read_config8(irq_router.bdf,
28 LINK_N2V(link, base));
30 pirq = readb(irq_router.ibase + LINK_N2V(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(int link)
43 return LINK_V2N(link, irq_router.link_base);
46 void pirq_assign_irq(int link, u8 irq)
48 int base = irq_router.link_base;
50 /* IRQ# 0/1/2/8/13 are reserved */
51 if (irq < 3 || irq == 8 || irq == 13)
54 if (irq_router.config == PIRQ_VIA_PCI)
55 x86_pci_write_config8(irq_router.bdf,
56 LINK_N2V(link, base), irq);
58 writeb(irq, irq_router.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_info *slot, int bus, int device,
80 slot->devfn = (device << 3) | 0;
81 slot->irq[pin - 1].link = LINK_N2V(pirq, irq_router.link_base);
82 slot->irq[pin - 1].bitmap = irq_router.irq_mask;
85 __weak void cpu_irq_init(void)
90 static int create_pirq_routing_table(void)
92 const void *blob = gd->fdt_blob;
93 struct fdt_pci_addr addr;
97 struct irq_routing_table *rt;
98 struct irq_info *slot, *slot_base;
103 node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_IRQ_ROUTER);
105 debug("%s: Cannot find irq router node\n", __func__);
109 ret = fdtdec_get_pci_addr(blob, node, FDT_PCI_SPACE_CONFIG,
114 /* extract the bdf from fdt_pci_addr */
115 irq_router.bdf = addr.phys_hi & 0xffff00;
117 ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
119 irq_router.config = PIRQ_VIA_PCI;
121 ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
123 irq_router.config = PIRQ_VIA_IBASE;
128 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
131 irq_router.link_base = ret;
133 irq_router.irq_mask = fdtdec_get_int(blob, node,
134 "intel,pirq-mask", PIRQ_BITMAP);
136 if (irq_router.config == PIRQ_VIA_IBASE) {
139 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
144 * Here we assume that the IBASE register has already been
145 * properly configured by U-Boot before.
147 * By 'valid' we mean:
148 * 1) a valid memory space carved within system memory space
149 * assigned to IBASE register block.
150 * 2) memory range decoding is enabled.
151 * Hence we don't do any santify test here.
153 irq_router.ibase = x86_pci_read_config32(irq_router.bdf,
155 irq_router.ibase &= ~0xf;
158 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
159 if (!cell || len % sizeof(struct pirq_routing))
161 count = len / sizeof(struct pirq_routing);
163 rt = calloc(1, sizeof(struct irq_routing_table));
167 /* Populate the PIRQ table fields */
168 rt->signature = PIRQ_SIGNATURE;
169 rt->version = PIRQ_VERSION;
170 rt->rtr_bus = PCI_BUS(irq_router.bdf);
171 rt->rtr_devfn = (PCI_DEV(irq_router.bdf) << 3) |
172 PCI_FUNC(irq_router.bdf);
173 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
174 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
176 slot_base = rt->slots;
178 /* Now fill in the irq_info entries in the PIRQ table */
179 for (i = 0; i < count;
180 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
181 struct pirq_routing pr;
183 pr.bdf = fdt_addr_to_cpu(cell[0]);
184 pr.pin = fdt_addr_to_cpu(cell[1]);
185 pr.pirq = fdt_addr_to_cpu(cell[2]);
187 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
188 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
189 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
192 slot = check_dup_entry(slot_base, irq_entries,
193 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
195 debug("found entry for bus %d device %d, ",
196 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
198 if (slot->irq[pr.pin - 1].link) {
202 * Sanity test on the routed PIRQ pin
204 * If they don't match, show a warning to tell
205 * there might be something wrong with the PIRQ
206 * routing information in the device tree.
208 if (slot->irq[pr.pin - 1].link !=
209 LINK_N2V(pr.pirq, irq_router.link_base))
210 debug("WARNING: Inconsistent PIRQ routing information\n");
214 slot = slot_base + irq_entries++;
216 debug("writing INT%c\n", 'A' + pr.pin - 1);
217 fill_irq_info(slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf), pr.pin,
221 rt->size = irq_entries * sizeof(struct irq_info) + 32;
223 pirq_routing_table = rt;
234 ret = create_pirq_routing_table();
236 debug("Failed to create pirq routing table\n");
240 pirq_route_irqs(pirq_routing_table->slots,
241 get_irq_slot_count(pirq_routing_table));
246 u32 write_pirq_routing_table(u32 addr)
248 if (!pirq_routing_table)
251 return copy_pirq_routing_table(addr, pirq_routing_table);