blob: a643eecf9d29fb5ba4bb15ce10101ee3768cb793 [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
Joerg Roedeleef93fd2012-03-30 11:46:59 -070021struct ioapic_scope {
22 struct intel_iommu *iommu;
23 unsigned int id;
24 unsigned int bus; /* PCI bus number */
25 unsigned int devfn; /* PCI devfn number */
26};
27
28struct hpet_scope {
29 struct intel_iommu *iommu;
30 u8 id;
31 unsigned int bus;
32 unsigned int devfn;
33};
34
Jiang Liu099c5c02015-04-14 10:29:51 +080035struct irq_2_iommu {
36 struct intel_iommu *iommu;
37 u16 irte_index;
38 u16 sub_handle;
39 u8 irte_mask;
40};
41
Jiang Liub106ee62015-04-13 14:11:32 +080042struct intel_ir_data {
43 struct irq_2_iommu irq_2_iommu;
44 struct irte irte_entry;
45 union {
46 struct msi_msg msi_entry;
47 };
48};
49
Joerg Roedeleef93fd2012-03-30 11:46:59 -070050#define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
Jiang Liu13d09b62015-01-07 15:31:37 +080051#define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
Joerg Roedeleef93fd2012-03-30 11:46:59 -070052
Jiang Liu13d09b62015-01-07 15:31:37 +080053static int __read_mostly eim_mode;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070054static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070055static struct hpet_scope ir_hpet[MAX_HPET_TBS];
Chris Wrightd1423d52010-07-20 11:06:49 -070056
Jiang Liu3a5670e2014-02-19 14:07:33 +080057/*
58 * Lock ordering:
59 * ->dmar_global_lock
60 * ->irq_2_ir_lock
61 * ->qi->q_lock
62 * ->iommu->register_lock
63 * Note:
64 * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
65 * in single-threaded environment with interrupt disabled, so no need to tabke
66 * the dmar_global_lock.
67 */
Thomas Gleixner96f8e982011-07-19 16:28:19 +020068static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Jiang Liub106ee62015-04-13 14:11:32 +080069static struct irq_domain_ops intel_ir_domain_ops;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020070
Jiang Liu694835d2014-01-06 14:18:16 +080071static int __init parse_ioapics_under_ir(void);
72
Jiang Liu8dedf4c2015-04-13 14:11:31 +080073static int alloc_irte(struct intel_iommu *iommu, int irq,
74 struct irq_2_iommu *irq_iommu, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070075{
76 struct ir_table *table = iommu->ir_table;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070077 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070078 unsigned long flags;
Dan Carpenter9f4c7442014-01-09 08:32:36 +030079 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070080
Thomas Gleixnerd585d062010-10-10 12:34:27 +020081 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070082 return -1;
83
Suresh Siddhab6fcb332008-07-10 11:16:44 -070084 if (count > 1) {
85 count = __roundup_pow_of_two(count);
86 mask = ilog2(count);
87 }
88
89 if (mask > ecap_max_handle_mask(iommu->ecap)) {
90 printk(KERN_ERR
91 "Requested mask %x exceeds the max invalidation handle"
92 " mask value %Lx\n", mask,
93 ecap_max_handle_mask(iommu->ecap));
94 return -1;
95 }
96
Thomas Gleixner96f8e982011-07-19 16:28:19 +020097 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Jiang Liu360eb3c52014-01-06 14:18:08 +080098 index = bitmap_find_free_region(table->bitmap,
99 INTR_REMAP_TABLE_ENTRIES, mask);
100 if (index < 0) {
101 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
102 } else {
Jiang Liu360eb3c52014-01-06 14:18:08 +0800103 irq_iommu->iommu = iommu;
104 irq_iommu->irte_index = index;
105 irq_iommu->sub_handle = 0;
106 irq_iommu->irte_mask = mask;
107 }
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200108 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700109
110 return index;
111}
112
Yu Zhao704126a2009-01-04 16:28:52 +0800113static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700114{
115 struct qi_desc desc;
116
117 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
118 | QI_IEC_SELECTIVE;
119 desc.high = 0;
120
Yu Zhao704126a2009-01-04 16:28:52 +0800121 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700122}
123
Jiang Liu8dedf4c2015-04-13 14:11:31 +0800124static int modify_irte(struct irq_2_iommu *irq_iommu,
125 struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700126{
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700127 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700128 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200129 struct irte *irte;
130 int rc, index;
131
132 if (!irq_iommu)
133 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700134
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200135 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700136
Yinghai Lue420dfb2008-08-19 20:50:21 -0700137 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700138
Yinghai Lue420dfb2008-08-19 20:50:21 -0700139 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700140 irte = &iommu->ir_table->base[index];
141
Linus Torvaldsc513b672010-08-06 11:02:31 -0700142 set_64bit(&irte->low, irte_modified->low);
143 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700144 __iommu_flush_cache(iommu, irte, sizeof(*irte));
145
Yu Zhao704126a2009-01-04 16:28:52 +0800146 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200147 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800148
149 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700150}
151
Joerg Roedel263b5e82012-03-30 11:47:06 -0700152static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700153{
154 int i;
155
156 for (i = 0; i < MAX_HPET_TBS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800157 if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
Suresh Siddha20f30972009-08-04 12:07:08 -0700158 return ir_hpet[i].iommu;
159 return NULL;
160}
161
Joerg Roedel263b5e82012-03-30 11:47:06 -0700162static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700163{
164 int i;
165
166 for (i = 0; i < MAX_IO_APICS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800167 if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
Suresh Siddha89027d32008-07-10 11:16:56 -0700168 return ir_ioapic[i].iommu;
169 return NULL;
170}
171
Joerg Roedel263b5e82012-03-30 11:47:06 -0700172static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700173{
174 struct dmar_drhd_unit *drhd;
175
176 drhd = dmar_find_matched_drhd_unit(dev);
177 if (!drhd)
178 return NULL;
179
180 return drhd->iommu;
181}
182
Weidong Hanc4658b42009-05-23 00:41:14 +0800183static int clear_entries(struct irq_2_iommu *irq_iommu)
184{
185 struct irte *start, *entry, *end;
186 struct intel_iommu *iommu;
187 int index;
188
189 if (irq_iommu->sub_handle)
190 return 0;
191
192 iommu = irq_iommu->iommu;
Jiang Liu8dedf4c2015-04-13 14:11:31 +0800193 index = irq_iommu->irte_index;
Weidong Hanc4658b42009-05-23 00:41:14 +0800194
195 start = iommu->ir_table->base + index;
196 end = start + (1 << irq_iommu->irte_mask);
197
198 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700199 set_64bit(&entry->low, 0);
200 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800201 }
Jiang Liu360eb3c52014-01-06 14:18:08 +0800202 bitmap_release_region(iommu->ir_table->bitmap, index,
203 irq_iommu->irte_mask);
Weidong Hanc4658b42009-05-23 00:41:14 +0800204
205 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
206}
207
Weidong Hanf007e992009-05-23 00:41:15 +0800208/*
209 * source validation type
210 */
211#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300212#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800213#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
214
215/*
216 * source-id qualifier
217 */
218#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
219#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
220 * the third least significant bit
221 */
222#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
223 * the second and third least significant bits
224 */
225#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
226 * the least three significant bits
227 */
228
229/*
230 * set SVT, SQ and SID fields of irte to verify
231 * source ids of interrupt requests
232 */
233static void set_irte_sid(struct irte *irte, unsigned int svt,
234 unsigned int sq, unsigned int sid)
235{
Chris Wrightd1423d52010-07-20 11:06:49 -0700236 if (disable_sourceid_checking)
237 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800238 irte->svt = svt;
239 irte->sq = sq;
240 irte->sid = sid;
241}
242
Joerg Roedel263b5e82012-03-30 11:47:06 -0700243static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800244{
245 int i;
246 u16 sid = 0;
247
248 if (!irte)
249 return -1;
250
Jiang Liu3a5670e2014-02-19 14:07:33 +0800251 down_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800252 for (i = 0; i < MAX_IO_APICS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800253 if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
Weidong Hanf007e992009-05-23 00:41:15 +0800254 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
255 break;
256 }
257 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800258 up_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800259
260 if (sid == 0) {
261 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
262 return -1;
263 }
264
Jiang Liu2fe2c602014-01-06 14:18:17 +0800265 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
Weidong Hanf007e992009-05-23 00:41:15 +0800266
267 return 0;
268}
269
Joerg Roedel263b5e82012-03-30 11:47:06 -0700270static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700271{
272 int i;
273 u16 sid = 0;
274
275 if (!irte)
276 return -1;
277
Jiang Liu3a5670e2014-02-19 14:07:33 +0800278 down_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700279 for (i = 0; i < MAX_HPET_TBS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800280 if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700281 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
282 break;
283 }
284 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800285 up_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700286
287 if (sid == 0) {
288 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
289 return -1;
290 }
291
292 /*
293 * Should really use SQ_ALL_16. Some platforms are broken.
294 * While we figure out the right quirks for these broken platforms, use
295 * SQ_13_IGNORE_3 for now.
296 */
297 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
298
299 return 0;
300}
301
Alex Williamson579305f2014-07-03 09:51:43 -0600302struct set_msi_sid_data {
303 struct pci_dev *pdev;
304 u16 alias;
305};
306
307static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
308{
309 struct set_msi_sid_data *data = opaque;
310
311 data->pdev = pdev;
312 data->alias = alias;
313
314 return 0;
315}
316
Joerg Roedel263b5e82012-03-30 11:47:06 -0700317static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800318{
Alex Williamson579305f2014-07-03 09:51:43 -0600319 struct set_msi_sid_data data;
Weidong Hanf007e992009-05-23 00:41:15 +0800320
321 if (!irte || !dev)
322 return -1;
323
Alex Williamson579305f2014-07-03 09:51:43 -0600324 pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
Weidong Hanf007e992009-05-23 00:41:15 +0800325
Alex Williamson579305f2014-07-03 09:51:43 -0600326 /*
327 * DMA alias provides us with a PCI device and alias. The only case
328 * where the it will return an alias on a different bus than the
329 * device is the case of a PCIe-to-PCI bridge, where the alias is for
330 * the subordinate bus. In this case we can only verify the bus.
331 *
332 * If the alias device is on a different bus than our source device
333 * then we have a topology based alias, use it.
334 *
335 * Otherwise, the alias is for a device DMA quirk and we cannot
336 * assume that MSI uses the same requester ID. Therefore use the
337 * original device.
338 */
339 if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
340 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
341 PCI_DEVID(PCI_BUS_NUM(data.alias),
342 dev->bus->number));
343 else if (data.pdev->bus->number != dev->bus->number)
344 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
345 else
346 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
347 PCI_DEVID(dev->bus->number, dev->devfn));
Weidong Hanf007e992009-05-23 00:41:15 +0800348
349 return 0;
350}
351
Suresh Siddha95a02e92012-03-30 11:47:07 -0700352static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700353{
354 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100355 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700356 unsigned long flags;
357
358 addr = virt_to_phys((void *)iommu->ir_table->base);
359
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200360 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700361
362 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
363 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
364
365 /* Set interrupt-remapping table pointer */
Jan Kiszkaf63ef692014-08-11 13:13:25 +0200366 writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700367
368 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
369 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200370 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700371
372 /*
373 * global invalidation of interrupt entry cache before enabling
374 * interrupt-remapping.
375 */
376 qi_global_iec(iommu);
377
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200378 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700379
380 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700381 iommu->gcmd |= DMA_GCMD_IRE;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800382 iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
David Woodhousec416daa2009-05-10 20:30:58 +0100383 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700384
385 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
386 readl, (sts & DMA_GSTS_IRES), sts);
387
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800388 /*
389 * With CFI clear in the Global Command register, we should be
390 * protected from dangerous (i.e. compatibility) interrupts
391 * regardless of x2apic status. Check just to be sure.
392 */
393 if (sts & DMA_GSTS_CFIS)
394 WARN(1, KERN_WARNING
395 "Compatibility-format IRQs enabled despite intr remapping;\n"
396 "you are vulnerable to IRQ injection.\n");
397
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200398 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700399}
400
Jiang Liua7a3dad2014-11-09 22:48:00 +0800401static int intel_setup_irq_remapping(struct intel_iommu *iommu)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700402{
403 struct ir_table *ir_table;
404 struct page *pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800405 unsigned long *bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700406
Jiang Liua7a3dad2014-11-09 22:48:00 +0800407 if (iommu->ir_table)
408 return 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700409
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800410 ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800411 if (!ir_table)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700412 return -ENOMEM;
413
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800414 pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO,
Suresh Siddha824cd752009-10-02 11:01:23 -0700415 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700416 if (!pages) {
Jiang Liu360eb3c52014-01-06 14:18:08 +0800417 pr_err("IR%d: failed to allocate pages of order %d\n",
418 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800419 goto out_free_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700420 }
421
Jiang Liu360eb3c52014-01-06 14:18:08 +0800422 bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
423 sizeof(long), GFP_ATOMIC);
424 if (bitmap == NULL) {
425 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800426 goto out_free_pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800427 }
428
Jiang Liub106ee62015-04-13 14:11:32 +0800429 iommu->ir_domain = irq_domain_add_hierarchy(arch_get_ir_parent_domain(),
430 0, INTR_REMAP_TABLE_ENTRIES,
431 NULL, &intel_ir_domain_ops,
432 iommu);
433 if (!iommu->ir_domain) {
434 pr_err("IR%d: failed to allocate irqdomain\n", iommu->seq_id);
435 goto out_free_bitmap;
436 }
437 iommu->ir_msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
438
Suresh Siddha2ae21012008-07-10 11:16:43 -0700439 ir_table->base = page_address(pages);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800440 ir_table->bitmap = bitmap;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800441 iommu->ir_table = ir_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700442 return 0;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800443
Jiang Liub106ee62015-04-13 14:11:32 +0800444out_free_bitmap:
445 kfree(bitmap);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800446out_free_pages:
447 __free_pages(pages, INTR_REMAP_PAGE_ORDER);
448out_free_table:
449 kfree(ir_table);
450 return -ENOMEM;
451}
452
453static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
454{
455 if (iommu && iommu->ir_table) {
Jiang Liub106ee62015-04-13 14:11:32 +0800456 if (iommu->ir_msi_domain) {
457 irq_domain_remove(iommu->ir_msi_domain);
458 iommu->ir_msi_domain = NULL;
459 }
460 if (iommu->ir_domain) {
461 irq_domain_remove(iommu->ir_domain);
462 iommu->ir_domain = NULL;
463 }
Jiang Liua7a3dad2014-11-09 22:48:00 +0800464 free_pages((unsigned long)iommu->ir_table->base,
465 INTR_REMAP_PAGE_ORDER);
466 kfree(iommu->ir_table->bitmap);
467 kfree(iommu->ir_table);
468 iommu->ir_table = NULL;
469 }
Suresh Siddha2ae21012008-07-10 11:16:43 -0700470}
471
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700472/*
473 * Disable Interrupt Remapping.
474 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700475static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700476{
477 unsigned long flags;
478 u32 sts;
479
480 if (!ecap_ir_support(iommu->ecap))
481 return;
482
Fenghua Yub24696b2009-03-27 14:22:44 -0700483 /*
484 * global invalidation of interrupt entry cache before disabling
485 * interrupt-remapping.
486 */
487 qi_global_iec(iommu);
488
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200489 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700490
491 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
492 if (!(sts & DMA_GSTS_IRES))
493 goto end;
494
495 iommu->gcmd &= ~DMA_GCMD_IRE;
496 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
497
498 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
499 readl, !(sts & DMA_GSTS_IRES), sts);
500
501end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200502 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700503}
504
Suresh Siddha41750d32011-08-23 17:05:18 -0700505static int __init dmar_x2apic_optout(void)
506{
507 struct acpi_table_dmar *dmar;
508 dmar = (struct acpi_table_dmar *)dmar_tbl;
509 if (!dmar || no_x2apic_optout)
510 return 0;
511 return dmar->flags & DMAR_X2APIC_OPT_OUT;
512}
513
Thomas Gleixner11190302015-01-07 15:31:29 +0800514static void __init intel_cleanup_irq_remapping(void)
515{
516 struct dmar_drhd_unit *drhd;
517 struct intel_iommu *iommu;
518
519 for_each_iommu(iommu, drhd) {
520 if (ecap_ir_support(iommu->ecap)) {
521 iommu_disable_irq_remapping(iommu);
522 intel_teardown_irq_remapping(iommu);
523 }
524 }
525
526 if (x2apic_supported())
527 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
528}
529
530static int __init intel_prepare_irq_remapping(void)
531{
532 struct dmar_drhd_unit *drhd;
533 struct intel_iommu *iommu;
534
Jiang Liu2966d952015-01-07 15:31:35 +0800535 if (irq_remap_broken) {
536 printk(KERN_WARNING
537 "This system BIOS has enabled interrupt remapping\n"
538 "on a chipset that contains an erratum making that\n"
539 "feature unstable. To maintain system stability\n"
540 "interrupt remapping is being disabled. Please\n"
541 "contact your BIOS vendor for an update\n");
542 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Jiang Liu2966d952015-01-07 15:31:35 +0800543 return -ENODEV;
544 }
545
Thomas Gleixner11190302015-01-07 15:31:29 +0800546 if (dmar_table_init() < 0)
Jiang Liu2966d952015-01-07 15:31:35 +0800547 return -ENODEV;
548
549 if (!dmar_ir_support())
550 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800551
552 if (parse_ioapics_under_ir() != 1) {
553 printk(KERN_INFO "Not enabling interrupt remapping\n");
554 goto error;
555 }
556
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800557 /* First make sure all IOMMUs support IRQ remapping */
Jiang Liu2966d952015-01-07 15:31:35 +0800558 for_each_iommu(iommu, drhd)
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800559 if (!ecap_ir_support(iommu->ecap))
Thomas Gleixner11190302015-01-07 15:31:29 +0800560 goto error;
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800561
562 /* Do the allocations early */
563 for_each_iommu(iommu, drhd)
564 if (intel_setup_irq_remapping(iommu))
565 goto error;
566
Thomas Gleixner11190302015-01-07 15:31:29 +0800567 return 0;
Jiang Liu2966d952015-01-07 15:31:35 +0800568
Thomas Gleixner11190302015-01-07 15:31:29 +0800569error:
570 intel_cleanup_irq_remapping();
Jiang Liu2966d952015-01-07 15:31:35 +0800571 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800572}
573
Suresh Siddha95a02e92012-03-30 11:47:07 -0700574static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700575{
576 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800577 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100578 bool setup = false;
Suresh Siddha41750d32011-08-23 17:05:18 -0700579 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700580
Thomas Gleixner11190302015-01-07 15:31:29 +0800581 if (x2apic_supported()) {
Suresh Siddha41750d32011-08-23 17:05:18 -0700582 eim = !dmar_x2apic_optout();
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800583 if (!eim)
Fenghua Yu68c1b892015-02-21 13:07:27 -0800584 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 -0700585 }
586
Jiang Liu7c919772014-01-06 14:18:18 +0800587 for_each_iommu(iommu, drhd) {
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700588 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800589 * If the queued invalidation is already initialized,
590 * shouldn't disable it.
591 */
592 if (iommu->qi)
593 continue;
594
595 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700596 * Clear previous faults.
597 */
598 dmar_fault(-1, iommu);
599
600 /*
601 * Disable intr remapping and queued invalidation, if already
602 * enabled prior to OS handover.
603 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700604 iommu_disable_irq_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700605
606 dmar_disable_qi(iommu);
607 }
608
Suresh Siddha2ae21012008-07-10 11:16:43 -0700609 /*
610 * check for the Interrupt-remapping support
611 */
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800612 for_each_iommu(iommu, drhd)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700613 if (eim && !ecap_eim_support(iommu->ecap)) {
614 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
615 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
Jiang Liu13d09b62015-01-07 15:31:37 +0800616 eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700617 }
Jiang Liu13d09b62015-01-07 15:31:37 +0800618 eim_mode = eim;
619 if (eim)
620 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700621
622 /*
623 * Enable queued invalidation for all the DRHD's.
624 */
Jiang Liu7c919772014-01-06 14:18:18 +0800625 for_each_iommu(iommu, drhd) {
626 int ret = dmar_enable_qi(iommu);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700627
628 if (ret) {
629 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
630 " invalidation, ecap %Lx, ret %d\n",
631 drhd->reg_base_addr, iommu->ecap, ret);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800632 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700633 }
634 }
635
636 /*
637 * Setup Interrupt-remapping for all the DRHD's now.
638 */
Jiang Liu7c919772014-01-06 14:18:18 +0800639 for_each_iommu(iommu, drhd) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800640 iommu_set_irq_remapping(iommu, eim);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100641 setup = true;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700642 }
643
644 if (!setup)
645 goto error;
646
Suresh Siddha95a02e92012-03-30 11:47:07 -0700647 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200648
Suresh Siddha41750d32011-08-23 17:05:18 -0700649 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700650
Suresh Siddha41750d32011-08-23 17:05:18 -0700651 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700652
653error:
Thomas Gleixner11190302015-01-07 15:31:29 +0800654 intel_cleanup_irq_remapping();
Suresh Siddha2ae21012008-07-10 11:16:43 -0700655 return -1;
656}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700657
Jiang Liua7a3dad2014-11-09 22:48:00 +0800658static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
659 struct intel_iommu *iommu,
660 struct acpi_dmar_hardware_unit *drhd)
Suresh Siddha20f30972009-08-04 12:07:08 -0700661{
662 struct acpi_dmar_pci_path *path;
663 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800664 int count, free = -1;
Suresh Siddha20f30972009-08-04 12:07:08 -0700665
666 bus = scope->bus;
667 path = (struct acpi_dmar_pci_path *)(scope + 1);
668 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
669 / sizeof(struct acpi_dmar_pci_path);
670
671 while (--count > 0) {
672 /*
673 * Access PCI directly due to the PCI
674 * subsystem isn't initialized yet.
675 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800676 bus = read_pci_config_byte(bus, path->device, path->function,
Suresh Siddha20f30972009-08-04 12:07:08 -0700677 PCI_SECONDARY_BUS);
678 path++;
679 }
Jiang Liua7a3dad2014-11-09 22:48:00 +0800680
681 for (count = 0; count < MAX_HPET_TBS; count++) {
682 if (ir_hpet[count].iommu == iommu &&
683 ir_hpet[count].id == scope->enumeration_id)
684 return 0;
685 else if (ir_hpet[count].iommu == NULL && free == -1)
686 free = count;
687 }
688 if (free == -1) {
689 pr_warn("Exceeded Max HPET blocks\n");
690 return -ENOSPC;
691 }
692
693 ir_hpet[free].iommu = iommu;
694 ir_hpet[free].id = scope->enumeration_id;
695 ir_hpet[free].bus = bus;
696 ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
697 pr_info("HPET id %d under DRHD base 0x%Lx\n",
698 scope->enumeration_id, drhd->address);
699
700 return 0;
Suresh Siddha20f30972009-08-04 12:07:08 -0700701}
702
Jiang Liua7a3dad2014-11-09 22:48:00 +0800703static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
704 struct intel_iommu *iommu,
705 struct acpi_dmar_hardware_unit *drhd)
Weidong Hanf007e992009-05-23 00:41:15 +0800706{
707 struct acpi_dmar_pci_path *path;
708 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800709 int count, free = -1;
Weidong Hanf007e992009-05-23 00:41:15 +0800710
711 bus = scope->bus;
712 path = (struct acpi_dmar_pci_path *)(scope + 1);
713 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
714 / sizeof(struct acpi_dmar_pci_path);
715
716 while (--count > 0) {
717 /*
718 * Access PCI directly due to the PCI
719 * subsystem isn't initialized yet.
720 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800721 bus = read_pci_config_byte(bus, path->device, path->function,
Weidong Hanf007e992009-05-23 00:41:15 +0800722 PCI_SECONDARY_BUS);
723 path++;
724 }
725
Jiang Liua7a3dad2014-11-09 22:48:00 +0800726 for (count = 0; count < MAX_IO_APICS; count++) {
727 if (ir_ioapic[count].iommu == iommu &&
728 ir_ioapic[count].id == scope->enumeration_id)
729 return 0;
730 else if (ir_ioapic[count].iommu == NULL && free == -1)
731 free = count;
732 }
733 if (free == -1) {
734 pr_warn("Exceeded Max IO APICS\n");
735 return -ENOSPC;
736 }
737
738 ir_ioapic[free].bus = bus;
739 ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
740 ir_ioapic[free].iommu = iommu;
741 ir_ioapic[free].id = scope->enumeration_id;
742 pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
743 scope->enumeration_id, drhd->address, iommu->seq_id);
744
745 return 0;
Weidong Hanf007e992009-05-23 00:41:15 +0800746}
747
Suresh Siddha20f30972009-08-04 12:07:08 -0700748static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
749 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700750{
Jiang Liua7a3dad2014-11-09 22:48:00 +0800751 int ret = 0;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700752 struct acpi_dmar_hardware_unit *drhd;
753 struct acpi_dmar_device_scope *scope;
754 void *start, *end;
755
756 drhd = (struct acpi_dmar_hardware_unit *)header;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700757 start = (void *)(drhd + 1);
758 end = ((void *)drhd) + header->length;
759
Jiang Liua7a3dad2014-11-09 22:48:00 +0800760 while (start < end && ret == 0) {
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700761 scope = start;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800762 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
763 ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
764 else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
765 ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700766 start += scope->length;
767 }
768
Jiang Liua7a3dad2014-11-09 22:48:00 +0800769 return ret;
770}
771
772static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
773{
774 int i;
775
776 for (i = 0; i < MAX_HPET_TBS; i++)
777 if (ir_hpet[i].iommu == iommu)
778 ir_hpet[i].iommu = NULL;
779
780 for (i = 0; i < MAX_IO_APICS; i++)
781 if (ir_ioapic[i].iommu == iommu)
782 ir_ioapic[i].iommu = NULL;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700783}
784
785/*
786 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
787 * hardware unit.
788 */
Jiang Liu694835d2014-01-06 14:18:16 +0800789static int __init parse_ioapics_under_ir(void)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700790{
791 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800792 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100793 bool ir_supported = false;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500794 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700795
Jiang Liu7c919772014-01-06 14:18:18 +0800796 for_each_iommu(iommu, drhd)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700797 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700798 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700799 return -1;
800
Quentin Lambert2f119c72015-02-06 10:59:53 +0100801 ir_supported = true;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700802 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700803
Seth Forshee32ab31e2012-08-08 08:27:03 -0500804 if (!ir_supported)
805 return 0;
806
807 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
808 int ioapic_id = mpc_ioapic_id(ioapic_idx);
809 if (!map_ioapic_to_ir(ioapic_id)) {
810 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
811 "interrupt remapping will be disabled\n",
812 ioapic_id);
813 return -1;
814 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700815 }
816
Seth Forshee32ab31e2012-08-08 08:27:03 -0500817 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700818}
Fenghua Yub24696b2009-03-27 14:22:44 -0700819
Rashika Kheria6a7885c2013-12-18 12:04:27 +0530820static int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700821{
Jiang Liu3a5670e2014-02-19 14:07:33 +0800822 int ret;
823
Suresh Siddha95a02e92012-03-30 11:47:07 -0700824 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700825 return 0;
826
Jiang Liu3a5670e2014-02-19 14:07:33 +0800827 down_write(&dmar_global_lock);
828 ret = dmar_dev_scope_init();
829 up_write(&dmar_global_lock);
830
831 return ret;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700832}
833rootfs_initcall(ir_dev_scope_init);
834
Suresh Siddha95a02e92012-03-30 11:47:07 -0700835static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700836{
837 struct dmar_drhd_unit *drhd;
838 struct intel_iommu *iommu = NULL;
839
840 /*
841 * Disable Interrupt-remapping for all the DRHD's now.
842 */
843 for_each_iommu(iommu, drhd) {
844 if (!ecap_ir_support(iommu->ecap))
845 continue;
846
Suresh Siddha95a02e92012-03-30 11:47:07 -0700847 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700848 }
849}
850
Suresh Siddha95a02e92012-03-30 11:47:07 -0700851static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700852{
853 struct dmar_drhd_unit *drhd;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100854 bool setup = false;
Fenghua Yub24696b2009-03-27 14:22:44 -0700855 struct intel_iommu *iommu = NULL;
856
857 for_each_iommu(iommu, drhd)
858 if (iommu->qi)
859 dmar_reenable_qi(iommu);
860
861 /*
862 * Setup Interrupt-remapping for all the DRHD's now.
863 */
864 for_each_iommu(iommu, drhd) {
865 if (!ecap_ir_support(iommu->ecap))
866 continue;
867
868 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -0700869 iommu_set_irq_remapping(iommu, eim);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100870 setup = true;
Fenghua Yub24696b2009-03-27 14:22:44 -0700871 }
872
873 if (!setup)
874 goto error;
875
876 return 0;
877
878error:
879 /*
880 * handle error condition gracefully here!
881 */
882 return -1;
883}
884
Jiang Liu3c6e5672015-04-14 10:29:47 +0800885static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700886{
887 memset(irte, 0, sizeof(*irte));
888
889 irte->present = 1;
890 irte->dst_mode = apic->irq_dest_mode;
891 /*
892 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
893 * actual level or edge trigger will be setup in the IO-APIC
894 * RTE. This will help simplify level triggered irq migration.
895 * For more details, see the comments (in io_apic.c) explainig IO-APIC
896 * irq migration in the presence of interrupt-remapping.
897 */
898 irte->trigger_mode = 0;
899 irte->dlvry_mode = apic->irq_delivery_mode;
900 irte->vector = vector;
901 irte->dest_id = IRTE_DEST(dest);
902 irte->redir_hint = 1;
903}
904
Jiang Liub106ee62015-04-13 14:11:32 +0800905static struct irq_domain *intel_get_ir_irq_domain(struct irq_alloc_info *info)
906{
907 struct intel_iommu *iommu = NULL;
908
909 if (!info)
910 return NULL;
911
912 switch (info->type) {
913 case X86_IRQ_ALLOC_TYPE_IOAPIC:
914 iommu = map_ioapic_to_ir(info->ioapic_id);
915 break;
916 case X86_IRQ_ALLOC_TYPE_HPET:
917 iommu = map_hpet_to_ir(info->hpet_id);
918 break;
919 case X86_IRQ_ALLOC_TYPE_MSI:
920 case X86_IRQ_ALLOC_TYPE_MSIX:
921 iommu = map_dev_to_ir(info->msi_dev);
922 break;
923 default:
924 BUG_ON(1);
925 break;
926 }
927
928 return iommu ? iommu->ir_domain : NULL;
929}
930
931static struct irq_domain *intel_get_irq_domain(struct irq_alloc_info *info)
932{
933 struct intel_iommu *iommu;
934
935 if (!info)
936 return NULL;
937
938 switch (info->type) {
939 case X86_IRQ_ALLOC_TYPE_MSI:
940 case X86_IRQ_ALLOC_TYPE_MSIX:
941 iommu = map_dev_to_ir(info->msi_dev);
942 if (iommu)
943 return iommu->ir_msi_domain;
944 break;
945 default:
946 break;
947 }
948
949 return NULL;
950}
951
Joerg Roedel736baef2012-03-30 11:47:00 -0700952struct irq_remap_ops intel_irq_remap_ops = {
Thomas Gleixner11190302015-01-07 15:31:29 +0800953 .prepare = intel_prepare_irq_remapping,
Suresh Siddha95a02e92012-03-30 11:47:07 -0700954 .enable = intel_enable_irq_remapping,
955 .disable = disable_irq_remapping,
956 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700957 .enable_faulting = enable_drhd_fault_handling,
Jiang Liub106ee62015-04-13 14:11:32 +0800958 .get_ir_irq_domain = intel_get_ir_irq_domain,
959 .get_irq_domain = intel_get_irq_domain,
960};
961
962/*
963 * Migrate the IO-APIC irq in the presence of intr-remapping.
964 *
965 * For both level and edge triggered, irq migration is a simple atomic
966 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
967 *
968 * For level triggered, we eliminate the io-apic RTE modification (with the
969 * updated vector information), by using a virtual vector (io-apic pin number).
970 * Real vector that is used for interrupting cpu will be coming from
971 * the interrupt-remapping table entry.
972 *
973 * As the migration is a simple atomic update of IRTE, the same mechanism
974 * is used to migrate MSI irq's in the presence of interrupt-remapping.
975 */
976static int
977intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
978 bool force)
979{
980 struct intel_ir_data *ir_data = data->chip_data;
981 struct irte *irte = &ir_data->irte_entry;
982 struct irq_cfg *cfg = irqd_cfg(data);
983 struct irq_data *parent = data->parent_data;
984 int ret;
985
986 ret = parent->chip->irq_set_affinity(parent, mask, force);
987 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
988 return ret;
989
990 /*
991 * Atomically updates the IRTE with the new destination, vector
992 * and flushes the interrupt entry cache.
993 */
994 irte->vector = cfg->vector;
995 irte->dest_id = IRTE_DEST(cfg->dest_apicid);
996 modify_irte(&ir_data->irq_2_iommu, irte);
997
998 /*
999 * After this point, all the interrupts will start arriving
1000 * at the new destination. So, time to cleanup the previous
1001 * vector allocation.
1002 */
Jiang Liuc6c20022015-04-14 10:30:02 +08001003 send_cleanup_vector(cfg);
Jiang Liub106ee62015-04-13 14:11:32 +08001004
1005 return IRQ_SET_MASK_OK_DONE;
1006}
1007
1008static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
1009 struct msi_msg *msg)
1010{
1011 struct intel_ir_data *ir_data = irq_data->chip_data;
1012
1013 *msg = ir_data->msi_entry;
1014}
1015
Feng Wu85411862015-06-09 13:20:31 +08001016static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info)
1017{
1018 struct intel_ir_data *ir_data = data->chip_data;
1019 struct vcpu_data *vcpu_pi_info = info;
1020
1021 /* stop posting interrupts, back to remapping mode */
1022 if (!vcpu_pi_info) {
1023 modify_irte(&ir_data->irq_2_iommu, &ir_data->irte_entry);
1024 } else {
1025 struct irte irte_pi;
1026
1027 /*
1028 * We are not caching the posted interrupt entry. We
1029 * copy the data from the remapped entry and modify
1030 * the fields which are relevant for posted mode. The
1031 * cached remapped entry is used for switching back to
1032 * remapped mode.
1033 */
1034 memset(&irte_pi, 0, sizeof(irte_pi));
1035 dmar_copy_shared_irte(&irte_pi, &ir_data->irte_entry);
1036
1037 /* Update the posted mode fields */
1038 irte_pi.p_pst = 1;
1039 irte_pi.p_urgent = 0;
1040 irte_pi.p_vector = vcpu_pi_info->vector;
1041 irte_pi.pda_l = (vcpu_pi_info->pi_desc_addr >>
1042 (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
1043 irte_pi.pda_h = (vcpu_pi_info->pi_desc_addr >> 32) &
1044 ~(-1UL << PDA_HIGH_BIT);
1045
1046 modify_irte(&ir_data->irq_2_iommu, &irte_pi);
1047 }
1048
1049 return 0;
1050}
1051
Jiang Liub106ee62015-04-13 14:11:32 +08001052static struct irq_chip intel_ir_chip = {
1053 .irq_ack = ir_ack_apic_edge,
1054 .irq_set_affinity = intel_ir_set_affinity,
1055 .irq_compose_msi_msg = intel_ir_compose_msi_msg,
Feng Wu85411862015-06-09 13:20:31 +08001056 .irq_set_vcpu_affinity = intel_ir_set_vcpu_affinity,
Jiang Liub106ee62015-04-13 14:11:32 +08001057};
1058
1059static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
1060 struct irq_cfg *irq_cfg,
1061 struct irq_alloc_info *info,
1062 int index, int sub_handle)
1063{
1064 struct IR_IO_APIC_route_entry *entry;
1065 struct irte *irte = &data->irte_entry;
1066 struct msi_msg *msg = &data->msi_entry;
1067
1068 prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
1069 switch (info->type) {
1070 case X86_IRQ_ALLOC_TYPE_IOAPIC:
1071 /* Set source-id of interrupt request */
1072 set_ioapic_sid(irte, info->ioapic_id);
1073 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",
1074 info->ioapic_id, irte->present, irte->fpd,
1075 irte->dst_mode, irte->redir_hint,
1076 irte->trigger_mode, irte->dlvry_mode,
1077 irte->avail, irte->vector, irte->dest_id,
1078 irte->sid, irte->sq, irte->svt);
1079
1080 entry = (struct IR_IO_APIC_route_entry *)info->ioapic_entry;
1081 info->ioapic_entry = NULL;
1082 memset(entry, 0, sizeof(*entry));
1083 entry->index2 = (index >> 15) & 0x1;
1084 entry->zero = 0;
1085 entry->format = 1;
1086 entry->index = (index & 0x7fff);
1087 /*
1088 * IO-APIC RTE will be configured with virtual vector.
1089 * irq handler will do the explicit EOI to the io-apic.
1090 */
1091 entry->vector = info->ioapic_pin;
1092 entry->mask = 0; /* enable IRQ */
1093 entry->trigger = info->ioapic_trigger;
1094 entry->polarity = info->ioapic_polarity;
1095 if (info->ioapic_trigger)
1096 entry->mask = 1; /* Mask level triggered irqs. */
1097 break;
1098
1099 case X86_IRQ_ALLOC_TYPE_HPET:
1100 case X86_IRQ_ALLOC_TYPE_MSI:
1101 case X86_IRQ_ALLOC_TYPE_MSIX:
1102 if (info->type == X86_IRQ_ALLOC_TYPE_HPET)
1103 set_hpet_sid(irte, info->hpet_id);
1104 else
1105 set_msi_sid(irte, info->msi_dev);
1106
1107 msg->address_hi = MSI_ADDR_BASE_HI;
1108 msg->data = sub_handle;
1109 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1110 MSI_ADDR_IR_SHV |
1111 MSI_ADDR_IR_INDEX1(index) |
1112 MSI_ADDR_IR_INDEX2(index);
1113 break;
1114
1115 default:
1116 BUG_ON(1);
1117 break;
1118 }
1119}
1120
1121static void intel_free_irq_resources(struct irq_domain *domain,
1122 unsigned int virq, unsigned int nr_irqs)
1123{
1124 struct irq_data *irq_data;
1125 struct intel_ir_data *data;
1126 struct irq_2_iommu *irq_iommu;
1127 unsigned long flags;
1128 int i;
1129
1130 for (i = 0; i < nr_irqs; i++) {
1131 irq_data = irq_domain_get_irq_data(domain, virq + i);
1132 if (irq_data && irq_data->chip_data) {
1133 data = irq_data->chip_data;
1134 irq_iommu = &data->irq_2_iommu;
1135 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
1136 clear_entries(irq_iommu);
1137 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
1138 irq_domain_reset_irq_data(irq_data);
1139 kfree(data);
1140 }
1141 }
1142}
1143
1144static int intel_irq_remapping_alloc(struct irq_domain *domain,
1145 unsigned int virq, unsigned int nr_irqs,
1146 void *arg)
1147{
1148 struct intel_iommu *iommu = domain->host_data;
1149 struct irq_alloc_info *info = arg;
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001150 struct intel_ir_data *data, *ird;
Jiang Liub106ee62015-04-13 14:11:32 +08001151 struct irq_data *irq_data;
1152 struct irq_cfg *irq_cfg;
1153 int i, ret, index;
1154
1155 if (!info || !iommu)
1156 return -EINVAL;
1157 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
1158 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
1159 return -EINVAL;
1160
1161 /*
1162 * With IRQ remapping enabled, don't need contiguous CPU vectors
1163 * to support multiple MSI interrupts.
1164 */
1165 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
1166 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
1167
1168 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
1169 if (ret < 0)
1170 return ret;
1171
1172 ret = -ENOMEM;
1173 data = kzalloc(sizeof(*data), GFP_KERNEL);
1174 if (!data)
1175 goto out_free_parent;
1176
1177 down_read(&dmar_global_lock);
1178 index = alloc_irte(iommu, virq, &data->irq_2_iommu, nr_irqs);
1179 up_read(&dmar_global_lock);
1180 if (index < 0) {
1181 pr_warn("Failed to allocate IRTE\n");
1182 kfree(data);
1183 goto out_free_parent;
1184 }
1185
1186 for (i = 0; i < nr_irqs; i++) {
1187 irq_data = irq_domain_get_irq_data(domain, virq + i);
1188 irq_cfg = irqd_cfg(irq_data);
1189 if (!irq_data || !irq_cfg) {
1190 ret = -EINVAL;
1191 goto out_free_data;
1192 }
1193
1194 if (i > 0) {
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001195 ird = kzalloc(sizeof(*ird), GFP_KERNEL);
1196 if (!ird)
Jiang Liub106ee62015-04-13 14:11:32 +08001197 goto out_free_data;
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001198 /* Initialize the common data */
1199 ird->irq_2_iommu = data->irq_2_iommu;
1200 ird->irq_2_iommu.sub_handle = i;
1201 } else {
1202 ird = data;
Jiang Liub106ee62015-04-13 14:11:32 +08001203 }
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001204
Jiang Liub106ee62015-04-13 14:11:32 +08001205 irq_data->hwirq = (index << 16) + i;
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001206 irq_data->chip_data = ird;
Jiang Liub106ee62015-04-13 14:11:32 +08001207 irq_data->chip = &intel_ir_chip;
Thomas Gleixner9d4c0312015-05-04 10:47:40 +08001208 intel_irq_remapping_prepare_irte(ird, irq_cfg, info, index, i);
Jiang Liub106ee62015-04-13 14:11:32 +08001209 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
1210 }
1211 return 0;
1212
1213out_free_data:
1214 intel_free_irq_resources(domain, virq, i);
1215out_free_parent:
1216 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1217 return ret;
1218}
1219
1220static void intel_irq_remapping_free(struct irq_domain *domain,
1221 unsigned int virq, unsigned int nr_irqs)
1222{
1223 intel_free_irq_resources(domain, virq, nr_irqs);
1224 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1225}
1226
1227static void intel_irq_remapping_activate(struct irq_domain *domain,
1228 struct irq_data *irq_data)
1229{
1230 struct intel_ir_data *data = irq_data->chip_data;
1231
1232 modify_irte(&data->irq_2_iommu, &data->irte_entry);
1233}
1234
1235static void intel_irq_remapping_deactivate(struct irq_domain *domain,
1236 struct irq_data *irq_data)
1237{
1238 struct intel_ir_data *data = irq_data->chip_data;
1239 struct irte entry;
1240
1241 memset(&entry, 0, sizeof(entry));
1242 modify_irte(&data->irq_2_iommu, &entry);
1243}
1244
1245static struct irq_domain_ops intel_ir_domain_ops = {
1246 .alloc = intel_irq_remapping_alloc,
1247 .free = intel_irq_remapping_free,
1248 .activate = intel_irq_remapping_activate,
1249 .deactivate = intel_irq_remapping_deactivate,
Joerg Roedel736baef2012-03-30 11:47:00 -07001250};
Jiang Liu6b197242014-11-09 22:47:58 +08001251
Jiang Liua7a3dad2014-11-09 22:48:00 +08001252/*
1253 * Support of Interrupt Remapping Unit Hotplug
1254 */
1255static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
1256{
1257 int ret;
1258 int eim = x2apic_enabled();
1259
1260 if (eim && !ecap_eim_support(iommu->ecap)) {
1261 pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
1262 iommu->reg_phys, iommu->ecap);
1263 return -ENODEV;
1264 }
1265
1266 if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
1267 pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
1268 iommu->reg_phys);
1269 return -ENODEV;
1270 }
1271
1272 /* TODO: check all IOAPICs are covered by IOMMU */
1273
1274 /* Setup Interrupt-remapping now. */
1275 ret = intel_setup_irq_remapping(iommu);
1276 if (ret) {
1277 pr_err("DRHD %Lx: failed to allocate resource\n",
1278 iommu->reg_phys);
1279 ir_remove_ioapic_hpet_scope(iommu);
1280 return ret;
1281 }
1282
1283 if (!iommu->qi) {
1284 /* Clear previous faults. */
1285 dmar_fault(-1, iommu);
1286 iommu_disable_irq_remapping(iommu);
1287 dmar_disable_qi(iommu);
1288 }
1289
1290 /* Enable queued invalidation */
1291 ret = dmar_enable_qi(iommu);
1292 if (!ret) {
1293 iommu_set_irq_remapping(iommu, eim);
1294 } else {
1295 pr_err("DRHD %Lx: failed to enable queued invalidation, ecap %Lx, ret %d\n",
1296 iommu->reg_phys, iommu->ecap, ret);
1297 intel_teardown_irq_remapping(iommu);
1298 ir_remove_ioapic_hpet_scope(iommu);
1299 }
1300
1301 return ret;
1302}
1303
Jiang Liu6b197242014-11-09 22:47:58 +08001304int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
1305{
Jiang Liua7a3dad2014-11-09 22:48:00 +08001306 int ret = 0;
1307 struct intel_iommu *iommu = dmaru->iommu;
1308
1309 if (!irq_remapping_enabled)
1310 return 0;
1311 if (iommu == NULL)
1312 return -EINVAL;
1313 if (!ecap_ir_support(iommu->ecap))
1314 return 0;
1315
1316 if (insert) {
1317 if (!iommu->ir_table)
1318 ret = dmar_ir_add(dmaru, iommu);
1319 } else {
1320 if (iommu->ir_table) {
1321 if (!bitmap_empty(iommu->ir_table->bitmap,
1322 INTR_REMAP_TABLE_ENTRIES)) {
1323 ret = -EBUSY;
1324 } else {
1325 iommu_disable_irq_remapping(iommu);
1326 intel_teardown_irq_remapping(iommu);
1327 ir_remove_ioapic_hpet_scope(iommu);
1328 }
1329 }
1330 }
1331
1332 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08001333}