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