blob: 2697ad6f6a84a68eca5c2b81393ea464d85e330f [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 Liub106ee62015-04-13 14:11:32 +080035struct intel_ir_data {
36 struct irq_2_iommu irq_2_iommu;
37 struct irte irte_entry;
38 union {
39 struct msi_msg msi_entry;
40 };
41};
42
Joerg Roedeleef93fd2012-03-30 11:46:59 -070043#define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
Jiang Liu13d09b62015-01-07 15:31:37 +080044#define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
Joerg Roedeleef93fd2012-03-30 11:46:59 -070045
Jiang Liu13d09b62015-01-07 15:31:37 +080046static int __read_mostly eim_mode;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070047static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070048static struct hpet_scope ir_hpet[MAX_HPET_TBS];
Chris Wrightd1423d52010-07-20 11:06:49 -070049
Jiang Liu3a5670e2014-02-19 14:07:33 +080050/*
51 * Lock ordering:
52 * ->dmar_global_lock
53 * ->irq_2_ir_lock
54 * ->qi->q_lock
55 * ->iommu->register_lock
56 * Note:
57 * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
58 * in single-threaded environment with interrupt disabled, so no need to tabke
59 * the dmar_global_lock.
60 */
Thomas Gleixner96f8e982011-07-19 16:28:19 +020061static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Jiang Liub106ee62015-04-13 14:11:32 +080062static struct irq_domain_ops intel_ir_domain_ops;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020063
Jiang Liu694835d2014-01-06 14:18:16 +080064static int __init parse_ioapics_under_ir(void);
65
Jiang Liu8dedf4c2015-04-13 14:11:31 +080066static int alloc_irte(struct intel_iommu *iommu, int irq,
67 struct irq_2_iommu *irq_iommu, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070068{
69 struct ir_table *table = iommu->ir_table;
Jiang Liu91411da2014-10-27 16:12:09 +080070 struct irq_cfg *cfg = irq_cfg(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070071 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070072 unsigned long flags;
Dan Carpenter9f4c7442014-01-09 08:32:36 +030073 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070074
Thomas Gleixnerd585d062010-10-10 12:34:27 +020075 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070076 return -1;
77
Suresh Siddhab6fcb332008-07-10 11:16:44 -070078 if (count > 1) {
79 count = __roundup_pow_of_two(count);
80 mask = ilog2(count);
81 }
82
83 if (mask > ecap_max_handle_mask(iommu->ecap)) {
84 printk(KERN_ERR
85 "Requested mask %x exceeds the max invalidation handle"
86 " mask value %Lx\n", mask,
87 ecap_max_handle_mask(iommu->ecap));
88 return -1;
89 }
90
Thomas Gleixner96f8e982011-07-19 16:28:19 +020091 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Jiang Liu360eb3c52014-01-06 14:18:08 +080092 index = bitmap_find_free_region(table->bitmap,
93 INTR_REMAP_TABLE_ENTRIES, mask);
94 if (index < 0) {
95 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
96 } else {
97 cfg->remapped = 1;
98 irq_iommu->iommu = iommu;
99 irq_iommu->irte_index = index;
100 irq_iommu->sub_handle = 0;
101 irq_iommu->irte_mask = mask;
102 }
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200103 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700104
105 return index;
106}
107
Yu Zhao704126a2009-01-04 16:28:52 +0800108static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700109{
110 struct qi_desc desc;
111
112 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
113 | QI_IEC_SELECTIVE;
114 desc.high = 0;
115
Yu Zhao704126a2009-01-04 16:28:52 +0800116 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700117}
118
Jiang Liu8dedf4c2015-04-13 14:11:31 +0800119static int modify_irte(struct irq_2_iommu *irq_iommu,
120 struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700121{
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700122 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700123 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200124 struct irte *irte;
125 int rc, index;
126
127 if (!irq_iommu)
128 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700129
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200130 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700131
Yinghai Lue420dfb2008-08-19 20:50:21 -0700132 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700133
Yinghai Lue420dfb2008-08-19 20:50:21 -0700134 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700135 irte = &iommu->ir_table->base[index];
136
Linus Torvaldsc513b672010-08-06 11:02:31 -0700137 set_64bit(&irte->low, irte_modified->low);
138 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700139 __iommu_flush_cache(iommu, irte, sizeof(*irte));
140
Yu Zhao704126a2009-01-04 16:28:52 +0800141 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200142 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800143
144 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700145}
146
Joerg Roedel263b5e82012-03-30 11:47:06 -0700147static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700148{
149 int i;
150
151 for (i = 0; i < MAX_HPET_TBS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800152 if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
Suresh Siddha20f30972009-08-04 12:07:08 -0700153 return ir_hpet[i].iommu;
154 return NULL;
155}
156
Joerg Roedel263b5e82012-03-30 11:47:06 -0700157static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700158{
159 int i;
160
161 for (i = 0; i < MAX_IO_APICS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800162 if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
Suresh Siddha89027d32008-07-10 11:16:56 -0700163 return ir_ioapic[i].iommu;
164 return NULL;
165}
166
Joerg Roedel263b5e82012-03-30 11:47:06 -0700167static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700168{
169 struct dmar_drhd_unit *drhd;
170
171 drhd = dmar_find_matched_drhd_unit(dev);
172 if (!drhd)
173 return NULL;
174
175 return drhd->iommu;
176}
177
Weidong Hanc4658b42009-05-23 00:41:14 +0800178static int clear_entries(struct irq_2_iommu *irq_iommu)
179{
180 struct irte *start, *entry, *end;
181 struct intel_iommu *iommu;
182 int index;
183
184 if (irq_iommu->sub_handle)
185 return 0;
186
187 iommu = irq_iommu->iommu;
Jiang Liu8dedf4c2015-04-13 14:11:31 +0800188 index = irq_iommu->irte_index;
Weidong Hanc4658b42009-05-23 00:41:14 +0800189
190 start = iommu->ir_table->base + index;
191 end = start + (1 << irq_iommu->irte_mask);
192
193 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700194 set_64bit(&entry->low, 0);
195 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800196 }
Jiang Liu360eb3c52014-01-06 14:18:08 +0800197 bitmap_release_region(iommu->ir_table->bitmap, index,
198 irq_iommu->irte_mask);
Weidong Hanc4658b42009-05-23 00:41:14 +0800199
200 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
201}
202
Weidong Hanf007e992009-05-23 00:41:15 +0800203/*
204 * source validation type
205 */
206#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300207#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800208#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
209
210/*
211 * source-id qualifier
212 */
213#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
214#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
215 * the third least significant bit
216 */
217#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
218 * the second and third least significant bits
219 */
220#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
221 * the least three significant bits
222 */
223
224/*
225 * set SVT, SQ and SID fields of irte to verify
226 * source ids of interrupt requests
227 */
228static void set_irte_sid(struct irte *irte, unsigned int svt,
229 unsigned int sq, unsigned int sid)
230{
Chris Wrightd1423d52010-07-20 11:06:49 -0700231 if (disable_sourceid_checking)
232 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800233 irte->svt = svt;
234 irte->sq = sq;
235 irte->sid = sid;
236}
237
Joerg Roedel263b5e82012-03-30 11:47:06 -0700238static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800239{
240 int i;
241 u16 sid = 0;
242
243 if (!irte)
244 return -1;
245
Jiang Liu3a5670e2014-02-19 14:07:33 +0800246 down_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800247 for (i = 0; i < MAX_IO_APICS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800248 if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
Weidong Hanf007e992009-05-23 00:41:15 +0800249 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
250 break;
251 }
252 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800253 up_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800254
255 if (sid == 0) {
256 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
257 return -1;
258 }
259
Jiang Liu2fe2c602014-01-06 14:18:17 +0800260 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
Weidong Hanf007e992009-05-23 00:41:15 +0800261
262 return 0;
263}
264
Joerg Roedel263b5e82012-03-30 11:47:06 -0700265static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700266{
267 int i;
268 u16 sid = 0;
269
270 if (!irte)
271 return -1;
272
Jiang Liu3a5670e2014-02-19 14:07:33 +0800273 down_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700274 for (i = 0; i < MAX_HPET_TBS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800275 if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700276 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
277 break;
278 }
279 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800280 up_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700281
282 if (sid == 0) {
283 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
284 return -1;
285 }
286
287 /*
288 * Should really use SQ_ALL_16. Some platforms are broken.
289 * While we figure out the right quirks for these broken platforms, use
290 * SQ_13_IGNORE_3 for now.
291 */
292 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
293
294 return 0;
295}
296
Alex Williamson579305f2014-07-03 09:51:43 -0600297struct set_msi_sid_data {
298 struct pci_dev *pdev;
299 u16 alias;
300};
301
302static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
303{
304 struct set_msi_sid_data *data = opaque;
305
306 data->pdev = pdev;
307 data->alias = alias;
308
309 return 0;
310}
311
Joerg Roedel263b5e82012-03-30 11:47:06 -0700312static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800313{
Alex Williamson579305f2014-07-03 09:51:43 -0600314 struct set_msi_sid_data data;
Weidong Hanf007e992009-05-23 00:41:15 +0800315
316 if (!irte || !dev)
317 return -1;
318
Alex Williamson579305f2014-07-03 09:51:43 -0600319 pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
Weidong Hanf007e992009-05-23 00:41:15 +0800320
Alex Williamson579305f2014-07-03 09:51:43 -0600321 /*
322 * DMA alias provides us with a PCI device and alias. The only case
323 * where the it will return an alias on a different bus than the
324 * device is the case of a PCIe-to-PCI bridge, where the alias is for
325 * the subordinate bus. In this case we can only verify the bus.
326 *
327 * If the alias device is on a different bus than our source device
328 * then we have a topology based alias, use it.
329 *
330 * Otherwise, the alias is for a device DMA quirk and we cannot
331 * assume that MSI uses the same requester ID. Therefore use the
332 * original device.
333 */
334 if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
335 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
336 PCI_DEVID(PCI_BUS_NUM(data.alias),
337 dev->bus->number));
338 else if (data.pdev->bus->number != dev->bus->number)
339 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
340 else
341 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
342 PCI_DEVID(dev->bus->number, dev->devfn));
Weidong Hanf007e992009-05-23 00:41:15 +0800343
344 return 0;
345}
346
Suresh Siddha95a02e92012-03-30 11:47:07 -0700347static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700348{
349 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100350 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700351 unsigned long flags;
352
353 addr = virt_to_phys((void *)iommu->ir_table->base);
354
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200355 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700356
357 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
358 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
359
360 /* Set interrupt-remapping table pointer */
Jan Kiszkaf63ef692014-08-11 13:13:25 +0200361 writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700362
363 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
364 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200365 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700366
367 /*
368 * global invalidation of interrupt entry cache before enabling
369 * interrupt-remapping.
370 */
371 qi_global_iec(iommu);
372
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200373 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700374
375 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700376 iommu->gcmd |= DMA_GCMD_IRE;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800377 iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
David Woodhousec416daa2009-05-10 20:30:58 +0100378 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700379
380 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
381 readl, (sts & DMA_GSTS_IRES), sts);
382
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800383 /*
384 * With CFI clear in the Global Command register, we should be
385 * protected from dangerous (i.e. compatibility) interrupts
386 * regardless of x2apic status. Check just to be sure.
387 */
388 if (sts & DMA_GSTS_CFIS)
389 WARN(1, KERN_WARNING
390 "Compatibility-format IRQs enabled despite intr remapping;\n"
391 "you are vulnerable to IRQ injection.\n");
392
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200393 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700394}
395
Jiang Liua7a3dad2014-11-09 22:48:00 +0800396static int intel_setup_irq_remapping(struct intel_iommu *iommu)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700397{
398 struct ir_table *ir_table;
399 struct page *pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800400 unsigned long *bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700401
Jiang Liua7a3dad2014-11-09 22:48:00 +0800402 if (iommu->ir_table)
403 return 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700404
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800405 ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800406 if (!ir_table)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700407 return -ENOMEM;
408
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800409 pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO,
Suresh Siddha824cd752009-10-02 11:01:23 -0700410 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700411 if (!pages) {
Jiang Liu360eb3c52014-01-06 14:18:08 +0800412 pr_err("IR%d: failed to allocate pages of order %d\n",
413 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800414 goto out_free_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700415 }
416
Jiang Liu360eb3c52014-01-06 14:18:08 +0800417 bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
418 sizeof(long), GFP_ATOMIC);
419 if (bitmap == NULL) {
420 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800421 goto out_free_pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800422 }
423
Jiang Liub106ee62015-04-13 14:11:32 +0800424 iommu->ir_domain = irq_domain_add_hierarchy(arch_get_ir_parent_domain(),
425 0, INTR_REMAP_TABLE_ENTRIES,
426 NULL, &intel_ir_domain_ops,
427 iommu);
428 if (!iommu->ir_domain) {
429 pr_err("IR%d: failed to allocate irqdomain\n", iommu->seq_id);
430 goto out_free_bitmap;
431 }
432 iommu->ir_msi_domain = arch_create_msi_irq_domain(iommu->ir_domain);
433
Suresh Siddha2ae21012008-07-10 11:16:43 -0700434 ir_table->base = page_address(pages);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800435 ir_table->bitmap = bitmap;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800436 iommu->ir_table = ir_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700437 return 0;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800438
Jiang Liub106ee62015-04-13 14:11:32 +0800439out_free_bitmap:
440 kfree(bitmap);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800441out_free_pages:
442 __free_pages(pages, INTR_REMAP_PAGE_ORDER);
443out_free_table:
444 kfree(ir_table);
445 return -ENOMEM;
446}
447
448static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
449{
450 if (iommu && iommu->ir_table) {
Jiang Liub106ee62015-04-13 14:11:32 +0800451 if (iommu->ir_msi_domain) {
452 irq_domain_remove(iommu->ir_msi_domain);
453 iommu->ir_msi_domain = NULL;
454 }
455 if (iommu->ir_domain) {
456 irq_domain_remove(iommu->ir_domain);
457 iommu->ir_domain = NULL;
458 }
Jiang Liua7a3dad2014-11-09 22:48:00 +0800459 free_pages((unsigned long)iommu->ir_table->base,
460 INTR_REMAP_PAGE_ORDER);
461 kfree(iommu->ir_table->bitmap);
462 kfree(iommu->ir_table);
463 iommu->ir_table = NULL;
464 }
Suresh Siddha2ae21012008-07-10 11:16:43 -0700465}
466
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700467/*
468 * Disable Interrupt Remapping.
469 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700470static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700471{
472 unsigned long flags;
473 u32 sts;
474
475 if (!ecap_ir_support(iommu->ecap))
476 return;
477
Fenghua Yub24696b2009-03-27 14:22:44 -0700478 /*
479 * global invalidation of interrupt entry cache before disabling
480 * interrupt-remapping.
481 */
482 qi_global_iec(iommu);
483
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200484 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700485
486 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
487 if (!(sts & DMA_GSTS_IRES))
488 goto end;
489
490 iommu->gcmd &= ~DMA_GCMD_IRE;
491 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
492
493 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
494 readl, !(sts & DMA_GSTS_IRES), sts);
495
496end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200497 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700498}
499
Suresh Siddha41750d32011-08-23 17:05:18 -0700500static int __init dmar_x2apic_optout(void)
501{
502 struct acpi_table_dmar *dmar;
503 dmar = (struct acpi_table_dmar *)dmar_tbl;
504 if (!dmar || no_x2apic_optout)
505 return 0;
506 return dmar->flags & DMAR_X2APIC_OPT_OUT;
507}
508
Thomas Gleixner11190302015-01-07 15:31:29 +0800509static void __init intel_cleanup_irq_remapping(void)
510{
511 struct dmar_drhd_unit *drhd;
512 struct intel_iommu *iommu;
513
514 for_each_iommu(iommu, drhd) {
515 if (ecap_ir_support(iommu->ecap)) {
516 iommu_disable_irq_remapping(iommu);
517 intel_teardown_irq_remapping(iommu);
518 }
519 }
520
521 if (x2apic_supported())
522 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
523}
524
525static int __init intel_prepare_irq_remapping(void)
526{
527 struct dmar_drhd_unit *drhd;
528 struct intel_iommu *iommu;
529
Jiang Liu2966d952015-01-07 15:31:35 +0800530 if (irq_remap_broken) {
531 printk(KERN_WARNING
532 "This system BIOS has enabled interrupt remapping\n"
533 "on a chipset that contains an erratum making that\n"
534 "feature unstable. To maintain system stability\n"
535 "interrupt remapping is being disabled. Please\n"
536 "contact your BIOS vendor for an update\n");
537 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Jiang Liu2966d952015-01-07 15:31:35 +0800538 return -ENODEV;
539 }
540
Thomas Gleixner11190302015-01-07 15:31:29 +0800541 if (dmar_table_init() < 0)
Jiang Liu2966d952015-01-07 15:31:35 +0800542 return -ENODEV;
543
544 if (!dmar_ir_support())
545 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800546
547 if (parse_ioapics_under_ir() != 1) {
548 printk(KERN_INFO "Not enabling interrupt remapping\n");
549 goto error;
550 }
551
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800552 /* First make sure all IOMMUs support IRQ remapping */
Jiang Liu2966d952015-01-07 15:31:35 +0800553 for_each_iommu(iommu, drhd)
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800554 if (!ecap_ir_support(iommu->ecap))
Thomas Gleixner11190302015-01-07 15:31:29 +0800555 goto error;
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800556
557 /* Do the allocations early */
558 for_each_iommu(iommu, drhd)
559 if (intel_setup_irq_remapping(iommu))
560 goto error;
561
Thomas Gleixner11190302015-01-07 15:31:29 +0800562 return 0;
Jiang Liu2966d952015-01-07 15:31:35 +0800563
Thomas Gleixner11190302015-01-07 15:31:29 +0800564error:
565 intel_cleanup_irq_remapping();
Jiang Liu2966d952015-01-07 15:31:35 +0800566 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800567}
568
Suresh Siddha95a02e92012-03-30 11:47:07 -0700569static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700570{
571 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800572 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100573 bool setup = false;
Suresh Siddha41750d32011-08-23 17:05:18 -0700574 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700575
Thomas Gleixner11190302015-01-07 15:31:29 +0800576 if (x2apic_supported()) {
Suresh Siddha41750d32011-08-23 17:05:18 -0700577 eim = !dmar_x2apic_optout();
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800578 if (!eim)
579 printk(KERN_WARNING
580 "Your BIOS is broken and requested that x2apic be disabled.\n"
581 "This will slightly decrease performance.\n"
582 "Use 'intremap=no_x2apic_optout' to override BIOS request.\n");
Suresh Siddha41750d32011-08-23 17:05:18 -0700583 }
584
Jiang Liu7c919772014-01-06 14:18:18 +0800585 for_each_iommu(iommu, drhd) {
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700586 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800587 * If the queued invalidation is already initialized,
588 * shouldn't disable it.
589 */
590 if (iommu->qi)
591 continue;
592
593 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700594 * Clear previous faults.
595 */
596 dmar_fault(-1, iommu);
597
598 /*
599 * Disable intr remapping and queued invalidation, if already
600 * enabled prior to OS handover.
601 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700602 iommu_disable_irq_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700603
604 dmar_disable_qi(iommu);
605 }
606
Suresh Siddha2ae21012008-07-10 11:16:43 -0700607 /*
608 * check for the Interrupt-remapping support
609 */
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800610 for_each_iommu(iommu, drhd)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700611 if (eim && !ecap_eim_support(iommu->ecap)) {
612 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
613 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
Jiang Liu13d09b62015-01-07 15:31:37 +0800614 eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700615 }
Jiang Liu13d09b62015-01-07 15:31:37 +0800616 eim_mode = eim;
617 if (eim)
618 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700619
620 /*
621 * Enable queued invalidation for all the DRHD's.
622 */
Jiang Liu7c919772014-01-06 14:18:18 +0800623 for_each_iommu(iommu, drhd) {
624 int ret = dmar_enable_qi(iommu);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700625
626 if (ret) {
627 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
628 " invalidation, ecap %Lx, ret %d\n",
629 drhd->reg_base_addr, iommu->ecap, ret);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800630 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700631 }
632 }
633
634 /*
635 * Setup Interrupt-remapping for all the DRHD's now.
636 */
Jiang Liu7c919772014-01-06 14:18:18 +0800637 for_each_iommu(iommu, drhd) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800638 iommu_set_irq_remapping(iommu, eim);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100639 setup = true;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700640 }
641
642 if (!setup)
643 goto error;
644
Suresh Siddha95a02e92012-03-30 11:47:07 -0700645 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200646
Suresh Siddha41750d32011-08-23 17:05:18 -0700647 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700648
Suresh Siddha41750d32011-08-23 17:05:18 -0700649 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700650
651error:
Thomas Gleixner11190302015-01-07 15:31:29 +0800652 intel_cleanup_irq_remapping();
Suresh Siddha2ae21012008-07-10 11:16:43 -0700653 return -1;
654}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700655
Jiang Liua7a3dad2014-11-09 22:48:00 +0800656static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
657 struct intel_iommu *iommu,
658 struct acpi_dmar_hardware_unit *drhd)
Suresh Siddha20f30972009-08-04 12:07:08 -0700659{
660 struct acpi_dmar_pci_path *path;
661 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800662 int count, free = -1;
Suresh Siddha20f30972009-08-04 12:07:08 -0700663
664 bus = scope->bus;
665 path = (struct acpi_dmar_pci_path *)(scope + 1);
666 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
667 / sizeof(struct acpi_dmar_pci_path);
668
669 while (--count > 0) {
670 /*
671 * Access PCI directly due to the PCI
672 * subsystem isn't initialized yet.
673 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800674 bus = read_pci_config_byte(bus, path->device, path->function,
Suresh Siddha20f30972009-08-04 12:07:08 -0700675 PCI_SECONDARY_BUS);
676 path++;
677 }
Jiang Liua7a3dad2014-11-09 22:48:00 +0800678
679 for (count = 0; count < MAX_HPET_TBS; count++) {
680 if (ir_hpet[count].iommu == iommu &&
681 ir_hpet[count].id == scope->enumeration_id)
682 return 0;
683 else if (ir_hpet[count].iommu == NULL && free == -1)
684 free = count;
685 }
686 if (free == -1) {
687 pr_warn("Exceeded Max HPET blocks\n");
688 return -ENOSPC;
689 }
690
691 ir_hpet[free].iommu = iommu;
692 ir_hpet[free].id = scope->enumeration_id;
693 ir_hpet[free].bus = bus;
694 ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
695 pr_info("HPET id %d under DRHD base 0x%Lx\n",
696 scope->enumeration_id, drhd->address);
697
698 return 0;
Suresh Siddha20f30972009-08-04 12:07:08 -0700699}
700
Jiang Liua7a3dad2014-11-09 22:48:00 +0800701static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
702 struct intel_iommu *iommu,
703 struct acpi_dmar_hardware_unit *drhd)
Weidong Hanf007e992009-05-23 00:41:15 +0800704{
705 struct acpi_dmar_pci_path *path;
706 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800707 int count, free = -1;
Weidong Hanf007e992009-05-23 00:41:15 +0800708
709 bus = scope->bus;
710 path = (struct acpi_dmar_pci_path *)(scope + 1);
711 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
712 / sizeof(struct acpi_dmar_pci_path);
713
714 while (--count > 0) {
715 /*
716 * Access PCI directly due to the PCI
717 * subsystem isn't initialized yet.
718 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800719 bus = read_pci_config_byte(bus, path->device, path->function,
Weidong Hanf007e992009-05-23 00:41:15 +0800720 PCI_SECONDARY_BUS);
721 path++;
722 }
723
Jiang Liua7a3dad2014-11-09 22:48:00 +0800724 for (count = 0; count < MAX_IO_APICS; count++) {
725 if (ir_ioapic[count].iommu == iommu &&
726 ir_ioapic[count].id == scope->enumeration_id)
727 return 0;
728 else if (ir_ioapic[count].iommu == NULL && free == -1)
729 free = count;
730 }
731 if (free == -1) {
732 pr_warn("Exceeded Max IO APICS\n");
733 return -ENOSPC;
734 }
735
736 ir_ioapic[free].bus = bus;
737 ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
738 ir_ioapic[free].iommu = iommu;
739 ir_ioapic[free].id = scope->enumeration_id;
740 pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
741 scope->enumeration_id, drhd->address, iommu->seq_id);
742
743 return 0;
Weidong Hanf007e992009-05-23 00:41:15 +0800744}
745
Suresh Siddha20f30972009-08-04 12:07:08 -0700746static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
747 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700748{
Jiang Liua7a3dad2014-11-09 22:48:00 +0800749 int ret = 0;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700750 struct acpi_dmar_hardware_unit *drhd;
751 struct acpi_dmar_device_scope *scope;
752 void *start, *end;
753
754 drhd = (struct acpi_dmar_hardware_unit *)header;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700755 start = (void *)(drhd + 1);
756 end = ((void *)drhd) + header->length;
757
Jiang Liua7a3dad2014-11-09 22:48:00 +0800758 while (start < end && ret == 0) {
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700759 scope = start;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800760 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
761 ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
762 else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
763 ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700764 start += scope->length;
765 }
766
Jiang Liua7a3dad2014-11-09 22:48:00 +0800767 return ret;
768}
769
770static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
771{
772 int i;
773
774 for (i = 0; i < MAX_HPET_TBS; i++)
775 if (ir_hpet[i].iommu == iommu)
776 ir_hpet[i].iommu = NULL;
777
778 for (i = 0; i < MAX_IO_APICS; i++)
779 if (ir_ioapic[i].iommu == iommu)
780 ir_ioapic[i].iommu = NULL;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700781}
782
783/*
784 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
785 * hardware unit.
786 */
Jiang Liu694835d2014-01-06 14:18:16 +0800787static int __init parse_ioapics_under_ir(void)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700788{
789 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800790 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100791 bool ir_supported = false;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500792 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700793
Jiang Liu7c919772014-01-06 14:18:18 +0800794 for_each_iommu(iommu, drhd)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700795 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700796 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700797 return -1;
798
Quentin Lambert2f119c72015-02-06 10:59:53 +0100799 ir_supported = true;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700800 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700801
Seth Forshee32ab31e2012-08-08 08:27:03 -0500802 if (!ir_supported)
803 return 0;
804
805 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
806 int ioapic_id = mpc_ioapic_id(ioapic_idx);
807 if (!map_ioapic_to_ir(ioapic_id)) {
808 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
809 "interrupt remapping will be disabled\n",
810 ioapic_id);
811 return -1;
812 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700813 }
814
Seth Forshee32ab31e2012-08-08 08:27:03 -0500815 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700816}
Fenghua Yub24696b2009-03-27 14:22:44 -0700817
Rashika Kheria6a7885c2013-12-18 12:04:27 +0530818static int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700819{
Jiang Liu3a5670e2014-02-19 14:07:33 +0800820 int ret;
821
Suresh Siddha95a02e92012-03-30 11:47:07 -0700822 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700823 return 0;
824
Jiang Liu3a5670e2014-02-19 14:07:33 +0800825 down_write(&dmar_global_lock);
826 ret = dmar_dev_scope_init();
827 up_write(&dmar_global_lock);
828
829 return ret;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700830}
831rootfs_initcall(ir_dev_scope_init);
832
Suresh Siddha95a02e92012-03-30 11:47:07 -0700833static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700834{
835 struct dmar_drhd_unit *drhd;
836 struct intel_iommu *iommu = NULL;
837
838 /*
839 * Disable Interrupt-remapping for all the DRHD's now.
840 */
841 for_each_iommu(iommu, drhd) {
842 if (!ecap_ir_support(iommu->ecap))
843 continue;
844
Suresh Siddha95a02e92012-03-30 11:47:07 -0700845 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700846 }
847}
848
Suresh Siddha95a02e92012-03-30 11:47:07 -0700849static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700850{
851 struct dmar_drhd_unit *drhd;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100852 bool setup = false;
Fenghua Yub24696b2009-03-27 14:22:44 -0700853 struct intel_iommu *iommu = NULL;
854
855 for_each_iommu(iommu, drhd)
856 if (iommu->qi)
857 dmar_reenable_qi(iommu);
858
859 /*
860 * Setup Interrupt-remapping for all the DRHD's now.
861 */
862 for_each_iommu(iommu, drhd) {
863 if (!ecap_ir_support(iommu->ecap))
864 continue;
865
866 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -0700867 iommu_set_irq_remapping(iommu, eim);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100868 setup = true;
Fenghua Yub24696b2009-03-27 14:22:44 -0700869 }
870
871 if (!setup)
872 goto error;
873
874 return 0;
875
876error:
877 /*
878 * handle error condition gracefully here!
879 */
880 return -1;
881}
882
Jiang Liu3c6e5672015-04-14 10:29:47 +0800883static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700884{
885 memset(irte, 0, sizeof(*irte));
886
887 irte->present = 1;
888 irte->dst_mode = apic->irq_dest_mode;
889 /*
890 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
891 * actual level or edge trigger will be setup in the IO-APIC
892 * RTE. This will help simplify level triggered irq migration.
893 * For more details, see the comments (in io_apic.c) explainig IO-APIC
894 * irq migration in the presence of interrupt-remapping.
895 */
896 irte->trigger_mode = 0;
897 irte->dlvry_mode = apic->irq_delivery_mode;
898 irte->vector = vector;
899 irte->dest_id = IRTE_DEST(dest);
900 irte->redir_hint = 1;
901}
902
Jiang Liub106ee62015-04-13 14:11:32 +0800903static struct irq_domain *intel_get_ir_irq_domain(struct irq_alloc_info *info)
904{
905 struct intel_iommu *iommu = NULL;
906
907 if (!info)
908 return NULL;
909
910 switch (info->type) {
911 case X86_IRQ_ALLOC_TYPE_IOAPIC:
912 iommu = map_ioapic_to_ir(info->ioapic_id);
913 break;
914 case X86_IRQ_ALLOC_TYPE_HPET:
915 iommu = map_hpet_to_ir(info->hpet_id);
916 break;
917 case X86_IRQ_ALLOC_TYPE_MSI:
918 case X86_IRQ_ALLOC_TYPE_MSIX:
919 iommu = map_dev_to_ir(info->msi_dev);
920 break;
921 default:
922 BUG_ON(1);
923 break;
924 }
925
926 return iommu ? iommu->ir_domain : NULL;
927}
928
929static struct irq_domain *intel_get_irq_domain(struct irq_alloc_info *info)
930{
931 struct intel_iommu *iommu;
932
933 if (!info)
934 return NULL;
935
936 switch (info->type) {
937 case X86_IRQ_ALLOC_TYPE_MSI:
938 case X86_IRQ_ALLOC_TYPE_MSIX:
939 iommu = map_dev_to_ir(info->msi_dev);
940 if (iommu)
941 return iommu->ir_msi_domain;
942 break;
943 default:
944 break;
945 }
946
947 return NULL;
948}
949
Joerg Roedel736baef2012-03-30 11:47:00 -0700950struct irq_remap_ops intel_irq_remap_ops = {
Thomas Gleixner11190302015-01-07 15:31:29 +0800951 .prepare = intel_prepare_irq_remapping,
Suresh Siddha95a02e92012-03-30 11:47:07 -0700952 .enable = intel_enable_irq_remapping,
953 .disable = disable_irq_remapping,
954 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700955 .enable_faulting = enable_drhd_fault_handling,
Jiang Liub106ee62015-04-13 14:11:32 +0800956 .get_ir_irq_domain = intel_get_ir_irq_domain,
957 .get_irq_domain = intel_get_irq_domain,
958};
959
960/*
961 * Migrate the IO-APIC irq in the presence of intr-remapping.
962 *
963 * For both level and edge triggered, irq migration is a simple atomic
964 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
965 *
966 * For level triggered, we eliminate the io-apic RTE modification (with the
967 * updated vector information), by using a virtual vector (io-apic pin number).
968 * Real vector that is used for interrupting cpu will be coming from
969 * the interrupt-remapping table entry.
970 *
971 * As the migration is a simple atomic update of IRTE, the same mechanism
972 * is used to migrate MSI irq's in the presence of interrupt-remapping.
973 */
974static int
975intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
976 bool force)
977{
978 struct intel_ir_data *ir_data = data->chip_data;
979 struct irte *irte = &ir_data->irte_entry;
980 struct irq_cfg *cfg = irqd_cfg(data);
981 struct irq_data *parent = data->parent_data;
982 int ret;
983
984 ret = parent->chip->irq_set_affinity(parent, mask, force);
985 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
986 return ret;
987
988 /*
989 * Atomically updates the IRTE with the new destination, vector
990 * and flushes the interrupt entry cache.
991 */
992 irte->vector = cfg->vector;
993 irte->dest_id = IRTE_DEST(cfg->dest_apicid);
994 modify_irte(&ir_data->irq_2_iommu, irte);
995
996 /*
997 * After this point, all the interrupts will start arriving
998 * at the new destination. So, time to cleanup the previous
999 * vector allocation.
1000 */
1001 if (cfg->move_in_progress)
1002 send_cleanup_vector(cfg);
1003
1004 return IRQ_SET_MASK_OK_DONE;
1005}
1006
1007static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
1008 struct msi_msg *msg)
1009{
1010 struct intel_ir_data *ir_data = irq_data->chip_data;
1011
1012 *msg = ir_data->msi_entry;
1013}
1014
1015static struct irq_chip intel_ir_chip = {
1016 .irq_ack = ir_ack_apic_edge,
1017 .irq_set_affinity = intel_ir_set_affinity,
1018 .irq_compose_msi_msg = intel_ir_compose_msi_msg,
1019};
1020
1021static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
1022 struct irq_cfg *irq_cfg,
1023 struct irq_alloc_info *info,
1024 int index, int sub_handle)
1025{
1026 struct IR_IO_APIC_route_entry *entry;
1027 struct irte *irte = &data->irte_entry;
1028 struct msi_msg *msg = &data->msi_entry;
1029
1030 prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
1031 switch (info->type) {
1032 case X86_IRQ_ALLOC_TYPE_IOAPIC:
1033 /* Set source-id of interrupt request */
1034 set_ioapic_sid(irte, info->ioapic_id);
1035 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",
1036 info->ioapic_id, irte->present, irte->fpd,
1037 irte->dst_mode, irte->redir_hint,
1038 irte->trigger_mode, irte->dlvry_mode,
1039 irte->avail, irte->vector, irte->dest_id,
1040 irte->sid, irte->sq, irte->svt);
1041
1042 entry = (struct IR_IO_APIC_route_entry *)info->ioapic_entry;
1043 info->ioapic_entry = NULL;
1044 memset(entry, 0, sizeof(*entry));
1045 entry->index2 = (index >> 15) & 0x1;
1046 entry->zero = 0;
1047 entry->format = 1;
1048 entry->index = (index & 0x7fff);
1049 /*
1050 * IO-APIC RTE will be configured with virtual vector.
1051 * irq handler will do the explicit EOI to the io-apic.
1052 */
1053 entry->vector = info->ioapic_pin;
1054 entry->mask = 0; /* enable IRQ */
1055 entry->trigger = info->ioapic_trigger;
1056 entry->polarity = info->ioapic_polarity;
1057 if (info->ioapic_trigger)
1058 entry->mask = 1; /* Mask level triggered irqs. */
1059 break;
1060
1061 case X86_IRQ_ALLOC_TYPE_HPET:
1062 case X86_IRQ_ALLOC_TYPE_MSI:
1063 case X86_IRQ_ALLOC_TYPE_MSIX:
1064 if (info->type == X86_IRQ_ALLOC_TYPE_HPET)
1065 set_hpet_sid(irte, info->hpet_id);
1066 else
1067 set_msi_sid(irte, info->msi_dev);
1068
1069 msg->address_hi = MSI_ADDR_BASE_HI;
1070 msg->data = sub_handle;
1071 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1072 MSI_ADDR_IR_SHV |
1073 MSI_ADDR_IR_INDEX1(index) |
1074 MSI_ADDR_IR_INDEX2(index);
1075 break;
1076
1077 default:
1078 BUG_ON(1);
1079 break;
1080 }
1081}
1082
1083static void intel_free_irq_resources(struct irq_domain *domain,
1084 unsigned int virq, unsigned int nr_irqs)
1085{
1086 struct irq_data *irq_data;
1087 struct intel_ir_data *data;
1088 struct irq_2_iommu *irq_iommu;
1089 unsigned long flags;
1090 int i;
1091
1092 for (i = 0; i < nr_irqs; i++) {
1093 irq_data = irq_domain_get_irq_data(domain, virq + i);
1094 if (irq_data && irq_data->chip_data) {
1095 data = irq_data->chip_data;
1096 irq_iommu = &data->irq_2_iommu;
1097 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
1098 clear_entries(irq_iommu);
1099 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
1100 irq_domain_reset_irq_data(irq_data);
1101 kfree(data);
1102 }
1103 }
1104}
1105
1106static int intel_irq_remapping_alloc(struct irq_domain *domain,
1107 unsigned int virq, unsigned int nr_irqs,
1108 void *arg)
1109{
1110 struct intel_iommu *iommu = domain->host_data;
1111 struct irq_alloc_info *info = arg;
1112 struct intel_ir_data *data;
1113 struct irq_data *irq_data;
1114 struct irq_cfg *irq_cfg;
1115 int i, ret, index;
1116
1117 if (!info || !iommu)
1118 return -EINVAL;
1119 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
1120 info->type != X86_IRQ_ALLOC_TYPE_MSIX)
1121 return -EINVAL;
1122
1123 /*
1124 * With IRQ remapping enabled, don't need contiguous CPU vectors
1125 * to support multiple MSI interrupts.
1126 */
1127 if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
1128 info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
1129
1130 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
1131 if (ret < 0)
1132 return ret;
1133
1134 ret = -ENOMEM;
1135 data = kzalloc(sizeof(*data), GFP_KERNEL);
1136 if (!data)
1137 goto out_free_parent;
1138
1139 down_read(&dmar_global_lock);
1140 index = alloc_irte(iommu, virq, &data->irq_2_iommu, nr_irqs);
1141 up_read(&dmar_global_lock);
1142 if (index < 0) {
1143 pr_warn("Failed to allocate IRTE\n");
1144 kfree(data);
1145 goto out_free_parent;
1146 }
1147
1148 for (i = 0; i < nr_irqs; i++) {
1149 irq_data = irq_domain_get_irq_data(domain, virq + i);
1150 irq_cfg = irqd_cfg(irq_data);
1151 if (!irq_data || !irq_cfg) {
1152 ret = -EINVAL;
1153 goto out_free_data;
1154 }
1155
1156 if (i > 0) {
1157 data = kzalloc(sizeof(*data), GFP_KERNEL);
1158 if (!data)
1159 goto out_free_data;
1160 }
1161 irq_data->hwirq = (index << 16) + i;
1162 irq_data->chip_data = data;
1163 irq_data->chip = &intel_ir_chip;
1164 intel_irq_remapping_prepare_irte(data, irq_cfg, info, index, i);
1165 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
1166 }
1167 return 0;
1168
1169out_free_data:
1170 intel_free_irq_resources(domain, virq, i);
1171out_free_parent:
1172 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1173 return ret;
1174}
1175
1176static void intel_irq_remapping_free(struct irq_domain *domain,
1177 unsigned int virq, unsigned int nr_irqs)
1178{
1179 intel_free_irq_resources(domain, virq, nr_irqs);
1180 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1181}
1182
1183static void intel_irq_remapping_activate(struct irq_domain *domain,
1184 struct irq_data *irq_data)
1185{
1186 struct intel_ir_data *data = irq_data->chip_data;
1187
1188 modify_irte(&data->irq_2_iommu, &data->irte_entry);
1189}
1190
1191static void intel_irq_remapping_deactivate(struct irq_domain *domain,
1192 struct irq_data *irq_data)
1193{
1194 struct intel_ir_data *data = irq_data->chip_data;
1195 struct irte entry;
1196
1197 memset(&entry, 0, sizeof(entry));
1198 modify_irte(&data->irq_2_iommu, &entry);
1199}
1200
1201static struct irq_domain_ops intel_ir_domain_ops = {
1202 .alloc = intel_irq_remapping_alloc,
1203 .free = intel_irq_remapping_free,
1204 .activate = intel_irq_remapping_activate,
1205 .deactivate = intel_irq_remapping_deactivate,
Joerg Roedel736baef2012-03-30 11:47:00 -07001206};
Jiang Liu6b197242014-11-09 22:47:58 +08001207
Jiang Liua7a3dad2014-11-09 22:48:00 +08001208/*
1209 * Support of Interrupt Remapping Unit Hotplug
1210 */
1211static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
1212{
1213 int ret;
1214 int eim = x2apic_enabled();
1215
1216 if (eim && !ecap_eim_support(iommu->ecap)) {
1217 pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
1218 iommu->reg_phys, iommu->ecap);
1219 return -ENODEV;
1220 }
1221
1222 if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
1223 pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
1224 iommu->reg_phys);
1225 return -ENODEV;
1226 }
1227
1228 /* TODO: check all IOAPICs are covered by IOMMU */
1229
1230 /* Setup Interrupt-remapping now. */
1231 ret = intel_setup_irq_remapping(iommu);
1232 if (ret) {
1233 pr_err("DRHD %Lx: failed to allocate resource\n",
1234 iommu->reg_phys);
1235 ir_remove_ioapic_hpet_scope(iommu);
1236 return ret;
1237 }
1238
1239 if (!iommu->qi) {
1240 /* Clear previous faults. */
1241 dmar_fault(-1, iommu);
1242 iommu_disable_irq_remapping(iommu);
1243 dmar_disable_qi(iommu);
1244 }
1245
1246 /* Enable queued invalidation */
1247 ret = dmar_enable_qi(iommu);
1248 if (!ret) {
1249 iommu_set_irq_remapping(iommu, eim);
1250 } else {
1251 pr_err("DRHD %Lx: failed to enable queued invalidation, ecap %Lx, ret %d\n",
1252 iommu->reg_phys, iommu->ecap, ret);
1253 intel_teardown_irq_remapping(iommu);
1254 ir_remove_ioapic_hpet_scope(iommu);
1255 }
1256
1257 return ret;
1258}
1259
Jiang Liu6b197242014-11-09 22:47:58 +08001260int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
1261{
Jiang Liua7a3dad2014-11-09 22:48:00 +08001262 int ret = 0;
1263 struct intel_iommu *iommu = dmaru->iommu;
1264
1265 if (!irq_remapping_enabled)
1266 return 0;
1267 if (iommu == NULL)
1268 return -EINVAL;
1269 if (!ecap_ir_support(iommu->ecap))
1270 return 0;
1271
1272 if (insert) {
1273 if (!iommu->ir_table)
1274 ret = dmar_ir_add(dmaru, iommu);
1275 } else {
1276 if (iommu->ir_table) {
1277 if (!bitmap_empty(iommu->ir_table->bitmap,
1278 INTR_REMAP_TABLE_ENTRIES)) {
1279 ret = -EBUSY;
1280 } else {
1281 iommu_disable_irq_remapping(iommu);
1282 intel_teardown_irq_remapping(iommu);
1283 ir_remove_ioapic_hpet_scope(iommu);
1284 }
1285 }
1286 }
1287
1288 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08001289}