blob: 205405b95fe4817734d80f327e0a93f9af453813 [file] [log] [blame]
Bin Meng9c7dea62015-05-25 22:35:04 +08001/*
2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <errno.h>
9#include <fdtdec.h>
10#include <malloc.h>
11#include <asm/io.h>
12#include <asm/irq.h>
13#include <asm/pci.h>
14#include <asm/pirq_routing.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18static struct irq_router irq_router;
19static struct irq_routing_table *pirq_routing_table;
20
21bool pirq_check_irq_routed(int link, u8 irq)
22{
23 u8 pirq;
24 int base = irq_router.link_base;
25
26 if (irq_router.config == PIRQ_VIA_PCI)
27 pirq = x86_pci_read_config8(irq_router.bdf,
28 LINK_N2V(link, base));
29 else
30 pirq = readb(irq_router.ibase + LINK_N2V(link, base));
31
32 pirq &= 0xf;
33
34 /* IRQ# 0/1/2/8/13 are reserved */
35 if (pirq < 3 || pirq == 8 || pirq == 13)
36 return false;
37
38 return pirq == irq ? true : false;
39}
40
41int pirq_translate_link(int link)
42{
43 return LINK_V2N(link, irq_router.link_base);
44}
45
46void pirq_assign_irq(int link, u8 irq)
47{
48 int base = irq_router.link_base;
49
50 /* IRQ# 0/1/2/8/13 are reserved */
51 if (irq < 3 || irq == 8 || irq == 13)
52 return;
53
54 if (irq_router.config == PIRQ_VIA_PCI)
55 x86_pci_write_config8(irq_router.bdf,
56 LINK_N2V(link, base), irq);
57 else
58 writeb(irq, irq_router.ibase + LINK_N2V(link, base));
59}
60
Bin Mengdf817492015-06-23 12:18:47 +080061static struct irq_info *check_dup_entry(struct irq_info *slot_base,
62 int entry_num, int bus, int device)
Bin Meng9c7dea62015-05-25 22:35:04 +080063{
Bin Mengdf817492015-06-23 12:18:47 +080064 struct irq_info *slot = slot_base;
65 int i;
Bin Meng9c7dea62015-05-25 22:35:04 +080066
Bin Mengdf817492015-06-23 12:18:47 +080067 for (i = 0; i < entry_num; i++) {
68 if (slot->bus == bus && slot->devfn == (device << 3))
69 break;
70 slot++;
71 }
72
73 return (i == entry_num) ? NULL : slot;
74}
75
76static inline void fill_irq_info(struct irq_info *slot, int bus, int device,
77 int pin, int pirq)
78{
Bin Meng9c7dea62015-05-25 22:35:04 +080079 slot->bus = bus;
Bin Meng8c38e4d2015-06-23 12:18:46 +080080 slot->devfn = (device << 3) | 0;
Bin Meng9c7dea62015-05-25 22:35:04 +080081 slot->irq[pin - 1].link = LINK_N2V(pirq, irq_router.link_base);
82 slot->irq[pin - 1].bitmap = irq_router.irq_mask;
Bin Meng9c7dea62015-05-25 22:35:04 +080083}
84
85__weak void cpu_irq_init(void)
86{
87 return;
88}
89
90static int create_pirq_routing_table(void)
91{
92 const void *blob = gd->fdt_blob;
93 struct fdt_pci_addr addr;
94 int node;
95 int len, count;
96 const u32 *cell;
97 struct irq_routing_table *rt;
Bin Mengdf817492015-06-23 12:18:47 +080098 struct irq_info *slot, *slot_base;
Bin Meng9c7dea62015-05-25 22:35:04 +080099 int irq_entries = 0;
Simon Glassf2b85ab2016-01-18 20:19:21 -0700100 int parent;
Bin Meng9c7dea62015-05-25 22:35:04 +0800101 int i;
102 int ret;
103
104 node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_IRQ_ROUTER);
105 if (node < 0) {
106 debug("%s: Cannot find irq router node\n", __func__);
107 return -EINVAL;
108 }
109
Simon Glassf2b85ab2016-01-18 20:19:21 -0700110 /* TODO(sjg@chromium.org): Drop this when PIRQ is a driver */
111 parent = fdt_parent_offset(blob, node);
112 if (parent < 0)
113 return -EINVAL;
114 ret = fdtdec_get_pci_addr(blob, parent, FDT_PCI_SPACE_CONFIG,
Bin Meng9c7dea62015-05-25 22:35:04 +0800115 "reg", &addr);
116 if (ret)
117 return ret;
118
119 /* extract the bdf from fdt_pci_addr */
120 irq_router.bdf = addr.phys_hi & 0xffff00;
121
122 ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
123 if (!ret) {
124 irq_router.config = PIRQ_VIA_PCI;
125 } else {
126 ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
127 if (!ret)
128 irq_router.config = PIRQ_VIA_IBASE;
129 else
130 return -EINVAL;
131 }
132
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600133 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
134 if (ret == -1)
Bin Meng9c7dea62015-05-25 22:35:04 +0800135 return ret;
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600136 irq_router.link_base = ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800137
138 irq_router.irq_mask = fdtdec_get_int(blob, node,
139 "intel,pirq-mask", PIRQ_BITMAP);
140
141 if (irq_router.config == PIRQ_VIA_IBASE) {
142 int ibase_off;
143
144 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
145 if (!ibase_off)
146 return -EINVAL;
147
148 /*
149 * Here we assume that the IBASE register has already been
150 * properly configured by U-Boot before.
151 *
152 * By 'valid' we mean:
153 * 1) a valid memory space carved within system memory space
154 * assigned to IBASE register block.
155 * 2) memory range decoding is enabled.
156 * Hence we don't do any santify test here.
157 */
158 irq_router.ibase = x86_pci_read_config32(irq_router.bdf,
159 ibase_off);
160 irq_router.ibase &= ~0xf;
161 }
162
163 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600164 if (!cell || len % sizeof(struct pirq_routing))
Bin Meng9c7dea62015-05-25 22:35:04 +0800165 return -EINVAL;
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600166 count = len / sizeof(struct pirq_routing);
Bin Meng9c7dea62015-05-25 22:35:04 +0800167
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600168 rt = calloc(1, sizeof(struct irq_routing_table));
Bin Meng9c7dea62015-05-25 22:35:04 +0800169 if (!rt)
170 return -ENOMEM;
Bin Meng9c7dea62015-05-25 22:35:04 +0800171
172 /* Populate the PIRQ table fields */
173 rt->signature = PIRQ_SIGNATURE;
174 rt->version = PIRQ_VERSION;
Bin Meng9c235432015-06-23 12:18:45 +0800175 rt->rtr_bus = PCI_BUS(irq_router.bdf);
Bin Meng9c7dea62015-05-25 22:35:04 +0800176 rt->rtr_devfn = (PCI_DEV(irq_router.bdf) << 3) |
177 PCI_FUNC(irq_router.bdf);
178 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
179 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
180
Bin Mengdf817492015-06-23 12:18:47 +0800181 slot_base = rt->slots;
Bin Meng9c7dea62015-05-25 22:35:04 +0800182
183 /* Now fill in the irq_info entries in the PIRQ table */
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600184 for (i = 0; i < count;
185 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800186 struct pirq_routing pr;
187
188 pr.bdf = fdt_addr_to_cpu(cell[0]);
189 pr.pin = fdt_addr_to_cpu(cell[1]);
190 pr.pirq = fdt_addr_to_cpu(cell[2]);
191
192 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
193 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
194 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
195 'A' + pr.pirq);
Bin Mengdf817492015-06-23 12:18:47 +0800196
197 slot = check_dup_entry(slot_base, irq_entries,
198 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
199 if (slot) {
200 debug("found entry for bus %d device %d, ",
201 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
202
203 if (slot->irq[pr.pin - 1].link) {
204 debug("skipping\n");
205
206 /*
207 * Sanity test on the routed PIRQ pin
208 *
209 * If they don't match, show a warning to tell
210 * there might be something wrong with the PIRQ
211 * routing information in the device tree.
212 */
213 if (slot->irq[pr.pin - 1].link !=
214 LINK_N2V(pr.pirq, irq_router.link_base))
215 debug("WARNING: Inconsistent PIRQ routing information\n");
Bin Mengdf817492015-06-23 12:18:47 +0800216 continue;
217 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600218 } else {
219 slot = slot_base + irq_entries++;
Bin Mengdf817492015-06-23 12:18:47 +0800220 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600221 debug("writing INT%c\n", 'A' + pr.pin - 1);
222 fill_irq_info(slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf), pr.pin,
223 pr.pirq);
Bin Meng9c7dea62015-05-25 22:35:04 +0800224 }
225
226 rt->size = irq_entries * sizeof(struct irq_info) + 32;
227
228 pirq_routing_table = rt;
229
230 return 0;
231}
232
Simon Glass7e4be122015-08-10 07:05:08 -0600233int pirq_init(void)
Bin Meng9c7dea62015-05-25 22:35:04 +0800234{
Simon Glass7e4be122015-08-10 07:05:08 -0600235 int ret;
236
Bin Meng9c7dea62015-05-25 22:35:04 +0800237 cpu_irq_init();
238
Simon Glass7e4be122015-08-10 07:05:08 -0600239 ret = create_pirq_routing_table();
240 if (ret) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800241 debug("Failed to create pirq routing table\n");
Simon Glass7e4be122015-08-10 07:05:08 -0600242 return ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800243 }
Simon Glass7e4be122015-08-10 07:05:08 -0600244 /* Route PIRQ */
245 pirq_route_irqs(pirq_routing_table->slots,
246 get_irq_slot_count(pirq_routing_table));
247
248 return 0;
Bin Meng9c7dea62015-05-25 22:35:04 +0800249}
250
251u32 write_pirq_routing_table(u32 addr)
252{
Bin Meng67b24972015-05-25 22:35:07 +0800253 if (!pirq_routing_table)
254 return addr;
255
Bin Meng9c7dea62015-05-25 22:35:04 +0800256 return copy_pirq_routing_table(addr, pirq_routing_table);
257}