blob: 7ca4947c3c10982ecd91b00b92ca43ad7becab7f [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
Yinghai Lue420dfb2008-08-19 20:50:21 -070043static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
44{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020045 struct irq_cfg *cfg = irq_get_chip_data(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020046 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080047}
48
Suresh Siddhab6fcb332008-07-10 11:16:44 -070049int get_irte(int irq, struct irte *entry)
50{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020051 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070052 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020053 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070054
Thomas Gleixnerd585d062010-10-10 12:34:27 +020055 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070056 return -1;
57
Thomas Gleixner96f8e982011-07-19 16:28:19 +020058 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070059
Yinghai Lue420dfb2008-08-19 20:50:21 -070060 index = irq_iommu->irte_index + irq_iommu->sub_handle;
61 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070062
Thomas Gleixner96f8e982011-07-19 16:28:19 +020063 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070064 return 0;
65}
66
Joerg Roedel263b5e82012-03-30 11:47:06 -070067static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070068{
69 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020070 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Joerg Roedel9b1b0e42012-09-26 12:44:45 +020071 struct irq_cfg *cfg = irq_get_chip_data(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070072 u16 index, start_index;
73 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070074 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070075 int i;
76
Thomas Gleixnerd585d062010-10-10 12:34:27 +020077 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070078 return -1;
79
80 /*
81 * start the IRTE search from index 0.
82 */
83 index = start_index = 0;
84
85 if (count > 1) {
86 count = __roundup_pow_of_two(count);
87 mask = ilog2(count);
88 }
89
90 if (mask > ecap_max_handle_mask(iommu->ecap)) {
91 printk(KERN_ERR
92 "Requested mask %x exceeds the max invalidation handle"
93 " mask value %Lx\n", mask,
94 ecap_max_handle_mask(iommu->ecap));
95 return -1;
96 }
97
Thomas Gleixner96f8e982011-07-19 16:28:19 +020098 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070099 do {
100 for (i = index; i < index + count; i++)
101 if (table->base[i].present)
102 break;
103 /* empty index found */
104 if (i == index + count)
105 break;
106
107 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
108
109 if (index == start_index) {
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200110 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700111 printk(KERN_ERR "can't allocate an IRTE\n");
112 return -1;
113 }
114 } while (1);
115
116 for (i = index; i < index + count; i++)
117 table->base[i].present = 1;
118
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200119 cfg->remapped = 1;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700120 irq_iommu->iommu = iommu;
121 irq_iommu->irte_index = index;
122 irq_iommu->sub_handle = 0;
123 irq_iommu->irte_mask = mask;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700124
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200125 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700126
127 return index;
128}
129
Yu Zhao704126a2009-01-04 16:28:52 +0800130static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700131{
132 struct qi_desc desc;
133
134 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
135 | QI_IEC_SELECTIVE;
136 desc.high = 0;
137
Yu Zhao704126a2009-01-04 16:28:52 +0800138 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700139}
140
Joerg Roedel263b5e82012-03-30 11:47:06 -0700141static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700142{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200143 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700144 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200145 int index;
146
147 if (!irq_iommu)
148 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700149
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200150 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700151 *sub_handle = irq_iommu->sub_handle;
152 index = irq_iommu->irte_index;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200153 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700154 return index;
155}
156
Joerg Roedel263b5e82012-03-30 11:47:06 -0700157static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700158{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200159 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200160 struct irq_cfg *cfg = irq_get_chip_data(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700161 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700162
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200163 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800164 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200165
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200166 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800167
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200168 cfg->remapped = 1;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700169 irq_iommu->iommu = iommu;
170 irq_iommu->irte_index = index;
171 irq_iommu->sub_handle = subhandle;
172 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700173
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200174 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700175
176 return 0;
177}
178
Joerg Roedel263b5e82012-03-30 11:47:06 -0700179static int modify_irte(int irq, struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700180{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200181 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700182 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700183 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200184 struct irte *irte;
185 int rc, index;
186
187 if (!irq_iommu)
188 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700189
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200190 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700191
Yinghai Lue420dfb2008-08-19 20:50:21 -0700192 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700193
Yinghai Lue420dfb2008-08-19 20:50:21 -0700194 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700195 irte = &iommu->ir_table->base[index];
196
Linus Torvaldsc513b672010-08-06 11:02:31 -0700197 set_64bit(&irte->low, irte_modified->low);
198 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700199 __iommu_flush_cache(iommu, irte, sizeof(*irte));
200
Yu Zhao704126a2009-01-04 16:28:52 +0800201 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200202 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800203
204 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700205}
206
Joerg Roedel263b5e82012-03-30 11:47:06 -0700207static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700208{
209 int i;
210
211 for (i = 0; i < MAX_HPET_TBS; i++)
212 if (ir_hpet[i].id == hpet_id)
213 return ir_hpet[i].iommu;
214 return NULL;
215}
216
Joerg Roedel263b5e82012-03-30 11:47:06 -0700217static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700218{
219 int i;
220
221 for (i = 0; i < MAX_IO_APICS; i++)
222 if (ir_ioapic[i].id == apic)
223 return ir_ioapic[i].iommu;
224 return NULL;
225}
226
Joerg Roedel263b5e82012-03-30 11:47:06 -0700227static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700228{
229 struct dmar_drhd_unit *drhd;
230
231 drhd = dmar_find_matched_drhd_unit(dev);
232 if (!drhd)
233 return NULL;
234
235 return drhd->iommu;
236}
237
Weidong Hanc4658b42009-05-23 00:41:14 +0800238static int clear_entries(struct irq_2_iommu *irq_iommu)
239{
240 struct irte *start, *entry, *end;
241 struct intel_iommu *iommu;
242 int index;
243
244 if (irq_iommu->sub_handle)
245 return 0;
246
247 iommu = irq_iommu->iommu;
248 index = irq_iommu->irte_index + irq_iommu->sub_handle;
249
250 start = iommu->ir_table->base + index;
251 end = start + (1 << irq_iommu->irte_mask);
252
253 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700254 set_64bit(&entry->low, 0);
255 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800256 }
257
258 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
259}
260
Joerg Roedel9d619f62012-03-30 11:47:04 -0700261static int free_irte(int irq)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700262{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200263 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700264 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200265 int rc;
266
267 if (!irq_iommu)
268 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700269
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200270 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700271
Weidong Hanc4658b42009-05-23 00:41:14 +0800272 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700273
Yinghai Lue420dfb2008-08-19 20:50:21 -0700274 irq_iommu->iommu = NULL;
275 irq_iommu->irte_index = 0;
276 irq_iommu->sub_handle = 0;
277 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700278
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200279 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700280
Yu Zhao704126a2009-01-04 16:28:52 +0800281 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700282}
283
Weidong Hanf007e992009-05-23 00:41:15 +0800284/*
285 * source validation type
286 */
287#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300288#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800289#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
290
291/*
292 * source-id qualifier
293 */
294#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
295#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
296 * the third least significant bit
297 */
298#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
299 * the second and third least significant bits
300 */
301#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
302 * the least three significant bits
303 */
304
305/*
306 * set SVT, SQ and SID fields of irte to verify
307 * source ids of interrupt requests
308 */
309static void set_irte_sid(struct irte *irte, unsigned int svt,
310 unsigned int sq, unsigned int sid)
311{
Chris Wrightd1423d52010-07-20 11:06:49 -0700312 if (disable_sourceid_checking)
313 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800314 irte->svt = svt;
315 irte->sq = sq;
316 irte->sid = sid;
317}
318
Joerg Roedel263b5e82012-03-30 11:47:06 -0700319static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800320{
321 int i;
322 u16 sid = 0;
323
324 if (!irte)
325 return -1;
326
327 for (i = 0; i < MAX_IO_APICS; i++) {
328 if (ir_ioapic[i].id == apic) {
329 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
330 break;
331 }
332 }
333
334 if (sid == 0) {
335 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
336 return -1;
337 }
338
339 set_irte_sid(irte, 1, 0, sid);
340
341 return 0;
342}
343
Joerg Roedel263b5e82012-03-30 11:47:06 -0700344static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700345{
346 int i;
347 u16 sid = 0;
348
349 if (!irte)
350 return -1;
351
352 for (i = 0; i < MAX_HPET_TBS; i++) {
353 if (ir_hpet[i].id == id) {
354 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
355 break;
356 }
357 }
358
359 if (sid == 0) {
360 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
361 return -1;
362 }
363
364 /*
365 * Should really use SQ_ALL_16. Some platforms are broken.
366 * While we figure out the right quirks for these broken platforms, use
367 * SQ_13_IGNORE_3 for now.
368 */
369 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
370
371 return 0;
372}
373
Joerg Roedel263b5e82012-03-30 11:47:06 -0700374static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800375{
376 struct pci_dev *bridge;
377
378 if (!irte || !dev)
379 return -1;
380
381 /* PCIe device or Root Complex integrated PCI device */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900382 if (pci_is_pcie(dev) || !dev->bus->parent) {
Weidong Hanf007e992009-05-23 00:41:15 +0800383 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
384 (dev->bus->number << 8) | dev->devfn);
385 return 0;
386 }
387
388 bridge = pci_find_upstream_pcie_bridge(dev);
389 if (bridge) {
Stefan Assmann45e829e2009-12-03 06:49:24 -0500390 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
Weidong Hanf007e992009-05-23 00:41:15 +0800391 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
392 (bridge->bus->number << 8) | dev->bus->number);
393 else /* this is a legacy PCI bridge */
394 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
395 (bridge->bus->number << 8) | bridge->devfn);
396 }
397
398 return 0;
399}
400
Suresh Siddha95a02e92012-03-30 11:47:07 -0700401static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700402{
403 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100404 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700405 unsigned long flags;
406
407 addr = virt_to_phys((void *)iommu->ir_table->base);
408
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200409 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700410
411 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
412 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
413
414 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800415 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100416 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700417
418 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
419 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200420 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700421
422 /*
423 * global invalidation of interrupt entry cache before enabling
424 * interrupt-remapping.
425 */
426 qi_global_iec(iommu);
427
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200428 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700429
430 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700431 iommu->gcmd |= DMA_GCMD_IRE;
David Woodhousec416daa2009-05-10 20:30:58 +0100432 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700433
434 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
435 readl, (sts & DMA_GSTS_IRES), sts);
436
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200437 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700438}
439
440
Suresh Siddha95a02e92012-03-30 11:47:07 -0700441static int intel_setup_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700442{
443 struct ir_table *ir_table;
444 struct page *pages;
445
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) {
456 printk(KERN_ERR "failed to allocate pages of order %d\n",
457 INTR_REMAP_PAGE_ORDER);
458 kfree(iommu->ir_table);
459 return -ENOMEM;
460 }
461
462 ir_table->base = page_address(pages);
463
Suresh Siddha95a02e92012-03-30 11:47:07 -0700464 iommu_set_irq_remapping(iommu, mode);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700465 return 0;
466}
467
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700468/*
469 * Disable Interrupt Remapping.
470 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700471static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700472{
473 unsigned long flags;
474 u32 sts;
475
476 if (!ecap_ir_support(iommu->ecap))
477 return;
478
Fenghua Yub24696b2009-03-27 14:22:44 -0700479 /*
480 * global invalidation of interrupt entry cache before disabling
481 * interrupt-remapping.
482 */
483 qi_global_iec(iommu);
484
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200485 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700486
487 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
488 if (!(sts & DMA_GSTS_IRES))
489 goto end;
490
491 iommu->gcmd &= ~DMA_GCMD_IRE;
492 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
493
494 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
495 readl, !(sts & DMA_GSTS_IRES), sts);
496
497end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200498 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700499}
500
Suresh Siddha41750d32011-08-23 17:05:18 -0700501static int __init dmar_x2apic_optout(void)
502{
503 struct acpi_table_dmar *dmar;
504 dmar = (struct acpi_table_dmar *)dmar_tbl;
505 if (!dmar || no_x2apic_optout)
506 return 0;
507 return dmar->flags & DMAR_X2APIC_OPT_OUT;
508}
509
Suresh Siddha95a02e92012-03-30 11:47:07 -0700510static int __init intel_irq_remapping_supported(void)
Weidong Han93758232009-04-17 16:42:14 +0800511{
512 struct dmar_drhd_unit *drhd;
513
Suresh Siddha95a02e92012-03-30 11:47:07 -0700514 if (disable_irq_remap)
Weidong Han03ea8152009-04-17 16:42:15 +0800515 return 0;
516
Youquan Song074835f2009-09-09 12:05:39 -0400517 if (!dmar_ir_support())
518 return 0;
519
Weidong Han93758232009-04-17 16:42:14 +0800520 for_each_drhd_unit(drhd) {
521 struct intel_iommu *iommu = drhd->iommu;
522
523 if (!ecap_ir_support(iommu->ecap))
524 return 0;
525 }
526
527 return 1;
528}
529
Suresh Siddha95a02e92012-03-30 11:47:07 -0700530static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700531{
532 struct dmar_drhd_unit *drhd;
533 int setup = 0;
Suresh Siddha41750d32011-08-23 17:05:18 -0700534 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700535
Youquan Songe936d072009-09-07 10:58:07 -0400536 if (parse_ioapics_under_ir() != 1) {
537 printk(KERN_INFO "Not enable interrupt remapping\n");
538 return -1;
539 }
540
Suresh Siddha41750d32011-08-23 17:05:18 -0700541 if (x2apic_supported()) {
542 eim = !dmar_x2apic_optout();
543 WARN(!eim, KERN_WARNING
544 "Your BIOS is broken and requested that x2apic be disabled\n"
545 "This will leave your machine vulnerable to irq-injection attacks\n"
546 "Use 'intremap=no_x2apic_optout' to override BIOS request\n");
547 }
548
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700549 for_each_drhd_unit(drhd) {
550 struct intel_iommu *iommu = drhd->iommu;
551
552 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800553 * If the queued invalidation is already initialized,
554 * shouldn't disable it.
555 */
556 if (iommu->qi)
557 continue;
558
559 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700560 * Clear previous faults.
561 */
562 dmar_fault(-1, iommu);
563
564 /*
565 * Disable intr remapping and queued invalidation, if already
566 * enabled prior to OS handover.
567 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700568 iommu_disable_irq_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700569
570 dmar_disable_qi(iommu);
571 }
572
Suresh Siddha2ae21012008-07-10 11:16:43 -0700573 /*
574 * check for the Interrupt-remapping support
575 */
576 for_each_drhd_unit(drhd) {
577 struct intel_iommu *iommu = drhd->iommu;
578
579 if (!ecap_ir_support(iommu->ecap))
580 continue;
581
582 if (eim && !ecap_eim_support(iommu->ecap)) {
583 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
584 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
585 return -1;
586 }
587 }
588
589 /*
590 * Enable queued invalidation for all the DRHD's.
591 */
592 for_each_drhd_unit(drhd) {
593 int ret;
594 struct intel_iommu *iommu = drhd->iommu;
595 ret = dmar_enable_qi(iommu);
596
597 if (ret) {
598 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
599 " invalidation, ecap %Lx, ret %d\n",
600 drhd->reg_base_addr, iommu->ecap, ret);
601 return -1;
602 }
603 }
604
605 /*
606 * Setup Interrupt-remapping for all the DRHD's now.
607 */
608 for_each_drhd_unit(drhd) {
609 struct intel_iommu *iommu = drhd->iommu;
610
611 if (!ecap_ir_support(iommu->ecap))
612 continue;
613
Suresh Siddha95a02e92012-03-30 11:47:07 -0700614 if (intel_setup_irq_remapping(iommu, eim))
Suresh Siddha2ae21012008-07-10 11:16:43 -0700615 goto error;
616
617 setup = 1;
618 }
619
620 if (!setup)
621 goto error;
622
Suresh Siddha95a02e92012-03-30 11:47:07 -0700623 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200624
625 /*
626 * VT-d has a different layout for IO-APIC entries when
627 * interrupt remapping is enabled. So it needs a special routine
628 * to print IO-APIC entries for debugging purposes too.
629 */
630 x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
631
Suresh Siddha41750d32011-08-23 17:05:18 -0700632 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700633
Suresh Siddha41750d32011-08-23 17:05:18 -0700634 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700635
636error:
637 /*
638 * handle error condition gracefully here!
639 */
640 return -1;
641}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700642
Suresh Siddha20f30972009-08-04 12:07:08 -0700643static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
644 struct intel_iommu *iommu)
645{
646 struct acpi_dmar_pci_path *path;
647 u8 bus;
648 int count;
649
650 bus = scope->bus;
651 path = (struct acpi_dmar_pci_path *)(scope + 1);
652 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
653 / sizeof(struct acpi_dmar_pci_path);
654
655 while (--count > 0) {
656 /*
657 * Access PCI directly due to the PCI
658 * subsystem isn't initialized yet.
659 */
660 bus = read_pci_config_byte(bus, path->dev, path->fn,
661 PCI_SECONDARY_BUS);
662 path++;
663 }
664 ir_hpet[ir_hpet_num].bus = bus;
665 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
666 ir_hpet[ir_hpet_num].iommu = iommu;
667 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
668 ir_hpet_num++;
669}
670
Weidong Hanf007e992009-05-23 00:41:15 +0800671static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
672 struct intel_iommu *iommu)
673{
674 struct acpi_dmar_pci_path *path;
675 u8 bus;
676 int count;
677
678 bus = scope->bus;
679 path = (struct acpi_dmar_pci_path *)(scope + 1);
680 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
681 / sizeof(struct acpi_dmar_pci_path);
682
683 while (--count > 0) {
684 /*
685 * Access PCI directly due to the PCI
686 * subsystem isn't initialized yet.
687 */
688 bus = read_pci_config_byte(bus, path->dev, path->fn,
689 PCI_SECONDARY_BUS);
690 path++;
691 }
692
693 ir_ioapic[ir_ioapic_num].bus = bus;
694 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
695 ir_ioapic[ir_ioapic_num].iommu = iommu;
696 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
697 ir_ioapic_num++;
698}
699
Suresh Siddha20f30972009-08-04 12:07:08 -0700700static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
701 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700702{
703 struct acpi_dmar_hardware_unit *drhd;
704 struct acpi_dmar_device_scope *scope;
705 void *start, *end;
706
707 drhd = (struct acpi_dmar_hardware_unit *)header;
708
709 start = (void *)(drhd + 1);
710 end = ((void *)drhd) + header->length;
711
712 while (start < end) {
713 scope = start;
714 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
715 if (ir_ioapic_num == MAX_IO_APICS) {
716 printk(KERN_WARNING "Exceeded Max IO APICS\n");
717 return -1;
718 }
719
Yinghai Lu680a7522010-04-08 19:58:23 +0100720 printk(KERN_INFO "IOAPIC id %d under DRHD base "
721 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
722 drhd->address, iommu->seq_id);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700723
Weidong Hanf007e992009-05-23 00:41:15 +0800724 ir_parse_one_ioapic_scope(scope, iommu);
Suresh Siddha20f30972009-08-04 12:07:08 -0700725 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
726 if (ir_hpet_num == MAX_HPET_TBS) {
727 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
728 return -1;
729 }
730
731 printk(KERN_INFO "HPET id %d under DRHD base"
732 " 0x%Lx\n", scope->enumeration_id,
733 drhd->address);
734
735 ir_parse_one_hpet_scope(scope, iommu);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700736 }
737 start += scope->length;
738 }
739
740 return 0;
741}
742
743/*
744 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
745 * hardware unit.
746 */
747int __init parse_ioapics_under_ir(void)
748{
749 struct dmar_drhd_unit *drhd;
750 int ir_supported = 0;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500751 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700752
753 for_each_drhd_unit(drhd) {
754 struct intel_iommu *iommu = drhd->iommu;
755
756 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700757 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700758 return -1;
759
760 ir_supported = 1;
761 }
762 }
763
Seth Forshee32ab31e2012-08-08 08:27:03 -0500764 if (!ir_supported)
765 return 0;
766
767 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
768 int ioapic_id = mpc_ioapic_id(ioapic_idx);
769 if (!map_ioapic_to_ir(ioapic_id)) {
770 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
771 "interrupt remapping will be disabled\n",
772 ioapic_id);
773 return -1;
774 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700775 }
776
Seth Forshee32ab31e2012-08-08 08:27:03 -0500777 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700778}
Fenghua Yub24696b2009-03-27 14:22:44 -0700779
Sergey Senozhatsky61ed26e2011-10-26 19:15:07 +0300780int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700781{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700782 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700783 return 0;
784
785 return dmar_dev_scope_init();
786}
787rootfs_initcall(ir_dev_scope_init);
788
Suresh Siddha95a02e92012-03-30 11:47:07 -0700789static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700790{
791 struct dmar_drhd_unit *drhd;
792 struct intel_iommu *iommu = NULL;
793
794 /*
795 * Disable Interrupt-remapping for all the DRHD's now.
796 */
797 for_each_iommu(iommu, drhd) {
798 if (!ecap_ir_support(iommu->ecap))
799 continue;
800
Suresh Siddha95a02e92012-03-30 11:47:07 -0700801 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700802 }
803}
804
Suresh Siddha95a02e92012-03-30 11:47:07 -0700805static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700806{
807 struct dmar_drhd_unit *drhd;
808 int setup = 0;
809 struct intel_iommu *iommu = NULL;
810
811 for_each_iommu(iommu, drhd)
812 if (iommu->qi)
813 dmar_reenable_qi(iommu);
814
815 /*
816 * Setup Interrupt-remapping for all the DRHD's now.
817 */
818 for_each_iommu(iommu, drhd) {
819 if (!ecap_ir_support(iommu->ecap))
820 continue;
821
822 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -0700823 iommu_set_irq_remapping(iommu, eim);
Fenghua Yub24696b2009-03-27 14:22:44 -0700824 setup = 1;
825 }
826
827 if (!setup)
828 goto error;
829
830 return 0;
831
832error:
833 /*
834 * handle error condition gracefully here!
835 */
836 return -1;
837}
838
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700839static void prepare_irte(struct irte *irte, int vector,
840 unsigned int dest)
841{
842 memset(irte, 0, sizeof(*irte));
843
844 irte->present = 1;
845 irte->dst_mode = apic->irq_dest_mode;
846 /*
847 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
848 * actual level or edge trigger will be setup in the IO-APIC
849 * RTE. This will help simplify level triggered irq migration.
850 * For more details, see the comments (in io_apic.c) explainig IO-APIC
851 * irq migration in the presence of interrupt-remapping.
852 */
853 irte->trigger_mode = 0;
854 irte->dlvry_mode = apic->irq_delivery_mode;
855 irte->vector = vector;
856 irte->dest_id = IRTE_DEST(dest);
857 irte->redir_hint = 1;
858}
859
860static int intel_setup_ioapic_entry(int irq,
861 struct IO_APIC_route_entry *route_entry,
862 unsigned int destination, int vector,
863 struct io_apic_irq_attr *attr)
864{
865 int ioapic_id = mpc_ioapic_id(attr->ioapic);
866 struct intel_iommu *iommu = map_ioapic_to_ir(ioapic_id);
867 struct IR_IO_APIC_route_entry *entry;
868 struct irte irte;
869 int index;
870
871 if (!iommu) {
872 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
873 return -ENODEV;
874 }
875
876 entry = (struct IR_IO_APIC_route_entry *)route_entry;
877
878 index = alloc_irte(iommu, irq, 1);
879 if (index < 0) {
880 pr_warn("Failed to allocate IRTE for ioapic %d\n", ioapic_id);
881 return -ENOMEM;
882 }
883
884 prepare_irte(&irte, vector, destination);
885
886 /* Set source-id of interrupt request */
887 set_ioapic_sid(&irte, ioapic_id);
888
889 modify_irte(irq, &irte);
890
891 apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
892 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
893 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
894 "Avail:%X Vector:%02X Dest:%08X "
895 "SID:%04X SQ:%X SVT:%X)\n",
896 attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
897 irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
898 irte.avail, irte.vector, irte.dest_id,
899 irte.sid, irte.sq, irte.svt);
900
901 memset(entry, 0, sizeof(*entry));
902
903 entry->index2 = (index >> 15) & 0x1;
904 entry->zero = 0;
905 entry->format = 1;
906 entry->index = (index & 0x7fff);
907 /*
908 * IO-APIC RTE will be configured with virtual vector.
909 * irq handler will do the explicit EOI to the io-apic.
910 */
911 entry->vector = attr->ioapic_pin;
912 entry->mask = 0; /* enable IRQ */
913 entry->trigger = attr->trigger;
914 entry->polarity = attr->polarity;
915
916 /* Mask level triggered irqs.
917 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
918 */
919 if (attr->trigger)
920 entry->mask = 1;
921
922 return 0;
923}
924
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700925/*
926 * Migrate the IO-APIC irq in the presence of intr-remapping.
927 *
928 * For both level and edge triggered, irq migration is a simple atomic
929 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
930 *
931 * For level triggered, we eliminate the io-apic RTE modification (with the
932 * updated vector information), by using a virtual vector (io-apic pin number).
933 * Real vector that is used for interrupting cpu will be coming from
934 * the interrupt-remapping table entry.
935 *
936 * As the migration is a simple atomic update of IRTE, the same mechanism
937 * is used to migrate MSI irq's in the presence of interrupt-remapping.
938 */
939static int
940intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
941 bool force)
942{
943 struct irq_cfg *cfg = data->chip_data;
944 unsigned int dest, irq = data->irq;
945 struct irte irte;
Alexander Gordeevff164322012-06-07 15:15:59 +0200946 int err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700947
Suresh Siddha7eb9ae02012-06-14 18:28:49 -0700948 if (!config_enabled(CONFIG_SMP))
949 return -EINVAL;
950
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700951 if (!cpumask_intersects(mask, cpu_online_mask))
952 return -EINVAL;
953
954 if (get_irte(irq, &irte))
955 return -EBUSY;
956
Alexander Gordeevff164322012-06-07 15:15:59 +0200957 err = assign_irq_vector(irq, cfg, mask);
958 if (err)
959 return err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700960
Alexander Gordeevff164322012-06-07 15:15:59 +0200961 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
962 if (err) {
Dan Carpentered88bed2012-06-12 19:26:33 +0300963 if (assign_irq_vector(irq, cfg, data->affinity))
Alexander Gordeevff164322012-06-07 15:15:59 +0200964 pr_err("Failed to recover vector for irq %d\n", irq);
965 return err;
966 }
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700967
968 irte.vector = cfg->vector;
969 irte.dest_id = IRTE_DEST(dest);
970
971 /*
972 * Atomically updates the IRTE with the new destination, vector
973 * and flushes the interrupt entry cache.
974 */
975 modify_irte(irq, &irte);
976
977 /*
978 * After this point, all the interrupts will start arriving
979 * at the new destination. So, time to cleanup the previous
980 * vector allocation.
981 */
982 if (cfg->move_in_progress)
983 send_cleanup_vector(cfg);
984
985 cpumask_copy(data->affinity, mask);
986 return 0;
987}
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700988
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700989static void intel_compose_msi_msg(struct pci_dev *pdev,
990 unsigned int irq, unsigned int dest,
991 struct msi_msg *msg, u8 hpet_id)
992{
993 struct irq_cfg *cfg;
994 struct irte irte;
Suresh Siddhac558df42012-05-08 00:08:54 -0700995 u16 sub_handle = 0;
Joerg Roedel5e2b9302012-03-30 11:47:05 -0700996 int ir_index;
997
998 cfg = irq_get_chip_data(irq);
999
1000 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
1001 BUG_ON(ir_index == -1);
1002
1003 prepare_irte(&irte, cfg->vector, dest);
1004
1005 /* Set source-id of interrupt request */
1006 if (pdev)
1007 set_msi_sid(&irte, pdev);
1008 else
1009 set_hpet_sid(&irte, hpet_id);
1010
1011 modify_irte(irq, &irte);
1012
1013 msg->address_hi = MSI_ADDR_BASE_HI;
1014 msg->data = sub_handle;
1015 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1016 MSI_ADDR_IR_SHV |
1017 MSI_ADDR_IR_INDEX1(ir_index) |
1018 MSI_ADDR_IR_INDEX2(ir_index);
1019}
1020
1021/*
1022 * Map the PCI dev to the corresponding remapping hardware unit
1023 * and allocate 'nvec' consecutive interrupt-remapping table entries
1024 * in it.
1025 */
1026static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
1027{
1028 struct intel_iommu *iommu;
1029 int index;
1030
1031 iommu = map_dev_to_ir(dev);
1032 if (!iommu) {
1033 printk(KERN_ERR
1034 "Unable to map PCI %s to iommu\n", pci_name(dev));
1035 return -ENOENT;
1036 }
1037
1038 index = alloc_irte(iommu, irq, nvec);
1039 if (index < 0) {
1040 printk(KERN_ERR
1041 "Unable to allocate %d IRTE for PCI %s\n", nvec,
1042 pci_name(dev));
1043 return -ENOSPC;
1044 }
1045 return index;
1046}
1047
1048static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
1049 int index, int sub_handle)
1050{
1051 struct intel_iommu *iommu;
1052
1053 iommu = map_dev_to_ir(pdev);
1054 if (!iommu)
1055 return -ENOENT;
1056 /*
1057 * setup the mapping between the irq and the IRTE
1058 * base index, the sub_handle pointing to the
1059 * appropriate interrupt remap table entry.
1060 */
1061 set_irte_irq(irq, iommu, index, sub_handle);
1062
1063 return 0;
1064}
1065
1066static int intel_setup_hpet_msi(unsigned int irq, unsigned int id)
1067{
1068 struct intel_iommu *iommu = map_hpet_to_ir(id);
1069 int index;
1070
1071 if (!iommu)
1072 return -1;
1073
1074 index = alloc_irte(iommu, irq, 1);
1075 if (index < 0)
1076 return -1;
1077
1078 return 0;
1079}
1080
Joerg Roedel736baef2012-03-30 11:47:00 -07001081struct irq_remap_ops intel_irq_remap_ops = {
Suresh Siddha95a02e92012-03-30 11:47:07 -07001082 .supported = intel_irq_remapping_supported,
1083 .prepare = dmar_table_init,
1084 .enable = intel_enable_irq_remapping,
1085 .disable = disable_irq_remapping,
1086 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -07001087 .enable_faulting = enable_drhd_fault_handling,
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001088 .setup_ioapic_entry = intel_setup_ioapic_entry,
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001089 .set_affinity = intel_ioapic_set_affinity,
Joerg Roedel9d619f62012-03-30 11:47:04 -07001090 .free_irq = free_irte,
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001091 .compose_msi_msg = intel_compose_msi_msg,
1092 .msi_alloc_irq = intel_msi_alloc_irq,
1093 .msi_setup_irq = intel_msi_setup_irq,
1094 .setup_hpet_msi = intel_setup_hpet_msi,
Joerg Roedel736baef2012-03-30 11:47:00 -07001095};