blob: c26633d7e7da75adbe48e63e4ef54bb18a26e9d7 [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>
4#include <linux/jiffies.h>
5#include <linux/pci.h>
Suresh Siddhab6fcb332008-07-10 11:16:44 -07006#include <linux/irq.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -07007#include <asm/io_apic.h>
Yinghai Lu17483a12008-12-12 13:14:18 -08008#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +05309#include <asm/cpu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030010#include <linux/intel-iommu.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070011#include "intr_remapping.h"
12
13static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
14static int ir_ioapic_num;
Suresh Siddha2ae21012008-07-10 11:16:43 -070015int intr_remapping_enabled;
16
Yinghai Lu5aeecaf2008-08-19 20:49:59 -070017struct irq_2_iommu {
Suresh Siddhab6fcb332008-07-10 11:16:44 -070018 struct intel_iommu *iommu;
19 u16 irte_index;
20 u16 sub_handle;
21 u8 irte_mask;
Yinghai Lu5aeecaf2008-08-19 20:49:59 -070022};
23
Yinghai Lud7e51e62009-01-07 15:03:13 -080024#ifdef CONFIG_GENERIC_HARDIRQS
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080025static struct irq_2_iommu *get_one_free_irq_2_iommu(int cpu)
26{
27 struct irq_2_iommu *iommu;
28 int node;
29
30 node = cpu_to_node(cpu);
31
32 iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node);
33 printk(KERN_DEBUG "alloc irq_2_iommu on cpu %d node %d\n", cpu, node);
34
35 return iommu;
36}
Yinghai Lue420dfb2008-08-19 20:50:21 -070037
38static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
39{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080040 struct irq_desc *desc;
41
42 desc = irq_to_desc(irq);
43
44 if (WARN_ON_ONCE(!desc))
45 return NULL;
46
47 return desc->irq_2_iommu;
48}
49
50static struct irq_2_iommu *irq_2_iommu_alloc_cpu(unsigned int irq, int cpu)
51{
52 struct irq_desc *desc;
53 struct irq_2_iommu *irq_iommu;
54
55 /*
56 * alloc irq desc if not allocated already.
57 */
58 desc = irq_to_desc_alloc_cpu(irq, cpu);
59 if (!desc) {
60 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
61 return NULL;
62 }
63
64 irq_iommu = desc->irq_2_iommu;
65
66 if (!irq_iommu)
67 desc->irq_2_iommu = get_one_free_irq_2_iommu(cpu);
68
69 return desc->irq_2_iommu;
Yinghai Lue420dfb2008-08-19 20:50:21 -070070}
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +020071
Yinghai Lue420dfb2008-08-19 20:50:21 -070072static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
73{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080074 return irq_2_iommu_alloc_cpu(irq, boot_cpu_id);
75}
76
77#else /* !CONFIG_SPARSE_IRQ */
78
79static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
80
81static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
82{
83 if (irq < nr_irqs)
84 return &irq_2_iommuX[irq];
85
86 return NULL;
87}
88static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
89{
Yinghai Lue420dfb2008-08-19 20:50:21 -070090 return irq_2_iommu(irq);
91}
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080092#endif
Suresh Siddhab6fcb332008-07-10 11:16:44 -070093
94static DEFINE_SPINLOCK(irq_2_ir_lock);
95
Yinghai Lue420dfb2008-08-19 20:50:21 -070096static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
97{
98 struct irq_2_iommu *irq_iommu;
99
100 irq_iommu = irq_2_iommu(irq);
101
102 if (!irq_iommu)
103 return NULL;
104
105 if (!irq_iommu->iommu)
106 return NULL;
107
108 return irq_iommu;
109}
110
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700111int irq_remapped(int irq)
112{
Yinghai Lue420dfb2008-08-19 20:50:21 -0700113 return valid_irq_2_iommu(irq) != NULL;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700114}
115
116int get_irte(int irq, struct irte *entry)
117{
118 int index;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700119 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700120 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700121
Yinghai Lue420dfb2008-08-19 20:50:21 -0700122 if (!entry)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700123 return -1;
124
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700125 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700126 irq_iommu = valid_irq_2_iommu(irq);
127 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700128 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700129 return -1;
130 }
131
Yinghai Lue420dfb2008-08-19 20:50:21 -0700132 index = irq_iommu->irte_index + irq_iommu->sub_handle;
133 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700134
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700135 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700136 return 0;
137}
138
139int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
140{
141 struct ir_table *table = iommu->ir_table;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700142 struct irq_2_iommu *irq_iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700143 u16 index, start_index;
144 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700145 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700146 int i;
147
148 if (!count)
149 return -1;
150
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800151#ifndef CONFIG_SPARSE_IRQ
Yinghai Lue420dfb2008-08-19 20:50:21 -0700152 /* protect irq_2_iommu_alloc later */
153 if (irq >= nr_irqs)
154 return -1;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800155#endif
Yinghai Lue420dfb2008-08-19 20:50:21 -0700156
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700157 /*
158 * start the IRTE search from index 0.
159 */
160 index = start_index = 0;
161
162 if (count > 1) {
163 count = __roundup_pow_of_two(count);
164 mask = ilog2(count);
165 }
166
167 if (mask > ecap_max_handle_mask(iommu->ecap)) {
168 printk(KERN_ERR
169 "Requested mask %x exceeds the max invalidation handle"
170 " mask value %Lx\n", mask,
171 ecap_max_handle_mask(iommu->ecap));
172 return -1;
173 }
174
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700175 spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700176 do {
177 for (i = index; i < index + count; i++)
178 if (table->base[i].present)
179 break;
180 /* empty index found */
181 if (i == index + count)
182 break;
183
184 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
185
186 if (index == start_index) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700187 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700188 printk(KERN_ERR "can't allocate an IRTE\n");
189 return -1;
190 }
191 } while (1);
192
193 for (i = index; i < index + count; i++)
194 table->base[i].present = 1;
195
Yinghai Lue420dfb2008-08-19 20:50:21 -0700196 irq_iommu = irq_2_iommu_alloc(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800197 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700198 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800199 printk(KERN_ERR "can't allocate irq_2_iommu\n");
200 return -1;
201 }
202
Yinghai Lue420dfb2008-08-19 20:50:21 -0700203 irq_iommu->iommu = iommu;
204 irq_iommu->irte_index = index;
205 irq_iommu->sub_handle = 0;
206 irq_iommu->irte_mask = mask;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700207
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700208 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700209
210 return index;
211}
212
Yu Zhao704126a2009-01-04 16:28:52 +0800213static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700214{
215 struct qi_desc desc;
216
217 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
218 | QI_IEC_SELECTIVE;
219 desc.high = 0;
220
Yu Zhao704126a2009-01-04 16:28:52 +0800221 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700222}
223
224int map_irq_to_irte_handle(int irq, u16 *sub_handle)
225{
226 int index;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700227 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700228 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700229
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700230 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700231 irq_iommu = valid_irq_2_iommu(irq);
232 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700233 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700234 return -1;
235 }
236
Yinghai Lue420dfb2008-08-19 20:50:21 -0700237 *sub_handle = irq_iommu->sub_handle;
238 index = irq_iommu->irte_index;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700239 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700240 return index;
241}
242
243int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
244{
Yinghai Lue420dfb2008-08-19 20:50:21 -0700245 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700246 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700247
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700248 spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddha7ddfb652008-08-20 17:22:51 -0700249
250 irq_iommu = irq_2_iommu_alloc(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700251
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800252 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700253 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800254 printk(KERN_ERR "can't allocate irq_2_iommu\n");
255 return -1;
256 }
257
Yinghai Lue420dfb2008-08-19 20:50:21 -0700258 irq_iommu->iommu = iommu;
259 irq_iommu->irte_index = index;
260 irq_iommu->sub_handle = subhandle;
261 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700262
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700263 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700264
265 return 0;
266}
267
268int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
269{
Yinghai Lue420dfb2008-08-19 20:50:21 -0700270 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700271 unsigned long flags;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700272
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700273 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700274 irq_iommu = valid_irq_2_iommu(irq);
275 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700276 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700277 return -1;
278 }
279
Yinghai Lue420dfb2008-08-19 20:50:21 -0700280 irq_iommu->iommu = NULL;
281 irq_iommu->irte_index = 0;
282 irq_iommu->sub_handle = 0;
283 irq_2_iommu(irq)->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700284
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700285 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700286
287 return 0;
288}
289
290int modify_irte(int irq, struct irte *irte_modified)
291{
Yu Zhao704126a2009-01-04 16:28:52 +0800292 int rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700293 int index;
294 struct irte *irte;
295 struct intel_iommu *iommu;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700296 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700297 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700298
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700299 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700300 irq_iommu = valid_irq_2_iommu(irq);
301 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700302 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700303 return -1;
304 }
305
Yinghai Lue420dfb2008-08-19 20:50:21 -0700306 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700307
Yinghai Lue420dfb2008-08-19 20:50:21 -0700308 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700309 irte = &iommu->ir_table->base[index];
310
Suresh Siddha9d783ba2009-03-16 17:04:55 -0700311 set_64bit((unsigned long *)irte, irte_modified->low);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700312 __iommu_flush_cache(iommu, irte, sizeof(*irte));
313
Yu Zhao704126a2009-01-04 16:28:52 +0800314 rc = qi_flush_iec(iommu, index, 0);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700315 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800316
317 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700318}
319
320int flush_irte(int irq)
321{
Yu Zhao704126a2009-01-04 16:28:52 +0800322 int rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700323 int index;
324 struct intel_iommu *iommu;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700325 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700326 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700327
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700328 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700329 irq_iommu = valid_irq_2_iommu(irq);
330 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700331 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700332 return -1;
333 }
334
Yinghai Lue420dfb2008-08-19 20:50:21 -0700335 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700336
Yinghai Lue420dfb2008-08-19 20:50:21 -0700337 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700338
Yu Zhao704126a2009-01-04 16:28:52 +0800339 rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700340 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700341
Yu Zhao704126a2009-01-04 16:28:52 +0800342 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700343}
344
Suresh Siddha89027d32008-07-10 11:16:56 -0700345struct intel_iommu *map_ioapic_to_ir(int apic)
346{
347 int i;
348
349 for (i = 0; i < MAX_IO_APICS; i++)
350 if (ir_ioapic[i].id == apic)
351 return ir_ioapic[i].iommu;
352 return NULL;
353}
354
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700355struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
356{
357 struct dmar_drhd_unit *drhd;
358
359 drhd = dmar_find_matched_drhd_unit(dev);
360 if (!drhd)
361 return NULL;
362
363 return drhd->iommu;
364}
365
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700366int free_irte(int irq)
367{
Yu Zhao704126a2009-01-04 16:28:52 +0800368 int rc = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700369 int index, i;
370 struct irte *irte;
371 struct intel_iommu *iommu;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700372 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700373 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700374
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700375 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700376 irq_iommu = valid_irq_2_iommu(irq);
377 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700378 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700379 return -1;
380 }
381
Yinghai Lue420dfb2008-08-19 20:50:21 -0700382 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700383
Yinghai Lue420dfb2008-08-19 20:50:21 -0700384 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700385 irte = &iommu->ir_table->base[index];
386
Yinghai Lue420dfb2008-08-19 20:50:21 -0700387 if (!irq_iommu->sub_handle) {
388 for (i = 0; i < (1 << irq_iommu->irte_mask); i++)
Suresh Siddha2e934562009-03-16 17:04:58 -0700389 set_64bit((unsigned long *)(irte + i), 0);
Yu Zhao704126a2009-01-04 16:28:52 +0800390 rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700391 }
392
Yinghai Lue420dfb2008-08-19 20:50:21 -0700393 irq_iommu->iommu = NULL;
394 irq_iommu->irte_index = 0;
395 irq_iommu->sub_handle = 0;
396 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700397
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700398 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700399
Yu Zhao704126a2009-01-04 16:28:52 +0800400 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700401}
402
Suresh Siddha2ae21012008-07-10 11:16:43 -0700403static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
404{
405 u64 addr;
406 u32 cmd, sts;
407 unsigned long flags;
408
409 addr = virt_to_phys((void *)iommu->ir_table->base);
410
411 spin_lock_irqsave(&iommu->register_lock, flags);
412
413 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
414 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
415
416 /* Set interrupt-remapping table pointer */
417 cmd = iommu->gcmd | DMA_GCMD_SIRTP;
418 writel(cmd, iommu->reg + DMAR_GCMD_REG);
419
420 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
421 readl, (sts & DMA_GSTS_IRTPS), sts);
422 spin_unlock_irqrestore(&iommu->register_lock, flags);
423
424 /*
425 * global invalidation of interrupt entry cache before enabling
426 * interrupt-remapping.
427 */
428 qi_global_iec(iommu);
429
430 spin_lock_irqsave(&iommu->register_lock, flags);
431
432 /* Enable interrupt-remapping */
433 cmd = iommu->gcmd | DMA_GCMD_IRE;
434 iommu->gcmd |= DMA_GCMD_IRE;
435 writel(cmd, iommu->reg + DMAR_GCMD_REG);
436
437 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
438 readl, (sts & DMA_GSTS_IRES), sts);
439
440 spin_unlock_irqrestore(&iommu->register_lock, flags);
441}
442
443
444static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
445{
446 struct ir_table *ir_table;
447 struct page *pages;
448
449 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700450 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700451
452 if (!iommu->ir_table)
453 return -ENOMEM;
454
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700455 pages = alloc_pages(GFP_ATOMIC | __GFP_ZERO, INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700456
457 if (!pages) {
458 printk(KERN_ERR "failed to allocate pages of order %d\n",
459 INTR_REMAP_PAGE_ORDER);
460 kfree(iommu->ir_table);
461 return -ENOMEM;
462 }
463
464 ir_table->base = page_address(pages);
465
466 iommu_set_intr_remapping(iommu, mode);
467 return 0;
468}
469
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700470/*
471 * Disable Interrupt Remapping.
472 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700473static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700474{
475 unsigned long flags;
476 u32 sts;
477
478 if (!ecap_ir_support(iommu->ecap))
479 return;
480
Fenghua Yub24696b2009-03-27 14:22:44 -0700481 /*
482 * global invalidation of interrupt entry cache before disabling
483 * interrupt-remapping.
484 */
485 qi_global_iec(iommu);
486
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700487 spin_lock_irqsave(&iommu->register_lock, flags);
488
489 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
490 if (!(sts & DMA_GSTS_IRES))
491 goto end;
492
493 iommu->gcmd &= ~DMA_GCMD_IRE;
494 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
495
496 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
497 readl, !(sts & DMA_GSTS_IRES), sts);
498
499end:
500 spin_unlock_irqrestore(&iommu->register_lock, flags);
501}
502
Suresh Siddha2ae21012008-07-10 11:16:43 -0700503int __init enable_intr_remapping(int eim)
504{
505 struct dmar_drhd_unit *drhd;
506 int setup = 0;
507
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700508 for_each_drhd_unit(drhd) {
509 struct intel_iommu *iommu = drhd->iommu;
510
511 /*
512 * Clear previous faults.
513 */
514 dmar_fault(-1, iommu);
515
516 /*
517 * Disable intr remapping and queued invalidation, if already
518 * enabled prior to OS handover.
519 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700520 iommu_disable_intr_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700521
522 dmar_disable_qi(iommu);
523 }
524
Suresh Siddha2ae21012008-07-10 11:16:43 -0700525 /*
526 * check for the Interrupt-remapping support
527 */
528 for_each_drhd_unit(drhd) {
529 struct intel_iommu *iommu = drhd->iommu;
530
531 if (!ecap_ir_support(iommu->ecap))
532 continue;
533
534 if (eim && !ecap_eim_support(iommu->ecap)) {
535 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
536 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
537 return -1;
538 }
539 }
540
541 /*
542 * Enable queued invalidation for all the DRHD's.
543 */
544 for_each_drhd_unit(drhd) {
545 int ret;
546 struct intel_iommu *iommu = drhd->iommu;
547 ret = dmar_enable_qi(iommu);
548
549 if (ret) {
550 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
551 " invalidation, ecap %Lx, ret %d\n",
552 drhd->reg_base_addr, iommu->ecap, ret);
553 return -1;
554 }
555 }
556
557 /*
558 * Setup Interrupt-remapping for all the DRHD's now.
559 */
560 for_each_drhd_unit(drhd) {
561 struct intel_iommu *iommu = drhd->iommu;
562
563 if (!ecap_ir_support(iommu->ecap))
564 continue;
565
566 if (setup_intr_remapping(iommu, eim))
567 goto error;
568
569 setup = 1;
570 }
571
572 if (!setup)
573 goto error;
574
575 intr_remapping_enabled = 1;
576
577 return 0;
578
579error:
580 /*
581 * handle error condition gracefully here!
582 */
583 return -1;
584}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700585
586static int ir_parse_ioapic_scope(struct acpi_dmar_header *header,
587 struct intel_iommu *iommu)
588{
589 struct acpi_dmar_hardware_unit *drhd;
590 struct acpi_dmar_device_scope *scope;
591 void *start, *end;
592
593 drhd = (struct acpi_dmar_hardware_unit *)header;
594
595 start = (void *)(drhd + 1);
596 end = ((void *)drhd) + header->length;
597
598 while (start < end) {
599 scope = start;
600 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
601 if (ir_ioapic_num == MAX_IO_APICS) {
602 printk(KERN_WARNING "Exceeded Max IO APICS\n");
603 return -1;
604 }
605
606 printk(KERN_INFO "IOAPIC id %d under DRHD base"
607 " 0x%Lx\n", scope->enumeration_id,
608 drhd->address);
609
610 ir_ioapic[ir_ioapic_num].iommu = iommu;
611 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
612 ir_ioapic_num++;
613 }
614 start += scope->length;
615 }
616
617 return 0;
618}
619
620/*
621 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
622 * hardware unit.
623 */
624int __init parse_ioapics_under_ir(void)
625{
626 struct dmar_drhd_unit *drhd;
627 int ir_supported = 0;
628
629 for_each_drhd_unit(drhd) {
630 struct intel_iommu *iommu = drhd->iommu;
631
632 if (ecap_ir_support(iommu->ecap)) {
633 if (ir_parse_ioapic_scope(drhd->hdr, iommu))
634 return -1;
635
636 ir_supported = 1;
637 }
638 }
639
640 if (ir_supported && ir_ioapic_num != nr_ioapics) {
641 printk(KERN_WARNING
642 "Not all IO-APIC's listed under remapping hardware\n");
643 return -1;
644 }
645
646 return ir_supported;
647}
Fenghua Yub24696b2009-03-27 14:22:44 -0700648
649void disable_intr_remapping(void)
650{
651 struct dmar_drhd_unit *drhd;
652 struct intel_iommu *iommu = NULL;
653
654 /*
655 * Disable Interrupt-remapping for all the DRHD's now.
656 */
657 for_each_iommu(iommu, drhd) {
658 if (!ecap_ir_support(iommu->ecap))
659 continue;
660
661 iommu_disable_intr_remapping(iommu);
662 }
663}
664
665int reenable_intr_remapping(int eim)
666{
667 struct dmar_drhd_unit *drhd;
668 int setup = 0;
669 struct intel_iommu *iommu = NULL;
670
671 for_each_iommu(iommu, drhd)
672 if (iommu->qi)
673 dmar_reenable_qi(iommu);
674
675 /*
676 * Setup Interrupt-remapping for all the DRHD's now.
677 */
678 for_each_iommu(iommu, drhd) {
679 if (!ecap_ir_support(iommu->ecap))
680 continue;
681
682 /* Set up interrupt remapping for iommu.*/
683 iommu_set_intr_remapping(iommu, eim);
684 setup = 1;
685 }
686
687 if (!setup)
688 goto error;
689
690 return 0;
691
692error:
693 /*
694 * handle error condition gracefully here!
695 */
696 return -1;
697}
698