blob: 305cd3d237e604aa526721919411265ed6a0312f [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>
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 Meng10d569e2016-05-11 07:44:57 -070015#include <asm/tables.h>
Bin Meng9c7dea62015-05-25 22:35:04 +080016
17DECLARE_GLOBAL_DATA_PTR;
18
Bin Mengb46c2082016-02-01 01:40:51 -080019bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
Bin Meng9c7dea62015-05-25 22:35:04 +080020{
Bin Mengb46c2082016-02-01 01:40:51 -080021 struct irq_router *priv = dev_get_priv(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +080022 u8 pirq;
Bin Mengb46c2082016-02-01 01:40:51 -080023 int base = priv->link_base;
Bin Meng9c7dea62015-05-25 22:35:04 +080024
Bin Mengb46c2082016-02-01 01:40:51 -080025 if (priv->config == PIRQ_VIA_PCI)
Bin Meng248c4fa2016-02-01 01:40:52 -080026 dm_pci_read_config8(dev->parent, LINK_N2V(link, base), &pirq);
Bin Meng9c7dea62015-05-25 22:35:04 +080027 else
Bin Meng63767072017-01-18 03:32:56 -080028 pirq = readb((uintptr_t)priv->ibase + LINK_N2V(link, base));
Bin Meng9c7dea62015-05-25 22:35:04 +080029
30 pirq &= 0xf;
31
32 /* IRQ# 0/1/2/8/13 are reserved */
33 if (pirq < 3 || pirq == 8 || pirq == 13)
34 return false;
35
36 return pirq == irq ? true : false;
37}
38
Bin Mengb46c2082016-02-01 01:40:51 -080039int pirq_translate_link(struct udevice *dev, int link)
Bin Meng9c7dea62015-05-25 22:35:04 +080040{
Bin Mengb46c2082016-02-01 01:40:51 -080041 struct irq_router *priv = dev_get_priv(dev);
42
43 return LINK_V2N(link, priv->link_base);
Bin Meng9c7dea62015-05-25 22:35:04 +080044}
45
Bin Mengb46c2082016-02-01 01:40:51 -080046void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
Bin Meng9c7dea62015-05-25 22:35:04 +080047{
Bin Mengb46c2082016-02-01 01:40:51 -080048 struct irq_router *priv = dev_get_priv(dev);
49 int base = priv->link_base;
Bin Meng9c7dea62015-05-25 22:35:04 +080050
51 /* IRQ# 0/1/2/8/13 are reserved */
52 if (irq < 3 || irq == 8 || irq == 13)
53 return;
54
Bin Mengb46c2082016-02-01 01:40:51 -080055 if (priv->config == PIRQ_VIA_PCI)
Bin Meng248c4fa2016-02-01 01:40:52 -080056 dm_pci_write_config8(dev->parent, LINK_N2V(link, base), irq);
Bin Meng9c7dea62015-05-25 22:35:04 +080057 else
Bin Meng63767072017-01-18 03:32:56 -080058 writeb(irq, (uintptr_t)priv->ibase + LINK_N2V(link, base));
Bin Meng9c7dea62015-05-25 22:35:04 +080059}
60
Bin Mengdf817492015-06-23 12:18:47 +080061static struct irq_info *check_dup_entry(struct irq_info *slot_base,
62 int entry_num, int bus, int device)
Bin Meng9c7dea62015-05-25 22:35:04 +080063{
Bin Mengdf817492015-06-23 12:18:47 +080064 struct irq_info *slot = slot_base;
65 int i;
Bin Meng9c7dea62015-05-25 22:35:04 +080066
Bin Mengdf817492015-06-23 12:18:47 +080067 for (i = 0; i < entry_num; i++) {
68 if (slot->bus == bus && slot->devfn == (device << 3))
69 break;
70 slot++;
71 }
72
73 return (i == entry_num) ? NULL : slot;
74}
75
Bin Mengb46c2082016-02-01 01:40:51 -080076static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
77 int bus, int device, int pin, int pirq)
Bin Mengdf817492015-06-23 12:18:47 +080078{
Bin Meng9c7dea62015-05-25 22:35:04 +080079 slot->bus = bus;
Bin Meng8c38e4d2015-06-23 12:18:46 +080080 slot->devfn = (device << 3) | 0;
Bin Mengb46c2082016-02-01 01:40:51 -080081 slot->irq[pin - 1].link = LINK_N2V(pirq, priv->link_base);
82 slot->irq[pin - 1].bitmap = priv->irq_mask;
Bin Meng9c7dea62015-05-25 22:35:04 +080083}
84
Simon Glassb565d662016-01-19 21:32:28 -070085static int create_pirq_routing_table(struct udevice *dev)
Bin Meng9c7dea62015-05-25 22:35:04 +080086{
Bin Mengb46c2082016-02-01 01:40:51 -080087 struct irq_router *priv = dev_get_priv(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +080088 const void *blob = gd->fdt_blob;
Bin Meng9c7dea62015-05-25 22:35:04 +080089 int node;
90 int len, count;
91 const u32 *cell;
92 struct irq_routing_table *rt;
Bin Mengdf817492015-06-23 12:18:47 +080093 struct irq_info *slot, *slot_base;
Bin Meng9c7dea62015-05-25 22:35:04 +080094 int irq_entries = 0;
95 int i;
96 int ret;
97
Simon Glasse160f7d2017-01-17 16:52:55 -070098 node = dev_of_offset(dev);
Bin Meng9c7dea62015-05-25 22:35:04 +080099
100 /* extract the bdf from fdt_pci_addr */
Bin Mengb46c2082016-02-01 01:40:51 -0800101 priv->bdf = dm_pci_get_bdf(dev->parent);
Bin Meng9c7dea62015-05-25 22:35:04 +0800102
Simon Glassb02e4042016-10-02 17:59:28 -0600103 ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
Bin Meng9c7dea62015-05-25 22:35:04 +0800104 if (!ret) {
Bin Mengb46c2082016-02-01 01:40:51 -0800105 priv->config = PIRQ_VIA_PCI;
Bin Meng9c7dea62015-05-25 22:35:04 +0800106 } else {
Simon Glassb02e4042016-10-02 17:59:28 -0600107 ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
108 "ibase");
Bin Meng9c7dea62015-05-25 22:35:04 +0800109 if (!ret)
Bin Mengb46c2082016-02-01 01:40:51 -0800110 priv->config = PIRQ_VIA_IBASE;
Bin Meng9c7dea62015-05-25 22:35:04 +0800111 else
112 return -EINVAL;
113 }
114
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600115 ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
116 if (ret == -1)
Bin Meng9c7dea62015-05-25 22:35:04 +0800117 return ret;
Bin Mengb46c2082016-02-01 01:40:51 -0800118 priv->link_base = ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800119
Bin Mengb46c2082016-02-01 01:40:51 -0800120 priv->irq_mask = fdtdec_get_int(blob, node,
121 "intel,pirq-mask", PIRQ_BITMAP);
Bin Meng9c7dea62015-05-25 22:35:04 +0800122
Bin Meng07ac84e2016-05-07 07:46:13 -0700123 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
124 /* Reserve IRQ9 for SCI */
125 priv->irq_mask &= ~(1 << 9);
126 }
127
Bin Mengb46c2082016-02-01 01:40:51 -0800128 if (priv->config == PIRQ_VIA_IBASE) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800129 int ibase_off;
130
131 ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
132 if (!ibase_off)
133 return -EINVAL;
134
135 /*
136 * Here we assume that the IBASE register has already been
137 * properly configured by U-Boot before.
138 *
139 * By 'valid' we mean:
140 * 1) a valid memory space carved within system memory space
141 * assigned to IBASE register block.
142 * 2) memory range decoding is enabled.
143 * Hence we don't do any santify test here.
144 */
Bin Meng248c4fa2016-02-01 01:40:52 -0800145 dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
Bin Mengb46c2082016-02-01 01:40:51 -0800146 priv->ibase &= ~0xf;
Bin Meng9c7dea62015-05-25 22:35:04 +0800147 }
148
Bin Mengd4e61f52016-05-07 07:46:14 -0700149 priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
150 priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
151
Bin Meng9c7dea62015-05-25 22:35:04 +0800152 cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600153 if (!cell || len % sizeof(struct pirq_routing))
Bin Meng9c7dea62015-05-25 22:35:04 +0800154 return -EINVAL;
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600155 count = len / sizeof(struct pirq_routing);
Bin Meng9c7dea62015-05-25 22:35:04 +0800156
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600157 rt = calloc(1, sizeof(struct irq_routing_table));
Bin Meng9c7dea62015-05-25 22:35:04 +0800158 if (!rt)
159 return -ENOMEM;
Bin Meng9c7dea62015-05-25 22:35:04 +0800160
161 /* Populate the PIRQ table fields */
162 rt->signature = PIRQ_SIGNATURE;
163 rt->version = PIRQ_VERSION;
Bin Mengb46c2082016-02-01 01:40:51 -0800164 rt->rtr_bus = PCI_BUS(priv->bdf);
165 rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
Bin Meng9c7dea62015-05-25 22:35:04 +0800166 rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
167 rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
168
Bin Mengdf817492015-06-23 12:18:47 +0800169 slot_base = rt->slots;
Bin Meng9c7dea62015-05-25 22:35:04 +0800170
171 /* Now fill in the irq_info entries in the PIRQ table */
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600172 for (i = 0; i < count;
173 i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800174 struct pirq_routing pr;
175
176 pr.bdf = fdt_addr_to_cpu(cell[0]);
177 pr.pin = fdt_addr_to_cpu(cell[1]);
178 pr.pirq = fdt_addr_to_cpu(cell[2]);
179
180 debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
181 i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
182 PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
183 'A' + pr.pirq);
Bin Mengdf817492015-06-23 12:18:47 +0800184
185 slot = check_dup_entry(slot_base, irq_entries,
186 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
187 if (slot) {
188 debug("found entry for bus %d device %d, ",
189 PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
190
191 if (slot->irq[pr.pin - 1].link) {
192 debug("skipping\n");
193
194 /*
195 * Sanity test on the routed PIRQ pin
196 *
197 * If they don't match, show a warning to tell
198 * there might be something wrong with the PIRQ
199 * routing information in the device tree.
200 */
201 if (slot->irq[pr.pin - 1].link !=
Bin Mengb46c2082016-02-01 01:40:51 -0800202 LINK_N2V(pr.pirq, priv->link_base))
Bin Mengdf817492015-06-23 12:18:47 +0800203 debug("WARNING: Inconsistent PIRQ routing information\n");
Bin Mengdf817492015-06-23 12:18:47 +0800204 continue;
205 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600206 } else {
207 slot = slot_base + irq_entries++;
Bin Mengdf817492015-06-23 12:18:47 +0800208 }
Simon Glass9e3ff9c2015-08-10 07:05:06 -0600209 debug("writing INT%c\n", 'A' + pr.pin - 1);
Bin Mengb46c2082016-02-01 01:40:51 -0800210 fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
211 pr.pin, pr.pirq);
Bin Meng9c7dea62015-05-25 22:35:04 +0800212 }
213
214 rt->size = irq_entries * sizeof(struct irq_info) + 32;
215
Bin Meng10d569e2016-05-11 07:44:57 -0700216 /* Fix up the table checksum */
217 rt->checksum = table_compute_checksum(rt, rt->size);
218
Simon Glass1bff8362017-01-16 07:04:16 -0700219 gd->arch.pirq_routing_table = rt;
Bin Meng9c7dea62015-05-25 22:35:04 +0800220
221 return 0;
222}
223
Bin Mengd4e61f52016-05-07 07:46:14 -0700224static void irq_enable_sci(struct udevice *dev)
225{
226 struct irq_router *priv = dev_get_priv(dev);
227
228 if (priv->actl_8bit) {
229 /* Bit7 must be turned on to enable ACPI */
230 dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
231 } else {
232 /* Write 0 to enable SCI on IRQ9 */
233 if (priv->config == PIRQ_VIA_PCI)
234 dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
235 else
Bin Meng63767072017-01-18 03:32:56 -0800236 writel(0, (uintptr_t)priv->ibase + priv->actl_addr);
Bin Mengd4e61f52016-05-07 07:46:14 -0700237 }
238}
239
Simon Glassd3b884b2016-01-19 21:32:27 -0700240int irq_router_common_init(struct udevice *dev)
Simon Glasse76187a2016-01-19 21:32:25 -0700241{
Simon Glass7e4be122015-08-10 07:05:08 -0600242 int ret;
243
Simon Glassb565d662016-01-19 21:32:28 -0700244 ret = create_pirq_routing_table(dev);
Simon Glass7e4be122015-08-10 07:05:08 -0600245 if (ret) {
Bin Meng9c7dea62015-05-25 22:35:04 +0800246 debug("Failed to create pirq routing table\n");
Simon Glass7e4be122015-08-10 07:05:08 -0600247 return ret;
Bin Meng9c7dea62015-05-25 22:35:04 +0800248 }
Simon Glass7e4be122015-08-10 07:05:08 -0600249 /* Route PIRQ */
Simon Glass1bff8362017-01-16 07:04:16 -0700250 pirq_route_irqs(dev, gd->arch.pirq_routing_table->slots,
251 get_irq_slot_count(gd->arch.pirq_routing_table));
Simon Glass7e4be122015-08-10 07:05:08 -0600252
Bin Mengd4e61f52016-05-07 07:46:14 -0700253 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
254 irq_enable_sci(dev);
255
Simon Glass7e4be122015-08-10 07:05:08 -0600256 return 0;
Bin Meng9c7dea62015-05-25 22:35:04 +0800257}
258
Simon Glassd3b884b2016-01-19 21:32:27 -0700259int irq_router_probe(struct udevice *dev)
260{
261 return irq_router_common_init(dev);
262}
263
Simon Glass42fd8c12017-01-16 07:03:35 -0700264ulong write_pirq_routing_table(ulong addr)
Bin Meng9c7dea62015-05-25 22:35:04 +0800265{
Simon Glass1bff8362017-01-16 07:04:16 -0700266 if (!gd->arch.pirq_routing_table)
Bin Meng67b24972015-05-25 22:35:07 +0800267 return addr;
268
Simon Glass1bff8362017-01-16 07:04:16 -0700269 return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table);
Bin Meng9c7dea62015-05-25 22:35:04 +0800270}
Simon Glasse76187a2016-01-19 21:32:25 -0700271
272static const struct udevice_id irq_router_ids[] = {
273 { .compatible = "intel,irq-router" },
274 { }
275};
276
277U_BOOT_DRIVER(irq_router_drv) = {
278 .name = "intel_irq",
279 .id = UCLASS_IRQ,
280 .of_match = irq_router_ids,
281 .probe = irq_router_probe,
Bin Mengb46c2082016-02-01 01:40:51 -0800282 .priv_auto_alloc_size = sizeof(struct irq_router),
Simon Glasse76187a2016-01-19 21:32:25 -0700283};
284
285UCLASS_DRIVER(irq) = {
286 .id = UCLASS_IRQ,
287 .name = "irq",
288};