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>
10 #include <linux/intel-iommu.h>
11 #include "intr_remapping.h"
13 static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
14 static int ir_ioapic_num;
15 int intr_remapping_enabled;
18 struct intel_iommu *iommu;
24 #ifdef CONFIG_SPARSE_IRQ
25 static struct irq_2_iommu *get_one_free_irq_2_iommu(int cpu)
27 struct irq_2_iommu *iommu;
30 node = cpu_to_node(cpu);
32 iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node);
33 printk(KERN_DEBUG "alloc irq_2_iommu on cpu %d node %d\n", cpu, node);
38 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
40 struct irq_desc *desc;
42 desc = irq_to_desc(irq);
44 if (WARN_ON_ONCE(!desc))
47 return desc->irq_2_iommu;
50 static struct irq_2_iommu *irq_2_iommu_alloc_cpu(unsigned int irq, int cpu)
52 struct irq_desc *desc;
53 struct irq_2_iommu *irq_iommu;
56 * alloc irq desc if not allocated already.
58 desc = irq_to_desc_alloc_cpu(irq, cpu);
60 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
64 irq_iommu = desc->irq_2_iommu;
67 desc->irq_2_iommu = get_one_free_irq_2_iommu(cpu);
69 return desc->irq_2_iommu;
72 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
74 return irq_2_iommu_alloc_cpu(irq, boot_cpu_id);
77 #else /* !CONFIG_SPARSE_IRQ */
79 static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
81 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
84 return &irq_2_iommuX[irq];
88 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
90 return irq_2_iommu(irq);
94 static DEFINE_SPINLOCK(irq_2_ir_lock);
96 static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
98 struct irq_2_iommu *irq_iommu;
100 irq_iommu = irq_2_iommu(irq);
105 if (!irq_iommu->iommu)
111 int irq_remapped(int irq)
113 return valid_irq_2_iommu(irq) != NULL;
116 int get_irte(int irq, struct irte *entry)
119 struct irq_2_iommu *irq_iommu;
124 spin_lock(&irq_2_ir_lock);
125 irq_iommu = valid_irq_2_iommu(irq);
127 spin_unlock(&irq_2_ir_lock);
131 index = irq_iommu->irte_index + irq_iommu->sub_handle;
132 *entry = *(irq_iommu->iommu->ir_table->base + index);
134 spin_unlock(&irq_2_ir_lock);
138 int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
140 struct ir_table *table = iommu->ir_table;
141 struct irq_2_iommu *irq_iommu;
142 u16 index, start_index;
143 unsigned int mask = 0;
149 #ifndef CONFIG_SPARSE_IRQ
150 /* protect irq_2_iommu_alloc later */
156 * start the IRTE search from index 0.
158 index = start_index = 0;
161 count = __roundup_pow_of_two(count);
165 if (mask > ecap_max_handle_mask(iommu->ecap)) {
167 "Requested mask %x exceeds the max invalidation handle"
168 " mask value %Lx\n", mask,
169 ecap_max_handle_mask(iommu->ecap));
173 spin_lock(&irq_2_ir_lock);
175 for (i = index; i < index + count; i++)
176 if (table->base[i].present)
178 /* empty index found */
179 if (i == index + count)
182 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
184 if (index == start_index) {
185 spin_unlock(&irq_2_ir_lock);
186 printk(KERN_ERR "can't allocate an IRTE\n");
191 for (i = index; i < index + count; i++)
192 table->base[i].present = 1;
194 irq_iommu = irq_2_iommu_alloc(irq);
196 spin_unlock(&irq_2_ir_lock);
197 printk(KERN_ERR "can't allocate irq_2_iommu\n");
201 irq_iommu->iommu = iommu;
202 irq_iommu->irte_index = index;
203 irq_iommu->sub_handle = 0;
204 irq_iommu->irte_mask = mask;
206 spin_unlock(&irq_2_ir_lock);
211 static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
215 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
219 return qi_submit_sync(&desc, iommu);
222 int map_irq_to_irte_handle(int irq, u16 *sub_handle)
225 struct irq_2_iommu *irq_iommu;
227 spin_lock(&irq_2_ir_lock);
228 irq_iommu = valid_irq_2_iommu(irq);
230 spin_unlock(&irq_2_ir_lock);
234 *sub_handle = irq_iommu->sub_handle;
235 index = irq_iommu->irte_index;
236 spin_unlock(&irq_2_ir_lock);
240 int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
242 struct irq_2_iommu *irq_iommu;
244 spin_lock(&irq_2_ir_lock);
246 irq_iommu = irq_2_iommu_alloc(irq);
249 spin_unlock(&irq_2_ir_lock);
250 printk(KERN_ERR "can't allocate irq_2_iommu\n");
254 irq_iommu->iommu = iommu;
255 irq_iommu->irte_index = index;
256 irq_iommu->sub_handle = subhandle;
257 irq_iommu->irte_mask = 0;
259 spin_unlock(&irq_2_ir_lock);
264 int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
266 struct irq_2_iommu *irq_iommu;
268 spin_lock(&irq_2_ir_lock);
269 irq_iommu = valid_irq_2_iommu(irq);
271 spin_unlock(&irq_2_ir_lock);
275 irq_iommu->iommu = NULL;
276 irq_iommu->irte_index = 0;
277 irq_iommu->sub_handle = 0;
278 irq_2_iommu(irq)->irte_mask = 0;
280 spin_unlock(&irq_2_ir_lock);
285 int modify_irte(int irq, struct irte *irte_modified)
290 struct intel_iommu *iommu;
291 struct irq_2_iommu *irq_iommu;
293 spin_lock(&irq_2_ir_lock);
294 irq_iommu = valid_irq_2_iommu(irq);
296 spin_unlock(&irq_2_ir_lock);
300 iommu = irq_iommu->iommu;
302 index = irq_iommu->irte_index + irq_iommu->sub_handle;
303 irte = &iommu->ir_table->base[index];
305 set_64bit((unsigned long *)irte, irte_modified->low | (1 << 1));
306 __iommu_flush_cache(iommu, irte, sizeof(*irte));
308 rc = qi_flush_iec(iommu, index, 0);
309 spin_unlock(&irq_2_ir_lock);
314 int flush_irte(int irq)
318 struct intel_iommu *iommu;
319 struct irq_2_iommu *irq_iommu;
321 spin_lock(&irq_2_ir_lock);
322 irq_iommu = valid_irq_2_iommu(irq);
324 spin_unlock(&irq_2_ir_lock);
328 iommu = irq_iommu->iommu;
330 index = irq_iommu->irte_index + irq_iommu->sub_handle;
332 rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
333 spin_unlock(&irq_2_ir_lock);
338 struct intel_iommu *map_ioapic_to_ir(int apic)
342 for (i = 0; i < MAX_IO_APICS; i++)
343 if (ir_ioapic[i].id == apic)
344 return ir_ioapic[i].iommu;
348 struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
350 struct dmar_drhd_unit *drhd;
352 drhd = dmar_find_matched_drhd_unit(dev);
359 int free_irte(int irq)
364 struct intel_iommu *iommu;
365 struct irq_2_iommu *irq_iommu;
367 spin_lock(&irq_2_ir_lock);
368 irq_iommu = valid_irq_2_iommu(irq);
370 spin_unlock(&irq_2_ir_lock);
374 iommu = irq_iommu->iommu;
376 index = irq_iommu->irte_index + irq_iommu->sub_handle;
377 irte = &iommu->ir_table->base[index];
379 if (!irq_iommu->sub_handle) {
380 for (i = 0; i < (1 << irq_iommu->irte_mask); i++)
381 set_64bit((unsigned long *)irte, 0);
382 rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
385 irq_iommu->iommu = NULL;
386 irq_iommu->irte_index = 0;
387 irq_iommu->sub_handle = 0;
388 irq_iommu->irte_mask = 0;
390 spin_unlock(&irq_2_ir_lock);
395 static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
401 addr = virt_to_phys((void *)iommu->ir_table->base);
403 spin_lock_irqsave(&iommu->register_lock, flags);
405 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
406 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
408 /* Set interrupt-remapping table pointer */
409 cmd = iommu->gcmd | DMA_GCMD_SIRTP;
410 writel(cmd, iommu->reg + DMAR_GCMD_REG);
412 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
413 readl, (sts & DMA_GSTS_IRTPS), sts);
414 spin_unlock_irqrestore(&iommu->register_lock, flags);
417 * global invalidation of interrupt entry cache before enabling
418 * interrupt-remapping.
420 qi_global_iec(iommu);
422 spin_lock_irqsave(&iommu->register_lock, flags);
424 /* Enable interrupt-remapping */
425 cmd = iommu->gcmd | DMA_GCMD_IRE;
426 iommu->gcmd |= DMA_GCMD_IRE;
427 writel(cmd, iommu->reg + DMAR_GCMD_REG);
429 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
430 readl, (sts & DMA_GSTS_IRES), sts);
432 spin_unlock_irqrestore(&iommu->register_lock, flags);
436 static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
438 struct ir_table *ir_table;
441 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
444 if (!iommu->ir_table)
447 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, INTR_REMAP_PAGE_ORDER);
450 printk(KERN_ERR "failed to allocate pages of order %d\n",
451 INTR_REMAP_PAGE_ORDER);
452 kfree(iommu->ir_table);
456 ir_table->base = page_address(pages);
458 iommu_set_intr_remapping(iommu, mode);
462 int __init enable_intr_remapping(int eim)
464 struct dmar_drhd_unit *drhd;
468 * check for the Interrupt-remapping support
470 for_each_drhd_unit(drhd) {
471 struct intel_iommu *iommu = drhd->iommu;
473 if (!ecap_ir_support(iommu->ecap))
476 if (eim && !ecap_eim_support(iommu->ecap)) {
477 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
478 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
484 * Enable queued invalidation for all the DRHD's.
486 for_each_drhd_unit(drhd) {
488 struct intel_iommu *iommu = drhd->iommu;
489 ret = dmar_enable_qi(iommu);
492 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
493 " invalidation, ecap %Lx, ret %d\n",
494 drhd->reg_base_addr, iommu->ecap, ret);
500 * Setup Interrupt-remapping for all the DRHD's now.
502 for_each_drhd_unit(drhd) {
503 struct intel_iommu *iommu = drhd->iommu;
505 if (!ecap_ir_support(iommu->ecap))
508 if (setup_intr_remapping(iommu, eim))
517 intr_remapping_enabled = 1;
523 * handle error condition gracefully here!
528 static int ir_parse_ioapic_scope(struct acpi_dmar_header *header,
529 struct intel_iommu *iommu)
531 struct acpi_dmar_hardware_unit *drhd;
532 struct acpi_dmar_device_scope *scope;
535 drhd = (struct acpi_dmar_hardware_unit *)header;
537 start = (void *)(drhd + 1);
538 end = ((void *)drhd) + header->length;
540 while (start < end) {
542 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
543 if (ir_ioapic_num == MAX_IO_APICS) {
544 printk(KERN_WARNING "Exceeded Max IO APICS\n");
548 printk(KERN_INFO "IOAPIC id %d under DRHD base"
549 " 0x%Lx\n", scope->enumeration_id,
552 ir_ioapic[ir_ioapic_num].iommu = iommu;
553 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
556 start += scope->length;
563 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
566 int __init parse_ioapics_under_ir(void)
568 struct dmar_drhd_unit *drhd;
569 int ir_supported = 0;
571 for_each_drhd_unit(drhd) {
572 struct intel_iommu *iommu = drhd->iommu;
574 if (ecap_ir_support(iommu->ecap)) {
575 if (ir_parse_ioapic_scope(drhd->hdr, iommu))
582 if (ir_supported && ir_ioapic_num != nr_ioapics) {
584 "Not all IO-APIC's listed under remapping hardware\n");