blob: 14e10de4a5481623ccb9e90a18699197c08b0bff [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>
Joerg Roedelaf3b3582015-06-12 15:00:21 +020014#include <linux/crash_dump.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070015#include <asm/io_apic.h>
Yinghai Lu17483a12008-12-12 13:14:18 -080016#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053017#include <asm/cpu.h>
Suresh Siddha8a8f4222012-03-30 11:47:08 -070018#include <asm/irq_remapping.h>
Weidong Hanf007e992009-05-23 00:41:15 +080019#include <asm/pci-direct.h>
Joerg Roedel5e2b9302012-03-30 11:47:05 -070020#include <asm/msidef.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070021
Suresh Siddha8a8f4222012-03-30 11:47:08 -070022#include "irq_remapping.h"
Joerg Roedel736baef2012-03-30 11:47:00 -070023
Joerg Roedeleef93fd2012-03-30 11:46:59 -070024struct ioapic_scope {
25 struct intel_iommu *iommu;
26 unsigned int id;
27 unsigned int bus; /* PCI bus number */
28 unsigned int devfn; /* PCI devfn number */
29};
30
31struct hpet_scope {
32 struct intel_iommu *iommu;
33 u8 id;
34 unsigned int bus;
35 unsigned int devfn;
36};
37
38#define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
Jiang Liu13d09b62015-01-07 15:31:37 +080039#define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
Joerg Roedeleef93fd2012-03-30 11:46:59 -070040
Jiang Liu13d09b62015-01-07 15:31:37 +080041static int __read_mostly eim_mode;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070042static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070043static struct hpet_scope ir_hpet[MAX_HPET_TBS];
Chris Wrightd1423d52010-07-20 11:06:49 -070044
Jiang Liu3a5670e2014-02-19 14:07:33 +080045/*
46 * Lock ordering:
47 * ->dmar_global_lock
48 * ->irq_2_ir_lock
49 * ->qi->q_lock
50 * ->iommu->register_lock
51 * Note:
52 * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
53 * in single-threaded environment with interrupt disabled, so no need to tabke
54 * the dmar_global_lock.
55 */
Thomas Gleixner96f8e982011-07-19 16:28:19 +020056static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Thomas Gleixnerd585d062010-10-10 12:34:27 +020057
Joerg Roedelaf3b3582015-06-12 15:00:21 +020058static void iommu_disable_irq_remapping(struct intel_iommu *iommu);
Jiang Liu694835d2014-01-06 14:18:16 +080059static int __init parse_ioapics_under_ir(void);
60
Joerg Roedelaf3b3582015-06-12 15:00:21 +020061static bool ir_pre_enabled(struct intel_iommu *iommu)
62{
63 return (iommu->flags & VTD_FLAG_IRQ_REMAP_PRE_ENABLED);
64}
65
66static void clear_ir_pre_enabled(struct intel_iommu *iommu)
67{
68 iommu->flags &= ~VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
69}
70
71static void init_ir_status(struct intel_iommu *iommu)
72{
73 u32 gsts;
74
75 gsts = readl(iommu->reg + DMAR_GSTS_REG);
76 if (gsts & DMA_GSTS_IRES)
77 iommu->flags |= VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
78}
79
Yinghai Lue420dfb2008-08-19 20:50:21 -070080static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
81{
Jiang Liu91411da2014-10-27 16:12:09 +080082 struct irq_cfg *cfg = irq_cfg(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020083 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080084}
85
Rashika Kheria6a7885c2013-12-18 12:04:27 +053086static int get_irte(int irq, struct irte *entry)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070087{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020088 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070089 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020090 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070091
Thomas Gleixnerd585d062010-10-10 12:34:27 +020092 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070093 return -1;
94
Thomas Gleixner96f8e982011-07-19 16:28:19 +020095 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070096
Greg Edwardsaf437462014-07-23 10:13:26 -060097 if (unlikely(!irq_iommu->iommu)) {
98 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
99 return -1;
100 }
101
Yinghai Lue420dfb2008-08-19 20:50:21 -0700102 index = irq_iommu->irte_index + irq_iommu->sub_handle;
103 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700104
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200105 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700106 return 0;
107}
108
Joerg Roedel263b5e82012-03-30 11:47:06 -0700109static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700110{
111 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200112 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Jiang Liu91411da2014-10-27 16:12:09 +0800113 struct irq_cfg *cfg = irq_cfg(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700114 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700115 unsigned long flags;
Dan Carpenter9f4c7442014-01-09 08:32:36 +0300116 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700117
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200118 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700119 return -1;
120
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700121 if (count > 1) {
122 count = __roundup_pow_of_two(count);
123 mask = ilog2(count);
124 }
125
126 if (mask > ecap_max_handle_mask(iommu->ecap)) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200127 pr_err("Requested mask %x exceeds the max invalidation handle"
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700128 " mask value %Lx\n", mask,
129 ecap_max_handle_mask(iommu->ecap));
130 return -1;
131 }
132
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200133 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Jiang Liu360eb3c2014-01-06 14:18:08 +0800134 index = bitmap_find_free_region(table->bitmap,
135 INTR_REMAP_TABLE_ENTRIES, mask);
136 if (index < 0) {
137 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
138 } else {
139 cfg->remapped = 1;
140 irq_iommu->iommu = iommu;
141 irq_iommu->irte_index = index;
142 irq_iommu->sub_handle = 0;
143 irq_iommu->irte_mask = mask;
144 }
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
147 return index;
148}
149
Yu Zhao704126a2009-01-04 16:28:52 +0800150static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700151{
152 struct qi_desc desc;
153
154 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
155 | QI_IEC_SELECTIVE;
156 desc.high = 0;
157
Yu Zhao704126a2009-01-04 16:28:52 +0800158 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700159}
160
Joerg Roedel263b5e82012-03-30 11:47:06 -0700161static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700162{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200163 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700164 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200165 int index;
166
167 if (!irq_iommu)
168 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700169
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200170 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700171 *sub_handle = irq_iommu->sub_handle;
172 index = irq_iommu->irte_index;
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 return index;
175}
176
Joerg Roedel263b5e82012-03-30 11:47:06 -0700177static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700178{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200179 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Jiang Liu91411da2014-10-27 16:12:09 +0800180 struct irq_cfg *cfg = irq_cfg(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700181 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700182
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200183 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800184 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200185
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200186 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800187
Joerg Roedel9b1b0e42012-09-26 12:44:45 +0200188 cfg->remapped = 1;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700189 irq_iommu->iommu = iommu;
190 irq_iommu->irte_index = index;
191 irq_iommu->sub_handle = subhandle;
192 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700193
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200194 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700195
196 return 0;
197}
198
Joerg Roedel263b5e82012-03-30 11:47:06 -0700199static int modify_irte(int irq, struct irte *irte_modified)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700200{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200201 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700202 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700203 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200204 struct irte *irte;
205 int rc, index;
206
207 if (!irq_iommu)
208 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700209
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200210 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700211
Yinghai Lue420dfb2008-08-19 20:50:21 -0700212 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700213
Yinghai Lue420dfb2008-08-19 20:50:21 -0700214 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700215 irte = &iommu->ir_table->base[index];
216
Linus Torvaldsc513b672010-08-06 11:02:31 -0700217 set_64bit(&irte->low, irte_modified->low);
218 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700219 __iommu_flush_cache(iommu, irte, sizeof(*irte));
220
Yu Zhao704126a2009-01-04 16:28:52 +0800221 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200222 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800223
224 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700225}
226
Joerg Roedel263b5e82012-03-30 11:47:06 -0700227static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700228{
229 int i;
230
231 for (i = 0; i < MAX_HPET_TBS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800232 if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
Suresh Siddha20f30972009-08-04 12:07:08 -0700233 return ir_hpet[i].iommu;
234 return NULL;
235}
236
Joerg Roedel263b5e82012-03-30 11:47:06 -0700237static struct intel_iommu *map_ioapic_to_ir(int apic)
Suresh Siddha89027d32008-07-10 11:16:56 -0700238{
239 int i;
240
241 for (i = 0; i < MAX_IO_APICS; i++)
Jiang Liua7a3dad2014-11-09 22:48:00 +0800242 if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
Suresh Siddha89027d32008-07-10 11:16:56 -0700243 return ir_ioapic[i].iommu;
244 return NULL;
245}
246
Joerg Roedel263b5e82012-03-30 11:47:06 -0700247static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700248{
249 struct dmar_drhd_unit *drhd;
250
251 drhd = dmar_find_matched_drhd_unit(dev);
252 if (!drhd)
253 return NULL;
254
255 return drhd->iommu;
256}
257
Weidong Hanc4658b42009-05-23 00:41:14 +0800258static int clear_entries(struct irq_2_iommu *irq_iommu)
259{
260 struct irte *start, *entry, *end;
261 struct intel_iommu *iommu;
262 int index;
263
264 if (irq_iommu->sub_handle)
265 return 0;
266
267 iommu = irq_iommu->iommu;
268 index = irq_iommu->irte_index + irq_iommu->sub_handle;
269
270 start = iommu->ir_table->base + index;
271 end = start + (1 << irq_iommu->irte_mask);
272
273 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700274 set_64bit(&entry->low, 0);
275 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800276 }
Jiang Liu360eb3c2014-01-06 14:18:08 +0800277 bitmap_release_region(iommu->ir_table->bitmap, index,
278 irq_iommu->irte_mask);
Weidong Hanc4658b42009-05-23 00:41:14 +0800279
280 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
281}
282
Joerg Roedel9d619f62012-03-30 11:47:04 -0700283static int free_irte(int irq)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700284{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200285 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700286 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200287 int rc;
288
289 if (!irq_iommu)
290 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700291
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200292 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700293
Weidong Hanc4658b42009-05-23 00:41:14 +0800294 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700295
Yinghai Lue420dfb2008-08-19 20:50:21 -0700296 irq_iommu->iommu = NULL;
297 irq_iommu->irte_index = 0;
298 irq_iommu->sub_handle = 0;
299 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700300
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200301 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700302
Yu Zhao704126a2009-01-04 16:28:52 +0800303 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700304}
305
Weidong Hanf007e992009-05-23 00:41:15 +0800306/*
307 * source validation type
308 */
309#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300310#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800311#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
312
313/*
314 * source-id qualifier
315 */
316#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
317#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
318 * the third least significant bit
319 */
320#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
321 * the second and third least significant bits
322 */
323#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
324 * the least three significant bits
325 */
326
327/*
328 * set SVT, SQ and SID fields of irte to verify
329 * source ids of interrupt requests
330 */
331static void set_irte_sid(struct irte *irte, unsigned int svt,
332 unsigned int sq, unsigned int sid)
333{
Chris Wrightd1423d52010-07-20 11:06:49 -0700334 if (disable_sourceid_checking)
335 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800336 irte->svt = svt;
337 irte->sq = sq;
338 irte->sid = sid;
339}
340
Joerg Roedel263b5e82012-03-30 11:47:06 -0700341static int set_ioapic_sid(struct irte *irte, int apic)
Weidong Hanf007e992009-05-23 00:41:15 +0800342{
343 int i;
344 u16 sid = 0;
345
346 if (!irte)
347 return -1;
348
Jiang Liu3a5670e2014-02-19 14:07:33 +0800349 down_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800350 for (i = 0; i < MAX_IO_APICS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800351 if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
Weidong Hanf007e992009-05-23 00:41:15 +0800352 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
353 break;
354 }
355 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800356 up_read(&dmar_global_lock);
Weidong Hanf007e992009-05-23 00:41:15 +0800357
358 if (sid == 0) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200359 pr_warn("Failed to set source-id of IOAPIC (%d)\n", apic);
Weidong Hanf007e992009-05-23 00:41:15 +0800360 return -1;
361 }
362
Jiang Liu2fe2c602014-01-06 14:18:17 +0800363 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
Weidong Hanf007e992009-05-23 00:41:15 +0800364
365 return 0;
366}
367
Joerg Roedel263b5e82012-03-30 11:47:06 -0700368static int set_hpet_sid(struct irte *irte, u8 id)
Suresh Siddha20f30972009-08-04 12:07:08 -0700369{
370 int i;
371 u16 sid = 0;
372
373 if (!irte)
374 return -1;
375
Jiang Liu3a5670e2014-02-19 14:07:33 +0800376 down_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700377 for (i = 0; i < MAX_HPET_TBS; i++) {
Jiang Liua7a3dad2014-11-09 22:48:00 +0800378 if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700379 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
380 break;
381 }
382 }
Jiang Liu3a5670e2014-02-19 14:07:33 +0800383 up_read(&dmar_global_lock);
Suresh Siddha20f30972009-08-04 12:07:08 -0700384
385 if (sid == 0) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200386 pr_warn("Failed to set source-id of HPET block (%d)\n", id);
Suresh Siddha20f30972009-08-04 12:07:08 -0700387 return -1;
388 }
389
390 /*
391 * Should really use SQ_ALL_16. Some platforms are broken.
392 * While we figure out the right quirks for these broken platforms, use
393 * SQ_13_IGNORE_3 for now.
394 */
395 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
396
397 return 0;
398}
399
Alex Williamson579305f2014-07-03 09:51:43 -0600400struct set_msi_sid_data {
401 struct pci_dev *pdev;
402 u16 alias;
403};
404
405static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
406{
407 struct set_msi_sid_data *data = opaque;
408
409 data->pdev = pdev;
410 data->alias = alias;
411
412 return 0;
413}
414
Joerg Roedel263b5e82012-03-30 11:47:06 -0700415static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
Weidong Hanf007e992009-05-23 00:41:15 +0800416{
Alex Williamson579305f2014-07-03 09:51:43 -0600417 struct set_msi_sid_data data;
Weidong Hanf007e992009-05-23 00:41:15 +0800418
419 if (!irte || !dev)
420 return -1;
421
Alex Williamson579305f2014-07-03 09:51:43 -0600422 pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
Weidong Hanf007e992009-05-23 00:41:15 +0800423
Alex Williamson579305f2014-07-03 09:51:43 -0600424 /*
425 * DMA alias provides us with a PCI device and alias. The only case
426 * where the it will return an alias on a different bus than the
427 * device is the case of a PCIe-to-PCI bridge, where the alias is for
428 * the subordinate bus. In this case we can only verify the bus.
429 *
430 * If the alias device is on a different bus than our source device
431 * then we have a topology based alias, use it.
432 *
433 * Otherwise, the alias is for a device DMA quirk and we cannot
434 * assume that MSI uses the same requester ID. Therefore use the
435 * original device.
436 */
437 if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
438 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
439 PCI_DEVID(PCI_BUS_NUM(data.alias),
440 dev->bus->number));
441 else if (data.pdev->bus->number != dev->bus->number)
442 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
443 else
444 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
445 PCI_DEVID(dev->bus->number, dev->devfn));
Weidong Hanf007e992009-05-23 00:41:15 +0800446
447 return 0;
448}
449
Joerg Roedelaf3b3582015-06-12 15:00:21 +0200450static int iommu_load_old_irte(struct intel_iommu *iommu)
451{
452 struct irte *old_ir_table;
453 phys_addr_t irt_phys;
Joerg Roedel7c3c9872015-06-12 15:06:26 +0200454 unsigned int i;
Joerg Roedelaf3b3582015-06-12 15:00:21 +0200455 size_t size;
456 u64 irta;
457
458 if (!is_kdump_kernel()) {
459 pr_warn("IRQ remapping was enabled on %s but we are not in kdump mode\n",
460 iommu->name);
461 clear_ir_pre_enabled(iommu);
462 iommu_disable_irq_remapping(iommu);
463 return -EINVAL;
464 }
465
466 /* Check whether the old ir-table has the same size as ours */
467 irta = dmar_readq(iommu->reg + DMAR_IRTA_REG);
468 if ((irta & INTR_REMAP_TABLE_REG_SIZE_MASK)
469 != INTR_REMAP_TABLE_REG_SIZE)
470 return -EINVAL;
471
472 irt_phys = irta & VTD_PAGE_MASK;
473 size = INTR_REMAP_TABLE_ENTRIES*sizeof(struct irte);
474
475 /* Map the old IR table */
476 old_ir_table = ioremap_cache(irt_phys, size);
477 if (!old_ir_table)
478 return -ENOMEM;
479
480 /* Copy data over */
481 memcpy(iommu->ir_table->base, old_ir_table, size);
482
483 __iommu_flush_cache(iommu, iommu->ir_table->base, size);
484
Joerg Roedel7c3c9872015-06-12 15:06:26 +0200485 /*
486 * Now check the table for used entries and mark those as
487 * allocated in the bitmap
488 */
489 for (i = 0; i < INTR_REMAP_TABLE_ENTRIES; i++) {
490 if (iommu->ir_table->base[i].present)
491 bitmap_set(iommu->ir_table->bitmap, i, 1);
492 }
493
Joerg Roedelaf3b3582015-06-12 15:00:21 +0200494 return 0;
495}
496
497
Suresh Siddha95a02e92012-03-30 11:47:07 -0700498static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700499{
Joerg Roedeld4d1c0f2015-06-12 14:35:54 +0200500 unsigned long flags;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700501 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100502 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700503
504 addr = virt_to_phys((void *)iommu->ir_table->base);
505
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200506 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700507
508 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
509 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
510
511 /* Set interrupt-remapping table pointer */
Jan Kiszkaf63ef692014-08-11 13:13:25 +0200512 writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700513
514 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
515 readl, (sts & DMA_GSTS_IRTPS), sts);
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200516 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700517
518 /*
Joerg Roedeld4d1c0f2015-06-12 14:35:54 +0200519 * Global invalidation of interrupt entry cache to make sure the
520 * hardware uses the new irq remapping table.
Suresh Siddha2ae21012008-07-10 11:16:43 -0700521 */
522 qi_global_iec(iommu);
Joerg Roedeld4d1c0f2015-06-12 14:35:54 +0200523}
524
525static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
526{
527 unsigned long flags;
528 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700529
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200530 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700531
532 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700533 iommu->gcmd |= DMA_GCMD_IRE;
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800534 iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
David Woodhousec416daa2009-05-10 20:30:58 +0100535 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700536
537 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
538 readl, (sts & DMA_GSTS_IRES), sts);
539
Andy Lutomirskiaf8d1022013-02-01 14:57:43 -0800540 /*
541 * With CFI clear in the Global Command register, we should be
542 * protected from dangerous (i.e. compatibility) interrupts
543 * regardless of x2apic status. Check just to be sure.
544 */
545 if (sts & DMA_GSTS_CFIS)
546 WARN(1, KERN_WARNING
547 "Compatibility-format IRQs enabled despite intr remapping;\n"
548 "you are vulnerable to IRQ injection.\n");
549
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200550 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700551}
552
Jiang Liua7a3dad2014-11-09 22:48:00 +0800553static int intel_setup_irq_remapping(struct intel_iommu *iommu)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700554{
555 struct ir_table *ir_table;
556 struct page *pages;
Jiang Liu360eb3c2014-01-06 14:18:08 +0800557 unsigned long *bitmap;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700558
Jiang Liua7a3dad2014-11-09 22:48:00 +0800559 if (iommu->ir_table)
560 return 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700561
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800562 ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800563 if (!ir_table)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700564 return -ENOMEM;
565
Thomas Gleixnere3a981d2015-01-07 15:31:30 +0800566 pages = alloc_pages_node(iommu->node, GFP_KERNEL | __GFP_ZERO,
Suresh Siddha824cd752009-10-02 11:01:23 -0700567 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700568
569 if (!pages) {
Jiang Liu360eb3c2014-01-06 14:18:08 +0800570 pr_err("IR%d: failed to allocate pages of order %d\n",
571 iommu->seq_id, INTR_REMAP_PAGE_ORDER);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800572 goto out_free_table;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700573 }
574
Jiang Liu360eb3c2014-01-06 14:18:08 +0800575 bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
576 sizeof(long), GFP_ATOMIC);
577 if (bitmap == NULL) {
578 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800579 goto out_free_pages;
Jiang Liu360eb3c2014-01-06 14:18:08 +0800580 }
581
Suresh Siddha2ae21012008-07-10 11:16:43 -0700582 ir_table->base = page_address(pages);
Jiang Liu360eb3c2014-01-06 14:18:08 +0800583 ir_table->bitmap = bitmap;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800584 iommu->ir_table = ir_table;
Joerg Roedel9e4e49d2015-06-12 14:23:56 +0200585
586 /*
587 * If the queued invalidation is already initialized,
588 * shouldn't disable it.
589 */
590 if (!iommu->qi) {
591 /*
592 * Clear previous faults.
593 */
594 dmar_fault(-1, iommu);
595 dmar_disable_qi(iommu);
596
597 if (dmar_enable_qi(iommu)) {
598 pr_err("Failed to enable queued invalidation\n");
599 goto out_free_bitmap;
600 }
601 }
602
Joerg Roedelaf3b3582015-06-12 15:00:21 +0200603 init_ir_status(iommu);
604
605 if (ir_pre_enabled(iommu)) {
606 if (iommu_load_old_irte(iommu))
607 pr_err("Failed to copy IR table for %s from previous kernel\n",
608 iommu->name);
609 else
610 pr_info("Copied IR table for %s from previous kernel\n",
611 iommu->name);
612 }
613
Joerg Roedeld4d1c0f2015-06-12 14:35:54 +0200614 iommu_set_irq_remapping(iommu, eim_mode);
615
Suresh Siddha2ae21012008-07-10 11:16:43 -0700616 return 0;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800617
Joerg Roedel9e4e49d2015-06-12 14:23:56 +0200618out_free_bitmap:
619 kfree(bitmap);
Jiang Liua7a3dad2014-11-09 22:48:00 +0800620out_free_pages:
621 __free_pages(pages, INTR_REMAP_PAGE_ORDER);
622out_free_table:
623 kfree(ir_table);
Joerg Roedel9e4e49d2015-06-12 14:23:56 +0200624
625 iommu->ir_table = NULL;
626
Jiang Liua7a3dad2014-11-09 22:48:00 +0800627 return -ENOMEM;
628}
629
630static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
631{
632 if (iommu && iommu->ir_table) {
633 free_pages((unsigned long)iommu->ir_table->base,
634 INTR_REMAP_PAGE_ORDER);
635 kfree(iommu->ir_table->bitmap);
636 kfree(iommu->ir_table);
637 iommu->ir_table = NULL;
638 }
Suresh Siddha2ae21012008-07-10 11:16:43 -0700639}
640
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700641/*
642 * Disable Interrupt Remapping.
643 */
Suresh Siddha95a02e92012-03-30 11:47:07 -0700644static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700645{
646 unsigned long flags;
647 u32 sts;
648
649 if (!ecap_ir_support(iommu->ecap))
650 return;
651
Fenghua Yub24696b2009-03-27 14:22:44 -0700652 /*
653 * global invalidation of interrupt entry cache before disabling
654 * interrupt-remapping.
655 */
656 qi_global_iec(iommu);
657
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200658 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700659
660 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
661 if (!(sts & DMA_GSTS_IRES))
662 goto end;
663
664 iommu->gcmd &= ~DMA_GCMD_IRE;
665 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
666
667 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
668 readl, !(sts & DMA_GSTS_IRES), sts);
669
670end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200671 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700672}
673
Suresh Siddha41750d32011-08-23 17:05:18 -0700674static int __init dmar_x2apic_optout(void)
675{
676 struct acpi_table_dmar *dmar;
677 dmar = (struct acpi_table_dmar *)dmar_tbl;
678 if (!dmar || no_x2apic_optout)
679 return 0;
680 return dmar->flags & DMAR_X2APIC_OPT_OUT;
681}
682
Thomas Gleixner11190302015-01-07 15:31:29 +0800683static void __init intel_cleanup_irq_remapping(void)
684{
685 struct dmar_drhd_unit *drhd;
686 struct intel_iommu *iommu;
687
688 for_each_iommu(iommu, drhd) {
689 if (ecap_ir_support(iommu->ecap)) {
690 iommu_disable_irq_remapping(iommu);
691 intel_teardown_irq_remapping(iommu);
692 }
693 }
694
695 if (x2apic_supported())
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200696 pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
Thomas Gleixner11190302015-01-07 15:31:29 +0800697}
698
699static int __init intel_prepare_irq_remapping(void)
700{
701 struct dmar_drhd_unit *drhd;
702 struct intel_iommu *iommu;
Joerg Roedel23256d02015-06-12 14:15:49 +0200703 int eim = 0;
Thomas Gleixner11190302015-01-07 15:31:29 +0800704
Jiang Liu2966d952015-01-07 15:31:35 +0800705 if (irq_remap_broken) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200706 pr_warn("This system BIOS has enabled interrupt remapping\n"
Jiang Liu2966d952015-01-07 15:31:35 +0800707 "on a chipset that contains an erratum making that\n"
708 "feature unstable. To maintain system stability\n"
709 "interrupt remapping is being disabled. Please\n"
710 "contact your BIOS vendor for an update\n");
711 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
Jiang Liu2966d952015-01-07 15:31:35 +0800712 return -ENODEV;
713 }
714
Thomas Gleixner11190302015-01-07 15:31:29 +0800715 if (dmar_table_init() < 0)
Jiang Liu2966d952015-01-07 15:31:35 +0800716 return -ENODEV;
717
718 if (!dmar_ir_support())
719 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800720
721 if (parse_ioapics_under_ir() != 1) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +0200722 pr_info("Not enabling interrupt remapping\n");
Thomas Gleixner11190302015-01-07 15:31:29 +0800723 goto error;
724 }
725
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800726 /* First make sure all IOMMUs support IRQ remapping */
Jiang Liu2966d952015-01-07 15:31:35 +0800727 for_each_iommu(iommu, drhd)
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800728 if (!ecap_ir_support(iommu->ecap))
Thomas Gleixner11190302015-01-07 15:31:29 +0800729 goto error;
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800730
Joerg Roedel23256d02015-06-12 14:15:49 +0200731 /* Detect remapping mode: lapic or x2apic */
732 if (x2apic_supported()) {
733 eim = !dmar_x2apic_optout();
734 if (!eim) {
735 pr_info("x2apic is disabled because BIOS sets x2apic opt out bit.");
736 pr_info("Use 'intremap=no_x2apic_optout' to override the BIOS setting.\n");
737 }
738 }
739
740 for_each_iommu(iommu, drhd) {
741 if (eim && !ecap_eim_support(iommu->ecap)) {
742 pr_info("%s does not support EIM\n", iommu->name);
743 eim = 0;
744 }
Joerg Roedelc676f582015-06-12 14:25:53 +0200745
746 /* Disable IRQ remapping if it is already enabled */
747 iommu_disable_irq_remapping(iommu);
Joerg Roedel23256d02015-06-12 14:15:49 +0200748 }
749
750 eim_mode = eim;
751 if (eim)
752 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
753
Joerg Roedel9e4e49d2015-06-12 14:23:56 +0200754 /* Do the initializations early */
755 for_each_iommu(iommu, drhd) {
756 if (intel_setup_irq_remapping(iommu)) {
757 pr_err("Failed to setup irq remapping for %s\n",
758 iommu->name);
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800759 goto error;
Joerg Roedel9e4e49d2015-06-12 14:23:56 +0200760 }
761 }
Joerg Roedel69cf1d82015-01-07 15:31:36 +0800762
Thomas Gleixner11190302015-01-07 15:31:29 +0800763 return 0;
Jiang Liu2966d952015-01-07 15:31:35 +0800764
Thomas Gleixner11190302015-01-07 15:31:29 +0800765error:
766 intel_cleanup_irq_remapping();
Jiang Liu2966d952015-01-07 15:31:35 +0800767 return -ENODEV;
Thomas Gleixner11190302015-01-07 15:31:29 +0800768}
769
Suresh Siddha95a02e92012-03-30 11:47:07 -0700770static int __init intel_enable_irq_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700771{
772 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800773 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100774 bool setup = false;
Suresh Siddha41750d32011-08-23 17:05:18 -0700775
Suresh Siddha2ae21012008-07-10 11:16:43 -0700776 /*
777 * Setup Interrupt-remapping for all the DRHD's now.
778 */
Jiang Liu7c919772014-01-06 14:18:18 +0800779 for_each_iommu(iommu, drhd) {
Joerg Roedeld4d1c0f2015-06-12 14:35:54 +0200780 iommu_enable_irq_remapping(iommu);
Quentin Lambert2f119c72015-02-06 10:59:53 +0100781 setup = true;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700782 }
783
784 if (!setup)
785 goto error;
786
Suresh Siddha95a02e92012-03-30 11:47:07 -0700787 irq_remapping_enabled = 1;
Joerg Roedelafcc8a42012-09-26 12:44:36 +0200788
789 /*
790 * VT-d has a different layout for IO-APIC entries when
791 * interrupt remapping is enabled. So it needs a special routine
792 * to print IO-APIC entries for debugging purposes too.
793 */
794 x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
795
Joerg Roedel23256d02015-06-12 14:15:49 +0200796 pr_info("Enabled IRQ remapping in %s mode\n", eim_mode ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700797
Joerg Roedel23256d02015-06-12 14:15:49 +0200798 return eim_mode ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700799
800error:
Thomas Gleixner11190302015-01-07 15:31:29 +0800801 intel_cleanup_irq_remapping();
Suresh Siddha2ae21012008-07-10 11:16:43 -0700802 return -1;
803}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700804
Jiang Liua7a3dad2014-11-09 22:48:00 +0800805static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
806 struct intel_iommu *iommu,
807 struct acpi_dmar_hardware_unit *drhd)
Suresh Siddha20f30972009-08-04 12:07:08 -0700808{
809 struct acpi_dmar_pci_path *path;
810 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800811 int count, free = -1;
Suresh Siddha20f30972009-08-04 12:07:08 -0700812
813 bus = scope->bus;
814 path = (struct acpi_dmar_pci_path *)(scope + 1);
815 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
816 / sizeof(struct acpi_dmar_pci_path);
817
818 while (--count > 0) {
819 /*
820 * Access PCI directly due to the PCI
821 * subsystem isn't initialized yet.
822 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800823 bus = read_pci_config_byte(bus, path->device, path->function,
Suresh Siddha20f30972009-08-04 12:07:08 -0700824 PCI_SECONDARY_BUS);
825 path++;
826 }
Jiang Liua7a3dad2014-11-09 22:48:00 +0800827
828 for (count = 0; count < MAX_HPET_TBS; count++) {
829 if (ir_hpet[count].iommu == iommu &&
830 ir_hpet[count].id == scope->enumeration_id)
831 return 0;
832 else if (ir_hpet[count].iommu == NULL && free == -1)
833 free = count;
834 }
835 if (free == -1) {
836 pr_warn("Exceeded Max HPET blocks\n");
837 return -ENOSPC;
838 }
839
840 ir_hpet[free].iommu = iommu;
841 ir_hpet[free].id = scope->enumeration_id;
842 ir_hpet[free].bus = bus;
843 ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
844 pr_info("HPET id %d under DRHD base 0x%Lx\n",
845 scope->enumeration_id, drhd->address);
846
847 return 0;
Suresh Siddha20f30972009-08-04 12:07:08 -0700848}
849
Jiang Liua7a3dad2014-11-09 22:48:00 +0800850static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
851 struct intel_iommu *iommu,
852 struct acpi_dmar_hardware_unit *drhd)
Weidong Hanf007e992009-05-23 00:41:15 +0800853{
854 struct acpi_dmar_pci_path *path;
855 u8 bus;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800856 int count, free = -1;
Weidong Hanf007e992009-05-23 00:41:15 +0800857
858 bus = scope->bus;
859 path = (struct acpi_dmar_pci_path *)(scope + 1);
860 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
861 / sizeof(struct acpi_dmar_pci_path);
862
863 while (--count > 0) {
864 /*
865 * Access PCI directly due to the PCI
866 * subsystem isn't initialized yet.
867 */
Lv Zhengfa5f5082013-10-31 09:30:22 +0800868 bus = read_pci_config_byte(bus, path->device, path->function,
Weidong Hanf007e992009-05-23 00:41:15 +0800869 PCI_SECONDARY_BUS);
870 path++;
871 }
872
Jiang Liua7a3dad2014-11-09 22:48:00 +0800873 for (count = 0; count < MAX_IO_APICS; count++) {
874 if (ir_ioapic[count].iommu == iommu &&
875 ir_ioapic[count].id == scope->enumeration_id)
876 return 0;
877 else if (ir_ioapic[count].iommu == NULL && free == -1)
878 free = count;
879 }
880 if (free == -1) {
881 pr_warn("Exceeded Max IO APICS\n");
882 return -ENOSPC;
883 }
884
885 ir_ioapic[free].bus = bus;
886 ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
887 ir_ioapic[free].iommu = iommu;
888 ir_ioapic[free].id = scope->enumeration_id;
889 pr_info("IOAPIC id %d under DRHD base 0x%Lx IOMMU %d\n",
890 scope->enumeration_id, drhd->address, iommu->seq_id);
891
892 return 0;
Weidong Hanf007e992009-05-23 00:41:15 +0800893}
894
Suresh Siddha20f30972009-08-04 12:07:08 -0700895static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
896 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700897{
Jiang Liua7a3dad2014-11-09 22:48:00 +0800898 int ret = 0;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700899 struct acpi_dmar_hardware_unit *drhd;
900 struct acpi_dmar_device_scope *scope;
901 void *start, *end;
902
903 drhd = (struct acpi_dmar_hardware_unit *)header;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700904 start = (void *)(drhd + 1);
905 end = ((void *)drhd) + header->length;
906
Jiang Liua7a3dad2014-11-09 22:48:00 +0800907 while (start < end && ret == 0) {
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700908 scope = start;
Jiang Liua7a3dad2014-11-09 22:48:00 +0800909 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
910 ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
911 else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
912 ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700913 start += scope->length;
914 }
915
Jiang Liua7a3dad2014-11-09 22:48:00 +0800916 return ret;
917}
918
919static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
920{
921 int i;
922
923 for (i = 0; i < MAX_HPET_TBS; i++)
924 if (ir_hpet[i].iommu == iommu)
925 ir_hpet[i].iommu = NULL;
926
927 for (i = 0; i < MAX_IO_APICS; i++)
928 if (ir_ioapic[i].iommu == iommu)
929 ir_ioapic[i].iommu = NULL;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700930}
931
932/*
933 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
934 * hardware unit.
935 */
Jiang Liu694835d2014-01-06 14:18:16 +0800936static int __init parse_ioapics_under_ir(void)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700937{
938 struct dmar_drhd_unit *drhd;
Jiang Liu7c919772014-01-06 14:18:18 +0800939 struct intel_iommu *iommu;
Quentin Lambert2f119c72015-02-06 10:59:53 +0100940 bool ir_supported = false;
Seth Forshee32ab31e2012-08-08 08:27:03 -0500941 int ioapic_idx;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700942
Jiang Liu7c919772014-01-06 14:18:18 +0800943 for_each_iommu(iommu, drhd)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700944 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700945 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700946 return -1;
947
Quentin Lambert2f119c72015-02-06 10:59:53 +0100948 ir_supported = true;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700949 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700950
Seth Forshee32ab31e2012-08-08 08:27:03 -0500951 if (!ir_supported)
952 return 0;
953
954 for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
955 int ioapic_id = mpc_ioapic_id(ioapic_idx);
956 if (!map_ioapic_to_ir(ioapic_id)) {
957 pr_err(FW_BUG "ioapic %d has no mapping iommu, "
958 "interrupt remapping will be disabled\n",
959 ioapic_id);
960 return -1;
961 }
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700962 }
963
Seth Forshee32ab31e2012-08-08 08:27:03 -0500964 return 1;
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700965}
Fenghua Yub24696b2009-03-27 14:22:44 -0700966
Rashika Kheria6a7885c2013-12-18 12:04:27 +0530967static int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700968{
Jiang Liu3a5670e2014-02-19 14:07:33 +0800969 int ret;
970
Suresh Siddha95a02e92012-03-30 11:47:07 -0700971 if (!irq_remapping_enabled)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700972 return 0;
973
Jiang Liu3a5670e2014-02-19 14:07:33 +0800974 down_write(&dmar_global_lock);
975 ret = dmar_dev_scope_init();
976 up_write(&dmar_global_lock);
977
978 return ret;
Suresh Siddhac2c72862011-08-23 17:05:19 -0700979}
980rootfs_initcall(ir_dev_scope_init);
981
Suresh Siddha95a02e92012-03-30 11:47:07 -0700982static void disable_irq_remapping(void)
Fenghua Yub24696b2009-03-27 14:22:44 -0700983{
984 struct dmar_drhd_unit *drhd;
985 struct intel_iommu *iommu = NULL;
986
987 /*
988 * Disable Interrupt-remapping for all the DRHD's now.
989 */
990 for_each_iommu(iommu, drhd) {
991 if (!ecap_ir_support(iommu->ecap))
992 continue;
993
Suresh Siddha95a02e92012-03-30 11:47:07 -0700994 iommu_disable_irq_remapping(iommu);
Fenghua Yub24696b2009-03-27 14:22:44 -0700995 }
996}
997
Suresh Siddha95a02e92012-03-30 11:47:07 -0700998static int reenable_irq_remapping(int eim)
Fenghua Yub24696b2009-03-27 14:22:44 -0700999{
1000 struct dmar_drhd_unit *drhd;
Quentin Lambert2f119c72015-02-06 10:59:53 +01001001 bool setup = false;
Fenghua Yub24696b2009-03-27 14:22:44 -07001002 struct intel_iommu *iommu = NULL;
1003
1004 for_each_iommu(iommu, drhd)
1005 if (iommu->qi)
1006 dmar_reenable_qi(iommu);
1007
1008 /*
1009 * Setup Interrupt-remapping for all the DRHD's now.
1010 */
1011 for_each_iommu(iommu, drhd) {
1012 if (!ecap_ir_support(iommu->ecap))
1013 continue;
1014
1015 /* Set up interrupt remapping for iommu.*/
Suresh Siddha95a02e92012-03-30 11:47:07 -07001016 iommu_set_irq_remapping(iommu, eim);
Joerg Roedeld4d1c0f2015-06-12 14:35:54 +02001017 iommu_enable_irq_remapping(iommu);
Quentin Lambert2f119c72015-02-06 10:59:53 +01001018 setup = true;
Fenghua Yub24696b2009-03-27 14:22:44 -07001019 }
1020
1021 if (!setup)
1022 goto error;
1023
1024 return 0;
1025
1026error:
1027 /*
1028 * handle error condition gracefully here!
1029 */
1030 return -1;
1031}
1032
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001033static void prepare_irte(struct irte *irte, int vector,
1034 unsigned int dest)
1035{
1036 memset(irte, 0, sizeof(*irte));
1037
1038 irte->present = 1;
1039 irte->dst_mode = apic->irq_dest_mode;
1040 /*
1041 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
1042 * actual level or edge trigger will be setup in the IO-APIC
1043 * RTE. This will help simplify level triggered irq migration.
1044 * For more details, see the comments (in io_apic.c) explainig IO-APIC
1045 * irq migration in the presence of interrupt-remapping.
1046 */
1047 irte->trigger_mode = 0;
1048 irte->dlvry_mode = apic->irq_delivery_mode;
1049 irte->vector = vector;
1050 irte->dest_id = IRTE_DEST(dest);
1051 irte->redir_hint = 1;
1052}
1053
1054static int intel_setup_ioapic_entry(int irq,
1055 struct IO_APIC_route_entry *route_entry,
1056 unsigned int destination, int vector,
1057 struct io_apic_irq_attr *attr)
1058{
1059 int ioapic_id = mpc_ioapic_id(attr->ioapic);
Jiang Liu3a5670e2014-02-19 14:07:33 +08001060 struct intel_iommu *iommu;
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001061 struct IR_IO_APIC_route_entry *entry;
1062 struct irte irte;
1063 int index;
1064
Jiang Liu3a5670e2014-02-19 14:07:33 +08001065 down_read(&dmar_global_lock);
1066 iommu = map_ioapic_to_ir(ioapic_id);
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001067 if (!iommu) {
1068 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
Jiang Liu3a5670e2014-02-19 14:07:33 +08001069 index = -ENODEV;
1070 } else {
1071 index = alloc_irte(iommu, irq, 1);
1072 if (index < 0) {
1073 pr_warn("Failed to allocate IRTE for ioapic %d\n",
1074 ioapic_id);
1075 index = -ENOMEM;
1076 }
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001077 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08001078 up_read(&dmar_global_lock);
1079 if (index < 0)
1080 return index;
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001081
1082 prepare_irte(&irte, vector, destination);
1083
1084 /* Set source-id of interrupt request */
1085 set_ioapic_sid(&irte, ioapic_id);
1086
1087 modify_irte(irq, &irte);
1088
1089 apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
1090 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
1091 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
1092 "Avail:%X Vector:%02X Dest:%08X "
1093 "SID:%04X SQ:%X SVT:%X)\n",
1094 attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
1095 irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
1096 irte.avail, irte.vector, irte.dest_id,
1097 irte.sid, irte.sq, irte.svt);
1098
Jiang Liu3a5670e2014-02-19 14:07:33 +08001099 entry = (struct IR_IO_APIC_route_entry *)route_entry;
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001100 memset(entry, 0, sizeof(*entry));
1101
1102 entry->index2 = (index >> 15) & 0x1;
1103 entry->zero = 0;
1104 entry->format = 1;
1105 entry->index = (index & 0x7fff);
1106 /*
1107 * IO-APIC RTE will be configured with virtual vector.
1108 * irq handler will do the explicit EOI to the io-apic.
1109 */
1110 entry->vector = attr->ioapic_pin;
1111 entry->mask = 0; /* enable IRQ */
1112 entry->trigger = attr->trigger;
1113 entry->polarity = attr->polarity;
1114
1115 /* Mask level triggered irqs.
1116 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1117 */
1118 if (attr->trigger)
1119 entry->mask = 1;
1120
1121 return 0;
1122}
1123
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001124/*
1125 * Migrate the IO-APIC irq in the presence of intr-remapping.
1126 *
1127 * For both level and edge triggered, irq migration is a simple atomic
1128 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
1129 *
1130 * For level triggered, we eliminate the io-apic RTE modification (with the
1131 * updated vector information), by using a virtual vector (io-apic pin number).
1132 * Real vector that is used for interrupting cpu will be coming from
1133 * the interrupt-remapping table entry.
1134 *
1135 * As the migration is a simple atomic update of IRTE, the same mechanism
1136 * is used to migrate MSI irq's in the presence of interrupt-remapping.
1137 */
1138static int
1139intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
1140 bool force)
1141{
Jiang Liu91411da2014-10-27 16:12:09 +08001142 struct irq_cfg *cfg = irqd_cfg(data);
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001143 unsigned int dest, irq = data->irq;
1144 struct irte irte;
Alexander Gordeevff164322012-06-07 15:15:59 +02001145 int err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001146
Suresh Siddha7eb9ae02012-06-14 18:28:49 -07001147 if (!config_enabled(CONFIG_SMP))
1148 return -EINVAL;
1149
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001150 if (!cpumask_intersects(mask, cpu_online_mask))
1151 return -EINVAL;
1152
1153 if (get_irte(irq, &irte))
1154 return -EBUSY;
1155
Alexander Gordeevff164322012-06-07 15:15:59 +02001156 err = assign_irq_vector(irq, cfg, mask);
1157 if (err)
1158 return err;
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001159
Alexander Gordeevff164322012-06-07 15:15:59 +02001160 err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
1161 if (err) {
Dan Carpentered88bed2012-06-12 19:26:33 +03001162 if (assign_irq_vector(irq, cfg, data->affinity))
Alexander Gordeevff164322012-06-07 15:15:59 +02001163 pr_err("Failed to recover vector for irq %d\n", irq);
1164 return err;
1165 }
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001166
1167 irte.vector = cfg->vector;
1168 irte.dest_id = IRTE_DEST(dest);
1169
1170 /*
1171 * Atomically updates the IRTE with the new destination, vector
1172 * and flushes the interrupt entry cache.
1173 */
1174 modify_irte(irq, &irte);
1175
1176 /*
1177 * After this point, all the interrupts will start arriving
1178 * at the new destination. So, time to cleanup the previous
1179 * vector allocation.
1180 */
1181 if (cfg->move_in_progress)
1182 send_cleanup_vector(cfg);
1183
1184 cpumask_copy(data->affinity, mask);
1185 return 0;
1186}
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001187
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001188static void intel_compose_msi_msg(struct pci_dev *pdev,
1189 unsigned int irq, unsigned int dest,
1190 struct msi_msg *msg, u8 hpet_id)
1191{
1192 struct irq_cfg *cfg;
1193 struct irte irte;
Suresh Siddhac558df42012-05-08 00:08:54 -07001194 u16 sub_handle = 0;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001195 int ir_index;
1196
Jiang Liu91411da2014-10-27 16:12:09 +08001197 cfg = irq_cfg(irq);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001198
1199 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
1200 BUG_ON(ir_index == -1);
1201
1202 prepare_irte(&irte, cfg->vector, dest);
1203
1204 /* Set source-id of interrupt request */
1205 if (pdev)
1206 set_msi_sid(&irte, pdev);
1207 else
1208 set_hpet_sid(&irte, hpet_id);
1209
1210 modify_irte(irq, &irte);
1211
1212 msg->address_hi = MSI_ADDR_BASE_HI;
1213 msg->data = sub_handle;
1214 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1215 MSI_ADDR_IR_SHV |
1216 MSI_ADDR_IR_INDEX1(ir_index) |
1217 MSI_ADDR_IR_INDEX2(ir_index);
1218}
1219
1220/*
1221 * Map the PCI dev to the corresponding remapping hardware unit
1222 * and allocate 'nvec' consecutive interrupt-remapping table entries
1223 * in it.
1224 */
1225static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
1226{
1227 struct intel_iommu *iommu;
1228 int index;
1229
Jiang Liu3a5670e2014-02-19 14:07:33 +08001230 down_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001231 iommu = map_dev_to_ir(dev);
1232 if (!iommu) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001233 pr_err("Unable to map PCI %s to iommu\n", pci_name(dev));
Jiang Liu3a5670e2014-02-19 14:07:33 +08001234 index = -ENOENT;
1235 } else {
1236 index = alloc_irte(iommu, irq, nvec);
1237 if (index < 0) {
Joerg Roedel9f10e5b2015-06-12 09:57:06 +02001238 pr_err("Unable to allocate %d IRTE for PCI %s\n",
Jiang Liu3a5670e2014-02-19 14:07:33 +08001239 nvec, pci_name(dev));
1240 index = -ENOSPC;
1241 }
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001242 }
Jiang Liu3a5670e2014-02-19 14:07:33 +08001243 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001244
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001245 return index;
1246}
1247
1248static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
1249 int index, int sub_handle)
1250{
1251 struct intel_iommu *iommu;
Jiang Liu3a5670e2014-02-19 14:07:33 +08001252 int ret = -ENOENT;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001253
Jiang Liu3a5670e2014-02-19 14:07:33 +08001254 down_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001255 iommu = map_dev_to_ir(pdev);
Jiang Liu3a5670e2014-02-19 14:07:33 +08001256 if (iommu) {
1257 /*
1258 * setup the mapping between the irq and the IRTE
1259 * base index, the sub_handle pointing to the
1260 * appropriate interrupt remap table entry.
1261 */
1262 set_irte_irq(irq, iommu, index, sub_handle);
1263 ret = 0;
1264 }
1265 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001266
Jiang Liu3a5670e2014-02-19 14:07:33 +08001267 return ret;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001268}
1269
Yijing Wang5fc24d82014-09-17 17:32:19 +08001270static int intel_alloc_hpet_msi(unsigned int irq, unsigned int id)
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001271{
Jiang Liu3a5670e2014-02-19 14:07:33 +08001272 int ret = -1;
1273 struct intel_iommu *iommu;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001274 int index;
1275
Jiang Liu3a5670e2014-02-19 14:07:33 +08001276 down_read(&dmar_global_lock);
1277 iommu = map_hpet_to_ir(id);
1278 if (iommu) {
1279 index = alloc_irte(iommu, irq, 1);
1280 if (index >= 0)
1281 ret = 0;
1282 }
1283 up_read(&dmar_global_lock);
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001284
Jiang Liu3a5670e2014-02-19 14:07:33 +08001285 return ret;
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001286}
1287
Joerg Roedel736baef2012-03-30 11:47:00 -07001288struct irq_remap_ops intel_irq_remap_ops = {
Thomas Gleixner11190302015-01-07 15:31:29 +08001289 .prepare = intel_prepare_irq_remapping,
Suresh Siddha95a02e92012-03-30 11:47:07 -07001290 .enable = intel_enable_irq_remapping,
1291 .disable = disable_irq_remapping,
1292 .reenable = reenable_irq_remapping,
Joerg Roedel4f3d8b62012-03-30 11:47:01 -07001293 .enable_faulting = enable_drhd_fault_handling,
Joerg Roedel0c3f1732012-03-30 11:47:02 -07001294 .setup_ioapic_entry = intel_setup_ioapic_entry,
Joerg Roedel4c1bad62012-03-30 11:47:03 -07001295 .set_affinity = intel_ioapic_set_affinity,
Joerg Roedel9d619f62012-03-30 11:47:04 -07001296 .free_irq = free_irte,
Joerg Roedel5e2b9302012-03-30 11:47:05 -07001297 .compose_msi_msg = intel_compose_msi_msg,
1298 .msi_alloc_irq = intel_msi_alloc_irq,
1299 .msi_setup_irq = intel_msi_setup_irq,
Yijing Wang5fc24d82014-09-17 17:32:19 +08001300 .alloc_hpet_msi = intel_alloc_hpet_msi,
Joerg Roedel736baef2012-03-30 11:47:00 -07001301};
Jiang Liu6b197242014-11-09 22:47:58 +08001302
Jiang Liua7a3dad2014-11-09 22:48:00 +08001303/*
1304 * Support of Interrupt Remapping Unit Hotplug
1305 */
1306static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
1307{
1308 int ret;
1309 int eim = x2apic_enabled();
1310
1311 if (eim && !ecap_eim_support(iommu->ecap)) {
1312 pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
1313 iommu->reg_phys, iommu->ecap);
1314 return -ENODEV;
1315 }
1316
1317 if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
1318 pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
1319 iommu->reg_phys);
1320 return -ENODEV;
1321 }
1322
1323 /* TODO: check all IOAPICs are covered by IOMMU */
1324
1325 /* Setup Interrupt-remapping now. */
1326 ret = intel_setup_irq_remapping(iommu);
1327 if (ret) {
Joerg Roedel9e4e49d2015-06-12 14:23:56 +02001328 pr_err("Failed to setup irq remapping for %s\n",
1329 iommu->name);
Jiang Liua7a3dad2014-11-09 22:48:00 +08001330 intel_teardown_irq_remapping(iommu);
1331 ir_remove_ioapic_hpet_scope(iommu);
Joerg Roedel9e4e49d2015-06-12 14:23:56 +02001332 } else {
Joerg Roedeld4d1c0f2015-06-12 14:35:54 +02001333 iommu_enable_irq_remapping(iommu);
Jiang Liua7a3dad2014-11-09 22:48:00 +08001334 }
1335
1336 return ret;
1337}
1338
Jiang Liu6b197242014-11-09 22:47:58 +08001339int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
1340{
Jiang Liua7a3dad2014-11-09 22:48:00 +08001341 int ret = 0;
1342 struct intel_iommu *iommu = dmaru->iommu;
1343
1344 if (!irq_remapping_enabled)
1345 return 0;
1346 if (iommu == NULL)
1347 return -EINVAL;
1348 if (!ecap_ir_support(iommu->ecap))
1349 return 0;
1350
1351 if (insert) {
1352 if (!iommu->ir_table)
1353 ret = dmar_ir_add(dmaru, iommu);
1354 } else {
1355 if (iommu->ir_table) {
1356 if (!bitmap_empty(iommu->ir_table->bitmap,
1357 INTR_REMAP_TABLE_ENTRIES)) {
1358 ret = -EBUSY;
1359 } else {
1360 iommu_disable_irq_remapping(iommu);
1361 intel_teardown_irq_remapping(iommu);
1362 ir_remove_ioapic_hpet_scope(iommu);
1363 }
1364 }
1365 }
1366
1367 return ret;
Jiang Liu6b197242014-11-09 22:47:58 +08001368}