1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
5 * Part of this file is ported from coreboot src/arch/x86/boot/pirq_routing.c
12 #include <asm/pirq_routing.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 static u8 pirq_get_next_free_irq(struct udevice *dev, u8 *pirq, u16 bitmap,
17 bool irq_already_routed[])
22 /* IRQ sharing starts from IRQ#3 */
23 for (i = 3; i < 16; i++) {
24 /* Can we assign this IRQ? */
25 if (!((bitmap >> i) & 1))
28 /* We can, now let's assume we can use this IRQ */
31 /* Have we already routed it? */
32 if (irq_already_routed[irq])
35 for (link = 0; link < CONFIG_MAX_PIRQ_LINKS; link++) {
36 if (pirq_check_irq_routed(dev, link, irq)) {
37 irq_already_routed[irq] = true;
42 /* If it's not yet routed, use it */
43 if (!irq_already_routed[irq]) {
44 irq_already_routed[irq] = true;
48 /* But if it was already routed, try the next one */
51 /* Now we get our IRQ */
55 void pirq_route_irqs(struct udevice *dev, struct irq_info *irq, int num)
57 unsigned char irq_slot[MAX_INTX_ENTRIES];
58 unsigned char pirq[CONFIG_MAX_PIRQ_LINKS];
59 bool irq_already_routed[16];
62 memset(pirq, 0, CONFIG_MAX_PIRQ_LINKS);
63 memset(irq_already_routed, '\0', sizeof(irq_already_routed));
66 for (i = 0; i < num; i++) {
67 debug("PIRQ Entry %d Dev: %d.%x.%d\n", i,
68 irq->bus, irq->devfn >> 3, irq->devfn & 7);
70 for (intx = 0; intx < MAX_INTX_ENTRIES; intx++) {
71 int link = irq->irq[intx].link;
72 int bitmap = irq->irq[intx].bitmap;
75 debug("INT%c link: %x bitmap: %x ",
76 'A' + intx, link, bitmap);
78 if (!bitmap || !link) {
79 debug("not routed\n");
84 /* translate link value to link number */
85 link = pirq_translate_link(dev, link);
89 irq = pirq_get_next_free_irq(dev, pirq, bitmap,
96 debug("IRQ: %d\n", irq);
99 /* Assign IRQ in the interrupt router */
100 pirq_assign_irq(dev, link, irq);
103 /* Bus, device, slots IRQs for {A,B,C,D} */
104 pci_assign_irqs(irq->bus, irq->devfn >> 3, irq_slot);
109 for (i = 0; i < CONFIG_MAX_PIRQ_LINKS; i++)
110 debug("PIRQ%c: %d\n", 'A' + i, pirq[i]);
113 u32 copy_pirq_routing_table(u32 addr, struct irq_routing_table *rt)
115 struct irq_routing_table *rom_rt;
117 /* Align the table to be 16 byte aligned */
118 addr = ALIGN(addr, 16);
120 debug("Copying Interrupt Routing Table to 0x%x\n", addr);
121 memcpy((void *)(uintptr_t)addr, rt, rt->size);
124 * We do the sanity check here against the copied table after memcpy,
125 * as something might go wrong after the memcpy, which is normally
126 * due to the F segment decode is not turned on to systeam RAM.
128 rom_rt = (struct irq_routing_table *)(uintptr_t)addr;
129 if (rom_rt->signature != PIRQ_SIGNATURE ||
130 rom_rt->version != PIRQ_VERSION || rom_rt->size % 16) {
131 printf("Interrupt Routing Table not valid\n");
135 return addr + rt->size;
138 ulong write_pirq_routing_table(ulong addr)
140 if (!gd->arch.pirq_routing_table)
143 return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table);