blob: 5a2591a26cd49df3f6c030a2748185389fd1ada2 [file] [log] [blame]
Bin Mengb5b6b012015-04-24 18:10:05 +08001/*
2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
3 *
4 * Part of this file is ported from coreboot src/arch/x86/boot/pirq_routing.c
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <pci.h>
11#include <asm/pci.h>
12#include <asm/pirq_routing.h>
13#include <asm/tables.h>
14
15static bool irq_already_routed[16];
16
17static u8 pirq_get_next_free_irq(u8 *pirq, u16 bitmap)
18{
19 int i, link;
20 u8 irq = 0;
21
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))
26 continue;
27
28 /* We can, now let's assume we can use this IRQ */
29 irq = i;
30
31 /* Have we already routed it? */
32 if (irq_already_routed[irq])
33 continue;
34
35 for (link = 0; link < CONFIG_MAX_PIRQ_LINKS; link++) {
36 if (pirq_check_irq_routed(link, irq)) {
37 irq_already_routed[irq] = true;
38 break;
39 }
40 }
41
42 /* If it's not yet routed, use it */
43 if (!irq_already_routed[irq]) {
44 irq_already_routed[irq] = true;
45 break;
46 }
47
48 /* But if it was already routed, try the next one */
49 }
50
51 /* Now we get our IRQ */
52 return irq;
53}
54
55void pirq_route_irqs(struct irq_info *irq, int num)
56{
57 unsigned char irq_slot[MAX_INTX_ENTRIES];
58 unsigned char pirq[CONFIG_MAX_PIRQ_LINKS];
59 int i, intx;
60
61 memset(pirq, 0, CONFIG_MAX_PIRQ_LINKS);
62
63 /* Set PCI IRQs */
64 for (i = 0; i < num; i++) {
65 debug("PIRQ Entry %d Dev: %d.%x.%d\n", i,
66 irq->bus, irq->devfn >> 3, irq->devfn & 7);
67
68 for (intx = 0; intx < MAX_INTX_ENTRIES; intx++) {
69 int link = irq->irq[intx].link;
70 int bitmap = irq->irq[intx].bitmap;
71 int irq = 0;
72
73 debug("INT%c link: %x bitmap: %x ",
74 'A' + intx, link, bitmap);
75
76 if (!bitmap || !link) {
77 debug("not routed\n");
78 irq_slot[intx] = irq;
79 continue;
80 }
81
82 /* translate link value to link number */
83 link = pirq_translate_link(link);
84
85 /* yet not routed */
86 if (!pirq[link]) {
87 irq = pirq_get_next_free_irq(pirq, bitmap);
88 pirq[link] = irq;
89 } else {
90 irq = pirq[link];
91 }
92
93 debug("IRQ: %d\n", irq);
94 irq_slot[intx] = irq;
95
96 /* Assign IRQ in the interrupt router */
97 pirq_assign_irq(link, irq);
98 }
99
100 /* Bus, device, slots IRQs for {A,B,C,D} */
101 pci_assign_irqs(irq->bus, irq->devfn >> 3, irq->devfn & 7,
102 irq_slot);
103
104 irq++;
105 }
106
107 for (i = 0; i < CONFIG_MAX_PIRQ_LINKS; i++)
108 debug("PIRQ%c: %d\n", 'A' + i, pirq[i]);
109}
110
111u32 copy_pirq_routing_table(u32 addr, struct irq_routing_table *rt)
112{
113 if (rt->signature != PIRQ_SIGNATURE || rt->version != PIRQ_VERSION ||
114 rt->size % 16) {
115 debug("Interrupt Routing Table not valid\n");
116 return addr;
117 }
118
119 /* Fix up the table checksum */
120 rt->checksum = table_compute_checksum(rt, rt->size);
121
122 /* Align the table to be 16 byte aligned */
123 addr = ALIGN(addr, 16);
124
125 debug("Copying Interrupt Routing Table to 0x%x\n", addr);
126 memcpy((void *)addr, rt, rt->size);
127
128 return addr + rt->size;
129}