1 #include <linux/interrupt.h>
2 #include <linux/dmar.h>
3 #include <linux/spinlock.h>
4 #include <linux/jiffies.h>
7 #include <asm/io_apic.h>
8 #include <linux/intel-iommu.h>
9 #include "intr_remapping.h"
11 static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
12 static int ir_ioapic_num;
13 int intr_remapping_enabled;
16 struct intel_iommu *iommu;
22 static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
24 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
26 return (irq < nr_irqs) ? irq_2_iommuX + irq : NULL;
29 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
31 return irq_2_iommu(irq);
34 static DEFINE_SPINLOCK(irq_2_ir_lock);
36 static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
38 struct irq_2_iommu *irq_iommu;
40 irq_iommu = irq_2_iommu(irq);
45 if (!irq_iommu->iommu)
51 int irq_remapped(int irq)
53 return valid_irq_2_iommu(irq) != NULL;
56 int get_irte(int irq, struct irte *entry)
59 struct irq_2_iommu *irq_iommu;
64 spin_lock(&irq_2_ir_lock);
65 irq_iommu = valid_irq_2_iommu(irq);
67 spin_unlock(&irq_2_ir_lock);
71 index = irq_iommu->irte_index + irq_iommu->sub_handle;
72 *entry = *(irq_iommu->iommu->ir_table->base + index);
74 spin_unlock(&irq_2_ir_lock);
78 int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
80 struct ir_table *table = iommu->ir_table;
81 struct irq_2_iommu *irq_iommu;
82 u16 index, start_index;
83 unsigned int mask = 0;
89 /* protect irq_2_iommu_alloc later */
94 * start the IRTE search from index 0.
96 index = start_index = 0;
99 count = __roundup_pow_of_two(count);
103 if (mask > ecap_max_handle_mask(iommu->ecap)) {
105 "Requested mask %x exceeds the max invalidation handle"
106 " mask value %Lx\n", mask,
107 ecap_max_handle_mask(iommu->ecap));
111 spin_lock(&irq_2_ir_lock);
113 for (i = index; i < index + count; i++)
114 if (table->base[i].present)
116 /* empty index found */
117 if (i == index + count)
120 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
122 if (index == start_index) {
123 spin_unlock(&irq_2_ir_lock);
124 printk(KERN_ERR "can't allocate an IRTE\n");
129 for (i = index; i < index + count; i++)
130 table->base[i].present = 1;
132 irq_iommu = irq_2_iommu_alloc(irq);
133 irq_iommu->iommu = iommu;
134 irq_iommu->irte_index = index;
135 irq_iommu->sub_handle = 0;
136 irq_iommu->irte_mask = mask;
138 spin_unlock(&irq_2_ir_lock);
143 static void qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
147 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
151 qi_submit_sync(&desc, iommu);
154 int map_irq_to_irte_handle(int irq, u16 *sub_handle)
157 struct irq_2_iommu *irq_iommu;
159 spin_lock(&irq_2_ir_lock);
160 irq_iommu = valid_irq_2_iommu(irq);
162 spin_unlock(&irq_2_ir_lock);
166 *sub_handle = irq_iommu->sub_handle;
167 index = irq_iommu->irte_index;
168 spin_unlock(&irq_2_ir_lock);
172 int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
174 struct irq_2_iommu *irq_iommu;
176 spin_lock(&irq_2_ir_lock);
178 irq_iommu = irq_2_iommu_alloc(irq);
180 irq_iommu->iommu = iommu;
181 irq_iommu->irte_index = index;
182 irq_iommu->sub_handle = subhandle;
183 irq_iommu->irte_mask = 0;
185 spin_unlock(&irq_2_ir_lock);
190 int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
192 struct irq_2_iommu *irq_iommu;
194 spin_lock(&irq_2_ir_lock);
195 irq_iommu = valid_irq_2_iommu(irq);
197 spin_unlock(&irq_2_ir_lock);
201 irq_iommu->iommu = NULL;
202 irq_iommu->irte_index = 0;
203 irq_iommu->sub_handle = 0;
204 irq_2_iommu(irq)->irte_mask = 0;
206 spin_unlock(&irq_2_ir_lock);
211 int modify_irte(int irq, struct irte *irte_modified)
215 struct intel_iommu *iommu;
216 struct irq_2_iommu *irq_iommu;
218 spin_lock(&irq_2_ir_lock);
219 irq_iommu = valid_irq_2_iommu(irq);
221 spin_unlock(&irq_2_ir_lock);
225 iommu = irq_iommu->iommu;
227 index = irq_iommu->irte_index + irq_iommu->sub_handle;
228 irte = &iommu->ir_table->base[index];
230 set_64bit((unsigned long *)irte, irte_modified->low | (1 << 1));
231 __iommu_flush_cache(iommu, irte, sizeof(*irte));
233 qi_flush_iec(iommu, index, 0);
235 spin_unlock(&irq_2_ir_lock);
239 int flush_irte(int irq)
242 struct intel_iommu *iommu;
243 struct irq_2_iommu *irq_iommu;
245 spin_lock(&irq_2_ir_lock);
246 irq_iommu = valid_irq_2_iommu(irq);
248 spin_unlock(&irq_2_ir_lock);
252 iommu = irq_iommu->iommu;
254 index = irq_iommu->irte_index + irq_iommu->sub_handle;
256 qi_flush_iec(iommu, index, irq_iommu->irte_mask);
257 spin_unlock(&irq_2_ir_lock);
262 struct intel_iommu *map_ioapic_to_ir(int apic)
266 for (i = 0; i < MAX_IO_APICS; i++)
267 if (ir_ioapic[i].id == apic)
268 return ir_ioapic[i].iommu;
272 struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
274 struct dmar_drhd_unit *drhd;
276 drhd = dmar_find_matched_drhd_unit(dev);
283 int free_irte(int irq)
287 struct intel_iommu *iommu;
288 struct irq_2_iommu *irq_iommu;
290 spin_lock(&irq_2_ir_lock);
291 irq_iommu = valid_irq_2_iommu(irq);
293 spin_unlock(&irq_2_ir_lock);
297 iommu = irq_iommu->iommu;
299 index = irq_iommu->irte_index + irq_iommu->sub_handle;
300 irte = &iommu->ir_table->base[index];
302 if (!irq_iommu->sub_handle) {
303 for (i = 0; i < (1 << irq_iommu->irte_mask); i++)
304 set_64bit((unsigned long *)irte, 0);
305 qi_flush_iec(iommu, index, irq_iommu->irte_mask);
308 irq_iommu->iommu = NULL;
309 irq_iommu->irte_index = 0;
310 irq_iommu->sub_handle = 0;
311 irq_iommu->irte_mask = 0;
313 spin_unlock(&irq_2_ir_lock);
318 static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
324 addr = virt_to_phys((void *)iommu->ir_table->base);
326 spin_lock_irqsave(&iommu->register_lock, flags);
328 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
329 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
331 /* Set interrupt-remapping table pointer */
332 cmd = iommu->gcmd | DMA_GCMD_SIRTP;
333 writel(cmd, iommu->reg + DMAR_GCMD_REG);
335 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
336 readl, (sts & DMA_GSTS_IRTPS), sts);
337 spin_unlock_irqrestore(&iommu->register_lock, flags);
340 * global invalidation of interrupt entry cache before enabling
341 * interrupt-remapping.
343 qi_global_iec(iommu);
345 spin_lock_irqsave(&iommu->register_lock, flags);
347 /* Enable interrupt-remapping */
348 cmd = iommu->gcmd | DMA_GCMD_IRE;
349 iommu->gcmd |= DMA_GCMD_IRE;
350 writel(cmd, iommu->reg + DMAR_GCMD_REG);
352 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
353 readl, (sts & DMA_GSTS_IRES), sts);
355 spin_unlock_irqrestore(&iommu->register_lock, flags);
359 static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
361 struct ir_table *ir_table;
364 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
367 if (!iommu->ir_table)
370 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, INTR_REMAP_PAGE_ORDER);
373 printk(KERN_ERR "failed to allocate pages of order %d\n",
374 INTR_REMAP_PAGE_ORDER);
375 kfree(iommu->ir_table);
379 ir_table->base = page_address(pages);
381 iommu_set_intr_remapping(iommu, mode);
385 int __init enable_intr_remapping(int eim)
387 struct dmar_drhd_unit *drhd;
391 * check for the Interrupt-remapping support
393 for_each_drhd_unit(drhd) {
394 struct intel_iommu *iommu = drhd->iommu;
396 if (!ecap_ir_support(iommu->ecap))
399 if (eim && !ecap_eim_support(iommu->ecap)) {
400 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
401 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
407 * Enable queued invalidation for all the DRHD's.
409 for_each_drhd_unit(drhd) {
411 struct intel_iommu *iommu = drhd->iommu;
412 ret = dmar_enable_qi(iommu);
415 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
416 " invalidation, ecap %Lx, ret %d\n",
417 drhd->reg_base_addr, iommu->ecap, ret);
423 * Setup Interrupt-remapping for all the DRHD's now.
425 for_each_drhd_unit(drhd) {
426 struct intel_iommu *iommu = drhd->iommu;
428 if (!ecap_ir_support(iommu->ecap))
431 if (setup_intr_remapping(iommu, eim))
440 intr_remapping_enabled = 1;
446 * handle error condition gracefully here!
451 static int ir_parse_ioapic_scope(struct acpi_dmar_header *header,
452 struct intel_iommu *iommu)
454 struct acpi_dmar_hardware_unit *drhd;
455 struct acpi_dmar_device_scope *scope;
458 drhd = (struct acpi_dmar_hardware_unit *)header;
460 start = (void *)(drhd + 1);
461 end = ((void *)drhd) + header->length;
463 while (start < end) {
465 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
466 if (ir_ioapic_num == MAX_IO_APICS) {
467 printk(KERN_WARNING "Exceeded Max IO APICS\n");
471 printk(KERN_INFO "IOAPIC id %d under DRHD base"
472 " 0x%Lx\n", scope->enumeration_id,
475 ir_ioapic[ir_ioapic_num].iommu = iommu;
476 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
479 start += scope->length;
486 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
489 int __init parse_ioapics_under_ir(void)
491 struct dmar_drhd_unit *drhd;
492 int ir_supported = 0;
494 for_each_drhd_unit(drhd) {
495 struct intel_iommu *iommu = drhd->iommu;
497 if (ecap_ir_support(iommu->ecap)) {
498 if (ir_parse_ioapic_scope(drhd->hdr, iommu))
505 if (ir_supported && ir_ioapic_num != nr_ioapics) {
507 "Not all IO-APIC's listed under remapping hardware\n");