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 #ifdef CONFIG_SPARSE_IRQ
23 static struct irq_2_iommu *get_one_free_irq_2_iommu(int cpu)
25 struct irq_2_iommu *iommu;
28 node = cpu_to_node(cpu);
30 iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node);
31 printk(KERN_DEBUG "alloc irq_2_iommu on cpu %d node %d\n", cpu, node);
36 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
38 struct irq_desc *desc;
40 desc = irq_to_desc(irq);
42 if (WARN_ON_ONCE(!desc))
45 return desc->irq_2_iommu;
48 static struct irq_2_iommu *irq_2_iommu_alloc_cpu(unsigned int irq, int cpu)
50 struct irq_desc *desc;
51 struct irq_2_iommu *irq_iommu;
54 * alloc irq desc if not allocated already.
56 desc = irq_to_desc_alloc_cpu(irq, cpu);
58 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
62 irq_iommu = desc->irq_2_iommu;
65 desc->irq_2_iommu = get_one_free_irq_2_iommu(cpu);
67 return desc->irq_2_iommu;
70 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
72 return irq_2_iommu_alloc_cpu(irq, boot_cpu_id);
75 #else /* !CONFIG_SPARSE_IRQ */
77 static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
79 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
82 return &irq_2_iommuX[irq];
86 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
88 return irq_2_iommu(irq);
92 static DEFINE_SPINLOCK(irq_2_ir_lock);
94 static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
96 struct irq_2_iommu *irq_iommu;
98 irq_iommu = irq_2_iommu(irq);
103 if (!irq_iommu->iommu)
109 int irq_remapped(int irq)
111 return valid_irq_2_iommu(irq) != NULL;
114 int get_irte(int irq, struct irte *entry)
117 struct irq_2_iommu *irq_iommu;
122 spin_lock(&irq_2_ir_lock);
123 irq_iommu = valid_irq_2_iommu(irq);
125 spin_unlock(&irq_2_ir_lock);
129 index = irq_iommu->irte_index + irq_iommu->sub_handle;
130 *entry = *(irq_iommu->iommu->ir_table->base + index);
132 spin_unlock(&irq_2_ir_lock);
136 int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
138 struct ir_table *table = iommu->ir_table;
139 struct irq_2_iommu *irq_iommu;
140 u16 index, start_index;
141 unsigned int mask = 0;
147 #ifndef CONFIG_SPARSE_IRQ
148 /* protect irq_2_iommu_alloc later */
154 * start the IRTE search from index 0.
156 index = start_index = 0;
159 count = __roundup_pow_of_two(count);
163 if (mask > ecap_max_handle_mask(iommu->ecap)) {
165 "Requested mask %x exceeds the max invalidation handle"
166 " mask value %Lx\n", mask,
167 ecap_max_handle_mask(iommu->ecap));
171 spin_lock(&irq_2_ir_lock);
173 for (i = index; i < index + count; i++)
174 if (table->base[i].present)
176 /* empty index found */
177 if (i == index + count)
180 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
182 if (index == start_index) {
183 spin_unlock(&irq_2_ir_lock);
184 printk(KERN_ERR "can't allocate an IRTE\n");
189 for (i = index; i < index + count; i++)
190 table->base[i].present = 1;
192 irq_iommu = irq_2_iommu_alloc(irq);
194 spin_unlock(&irq_2_ir_lock);
195 printk(KERN_ERR "can't allocate irq_2_iommu\n");
199 irq_iommu->iommu = iommu;
200 irq_iommu->irte_index = index;
201 irq_iommu->sub_handle = 0;
202 irq_iommu->irte_mask = mask;
204 spin_unlock(&irq_2_ir_lock);
209 static void qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
213 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
217 qi_submit_sync(&desc, iommu);
220 int map_irq_to_irte_handle(int irq, u16 *sub_handle)
223 struct irq_2_iommu *irq_iommu;
225 spin_lock(&irq_2_ir_lock);
226 irq_iommu = valid_irq_2_iommu(irq);
228 spin_unlock(&irq_2_ir_lock);
232 *sub_handle = irq_iommu->sub_handle;
233 index = irq_iommu->irte_index;
234 spin_unlock(&irq_2_ir_lock);
238 int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
240 struct irq_2_iommu *irq_iommu;
242 spin_lock(&irq_2_ir_lock);
244 irq_iommu = irq_2_iommu_alloc(irq);
247 spin_unlock(&irq_2_ir_lock);
248 printk(KERN_ERR "can't allocate irq_2_iommu\n");
252 irq_iommu->iommu = iommu;
253 irq_iommu->irte_index = index;
254 irq_iommu->sub_handle = subhandle;
255 irq_iommu->irte_mask = 0;
257 spin_unlock(&irq_2_ir_lock);
262 int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
264 struct irq_2_iommu *irq_iommu;
266 spin_lock(&irq_2_ir_lock);
267 irq_iommu = valid_irq_2_iommu(irq);
269 spin_unlock(&irq_2_ir_lock);
273 irq_iommu->iommu = NULL;
274 irq_iommu->irte_index = 0;
275 irq_iommu->sub_handle = 0;
276 irq_2_iommu(irq)->irte_mask = 0;
278 spin_unlock(&irq_2_ir_lock);
283 int modify_irte(int irq, struct irte *irte_modified)
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 set_64bit((unsigned long *)irte, irte_modified->low | (1 << 1));
303 __iommu_flush_cache(iommu, irte, sizeof(*irte));
305 qi_flush_iec(iommu, index, 0);
307 spin_unlock(&irq_2_ir_lock);
311 int flush_irte(int irq)
314 struct intel_iommu *iommu;
315 struct irq_2_iommu *irq_iommu;
317 spin_lock(&irq_2_ir_lock);
318 irq_iommu = valid_irq_2_iommu(irq);
320 spin_unlock(&irq_2_ir_lock);
324 iommu = irq_iommu->iommu;
326 index = irq_iommu->irte_index + irq_iommu->sub_handle;
328 qi_flush_iec(iommu, index, irq_iommu->irte_mask);
329 spin_unlock(&irq_2_ir_lock);
334 struct intel_iommu *map_ioapic_to_ir(int apic)
338 for (i = 0; i < MAX_IO_APICS; i++)
339 if (ir_ioapic[i].id == apic)
340 return ir_ioapic[i].iommu;
344 struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
346 struct dmar_drhd_unit *drhd;
348 drhd = dmar_find_matched_drhd_unit(dev);
355 int free_irte(int irq)
359 struct intel_iommu *iommu;
360 struct irq_2_iommu *irq_iommu;
362 spin_lock(&irq_2_ir_lock);
363 irq_iommu = valid_irq_2_iommu(irq);
365 spin_unlock(&irq_2_ir_lock);
369 iommu = irq_iommu->iommu;
371 index = irq_iommu->irte_index + irq_iommu->sub_handle;
372 irte = &iommu->ir_table->base[index];
374 if (!irq_iommu->sub_handle) {
375 for (i = 0; i < (1 << irq_iommu->irte_mask); i++)
376 set_64bit((unsigned long *)irte, 0);
377 qi_flush_iec(iommu, index, irq_iommu->irte_mask);
380 irq_iommu->iommu = NULL;
381 irq_iommu->irte_index = 0;
382 irq_iommu->sub_handle = 0;
383 irq_iommu->irte_mask = 0;
385 spin_unlock(&irq_2_ir_lock);
390 static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
396 addr = virt_to_phys((void *)iommu->ir_table->base);
398 spin_lock_irqsave(&iommu->register_lock, flags);
400 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
401 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
403 /* Set interrupt-remapping table pointer */
404 cmd = iommu->gcmd | DMA_GCMD_SIRTP;
405 writel(cmd, iommu->reg + DMAR_GCMD_REG);
407 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
408 readl, (sts & DMA_GSTS_IRTPS), sts);
409 spin_unlock_irqrestore(&iommu->register_lock, flags);
412 * global invalidation of interrupt entry cache before enabling
413 * interrupt-remapping.
415 qi_global_iec(iommu);
417 spin_lock_irqsave(&iommu->register_lock, flags);
419 /* Enable interrupt-remapping */
420 cmd = iommu->gcmd | DMA_GCMD_IRE;
421 iommu->gcmd |= DMA_GCMD_IRE;
422 writel(cmd, iommu->reg + DMAR_GCMD_REG);
424 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
425 readl, (sts & DMA_GSTS_IRES), sts);
427 spin_unlock_irqrestore(&iommu->register_lock, flags);
431 static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
433 struct ir_table *ir_table;
436 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
439 if (!iommu->ir_table)
442 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, INTR_REMAP_PAGE_ORDER);
445 printk(KERN_ERR "failed to allocate pages of order %d\n",
446 INTR_REMAP_PAGE_ORDER);
447 kfree(iommu->ir_table);
451 ir_table->base = page_address(pages);
453 iommu_set_intr_remapping(iommu, mode);
457 int __init enable_intr_remapping(int eim)
459 struct dmar_drhd_unit *drhd;
463 * check for the Interrupt-remapping support
465 for_each_drhd_unit(drhd) {
466 struct intel_iommu *iommu = drhd->iommu;
468 if (!ecap_ir_support(iommu->ecap))
471 if (eim && !ecap_eim_support(iommu->ecap)) {
472 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
473 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
479 * Enable queued invalidation for all the DRHD's.
481 for_each_drhd_unit(drhd) {
483 struct intel_iommu *iommu = drhd->iommu;
484 ret = dmar_enable_qi(iommu);
487 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
488 " invalidation, ecap %Lx, ret %d\n",
489 drhd->reg_base_addr, iommu->ecap, ret);
495 * Setup Interrupt-remapping for all the DRHD's now.
497 for_each_drhd_unit(drhd) {
498 struct intel_iommu *iommu = drhd->iommu;
500 if (!ecap_ir_support(iommu->ecap))
503 if (setup_intr_remapping(iommu, eim))
512 intr_remapping_enabled = 1;
518 * handle error condition gracefully here!
523 static int ir_parse_ioapic_scope(struct acpi_dmar_header *header,
524 struct intel_iommu *iommu)
526 struct acpi_dmar_hardware_unit *drhd;
527 struct acpi_dmar_device_scope *scope;
530 drhd = (struct acpi_dmar_hardware_unit *)header;
532 start = (void *)(drhd + 1);
533 end = ((void *)drhd) + header->length;
535 while (start < end) {
537 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
538 if (ir_ioapic_num == MAX_IO_APICS) {
539 printk(KERN_WARNING "Exceeded Max IO APICS\n");
543 printk(KERN_INFO "IOAPIC id %d under DRHD base"
544 " 0x%Lx\n", scope->enumeration_id,
547 ir_ioapic[ir_ioapic_num].iommu = iommu;
548 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
551 start += scope->length;
558 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
561 int __init parse_ioapics_under_ir(void)
563 struct dmar_drhd_unit *drhd;
564 int ir_supported = 0;
566 for_each_drhd_unit(drhd) {
567 struct intel_iommu *iommu = drhd->iommu;
569 if (ecap_ir_support(iommu->ecap)) {
570 if (ir_parse_ioapic_scope(drhd->hdr, iommu))
577 if (ir_supported && ir_ioapic_num != nr_ioapics) {
579 "Not all IO-APIC's listed under remapping hardware\n");