blob: bafa0310822d5b133f81dbad160fac9b56081b57 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Meng9c7dea62015-05-25 22:35:04 +08002/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
Bin Meng9c7dea62015-05-25 22:35:04 +08004 */
5
6#include <common.h>
Simon Glasse76187a2016-01-19 21:32:25 -07007#include <dm.h>
Bin Meng9c7dea62015-05-25 22:35:04 +08008#include <errno.h>
9#include <fdtdec.h>
Simon Glass69c2dc92020-02-06 09:54:58 -070010#include <irq.h>
Bin Meng9c7dea62015-05-25 22:35:04 +080011#include <malloc.h>
12#include <asm/io.h>
13#include <asm/irq.h>
14#include <asm/pci.h>
15#include <asm/pirq_routing.h>
Bin Meng10d569e2016-05-11 07:44:57 -070016#include <asm/tables.h>
Bin Meng9c7dea62015-05-25 22:35:04 +080017
18DECLARE_GLOBAL_DATA_PTR;
19
Bin Meng51050ff2018-06-12 01:26:46 -070020/**
21 * pirq_reg_to_linkno() - Convert a PIRQ routing register offset to link number
22 *
23 * @priv: IRQ router driver's priv data
24 * @reg: PIRQ routing register offset from the base address
25 * @return: PIRQ link number (0 for PIRQA, 1 for PIRQB, etc)
26 */
27static inline int pirq_reg_to_linkno(struct irq_router *priv, int reg)
28{
29 int linkno = 0;
30
31 if (priv->has_regmap) {
32 struct pirq_regmap *map = priv->regmap;
33 int i;
34
35 for (i = 0; i < priv->link_num; i++) {
36 if (reg - priv->link_base == map->offset) {
37 linkno = map->link;
38 break;
39 }
40 map++;
41 }
42 } else {
43 linkno = reg - priv->link_base;
44 }
45
46 return linkno;
47}
48
49/**
50 * pirq_linkno_to_reg() - Convert a PIRQ link number to routing register offset
51 *
52 * @priv: IRQ router driver's priv data
53 * @linkno: PIRQ link number (0 for PIRQA, 1 for PIRQB, etc)
54 * @return: PIRQ routing register offset from the base address
55 */
56static inline int pirq_linkno_to_reg(struct irq_router *priv, int linkno)
57{
58 int reg = 0;
59
60 if (priv->has_regmap) {
61 struct pirq_regmap *map = priv->regmap;
62 int i;
63
64 for (i = 0; i < priv->link_num; i++) {
65 if (linkno == map->link) {
66 reg = map->offset + priv->link_base;
67 break;
68 }
69 map++;
70 }
71 } else {
72 reg = linkno + priv->link_base;
73 }
74
75 return reg;
76}
77
Bin Mengb46c2082016-02-01 01:40:51 -080078bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
Bin Meng9c7dea62015-05-25 22:35:04 +080079{
Bin Mengb46c2082016-02-01 01:40:51 -080080 struct irq_router *priv = dev_get_priv(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +080081 u8 pirq;
Bin Meng9c7dea62015-05-25 22:35:04 +080082
Bin Mengb46c2082016-02-01 01:40:51 -080083 if (priv->config == PIRQ_VIA_PCI)
Bin Meng594d0892018-06-03 19:04:23 -070084 dm_pci_read_config8(dev->parent,
Bin Meng51050ff2018-06-12 01:26:46 -070085 pirq_linkno_to_reg(priv, link), &pirq);
Bin Meng9c7dea62015-05-25 22:35:04 +080086 else
Bin Meng594d0892018-06-03 19:04:23 -070087 pirq = readb((uintptr_t)priv->ibase +
Bin Meng51050ff2018-06-12 01:26:46 -070088 pirq_linkno_to_reg(priv, link));
Bin Meng9c7dea62015-05-25 22:35:04 +080089
90 pirq &= 0xf;
91
92 /* IRQ# 0/1/2/8/13 are reserved */
93 if (pirq < 3 || pirq == 8 || pirq == 13)
94 return false;
95
96 return pirq == irq ? true : false;
97}
98
Bin Mengb46c2082016-02-01 01:40:51 -080099int pirq_translate_link(struct udevice *dev, int link)
Bin Meng9c7dea62015-05-25 22:35:04 +0800100{
Bin Mengb46c2082016-02-01 01:40:51 -0800101 struct irq_router *priv = dev_get_priv(dev);
102
Bin Meng51050ff2018-06-12 01:26:46 -0700103 return pirq_reg_to_linkno(priv, link);
Bin Meng9c7dea62015-05-25 22:35:04 +0800104}
105
Bin Mengb46c2082016-02-01 01:40:51 -0800106void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
Bin Meng9c7dea62015-05-25 22:35:04 +0800107{
Bin Mengb46c2082016-02-01 01:40:51 -0800108 struct irq_router *priv = dev_get_priv(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +0800109
110 /* IRQ# 0/1/2/8/13 are reserved */
111 if (irq < 3 || irq == 8 || irq == 13)
112 return;
113
Bin Mengb46c2082016-02-01 01:40:51 -0800114 if (priv->config == PIRQ_VIA_PCI)
Bin Meng594d0892018-06-03 19:04:23 -0700115 dm_pci_write_config8(dev->parent,
Bin Meng51050ff2018-06-12 01:26:46 -0700116 pirq_linkno_to_reg(priv, link), irq);
Bin Meng9c7dea62015-05-25 22:35:04 +0800117 else
Bin Meng594d0892018-06-03 19:04:23 -0700118 writeb(irq, (uintptr_t)priv->ibase +
Bin Meng51050ff2018-06-12 01:26:46 -0700119 pirq_linkno_to_reg(priv, link));
Bin Meng9c7dea62015-05-25 22:35:04 +0800120}
121
Bin Mengdf817492015-06-23 12:18:47 +0800122static struct irq_info *check_dup_entry(struct irq_info *slot_base,
123 int entry_num, int bus, int device)
Bin Meng9c7dea62015-05-25 22:35:04 +0800124{
Bin Mengdf817492015-06-23 12:18:47 +0800125 struct irq_info *slot = slot_base;
126 int i;
Bin Meng9c7dea62015-05-25 22:35:04 +0800127
Bin Mengdf817492015-06-23 12:18:47 +0800128 for (i = 0; i < entry_num; i++) {
129 if (slot->bus == bus && slot->devfn == (device << 3))
130 break;
131 slot++;
132 }
133
134 return (i == entry_num) ? NULL : slot;
135}
136
Bin Mengb46c2082016-02-01 01:40:51 -0800137static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
138 int bus, int device, int pin, int pirq)
Bin Mengdf817492015-06-23 12:18:47 +0800139{
Bin Meng9c7dea62015-05-25 22:35:04 +0800140 slot->bus = bus;
Bin Meng8c38e4d2015-06-23 12:18:46 +0800141 slot->devfn = (device << 3) | 0;
Bin Meng51050ff2018-06-12 01:26:46 -0700142 slot->irq[pin - 1].link = pirq_linkno_to_reg(priv, pirq);
Bin Mengb46c2082016-02-01 01:40:51 -0800143 slot->irq[pin - 1].bitmap = priv->irq_mask;
Bin Meng9c7dea62015-05-25 22:35:04 +0800144}
145
Simon Glassb565d662016-01-19 21:32:28 -0700146static int create_pirq_routing_table(struct udevice *dev)
Bin Meng9c7dea62015-05-25 22:35:04 +0800147{
Bin Mengb46c2082016-02-01 01:40:51 -0800148 struct irq_router *priv = dev_get_priv(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +0800149 const void *blob = gd->fdt_blob;
Bin Meng9c7dea62015-05-25 22:35:04 +0800150 int node;
151 int len, count;
152 const u32 *cell;
Bin Meng51050ff2018-06-12 01:26:46 -0700153 struct pirq_regmap *map;
Bin Meng9c7dea62015-05-25 22:35:04 +0800154 struct irq_routing_table *rt;
Bin Mengdf817492015-06-23 12:18:47 +0800155 struct irq_info *slot, *slot_base;
Bin Meng9c7dea62015-05-25 22:35:04 +0800156 int irq_entries = 0;
157 int i;
158 int ret;
159
Simon Glasse160f7d2017-01-17 16:52:55 -0700160 node = dev_of_offset(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +0800161
162 /* extract the bdf from fdt_pci_addr */
Bin Mengb46c2082016-02-01 01:40:51 -0800163 priv->bdf = dm_pci_get_bdf(dev->parent);
Bin Meng9c7dea62015-05-25 22:35:04 +0800164
Simon Glassb02e4042016-10-02 17:59:28 -0600165 ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
Bin Meng9c7dea62015-05-25 22:35:04 +0800166 if (!ret) {
Bin Mengb46c2082016-02-01 01:40:51 -0800167 priv->config = PIRQ_VIA_PCI;
Bin Meng9c7dea62015-05-25 22:35:04 +0800168 } else {
Simon Glassb02e4042016-10-02 17:59:28 -0600169 ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
170 "ibase");
Bin Meng9c7dea62015-05-25 22:35:04 +0800171 if (!ret)
Bin Mengb46c2082016-02-01 01:40:51 -0800172 priv->config = PIRQ_VIA_IBASE;
Bin Meng9c7dea62015-05-25 22:35:04 +0800173 else
174 return -EINVAL;
175 }
176
Bin Mengdcec5d52018-06-12 01:26:45 -0700177 cell = fdt_getprop(blob, node, "intel,pirq-link", &len);
178 if (!cell || len != 8)
179 return -EINVAL;
180 priv->link_base = fdt_addr_to_cpu(cell[0]);
181 priv->link_num = fdt_addr_to_cpu(cell[1]);
182 if (priv->link_num > CONFIG_MAX_PIRQ_LINKS) {
183 debug("Limiting supported PIRQ link number from %d to %d\n",
184 priv->link_num, CONFIG_MAX_PIRQ_LINKS);
185 priv->link_num = CONFIG_MAX_PIRQ_LINKS;
186 }
Bin Meng9c7dea62015-05-25 22:35:04 +0800187
Bin Meng51050ff2018-06-12 01:26:46 -0700188 cell = fdt_getprop(blob, node, "intel,pirq-regmap", &len);
189 if (cell) {
190 if (len % sizeof(struct pirq_regmap))
191 return -EINVAL;
192
193 count = len / sizeof(struct pirq_regmap);
194 if (count < priv->link_num) {
195 printf("Number of pirq-regmap entires is wrong\n");
196 return -EINVAL;
197 }
198
199 count = priv->link_num;
200 priv->regmap = calloc(count, sizeof(struct pirq_regmap));
201 if (!priv->regmap)
202 return -ENOMEM;
203
204 priv->has_regmap = true;
205 map = priv->regmap;
206 for (i = 0; i < count; i++) {
207 map->link = fdt_addr_to_cpu(cell[0]);
208 map->offset = fdt_addr_to_cpu(cell[1]);
209
210 cell += sizeof(struct pirq_regmap) / sizeof(u32);
211 map++;
212 }
213 }
214
Bin Mengb46c2082016-02-01 01:40:51 -0800215 priv->irq_mask = fdtdec_get_int(blob, node,
216 "intel,pirq-mask", PIRQ_BITMAP);
Bin Meng9c7dea62015-05-25 22:35:04 +0800217
Bin Meng07ac84e2016-05-07 07:46:13 -0700218 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
219 /* Reserve IRQ9 for SCI */
220 priv->irq_mask &= ~(1 << 9);
221 }
222
Bin Mengb46c2082016-02-01 01:40:51 -0800223 if (priv->config == PIRQ_VIA_IBASE) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800224 int ibase_off;
225
226 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
227 if (!ibase_off)
228 return -EINVAL;
229
230 /*
231 * Here we assume that the IBASE register has already been
232 * properly configured by U-Boot before.
233 *
234 * By 'valid' we mean:
235 * 1) a valid memory space carved within system memory space
236 * assigned to IBASE register block.
237 * 2) memory range decoding is enabled.
238 * Hence we don't do any santify test here.
239 */
Bin Meng248c4fa2016-02-01 01:40:52 -0800240 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
Bin Mengb46c2082016-02-01 01:40:51 -0800241 priv->ibase &= ~0xf;
Bin Meng9c7dea62015-05-25 22:35:04 +0800242 }
243
Bin Mengd4e61f52016-05-07 07:46:14 -0700244 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
245 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
246
Bin Meng9c7dea62015-05-25 22:35:04 +0800247 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600248 if (!cell || len % sizeof(struct pirq_routing))
Bin Meng9c7dea62015-05-25 22:35:04 +0800249 return -EINVAL;
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600250 count = len / sizeof(struct pirq_routing);
Bin Meng9c7dea62015-05-25 22:35:04 +0800251
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600252 rt = calloc(1, sizeof(struct irq_routing_table));
Bin Meng9c7dea62015-05-25 22:35:04 +0800253 if (!rt)
254 return -ENOMEM;
Bin Meng9c7dea62015-05-25 22:35:04 +0800255
256 /* Populate the PIRQ table fields */
257 rt->signature = PIRQ_SIGNATURE;
258 rt->version = PIRQ_VERSION;
Bin Mengb46c2082016-02-01 01:40:51 -0800259 rt->rtr_bus = PCI_BUS(priv->bdf);
260 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
Bin Meng9c7dea62015-05-25 22:35:04 +0800261 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
262 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
263
Bin Mengdf817492015-06-23 12:18:47 +0800264 slot_base = rt->slots;
Bin Meng9c7dea62015-05-25 22:35:04 +0800265
266 /* Now fill in the irq_info entries in the PIRQ table */
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600267 for (i = 0; i < count;
268 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800269 struct pirq_routing pr;
270
271 pr.bdf = fdt_addr_to_cpu(cell[0]);
272 pr.pin = fdt_addr_to_cpu(cell[1]);
273 pr.pirq = fdt_addr_to_cpu(cell[2]);
274
275 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
276 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
277 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
278 'A' + pr.pirq);
Bin Mengdf817492015-06-23 12:18:47 +0800279
280 slot = check_dup_entry(slot_base, irq_entries,
281 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
282 if (slot) {
283 debug("found entry for bus %d device %d, ",
284 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
285
286 if (slot->irq[pr.pin - 1].link) {
287 debug("skipping\n");
288
289 /*
290 * Sanity test on the routed PIRQ pin
291 *
292 * If they don't match, show a warning to tell
293 * there might be something wrong with the PIRQ
294 * routing information in the device tree.
295 */
296 if (slot->irq[pr.pin - 1].link !=
Bin Meng51050ff2018-06-12 01:26:46 -0700297 pirq_linkno_to_reg(priv, pr.pirq))
Bin Mengdf817492015-06-23 12:18:47 +0800298 debug("WARNING: Inconsistent PIRQ routing information\n");
Bin Mengdf817492015-06-23 12:18:47 +0800299 continue;
300 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600301 } else {
302 slot = slot_base + irq_entries++;
Bin Mengdf817492015-06-23 12:18:47 +0800303 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600304 debug("writing INT%c\n", 'A' + pr.pin - 1);
Bin Mengb46c2082016-02-01 01:40:51 -0800305 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
306 pr.pin, pr.pirq);
Bin Meng9c7dea62015-05-25 22:35:04 +0800307 }
308
309 rt->size = irq_entries * sizeof(struct irq_info) + 32;
310
Bin Meng10d569e2016-05-11 07:44:57 -0700311 /* Fix up the table checksum */
312 rt->checksum = table_compute_checksum(rt, rt->size);
313
Simon Glass1bff8362017-01-16 07:04:16 -0700314 gd->arch.pirq_routing_table = rt;
Bin Meng9c7dea62015-05-25 22:35:04 +0800315
316 return 0;
317}
318
Bin Mengd4e61f52016-05-07 07:46:14 -0700319static void irq_enable_sci(struct udevice *dev)
320{
321 struct irq_router *priv = dev_get_priv(dev);
322
323 if (priv->actl_8bit) {
324 /* Bit7 must be turned on to enable ACPI */
325 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
326 } else {
327 /* Write 0 to enable SCI on IRQ9 */
328 if (priv->config == PIRQ_VIA_PCI)
329 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
330 else
Bin Meng63767072017-01-18 03:32:56 -0800331 writel(0, (uintptr_t)priv->ibase + priv->actl_addr);
Bin Mengd4e61f52016-05-07 07:46:14 -0700332 }
333}
334
Bin Mengbc728b12018-06-03 19:04:22 -0700335int irq_router_probe(struct udevice *dev)
Simon Glasse76187a2016-01-19 21:32:25 -0700336{
Simon Glass7e4be122015-08-10 07:05:08 -0600337 int ret;
338
Simon Glassb565d662016-01-19 21:32:28 -0700339 ret = create_pirq_routing_table(dev);
Simon Glass7e4be122015-08-10 07:05:08 -0600340 if (ret) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800341 debug("Failed to create pirq routing table\n");
Simon Glass7e4be122015-08-10 07:05:08 -0600342 return ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800343 }
Simon Glass7e4be122015-08-10 07:05:08 -0600344 /* Route PIRQ */
Simon Glass1bff8362017-01-16 07:04:16 -0700345 pirq_route_irqs(dev, gd->arch.pirq_routing_table->slots,
346 get_irq_slot_count(gd->arch.pirq_routing_table));
Simon Glass7e4be122015-08-10 07:05:08 -0600347
Bin Mengd4e61f52016-05-07 07:46:14 -0700348 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
349 irq_enable_sci(dev);
350
Simon Glass7e4be122015-08-10 07:05:08 -0600351 return 0;
Bin Meng9c7dea62015-05-25 22:35:04 +0800352}
353
Simon Glasse76187a2016-01-19 21:32:25 -0700354static const struct udevice_id irq_router_ids[] = {
Simon Glass69c2dc92020-02-06 09:54:58 -0700355 { .compatible = "intel,irq-router", .data = X86_IRQT_BASE },
Simon Glasse76187a2016-01-19 21:32:25 -0700356 { }
357};
358
359U_BOOT_DRIVER(irq_router_drv) = {
360 .name = "intel_irq",
361 .id = UCLASS_IRQ,
362 .of_match = irq_router_ids,
363 .probe = irq_router_probe,
Bin Mengb46c2082016-02-01 01:40:51 -0800364 .priv_auto_alloc_size = sizeof(struct irq_router),
Simon Glasse76187a2016-01-19 21:32:25 -0700365};