1 #include <linux/interrupt.h>
2 #include <linux/dmar.h>
3 #include <linux/spinlock.h>
4 #include <linux/jiffies.h>
5 #include <linux/hpet.h>
8 #include <asm/io_apic.h>
11 #include <linux/intel-iommu.h>
12 #include "intr_remapping.h"
13 #include <acpi/acpi.h>
14 #include <asm/pci-direct.h>
17 static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
18 static struct hpet_scope ir_hpet[MAX_HPET_TBS];
19 static int ir_ioapic_num, ir_hpet_num;
20 int intr_remapping_enabled;
22 static int disable_intremap;
23 static __init int setup_nointremap(char *str)
28 early_param("nointremap", setup_nointremap);
31 struct intel_iommu *iommu;
37 #ifdef CONFIG_GENERIC_HARDIRQS
38 static struct irq_2_iommu *get_one_free_irq_2_iommu(int node)
40 struct irq_2_iommu *iommu;
42 iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node);
43 printk(KERN_DEBUG "alloc irq_2_iommu on node %d\n", node);
48 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
50 struct irq_desc *desc;
52 desc = irq_to_desc(irq);
54 if (WARN_ON_ONCE(!desc))
57 return desc->irq_2_iommu;
60 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
62 struct irq_desc *desc;
63 struct irq_2_iommu *irq_iommu;
65 desc = irq_to_desc(irq);
67 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
71 irq_iommu = desc->irq_2_iommu;
74 desc->irq_2_iommu = get_one_free_irq_2_iommu(irq_node(irq));
76 return desc->irq_2_iommu;
79 #else /* !CONFIG_SPARSE_IRQ */
81 static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
83 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
86 return &irq_2_iommuX[irq];
90 static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
92 return irq_2_iommu(irq);
96 static DEFINE_SPINLOCK(irq_2_ir_lock);
98 static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
100 struct irq_2_iommu *irq_iommu;
102 irq_iommu = irq_2_iommu(irq);
107 if (!irq_iommu->iommu)
113 int irq_remapped(int irq)
115 return valid_irq_2_iommu(irq) != NULL;
118 int get_irte(int irq, struct irte *entry)
121 struct irq_2_iommu *irq_iommu;
127 spin_lock_irqsave(&irq_2_ir_lock, flags);
128 irq_iommu = valid_irq_2_iommu(irq);
130 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
134 index = irq_iommu->irte_index + irq_iommu->sub_handle;
135 *entry = *(irq_iommu->iommu->ir_table->base + index);
137 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
141 int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
143 struct ir_table *table = iommu->ir_table;
144 struct irq_2_iommu *irq_iommu;
145 u16 index, start_index;
146 unsigned int mask = 0;
153 #ifndef CONFIG_SPARSE_IRQ
154 /* protect irq_2_iommu_alloc later */
160 * start the IRTE search from index 0.
162 index = start_index = 0;
165 count = __roundup_pow_of_two(count);
169 if (mask > ecap_max_handle_mask(iommu->ecap)) {
171 "Requested mask %x exceeds the max invalidation handle"
172 " mask value %Lx\n", mask,
173 ecap_max_handle_mask(iommu->ecap));
177 spin_lock_irqsave(&irq_2_ir_lock, flags);
179 for (i = index; i < index + count; i++)
180 if (table->base[i].present)
182 /* empty index found */
183 if (i == index + count)
186 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
188 if (index == start_index) {
189 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
190 printk(KERN_ERR "can't allocate an IRTE\n");
195 for (i = index; i < index + count; i++)
196 table->base[i].present = 1;
198 irq_iommu = irq_2_iommu_alloc(irq);
200 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
201 printk(KERN_ERR "can't allocate irq_2_iommu\n");
205 irq_iommu->iommu = iommu;
206 irq_iommu->irte_index = index;
207 irq_iommu->sub_handle = 0;
208 irq_iommu->irte_mask = mask;
210 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
215 static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
219 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
223 return qi_submit_sync(&desc, iommu);
226 int map_irq_to_irte_handle(int irq, u16 *sub_handle)
229 struct irq_2_iommu *irq_iommu;
232 spin_lock_irqsave(&irq_2_ir_lock, flags);
233 irq_iommu = valid_irq_2_iommu(irq);
235 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
239 *sub_handle = irq_iommu->sub_handle;
240 index = irq_iommu->irte_index;
241 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
245 int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
247 struct irq_2_iommu *irq_iommu;
250 spin_lock_irqsave(&irq_2_ir_lock, flags);
252 irq_iommu = irq_2_iommu_alloc(irq);
255 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
256 printk(KERN_ERR "can't allocate irq_2_iommu\n");
260 irq_iommu->iommu = iommu;
261 irq_iommu->irte_index = index;
262 irq_iommu->sub_handle = subhandle;
263 irq_iommu->irte_mask = 0;
265 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
270 int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
272 struct irq_2_iommu *irq_iommu;
275 spin_lock_irqsave(&irq_2_ir_lock, flags);
276 irq_iommu = valid_irq_2_iommu(irq);
278 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
282 irq_iommu->iommu = NULL;
283 irq_iommu->irte_index = 0;
284 irq_iommu->sub_handle = 0;
285 irq_2_iommu(irq)->irte_mask = 0;
287 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
292 int modify_irte(int irq, struct irte *irte_modified)
297 struct intel_iommu *iommu;
298 struct irq_2_iommu *irq_iommu;
301 spin_lock_irqsave(&irq_2_ir_lock, flags);
302 irq_iommu = valid_irq_2_iommu(irq);
304 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
308 iommu = irq_iommu->iommu;
310 index = irq_iommu->irte_index + irq_iommu->sub_handle;
311 irte = &iommu->ir_table->base[index];
313 set_64bit((unsigned long *)&irte->low, irte_modified->low);
314 set_64bit((unsigned long *)&irte->high, irte_modified->high);
315 __iommu_flush_cache(iommu, irte, sizeof(*irte));
317 rc = qi_flush_iec(iommu, index, 0);
318 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
323 int flush_irte(int irq)
327 struct intel_iommu *iommu;
328 struct irq_2_iommu *irq_iommu;
331 spin_lock_irqsave(&irq_2_ir_lock, flags);
332 irq_iommu = valid_irq_2_iommu(irq);
334 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
338 iommu = irq_iommu->iommu;
340 index = irq_iommu->irte_index + irq_iommu->sub_handle;
342 rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
343 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
348 struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
352 for (i = 0; i < MAX_HPET_TBS; i++)
353 if (ir_hpet[i].id == hpet_id)
354 return ir_hpet[i].iommu;
358 struct intel_iommu *map_ioapic_to_ir(int apic)
362 for (i = 0; i < MAX_IO_APICS; i++)
363 if (ir_ioapic[i].id == apic)
364 return ir_ioapic[i].iommu;
368 struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
370 struct dmar_drhd_unit *drhd;
372 drhd = dmar_find_matched_drhd_unit(dev);
379 static int clear_entries(struct irq_2_iommu *irq_iommu)
381 struct irte *start, *entry, *end;
382 struct intel_iommu *iommu;
385 if (irq_iommu->sub_handle)
388 iommu = irq_iommu->iommu;
389 index = irq_iommu->irte_index + irq_iommu->sub_handle;
391 start = iommu->ir_table->base + index;
392 end = start + (1 << irq_iommu->irte_mask);
394 for (entry = start; entry < end; entry++) {
395 set_64bit((unsigned long *)&entry->low, 0);
396 set_64bit((unsigned long *)&entry->high, 0);
399 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
402 int free_irte(int irq)
405 struct irq_2_iommu *irq_iommu;
408 spin_lock_irqsave(&irq_2_ir_lock, flags);
409 irq_iommu = valid_irq_2_iommu(irq);
411 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
415 rc = clear_entries(irq_iommu);
417 irq_iommu->iommu = NULL;
418 irq_iommu->irte_index = 0;
419 irq_iommu->sub_handle = 0;
420 irq_iommu->irte_mask = 0;
422 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
428 * source validation type
430 #define SVT_NO_VERIFY 0x0 /* no verification is required */
431 #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fiels */
432 #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
435 * source-id qualifier
437 #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
438 #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
439 * the third least significant bit
441 #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
442 * the second and third least significant bits
444 #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
445 * the least three significant bits
449 * set SVT, SQ and SID fields of irte to verify
450 * source ids of interrupt requests
452 static void set_irte_sid(struct irte *irte, unsigned int svt,
453 unsigned int sq, unsigned int sid)
460 int set_ioapic_sid(struct irte *irte, int apic)
468 for (i = 0; i < MAX_IO_APICS; i++) {
469 if (ir_ioapic[i].id == apic) {
470 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
476 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
480 set_irte_sid(irte, 1, 0, sid);
485 int set_hpet_sid(struct irte *irte, u8 id)
493 for (i = 0; i < MAX_HPET_TBS; i++) {
494 if (ir_hpet[i].id == id) {
495 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
501 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
506 * Should really use SQ_ALL_16. Some platforms are broken.
507 * While we figure out the right quirks for these broken platforms, use
508 * SQ_13_IGNORE_3 for now.
510 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
515 int set_msi_sid(struct irte *irte, struct pci_dev *dev)
517 struct pci_dev *bridge;
522 /* PCIe device or Root Complex integrated PCI device */
523 if (pci_is_pcie(dev) || !dev->bus->parent) {
524 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
525 (dev->bus->number << 8) | dev->devfn);
529 bridge = pci_find_upstream_pcie_bridge(dev);
531 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
532 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
533 (bridge->bus->number << 8) | dev->bus->number);
534 else /* this is a legacy PCI bridge */
535 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
536 (bridge->bus->number << 8) | bridge->devfn);
542 static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
548 addr = virt_to_phys((void *)iommu->ir_table->base);
550 spin_lock_irqsave(&iommu->register_lock, flags);
552 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
553 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
555 /* Set interrupt-remapping table pointer */
556 iommu->gcmd |= DMA_GCMD_SIRTP;
557 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
559 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
560 readl, (sts & DMA_GSTS_IRTPS), sts);
561 spin_unlock_irqrestore(&iommu->register_lock, flags);
564 * global invalidation of interrupt entry cache before enabling
565 * interrupt-remapping.
567 qi_global_iec(iommu);
569 spin_lock_irqsave(&iommu->register_lock, flags);
571 /* Enable interrupt-remapping */
572 iommu->gcmd |= DMA_GCMD_IRE;
573 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
575 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
576 readl, (sts & DMA_GSTS_IRES), sts);
578 spin_unlock_irqrestore(&iommu->register_lock, flags);
582 static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
584 struct ir_table *ir_table;
587 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
590 if (!iommu->ir_table)
593 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
594 INTR_REMAP_PAGE_ORDER);
597 printk(KERN_ERR "failed to allocate pages of order %d\n",
598 INTR_REMAP_PAGE_ORDER);
599 kfree(iommu->ir_table);
603 ir_table->base = page_address(pages);
605 iommu_set_intr_remapping(iommu, mode);
610 * Disable Interrupt Remapping.
612 static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
617 if (!ecap_ir_support(iommu->ecap))
621 * global invalidation of interrupt entry cache before disabling
622 * interrupt-remapping.
624 qi_global_iec(iommu);
626 spin_lock_irqsave(&iommu->register_lock, flags);
628 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
629 if (!(sts & DMA_GSTS_IRES))
632 iommu->gcmd &= ~DMA_GCMD_IRE;
633 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
635 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
636 readl, !(sts & DMA_GSTS_IRES), sts);
639 spin_unlock_irqrestore(&iommu->register_lock, flags);
642 int __init intr_remapping_supported(void)
644 struct dmar_drhd_unit *drhd;
646 if (disable_intremap)
649 if (!dmar_ir_support())
652 for_each_drhd_unit(drhd) {
653 struct intel_iommu *iommu = drhd->iommu;
655 if (!ecap_ir_support(iommu->ecap))
662 int __init enable_intr_remapping(int eim)
664 struct dmar_drhd_unit *drhd;
667 if (parse_ioapics_under_ir() != 1) {
668 printk(KERN_INFO "Not enable interrupt remapping\n");
672 for_each_drhd_unit(drhd) {
673 struct intel_iommu *iommu = drhd->iommu;
676 * If the queued invalidation is already initialized,
677 * shouldn't disable it.
683 * Clear previous faults.
685 dmar_fault(-1, iommu);
688 * Disable intr remapping and queued invalidation, if already
689 * enabled prior to OS handover.
691 iommu_disable_intr_remapping(iommu);
693 dmar_disable_qi(iommu);
697 * check for the Interrupt-remapping support
699 for_each_drhd_unit(drhd) {
700 struct intel_iommu *iommu = drhd->iommu;
702 if (!ecap_ir_support(iommu->ecap))
705 if (eim && !ecap_eim_support(iommu->ecap)) {
706 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
707 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
713 * Enable queued invalidation for all the DRHD's.
715 for_each_drhd_unit(drhd) {
717 struct intel_iommu *iommu = drhd->iommu;
718 ret = dmar_enable_qi(iommu);
721 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
722 " invalidation, ecap %Lx, ret %d\n",
723 drhd->reg_base_addr, iommu->ecap, ret);
729 * Setup Interrupt-remapping for all the DRHD's now.
731 for_each_drhd_unit(drhd) {
732 struct intel_iommu *iommu = drhd->iommu;
734 if (!ecap_ir_support(iommu->ecap))
737 if (setup_intr_remapping(iommu, eim))
746 intr_remapping_enabled = 1;
752 * handle error condition gracefully here!
757 static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
758 struct intel_iommu *iommu)
760 struct acpi_dmar_pci_path *path;
765 path = (struct acpi_dmar_pci_path *)(scope + 1);
766 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
767 / sizeof(struct acpi_dmar_pci_path);
769 while (--count > 0) {
771 * Access PCI directly due to the PCI
772 * subsystem isn't initialized yet.
774 bus = read_pci_config_byte(bus, path->dev, path->fn,
778 ir_hpet[ir_hpet_num].bus = bus;
779 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
780 ir_hpet[ir_hpet_num].iommu = iommu;
781 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
785 static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
786 struct intel_iommu *iommu)
788 struct acpi_dmar_pci_path *path;
793 path = (struct acpi_dmar_pci_path *)(scope + 1);
794 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
795 / sizeof(struct acpi_dmar_pci_path);
797 while (--count > 0) {
799 * Access PCI directly due to the PCI
800 * subsystem isn't initialized yet.
802 bus = read_pci_config_byte(bus, path->dev, path->fn,
807 ir_ioapic[ir_ioapic_num].bus = bus;
808 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
809 ir_ioapic[ir_ioapic_num].iommu = iommu;
810 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
814 static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
815 struct intel_iommu *iommu)
817 struct acpi_dmar_hardware_unit *drhd;
818 struct acpi_dmar_device_scope *scope;
821 drhd = (struct acpi_dmar_hardware_unit *)header;
823 start = (void *)(drhd + 1);
824 end = ((void *)drhd) + header->length;
826 while (start < end) {
828 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
829 if (ir_ioapic_num == MAX_IO_APICS) {
830 printk(KERN_WARNING "Exceeded Max IO APICS\n");
834 printk(KERN_INFO "IOAPIC id %d under DRHD base"
835 " 0x%Lx\n", scope->enumeration_id,
838 ir_parse_one_ioapic_scope(scope, iommu);
839 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
840 if (ir_hpet_num == MAX_HPET_TBS) {
841 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
845 printk(KERN_INFO "HPET id %d under DRHD base"
846 " 0x%Lx\n", scope->enumeration_id,
849 ir_parse_one_hpet_scope(scope, iommu);
851 start += scope->length;
858 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
861 int __init parse_ioapics_under_ir(void)
863 struct dmar_drhd_unit *drhd;
864 int ir_supported = 0;
866 for_each_drhd_unit(drhd) {
867 struct intel_iommu *iommu = drhd->iommu;
869 if (ecap_ir_support(iommu->ecap)) {
870 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
877 if (ir_supported && ir_ioapic_num != nr_ioapics) {
879 "Not all IO-APIC's listed under remapping hardware\n");
886 void disable_intr_remapping(void)
888 struct dmar_drhd_unit *drhd;
889 struct intel_iommu *iommu = NULL;
892 * Disable Interrupt-remapping for all the DRHD's now.
894 for_each_iommu(iommu, drhd) {
895 if (!ecap_ir_support(iommu->ecap))
898 iommu_disable_intr_remapping(iommu);
902 int reenable_intr_remapping(int eim)
904 struct dmar_drhd_unit *drhd;
906 struct intel_iommu *iommu = NULL;
908 for_each_iommu(iommu, drhd)
910 dmar_reenable_qi(iommu);
913 * Setup Interrupt-remapping for all the DRHD's now.
915 for_each_iommu(iommu, drhd) {
916 if (!ecap_ir_support(iommu->ecap))
919 /* Set up interrupt remapping for iommu.*/
920 iommu_set_intr_remapping(iommu, eim);
931 * handle error condition gracefully here!