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 <acpi/acpi.h>
14 #include <asm/irq_remapping.h>
15 #include <asm/pci-direct.h>
16 #include <asm/msidef.h>
18 #include "irq_remapping.h"
21 struct intel_iommu
*iommu
;
23 unsigned int bus
; /* PCI bus number */
24 unsigned int devfn
; /* PCI devfn number */
28 struct intel_iommu
*iommu
;
34 #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
35 #define IRTE_DEST(dest) ((x2apic_mode) ? dest : dest << 8)
37 static struct ioapic_scope ir_ioapic
[MAX_IO_APICS
];
38 static struct hpet_scope ir_hpet
[MAX_HPET_TBS
];
39 static int ir_ioapic_num
, ir_hpet_num
;
41 static DEFINE_RAW_SPINLOCK(irq_2_ir_lock
);
43 static struct irq_2_iommu
*irq_2_iommu(unsigned int irq
)
45 struct irq_cfg
*cfg
= irq_get_chip_data(irq
);
46 return cfg
? &cfg
->irq_2_iommu
: NULL
;
49 static int get_irte(int irq
, struct irte
*entry
)
51 struct irq_2_iommu
*irq_iommu
= irq_2_iommu(irq
);
55 if (!entry
|| !irq_iommu
)
58 raw_spin_lock_irqsave(&irq_2_ir_lock
, flags
);
60 index
= irq_iommu
->irte_index
+ irq_iommu
->sub_handle
;
61 *entry
= *(irq_iommu
->iommu
->ir_table
->base
+ index
);
63 raw_spin_unlock_irqrestore(&irq_2_ir_lock
, flags
);
67 static int alloc_irte(struct intel_iommu
*iommu
, int irq
, u16 count
)
69 struct ir_table
*table
= iommu
->ir_table
;
70 struct irq_2_iommu
*irq_iommu
= irq_2_iommu(irq
);
71 struct irq_cfg
*cfg
= irq_get_chip_data(irq
);
72 u16 index
, start_index
;
73 unsigned int mask
= 0;
76 if (!count
|| !irq_iommu
)
80 * start the IRTE search from index 0.
82 index
= start_index
= 0;
85 count
= __roundup_pow_of_two(count
);
89 if (mask
> ecap_max_handle_mask(iommu
->ecap
)) {
91 "Requested mask %x exceeds the max invalidation handle"
92 " mask value %Lx\n", mask
,
93 ecap_max_handle_mask(iommu
->ecap
));
97 raw_spin_lock_irqsave(&irq_2_ir_lock
, flags
);
98 index
= bitmap_find_free_region(table
->bitmap
,
99 INTR_REMAP_TABLE_ENTRIES
, mask
);
101 pr_warn("IR%d: can't allocate an IRTE\n", iommu
->seq_id
);
104 irq_iommu
->iommu
= iommu
;
105 irq_iommu
->irte_index
= index
;
106 irq_iommu
->sub_handle
= 0;
107 irq_iommu
->irte_mask
= mask
;
109 raw_spin_unlock_irqrestore(&irq_2_ir_lock
, flags
);
114 static int qi_flush_iec(struct intel_iommu
*iommu
, int index
, int mask
)
118 desc
.low
= QI_IEC_IIDEX(index
) | QI_IEC_TYPE
| QI_IEC_IM(mask
)
122 return qi_submit_sync(&desc
, iommu
);
125 static int map_irq_to_irte_handle(int irq
, u16
*sub_handle
)
127 struct irq_2_iommu
*irq_iommu
= irq_2_iommu(irq
);
134 raw_spin_lock_irqsave(&irq_2_ir_lock
, flags
);
135 *sub_handle
= irq_iommu
->sub_handle
;
136 index
= irq_iommu
->irte_index
;
137 raw_spin_unlock_irqrestore(&irq_2_ir_lock
, flags
);
141 static int set_irte_irq(int irq
, struct intel_iommu
*iommu
, u16 index
, u16 subhandle
)
143 struct irq_2_iommu
*irq_iommu
= irq_2_iommu(irq
);
144 struct irq_cfg
*cfg
= irq_get_chip_data(irq
);
150 raw_spin_lock_irqsave(&irq_2_ir_lock
, flags
);
153 irq_iommu
->iommu
= iommu
;
154 irq_iommu
->irte_index
= index
;
155 irq_iommu
->sub_handle
= subhandle
;
156 irq_iommu
->irte_mask
= 0;
158 raw_spin_unlock_irqrestore(&irq_2_ir_lock
, flags
);
163 static int modify_irte(int irq
, struct irte
*irte_modified
)
165 struct irq_2_iommu
*irq_iommu
= irq_2_iommu(irq
);
166 struct intel_iommu
*iommu
;
174 raw_spin_lock_irqsave(&irq_2_ir_lock
, flags
);
176 iommu
= irq_iommu
->iommu
;
178 index
= irq_iommu
->irte_index
+ irq_iommu
->sub_handle
;
179 irte
= &iommu
->ir_table
->base
[index
];
181 set_64bit(&irte
->low
, irte_modified
->low
);
182 set_64bit(&irte
->high
, irte_modified
->high
);
183 __iommu_flush_cache(iommu
, irte
, sizeof(*irte
));
185 rc
= qi_flush_iec(iommu
, index
, 0);
186 raw_spin_unlock_irqrestore(&irq_2_ir_lock
, flags
);
191 static struct intel_iommu
*map_hpet_to_ir(u8 hpet_id
)
195 for (i
= 0; i
< MAX_HPET_TBS
; i
++)
196 if (ir_hpet
[i
].id
== hpet_id
)
197 return ir_hpet
[i
].iommu
;
201 static struct intel_iommu
*map_ioapic_to_ir(int apic
)
205 for (i
= 0; i
< MAX_IO_APICS
; i
++)
206 if (ir_ioapic
[i
].id
== apic
)
207 return ir_ioapic
[i
].iommu
;
211 static struct intel_iommu
*map_dev_to_ir(struct pci_dev
*dev
)
213 struct dmar_drhd_unit
*drhd
;
215 drhd
= dmar_find_matched_drhd_unit(dev
);
222 static int clear_entries(struct irq_2_iommu
*irq_iommu
)
224 struct irte
*start
, *entry
, *end
;
225 struct intel_iommu
*iommu
;
228 if (irq_iommu
->sub_handle
)
231 iommu
= irq_iommu
->iommu
;
232 index
= irq_iommu
->irte_index
+ irq_iommu
->sub_handle
;
234 start
= iommu
->ir_table
->base
+ index
;
235 end
= start
+ (1 << irq_iommu
->irte_mask
);
237 for (entry
= start
; entry
< end
; entry
++) {
238 set_64bit(&entry
->low
, 0);
239 set_64bit(&entry
->high
, 0);
241 bitmap_release_region(iommu
->ir_table
->bitmap
, index
,
242 irq_iommu
->irte_mask
);
244 return qi_flush_iec(iommu
, index
, irq_iommu
->irte_mask
);
247 static int free_irte(int irq
)
249 struct irq_2_iommu
*irq_iommu
= irq_2_iommu(irq
);
256 raw_spin_lock_irqsave(&irq_2_ir_lock
, flags
);
258 rc
= clear_entries(irq_iommu
);
260 irq_iommu
->iommu
= NULL
;
261 irq_iommu
->irte_index
= 0;
262 irq_iommu
->sub_handle
= 0;
263 irq_iommu
->irte_mask
= 0;
265 raw_spin_unlock_irqrestore(&irq_2_ir_lock
, flags
);
271 * source validation type
273 #define SVT_NO_VERIFY 0x0 /* no verification is required */
274 #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
275 #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
278 * source-id qualifier
280 #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
281 #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
282 * the third least significant bit
284 #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
285 * the second and third least significant bits
287 #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
288 * the least three significant bits
292 * set SVT, SQ and SID fields of irte to verify
293 * source ids of interrupt requests
295 static void set_irte_sid(struct irte
*irte
, unsigned int svt
,
296 unsigned int sq
, unsigned int sid
)
298 if (disable_sourceid_checking
)
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
].id
== apic
) {
315 sid
= (ir_ioapic
[i
].bus
<< 8) | ir_ioapic
[i
].devfn
;
321 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic
);
325 set_irte_sid(irte
, 1, 0, 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
].id
== id
) {
340 sid
= (ir_hpet
[i
].bus
<< 8) | ir_hpet
[i
].devfn
;
346 pr_warning("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 static int set_msi_sid(struct irte
*irte
, struct pci_dev
*dev
)
362 struct pci_dev
*bridge
;
367 /* PCIe device or Root Complex integrated PCI device */
368 if (pci_is_pcie(dev
) || !dev
->bus
->parent
) {
369 set_irte_sid(irte
, SVT_VERIFY_SID_SQ
, SQ_ALL_16
,
370 (dev
->bus
->number
<< 8) | dev
->devfn
);
374 bridge
= pci_find_upstream_pcie_bridge(dev
);
376 if (pci_is_pcie(bridge
))/* this is a PCIe-to-PCI/PCIX bridge */
377 set_irte_sid(irte
, SVT_VERIFY_BUS
, SQ_ALL_16
,
378 (bridge
->bus
->number
<< 8) | dev
->bus
->number
);
379 else /* this is a legacy PCI bridge */
380 set_irte_sid(irte
, SVT_VERIFY_SID_SQ
, SQ_ALL_16
,
381 (bridge
->bus
->number
<< 8) | bridge
->devfn
);
387 static void iommu_set_irq_remapping(struct intel_iommu
*iommu
, int mode
)
393 addr
= virt_to_phys((void *)iommu
->ir_table
->base
);
395 raw_spin_lock_irqsave(&iommu
->register_lock
, flags
);
397 dmar_writeq(iommu
->reg
+ DMAR_IRTA_REG
,
398 (addr
) | IR_X2APIC_MODE(mode
) | INTR_REMAP_TABLE_REG_SIZE
);
400 /* Set interrupt-remapping table pointer */
401 iommu
->gcmd
|= DMA_GCMD_SIRTP
;
402 writel(iommu
->gcmd
, iommu
->reg
+ DMAR_GCMD_REG
);
404 IOMMU_WAIT_OP(iommu
, DMAR_GSTS_REG
,
405 readl
, (sts
& DMA_GSTS_IRTPS
), sts
);
406 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flags
);
409 * global invalidation of interrupt entry cache before enabling
410 * interrupt-remapping.
412 qi_global_iec(iommu
);
414 raw_spin_lock_irqsave(&iommu
->register_lock
, flags
);
416 /* Enable interrupt-remapping */
417 iommu
->gcmd
|= DMA_GCMD_IRE
;
418 iommu
->gcmd
&= ~DMA_GCMD_CFI
; /* Block compatibility-format MSIs */
419 writel(iommu
->gcmd
, iommu
->reg
+ DMAR_GCMD_REG
);
421 IOMMU_WAIT_OP(iommu
, DMAR_GSTS_REG
,
422 readl
, (sts
& DMA_GSTS_IRES
), sts
);
425 * With CFI clear in the Global Command register, we should be
426 * protected from dangerous (i.e. compatibility) interrupts
427 * regardless of x2apic status. Check just to be sure.
429 if (sts
& DMA_GSTS_CFIS
)
431 "Compatibility-format IRQs enabled despite intr remapping;\n"
432 "you are vulnerable to IRQ injection.\n");
434 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flags
);
438 static int intel_setup_irq_remapping(struct intel_iommu
*iommu
, int mode
)
440 struct ir_table
*ir_table
;
442 unsigned long *bitmap
;
444 ir_table
= iommu
->ir_table
= kzalloc(sizeof(struct ir_table
),
447 if (!iommu
->ir_table
)
450 pages
= alloc_pages_node(iommu
->node
, GFP_ATOMIC
| __GFP_ZERO
,
451 INTR_REMAP_PAGE_ORDER
);
454 pr_err("IR%d: failed to allocate pages of order %d\n",
455 iommu
->seq_id
, INTR_REMAP_PAGE_ORDER
);
456 kfree(iommu
->ir_table
);
460 bitmap
= kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES
),
461 sizeof(long), GFP_ATOMIC
);
462 if (bitmap
== NULL
) {
463 pr_err("IR%d: failed to allocate bitmap\n", iommu
->seq_id
);
464 __free_pages(pages
, INTR_REMAP_PAGE_ORDER
);
469 ir_table
->base
= page_address(pages
);
470 ir_table
->bitmap
= bitmap
;
472 iommu_set_irq_remapping(iommu
, mode
);
477 * Disable Interrupt Remapping.
479 static void iommu_disable_irq_remapping(struct intel_iommu
*iommu
)
484 if (!ecap_ir_support(iommu
->ecap
))
488 * global invalidation of interrupt entry cache before disabling
489 * interrupt-remapping.
491 qi_global_iec(iommu
);
493 raw_spin_lock_irqsave(&iommu
->register_lock
, flags
);
495 sts
= dmar_readq(iommu
->reg
+ DMAR_GSTS_REG
);
496 if (!(sts
& DMA_GSTS_IRES
))
499 iommu
->gcmd
&= ~DMA_GCMD_IRE
;
500 writel(iommu
->gcmd
, iommu
->reg
+ DMAR_GCMD_REG
);
502 IOMMU_WAIT_OP(iommu
, DMAR_GSTS_REG
,
503 readl
, !(sts
& DMA_GSTS_IRES
), sts
);
506 raw_spin_unlock_irqrestore(&iommu
->register_lock
, flags
);
509 static int __init
dmar_x2apic_optout(void)
511 struct acpi_table_dmar
*dmar
;
512 dmar
= (struct acpi_table_dmar
*)dmar_tbl
;
513 if (!dmar
|| no_x2apic_optout
)
515 return dmar
->flags
& DMAR_X2APIC_OPT_OUT
;
518 static int __init
intel_irq_remapping_supported(void)
520 struct dmar_drhd_unit
*drhd
;
522 if (disable_irq_remap
)
524 if (irq_remap_broken
) {
526 "This system BIOS has enabled interrupt remapping\n"
527 "on a chipset that contains an erratum making that\n"
528 "feature unstable. To maintain system stability\n"
529 "interrupt remapping is being disabled. Please\n"
530 "contact your BIOS vendor for an update\n");
531 add_taint(TAINT_FIRMWARE_WORKAROUND
, LOCKDEP_STILL_OK
);
532 disable_irq_remap
= 1;
536 if (!dmar_ir_support())
539 for_each_drhd_unit(drhd
) {
540 struct intel_iommu
*iommu
= drhd
->iommu
;
542 if (!ecap_ir_support(iommu
->ecap
))
549 static int __init
intel_enable_irq_remapping(void)
551 struct dmar_drhd_unit
*drhd
;
556 x2apic_present
= x2apic_supported();
558 if (parse_ioapics_under_ir() != 1) {
559 printk(KERN_INFO
"Not enable interrupt remapping\n");
563 if (x2apic_present
) {
564 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
566 eim
= !dmar_x2apic_optout();
569 "Your BIOS is broken and requested that x2apic be disabled.\n"
570 "This will slightly decrease performance.\n"
571 "Use 'intremap=no_x2apic_optout' to override BIOS request.\n");
574 for_each_drhd_unit(drhd
) {
575 struct intel_iommu
*iommu
= drhd
->iommu
;
578 * If the queued invalidation is already initialized,
579 * shouldn't disable it.
585 * Clear previous faults.
587 dmar_fault(-1, iommu
);
590 * Disable intr remapping and queued invalidation, if already
591 * enabled prior to OS handover.
593 iommu_disable_irq_remapping(iommu
);
595 dmar_disable_qi(iommu
);
599 * check for the Interrupt-remapping support
601 for_each_drhd_unit(drhd
) {
602 struct intel_iommu
*iommu
= drhd
->iommu
;
604 if (!ecap_ir_support(iommu
->ecap
))
607 if (eim
&& !ecap_eim_support(iommu
->ecap
)) {
608 printk(KERN_INFO
"DRHD %Lx: EIM not supported by DRHD, "
609 " ecap %Lx\n", drhd
->reg_base_addr
, iommu
->ecap
);
615 * Enable queued invalidation for all the DRHD's.
617 for_each_drhd_unit(drhd
) {
619 struct intel_iommu
*iommu
= drhd
->iommu
;
620 ret
= dmar_enable_qi(iommu
);
623 printk(KERN_ERR
"DRHD %Lx: failed to enable queued, "
624 " invalidation, ecap %Lx, ret %d\n",
625 drhd
->reg_base_addr
, iommu
->ecap
, ret
);
631 * Setup Interrupt-remapping for all the DRHD's now.
633 for_each_drhd_unit(drhd
) {
634 struct intel_iommu
*iommu
= drhd
->iommu
;
636 if (!ecap_ir_support(iommu
->ecap
))
639 if (intel_setup_irq_remapping(iommu
, eim
))
648 irq_remapping_enabled
= 1;
651 * VT-d has a different layout for IO-APIC entries when
652 * interrupt remapping is enabled. So it needs a special routine
653 * to print IO-APIC entries for debugging purposes too.
655 x86_io_apic_ops
.print_entries
= intel_ir_io_apic_print_entries
;
657 pr_info("Enabled IRQ remapping in %s mode\n", eim
? "x2apic" : "xapic");
659 return eim
? IRQ_REMAP_X2APIC_MODE
: IRQ_REMAP_XAPIC_MODE
;
663 * handle error condition gracefully here!
667 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
672 static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope
*scope
,
673 struct intel_iommu
*iommu
)
675 struct acpi_dmar_pci_path
*path
;
680 path
= (struct acpi_dmar_pci_path
*)(scope
+ 1);
681 count
= (scope
->length
- sizeof(struct acpi_dmar_device_scope
))
682 / sizeof(struct acpi_dmar_pci_path
);
684 while (--count
> 0) {
686 * Access PCI directly due to the PCI
687 * subsystem isn't initialized yet.
689 bus
= read_pci_config_byte(bus
, path
->device
, path
->function
,
693 ir_hpet
[ir_hpet_num
].bus
= bus
;
694 ir_hpet
[ir_hpet_num
].devfn
= PCI_DEVFN(path
->device
, path
->function
);
695 ir_hpet
[ir_hpet_num
].iommu
= iommu
;
696 ir_hpet
[ir_hpet_num
].id
= scope
->enumeration_id
;
700 static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope
*scope
,
701 struct intel_iommu
*iommu
)
703 struct acpi_dmar_pci_path
*path
;
708 path
= (struct acpi_dmar_pci_path
*)(scope
+ 1);
709 count
= (scope
->length
- sizeof(struct acpi_dmar_device_scope
))
710 / sizeof(struct acpi_dmar_pci_path
);
712 while (--count
> 0) {
714 * Access PCI directly due to the PCI
715 * subsystem isn't initialized yet.
717 bus
= read_pci_config_byte(bus
, path
->device
, path
->function
,
722 ir_ioapic
[ir_ioapic_num
].bus
= bus
;
723 ir_ioapic
[ir_ioapic_num
].devfn
= PCI_DEVFN(path
->device
, path
->function
);
724 ir_ioapic
[ir_ioapic_num
].iommu
= iommu
;
725 ir_ioapic
[ir_ioapic_num
].id
= scope
->enumeration_id
;
729 static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header
*header
,
730 struct intel_iommu
*iommu
)
732 struct acpi_dmar_hardware_unit
*drhd
;
733 struct acpi_dmar_device_scope
*scope
;
736 drhd
= (struct acpi_dmar_hardware_unit
*)header
;
738 start
= (void *)(drhd
+ 1);
739 end
= ((void *)drhd
) + header
->length
;
741 while (start
< end
) {
743 if (scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_IOAPIC
) {
744 if (ir_ioapic_num
== MAX_IO_APICS
) {
745 printk(KERN_WARNING
"Exceeded Max IO APICS\n");
749 printk(KERN_INFO
"IOAPIC id %d under DRHD base "
750 " 0x%Lx IOMMU %d\n", scope
->enumeration_id
,
751 drhd
->address
, iommu
->seq_id
);
753 ir_parse_one_ioapic_scope(scope
, iommu
);
754 } else if (scope
->entry_type
== ACPI_DMAR_SCOPE_TYPE_HPET
) {
755 if (ir_hpet_num
== MAX_HPET_TBS
) {
756 printk(KERN_WARNING
"Exceeded Max HPET blocks\n");
760 printk(KERN_INFO
"HPET id %d under DRHD base"
761 " 0x%Lx\n", scope
->enumeration_id
,
764 ir_parse_one_hpet_scope(scope
, iommu
);
766 start
+= scope
->length
;
773 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
776 int __init
parse_ioapics_under_ir(void)
778 struct dmar_drhd_unit
*drhd
;
779 int ir_supported
= 0;
782 for_each_drhd_unit(drhd
) {
783 struct intel_iommu
*iommu
= drhd
->iommu
;
785 if (ecap_ir_support(iommu
->ecap
)) {
786 if (ir_parse_ioapic_hpet_scope(drhd
->hdr
, iommu
))
796 for (ioapic_idx
= 0; ioapic_idx
< nr_ioapics
; ioapic_idx
++) {
797 int ioapic_id
= mpc_ioapic_id(ioapic_idx
);
798 if (!map_ioapic_to_ir(ioapic_id
)) {
799 pr_err(FW_BUG
"ioapic %d has no mapping iommu, "
800 "interrupt remapping will be disabled\n",
809 static int __init
ir_dev_scope_init(void)
811 if (!irq_remapping_enabled
)
814 return dmar_dev_scope_init();
816 rootfs_initcall(ir_dev_scope_init
);
818 static void disable_irq_remapping(void)
820 struct dmar_drhd_unit
*drhd
;
821 struct intel_iommu
*iommu
= NULL
;
824 * Disable Interrupt-remapping for all the DRHD's now.
826 for_each_iommu(iommu
, drhd
) {
827 if (!ecap_ir_support(iommu
->ecap
))
830 iommu_disable_irq_remapping(iommu
);
834 static int reenable_irq_remapping(int eim
)
836 struct dmar_drhd_unit
*drhd
;
838 struct intel_iommu
*iommu
= NULL
;
840 for_each_iommu(iommu
, drhd
)
842 dmar_reenable_qi(iommu
);
845 * Setup Interrupt-remapping for all the DRHD's now.
847 for_each_iommu(iommu
, drhd
) {
848 if (!ecap_ir_support(iommu
->ecap
))
851 /* Set up interrupt remapping for iommu.*/
852 iommu_set_irq_remapping(iommu
, eim
);
863 * handle error condition gracefully here!
868 static void prepare_irte(struct irte
*irte
, int vector
,
871 memset(irte
, 0, sizeof(*irte
));
874 irte
->dst_mode
= apic
->irq_dest_mode
;
876 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
877 * actual level or edge trigger will be setup in the IO-APIC
878 * RTE. This will help simplify level triggered irq migration.
879 * For more details, see the comments (in io_apic.c) explainig IO-APIC
880 * irq migration in the presence of interrupt-remapping.
882 irte
->trigger_mode
= 0;
883 irte
->dlvry_mode
= apic
->irq_delivery_mode
;
884 irte
->vector
= vector
;
885 irte
->dest_id
= IRTE_DEST(dest
);
886 irte
->redir_hint
= 1;
889 static int intel_setup_ioapic_entry(int irq
,
890 struct IO_APIC_route_entry
*route_entry
,
891 unsigned int destination
, int vector
,
892 struct io_apic_irq_attr
*attr
)
894 int ioapic_id
= mpc_ioapic_id(attr
->ioapic
);
895 struct intel_iommu
*iommu
= map_ioapic_to_ir(ioapic_id
);
896 struct IR_IO_APIC_route_entry
*entry
;
901 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id
);
905 entry
= (struct IR_IO_APIC_route_entry
*)route_entry
;
907 index
= alloc_irte(iommu
, irq
, 1);
909 pr_warn("Failed to allocate IRTE for ioapic %d\n", ioapic_id
);
913 prepare_irte(&irte
, vector
, destination
);
915 /* Set source-id of interrupt request */
916 set_ioapic_sid(&irte
, ioapic_id
);
918 modify_irte(irq
, &irte
);
920 apic_printk(APIC_VERBOSE
, KERN_DEBUG
"IOAPIC[%d]: "
921 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
922 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
923 "Avail:%X Vector:%02X Dest:%08X "
924 "SID:%04X SQ:%X SVT:%X)\n",
925 attr
->ioapic
, irte
.present
, irte
.fpd
, irte
.dst_mode
,
926 irte
.redir_hint
, irte
.trigger_mode
, irte
.dlvry_mode
,
927 irte
.avail
, irte
.vector
, irte
.dest_id
,
928 irte
.sid
, irte
.sq
, irte
.svt
);
930 memset(entry
, 0, sizeof(*entry
));
932 entry
->index2
= (index
>> 15) & 0x1;
935 entry
->index
= (index
& 0x7fff);
937 * IO-APIC RTE will be configured with virtual vector.
938 * irq handler will do the explicit EOI to the io-apic.
940 entry
->vector
= attr
->ioapic_pin
;
941 entry
->mask
= 0; /* enable IRQ */
942 entry
->trigger
= attr
->trigger
;
943 entry
->polarity
= attr
->polarity
;
945 /* Mask level triggered irqs.
946 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
955 * Migrate the IO-APIC irq in the presence of intr-remapping.
957 * For both level and edge triggered, irq migration is a simple atomic
958 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
960 * For level triggered, we eliminate the io-apic RTE modification (with the
961 * updated vector information), by using a virtual vector (io-apic pin number).
962 * Real vector that is used for interrupting cpu will be coming from
963 * the interrupt-remapping table entry.
965 * As the migration is a simple atomic update of IRTE, the same mechanism
966 * is used to migrate MSI irq's in the presence of interrupt-remapping.
969 intel_ioapic_set_affinity(struct irq_data
*data
, const struct cpumask
*mask
,
972 struct irq_cfg
*cfg
= data
->chip_data
;
973 unsigned int dest
, irq
= data
->irq
;
977 if (!config_enabled(CONFIG_SMP
))
980 if (!cpumask_intersects(mask
, cpu_online_mask
))
983 if (get_irte(irq
, &irte
))
986 err
= assign_irq_vector(irq
, cfg
, mask
);
990 err
= apic
->cpu_mask_to_apicid_and(cfg
->domain
, mask
, &dest
);
992 if (assign_irq_vector(irq
, cfg
, data
->affinity
))
993 pr_err("Failed to recover vector for irq %d\n", irq
);
997 irte
.vector
= cfg
->vector
;
998 irte
.dest_id
= IRTE_DEST(dest
);
1001 * Atomically updates the IRTE with the new destination, vector
1002 * and flushes the interrupt entry cache.
1004 modify_irte(irq
, &irte
);
1007 * After this point, all the interrupts will start arriving
1008 * at the new destination. So, time to cleanup the previous
1009 * vector allocation.
1011 if (cfg
->move_in_progress
)
1012 send_cleanup_vector(cfg
);
1014 cpumask_copy(data
->affinity
, mask
);
1018 static void intel_compose_msi_msg(struct pci_dev
*pdev
,
1019 unsigned int irq
, unsigned int dest
,
1020 struct msi_msg
*msg
, u8 hpet_id
)
1022 struct irq_cfg
*cfg
;
1027 cfg
= irq_get_chip_data(irq
);
1029 ir_index
= map_irq_to_irte_handle(irq
, &sub_handle
);
1030 BUG_ON(ir_index
== -1);
1032 prepare_irte(&irte
, cfg
->vector
, dest
);
1034 /* Set source-id of interrupt request */
1036 set_msi_sid(&irte
, pdev
);
1038 set_hpet_sid(&irte
, hpet_id
);
1040 modify_irte(irq
, &irte
);
1042 msg
->address_hi
= MSI_ADDR_BASE_HI
;
1043 msg
->data
= sub_handle
;
1044 msg
->address_lo
= MSI_ADDR_BASE_LO
| MSI_ADDR_IR_EXT_INT
|
1046 MSI_ADDR_IR_INDEX1(ir_index
) |
1047 MSI_ADDR_IR_INDEX2(ir_index
);
1051 * Map the PCI dev to the corresponding remapping hardware unit
1052 * and allocate 'nvec' consecutive interrupt-remapping table entries
1055 static int intel_msi_alloc_irq(struct pci_dev
*dev
, int irq
, int nvec
)
1057 struct intel_iommu
*iommu
;
1060 iommu
= map_dev_to_ir(dev
);
1063 "Unable to map PCI %s to iommu\n", pci_name(dev
));
1067 index
= alloc_irte(iommu
, irq
, nvec
);
1070 "Unable to allocate %d IRTE for PCI %s\n", nvec
,
1077 static int intel_msi_setup_irq(struct pci_dev
*pdev
, unsigned int irq
,
1078 int index
, int sub_handle
)
1080 struct intel_iommu
*iommu
;
1082 iommu
= map_dev_to_ir(pdev
);
1086 * setup the mapping between the irq and the IRTE
1087 * base index, the sub_handle pointing to the
1088 * appropriate interrupt remap table entry.
1090 set_irte_irq(irq
, iommu
, index
, sub_handle
);
1095 static int intel_setup_hpet_msi(unsigned int irq
, unsigned int id
)
1097 struct intel_iommu
*iommu
= map_hpet_to_ir(id
);
1103 index
= alloc_irte(iommu
, irq
, 1);
1110 struct irq_remap_ops intel_irq_remap_ops
= {
1111 .supported
= intel_irq_remapping_supported
,
1112 .prepare
= dmar_table_init
,
1113 .enable
= intel_enable_irq_remapping
,
1114 .disable
= disable_irq_remapping
,
1115 .reenable
= reenable_irq_remapping
,
1116 .enable_faulting
= enable_drhd_fault_handling
,
1117 .setup_ioapic_entry
= intel_setup_ioapic_entry
,
1118 .set_affinity
= intel_ioapic_set_affinity
,
1119 .free_irq
= free_irte
,
1120 .compose_msi_msg
= intel_compose_msi_msg
,
1121 .msi_alloc_irq
= intel_msi_alloc_irq
,
1122 .msi_setup_irq
= intel_msi_setup_irq
,
1123 .setup_hpet_msi
= intel_setup_hpet_msi
,