Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1 | |
| 2 | #define pr_fmt(fmt) "DMAR-IR: " fmt |
| 3 | |
Yinghai Lu | 5aeecaf | 2008-08-19 20:49:59 -0700 | [diff] [blame] | 4 | #include <linux/interrupt.h> |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 5 | #include <linux/dmar.h> |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 6 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 8 | #include <linux/jiffies.h> |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 9 | #include <linux/hpet.h> |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 10 | #include <linux/pci.h> |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 11 | #include <linux/irq.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 12 | #include <linux/intel-iommu.h> |
| 13 | #include <linux/acpi.h> |
Joerg Roedel | af3b358 | 2015-06-12 15:00:21 +0200 | [diff] [blame] | 14 | #include <linux/crash_dump.h> |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 15 | #include <asm/io_apic.h> |
Yinghai Lu | 17483a1 | 2008-12-12 13:14:18 -0800 | [diff] [blame] | 16 | #include <asm/smp.h> |
Jaswinder Singh Rajput | 6d652ea | 2009-01-07 21:38:59 +0530 | [diff] [blame] | 17 | #include <asm/cpu.h> |
Suresh Siddha | 8a8f422 | 2012-03-30 11:47:08 -0700 | [diff] [blame] | 18 | #include <asm/irq_remapping.h> |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 19 | #include <asm/pci-direct.h> |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 20 | #include <asm/msidef.h> |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 21 | |
Suresh Siddha | 8a8f422 | 2012-03-30 11:47:08 -0700 | [diff] [blame] | 22 | #include "irq_remapping.h" |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 23 | |
Joerg Roedel | eef93fd | 2012-03-30 11:46:59 -0700 | [diff] [blame] | 24 | struct ioapic_scope { |
| 25 | struct intel_iommu *iommu; |
| 26 | unsigned int id; |
| 27 | unsigned int bus; /* PCI bus number */ |
| 28 | unsigned int devfn; /* PCI devfn number */ |
| 29 | }; |
| 30 | |
| 31 | struct hpet_scope { |
| 32 | struct intel_iommu *iommu; |
| 33 | u8 id; |
| 34 | unsigned int bus; |
| 35 | unsigned int devfn; |
| 36 | }; |
| 37 | |
| 38 | #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0) |
Jiang Liu | 13d09b6 | 2015-01-07 15:31:37 +0800 | [diff] [blame] | 39 | #define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8) |
Joerg Roedel | eef93fd | 2012-03-30 11:46:59 -0700 | [diff] [blame] | 40 | |
Jiang Liu | 13d09b6 | 2015-01-07 15:31:37 +0800 | [diff] [blame] | 41 | static int __read_mostly eim_mode; |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 42 | static struct ioapic_scope ir_ioapic[MAX_IO_APICS]; |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 43 | static struct hpet_scope ir_hpet[MAX_HPET_TBS]; |
Chris Wright | d1423d5 | 2010-07-20 11:06:49 -0700 | [diff] [blame] | 44 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 45 | /* |
| 46 | * Lock ordering: |
| 47 | * ->dmar_global_lock |
| 48 | * ->irq_2_ir_lock |
| 49 | * ->qi->q_lock |
| 50 | * ->iommu->register_lock |
| 51 | * Note: |
| 52 | * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called |
| 53 | * in single-threaded environment with interrupt disabled, so no need to tabke |
| 54 | * the dmar_global_lock. |
| 55 | */ |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 56 | static DEFINE_RAW_SPINLOCK(irq_2_ir_lock); |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 57 | |
Joerg Roedel | af3b358 | 2015-06-12 15:00:21 +0200 | [diff] [blame] | 58 | static void iommu_disable_irq_remapping(struct intel_iommu *iommu); |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 59 | static int __init parse_ioapics_under_ir(void); |
| 60 | |
Joerg Roedel | af3b358 | 2015-06-12 15:00:21 +0200 | [diff] [blame] | 61 | static bool ir_pre_enabled(struct intel_iommu *iommu) |
| 62 | { |
| 63 | return (iommu->flags & VTD_FLAG_IRQ_REMAP_PRE_ENABLED); |
| 64 | } |
| 65 | |
| 66 | static void clear_ir_pre_enabled(struct intel_iommu *iommu) |
| 67 | { |
| 68 | iommu->flags &= ~VTD_FLAG_IRQ_REMAP_PRE_ENABLED; |
| 69 | } |
| 70 | |
| 71 | static void init_ir_status(struct intel_iommu *iommu) |
| 72 | { |
| 73 | u32 gsts; |
| 74 | |
| 75 | gsts = readl(iommu->reg + DMAR_GSTS_REG); |
| 76 | if (gsts & DMA_GSTS_IRES) |
| 77 | iommu->flags |= VTD_FLAG_IRQ_REMAP_PRE_ENABLED; |
| 78 | } |
| 79 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 80 | static struct irq_2_iommu *irq_2_iommu(unsigned int irq) |
| 81 | { |
Jiang Liu | 91411da | 2014-10-27 16:12:09 +0800 | [diff] [blame] | 82 | struct irq_cfg *cfg = irq_cfg(irq); |
Thomas Gleixner | 349d676 | 2010-10-10 12:29:27 +0200 | [diff] [blame] | 83 | return cfg ? &cfg->irq_2_iommu : NULL; |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Rashika Kheria | 6a7885c | 2013-12-18 12:04:27 +0530 | [diff] [blame] | 86 | static int get_irte(int irq, struct irte *entry) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 87 | { |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 88 | struct irq_2_iommu *irq_iommu = irq_2_iommu(irq); |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 89 | unsigned long flags; |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 90 | int index; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 91 | |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 92 | if (!entry || !irq_iommu) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 93 | return -1; |
| 94 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 95 | raw_spin_lock_irqsave(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 96 | |
Greg Edwards | af43746 | 2014-07-23 10:13:26 -0600 | [diff] [blame] | 97 | if (unlikely(!irq_iommu->iommu)) { |
| 98 | raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
| 99 | return -1; |
| 100 | } |
| 101 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 102 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
| 103 | *entry = *(irq_iommu->iommu->ir_table->base + index); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 104 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 105 | raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 109 | static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 110 | { |
| 111 | struct ir_table *table = iommu->ir_table; |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 112 | struct irq_2_iommu *irq_iommu = irq_2_iommu(irq); |
Jiang Liu | 91411da | 2014-10-27 16:12:09 +0800 | [diff] [blame] | 113 | struct irq_cfg *cfg = irq_cfg(irq); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 114 | unsigned int mask = 0; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 115 | unsigned long flags; |
Dan Carpenter | 9f4c744 | 2014-01-09 08:32:36 +0300 | [diff] [blame] | 116 | int index; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 117 | |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 118 | if (!count || !irq_iommu) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 119 | return -1; |
| 120 | |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 121 | if (count > 1) { |
| 122 | count = __roundup_pow_of_two(count); |
| 123 | mask = ilog2(count); |
| 124 | } |
| 125 | |
| 126 | if (mask > ecap_max_handle_mask(iommu->ecap)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 127 | pr_err("Requested mask %x exceeds the max invalidation handle" |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 128 | " mask value %Lx\n", mask, |
| 129 | ecap_max_handle_mask(iommu->ecap)); |
| 130 | return -1; |
| 131 | } |
| 132 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 133 | raw_spin_lock_irqsave(&irq_2_ir_lock, flags); |
Jiang Liu | 360eb3c | 2014-01-06 14:18:08 +0800 | [diff] [blame] | 134 | index = bitmap_find_free_region(table->bitmap, |
| 135 | INTR_REMAP_TABLE_ENTRIES, mask); |
| 136 | if (index < 0) { |
| 137 | pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id); |
| 138 | } else { |
| 139 | cfg->remapped = 1; |
| 140 | irq_iommu->iommu = iommu; |
| 141 | irq_iommu->irte_index = index; |
| 142 | irq_iommu->sub_handle = 0; |
| 143 | irq_iommu->irte_mask = mask; |
| 144 | } |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 145 | raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 146 | |
| 147 | return index; |
| 148 | } |
| 149 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 150 | static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 151 | { |
| 152 | struct qi_desc desc; |
| 153 | |
| 154 | desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask) |
| 155 | | QI_IEC_SELECTIVE; |
| 156 | desc.high = 0; |
| 157 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 158 | return qi_submit_sync(&desc, iommu); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 161 | static int map_irq_to_irte_handle(int irq, u16 *sub_handle) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 162 | { |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 163 | struct irq_2_iommu *irq_iommu = irq_2_iommu(irq); |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 164 | unsigned long flags; |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 165 | int index; |
| 166 | |
| 167 | if (!irq_iommu) |
| 168 | return -1; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 169 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 170 | raw_spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 171 | *sub_handle = irq_iommu->sub_handle; |
| 172 | index = irq_iommu->irte_index; |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 173 | raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 174 | return index; |
| 175 | } |
| 176 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 177 | static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 178 | { |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 179 | struct irq_2_iommu *irq_iommu = irq_2_iommu(irq); |
Jiang Liu | 91411da | 2014-10-27 16:12:09 +0800 | [diff] [blame] | 180 | struct irq_cfg *cfg = irq_cfg(irq); |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 181 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 182 | |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 183 | if (!irq_iommu) |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 184 | return -1; |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 185 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 186 | raw_spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 187 | |
Joerg Roedel | 9b1b0e4 | 2012-09-26 12:44:45 +0200 | [diff] [blame] | 188 | cfg->remapped = 1; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 189 | irq_iommu->iommu = iommu; |
| 190 | irq_iommu->irte_index = index; |
| 191 | irq_iommu->sub_handle = subhandle; |
| 192 | irq_iommu->irte_mask = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 193 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 194 | raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 199 | static int modify_irte(int irq, struct irte *irte_modified) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 200 | { |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 201 | struct irq_2_iommu *irq_iommu = irq_2_iommu(irq); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 202 | struct intel_iommu *iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 203 | unsigned long flags; |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 204 | struct irte *irte; |
| 205 | int rc, index; |
| 206 | |
| 207 | if (!irq_iommu) |
| 208 | return -1; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 209 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 210 | raw_spin_lock_irqsave(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 211 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 212 | iommu = irq_iommu->iommu; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 213 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 214 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 215 | irte = &iommu->ir_table->base[index]; |
| 216 | |
Linus Torvalds | c513b67 | 2010-08-06 11:02:31 -0700 | [diff] [blame] | 217 | set_64bit(&irte->low, irte_modified->low); |
| 218 | set_64bit(&irte->high, irte_modified->high); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 219 | __iommu_flush_cache(iommu, irte, sizeof(*irte)); |
| 220 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 221 | rc = qi_flush_iec(iommu, index, 0); |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 222 | raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 223 | |
| 224 | return rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 227 | static struct intel_iommu *map_hpet_to_ir(u8 hpet_id) |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 228 | { |
| 229 | int i; |
| 230 | |
| 231 | for (i = 0; i < MAX_HPET_TBS; i++) |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 232 | if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu) |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 233 | return ir_hpet[i].iommu; |
| 234 | return NULL; |
| 235 | } |
| 236 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 237 | static struct intel_iommu *map_ioapic_to_ir(int apic) |
Suresh Siddha | 89027d3 | 2008-07-10 11:16:56 -0700 | [diff] [blame] | 238 | { |
| 239 | int i; |
| 240 | |
| 241 | for (i = 0; i < MAX_IO_APICS; i++) |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 242 | if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu) |
Suresh Siddha | 89027d3 | 2008-07-10 11:16:56 -0700 | [diff] [blame] | 243 | return ir_ioapic[i].iommu; |
| 244 | return NULL; |
| 245 | } |
| 246 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 247 | static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev) |
Suresh Siddha | 75c46fa | 2008-07-10 11:16:57 -0700 | [diff] [blame] | 248 | { |
| 249 | struct dmar_drhd_unit *drhd; |
| 250 | |
| 251 | drhd = dmar_find_matched_drhd_unit(dev); |
| 252 | if (!drhd) |
| 253 | return NULL; |
| 254 | |
| 255 | return drhd->iommu; |
| 256 | } |
| 257 | |
Weidong Han | c4658b4 | 2009-05-23 00:41:14 +0800 | [diff] [blame] | 258 | static int clear_entries(struct irq_2_iommu *irq_iommu) |
| 259 | { |
| 260 | struct irte *start, *entry, *end; |
| 261 | struct intel_iommu *iommu; |
| 262 | int index; |
| 263 | |
| 264 | if (irq_iommu->sub_handle) |
| 265 | return 0; |
| 266 | |
| 267 | iommu = irq_iommu->iommu; |
| 268 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
| 269 | |
| 270 | start = iommu->ir_table->base + index; |
| 271 | end = start + (1 << irq_iommu->irte_mask); |
| 272 | |
| 273 | for (entry = start; entry < end; entry++) { |
Linus Torvalds | c513b67 | 2010-08-06 11:02:31 -0700 | [diff] [blame] | 274 | set_64bit(&entry->low, 0); |
| 275 | set_64bit(&entry->high, 0); |
Weidong Han | c4658b4 | 2009-05-23 00:41:14 +0800 | [diff] [blame] | 276 | } |
Jiang Liu | 360eb3c | 2014-01-06 14:18:08 +0800 | [diff] [blame] | 277 | bitmap_release_region(iommu->ir_table->bitmap, index, |
| 278 | irq_iommu->irte_mask); |
Weidong Han | c4658b4 | 2009-05-23 00:41:14 +0800 | [diff] [blame] | 279 | |
| 280 | return qi_flush_iec(iommu, index, irq_iommu->irte_mask); |
| 281 | } |
| 282 | |
Joerg Roedel | 9d619f6 | 2012-03-30 11:47:04 -0700 | [diff] [blame] | 283 | static int free_irte(int irq) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 284 | { |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 285 | struct irq_2_iommu *irq_iommu = irq_2_iommu(irq); |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 286 | unsigned long flags; |
Thomas Gleixner | d585d06 | 2010-10-10 12:34:27 +0200 | [diff] [blame] | 287 | int rc; |
| 288 | |
| 289 | if (!irq_iommu) |
| 290 | return -1; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 291 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 292 | raw_spin_lock_irqsave(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 293 | |
Weidong Han | c4658b4 | 2009-05-23 00:41:14 +0800 | [diff] [blame] | 294 | rc = clear_entries(irq_iommu); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 295 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 296 | irq_iommu->iommu = NULL; |
| 297 | irq_iommu->irte_index = 0; |
| 298 | irq_iommu->sub_handle = 0; |
| 299 | irq_iommu->irte_mask = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 300 | |
Thomas Gleixner | 96f8e98 | 2011-07-19 16:28:19 +0200 | [diff] [blame] | 301 | raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 302 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 303 | return rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 306 | /* |
| 307 | * source validation type |
| 308 | */ |
| 309 | #define SVT_NO_VERIFY 0x0 /* no verification is required */ |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 310 | #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */ |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 311 | #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */ |
| 312 | |
| 313 | /* |
| 314 | * source-id qualifier |
| 315 | */ |
| 316 | #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */ |
| 317 | #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore |
| 318 | * the third least significant bit |
| 319 | */ |
| 320 | #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore |
| 321 | * the second and third least significant bits |
| 322 | */ |
| 323 | #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore |
| 324 | * the least three significant bits |
| 325 | */ |
| 326 | |
| 327 | /* |
| 328 | * set SVT, SQ and SID fields of irte to verify |
| 329 | * source ids of interrupt requests |
| 330 | */ |
| 331 | static void set_irte_sid(struct irte *irte, unsigned int svt, |
| 332 | unsigned int sq, unsigned int sid) |
| 333 | { |
Chris Wright | d1423d5 | 2010-07-20 11:06:49 -0700 | [diff] [blame] | 334 | if (disable_sourceid_checking) |
| 335 | svt = SVT_NO_VERIFY; |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 336 | irte->svt = svt; |
| 337 | irte->sq = sq; |
| 338 | irte->sid = sid; |
| 339 | } |
| 340 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 341 | static int set_ioapic_sid(struct irte *irte, int apic) |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 342 | { |
| 343 | int i; |
| 344 | u16 sid = 0; |
| 345 | |
| 346 | if (!irte) |
| 347 | return -1; |
| 348 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 349 | down_read(&dmar_global_lock); |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 350 | for (i = 0; i < MAX_IO_APICS; i++) { |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 351 | if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) { |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 352 | sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn; |
| 353 | break; |
| 354 | } |
| 355 | } |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 356 | up_read(&dmar_global_lock); |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 357 | |
| 358 | if (sid == 0) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 359 | pr_warn("Failed to set source-id of IOAPIC (%d)\n", apic); |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 360 | return -1; |
| 361 | } |
| 362 | |
Jiang Liu | 2fe2c60 | 2014-01-06 14:18:17 +0800 | [diff] [blame] | 363 | set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid); |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 368 | static int set_hpet_sid(struct irte *irte, u8 id) |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 369 | { |
| 370 | int i; |
| 371 | u16 sid = 0; |
| 372 | |
| 373 | if (!irte) |
| 374 | return -1; |
| 375 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 376 | down_read(&dmar_global_lock); |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 377 | for (i = 0; i < MAX_HPET_TBS; i++) { |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 378 | if (ir_hpet[i].iommu && ir_hpet[i].id == id) { |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 379 | sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn; |
| 380 | break; |
| 381 | } |
| 382 | } |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 383 | up_read(&dmar_global_lock); |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 384 | |
| 385 | if (sid == 0) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 386 | pr_warn("Failed to set source-id of HPET block (%d)\n", id); |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Should really use SQ_ALL_16. Some platforms are broken. |
| 392 | * While we figure out the right quirks for these broken platforms, use |
| 393 | * SQ_13_IGNORE_3 for now. |
| 394 | */ |
| 395 | set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid); |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 400 | struct set_msi_sid_data { |
| 401 | struct pci_dev *pdev; |
| 402 | u16 alias; |
| 403 | }; |
| 404 | |
| 405 | static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque) |
| 406 | { |
| 407 | struct set_msi_sid_data *data = opaque; |
| 408 | |
| 409 | data->pdev = pdev; |
| 410 | data->alias = alias; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Joerg Roedel | 263b5e8 | 2012-03-30 11:47:06 -0700 | [diff] [blame] | 415 | static int set_msi_sid(struct irte *irte, struct pci_dev *dev) |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 416 | { |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 417 | struct set_msi_sid_data data; |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 418 | |
| 419 | if (!irte || !dev) |
| 420 | return -1; |
| 421 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 422 | pci_for_each_dma_alias(dev, set_msi_sid_cb, &data); |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 423 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 424 | /* |
| 425 | * DMA alias provides us with a PCI device and alias. The only case |
| 426 | * where the it will return an alias on a different bus than the |
| 427 | * device is the case of a PCIe-to-PCI bridge, where the alias is for |
| 428 | * the subordinate bus. In this case we can only verify the bus. |
| 429 | * |
| 430 | * If the alias device is on a different bus than our source device |
| 431 | * then we have a topology based alias, use it. |
| 432 | * |
| 433 | * Otherwise, the alias is for a device DMA quirk and we cannot |
| 434 | * assume that MSI uses the same requester ID. Therefore use the |
| 435 | * original device. |
| 436 | */ |
| 437 | if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number) |
| 438 | set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16, |
| 439 | PCI_DEVID(PCI_BUS_NUM(data.alias), |
| 440 | dev->bus->number)); |
| 441 | else if (data.pdev->bus->number != dev->bus->number) |
| 442 | set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias); |
| 443 | else |
| 444 | set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, |
| 445 | PCI_DEVID(dev->bus->number, dev->devfn)); |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
Joerg Roedel | af3b358 | 2015-06-12 15:00:21 +0200 | [diff] [blame] | 450 | static int iommu_load_old_irte(struct intel_iommu *iommu) |
| 451 | { |
| 452 | struct irte *old_ir_table; |
| 453 | phys_addr_t irt_phys; |
Joerg Roedel | 7c3c987 | 2015-06-12 15:06:26 +0200 | [diff] [blame] | 454 | unsigned int i; |
Joerg Roedel | af3b358 | 2015-06-12 15:00:21 +0200 | [diff] [blame] | 455 | size_t size; |
| 456 | u64 irta; |
| 457 | |
| 458 | if (!is_kdump_kernel()) { |
| 459 | pr_warn("IRQ remapping was enabled on %s but we are not in kdump mode\n", |
| 460 | iommu->name); |
| 461 | clear_ir_pre_enabled(iommu); |
| 462 | iommu_disable_irq_remapping(iommu); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | /* Check whether the old ir-table has the same size as ours */ |
| 467 | irta = dmar_readq(iommu->reg + DMAR_IRTA_REG); |
| 468 | if ((irta & INTR_REMAP_TABLE_REG_SIZE_MASK) |
| 469 | != INTR_REMAP_TABLE_REG_SIZE) |
| 470 | return -EINVAL; |
| 471 | |
| 472 | irt_phys = irta & VTD_PAGE_MASK; |
| 473 | size = INTR_REMAP_TABLE_ENTRIES*sizeof(struct irte); |
| 474 | |
| 475 | /* Map the old IR table */ |
| 476 | old_ir_table = ioremap_cache(irt_phys, size); |
| 477 | if (!old_ir_table) |
| 478 | return -ENOMEM; |
| 479 | |
| 480 | /* Copy data over */ |
| 481 | memcpy(iommu->ir_table->base, old_ir_table, size); |
| 482 | |
| 483 | __iommu_flush_cache(iommu, iommu->ir_table->base, size); |
| 484 | |
Joerg Roedel | 7c3c987 | 2015-06-12 15:06:26 +0200 | [diff] [blame] | 485 | /* |
| 486 | * Now check the table for used entries and mark those as |
| 487 | * allocated in the bitmap |
| 488 | */ |
| 489 | for (i = 0; i < INTR_REMAP_TABLE_ENTRIES; i++) { |
| 490 | if (iommu->ir_table->base[i].present) |
| 491 | bitmap_set(iommu->ir_table->bitmap, i, 1); |
| 492 | } |
| 493 | |
Joerg Roedel | af3b358 | 2015-06-12 15:00:21 +0200 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 498 | static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode) |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 499 | { |
Joerg Roedel | d4d1c0f | 2015-06-12 14:35:54 +0200 | [diff] [blame] | 500 | unsigned long flags; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 501 | u64 addr; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 502 | u32 sts; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 503 | |
| 504 | addr = virt_to_phys((void *)iommu->ir_table->base); |
| 505 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 506 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 507 | |
| 508 | dmar_writeq(iommu->reg + DMAR_IRTA_REG, |
| 509 | (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE); |
| 510 | |
| 511 | /* Set interrupt-remapping table pointer */ |
Jan Kiszka | f63ef69 | 2014-08-11 13:13:25 +0200 | [diff] [blame] | 512 | writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 513 | |
| 514 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 515 | readl, (sts & DMA_GSTS_IRTPS), sts); |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 516 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 517 | |
| 518 | /* |
Joerg Roedel | d4d1c0f | 2015-06-12 14:35:54 +0200 | [diff] [blame] | 519 | * Global invalidation of interrupt entry cache to make sure the |
| 520 | * hardware uses the new irq remapping table. |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 521 | */ |
| 522 | qi_global_iec(iommu); |
Joerg Roedel | d4d1c0f | 2015-06-12 14:35:54 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static void iommu_enable_irq_remapping(struct intel_iommu *iommu) |
| 526 | { |
| 527 | unsigned long flags; |
| 528 | u32 sts; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 529 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 530 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 531 | |
| 532 | /* Enable interrupt-remapping */ |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 533 | iommu->gcmd |= DMA_GCMD_IRE; |
Andy Lutomirski | af8d102 | 2013-02-01 14:57:43 -0800 | [diff] [blame] | 534 | iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */ |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 535 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 536 | |
| 537 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 538 | readl, (sts & DMA_GSTS_IRES), sts); |
| 539 | |
Andy Lutomirski | af8d102 | 2013-02-01 14:57:43 -0800 | [diff] [blame] | 540 | /* |
| 541 | * With CFI clear in the Global Command register, we should be |
| 542 | * protected from dangerous (i.e. compatibility) interrupts |
| 543 | * regardless of x2apic status. Check just to be sure. |
| 544 | */ |
| 545 | if (sts & DMA_GSTS_CFIS) |
| 546 | WARN(1, KERN_WARNING |
| 547 | "Compatibility-format IRQs enabled despite intr remapping;\n" |
| 548 | "you are vulnerable to IRQ injection.\n"); |
| 549 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 550 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 553 | static int intel_setup_irq_remapping(struct intel_iommu *iommu) |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 554 | { |
| 555 | struct ir_table *ir_table; |
| 556 | struct page *pages; |
Jiang Liu | 360eb3c | 2014-01-06 14:18:08 +0800 | [diff] [blame] | 557 | unsigned long *bitmap; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 558 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 559 | if (iommu->ir_table) |
| 560 | return 0; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 561 | |
Thomas Gleixner | e3a981d | 2015-01-07 15:31:30 +0800 | [diff] [blame] | 562 | ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL); |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 563 | if (!ir_table) |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 564 | return -ENOMEM; |
| 565 | |
Thomas Gleixner | e3a981d | 2015-01-07 15:31:30 +0800 | [diff] [blame] | 566 | pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO, |
Suresh Siddha | 824cd75 | 2009-10-02 11:01:23 -0700 | [diff] [blame] | 567 | INTR_REMAP_PAGE_ORDER); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 568 | |
| 569 | if (!pages) { |
Jiang Liu | 360eb3c | 2014-01-06 14:18:08 +0800 | [diff] [blame] | 570 | pr_err("IR%d: failed to allocate pages of order %d\n", |
| 571 | iommu->seq_id, INTR_REMAP_PAGE_ORDER); |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 572 | goto out_free_table; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Jiang Liu | 360eb3c | 2014-01-06 14:18:08 +0800 | [diff] [blame] | 575 | bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES), |
| 576 | sizeof(long), GFP_ATOMIC); |
| 577 | if (bitmap == NULL) { |
| 578 | pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id); |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 579 | goto out_free_pages; |
Jiang Liu | 360eb3c | 2014-01-06 14:18:08 +0800 | [diff] [blame] | 580 | } |
| 581 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 582 | ir_table->base = page_address(pages); |
Jiang Liu | 360eb3c | 2014-01-06 14:18:08 +0800 | [diff] [blame] | 583 | ir_table->bitmap = bitmap; |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 584 | iommu->ir_table = ir_table; |
Joerg Roedel | 9e4e49d | 2015-06-12 14:23:56 +0200 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * If the queued invalidation is already initialized, |
| 588 | * shouldn't disable it. |
| 589 | */ |
| 590 | if (!iommu->qi) { |
| 591 | /* |
| 592 | * Clear previous faults. |
| 593 | */ |
| 594 | dmar_fault(-1, iommu); |
| 595 | dmar_disable_qi(iommu); |
| 596 | |
| 597 | if (dmar_enable_qi(iommu)) { |
| 598 | pr_err("Failed to enable queued invalidation\n"); |
| 599 | goto out_free_bitmap; |
| 600 | } |
| 601 | } |
| 602 | |
Joerg Roedel | af3b358 | 2015-06-12 15:00:21 +0200 | [diff] [blame] | 603 | init_ir_status(iommu); |
| 604 | |
| 605 | if (ir_pre_enabled(iommu)) { |
| 606 | if (iommu_load_old_irte(iommu)) |
| 607 | pr_err("Failed to copy IR table for %s from previous kernel\n", |
| 608 | iommu->name); |
| 609 | else |
| 610 | pr_info("Copied IR table for %s from previous kernel\n", |
| 611 | iommu->name); |
| 612 | } |
| 613 | |
Joerg Roedel | d4d1c0f | 2015-06-12 14:35:54 +0200 | [diff] [blame] | 614 | iommu_set_irq_remapping(iommu, eim_mode); |
| 615 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 616 | return 0; |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 617 | |
Joerg Roedel | 9e4e49d | 2015-06-12 14:23:56 +0200 | [diff] [blame] | 618 | out_free_bitmap: |
| 619 | kfree(bitmap); |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 620 | out_free_pages: |
| 621 | __free_pages(pages, INTR_REMAP_PAGE_ORDER); |
| 622 | out_free_table: |
| 623 | kfree(ir_table); |
Joerg Roedel | 9e4e49d | 2015-06-12 14:23:56 +0200 | [diff] [blame] | 624 | |
| 625 | iommu->ir_table = NULL; |
| 626 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 627 | return -ENOMEM; |
| 628 | } |
| 629 | |
| 630 | static void intel_teardown_irq_remapping(struct intel_iommu *iommu) |
| 631 | { |
| 632 | if (iommu && iommu->ir_table) { |
| 633 | free_pages((unsigned long)iommu->ir_table->base, |
| 634 | INTR_REMAP_PAGE_ORDER); |
| 635 | kfree(iommu->ir_table->bitmap); |
| 636 | kfree(iommu->ir_table); |
| 637 | iommu->ir_table = NULL; |
| 638 | } |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 641 | /* |
| 642 | * Disable Interrupt Remapping. |
| 643 | */ |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 644 | static void iommu_disable_irq_remapping(struct intel_iommu *iommu) |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 645 | { |
| 646 | unsigned long flags; |
| 647 | u32 sts; |
| 648 | |
| 649 | if (!ecap_ir_support(iommu->ecap)) |
| 650 | return; |
| 651 | |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 652 | /* |
| 653 | * global invalidation of interrupt entry cache before disabling |
| 654 | * interrupt-remapping. |
| 655 | */ |
| 656 | qi_global_iec(iommu); |
| 657 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 658 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 659 | |
| 660 | sts = dmar_readq(iommu->reg + DMAR_GSTS_REG); |
| 661 | if (!(sts & DMA_GSTS_IRES)) |
| 662 | goto end; |
| 663 | |
| 664 | iommu->gcmd &= ~DMA_GCMD_IRE; |
| 665 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 666 | |
| 667 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 668 | readl, !(sts & DMA_GSTS_IRES), sts); |
| 669 | |
| 670 | end: |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 671 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Suresh Siddha | 41750d3 | 2011-08-23 17:05:18 -0700 | [diff] [blame] | 674 | static int __init dmar_x2apic_optout(void) |
| 675 | { |
| 676 | struct acpi_table_dmar *dmar; |
| 677 | dmar = (struct acpi_table_dmar *)dmar_tbl; |
| 678 | if (!dmar || no_x2apic_optout) |
| 679 | return 0; |
| 680 | return dmar->flags & DMAR_X2APIC_OPT_OUT; |
| 681 | } |
| 682 | |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 683 | static void __init intel_cleanup_irq_remapping(void) |
| 684 | { |
| 685 | struct dmar_drhd_unit *drhd; |
| 686 | struct intel_iommu *iommu; |
| 687 | |
| 688 | for_each_iommu(iommu, drhd) { |
| 689 | if (ecap_ir_support(iommu->ecap)) { |
| 690 | iommu_disable_irq_remapping(iommu); |
| 691 | intel_teardown_irq_remapping(iommu); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (x2apic_supported()) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 696 | pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n"); |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static int __init intel_prepare_irq_remapping(void) |
| 700 | { |
| 701 | struct dmar_drhd_unit *drhd; |
| 702 | struct intel_iommu *iommu; |
Joerg Roedel | 23256d0 | 2015-06-12 14:15:49 +0200 | [diff] [blame] | 703 | int eim = 0; |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 704 | |
Jiang Liu | 2966d95 | 2015-01-07 15:31:35 +0800 | [diff] [blame] | 705 | if (irq_remap_broken) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 706 | pr_warn("This system BIOS has enabled interrupt remapping\n" |
Jiang Liu | 2966d95 | 2015-01-07 15:31:35 +0800 | [diff] [blame] | 707 | "on a chipset that contains an erratum making that\n" |
| 708 | "feature unstable. To maintain system stability\n" |
| 709 | "interrupt remapping is being disabled. Please\n" |
| 710 | "contact your BIOS vendor for an update\n"); |
| 711 | add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); |
Jiang Liu | 2966d95 | 2015-01-07 15:31:35 +0800 | [diff] [blame] | 712 | return -ENODEV; |
| 713 | } |
| 714 | |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 715 | if (dmar_table_init() < 0) |
Jiang Liu | 2966d95 | 2015-01-07 15:31:35 +0800 | [diff] [blame] | 716 | return -ENODEV; |
| 717 | |
| 718 | if (!dmar_ir_support()) |
| 719 | return -ENODEV; |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 720 | |
| 721 | if (parse_ioapics_under_ir() != 1) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 722 | pr_info("Not enabling interrupt remapping\n"); |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 723 | goto error; |
| 724 | } |
| 725 | |
Joerg Roedel | 69cf1d8 | 2015-01-07 15:31:36 +0800 | [diff] [blame] | 726 | /* First make sure all IOMMUs support IRQ remapping */ |
Jiang Liu | 2966d95 | 2015-01-07 15:31:35 +0800 | [diff] [blame] | 727 | for_each_iommu(iommu, drhd) |
Joerg Roedel | 69cf1d8 | 2015-01-07 15:31:36 +0800 | [diff] [blame] | 728 | if (!ecap_ir_support(iommu->ecap)) |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 729 | goto error; |
Joerg Roedel | 69cf1d8 | 2015-01-07 15:31:36 +0800 | [diff] [blame] | 730 | |
Joerg Roedel | 23256d0 | 2015-06-12 14:15:49 +0200 | [diff] [blame] | 731 | /* Detect remapping mode: lapic or x2apic */ |
| 732 | if (x2apic_supported()) { |
| 733 | eim = !dmar_x2apic_optout(); |
| 734 | if (!eim) { |
| 735 | pr_info("x2apic is disabled because BIOS sets x2apic opt out bit."); |
| 736 | pr_info("Use 'intremap=no_x2apic_optout' to override the BIOS setting.\n"); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | for_each_iommu(iommu, drhd) { |
| 741 | if (eim && !ecap_eim_support(iommu->ecap)) { |
| 742 | pr_info("%s does not support EIM\n", iommu->name); |
| 743 | eim = 0; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | eim_mode = eim; |
| 748 | if (eim) |
| 749 | pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n"); |
| 750 | |
Joerg Roedel | 9e4e49d | 2015-06-12 14:23:56 +0200 | [diff] [blame] | 751 | /* Do the initializations early */ |
| 752 | for_each_iommu(iommu, drhd) { |
| 753 | if (intel_setup_irq_remapping(iommu)) { |
| 754 | pr_err("Failed to setup irq remapping for %s\n", |
| 755 | iommu->name); |
Joerg Roedel | 69cf1d8 | 2015-01-07 15:31:36 +0800 | [diff] [blame] | 756 | goto error; |
Joerg Roedel | 9e4e49d | 2015-06-12 14:23:56 +0200 | [diff] [blame] | 757 | } |
| 758 | } |
Joerg Roedel | 69cf1d8 | 2015-01-07 15:31:36 +0800 | [diff] [blame] | 759 | |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 760 | return 0; |
Jiang Liu | 2966d95 | 2015-01-07 15:31:35 +0800 | [diff] [blame] | 761 | |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 762 | error: |
| 763 | intel_cleanup_irq_remapping(); |
Jiang Liu | 2966d95 | 2015-01-07 15:31:35 +0800 | [diff] [blame] | 764 | return -ENODEV; |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 765 | } |
| 766 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 767 | static int __init intel_enable_irq_remapping(void) |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 768 | { |
| 769 | struct dmar_drhd_unit *drhd; |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 770 | struct intel_iommu *iommu; |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 771 | bool setup = false; |
Suresh Siddha | 41750d3 | 2011-08-23 17:05:18 -0700 | [diff] [blame] | 772 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 773 | /* |
| 774 | * Setup Interrupt-remapping for all the DRHD's now. |
| 775 | */ |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 776 | for_each_iommu(iommu, drhd) { |
Joerg Roedel | 571dbbd | 2015-06-12 15:15:34 +0200 | [diff] [blame^] | 777 | if (!ir_pre_enabled(iommu)) |
| 778 | iommu_enable_irq_remapping(iommu); |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 779 | setup = true; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | if (!setup) |
| 783 | goto error; |
| 784 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 785 | irq_remapping_enabled = 1; |
Joerg Roedel | afcc8a4 | 2012-09-26 12:44:36 +0200 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * VT-d has a different layout for IO-APIC entries when |
| 789 | * interrupt remapping is enabled. So it needs a special routine |
| 790 | * to print IO-APIC entries for debugging purposes too. |
| 791 | */ |
| 792 | x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries; |
| 793 | |
Joerg Roedel | 23256d0 | 2015-06-12 14:15:49 +0200 | [diff] [blame] | 794 | pr_info("Enabled IRQ remapping in %s mode\n", eim_mode ? "x2apic" : "xapic"); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 795 | |
Joerg Roedel | 23256d0 | 2015-06-12 14:15:49 +0200 | [diff] [blame] | 796 | return eim_mode ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 797 | |
| 798 | error: |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 799 | intel_cleanup_irq_remapping(); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 800 | return -1; |
| 801 | } |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 802 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 803 | static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope, |
| 804 | struct intel_iommu *iommu, |
| 805 | struct acpi_dmar_hardware_unit *drhd) |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 806 | { |
| 807 | struct acpi_dmar_pci_path *path; |
| 808 | u8 bus; |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 809 | int count, free = -1; |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 810 | |
| 811 | bus = scope->bus; |
| 812 | path = (struct acpi_dmar_pci_path *)(scope + 1); |
| 813 | count = (scope->length - sizeof(struct acpi_dmar_device_scope)) |
| 814 | / sizeof(struct acpi_dmar_pci_path); |
| 815 | |
| 816 | while (--count > 0) { |
| 817 | /* |
| 818 | * Access PCI directly due to the PCI |
| 819 | * subsystem isn't initialized yet. |
| 820 | */ |
Lv Zheng | fa5f508 | 2013-10-31 09:30:22 +0800 | [diff] [blame] | 821 | bus = read_pci_config_byte(bus, path->device, path->function, |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 822 | PCI_SECONDARY_BUS); |
| 823 | path++; |
| 824 | } |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 825 | |
| 826 | for (count = 0; count < MAX_HPET_TBS; count++) { |
| 827 | if (ir_hpet[count].iommu == iommu && |
| 828 | ir_hpet[count].id == scope->enumeration_id) |
| 829 | return 0; |
| 830 | else if (ir_hpet[count].iommu == NULL && free == -1) |
| 831 | free = count; |
| 832 | } |
| 833 | if (free == -1) { |
| 834 | pr_warn("Exceeded Max HPET blocks\n"); |
| 835 | return -ENOSPC; |
| 836 | } |
| 837 | |
| 838 | ir_hpet[free].iommu = iommu; |
| 839 | ir_hpet[free].id = scope->enumeration_id; |
| 840 | ir_hpet[free].bus = bus; |
| 841 | ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function); |
| 842 | pr_info("HPET id %d under DRHD base 0x%Lx\n", |
| 843 | scope->enumeration_id, drhd->address); |
| 844 | |
| 845 | return 0; |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 848 | static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope, |
| 849 | struct intel_iommu *iommu, |
| 850 | struct acpi_dmar_hardware_unit *drhd) |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 851 | { |
| 852 | struct acpi_dmar_pci_path *path; |
| 853 | u8 bus; |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 854 | int count, free = -1; |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 855 | |
| 856 | bus = scope->bus; |
| 857 | path = (struct acpi_dmar_pci_path *)(scope + 1); |
| 858 | count = (scope->length - sizeof(struct acpi_dmar_device_scope)) |
| 859 | / sizeof(struct acpi_dmar_pci_path); |
| 860 | |
| 861 | while (--count > 0) { |
| 862 | /* |
| 863 | * Access PCI directly due to the PCI |
| 864 | * subsystem isn't initialized yet. |
| 865 | */ |
Lv Zheng | fa5f508 | 2013-10-31 09:30:22 +0800 | [diff] [blame] | 866 | bus = read_pci_config_byte(bus, path->device, path->function, |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 867 | PCI_SECONDARY_BUS); |
| 868 | path++; |
| 869 | } |
| 870 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 871 | for (count = 0; count < MAX_IO_APICS; count++) { |
| 872 | if (ir_ioapic[count].iommu == iommu && |
| 873 | ir_ioapic[count].id == scope->enumeration_id) |
| 874 | return 0; |
| 875 | else if (ir_ioapic[count].iommu == NULL && free == -1) |
| 876 | free = count; |
| 877 | } |
| 878 | if (free == -1) { |
| 879 | pr_warn("Exceeded Max IO APICS\n"); |
| 880 | return -ENOSPC; |
| 881 | } |
| 882 | |
| 883 | ir_ioapic[free].bus = bus; |
| 884 | ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function); |
| 885 | ir_ioapic[free].iommu = iommu; |
| 886 | ir_ioapic[free].id = scope->enumeration_id; |
| 887 | pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n", |
| 888 | scope->enumeration_id, drhd->address, iommu->seq_id); |
| 889 | |
| 890 | return 0; |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 891 | } |
| 892 | |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 893 | static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header, |
| 894 | struct intel_iommu *iommu) |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 895 | { |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 896 | int ret = 0; |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 897 | struct acpi_dmar_hardware_unit *drhd; |
| 898 | struct acpi_dmar_device_scope *scope; |
| 899 | void *start, *end; |
| 900 | |
| 901 | drhd = (struct acpi_dmar_hardware_unit *)header; |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 902 | start = (void *)(drhd + 1); |
| 903 | end = ((void *)drhd) + header->length; |
| 904 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 905 | while (start < end && ret == 0) { |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 906 | scope = start; |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 907 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) |
| 908 | ret = ir_parse_one_ioapic_scope(scope, iommu, drhd); |
| 909 | else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) |
| 910 | ret = ir_parse_one_hpet_scope(scope, iommu, drhd); |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 911 | start += scope->length; |
| 912 | } |
| 913 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 914 | return ret; |
| 915 | } |
| 916 | |
| 917 | static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu) |
| 918 | { |
| 919 | int i; |
| 920 | |
| 921 | for (i = 0; i < MAX_HPET_TBS; i++) |
| 922 | if (ir_hpet[i].iommu == iommu) |
| 923 | ir_hpet[i].iommu = NULL; |
| 924 | |
| 925 | for (i = 0; i < MAX_IO_APICS; i++) |
| 926 | if (ir_ioapic[i].iommu == iommu) |
| 927 | ir_ioapic[i].iommu = NULL; |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /* |
| 931 | * Finds the assocaition between IOAPIC's and its Interrupt-remapping |
| 932 | * hardware unit. |
| 933 | */ |
Jiang Liu | 694835d | 2014-01-06 14:18:16 +0800 | [diff] [blame] | 934 | static int __init parse_ioapics_under_ir(void) |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 935 | { |
| 936 | struct dmar_drhd_unit *drhd; |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 937 | struct intel_iommu *iommu; |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 938 | bool ir_supported = false; |
Seth Forshee | 32ab31e | 2012-08-08 08:27:03 -0500 | [diff] [blame] | 939 | int ioapic_idx; |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 940 | |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 941 | for_each_iommu(iommu, drhd) |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 942 | if (ecap_ir_support(iommu->ecap)) { |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 943 | if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu)) |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 944 | return -1; |
| 945 | |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 946 | ir_supported = true; |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 947 | } |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 948 | |
Seth Forshee | 32ab31e | 2012-08-08 08:27:03 -0500 | [diff] [blame] | 949 | if (!ir_supported) |
| 950 | return 0; |
| 951 | |
| 952 | for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) { |
| 953 | int ioapic_id = mpc_ioapic_id(ioapic_idx); |
| 954 | if (!map_ioapic_to_ir(ioapic_id)) { |
| 955 | pr_err(FW_BUG "ioapic %d has no mapping iommu, " |
| 956 | "interrupt remapping will be disabled\n", |
| 957 | ioapic_id); |
| 958 | return -1; |
| 959 | } |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Seth Forshee | 32ab31e | 2012-08-08 08:27:03 -0500 | [diff] [blame] | 962 | return 1; |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 963 | } |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 964 | |
Rashika Kheria | 6a7885c | 2013-12-18 12:04:27 +0530 | [diff] [blame] | 965 | static int __init ir_dev_scope_init(void) |
Suresh Siddha | c2c7286 | 2011-08-23 17:05:19 -0700 | [diff] [blame] | 966 | { |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 967 | int ret; |
| 968 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 969 | if (!irq_remapping_enabled) |
Suresh Siddha | c2c7286 | 2011-08-23 17:05:19 -0700 | [diff] [blame] | 970 | return 0; |
| 971 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 972 | down_write(&dmar_global_lock); |
| 973 | ret = dmar_dev_scope_init(); |
| 974 | up_write(&dmar_global_lock); |
| 975 | |
| 976 | return ret; |
Suresh Siddha | c2c7286 | 2011-08-23 17:05:19 -0700 | [diff] [blame] | 977 | } |
| 978 | rootfs_initcall(ir_dev_scope_init); |
| 979 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 980 | static void disable_irq_remapping(void) |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 981 | { |
| 982 | struct dmar_drhd_unit *drhd; |
| 983 | struct intel_iommu *iommu = NULL; |
| 984 | |
| 985 | /* |
| 986 | * Disable Interrupt-remapping for all the DRHD's now. |
| 987 | */ |
| 988 | for_each_iommu(iommu, drhd) { |
| 989 | if (!ecap_ir_support(iommu->ecap)) |
| 990 | continue; |
| 991 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 992 | iommu_disable_irq_remapping(iommu); |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 996 | static int reenable_irq_remapping(int eim) |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 997 | { |
| 998 | struct dmar_drhd_unit *drhd; |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 999 | bool setup = false; |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 1000 | struct intel_iommu *iommu = NULL; |
| 1001 | |
| 1002 | for_each_iommu(iommu, drhd) |
| 1003 | if (iommu->qi) |
| 1004 | dmar_reenable_qi(iommu); |
| 1005 | |
| 1006 | /* |
| 1007 | * Setup Interrupt-remapping for all the DRHD's now. |
| 1008 | */ |
| 1009 | for_each_iommu(iommu, drhd) { |
| 1010 | if (!ecap_ir_support(iommu->ecap)) |
| 1011 | continue; |
| 1012 | |
| 1013 | /* Set up interrupt remapping for iommu.*/ |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 1014 | iommu_set_irq_remapping(iommu, eim); |
Joerg Roedel | d4d1c0f | 2015-06-12 14:35:54 +0200 | [diff] [blame] | 1015 | iommu_enable_irq_remapping(iommu); |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 1016 | setup = true; |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | if (!setup) |
| 1020 | goto error; |
| 1021 | |
| 1022 | return 0; |
| 1023 | |
| 1024 | error: |
| 1025 | /* |
| 1026 | * handle error condition gracefully here! |
| 1027 | */ |
| 1028 | return -1; |
| 1029 | } |
| 1030 | |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 1031 | static void prepare_irte(struct irte *irte, int vector, |
| 1032 | unsigned int dest) |
| 1033 | { |
| 1034 | memset(irte, 0, sizeof(*irte)); |
| 1035 | |
| 1036 | irte->present = 1; |
| 1037 | irte->dst_mode = apic->irq_dest_mode; |
| 1038 | /* |
| 1039 | * Trigger mode in the IRTE will always be edge, and for IO-APIC, the |
| 1040 | * actual level or edge trigger will be setup in the IO-APIC |
| 1041 | * RTE. This will help simplify level triggered irq migration. |
| 1042 | * For more details, see the comments (in io_apic.c) explainig IO-APIC |
| 1043 | * irq migration in the presence of interrupt-remapping. |
| 1044 | */ |
| 1045 | irte->trigger_mode = 0; |
| 1046 | irte->dlvry_mode = apic->irq_delivery_mode; |
| 1047 | irte->vector = vector; |
| 1048 | irte->dest_id = IRTE_DEST(dest); |
| 1049 | irte->redir_hint = 1; |
| 1050 | } |
| 1051 | |
| 1052 | static int intel_setup_ioapic_entry(int irq, |
| 1053 | struct IO_APIC_route_entry *route_entry, |
| 1054 | unsigned int destination, int vector, |
| 1055 | struct io_apic_irq_attr *attr) |
| 1056 | { |
| 1057 | int ioapic_id = mpc_ioapic_id(attr->ioapic); |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1058 | struct intel_iommu *iommu; |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 1059 | struct IR_IO_APIC_route_entry *entry; |
| 1060 | struct irte irte; |
| 1061 | int index; |
| 1062 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1063 | down_read(&dmar_global_lock); |
| 1064 | iommu = map_ioapic_to_ir(ioapic_id); |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 1065 | if (!iommu) { |
| 1066 | pr_warn("No mapping iommu for ioapic %d\n", ioapic_id); |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1067 | index = -ENODEV; |
| 1068 | } else { |
| 1069 | index = alloc_irte(iommu, irq, 1); |
| 1070 | if (index < 0) { |
| 1071 | pr_warn("Failed to allocate IRTE for ioapic %d\n", |
| 1072 | ioapic_id); |
| 1073 | index = -ENOMEM; |
| 1074 | } |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 1075 | } |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1076 | up_read(&dmar_global_lock); |
| 1077 | if (index < 0) |
| 1078 | return index; |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 1079 | |
| 1080 | prepare_irte(&irte, vector, destination); |
| 1081 | |
| 1082 | /* Set source-id of interrupt request */ |
| 1083 | set_ioapic_sid(&irte, ioapic_id); |
| 1084 | |
| 1085 | modify_irte(irq, &irte); |
| 1086 | |
| 1087 | apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: " |
| 1088 | "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d " |
| 1089 | "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X " |
| 1090 | "Avail:%X Vector:%02X Dest:%08X " |
| 1091 | "SID:%04X SQ:%X SVT:%X)\n", |
| 1092 | attr->ioapic, irte.present, irte.fpd, irte.dst_mode, |
| 1093 | irte.redir_hint, irte.trigger_mode, irte.dlvry_mode, |
| 1094 | irte.avail, irte.vector, irte.dest_id, |
| 1095 | irte.sid, irte.sq, irte.svt); |
| 1096 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1097 | entry = (struct IR_IO_APIC_route_entry *)route_entry; |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 1098 | memset(entry, 0, sizeof(*entry)); |
| 1099 | |
| 1100 | entry->index2 = (index >> 15) & 0x1; |
| 1101 | entry->zero = 0; |
| 1102 | entry->format = 1; |
| 1103 | entry->index = (index & 0x7fff); |
| 1104 | /* |
| 1105 | * IO-APIC RTE will be configured with virtual vector. |
| 1106 | * irq handler will do the explicit EOI to the io-apic. |
| 1107 | */ |
| 1108 | entry->vector = attr->ioapic_pin; |
| 1109 | entry->mask = 0; /* enable IRQ */ |
| 1110 | entry->trigger = attr->trigger; |
| 1111 | entry->polarity = attr->polarity; |
| 1112 | |
| 1113 | /* Mask level triggered irqs. |
| 1114 | * Use IRQ_DELAYED_DISABLE for edge triggered irqs. |
| 1115 | */ |
| 1116 | if (attr->trigger) |
| 1117 | entry->mask = 1; |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
Joerg Roedel | 4c1bad6 | 2012-03-30 11:47:03 -0700 | [diff] [blame] | 1122 | /* |
| 1123 | * Migrate the IO-APIC irq in the presence of intr-remapping. |
| 1124 | * |
| 1125 | * For both level and edge triggered, irq migration is a simple atomic |
| 1126 | * update(of vector and cpu destination) of IRTE and flush the hardware cache. |
| 1127 | * |
| 1128 | * For level triggered, we eliminate the io-apic RTE modification (with the |
| 1129 | * updated vector information), by using a virtual vector (io-apic pin number). |
| 1130 | * Real vector that is used for interrupting cpu will be coming from |
| 1131 | * the interrupt-remapping table entry. |
| 1132 | * |
| 1133 | * As the migration is a simple atomic update of IRTE, the same mechanism |
| 1134 | * is used to migrate MSI irq's in the presence of interrupt-remapping. |
| 1135 | */ |
| 1136 | static int |
| 1137 | intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask, |
| 1138 | bool force) |
| 1139 | { |
Jiang Liu | 91411da | 2014-10-27 16:12:09 +0800 | [diff] [blame] | 1140 | struct irq_cfg *cfg = irqd_cfg(data); |
Joerg Roedel | 4c1bad6 | 2012-03-30 11:47:03 -0700 | [diff] [blame] | 1141 | unsigned int dest, irq = data->irq; |
| 1142 | struct irte irte; |
Alexander Gordeev | ff16432 | 2012-06-07 15:15:59 +0200 | [diff] [blame] | 1143 | int err; |
Joerg Roedel | 4c1bad6 | 2012-03-30 11:47:03 -0700 | [diff] [blame] | 1144 | |
Suresh Siddha | 7eb9ae0 | 2012-06-14 18:28:49 -0700 | [diff] [blame] | 1145 | if (!config_enabled(CONFIG_SMP)) |
| 1146 | return -EINVAL; |
| 1147 | |
Joerg Roedel | 4c1bad6 | 2012-03-30 11:47:03 -0700 | [diff] [blame] | 1148 | if (!cpumask_intersects(mask, cpu_online_mask)) |
| 1149 | return -EINVAL; |
| 1150 | |
| 1151 | if (get_irte(irq, &irte)) |
| 1152 | return -EBUSY; |
| 1153 | |
Alexander Gordeev | ff16432 | 2012-06-07 15:15:59 +0200 | [diff] [blame] | 1154 | err = assign_irq_vector(irq, cfg, mask); |
| 1155 | if (err) |
| 1156 | return err; |
Joerg Roedel | 4c1bad6 | 2012-03-30 11:47:03 -0700 | [diff] [blame] | 1157 | |
Alexander Gordeev | ff16432 | 2012-06-07 15:15:59 +0200 | [diff] [blame] | 1158 | err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest); |
| 1159 | if (err) { |
Dan Carpenter | ed88bed | 2012-06-12 19:26:33 +0300 | [diff] [blame] | 1160 | if (assign_irq_vector(irq, cfg, data->affinity)) |
Alexander Gordeev | ff16432 | 2012-06-07 15:15:59 +0200 | [diff] [blame] | 1161 | pr_err("Failed to recover vector for irq %d\n", irq); |
| 1162 | return err; |
| 1163 | } |
Joerg Roedel | 4c1bad6 | 2012-03-30 11:47:03 -0700 | [diff] [blame] | 1164 | |
| 1165 | irte.vector = cfg->vector; |
| 1166 | irte.dest_id = IRTE_DEST(dest); |
| 1167 | |
| 1168 | /* |
| 1169 | * Atomically updates the IRTE with the new destination, vector |
| 1170 | * and flushes the interrupt entry cache. |
| 1171 | */ |
| 1172 | modify_irte(irq, &irte); |
| 1173 | |
| 1174 | /* |
| 1175 | * After this point, all the interrupts will start arriving |
| 1176 | * at the new destination. So, time to cleanup the previous |
| 1177 | * vector allocation. |
| 1178 | */ |
| 1179 | if (cfg->move_in_progress) |
| 1180 | send_cleanup_vector(cfg); |
| 1181 | |
| 1182 | cpumask_copy(data->affinity, mask); |
| 1183 | return 0; |
| 1184 | } |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 1185 | |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1186 | static void intel_compose_msi_msg(struct pci_dev *pdev, |
| 1187 | unsigned int irq, unsigned int dest, |
| 1188 | struct msi_msg *msg, u8 hpet_id) |
| 1189 | { |
| 1190 | struct irq_cfg *cfg; |
| 1191 | struct irte irte; |
Suresh Siddha | c558df4 | 2012-05-08 00:08:54 -0700 | [diff] [blame] | 1192 | u16 sub_handle = 0; |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1193 | int ir_index; |
| 1194 | |
Jiang Liu | 91411da | 2014-10-27 16:12:09 +0800 | [diff] [blame] | 1195 | cfg = irq_cfg(irq); |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1196 | |
| 1197 | ir_index = map_irq_to_irte_handle(irq, &sub_handle); |
| 1198 | BUG_ON(ir_index == -1); |
| 1199 | |
| 1200 | prepare_irte(&irte, cfg->vector, dest); |
| 1201 | |
| 1202 | /* Set source-id of interrupt request */ |
| 1203 | if (pdev) |
| 1204 | set_msi_sid(&irte, pdev); |
| 1205 | else |
| 1206 | set_hpet_sid(&irte, hpet_id); |
| 1207 | |
| 1208 | modify_irte(irq, &irte); |
| 1209 | |
| 1210 | msg->address_hi = MSI_ADDR_BASE_HI; |
| 1211 | msg->data = sub_handle; |
| 1212 | msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT | |
| 1213 | MSI_ADDR_IR_SHV | |
| 1214 | MSI_ADDR_IR_INDEX1(ir_index) | |
| 1215 | MSI_ADDR_IR_INDEX2(ir_index); |
| 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Map the PCI dev to the corresponding remapping hardware unit |
| 1220 | * and allocate 'nvec' consecutive interrupt-remapping table entries |
| 1221 | * in it. |
| 1222 | */ |
| 1223 | static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec) |
| 1224 | { |
| 1225 | struct intel_iommu *iommu; |
| 1226 | int index; |
| 1227 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1228 | down_read(&dmar_global_lock); |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1229 | iommu = map_dev_to_ir(dev); |
| 1230 | if (!iommu) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1231 | pr_err("Unable to map PCI %s to iommu\n", pci_name(dev)); |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1232 | index = -ENOENT; |
| 1233 | } else { |
| 1234 | index = alloc_irte(iommu, irq, nvec); |
| 1235 | if (index < 0) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1236 | pr_err("Unable to allocate %d IRTE for PCI %s\n", |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1237 | nvec, pci_name(dev)); |
| 1238 | index = -ENOSPC; |
| 1239 | } |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1240 | } |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1241 | up_read(&dmar_global_lock); |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1242 | |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1243 | return index; |
| 1244 | } |
| 1245 | |
| 1246 | static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq, |
| 1247 | int index, int sub_handle) |
| 1248 | { |
| 1249 | struct intel_iommu *iommu; |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1250 | int ret = -ENOENT; |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1251 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1252 | down_read(&dmar_global_lock); |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1253 | iommu = map_dev_to_ir(pdev); |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1254 | if (iommu) { |
| 1255 | /* |
| 1256 | * setup the mapping between the irq and the IRTE |
| 1257 | * base index, the sub_handle pointing to the |
| 1258 | * appropriate interrupt remap table entry. |
| 1259 | */ |
| 1260 | set_irte_irq(irq, iommu, index, sub_handle); |
| 1261 | ret = 0; |
| 1262 | } |
| 1263 | up_read(&dmar_global_lock); |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1264 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1265 | return ret; |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Yijing Wang | 5fc24d8 | 2014-09-17 17:32:19 +0800 | [diff] [blame] | 1268 | static int intel_alloc_hpet_msi(unsigned int irq, unsigned int id) |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1269 | { |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1270 | int ret = -1; |
| 1271 | struct intel_iommu *iommu; |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1272 | int index; |
| 1273 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1274 | down_read(&dmar_global_lock); |
| 1275 | iommu = map_hpet_to_ir(id); |
| 1276 | if (iommu) { |
| 1277 | index = alloc_irte(iommu, irq, 1); |
| 1278 | if (index >= 0) |
| 1279 | ret = 0; |
| 1280 | } |
| 1281 | up_read(&dmar_global_lock); |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1282 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 1283 | return ret; |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 1286 | struct irq_remap_ops intel_irq_remap_ops = { |
Thomas Gleixner | 1119030 | 2015-01-07 15:31:29 +0800 | [diff] [blame] | 1287 | .prepare = intel_prepare_irq_remapping, |
Suresh Siddha | 95a02e9 | 2012-03-30 11:47:07 -0700 | [diff] [blame] | 1288 | .enable = intel_enable_irq_remapping, |
| 1289 | .disable = disable_irq_remapping, |
| 1290 | .reenable = reenable_irq_remapping, |
Joerg Roedel | 4f3d8b6 | 2012-03-30 11:47:01 -0700 | [diff] [blame] | 1291 | .enable_faulting = enable_drhd_fault_handling, |
Joerg Roedel | 0c3f173 | 2012-03-30 11:47:02 -0700 | [diff] [blame] | 1292 | .setup_ioapic_entry = intel_setup_ioapic_entry, |
Joerg Roedel | 4c1bad6 | 2012-03-30 11:47:03 -0700 | [diff] [blame] | 1293 | .set_affinity = intel_ioapic_set_affinity, |
Joerg Roedel | 9d619f6 | 2012-03-30 11:47:04 -0700 | [diff] [blame] | 1294 | .free_irq = free_irte, |
Joerg Roedel | 5e2b930 | 2012-03-30 11:47:05 -0700 | [diff] [blame] | 1295 | .compose_msi_msg = intel_compose_msi_msg, |
| 1296 | .msi_alloc_irq = intel_msi_alloc_irq, |
| 1297 | .msi_setup_irq = intel_msi_setup_irq, |
Yijing Wang | 5fc24d8 | 2014-09-17 17:32:19 +0800 | [diff] [blame] | 1298 | .alloc_hpet_msi = intel_alloc_hpet_msi, |
Joerg Roedel | 736baef | 2012-03-30 11:47:00 -0700 | [diff] [blame] | 1299 | }; |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 1300 | |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 1301 | /* |
| 1302 | * Support of Interrupt Remapping Unit Hotplug |
| 1303 | */ |
| 1304 | static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu) |
| 1305 | { |
| 1306 | int ret; |
| 1307 | int eim = x2apic_enabled(); |
| 1308 | |
| 1309 | if (eim && !ecap_eim_support(iommu->ecap)) { |
| 1310 | pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n", |
| 1311 | iommu->reg_phys, iommu->ecap); |
| 1312 | return -ENODEV; |
| 1313 | } |
| 1314 | |
| 1315 | if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) { |
| 1316 | pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n", |
| 1317 | iommu->reg_phys); |
| 1318 | return -ENODEV; |
| 1319 | } |
| 1320 | |
| 1321 | /* TODO: check all IOAPICs are covered by IOMMU */ |
| 1322 | |
| 1323 | /* Setup Interrupt-remapping now. */ |
| 1324 | ret = intel_setup_irq_remapping(iommu); |
| 1325 | if (ret) { |
Joerg Roedel | 9e4e49d | 2015-06-12 14:23:56 +0200 | [diff] [blame] | 1326 | pr_err("Failed to setup irq remapping for %s\n", |
| 1327 | iommu->name); |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 1328 | intel_teardown_irq_remapping(iommu); |
| 1329 | ir_remove_ioapic_hpet_scope(iommu); |
Joerg Roedel | 9e4e49d | 2015-06-12 14:23:56 +0200 | [diff] [blame] | 1330 | } else { |
Joerg Roedel | d4d1c0f | 2015-06-12 14:35:54 +0200 | [diff] [blame] | 1331 | iommu_enable_irq_remapping(iommu); |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | return ret; |
| 1335 | } |
| 1336 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 1337 | int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert) |
| 1338 | { |
Jiang Liu | a7a3dad | 2014-11-09 22:48:00 +0800 | [diff] [blame] | 1339 | int ret = 0; |
| 1340 | struct intel_iommu *iommu = dmaru->iommu; |
| 1341 | |
| 1342 | if (!irq_remapping_enabled) |
| 1343 | return 0; |
| 1344 | if (iommu == NULL) |
| 1345 | return -EINVAL; |
| 1346 | if (!ecap_ir_support(iommu->ecap)) |
| 1347 | return 0; |
| 1348 | |
| 1349 | if (insert) { |
| 1350 | if (!iommu->ir_table) |
| 1351 | ret = dmar_ir_add(dmaru, iommu); |
| 1352 | } else { |
| 1353 | if (iommu->ir_table) { |
| 1354 | if (!bitmap_empty(iommu->ir_table->bitmap, |
| 1355 | INTR_REMAP_TABLE_ENTRIES)) { |
| 1356 | ret = -EBUSY; |
| 1357 | } else { |
| 1358 | iommu_disable_irq_remapping(iommu); |
| 1359 | intel_teardown_irq_remapping(iommu); |
| 1360 | ir_remove_ioapic_hpet_scope(iommu); |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | return ret; |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 1366 | } |