blob: 766b2451a2cc3e4b8762b22eed17cf6e48569e78 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Meng51c3b1e2015-05-25 22:35:04 +08002/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
Bin Meng51c3b1e2015-05-25 22:35:04 +08004 */
5
6#include <common.h>
Simon Glass18a8e092016-01-19 21:32:25 -07007#include <dm.h>
Bin Meng51c3b1e2015-05-25 22:35:04 +08008#include <errno.h>
9#include <fdtdec.h>
Simon Glass21bb12a2020-02-06 09:54:58 -070010#include <irq.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Bin Meng51c3b1e2015-05-25 22:35:04 +080012#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Bin Meng51c3b1e2015-05-25 22:35:04 +080014#include <asm/io.h>
15#include <asm/irq.h>
16#include <asm/pci.h>
17#include <asm/pirq_routing.h>
Bin Meng3371c0b2016-05-11 07:44:57 -070018#include <asm/tables.h>
Bin Meng51c3b1e2015-05-25 22:35:04 +080019
20DECLARE_GLOBAL_DATA_PTR;
21
Bin Mengd803f542018-06-12 01:26:46 -070022/**
23 * pirq_reg_to_linkno() - Convert a PIRQ routing register offset to link number
24 *
25 * @priv: IRQ router driver's priv data
26 * @reg: PIRQ routing register offset from the base address
27 * @return: PIRQ link number (0 for PIRQA, 1 for PIRQB, etc)
28 */
29static inline int pirq_reg_to_linkno(struct irq_router *priv, int reg)
30{
31 int linkno = 0;
32
33 if (priv->has_regmap) {
34 struct pirq_regmap *map = priv->regmap;
35 int i;
36
37 for (i = 0; i < priv->link_num; i++) {
38 if (reg - priv->link_base == map->offset) {
39 linkno = map->link;
40 break;
41 }
42 map++;
43 }
44 } else {
45 linkno = reg - priv->link_base;
46 }
47
48 return linkno;
49}
50
51/**
52 * pirq_linkno_to_reg() - Convert a PIRQ link number to routing register offset
53 *
54 * @priv: IRQ router driver's priv data
55 * @linkno: PIRQ link number (0 for PIRQA, 1 for PIRQB, etc)
56 * @return: PIRQ routing register offset from the base address
57 */
58static inline int pirq_linkno_to_reg(struct irq_router *priv, int linkno)
59{
60 int reg = 0;
61
62 if (priv->has_regmap) {
63 struct pirq_regmap *map = priv->regmap;
64 int i;
65
66 for (i = 0; i < priv->link_num; i++) {
67 if (linkno == map->link) {
68 reg = map->offset + priv->link_base;
69 break;
70 }
71 map++;
72 }
73 } else {
74 reg = linkno + priv->link_base;
75 }
76
77 return reg;
78}
79
Bin Menga5a20032016-02-01 01:40:51 -080080bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
Bin Meng51c3b1e2015-05-25 22:35:04 +080081{
Bin Menga5a20032016-02-01 01:40:51 -080082 struct irq_router *priv = dev_get_priv(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +080083 u8 pirq;
Bin Meng51c3b1e2015-05-25 22:35:04 +080084
Bin Menga5a20032016-02-01 01:40:51 -080085 if (priv->config == PIRQ_VIA_PCI)
Bin Meng1defbb12018-06-03 19:04:23 -070086 dm_pci_read_config8(dev->parent,
Bin Mengd803f542018-06-12 01:26:46 -070087 pirq_linkno_to_reg(priv, link), &pirq);
Bin Meng51c3b1e2015-05-25 22:35:04 +080088 else
Bin Meng1defbb12018-06-03 19:04:23 -070089 pirq = readb((uintptr_t)priv->ibase +
Bin Mengd803f542018-06-12 01:26:46 -070090 pirq_linkno_to_reg(priv, link));
Bin Meng51c3b1e2015-05-25 22:35:04 +080091
92 pirq &= 0xf;
93
94 /* IRQ# 0/1/2/8/13 are reserved */
95 if (pirq < 3 || pirq == 8 || pirq == 13)
96 return false;
97
98 return pirq == irq ? true : false;
99}
100
Bin Menga5a20032016-02-01 01:40:51 -0800101int pirq_translate_link(struct udevice *dev, int link)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800102{
Bin Menga5a20032016-02-01 01:40:51 -0800103 struct irq_router *priv = dev_get_priv(dev);
104
Bin Mengd803f542018-06-12 01:26:46 -0700105 return pirq_reg_to_linkno(priv, link);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800106}
107
Bin Menga5a20032016-02-01 01:40:51 -0800108void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800109{
Bin Menga5a20032016-02-01 01:40:51 -0800110 struct irq_router *priv = dev_get_priv(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800111
112 /* IRQ# 0/1/2/8/13 are reserved */
113 if (irq < 3 || irq == 8 || irq == 13)
114 return;
115
Bin Menga5a20032016-02-01 01:40:51 -0800116 if (priv->config == PIRQ_VIA_PCI)
Bin Meng1defbb12018-06-03 19:04:23 -0700117 dm_pci_write_config8(dev->parent,
Bin Mengd803f542018-06-12 01:26:46 -0700118 pirq_linkno_to_reg(priv, link), irq);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800119 else
Bin Meng1defbb12018-06-03 19:04:23 -0700120 writeb(irq, (uintptr_t)priv->ibase +
Bin Mengd803f542018-06-12 01:26:46 -0700121 pirq_linkno_to_reg(priv, link));
Bin Meng51c3b1e2015-05-25 22:35:04 +0800122}
123
Bin Meng16758a32015-06-23 12:18:47 +0800124static struct irq_info *check_dup_entry(struct irq_info *slot_base,
125 int entry_num, int bus, int device)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800126{
Bin Meng16758a32015-06-23 12:18:47 +0800127 struct irq_info *slot = slot_base;
128 int i;
129
130 for (i = 0; i < entry_num; i++) {
131 if (slot->bus == bus && slot->devfn == (device << 3))
132 break;
133 slot++;
134 }
Bin Meng51c3b1e2015-05-25 22:35:04 +0800135
Bin Meng16758a32015-06-23 12:18:47 +0800136 return (i == entry_num) ? NULL : slot;
137}
138
Bin Menga5a20032016-02-01 01:40:51 -0800139static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
140 int bus, int device, int pin, int pirq)
Bin Meng16758a32015-06-23 12:18:47 +0800141{
Bin Meng51c3b1e2015-05-25 22:35:04 +0800142 slot->bus = bus;
Bin Meng3a531a32015-06-23 12:18:46 +0800143 slot->devfn = (device << 3) | 0;
Bin Mengd803f542018-06-12 01:26:46 -0700144 slot->irq[pin - 1].link = pirq_linkno_to_reg(priv, pirq);
Bin Menga5a20032016-02-01 01:40:51 -0800145 slot->irq[pin - 1].bitmap = priv->irq_mask;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800146}
147
Simon Glassddcafd62016-01-19 21:32:28 -0700148static int create_pirq_routing_table(struct udevice *dev)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800149{
Bin Menga5a20032016-02-01 01:40:51 -0800150 struct irq_router *priv = dev_get_priv(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800151 const void *blob = gd->fdt_blob;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800152 int node;
153 int len, count;
154 const u32 *cell;
Bin Mengd803f542018-06-12 01:26:46 -0700155 struct pirq_regmap *map;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800156 struct irq_routing_table *rt;
Bin Meng16758a32015-06-23 12:18:47 +0800157 struct irq_info *slot, *slot_base;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800158 int irq_entries = 0;
159 int i;
160 int ret;
161
Simon Glassdd79d6e2017-01-17 16:52:55 -0700162 node = dev_of_offset(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800163
164 /* extract the bdf from fdt_pci_addr */
Bin Menga5a20032016-02-01 01:40:51 -0800165 priv->bdf = dm_pci_get_bdf(dev->parent);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800166
Simon Glassb0ea7402016-10-02 17:59:28 -0600167 ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
Bin Meng51c3b1e2015-05-25 22:35:04 +0800168 if (!ret) {
Bin Menga5a20032016-02-01 01:40:51 -0800169 priv->config = PIRQ_VIA_PCI;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800170 } else {
Simon Glassb0ea7402016-10-02 17:59:28 -0600171 ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
172 "ibase");
Bin Meng51c3b1e2015-05-25 22:35:04 +0800173 if (!ret)
Bin Menga5a20032016-02-01 01:40:51 -0800174 priv->config = PIRQ_VIA_IBASE;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800175 else
176 return -EINVAL;
177 }
178
Bin Mengc332fca2018-06-12 01:26:45 -0700179 cell = fdt_getprop(blob, node, "intel,pirq-link", &len);
180 if (!cell || len != 8)
181 return -EINVAL;
182 priv->link_base = fdt_addr_to_cpu(cell[0]);
183 priv->link_num = fdt_addr_to_cpu(cell[1]);
184 if (priv->link_num > CONFIG_MAX_PIRQ_LINKS) {
185 debug("Limiting supported PIRQ link number from %d to %d\n",
186 priv->link_num, CONFIG_MAX_PIRQ_LINKS);
187 priv->link_num = CONFIG_MAX_PIRQ_LINKS;
188 }
Bin Meng51c3b1e2015-05-25 22:35:04 +0800189
Bin Mengd803f542018-06-12 01:26:46 -0700190 cell = fdt_getprop(blob, node, "intel,pirq-regmap", &len);
191 if (cell) {
192 if (len % sizeof(struct pirq_regmap))
193 return -EINVAL;
194
195 count = len / sizeof(struct pirq_regmap);
196 if (count < priv->link_num) {
197 printf("Number of pirq-regmap entires is wrong\n");
198 return -EINVAL;
199 }
200
201 count = priv->link_num;
202 priv->regmap = calloc(count, sizeof(struct pirq_regmap));
203 if (!priv->regmap)
204 return -ENOMEM;
205
206 priv->has_regmap = true;
207 map = priv->regmap;
208 for (i = 0; i < count; i++) {
209 map->link = fdt_addr_to_cpu(cell[0]);
210 map->offset = fdt_addr_to_cpu(cell[1]);
211
212 cell += sizeof(struct pirq_regmap) / sizeof(u32);
213 map++;
214 }
215 }
216
Bin Menga5a20032016-02-01 01:40:51 -0800217 priv->irq_mask = fdtdec_get_int(blob, node,
218 "intel,pirq-mask", PIRQ_BITMAP);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800219
Bin Meng61ad3712016-05-07 07:46:13 -0700220 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
221 /* Reserve IRQ9 for SCI */
222 priv->irq_mask &= ~(1 << 9);
223 }
224
Bin Menga5a20032016-02-01 01:40:51 -0800225 if (priv->config == PIRQ_VIA_IBASE) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800226 int ibase_off;
227
228 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
229 if (!ibase_off)
230 return -EINVAL;
231
232 /*
233 * Here we assume that the IBASE register has already been
234 * properly configured by U-Boot before.
235 *
236 * By 'valid' we mean:
237 * 1) a valid memory space carved within system memory space
238 * assigned to IBASE register block.
239 * 2) memory range decoding is enabled.
240 * Hence we don't do any santify test here.
241 */
Bin Mengbfe20b72016-02-01 01:40:52 -0800242 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
Bin Menga5a20032016-02-01 01:40:51 -0800243 priv->ibase &= ~0xf;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800244 }
245
Bin Mengc3b03ea2016-05-07 07:46:14 -0700246 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
247 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
248
Bin Meng51c3b1e2015-05-25 22:35:04 +0800249 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600250 if (!cell || len % sizeof(struct pirq_routing))
Bin Meng51c3b1e2015-05-25 22:35:04 +0800251 return -EINVAL;
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600252 count = len / sizeof(struct pirq_routing);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800253
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600254 rt = calloc(1, sizeof(struct irq_routing_table));
Bin Meng51c3b1e2015-05-25 22:35:04 +0800255 if (!rt)
256 return -ENOMEM;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800257
258 /* Populate the PIRQ table fields */
259 rt->signature = PIRQ_SIGNATURE;
260 rt->version = PIRQ_VERSION;
Bin Menga5a20032016-02-01 01:40:51 -0800261 rt->rtr_bus = PCI_BUS(priv->bdf);
262 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800263 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
264 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
265
Bin Meng16758a32015-06-23 12:18:47 +0800266 slot_base = rt->slots;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800267
268 /* Now fill in the irq_info entries in the PIRQ table */
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600269 for (i = 0; i < count;
270 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800271 struct pirq_routing pr;
272
273 pr.bdf = fdt_addr_to_cpu(cell[0]);
274 pr.pin = fdt_addr_to_cpu(cell[1]);
275 pr.pirq = fdt_addr_to_cpu(cell[2]);
276
277 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
278 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
279 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
280 'A' + pr.pirq);
Bin Meng16758a32015-06-23 12:18:47 +0800281
282 slot = check_dup_entry(slot_base, irq_entries,
283 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
284 if (slot) {
285 debug("found entry for bus %d device %d, ",
286 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
287
288 if (slot->irq[pr.pin - 1].link) {
289 debug("skipping\n");
290
291 /*
292 * Sanity test on the routed PIRQ pin
293 *
294 * If they don't match, show a warning to tell
295 * there might be something wrong with the PIRQ
296 * routing information in the device tree.
297 */
298 if (slot->irq[pr.pin - 1].link !=
Bin Mengd803f542018-06-12 01:26:46 -0700299 pirq_linkno_to_reg(priv, pr.pirq))
Bin Meng16758a32015-06-23 12:18:47 +0800300 debug("WARNING: Inconsistent PIRQ routing information\n");
Bin Meng16758a32015-06-23 12:18:47 +0800301 continue;
302 }
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600303 } else {
304 slot = slot_base + irq_entries++;
Bin Meng16758a32015-06-23 12:18:47 +0800305 }
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600306 debug("writing INT%c\n", 'A' + pr.pin - 1);
Bin Menga5a20032016-02-01 01:40:51 -0800307 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
308 pr.pin, pr.pirq);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800309 }
310
311 rt->size = irq_entries * sizeof(struct irq_info) + 32;
312
Bin Meng3371c0b2016-05-11 07:44:57 -0700313 /* Fix up the table checksum */
314 rt->checksum = table_compute_checksum(rt, rt->size);
315
Simon Glassf64d6f72017-01-16 07:04:16 -0700316 gd->arch.pirq_routing_table = rt;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800317
318 return 0;
319}
320
Bin Mengc3b03ea2016-05-07 07:46:14 -0700321static void irq_enable_sci(struct udevice *dev)
322{
323 struct irq_router *priv = dev_get_priv(dev);
324
325 if (priv->actl_8bit) {
326 /* Bit7 must be turned on to enable ACPI */
327 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
328 } else {
329 /* Write 0 to enable SCI on IRQ9 */
330 if (priv->config == PIRQ_VIA_PCI)
331 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
332 else
Bin Meng95e4a392017-01-18 03:32:56 -0800333 writel(0, (uintptr_t)priv->ibase + priv->actl_addr);
Bin Mengc3b03ea2016-05-07 07:46:14 -0700334 }
335}
336
Bin Meng0c9f5942018-06-03 19:04:22 -0700337int irq_router_probe(struct udevice *dev)
Simon Glass18a8e092016-01-19 21:32:25 -0700338{
Simon Glassaf1c2d682015-08-10 07:05:08 -0600339 int ret;
340
Simon Glassddcafd62016-01-19 21:32:28 -0700341 ret = create_pirq_routing_table(dev);
Simon Glassaf1c2d682015-08-10 07:05:08 -0600342 if (ret) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800343 debug("Failed to create pirq routing table\n");
Simon Glassaf1c2d682015-08-10 07:05:08 -0600344 return ret;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800345 }
Simon Glassaf1c2d682015-08-10 07:05:08 -0600346 /* Route PIRQ */
Simon Glassf64d6f72017-01-16 07:04:16 -0700347 pirq_route_irqs(dev, gd->arch.pirq_routing_table->slots,
348 get_irq_slot_count(gd->arch.pirq_routing_table));
Simon Glassaf1c2d682015-08-10 07:05:08 -0600349
Bin Mengc3b03ea2016-05-07 07:46:14 -0700350 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
351 irq_enable_sci(dev);
352
Simon Glassaf1c2d682015-08-10 07:05:08 -0600353 return 0;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800354}
355
Simon Glass18a8e092016-01-19 21:32:25 -0700356static const struct udevice_id irq_router_ids[] = {
Simon Glass21bb12a2020-02-06 09:54:58 -0700357 { .compatible = "intel,irq-router", .data = X86_IRQT_BASE },
Simon Glass18a8e092016-01-19 21:32:25 -0700358 { }
359};
360
361U_BOOT_DRIVER(irq_router_drv) = {
362 .name = "intel_irq",
363 .id = UCLASS_IRQ,
364 .of_match = irq_router_ids,
365 .probe = irq_router_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700366 .priv_auto = sizeof(struct irq_router),
Simon Glass18a8e092016-01-19 21:32:25 -0700367};