blob: 3fe3fc78060c8feaf8304cd498e681b5eeb0f9e8 [file] [log] [blame]
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001
2#define pr_fmt(fmt) "DMAR-IR: " fmt
3
Yinghai Lu5aeecaf2008-08-19 20:49:59 -07004#include <linux/interrupt.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -07005#include <linux/dmar.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07006#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07008#include <linux/jiffies.h>
Suresh Siddha20f30972009-08-04 12:07:08 -07009#include <linux/hpet.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -070010#include <linux/pci.h>
Suresh Siddhab6fcb332008-07-10 11:16:44 -070011#include <linux/irq.h>
Lv Zheng8b484632013-12-03 08:49:16 +080012#include <linux/intel-iommu.h>
13#include <linux/acpi.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070014#include <asm/io_apic.h>
Yinghai Lu17483a12008-12-12 13:14:18 -080015#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053016#include <asm/cpu.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070017#include <asm/irq_remapping.h>
Weidong Hanf007e992009-05-23 00:41:15 +080018#include <asm/pci-direct.h>
Joerg Roedel5e2b9302012-03-30 11:47:05 -070019#include <asm/msidef.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070020
Suresh Siddha8a8f4222012-03-30 11:47:08 -070021#include "irq_remapping.h"
Joerg Roedel736baef2012-03-30 11:47:00 -070022
Joerg Roedeleef93fd2012-03-30 11:46:59 -070023struct ioapic_scope {
24 struct intel_iommu *iommu;
25 unsigned int id;
26 unsigned int bus; /* PCI bus number */
27 unsigned int devfn; /* PCI devfn number */
28};
29
30struct hpet_scope {
31 struct intel_iommu *iommu;
32 u8 id;
33 unsigned int bus;
34 unsigned int devfn;
35};
36
37#define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
Jiang Liu13d09b62015-01-07 15:31:37 +080038#define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
Joerg Roedeleef93fd2012-03-30 11:46:59 -070039
Jiang Liu13d09b62015-01-07 15:31:37 +080040static int __read_mostly eim_mode;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070041static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070042static struct hpet_scope ir_hpet[MAX_HPET_TBS];
Chris Wrightd1423d52010-07-20 11:06:49 -070043
Jiang Liu3a5670e2014-02-19 14:07:33 +080044/*
45 * Lock ordering:
46 * ->dmar_global_lock
47 * ->irq_2_ir_lock
48 * ->qi->q_lock
49 * ->iommu->register_lock
50 * Note:
51 * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
52 * in single-threaded environment with interrupt disabled, so no need to tabke
53 * the dmar_global_lock.
54 */
Thomas Gleixner96f8e982011-07-19 16:28:19 +020055static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Thomas Gleixnerd585d062010-10-10 12:34:27 +020056
Jiang Liu694835d2014-01-06 14:18:16 +080057static int __init parse_ioapics_under_ir(void);
58
Yinghai Lue420dfb2008-08-19 20:50:21 -070059static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
60{
Jiang Liu91411da2014-10-27 16:12:09 +080061 struct irq_cfg *cfg = irq_cfg(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020062 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080063}
64
Rashika Kheria6a7885c2013-12-18 12:04:27 +053065static int get_irte(int irq, struct irte *entry)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070066{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020067 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070068 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020069 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070070
Thomas Gleixnerd585d062010-10-10 12:34:27 +020071 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070072 return -1;
73
Thomas Gleixner96f8e982011-07-19 16:28:19 +020074 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070075
Greg Edwardsaf437462014-07-23 10:13:26 -060076 if (unlikely(!irq_iommu->iommu)) {
77 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
78 return -1;
79 }
80
Yinghai Lue420dfb2008-08-19 20:50:21 -070081 index = irq_iommu->irte_index + irq_iommu->sub_handle;
82 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070083
Thomas Gleixner96f8e982011-07-19 16:28:19 +020084 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070085 return 0;
86}
87
Joerg Roedel263b5e82012-03-30 11:47:06 -070088static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070089{
90 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020091 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Jiang Liu91411da2014-10-27 16:12:09 +080092 struct irq_cfg *cfg = irq_cfg(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070093 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070094 unsigned long flags;
Dan Carpenter9f4c7442014-01-09 08:32:36 +030095 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070096
Thomas Gleixnerd585d062010-10-10 12:34:27 +020097 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070098 return -1;
99
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700100 if (count > 1) {
101 count = __roundup_pow_of_two(count);
102 mask = ilog2(count);
103 }
104
105 if (mask > ecap_max_handle_mask(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200106 pr_err("Requested mask %x exceeds the max invalidation handle"
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700107 " mask value %Lx\n", mask,
108 ecap_max_handle_mask(iommu->ecap));
109 return -1;
110 }
111
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200112 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800113 index = bitmap_find_free_region(table->bitmap,
114 INTR_REMAP_TABLE_ENTRIES, mask);
115 if (index < 0) {
116 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
117 } else {
118 cfg->remapped = 1;
119 irq_iommu->iommu = iommu;
120 irq_iommu->irte_index = index;
121 irq_iommu->sub_handle = 0;
122 irq_iommu->irte_mask = mask;
123 }
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200124 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700125
126 return index;
127}
128
Yu Zhao704126a2009-01-04 16:28:52 +0800129static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700130{
131 struct qi_desc desc;
132
133 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
134 | QI_IEC_SELECTIVE;
135 desc.high = 0;
136
Yu Zhao704126a2009-01-04 16:28:52 +0800137 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700138}
139
Joerg Roedel263b5e82012-03-30 11:47:06 -0700140static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700141{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200142 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700143 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200144 int index;
145
146 if (!irq_iommu)
147 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700148
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200149 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700150 *sub_handle = irq_iommu->sub_handle;
151 index = irq_iommu->irte_index;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200152 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700153 return index;
154}
155
Joerg Roedel263b5e82012-03-30 11:47:06 -0700156static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700157{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200158 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Jiang Liu91411da2014-10-27 16:12:09 +0800159 struct irq_cfg *cfg = irq_cfg(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700160 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700161
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200162 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800163 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200164
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200165 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800166
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200167 cfg->remapped = 1;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700168 irq_iommu->iommu = iommu;
169 irq_iommu->irte_index = index;
170 irq_iommu->sub_handle = subhandle;
171 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700172
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200173 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700174
175 return 0;
176}
177
Joerg Roedel263b5e82012-03-30 11:47:06 -0700178static int modify_irte(int irq, struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700179{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200180 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700181 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700182 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200183 struct irte *irte;
184 int rc, index;
185
186 if (!irq_iommu)
187 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700188
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200189 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700190
Yinghai Lue420dfb2008-08-19 20:50:21 -0700191 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700192
Yinghai Lue420dfb2008-08-19 20:50:21 -0700193 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700194 irte = &iommu->ir_table->base[index];
195
Linus Torvaldsc513b672010-08-06 11:02:31 -0700196 set_64bit(&irte->low, irte_modified->low);
197 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700198 __iommu_flush_cache(iommu, irte, sizeof(*irte));
199
Yu Zhao704126a2009-01-04 16:28:52 +0800200 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200201 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800202
203 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700204}
205
Joerg Roedel263b5e82012-03-30 11:47:06 -0700206static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700207{
208 int i;
209
210 for (i = 0; i < MAX_HPET_TBS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800211 if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
Suresh Siddha20f30972009-08-04 12:07:08 -0700212 return ir_hpet[i].iommu;
213 return NULL;
214}
215
Joerg Roedel263b5e82012-03-30 11:47:06 -0700216static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700217{
218 int i;
219
220 for (i = 0; i < MAX_IO_APICS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800221 if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
Suresh Siddha89027d32008-07-10 11:16:56 -0700222 return ir_ioapic[i].iommu;
223 return NULL;
224}
225
Joerg Roedel263b5e82012-03-30 11:47:06 -0700226static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700227{
228 struct dmar_drhd_unit *drhd;
229
230 drhd = dmar_find_matched_drhd_unit(dev);
231 if (!drhd)
232 return NULL;
233
234 return drhd->iommu;
235}
236
Weidong Hanc4658b42009-05-23 00:41:14 +0800237static int clear_entries(struct irq_2_iommu *irq_iommu)
238{
239 struct irte *start, *entry, *end;
240 struct intel_iommu *iommu;
241 int index;
242
243 if (irq_iommu->sub_handle)
244 return 0;
245
246 iommu = irq_iommu->iommu;
247 index = irq_iommu->irte_index + irq_iommu->sub_handle;
248
249 start = iommu->ir_table->base + index;
250 end = start + (1 << irq_iommu->irte_mask);
251
252 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700253 set_64bit(&entry->low, 0);
254 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800255 }
Jiang Liu360eb3c52014-01-06 14:18:08 +0800256 bitmap_release_region(iommu->ir_table->bitmap, index,
257 irq_iommu->irte_mask);
Weidong Hanc4658b42009-05-23 00:41:14 +0800258
259 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
260}
261
Joerg Roedel9d619f62012-03-30 11:47:04 -0700262static int free_irte(int irq)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700263{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200264 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700265 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200266 int rc;
267
268 if (!irq_iommu)
269 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700270
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200271 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700272
Weidong Hanc4658b42009-05-23 00:41:14 +0800273 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700274
Yinghai Lue420dfb2008-08-19 20:50:21 -0700275 irq_iommu->iommu = NULL;
276 irq_iommu->irte_index = 0;
277 irq_iommu->sub_handle = 0;
278 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700279
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200280 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700281
Yu Zhao704126a2009-01-04 16:28:52 +0800282 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700283}
284
Weidong Hanf007e992009-05-23 00:41:15 +0800285/*
286 * source validation type
287 */
288#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300289#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800290#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
291
292/*
293 * source-id qualifier
294 */
295#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
296#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
297 * the third least significant bit
298 */
299#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
300 * the second and third least significant bits
301 */
302#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
303 * the least three significant bits
304 */
305
306/*
307 * set SVT, SQ and SID fields of irte to verify
308 * source ids of interrupt requests
309 */
310static void set_irte_sid(struct irte *irte, unsigned int svt,
311 unsigned int sq, unsigned int sid)
312{
Chris Wrightd1423d52010-07-20 11:06:49 -0700313 if (disable_sourceid_checking)
314 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800315 irte->svt = svt;
316 irte->sq = sq;
317 irte->sid = sid;
318}
319
Joerg Roedel263b5e82012-03-30 11:47:06 -0700320static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800321{
322 int i;
323 u16 sid = 0;
324
325 if (!irte)
326 return -1;
327
Jiang Liu3a5670e2014-02-19 14:07:33 +0800328 down_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800329 for (i = 0; i < MAX_IO_APICS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800330 if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
Weidong Hanf007e992009-05-23 00:41:15 +0800331 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
332 break;
333 }
334 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800335 up_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800336
337 if (sid == 0) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200338 pr_warn("Failed to set source-id of IOAPIC (%d)\n", apic);
Weidong Hanf007e992009-05-23 00:41:15 +0800339 return -1;
340 }
341
Jiang Liu2fe2c602014-01-06 14:18:17 +0800342 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
Weidong Hanf007e992009-05-23 00:41:15 +0800343
344 return 0;
345}
346
Joerg Roedel263b5e82012-03-30 11:47:06 -0700347static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700348{
349 int i;
350 u16 sid = 0;
351
352 if (!irte)
353 return -1;
354
Jiang Liu3a5670e2014-02-19 14:07:33 +0800355 down_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700356 for (i = 0; i < MAX_HPET_TBS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800357 if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700358 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
359 break;
360 }
361 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800362 up_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700363
364 if (sid == 0) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200365 pr_warn("Failed to set source-id of HPET block (%d)\n", id);
Suresh Siddha20f30972009-08-04 12:07:08 -0700366 return -1;
367 }
368
369 /*
370 * Should really use SQ_ALL_16. Some platforms are broken.
371 * While we figure out the right quirks for these broken platforms, use
372 * SQ_13_IGNORE_3 for now.
373 */
374 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
375
376 return 0;
377}
378
Alex Williamson579305f2014-07-03 09:51:43 -0600379struct set_msi_sid_data {
380 struct pci_dev *pdev;
381 u16 alias;
382};
383
384static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
385{
386 struct set_msi_sid_data *data = opaque;
387
388 data->pdev = pdev;
389 data->alias = alias;
390
391 return 0;
392}
393
Joerg Roedel263b5e82012-03-30 11:47:06 -0700394static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800395{
Alex Williamson579305f2014-07-03 09:51:43 -0600396 struct set_msi_sid_data data;
Weidong Hanf007e992009-05-23 00:41:15 +0800397
398 if (!irte || !dev)
399 return -1;
400
Alex Williamson579305f2014-07-03 09:51:43 -0600401 pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
Weidong Hanf007e992009-05-23 00:41:15 +0800402
Alex Williamson579305f2014-07-03 09:51:43 -0600403 /*
404 * DMA alias provides us with a PCI device and alias. The only case
405 * where the it will return an alias on a different bus than the
406 * device is the case of a PCIe-to-PCI bridge, where the alias is for
407 * the subordinate bus. In this case we can only verify the bus.
408 *
409 * If the alias device is on a different bus than our source device
410 * then we have a topology based alias, use it.
411 *
412 * Otherwise, the alias is for a device DMA quirk and we cannot
413 * assume that MSI uses the same requester ID. Therefore use the
414 * original device.
415 */
416 if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
417 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
418 PCI_DEVID(PCI_BUS_NUM(data.alias),
419 dev->bus->number));
420 else if (data.pdev->bus->number != dev->bus->number)
421 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
422 else
423 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
424 PCI_DEVID(dev->bus->number, dev->devfn));
Weidong Hanf007e992009-05-23 00:41:15 +0800425
426 return 0;
427}
428
Suresh Siddha95a02e92012-03-30 11:47:07 -0700429static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700430{
431 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100432 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700433 unsigned long flags;
434
435 addr = virt_to_phys((void *)iommu->ir_table->base);
436
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200437 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700438
439 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
440 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
441
442 /* Set interrupt-remapping table pointer */
Jan Kiszkaf63ef692014-08-11 13:13:25 +0200443 writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700444
445 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
446 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200447 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700448
449 /*
450 * global invalidation of interrupt entry cache before enabling
451 * interrupt-remapping.
452 */
453 qi_global_iec(iommu);
454
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200455 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700456
457 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700458 iommu->gcmd |= DMA_GCMD_IRE;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800459 iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
David Woodhousec416daa2009-05-10 20:30:58 +0100460 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700461
462 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
463 readl, (sts & DMA_GSTS_IRES), sts);
464
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800465 /*
466 * With CFI clear in the Global Command register, we should be
467 * protected from dangerous (i.e. compatibility) interrupts
468 * regardless of x2apic status. Check just to be sure.
469 */
470 if (sts & DMA_GSTS_CFIS)
471 WARN(1, KERN_WARNING
472 "Compatibility-format IRQs enabled despite intr remapping;\n"
473 "you are vulnerable to IRQ injection.\n");
474
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200475 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700476}
477
Jiang Liua7a3dad2014-11-09 22:48:00 +0800478static int intel_setup_irq_remapping(struct intel_iommu *iommu)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700479{
480 struct ir_table *ir_table;
481 struct page *pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800482 unsigned long *bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700483
Jiang Liua7a3dad2014-11-09 22:48:00 +0800484 if (iommu->ir_table)
485 return 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700486
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800487 ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800488 if (!ir_table)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700489 return -ENOMEM;
490
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800491 pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO,
Suresh Siddha824cd752009-10-02 11:01:23 -0700492 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700493
494 if (!pages) {
Jiang Liu360eb3c52014-01-06 14:18:08 +0800495 pr_err("IR%d: failed to allocate pages of order %d\n",
496 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800497 goto out_free_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700498 }
499
Jiang Liu360eb3c52014-01-06 14:18:08 +0800500 bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
501 sizeof(long), GFP_ATOMIC);
502 if (bitmap == NULL) {
503 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800504 goto out_free_pages;
Jiang Liu360eb3c52014-01-06 14:18:08 +0800505 }
506
Suresh Siddha2ae21012008-07-10 11:16:43 -0700507 ir_table->base = page_address(pages);
Jiang Liu360eb3c52014-01-06 14:18:08 +0800508 ir_table->bitmap = bitmap;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800509 iommu->ir_table = ir_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700510 return 0;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800511
512out_free_pages:
513 __free_pages(pages, INTR_REMAP_PAGE_ORDER);
514out_free_table:
515 kfree(ir_table);
516 return -ENOMEM;
517}
518
519static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
520{
521 if (iommu && iommu->ir_table) {
522 free_pages((unsigned long)iommu->ir_table->base,
523 INTR_REMAP_PAGE_ORDER);
524 kfree(iommu->ir_table->bitmap);
525 kfree(iommu->ir_table);
526 iommu->ir_table = NULL;
527 }
Suresh Siddha2ae21012008-07-10 11:16:43 -0700528}
529
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700530/*
531 * Disable Interrupt Remapping.
532 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700533static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700534{
535 unsigned long flags;
536 u32 sts;
537
538 if (!ecap_ir_support(iommu->ecap))
539 return;
540
Fenghua Yub24696b2009-03-27 14:22:44 -0700541 /*
542 * global invalidation of interrupt entry cache before disabling
543 * interrupt-remapping.
544 */
545 qi_global_iec(iommu);
546
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200547 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700548
549 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
550 if (!(sts & DMA_GSTS_IRES))
551 goto end;
552
553 iommu->gcmd &= ~DMA_GCMD_IRE;
554 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
555
556 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
557 readl, !(sts & DMA_GSTS_IRES), sts);
558
559end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200560 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700561}
562
Suresh Siddha41750d32011-08-23 17:05:18 -0700563static int __init dmar_x2apic_optout(void)
564{
565 struct acpi_table_dmar *dmar;
566 dmar = (struct acpi_table_dmar *)dmar_tbl;
567 if (!dmar || no_x2apic_optout)
568 return 0;
569 return dmar->flags & DMAR_X2APIC_OPT_OUT;
570}
571
Thomas Gleixner11190302015-01-07 15:31:29 +0800572static void __init intel_cleanup_irq_remapping(void)
573{
574 struct dmar_drhd_unit *drhd;
575 struct intel_iommu *iommu;
576
577 for_each_iommu(iommu, drhd) {
578 if (ecap_ir_support(iommu->ecap)) {
579 iommu_disable_irq_remapping(iommu);
580 intel_teardown_irq_remapping(iommu);
581 }
582 }
583
584 if (x2apic_supported())
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200585 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
Thomas Gleixner11190302015-01-07 15:31:29 +0800586}
587
588static int __init intel_prepare_irq_remapping(void)
589{
590 struct dmar_drhd_unit *drhd;
591 struct intel_iommu *iommu;
592
Jiang Liu2966d952015-01-07 15:31:35 +0800593 if (irq_remap_broken) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200594 pr_warn("This system BIOS has enabled interrupt remapping\n"
Jiang Liu2966d952015-01-07 15:31:35 +0800595 "on a chipset that contains an erratum making that\n"
596 "feature unstable. To maintain system stability\n"
597 "interrupt remapping is being disabled. Please\n"
598 "contact your BIOS vendor for an update\n");
599 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Jiang Liu2966d952015-01-07 15:31:35 +0800600 return -ENODEV;
601 }
602
Thomas Gleixner11190302015-01-07 15:31:29 +0800603 if (dmar_table_init() < 0)
Jiang Liu2966d952015-01-07 15:31:35 +0800604 return -ENODEV;
605
606 if (!dmar_ir_support())
607 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800608
609 if (parse_ioapics_under_ir() != 1) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200610 pr_info("Not enabling interrupt remapping\n");
Thomas Gleixner11190302015-01-07 15:31:29 +0800611 goto error;
612 }
613
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800614 /* First make sure all IOMMUs support IRQ remapping */
Jiang Liu2966d952015-01-07 15:31:35 +0800615 for_each_iommu(iommu, drhd)
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800616 if (!ecap_ir_support(iommu->ecap))
Thomas Gleixner11190302015-01-07 15:31:29 +0800617 goto error;
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800618
619 /* Do the allocations early */
620 for_each_iommu(iommu, drhd)
621 if (intel_setup_irq_remapping(iommu))
622 goto error;
623
Thomas Gleixner11190302015-01-07 15:31:29 +0800624 return 0;
Jiang Liu2966d952015-01-07 15:31:35 +0800625
Thomas Gleixner11190302015-01-07 15:31:29 +0800626error:
627 intel_cleanup_irq_remapping();
Jiang Liu2966d952015-01-07 15:31:35 +0800628 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800629}
630
Suresh Siddha95a02e92012-03-30 11:47:07 -0700631static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700632{
633 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800634 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100635 bool setup = false;
Suresh Siddha41750d32011-08-23 17:05:18 -0700636 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700637
Thomas Gleixner11190302015-01-07 15:31:29 +0800638 if (x2apic_supported()) {
Suresh Siddha41750d32011-08-23 17:05:18 -0700639 eim = !dmar_x2apic_optout();
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800640 if (!eim)
Fenghua Yu68c1b892015-02-21 13:07:27 -0800641 pr_info("x2apic is disabled because BIOS sets x2apic opt out bit. You can use 'intremap=no_x2apic_optout' to override the BIOS setting.\n");
Suresh Siddha41750d32011-08-23 17:05:18 -0700642 }
643
Jiang Liu7c919772014-01-06 14:18:18 +0800644 for_each_iommu(iommu, drhd) {
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700645 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800646 * If the queued invalidation is already initialized,
647 * shouldn't disable it.
648 */
649 if (iommu->qi)
650 continue;
651
652 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700653 * Clear previous faults.
654 */
655 dmar_fault(-1, iommu);
656
657 /*
658 * Disable intr remapping and queued invalidation, if already
659 * enabled prior to OS handover.
660 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700661 iommu_disable_irq_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700662
663 dmar_disable_qi(iommu);
664 }
665
Suresh Siddha2ae21012008-07-10 11:16:43 -0700666 /*
667 * check for the Interrupt-remapping support
668 */
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800669 for_each_iommu(iommu, drhd)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700670 if (eim && !ecap_eim_support(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200671 pr_info("DRHD %Lx: EIM not supported by DRHD, "
672 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
Jiang Liu13d09b62015-01-07 15:31:37 +0800673 eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700674 }
Jiang Liu13d09b62015-01-07 15:31:37 +0800675 eim_mode = eim;
676 if (eim)
677 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700678
679 /*
680 * Enable queued invalidation for all the DRHD's.
681 */
Jiang Liu7c919772014-01-06 14:18:18 +0800682 for_each_iommu(iommu, drhd) {
683 int ret = dmar_enable_qi(iommu);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700684
685 if (ret) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200686 pr_err("DRHD %Lx: failed to enable queued, "
Suresh Siddha2ae21012008-07-10 11:16:43 -0700687 " invalidation, ecap %Lx, ret %d\n",
688 drhd->reg_base_addr, iommu->ecap, ret);
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800689 goto error;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700690 }
691 }
692
693 /*
694 * Setup Interrupt-remapping for all the DRHD's now.
695 */
Jiang Liu7c919772014-01-06 14:18:18 +0800696 for_each_iommu(iommu, drhd) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800697 iommu_set_irq_remapping(iommu, eim);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100698 setup = true;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700699 }
700
701 if (!setup)
702 goto error;
703
Suresh Siddha95a02e92012-03-30 11:47:07 -0700704 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200705
706 /*
707 * VT-d has a different layout for IO-APIC entries when
708 * interrupt remapping is enabled. So it needs a special routine
709 * to print IO-APIC entries for debugging purposes too.
710 */
711 x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
712
Suresh Siddha41750d32011-08-23 17:05:18 -0700713 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700714
Suresh Siddha41750d32011-08-23 17:05:18 -0700715 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700716
717error:
Thomas Gleixner11190302015-01-07 15:31:29 +0800718 intel_cleanup_irq_remapping();
Suresh Siddha2ae21012008-07-10 11:16:43 -0700719 return -1;
720}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700721
Jiang Liua7a3dad2014-11-09 22:48:00 +0800722static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
723 struct intel_iommu *iommu,
724 struct acpi_dmar_hardware_unit *drhd)
Suresh Siddha20f30972009-08-04 12:07:08 -0700725{
726 struct acpi_dmar_pci_path *path;
727 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800728 int count, free = -1;
Suresh Siddha20f30972009-08-04 12:07:08 -0700729
730 bus = scope->bus;
731 path = (struct acpi_dmar_pci_path *)(scope + 1);
732 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
733 / sizeof(struct acpi_dmar_pci_path);
734
735 while (--count > 0) {
736 /*
737 * Access PCI directly due to the PCI
738 * subsystem isn't initialized yet.
739 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800740 bus = read_pci_config_byte(bus, path->device, path->function,
Suresh Siddha20f30972009-08-04 12:07:08 -0700741 PCI_SECONDARY_BUS);
742 path++;
743 }
Jiang Liua7a3dad2014-11-09 22:48:00 +0800744
745 for (count = 0; count < MAX_HPET_TBS; count++) {
746 if (ir_hpet[count].iommu == iommu &&
747 ir_hpet[count].id == scope->enumeration_id)
748 return 0;
749 else if (ir_hpet[count].iommu == NULL && free == -1)
750 free = count;
751 }
752 if (free == -1) {
753 pr_warn("Exceeded Max HPET blocks\n");
754 return -ENOSPC;
755 }
756
757 ir_hpet[free].iommu = iommu;
758 ir_hpet[free].id = scope->enumeration_id;
759 ir_hpet[free].bus = bus;
760 ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
761 pr_info("HPET id %d under DRHD base 0x%Lx\n",
762 scope->enumeration_id, drhd->address);
763
764 return 0;
Suresh Siddha20f30972009-08-04 12:07:08 -0700765}
766
Jiang Liua7a3dad2014-11-09 22:48:00 +0800767static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
768 struct intel_iommu *iommu,
769 struct acpi_dmar_hardware_unit *drhd)
Weidong Hanf007e992009-05-23 00:41:15 +0800770{
771 struct acpi_dmar_pci_path *path;
772 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800773 int count, free = -1;
Weidong Hanf007e992009-05-23 00:41:15 +0800774
775 bus = scope->bus;
776 path = (struct acpi_dmar_pci_path *)(scope + 1);
777 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
778 / sizeof(struct acpi_dmar_pci_path);
779
780 while (--count > 0) {
781 /*
782 * Access PCI directly due to the PCI
783 * subsystem isn't initialized yet.
784 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800785 bus = read_pci_config_byte(bus, path->device, path->function,
Weidong Hanf007e992009-05-23 00:41:15 +0800786 PCI_SECONDARY_BUS);
787 path++;
788 }
789
Jiang Liua7a3dad2014-11-09 22:48:00 +0800790 for (count = 0; count < MAX_IO_APICS; count++) {
791 if (ir_ioapic[count].iommu == iommu &&
792 ir_ioapic[count].id == scope->enumeration_id)
793 return 0;
794 else if (ir_ioapic[count].iommu == NULL && free == -1)
795 free = count;
796 }
797 if (free == -1) {
798 pr_warn("Exceeded Max IO APICS\n");
799 return -ENOSPC;
800 }
801
802 ir_ioapic[free].bus = bus;
803 ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
804 ir_ioapic[free].iommu = iommu;
805 ir_ioapic[free].id = scope->enumeration_id;
806 pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
807 scope->enumeration_id, drhd->address, iommu->seq_id);
808
809 return 0;
Weidong Hanf007e992009-05-23 00:41:15 +0800810}
811
Suresh Siddha20f30972009-08-04 12:07:08 -0700812static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
813 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700814{
Jiang Liua7a3dad2014-11-09 22:48:00 +0800815 int ret = 0;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700816 struct acpi_dmar_hardware_unit *drhd;
817 struct acpi_dmar_device_scope *scope;
818 void *start, *end;
819
820 drhd = (struct acpi_dmar_hardware_unit *)header;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700821 start = (void *)(drhd + 1);
822 end = ((void *)drhd) + header->length;
823
Jiang Liua7a3dad2014-11-09 22:48:00 +0800824 while (start < end && ret == 0) {
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700825 scope = start;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800826 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
827 ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
828 else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
829 ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700830 start += scope->length;
831 }
832
Jiang Liua7a3dad2014-11-09 22:48:00 +0800833 return ret;
834}
835
836static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
837{
838 int i;
839
840 for (i = 0; i < MAX_HPET_TBS; i++)
841 if (ir_hpet[i].iommu == iommu)
842 ir_hpet[i].iommu = NULL;
843
844 for (i = 0; i < MAX_IO_APICS; i++)
845 if (ir_ioapic[i].iommu == iommu)
846 ir_ioapic[i].iommu = NULL;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700847}
848
849/*
850 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
851 * hardware unit.
852 */
Jiang Liu694835d2014-01-06 14:18:16 +0800853static int __init parse_ioapics_under_ir(void)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700854{
855 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800856 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100857 bool ir_supported = false;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500858 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700859
Jiang Liu7c919772014-01-06 14:18:18 +0800860 for_each_iommu(iommu, drhd)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700861 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700862 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700863 return -1;
864
Quentin Lambert2f119c72015-02-06 10:59:53 +0100865 ir_supported = true;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700866 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700867
Seth Forshee32ab31e2012-08-08 08:27:03 -0500868 if (!ir_supported)
869 return 0;
870
871 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
872 int ioapic_id = mpc_ioapic_id(ioapic_idx);
873 if (!map_ioapic_to_ir(ioapic_id)) {
874 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
875 "interrupt remapping will be disabled\n",
876 ioapic_id);
877 return -1;
878 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700879 }
880
Seth Forshee32ab31e2012-08-08 08:27:03 -0500881 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700882}
Fenghua Yub24696b2009-03-27 14:22:44 -0700883
Rashika Kheria6a7885c2013-12-18 12:04:27 +0530884static int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700885{
Jiang Liu3a5670e2014-02-19 14:07:33 +0800886 int ret;
887
Suresh Siddha95a02e92012-03-30 11:47:07 -0700888 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700889 return 0;
890
Jiang Liu3a5670e2014-02-19 14:07:33 +0800891 down_write(&dmar_global_lock);
892 ret = dmar_dev_scope_init();
893 up_write(&dmar_global_lock);
894
895 return ret;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700896}
897rootfs_initcall(ir_dev_scope_init);
898
Suresh Siddha95a02e92012-03-30 11:47:07 -0700899static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700900{
901 struct dmar_drhd_unit *drhd;
902 struct intel_iommu *iommu = NULL;
903
904 /*
905 * Disable Interrupt-remapping for all the DRHD's now.
906 */
907 for_each_iommu(iommu, drhd) {
908 if (!ecap_ir_support(iommu->ecap))
909 continue;
910
Suresh Siddha95a02e92012-03-30 11:47:07 -0700911 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700912 }
913}
914
Suresh Siddha95a02e92012-03-30 11:47:07 -0700915static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700916{
917 struct dmar_drhd_unit *drhd;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100918 bool setup = false;
Fenghua Yub24696b2009-03-27 14:22:44 -0700919 struct intel_iommu *iommu = NULL;
920
921 for_each_iommu(iommu, drhd)
922 if (iommu->qi)
923 dmar_reenable_qi(iommu);
924
925 /*
926 * Setup Interrupt-remapping for all the DRHD's now.
927 */
928 for_each_iommu(iommu, drhd) {
929 if (!ecap_ir_support(iommu->ecap))
930 continue;
931
932 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -0700933 iommu_set_irq_remapping(iommu, eim);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100934 setup = true;
Fenghua Yub24696b2009-03-27 14:22:44 -0700935 }
936
937 if (!setup)
938 goto error;
939
940 return 0;
941
942error:
943 /*
944 * handle error condition gracefully here!
945 */
946 return -1;
947}
948
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700949static void prepare_irte(struct irte *irte, int vector,
950 unsigned int dest)
951{
952 memset(irte, 0, sizeof(*irte));
953
954 irte->present = 1;
955 irte->dst_mode = apic->irq_dest_mode;
956 /*
957 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
958 * actual level or edge trigger will be setup in the IO-APIC
959 * RTE. This will help simplify level triggered irq migration.
960 * For more details, see the comments (in io_apic.c) explainig IO-APIC
961 * irq migration in the presence of interrupt-remapping.
962 */
963 irte->trigger_mode = 0;
964 irte->dlvry_mode = apic->irq_delivery_mode;
965 irte->vector = vector;
966 irte->dest_id = IRTE_DEST(dest);
967 irte->redir_hint = 1;
968}
969
970static int intel_setup_ioapic_entry(int irq,
971 struct IO_APIC_route_entry *route_entry,
972 unsigned int destination, int vector,
973 struct io_apic_irq_attr *attr)
974{
975 int ioapic_id = mpc_ioapic_id(attr->ioapic);
Jiang Liu3a5670e2014-02-19 14:07:33 +0800976 struct intel_iommu *iommu;
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700977 struct IR_IO_APIC_route_entry *entry;
978 struct irte irte;
979 int index;
980
Jiang Liu3a5670e2014-02-19 14:07:33 +0800981 down_read(&dmar_global_lock);
982 iommu = map_ioapic_to_ir(ioapic_id);
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700983 if (!iommu) {
984 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
Jiang Liu3a5670e2014-02-19 14:07:33 +0800985 index = -ENODEV;
986 } else {
987 index = alloc_irte(iommu, irq, 1);
988 if (index < 0) {
989 pr_warn("Failed to allocate IRTE for ioapic %d\n",
990 ioapic_id);
991 index = -ENOMEM;
992 }
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700993 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800994 up_read(&dmar_global_lock);
995 if (index < 0)
996 return index;
Joerg Roedel0c3f1732012-03-30 11:47:02 -0700997
998 prepare_irte(&irte, vector, destination);
999
1000 /* Set source-id of interrupt request */
1001 set_ioapic_sid(&irte, ioapic_id);
1002
1003 modify_irte(irq, &irte);
1004
1005 apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
1006 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
1007 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
1008 "Avail:%X Vector:%02X Dest:%08X "
1009 "SID:%04X SQ:%X SVT:%X)\n",
1010 attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
1011 irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
1012 irte.avail, irte.vector, irte.dest_id,
1013 irte.sid, irte.sq, irte.svt);
1014
Jiang Liu3a5670e2014-02-19 14:07:33 +08001015 entry = (struct IR_IO_APIC_route_entry *)route_entry;
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001016 memset(entry, 0, sizeof(*entry));
1017
1018 entry->index2 = (index >> 15) & 0x1;
1019 entry->zero = 0;
1020 entry->format = 1;
1021 entry->index = (index & 0x7fff);
1022 /*
1023 * IO-APIC RTE will be configured with virtual vector.
1024 * irq handler will do the explicit EOI to the io-apic.
1025 */
1026 entry->vector = attr->ioapic_pin;
1027 entry->mask = 0; /* enable IRQ */
1028 entry->trigger = attr->trigger;
1029 entry->polarity = attr->polarity;
1030
1031 /* Mask level triggered irqs.
1032 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1033 */
1034 if (attr->trigger)
1035 entry->mask = 1;
1036
1037 return 0;
1038}
1039
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001040/*
1041 * Migrate the IO-APIC irq in the presence of intr-remapping.
1042 *
1043 * For both level and edge triggered, irq migration is a simple atomic
1044 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
1045 *
1046 * For level triggered, we eliminate the io-apic RTE modification (with the
1047 * updated vector information), by using a virtual vector (io-apic pin number).
1048 * Real vector that is used for interrupting cpu will be coming from
1049 * the interrupt-remapping table entry.
1050 *
1051 * As the migration is a simple atomic update of IRTE, the same mechanism
1052 * is used to migrate MSI irq's in the presence of interrupt-remapping.
1053 */
1054static int
1055intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
1056 bool force)
1057{
Jiang Liu91411da2014-10-27 16:12:09 +08001058 struct irq_cfg *cfg = irqd_cfg(data);
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001059 unsigned int dest, irq = data->irq;
1060 struct irte irte;
Alexander Gordeevff164322012-06-07 15:15:59 +02001061 int err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001062
Suresh Siddha7eb9ae02012-06-14 18:28:49 -07001063 if (!config_enabled(CONFIG_SMP))
1064 return -EINVAL;
1065
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001066 if (!cpumask_intersects(mask, cpu_online_mask))
1067 return -EINVAL;
1068
1069 if (get_irte(irq, &irte))
1070 return -EBUSY;
1071
Alexander Gordeevff164322012-06-07 15:15:59 +02001072 err = assign_irq_vector(irq, cfg, mask);
1073 if (err)
1074 return err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001075
Alexander Gordeevff164322012-06-07 15:15:59 +02001076 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
1077 if (err) {
Dan Carpentered88bed2012-06-12 19:26:33 +03001078 if (assign_irq_vector(irq, cfg, data->affinity))
Alexander Gordeevff164322012-06-07 15:15:59 +02001079 pr_err("Failed to recover vector for irq %d\n", irq);
1080 return err;
1081 }
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001082
1083 irte.vector = cfg->vector;
1084 irte.dest_id = IRTE_DEST(dest);
1085
1086 /*
1087 * Atomically updates the IRTE with the new destination, vector
1088 * and flushes the interrupt entry cache.
1089 */
1090 modify_irte(irq, &irte);
1091
1092 /*
1093 * After this point, all the interrupts will start arriving
1094 * at the new destination. So, time to cleanup the previous
1095 * vector allocation.
1096 */
1097 if (cfg->move_in_progress)
1098 send_cleanup_vector(cfg);
1099
1100 cpumask_copy(data->affinity, mask);
1101 return 0;
1102}
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001103
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001104static void intel_compose_msi_msg(struct pci_dev *pdev,
1105 unsigned int irq, unsigned int dest,
1106 struct msi_msg *msg, u8 hpet_id)
1107{
1108 struct irq_cfg *cfg;
1109 struct irte irte;
Suresh Siddhac558df42012-05-08 00:08:54 -07001110 u16 sub_handle = 0;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001111 int ir_index;
1112
Jiang Liu91411da2014-10-27 16:12:09 +08001113 cfg = irq_cfg(irq);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001114
1115 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
1116 BUG_ON(ir_index == -1);
1117
1118 prepare_irte(&irte, cfg->vector, dest);
1119
1120 /* Set source-id of interrupt request */
1121 if (pdev)
1122 set_msi_sid(&irte, pdev);
1123 else
1124 set_hpet_sid(&irte, hpet_id);
1125
1126 modify_irte(irq, &irte);
1127
1128 msg->address_hi = MSI_ADDR_BASE_HI;
1129 msg->data = sub_handle;
1130 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1131 MSI_ADDR_IR_SHV |
1132 MSI_ADDR_IR_INDEX1(ir_index) |
1133 MSI_ADDR_IR_INDEX2(ir_index);
1134}
1135
1136/*
1137 * Map the PCI dev to the corresponding remapping hardware unit
1138 * and allocate 'nvec' consecutive interrupt-remapping table entries
1139 * in it.
1140 */
1141static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
1142{
1143 struct intel_iommu *iommu;
1144 int index;
1145
Jiang Liu3a5670e2014-02-19 14:07:33 +08001146 down_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001147 iommu = map_dev_to_ir(dev);
1148 if (!iommu) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001149 pr_err("Unable to map PCI %s to iommu\n", pci_name(dev));
Jiang Liu3a5670e2014-02-19 14:07:33 +08001150 index = -ENOENT;
1151 } else {
1152 index = alloc_irte(iommu, irq, nvec);
1153 if (index < 0) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001154 pr_err("Unable to allocate %d IRTE for PCI %s\n",
Jiang Liu3a5670e2014-02-19 14:07:33 +08001155 nvec, pci_name(dev));
1156 index = -ENOSPC;
1157 }
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001158 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08001159 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001160
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001161 return index;
1162}
1163
1164static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
1165 int index, int sub_handle)
1166{
1167 struct intel_iommu *iommu;
Jiang Liu3a5670e2014-02-19 14:07:33 +08001168 int ret = -ENOENT;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001169
Jiang Liu3a5670e2014-02-19 14:07:33 +08001170 down_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001171 iommu = map_dev_to_ir(pdev);
Jiang Liu3a5670e2014-02-19 14:07:33 +08001172 if (iommu) {
1173 /*
1174 * setup the mapping between the irq and the IRTE
1175 * base index, the sub_handle pointing to the
1176 * appropriate interrupt remap table entry.
1177 */
1178 set_irte_irq(irq, iommu, index, sub_handle);
1179 ret = 0;
1180 }
1181 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001182
Jiang Liu3a5670e2014-02-19 14:07:33 +08001183 return ret;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001184}
1185
Yijing Wang5fc24d82014-09-17 17:32:19 +08001186static int intel_alloc_hpet_msi(unsigned int irq, unsigned int id)
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001187{
Jiang Liu3a5670e2014-02-19 14:07:33 +08001188 int ret = -1;
1189 struct intel_iommu *iommu;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001190 int index;
1191
Jiang Liu3a5670e2014-02-19 14:07:33 +08001192 down_read(&dmar_global_lock);
1193 iommu = map_hpet_to_ir(id);
1194 if (iommu) {
1195 index = alloc_irte(iommu, irq, 1);
1196 if (index >= 0)
1197 ret = 0;
1198 }
1199 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001200
Jiang Liu3a5670e2014-02-19 14:07:33 +08001201 return ret;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001202}
1203
Joerg Roedel736baef2012-03-30 11:47:00 -07001204struct irq_remap_ops intel_irq_remap_ops = {
Thomas Gleixner11190302015-01-07 15:31:29 +08001205 .prepare = intel_prepare_irq_remapping,
Suresh Siddha95a02e92012-03-30 11:47:07 -07001206 .enable = intel_enable_irq_remapping,
1207 .disable = disable_irq_remapping,
1208 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -07001209 .enable_faulting = enable_drhd_fault_handling,
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001210 .setup_ioapic_entry = intel_setup_ioapic_entry,
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001211 .set_affinity = intel_ioapic_set_affinity,
Joerg Roedel9d619f62012-03-30 11:47:04 -07001212 .free_irq = free_irte,
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001213 .compose_msi_msg = intel_compose_msi_msg,
1214 .msi_alloc_irq = intel_msi_alloc_irq,
1215 .msi_setup_irq = intel_msi_setup_irq,
Yijing Wang5fc24d82014-09-17 17:32:19 +08001216 .alloc_hpet_msi = intel_alloc_hpet_msi,
Joerg Roedel736baef2012-03-30 11:47:00 -07001217};
Jiang Liu6b197242014-11-09 22:47:58 +08001218
Jiang Liua7a3dad2014-11-09 22:48:00 +08001219/*
1220 * Support of Interrupt Remapping Unit Hotplug
1221 */
1222static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
1223{
1224 int ret;
1225 int eim = x2apic_enabled();
1226
1227 if (eim && !ecap_eim_support(iommu->ecap)) {
1228 pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
1229 iommu->reg_phys, iommu->ecap);
1230 return -ENODEV;
1231 }
1232
1233 if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
1234 pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
1235 iommu->reg_phys);
1236 return -ENODEV;
1237 }
1238
1239 /* TODO: check all IOAPICs are covered by IOMMU */
1240
1241 /* Setup Interrupt-remapping now. */
1242 ret = intel_setup_irq_remapping(iommu);
1243 if (ret) {
1244 pr_err("DRHD %Lx: failed to allocate resource\n",
1245 iommu->reg_phys);
1246 ir_remove_ioapic_hpet_scope(iommu);
1247 return ret;
1248 }
1249
1250 if (!iommu->qi) {
1251 /* Clear previous faults. */
1252 dmar_fault(-1, iommu);
1253 iommu_disable_irq_remapping(iommu);
1254 dmar_disable_qi(iommu);
1255 }
1256
1257 /* Enable queued invalidation */
1258 ret = dmar_enable_qi(iommu);
1259 if (!ret) {
1260 iommu_set_irq_remapping(iommu, eim);
1261 } else {
1262 pr_err("DRHD %Lx: failed to enable queued invalidation, ecap %Lx, ret %d\n",
1263 iommu->reg_phys, iommu->ecap, ret);
1264 intel_teardown_irq_remapping(iommu);
1265 ir_remove_ioapic_hpet_scope(iommu);
1266 }
1267
1268 return ret;
1269}
1270
Jiang Liu6b197242014-11-09 22:47:58 +08001271int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
1272{
Jiang Liua7a3dad2014-11-09 22:48:00 +08001273 int ret = 0;
1274 struct intel_iommu *iommu = dmaru->iommu;
1275
1276 if (!irq_remapping_enabled)
1277 return 0;
1278 if (iommu == NULL)
1279 return -EINVAL;
1280 if (!ecap_ir_support(iommu->ecap))
1281 return 0;
1282
1283 if (insert) {
1284 if (!iommu->ir_table)
1285 ret = dmar_ir_add(dmaru, iommu);
1286 } else {
1287 if (iommu->ir_table) {
1288 if (!bitmap_empty(iommu->ir_table->bitmap,
1289 INTR_REMAP_TABLE_ENTRIES)) {
1290 ret = -EBUSY;
1291 } else {
1292 iommu_disable_irq_remapping(iommu);
1293 intel_teardown_irq_remapping(iommu);
1294 ir_remove_ioapic_hpet_scope(iommu);
1295 }
1296 }
1297 }
1298
1299 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08001300}