blob: e3e928bb79bd93a3917e3b1b3c6e77c53a5cbd0f [file] [log] [blame]
Bin Meng51c3b1e2015-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 Glass18a8e092016-01-19 21:32:25 -07008#include <dm.h>
Bin Meng51c3b1e2015-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>
Bin Meng3371c0b2016-05-11 07:44:57 -070016#include <asm/tables.h>
Bin Meng51c3b1e2015-05-25 22:35:04 +080017
18DECLARE_GLOBAL_DATA_PTR;
19
Bin Meng51c3b1e2015-05-25 22:35:04 +080020static struct irq_routing_table *pirq_routing_table;
21
Bin Menga5a20032016-02-01 01:40:51 -080022bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
Bin Meng51c3b1e2015-05-25 22:35:04 +080023{
Bin Menga5a20032016-02-01 01:40:51 -080024 struct irq_router *priv = dev_get_priv(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +080025 u8 pirq;
Bin Menga5a20032016-02-01 01:40:51 -080026 int base = priv->link_base;
Bin Meng51c3b1e2015-05-25 22:35:04 +080027
Bin Menga5a20032016-02-01 01:40:51 -080028 if (priv->config == PIRQ_VIA_PCI)
Bin Mengbfe20b72016-02-01 01:40:52 -080029 dm_pci_read_config8(dev->parent, LINK_N2V(link, base), &pirq);
Bin Meng51c3b1e2015-05-25 22:35:04 +080030 else
Bin Menga5a20032016-02-01 01:40:51 -080031 pirq = readb(priv->ibase + LINK_N2V(link, base));
Bin Meng51c3b1e2015-05-25 22:35:04 +080032
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
Bin Menga5a20032016-02-01 01:40:51 -080042int pirq_translate_link(struct udevice *dev, int link)
Bin Meng51c3b1e2015-05-25 22:35:04 +080043{
Bin Menga5a20032016-02-01 01:40:51 -080044 struct irq_router *priv = dev_get_priv(dev);
45
46 return LINK_V2N(link, priv->link_base);
Bin Meng51c3b1e2015-05-25 22:35:04 +080047}
48
Bin Menga5a20032016-02-01 01:40:51 -080049void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
Bin Meng51c3b1e2015-05-25 22:35:04 +080050{
Bin Menga5a20032016-02-01 01:40:51 -080051 struct irq_router *priv = dev_get_priv(dev);
52 int base = priv->link_base;
Bin Meng51c3b1e2015-05-25 22:35:04 +080053
54 /* IRQ# 0/1/2/8/13 are reserved */
55 if (irq < 3 || irq == 8 || irq == 13)
56 return;
57
Bin Menga5a20032016-02-01 01:40:51 -080058 if (priv->config == PIRQ_VIA_PCI)
Bin Mengbfe20b72016-02-01 01:40:52 -080059 dm_pci_write_config8(dev->parent, LINK_N2V(link, base), irq);
Bin Meng51c3b1e2015-05-25 22:35:04 +080060 else
Bin Menga5a20032016-02-01 01:40:51 -080061 writeb(irq, priv->ibase + LINK_N2V(link, base));
Bin Meng51c3b1e2015-05-25 22:35:04 +080062}
63
Bin Meng16758a32015-06-23 12:18:47 +080064static struct irq_info *check_dup_entry(struct irq_info *slot_base,
65 int entry_num, int bus, int device)
Bin Meng51c3b1e2015-05-25 22:35:04 +080066{
Bin Meng16758a32015-06-23 12:18:47 +080067 struct irq_info *slot = slot_base;
68 int i;
69
70 for (i = 0; i < entry_num; i++) {
71 if (slot->bus == bus && slot->devfn == (device << 3))
72 break;
73 slot++;
74 }
Bin Meng51c3b1e2015-05-25 22:35:04 +080075
Bin Meng16758a32015-06-23 12:18:47 +080076 return (i == entry_num) ? NULL : slot;
77}
78
Bin Menga5a20032016-02-01 01:40:51 -080079static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
80 int bus, int device, int pin, int pirq)
Bin Meng16758a32015-06-23 12:18:47 +080081{
Bin Meng51c3b1e2015-05-25 22:35:04 +080082 slot->bus = bus;
Bin Meng3a531a32015-06-23 12:18:46 +080083 slot->devfn = (device << 3) | 0;
Bin Menga5a20032016-02-01 01:40:51 -080084 slot->irq[pin - 1].link = LINK_N2V(pirq, priv->link_base);
85 slot->irq[pin - 1].bitmap = priv->irq_mask;
Bin Meng51c3b1e2015-05-25 22:35:04 +080086}
87
Simon Glassddcafd62016-01-19 21:32:28 -070088static int create_pirq_routing_table(struct udevice *dev)
Bin Meng51c3b1e2015-05-25 22:35:04 +080089{
Bin Menga5a20032016-02-01 01:40:51 -080090 struct irq_router *priv = dev_get_priv(dev);
Bin Meng51c3b1e2015-05-25 22:35:04 +080091 const void *blob = gd->fdt_blob;
Bin Meng51c3b1e2015-05-25 22:35:04 +080092 int node;
93 int len, count;
94 const u32 *cell;
95 struct irq_routing_table *rt;
Bin Meng16758a32015-06-23 12:18:47 +080096 struct irq_info *slot, *slot_base;
Bin Meng51c3b1e2015-05-25 22:35:04 +080097 int irq_entries = 0;
98 int i;
99 int ret;
100
Simon Glassddcafd62016-01-19 21:32:28 -0700101 node = dev->of_offset;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800102
103 /* extract the bdf from fdt_pci_addr */
Bin Menga5a20032016-02-01 01:40:51 -0800104 priv->bdf = dm_pci_get_bdf(dev->parent);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800105
Simon Glassb0ea7402016-10-02 17:59:28 -0600106 ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
Bin Meng51c3b1e2015-05-25 22:35:04 +0800107 if (!ret) {
Bin Menga5a20032016-02-01 01:40:51 -0800108 priv->config = PIRQ_VIA_PCI;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800109 } else {
Simon Glassb0ea7402016-10-02 17:59:28 -0600110 ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
111 "ibase");
Bin Meng51c3b1e2015-05-25 22:35:04 +0800112 if (!ret)
Bin Menga5a20032016-02-01 01:40:51 -0800113 priv->config = PIRQ_VIA_IBASE;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800114 else
115 return -EINVAL;
116 }
117
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600118 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
119 if (ret == -1)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800120 return ret;
Bin Menga5a20032016-02-01 01:40:51 -0800121 priv->link_base = ret;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800122
Bin Menga5a20032016-02-01 01:40:51 -0800123 priv->irq_mask = fdtdec_get_int(blob, node,
124 "intel,pirq-mask", PIRQ_BITMAP);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800125
Bin Meng61ad3712016-05-07 07:46:13 -0700126 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
127 /* Reserve IRQ9 for SCI */
128 priv->irq_mask &= ~(1 << 9);
129 }
130
Bin Menga5a20032016-02-01 01:40:51 -0800131 if (priv->config == PIRQ_VIA_IBASE) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800132 int ibase_off;
133
134 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
135 if (!ibase_off)
136 return -EINVAL;
137
138 /*
139 * Here we assume that the IBASE register has already been
140 * properly configured by U-Boot before.
141 *
142 * By 'valid' we mean:
143 * 1) a valid memory space carved within system memory space
144 * assigned to IBASE register block.
145 * 2) memory range decoding is enabled.
146 * Hence we don't do any santify test here.
147 */
Bin Mengbfe20b72016-02-01 01:40:52 -0800148 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
Bin Menga5a20032016-02-01 01:40:51 -0800149 priv->ibase &= ~0xf;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800150 }
151
Bin Mengc3b03ea2016-05-07 07:46:14 -0700152 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
153 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
154
Bin Meng51c3b1e2015-05-25 22:35:04 +0800155 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600156 if (!cell || len % sizeof(struct pirq_routing))
Bin Meng51c3b1e2015-05-25 22:35:04 +0800157 return -EINVAL;
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600158 count = len / sizeof(struct pirq_routing);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800159
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600160 rt = calloc(1, sizeof(struct irq_routing_table));
Bin Meng51c3b1e2015-05-25 22:35:04 +0800161 if (!rt)
162 return -ENOMEM;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800163
164 /* Populate the PIRQ table fields */
165 rt->signature = PIRQ_SIGNATURE;
166 rt->version = PIRQ_VERSION;
Bin Menga5a20032016-02-01 01:40:51 -0800167 rt->rtr_bus = PCI_BUS(priv->bdf);
168 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800169 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
170 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
171
Bin Meng16758a32015-06-23 12:18:47 +0800172 slot_base = rt->slots;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800173
174 /* Now fill in the irq_info entries in the PIRQ table */
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600175 for (i = 0; i < count;
176 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800177 struct pirq_routing pr;
178
179 pr.bdf = fdt_addr_to_cpu(cell[0]);
180 pr.pin = fdt_addr_to_cpu(cell[1]);
181 pr.pirq = fdt_addr_to_cpu(cell[2]);
182
183 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
184 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
185 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
186 'A' + pr.pirq);
Bin Meng16758a32015-06-23 12:18:47 +0800187
188 slot = check_dup_entry(slot_base, irq_entries,
189 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
190 if (slot) {
191 debug("found entry for bus %d device %d, ",
192 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
193
194 if (slot->irq[pr.pin - 1].link) {
195 debug("skipping\n");
196
197 /*
198 * Sanity test on the routed PIRQ pin
199 *
200 * If they don't match, show a warning to tell
201 * there might be something wrong with the PIRQ
202 * routing information in the device tree.
203 */
204 if (slot->irq[pr.pin - 1].link !=
Bin Menga5a20032016-02-01 01:40:51 -0800205 LINK_N2V(pr.pirq, priv->link_base))
Bin Meng16758a32015-06-23 12:18:47 +0800206 debug("WARNING: Inconsistent PIRQ routing information\n");
Bin Meng16758a32015-06-23 12:18:47 +0800207 continue;
208 }
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600209 } else {
210 slot = slot_base + irq_entries++;
Bin Meng16758a32015-06-23 12:18:47 +0800211 }
Simon Glass3b1ed8a2015-08-10 07:05:06 -0600212 debug("writing INT%c\n", 'A' + pr.pin - 1);
Bin Menga5a20032016-02-01 01:40:51 -0800213 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
214 pr.pin, pr.pirq);
Bin Meng51c3b1e2015-05-25 22:35:04 +0800215 }
216
217 rt->size = irq_entries * sizeof(struct irq_info) + 32;
218
Bin Meng3371c0b2016-05-11 07:44:57 -0700219 /* Fix up the table checksum */
220 rt->checksum = table_compute_checksum(rt, rt->size);
221
Bin Meng51c3b1e2015-05-25 22:35:04 +0800222 pirq_routing_table = rt;
223
224 return 0;
225}
226
Bin Mengc3b03ea2016-05-07 07:46:14 -0700227static void irq_enable_sci(struct udevice *dev)
228{
229 struct irq_router *priv = dev_get_priv(dev);
230
231 if (priv->actl_8bit) {
232 /* Bit7 must be turned on to enable ACPI */
233 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
234 } else {
235 /* Write 0 to enable SCI on IRQ9 */
236 if (priv->config == PIRQ_VIA_PCI)
237 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
238 else
239 writel(0, priv->ibase + priv->actl_addr);
240 }
241}
242
Simon Glass7da3ca62016-01-19 21:32:27 -0700243int irq_router_common_init(struct udevice *dev)
Simon Glass18a8e092016-01-19 21:32:25 -0700244{
Simon Glassaf1c2d682015-08-10 07:05:08 -0600245 int ret;
246
Simon Glassddcafd62016-01-19 21:32:28 -0700247 ret = create_pirq_routing_table(dev);
Simon Glassaf1c2d682015-08-10 07:05:08 -0600248 if (ret) {
Bin Meng51c3b1e2015-05-25 22:35:04 +0800249 debug("Failed to create pirq routing table\n");
Simon Glassaf1c2d682015-08-10 07:05:08 -0600250 return ret;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800251 }
Simon Glassaf1c2d682015-08-10 07:05:08 -0600252 /* Route PIRQ */
Bin Menga5a20032016-02-01 01:40:51 -0800253 pirq_route_irqs(dev, pirq_routing_table->slots,
Simon Glassaf1c2d682015-08-10 07:05:08 -0600254 get_irq_slot_count(pirq_routing_table));
255
Bin Mengc3b03ea2016-05-07 07:46:14 -0700256 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
257 irq_enable_sci(dev);
258
Simon Glassaf1c2d682015-08-10 07:05:08 -0600259 return 0;
Bin Meng51c3b1e2015-05-25 22:35:04 +0800260}
261
Simon Glass7da3ca62016-01-19 21:32:27 -0700262int irq_router_probe(struct udevice *dev)
263{
264 return irq_router_common_init(dev);
265}
266
Simon Glassca37a392017-01-16 07:03:35 -0700267ulong write_pirq_routing_table(ulong addr)
Bin Meng51c3b1e2015-05-25 22:35:04 +0800268{
Bin Meng4a6da302015-05-25 22:35:07 +0800269 if (!pirq_routing_table)
270 return addr;
271
Bin Meng51c3b1e2015-05-25 22:35:04 +0800272 return copy_pirq_routing_table(addr, pirq_routing_table);
273}
Simon Glass18a8e092016-01-19 21:32:25 -0700274
275static const struct udevice_id irq_router_ids[] = {
276 { .compatible = "intel,irq-router" },
277 { }
278};
279
280U_BOOT_DRIVER(irq_router_drv) = {
281 .name = "intel_irq",
282 .id = UCLASS_IRQ,
283 .of_match = irq_router_ids,
284 .probe = irq_router_probe,
Bin Menga5a20032016-02-01 01:40:51 -0800285 .priv_auto_alloc_size = sizeof(struct irq_router),
Simon Glass18a8e092016-01-19 21:32:25 -0700286};
287
288UCLASS_DRIVER(irq) = {
289 .id = UCLASS_IRQ,
290 .name = "irq",
291};