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
11 #include <asm/pirq_routing.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 static u8 pirq_get_next_free_irq(struct udevice *dev, u8 *pirq, u16 bitmap,
16 bool irq_already_routed[])
21 /* IRQ sharing starts from IRQ#3 */
22 for (i = 3; i < 16; i++) {
23 /* Can we assign this IRQ? */
24 if (!((bitmap >> i) & 1))
27 /* We can, now let's assume we can use this IRQ */
30 /* Have we already routed it? */
31 if (irq_already_routed[irq])
34 for (link = 0; link < CONFIG_MAX_PIRQ_LINKS; link++) {
35 if (pirq_check_irq_routed(dev, link, irq)) {
36 irq_already_routed[irq] = true;
41 /* If it's not yet routed, use it */
42 if (!irq_already_routed[irq]) {
43 irq_already_routed[irq] = true;
47 /* But if it was already routed, try the next one */
50 /* Now we get our IRQ */
54 void pirq_route_irqs(struct udevice *dev, struct irq_info *irq, int num)
56 unsigned char irq_slot[MAX_INTX_ENTRIES];
57 unsigned char pirq[CONFIG_MAX_PIRQ_LINKS];
58 bool irq_already_routed[16];
61 memset(pirq, 0, CONFIG_MAX_PIRQ_LINKS);
62 memset(irq_already_routed, '\0', sizeof(irq_already_routed));
65 for (i = 0; i < num; i++) {
66 debug("PIRQ Entry %d Dev: %d.%x.%d\n", i,
67 irq->bus, irq->devfn >> 3, irq->devfn & 7);
69 for (intx = 0; intx < MAX_INTX_ENTRIES; intx++) {
70 int link = irq->irq[intx].link;
71 int bitmap = irq->irq[intx].bitmap;
74 debug("INT%c link: %x bitmap: %x ",
75 'A' + intx, link, bitmap);
77 if (!bitmap || !link) {
78 debug("not routed\n");
83 /* translate link value to link number */
84 link = pirq_translate_link(dev, link);
88 irq = pirq_get_next_free_irq(dev, pirq, bitmap,
95 debug("IRQ: %d\n", irq);
98 /* Assign IRQ in the interrupt router */
99 pirq_assign_irq(dev, link, irq);
102 /* Bus, device, slots IRQs for {A,B,C,D} */
103 pci_assign_irqs(irq->bus, irq->devfn >> 3, irq_slot);
108 for (i = 0; i < CONFIG_MAX_PIRQ_LINKS; i++)
109 debug("PIRQ%c: %d\n", 'A' + i, pirq[i]);
112 u32 copy_pirq_routing_table(u32 addr, struct irq_routing_table *rt)
114 struct irq_routing_table *rom_rt;
116 /* Align the table to be 16 byte aligned */
117 addr = ALIGN(addr, 16);
119 debug("Copying Interrupt Routing Table to 0x%x\n", addr);
120 memcpy((void *)(uintptr_t)addr, rt, rt->size);
123 * We do the sanity check here against the copied table after memcpy,
124 * as something might go wrong after the memcpy, which is normally
125 * due to the F segment decode is not turned on to systeam RAM.
127 rom_rt = (struct irq_routing_table *)(uintptr_t)addr;
128 if (rom_rt->signature != PIRQ_SIGNATURE ||
129 rom_rt->version != PIRQ_VERSION || rom_rt->size % 16) {
130 printf("Interrupt Routing Table not valid\n");
134 return addr + rt->size;
137 ulong write_pirq_routing_table(ulong addr)
139 if (!gd->arch.pirq_routing_table)
142 return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table);