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>
9 #include <linux/intel-iommu.h>
10 #include "intr_remapping.h"
12 static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
13 static int ir_ioapic_num;
14 int intr_remapping_enabled;
17 struct intel_iommu *iommu;
23 #ifdef CONFIG_SPARSE_IRQ
24 static struct irq_2_iommu *get_one_free_irq_2_iommu(int cpu)
26 struct irq_2_iommu *iommu;
29 node = cpu_to_node(cpu);
31 iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node);
32 printk(KERN_DEBUG "alloc irq_2_iommu on cpu %d node %d\n", cpu, node);
37 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
39 struct irq_desc *desc;
41 desc = irq_to_desc(irq);
43 if (WARN_ON_ONCE(!desc))
46 return desc->irq_2_iommu;
49 static struct irq_2_iommu *irq_2_iommu_alloc_cpu(unsigned int irq, int cpu)
51 struct irq_desc *desc;
52 struct irq_2_iommu *irq_iommu;
55 * alloc irq desc if not allocated already.
57 desc = irq_to_desc_alloc_cpu(irq, cpu);
59 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
63 irq_iommu = desc->irq_2_iommu;
66 desc->irq_2_iommu = get_one_free_irq_2_iommu(cpu);
68 return desc->irq_2_iommu;
71 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
73 return irq_2_iommu_alloc_cpu(irq, boot_cpu_id);
76 #else /* !CONFIG_SPARSE_IRQ */
78 static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
80 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
83 return &irq_2_iommuX[irq];
87 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
89 return irq_2_iommu(irq);
93 static DEFINE_SPINLOCK(irq_2_ir_lock);
95 static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
97 struct irq_2_iommu *irq_iommu;
99 irq_iommu = irq_2_iommu(irq);
104 if (!irq_iommu->iommu)
110 int irq_remapped(int irq)
112 return valid_irq_2_iommu(irq) != NULL;
115 int get_irte(int irq, struct irte *entry)
118 struct irq_2_iommu *irq_iommu;
123 spin_lock(&irq_2_ir_lock);
124 irq_iommu = valid_irq_2_iommu(irq);
126 spin_unlock(&irq_2_ir_lock);
130 index = irq_iommu->irte_index + irq_iommu->sub_handle;
131 *entry = *(irq_iommu->iommu->ir_table->base + index);
133 spin_unlock(&irq_2_ir_lock);
137 int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
139 struct ir_table *table = iommu->ir_table;
140 struct irq_2_iommu *irq_iommu;
141 u16 index, start_index;
142 unsigned int mask = 0;
148 #ifndef CONFIG_SPARSE_IRQ
149 /* protect irq_2_iommu_alloc later */
155 * start the IRTE search from index 0.
157 index = start_index = 0;
160 count = __roundup_pow_of_two(count);
164 if (mask > ecap_max_handle_mask(iommu->ecap)) {
166 "Requested mask %x exceeds the max invalidation handle"
167 " mask value %Lx\n", mask,
168 ecap_max_handle_mask(iommu->ecap));
172 spin_lock(&irq_2_ir_lock);
174 for (i = index; i < index + count; i++)
175 if (table->base[i].present)
177 /* empty index found */
178 if (i == index + count)
181 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
183 if (index == start_index) {
184 spin_unlock(&irq_2_ir_lock);
185 printk(KERN_ERR "can't allocate an IRTE\n");
190 for (i = index; i < index + count; i++)
191 table->base[i].present = 1;
193 irq_iommu = irq_2_iommu_alloc(irq);
195 spin_unlock(&irq_2_ir_lock);
196 printk(KERN_ERR "can't allocate irq_2_iommu\n");
200 irq_iommu->iommu = iommu;
201 irq_iommu->irte_index = index;
202 irq_iommu->sub_handle = 0;
203 irq_iommu->irte_mask = mask;
205 spin_unlock(&irq_2_ir_lock);
210 static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
214 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
218 return qi_submit_sync(&desc, iommu);
221 int map_irq_to_irte_handle(int irq, u16 *sub_handle)
224 struct irq_2_iommu *irq_iommu;
226 spin_lock(&irq_2_ir_lock);
227 irq_iommu = valid_irq_2_iommu(irq);
229 spin_unlock(&irq_2_ir_lock);
233 *sub_handle = irq_iommu->sub_handle;
234 index = irq_iommu->irte_index;
235 spin_unlock(&irq_2_ir_lock);
239 int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
241 struct irq_2_iommu *irq_iommu;
243 spin_lock(&irq_2_ir_lock);
245 irq_iommu = irq_2_iommu_alloc(irq);
248 spin_unlock(&irq_2_ir_lock);
249 printk(KERN_ERR "can't allocate irq_2_iommu\n");
253 irq_iommu->iommu = iommu;
254 irq_iommu->irte_index = index;
255 irq_iommu->sub_handle = subhandle;
256 irq_iommu->irte_mask = 0;
258 spin_unlock(&irq_2_ir_lock);
263 int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
265 struct irq_2_iommu *irq_iommu;
267 spin_lock(&irq_2_ir_lock);
268 irq_iommu = valid_irq_2_iommu(irq);
270 spin_unlock(&irq_2_ir_lock);
274 irq_iommu->iommu = NULL;
275 irq_iommu->irte_index = 0;
276 irq_iommu->sub_handle = 0;
277 irq_2_iommu(irq)->irte_mask = 0;
279 spin_unlock(&irq_2_ir_lock);
284 int modify_irte(int irq, struct irte *irte_modified)
289 struct intel_iommu *iommu;
290 struct irq_2_iommu *irq_iommu;
292 spin_lock(&irq_2_ir_lock);
293 irq_iommu = valid_irq_2_iommu(irq);
295 spin_unlock(&irq_2_ir_lock);
299 iommu = irq_iommu->iommu;
301 index = irq_iommu->irte_index + irq_iommu->sub_handle;
302 irte = &iommu->ir_table->base[index];
304 set_64bit((unsigned long *)irte, irte_modified->low | (1 << 1));
305 __iommu_flush_cache(iommu, irte, sizeof(*irte));
307 rc = qi_flush_iec(iommu, index, 0);
308 spin_unlock(&irq_2_ir_lock);
313 int flush_irte(int irq)
317 struct intel_iommu *iommu;
318 struct irq_2_iommu *irq_iommu;
320 spin_lock(&irq_2_ir_lock);
321 irq_iommu = valid_irq_2_iommu(irq);
323 spin_unlock(&irq_2_ir_lock);
327 iommu = irq_iommu->iommu;
329 index = irq_iommu->irte_index + irq_iommu->sub_handle;
331 rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
332 spin_unlock(&irq_2_ir_lock);
337 struct intel_iommu *map_ioapic_to_ir(int apic)
341 for (i = 0; i < MAX_IO_APICS; i++)
342 if (ir_ioapic[i].id == apic)
343 return ir_ioapic[i].iommu;
347 struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
349 struct dmar_drhd_unit *drhd;
351 drhd = dmar_find_matched_drhd_unit(dev);
358 int free_irte(int irq)
363 struct intel_iommu *iommu;
364 struct irq_2_iommu *irq_iommu;
366 spin_lock(&irq_2_ir_lock);
367 irq_iommu = valid_irq_2_iommu(irq);
369 spin_unlock(&irq_2_ir_lock);
373 iommu = irq_iommu->iommu;
375 index = irq_iommu->irte_index + irq_iommu->sub_handle;
376 irte = &iommu->ir_table->base[index];
378 if (!irq_iommu->sub_handle) {
379 for (i = 0; i < (1 << irq_iommu->irte_mask); i++)
380 set_64bit((unsigned long *)irte, 0);
381 rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
384 irq_iommu->iommu = NULL;
385 irq_iommu->irte_index = 0;
386 irq_iommu->sub_handle = 0;
387 irq_iommu->irte_mask = 0;
389 spin_unlock(&irq_2_ir_lock);
394 static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
400 addr = virt_to_phys((void *)iommu->ir_table->base);
402 spin_lock_irqsave(&iommu->register_lock, flags);
404 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
405 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
407 /* Set interrupt-remapping table pointer */
408 cmd = iommu->gcmd | DMA_GCMD_SIRTP;
409 writel(cmd, iommu->reg + DMAR_GCMD_REG);
411 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
412 readl, (sts & DMA_GSTS_IRTPS), sts);
413 spin_unlock_irqrestore(&iommu->register_lock, flags);
416 * global invalidation of interrupt entry cache before enabling
417 * interrupt-remapping.
419 qi_global_iec(iommu);
421 spin_lock_irqsave(&iommu->register_lock, flags);
423 /* Enable interrupt-remapping */
424 cmd = iommu->gcmd | DMA_GCMD_IRE;
425 iommu->gcmd |= DMA_GCMD_IRE;
426 writel(cmd, iommu->reg + DMAR_GCMD_REG);
428 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
429 readl, (sts & DMA_GSTS_IRES), sts);
431 spin_unlock_irqrestore(&iommu->register_lock, flags);
435 static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
437 struct ir_table *ir_table;
440 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
443 if (!iommu->ir_table)
446 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, INTR_REMAP_PAGE_ORDER);
449 printk(KERN_ERR "failed to allocate pages of order %d\n",
450 INTR_REMAP_PAGE_ORDER);
451 kfree(iommu->ir_table);
455 ir_table->base = page_address(pages);
457 iommu_set_intr_remapping(iommu, mode);
461 int __init enable_intr_remapping(int eim)
463 struct dmar_drhd_unit *drhd;
467 * check for the Interrupt-remapping support
469 for_each_drhd_unit(drhd) {
470 struct intel_iommu *iommu = drhd->iommu;
472 if (!ecap_ir_support(iommu->ecap))
475 if (eim && !ecap_eim_support(iommu->ecap)) {
476 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
477 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
483 * Enable queued invalidation for all the DRHD's.
485 for_each_drhd_unit(drhd) {
487 struct intel_iommu *iommu = drhd->iommu;
488 ret = dmar_enable_qi(iommu);
491 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
492 " invalidation, ecap %Lx, ret %d\n",
493 drhd->reg_base_addr, iommu->ecap, ret);
499 * Setup Interrupt-remapping for all the DRHD's now.
501 for_each_drhd_unit(drhd) {
502 struct intel_iommu *iommu = drhd->iommu;
504 if (!ecap_ir_support(iommu->ecap))
507 if (setup_intr_remapping(iommu, eim))
516 intr_remapping_enabled = 1;
522 * handle error condition gracefully here!
527 static int ir_parse_ioapic_scope(struct acpi_dmar_header *header,
528 struct intel_iommu *iommu)
530 struct acpi_dmar_hardware_unit *drhd;
531 struct acpi_dmar_device_scope *scope;
534 drhd = (struct acpi_dmar_hardware_unit *)header;
536 start = (void *)(drhd + 1);
537 end = ((void *)drhd) + header->length;
539 while (start < end) {
541 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
542 if (ir_ioapic_num == MAX_IO_APICS) {
543 printk(KERN_WARNING "Exceeded Max IO APICS\n");
547 printk(KERN_INFO "IOAPIC id %d under DRHD base"
548 " 0x%Lx\n", scope->enumeration_id,
551 ir_ioapic[ir_ioapic_num].iommu = iommu;
552 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
555 start += scope->length;
562 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
565 int __init parse_ioapics_under_ir(void)
567 struct dmar_drhd_unit *drhd;
568 int ir_supported = 0;
570 for_each_drhd_unit(drhd) {
571 struct intel_iommu *iommu = drhd->iommu;
573 if (ecap_ir_support(iommu->ecap)) {
574 if (ir_parse_ioapic_scope(drhd->hdr, iommu))
581 if (ir_supported && ir_ioapic_num != nr_ioapics) {
583 "Not all IO-APIC's listed under remapping hardware\n");