blob: 10d3187e5fa07215030c69353466c77206f159ad [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>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -07009#include <asm/io_apic.h>
Yinghai Lu17483a12008-12-12 13:14:18 -080010#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053011#include <asm/cpu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030012#include <linux/intel-iommu.h>
Alexander Beregalov46f06b722009-04-06 16:45:28 +010013#include <acpi/acpi.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070014#include <asm/irq_remapping.h>
Weidong Hanf007e992009-05-23 00:41:15 +080015#include <asm/pci-direct.h>
Joerg Roedel5e2b9302012-03-30 11:47:05 -070016#include <asm/msidef.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070017
Suresh Siddha8a8f4222012-03-30 11:47:08 -070018#include "irq_remapping.h"
Joerg Roedel736baef2012-03-30 11:47:00 -070019
Joerg Roedeleef93fd2012-03-30 11:46:59 -070020struct ioapic_scope {
21 struct intel_iommu *iommu;
22 unsigned int id;
23 unsigned int bus; /* PCI bus number */
24 unsigned int devfn; /* PCI devfn number */
25};
26
27struct hpet_scope {
28 struct intel_iommu *iommu;
29 u8 id;
30 unsigned int bus;
31 unsigned int devfn;
32};
33
34#define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
Joerg Roedel0c3f1732012-03-30 11:47:02 -070035#define IRTE_DEST(dest) ((x2apic_mode) ? dest : dest << 8)
Joerg Roedeleef93fd2012-03-30 11:46:59 -070036
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070037static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070038static struct hpet_scope ir_hpet[MAX_HPET_TBS];
39static int ir_ioapic_num, ir_hpet_num;
Chris Wrightd1423d52010-07-20 11:06:49 -070040
Thomas Gleixner96f8e982011-07-19 16:28:19 +020041static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Thomas Gleixnerd585d062010-10-10 12:34:27 +020042
Jiang Liu694835d2014-01-06 14:18:16 +080043static int __init parse_ioapics_under_ir(void);
44
Yinghai Lue420dfb2008-08-19 20:50:21 -070045static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
46{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020047 struct irq_cfg *cfg = irq_get_chip_data(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020048 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080049}
50
Rashika Kheria6a7885c2013-12-18 12:04:27 +053051static int get_irte(int irq, struct irte *entry)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070052{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020053 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070054 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020055 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070056
Thomas Gleixnerd585d062010-10-10 12:34:27 +020057 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070058 return -1;
59
Thomas Gleixner96f8e982011-07-19 16:28:19 +020060 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070061
Yinghai Lue420dfb2008-08-19 20:50:21 -070062 index = irq_iommu->irte_index + irq_iommu->sub_handle;
63 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070064
Thomas Gleixner96f8e982011-07-19 16:28:19 +020065 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070066 return 0;
67}
68
Joerg Roedel263b5e82012-03-30 11:47:06 -070069static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070070{
71 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020072 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Joerg Roedel9b1b0e42012-09-26 12:44:45 +020073 struct irq_cfg *cfg = irq_get_chip_data(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070074 u16 index, start_index;
75 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070076 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070077
Thomas Gleixnerd585d062010-10-10 12:34:27 +020078 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070079 return -1;
80
81 /*
82 * start the IRTE search from index 0.
83 */
84 index = start_index = 0;
85
86 if (count > 1) {
87 count = __roundup_pow_of_two(count);
88 mask = ilog2(count);
89 }
90
91 if (mask > ecap_max_handle_mask(iommu->ecap)) {
92 printk(KERN_ERR
93 "Requested mask %x exceeds the max invalidation handle"
94 " mask value %Lx\n", mask,
95 ecap_max_handle_mask(iommu->ecap));
96 return -1;
97 }
98
Thomas Gleixner96f8e982011-07-19 16:28:19 +020099 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800100 index = bitmap_find_free_region(table->bitmap,
101 INTR_REMAP_TABLE_ENTRIES, mask);
102 if (index < 0) {
103 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
104 } else {
105 cfg->remapped = 1;
106 irq_iommu->iommu = iommu;
107 irq_iommu->irte_index = index;
108 irq_iommu->sub_handle = 0;
109 irq_iommu->irte_mask = mask;
110 }
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200111 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700112
113 return index;
114}
115
Yu Zhao704126a2009-01-04 16:28:52 +0800116static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700117{
118 struct qi_desc desc;
119
120 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
121 | QI_IEC_SELECTIVE;
122 desc.high = 0;
123
Yu Zhao704126a2009-01-04 16:28:52 +0800124 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700125}
126
Joerg Roedel263b5e82012-03-30 11:47:06 -0700127static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700128{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200129 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700130 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200131 int index;
132
133 if (!irq_iommu)
134 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700135
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200136 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700137 *sub_handle = irq_iommu->sub_handle;
138 index = irq_iommu->irte_index;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200139 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700140 return index;
141}
142
Joerg Roedel263b5e82012-03-30 11:47:06 -0700143static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700144{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200145 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200146 struct irq_cfg *cfg = irq_get_chip_data(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700147 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700148
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200149 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800150 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200151
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200152 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800153
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200154 cfg->remapped = 1;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700155 irq_iommu->iommu = iommu;
156 irq_iommu->irte_index = index;
157 irq_iommu->sub_handle = subhandle;
158 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700159
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200160 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700161
162 return 0;
163}
164
Joerg Roedel263b5e82012-03-30 11:47:06 -0700165static int modify_irte(int irq, struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700166{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200167 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700168 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700169 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200170 struct irte *irte;
171 int rc, index;
172
173 if (!irq_iommu)
174 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700175
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200176 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700177
Yinghai Lue420dfb2008-08-19 20:50:21 -0700178 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700179
Yinghai Lue420dfb2008-08-19 20:50:21 -0700180 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700181 irte = &iommu->ir_table->base[index];
182
Linus Torvaldsc513b672010-08-06 11:02:31 -0700183 set_64bit(&irte->low, irte_modified->low);
184 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700185 __iommu_flush_cache(iommu, irte, sizeof(*irte));
186
Yu Zhao704126a2009-01-04 16:28:52 +0800187 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200188 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800189
190 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700191}
192
Joerg Roedel263b5e82012-03-30 11:47:06 -0700193static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700194{
195 int i;
196
197 for (i = 0; i < MAX_HPET_TBS; i++)
198 if (ir_hpet[i].id == hpet_id)
199 return ir_hpet[i].iommu;
200 return NULL;
201}
202
Joerg Roedel263b5e82012-03-30 11:47:06 -0700203static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700204{
205 int i;
206
207 for (i = 0; i < MAX_IO_APICS; i++)
208 if (ir_ioapic[i].id == apic)
209 return ir_ioapic[i].iommu;
210 return NULL;
211}
212
Joerg Roedel263b5e82012-03-30 11:47:06 -0700213static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700214{
215 struct dmar_drhd_unit *drhd;
216
217 drhd = dmar_find_matched_drhd_unit(dev);
218 if (!drhd)
219 return NULL;
220
221 return drhd->iommu;
222}
223
Weidong Hanc4658b42009-05-23 00:41:14 +0800224static int clear_entries(struct irq_2_iommu *irq_iommu)
225{
226 struct irte *start, *entry, *end;
227 struct intel_iommu *iommu;
228 int index;
229
230 if (irq_iommu->sub_handle)
231 return 0;
232
233 iommu = irq_iommu->iommu;
234 index = irq_iommu->irte_index + irq_iommu->sub_handle;
235
236 start = iommu->ir_table->base + index;
237 end = start + (1 << irq_iommu->irte_mask);
238
239 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700240 set_64bit(&entry->low, 0);
241 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800242 }
Jiang Liu360eb3c52014-01-06 14:18:08 +0800243 bitmap_release_region(iommu->ir_table->bitmap, index,
244 irq_iommu->irte_mask);
Weidong Hanc4658b42009-05-23 00:41:14 +0800245
246 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
247}
248
Joerg Roedel9d619f62012-03-30 11:47:04 -0700249static int free_irte(int irq)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700250{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200251 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700252 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200253 int rc;
254
255 if (!irq_iommu)
256 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700257
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200258 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700259
Weidong Hanc4658b42009-05-23 00:41:14 +0800260 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700261
Yinghai Lue420dfb2008-08-19 20:50:21 -0700262 irq_iommu->iommu = NULL;
263 irq_iommu->irte_index = 0;
264 irq_iommu->sub_handle = 0;
265 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700266
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200267 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700268
Yu Zhao704126a2009-01-04 16:28:52 +0800269 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700270}
271
Weidong Hanf007e992009-05-23 00:41:15 +0800272/*
273 * source validation type
274 */
275#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300276#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800277#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
278
279/*
280 * source-id qualifier
281 */
282#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
283#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
284 * the third least significant bit
285 */
286#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
287 * the second and third least significant bits
288 */
289#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
290 * the least three significant bits
291 */
292
293/*
294 * set SVT, SQ and SID fields of irte to verify
295 * source ids of interrupt requests
296 */
297static void set_irte_sid(struct irte *irte, unsigned int svt,
298 unsigned int sq, unsigned int sid)
299{
Chris Wrightd1423d52010-07-20 11:06:49 -0700300 if (disable_sourceid_checking)
301 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800302 irte->svt = svt;
303 irte->sq = sq;
304 irte->sid = sid;
305}
306
Joerg Roedel263b5e82012-03-30 11:47:06 -0700307static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800308{
309 int i;
310 u16 sid = 0;
311
312 if (!irte)
313 return -1;
314
315 for (i = 0; i < MAX_IO_APICS; i++) {
316 if (ir_ioapic[i].id == apic) {
317 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
318 break;
319 }
320 }
321
322 if (sid == 0) {
323 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
324 return -1;
325 }
326
327 set_irte_sid(irte, 1, 0, sid);
328
329 return 0;
330}
331
Joerg Roedel263b5e82012-03-30 11:47:06 -0700332static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700333{
334 int i;
335 u16 sid = 0;
336
337 if (!irte)
338 return -1;
339
340 for (i = 0; i < MAX_HPET_TBS; i++) {
341 if (ir_hpet[i].id == id) {
342 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
343 break;
344 }
345 }
346
347 if (sid == 0) {
348 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
349 return -1;
350 }
351
352 /*
353 * Should really use SQ_ALL_16. Some platforms are broken.
354 * While we figure out the right quirks for these broken platforms, use
355 * SQ_13_IGNORE_3 for now.
356 */
357 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
358
359 return 0;
360}
361
Joerg Roedel263b5e82012-03-30 11:47:06 -0700362static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800363{
364 struct pci_dev *bridge;
365
366 if (!irte || !dev)
367 return -1;
368
369 /* PCIe device or Root Complex integrated PCI device */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900370 if (pci_is_pcie(dev) || !dev->bus->parent) {
Weidong Hanf007e992009-05-23 00:41:15 +0800371 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
372 (dev->bus->number << 8) | dev->devfn);
373 return 0;
374 }
375
376 bridge = pci_find_upstream_pcie_bridge(dev);
377 if (bridge) {
Stefan Assmann45e829e2009-12-03 06:49:24 -0500378 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
Weidong Hanf007e992009-05-23 00:41:15 +0800379 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
380 (bridge->bus->number << 8) | dev->bus->number);
381 else /* this is a legacy PCI bridge */
382 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
383 (bridge->bus->number << 8) | bridge->devfn);
384 }
385
386 return 0;
387}
388
Suresh Siddha95a02e92012-03-30 11:47:07 -0700389static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700390{
391 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100392 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700393 unsigned long flags;
394
395 addr = virt_to_phys((void *)iommu->ir_table->base);
396
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200397 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700398
399 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
400 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
401
402 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800403 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100404 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700405
406 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
407 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200408 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700409
410 /*
411 * global invalidation of interrupt entry cache before enabling
412 * interrupt-remapping.
413 */
414 qi_global_iec(iommu);
415
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200416 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700417
418 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700419 iommu->gcmd |= DMA_GCMD_IRE;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800420 iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
David Woodhousec416daa2009-05-10 20:30:58 +0100421 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700422
423 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
424 readl, (sts & DMA_GSTS_IRES), sts);
425
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800426 /*
427 * With CFI clear in the Global Command register, we should be
428 * protected from dangerous (i.e. compatibility) interrupts
429 * regardless of x2apic status. Check just to be sure.
430 */
431 if (sts & DMA_GSTS_CFIS)
432 WARN(1, KERN_WARNING
433 "Compatibility-format IRQs enabled despite intr remapping;\n"
434 "you are vulnerable to IRQ injection.\n");
435
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200436 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700437}
438
439
Suresh Siddha95a02e92012-03-30 11:47:07 -0700440static int intel_setup_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700441{
442 struct ir_table *ir_table;
443 struct page *pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800444 unsigned long *bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700445
446 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700447 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700448
449 if (!iommu->ir_table)
450 return -ENOMEM;
451
Suresh Siddha824cd752009-10-02 11:01:23 -0700452 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
453 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700454
455 if (!pages) {
Jiang Liu360eb3c52014-01-06 14:18:08 +0800456 pr_err("IR%d: failed to allocate pages of order %d\n",
457 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700458 kfree(iommu->ir_table);
459 return -ENOMEM;
460 }
461
Jiang Liu360eb3c52014-01-06 14:18:08 +0800462 bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
463 sizeof(long), GFP_ATOMIC);
464 if (bitmap == NULL) {
465 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
466 __free_pages(pages, INTR_REMAP_PAGE_ORDER);
467 kfree(ir_table);
468 return -ENOMEM;
469 }
470
Suresh Siddha2ae21012008-07-10 11:16:43 -0700471 ir_table->base = page_address(pages);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800472 ir_table->bitmap = bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700473
Suresh Siddha95a02e92012-03-30 11:47:07 -0700474 iommu_set_irq_remapping(iommu, mode);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700475 return 0;
476}
477
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700478/*
479 * Disable Interrupt Remapping.
480 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700481static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700482{
483 unsigned long flags;
484 u32 sts;
485
486 if (!ecap_ir_support(iommu->ecap))
487 return;
488
Fenghua Yub24696b2009-03-27 14:22:44 -0700489 /*
490 * global invalidation of interrupt entry cache before disabling
491 * interrupt-remapping.
492 */
493 qi_global_iec(iommu);
494
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200495 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700496
497 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
498 if (!(sts & DMA_GSTS_IRES))
499 goto end;
500
501 iommu->gcmd &= ~DMA_GCMD_IRE;
502 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
503
504 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
505 readl, !(sts & DMA_GSTS_IRES), sts);
506
507end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200508 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700509}
510
Suresh Siddha41750d32011-08-23 17:05:18 -0700511static int __init dmar_x2apic_optout(void)
512{
513 struct acpi_table_dmar *dmar;
514 dmar = (struct acpi_table_dmar *)dmar_tbl;
515 if (!dmar || no_x2apic_optout)
516 return 0;
517 return dmar->flags & DMAR_X2APIC_OPT_OUT;
518}
519
Suresh Siddha95a02e92012-03-30 11:47:07 -0700520static int __init intel_irq_remapping_supported(void)
Weidong Han93758232009-04-17 16:42:14 +0800521{
522 struct dmar_drhd_unit *drhd;
523
Suresh Siddha95a02e92012-03-30 11:47:07 -0700524 if (disable_irq_remap)
Weidong Han03ea8152009-04-17 16:42:15 +0800525 return 0;
Neil Horman03bbcb22013-04-16 16:38:32 -0400526 if (irq_remap_broken) {
Neil Horman05104a42013-09-27 12:53:35 -0400527 printk(KERN_WARNING
528 "This system BIOS has enabled interrupt remapping\n"
529 "on a chipset that contains an erratum making that\n"
530 "feature unstable. To maintain system stability\n"
531 "interrupt remapping is being disabled. Please\n"
532 "contact your BIOS vendor for an update\n");
533 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Neil Horman03bbcb22013-04-16 16:38:32 -0400534 disable_irq_remap = 1;
535 return 0;
536 }
Weidong Han03ea8152009-04-17 16:42:15 +0800537
Youquan Song074835f2009-09-09 12:05:39 -0400538 if (!dmar_ir_support())
539 return 0;
540
Weidong Han93758232009-04-17 16:42:14 +0800541 for_each_drhd_unit(drhd) {
542 struct intel_iommu *iommu = drhd->iommu;
543
544 if (!ecap_ir_support(iommu->ecap))
545 return 0;
546 }
547
548 return 1;
549}
550
Suresh Siddha95a02e92012-03-30 11:47:07 -0700551static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700552{
553 struct dmar_drhd_unit *drhd;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800554 bool x2apic_present;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700555 int setup = 0;
Suresh Siddha41750d32011-08-23 17:05:18 -0700556 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700557
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800558 x2apic_present = x2apic_supported();
559
Youquan Songe936d072009-09-07 10:58:07 -0400560 if (parse_ioapics_under_ir() != 1) {
561 printk(KERN_INFO "Not enable interrupt remapping\n");
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800562 goto error;
Youquan Songe936d072009-09-07 10:58:07 -0400563 }
564
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800565 if (x2apic_present) {
Jiang Liub977e732014-01-06 14:18:14 +0800566 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
567
Suresh Siddha41750d32011-08-23 17:05:18 -0700568 eim = !dmar_x2apic_optout();
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800569 if (!eim)
570 printk(KERN_WARNING
571 "Your BIOS is broken and requested that x2apic be disabled.\n"
572 "This will slightly decrease performance.\n"
573 "Use 'intremap=no_x2apic_optout' to override BIOS request.\n");
Suresh Siddha41750d32011-08-23 17:05:18 -0700574 }
575
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700576 for_each_drhd_unit(drhd) {
577 struct intel_iommu *iommu = drhd->iommu;
578
579 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800580 * If the queued invalidation is already initialized,
581 * shouldn't disable it.
582 */
583 if (iommu->qi)
584 continue;
585
586 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700587 * Clear previous faults.
588 */
589 dmar_fault(-1, iommu);
590
591 /*
592 * Disable intr remapping and queued invalidation, if already
593 * enabled prior to OS handover.
594 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700595 iommu_disable_irq_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700596
597 dmar_disable_qi(iommu);
598 }
599
Suresh Siddha2ae21012008-07-10 11:16:43 -0700600 /*
601 * check for the Interrupt-remapping support
602 */
603 for_each_drhd_unit(drhd) {
604 struct intel_iommu *iommu = drhd->iommu;
605
606 if (!ecap_ir_support(iommu->ecap))
607 continue;
608
609 if (eim && !ecap_eim_support(iommu->ecap)) {
610 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
611 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800612 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700613 }
614 }
615
616 /*
617 * Enable queued invalidation for all the DRHD's.
618 */
619 for_each_drhd_unit(drhd) {
620 int ret;
621 struct intel_iommu *iommu = drhd->iommu;
622 ret = dmar_enable_qi(iommu);
623
624 if (ret) {
625 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
626 " invalidation, ecap %Lx, ret %d\n",
627 drhd->reg_base_addr, iommu->ecap, ret);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800628 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700629 }
630 }
631
632 /*
633 * Setup Interrupt-remapping for all the DRHD's now.
634 */
635 for_each_drhd_unit(drhd) {
636 struct intel_iommu *iommu = drhd->iommu;
637
638 if (!ecap_ir_support(iommu->ecap))
639 continue;
640
Suresh Siddha95a02e92012-03-30 11:47:07 -0700641 if (intel_setup_irq_remapping(iommu, eim))
Suresh Siddha2ae21012008-07-10 11:16:43 -0700642 goto error;
643
644 setup = 1;
645 }
646
647 if (!setup)
648 goto error;
649
Suresh Siddha95a02e92012-03-30 11:47:07 -0700650 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200651
652 /*
653 * VT-d has a different layout for IO-APIC entries when
654 * interrupt remapping is enabled. So it needs a special routine
655 * to print IO-APIC entries for debugging purposes too.
656 */
657 x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
658
Suresh Siddha41750d32011-08-23 17:05:18 -0700659 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700660
Suresh Siddha41750d32011-08-23 17:05:18 -0700661 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700662
663error:
664 /*
665 * handle error condition gracefully here!
666 */
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800667
668 if (x2apic_present)
Andy Lutomirskid01140d2013-05-13 15:22:42 -0700669 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800670
Suresh Siddha2ae21012008-07-10 11:16:43 -0700671 return -1;
672}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700673
Suresh Siddha20f30972009-08-04 12:07:08 -0700674static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
675 struct intel_iommu *iommu)
676{
677 struct acpi_dmar_pci_path *path;
678 u8 bus;
679 int count;
680
681 bus = scope->bus;
682 path = (struct acpi_dmar_pci_path *)(scope + 1);
683 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
684 / sizeof(struct acpi_dmar_pci_path);
685
686 while (--count > 0) {
687 /*
688 * Access PCI directly due to the PCI
689 * subsystem isn't initialized yet.
690 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800691 bus = read_pci_config_byte(bus, path->device, path->function,
Suresh Siddha20f30972009-08-04 12:07:08 -0700692 PCI_SECONDARY_BUS);
693 path++;
694 }
695 ir_hpet[ir_hpet_num].bus = bus;
Lv Zhengfa5f5082013-10-31 09:30:22 +0800696 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->device, path->function);
Suresh Siddha20f30972009-08-04 12:07:08 -0700697 ir_hpet[ir_hpet_num].iommu = iommu;
698 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
699 ir_hpet_num++;
700}
701
Weidong Hanf007e992009-05-23 00:41:15 +0800702static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
703 struct intel_iommu *iommu)
704{
705 struct acpi_dmar_pci_path *path;
706 u8 bus;
707 int count;
708
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
724 ir_ioapic[ir_ioapic_num].bus = bus;
Lv Zhengfa5f5082013-10-31 09:30:22 +0800725 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->device, path->function);
Weidong Hanf007e992009-05-23 00:41:15 +0800726 ir_ioapic[ir_ioapic_num].iommu = iommu;
727 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
728 ir_ioapic_num++;
729}
730
Suresh Siddha20f30972009-08-04 12:07:08 -0700731static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
732 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700733{
734 struct acpi_dmar_hardware_unit *drhd;
735 struct acpi_dmar_device_scope *scope;
736 void *start, *end;
737
738 drhd = (struct acpi_dmar_hardware_unit *)header;
739
740 start = (void *)(drhd + 1);
741 end = ((void *)drhd) + header->length;
742
743 while (start < end) {
744 scope = start;
745 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
746 if (ir_ioapic_num == MAX_IO_APICS) {
747 printk(KERN_WARNING "Exceeded Max IO APICS\n");
748 return -1;
749 }
750
Yinghai Lu680a7522010-04-08 19:58:23 +0100751 printk(KERN_INFO "IOAPIC id %d under DRHD base "
752 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
753 drhd->address, iommu->seq_id);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700754
Weidong Hanf007e992009-05-23 00:41:15 +0800755 ir_parse_one_ioapic_scope(scope, iommu);
Suresh Siddha20f30972009-08-04 12:07:08 -0700756 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
757 if (ir_hpet_num == MAX_HPET_TBS) {
758 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
759 return -1;
760 }
761
762 printk(KERN_INFO "HPET id %d under DRHD base"
763 " 0x%Lx\n", scope->enumeration_id,
764 drhd->address);
765
766 ir_parse_one_hpet_scope(scope, iommu);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700767 }
768 start += scope->length;
769 }
770
771 return 0;
772}
773
774/*
775 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
776 * hardware unit.
777 */
Jiang Liu694835d2014-01-06 14:18:16 +0800778static int __init parse_ioapics_under_ir(void)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700779{
780 struct dmar_drhd_unit *drhd;
781 int ir_supported = 0;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500782 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700783
784 for_each_drhd_unit(drhd) {
785 struct intel_iommu *iommu = drhd->iommu;
786
787 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700788 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700789 return -1;
790
791 ir_supported = 1;
792 }
793 }
794
Seth Forshee32ab31e2012-08-08 08:27:03 -0500795 if (!ir_supported)
796 return 0;
797
798 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
799 int ioapic_id = mpc_ioapic_id(ioapic_idx);
800 if (!map_ioapic_to_ir(ioapic_id)) {
801 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
802 "interrupt remapping will be disabled\n",
803 ioapic_id);
804 return -1;
805 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700806 }
807
Seth Forshee32ab31e2012-08-08 08:27:03 -0500808 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700809}
Fenghua Yub24696b2009-03-27 14:22:44 -0700810
Rashika Kheria6a7885c2013-12-18 12:04:27 +0530811static int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700812{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700813 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700814 return 0;
815
816 return dmar_dev_scope_init();
817}
818rootfs_initcall(ir_dev_scope_init);
819
Suresh Siddha95a02e92012-03-30 11:47:07 -0700820static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700821{
822 struct dmar_drhd_unit *drhd;
823 struct intel_iommu *iommu = NULL;
824
825 /*
826 * Disable Interrupt-remapping for all the DRHD's now.
827 */
828 for_each_iommu(iommu, drhd) {
829 if (!ecap_ir_support(iommu->ecap))
830 continue;
831
Suresh Siddha95a02e92012-03-30 11:47:07 -0700832 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700833 }
834}
835
Suresh Siddha95a02e92012-03-30 11:47:07 -0700836static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700837{
838 struct dmar_drhd_unit *drhd;
839 int setup = 0;
840 struct intel_iommu *iommu = NULL;
841
842 for_each_iommu(iommu, drhd)
843 if (iommu->qi)
844 dmar_reenable_qi(iommu);
845
846 /*
847 * Setup Interrupt-remapping for all the DRHD's now.
848 */
849 for_each_iommu(iommu, drhd) {
850 if (!ecap_ir_support(iommu->ecap))
851 continue;
852
853 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -0700854 iommu_set_irq_remapping(iommu, eim);
Fenghua Yub24696b2009-03-27 14:22:44 -0700855 setup = 1;
856 }
857
858 if (!setup)
859 goto error;
860
861 return 0;
862
863error:
864 /*
865 * handle error condition gracefully here!
866 */
867 return -1;
868}
869
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700870static void prepare_irte(struct irte *irte, int vector,
871 unsigned int dest)
872{
873 memset(irte, 0, sizeof(*irte));
874
875 irte->present = 1;
876 irte->dst_mode = apic->irq_dest_mode;
877 /*
878 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
879 * actual level or edge trigger will be setup in the IO-APIC
880 * RTE. This will help simplify level triggered irq migration.
881 * For more details, see the comments (in io_apic.c) explainig IO-APIC
882 * irq migration in the presence of interrupt-remapping.
883 */
884 irte->trigger_mode = 0;
885 irte->dlvry_mode = apic->irq_delivery_mode;
886 irte->vector = vector;
887 irte->dest_id = IRTE_DEST(dest);
888 irte->redir_hint = 1;
889}
890
891static int intel_setup_ioapic_entry(int irq,
892 struct IO_APIC_route_entry *route_entry,
893 unsigned int destination, int vector,
894 struct io_apic_irq_attr *attr)
895{
896 int ioapic_id = mpc_ioapic_id(attr->ioapic);
897 struct intel_iommu *iommu = map_ioapic_to_ir(ioapic_id);
898 struct IR_IO_APIC_route_entry *entry;
899 struct irte irte;
900 int index;
901
902 if (!iommu) {
903 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
904 return -ENODEV;
905 }
906
907 entry = (struct IR_IO_APIC_route_entry *)route_entry;
908
909 index = alloc_irte(iommu, irq, 1);
910 if (index < 0) {
911 pr_warn("Failed to allocate IRTE for ioapic %d\n", ioapic_id);
912 return -ENOMEM;
913 }
914
915 prepare_irte(&irte, vector, destination);
916
917 /* Set source-id of interrupt request */
918 set_ioapic_sid(&irte, ioapic_id);
919
920 modify_irte(irq, &irte);
921
922 apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
923 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
924 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
925 "Avail:%X Vector:%02X Dest:%08X "
926 "SID:%04X SQ:%X SVT:%X)\n",
927 attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
928 irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
929 irte.avail, irte.vector, irte.dest_id,
930 irte.sid, irte.sq, irte.svt);
931
932 memset(entry, 0, sizeof(*entry));
933
934 entry->index2 = (index >> 15) & 0x1;
935 entry->zero = 0;
936 entry->format = 1;
937 entry->index = (index & 0x7fff);
938 /*
939 * IO-APIC RTE will be configured with virtual vector.
940 * irq handler will do the explicit EOI to the io-apic.
941 */
942 entry->vector = attr->ioapic_pin;
943 entry->mask = 0; /* enable IRQ */
944 entry->trigger = attr->trigger;
945 entry->polarity = attr->polarity;
946
947 /* Mask level triggered irqs.
948 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
949 */
950 if (attr->trigger)
951 entry->mask = 1;
952
953 return 0;
954}
955
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700956/*
957 * Migrate the IO-APIC irq in the presence of intr-remapping.
958 *
959 * For both level and edge triggered, irq migration is a simple atomic
960 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
961 *
962 * For level triggered, we eliminate the io-apic RTE modification (with the
963 * updated vector information), by using a virtual vector (io-apic pin number).
964 * Real vector that is used for interrupting cpu will be coming from
965 * the interrupt-remapping table entry.
966 *
967 * As the migration is a simple atomic update of IRTE, the same mechanism
968 * is used to migrate MSI irq's in the presence of interrupt-remapping.
969 */
970static int
971intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
972 bool force)
973{
974 struct irq_cfg *cfg = data->chip_data;
975 unsigned int dest, irq = data->irq;
976 struct irte irte;
Alexander Gordeevff164322012-06-07 15:15:59 +0200977 int err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700978
Suresh Siddha7eb9ae02012-06-14 18:28:49 -0700979 if (!config_enabled(CONFIG_SMP))
980 return -EINVAL;
981
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700982 if (!cpumask_intersects(mask, cpu_online_mask))
983 return -EINVAL;
984
985 if (get_irte(irq, &irte))
986 return -EBUSY;
987
Alexander Gordeevff164322012-06-07 15:15:59 +0200988 err = assign_irq_vector(irq, cfg, mask);
989 if (err)
990 return err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700991
Alexander Gordeevff164322012-06-07 15:15:59 +0200992 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
993 if (err) {
Dan Carpentered88bed2012-06-12 19:26:33 +0300994 if (assign_irq_vector(irq, cfg, data->affinity))
Alexander Gordeevff164322012-06-07 15:15:59 +0200995 pr_err("Failed to recover vector for irq %d\n", irq);
996 return err;
997 }
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700998
999 irte.vector = cfg->vector;
1000 irte.dest_id = IRTE_DEST(dest);
1001
1002 /*
1003 * Atomically updates the IRTE with the new destination, vector
1004 * and flushes the interrupt entry cache.
1005 */
1006 modify_irte(irq, &irte);
1007
1008 /*
1009 * After this point, all the interrupts will start arriving
1010 * at the new destination. So, time to cleanup the previous
1011 * vector allocation.
1012 */
1013 if (cfg->move_in_progress)
1014 send_cleanup_vector(cfg);
1015
1016 cpumask_copy(data->affinity, mask);
1017 return 0;
1018}
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001019
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001020static void intel_compose_msi_msg(struct pci_dev *pdev,
1021 unsigned int irq, unsigned int dest,
1022 struct msi_msg *msg, u8 hpet_id)
1023{
1024 struct irq_cfg *cfg;
1025 struct irte irte;
Suresh Siddhac558df42012-05-08 00:08:54 -07001026 u16 sub_handle = 0;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001027 int ir_index;
1028
1029 cfg = irq_get_chip_data(irq);
1030
1031 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
1032 BUG_ON(ir_index == -1);
1033
1034 prepare_irte(&irte, cfg->vector, dest);
1035
1036 /* Set source-id of interrupt request */
1037 if (pdev)
1038 set_msi_sid(&irte, pdev);
1039 else
1040 set_hpet_sid(&irte, hpet_id);
1041
1042 modify_irte(irq, &irte);
1043
1044 msg->address_hi = MSI_ADDR_BASE_HI;
1045 msg->data = sub_handle;
1046 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1047 MSI_ADDR_IR_SHV |
1048 MSI_ADDR_IR_INDEX1(ir_index) |
1049 MSI_ADDR_IR_INDEX2(ir_index);
1050}
1051
1052/*
1053 * Map the PCI dev to the corresponding remapping hardware unit
1054 * and allocate 'nvec' consecutive interrupt-remapping table entries
1055 * in it.
1056 */
1057static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
1058{
1059 struct intel_iommu *iommu;
1060 int index;
1061
1062 iommu = map_dev_to_ir(dev);
1063 if (!iommu) {
1064 printk(KERN_ERR
1065 "Unable to map PCI %s to iommu\n", pci_name(dev));
1066 return -ENOENT;
1067 }
1068
1069 index = alloc_irte(iommu, irq, nvec);
1070 if (index < 0) {
1071 printk(KERN_ERR
1072 "Unable to allocate %d IRTE for PCI %s\n", nvec,
1073 pci_name(dev));
1074 return -ENOSPC;
1075 }
1076 return index;
1077}
1078
1079static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
1080 int index, int sub_handle)
1081{
1082 struct intel_iommu *iommu;
1083
1084 iommu = map_dev_to_ir(pdev);
1085 if (!iommu)
1086 return -ENOENT;
1087 /*
1088 * setup the mapping between the irq and the IRTE
1089 * base index, the sub_handle pointing to the
1090 * appropriate interrupt remap table entry.
1091 */
1092 set_irte_irq(irq, iommu, index, sub_handle);
1093
1094 return 0;
1095}
1096
1097static int intel_setup_hpet_msi(unsigned int irq, unsigned int id)
1098{
1099 struct intel_iommu *iommu = map_hpet_to_ir(id);
1100 int index;
1101
1102 if (!iommu)
1103 return -1;
1104
1105 index = alloc_irte(iommu, irq, 1);
1106 if (index < 0)
1107 return -1;
1108
1109 return 0;
1110}
1111
Joerg Roedel736baef2012-03-30 11:47:00 -07001112struct irq_remap_ops intel_irq_remap_ops = {
Suresh Siddha95a02e92012-03-30 11:47:07 -07001113 .supported = intel_irq_remapping_supported,
1114 .prepare = dmar_table_init,
1115 .enable = intel_enable_irq_remapping,
1116 .disable = disable_irq_remapping,
1117 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -07001118 .enable_faulting = enable_drhd_fault_handling,
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001119 .setup_ioapic_entry = intel_setup_ioapic_entry,
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001120 .set_affinity = intel_ioapic_set_affinity,
Joerg Roedel9d619f62012-03-30 11:47:04 -07001121 .free_irq = free_irte,
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001122 .compose_msi_msg = intel_compose_msi_msg,
1123 .msi_alloc_irq = intel_msi_alloc_irq,
1124 .msi_setup_irq = intel_msi_setup_irq,
1125 .setup_hpet_msi = intel_setup_hpet_msi,
Joerg Roedel736baef2012-03-30 11:47:00 -07001126};