blob: df3cd0abc7771bef450f70c458ad13613d2162e4 [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>
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 Meng9c7dea62015-05-25 22:35:04 +080020static struct irq_routing_table *pirq_routing_table;
21
Bin Mengb46c2082016-02-01 01:40:51 -080022bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
Bin Meng9c7dea62015-05-25 22:35:04 +080023{
Bin Mengb46c2082016-02-01 01:40:51 -080024 struct irq_router *priv = dev_get_priv(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +080025 u8 pirq;
Bin Mengb46c2082016-02-01 01:40:51 -080026 int base = priv->link_base;
Bin Meng9c7dea62015-05-25 22:35:04 +080027
Bin Mengb46c2082016-02-01 01:40:51 -080028 if (priv->config == PIRQ_VIA_PCI)
Bin Meng248c4fa2016-02-01 01:40:52 -080029 dm_pci_read_config8(dev->parent, LINK_N2V(link, base), &pirq);
Bin Meng9c7dea62015-05-25 22:35:04 +080030 else
Bin Mengb46c2082016-02-01 01:40:51 -080031 pirq = readb(priv->ibase + LINK_N2V(link, base));
Bin Meng9c7dea62015-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 Mengb46c2082016-02-01 01:40:51 -080042int pirq_translate_link(struct udevice *dev, int link)
Bin Meng9c7dea62015-05-25 22:35:04 +080043{
Bin Mengb46c2082016-02-01 01:40:51 -080044 struct irq_router *priv = dev_get_priv(dev);
45
46 return LINK_V2N(link, priv->link_base);
Bin Meng9c7dea62015-05-25 22:35:04 +080047}
48
Bin Mengb46c2082016-02-01 01:40:51 -080049void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
Bin Meng9c7dea62015-05-25 22:35:04 +080050{
Bin Mengb46c2082016-02-01 01:40:51 -080051 struct irq_router *priv = dev_get_priv(dev);
52 int base = priv->link_base;
Bin Meng9c7dea62015-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 Mengb46c2082016-02-01 01:40:51 -080058 if (priv->config == PIRQ_VIA_PCI)
Bin Meng248c4fa2016-02-01 01:40:52 -080059 dm_pci_write_config8(dev->parent, LINK_N2V(link, base), irq);
Bin Meng9c7dea62015-05-25 22:35:04 +080060 else
Bin Mengb46c2082016-02-01 01:40:51 -080061 writeb(irq, priv->ibase + LINK_N2V(link, base));
Bin Meng9c7dea62015-05-25 22:35:04 +080062}
63
Bin Mengdf817492015-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 Meng9c7dea62015-05-25 22:35:04 +080066{
Bin Mengdf817492015-06-23 12:18:47 +080067 struct irq_info *slot = slot_base;
68 int i;
Bin Meng9c7dea62015-05-25 22:35:04 +080069
Bin Mengdf817492015-06-23 12:18:47 +080070 for (i = 0; i < entry_num; i++) {
71 if (slot->bus == bus && slot->devfn == (device << 3))
72 break;
73 slot++;
74 }
75
76 return (i == entry_num) ? NULL : slot;
77}
78
Bin Mengb46c2082016-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 Mengdf817492015-06-23 12:18:47 +080081{
Bin Meng9c7dea62015-05-25 22:35:04 +080082 slot->bus = bus;
Bin Meng8c38e4d2015-06-23 12:18:46 +080083 slot->devfn = (device << 3) | 0;
Bin Mengb46c2082016-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 Meng9c7dea62015-05-25 22:35:04 +080086}
87
Simon Glassb565d662016-01-19 21:32:28 -070088static int create_pirq_routing_table(struct udevice *dev)
Bin Meng9c7dea62015-05-25 22:35:04 +080089{
Bin Mengb46c2082016-02-01 01:40:51 -080090 struct irq_router *priv = dev_get_priv(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +080091 const void *blob = gd->fdt_blob;
Bin Meng9c7dea62015-05-25 22:35:04 +080092 int node;
93 int len, count;
94 const u32 *cell;
95 struct irq_routing_table *rt;
Bin Mengdf817492015-06-23 12:18:47 +080096 struct irq_info *slot, *slot_base;
Bin Meng9c7dea62015-05-25 22:35:04 +080097 int irq_entries = 0;
98 int i;
99 int ret;
100
Simon Glassb565d662016-01-19 21:32:28 -0700101 node = dev->of_offset;
Bin Meng9c7dea62015-05-25 22:35:04 +0800102
103 /* extract the bdf from fdt_pci_addr */
Bin Mengb46c2082016-02-01 01:40:51 -0800104 priv->bdf = dm_pci_get_bdf(dev->parent);
Bin Meng9c7dea62015-05-25 22:35:04 +0800105
106 ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
107 if (!ret) {
Bin Mengb46c2082016-02-01 01:40:51 -0800108 priv->config = PIRQ_VIA_PCI;
Bin Meng9c7dea62015-05-25 22:35:04 +0800109 } else {
110 ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
111 if (!ret)
Bin Mengb46c2082016-02-01 01:40:51 -0800112 priv->config = PIRQ_VIA_IBASE;
Bin Meng9c7dea62015-05-25 22:35:04 +0800113 else
114 return -EINVAL;
115 }
116
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600117 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
118 if (ret == -1)
Bin Meng9c7dea62015-05-25 22:35:04 +0800119 return ret;
Bin Mengb46c2082016-02-01 01:40:51 -0800120 priv->link_base = ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800121
Bin Mengb46c2082016-02-01 01:40:51 -0800122 priv->irq_mask = fdtdec_get_int(blob, node,
123 "intel,pirq-mask", PIRQ_BITMAP);
Bin Meng9c7dea62015-05-25 22:35:04 +0800124
Bin Meng07ac84e2016-05-07 07:46:13 -0700125 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
126 /* Reserve IRQ9 for SCI */
127 priv->irq_mask &= ~(1 << 9);
128 }
129
Bin Mengb46c2082016-02-01 01:40:51 -0800130 if (priv->config == PIRQ_VIA_IBASE) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800131 int ibase_off;
132
133 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
134 if (!ibase_off)
135 return -EINVAL;
136
137 /*
138 * Here we assume that the IBASE register has already been
139 * properly configured by U-Boot before.
140 *
141 * By 'valid' we mean:
142 * 1) a valid memory space carved within system memory space
143 * assigned to IBASE register block.
144 * 2) memory range decoding is enabled.
145 * Hence we don't do any santify test here.
146 */
Bin Meng248c4fa2016-02-01 01:40:52 -0800147 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
Bin Mengb46c2082016-02-01 01:40:51 -0800148 priv->ibase &= ~0xf;
Bin Meng9c7dea62015-05-25 22:35:04 +0800149 }
150
Bin Mengd4e61f52016-05-07 07:46:14 -0700151 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
152 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
153
Bin Meng9c7dea62015-05-25 22:35:04 +0800154 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600155 if (!cell || len % sizeof(struct pirq_routing))
Bin Meng9c7dea62015-05-25 22:35:04 +0800156 return -EINVAL;
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600157 count = len / sizeof(struct pirq_routing);
Bin Meng9c7dea62015-05-25 22:35:04 +0800158
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600159 rt = calloc(1, sizeof(struct irq_routing_table));
Bin Meng9c7dea62015-05-25 22:35:04 +0800160 if (!rt)
161 return -ENOMEM;
Bin Meng9c7dea62015-05-25 22:35:04 +0800162
163 /* Populate the PIRQ table fields */
164 rt->signature = PIRQ_SIGNATURE;
165 rt->version = PIRQ_VERSION;
Bin Mengb46c2082016-02-01 01:40:51 -0800166 rt->rtr_bus = PCI_BUS(priv->bdf);
167 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
Bin Meng9c7dea62015-05-25 22:35:04 +0800168 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
169 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
170
Bin Mengdf817492015-06-23 12:18:47 +0800171 slot_base = rt->slots;
Bin Meng9c7dea62015-05-25 22:35:04 +0800172
173 /* Now fill in the irq_info entries in the PIRQ table */
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600174 for (i = 0; i < count;
175 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800176 struct pirq_routing pr;
177
178 pr.bdf = fdt_addr_to_cpu(cell[0]);
179 pr.pin = fdt_addr_to_cpu(cell[1]);
180 pr.pirq = fdt_addr_to_cpu(cell[2]);
181
182 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
183 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
184 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
185 'A' + pr.pirq);
Bin Mengdf817492015-06-23 12:18:47 +0800186
187 slot = check_dup_entry(slot_base, irq_entries,
188 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
189 if (slot) {
190 debug("found entry for bus %d device %d, ",
191 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
192
193 if (slot->irq[pr.pin - 1].link) {
194 debug("skipping\n");
195
196 /*
197 * Sanity test on the routed PIRQ pin
198 *
199 * If they don't match, show a warning to tell
200 * there might be something wrong with the PIRQ
201 * routing information in the device tree.
202 */
203 if (slot->irq[pr.pin - 1].link !=
Bin Mengb46c2082016-02-01 01:40:51 -0800204 LINK_N2V(pr.pirq, priv->link_base))
Bin Mengdf817492015-06-23 12:18:47 +0800205 debug("WARNING: Inconsistent PIRQ routing information\n");
Bin Mengdf817492015-06-23 12:18:47 +0800206 continue;
207 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600208 } else {
209 slot = slot_base + irq_entries++;
Bin Mengdf817492015-06-23 12:18:47 +0800210 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600211 debug("writing INT%c\n", 'A' + pr.pin - 1);
Bin Mengb46c2082016-02-01 01:40:51 -0800212 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
213 pr.pin, pr.pirq);
Bin Meng9c7dea62015-05-25 22:35:04 +0800214 }
215
216 rt->size = irq_entries * sizeof(struct irq_info) + 32;
217
Bin Meng10d569e2016-05-11 07:44:57 -0700218 /* Fix up the table checksum */
219 rt->checksum = table_compute_checksum(rt, rt->size);
220
Bin Meng9c7dea62015-05-25 22:35:04 +0800221 pirq_routing_table = rt;
222
223 return 0;
224}
225
Bin Mengd4e61f52016-05-07 07:46:14 -0700226static void irq_enable_sci(struct udevice *dev)
227{
228 struct irq_router *priv = dev_get_priv(dev);
229
230 if (priv->actl_8bit) {
231 /* Bit7 must be turned on to enable ACPI */
232 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
233 } else {
234 /* Write 0 to enable SCI on IRQ9 */
235 if (priv->config == PIRQ_VIA_PCI)
236 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
237 else
238 writel(0, priv->ibase + priv->actl_addr);
239 }
240}
241
Simon Glassd3b884b2016-01-19 21:32:27 -0700242int irq_router_common_init(struct udevice *dev)
Simon Glasse76187a2016-01-19 21:32:25 -0700243{
Simon Glass7e4be122015-08-10 07:05:08 -0600244 int ret;
245
Simon Glassb565d662016-01-19 21:32:28 -0700246 ret = create_pirq_routing_table(dev);
Simon Glass7e4be122015-08-10 07:05:08 -0600247 if (ret) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800248 debug("Failed to create pirq routing table\n");
Simon Glass7e4be122015-08-10 07:05:08 -0600249 return ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800250 }
Simon Glass7e4be122015-08-10 07:05:08 -0600251 /* Route PIRQ */
Bin Mengb46c2082016-02-01 01:40:51 -0800252 pirq_route_irqs(dev, pirq_routing_table->slots,
Simon Glass7e4be122015-08-10 07:05:08 -0600253 get_irq_slot_count(pirq_routing_table));
254
Bin Mengd4e61f52016-05-07 07:46:14 -0700255 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
256 irq_enable_sci(dev);
257
Simon Glass7e4be122015-08-10 07:05:08 -0600258 return 0;
Bin Meng9c7dea62015-05-25 22:35:04 +0800259}
260
Simon Glassd3b884b2016-01-19 21:32:27 -0700261int irq_router_probe(struct udevice *dev)
262{
263 return irq_router_common_init(dev);
264}
265
Bin Meng9c7dea62015-05-25 22:35:04 +0800266u32 write_pirq_routing_table(u32 addr)
267{
Bin Meng67b24972015-05-25 22:35:07 +0800268 if (!pirq_routing_table)
269 return addr;
270
Bin Meng9c7dea62015-05-25 22:35:04 +0800271 return copy_pirq_routing_table(addr, pirq_routing_table);
272}
Simon Glasse76187a2016-01-19 21:32:25 -0700273
274static const struct udevice_id irq_router_ids[] = {
275 { .compatible = "intel,irq-router" },
276 { }
277};
278
279U_BOOT_DRIVER(irq_router_drv) = {
280 .name = "intel_irq",
281 .id = UCLASS_IRQ,
282 .of_match = irq_router_ids,
283 .probe = irq_router_probe,
Bin Mengb46c2082016-02-01 01:40:51 -0800284 .priv_auto_alloc_size = sizeof(struct irq_router),
Simon Glasse76187a2016-01-19 21:32:25 -0700285};
286
287UCLASS_DRIVER(irq) = {
288 .id = UCLASS_IRQ,
289 .name = "irq",
290};