blob: 212fff0c24b56e69e65cfa3932e2be6a903d8baf [file] [log] [blame]
Yinghai Lu5aeecaf2008-08-19 20:49:59 -07001#include <linux/interrupt.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -07002#include <linux/dmar.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07003#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07005#include <linux/jiffies.h>
Suresh Siddha20f30972009-08-04 12:07:08 -07006#include <linux/hpet.h>
Suresh Siddha2ae21012008-07-10 11:16:43 -07007#include <linux/pci.h>
Suresh Siddhab6fcb332008-07-10 11:16:44 -07008#include <linux/irq.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -07009#include <asm/io_apic.h>
Yinghai Lu17483a12008-12-12 13:14:18 -080010#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053011#include <asm/cpu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030012#include <linux/intel-iommu.h>
Alexander Beregalov46f06b722009-04-06 16:45:28 +010013#include <acpi/acpi.h>
Weidong Hanf007e992009-05-23 00:41:15 +080014#include <asm/pci-direct.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070015
Joerg Roedeleef93fd2012-03-30 11:46:59 -070016struct ioapic_scope {
17 struct intel_iommu *iommu;
18 unsigned int id;
19 unsigned int bus; /* PCI bus number */
20 unsigned int devfn; /* PCI devfn number */
21};
22
23struct hpet_scope {
24 struct intel_iommu *iommu;
25 u8 id;
26 unsigned int bus;
27 unsigned int devfn;
28};
29
30#define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
31
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070032static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070033static struct hpet_scope ir_hpet[MAX_HPET_TBS];
34static int ir_ioapic_num, ir_hpet_num;
Suresh Siddha2ae21012008-07-10 11:16:43 -070035int intr_remapping_enabled;
36
Weidong Han03ea8152009-04-17 16:42:15 +080037static int disable_intremap;
Chris Wrightd1423d52010-07-20 11:06:49 -070038static int disable_sourceid_checking;
Suresh Siddha41750d32011-08-23 17:05:18 -070039static int no_x2apic_optout;
Chris Wrightd1423d52010-07-20 11:06:49 -070040
Weidong Han03ea8152009-04-17 16:42:15 +080041static __init int setup_nointremap(char *str)
42{
43 disable_intremap = 1;
44 return 0;
45}
46early_param("nointremap", setup_nointremap);
47
Chris Wrightd1423d52010-07-20 11:06:49 -070048static __init int setup_intremap(char *str)
49{
50 if (!str)
51 return -EINVAL;
52
Suresh Siddha41750d32011-08-23 17:05:18 -070053 while (*str) {
54 if (!strncmp(str, "on", 2))
55 disable_intremap = 0;
56 else if (!strncmp(str, "off", 3))
57 disable_intremap = 1;
58 else if (!strncmp(str, "nosid", 5))
59 disable_sourceid_checking = 1;
60 else if (!strncmp(str, "no_x2apic_optout", 16))
61 no_x2apic_optout = 1;
62
63 str += strcspn(str, ",");
64 while (*str == ',')
65 str++;
66 }
Chris Wrightd1423d52010-07-20 11:06:49 -070067
68 return 0;
69}
70early_param("intremap", setup_intremap);
71
Thomas Gleixner96f8e982011-07-19 16:28:19 +020072static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
Thomas Gleixnerd585d062010-10-10 12:34:27 +020073
Yinghai Lue420dfb2008-08-19 20:50:21 -070074static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
75{
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020076 struct irq_cfg *cfg = irq_get_chip_data(irq);
Thomas Gleixner349d6762010-10-10 12:29:27 +020077 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080078}
79
Suresh Siddhab6fcb332008-07-10 11:16:44 -070080int get_irte(int irq, struct irte *entry)
81{
Thomas Gleixnerd585d062010-10-10 12:34:27 +020082 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -070083 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +020084 int index;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070085
Thomas Gleixnerd585d062010-10-10 12:34:27 +020086 if (!entry || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070087 return -1;
88
Thomas Gleixner96f8e982011-07-19 16:28:19 +020089 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070090
Yinghai Lue420dfb2008-08-19 20:50:21 -070091 index = irq_iommu->irte_index + irq_iommu->sub_handle;
92 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070093
Thomas Gleixner96f8e982011-07-19 16:28:19 +020094 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070095 return 0;
96}
97
98int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
99{
100 struct ir_table *table = iommu->ir_table;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200101 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700102 u16 index, start_index;
103 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700104 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700105 int i;
106
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200107 if (!count || !irq_iommu)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700108 return -1;
109
110 /*
111 * start the IRTE search from index 0.
112 */
113 index = start_index = 0;
114
115 if (count > 1) {
116 count = __roundup_pow_of_two(count);
117 mask = ilog2(count);
118 }
119
120 if (mask > ecap_max_handle_mask(iommu->ecap)) {
121 printk(KERN_ERR
122 "Requested mask %x exceeds the max invalidation handle"
123 " mask value %Lx\n", mask,
124 ecap_max_handle_mask(iommu->ecap));
125 return -1;
126 }
127
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200128 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700129 do {
130 for (i = index; i < index + count; i++)
131 if (table->base[i].present)
132 break;
133 /* empty index found */
134 if (i == index + count)
135 break;
136
137 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
138
139 if (index == start_index) {
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200140 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700141 printk(KERN_ERR "can't allocate an IRTE\n");
142 return -1;
143 }
144 } while (1);
145
146 for (i = index; i < index + count; i++)
147 table->base[i].present = 1;
148
Yinghai Lue420dfb2008-08-19 20:50:21 -0700149 irq_iommu->iommu = iommu;
150 irq_iommu->irte_index = index;
151 irq_iommu->sub_handle = 0;
152 irq_iommu->irte_mask = mask;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700153
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200154 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700155
156 return index;
157}
158
Yu Zhao704126a2009-01-04 16:28:52 +0800159static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700160{
161 struct qi_desc desc;
162
163 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
164 | QI_IEC_SELECTIVE;
165 desc.high = 0;
166
Yu Zhao704126a2009-01-04 16:28:52 +0800167 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700168}
169
170int map_irq_to_irte_handle(int irq, u16 *sub_handle)
171{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200172 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700173 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200174 int index;
175
176 if (!irq_iommu)
177 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700178
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200179 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700180 *sub_handle = irq_iommu->sub_handle;
181 index = irq_iommu->irte_index;
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200182 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700183 return index;
184}
185
186int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
187{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200188 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700189 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700190
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200191 if (!irq_iommu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800192 return -1;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200193
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200194 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800195
Yinghai Lue420dfb2008-08-19 20:50:21 -0700196 irq_iommu->iommu = iommu;
197 irq_iommu->irte_index = index;
198 irq_iommu->sub_handle = subhandle;
199 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700200
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200201 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700202
203 return 0;
204}
205
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700206int modify_irte(int irq, struct irte *irte_modified)
207{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200208 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700209 struct intel_iommu *iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700210 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200211 struct irte *irte;
212 int rc, index;
213
214 if (!irq_iommu)
215 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700216
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200217 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700218
Yinghai Lue420dfb2008-08-19 20:50:21 -0700219 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700220
Yinghai Lue420dfb2008-08-19 20:50:21 -0700221 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700222 irte = &iommu->ir_table->base[index];
223
Linus Torvaldsc513b672010-08-06 11:02:31 -0700224 set_64bit(&irte->low, irte_modified->low);
225 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700226 __iommu_flush_cache(iommu, irte, sizeof(*irte));
227
Yu Zhao704126a2009-01-04 16:28:52 +0800228 rc = qi_flush_iec(iommu, index, 0);
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200229 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800230
231 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700232}
233
Suresh Siddha20f30972009-08-04 12:07:08 -0700234struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
235{
236 int i;
237
238 for (i = 0; i < MAX_HPET_TBS; i++)
239 if (ir_hpet[i].id == hpet_id)
240 return ir_hpet[i].iommu;
241 return NULL;
242}
243
Suresh Siddha89027d32008-07-10 11:16:56 -0700244struct intel_iommu *map_ioapic_to_ir(int apic)
245{
246 int i;
247
248 for (i = 0; i < MAX_IO_APICS; i++)
249 if (ir_ioapic[i].id == apic)
250 return ir_ioapic[i].iommu;
251 return NULL;
252}
253
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700254struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
255{
256 struct dmar_drhd_unit *drhd;
257
258 drhd = dmar_find_matched_drhd_unit(dev);
259 if (!drhd)
260 return NULL;
261
262 return drhd->iommu;
263}
264
Weidong Hanc4658b42009-05-23 00:41:14 +0800265static int clear_entries(struct irq_2_iommu *irq_iommu)
266{
267 struct irte *start, *entry, *end;
268 struct intel_iommu *iommu;
269 int index;
270
271 if (irq_iommu->sub_handle)
272 return 0;
273
274 iommu = irq_iommu->iommu;
275 index = irq_iommu->irte_index + irq_iommu->sub_handle;
276
277 start = iommu->ir_table->base + index;
278 end = start + (1 << irq_iommu->irte_mask);
279
280 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700281 set_64bit(&entry->low, 0);
282 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800283 }
284
285 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
286}
287
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700288int free_irte(int irq)
289{
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200290 struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700291 unsigned long flags;
Thomas Gleixnerd585d062010-10-10 12:34:27 +0200292 int rc;
293
294 if (!irq_iommu)
295 return -1;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700296
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200297 raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700298
Weidong Hanc4658b42009-05-23 00:41:14 +0800299 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700300
Yinghai Lue420dfb2008-08-19 20:50:21 -0700301 irq_iommu->iommu = NULL;
302 irq_iommu->irte_index = 0;
303 irq_iommu->sub_handle = 0;
304 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700305
Thomas Gleixner96f8e982011-07-19 16:28:19 +0200306 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700307
Yu Zhao704126a2009-01-04 16:28:52 +0800308 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700309}
310
Weidong Hanf007e992009-05-23 00:41:15 +0800311/*
312 * source validation type
313 */
314#define SVT_NO_VERIFY 0x0 /* no verification is required */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300315#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fields */
Weidong Hanf007e992009-05-23 00:41:15 +0800316#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
317
318/*
319 * source-id qualifier
320 */
321#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
322#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
323 * the third least significant bit
324 */
325#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
326 * the second and third least significant bits
327 */
328#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
329 * the least three significant bits
330 */
331
332/*
333 * set SVT, SQ and SID fields of irte to verify
334 * source ids of interrupt requests
335 */
336static void set_irte_sid(struct irte *irte, unsigned int svt,
337 unsigned int sq, unsigned int sid)
338{
Chris Wrightd1423d52010-07-20 11:06:49 -0700339 if (disable_sourceid_checking)
340 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800341 irte->svt = svt;
342 irte->sq = sq;
343 irte->sid = sid;
344}
345
346int set_ioapic_sid(struct irte *irte, int apic)
347{
348 int i;
349 u16 sid = 0;
350
351 if (!irte)
352 return -1;
353
354 for (i = 0; i < MAX_IO_APICS; i++) {
355 if (ir_ioapic[i].id == apic) {
356 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
357 break;
358 }
359 }
360
361 if (sid == 0) {
362 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
363 return -1;
364 }
365
366 set_irte_sid(irte, 1, 0, sid);
367
368 return 0;
369}
370
Suresh Siddha20f30972009-08-04 12:07:08 -0700371int set_hpet_sid(struct irte *irte, u8 id)
372{
373 int i;
374 u16 sid = 0;
375
376 if (!irte)
377 return -1;
378
379 for (i = 0; i < MAX_HPET_TBS; i++) {
380 if (ir_hpet[i].id == id) {
381 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
382 break;
383 }
384 }
385
386 if (sid == 0) {
387 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
388 return -1;
389 }
390
391 /*
392 * Should really use SQ_ALL_16. Some platforms are broken.
393 * While we figure out the right quirks for these broken platforms, use
394 * SQ_13_IGNORE_3 for now.
395 */
396 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
397
398 return 0;
399}
400
Weidong Hanf007e992009-05-23 00:41:15 +0800401int set_msi_sid(struct irte *irte, struct pci_dev *dev)
402{
403 struct pci_dev *bridge;
404
405 if (!irte || !dev)
406 return -1;
407
408 /* PCIe device or Root Complex integrated PCI device */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900409 if (pci_is_pcie(dev) || !dev->bus->parent) {
Weidong Hanf007e992009-05-23 00:41:15 +0800410 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
411 (dev->bus->number << 8) | dev->devfn);
412 return 0;
413 }
414
415 bridge = pci_find_upstream_pcie_bridge(dev);
416 if (bridge) {
Stefan Assmann45e829e2009-12-03 06:49:24 -0500417 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
Weidong Hanf007e992009-05-23 00:41:15 +0800418 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
419 (bridge->bus->number << 8) | dev->bus->number);
420 else /* this is a legacy PCI bridge */
421 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
422 (bridge->bus->number << 8) | bridge->devfn);
423 }
424
425 return 0;
426}
427
Suresh Siddha2ae21012008-07-10 11:16:43 -0700428static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
429{
430 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100431 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700432 unsigned long flags;
433
434 addr = virt_to_phys((void *)iommu->ir_table->base);
435
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200436 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700437
438 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
439 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
440
441 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800442 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100443 writel(iommu->gcmd, 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;
David Woodhousec416daa2009-05-10 20:30:58 +0100459 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700460
461 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
462 readl, (sts & DMA_GSTS_IRES), sts);
463
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200464 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700465}
466
467
468static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
469{
470 struct ir_table *ir_table;
471 struct page *pages;
472
473 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700474 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700475
476 if (!iommu->ir_table)
477 return -ENOMEM;
478
Suresh Siddha824cd752009-10-02 11:01:23 -0700479 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
480 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700481
482 if (!pages) {
483 printk(KERN_ERR "failed to allocate pages of order %d\n",
484 INTR_REMAP_PAGE_ORDER);
485 kfree(iommu->ir_table);
486 return -ENOMEM;
487 }
488
489 ir_table->base = page_address(pages);
490
491 iommu_set_intr_remapping(iommu, mode);
492 return 0;
493}
494
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700495/*
496 * Disable Interrupt Remapping.
497 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700498static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700499{
500 unsigned long flags;
501 u32 sts;
502
503 if (!ecap_ir_support(iommu->ecap))
504 return;
505
Fenghua Yub24696b2009-03-27 14:22:44 -0700506 /*
507 * global invalidation of interrupt entry cache before disabling
508 * interrupt-remapping.
509 */
510 qi_global_iec(iommu);
511
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200512 raw_spin_lock_irqsave(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700513
514 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
515 if (!(sts & DMA_GSTS_IRES))
516 goto end;
517
518 iommu->gcmd &= ~DMA_GCMD_IRE;
519 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
520
521 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
522 readl, !(sts & DMA_GSTS_IRES), sts);
523
524end:
Thomas Gleixner1f5b3c32011-07-19 16:19:51 +0200525 raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700526}
527
Suresh Siddha41750d32011-08-23 17:05:18 -0700528static int __init dmar_x2apic_optout(void)
529{
530 struct acpi_table_dmar *dmar;
531 dmar = (struct acpi_table_dmar *)dmar_tbl;
532 if (!dmar || no_x2apic_optout)
533 return 0;
534 return dmar->flags & DMAR_X2APIC_OPT_OUT;
535}
536
Weidong Han93758232009-04-17 16:42:14 +0800537int __init intr_remapping_supported(void)
538{
539 struct dmar_drhd_unit *drhd;
540
Weidong Han03ea8152009-04-17 16:42:15 +0800541 if (disable_intremap)
542 return 0;
543
Youquan Song074835f2009-09-09 12:05:39 -0400544 if (!dmar_ir_support())
545 return 0;
546
Weidong Han93758232009-04-17 16:42:14 +0800547 for_each_drhd_unit(drhd) {
548 struct intel_iommu *iommu = drhd->iommu;
549
550 if (!ecap_ir_support(iommu->ecap))
551 return 0;
552 }
553
554 return 1;
555}
556
Suresh Siddha41750d32011-08-23 17:05:18 -0700557int __init enable_intr_remapping(void)
Suresh Siddha2ae21012008-07-10 11:16:43 -0700558{
559 struct dmar_drhd_unit *drhd;
560 int setup = 0;
Suresh Siddha41750d32011-08-23 17:05:18 -0700561 int eim = 0;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700562
Youquan Songe936d072009-09-07 10:58:07 -0400563 if (parse_ioapics_under_ir() != 1) {
564 printk(KERN_INFO "Not enable interrupt remapping\n");
565 return -1;
566 }
567
Suresh Siddha41750d32011-08-23 17:05:18 -0700568 if (x2apic_supported()) {
569 eim = !dmar_x2apic_optout();
570 WARN(!eim, KERN_WARNING
571 "Your BIOS is broken and requested that x2apic be disabled\n"
572 "This will leave your machine vulnerable to irq-injection attacks\n"
573 "Use 'intremap=no_x2apic_optout' to override BIOS request\n");
574 }
575
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700576 for_each_drhd_unit(drhd) {
577 struct intel_iommu *iommu = drhd->iommu;
578
579 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800580 * If the queued invalidation is already initialized,
581 * shouldn't disable it.
582 */
583 if (iommu->qi)
584 continue;
585
586 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700587 * Clear previous faults.
588 */
589 dmar_fault(-1, iommu);
590
591 /*
592 * Disable intr remapping and queued invalidation, if already
593 * enabled prior to OS handover.
594 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700595 iommu_disable_intr_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700596
597 dmar_disable_qi(iommu);
598 }
599
Suresh Siddha2ae21012008-07-10 11:16:43 -0700600 /*
601 * check for the Interrupt-remapping support
602 */
603 for_each_drhd_unit(drhd) {
604 struct intel_iommu *iommu = drhd->iommu;
605
606 if (!ecap_ir_support(iommu->ecap))
607 continue;
608
609 if (eim && !ecap_eim_support(iommu->ecap)) {
610 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
611 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
612 return -1;
613 }
614 }
615
616 /*
617 * Enable queued invalidation for all the DRHD's.
618 */
619 for_each_drhd_unit(drhd) {
620 int ret;
621 struct intel_iommu *iommu = drhd->iommu;
622 ret = dmar_enable_qi(iommu);
623
624 if (ret) {
625 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
626 " invalidation, ecap %Lx, ret %d\n",
627 drhd->reg_base_addr, iommu->ecap, ret);
628 return -1;
629 }
630 }
631
632 /*
633 * Setup Interrupt-remapping for all the DRHD's now.
634 */
635 for_each_drhd_unit(drhd) {
636 struct intel_iommu *iommu = drhd->iommu;
637
638 if (!ecap_ir_support(iommu->ecap))
639 continue;
640
641 if (setup_intr_remapping(iommu, eim))
642 goto error;
643
644 setup = 1;
645 }
646
647 if (!setup)
648 goto error;
649
650 intr_remapping_enabled = 1;
Suresh Siddha41750d32011-08-23 17:05:18 -0700651 pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
Suresh Siddha2ae21012008-07-10 11:16:43 -0700652
Suresh Siddha41750d32011-08-23 17:05:18 -0700653 return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700654
655error:
656 /*
657 * handle error condition gracefully here!
658 */
659 return -1;
660}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700661
Suresh Siddha20f30972009-08-04 12:07:08 -0700662static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
663 struct intel_iommu *iommu)
664{
665 struct acpi_dmar_pci_path *path;
666 u8 bus;
667 int count;
668
669 bus = scope->bus;
670 path = (struct acpi_dmar_pci_path *)(scope + 1);
671 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
672 / sizeof(struct acpi_dmar_pci_path);
673
674 while (--count > 0) {
675 /*
676 * Access PCI directly due to the PCI
677 * subsystem isn't initialized yet.
678 */
679 bus = read_pci_config_byte(bus, path->dev, path->fn,
680 PCI_SECONDARY_BUS);
681 path++;
682 }
683 ir_hpet[ir_hpet_num].bus = bus;
684 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
685 ir_hpet[ir_hpet_num].iommu = iommu;
686 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
687 ir_hpet_num++;
688}
689
Weidong Hanf007e992009-05-23 00:41:15 +0800690static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
691 struct intel_iommu *iommu)
692{
693 struct acpi_dmar_pci_path *path;
694 u8 bus;
695 int count;
696
697 bus = scope->bus;
698 path = (struct acpi_dmar_pci_path *)(scope + 1);
699 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
700 / sizeof(struct acpi_dmar_pci_path);
701
702 while (--count > 0) {
703 /*
704 * Access PCI directly due to the PCI
705 * subsystem isn't initialized yet.
706 */
707 bus = read_pci_config_byte(bus, path->dev, path->fn,
708 PCI_SECONDARY_BUS);
709 path++;
710 }
711
712 ir_ioapic[ir_ioapic_num].bus = bus;
713 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
714 ir_ioapic[ir_ioapic_num].iommu = iommu;
715 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
716 ir_ioapic_num++;
717}
718
Suresh Siddha20f30972009-08-04 12:07:08 -0700719static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
720 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700721{
722 struct acpi_dmar_hardware_unit *drhd;
723 struct acpi_dmar_device_scope *scope;
724 void *start, *end;
725
726 drhd = (struct acpi_dmar_hardware_unit *)header;
727
728 start = (void *)(drhd + 1);
729 end = ((void *)drhd) + header->length;
730
731 while (start < end) {
732 scope = start;
733 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
734 if (ir_ioapic_num == MAX_IO_APICS) {
735 printk(KERN_WARNING "Exceeded Max IO APICS\n");
736 return -1;
737 }
738
Yinghai Lu680a7522010-04-08 19:58:23 +0100739 printk(KERN_INFO "IOAPIC id %d under DRHD base "
740 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
741 drhd->address, iommu->seq_id);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700742
Weidong Hanf007e992009-05-23 00:41:15 +0800743 ir_parse_one_ioapic_scope(scope, iommu);
Suresh Siddha20f30972009-08-04 12:07:08 -0700744 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
745 if (ir_hpet_num == MAX_HPET_TBS) {
746 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
747 return -1;
748 }
749
750 printk(KERN_INFO "HPET id %d under DRHD base"
751 " 0x%Lx\n", scope->enumeration_id,
752 drhd->address);
753
754 ir_parse_one_hpet_scope(scope, iommu);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700755 }
756 start += scope->length;
757 }
758
759 return 0;
760}
761
762/*
763 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
764 * hardware unit.
765 */
766int __init parse_ioapics_under_ir(void)
767{
768 struct dmar_drhd_unit *drhd;
769 int ir_supported = 0;
770
771 for_each_drhd_unit(drhd) {
772 struct intel_iommu *iommu = drhd->iommu;
773
774 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700775 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700776 return -1;
777
778 ir_supported = 1;
779 }
780 }
781
782 if (ir_supported && ir_ioapic_num != nr_ioapics) {
783 printk(KERN_WARNING
784 "Not all IO-APIC's listed under remapping hardware\n");
785 return -1;
786 }
787
788 return ir_supported;
789}
Fenghua Yub24696b2009-03-27 14:22:44 -0700790
Sergey Senozhatsky61ed26e2011-10-26 19:15:07 +0300791int __init ir_dev_scope_init(void)
Suresh Siddhac2c72862011-08-23 17:05:19 -0700792{
793 if (!intr_remapping_enabled)
794 return 0;
795
796 return dmar_dev_scope_init();
797}
798rootfs_initcall(ir_dev_scope_init);
799
Fenghua Yub24696b2009-03-27 14:22:44 -0700800void disable_intr_remapping(void)
801{
802 struct dmar_drhd_unit *drhd;
803 struct intel_iommu *iommu = NULL;
804
805 /*
806 * Disable Interrupt-remapping for all the DRHD's now.
807 */
808 for_each_iommu(iommu, drhd) {
809 if (!ecap_ir_support(iommu->ecap))
810 continue;
811
812 iommu_disable_intr_remapping(iommu);
813 }
814}
815
816int reenable_intr_remapping(int eim)
817{
818 struct dmar_drhd_unit *drhd;
819 int setup = 0;
820 struct intel_iommu *iommu = NULL;
821
822 for_each_iommu(iommu, drhd)
823 if (iommu->qi)
824 dmar_reenable_qi(iommu);
825
826 /*
827 * Setup Interrupt-remapping for all the DRHD's now.
828 */
829 for_each_iommu(iommu, drhd) {
830 if (!ecap_ir_support(iommu->ecap))
831 continue;
832
833 /* Set up interrupt remapping for iommu.*/
834 iommu_set_intr_remapping(iommu, eim);
835 setup = 1;
836 }
837
838 if (!setup)
839 goto error;
840
841 return 0;
842
843error:
844 /*
845 * handle error condition gracefully here!
846 */
847 return -1;
848}
849