blob: 757e0b0d19ffb088b958e07636189817478cdcd3 [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>
Lv Zheng8b484632013-12-03 08:49:16 +08009#include <linux/intel-iommu.h>
10#include <linux/acpi.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070011#include <asm/io_apic.h>
Yinghai Lu17483a12008-12-12 13:14:18 -080012#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053013#include <asm/cpu.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
Jiang Liu3a5670e2014-02-19 14:07:33 +080041/*
42 * Lock ordering:
43 * ->dmar_global_lock
44 * ->irq_2_ir_lock
45 * ->qi->q_lock
46 * ->iommu->register_lock
47 * Note:
48 * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
49 * in single-threaded environment with interrupt disabled, so no need to tabke
50 * the dmar_global_lock.
51 */
Thomas Gleixner96f8e982011-07-19 16:28:19 +020052static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Thomas Gleixnerd585d062010-10-10 12:34:27 +020053
Jiang Liu694835d2014-01-06 14:18:16 +080054static int __init parse_ioapics_under_ir(void);
55
Yinghai Lue420dfb2008-08-19 20:50:21 -070056static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
57{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020058 struct irq_cfg *cfg = irq_get_chip_data(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020059 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080060}
61
Rashika Kheria6a7885c2013-12-18 12:04:27 +053062static int get_irte(int irq, struct irte *entry)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070063{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020064 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070065 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020066 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070067
Thomas Gleixnerd585d062010-10-10 12:34:27 +020068 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070069 return -1;
70
Thomas Gleixner96f8e982011-07-19 16:28:19 +020071 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070072
Yinghai Lue420dfb2008-08-19 20:50:21 -070073 index = irq_iommu->irte_index + irq_iommu->sub_handle;
74 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070075
Thomas Gleixner96f8e982011-07-19 16:28:19 +020076 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070077 return 0;
78}
79
Joerg Roedel263b5e82012-03-30 11:47:06 -070080static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070081{
82 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020083 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Joerg Roedel9b1b0e42012-09-26 12:44:45 +020084 struct irq_cfg *cfg = irq_get_chip_data(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070085 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070086 unsigned long flags;
Dan Carpenter9f4c7442014-01-09 08:32:36 +030087 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070088
Thomas Gleixnerd585d062010-10-10 12:34:27 +020089 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070090 return -1;
91
Suresh Siddhab6fcb332008-07-10 11:16:44 -070092 if (count > 1) {
93 count = __roundup_pow_of_two(count);
94 mask = ilog2(count);
95 }
96
97 if (mask > ecap_max_handle_mask(iommu->ecap)) {
98 printk(KERN_ERR
99 "Requested mask %x exceeds the max invalidation handle"
100 " mask value %Lx\n", mask,
101 ecap_max_handle_mask(iommu->ecap));
102 return -1;
103 }
104
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200105 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Jiang Liu360eb3c2014-01-06 14:18:08 +0800106 index = bitmap_find_free_region(table->bitmap,
107 INTR_REMAP_TABLE_ENTRIES, mask);
108 if (index < 0) {
109 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
110 } else {
111 cfg->remapped = 1;
112 irq_iommu->iommu = iommu;
113 irq_iommu->irte_index = index;
114 irq_iommu->sub_handle = 0;
115 irq_iommu->irte_mask = mask;
116 }
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200117 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700118
119 return index;
120}
121
Yu Zhao704126a2009-01-04 16:28:52 +0800122static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700123{
124 struct qi_desc desc;
125
126 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
127 | QI_IEC_SELECTIVE;
128 desc.high = 0;
129
Yu Zhao704126a2009-01-04 16:28:52 +0800130 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700131}
132
Joerg Roedel263b5e82012-03-30 11:47:06 -0700133static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700134{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200135 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700136 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200137 int index;
138
139 if (!irq_iommu)
140 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700141
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200142 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700143 *sub_handle = irq_iommu->sub_handle;
144 index = irq_iommu->irte_index;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200145 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700146 return index;
147}
148
Joerg Roedel263b5e82012-03-30 11:47:06 -0700149static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700150{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200151 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200152 struct irq_cfg *cfg = irq_get_chip_data(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700153 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700154
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200155 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800156 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200157
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200158 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800159
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200160 cfg->remapped = 1;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700161 irq_iommu->iommu = iommu;
162 irq_iommu->irte_index = index;
163 irq_iommu->sub_handle = subhandle;
164 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700165
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200166 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700167
168 return 0;
169}
170
Joerg Roedel263b5e82012-03-30 11:47:06 -0700171static int modify_irte(int irq, struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700172{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200173 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700174 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700175 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200176 struct irte *irte;
177 int rc, index;
178
179 if (!irq_iommu)
180 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700181
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200182 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700183
Yinghai Lue420dfb2008-08-19 20:50:21 -0700184 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700185
Yinghai Lue420dfb2008-08-19 20:50:21 -0700186 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700187 irte = &iommu->ir_table->base[index];
188
Linus Torvaldsc513b672010-08-06 11:02:31 -0700189 set_64bit(&irte->low, irte_modified->low);
190 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700191 __iommu_flush_cache(iommu, irte, sizeof(*irte));
192
Yu Zhao704126a2009-01-04 16:28:52 +0800193 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200194 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800195
196 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700197}
198
Joerg Roedel263b5e82012-03-30 11:47:06 -0700199static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700200{
201 int i;
202
203 for (i = 0; i < MAX_HPET_TBS; i++)
204 if (ir_hpet[i].id == hpet_id)
205 return ir_hpet[i].iommu;
206 return NULL;
207}
208
Joerg Roedel263b5e82012-03-30 11:47:06 -0700209static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700210{
211 int i;
212
213 for (i = 0; i < MAX_IO_APICS; i++)
214 if (ir_ioapic[i].id == apic)
215 return ir_ioapic[i].iommu;
216 return NULL;
217}
218
Joerg Roedel263b5e82012-03-30 11:47:06 -0700219static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700220{
221 struct dmar_drhd_unit *drhd;
222
223 drhd = dmar_find_matched_drhd_unit(dev);
224 if (!drhd)
225 return NULL;
226
227 return drhd->iommu;
228}
229
Weidong Hanc4658b42009-05-23 00:41:14 +0800230static int clear_entries(struct irq_2_iommu *irq_iommu)
231{
232 struct irte *start, *entry, *end;
233 struct intel_iommu *iommu;
234 int index;
235
236 if (irq_iommu->sub_handle)
237 return 0;
238
239 iommu = irq_iommu->iommu;
240 index = irq_iommu->irte_index + irq_iommu->sub_handle;
241
242 start = iommu->ir_table->base + index;
243 end = start + (1 << irq_iommu->irte_mask);
244
245 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700246 set_64bit(&entry->low, 0);
247 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800248 }
Jiang Liu360eb3c2014-01-06 14:18:08 +0800249 bitmap_release_region(iommu->ir_table->bitmap, index,
250 irq_iommu->irte_mask);
Weidong Hanc4658b42009-05-23 00:41:14 +0800251
252 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
253}
254
Joerg Roedel9d619f62012-03-30 11:47:04 -0700255static int free_irte(int irq)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700256{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200257 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700258 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200259 int rc;
260
261 if (!irq_iommu)
262 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700263
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200264 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700265
Weidong Hanc4658b42009-05-23 00:41:14 +0800266 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700267
Yinghai Lue420dfb2008-08-19 20:50:21 -0700268 irq_iommu->iommu = NULL;
269 irq_iommu->irte_index = 0;
270 irq_iommu->sub_handle = 0;
271 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700272
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200273 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700274
Yu Zhao704126a2009-01-04 16:28:52 +0800275 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700276}
277
Weidong Hanf007e992009-05-23 00:41:15 +0800278/*
279 * source validation type
280 */
281#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300282#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800283#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
284
285/*
286 * source-id qualifier
287 */
288#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
289#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
290 * the third least significant bit
291 */
292#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
293 * the second and third least significant bits
294 */
295#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
296 * the least three significant bits
297 */
298
299/*
300 * set SVT, SQ and SID fields of irte to verify
301 * source ids of interrupt requests
302 */
303static void set_irte_sid(struct irte *irte, unsigned int svt,
304 unsigned int sq, unsigned int sid)
305{
Chris Wrightd1423d52010-07-20 11:06:49 -0700306 if (disable_sourceid_checking)
307 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800308 irte->svt = svt;
309 irte->sq = sq;
310 irte->sid = sid;
311}
312
Joerg Roedel263b5e82012-03-30 11:47:06 -0700313static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800314{
315 int i;
316 u16 sid = 0;
317
318 if (!irte)
319 return -1;
320
Jiang Liu3a5670e2014-02-19 14:07:33 +0800321 down_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800322 for (i = 0; i < MAX_IO_APICS; i++) {
323 if (ir_ioapic[i].id == apic) {
324 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
325 break;
326 }
327 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800328 up_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800329
330 if (sid == 0) {
331 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
332 return -1;
333 }
334
Jiang Liu2fe2c602014-01-06 14:18:17 +0800335 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
Weidong Hanf007e992009-05-23 00:41:15 +0800336
337 return 0;
338}
339
Joerg Roedel263b5e82012-03-30 11:47:06 -0700340static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700341{
342 int i;
343 u16 sid = 0;
344
345 if (!irte)
346 return -1;
347
Jiang Liu3a5670e2014-02-19 14:07:33 +0800348 down_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700349 for (i = 0; i < MAX_HPET_TBS; i++) {
350 if (ir_hpet[i].id == id) {
351 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
352 break;
353 }
354 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800355 up_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700356
357 if (sid == 0) {
358 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
359 return -1;
360 }
361
362 /*
363 * Should really use SQ_ALL_16. Some platforms are broken.
364 * While we figure out the right quirks for these broken platforms, use
365 * SQ_13_IGNORE_3 for now.
366 */
367 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
368
369 return 0;
370}
371
Alex Williamson579305f2014-07-03 09:51:43 -0600372struct set_msi_sid_data {
373 struct pci_dev *pdev;
374 u16 alias;
375};
376
377static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
378{
379 struct set_msi_sid_data *data = opaque;
380
381 data->pdev = pdev;
382 data->alias = alias;
383
384 return 0;
385}
386
Joerg Roedel263b5e82012-03-30 11:47:06 -0700387static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800388{
Alex Williamson579305f2014-07-03 09:51:43 -0600389 struct set_msi_sid_data data;
Weidong Hanf007e992009-05-23 00:41:15 +0800390
391 if (!irte || !dev)
392 return -1;
393
Alex Williamson579305f2014-07-03 09:51:43 -0600394 pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
Weidong Hanf007e992009-05-23 00:41:15 +0800395
Alex Williamson579305f2014-07-03 09:51:43 -0600396 /*
397 * DMA alias provides us with a PCI device and alias. The only case
398 * where the it will return an alias on a different bus than the
399 * device is the case of a PCIe-to-PCI bridge, where the alias is for
400 * the subordinate bus. In this case we can only verify the bus.
401 *
402 * If the alias device is on a different bus than our source device
403 * then we have a topology based alias, use it.
404 *
405 * Otherwise, the alias is for a device DMA quirk and we cannot
406 * assume that MSI uses the same requester ID. Therefore use the
407 * original device.
408 */
409 if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
410 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
411 PCI_DEVID(PCI_BUS_NUM(data.alias),
412 dev->bus->number));
413 else if (data.pdev->bus->number != dev->bus->number)
414 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
415 else
416 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
417 PCI_DEVID(dev->bus->number, dev->devfn));
Weidong Hanf007e992009-05-23 00:41:15 +0800418
419 return 0;
420}
421
Suresh Siddha95a02e92012-03-30 11:47:07 -0700422static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700423{
424 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100425 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700426 unsigned long flags;
427
428 addr = virt_to_phys((void *)iommu->ir_table->base);
429
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200430 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700431
432 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
433 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
434
435 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800436 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100437 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700438
439 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
440 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200441 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700442
443 /*
444 * global invalidation of interrupt entry cache before enabling
445 * interrupt-remapping.
446 */
447 qi_global_iec(iommu);
448
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200449 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700450
451 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700452 iommu->gcmd |= DMA_GCMD_IRE;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800453 iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
David Woodhousec416daa2009-05-10 20:30:58 +0100454 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700455
456 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
457 readl, (sts & DMA_GSTS_IRES), sts);
458
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800459 /*
460 * With CFI clear in the Global Command register, we should be
461 * protected from dangerous (i.e. compatibility) interrupts
462 * regardless of x2apic status. Check just to be sure.
463 */
464 if (sts & DMA_GSTS_CFIS)
465 WARN(1, KERN_WARNING
466 "Compatibility-format IRQs enabled despite intr remapping;\n"
467 "you are vulnerable to IRQ injection.\n");
468
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200469 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700470}
471
472
Suresh Siddha95a02e92012-03-30 11:47:07 -0700473static int intel_setup_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700474{
475 struct ir_table *ir_table;
476 struct page *pages;
Jiang Liu360eb3c2014-01-06 14:18:08 +0800477 unsigned long *bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700478
479 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700480 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700481
482 if (!iommu->ir_table)
483 return -ENOMEM;
484
Suresh Siddha824cd752009-10-02 11:01:23 -0700485 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
486 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700487
488 if (!pages) {
Jiang Liu360eb3c2014-01-06 14:18:08 +0800489 pr_err("IR%d: failed to allocate pages of order %d\n",
490 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700491 kfree(iommu->ir_table);
492 return -ENOMEM;
493 }
494
Jiang Liu360eb3c2014-01-06 14:18:08 +0800495 bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
496 sizeof(long), GFP_ATOMIC);
497 if (bitmap == NULL) {
498 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
499 __free_pages(pages, INTR_REMAP_PAGE_ORDER);
500 kfree(ir_table);
501 return -ENOMEM;
502 }
503
Suresh Siddha2ae21012008-07-10 11:16:43 -0700504 ir_table->base = page_address(pages);
Jiang Liu360eb3c2014-01-06 14:18:08 +0800505 ir_table->bitmap = bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700506
Suresh Siddha95a02e92012-03-30 11:47:07 -0700507 iommu_set_irq_remapping(iommu, mode);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700508 return 0;
509}
510
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700511/*
512 * Disable Interrupt Remapping.
513 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700514static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700515{
516 unsigned long flags;
517 u32 sts;
518
519 if (!ecap_ir_support(iommu->ecap))
520 return;
521
Fenghua Yub24696b2009-03-27 14:22:44 -0700522 /*
523 * global invalidation of interrupt entry cache before disabling
524 * interrupt-remapping.
525 */
526 qi_global_iec(iommu);
527
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200528 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700529
530 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
531 if (!(sts & DMA_GSTS_IRES))
532 goto end;
533
534 iommu->gcmd &= ~DMA_GCMD_IRE;
535 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
536
537 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
538 readl, !(sts & DMA_GSTS_IRES), sts);
539
540end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200541 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700542}
543
Suresh Siddha41750d32011-08-23 17:05:18 -0700544static int __init dmar_x2apic_optout(void)
545{
546 struct acpi_table_dmar *dmar;
547 dmar = (struct acpi_table_dmar *)dmar_tbl;
548 if (!dmar || no_x2apic_optout)
549 return 0;
550 return dmar->flags & DMAR_X2APIC_OPT_OUT;
551}
552
Suresh Siddha95a02e92012-03-30 11:47:07 -0700553static int __init intel_irq_remapping_supported(void)
Weidong Han93758232009-04-17 16:42:14 +0800554{
555 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800556 struct intel_iommu *iommu;
Weidong Han93758232009-04-17 16:42:14 +0800557
Suresh Siddha95a02e92012-03-30 11:47:07 -0700558 if (disable_irq_remap)
Weidong Han03ea8152009-04-17 16:42:15 +0800559 return 0;
Neil Horman03bbcb22013-04-16 16:38:32 -0400560 if (irq_remap_broken) {
Neil Horman05104a42013-09-27 12:53:35 -0400561 printk(KERN_WARNING
562 "This system BIOS has enabled interrupt remapping\n"
563 "on a chipset that contains an erratum making that\n"
564 "feature unstable. To maintain system stability\n"
565 "interrupt remapping is being disabled. Please\n"
566 "contact your BIOS vendor for an update\n");
567 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Neil Horman03bbcb22013-04-16 16:38:32 -0400568 disable_irq_remap = 1;
569 return 0;
570 }
Weidong Han03ea8152009-04-17 16:42:15 +0800571
Youquan Song074835f2009-09-09 12:05:39 -0400572 if (!dmar_ir_support())
573 return 0;
574
Jiang Liu7c919772014-01-06 14:18:18 +0800575 for_each_iommu(iommu, drhd)
Weidong Han93758232009-04-17 16:42:14 +0800576 if (!ecap_ir_support(iommu->ecap))
577 return 0;
Weidong Han93758232009-04-17 16:42:14 +0800578
579 return 1;
580}
581
Suresh Siddha95a02e92012-03-30 11:47:07 -0700582static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700583{
584 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800585 struct intel_iommu *iommu;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800586 bool x2apic_present;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700587 int setup = 0;
Suresh Siddha41750d32011-08-23 17:05:18 -0700588 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700589
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800590 x2apic_present = x2apic_supported();
591
Youquan Songe936d072009-09-07 10:58:07 -0400592 if (parse_ioapics_under_ir() != 1) {
593 printk(KERN_INFO "Not enable interrupt remapping\n");
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800594 goto error;
Youquan Songe936d072009-09-07 10:58:07 -0400595 }
596
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800597 if (x2apic_present) {
Jiang Liub977e732014-01-06 14:18:14 +0800598 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
599
Suresh Siddha41750d32011-08-23 17:05:18 -0700600 eim = !dmar_x2apic_optout();
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800601 if (!eim)
602 printk(KERN_WARNING
603 "Your BIOS is broken and requested that x2apic be disabled.\n"
604 "This will slightly decrease performance.\n"
605 "Use 'intremap=no_x2apic_optout' to override BIOS request.\n");
Suresh Siddha41750d32011-08-23 17:05:18 -0700606 }
607
Jiang Liu7c919772014-01-06 14:18:18 +0800608 for_each_iommu(iommu, drhd) {
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700609 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800610 * If the queued invalidation is already initialized,
611 * shouldn't disable it.
612 */
613 if (iommu->qi)
614 continue;
615
616 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700617 * Clear previous faults.
618 */
619 dmar_fault(-1, iommu);
620
621 /*
622 * Disable intr remapping and queued invalidation, if already
623 * enabled prior to OS handover.
624 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700625 iommu_disable_irq_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700626
627 dmar_disable_qi(iommu);
628 }
629
Suresh Siddha2ae21012008-07-10 11:16:43 -0700630 /*
631 * check for the Interrupt-remapping support
632 */
Jiang Liu7c919772014-01-06 14:18:18 +0800633 for_each_iommu(iommu, drhd) {
Suresh Siddha2ae21012008-07-10 11:16:43 -0700634 if (!ecap_ir_support(iommu->ecap))
635 continue;
636
637 if (eim && !ecap_eim_support(iommu->ecap)) {
638 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
639 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800640 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700641 }
642 }
643
644 /*
645 * Enable queued invalidation for all the DRHD's.
646 */
Jiang Liu7c919772014-01-06 14:18:18 +0800647 for_each_iommu(iommu, drhd) {
648 int ret = dmar_enable_qi(iommu);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700649
650 if (ret) {
651 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
652 " invalidation, ecap %Lx, ret %d\n",
653 drhd->reg_base_addr, iommu->ecap, ret);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800654 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700655 }
656 }
657
658 /*
659 * Setup Interrupt-remapping for all the DRHD's now.
660 */
Jiang Liu7c919772014-01-06 14:18:18 +0800661 for_each_iommu(iommu, drhd) {
Suresh Siddha2ae21012008-07-10 11:16:43 -0700662 if (!ecap_ir_support(iommu->ecap))
663 continue;
664
Suresh Siddha95a02e92012-03-30 11:47:07 -0700665 if (intel_setup_irq_remapping(iommu, eim))
Suresh Siddha2ae21012008-07-10 11:16:43 -0700666 goto error;
667
668 setup = 1;
669 }
670
671 if (!setup)
672 goto error;
673
Suresh Siddha95a02e92012-03-30 11:47:07 -0700674 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200675
676 /*
677 * VT-d has a different layout for IO-APIC entries when
678 * interrupt remapping is enabled. So it needs a special routine
679 * to print IO-APIC entries for debugging purposes too.
680 */
681 x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
682
Suresh Siddha41750d32011-08-23 17:05:18 -0700683 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700684
Suresh Siddha41750d32011-08-23 17:05:18 -0700685 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700686
687error:
688 /*
689 * handle error condition gracefully here!
690 */
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800691
692 if (x2apic_present)
Andy Lutomirskid01140d2013-05-13 15:22:42 -0700693 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800694
Suresh Siddha2ae21012008-07-10 11:16:43 -0700695 return -1;
696}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700697
Suresh Siddha20f30972009-08-04 12:07:08 -0700698static void ir_parse_one_hpet_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,
Suresh Siddha20f30972009-08-04 12:07:08 -0700716 PCI_SECONDARY_BUS);
717 path++;
718 }
719 ir_hpet[ir_hpet_num].bus = bus;
Lv Zhengfa5f5082013-10-31 09:30:22 +0800720 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->device, path->function);
Suresh Siddha20f30972009-08-04 12:07:08 -0700721 ir_hpet[ir_hpet_num].iommu = iommu;
722 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
723 ir_hpet_num++;
724}
725
Weidong Hanf007e992009-05-23 00:41:15 +0800726static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
727 struct intel_iommu *iommu)
728{
729 struct acpi_dmar_pci_path *path;
730 u8 bus;
731 int count;
732
733 bus = scope->bus;
734 path = (struct acpi_dmar_pci_path *)(scope + 1);
735 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
736 / sizeof(struct acpi_dmar_pci_path);
737
738 while (--count > 0) {
739 /*
740 * Access PCI directly due to the PCI
741 * subsystem isn't initialized yet.
742 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800743 bus = read_pci_config_byte(bus, path->device, path->function,
Weidong Hanf007e992009-05-23 00:41:15 +0800744 PCI_SECONDARY_BUS);
745 path++;
746 }
747
748 ir_ioapic[ir_ioapic_num].bus = bus;
Lv Zhengfa5f5082013-10-31 09:30:22 +0800749 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->device, path->function);
Weidong Hanf007e992009-05-23 00:41:15 +0800750 ir_ioapic[ir_ioapic_num].iommu = iommu;
751 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
752 ir_ioapic_num++;
753}
754
Suresh Siddha20f30972009-08-04 12:07:08 -0700755static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
756 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700757{
758 struct acpi_dmar_hardware_unit *drhd;
759 struct acpi_dmar_device_scope *scope;
760 void *start, *end;
761
762 drhd = (struct acpi_dmar_hardware_unit *)header;
763
764 start = (void *)(drhd + 1);
765 end = ((void *)drhd) + header->length;
766
767 while (start < end) {
768 scope = start;
769 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
770 if (ir_ioapic_num == MAX_IO_APICS) {
771 printk(KERN_WARNING "Exceeded Max IO APICS\n");
772 return -1;
773 }
774
Yinghai Lu680a7522010-04-08 19:58:23 +0100775 printk(KERN_INFO "IOAPIC id %d under DRHD base "
776 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
777 drhd->address, iommu->seq_id);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700778
Weidong Hanf007e992009-05-23 00:41:15 +0800779 ir_parse_one_ioapic_scope(scope, iommu);
Suresh Siddha20f30972009-08-04 12:07:08 -0700780 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
781 if (ir_hpet_num == MAX_HPET_TBS) {
782 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
783 return -1;
784 }
785
786 printk(KERN_INFO "HPET id %d under DRHD base"
787 " 0x%Lx\n", scope->enumeration_id,
788 drhd->address);
789
790 ir_parse_one_hpet_scope(scope, iommu);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700791 }
792 start += scope->length;
793 }
794
795 return 0;
796}
797
798/*
799 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
800 * hardware unit.
801 */
Jiang Liu694835d2014-01-06 14:18:16 +0800802static int __init parse_ioapics_under_ir(void)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700803{
804 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800805 struct intel_iommu *iommu;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700806 int ir_supported = 0;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500807 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700808
Jiang Liu7c919772014-01-06 14:18:18 +0800809 for_each_iommu(iommu, drhd)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700810 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700811 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700812 return -1;
813
814 ir_supported = 1;
815 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700816
Seth Forshee32ab31e2012-08-08 08:27:03 -0500817 if (!ir_supported)
818 return 0;
819
820 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
821 int ioapic_id = mpc_ioapic_id(ioapic_idx);
822 if (!map_ioapic_to_ir(ioapic_id)) {
823 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
824 "interrupt remapping will be disabled\n",
825 ioapic_id);
826 return -1;
827 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700828 }
829
Seth Forshee32ab31e2012-08-08 08:27:03 -0500830 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700831}
Fenghua Yub24696b2009-03-27 14:22:44 -0700832
Rashika Kheria6a7885c2013-12-18 12:04:27 +0530833static int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700834{
Jiang Liu3a5670e2014-02-19 14:07:33 +0800835 int ret;
836
Suresh Siddha95a02e92012-03-30 11:47:07 -0700837 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700838 return 0;
839
Jiang Liu3a5670e2014-02-19 14:07:33 +0800840 down_write(&dmar_global_lock);
841 ret = dmar_dev_scope_init();
842 up_write(&dmar_global_lock);
843
844 return ret;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700845}
846rootfs_initcall(ir_dev_scope_init);
847
Suresh Siddha95a02e92012-03-30 11:47:07 -0700848static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700849{
850 struct dmar_drhd_unit *drhd;
851 struct intel_iommu *iommu = NULL;
852
853 /*
854 * Disable Interrupt-remapping for all the DRHD's now.
855 */
856 for_each_iommu(iommu, drhd) {
857 if (!ecap_ir_support(iommu->ecap))
858 continue;
859
Suresh Siddha95a02e92012-03-30 11:47:07 -0700860 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700861 }
862}
863
Suresh Siddha95a02e92012-03-30 11:47:07 -0700864static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700865{
866 struct dmar_drhd_unit *drhd;
867 int setup = 0;
868 struct intel_iommu *iommu = NULL;
869
870 for_each_iommu(iommu, drhd)
871 if (iommu->qi)
872 dmar_reenable_qi(iommu);
873
874 /*
875 * Setup Interrupt-remapping for all the DRHD's now.
876 */
877 for_each_iommu(iommu, drhd) {
878 if (!ecap_ir_support(iommu->ecap))
879 continue;
880
881 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -0700882 iommu_set_irq_remapping(iommu, eim);
Fenghua Yub24696b2009-03-27 14:22:44 -0700883 setup = 1;
884 }
885
886 if (!setup)
887 goto error;
888
889 return 0;
890
891error:
892 /*
893 * handle error condition gracefully here!
894 */
895 return -1;
896}
897
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700898static void prepare_irte(struct irte *irte, int vector,
899 unsigned int dest)
900{
901 memset(irte, 0, sizeof(*irte));
902
903 irte->present = 1;
904 irte->dst_mode = apic->irq_dest_mode;
905 /*
906 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
907 * actual level or edge trigger will be setup in the IO-APIC
908 * RTE. This will help simplify level triggered irq migration.
909 * For more details, see the comments (in io_apic.c) explainig IO-APIC
910 * irq migration in the presence of interrupt-remapping.
911 */
912 irte->trigger_mode = 0;
913 irte->dlvry_mode = apic->irq_delivery_mode;
914 irte->vector = vector;
915 irte->dest_id = IRTE_DEST(dest);
916 irte->redir_hint = 1;
917}
918
919static int intel_setup_ioapic_entry(int irq,
920 struct IO_APIC_route_entry *route_entry,
921 unsigned int destination, int vector,
922 struct io_apic_irq_attr *attr)
923{
924 int ioapic_id = mpc_ioapic_id(attr->ioapic);
Jiang Liu3a5670e2014-02-19 14:07:33 +0800925 struct intel_iommu *iommu;
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700926 struct IR_IO_APIC_route_entry *entry;
927 struct irte irte;
928 int index;
929
Jiang Liu3a5670e2014-02-19 14:07:33 +0800930 down_read(&dmar_global_lock);
931 iommu = map_ioapic_to_ir(ioapic_id);
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700932 if (!iommu) {
933 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
Jiang Liu3a5670e2014-02-19 14:07:33 +0800934 index = -ENODEV;
935 } else {
936 index = alloc_irte(iommu, irq, 1);
937 if (index < 0) {
938 pr_warn("Failed to allocate IRTE for ioapic %d\n",
939 ioapic_id);
940 index = -ENOMEM;
941 }
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700942 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800943 up_read(&dmar_global_lock);
944 if (index < 0)
945 return index;
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700946
947 prepare_irte(&irte, vector, destination);
948
949 /* Set source-id of interrupt request */
950 set_ioapic_sid(&irte, ioapic_id);
951
952 modify_irte(irq, &irte);
953
954 apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
955 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
956 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
957 "Avail:%X Vector:%02X Dest:%08X "
958 "SID:%04X SQ:%X SVT:%X)\n",
959 attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
960 irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
961 irte.avail, irte.vector, irte.dest_id,
962 irte.sid, irte.sq, irte.svt);
963
Jiang Liu3a5670e2014-02-19 14:07:33 +0800964 entry = (struct IR_IO_APIC_route_entry *)route_entry;
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700965 memset(entry, 0, sizeof(*entry));
966
967 entry->index2 = (index >> 15) & 0x1;
968 entry->zero = 0;
969 entry->format = 1;
970 entry->index = (index & 0x7fff);
971 /*
972 * IO-APIC RTE will be configured with virtual vector.
973 * irq handler will do the explicit EOI to the io-apic.
974 */
975 entry->vector = attr->ioapic_pin;
976 entry->mask = 0; /* enable IRQ */
977 entry->trigger = attr->trigger;
978 entry->polarity = attr->polarity;
979
980 /* Mask level triggered irqs.
981 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
982 */
983 if (attr->trigger)
984 entry->mask = 1;
985
986 return 0;
987}
988
Joerg Roedel4c1bad62012-03-30 11:47:03 -0700989/*
990 * Migrate the IO-APIC irq in the presence of intr-remapping.
991 *
992 * For both level and edge triggered, irq migration is a simple atomic
993 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
994 *
995 * For level triggered, we eliminate the io-apic RTE modification (with the
996 * updated vector information), by using a virtual vector (io-apic pin number).
997 * Real vector that is used for interrupting cpu will be coming from
998 * the interrupt-remapping table entry.
999 *
1000 * As the migration is a simple atomic update of IRTE, the same mechanism
1001 * is used to migrate MSI irq's in the presence of interrupt-remapping.
1002 */
1003static int
1004intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
1005 bool force)
1006{
1007 struct irq_cfg *cfg = data->chip_data;
1008 unsigned int dest, irq = data->irq;
1009 struct irte irte;
Alexander Gordeevff164322012-06-07 15:15:59 +02001010 int err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001011
Suresh Siddha7eb9ae02012-06-14 18:28:49 -07001012 if (!config_enabled(CONFIG_SMP))
1013 return -EINVAL;
1014
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001015 if (!cpumask_intersects(mask, cpu_online_mask))
1016 return -EINVAL;
1017
1018 if (get_irte(irq, &irte))
1019 return -EBUSY;
1020
Alexander Gordeevff164322012-06-07 15:15:59 +02001021 err = assign_irq_vector(irq, cfg, mask);
1022 if (err)
1023 return err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001024
Alexander Gordeevff164322012-06-07 15:15:59 +02001025 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
1026 if (err) {
Dan Carpentered88bed2012-06-12 19:26:33 +03001027 if (assign_irq_vector(irq, cfg, data->affinity))
Alexander Gordeevff164322012-06-07 15:15:59 +02001028 pr_err("Failed to recover vector for irq %d\n", irq);
1029 return err;
1030 }
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001031
1032 irte.vector = cfg->vector;
1033 irte.dest_id = IRTE_DEST(dest);
1034
1035 /*
1036 * Atomically updates the IRTE with the new destination, vector
1037 * and flushes the interrupt entry cache.
1038 */
1039 modify_irte(irq, &irte);
1040
1041 /*
1042 * After this point, all the interrupts will start arriving
1043 * at the new destination. So, time to cleanup the previous
1044 * vector allocation.
1045 */
1046 if (cfg->move_in_progress)
1047 send_cleanup_vector(cfg);
1048
1049 cpumask_copy(data->affinity, mask);
1050 return 0;
1051}
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001052
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001053static void intel_compose_msi_msg(struct pci_dev *pdev,
1054 unsigned int irq, unsigned int dest,
1055 struct msi_msg *msg, u8 hpet_id)
1056{
1057 struct irq_cfg *cfg;
1058 struct irte irte;
Suresh Siddhac558df42012-05-08 00:08:54 -07001059 u16 sub_handle = 0;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001060 int ir_index;
1061
1062 cfg = irq_get_chip_data(irq);
1063
1064 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
1065 BUG_ON(ir_index == -1);
1066
1067 prepare_irte(&irte, cfg->vector, dest);
1068
1069 /* Set source-id of interrupt request */
1070 if (pdev)
1071 set_msi_sid(&irte, pdev);
1072 else
1073 set_hpet_sid(&irte, hpet_id);
1074
1075 modify_irte(irq, &irte);
1076
1077 msg->address_hi = MSI_ADDR_BASE_HI;
1078 msg->data = sub_handle;
1079 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1080 MSI_ADDR_IR_SHV |
1081 MSI_ADDR_IR_INDEX1(ir_index) |
1082 MSI_ADDR_IR_INDEX2(ir_index);
1083}
1084
1085/*
1086 * Map the PCI dev to the corresponding remapping hardware unit
1087 * and allocate 'nvec' consecutive interrupt-remapping table entries
1088 * in it.
1089 */
1090static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
1091{
1092 struct intel_iommu *iommu;
1093 int index;
1094
Jiang Liu3a5670e2014-02-19 14:07:33 +08001095 down_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001096 iommu = map_dev_to_ir(dev);
1097 if (!iommu) {
1098 printk(KERN_ERR
1099 "Unable to map PCI %s to iommu\n", pci_name(dev));
Jiang Liu3a5670e2014-02-19 14:07:33 +08001100 index = -ENOENT;
1101 } else {
1102 index = alloc_irte(iommu, irq, nvec);
1103 if (index < 0) {
1104 printk(KERN_ERR
1105 "Unable to allocate %d IRTE for PCI %s\n",
1106 nvec, pci_name(dev));
1107 index = -ENOSPC;
1108 }
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001109 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08001110 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001111
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001112 return index;
1113}
1114
1115static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
1116 int index, int sub_handle)
1117{
1118 struct intel_iommu *iommu;
Jiang Liu3a5670e2014-02-19 14:07:33 +08001119 int ret = -ENOENT;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001120
Jiang Liu3a5670e2014-02-19 14:07:33 +08001121 down_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001122 iommu = map_dev_to_ir(pdev);
Jiang Liu3a5670e2014-02-19 14:07:33 +08001123 if (iommu) {
1124 /*
1125 * setup the mapping between the irq and the IRTE
1126 * base index, the sub_handle pointing to the
1127 * appropriate interrupt remap table entry.
1128 */
1129 set_irte_irq(irq, iommu, index, sub_handle);
1130 ret = 0;
1131 }
1132 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001133
Jiang Liu3a5670e2014-02-19 14:07:33 +08001134 return ret;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001135}
1136
1137static int intel_setup_hpet_msi(unsigned int irq, unsigned int id)
1138{
Jiang Liu3a5670e2014-02-19 14:07:33 +08001139 int ret = -1;
1140 struct intel_iommu *iommu;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001141 int index;
1142
Jiang Liu3a5670e2014-02-19 14:07:33 +08001143 down_read(&dmar_global_lock);
1144 iommu = map_hpet_to_ir(id);
1145 if (iommu) {
1146 index = alloc_irte(iommu, irq, 1);
1147 if (index >= 0)
1148 ret = 0;
1149 }
1150 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001151
Jiang Liu3a5670e2014-02-19 14:07:33 +08001152 return ret;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001153}
1154
Joerg Roedel736baef2012-03-30 11:47:00 -07001155struct irq_remap_ops intel_irq_remap_ops = {
Suresh Siddha95a02e92012-03-30 11:47:07 -07001156 .supported = intel_irq_remapping_supported,
1157 .prepare = dmar_table_init,
1158 .enable = intel_enable_irq_remapping,
1159 .disable = disable_irq_remapping,
1160 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -07001161 .enable_faulting = enable_drhd_fault_handling,
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001162 .setup_ioapic_entry = intel_setup_ioapic_entry,
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001163 .set_affinity = intel_ioapic_set_affinity,
Joerg Roedel9d619f62012-03-30 11:47:04 -07001164 .free_irq = free_irte,
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001165 .compose_msi_msg = intel_compose_msi_msg,
1166 .msi_alloc_irq = intel_msi_alloc_irq,
1167 .msi_setup_irq = intel_msi_setup_irq,
1168 .setup_hpet_msi = intel_setup_hpet_msi,
Joerg Roedel736baef2012-03-30 11:47:00 -07001169};