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 inline void fill_irq_info(struct irq_info **slotp, int *entries, u8 bus,
62 u8 device, u8 func, u8 pin, u8 pirq)
64 struct irq_info *slot = *slotp;
67 slot->devfn = (device << 3) | func;
68 slot->irq[pin - 1].link = LINK_N2V(pirq, irq_router.link_base);
69 slot->irq[pin - 1].bitmap = irq_router.irq_mask;
74 __weak void cpu_irq_init(void)
79 static int create_pirq_routing_table(void)
81 const void *blob = gd->fdt_blob;
82 struct fdt_pci_addr addr;
86 struct irq_routing_table *rt;
87 struct irq_info *slot;
92 node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_IRQ_ROUTER);
94 debug("%s: Cannot find irq router node\n", __func__);
98 ret = fdtdec_get_pci_addr(blob, node, FDT_PCI_SPACE_CONFIG,
103 /* extract the bdf from fdt_pci_addr */
104 irq_router.bdf = addr.phys_hi & 0xffff00;
106 ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
108 irq_router.config = PIRQ_VIA_PCI;
110 ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
112 irq_router.config = PIRQ_VIA_IBASE;
117 ret = fdtdec_get_int_array(blob, node, "intel,pirq-link",
118 &irq_router.link_base, 1);
122 irq_router.irq_mask = fdtdec_get_int(blob, node,
123 "intel,pirq-mask", PIRQ_BITMAP);
125 if (irq_router.config == PIRQ_VIA_IBASE) {
128 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
133 * Here we assume that the IBASE register has already been
134 * properly configured by U-Boot before.
136 * By 'valid' we mean:
137 * 1) a valid memory space carved within system memory space
138 * assigned to IBASE register block.
139 * 2) memory range decoding is enabled.
140 * Hence we don't do any santify test here.
142 irq_router.ibase = x86_pci_read_config32(irq_router.bdf,
144 irq_router.ibase &= ~0xf;
147 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
151 if ((len % sizeof(struct pirq_routing)) == 0)
152 count = len / sizeof(struct pirq_routing);
156 rt = malloc(sizeof(struct irq_routing_table));
159 memset((char *)rt, 0, 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(irq_router.bdf);
165 rt->rtr_devfn = (PCI_DEV(irq_router.bdf) << 3) |
166 PCI_FUNC(irq_router.bdf);
167 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
168 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
172 /* Now fill in the irq_info entries in the PIRQ table */
173 for (i = 0; i < count; i++) {
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,
184 fill_irq_info(&slot, &irq_entries, PCI_BUS(pr.bdf),
185 PCI_DEV(pr.bdf), PCI_FUNC(pr.bdf),
187 cell += sizeof(struct pirq_routing) / sizeof(u32);
190 rt->size = irq_entries * sizeof(struct irq_info) + 32;
192 pirq_routing_table = rt;
201 if (create_pirq_routing_table()) {
202 debug("Failed to create pirq routing table\n");
205 pirq_route_irqs(pirq_routing_table->slots,
206 get_irq_slot_count(pirq_routing_table));
210 u32 write_pirq_routing_table(u32 addr)
212 if (!pirq_routing_table)
215 return copy_pirq_routing_table(addr, pirq_routing_table);