1 #include <linux/interrupt.h>
2 #include <linux/dmar.h>
3 #include <linux/spinlock.h>
4 #include <linux/slab.h>
5 #include <linux/jiffies.h>
6 #include <linux/hpet.h>
9 #include <asm/io_apic.h>
12 #include <linux/intel-iommu.h>
13 #include "intr_remapping.h"
14 #include <acpi/acpi.h>
15 #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 int disable_sourceid_checking;
25 static __init int setup_nointremap(char *str)
30 early_param("nointremap", setup_nointremap);
32 static __init int setup_intremap(char *str)
37 if (!strncmp(str, "on", 2))
39 else if (!strncmp(str, "off", 3))
41 else if (!strncmp(str, "nosid", 5))
42 disable_sourceid_checking = 1;
46 early_param("intremap", setup_intremap);
48 static DEFINE_SPINLOCK(irq_2_ir_lock);
50 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
52 struct irq_cfg *cfg = irq_get_chip_data(irq);
53 return cfg ? &cfg->irq_2_iommu : NULL;
56 int get_irte(int irq, struct irte *entry)
58 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
62 if (!entry || !irq_iommu)
65 spin_lock_irqsave(&irq_2_ir_lock, flags);
67 index = irq_iommu->irte_index + irq_iommu->sub_handle;
68 *entry = *(irq_iommu->iommu->ir_table->base + index);
70 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
74 int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
76 struct ir_table *table = iommu->ir_table;
77 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
78 u16 index, start_index;
79 unsigned int mask = 0;
83 if (!count || !irq_iommu)
87 * start the IRTE search from index 0.
89 index = start_index = 0;
92 count = __roundup_pow_of_two(count);
96 if (mask > ecap_max_handle_mask(iommu->ecap)) {
98 "Requested mask %x exceeds the max invalidation handle"
99 " mask value %Lx\n", mask,
100 ecap_max_handle_mask(iommu->ecap));
104 spin_lock_irqsave(&irq_2_ir_lock, flags);
106 for (i = index; i < index + count; i++)
107 if (table->base[i].present)
109 /* empty index found */
110 if (i == index + count)
113 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
115 if (index == start_index) {
116 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
117 printk(KERN_ERR "can't allocate an IRTE\n");
122 for (i = index; i < index + count; i++)
123 table->base[i].present = 1;
125 irq_iommu->iommu = iommu;
126 irq_iommu->irte_index = index;
127 irq_iommu->sub_handle = 0;
128 irq_iommu->irte_mask = mask;
130 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
135 static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
139 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
143 return qi_submit_sync(&desc, iommu);
146 int map_irq_to_irte_handle(int irq, u16 *sub_handle)
148 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
155 spin_lock_irqsave(&irq_2_ir_lock, flags);
156 *sub_handle = irq_iommu->sub_handle;
157 index = irq_iommu->irte_index;
158 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
162 int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
164 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
170 spin_lock_irqsave(&irq_2_ir_lock, flags);
172 irq_iommu->iommu = iommu;
173 irq_iommu->irte_index = index;
174 irq_iommu->sub_handle = subhandle;
175 irq_iommu->irte_mask = 0;
177 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
182 int modify_irte(int irq, struct irte *irte_modified)
184 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
185 struct intel_iommu *iommu;
193 spin_lock_irqsave(&irq_2_ir_lock, flags);
195 iommu = irq_iommu->iommu;
197 index = irq_iommu->irte_index + irq_iommu->sub_handle;
198 irte = &iommu->ir_table->base[index];
200 set_64bit(&irte->low, irte_modified->low);
201 set_64bit(&irte->high, irte_modified->high);
202 __iommu_flush_cache(iommu, irte, sizeof(*irte));
204 rc = qi_flush_iec(iommu, index, 0);
205 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
210 struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
214 for (i = 0; i < MAX_HPET_TBS; i++)
215 if (ir_hpet[i].id == hpet_id)
216 return ir_hpet[i].iommu;
220 struct intel_iommu *map_ioapic_to_ir(int apic)
224 for (i = 0; i < MAX_IO_APICS; i++)
225 if (ir_ioapic[i].id == apic)
226 return ir_ioapic[i].iommu;
230 struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
232 struct dmar_drhd_unit *drhd;
234 drhd = dmar_find_matched_drhd_unit(dev);
241 static int clear_entries(struct irq_2_iommu *irq_iommu)
243 struct irte *start, *entry, *end;
244 struct intel_iommu *iommu;
247 if (irq_iommu->sub_handle)
250 iommu = irq_iommu->iommu;
251 index = irq_iommu->irte_index + irq_iommu->sub_handle;
253 start = iommu->ir_table->base + index;
254 end = start + (1 << irq_iommu->irte_mask);
256 for (entry = start; entry < end; entry++) {
257 set_64bit(&entry->low, 0);
258 set_64bit(&entry->high, 0);
261 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
264 int free_irte(int irq)
266 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
273 spin_lock_irqsave(&irq_2_ir_lock, flags);
275 rc = clear_entries(irq_iommu);
277 irq_iommu->iommu = NULL;
278 irq_iommu->irte_index = 0;
279 irq_iommu->sub_handle = 0;
280 irq_iommu->irte_mask = 0;
282 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
288 * source validation type
290 #define SVT_NO_VERIFY 0x0 /* no verification is required */
291 #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
292 #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
295 * source-id qualifier
297 #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
298 #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
299 * the third least significant bit
301 #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
302 * the second and third least significant bits
304 #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
305 * the least three significant bits
309 * set SVT, SQ and SID fields of irte to verify
310 * source ids of interrupt requests
312 static void set_irte_sid(struct irte *irte, unsigned int svt,
313 unsigned int sq, unsigned int sid)
315 if (disable_sourceid_checking)
322 int set_ioapic_sid(struct irte *irte, int apic)
330 for (i = 0; i < MAX_IO_APICS; i++) {
331 if (ir_ioapic[i].id == apic) {
332 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
338 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
342 set_irte_sid(irte, 1, 0, sid);
347 int set_hpet_sid(struct irte *irte, u8 id)
355 for (i = 0; i < MAX_HPET_TBS; i++) {
356 if (ir_hpet[i].id == id) {
357 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
363 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
368 * Should really use SQ_ALL_16. Some platforms are broken.
369 * While we figure out the right quirks for these broken platforms, use
370 * SQ_13_IGNORE_3 for now.
372 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
377 int set_msi_sid(struct irte *irte, struct pci_dev *dev)
379 struct pci_dev *bridge;
384 /* PCIe device or Root Complex integrated PCI device */
385 if (pci_is_pcie(dev) || !dev->bus->parent) {
386 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
387 (dev->bus->number << 8) | dev->devfn);
391 bridge = pci_find_upstream_pcie_bridge(dev);
393 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
394 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
395 (bridge->bus->number << 8) | dev->bus->number);
396 else /* this is a legacy PCI bridge */
397 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
398 (bridge->bus->number << 8) | bridge->devfn);
404 static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
410 addr = virt_to_phys((void *)iommu->ir_table->base);
412 spin_lock_irqsave(&iommu->register_lock, flags);
414 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
415 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
417 /* Set interrupt-remapping table pointer */
418 iommu->gcmd |= DMA_GCMD_SIRTP;
419 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
421 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
422 readl, (sts & DMA_GSTS_IRTPS), sts);
423 spin_unlock_irqrestore(&iommu->register_lock, flags);
426 * global invalidation of interrupt entry cache before enabling
427 * interrupt-remapping.
429 qi_global_iec(iommu);
431 spin_lock_irqsave(&iommu->register_lock, flags);
433 /* Enable interrupt-remapping */
434 iommu->gcmd |= DMA_GCMD_IRE;
435 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
437 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
438 readl, (sts & DMA_GSTS_IRES), sts);
440 spin_unlock_irqrestore(&iommu->register_lock, flags);
444 static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
446 struct ir_table *ir_table;
449 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
452 if (!iommu->ir_table)
455 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
456 INTR_REMAP_PAGE_ORDER);
459 printk(KERN_ERR "failed to allocate pages of order %d\n",
460 INTR_REMAP_PAGE_ORDER);
461 kfree(iommu->ir_table);
465 ir_table->base = page_address(pages);
467 iommu_set_intr_remapping(iommu, mode);
472 * Disable Interrupt Remapping.
474 static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
479 if (!ecap_ir_support(iommu->ecap))
483 * global invalidation of interrupt entry cache before disabling
484 * interrupt-remapping.
486 qi_global_iec(iommu);
488 spin_lock_irqsave(&iommu->register_lock, flags);
490 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
491 if (!(sts & DMA_GSTS_IRES))
494 iommu->gcmd &= ~DMA_GCMD_IRE;
495 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
497 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
498 readl, !(sts & DMA_GSTS_IRES), sts);
501 spin_unlock_irqrestore(&iommu->register_lock, flags);
504 int __init intr_remapping_supported(void)
506 struct dmar_drhd_unit *drhd;
508 if (disable_intremap)
511 if (!dmar_ir_support())
514 for_each_drhd_unit(drhd) {
515 struct intel_iommu *iommu = drhd->iommu;
517 if (!ecap_ir_support(iommu->ecap))
524 int __init enable_intr_remapping(int eim)
526 struct dmar_drhd_unit *drhd;
529 if (parse_ioapics_under_ir() != 1) {
530 printk(KERN_INFO "Not enable interrupt remapping\n");
534 for_each_drhd_unit(drhd) {
535 struct intel_iommu *iommu = drhd->iommu;
538 * If the queued invalidation is already initialized,
539 * shouldn't disable it.
545 * Clear previous faults.
547 dmar_fault(-1, iommu);
550 * Disable intr remapping and queued invalidation, if already
551 * enabled prior to OS handover.
553 iommu_disable_intr_remapping(iommu);
555 dmar_disable_qi(iommu);
559 * check for the Interrupt-remapping support
561 for_each_drhd_unit(drhd) {
562 struct intel_iommu *iommu = drhd->iommu;
564 if (!ecap_ir_support(iommu->ecap))
567 if (eim && !ecap_eim_support(iommu->ecap)) {
568 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
569 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
575 * Enable queued invalidation for all the DRHD's.
577 for_each_drhd_unit(drhd) {
579 struct intel_iommu *iommu = drhd->iommu;
580 ret = dmar_enable_qi(iommu);
583 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
584 " invalidation, ecap %Lx, ret %d\n",
585 drhd->reg_base_addr, iommu->ecap, ret);
591 * Setup Interrupt-remapping for all the DRHD's now.
593 for_each_drhd_unit(drhd) {
594 struct intel_iommu *iommu = drhd->iommu;
596 if (!ecap_ir_support(iommu->ecap))
599 if (setup_intr_remapping(iommu, eim))
608 intr_remapping_enabled = 1;
614 * handle error condition gracefully here!
619 static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
620 struct intel_iommu *iommu)
622 struct acpi_dmar_pci_path *path;
627 path = (struct acpi_dmar_pci_path *)(scope + 1);
628 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
629 / sizeof(struct acpi_dmar_pci_path);
631 while (--count > 0) {
633 * Access PCI directly due to the PCI
634 * subsystem isn't initialized yet.
636 bus = read_pci_config_byte(bus, path->dev, path->fn,
640 ir_hpet[ir_hpet_num].bus = bus;
641 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
642 ir_hpet[ir_hpet_num].iommu = iommu;
643 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
647 static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
648 struct intel_iommu *iommu)
650 struct acpi_dmar_pci_path *path;
655 path = (struct acpi_dmar_pci_path *)(scope + 1);
656 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
657 / sizeof(struct acpi_dmar_pci_path);
659 while (--count > 0) {
661 * Access PCI directly due to the PCI
662 * subsystem isn't initialized yet.
664 bus = read_pci_config_byte(bus, path->dev, path->fn,
669 ir_ioapic[ir_ioapic_num].bus = bus;
670 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
671 ir_ioapic[ir_ioapic_num].iommu = iommu;
672 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
676 static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
677 struct intel_iommu *iommu)
679 struct acpi_dmar_hardware_unit *drhd;
680 struct acpi_dmar_device_scope *scope;
683 drhd = (struct acpi_dmar_hardware_unit *)header;
685 start = (void *)(drhd + 1);
686 end = ((void *)drhd) + header->length;
688 while (start < end) {
690 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
691 if (ir_ioapic_num == MAX_IO_APICS) {
692 printk(KERN_WARNING "Exceeded Max IO APICS\n");
696 printk(KERN_INFO "IOAPIC id %d under DRHD base "
697 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
698 drhd->address, iommu->seq_id);
700 ir_parse_one_ioapic_scope(scope, iommu);
701 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
702 if (ir_hpet_num == MAX_HPET_TBS) {
703 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
707 printk(KERN_INFO "HPET id %d under DRHD base"
708 " 0x%Lx\n", scope->enumeration_id,
711 ir_parse_one_hpet_scope(scope, iommu);
713 start += scope->length;
720 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
723 int __init parse_ioapics_under_ir(void)
725 struct dmar_drhd_unit *drhd;
726 int ir_supported = 0;
728 for_each_drhd_unit(drhd) {
729 struct intel_iommu *iommu = drhd->iommu;
731 if (ecap_ir_support(iommu->ecap)) {
732 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
739 if (ir_supported && ir_ioapic_num != nr_ioapics) {
741 "Not all IO-APIC's listed under remapping hardware\n");
748 void disable_intr_remapping(void)
750 struct dmar_drhd_unit *drhd;
751 struct intel_iommu *iommu = NULL;
754 * Disable Interrupt-remapping for all the DRHD's now.
756 for_each_iommu(iommu, drhd) {
757 if (!ecap_ir_support(iommu->ecap))
760 iommu_disable_intr_remapping(iommu);
764 int reenable_intr_remapping(int eim)
766 struct dmar_drhd_unit *drhd;
768 struct intel_iommu *iommu = NULL;
770 for_each_iommu(iommu, drhd)
772 dmar_reenable_qi(iommu);
775 * Setup Interrupt-remapping for all the DRHD's now.
777 for_each_iommu(iommu, drhd) {
778 if (!ecap_ir_support(iommu->ecap))
781 /* Set up interrupt remapping for iommu.*/
782 iommu_set_intr_remapping(iommu, eim);
793 * handle error condition gracefully here!