blob: 3bcb459905c451c8ce20c3d7c2cce8c7feb54164 [file] [log] [blame]
Yinghai Lu5aeecaf2008-08-19 20:49:59 -07001#include <linux/interrupt.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -07002#include <linux/dmar.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07003#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07005#include <linux/jiffies.h>
Suresh Siddha20f30972009-08-04 12:07:08 -07006#include <linux/hpet.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07007#include <linux/pci.h>
Suresh Siddhab6fcb332008-07-10 11:16:44 -07008#include <linux/irq.h>
Lv Zheng8b484632013-12-03 08:49:16 +08009#include <linux/intel-iommu.h>
10#include <linux/acpi.h>
Jiang Liub106ee62015-04-13 14:11:32 +080011#include <linux/irqdomain.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070012#include <asm/io_apic.h>
Yinghai Lu17483a12008-12-12 13:14:18 -080013#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053014#include <asm/cpu.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070015#include <asm/irq_remapping.h>
Weidong Hanf007e992009-05-23 00:41:15 +080016#include <asm/pci-direct.h>
Joerg Roedel5e2b9302012-03-30 11:47:05 -070017#include <asm/msidef.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070018
Suresh Siddha8a8f4222012-03-30 11:47:08 -070019#include "irq_remapping.h"
Joerg Roedel736baef2012-03-30 11:47:00 -070020
Feng Wu2705a3d2015-06-09 13:20:32 +080021enum irq_mode {
22 IRQ_REMAPPING,
23 IRQ_POSTING,
24};
25
Joerg Roedeleef93fd2012-03-30 11:46:59 -070026struct ioapic_scope {
27 struct intel_iommu *iommu;
28 unsigned int id;
29 unsigned int bus; /* PCI bus number */
30 unsigned int devfn; /* PCI devfn number */
31};
32
33struct hpet_scope {
34 struct intel_iommu *iommu;
35 u8 id;
36 unsigned int bus;
37 unsigned int devfn;
38};
39
Jiang Liu099c5c02015-04-14 10:29:51 +080040struct irq_2_iommu {
41 struct intel_iommu *iommu;
42 u16 irte_index;
43 u16 sub_handle;
44 u8 irte_mask;
Feng Wu2705a3d2015-06-09 13:20:32 +080045 enum irq_mode mode;
Jiang Liu099c5c02015-04-14 10:29:51 +080046};
47
Jiang Liub106ee62015-04-13 14:11:32 +080048struct intel_ir_data {
49 struct irq_2_iommu irq_2_iommu;
50 struct irte irte_entry;
51 union {
52 struct msi_msg msi_entry;
53 };
54};
55
Joerg Roedeleef93fd2012-03-30 11:46:59 -070056#define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
Jiang Liu13d09b62015-01-07 15:31:37 +080057#define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
Joerg Roedeleef93fd2012-03-30 11:46:59 -070058
Jiang Liu13d09b62015-01-07 15:31:37 +080059static int __read_mostly eim_mode;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070060static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070061static struct hpet_scope ir_hpet[MAX_HPET_TBS];
Chris Wrightd1423d52010-07-20 11:06:49 -070062
Jiang Liu3a5670e2014-02-19 14:07:33 +080063/*
64 * Lock ordering:
65 * ->dmar_global_lock
66 * ->irq_2_ir_lock
67 * ->qi->q_lock
68 * ->iommu->register_lock
69 * Note:
70 * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
71 * in single-threaded environment with interrupt disabled, so no need to tabke
72 * the dmar_global_lock.
73 */
Thomas Gleixner96f8e982011-07-19 16:28:19 +020074static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Jiang Liub106ee62015-04-13 14:11:32 +080075static struct irq_domain_ops intel_ir_domain_ops;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020076
Jiang Liu694835d2014-01-06 14:18:16 +080077static int __init parse_ioapics_under_ir(void);
78
Jiang Liu8dedf4c2015-04-13 14:11:31 +080079static int alloc_irte(struct intel_iommu *iommu, int irq,
80 struct irq_2_iommu *irq_iommu, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070081{
82 struct ir_table *table = iommu->ir_table;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070083 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070084 unsigned long flags;
Dan Carpenter9f4c7442014-01-09 08:32:36 +030085 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070086
Thomas Gleixnerd585d062010-10-10 12:34:27 +020087 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070088 return -1;
89
Suresh Siddhab6fcb332008-07-10 11:16:44 -070090 if (count > 1) {
91 count = __roundup_pow_of_two(count);
92 mask = ilog2(count);
93 }
94
95 if (mask > ecap_max_handle_mask(iommu->ecap)) {
96 printk(KERN_ERR
97 "Requested mask %x exceeds the max invalidation handle"
98 " mask value %Lx\n", mask,
99 ecap_max_handle_mask(iommu->ecap));
100 return -1;
101 }
102
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200103 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800104 index = bitmap_find_free_region(table->bitmap,
105 INTR_REMAP_TABLE_ENTRIES, mask);
106 if (index < 0) {
107 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
108 } else {
Jiang Liu360eb3c52014-01-06 14:18:08 +0800109 irq_iommu->iommu = iommu;
110 irq_iommu->irte_index = index;
111 irq_iommu->sub_handle = 0;
112 irq_iommu->irte_mask = mask;
Feng Wu2705a3d2015-06-09 13:20:32 +0800113 irq_iommu->mode = IRQ_REMAPPING;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800114 }
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200115 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700116
117 return index;
118}
119
Yu Zhao704126a2009-01-04 16:28:52 +0800120static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700121{
122 struct qi_desc desc;
123
124 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
125 | QI_IEC_SELECTIVE;
126 desc.high = 0;
127
Yu Zhao704126a2009-01-04 16:28:52 +0800128 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700129}
130
Jiang Liu8dedf4c2015-04-13 14:11:31 +0800131static int modify_irte(struct irq_2_iommu *irq_iommu,
132 struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700133{
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700134 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700135 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200136 struct irte *irte;
137 int rc, index;
138
139 if (!irq_iommu)
140 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700141
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200142 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700143
Yinghai Lue420dfb2008-08-19 20:50:21 -0700144 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700145
Yinghai Lue420dfb2008-08-19 20:50:21 -0700146 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700147 irte = &iommu->ir_table->base[index];
148
Linus Torvaldsc513b672010-08-06 11:02:31 -0700149 set_64bit(&irte->low, irte_modified->low);
150 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700151 __iommu_flush_cache(iommu, irte, sizeof(*irte));
152
Yu Zhao704126a2009-01-04 16:28:52 +0800153 rc = qi_flush_iec(iommu, index, 0);
Feng Wu2705a3d2015-06-09 13:20:32 +0800154
155 /* Update iommu mode according to the IRTE mode */
156 irq_iommu->mode = irte->pst ? IRQ_POSTING : IRQ_REMAPPING;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200157 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800158
159 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700160}
161
Joerg Roedel263b5e82012-03-30 11:47:06 -0700162static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700163{
164 int i;
165
166 for (i = 0; i < MAX_HPET_TBS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800167 if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
Suresh Siddha20f30972009-08-04 12:07:08 -0700168 return ir_hpet[i].iommu;
169 return NULL;
170}
171
Joerg Roedel263b5e82012-03-30 11:47:06 -0700172static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700173{
174 int i;
175
176 for (i = 0; i < MAX_IO_APICS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800177 if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
Suresh Siddha89027d32008-07-10 11:16:56 -0700178 return ir_ioapic[i].iommu;
179 return NULL;
180}
181
Joerg Roedel263b5e82012-03-30 11:47:06 -0700182static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700183{
184 struct dmar_drhd_unit *drhd;
185
186 drhd = dmar_find_matched_drhd_unit(dev);
187 if (!drhd)
188 return NULL;
189
190 return drhd->iommu;
191}
192
Weidong Hanc4658b42009-05-23 00:41:14 +0800193static int clear_entries(struct irq_2_iommu *irq_iommu)
194{
195 struct irte *start, *entry, *end;
196 struct intel_iommu *iommu;
197 int index;
198
199 if (irq_iommu->sub_handle)
200 return 0;
201
202 iommu = irq_iommu->iommu;
Jiang Liu8dedf4c2015-04-13 14:11:31 +0800203 index = irq_iommu->irte_index;
Weidong Hanc4658b42009-05-23 00:41:14 +0800204
205 start = iommu->ir_table->base + index;
206 end = start + (1 << irq_iommu->irte_mask);
207
208 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700209 set_64bit(&entry->low, 0);
210 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800211 }
Jiang Liu360eb3c52014-01-06 14:18:08 +0800212 bitmap_release_region(iommu->ir_table->bitmap, index,
213 irq_iommu->irte_mask);
Weidong Hanc4658b42009-05-23 00:41:14 +0800214
215 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
216}
217
Weidong Hanf007e992009-05-23 00:41:15 +0800218/*
219 * source validation type
220 */
221#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300222#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800223#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
224
225/*
226 * source-id qualifier
227 */
228#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
229#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
230 * the third least significant bit
231 */
232#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
233 * the second and third least significant bits
234 */
235#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
236 * the least three significant bits
237 */
238
239/*
240 * set SVT, SQ and SID fields of irte to verify
241 * source ids of interrupt requests
242 */
243static void set_irte_sid(struct irte *irte, unsigned int svt,
244 unsigned int sq, unsigned int sid)
245{
Chris Wrightd1423d52010-07-20 11:06:49 -0700246 if (disable_sourceid_checking)
247 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800248 irte->svt = svt;
249 irte->sq = sq;
250 irte->sid = sid;
251}
252
Joerg Roedel263b5e82012-03-30 11:47:06 -0700253static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800254{
255 int i;
256 u16 sid = 0;
257
258 if (!irte)
259 return -1;
260
Jiang Liu3a5670e2014-02-19 14:07:33 +0800261 down_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800262 for (i = 0; i < MAX_IO_APICS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800263 if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
Weidong Hanf007e992009-05-23 00:41:15 +0800264 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
265 break;
266 }
267 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800268 up_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800269
270 if (sid == 0) {
271 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
272 return -1;
273 }
274
Jiang Liu2fe2c602014-01-06 14:18:17 +0800275 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
Weidong Hanf007e992009-05-23 00:41:15 +0800276
277 return 0;
278}
279
Joerg Roedel263b5e82012-03-30 11:47:06 -0700280static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700281{
282 int i;
283 u16 sid = 0;
284
285 if (!irte)
286 return -1;
287
Jiang Liu3a5670e2014-02-19 14:07:33 +0800288 down_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700289 for (i = 0; i < MAX_HPET_TBS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800290 if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700291 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
292 break;
293 }
294 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800295 up_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700296
297 if (sid == 0) {
298 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
299 return -1;
300 }
301
302 /*
303 * Should really use SQ_ALL_16. Some platforms are broken.
304 * While we figure out the right quirks for these broken platforms, use
305 * SQ_13_IGNORE_3 for now.
306 */
307 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
308
309 return 0;
310}
311
Alex Williamson579305f2014-07-03 09:51:43 -0600312struct set_msi_sid_data {
313 struct pci_dev *pdev;
314 u16 alias;
315};
316
317static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
318{
319 struct set_msi_sid_data *data = opaque;
320
321 data->pdev = pdev;
322 data->alias = alias;
323
324 return 0;
325}
326
Joerg Roedel263b5e82012-03-30 11:47:06 -0700327static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800328{
Alex Williamson579305f2014-07-03 09:51:43 -0600329 struct set_msi_sid_data data;
Weidong Hanf007e992009-05-23 00:41:15 +0800330
331 if (!irte || !dev)
332 return -1;
333
Alex Williamson579305f2014-07-03 09:51:43 -0600334 pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
Weidong Hanf007e992009-05-23 00:41:15 +0800335
Alex Williamson579305f2014-07-03 09:51:43 -0600336 /*
337 * DMA alias provides us with a PCI device and alias. The only case
338 * where the it will return an alias on a different bus than the
339 * device is the case of a PCIe-to-PCI bridge, where the alias is for
340 * the subordinate bus. In this case we can only verify the bus.
341 *
342 * If the alias device is on a different bus than our source device
343 * then we have a topology based alias, use it.
344 *
345 * Otherwise, the alias is for a device DMA quirk and we cannot
346 * assume that MSI uses the same requester ID. Therefore use the
347 * original device.
348 */
349 if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
350 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
351 PCI_DEVID(PCI_BUS_NUM(data.alias),
352 dev->bus->number));
353 else if (data.pdev->bus->number != dev->bus->number)
354 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
355 else
356 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
357 PCI_DEVID(dev->bus->number, dev->devfn));
Weidong Hanf007e992009-05-23 00:41:15 +0800358
359 return 0;
360}
361
Suresh Siddha95a02e92012-03-30 11:47:07 -0700362static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700363{
364 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100365 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700366 unsigned long flags;
367
368 addr = virt_to_phys((void *)iommu->ir_table->base);
369
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200370 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700371
372 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
373 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
374
375 /* Set interrupt-remapping table pointer */
Jan Kiszkaf63ef692014-08-11 13:13:25 +0200376 writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700377
378 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
379 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200380 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700381
382 /*
383 * global invalidation of interrupt entry cache before enabling
384 * interrupt-remapping.
385 */
386 qi_global_iec(iommu);
387
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200388 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700389
390 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700391 iommu->gcmd |= DMA_GCMD_IRE;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800392 iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
David Woodhousec416daa2009-05-10 20:30:58 +0100393 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700394
395 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
396 readl, (sts & DMA_GSTS_IRES), sts);
397
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800398 /*
399 * With CFI clear in the Global Command register, we should be
400 * protected from dangerous (i.e. compatibility) interrupts
401 * regardless of x2apic status. Check just to be sure.
402 */
403 if (sts & DMA_GSTS_CFIS)
404 WARN(1, KERN_WARNING
405 "Compatibility-format IRQs enabled despite intr remapping;\n"
406 "you are vulnerable to IRQ injection.\n");
407
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200408 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700409}
410
Jiang Liua7a3dad2014-11-09 22:48:00 +0800411static int intel_setup_irq_remapping(struct intel_iommu *iommu)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700412{
413 struct ir_table *ir_table;
414 struct page *pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800415 unsigned long *bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700416
Jiang Liua7a3dad2014-11-09 22:48:00 +0800417 if (iommu->ir_table)
418 return 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700419
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800420 ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800421 if (!ir_table)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700422 return -ENOMEM;
423
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800424 pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO,
Suresh Siddha824cd752009-10-02 11:01:23 -0700425 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700426 if (!pages) {
Jiang Liu360eb3c52014-01-06 14:18:08 +0800427 pr_err("IR%d: failed to allocate pages of order %d\n",
428 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800429 goto out_free_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700430 }
431
Jiang Liu360eb3c52014-01-06 14:18:08 +0800432 bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
433 sizeof(long), GFP_ATOMIC);
434 if (bitmap == NULL) {
435 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800436 goto out_free_pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800437 }
438
Jiang Liub106ee62015-04-13 14:11:32 +0800439 iommu->ir_domain = irq_domain_add_hierarchy(arch_get_ir_parent_domain(),
440 0, INTR_REMAP_TABLE_ENTRIES,
441 NULL, &intel_ir_domain_ops,
442 iommu);
443 if (!iommu->ir_domain) {
444 pr_err("IR%d: failed to allocate irqdomain\n", iommu->seq_id);
445 goto out_free_bitmap;
446 }
447 iommu->ir_msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
448
Suresh Siddha2ae21012008-07-10 11:16:43 -0700449 ir_table->base = page_address(pages);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800450 ir_table->bitmap = bitmap;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800451 iommu->ir_table = ir_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700452 return 0;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800453
Jiang Liub106ee62015-04-13 14:11:32 +0800454out_free_bitmap:
455 kfree(bitmap);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800456out_free_pages:
457 __free_pages(pages, INTR_REMAP_PAGE_ORDER);
458out_free_table:
459 kfree(ir_table);
460 return -ENOMEM;
461}
462
463static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
464{
465 if (iommu && iommu->ir_table) {
Jiang Liub106ee62015-04-13 14:11:32 +0800466 if (iommu->ir_msi_domain) {
467 irq_domain_remove(iommu->ir_msi_domain);
468 iommu->ir_msi_domain = NULL;
469 }
470 if (iommu->ir_domain) {
471 irq_domain_remove(iommu->ir_domain);
472 iommu->ir_domain = NULL;
473 }
Jiang Liua7a3dad2014-11-09 22:48:00 +0800474 free_pages((unsigned long)iommu->ir_table->base,
475 INTR_REMAP_PAGE_ORDER);
476 kfree(iommu->ir_table->bitmap);
477 kfree(iommu->ir_table);
478 iommu->ir_table = NULL;
479 }
Suresh Siddha2ae21012008-07-10 11:16:43 -0700480}
481
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700482/*
483 * Disable Interrupt Remapping.
484 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700485static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700486{
487 unsigned long flags;
488 u32 sts;
489
490 if (!ecap_ir_support(iommu->ecap))
491 return;
492
Fenghua Yub24696b2009-03-27 14:22:44 -0700493 /*
494 * global invalidation of interrupt entry cache before disabling
495 * interrupt-remapping.
496 */
497 qi_global_iec(iommu);
498
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200499 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700500
501 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
502 if (!(sts & DMA_GSTS_IRES))
503 goto end;
504
505 iommu->gcmd &= ~DMA_GCMD_IRE;
506 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
507
508 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
509 readl, !(sts & DMA_GSTS_IRES), sts);
510
511end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200512 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700513}
514
Suresh Siddha41750d32011-08-23 17:05:18 -0700515static int __init dmar_x2apic_optout(void)
516{
517 struct acpi_table_dmar *dmar;
518 dmar = (struct acpi_table_dmar *)dmar_tbl;
519 if (!dmar || no_x2apic_optout)
520 return 0;
521 return dmar->flags & DMAR_X2APIC_OPT_OUT;
522}
523
Thomas Gleixner11190302015-01-07 15:31:29 +0800524static void __init intel_cleanup_irq_remapping(void)
525{
526 struct dmar_drhd_unit *drhd;
527 struct intel_iommu *iommu;
528
529 for_each_iommu(iommu, drhd) {
530 if (ecap_ir_support(iommu->ecap)) {
531 iommu_disable_irq_remapping(iommu);
532 intel_teardown_irq_remapping(iommu);
533 }
534 }
535
536 if (x2apic_supported())
537 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
538}
539
540static int __init intel_prepare_irq_remapping(void)
541{
542 struct dmar_drhd_unit *drhd;
543 struct intel_iommu *iommu;
544
Jiang Liu2966d952015-01-07 15:31:35 +0800545 if (irq_remap_broken) {
546 printk(KERN_WARNING
547 "This system BIOS has enabled interrupt remapping\n"
548 "on a chipset that contains an erratum making that\n"
549 "feature unstable. To maintain system stability\n"
550 "interrupt remapping is being disabled. Please\n"
551 "contact your BIOS vendor for an update\n");
552 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Jiang Liu2966d952015-01-07 15:31:35 +0800553 return -ENODEV;
554 }
555
Thomas Gleixner11190302015-01-07 15:31:29 +0800556 if (dmar_table_init() < 0)
Jiang Liu2966d952015-01-07 15:31:35 +0800557 return -ENODEV;
558
559 if (!dmar_ir_support())
560 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800561
562 if (parse_ioapics_under_ir() != 1) {
563 printk(KERN_INFO "Not enabling interrupt remapping\n");
564 goto error;
565 }
566
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800567 /* First make sure all IOMMUs support IRQ remapping */
Jiang Liu2966d952015-01-07 15:31:35 +0800568 for_each_iommu(iommu, drhd)
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800569 if (!ecap_ir_support(iommu->ecap))
Thomas Gleixner11190302015-01-07 15:31:29 +0800570 goto error;
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800571
572 /* Do the allocations early */
573 for_each_iommu(iommu, drhd)
574 if (intel_setup_irq_remapping(iommu))
575 goto error;
576
Thomas Gleixner11190302015-01-07 15:31:29 +0800577 return 0;
Jiang Liu2966d952015-01-07 15:31:35 +0800578
Thomas Gleixner11190302015-01-07 15:31:29 +0800579error:
580 intel_cleanup_irq_remapping();
Jiang Liu2966d952015-01-07 15:31:35 +0800581 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800582}
583
Suresh Siddha95a02e92012-03-30 11:47:07 -0700584static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700585{
586 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800587 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100588 bool setup = false;
Suresh Siddha41750d32011-08-23 17:05:18 -0700589 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700590
Thomas Gleixner11190302015-01-07 15:31:29 +0800591 if (x2apic_supported()) {
Suresh Siddha41750d32011-08-23 17:05:18 -0700592 eim = !dmar_x2apic_optout();
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800593 if (!eim)
Fenghua Yu68c1b892015-02-21 13:07:27 -0800594 pr_info("x2apic is disabled because BIOS sets x2apic opt out bit. You can use 'intremap=no_x2apic_optout' to override the BIOS setting.\n");
Suresh Siddha41750d32011-08-23 17:05:18 -0700595 }
596
Jiang Liu7c919772014-01-06 14:18:18 +0800597 for_each_iommu(iommu, drhd) {
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700598 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800599 * If the queued invalidation is already initialized,
600 * shouldn't disable it.
601 */
602 if (iommu->qi)
603 continue;
604
605 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700606 * Clear previous faults.
607 */
608 dmar_fault(-1, iommu);
609
610 /*
611 * Disable intr remapping and queued invalidation, if already
612 * enabled prior to OS handover.
613 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700614 iommu_disable_irq_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700615
616 dmar_disable_qi(iommu);
617 }
618
Suresh Siddha2ae21012008-07-10 11:16:43 -0700619 /*
620 * check for the Interrupt-remapping support
621 */
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800622 for_each_iommu(iommu, drhd)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700623 if (eim && !ecap_eim_support(iommu->ecap)) {
624 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
625 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
Jiang Liu13d09b62015-01-07 15:31:37 +0800626 eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700627 }
Jiang Liu13d09b62015-01-07 15:31:37 +0800628 eim_mode = eim;
629 if (eim)
630 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700631
632 /*
633 * Enable queued invalidation for all the DRHD's.
634 */
Jiang Liu7c919772014-01-06 14:18:18 +0800635 for_each_iommu(iommu, drhd) {
636 int ret = dmar_enable_qi(iommu);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700637
638 if (ret) {
639 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
640 " invalidation, ecap %Lx, ret %d\n",
641 drhd->reg_base_addr, iommu->ecap, ret);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800642 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700643 }
644 }
645
646 /*
647 * Setup Interrupt-remapping for all the DRHD's now.
648 */
Jiang Liu7c919772014-01-06 14:18:18 +0800649 for_each_iommu(iommu, drhd) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800650 iommu_set_irq_remapping(iommu, eim);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100651 setup = true;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700652 }
653
654 if (!setup)
655 goto error;
656
Suresh Siddha95a02e92012-03-30 11:47:07 -0700657 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200658
Suresh Siddha41750d32011-08-23 17:05:18 -0700659 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700660
Suresh Siddha41750d32011-08-23 17:05:18 -0700661 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700662
663error:
Thomas Gleixner11190302015-01-07 15:31:29 +0800664 intel_cleanup_irq_remapping();
Suresh Siddha2ae21012008-07-10 11:16:43 -0700665 return -1;
666}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700667
Jiang Liua7a3dad2014-11-09 22:48:00 +0800668static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
669 struct intel_iommu *iommu,
670 struct acpi_dmar_hardware_unit *drhd)
Suresh Siddha20f30972009-08-04 12:07:08 -0700671{
672 struct acpi_dmar_pci_path *path;
673 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800674 int count, free = -1;
Suresh Siddha20f30972009-08-04 12:07:08 -0700675
676 bus = scope->bus;
677 path = (struct acpi_dmar_pci_path *)(scope + 1);
678 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
679 / sizeof(struct acpi_dmar_pci_path);
680
681 while (--count > 0) {
682 /*
683 * Access PCI directly due to the PCI
684 * subsystem isn't initialized yet.
685 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800686 bus = read_pci_config_byte(bus, path->device, path->function,
Suresh Siddha20f30972009-08-04 12:07:08 -0700687 PCI_SECONDARY_BUS);
688 path++;
689 }
Jiang Liua7a3dad2014-11-09 22:48:00 +0800690
691 for (count = 0; count < MAX_HPET_TBS; count++) {
692 if (ir_hpet[count].iommu == iommu &&
693 ir_hpet[count].id == scope->enumeration_id)
694 return 0;
695 else if (ir_hpet[count].iommu == NULL && free == -1)
696 free = count;
697 }
698 if (free == -1) {
699 pr_warn("Exceeded Max HPET blocks\n");
700 return -ENOSPC;
701 }
702
703 ir_hpet[free].iommu = iommu;
704 ir_hpet[free].id = scope->enumeration_id;
705 ir_hpet[free].bus = bus;
706 ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
707 pr_info("HPET id %d under DRHD base 0x%Lx\n",
708 scope->enumeration_id, drhd->address);
709
710 return 0;
Suresh Siddha20f30972009-08-04 12:07:08 -0700711}
712
Jiang Liua7a3dad2014-11-09 22:48:00 +0800713static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
714 struct intel_iommu *iommu,
715 struct acpi_dmar_hardware_unit *drhd)
Weidong Hanf007e992009-05-23 00:41:15 +0800716{
717 struct acpi_dmar_pci_path *path;
718 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800719 int count, free = -1;
Weidong Hanf007e992009-05-23 00:41:15 +0800720
721 bus = scope->bus;
722 path = (struct acpi_dmar_pci_path *)(scope + 1);
723 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
724 / sizeof(struct acpi_dmar_pci_path);
725
726 while (--count > 0) {
727 /*
728 * Access PCI directly due to the PCI
729 * subsystem isn't initialized yet.
730 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800731 bus = read_pci_config_byte(bus, path->device, path->function,
Weidong Hanf007e992009-05-23 00:41:15 +0800732 PCI_SECONDARY_BUS);
733 path++;
734 }
735
Jiang Liua7a3dad2014-11-09 22:48:00 +0800736 for (count = 0; count < MAX_IO_APICS; count++) {
737 if (ir_ioapic[count].iommu == iommu &&
738 ir_ioapic[count].id == scope->enumeration_id)
739 return 0;
740 else if (ir_ioapic[count].iommu == NULL && free == -1)
741 free = count;
742 }
743 if (free == -1) {
744 pr_warn("Exceeded Max IO APICS\n");
745 return -ENOSPC;
746 }
747
748 ir_ioapic[free].bus = bus;
749 ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
750 ir_ioapic[free].iommu = iommu;
751 ir_ioapic[free].id = scope->enumeration_id;
752 pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
753 scope->enumeration_id, drhd->address, iommu->seq_id);
754
755 return 0;
Weidong Hanf007e992009-05-23 00:41:15 +0800756}
757
Suresh Siddha20f30972009-08-04 12:07:08 -0700758static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
759 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700760{
Jiang Liua7a3dad2014-11-09 22:48:00 +0800761 int ret = 0;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700762 struct acpi_dmar_hardware_unit *drhd;
763 struct acpi_dmar_device_scope *scope;
764 void *start, *end;
765
766 drhd = (struct acpi_dmar_hardware_unit *)header;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700767 start = (void *)(drhd + 1);
768 end = ((void *)drhd) + header->length;
769
Jiang Liua7a3dad2014-11-09 22:48:00 +0800770 while (start < end && ret == 0) {
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700771 scope = start;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800772 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
773 ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
774 else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
775 ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700776 start += scope->length;
777 }
778
Jiang Liua7a3dad2014-11-09 22:48:00 +0800779 return ret;
780}
781
782static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
783{
784 int i;
785
786 for (i = 0; i < MAX_HPET_TBS; i++)
787 if (ir_hpet[i].iommu == iommu)
788 ir_hpet[i].iommu = NULL;
789
790 for (i = 0; i < MAX_IO_APICS; i++)
791 if (ir_ioapic[i].iommu == iommu)
792 ir_ioapic[i].iommu = NULL;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700793}
794
795/*
796 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
797 * hardware unit.
798 */
Jiang Liu694835d2014-01-06 14:18:16 +0800799static int __init parse_ioapics_under_ir(void)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700800{
801 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800802 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100803 bool ir_supported = false;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500804 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700805
Jiang Liu7c919772014-01-06 14:18:18 +0800806 for_each_iommu(iommu, drhd)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700807 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700808 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700809 return -1;
810
Quentin Lambert2f119c72015-02-06 10:59:53 +0100811 ir_supported = true;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700812 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700813
Seth Forshee32ab31e2012-08-08 08:27:03 -0500814 if (!ir_supported)
815 return 0;
816
817 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
818 int ioapic_id = mpc_ioapic_id(ioapic_idx);
819 if (!map_ioapic_to_ir(ioapic_id)) {
820 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
821 "interrupt remapping will be disabled\n",
822 ioapic_id);
823 return -1;
824 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700825 }
826
Seth Forshee32ab31e2012-08-08 08:27:03 -0500827 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700828}
Fenghua Yub24696b2009-03-27 14:22:44 -0700829
Rashika Kheria6a7885c2013-12-18 12:04:27 +0530830static int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700831{
Jiang Liu3a5670e2014-02-19 14:07:33 +0800832 int ret;
833
Suresh Siddha95a02e92012-03-30 11:47:07 -0700834 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700835 return 0;
836
Jiang Liu3a5670e2014-02-19 14:07:33 +0800837 down_write(&dmar_global_lock);
838 ret = dmar_dev_scope_init();
839 up_write(&dmar_global_lock);
840
841 return ret;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700842}
843rootfs_initcall(ir_dev_scope_init);
844
Suresh Siddha95a02e92012-03-30 11:47:07 -0700845static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700846{
847 struct dmar_drhd_unit *drhd;
848 struct intel_iommu *iommu = NULL;
849
850 /*
851 * Disable Interrupt-remapping for all the DRHD's now.
852 */
853 for_each_iommu(iommu, drhd) {
854 if (!ecap_ir_support(iommu->ecap))
855 continue;
856
Suresh Siddha95a02e92012-03-30 11:47:07 -0700857 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700858 }
859}
860
Suresh Siddha95a02e92012-03-30 11:47:07 -0700861static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700862{
863 struct dmar_drhd_unit *drhd;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100864 bool setup = false;
Fenghua Yub24696b2009-03-27 14:22:44 -0700865 struct intel_iommu *iommu = NULL;
866
867 for_each_iommu(iommu, drhd)
868 if (iommu->qi)
869 dmar_reenable_qi(iommu);
870
871 /*
872 * Setup Interrupt-remapping for all the DRHD's now.
873 */
874 for_each_iommu(iommu, drhd) {
875 if (!ecap_ir_support(iommu->ecap))
876 continue;
877
878 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -0700879 iommu_set_irq_remapping(iommu, eim);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100880 setup = true;
Fenghua Yub24696b2009-03-27 14:22:44 -0700881 }
882
883 if (!setup)
884 goto error;
885
886 return 0;
887
888error:
889 /*
890 * handle error condition gracefully here!
891 */
892 return -1;
893}
894
Jiang Liu3c6e5672015-04-14 10:29:47 +0800895static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700896{
897 memset(irte, 0, sizeof(*irte));
898
899 irte->present = 1;
900 irte->dst_mode = apic->irq_dest_mode;
901 /*
902 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
903 * actual level or edge trigger will be setup in the IO-APIC
904 * RTE. This will help simplify level triggered irq migration.
905 * For more details, see the comments (in io_apic.c) explainig IO-APIC
906 * irq migration in the presence of interrupt-remapping.
907 */
908 irte->trigger_mode = 0;
909 irte->dlvry_mode = apic->irq_delivery_mode;
910 irte->vector = vector;
911 irte->dest_id = IRTE_DEST(dest);
912 irte->redir_hint = 1;
913}
914
Jiang Liub106ee62015-04-13 14:11:32 +0800915static struct irq_domain *intel_get_ir_irq_domain(struct irq_alloc_info *info)
916{
917 struct intel_iommu *iommu = NULL;
918
919 if (!info)
920 return NULL;
921
922 switch (info->type) {
923 case X86_IRQ_ALLOC_TYPE_IOAPIC:
924 iommu = map_ioapic_to_ir(info->ioapic_id);
925 break;
926 case X86_IRQ_ALLOC_TYPE_HPET:
927 iommu = map_hpet_to_ir(info->hpet_id);
928 break;
929 case X86_IRQ_ALLOC_TYPE_MSI:
930 case X86_IRQ_ALLOC_TYPE_MSIX:
931 iommu = map_dev_to_ir(info->msi_dev);
932 break;
933 default:
934 BUG_ON(1);
935 break;
936 }
937
938 return iommu ? iommu->ir_domain : NULL;
939}
940
941static struct irq_domain *intel_get_irq_domain(struct irq_alloc_info *info)
942{
943 struct intel_iommu *iommu;
944
945 if (!info)
946 return NULL;
947
948 switch (info->type) {
949 case X86_IRQ_ALLOC_TYPE_MSI:
950 case X86_IRQ_ALLOC_TYPE_MSIX:
951 iommu = map_dev_to_ir(info->msi_dev);
952 if (iommu)
953 return iommu->ir_msi_domain;
954 break;
955 default:
956 break;
957 }
958
959 return NULL;
960}
961
Joerg Roedel736baef2012-03-30 11:47:00 -0700962struct irq_remap_ops intel_irq_remap_ops = {
Thomas Gleixner11190302015-01-07 15:31:29 +0800963 .prepare = intel_prepare_irq_remapping,
Suresh Siddha95a02e92012-03-30 11:47:07 -0700964 .enable = intel_enable_irq_remapping,
965 .disable = disable_irq_remapping,
966 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700967 .enable_faulting = enable_drhd_fault_handling,
Jiang Liub106ee62015-04-13 14:11:32 +0800968 .get_ir_irq_domain = intel_get_ir_irq_domain,
969 .get_irq_domain = intel_get_irq_domain,
970};
971
972/*
973 * Migrate the IO-APIC irq in the presence of intr-remapping.
974 *
975 * For both level and edge triggered, irq migration is a simple atomic
976 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
977 *
978 * For level triggered, we eliminate the io-apic RTE modification (with the
979 * updated vector information), by using a virtual vector (io-apic pin number).
980 * Real vector that is used for interrupting cpu will be coming from
981 * the interrupt-remapping table entry.
982 *
983 * As the migration is a simple atomic update of IRTE, the same mechanism
984 * is used to migrate MSI irq's in the presence of interrupt-remapping.
985 */
986static int
987intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
988 bool force)
989{
990 struct intel_ir_data *ir_data = data->chip_data;
991 struct irte *irte = &ir_data->irte_entry;
992 struct irq_cfg *cfg = irqd_cfg(data);
993 struct irq_data *parent = data->parent_data;
994 int ret;
995
996 ret = parent->chip->irq_set_affinity(parent, mask, force);
997 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
998 return ret;
999
1000 /*
1001 * Atomically updates the IRTE with the new destination, vector
1002 * and flushes the interrupt entry cache.
1003 */
1004 irte->vector = cfg->vector;
1005 irte->dest_id = IRTE_DEST(cfg->dest_apicid);
Feng Wud75f1522015-06-09 13:20:33 +08001006
1007 /* Update the hardware only if the interrupt is in remapped mode. */
1008 if (ir_data->irq_2_iommu.mode == IRQ_REMAPPING)
1009 modify_irte(&ir_data->irq_2_iommu, irte);
Jiang Liub106ee62015-04-13 14:11:32 +08001010
1011 /*
1012 * After this point, all the interrupts will start arriving
1013 * at the new destination. So, time to cleanup the previous
1014 * vector allocation.
1015 */
Jiang Liuc6c20022015-04-14 10:30:02 +08001016 send_cleanup_vector(cfg);
Jiang Liub106ee62015-04-13 14:11:32 +08001017
1018 return IRQ_SET_MASK_OK_DONE;
1019}
1020
1021static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
1022 struct msi_msg *msg)
1023{
1024 struct intel_ir_data *ir_data = irq_data->chip_data;
1025
1026 *msg = ir_data->msi_entry;
1027}
1028
Feng Wu85411862015-06-09 13:20:31 +08001029static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info)
1030{
1031 struct intel_ir_data *ir_data = data->chip_data;
1032 struct vcpu_data *vcpu_pi_info = info;
1033
1034 /* stop posting interrupts, back to remapping mode */
1035 if (!vcpu_pi_info) {
1036 modify_irte(&ir_data->irq_2_iommu, &ir_data->irte_entry);
1037 } else {
1038 struct irte irte_pi;
1039
1040 /*
1041 * We are not caching the posted interrupt entry. We
1042 * copy the data from the remapped entry and modify
1043 * the fields which are relevant for posted mode. The
1044 * cached remapped entry is used for switching back to
1045 * remapped mode.
1046 */
1047 memset(&irte_pi, 0, sizeof(irte_pi));
1048 dmar_copy_shared_irte(&irte_pi, &ir_data->irte_entry);
1049
1050 /* Update the posted mode fields */
1051 irte_pi.p_pst = 1;
1052 irte_pi.p_urgent = 0;
1053 irte_pi.p_vector = vcpu_pi_info->vector;
1054 irte_pi.pda_l = (vcpu_pi_info->pi_desc_addr >>
1055 (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
1056 irte_pi.pda_h = (vcpu_pi_info->pi_desc_addr >> 32) &
1057 ~(-1UL << PDA_HIGH_BIT);
1058
1059 modify_irte(&ir_data->irq_2_iommu, &irte_pi);
1060 }
1061
1062 return 0;
1063}
1064
Jiang Liub106ee62015-04-13 14:11:32 +08001065static struct irq_chip intel_ir_chip = {
1066 .irq_ack = ir_ack_apic_edge,
1067 .irq_set_affinity = intel_ir_set_affinity,
1068 .irq_compose_msi_msg = intel_ir_compose_msi_msg,
Feng Wu85411862015-06-09 13:20:31 +08001069 .irq_set_vcpu_affinity = intel_ir_set_vcpu_affinity,
Jiang Liub106ee62015-04-13 14:11:32 +08001070};
1071
1072static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
1073 struct irq_cfg *irq_cfg,
1074 struct irq_alloc_info *info,
1075 int index, int sub_handle)
1076{
1077 struct IR_IO_APIC_route_entry *entry;
1078 struct irte *irte = &data->irte_entry;
1079 struct msi_msg *msg = &data->msi_entry;
1080
1081 prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
1082 switch (info->type) {
1083 case X86_IRQ_ALLOC_TYPE_IOAPIC:
1084 /* Set source-id of interrupt request */
1085 set_ioapic_sid(irte, info->ioapic_id);
1086 apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: Set IRTE entry (P:%d FPD:%d Dst_Mode:%d Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X Avail:%X Vector:%02X Dest:%08X SID:%04X SQ:%X SVT:%X)\n",
1087 info->ioapic_id, irte->present, irte->fpd,
1088 irte->dst_mode, irte->redir_hint,
1089 irte->trigger_mode, irte->dlvry_mode,
1090 irte->avail, irte->vector, irte->dest_id,
1091 irte->sid, irte->sq, irte->svt);
1092
1093 entry = (struct IR_IO_APIC_route_entry *)info->ioapic_entry;
1094 info->ioapic_entry = NULL;
1095 memset(entry, 0, sizeof(*entry));
1096 entry->index2 = (index >> 15) & 0x1;
1097 entry->zero = 0;
1098 entry->format = 1;
1099 entry->index = (index & 0x7fff);
1100 /*
1101 * IO-APIC RTE will be configured with virtual vector.
1102 * irq handler will do the explicit EOI to the io-apic.
1103 */
1104 entry->vector = info->ioapic_pin;
1105 entry->mask = 0; /* enable IRQ */
1106 entry->trigger = info->ioapic_trigger;
1107 entry->polarity = info->ioapic_polarity;
1108 if (info->ioapic_trigger)
1109 entry->mask = 1; /* Mask level triggered irqs. */
1110 break;
1111
1112 case X86_IRQ_ALLOC_TYPE_HPET:
1113 case X86_IRQ_ALLOC_TYPE_MSI:
1114 case X86_IRQ_ALLOC_TYPE_MSIX:
1115 if (info->type == X86_IRQ_ALLOC_TYPE_HPET)
1116 set_hpet_sid(irte, info->hpet_id);
1117 else
1118 set_msi_sid(irte, info->msi_dev);
1119
1120 msg->address_hi = MSI_ADDR_BASE_HI;
1121 msg->data = sub_handle;
1122 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1123 MSI_ADDR_IR_SHV |
1124 MSI_ADDR_IR_INDEX1(index) |
1125 MSI_ADDR_IR_INDEX2(index);
1126 break;
1127
1128 default:
1129 BUG_ON(1);
1130 break;
1131 }
1132}
1133
1134static void intel_free_irq_resources(struct irq_domain *domain,
1135 unsigned int virq, unsigned int nr_irqs)
1136{
1137 struct irq_data *irq_data;
1138 struct intel_ir_data *data;
1139 struct irq_2_iommu *irq_iommu;
1140 unsigned long flags;
1141 int i;
1142
1143 for (i = 0; i < nr_irqs; i++) {
1144 irq_data = irq_domain_get_irq_data(domain, virq + i);
1145 if (irq_data && irq_data->chip_data) {
1146 data = irq_data->chip_data;
1147 irq_iommu = &data->irq_2_iommu;
1148 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
1149 clear_entries(irq_iommu);
1150 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
1151 irq_domain_reset_irq_data(irq_data);
1152 kfree(data);
1153 }
1154 }
1155}
1156
1157static int intel_irq_remapping_alloc(struct irq_domain *domain,
1158 unsigned int virq, unsigned int nr_irqs,
1159 void *arg)
1160{
1161 struct intel_iommu *iommu = domain->host_data;
1162 struct irq_alloc_info *info = arg;
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001163 struct intel_ir_data *data, *ird;
Jiang Liub106ee62015-04-13 14:11:32 +08001164 struct irq_data *irq_data;
1165 struct irq_cfg *irq_cfg;
1166 int i, ret, index;
1167
1168 if (!info || !iommu)
1169 return -EINVAL;
1170 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
1171 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
1172 return -EINVAL;
1173
1174 /*
1175 * With IRQ remapping enabled, don't need contiguous CPU vectors
1176 * to support multiple MSI interrupts.
1177 */
1178 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
1179 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
1180
1181 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
1182 if (ret < 0)
1183 return ret;
1184
1185 ret = -ENOMEM;
1186 data = kzalloc(sizeof(*data), GFP_KERNEL);
1187 if (!data)
1188 goto out_free_parent;
1189
1190 down_read(&dmar_global_lock);
1191 index = alloc_irte(iommu, virq, &data->irq_2_iommu, nr_irqs);
1192 up_read(&dmar_global_lock);
1193 if (index < 0) {
1194 pr_warn("Failed to allocate IRTE\n");
1195 kfree(data);
1196 goto out_free_parent;
1197 }
1198
1199 for (i = 0; i < nr_irqs; i++) {
1200 irq_data = irq_domain_get_irq_data(domain, virq + i);
1201 irq_cfg = irqd_cfg(irq_data);
1202 if (!irq_data || !irq_cfg) {
1203 ret = -EINVAL;
1204 goto out_free_data;
1205 }
1206
1207 if (i > 0) {
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001208 ird = kzalloc(sizeof(*ird), GFP_KERNEL);
1209 if (!ird)
Jiang Liub106ee62015-04-13 14:11:32 +08001210 goto out_free_data;
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001211 /* Initialize the common data */
1212 ird->irq_2_iommu = data->irq_2_iommu;
1213 ird->irq_2_iommu.sub_handle = i;
1214 } else {
1215 ird = data;
Jiang Liub106ee62015-04-13 14:11:32 +08001216 }
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001217
Jiang Liub106ee62015-04-13 14:11:32 +08001218 irq_data->hwirq = (index << 16) + i;
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001219 irq_data->chip_data = ird;
Jiang Liub106ee62015-04-13 14:11:32 +08001220 irq_data->chip = &intel_ir_chip;
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001221 intel_irq_remapping_prepare_irte(ird, irq_cfg, info, index, i);
Jiang Liub106ee62015-04-13 14:11:32 +08001222 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
1223 }
1224 return 0;
1225
1226out_free_data:
1227 intel_free_irq_resources(domain, virq, i);
1228out_free_parent:
1229 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1230 return ret;
1231}
1232
1233static void intel_irq_remapping_free(struct irq_domain *domain,
1234 unsigned int virq, unsigned int nr_irqs)
1235{
1236 intel_free_irq_resources(domain, virq, nr_irqs);
1237 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1238}
1239
1240static void intel_irq_remapping_activate(struct irq_domain *domain,
1241 struct irq_data *irq_data)
1242{
1243 struct intel_ir_data *data = irq_data->chip_data;
1244
1245 modify_irte(&data->irq_2_iommu, &data->irte_entry);
1246}
1247
1248static void intel_irq_remapping_deactivate(struct irq_domain *domain,
1249 struct irq_data *irq_data)
1250{
1251 struct intel_ir_data *data = irq_data->chip_data;
1252 struct irte entry;
1253
1254 memset(&entry, 0, sizeof(entry));
1255 modify_irte(&data->irq_2_iommu, &entry);
1256}
1257
1258static struct irq_domain_ops intel_ir_domain_ops = {
1259 .alloc = intel_irq_remapping_alloc,
1260 .free = intel_irq_remapping_free,
1261 .activate = intel_irq_remapping_activate,
1262 .deactivate = intel_irq_remapping_deactivate,
Joerg Roedel736baef2012-03-30 11:47:00 -07001263};
Jiang Liu6b197242014-11-09 22:47:58 +08001264
Jiang Liua7a3dad2014-11-09 22:48:00 +08001265/*
1266 * Support of Interrupt Remapping Unit Hotplug
1267 */
1268static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
1269{
1270 int ret;
1271 int eim = x2apic_enabled();
1272
1273 if (eim && !ecap_eim_support(iommu->ecap)) {
1274 pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
1275 iommu->reg_phys, iommu->ecap);
1276 return -ENODEV;
1277 }
1278
1279 if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
1280 pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
1281 iommu->reg_phys);
1282 return -ENODEV;
1283 }
1284
1285 /* TODO: check all IOAPICs are covered by IOMMU */
1286
1287 /* Setup Interrupt-remapping now. */
1288 ret = intel_setup_irq_remapping(iommu);
1289 if (ret) {
1290 pr_err("DRHD %Lx: failed to allocate resource\n",
1291 iommu->reg_phys);
1292 ir_remove_ioapic_hpet_scope(iommu);
1293 return ret;
1294 }
1295
1296 if (!iommu->qi) {
1297 /* Clear previous faults. */
1298 dmar_fault(-1, iommu);
1299 iommu_disable_irq_remapping(iommu);
1300 dmar_disable_qi(iommu);
1301 }
1302
1303 /* Enable queued invalidation */
1304 ret = dmar_enable_qi(iommu);
1305 if (!ret) {
1306 iommu_set_irq_remapping(iommu, eim);
1307 } else {
1308 pr_err("DRHD %Lx: failed to enable queued invalidation, ecap %Lx, ret %d\n",
1309 iommu->reg_phys, iommu->ecap, ret);
1310 intel_teardown_irq_remapping(iommu);
1311 ir_remove_ioapic_hpet_scope(iommu);
1312 }
1313
1314 return ret;
1315}
1316
Jiang Liu6b197242014-11-09 22:47:58 +08001317int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
1318{
Jiang Liua7a3dad2014-11-09 22:48:00 +08001319 int ret = 0;
1320 struct intel_iommu *iommu = dmaru->iommu;
1321
1322 if (!irq_remapping_enabled)
1323 return 0;
1324 if (iommu == NULL)
1325 return -EINVAL;
1326 if (!ecap_ir_support(iommu->ecap))
1327 return 0;
1328
1329 if (insert) {
1330 if (!iommu->ir_table)
1331 ret = dmar_ir_add(dmaru, iommu);
1332 } else {
1333 if (iommu->ir_table) {
1334 if (!bitmap_empty(iommu->ir_table->bitmap,
1335 INTR_REMAP_TABLE_ENTRIES)) {
1336 ret = -EBUSY;
1337 } else {
1338 iommu_disable_irq_remapping(iommu);
1339 intel_teardown_irq_remapping(iommu);
1340 ir_remove_ioapic_hpet_scope(iommu);
1341 }
1342 }
1343 }
1344
1345 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08001346}