blob: 3aa9b5c347e4e457d33abfafa5e6a63fda156e82 [file] [log] [blame]
Yinghai Lu5aeecaf2008-08-19 20:49:59 -07001#include <linux/interrupt.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -07002#include <linux/dmar.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07003#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07005#include <linux/jiffies.h>
Suresh Siddha20f30972009-08-04 12:07:08 -07006#include <linux/hpet.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07007#include <linux/pci.h>
Suresh Siddhab6fcb332008-07-10 11:16:44 -07008#include <linux/irq.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -07009#include <asm/io_apic.h>
Yinghai Lu17483a12008-12-12 13:14:18 -080010#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053011#include <asm/cpu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030012#include <linux/intel-iommu.h>
Alexander Beregalov46f06b722009-04-06 16:45:28 +010013#include <acpi/acpi.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070014#include <asm/irq_remapping.h>
Weidong Hanf007e992009-05-23 00:41:15 +080015#include <asm/pci-direct.h>
Joerg Roedel5e2b9302012-03-30 11:47:05 -070016#include <asm/msidef.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070017
Suresh Siddha8a8f4222012-03-30 11:47:08 -070018#include "irq_remapping.h"
Joerg Roedel736baef2012-03-30 11:47:00 -070019
Joerg Roedeleef93fd2012-03-30 11:46:59 -070020struct 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
27struct 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)
Joerg Roedel0c3f1732012-03-30 11:47:02 -070035#define IRTE_DEST(dest) ((x2apic_mode) ? dest : dest << 8)
Joerg Roedeleef93fd2012-03-30 11:46:59 -070036
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070037static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070038static struct hpet_scope ir_hpet[MAX_HPET_TBS];
39static int ir_ioapic_num, ir_hpet_num;
Chris Wrightd1423d52010-07-20 11:06:49 -070040
Thomas Gleixner96f8e982011-07-19 16:28:19 +020041static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Thomas Gleixnerd585d062010-10-10 12:34:27 +020042
Yinghai Lue420dfb2008-08-19 20:50:21 -070043static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
44{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020045 struct irq_cfg *cfg = irq_get_chip_data(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020046 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080047}
48
Rashika Kheria6a7885c2013-12-18 12:04:27 +053049static int get_irte(int irq, struct irte *entry)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070050{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020051 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070052 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020053 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070054
Thomas Gleixnerd585d062010-10-10 12:34:27 +020055 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070056 return -1;
57
Thomas Gleixner96f8e982011-07-19 16:28:19 +020058 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070059
Yinghai Lue420dfb2008-08-19 20:50:21 -070060 index = irq_iommu->irte_index + irq_iommu->sub_handle;
61 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070062
Thomas Gleixner96f8e982011-07-19 16:28:19 +020063 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070064 return 0;
65}
66
Joerg Roedel263b5e82012-03-30 11:47:06 -070067static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070068{
69 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020070 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Joerg Roedel9b1b0e42012-09-26 12:44:45 +020071 struct irq_cfg *cfg = irq_get_chip_data(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070072 u16 index, start_index;
73 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070074 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070075
Thomas Gleixnerd585d062010-10-10 12:34:27 +020076 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070077 return -1;
78
79 /*
80 * start the IRTE search from index 0.
81 */
82 index = start_index = 0;
83
84 if (count > 1) {
85 count = __roundup_pow_of_two(count);
86 mask = ilog2(count);
87 }
88
89 if (mask > ecap_max_handle_mask(iommu->ecap)) {
90 printk(KERN_ERR
91 "Requested mask %x exceeds the max invalidation handle"
92 " mask value %Lx\n", mask,
93 ecap_max_handle_mask(iommu->ecap));
94 return -1;
95 }
96
Thomas Gleixner96f8e982011-07-19 16:28:19 +020097 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Jiang Liu360eb3c52014-01-06 14:18:08 +080098 index = bitmap_find_free_region(table->bitmap,
99 INTR_REMAP_TABLE_ENTRIES, mask);
100 if (index < 0) {
101 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
102 } else {
103 cfg->remapped = 1;
104 irq_iommu->iommu = iommu;
105 irq_iommu->irte_index = index;
106 irq_iommu->sub_handle = 0;
107 irq_iommu->irte_mask = mask;
108 }
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200109 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700110
111 return index;
112}
113
Yu Zhao704126a2009-01-04 16:28:52 +0800114static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700115{
116 struct qi_desc desc;
117
118 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
119 | QI_IEC_SELECTIVE;
120 desc.high = 0;
121
Yu Zhao704126a2009-01-04 16:28:52 +0800122 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700123}
124
Joerg Roedel263b5e82012-03-30 11:47:06 -0700125static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700126{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200127 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700128 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200129 int index;
130
131 if (!irq_iommu)
132 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700133
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200134 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700135 *sub_handle = irq_iommu->sub_handle;
136 index = irq_iommu->irte_index;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200137 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700138 return index;
139}
140
Joerg Roedel263b5e82012-03-30 11:47:06 -0700141static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700142{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200143 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200144 struct irq_cfg *cfg = irq_get_chip_data(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700145 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700146
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200147 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800148 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200149
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200150 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800151
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200152 cfg->remapped = 1;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700153 irq_iommu->iommu = iommu;
154 irq_iommu->irte_index = index;
155 irq_iommu->sub_handle = subhandle;
156 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700157
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200158 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700159
160 return 0;
161}
162
Joerg Roedel263b5e82012-03-30 11:47:06 -0700163static int modify_irte(int irq, struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700164{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200165 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700166 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700167 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200168 struct irte *irte;
169 int rc, index;
170
171 if (!irq_iommu)
172 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700173
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200174 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700175
Yinghai Lue420dfb2008-08-19 20:50:21 -0700176 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700177
Yinghai Lue420dfb2008-08-19 20:50:21 -0700178 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700179 irte = &iommu->ir_table->base[index];
180
Linus Torvaldsc513b672010-08-06 11:02:31 -0700181 set_64bit(&irte->low, irte_modified->low);
182 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700183 __iommu_flush_cache(iommu, irte, sizeof(*irte));
184
Yu Zhao704126a2009-01-04 16:28:52 +0800185 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200186 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800187
188 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700189}
190
Joerg Roedel263b5e82012-03-30 11:47:06 -0700191static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700192{
193 int i;
194
195 for (i = 0; i < MAX_HPET_TBS; i++)
196 if (ir_hpet[i].id == hpet_id)
197 return ir_hpet[i].iommu;
198 return NULL;
199}
200
Joerg Roedel263b5e82012-03-30 11:47:06 -0700201static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700202{
203 int i;
204
205 for (i = 0; i < MAX_IO_APICS; i++)
206 if (ir_ioapic[i].id == apic)
207 return ir_ioapic[i].iommu;
208 return NULL;
209}
210
Joerg Roedel263b5e82012-03-30 11:47:06 -0700211static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700212{
213 struct dmar_drhd_unit *drhd;
214
215 drhd = dmar_find_matched_drhd_unit(dev);
216 if (!drhd)
217 return NULL;
218
219 return drhd->iommu;
220}
221
Weidong Hanc4658b42009-05-23 00:41:14 +0800222static int clear_entries(struct irq_2_iommu *irq_iommu)
223{
224 struct irte *start, *entry, *end;
225 struct intel_iommu *iommu;
226 int index;
227
228 if (irq_iommu->sub_handle)
229 return 0;
230
231 iommu = irq_iommu->iommu;
232 index = irq_iommu->irte_index + irq_iommu->sub_handle;
233
234 start = iommu->ir_table->base + index;
235 end = start + (1 << irq_iommu->irte_mask);
236
237 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700238 set_64bit(&entry->low, 0);
239 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800240 }
Jiang Liu360eb3c52014-01-06 14:18:08 +0800241 bitmap_release_region(iommu->ir_table->bitmap, index,
242 irq_iommu->irte_mask);
Weidong Hanc4658b42009-05-23 00:41:14 +0800243
244 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
245}
246
Joerg Roedel9d619f62012-03-30 11:47:04 -0700247static int free_irte(int irq)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700248{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200249 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700250 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200251 int rc;
252
253 if (!irq_iommu)
254 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700255
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200256 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700257
Weidong Hanc4658b42009-05-23 00:41:14 +0800258 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700259
Yinghai Lue420dfb2008-08-19 20:50:21 -0700260 irq_iommu->iommu = NULL;
261 irq_iommu->irte_index = 0;
262 irq_iommu->sub_handle = 0;
263 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700264
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200265 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700266
Yu Zhao704126a2009-01-04 16:28:52 +0800267 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700268}
269
Weidong Hanf007e992009-05-23 00:41:15 +0800270/*
271 * source validation type
272 */
273#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300274#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800275#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
276
277/*
278 * source-id qualifier
279 */
280#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
281#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
282 * the third least significant bit
283 */
284#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
285 * the second and third least significant bits
286 */
287#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
288 * the least three significant bits
289 */
290
291/*
292 * set SVT, SQ and SID fields of irte to verify
293 * source ids of interrupt requests
294 */
295static void set_irte_sid(struct irte *irte, unsigned int svt,
296 unsigned int sq, unsigned int sid)
297{
Chris Wrightd1423d52010-07-20 11:06:49 -0700298 if (disable_sourceid_checking)
299 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800300 irte->svt = svt;
301 irte->sq = sq;
302 irte->sid = sid;
303}
304
Joerg Roedel263b5e82012-03-30 11:47:06 -0700305static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800306{
307 int i;
308 u16 sid = 0;
309
310 if (!irte)
311 return -1;
312
313 for (i = 0; i < MAX_IO_APICS; i++) {
314 if (ir_ioapic[i].id == apic) {
315 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
316 break;
317 }
318 }
319
320 if (sid == 0) {
321 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
322 return -1;
323 }
324
325 set_irte_sid(irte, 1, 0, sid);
326
327 return 0;
328}
329
Joerg Roedel263b5e82012-03-30 11:47:06 -0700330static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700331{
332 int i;
333 u16 sid = 0;
334
335 if (!irte)
336 return -1;
337
338 for (i = 0; i < MAX_HPET_TBS; i++) {
339 if (ir_hpet[i].id == id) {
340 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
341 break;
342 }
343 }
344
345 if (sid == 0) {
346 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
347 return -1;
348 }
349
350 /*
351 * Should really use SQ_ALL_16. Some platforms are broken.
352 * While we figure out the right quirks for these broken platforms, use
353 * SQ_13_IGNORE_3 for now.
354 */
355 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
356
357 return 0;
358}
359
Joerg Roedel263b5e82012-03-30 11:47:06 -0700360static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800361{
362 struct pci_dev *bridge;
363
364 if (!irte || !dev)
365 return -1;
366
367 /* PCIe device or Root Complex integrated PCI device */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900368 if (pci_is_pcie(dev) || !dev->bus->parent) {
Weidong Hanf007e992009-05-23 00:41:15 +0800369 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
370 (dev->bus->number << 8) | dev->devfn);
371 return 0;
372 }
373
374 bridge = pci_find_upstream_pcie_bridge(dev);
375 if (bridge) {
Stefan Assmann45e829e2009-12-03 06:49:24 -0500376 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
Weidong Hanf007e992009-05-23 00:41:15 +0800377 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
378 (bridge->bus->number << 8) | dev->bus->number);
379 else /* this is a legacy PCI bridge */
380 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
381 (bridge->bus->number << 8) | bridge->devfn);
382 }
383
384 return 0;
385}
386
Suresh Siddha95a02e92012-03-30 11:47:07 -0700387static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700388{
389 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100390 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700391 unsigned long flags;
392
393 addr = virt_to_phys((void *)iommu->ir_table->base);
394
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200395 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700396
397 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
398 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
399
400 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800401 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100402 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700403
404 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
405 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200406 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700407
408 /*
409 * global invalidation of interrupt entry cache before enabling
410 * interrupt-remapping.
411 */
412 qi_global_iec(iommu);
413
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200414 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700415
416 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700417 iommu->gcmd |= DMA_GCMD_IRE;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800418 iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
David Woodhousec416daa2009-05-10 20:30:58 +0100419 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700420
421 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
422 readl, (sts & DMA_GSTS_IRES), sts);
423
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800424 /*
425 * With CFI clear in the Global Command register, we should be
426 * protected from dangerous (i.e. compatibility) interrupts
427 * regardless of x2apic status. Check just to be sure.
428 */
429 if (sts & DMA_GSTS_CFIS)
430 WARN(1, KERN_WARNING
431 "Compatibility-format IRQs enabled despite intr remapping;\n"
432 "you are vulnerable to IRQ injection.\n");
433
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200434 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700435}
436
437
Suresh Siddha95a02e92012-03-30 11:47:07 -0700438static int intel_setup_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700439{
440 struct ir_table *ir_table;
441 struct page *pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800442 unsigned long *bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700443
444 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700445 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700446
447 if (!iommu->ir_table)
448 return -ENOMEM;
449
Suresh Siddha824cd752009-10-02 11:01:23 -0700450 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
451 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700452
453 if (!pages) {
Jiang Liu360eb3c52014-01-06 14:18:08 +0800454 pr_err("IR%d: failed to allocate pages of order %d\n",
455 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700456 kfree(iommu->ir_table);
457 return -ENOMEM;
458 }
459
Jiang Liu360eb3c52014-01-06 14:18:08 +0800460 bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
461 sizeof(long), GFP_ATOMIC);
462 if (bitmap == NULL) {
463 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
464 __free_pages(pages, INTR_REMAP_PAGE_ORDER);
465 kfree(ir_table);
466 return -ENOMEM;
467 }
468
Suresh Siddha2ae21012008-07-10 11:16:43 -0700469 ir_table->base = page_address(pages);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800470 ir_table->bitmap = bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700471
Suresh Siddha95a02e92012-03-30 11:47:07 -0700472 iommu_set_irq_remapping(iommu, mode);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700473 return 0;
474}
475
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700476/*
477 * Disable Interrupt Remapping.
478 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700479static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700480{
481 unsigned long flags;
482 u32 sts;
483
484 if (!ecap_ir_support(iommu->ecap))
485 return;
486
Fenghua Yub24696b2009-03-27 14:22:44 -0700487 /*
488 * global invalidation of interrupt entry cache before disabling
489 * interrupt-remapping.
490 */
491 qi_global_iec(iommu);
492
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200493 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700494
495 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
496 if (!(sts & DMA_GSTS_IRES))
497 goto end;
498
499 iommu->gcmd &= ~DMA_GCMD_IRE;
500 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
501
502 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
503 readl, !(sts & DMA_GSTS_IRES), sts);
504
505end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200506 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700507}
508
Suresh Siddha41750d32011-08-23 17:05:18 -0700509static int __init dmar_x2apic_optout(void)
510{
511 struct acpi_table_dmar *dmar;
512 dmar = (struct acpi_table_dmar *)dmar_tbl;
513 if (!dmar || no_x2apic_optout)
514 return 0;
515 return dmar->flags & DMAR_X2APIC_OPT_OUT;
516}
517
Suresh Siddha95a02e92012-03-30 11:47:07 -0700518static int __init intel_irq_remapping_supported(void)
Weidong Han93758232009-04-17 16:42:14 +0800519{
520 struct dmar_drhd_unit *drhd;
521
Suresh Siddha95a02e92012-03-30 11:47:07 -0700522 if (disable_irq_remap)
Weidong Han03ea8152009-04-17 16:42:15 +0800523 return 0;
Neil Horman03bbcb22013-04-16 16:38:32 -0400524 if (irq_remap_broken) {
Neil Horman05104a42013-09-27 12:53:35 -0400525 printk(KERN_WARNING
526 "This system BIOS has enabled interrupt remapping\n"
527 "on a chipset that contains an erratum making that\n"
528 "feature unstable. To maintain system stability\n"
529 "interrupt remapping is being disabled. Please\n"
530 "contact your BIOS vendor for an update\n");
531 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Neil Horman03bbcb22013-04-16 16:38:32 -0400532 disable_irq_remap = 1;
533 return 0;
534 }
Weidong Han03ea8152009-04-17 16:42:15 +0800535
Youquan Song074835f2009-09-09 12:05:39 -0400536 if (!dmar_ir_support())
537 return 0;
538
Weidong Han93758232009-04-17 16:42:14 +0800539 for_each_drhd_unit(drhd) {
540 struct intel_iommu *iommu = drhd->iommu;
541
542 if (!ecap_ir_support(iommu->ecap))
543 return 0;
544 }
545
546 return 1;
547}
548
Suresh Siddha95a02e92012-03-30 11:47:07 -0700549static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700550{
551 struct dmar_drhd_unit *drhd;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800552 bool x2apic_present;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700553 int setup = 0;
Suresh Siddha41750d32011-08-23 17:05:18 -0700554 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700555
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800556 x2apic_present = x2apic_supported();
557
Youquan Songe936d072009-09-07 10:58:07 -0400558 if (parse_ioapics_under_ir() != 1) {
559 printk(KERN_INFO "Not enable interrupt remapping\n");
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800560 goto error;
Youquan Songe936d072009-09-07 10:58:07 -0400561 }
562
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800563 if (x2apic_present) {
Suresh Siddha41750d32011-08-23 17:05:18 -0700564 eim = !dmar_x2apic_optout();
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800565 if (!eim)
566 printk(KERN_WARNING
567 "Your BIOS is broken and requested that x2apic be disabled.\n"
568 "This will slightly decrease performance.\n"
569 "Use 'intremap=no_x2apic_optout' to override BIOS request.\n");
Suresh Siddha41750d32011-08-23 17:05:18 -0700570 }
571
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700572 for_each_drhd_unit(drhd) {
573 struct intel_iommu *iommu = drhd->iommu;
574
575 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800576 * If the queued invalidation is already initialized,
577 * shouldn't disable it.
578 */
579 if (iommu->qi)
580 continue;
581
582 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700583 * Clear previous faults.
584 */
585 dmar_fault(-1, iommu);
586
587 /*
588 * Disable intr remapping and queued invalidation, if already
589 * enabled prior to OS handover.
590 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700591 iommu_disable_irq_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700592
593 dmar_disable_qi(iommu);
594 }
595
Suresh Siddha2ae21012008-07-10 11:16:43 -0700596 /*
597 * check for the Interrupt-remapping support
598 */
599 for_each_drhd_unit(drhd) {
600 struct intel_iommu *iommu = drhd->iommu;
601
602 if (!ecap_ir_support(iommu->ecap))
603 continue;
604
605 if (eim && !ecap_eim_support(iommu->ecap)) {
606 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
607 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800608 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700609 }
610 }
611
612 /*
613 * Enable queued invalidation for all the DRHD's.
614 */
615 for_each_drhd_unit(drhd) {
616 int ret;
617 struct intel_iommu *iommu = drhd->iommu;
618 ret = dmar_enable_qi(iommu);
619
620 if (ret) {
621 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
622 " invalidation, ecap %Lx, ret %d\n",
623 drhd->reg_base_addr, iommu->ecap, ret);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800624 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700625 }
626 }
627
628 /*
629 * Setup Interrupt-remapping for all the DRHD's now.
630 */
631 for_each_drhd_unit(drhd) {
632 struct intel_iommu *iommu = drhd->iommu;
633
634 if (!ecap_ir_support(iommu->ecap))
635 continue;
636
Suresh Siddha95a02e92012-03-30 11:47:07 -0700637 if (intel_setup_irq_remapping(iommu, eim))
Suresh Siddha2ae21012008-07-10 11:16:43 -0700638 goto error;
639
640 setup = 1;
641 }
642
643 if (!setup)
644 goto error;
645
Suresh Siddha95a02e92012-03-30 11:47:07 -0700646 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200647
648 /*
649 * VT-d has a different layout for IO-APIC entries when
650 * interrupt remapping is enabled. So it needs a special routine
651 * to print IO-APIC entries for debugging purposes too.
652 */
653 x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
654
Suresh Siddha41750d32011-08-23 17:05:18 -0700655 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700656
Suresh Siddha41750d32011-08-23 17:05:18 -0700657 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700658
659error:
660 /*
661 * handle error condition gracefully here!
662 */
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800663
664 if (x2apic_present)
Andy Lutomirskid01140d2013-05-13 15:22:42 -0700665 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800666
Suresh Siddha2ae21012008-07-10 11:16:43 -0700667 return -1;
668}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700669
Suresh Siddha20f30972009-08-04 12:07:08 -0700670static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
671 struct intel_iommu *iommu)
672{
673 struct acpi_dmar_pci_path *path;
674 u8 bus;
675 int count;
676
677 bus = scope->bus;
678 path = (struct acpi_dmar_pci_path *)(scope + 1);
679 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
680 / sizeof(struct acpi_dmar_pci_path);
681
682 while (--count > 0) {
683 /*
684 * Access PCI directly due to the PCI
685 * subsystem isn't initialized yet.
686 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800687 bus = read_pci_config_byte(bus, path->device, path->function,
Suresh Siddha20f30972009-08-04 12:07:08 -0700688 PCI_SECONDARY_BUS);
689 path++;
690 }
691 ir_hpet[ir_hpet_num].bus = bus;
Lv Zhengfa5f5082013-10-31 09:30:22 +0800692 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->device, path->function);
Suresh Siddha20f30972009-08-04 12:07:08 -0700693 ir_hpet[ir_hpet_num].iommu = iommu;
694 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
695 ir_hpet_num++;
696}
697
Weidong Hanf007e992009-05-23 00:41:15 +0800698static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
699 struct intel_iommu *iommu)
700{
701 struct acpi_dmar_pci_path *path;
702 u8 bus;
703 int count;
704
705 bus = scope->bus;
706 path = (struct acpi_dmar_pci_path *)(scope + 1);
707 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
708 / sizeof(struct acpi_dmar_pci_path);
709
710 while (--count > 0) {
711 /*
712 * Access PCI directly due to the PCI
713 * subsystem isn't initialized yet.
714 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800715 bus = read_pci_config_byte(bus, path->device, path->function,
Weidong Hanf007e992009-05-23 00:41:15 +0800716 PCI_SECONDARY_BUS);
717 path++;
718 }
719
720 ir_ioapic[ir_ioapic_num].bus = bus;
Lv Zhengfa5f5082013-10-31 09:30:22 +0800721 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->device, path->function);
Weidong Hanf007e992009-05-23 00:41:15 +0800722 ir_ioapic[ir_ioapic_num].iommu = iommu;
723 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
724 ir_ioapic_num++;
725}
726
Suresh Siddha20f30972009-08-04 12:07:08 -0700727static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
728 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700729{
730 struct acpi_dmar_hardware_unit *drhd;
731 struct acpi_dmar_device_scope *scope;
732 void *start, *end;
733
734 drhd = (struct acpi_dmar_hardware_unit *)header;
735
736 start = (void *)(drhd + 1);
737 end = ((void *)drhd) + header->length;
738
739 while (start < end) {
740 scope = start;
741 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
742 if (ir_ioapic_num == MAX_IO_APICS) {
743 printk(KERN_WARNING "Exceeded Max IO APICS\n");
744 return -1;
745 }
746
Yinghai Lu680a7522010-04-08 19:58:23 +0100747 printk(KERN_INFO "IOAPIC id %d under DRHD base "
748 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
749 drhd->address, iommu->seq_id);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700750
Weidong Hanf007e992009-05-23 00:41:15 +0800751 ir_parse_one_ioapic_scope(scope, iommu);
Suresh Siddha20f30972009-08-04 12:07:08 -0700752 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
753 if (ir_hpet_num == MAX_HPET_TBS) {
754 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
755 return -1;
756 }
757
758 printk(KERN_INFO "HPET id %d under DRHD base"
759 " 0x%Lx\n", scope->enumeration_id,
760 drhd->address);
761
762 ir_parse_one_hpet_scope(scope, iommu);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700763 }
764 start += scope->length;
765 }
766
767 return 0;
768}
769
770/*
771 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
772 * hardware unit.
773 */
774int __init parse_ioapics_under_ir(void)
775{
776 struct dmar_drhd_unit *drhd;
777 int ir_supported = 0;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500778 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700779
780 for_each_drhd_unit(drhd) {
781 struct intel_iommu *iommu = drhd->iommu;
782
783 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700784 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700785 return -1;
786
787 ir_supported = 1;
788 }
789 }
790
Seth Forshee32ab31e2012-08-08 08:27:03 -0500791 if (!ir_supported)
792 return 0;
793
794 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
795 int ioapic_id = mpc_ioapic_id(ioapic_idx);
796 if (!map_ioapic_to_ir(ioapic_id)) {
797 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
798 "interrupt remapping will be disabled\n",
799 ioapic_id);
800 return -1;
801 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700802 }
803
Seth Forshee32ab31e2012-08-08 08:27:03 -0500804 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700805}
Fenghua Yub24696b2009-03-27 14:22:44 -0700806
Rashika Kheria6a7885c2013-12-18 12:04:27 +0530807static int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700808{
Suresh Siddha95a02e92012-03-30 11:47:07 -0700809 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700810 return 0;
811
812 return dmar_dev_scope_init();
813}
814rootfs_initcall(ir_dev_scope_init);
815
Suresh Siddha95a02e92012-03-30 11:47:07 -0700816static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700817{
818 struct dmar_drhd_unit *drhd;
819 struct intel_iommu *iommu = NULL;
820
821 /*
822 * Disable Interrupt-remapping for all the DRHD's now.
823 */
824 for_each_iommu(iommu, drhd) {
825 if (!ecap_ir_support(iommu->ecap))
826 continue;
827
Suresh Siddha95a02e92012-03-30 11:47:07 -0700828 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700829 }
830}
831
Suresh Siddha95a02e92012-03-30 11:47:07 -0700832static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700833{
834 struct dmar_drhd_unit *drhd;
835 int setup = 0;
836 struct intel_iommu *iommu = NULL;
837
838 for_each_iommu(iommu, drhd)
839 if (iommu->qi)
840 dmar_reenable_qi(iommu);
841
842 /*
843 * Setup Interrupt-remapping for all the DRHD's now.
844 */
845 for_each_iommu(iommu, drhd) {
846 if (!ecap_ir_support(iommu->ecap))
847 continue;
848
849 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -0700850 iommu_set_irq_remapping(iommu, eim);
Fenghua Yub24696b2009-03-27 14:22:44 -0700851 setup = 1;
852 }
853
854 if (!setup)
855 goto error;
856
857 return 0;
858
859error:
860 /*
861 * handle error condition gracefully here!
862 */
863 return -1;
864}
865
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700866static void prepare_irte(struct irte *irte, int vector,
867 unsigned int dest)
868{
869 memset(irte, 0, sizeof(*irte));
870
871 irte->present = 1;
872 irte->dst_mode = apic->irq_dest_mode;
873 /*
874 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
875 * actual level or edge trigger will be setup in the IO-APIC
876 * RTE. This will help simplify level triggered irq migration.
877 * For more details, see the comments (in io_apic.c) explainig IO-APIC
878 * irq migration in the presence of interrupt-remapping.
879 */
880 irte->trigger_mode = 0;
881 irte->dlvry_mode = apic->irq_delivery_mode;
882 irte->vector = vector;
883 irte->dest_id = IRTE_DEST(dest);
884 irte->redir_hint = 1;
885}
886
887static int intel_setup_ioapic_entry(int irq,
888 struct IO_APIC_route_entry *route_entry,
889 unsigned int destination, int vector,
890 struct io_apic_irq_attr *attr)
891{
892 int ioapic_id = mpc_ioapic_id(attr->ioapic);
893 struct intel_iommu *iommu = map_ioapic_to_ir(ioapic_id);
894 struct IR_IO_APIC_route_entry *entry;
895 struct irte irte;
896 int index;
897
898 if (!iommu) {
899 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
900 return -ENODEV;
901 }
902
903 entry = (struct IR_IO_APIC_route_entry *)route_entry;
904
905 index = alloc_irte(iommu, irq, 1);
906 if (index < 0) {
907 pr_warn("Failed to allocate IRTE for ioapic %d\n", ioapic_id);
908 return -ENOMEM;
909 }
910
911 prepare_irte(&irte, vector, destination);
912
913 /* Set source-id of interrupt request */
914 set_ioapic_sid(&irte, ioapic_id);
915
916 modify_irte(irq, &irte);
917
918 apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
919 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
920 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
921 "Avail:%X Vector:%02X Dest:%08X "
922 "SID:%04X SQ:%X SVT:%X)\n",
923 attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
924 irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
925 irte.avail, irte.vector, irte.dest_id,
926 irte.sid, irte.sq, irte.svt);
927
928 memset(entry, 0, sizeof(*entry));
929
930 entry->index2 = (index >> 15) & 0x1;
931 entry->zero = 0;
932 entry->format = 1;
933 entry->index = (index & 0x7fff);
934 /*
935 * IO-APIC RTE will be configured with virtual vector.
936 * irq handler will do the explicit EOI to the io-apic.
937 */
938 entry->vector = attr->ioapic_pin;
939 entry->mask = 0; /* enable IRQ */
940 entry->trigger = attr->trigger;
941 entry->polarity = attr->polarity;
942
943 /* Mask level triggered irqs.
944 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
945 */
946 if (attr->trigger)
947 entry->mask = 1;
948
949 return 0;
950}
951
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700952/*
953 * Migrate the IO-APIC irq in the presence of intr-remapping.
954 *
955 * For both level and edge triggered, irq migration is a simple atomic
956 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
957 *
958 * For level triggered, we eliminate the io-apic RTE modification (with the
959 * updated vector information), by using a virtual vector (io-apic pin number).
960 * Real vector that is used for interrupting cpu will be coming from
961 * the interrupt-remapping table entry.
962 *
963 * As the migration is a simple atomic update of IRTE, the same mechanism
964 * is used to migrate MSI irq's in the presence of interrupt-remapping.
965 */
966static int
967intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
968 bool force)
969{
970 struct irq_cfg *cfg = data->chip_data;
971 unsigned int dest, irq = data->irq;
972 struct irte irte;
Alexander Gordeevff164322012-06-07 15:15:59 +0200973 int err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700974
Suresh Siddha7eb9ae02012-06-14 18:28:49 -0700975 if (!config_enabled(CONFIG_SMP))
976 return -EINVAL;
977
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700978 if (!cpumask_intersects(mask, cpu_online_mask))
979 return -EINVAL;
980
981 if (get_irte(irq, &irte))
982 return -EBUSY;
983
Alexander Gordeevff164322012-06-07 15:15:59 +0200984 err = assign_irq_vector(irq, cfg, mask);
985 if (err)
986 return err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700987
Alexander Gordeevff164322012-06-07 15:15:59 +0200988 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
989 if (err) {
Dan Carpentered88bed2012-06-12 19:26:33 +0300990 if (assign_irq_vector(irq, cfg, data->affinity))
Alexander Gordeevff164322012-06-07 15:15:59 +0200991 pr_err("Failed to recover vector for irq %d\n", irq);
992 return err;
993 }
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700994
995 irte.vector = cfg->vector;
996 irte.dest_id = IRTE_DEST(dest);
997
998 /*
999 * Atomically updates the IRTE with the new destination, vector
1000 * and flushes the interrupt entry cache.
1001 */
1002 modify_irte(irq, &irte);
1003
1004 /*
1005 * After this point, all the interrupts will start arriving
1006 * at the new destination. So, time to cleanup the previous
1007 * vector allocation.
1008 */
1009 if (cfg->move_in_progress)
1010 send_cleanup_vector(cfg);
1011
1012 cpumask_copy(data->affinity, mask);
1013 return 0;
1014}
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001015
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001016static void intel_compose_msi_msg(struct pci_dev *pdev,
1017 unsigned int irq, unsigned int dest,
1018 struct msi_msg *msg, u8 hpet_id)
1019{
1020 struct irq_cfg *cfg;
1021 struct irte irte;
Suresh Siddhac558df42012-05-08 00:08:54 -07001022 u16 sub_handle = 0;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001023 int ir_index;
1024
1025 cfg = irq_get_chip_data(irq);
1026
1027 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
1028 BUG_ON(ir_index == -1);
1029
1030 prepare_irte(&irte, cfg->vector, dest);
1031
1032 /* Set source-id of interrupt request */
1033 if (pdev)
1034 set_msi_sid(&irte, pdev);
1035 else
1036 set_hpet_sid(&irte, hpet_id);
1037
1038 modify_irte(irq, &irte);
1039
1040 msg->address_hi = MSI_ADDR_BASE_HI;
1041 msg->data = sub_handle;
1042 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1043 MSI_ADDR_IR_SHV |
1044 MSI_ADDR_IR_INDEX1(ir_index) |
1045 MSI_ADDR_IR_INDEX2(ir_index);
1046}
1047
1048/*
1049 * Map the PCI dev to the corresponding remapping hardware unit
1050 * and allocate 'nvec' consecutive interrupt-remapping table entries
1051 * in it.
1052 */
1053static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
1054{
1055 struct intel_iommu *iommu;
1056 int index;
1057
1058 iommu = map_dev_to_ir(dev);
1059 if (!iommu) {
1060 printk(KERN_ERR
1061 "Unable to map PCI %s to iommu\n", pci_name(dev));
1062 return -ENOENT;
1063 }
1064
1065 index = alloc_irte(iommu, irq, nvec);
1066 if (index < 0) {
1067 printk(KERN_ERR
1068 "Unable to allocate %d IRTE for PCI %s\n", nvec,
1069 pci_name(dev));
1070 return -ENOSPC;
1071 }
1072 return index;
1073}
1074
1075static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
1076 int index, int sub_handle)
1077{
1078 struct intel_iommu *iommu;
1079
1080 iommu = map_dev_to_ir(pdev);
1081 if (!iommu)
1082 return -ENOENT;
1083 /*
1084 * setup the mapping between the irq and the IRTE
1085 * base index, the sub_handle pointing to the
1086 * appropriate interrupt remap table entry.
1087 */
1088 set_irte_irq(irq, iommu, index, sub_handle);
1089
1090 return 0;
1091}
1092
1093static int intel_setup_hpet_msi(unsigned int irq, unsigned int id)
1094{
1095 struct intel_iommu *iommu = map_hpet_to_ir(id);
1096 int index;
1097
1098 if (!iommu)
1099 return -1;
1100
1101 index = alloc_irte(iommu, irq, 1);
1102 if (index < 0)
1103 return -1;
1104
1105 return 0;
1106}
1107
Joerg Roedel736baef2012-03-30 11:47:00 -07001108struct irq_remap_ops intel_irq_remap_ops = {
Suresh Siddha95a02e92012-03-30 11:47:07 -07001109 .supported = intel_irq_remapping_supported,
1110 .prepare = dmar_table_init,
1111 .enable = intel_enable_irq_remapping,
1112 .disable = disable_irq_remapping,
1113 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -07001114 .enable_faulting = enable_drhd_fault_handling,
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001115 .setup_ioapic_entry = intel_setup_ioapic_entry,
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001116 .set_affinity = intel_ioapic_set_affinity,
Joerg Roedel9d619f62012-03-30 11:47:04 -07001117 .free_irq = free_irte,
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001118 .compose_msi_msg = intel_compose_msi_msg,
1119 .msi_alloc_irq = intel_msi_alloc_irq,
1120 .msi_setup_irq = intel_msi_setup_irq,
1121 .setup_hpet_msi = intel_setup_hpet_msi,
Joerg Roedel736baef2012-03-30 11:47:00 -07001122};