blob: cb183496b7a9cfeba89048c2c62131d545e12cb6 [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>
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>
Bin Meng3371c0b2016-05-11 07:44:57 -070015#include <asm/tables.h>
Bin Meng51c3b1e2015-05-25 22:35:04 +080016
17DECLARE_GLOBAL_DATA_PTR;
18
Bin Mengd803f542018-06-12 01:26:46 -070019/**
20 * pirq_reg_to_linkno() - Convert a PIRQ routing register offset to link number
21 *
22 * @priv: IRQ router driver's priv data
23 * @reg: PIRQ routing register offset from the base address
24 * @return: PIRQ link number (0 for PIRQA, 1 for PIRQB, etc)
25 */
26static inline int pirq_reg_to_linkno(struct irq_router *priv, int reg)
27{
28 int linkno = 0;
29
30 if (priv->has_regmap) {
31 struct pirq_regmap *map = priv->regmap;
32 int i;
33
34 for (i = 0; i < priv->link_num; i++) {
35 if (reg - priv->link_base == map->offset) {
36 linkno = map->link;
37 break;
38 }
39 map++;
40 }
41 } else {
42 linkno = reg - priv->link_base;
43 }
44
45 return linkno;
46}
47
48/**
49 * pirq_linkno_to_reg() - Convert a PIRQ link number to routing register offset
50 *
51 * @priv: IRQ router driver's priv data
52 * @linkno: PIRQ link number (0 for PIRQA, 1 for PIRQB, etc)
53 * @return: PIRQ routing register offset from the base address
54 */
55static inline int pirq_linkno_to_reg(struct irq_router *priv, int linkno)
56{
57 int reg = 0;
58
59 if (priv->has_regmap) {
60 struct pirq_regmap *map = priv->regmap;
61 int i;
62
63 for (i = 0; i < priv->link_num; i++) {
64 if (linkno == map->link) {
65 reg = map->offset + priv->link_base;
66 break;
67 }
68 map++;
69 }
70 } else {
71 reg = linkno + priv->link_base;
72 }
73
74 return reg;
75}
76
Bin Menga5a20032016-02-01 01:40:51 -080077bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
Bin Meng51c3b1e2015-05-25 22:35:04 +080078{
Bin Menga5a20032016-02-01 01:40:51 -080079 struct irq_router *priv = dev_get_priv(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +080080 u8 pirq;
Bin Meng51c3b1e2015-05-25 22:35:04 +080081
Bin Menga5a20032016-02-01 01:40:51 -080082 if (priv->config == PIRQ_VIA_PCI)
Bin Meng1defbb12018-06-03 19:04:23 -070083 dm_pci_read_config8(dev->parent,
Bin Mengd803f542018-06-12 01:26:46 -070084 pirq_linkno_to_reg(priv, link), &pirq);
Bin Meng51c3b1e2015-05-25 22:35:04 +080085 else
Bin Meng1defbb12018-06-03 19:04:23 -070086 pirq = readb((uintptr_t)priv->ibase +
Bin Mengd803f542018-06-12 01:26:46 -070087 pirq_linkno_to_reg(priv, link));
Bin Meng51c3b1e2015-05-25 22:35:04 +080088
89 pirq &= 0xf;
90
91 /* IRQ# 0/1/2/8/13 are reserved */
92 if (pirq < 3 || pirq == 8 || pirq == 13)
93 return false;
94
95 return pirq == irq ? true : false;
96}
97
Bin Menga5a20032016-02-01 01:40:51 -080098int pirq_translate_link(struct udevice *dev, int link)
Bin Meng51c3b1e2015-05-25 22:35:04 +080099{
Bin Menga5a20032016-02-01 01:40:51 -0800100 struct irq_router *priv = dev_get_priv(dev);
101
Bin Mengd803f542018-06-12 01:26:46 -0700102 return pirq_reg_to_linkno(priv, link);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800103}
104
Bin Menga5a20032016-02-01 01:40:51 -0800105void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800106{
Bin Menga5a20032016-02-01 01:40:51 -0800107 struct irq_router *priv = dev_get_priv(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800108
109 /* IRQ# 0/1/2/8/13 are reserved */
110 if (irq < 3 || irq == 8 || irq == 13)
111 return;
112
Bin Menga5a20032016-02-01 01:40:51 -0800113 if (priv->config == PIRQ_VIA_PCI)
Bin Meng1defbb12018-06-03 19:04:23 -0700114 dm_pci_write_config8(dev->parent,
Bin Mengd803f542018-06-12 01:26:46 -0700115 pirq_linkno_to_reg(priv, link), irq);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800116 else
Bin Meng1defbb12018-06-03 19:04:23 -0700117 writeb(irq, (uintptr_t)priv->ibase +
Bin Mengd803f542018-06-12 01:26:46 -0700118 pirq_linkno_to_reg(priv, link));
Bin Meng51c3b1e2015-05-25 22:35:04 +0800119}
120
Bin Meng16758a32015-06-23 12:18:47 +0800121static struct irq_info *check_dup_entry(struct irq_info *slot_base,
122 int entry_num, int bus, int device)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800123{
Bin Meng16758a32015-06-23 12:18:47 +0800124 struct irq_info *slot = slot_base;
125 int i;
126
127 for (i = 0; i < entry_num; i++) {
128 if (slot->bus == bus && slot->devfn == (device << 3))
129 break;
130 slot++;
131 }
Bin Meng51c3b1e2015-05-25 22:35:04 +0800132
Bin Meng16758a32015-06-23 12:18:47 +0800133 return (i == entry_num) ? NULL : slot;
134}
135
Bin Menga5a20032016-02-01 01:40:51 -0800136static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
137 int bus, int device, int pin, int pirq)
Bin Meng16758a32015-06-23 12:18:47 +0800138{
Bin Meng51c3b1e2015-05-25 22:35:04 +0800139 slot->bus = bus;
Bin Meng3a531a32015-06-23 12:18:46 +0800140 slot->devfn = (device << 3) | 0;
Bin Mengd803f542018-06-12 01:26:46 -0700141 slot->irq[pin - 1].link = pirq_linkno_to_reg(priv, pirq);
Bin Menga5a20032016-02-01 01:40:51 -0800142 slot->irq[pin - 1].bitmap = priv->irq_mask;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800143}
144
Simon Glassddcafd62016-01-19 21:32:28 -0700145static int create_pirq_routing_table(struct udevice *dev)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800146{
Bin Menga5a20032016-02-01 01:40:51 -0800147 struct irq_router *priv = dev_get_priv(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800148 const void *blob = gd->fdt_blob;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800149 int node;
150 int len, count;
151 const u32 *cell;
Bin Mengd803f542018-06-12 01:26:46 -0700152 struct pirq_regmap *map;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800153 struct irq_routing_table *rt;
Bin Meng16758a32015-06-23 12:18:47 +0800154 struct irq_info *slot, *slot_base;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800155 int irq_entries = 0;
156 int i;
157 int ret;
158
Simon Glassdd79d6e2017-01-17 16:52:55 -0700159 node = dev_of_offset(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800160
161 /* extract the bdf from fdt_pci_addr */
Bin Menga5a20032016-02-01 01:40:51 -0800162 priv->bdf = dm_pci_get_bdf(dev->parent);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800163
Simon Glassb0ea7402016-10-02 17:59:28 -0600164 ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
Bin Meng51c3b1e2015-05-25 22:35:04 +0800165 if (!ret) {
Bin Menga5a20032016-02-01 01:40:51 -0800166 priv->config = PIRQ_VIA_PCI;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800167 } else {
Simon Glassb0ea7402016-10-02 17:59:28 -0600168 ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
169 "ibase");
Bin Meng51c3b1e2015-05-25 22:35:04 +0800170 if (!ret)
Bin Menga5a20032016-02-01 01:40:51 -0800171 priv->config = PIRQ_VIA_IBASE;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800172 else
173 return -EINVAL;
174 }
175
Bin Mengc332fca2018-06-12 01:26:45 -0700176 cell = fdt_getprop(blob, node, "intel,pirq-link", &len);
177 if (!cell || len != 8)
178 return -EINVAL;
179 priv->link_base = fdt_addr_to_cpu(cell[0]);
180 priv->link_num = fdt_addr_to_cpu(cell[1]);
181 if (priv->link_num > CONFIG_MAX_PIRQ_LINKS) {
182 debug("Limiting supported PIRQ link number from %d to %d\n",
183 priv->link_num, CONFIG_MAX_PIRQ_LINKS);
184 priv->link_num = CONFIG_MAX_PIRQ_LINKS;
185 }
Bin Meng51c3b1e2015-05-25 22:35:04 +0800186
Bin Mengd803f542018-06-12 01:26:46 -0700187 cell = fdt_getprop(blob, node, "intel,pirq-regmap", &len);
188 if (cell) {
189 if (len % sizeof(struct pirq_regmap))
190 return -EINVAL;
191
192 count = len / sizeof(struct pirq_regmap);
193 if (count < priv->link_num) {
194 printf("Number of pirq-regmap entires is wrong\n");
195 return -EINVAL;
196 }
197
198 count = priv->link_num;
199 priv->regmap = calloc(count, sizeof(struct pirq_regmap));
200 if (!priv->regmap)
201 return -ENOMEM;
202
203 priv->has_regmap = true;
204 map = priv->regmap;
205 for (i = 0; i < count; i++) {
206 map->link = fdt_addr_to_cpu(cell[0]);
207 map->offset = fdt_addr_to_cpu(cell[1]);
208
209 cell += sizeof(struct pirq_regmap) / sizeof(u32);
210 map++;
211 }
212 }
213
Bin Menga5a20032016-02-01 01:40:51 -0800214 priv->irq_mask = fdtdec_get_int(blob, node,
215 "intel,pirq-mask", PIRQ_BITMAP);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800216
Bin Meng61ad3712016-05-07 07:46:13 -0700217 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
218 /* Reserve IRQ9 for SCI */
219 priv->irq_mask &= ~(1 << 9);
220 }
221
Bin Menga5a20032016-02-01 01:40:51 -0800222 if (priv->config == PIRQ_VIA_IBASE) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800223 int ibase_off;
224
225 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
226 if (!ibase_off)
227 return -EINVAL;
228
229 /*
230 * Here we assume that the IBASE register has already been
231 * properly configured by U-Boot before.
232 *
233 * By 'valid' we mean:
234 * 1) a valid memory space carved within system memory space
235 * assigned to IBASE register block.
236 * 2) memory range decoding is enabled.
237 * Hence we don't do any santify test here.
238 */
Bin Mengbfe20b72016-02-01 01:40:52 -0800239 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
Bin Menga5a20032016-02-01 01:40:51 -0800240 priv->ibase &= ~0xf;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800241 }
242
Bin Mengc3b03ea2016-05-07 07:46:14 -0700243 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
244 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
245
Bin Meng51c3b1e2015-05-25 22:35:04 +0800246 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600247 if (!cell || len % sizeof(struct pirq_routing))
Bin Meng51c3b1e2015-05-25 22:35:04 +0800248 return -EINVAL;
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600249 count = len / sizeof(struct pirq_routing);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800250
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600251 rt = calloc(1, sizeof(struct irq_routing_table));
Bin Meng51c3b1e2015-05-25 22:35:04 +0800252 if (!rt)
253 return -ENOMEM;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800254
255 /* Populate the PIRQ table fields */
256 rt->signature = PIRQ_SIGNATURE;
257 rt->version = PIRQ_VERSION;
Bin Menga5a20032016-02-01 01:40:51 -0800258 rt->rtr_bus = PCI_BUS(priv->bdf);
259 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800260 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
261 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
262
Bin Meng16758a32015-06-23 12:18:47 +0800263 slot_base = rt->slots;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800264
265 /* Now fill in the irq_info entries in the PIRQ table */
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600266 for (i = 0; i < count;
267 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800268 struct pirq_routing pr;
269
270 pr.bdf = fdt_addr_to_cpu(cell[0]);
271 pr.pin = fdt_addr_to_cpu(cell[1]);
272 pr.pirq = fdt_addr_to_cpu(cell[2]);
273
274 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
275 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
276 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
277 'A' + pr.pirq);
Bin Meng16758a32015-06-23 12:18:47 +0800278
279 slot = check_dup_entry(slot_base, irq_entries,
280 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
281 if (slot) {
282 debug("found entry for bus %d device %d, ",
283 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
284
285 if (slot->irq[pr.pin - 1].link) {
286 debug("skipping\n");
287
288 /*
289 * Sanity test on the routed PIRQ pin
290 *
291 * If they don't match, show a warning to tell
292 * there might be something wrong with the PIRQ
293 * routing information in the device tree.
294 */
295 if (slot->irq[pr.pin - 1].link !=
Bin Mengd803f542018-06-12 01:26:46 -0700296 pirq_linkno_to_reg(priv, pr.pirq))
Bin Meng16758a32015-06-23 12:18:47 +0800297 debug("WARNING: Inconsistent PIRQ routing information\n");
Bin Meng16758a32015-06-23 12:18:47 +0800298 continue;
299 }
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600300 } else {
301 slot = slot_base + irq_entries++;
Bin Meng16758a32015-06-23 12:18:47 +0800302 }
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600303 debug("writing INT%c\n", 'A' + pr.pin - 1);
Bin Menga5a20032016-02-01 01:40:51 -0800304 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
305 pr.pin, pr.pirq);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800306 }
307
308 rt->size = irq_entries * sizeof(struct irq_info) + 32;
309
Bin Meng3371c0b2016-05-11 07:44:57 -0700310 /* Fix up the table checksum */
311 rt->checksum = table_compute_checksum(rt, rt->size);
312
Simon Glassf64d6f72017-01-16 07:04:16 -0700313 gd->arch.pirq_routing_table = rt;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800314
315 return 0;
316}
317
Bin Mengc3b03ea2016-05-07 07:46:14 -0700318static void irq_enable_sci(struct udevice *dev)
319{
320 struct irq_router *priv = dev_get_priv(dev);
321
322 if (priv->actl_8bit) {
323 /* Bit7 must be turned on to enable ACPI */
324 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
325 } else {
326 /* Write 0 to enable SCI on IRQ9 */
327 if (priv->config == PIRQ_VIA_PCI)
328 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
329 else
Bin Meng95e4a392017-01-18 03:32:56 -0800330 writel(0, (uintptr_t)priv->ibase + priv->actl_addr);
Bin Mengc3b03ea2016-05-07 07:46:14 -0700331 }
332}
333
Bin Meng0c9f5942018-06-03 19:04:22 -0700334int irq_router_probe(struct udevice *dev)
Simon Glass18a8e092016-01-19 21:32:25 -0700335{
Simon Glassaf1c2d682015-08-10 07:05:08 -0600336 int ret;
337
Simon Glassddcafd62016-01-19 21:32:28 -0700338 ret = create_pirq_routing_table(dev);
Simon Glassaf1c2d682015-08-10 07:05:08 -0600339 if (ret) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800340 debug("Failed to create pirq routing table\n");
Simon Glassaf1c2d682015-08-10 07:05:08 -0600341 return ret;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800342 }
Simon Glassaf1c2d682015-08-10 07:05:08 -0600343 /* Route PIRQ */
Simon Glassf64d6f72017-01-16 07:04:16 -0700344 pirq_route_irqs(dev, gd->arch.pirq_routing_table->slots,
345 get_irq_slot_count(gd->arch.pirq_routing_table));
Simon Glassaf1c2d682015-08-10 07:05:08 -0600346
Bin Mengc3b03ea2016-05-07 07:46:14 -0700347 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
348 irq_enable_sci(dev);
349
Simon Glassaf1c2d682015-08-10 07:05:08 -0600350 return 0;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800351}
352
Simon Glassca37a392017-01-16 07:03:35 -0700353ulong write_pirq_routing_table(ulong addr)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800354{
Simon Glassf64d6f72017-01-16 07:04:16 -0700355 if (!gd->arch.pirq_routing_table)
Bin Meng4a6da302015-05-25 22:35:07 +0800356 return addr;
357
Simon Glassf64d6f72017-01-16 07:04:16 -0700358 return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800359}
Simon Glass18a8e092016-01-19 21:32:25 -0700360
361static const struct udevice_id irq_router_ids[] = {
362 { .compatible = "intel,irq-router" },
363 { }
364};
365
366U_BOOT_DRIVER(irq_router_drv) = {
367 .name = "intel_irq",
368 .id = UCLASS_IRQ,
369 .of_match = irq_router_ids,
370 .probe = irq_router_probe,
Bin Menga5a20032016-02-01 01:40:51 -0800371 .priv_auto_alloc_size = sizeof(struct irq_router),
Simon Glass18a8e092016-01-19 21:32:25 -0700372};