1 // SPDX-License-Identifier: GPL-2.0+
14 #include <asm/pirq_routing.h>
15 #include <asm/tables.h>
17 DECLARE_GLOBAL_DATA_PTR;
20 * pirq_reg_to_linkno() - Convert a PIRQ routing register offset to link number
22 * @priv: IRQ router driver's priv data
23 * @reg: PIRQ routing register offset from the base address
24 * @return: PIRQ link number (0 for PIRQA, 1 for PIRQB, etc)
26 static inline int pirq_reg_to_linkno(struct irq_router *priv, int reg)
30 if (priv->has_regmap) {
31 struct pirq_regmap *map = priv->regmap;
34 for (i = 0; i < priv->link_num; i++) {
35 if (reg - priv->link_base == map->offset) {
42 linkno = reg - priv->link_base;
49 * pirq_linkno_to_reg() - Convert a PIRQ link number to routing register offset
51 * @priv: IRQ router driver's priv data
52 * @linkno: PIRQ link number (0 for PIRQA, 1 for PIRQB, etc)
53 * @return: PIRQ routing register offset from the base address
55 static inline int pirq_linkno_to_reg(struct irq_router *priv, int linkno)
59 if (priv->has_regmap) {
60 struct pirq_regmap *map = priv->regmap;
63 for (i = 0; i < priv->link_num; i++) {
64 if (linkno == map->link) {
65 reg = map->offset + priv->link_base;
71 reg = linkno + priv->link_base;
77 bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
79 struct irq_router *priv = dev_get_priv(dev);
82 if (priv->config == PIRQ_VIA_PCI)
83 dm_pci_read_config8(dev->parent,
84 pirq_linkno_to_reg(priv, link), &pirq);
86 pirq = readb((uintptr_t)priv->ibase +
87 pirq_linkno_to_reg(priv, link));
91 /* IRQ# 0/1/2/8/13 are reserved */
92 if (pirq < 3 || pirq == 8 || pirq == 13)
95 return pirq == irq ? true : false;
98 int pirq_translate_link(struct udevice *dev, int link)
100 struct irq_router *priv = dev_get_priv(dev);
102 return pirq_reg_to_linkno(priv, link);
105 void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
107 struct irq_router *priv = dev_get_priv(dev);
109 /* IRQ# 0/1/2/8/13 are reserved */
110 if (irq < 3 || irq == 8 || irq == 13)
113 if (priv->config == PIRQ_VIA_PCI)
114 dm_pci_write_config8(dev->parent,
115 pirq_linkno_to_reg(priv, link), irq);
117 writeb(irq, (uintptr_t)priv->ibase +
118 pirq_linkno_to_reg(priv, link));
121 static struct irq_info *check_dup_entry(struct irq_info *slot_base,
122 int entry_num, int bus, int device)
124 struct irq_info *slot = slot_base;
127 for (i = 0; i < entry_num; i++) {
128 if (slot->bus == bus && slot->devfn == (device << 3))
133 return (i == entry_num) ? NULL : slot;
136 static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
137 int bus, int device, int pin, int pirq)
140 slot->devfn = (device << 3) | 0;
141 slot->irq[pin - 1].link = pirq_linkno_to_reg(priv, pirq);
142 slot->irq[pin - 1].bitmap = priv->irq_mask;
145 static int create_pirq_routing_table(struct udevice *dev)
147 struct irq_router *priv = dev_get_priv(dev);
148 const void *blob = gd->fdt_blob;
152 struct pirq_regmap *map;
153 struct irq_routing_table *rt;
154 struct irq_info *slot, *slot_base;
159 node = dev_of_offset(dev);
161 /* extract the bdf from fdt_pci_addr */
162 priv->bdf = dm_pci_get_bdf(dev->parent);
164 ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
166 priv->config = PIRQ_VIA_PCI;
168 ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
171 priv->config = PIRQ_VIA_IBASE;
176 cell = fdt_getprop(blob, node, "intel,pirq-link", &len);
177 if (!cell || len != 8)
179 priv->link_base = fdt_addr_to_cpu(cell[0]);
180 priv->link_num = fdt_addr_to_cpu(cell[1]);
181 if (priv->link_num > CONFIG_MAX_PIRQ_LINKS) {
182 debug("Limiting supported PIRQ link number from %d to %d\n",
183 priv->link_num, CONFIG_MAX_PIRQ_LINKS);
184 priv->link_num = CONFIG_MAX_PIRQ_LINKS;
187 cell = fdt_getprop(blob, node, "intel,pirq-regmap", &len);
189 if (len % sizeof(struct pirq_regmap))
192 count = len / sizeof(struct pirq_regmap);
193 if (count < priv->link_num) {
194 printf("Number of pirq-regmap entires is wrong\n");
198 count = priv->link_num;
199 priv->regmap = calloc(count, sizeof(struct pirq_regmap));
203 priv->has_regmap = true;
205 for (i = 0; i < count; i++) {
206 map->link = fdt_addr_to_cpu(cell[0]);
207 map->offset = fdt_addr_to_cpu(cell[1]);
209 cell += sizeof(struct pirq_regmap) / sizeof(u32);
214 priv->irq_mask = fdtdec_get_int(blob, node,
215 "intel,pirq-mask", PIRQ_BITMAP);
217 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
218 /* Reserve IRQ9 for SCI */
219 priv->irq_mask &= ~(1 << 9);
222 if (priv->config == PIRQ_VIA_IBASE) {
225 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
230 * Here we assume that the IBASE register has already been
231 * properly configured by U-Boot before.
233 * By 'valid' we mean:
234 * 1) a valid memory space carved within system memory space
235 * assigned to IBASE register block.
236 * 2) memory range decoding is enabled.
237 * Hence we don't do any santify test here.
239 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
243 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
244 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
246 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
247 if (!cell || len % sizeof(struct pirq_routing))
249 count = len / sizeof(struct pirq_routing);
251 rt = calloc(1, sizeof(struct irq_routing_table));
255 /* Populate the PIRQ table fields */
256 rt->signature = PIRQ_SIGNATURE;
257 rt->version = PIRQ_VERSION;
258 rt->rtr_bus = PCI_BUS(priv->bdf);
259 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
260 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
261 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
263 slot_base = rt->slots;
265 /* Now fill in the irq_info entries in the PIRQ table */
266 for (i = 0; i < count;
267 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
268 struct pirq_routing pr;
270 pr.bdf = fdt_addr_to_cpu(cell[0]);
271 pr.pin = fdt_addr_to_cpu(cell[1]);
272 pr.pirq = fdt_addr_to_cpu(cell[2]);
274 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
275 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
276 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
279 slot = check_dup_entry(slot_base, irq_entries,
280 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
282 debug("found entry for bus %d device %d, ",
283 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
285 if (slot->irq[pr.pin - 1].link) {
289 * Sanity test on the routed PIRQ pin
291 * If they don't match, show a warning to tell
292 * there might be something wrong with the PIRQ
293 * routing information in the device tree.
295 if (slot->irq[pr.pin - 1].link !=
296 pirq_linkno_to_reg(priv, pr.pirq))
297 debug("WARNING: Inconsistent PIRQ routing information\n");
301 slot = slot_base + irq_entries++;
303 debug("writing INT%c\n", 'A' + pr.pin - 1);
304 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
308 rt->size = irq_entries * sizeof(struct irq_info) + 32;
310 /* Fix up the table checksum */
311 rt->checksum = table_compute_checksum(rt, rt->size);
313 gd->arch.pirq_routing_table = rt;
318 static void irq_enable_sci(struct udevice *dev)
320 struct irq_router *priv = dev_get_priv(dev);
322 if (priv->actl_8bit) {
323 /* Bit7 must be turned on to enable ACPI */
324 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
326 /* Write 0 to enable SCI on IRQ9 */
327 if (priv->config == PIRQ_VIA_PCI)
328 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
330 writel(0, (uintptr_t)priv->ibase + priv->actl_addr);
334 int irq_router_probe(struct udevice *dev)
338 ret = create_pirq_routing_table(dev);
340 debug("Failed to create pirq routing table\n");
344 pirq_route_irqs(dev, gd->arch.pirq_routing_table->slots,
345 get_irq_slot_count(gd->arch.pirq_routing_table));
347 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
353 ulong write_pirq_routing_table(ulong addr)
355 if (!gd->arch.pirq_routing_table)
358 return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table);
361 static const struct udevice_id irq_router_ids[] = {
362 { .compatible = "intel,irq-router" },
366 U_BOOT_DRIVER(irq_router_drv) = {
369 .of_match = irq_router_ids,
370 .probe = irq_router_probe,
371 .priv_auto_alloc_size = sizeof(struct irq_router),