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