blob: d6151e085aadc023697653b6924ac1da5469e893 [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>
Simon Glasse76187a2016-01-19 21:32:25 -07008#include <dm.h>
Bin Meng9c7dea62015-05-25 22:35:04 +08009#include <errno.h>
10#include <fdtdec.h>
11#include <malloc.h>
12#include <asm/io.h>
13#include <asm/irq.h>
14#include <asm/pci.h>
15#include <asm/pirq_routing.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19static struct irq_router irq_router;
20static struct irq_routing_table *pirq_routing_table;
21
22bool pirq_check_irq_routed(int link, u8 irq)
23{
24 u8 pirq;
25 int base = irq_router.link_base;
26
27 if (irq_router.config == PIRQ_VIA_PCI)
28 pirq = x86_pci_read_config8(irq_router.bdf,
29 LINK_N2V(link, base));
30 else
31 pirq = readb(irq_router.ibase + LINK_N2V(link, base));
32
33 pirq &= 0xf;
34
35 /* IRQ# 0/1/2/8/13 are reserved */
36 if (pirq < 3 || pirq == 8 || pirq == 13)
37 return false;
38
39 return pirq == irq ? true : false;
40}
41
42int pirq_translate_link(int link)
43{
44 return LINK_V2N(link, irq_router.link_base);
45}
46
47void pirq_assign_irq(int link, u8 irq)
48{
49 int base = irq_router.link_base;
50
51 /* IRQ# 0/1/2/8/13 are reserved */
52 if (irq < 3 || irq == 8 || irq == 13)
53 return;
54
55 if (irq_router.config == PIRQ_VIA_PCI)
56 x86_pci_write_config8(irq_router.bdf,
57 LINK_N2V(link, base), irq);
58 else
59 writeb(irq, irq_router.ibase + LINK_N2V(link, base));
60}
61
Bin Mengdf817492015-06-23 12:18:47 +080062static struct irq_info *check_dup_entry(struct irq_info *slot_base,
63 int entry_num, int bus, int device)
Bin Meng9c7dea62015-05-25 22:35:04 +080064{
Bin Mengdf817492015-06-23 12:18:47 +080065 struct irq_info *slot = slot_base;
66 int i;
Bin Meng9c7dea62015-05-25 22:35:04 +080067
Bin Mengdf817492015-06-23 12:18:47 +080068 for (i = 0; i < entry_num; i++) {
69 if (slot->bus == bus && slot->devfn == (device << 3))
70 break;
71 slot++;
72 }
73
74 return (i == entry_num) ? NULL : slot;
75}
76
77static inline void fill_irq_info(struct irq_info *slot, int bus, int device,
78 int pin, int pirq)
79{
Bin Meng9c7dea62015-05-25 22:35:04 +080080 slot->bus = bus;
Bin Meng8c38e4d2015-06-23 12:18:46 +080081 slot->devfn = (device << 3) | 0;
Bin Meng9c7dea62015-05-25 22:35:04 +080082 slot->irq[pin - 1].link = LINK_N2V(pirq, irq_router.link_base);
83 slot->irq[pin - 1].bitmap = irq_router.irq_mask;
Bin Meng9c7dea62015-05-25 22:35:04 +080084}
85
86__weak void cpu_irq_init(void)
87{
88 return;
89}
90
Simon Glassb565d662016-01-19 21:32:28 -070091static int create_pirq_routing_table(struct udevice *dev)
Bin Meng9c7dea62015-05-25 22:35:04 +080092{
93 const void *blob = gd->fdt_blob;
94 struct fdt_pci_addr addr;
95 int node;
96 int len, count;
97 const u32 *cell;
98 struct irq_routing_table *rt;
Bin Mengdf817492015-06-23 12:18:47 +080099 struct irq_info *slot, *slot_base;
Bin Meng9c7dea62015-05-25 22:35:04 +0800100 int irq_entries = 0;
Simon Glassf2b85ab2016-01-18 20:19:21 -0700101 int parent;
Bin Meng9c7dea62015-05-25 22:35:04 +0800102 int i;
103 int ret;
104
Simon Glassb565d662016-01-19 21:32:28 -0700105 node = dev->of_offset;
106 parent = dev->parent->of_offset;
Simon Glassf2b85ab2016-01-18 20:19:21 -0700107 ret = fdtdec_get_pci_addr(blob, parent, FDT_PCI_SPACE_CONFIG,
Bin Meng9c7dea62015-05-25 22:35:04 +0800108 "reg", &addr);
109 if (ret)
110 return ret;
111
112 /* extract the bdf from fdt_pci_addr */
113 irq_router.bdf = addr.phys_hi & 0xffff00;
114
115 ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
116 if (!ret) {
117 irq_router.config = PIRQ_VIA_PCI;
118 } else {
119 ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
120 if (!ret)
121 irq_router.config = PIRQ_VIA_IBASE;
122 else
123 return -EINVAL;
124 }
125
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600126 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
127 if (ret == -1)
Bin Meng9c7dea62015-05-25 22:35:04 +0800128 return ret;
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600129 irq_router.link_base = ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800130
131 irq_router.irq_mask = fdtdec_get_int(blob, node,
132 "intel,pirq-mask", PIRQ_BITMAP);
133
134 if (irq_router.config == PIRQ_VIA_IBASE) {
135 int ibase_off;
136
137 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
138 if (!ibase_off)
139 return -EINVAL;
140
141 /*
142 * Here we assume that the IBASE register has already been
143 * properly configured by U-Boot before.
144 *
145 * By 'valid' we mean:
146 * 1) a valid memory space carved within system memory space
147 * assigned to IBASE register block.
148 * 2) memory range decoding is enabled.
149 * Hence we don't do any santify test here.
150 */
151 irq_router.ibase = x86_pci_read_config32(irq_router.bdf,
152 ibase_off);
153 irq_router.ibase &= ~0xf;
154 }
155
156 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600157 if (!cell || len % sizeof(struct pirq_routing))
Bin Meng9c7dea62015-05-25 22:35:04 +0800158 return -EINVAL;
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600159 count = len / sizeof(struct pirq_routing);
Bin Meng9c7dea62015-05-25 22:35:04 +0800160
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600161 rt = calloc(1, sizeof(struct irq_routing_table));
Bin Meng9c7dea62015-05-25 22:35:04 +0800162 if (!rt)
163 return -ENOMEM;
Bin Meng9c7dea62015-05-25 22:35:04 +0800164
165 /* Populate the PIRQ table fields */
166 rt->signature = PIRQ_SIGNATURE;
167 rt->version = PIRQ_VERSION;
Bin Meng9c235432015-06-23 12:18:45 +0800168 rt->rtr_bus = PCI_BUS(irq_router.bdf);
Bin Meng9c7dea62015-05-25 22:35:04 +0800169 rt->rtr_devfn = (PCI_DEV(irq_router.bdf) << 3) |
170 PCI_FUNC(irq_router.bdf);
171 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
172 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
173
Bin Mengdf817492015-06-23 12:18:47 +0800174 slot_base = rt->slots;
Bin Meng9c7dea62015-05-25 22:35:04 +0800175
176 /* Now fill in the irq_info entries in the PIRQ table */
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600177 for (i = 0; i < count;
178 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800179 struct pirq_routing pr;
180
181 pr.bdf = fdt_addr_to_cpu(cell[0]);
182 pr.pin = fdt_addr_to_cpu(cell[1]);
183 pr.pirq = fdt_addr_to_cpu(cell[2]);
184
185 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
186 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
187 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
188 'A' + pr.pirq);
Bin Mengdf817492015-06-23 12:18:47 +0800189
190 slot = check_dup_entry(slot_base, irq_entries,
191 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
192 if (slot) {
193 debug("found entry for bus %d device %d, ",
194 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
195
196 if (slot->irq[pr.pin - 1].link) {
197 debug("skipping\n");
198
199 /*
200 * Sanity test on the routed PIRQ pin
201 *
202 * If they don't match, show a warning to tell
203 * there might be something wrong with the PIRQ
204 * routing information in the device tree.
205 */
206 if (slot->irq[pr.pin - 1].link !=
207 LINK_N2V(pr.pirq, irq_router.link_base))
208 debug("WARNING: Inconsistent PIRQ routing information\n");
Bin Mengdf817492015-06-23 12:18:47 +0800209 continue;
210 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600211 } else {
212 slot = slot_base + irq_entries++;
Bin Mengdf817492015-06-23 12:18:47 +0800213 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600214 debug("writing INT%c\n", 'A' + pr.pin - 1);
215 fill_irq_info(slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf), pr.pin,
216 pr.pirq);
Bin Meng9c7dea62015-05-25 22:35:04 +0800217 }
218
219 rt->size = irq_entries * sizeof(struct irq_info) + 32;
220
221 pirq_routing_table = rt;
222
223 return 0;
224}
225
Simon Glassd3b884b2016-01-19 21:32:27 -0700226int irq_router_common_init(struct udevice *dev)
Simon Glasse76187a2016-01-19 21:32:25 -0700227{
Simon Glass7e4be122015-08-10 07:05:08 -0600228 int ret;
229
Bin Meng9c7dea62015-05-25 22:35:04 +0800230 cpu_irq_init();
231
Simon Glassb565d662016-01-19 21:32:28 -0700232 ret = create_pirq_routing_table(dev);
Simon Glass7e4be122015-08-10 07:05:08 -0600233 if (ret) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800234 debug("Failed to create pirq routing table\n");
Simon Glass7e4be122015-08-10 07:05:08 -0600235 return ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800236 }
Simon Glass7e4be122015-08-10 07:05:08 -0600237 /* Route PIRQ */
238 pirq_route_irqs(pirq_routing_table->slots,
239 get_irq_slot_count(pirq_routing_table));
240
241 return 0;
Bin Meng9c7dea62015-05-25 22:35:04 +0800242}
243
Simon Glassd3b884b2016-01-19 21:32:27 -0700244int irq_router_probe(struct udevice *dev)
245{
246 return irq_router_common_init(dev);
247}
248
Bin Meng9c7dea62015-05-25 22:35:04 +0800249u32 write_pirq_routing_table(u32 addr)
250{
Bin Meng67b24972015-05-25 22:35:07 +0800251 if (!pirq_routing_table)
252 return addr;
253
Bin Meng9c7dea62015-05-25 22:35:04 +0800254 return copy_pirq_routing_table(addr, pirq_routing_table);
255}
Simon Glasse76187a2016-01-19 21:32:25 -0700256
257static const struct udevice_id irq_router_ids[] = {
258 { .compatible = "intel,irq-router" },
259 { }
260};
261
262U_BOOT_DRIVER(irq_router_drv) = {
263 .name = "intel_irq",
264 .id = UCLASS_IRQ,
265 .of_match = irq_router_ids,
266 .probe = irq_router_probe,
267};
268
269UCLASS_DRIVER(irq) = {
270 .id = UCLASS_IRQ,
271 .name = "irq",
272};