1 // SPDX-License-Identifier: GPL-2.0
3 #define pr_fmt(fmt) "DMAR-IR: " fmt
5 #include <linux/interrupt.h>
6 #include <linux/dmar.h>
7 #include <linux/spinlock.h>
8 #include <linux/slab.h>
9 #include <linux/jiffies.h>
10 #include <linux/hpet.h>
11 #include <linux/pci.h>
12 #include <linux/irq.h>
13 #include <linux/acpi.h>
14 #include <linux/irqdomain.h>
15 #include <linux/crash_dump.h>
16 #include <asm/io_apic.h>
20 #include <asm/irq_remapping.h>
21 #include <asm/pci-direct.h>
22 #include <asm/posted_intr.h>
25 #include "../irq_remapping.h"
26 #include "../iommu-pages.h"
27 #include "cap_audit.h"
35 struct intel_iommu *iommu;
37 unsigned int bus; /* PCI bus number */
38 unsigned int devfn; /* PCI devfn number */
42 struct intel_iommu *iommu;
49 struct intel_iommu *iommu;
57 struct intel_ir_data {
58 struct irq_2_iommu irq_2_iommu;
59 struct irte irte_entry;
61 struct msi_msg msi_entry;
65 #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
66 #define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
68 static int __read_mostly eim_mode;
69 static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
70 static struct hpet_scope ir_hpet[MAX_HPET_TBS];
77 * ->iommu->register_lock
79 * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
80 * in single-threaded environment with interrupt disabled, so no need to tabke
81 * the dmar_global_lock.
83 DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
84 static const struct irq_domain_ops intel_ir_domain_ops;
86 static void iommu_disable_irq_remapping(struct intel_iommu *iommu);
87 static int __init parse_ioapics_under_ir(void);
88 static const struct msi_parent_ops dmar_msi_parent_ops;
90 static bool ir_pre_enabled(struct intel_iommu *iommu)
92 return (iommu->flags & VTD_FLAG_IRQ_REMAP_PRE_ENABLED);
95 static void clear_ir_pre_enabled(struct intel_iommu *iommu)
97 iommu->flags &= ~VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
100 static void init_ir_status(struct intel_iommu *iommu)
104 gsts = readl(iommu->reg + DMAR_GSTS_REG);
105 if (gsts & DMA_GSTS_IRES)
106 iommu->flags |= VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
109 static int alloc_irte(struct intel_iommu *iommu,
110 struct irq_2_iommu *irq_iommu, u16 count)
112 struct ir_table *table = iommu->ir_table;
113 unsigned int mask = 0;
117 if (!count || !irq_iommu)
121 count = __roundup_pow_of_two(count);
125 if (mask > ecap_max_handle_mask(iommu->ecap)) {
126 pr_err("Requested mask %x exceeds the max invalidation handle"
127 " mask value %Lx\n", mask,
128 ecap_max_handle_mask(iommu->ecap));
132 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
133 index = bitmap_find_free_region(table->bitmap,
134 INTR_REMAP_TABLE_ENTRIES, mask);
136 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
138 irq_iommu->iommu = iommu;
139 irq_iommu->irte_index = index;
140 irq_iommu->sub_handle = 0;
141 irq_iommu->irte_mask = mask;
142 irq_iommu->mode = IRQ_REMAPPING;
144 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
149 static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
153 desc.qw0 = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
159 return qi_submit_sync(iommu, &desc, 1, 0);
162 static int modify_irte(struct irq_2_iommu *irq_iommu,
163 struct irte *irte_modified)
165 struct intel_iommu *iommu;
173 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
175 iommu = irq_iommu->iommu;
177 index = irq_iommu->irte_index + irq_iommu->sub_handle;
178 irte = &iommu->ir_table->base[index];
180 if ((irte->pst == 1) || (irte_modified->pst == 1)) {
182 * We use cmpxchg16 to atomically update the 128-bit IRTE,
183 * and it cannot be updated by the hardware or other processors
184 * behind us, so the return value of cmpxchg16 should be the
185 * same as the old value.
187 u128 old = irte->irte;
188 WARN_ON(!try_cmpxchg128(&irte->irte, &old, irte_modified->irte));
190 WRITE_ONCE(irte->low, irte_modified->low);
191 WRITE_ONCE(irte->high, irte_modified->high);
193 __iommu_flush_cache(iommu, irte, sizeof(*irte));
195 rc = qi_flush_iec(iommu, index, 0);
197 /* Update iommu mode according to the IRTE mode */
198 irq_iommu->mode = irte->pst ? IRQ_POSTING : IRQ_REMAPPING;
199 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
204 static struct intel_iommu *map_hpet_to_iommu(u8 hpet_id)
208 for (i = 0; i < MAX_HPET_TBS; i++) {
209 if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
210 return ir_hpet[i].iommu;
215 static struct intel_iommu *map_ioapic_to_iommu(int apic)
219 for (i = 0; i < MAX_IO_APICS; i++) {
220 if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
221 return ir_ioapic[i].iommu;
226 static struct irq_domain *map_dev_to_ir(struct pci_dev *dev)
228 struct dmar_drhd_unit *drhd = dmar_find_matched_drhd_unit(dev);
230 return drhd ? drhd->iommu->ir_domain : NULL;
233 static int clear_entries(struct irq_2_iommu *irq_iommu)
235 struct irte *start, *entry, *end;
236 struct intel_iommu *iommu;
239 if (irq_iommu->sub_handle)
242 iommu = irq_iommu->iommu;
243 index = irq_iommu->irte_index;
245 start = iommu->ir_table->base + index;
246 end = start + (1 << irq_iommu->irte_mask);
248 for (entry = start; entry < end; entry++) {
249 WRITE_ONCE(entry->low, 0);
250 WRITE_ONCE(entry->high, 0);
252 bitmap_release_region(iommu->ir_table->bitmap, index,
253 irq_iommu->irte_mask);
255 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
259 * source validation type
261 #define SVT_NO_VERIFY 0x0 /* no verification is required */
262 #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
263 #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
266 * source-id qualifier
268 #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
269 #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
270 * the third least significant bit
272 #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
273 * the second and third least significant bits
275 #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
276 * the least three significant bits
280 * set SVT, SQ and SID fields of irte to verify
281 * source ids of interrupt requests
283 static void set_irte_sid(struct irte *irte, unsigned int svt,
284 unsigned int sq, unsigned int sid)
286 if (disable_sourceid_checking)
294 * Set an IRTE to match only the bus number. Interrupt requests that reference
295 * this IRTE must have a requester-id whose bus number is between or equal
296 * to the start_bus and end_bus arguments.
298 static void set_irte_verify_bus(struct irte *irte, unsigned int start_bus,
299 unsigned int end_bus)
301 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
302 (start_bus << 8) | end_bus);
305 static int set_ioapic_sid(struct irte *irte, int apic)
313 for (i = 0; i < MAX_IO_APICS; i++) {
314 if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
315 sid = PCI_DEVID(ir_ioapic[i].bus, ir_ioapic[i].devfn);
321 pr_warn("Failed to set source-id of IOAPIC (%d)\n", apic);
325 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
330 static int set_hpet_sid(struct irte *irte, u8 id)
338 for (i = 0; i < MAX_HPET_TBS; i++) {
339 if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
340 sid = PCI_DEVID(ir_hpet[i].bus, ir_hpet[i].devfn);
346 pr_warn("Failed to set source-id of HPET block (%d)\n", id);
351 * Should really use SQ_ALL_16. Some platforms are broken.
352 * While we figure out the right quirks for these broken platforms, use
353 * SQ_13_IGNORE_3 for now.
355 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
360 struct set_msi_sid_data {
361 struct pci_dev *pdev;
367 static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
369 struct set_msi_sid_data *data = opaque;
371 if (data->count == 0 || PCI_BUS_NUM(alias) == PCI_BUS_NUM(data->alias))
372 data->busmatch_count++;
381 static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
383 struct set_msi_sid_data data;
389 data.busmatch_count = 0;
390 pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
393 * DMA alias provides us with a PCI device and alias. The only case
394 * where the it will return an alias on a different bus than the
395 * device is the case of a PCIe-to-PCI bridge, where the alias is for
396 * the subordinate bus. In this case we can only verify the bus.
398 * If there are multiple aliases, all with the same bus number,
399 * then all we can do is verify the bus. This is typical in NTB
400 * hardware which use proxy IDs where the device will generate traffic
401 * from multiple devfn numbers on the same bus.
403 * If the alias device is on a different bus than our source device
404 * then we have a topology based alias, use it.
406 * Otherwise, the alias is for a device DMA quirk and we cannot
407 * assume that MSI uses the same requester ID. Therefore use the
410 if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
411 set_irte_verify_bus(irte, PCI_BUS_NUM(data.alias),
413 else if (data.count >= 2 && data.busmatch_count == data.count)
414 set_irte_verify_bus(irte, dev->bus->number, dev->bus->number);
415 else if (data.pdev->bus->number != dev->bus->number)
416 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
418 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
424 static int iommu_load_old_irte(struct intel_iommu *iommu)
426 struct irte *old_ir_table;
427 phys_addr_t irt_phys;
432 /* Check whether the old ir-table has the same size as ours */
433 irta = dmar_readq(iommu->reg + DMAR_IRTA_REG);
434 if ((irta & INTR_REMAP_TABLE_REG_SIZE_MASK)
435 != INTR_REMAP_TABLE_REG_SIZE)
438 irt_phys = irta & VTD_PAGE_MASK;
439 size = INTR_REMAP_TABLE_ENTRIES*sizeof(struct irte);
441 /* Map the old IR table */
442 old_ir_table = memremap(irt_phys, size, MEMREMAP_WB);
447 memcpy(iommu->ir_table->base, old_ir_table, size);
449 __iommu_flush_cache(iommu, iommu->ir_table->base, size);
452 * Now check the table for used entries and mark those as
453 * allocated in the bitmap
455 for (i = 0; i < INTR_REMAP_TABLE_ENTRIES; i++) {
456 if (iommu->ir_table->base[i].present)
457 bitmap_set(iommu->ir_table->bitmap, i, 1);
460 memunmap(old_ir_table);
466 static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
472 addr = virt_to_phys((void *)iommu->ir_table->base);
474 raw_spin_lock_irqsave(&iommu->register_lock, flags);
476 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
477 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
479 /* Set interrupt-remapping table pointer */
480 writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
482 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
483 readl, (sts & DMA_GSTS_IRTPS), sts);
484 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
487 * Global invalidation of interrupt entry cache to make sure the
488 * hardware uses the new irq remapping table.
490 if (!cap_esirtps(iommu->cap))
491 qi_global_iec(iommu);
494 static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
499 raw_spin_lock_irqsave(&iommu->register_lock, flags);
501 /* Enable interrupt-remapping */
502 iommu->gcmd |= DMA_GCMD_IRE;
503 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
504 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
505 readl, (sts & DMA_GSTS_IRES), sts);
507 /* Block compatibility-format MSIs */
508 if (sts & DMA_GSTS_CFIS) {
509 iommu->gcmd &= ~DMA_GCMD_CFI;
510 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
511 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
512 readl, !(sts & DMA_GSTS_CFIS), sts);
516 * With CFI clear in the Global Command register, we should be
517 * protected from dangerous (i.e. compatibility) interrupts
518 * regardless of x2apic status. Check just to be sure.
520 if (sts & DMA_GSTS_CFIS)
522 "Compatibility-format IRQs enabled despite intr remapping;\n"
523 "you are vulnerable to IRQ injection.\n");
525 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
528 static int intel_setup_irq_remapping(struct intel_iommu *iommu)
530 struct ir_table *ir_table;
531 struct fwnode_handle *fn;
532 unsigned long *bitmap;
538 ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
542 ir_table_base = iommu_alloc_pages_node(iommu->node, GFP_KERNEL,
543 INTR_REMAP_PAGE_ORDER);
544 if (!ir_table_base) {
545 pr_err("IR%d: failed to allocate pages of order %d\n",
546 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
550 bitmap = bitmap_zalloc(INTR_REMAP_TABLE_ENTRIES, GFP_KERNEL);
551 if (bitmap == NULL) {
552 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
556 fn = irq_domain_alloc_named_id_fwnode("INTEL-IR", iommu->seq_id);
558 goto out_free_bitmap;
561 irq_domain_create_hierarchy(arch_get_ir_parent_domain(),
562 0, INTR_REMAP_TABLE_ENTRIES,
563 fn, &intel_ir_domain_ops,
565 if (!iommu->ir_domain) {
566 pr_err("IR%d: failed to allocate irqdomain\n", iommu->seq_id);
567 goto out_free_fwnode;
570 irq_domain_update_bus_token(iommu->ir_domain, DOMAIN_BUS_DMAR);
571 iommu->ir_domain->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT |
572 IRQ_DOMAIN_FLAG_ISOLATED_MSI;
573 iommu->ir_domain->msi_parent_ops = &dmar_msi_parent_ops;
575 ir_table->base = ir_table_base;
576 ir_table->bitmap = bitmap;
577 iommu->ir_table = ir_table;
580 * If the queued invalidation is already initialized,
581 * shouldn't disable it.
585 * Clear previous faults.
587 dmar_fault(-1, iommu);
588 dmar_disable_qi(iommu);
590 if (dmar_enable_qi(iommu)) {
591 pr_err("Failed to enable queued invalidation\n");
592 goto out_free_ir_domain;
596 init_ir_status(iommu);
598 if (ir_pre_enabled(iommu)) {
599 if (!is_kdump_kernel()) {
600 pr_info_once("IRQ remapping was enabled on %s but we are not in kdump mode\n",
602 clear_ir_pre_enabled(iommu);
603 iommu_disable_irq_remapping(iommu);
604 } else if (iommu_load_old_irte(iommu))
605 pr_err("Failed to copy IR table for %s from previous kernel\n",
608 pr_info("Copied IR table for %s from previous kernel\n",
612 iommu_set_irq_remapping(iommu, eim_mode);
617 irq_domain_remove(iommu->ir_domain);
618 iommu->ir_domain = NULL;
620 irq_domain_free_fwnode(fn);
624 iommu_free_pages(ir_table_base, INTR_REMAP_PAGE_ORDER);
628 iommu->ir_table = NULL;
633 static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
635 struct fwnode_handle *fn;
637 if (iommu && iommu->ir_table) {
638 if (iommu->ir_domain) {
639 fn = iommu->ir_domain->fwnode;
641 irq_domain_remove(iommu->ir_domain);
642 irq_domain_free_fwnode(fn);
643 iommu->ir_domain = NULL;
645 iommu_free_pages(iommu->ir_table->base, INTR_REMAP_PAGE_ORDER);
646 bitmap_free(iommu->ir_table->bitmap);
647 kfree(iommu->ir_table);
648 iommu->ir_table = NULL;
653 * Disable Interrupt Remapping.
655 static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
660 if (!ecap_ir_support(iommu->ecap))
664 * global invalidation of interrupt entry cache before disabling
665 * interrupt-remapping.
667 if (!cap_esirtps(iommu->cap))
668 qi_global_iec(iommu);
670 raw_spin_lock_irqsave(&iommu->register_lock, flags);
672 sts = readl(iommu->reg + DMAR_GSTS_REG);
673 if (!(sts & DMA_GSTS_IRES))
676 iommu->gcmd &= ~DMA_GCMD_IRE;
677 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
679 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
680 readl, !(sts & DMA_GSTS_IRES), sts);
683 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
686 static int __init dmar_x2apic_optout(void)
688 struct acpi_table_dmar *dmar;
689 dmar = (struct acpi_table_dmar *)dmar_tbl;
690 if (!dmar || no_x2apic_optout)
692 return dmar->flags & DMAR_X2APIC_OPT_OUT;
695 static void __init intel_cleanup_irq_remapping(void)
697 struct dmar_drhd_unit *drhd;
698 struct intel_iommu *iommu;
700 for_each_iommu(iommu, drhd) {
701 if (ecap_ir_support(iommu->ecap)) {
702 iommu_disable_irq_remapping(iommu);
703 intel_teardown_irq_remapping(iommu);
707 if (x2apic_supported())
708 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
711 static int __init intel_prepare_irq_remapping(void)
713 struct dmar_drhd_unit *drhd;
714 struct intel_iommu *iommu;
717 if (irq_remap_broken) {
718 pr_warn("This system BIOS has enabled interrupt remapping\n"
719 "on a chipset that contains an erratum making that\n"
720 "feature unstable. To maintain system stability\n"
721 "interrupt remapping is being disabled. Please\n"
722 "contact your BIOS vendor for an update\n");
723 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
727 if (dmar_table_init() < 0)
730 if (intel_cap_audit(CAP_AUDIT_STATIC_IRQR, NULL))
733 if (!dmar_ir_support())
736 if (parse_ioapics_under_ir()) {
737 pr_info("Not enabling interrupt remapping\n");
741 /* First make sure all IOMMUs support IRQ remapping */
742 for_each_iommu(iommu, drhd)
743 if (!ecap_ir_support(iommu->ecap))
746 /* Detect remapping mode: lapic or x2apic */
747 if (x2apic_supported()) {
748 eim = !dmar_x2apic_optout();
750 pr_info("x2apic is disabled because BIOS sets x2apic opt out bit.");
751 pr_info("Use 'intremap=no_x2apic_optout' to override the BIOS setting.\n");
755 for_each_iommu(iommu, drhd) {
756 if (eim && !ecap_eim_support(iommu->ecap)) {
757 pr_info("%s does not support EIM\n", iommu->name);
764 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
766 /* Do the initializations early */
767 for_each_iommu(iommu, drhd) {
768 if (intel_setup_irq_remapping(iommu)) {
769 pr_err("Failed to setup irq remapping for %s\n",
778 intel_cleanup_irq_remapping();
783 * Set Posted-Interrupts capability.
785 static inline void set_irq_posting_cap(void)
787 struct dmar_drhd_unit *drhd;
788 struct intel_iommu *iommu;
790 if (!disable_irq_post) {
792 * If IRTE is in posted format, the 'pda' field goes across the
793 * 64-bit boundary, we need use cmpxchg16b to atomically update
794 * it. We only expose posted-interrupt when X86_FEATURE_CX16
795 * is supported. Actually, hardware platforms supporting PI
796 * should have X86_FEATURE_CX16 support, this has been confirmed
797 * with Intel hardware guys.
799 if (boot_cpu_has(X86_FEATURE_CX16))
800 intel_irq_remap_ops.capability |= 1 << IRQ_POSTING_CAP;
802 for_each_iommu(iommu, drhd)
803 if (!cap_pi_support(iommu->cap)) {
804 intel_irq_remap_ops.capability &=
805 ~(1 << IRQ_POSTING_CAP);
811 static int __init intel_enable_irq_remapping(void)
813 struct dmar_drhd_unit *drhd;
814 struct intel_iommu *iommu;
818 * Setup Interrupt-remapping for all the DRHD's now.
820 for_each_iommu(iommu, drhd) {
821 if (!ir_pre_enabled(iommu))
822 iommu_enable_irq_remapping(iommu);
829 irq_remapping_enabled = 1;
831 set_irq_posting_cap();
833 pr_info("Enabled IRQ remapping in %s mode\n", eim_mode ? "x2apic" : "xapic");
835 return eim_mode ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
838 intel_cleanup_irq_remapping();
842 static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
843 struct intel_iommu *iommu,
844 struct acpi_dmar_hardware_unit *drhd)
846 struct acpi_dmar_pci_path *path;
848 int count, free = -1;
851 path = (struct acpi_dmar_pci_path *)(scope + 1);
852 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
853 / sizeof(struct acpi_dmar_pci_path);
855 while (--count > 0) {
857 * Access PCI directly due to the PCI
858 * subsystem isn't initialized yet.
860 bus = read_pci_config_byte(bus, path->device, path->function,
865 for (count = 0; count < MAX_HPET_TBS; count++) {
866 if (ir_hpet[count].iommu == iommu &&
867 ir_hpet[count].id == scope->enumeration_id)
869 else if (ir_hpet[count].iommu == NULL && free == -1)
873 pr_warn("Exceeded Max HPET blocks\n");
877 ir_hpet[free].iommu = iommu;
878 ir_hpet[free].id = scope->enumeration_id;
879 ir_hpet[free].bus = bus;
880 ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
881 pr_info("HPET id %d under DRHD base 0x%Lx\n",
882 scope->enumeration_id, drhd->address);
887 static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
888 struct intel_iommu *iommu,
889 struct acpi_dmar_hardware_unit *drhd)
891 struct acpi_dmar_pci_path *path;
893 int count, free = -1;
896 path = (struct acpi_dmar_pci_path *)(scope + 1);
897 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
898 / sizeof(struct acpi_dmar_pci_path);
900 while (--count > 0) {
902 * Access PCI directly due to the PCI
903 * subsystem isn't initialized yet.
905 bus = read_pci_config_byte(bus, path->device, path->function,
910 for (count = 0; count < MAX_IO_APICS; count++) {
911 if (ir_ioapic[count].iommu == iommu &&
912 ir_ioapic[count].id == scope->enumeration_id)
914 else if (ir_ioapic[count].iommu == NULL && free == -1)
918 pr_warn("Exceeded Max IO APICS\n");
922 ir_ioapic[free].bus = bus;
923 ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
924 ir_ioapic[free].iommu = iommu;
925 ir_ioapic[free].id = scope->enumeration_id;
926 pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
927 scope->enumeration_id, drhd->address, iommu->seq_id);
932 static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
933 struct intel_iommu *iommu)
936 struct acpi_dmar_hardware_unit *drhd;
937 struct acpi_dmar_device_scope *scope;
940 drhd = (struct acpi_dmar_hardware_unit *)header;
941 start = (void *)(drhd + 1);
942 end = ((void *)drhd) + header->length;
944 while (start < end && ret == 0) {
946 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
947 ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
948 else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
949 ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
950 start += scope->length;
956 static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
960 for (i = 0; i < MAX_HPET_TBS; i++)
961 if (ir_hpet[i].iommu == iommu)
962 ir_hpet[i].iommu = NULL;
964 for (i = 0; i < MAX_IO_APICS; i++)
965 if (ir_ioapic[i].iommu == iommu)
966 ir_ioapic[i].iommu = NULL;
970 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
973 static int __init parse_ioapics_under_ir(void)
975 struct dmar_drhd_unit *drhd;
976 struct intel_iommu *iommu;
977 bool ir_supported = false;
980 for_each_iommu(iommu, drhd) {
983 if (!ecap_ir_support(iommu->ecap))
986 ret = ir_parse_ioapic_hpet_scope(drhd->hdr, iommu);
996 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
997 int ioapic_id = mpc_ioapic_id(ioapic_idx);
998 if (!map_ioapic_to_iommu(ioapic_id)) {
999 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
1000 "interrupt remapping will be disabled\n",
1009 static int __init ir_dev_scope_init(void)
1013 if (!irq_remapping_enabled)
1016 down_write(&dmar_global_lock);
1017 ret = dmar_dev_scope_init();
1018 up_write(&dmar_global_lock);
1022 rootfs_initcall(ir_dev_scope_init);
1024 static void disable_irq_remapping(void)
1026 struct dmar_drhd_unit *drhd;
1027 struct intel_iommu *iommu = NULL;
1030 * Disable Interrupt-remapping for all the DRHD's now.
1032 for_each_iommu(iommu, drhd) {
1033 if (!ecap_ir_support(iommu->ecap))
1036 iommu_disable_irq_remapping(iommu);
1040 * Clear Posted-Interrupts capability.
1042 if (!disable_irq_post)
1043 intel_irq_remap_ops.capability &= ~(1 << IRQ_POSTING_CAP);
1046 static int reenable_irq_remapping(int eim)
1048 struct dmar_drhd_unit *drhd;
1050 struct intel_iommu *iommu = NULL;
1052 for_each_iommu(iommu, drhd)
1054 dmar_reenable_qi(iommu);
1057 * Setup Interrupt-remapping for all the DRHD's now.
1059 for_each_iommu(iommu, drhd) {
1060 if (!ecap_ir_support(iommu->ecap))
1063 /* Set up interrupt remapping for iommu.*/
1064 iommu_set_irq_remapping(iommu, eim);
1065 iommu_enable_irq_remapping(iommu);
1072 set_irq_posting_cap();
1078 * handle error condition gracefully here!
1084 * Store the MSI remapping domain pointer in the device if enabled.
1086 * This is called from dmar_pci_bus_add_dev() so it works even when DMA
1087 * remapping is disabled. Only update the pointer if the device is not
1088 * already handled by a non default PCI/MSI interrupt domain. This protects
1091 void intel_irq_remap_add_device(struct dmar_pci_notify_info *info)
1093 if (!irq_remapping_enabled || !pci_dev_has_default_msi_parent_domain(info->dev))
1096 dev_set_msi_domain(&info->dev->dev, map_dev_to_ir(info->dev));
1099 static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
1101 memset(irte, 0, sizeof(*irte));
1104 irte->dst_mode = apic->dest_mode_logical;
1106 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
1107 * actual level or edge trigger will be setup in the IO-APIC
1108 * RTE. This will help simplify level triggered irq migration.
1109 * For more details, see the comments (in io_apic.c) explainig IO-APIC
1110 * irq migration in the presence of interrupt-remapping.
1112 irte->trigger_mode = 0;
1113 irte->dlvry_mode = APIC_DELIVERY_MODE_FIXED;
1114 irte->vector = vector;
1115 irte->dest_id = IRTE_DEST(dest);
1116 irte->redir_hint = 1;
1119 static void prepare_irte_posted(struct irte *irte)
1121 memset(irte, 0, sizeof(*irte));
1127 struct irq_remap_ops intel_irq_remap_ops = {
1128 .prepare = intel_prepare_irq_remapping,
1129 .enable = intel_enable_irq_remapping,
1130 .disable = disable_irq_remapping,
1131 .reenable = reenable_irq_remapping,
1132 .enable_faulting = enable_drhd_fault_handling,
1135 #ifdef CONFIG_X86_POSTED_MSI
1137 static phys_addr_t get_pi_desc_addr(struct irq_data *irqd)
1139 int cpu = cpumask_first(irq_data_get_effective_affinity_mask(irqd));
1141 if (WARN_ON(cpu >= nr_cpu_ids))
1144 return __pa(per_cpu_ptr(&posted_msi_pi_desc, cpu));
1147 static void intel_ir_reconfigure_irte_posted(struct irq_data *irqd)
1149 struct intel_ir_data *ir_data = irqd->chip_data;
1150 struct irte *irte = &ir_data->irte_entry;
1151 struct irte irte_pi;
1154 pid_addr = get_pi_desc_addr(irqd);
1157 pr_warn("Failed to setup IRQ %d for posted mode", irqd->irq);
1161 memset(&irte_pi, 0, sizeof(irte_pi));
1163 /* The shared IRTE already be set up as posted during alloc_irte */
1164 dmar_copy_shared_irte(&irte_pi, irte);
1166 irte_pi.pda_l = (pid_addr >> (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
1167 irte_pi.pda_h = (pid_addr >> 32) & ~(-1UL << PDA_HIGH_BIT);
1169 modify_irte(&ir_data->irq_2_iommu, &irte_pi);
1173 static inline void intel_ir_reconfigure_irte_posted(struct irq_data *irqd) {}
1176 static void intel_ir_reconfigure_irte(struct irq_data *irqd, bool force)
1178 struct intel_ir_data *ir_data = irqd->chip_data;
1179 struct irte *irte = &ir_data->irte_entry;
1180 struct irq_cfg *cfg = irqd_cfg(irqd);
1183 * Atomically updates the IRTE with the new destination, vector
1184 * and flushes the interrupt entry cache.
1186 irte->vector = cfg->vector;
1187 irte->dest_id = IRTE_DEST(cfg->dest_apicid);
1189 if (ir_data->irq_2_iommu.posted_msi)
1190 intel_ir_reconfigure_irte_posted(irqd);
1191 else if (force || ir_data->irq_2_iommu.mode == IRQ_REMAPPING)
1192 modify_irte(&ir_data->irq_2_iommu, irte);
1196 * Migrate the IO-APIC irq in the presence of intr-remapping.
1198 * For both level and edge triggered, irq migration is a simple atomic
1199 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
1201 * For level triggered, we eliminate the io-apic RTE modification (with the
1202 * updated vector information), by using a virtual vector (io-apic pin number).
1203 * Real vector that is used for interrupting cpu will be coming from
1204 * the interrupt-remapping table entry.
1206 * As the migration is a simple atomic update of IRTE, the same mechanism
1207 * is used to migrate MSI irq's in the presence of interrupt-remapping.
1210 intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
1213 struct irq_data *parent = data->parent_data;
1214 struct irq_cfg *cfg = irqd_cfg(data);
1217 ret = parent->chip->irq_set_affinity(parent, mask, force);
1218 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
1221 intel_ir_reconfigure_irte(data, false);
1223 * After this point, all the interrupts will start arriving
1224 * at the new destination. So, time to cleanup the previous
1225 * vector allocation.
1227 vector_schedule_cleanup(cfg);
1229 return IRQ_SET_MASK_OK_DONE;
1232 static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
1233 struct msi_msg *msg)
1235 struct intel_ir_data *ir_data = irq_data->chip_data;
1237 *msg = ir_data->msi_entry;
1240 static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info)
1242 struct intel_ir_data *ir_data = data->chip_data;
1243 struct vcpu_data *vcpu_pi_info = info;
1245 /* stop posting interrupts, back to the default mode */
1246 if (!vcpu_pi_info) {
1247 modify_irte(&ir_data->irq_2_iommu, &ir_data->irte_entry);
1249 struct irte irte_pi;
1252 * We are not caching the posted interrupt entry. We
1253 * copy the data from the remapped entry and modify
1254 * the fields which are relevant for posted mode. The
1255 * cached remapped entry is used for switching back to
1258 memset(&irte_pi, 0, sizeof(irte_pi));
1259 dmar_copy_shared_irte(&irte_pi, &ir_data->irte_entry);
1261 /* Update the posted mode fields */
1263 irte_pi.p_urgent = 0;
1264 irte_pi.p_vector = vcpu_pi_info->vector;
1265 irte_pi.pda_l = (vcpu_pi_info->pi_desc_addr >>
1266 (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
1267 irte_pi.pda_h = (vcpu_pi_info->pi_desc_addr >> 32) &
1268 ~(-1UL << PDA_HIGH_BIT);
1270 modify_irte(&ir_data->irq_2_iommu, &irte_pi);
1276 static struct irq_chip intel_ir_chip = {
1278 .irq_ack = apic_ack_irq,
1279 .irq_set_affinity = intel_ir_set_affinity,
1280 .irq_compose_msi_msg = intel_ir_compose_msi_msg,
1281 .irq_set_vcpu_affinity = intel_ir_set_vcpu_affinity,
1285 * With posted MSIs, all vectors are multiplexed into a single notification
1286 * vector. Devices MSIs are then dispatched in a demux loop where
1287 * EOIs can be coalesced as well.
1289 * "INTEL-IR-POST" IRQ chip does not do EOI on ACK, thus the dummy irq_ack()
1290 * function. Instead EOI is performed by the posted interrupt notification
1293 * For the example below, 3 MSIs are coalesced into one CPU notification. Only
1294 * one apic_eoi() is needed.
1296 * __sysvec_posted_msi_notification()
1299 * irq_chip_ack_parent()
1300 * dummy(); // No EOI
1301 * handle_irq_event()
1304 * irq_chip_ack_parent()
1305 * dummy(); // No EOI
1306 * handle_irq_event()
1309 * irq_chip_ack_parent()
1310 * dummy(); // No EOI
1311 * handle_irq_event()
1317 static void dummy_ack(struct irq_data *d) { }
1319 static struct irq_chip intel_ir_chip_post_msi = {
1320 .name = "INTEL-IR-POST",
1321 .irq_ack = dummy_ack,
1322 .irq_set_affinity = intel_ir_set_affinity,
1323 .irq_compose_msi_msg = intel_ir_compose_msi_msg,
1324 .irq_set_vcpu_affinity = intel_ir_set_vcpu_affinity,
1327 static void fill_msi_msg(struct msi_msg *msg, u32 index, u32 subhandle)
1329 memset(msg, 0, sizeof(*msg));
1331 msg->arch_addr_lo.dmar_base_address = X86_MSI_BASE_ADDRESS_LOW;
1332 msg->arch_addr_lo.dmar_subhandle_valid = true;
1333 msg->arch_addr_lo.dmar_format = true;
1334 msg->arch_addr_lo.dmar_index_0_14 = index & 0x7FFF;
1335 msg->arch_addr_lo.dmar_index_15 = !!(index & 0x8000);
1337 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
1339 msg->arch_data.dmar_subhandle = subhandle;
1342 static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
1343 struct irq_cfg *irq_cfg,
1344 struct irq_alloc_info *info,
1345 int index, int sub_handle)
1347 struct irte *irte = &data->irte_entry;
1349 prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
1351 switch (info->type) {
1352 case X86_IRQ_ALLOC_TYPE_IOAPIC:
1353 /* Set source-id of interrupt request */
1354 set_ioapic_sid(irte, info->devid);
1355 apic_pr_verbose("IOAPIC[%d]: Set IRTE entry (P:%d FPD:%d Dst_Mode:%d Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X Avail:%X Vector:%02X Dest:%08X SID:%04X SQ:%X SVT:%X)\n",
1356 info->devid, irte->present, irte->fpd, irte->dst_mode,
1357 irte->redir_hint, irte->trigger_mode, irte->dlvry_mode,
1358 irte->avail, irte->vector, irte->dest_id, irte->sid,
1359 irte->sq, irte->svt);
1360 sub_handle = info->ioapic.pin;
1362 case X86_IRQ_ALLOC_TYPE_HPET:
1363 set_hpet_sid(irte, info->devid);
1365 case X86_IRQ_ALLOC_TYPE_PCI_MSI:
1366 case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
1367 if (posted_msi_supported()) {
1368 prepare_irte_posted(irte);
1369 data->irq_2_iommu.posted_msi = 1;
1373 pci_real_dma_dev(msi_desc_to_pci_dev(info->desc)));
1379 fill_msi_msg(&data->msi_entry, index, sub_handle);
1382 static void intel_free_irq_resources(struct irq_domain *domain,
1383 unsigned int virq, unsigned int nr_irqs)
1385 struct irq_data *irq_data;
1386 struct intel_ir_data *data;
1387 struct irq_2_iommu *irq_iommu;
1388 unsigned long flags;
1390 for (i = 0; i < nr_irqs; i++) {
1391 irq_data = irq_domain_get_irq_data(domain, virq + i);
1392 if (irq_data && irq_data->chip_data) {
1393 data = irq_data->chip_data;
1394 irq_iommu = &data->irq_2_iommu;
1395 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
1396 clear_entries(irq_iommu);
1397 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
1398 irq_domain_reset_irq_data(irq_data);
1404 static int intel_irq_remapping_alloc(struct irq_domain *domain,
1405 unsigned int virq, unsigned int nr_irqs,
1408 struct intel_iommu *iommu = domain->host_data;
1409 struct irq_alloc_info *info = arg;
1410 struct intel_ir_data *data, *ird;
1411 struct irq_data *irq_data;
1412 struct irq_cfg *irq_cfg;
1415 if (!info || !iommu)
1417 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI)
1420 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
1425 data = kzalloc(sizeof(*data), GFP_KERNEL);
1427 goto out_free_parent;
1429 index = alloc_irte(iommu, &data->irq_2_iommu, nr_irqs);
1431 pr_warn("Failed to allocate IRTE\n");
1433 goto out_free_parent;
1436 for (i = 0; i < nr_irqs; i++) {
1437 irq_data = irq_domain_get_irq_data(domain, virq + i);
1438 irq_cfg = irqd_cfg(irq_data);
1439 if (!irq_data || !irq_cfg) {
1447 ird = kzalloc(sizeof(*ird), GFP_KERNEL);
1450 /* Initialize the common data */
1451 ird->irq_2_iommu = data->irq_2_iommu;
1452 ird->irq_2_iommu.sub_handle = i;
1457 irq_data->hwirq = (index << 16) + i;
1458 irq_data->chip_data = ird;
1459 if (posted_msi_supported() &&
1460 ((info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI) ||
1461 (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX)))
1462 irq_data->chip = &intel_ir_chip_post_msi;
1464 irq_data->chip = &intel_ir_chip;
1465 intel_irq_remapping_prepare_irte(ird, irq_cfg, info, index, i);
1466 irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
1471 intel_free_irq_resources(domain, virq, i);
1473 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1477 static void intel_irq_remapping_free(struct irq_domain *domain,
1478 unsigned int virq, unsigned int nr_irqs)
1480 intel_free_irq_resources(domain, virq, nr_irqs);
1481 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1484 static int intel_irq_remapping_activate(struct irq_domain *domain,
1485 struct irq_data *irq_data, bool reserve)
1487 intel_ir_reconfigure_irte(irq_data, true);
1491 static void intel_irq_remapping_deactivate(struct irq_domain *domain,
1492 struct irq_data *irq_data)
1494 struct intel_ir_data *data = irq_data->chip_data;
1497 memset(&entry, 0, sizeof(entry));
1498 modify_irte(&data->irq_2_iommu, &entry);
1501 static int intel_irq_remapping_select(struct irq_domain *d,
1502 struct irq_fwspec *fwspec,
1503 enum irq_domain_bus_token bus_token)
1505 struct intel_iommu *iommu = NULL;
1507 if (x86_fwspec_is_ioapic(fwspec))
1508 iommu = map_ioapic_to_iommu(fwspec->param[0]);
1509 else if (x86_fwspec_is_hpet(fwspec))
1510 iommu = map_hpet_to_iommu(fwspec->param[0]);
1512 return iommu && d == iommu->ir_domain;
1515 static const struct irq_domain_ops intel_ir_domain_ops = {
1516 .select = intel_irq_remapping_select,
1517 .alloc = intel_irq_remapping_alloc,
1518 .free = intel_irq_remapping_free,
1519 .activate = intel_irq_remapping_activate,
1520 .deactivate = intel_irq_remapping_deactivate,
1523 static const struct msi_parent_ops dmar_msi_parent_ops = {
1524 .supported_flags = X86_VECTOR_MSI_FLAGS_SUPPORTED | MSI_FLAG_MULTI_PCI_MSI,
1526 .init_dev_msi_info = msi_parent_init_dev_msi_info,
1530 * Support of Interrupt Remapping Unit Hotplug
1532 static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
1535 int eim = x2apic_enabled();
1537 ret = intel_cap_audit(CAP_AUDIT_HOTPLUG_IRQR, iommu);
1541 if (eim && !ecap_eim_support(iommu->ecap)) {
1542 pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
1543 iommu->reg_phys, iommu->ecap);
1547 if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
1548 pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
1553 /* TODO: check all IOAPICs are covered by IOMMU */
1555 /* Setup Interrupt-remapping now. */
1556 ret = intel_setup_irq_remapping(iommu);
1558 pr_err("Failed to setup irq remapping for %s\n",
1560 intel_teardown_irq_remapping(iommu);
1561 ir_remove_ioapic_hpet_scope(iommu);
1563 iommu_enable_irq_remapping(iommu);
1569 int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
1572 struct intel_iommu *iommu = dmaru->iommu;
1574 if (!irq_remapping_enabled)
1578 if (!ecap_ir_support(iommu->ecap))
1580 if (irq_remapping_cap(IRQ_POSTING_CAP) &&
1581 !cap_pi_support(iommu->cap))
1585 if (!iommu->ir_table)
1586 ret = dmar_ir_add(dmaru, iommu);
1588 if (iommu->ir_table) {
1589 if (!bitmap_empty(iommu->ir_table->bitmap,
1590 INTR_REMAP_TABLE_ENTRIES)) {
1593 iommu_disable_irq_remapping(iommu);
1594 intel_teardown_irq_remapping(iommu);
1595 ir_remove_ioapic_hpet_scope(iommu);