blob: 610b75b66c072cddbeb2ab60f012a9c944e39d94 [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)
34
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070035static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070036static struct hpet_scope ir_hpet[MAX_HPET_TBS];
37static int ir_ioapic_num, ir_hpet_num;
Chris Wrightd1423d52010-07-20 11:06:49 -070038
Thomas Gleixner96f8e982011-07-19 16:28:19 +020039static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Thomas Gleixnerd585d062010-10-10 12:34:27 +020040
Yinghai Lue420dfb2008-08-19 20:50:21 -070041static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
42{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020043 struct irq_cfg *cfg = irq_get_chip_data(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020044 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080045}
46
Suresh Siddhab6fcb332008-07-10 11:16:44 -070047int get_irte(int irq, struct irte *entry)
48{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020049 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070050 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020051 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070052
Thomas Gleixnerd585d062010-10-10 12:34:27 +020053 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070054 return -1;
55
Thomas Gleixner96f8e982011-07-19 16:28:19 +020056 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070057
Yinghai Lue420dfb2008-08-19 20:50:21 -070058 index = irq_iommu->irte_index + irq_iommu->sub_handle;
59 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070060
Thomas Gleixner96f8e982011-07-19 16:28:19 +020061 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070062 return 0;
63}
64
65int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
66{
67 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020068 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070069 u16 index, start_index;
70 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070071 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070072 int i;
73
Thomas Gleixnerd585d062010-10-10 12:34:27 +020074 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070075 return -1;
76
77 /*
78 * start the IRTE search from index 0.
79 */
80 index = start_index = 0;
81
82 if (count > 1) {
83 count = __roundup_pow_of_two(count);
84 mask = ilog2(count);
85 }
86
87 if (mask > ecap_max_handle_mask(iommu->ecap)) {
88 printk(KERN_ERR
89 "Requested mask %x exceeds the max invalidation handle"
90 " mask value %Lx\n", mask,
91 ecap_max_handle_mask(iommu->ecap));
92 return -1;
93 }
94
Thomas Gleixner96f8e982011-07-19 16:28:19 +020095 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070096 do {
97 for (i = index; i < index + count; i++)
98 if (table->base[i].present)
99 break;
100 /* empty index found */
101 if (i == index + count)
102 break;
103
104 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
105
106 if (index == start_index) {
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200107 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700108 printk(KERN_ERR "can't allocate an IRTE\n");
109 return -1;
110 }
111 } while (1);
112
113 for (i = index; i < index + count; i++)
114 table->base[i].present = 1;
115
Yinghai Lue420dfb2008-08-19 20:50:21 -0700116 irq_iommu->iommu = iommu;
117 irq_iommu->irte_index = index;
118 irq_iommu->sub_handle = 0;
119 irq_iommu->irte_mask = mask;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700120
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200121 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700122
123 return index;
124}
125
Yu Zhao704126a2009-01-04 16:28:52 +0800126static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700127{
128 struct qi_desc desc;
129
130 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
131 | QI_IEC_SELECTIVE;
132 desc.high = 0;
133
Yu Zhao704126a2009-01-04 16:28:52 +0800134 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700135}
136
137int map_irq_to_irte_handle(int irq, u16 *sub_handle)
138{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200139 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700140 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200141 int index;
142
143 if (!irq_iommu)
144 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700145
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200146 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700147 *sub_handle = irq_iommu->sub_handle;
148 index = irq_iommu->irte_index;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200149 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700150 return index;
151}
152
153int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
154{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200155 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700156 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700157
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200158 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800159 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200160
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200161 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800162
Yinghai Lue420dfb2008-08-19 20:50:21 -0700163 irq_iommu->iommu = iommu;
164 irq_iommu->irte_index = index;
165 irq_iommu->sub_handle = subhandle;
166 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700167
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200168 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700169
170 return 0;
171}
172
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700173int modify_irte(int irq, struct irte *irte_modified)
174{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200175 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700176 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700177 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200178 struct irte *irte;
179 int rc, index;
180
181 if (!irq_iommu)
182 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700183
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200184 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700185
Yinghai Lue420dfb2008-08-19 20:50:21 -0700186 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700187
Yinghai Lue420dfb2008-08-19 20:50:21 -0700188 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700189 irte = &iommu->ir_table->base[index];
190
Linus Torvaldsc513b672010-08-06 11:02:31 -0700191 set_64bit(&irte->low, irte_modified->low);
192 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700193 __iommu_flush_cache(iommu, irte, sizeof(*irte));
194
Yu Zhao704126a2009-01-04 16:28:52 +0800195 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200196 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800197
198 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700199}
200
Suresh Siddha20f30972009-08-04 12:07:08 -0700201struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
202{
203 int i;
204
205 for (i = 0; i < MAX_HPET_TBS; i++)
206 if (ir_hpet[i].id == hpet_id)
207 return ir_hpet[i].iommu;
208 return NULL;
209}
210
Suresh Siddha89027d32008-07-10 11:16:56 -0700211struct intel_iommu *map_ioapic_to_ir(int apic)
212{
213 int i;
214
215 for (i = 0; i < MAX_IO_APICS; i++)
216 if (ir_ioapic[i].id == apic)
217 return ir_ioapic[i].iommu;
218 return NULL;
219}
220
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700221struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
222{
223 struct dmar_drhd_unit *drhd;
224
225 drhd = dmar_find_matched_drhd_unit(dev);
226 if (!drhd)
227 return NULL;
228
229 return drhd->iommu;
230}
231
Weidong Hanc4658b42009-05-23 00:41:14 +0800232static int clear_entries(struct irq_2_iommu *irq_iommu)
233{
234 struct irte *start, *entry, *end;
235 struct intel_iommu *iommu;
236 int index;
237
238 if (irq_iommu->sub_handle)
239 return 0;
240
241 iommu = irq_iommu->iommu;
242 index = irq_iommu->irte_index + irq_iommu->sub_handle;
243
244 start = iommu->ir_table->base + index;
245 end = start + (1 << irq_iommu->irte_mask);
246
247 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700248 set_64bit(&entry->low, 0);
249 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800250 }
251
252 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
253}
254
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700255int free_irte(int irq)
256{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200257 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700258 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200259 int rc;
260
261 if (!irq_iommu)
262 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700263
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200264 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700265
Weidong Hanc4658b42009-05-23 00:41:14 +0800266 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700267
Yinghai Lue420dfb2008-08-19 20:50:21 -0700268 irq_iommu->iommu = NULL;
269 irq_iommu->irte_index = 0;
270 irq_iommu->sub_handle = 0;
271 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700272
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200273 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700274
Yu Zhao704126a2009-01-04 16:28:52 +0800275 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700276}
277
Weidong Hanf007e992009-05-23 00:41:15 +0800278/*
279 * source validation type
280 */
281#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300282#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800283#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
284
285/*
286 * source-id qualifier
287 */
288#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
289#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
290 * the third least significant bit
291 */
292#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
293 * the second and third least significant bits
294 */
295#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
296 * the least three significant bits
297 */
298
299/*
300 * set SVT, SQ and SID fields of irte to verify
301 * source ids of interrupt requests
302 */
303static void set_irte_sid(struct irte *irte, unsigned int svt,
304 unsigned int sq, unsigned int sid)
305{
Chris Wrightd1423d52010-07-20 11:06:49 -0700306 if (disable_sourceid_checking)
307 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800308 irte->svt = svt;
309 irte->sq = sq;
310 irte->sid = sid;
311}
312
313int set_ioapic_sid(struct irte *irte, int apic)
314{
315 int i;
316 u16 sid = 0;
317
318 if (!irte)
319 return -1;
320
321 for (i = 0; i < MAX_IO_APICS; i++) {
322 if (ir_ioapic[i].id == apic) {
323 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
324 break;
325 }
326 }
327
328 if (sid == 0) {
329 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
330 return -1;
331 }
332
333 set_irte_sid(irte, 1, 0, sid);
334
335 return 0;
336}
337
Suresh Siddha20f30972009-08-04 12:07:08 -0700338int set_hpet_sid(struct irte *irte, u8 id)
339{
340 int i;
341 u16 sid = 0;
342
343 if (!irte)
344 return -1;
345
346 for (i = 0; i < MAX_HPET_TBS; i++) {
347 if (ir_hpet[i].id == id) {
348 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
349 break;
350 }
351 }
352
353 if (sid == 0) {
354 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
355 return -1;
356 }
357
358 /*
359 * Should really use SQ_ALL_16. Some platforms are broken.
360 * While we figure out the right quirks for these broken platforms, use
361 * SQ_13_IGNORE_3 for now.
362 */
363 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
364
365 return 0;
366}
367
Weidong Hanf007e992009-05-23 00:41:15 +0800368int set_msi_sid(struct irte *irte, struct pci_dev *dev)
369{
370 struct pci_dev *bridge;
371
372 if (!irte || !dev)
373 return -1;
374
375 /* PCIe device or Root Complex integrated PCI device */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900376 if (pci_is_pcie(dev) || !dev->bus->parent) {
Weidong Hanf007e992009-05-23 00:41:15 +0800377 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
378 (dev->bus->number << 8) | dev->devfn);
379 return 0;
380 }
381
382 bridge = pci_find_upstream_pcie_bridge(dev);
383 if (bridge) {
Stefan Assmann45e829e2009-12-03 06:49:24 -0500384 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
Weidong Hanf007e992009-05-23 00:41:15 +0800385 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
386 (bridge->bus->number << 8) | dev->bus->number);
387 else /* this is a legacy PCI bridge */
388 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
389 (bridge->bus->number << 8) | bridge->devfn);
390 }
391
392 return 0;
393}
394
Suresh Siddha2ae21012008-07-10 11:16:43 -0700395static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
396{
397 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100398 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700399 unsigned long flags;
400
401 addr = virt_to_phys((void *)iommu->ir_table->base);
402
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200403 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700404
405 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
406 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
407
408 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800409 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100410 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700411
412 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
413 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200414 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700415
416 /*
417 * global invalidation of interrupt entry cache before enabling
418 * interrupt-remapping.
419 */
420 qi_global_iec(iommu);
421
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200422 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700423
424 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700425 iommu->gcmd |= DMA_GCMD_IRE;
David Woodhousec416daa2009-05-10 20:30:58 +0100426 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700427
428 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
429 readl, (sts & DMA_GSTS_IRES), sts);
430
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200431 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700432}
433
434
Joerg Roedel736baef2012-03-30 11:47:00 -0700435static int intel_setup_intr_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700436{
437 struct ir_table *ir_table;
438 struct page *pages;
439
440 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700441 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700442
443 if (!iommu->ir_table)
444 return -ENOMEM;
445
Suresh Siddha824cd752009-10-02 11:01:23 -0700446 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
447 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700448
449 if (!pages) {
450 printk(KERN_ERR "failed to allocate pages of order %d\n",
451 INTR_REMAP_PAGE_ORDER);
452 kfree(iommu->ir_table);
453 return -ENOMEM;
454 }
455
456 ir_table->base = page_address(pages);
457
458 iommu_set_intr_remapping(iommu, mode);
459 return 0;
460}
461
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700462/*
463 * Disable Interrupt Remapping.
464 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700465static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700466{
467 unsigned long flags;
468 u32 sts;
469
470 if (!ecap_ir_support(iommu->ecap))
471 return;
472
Fenghua Yub24696b2009-03-27 14:22:44 -0700473 /*
474 * global invalidation of interrupt entry cache before disabling
475 * interrupt-remapping.
476 */
477 qi_global_iec(iommu);
478
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200479 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700480
481 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
482 if (!(sts & DMA_GSTS_IRES))
483 goto end;
484
485 iommu->gcmd &= ~DMA_GCMD_IRE;
486 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
487
488 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
489 readl, !(sts & DMA_GSTS_IRES), sts);
490
491end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200492 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700493}
494
Suresh Siddha41750d32011-08-23 17:05:18 -0700495static int __init dmar_x2apic_optout(void)
496{
497 struct acpi_table_dmar *dmar;
498 dmar = (struct acpi_table_dmar *)dmar_tbl;
499 if (!dmar || no_x2apic_optout)
500 return 0;
501 return dmar->flags & DMAR_X2APIC_OPT_OUT;
502}
503
Joerg Roedel736baef2012-03-30 11:47:00 -0700504static int __init intel_intr_remapping_supported(void)
Weidong Han93758232009-04-17 16:42:14 +0800505{
506 struct dmar_drhd_unit *drhd;
507
Weidong Han03ea8152009-04-17 16:42:15 +0800508 if (disable_intremap)
509 return 0;
510
Youquan Song074835f2009-09-09 12:05:39 -0400511 if (!dmar_ir_support())
512 return 0;
513
Weidong Han93758232009-04-17 16:42:14 +0800514 for_each_drhd_unit(drhd) {
515 struct intel_iommu *iommu = drhd->iommu;
516
517 if (!ecap_ir_support(iommu->ecap))
518 return 0;
519 }
520
521 return 1;
522}
523
Joerg Roedel736baef2012-03-30 11:47:00 -0700524static int __init intel_enable_intr_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700525{
526 struct dmar_drhd_unit *drhd;
527 int setup = 0;
Suresh Siddha41750d32011-08-23 17:05:18 -0700528 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700529
Youquan Songe936d072009-09-07 10:58:07 -0400530 if (parse_ioapics_under_ir() != 1) {
531 printk(KERN_INFO "Not enable interrupt remapping\n");
532 return -1;
533 }
534
Suresh Siddha41750d32011-08-23 17:05:18 -0700535 if (x2apic_supported()) {
536 eim = !dmar_x2apic_optout();
537 WARN(!eim, KERN_WARNING
538 "Your BIOS is broken and requested that x2apic be disabled\n"
539 "This will leave your machine vulnerable to irq-injection attacks\n"
540 "Use 'intremap=no_x2apic_optout' to override BIOS request\n");
541 }
542
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700543 for_each_drhd_unit(drhd) {
544 struct intel_iommu *iommu = drhd->iommu;
545
546 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800547 * If the queued invalidation is already initialized,
548 * shouldn't disable it.
549 */
550 if (iommu->qi)
551 continue;
552
553 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700554 * Clear previous faults.
555 */
556 dmar_fault(-1, iommu);
557
558 /*
559 * Disable intr remapping and queued invalidation, if already
560 * enabled prior to OS handover.
561 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700562 iommu_disable_intr_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700563
564 dmar_disable_qi(iommu);
565 }
566
Suresh Siddha2ae21012008-07-10 11:16:43 -0700567 /*
568 * check for the Interrupt-remapping support
569 */
570 for_each_drhd_unit(drhd) {
571 struct intel_iommu *iommu = drhd->iommu;
572
573 if (!ecap_ir_support(iommu->ecap))
574 continue;
575
576 if (eim && !ecap_eim_support(iommu->ecap)) {
577 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
578 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
579 return -1;
580 }
581 }
582
583 /*
584 * Enable queued invalidation for all the DRHD's.
585 */
586 for_each_drhd_unit(drhd) {
587 int ret;
588 struct intel_iommu *iommu = drhd->iommu;
589 ret = dmar_enable_qi(iommu);
590
591 if (ret) {
592 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
593 " invalidation, ecap %Lx, ret %d\n",
594 drhd->reg_base_addr, iommu->ecap, ret);
595 return -1;
596 }
597 }
598
599 /*
600 * Setup Interrupt-remapping for all the DRHD's now.
601 */
602 for_each_drhd_unit(drhd) {
603 struct intel_iommu *iommu = drhd->iommu;
604
605 if (!ecap_ir_support(iommu->ecap))
606 continue;
607
Joerg Roedel736baef2012-03-30 11:47:00 -0700608 if (intel_setup_intr_remapping(iommu, eim))
Suresh Siddha2ae21012008-07-10 11:16:43 -0700609 goto error;
610
611 setup = 1;
612 }
613
614 if (!setup)
615 goto error;
616
617 intr_remapping_enabled = 1;
Suresh Siddha41750d32011-08-23 17:05:18 -0700618 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700619
Suresh Siddha41750d32011-08-23 17:05:18 -0700620 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700621
622error:
623 /*
624 * handle error condition gracefully here!
625 */
626 return -1;
627}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700628
Suresh Siddha20f30972009-08-04 12:07:08 -0700629static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
630 struct intel_iommu *iommu)
631{
632 struct acpi_dmar_pci_path *path;
633 u8 bus;
634 int count;
635
636 bus = scope->bus;
637 path = (struct acpi_dmar_pci_path *)(scope + 1);
638 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
639 / sizeof(struct acpi_dmar_pci_path);
640
641 while (--count > 0) {
642 /*
643 * Access PCI directly due to the PCI
644 * subsystem isn't initialized yet.
645 */
646 bus = read_pci_config_byte(bus, path->dev, path->fn,
647 PCI_SECONDARY_BUS);
648 path++;
649 }
650 ir_hpet[ir_hpet_num].bus = bus;
651 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
652 ir_hpet[ir_hpet_num].iommu = iommu;
653 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
654 ir_hpet_num++;
655}
656
Weidong Hanf007e992009-05-23 00:41:15 +0800657static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
658 struct intel_iommu *iommu)
659{
660 struct acpi_dmar_pci_path *path;
661 u8 bus;
662 int count;
663
664 bus = scope->bus;
665 path = (struct acpi_dmar_pci_path *)(scope + 1);
666 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
667 / sizeof(struct acpi_dmar_pci_path);
668
669 while (--count > 0) {
670 /*
671 * Access PCI directly due to the PCI
672 * subsystem isn't initialized yet.
673 */
674 bus = read_pci_config_byte(bus, path->dev, path->fn,
675 PCI_SECONDARY_BUS);
676 path++;
677 }
678
679 ir_ioapic[ir_ioapic_num].bus = bus;
680 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
681 ir_ioapic[ir_ioapic_num].iommu = iommu;
682 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
683 ir_ioapic_num++;
684}
685
Suresh Siddha20f30972009-08-04 12:07:08 -0700686static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
687 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700688{
689 struct acpi_dmar_hardware_unit *drhd;
690 struct acpi_dmar_device_scope *scope;
691 void *start, *end;
692
693 drhd = (struct acpi_dmar_hardware_unit *)header;
694
695 start = (void *)(drhd + 1);
696 end = ((void *)drhd) + header->length;
697
698 while (start < end) {
699 scope = start;
700 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
701 if (ir_ioapic_num == MAX_IO_APICS) {
702 printk(KERN_WARNING "Exceeded Max IO APICS\n");
703 return -1;
704 }
705
Yinghai Lu680a7522010-04-08 19:58:23 +0100706 printk(KERN_INFO "IOAPIC id %d under DRHD base "
707 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
708 drhd->address, iommu->seq_id);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700709
Weidong Hanf007e992009-05-23 00:41:15 +0800710 ir_parse_one_ioapic_scope(scope, iommu);
Suresh Siddha20f30972009-08-04 12:07:08 -0700711 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
712 if (ir_hpet_num == MAX_HPET_TBS) {
713 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
714 return -1;
715 }
716
717 printk(KERN_INFO "HPET id %d under DRHD base"
718 " 0x%Lx\n", scope->enumeration_id,
719 drhd->address);
720
721 ir_parse_one_hpet_scope(scope, iommu);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700722 }
723 start += scope->length;
724 }
725
726 return 0;
727}
728
729/*
730 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
731 * hardware unit.
732 */
733int __init parse_ioapics_under_ir(void)
734{
735 struct dmar_drhd_unit *drhd;
736 int ir_supported = 0;
737
738 for_each_drhd_unit(drhd) {
739 struct intel_iommu *iommu = drhd->iommu;
740
741 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700742 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700743 return -1;
744
745 ir_supported = 1;
746 }
747 }
748
749 if (ir_supported && ir_ioapic_num != nr_ioapics) {
750 printk(KERN_WARNING
751 "Not all IO-APIC's listed under remapping hardware\n");
752 return -1;
753 }
754
755 return ir_supported;
756}
Fenghua Yub24696b2009-03-27 14:22:44 -0700757
Sergey Senozhatsky61ed26e2011-10-26 19:15:07 +0300758int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700759{
760 if (!intr_remapping_enabled)
761 return 0;
762
763 return dmar_dev_scope_init();
764}
765rootfs_initcall(ir_dev_scope_init);
766
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700767static void disable_intr_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700768{
769 struct dmar_drhd_unit *drhd;
770 struct intel_iommu *iommu = NULL;
771
772 /*
773 * Disable Interrupt-remapping for all the DRHD's now.
774 */
775 for_each_iommu(iommu, drhd) {
776 if (!ecap_ir_support(iommu->ecap))
777 continue;
778
779 iommu_disable_intr_remapping(iommu);
780 }
781}
782
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700783static int reenable_intr_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700784{
785 struct dmar_drhd_unit *drhd;
786 int setup = 0;
787 struct intel_iommu *iommu = NULL;
788
789 for_each_iommu(iommu, drhd)
790 if (iommu->qi)
791 dmar_reenable_qi(iommu);
792
793 /*
794 * Setup Interrupt-remapping for all the DRHD's now.
795 */
796 for_each_iommu(iommu, drhd) {
797 if (!ecap_ir_support(iommu->ecap))
798 continue;
799
800 /* Set up interrupt remapping for iommu.*/
801 iommu_set_intr_remapping(iommu, eim);
802 setup = 1;
803 }
804
805 if (!setup)
806 goto error;
807
808 return 0;
809
810error:
811 /*
812 * handle error condition gracefully here!
813 */
814 return -1;
815}
816
Joerg Roedel736baef2012-03-30 11:47:00 -0700817struct irq_remap_ops intel_irq_remap_ops = {
818 .supported = intel_intr_remapping_supported,
819 .hardware_init = dmar_table_init,
820 .hardware_enable = intel_enable_intr_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -0700821 .hardware_disable = disable_intr_remapping,
822 .hardware_reenable = reenable_intr_remapping,
823 .enable_faulting = enable_drhd_fault_handling,
Joerg Roedel736baef2012-03-30 11:47:00 -0700824};