blob: f495eba4b6ab4526cf671135abd8ebb2b85d5de5 [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>
Joerg Roedel736baef2012-03-30 11:47:00 -070014#include <asm/intr_remapping.h>
Weidong Hanf007e992009-05-23 00:41:15 +080015#include <asm/pci-direct.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070016
Joerg Roedel736baef2012-03-30 11:47:00 -070017#include "intr_remapping.h"
18
Joerg Roedeleef93fd2012-03-30 11:46:59 -070019struct ioapic_scope {
20 struct intel_iommu *iommu;
21 unsigned int id;
22 unsigned int bus; /* PCI bus number */
23 unsigned int devfn; /* PCI devfn number */
24};
25
26struct hpet_scope {
27 struct intel_iommu *iommu;
28 u8 id;
29 unsigned int bus;
30 unsigned int devfn;
31};
32
33#define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
Joerg Roedel0c3f1732012-03-30 11:47:02 -070034#define IRTE_DEST(dest) ((x2apic_mode) ? dest : dest << 8)
Joerg Roedeleef93fd2012-03-30 11:46:59 -070035
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070036static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070037static struct hpet_scope ir_hpet[MAX_HPET_TBS];
38static int ir_ioapic_num, ir_hpet_num;
Chris Wrightd1423d52010-07-20 11:06:49 -070039
Thomas Gleixner96f8e982011-07-19 16:28:19 +020040static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Thomas Gleixnerd585d062010-10-10 12:34:27 +020041
Yinghai Lue420dfb2008-08-19 20:50:21 -070042static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
43{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020044 struct irq_cfg *cfg = irq_get_chip_data(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020045 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080046}
47
Suresh Siddhab6fcb332008-07-10 11:16:44 -070048int get_irte(int irq, struct irte *entry)
49{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020050 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070051 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020052 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070053
Thomas Gleixnerd585d062010-10-10 12:34:27 +020054 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070055 return -1;
56
Thomas Gleixner96f8e982011-07-19 16:28:19 +020057 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070058
Yinghai Lue420dfb2008-08-19 20:50:21 -070059 index = irq_iommu->irte_index + irq_iommu->sub_handle;
60 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070061
Thomas Gleixner96f8e982011-07-19 16:28:19 +020062 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070063 return 0;
64}
65
66int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
67{
68 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020069 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070070 u16 index, start_index;
71 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070072 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070073 int i;
74
Thomas Gleixnerd585d062010-10-10 12:34:27 +020075 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070076 return -1;
77
78 /*
79 * start the IRTE search from index 0.
80 */
81 index = start_index = 0;
82
83 if (count > 1) {
84 count = __roundup_pow_of_two(count);
85 mask = ilog2(count);
86 }
87
88 if (mask > ecap_max_handle_mask(iommu->ecap)) {
89 printk(KERN_ERR
90 "Requested mask %x exceeds the max invalidation handle"
91 " mask value %Lx\n", mask,
92 ecap_max_handle_mask(iommu->ecap));
93 return -1;
94 }
95
Thomas Gleixner96f8e982011-07-19 16:28:19 +020096 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070097 do {
98 for (i = index; i < index + count; i++)
99 if (table->base[i].present)
100 break;
101 /* empty index found */
102 if (i == index + count)
103 break;
104
105 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
106
107 if (index == start_index) {
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200108 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700109 printk(KERN_ERR "can't allocate an IRTE\n");
110 return -1;
111 }
112 } while (1);
113
114 for (i = index; i < index + count; i++)
115 table->base[i].present = 1;
116
Yinghai Lue420dfb2008-08-19 20:50:21 -0700117 irq_iommu->iommu = iommu;
118 irq_iommu->irte_index = index;
119 irq_iommu->sub_handle = 0;
120 irq_iommu->irte_mask = mask;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700121
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200122 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700123
124 return index;
125}
126
Yu Zhao704126a2009-01-04 16:28:52 +0800127static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700128{
129 struct qi_desc desc;
130
131 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
132 | QI_IEC_SELECTIVE;
133 desc.high = 0;
134
Yu Zhao704126a2009-01-04 16:28:52 +0800135 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700136}
137
138int map_irq_to_irte_handle(int irq, u16 *sub_handle)
139{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200140 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700141 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200142 int index;
143
144 if (!irq_iommu)
145 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700146
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200147 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700148 *sub_handle = irq_iommu->sub_handle;
149 index = irq_iommu->irte_index;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200150 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700151 return index;
152}
153
154int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
155{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200156 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700157 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700158
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200159 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800160 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200161
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200162 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800163
Yinghai Lue420dfb2008-08-19 20:50:21 -0700164 irq_iommu->iommu = iommu;
165 irq_iommu->irte_index = index;
166 irq_iommu->sub_handle = subhandle;
167 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700168
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200169 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700170
171 return 0;
172}
173
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700174int modify_irte(int irq, struct irte *irte_modified)
175{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200176 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700177 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700178 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200179 struct irte *irte;
180 int rc, index;
181
182 if (!irq_iommu)
183 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700184
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200185 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700186
Yinghai Lue420dfb2008-08-19 20:50:21 -0700187 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700188
Yinghai Lue420dfb2008-08-19 20:50:21 -0700189 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700190 irte = &iommu->ir_table->base[index];
191
Linus Torvaldsc513b672010-08-06 11:02:31 -0700192 set_64bit(&irte->low, irte_modified->low);
193 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700194 __iommu_flush_cache(iommu, irte, sizeof(*irte));
195
Yu Zhao704126a2009-01-04 16:28:52 +0800196 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200197 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800198
199 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700200}
201
Suresh Siddha20f30972009-08-04 12:07:08 -0700202struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
203{
204 int i;
205
206 for (i = 0; i < MAX_HPET_TBS; i++)
207 if (ir_hpet[i].id == hpet_id)
208 return ir_hpet[i].iommu;
209 return NULL;
210}
211
Suresh Siddha89027d32008-07-10 11:16:56 -0700212struct intel_iommu *map_ioapic_to_ir(int apic)
213{
214 int i;
215
216 for (i = 0; i < MAX_IO_APICS; i++)
217 if (ir_ioapic[i].id == apic)
218 return ir_ioapic[i].iommu;
219 return NULL;
220}
221
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700222struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
223{
224 struct dmar_drhd_unit *drhd;
225
226 drhd = dmar_find_matched_drhd_unit(dev);
227 if (!drhd)
228 return NULL;
229
230 return drhd->iommu;
231}
232
Weidong Hanc4658b42009-05-23 00:41:14 +0800233static int clear_entries(struct irq_2_iommu *irq_iommu)
234{
235 struct irte *start, *entry, *end;
236 struct intel_iommu *iommu;
237 int index;
238
239 if (irq_iommu->sub_handle)
240 return 0;
241
242 iommu = irq_iommu->iommu;
243 index = irq_iommu->irte_index + irq_iommu->sub_handle;
244
245 start = iommu->ir_table->base + index;
246 end = start + (1 << irq_iommu->irte_mask);
247
248 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700249 set_64bit(&entry->low, 0);
250 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800251 }
252
253 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
254}
255
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700256int free_irte(int irq)
257{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200258 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700259 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200260 int rc;
261
262 if (!irq_iommu)
263 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700264
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200265 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700266
Weidong Hanc4658b42009-05-23 00:41:14 +0800267 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700268
Yinghai Lue420dfb2008-08-19 20:50:21 -0700269 irq_iommu->iommu = NULL;
270 irq_iommu->irte_index = 0;
271 irq_iommu->sub_handle = 0;
272 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700273
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200274 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700275
Yu Zhao704126a2009-01-04 16:28:52 +0800276 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700277}
278
Weidong Hanf007e992009-05-23 00:41:15 +0800279/*
280 * source validation type
281 */
282#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300283#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800284#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
285
286/*
287 * source-id qualifier
288 */
289#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
290#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
291 * the third least significant bit
292 */
293#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
294 * the second and third least significant bits
295 */
296#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
297 * the least three significant bits
298 */
299
300/*
301 * set SVT, SQ and SID fields of irte to verify
302 * source ids of interrupt requests
303 */
304static void set_irte_sid(struct irte *irte, unsigned int svt,
305 unsigned int sq, unsigned int sid)
306{
Chris Wrightd1423d52010-07-20 11:06:49 -0700307 if (disable_sourceid_checking)
308 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800309 irte->svt = svt;
310 irte->sq = sq;
311 irte->sid = sid;
312}
313
314int set_ioapic_sid(struct irte *irte, int apic)
315{
316 int i;
317 u16 sid = 0;
318
319 if (!irte)
320 return -1;
321
322 for (i = 0; i < MAX_IO_APICS; i++) {
323 if (ir_ioapic[i].id == apic) {
324 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
325 break;
326 }
327 }
328
329 if (sid == 0) {
330 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
331 return -1;
332 }
333
334 set_irte_sid(irte, 1, 0, sid);
335
336 return 0;
337}
338
Suresh Siddha20f30972009-08-04 12:07:08 -0700339int set_hpet_sid(struct irte *irte, u8 id)
340{
341 int i;
342 u16 sid = 0;
343
344 if (!irte)
345 return -1;
346
347 for (i = 0; i < MAX_HPET_TBS; i++) {
348 if (ir_hpet[i].id == id) {
349 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
350 break;
351 }
352 }
353
354 if (sid == 0) {
355 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
356 return -1;
357 }
358
359 /*
360 * Should really use SQ_ALL_16. Some platforms are broken.
361 * While we figure out the right quirks for these broken platforms, use
362 * SQ_13_IGNORE_3 for now.
363 */
364 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
365
366 return 0;
367}
368
Weidong Hanf007e992009-05-23 00:41:15 +0800369int set_msi_sid(struct irte *irte, struct pci_dev *dev)
370{
371 struct pci_dev *bridge;
372
373 if (!irte || !dev)
374 return -1;
375
376 /* PCIe device or Root Complex integrated PCI device */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900377 if (pci_is_pcie(dev) || !dev->bus->parent) {
Weidong Hanf007e992009-05-23 00:41:15 +0800378 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
379 (dev->bus->number << 8) | dev->devfn);
380 return 0;
381 }
382
383 bridge = pci_find_upstream_pcie_bridge(dev);
384 if (bridge) {
Stefan Assmann45e829e2009-12-03 06:49:24 -0500385 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
Weidong Hanf007e992009-05-23 00:41:15 +0800386 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
387 (bridge->bus->number << 8) | dev->bus->number);
388 else /* this is a legacy PCI bridge */
389 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
390 (bridge->bus->number << 8) | bridge->devfn);
391 }
392
393 return 0;
394}
395
Suresh Siddha2ae21012008-07-10 11:16:43 -0700396static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
397{
398 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100399 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700400 unsigned long flags;
401
402 addr = virt_to_phys((void *)iommu->ir_table->base);
403
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200404 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700405
406 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
407 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
408
409 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800410 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100411 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700412
413 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
414 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200415 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700416
417 /*
418 * global invalidation of interrupt entry cache before enabling
419 * interrupt-remapping.
420 */
421 qi_global_iec(iommu);
422
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200423 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700424
425 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700426 iommu->gcmd |= DMA_GCMD_IRE;
David Woodhousec416daa2009-05-10 20:30:58 +0100427 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700428
429 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
430 readl, (sts & DMA_GSTS_IRES), sts);
431
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200432 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700433}
434
435
Joerg Roedel736baef2012-03-30 11:47:00 -0700436static int intel_setup_intr_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700437{
438 struct ir_table *ir_table;
439 struct page *pages;
440
441 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700442 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700443
444 if (!iommu->ir_table)
445 return -ENOMEM;
446
Suresh Siddha824cd752009-10-02 11:01:23 -0700447 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
448 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700449
450 if (!pages) {
451 printk(KERN_ERR "failed to allocate pages of order %d\n",
452 INTR_REMAP_PAGE_ORDER);
453 kfree(iommu->ir_table);
454 return -ENOMEM;
455 }
456
457 ir_table->base = page_address(pages);
458
459 iommu_set_intr_remapping(iommu, mode);
460 return 0;
461}
462
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700463/*
464 * Disable Interrupt Remapping.
465 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700466static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700467{
468 unsigned long flags;
469 u32 sts;
470
471 if (!ecap_ir_support(iommu->ecap))
472 return;
473
Fenghua Yub24696b2009-03-27 14:22:44 -0700474 /*
475 * global invalidation of interrupt entry cache before disabling
476 * interrupt-remapping.
477 */
478 qi_global_iec(iommu);
479
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200480 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700481
482 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
483 if (!(sts & DMA_GSTS_IRES))
484 goto end;
485
486 iommu->gcmd &= ~DMA_GCMD_IRE;
487 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
488
489 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
490 readl, !(sts & DMA_GSTS_IRES), sts);
491
492end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200493 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700494}
495
Suresh Siddha41750d32011-08-23 17:05:18 -0700496static int __init dmar_x2apic_optout(void)
497{
498 struct acpi_table_dmar *dmar;
499 dmar = (struct acpi_table_dmar *)dmar_tbl;
500 if (!dmar || no_x2apic_optout)
501 return 0;
502 return dmar->flags & DMAR_X2APIC_OPT_OUT;
503}
504
Joerg Roedel736baef2012-03-30 11:47:00 -0700505static int __init intel_intr_remapping_supported(void)
Weidong Han93758232009-04-17 16:42:14 +0800506{
507 struct dmar_drhd_unit *drhd;
508
Weidong Han03ea8152009-04-17 16:42:15 +0800509 if (disable_intremap)
510 return 0;
511
Youquan Song074835f2009-09-09 12:05:39 -0400512 if (!dmar_ir_support())
513 return 0;
514
Weidong Han93758232009-04-17 16:42:14 +0800515 for_each_drhd_unit(drhd) {
516 struct intel_iommu *iommu = drhd->iommu;
517
518 if (!ecap_ir_support(iommu->ecap))
519 return 0;
520 }
521
522 return 1;
523}
524
Joerg Roedel736baef2012-03-30 11:47:00 -0700525static int __init intel_enable_intr_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700526{
527 struct dmar_drhd_unit *drhd;
528 int setup = 0;
Suresh Siddha41750d32011-08-23 17:05:18 -0700529 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700530
Youquan Songe936d072009-09-07 10:58:07 -0400531 if (parse_ioapics_under_ir() != 1) {
532 printk(KERN_INFO "Not enable interrupt remapping\n");
533 return -1;
534 }
535
Suresh Siddha41750d32011-08-23 17:05:18 -0700536 if (x2apic_supported()) {
537 eim = !dmar_x2apic_optout();
538 WARN(!eim, KERN_WARNING
539 "Your BIOS is broken and requested that x2apic be disabled\n"
540 "This will leave your machine vulnerable to irq-injection attacks\n"
541 "Use 'intremap=no_x2apic_optout' to override BIOS request\n");
542 }
543
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700544 for_each_drhd_unit(drhd) {
545 struct intel_iommu *iommu = drhd->iommu;
546
547 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800548 * If the queued invalidation is already initialized,
549 * shouldn't disable it.
550 */
551 if (iommu->qi)
552 continue;
553
554 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700555 * Clear previous faults.
556 */
557 dmar_fault(-1, iommu);
558
559 /*
560 * Disable intr remapping and queued invalidation, if already
561 * enabled prior to OS handover.
562 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700563 iommu_disable_intr_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700564
565 dmar_disable_qi(iommu);
566 }
567
Suresh Siddha2ae21012008-07-10 11:16:43 -0700568 /*
569 * check for the Interrupt-remapping support
570 */
571 for_each_drhd_unit(drhd) {
572 struct intel_iommu *iommu = drhd->iommu;
573
574 if (!ecap_ir_support(iommu->ecap))
575 continue;
576
577 if (eim && !ecap_eim_support(iommu->ecap)) {
578 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
579 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
580 return -1;
581 }
582 }
583
584 /*
585 * Enable queued invalidation for all the DRHD's.
586 */
587 for_each_drhd_unit(drhd) {
588 int ret;
589 struct intel_iommu *iommu = drhd->iommu;
590 ret = dmar_enable_qi(iommu);
591
592 if (ret) {
593 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
594 " invalidation, ecap %Lx, ret %d\n",
595 drhd->reg_base_addr, iommu->ecap, ret);
596 return -1;
597 }
598 }
599
600 /*
601 * Setup Interrupt-remapping for all the DRHD's now.
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
Joerg Roedel736baef2012-03-30 11:47:00 -0700609 if (intel_setup_intr_remapping(iommu, eim))
Suresh Siddha2ae21012008-07-10 11:16:43 -0700610 goto error;
611
612 setup = 1;
613 }
614
615 if (!setup)
616 goto error;
617
618 intr_remapping_enabled = 1;
Suresh Siddha41750d32011-08-23 17:05:18 -0700619 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700620
Suresh Siddha41750d32011-08-23 17:05:18 -0700621 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700622
623error:
624 /*
625 * handle error condition gracefully here!
626 */
627 return -1;
628}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700629
Suresh Siddha20f30972009-08-04 12:07:08 -0700630static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
631 struct intel_iommu *iommu)
632{
633 struct acpi_dmar_pci_path *path;
634 u8 bus;
635 int count;
636
637 bus = scope->bus;
638 path = (struct acpi_dmar_pci_path *)(scope + 1);
639 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
640 / sizeof(struct acpi_dmar_pci_path);
641
642 while (--count > 0) {
643 /*
644 * Access PCI directly due to the PCI
645 * subsystem isn't initialized yet.
646 */
647 bus = read_pci_config_byte(bus, path->dev, path->fn,
648 PCI_SECONDARY_BUS);
649 path++;
650 }
651 ir_hpet[ir_hpet_num].bus = bus;
652 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
653 ir_hpet[ir_hpet_num].iommu = iommu;
654 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
655 ir_hpet_num++;
656}
657
Weidong Hanf007e992009-05-23 00:41:15 +0800658static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
659 struct intel_iommu *iommu)
660{
661 struct acpi_dmar_pci_path *path;
662 u8 bus;
663 int count;
664
665 bus = scope->bus;
666 path = (struct acpi_dmar_pci_path *)(scope + 1);
667 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
668 / sizeof(struct acpi_dmar_pci_path);
669
670 while (--count > 0) {
671 /*
672 * Access PCI directly due to the PCI
673 * subsystem isn't initialized yet.
674 */
675 bus = read_pci_config_byte(bus, path->dev, path->fn,
676 PCI_SECONDARY_BUS);
677 path++;
678 }
679
680 ir_ioapic[ir_ioapic_num].bus = bus;
681 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
682 ir_ioapic[ir_ioapic_num].iommu = iommu;
683 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
684 ir_ioapic_num++;
685}
686
Suresh Siddha20f30972009-08-04 12:07:08 -0700687static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
688 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700689{
690 struct acpi_dmar_hardware_unit *drhd;
691 struct acpi_dmar_device_scope *scope;
692 void *start, *end;
693
694 drhd = (struct acpi_dmar_hardware_unit *)header;
695
696 start = (void *)(drhd + 1);
697 end = ((void *)drhd) + header->length;
698
699 while (start < end) {
700 scope = start;
701 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
702 if (ir_ioapic_num == MAX_IO_APICS) {
703 printk(KERN_WARNING "Exceeded Max IO APICS\n");
704 return -1;
705 }
706
Yinghai Lu680a7522010-04-08 19:58:23 +0100707 printk(KERN_INFO "IOAPIC id %d under DRHD base "
708 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
709 drhd->address, iommu->seq_id);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700710
Weidong Hanf007e992009-05-23 00:41:15 +0800711 ir_parse_one_ioapic_scope(scope, iommu);
Suresh Siddha20f30972009-08-04 12:07:08 -0700712 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
713 if (ir_hpet_num == MAX_HPET_TBS) {
714 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
715 return -1;
716 }
717
718 printk(KERN_INFO "HPET id %d under DRHD base"
719 " 0x%Lx\n", scope->enumeration_id,
720 drhd->address);
721
722 ir_parse_one_hpet_scope(scope, iommu);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700723 }
724 start += scope->length;
725 }
726
727 return 0;
728}
729
730/*
731 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
732 * hardware unit.
733 */
734int __init parse_ioapics_under_ir(void)
735{
736 struct dmar_drhd_unit *drhd;
737 int ir_supported = 0;
738
739 for_each_drhd_unit(drhd) {
740 struct intel_iommu *iommu = drhd->iommu;
741
742 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700743 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700744 return -1;
745
746 ir_supported = 1;
747 }
748 }
749
750 if (ir_supported && ir_ioapic_num != nr_ioapics) {
751 printk(KERN_WARNING
752 "Not all IO-APIC's listed under remapping hardware\n");
753 return -1;
754 }
755
756 return ir_supported;
757}
Fenghua Yub24696b2009-03-27 14:22:44 -0700758
Sergey Senozhatsky61ed26e2011-10-26 19:15:07 +0300759int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700760{
761 if (!intr_remapping_enabled)
762 return 0;
763
764 return dmar_dev_scope_init();
765}
766rootfs_initcall(ir_dev_scope_init);
767
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700768static void disable_intr_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700769{
770 struct dmar_drhd_unit *drhd;
771 struct intel_iommu *iommu = NULL;
772
773 /*
774 * Disable Interrupt-remapping for all the DRHD's now.
775 */
776 for_each_iommu(iommu, drhd) {
777 if (!ecap_ir_support(iommu->ecap))
778 continue;
779
780 iommu_disable_intr_remapping(iommu);
781 }
782}
783
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700784static int reenable_intr_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700785{
786 struct dmar_drhd_unit *drhd;
787 int setup = 0;
788 struct intel_iommu *iommu = NULL;
789
790 for_each_iommu(iommu, drhd)
791 if (iommu->qi)
792 dmar_reenable_qi(iommu);
793
794 /*
795 * Setup 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
801 /* Set up interrupt remapping for iommu.*/
802 iommu_set_intr_remapping(iommu, eim);
803 setup = 1;
804 }
805
806 if (!setup)
807 goto error;
808
809 return 0;
810
811error:
812 /*
813 * handle error condition gracefully here!
814 */
815 return -1;
816}
817
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700818static void prepare_irte(struct irte *irte, int vector,
819 unsigned int dest)
820{
821 memset(irte, 0, sizeof(*irte));
822
823 irte->present = 1;
824 irte->dst_mode = apic->irq_dest_mode;
825 /*
826 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
827 * actual level or edge trigger will be setup in the IO-APIC
828 * RTE. This will help simplify level triggered irq migration.
829 * For more details, see the comments (in io_apic.c) explainig IO-APIC
830 * irq migration in the presence of interrupt-remapping.
831 */
832 irte->trigger_mode = 0;
833 irte->dlvry_mode = apic->irq_delivery_mode;
834 irte->vector = vector;
835 irte->dest_id = IRTE_DEST(dest);
836 irte->redir_hint = 1;
837}
838
839static int intel_setup_ioapic_entry(int irq,
840 struct IO_APIC_route_entry *route_entry,
841 unsigned int destination, int vector,
842 struct io_apic_irq_attr *attr)
843{
844 int ioapic_id = mpc_ioapic_id(attr->ioapic);
845 struct intel_iommu *iommu = map_ioapic_to_ir(ioapic_id);
846 struct IR_IO_APIC_route_entry *entry;
847 struct irte irte;
848 int index;
849
850 if (!iommu) {
851 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
852 return -ENODEV;
853 }
854
855 entry = (struct IR_IO_APIC_route_entry *)route_entry;
856
857 index = alloc_irte(iommu, irq, 1);
858 if (index < 0) {
859 pr_warn("Failed to allocate IRTE for ioapic %d\n", ioapic_id);
860 return -ENOMEM;
861 }
862
863 prepare_irte(&irte, vector, destination);
864
865 /* Set source-id of interrupt request */
866 set_ioapic_sid(&irte, ioapic_id);
867
868 modify_irte(irq, &irte);
869
870 apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
871 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
872 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
873 "Avail:%X Vector:%02X Dest:%08X "
874 "SID:%04X SQ:%X SVT:%X)\n",
875 attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
876 irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
877 irte.avail, irte.vector, irte.dest_id,
878 irte.sid, irte.sq, irte.svt);
879
880 memset(entry, 0, sizeof(*entry));
881
882 entry->index2 = (index >> 15) & 0x1;
883 entry->zero = 0;
884 entry->format = 1;
885 entry->index = (index & 0x7fff);
886 /*
887 * IO-APIC RTE will be configured with virtual vector.
888 * irq handler will do the explicit EOI to the io-apic.
889 */
890 entry->vector = attr->ioapic_pin;
891 entry->mask = 0; /* enable IRQ */
892 entry->trigger = attr->trigger;
893 entry->polarity = attr->polarity;
894
895 /* Mask level triggered irqs.
896 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
897 */
898 if (attr->trigger)
899 entry->mask = 1;
900
901 return 0;
902}
903
904
Joerg Roedel736baef2012-03-30 11:47:00 -0700905struct irq_remap_ops intel_irq_remap_ops = {
906 .supported = intel_intr_remapping_supported,
907 .hardware_init = dmar_table_init,
908 .hardware_enable = intel_enable_intr_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700909 .hardware_disable = disable_intr_remapping,
910 .hardware_reenable = reenable_intr_remapping,
911 .enable_faulting = enable_drhd_fault_handling,
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700912 .setup_ioapic_entry = intel_setup_ioapic_entry,
Joerg Roedel736baef2012-03-30 11:47:00 -0700913};