blob: 44025a0c2bb60cc5eb69e1187887649c6aaf2dfd [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"
Alexander Beregalov46f06b722009-04-06 16:45:28 +010012#include <acpi/acpi.h>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070013
14static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
15static int ir_ioapic_num;
Suresh Siddha2ae21012008-07-10 11:16:43 -070016int intr_remapping_enabled;
17
Weidong Han03ea8152009-04-17 16:42:15 +080018static int disable_intremap;
19static __init int setup_nointremap(char *str)
20{
21 disable_intremap = 1;
22 return 0;
23}
24early_param("nointremap", setup_nointremap);
25
Yinghai Lu5aeecaf2008-08-19 20:49:59 -070026struct irq_2_iommu {
Suresh Siddhab6fcb332008-07-10 11:16:44 -070027 struct intel_iommu *iommu;
28 u16 irte_index;
29 u16 sub_handle;
30 u8 irte_mask;
Yinghai Lu5aeecaf2008-08-19 20:49:59 -070031};
32
Yinghai Lud7e51e62009-01-07 15:03:13 -080033#ifdef CONFIG_GENERIC_HARDIRQS
Yinghai Lu85ac16d2009-04-27 18:00:38 -070034static struct irq_2_iommu *get_one_free_irq_2_iommu(int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080035{
36 struct irq_2_iommu *iommu;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080037
38 iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node);
Yinghai Lu85ac16d2009-04-27 18:00:38 -070039 printk(KERN_DEBUG "alloc irq_2_iommu on node %d\n", node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080040
41 return iommu;
42}
Yinghai Lue420dfb2008-08-19 20:50:21 -070043
44static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
45{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080046 struct irq_desc *desc;
47
48 desc = irq_to_desc(irq);
49
50 if (WARN_ON_ONCE(!desc))
51 return NULL;
52
53 return desc->irq_2_iommu;
54}
55
Yinghai Lu85ac16d2009-04-27 18:00:38 -070056static struct irq_2_iommu *irq_2_iommu_alloc_node(unsigned int irq, int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080057{
58 struct irq_desc *desc;
59 struct irq_2_iommu *irq_iommu;
60
61 /*
62 * alloc irq desc if not allocated already.
63 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -070064 desc = irq_to_desc_alloc_node(irq, node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080065 if (!desc) {
66 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
67 return NULL;
68 }
69
70 irq_iommu = desc->irq_2_iommu;
71
72 if (!irq_iommu)
Yinghai Lu85ac16d2009-04-27 18:00:38 -070073 desc->irq_2_iommu = get_one_free_irq_2_iommu(node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080074
75 return desc->irq_2_iommu;
Yinghai Lue420dfb2008-08-19 20:50:21 -070076}
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +020077
Yinghai Lue420dfb2008-08-19 20:50:21 -070078static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
79{
Yinghai Lu85ac16d2009-04-27 18:00:38 -070080 return irq_2_iommu_alloc_node(irq, cpu_to_node(boot_cpu_id));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080081}
82
83#else /* !CONFIG_SPARSE_IRQ */
84
85static struct irq_2_iommu irq_2_iommuX[NR_IRQS];
86
87static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
88{
89 if (irq < nr_irqs)
90 return &irq_2_iommuX[irq];
91
92 return NULL;
93}
94static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
95{
Yinghai Lue420dfb2008-08-19 20:50:21 -070096 return irq_2_iommu(irq);
97}
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080098#endif
Suresh Siddhab6fcb332008-07-10 11:16:44 -070099
100static DEFINE_SPINLOCK(irq_2_ir_lock);
101
Yinghai Lue420dfb2008-08-19 20:50:21 -0700102static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
103{
104 struct irq_2_iommu *irq_iommu;
105
106 irq_iommu = irq_2_iommu(irq);
107
108 if (!irq_iommu)
109 return NULL;
110
111 if (!irq_iommu->iommu)
112 return NULL;
113
114 return irq_iommu;
115}
116
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700117int irq_remapped(int irq)
118{
Yinghai Lue420dfb2008-08-19 20:50:21 -0700119 return valid_irq_2_iommu(irq) != NULL;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700120}
121
122int get_irte(int irq, struct irte *entry)
123{
124 int index;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700125 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700126 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700127
Yinghai Lue420dfb2008-08-19 20:50:21 -0700128 if (!entry)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700129 return -1;
130
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700131 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700132 irq_iommu = valid_irq_2_iommu(irq);
133 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700134 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700135 return -1;
136 }
137
Yinghai Lue420dfb2008-08-19 20:50:21 -0700138 index = irq_iommu->irte_index + irq_iommu->sub_handle;
139 *entry = *(irq_iommu->iommu->ir_table->base + index);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700140
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700141 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700142 return 0;
143}
144
145int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
146{
147 struct ir_table *table = iommu->ir_table;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700148 struct irq_2_iommu *irq_iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700149 u16 index, start_index;
150 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700151 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700152 int i;
153
154 if (!count)
155 return -1;
156
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800157#ifndef CONFIG_SPARSE_IRQ
Yinghai Lue420dfb2008-08-19 20:50:21 -0700158 /* protect irq_2_iommu_alloc later */
159 if (irq >= nr_irqs)
160 return -1;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800161#endif
Yinghai Lue420dfb2008-08-19 20:50:21 -0700162
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700163 /*
164 * start the IRTE search from index 0.
165 */
166 index = start_index = 0;
167
168 if (count > 1) {
169 count = __roundup_pow_of_two(count);
170 mask = ilog2(count);
171 }
172
173 if (mask > ecap_max_handle_mask(iommu->ecap)) {
174 printk(KERN_ERR
175 "Requested mask %x exceeds the max invalidation handle"
176 " mask value %Lx\n", mask,
177 ecap_max_handle_mask(iommu->ecap));
178 return -1;
179 }
180
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700181 spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700182 do {
183 for (i = index; i < index + count; i++)
184 if (table->base[i].present)
185 break;
186 /* empty index found */
187 if (i == index + count)
188 break;
189
190 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
191
192 if (index == start_index) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700193 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700194 printk(KERN_ERR "can't allocate an IRTE\n");
195 return -1;
196 }
197 } while (1);
198
199 for (i = index; i < index + count; i++)
200 table->base[i].present = 1;
201
Yinghai Lue420dfb2008-08-19 20:50:21 -0700202 irq_iommu = irq_2_iommu_alloc(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800203 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700204 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800205 printk(KERN_ERR "can't allocate irq_2_iommu\n");
206 return -1;
207 }
208
Yinghai Lue420dfb2008-08-19 20:50:21 -0700209 irq_iommu->iommu = iommu;
210 irq_iommu->irte_index = index;
211 irq_iommu->sub_handle = 0;
212 irq_iommu->irte_mask = mask;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700213
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700214 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700215
216 return index;
217}
218
Yu Zhao704126a2009-01-04 16:28:52 +0800219static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700220{
221 struct qi_desc desc;
222
223 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
224 | QI_IEC_SELECTIVE;
225 desc.high = 0;
226
Yu Zhao704126a2009-01-04 16:28:52 +0800227 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700228}
229
230int map_irq_to_irte_handle(int irq, u16 *sub_handle)
231{
232 int index;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700233 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700234 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700235
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700236 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700237 irq_iommu = valid_irq_2_iommu(irq);
238 if (!irq_iommu) {
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 -1;
241 }
242
Yinghai Lue420dfb2008-08-19 20:50:21 -0700243 *sub_handle = irq_iommu->sub_handle;
244 index = irq_iommu->irte_index;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700245 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700246 return index;
247}
248
249int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
250{
Yinghai Lue420dfb2008-08-19 20:50:21 -0700251 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700252 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700253
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700254 spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddha7ddfb652008-08-20 17:22:51 -0700255
256 irq_iommu = irq_2_iommu_alloc(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700257
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800258 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700259 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800260 printk(KERN_ERR "can't allocate irq_2_iommu\n");
261 return -1;
262 }
263
Yinghai Lue420dfb2008-08-19 20:50:21 -0700264 irq_iommu->iommu = iommu;
265 irq_iommu->irte_index = index;
266 irq_iommu->sub_handle = subhandle;
267 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700268
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700269 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700270
271 return 0;
272}
273
274int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index)
275{
Yinghai Lue420dfb2008-08-19 20:50:21 -0700276 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700277 unsigned long flags;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700278
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700279 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700280 irq_iommu = valid_irq_2_iommu(irq);
281 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700282 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700283 return -1;
284 }
285
Yinghai Lue420dfb2008-08-19 20:50:21 -0700286 irq_iommu->iommu = NULL;
287 irq_iommu->irte_index = 0;
288 irq_iommu->sub_handle = 0;
289 irq_2_iommu(irq)->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700290
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700291 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700292
293 return 0;
294}
295
296int modify_irte(int irq, struct irte *irte_modified)
297{
Yu Zhao704126a2009-01-04 16:28:52 +0800298 int rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700299 int index;
300 struct irte *irte;
301 struct intel_iommu *iommu;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700302 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700303 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700304
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700305 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700306 irq_iommu = valid_irq_2_iommu(irq);
307 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700308 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700309 return -1;
310 }
311
Yinghai Lue420dfb2008-08-19 20:50:21 -0700312 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700313
Yinghai Lue420dfb2008-08-19 20:50:21 -0700314 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700315 irte = &iommu->ir_table->base[index];
316
Weidong Hanc4658b42009-05-23 00:41:14 +0800317 set_64bit((unsigned long *)&irte->low, irte_modified->low);
318 set_64bit((unsigned long *)&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700319 __iommu_flush_cache(iommu, irte, sizeof(*irte));
320
Yu Zhao704126a2009-01-04 16:28:52 +0800321 rc = qi_flush_iec(iommu, index, 0);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700322 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800323
324 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700325}
326
327int flush_irte(int irq)
328{
Yu Zhao704126a2009-01-04 16:28:52 +0800329 int rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700330 int index;
331 struct intel_iommu *iommu;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700332 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700333 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700334
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700335 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700336 irq_iommu = valid_irq_2_iommu(irq);
337 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700338 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700339 return -1;
340 }
341
Yinghai Lue420dfb2008-08-19 20:50:21 -0700342 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700343
Yinghai Lue420dfb2008-08-19 20:50:21 -0700344 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700345
Yu Zhao704126a2009-01-04 16:28:52 +0800346 rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700347 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700348
Yu Zhao704126a2009-01-04 16:28:52 +0800349 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700350}
351
Suresh Siddha89027d32008-07-10 11:16:56 -0700352struct intel_iommu *map_ioapic_to_ir(int apic)
353{
354 int i;
355
356 for (i = 0; i < MAX_IO_APICS; i++)
357 if (ir_ioapic[i].id == apic)
358 return ir_ioapic[i].iommu;
359 return NULL;
360}
361
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700362struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
363{
364 struct dmar_drhd_unit *drhd;
365
366 drhd = dmar_find_matched_drhd_unit(dev);
367 if (!drhd)
368 return NULL;
369
370 return drhd->iommu;
371}
372
Weidong Hanc4658b42009-05-23 00:41:14 +0800373static int clear_entries(struct irq_2_iommu *irq_iommu)
374{
375 struct irte *start, *entry, *end;
376 struct intel_iommu *iommu;
377 int index;
378
379 if (irq_iommu->sub_handle)
380 return 0;
381
382 iommu = irq_iommu->iommu;
383 index = irq_iommu->irte_index + irq_iommu->sub_handle;
384
385 start = iommu->ir_table->base + index;
386 end = start + (1 << irq_iommu->irte_mask);
387
388 for (entry = start; entry < end; entry++) {
389 set_64bit((unsigned long *)&entry->low, 0);
390 set_64bit((unsigned long *)&entry->high, 0);
391 }
392
393 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
394}
395
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700396int free_irte(int irq)
397{
Yu Zhao704126a2009-01-04 16:28:52 +0800398 int rc = 0;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700399 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700400 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700401
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700402 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700403 irq_iommu = valid_irq_2_iommu(irq);
404 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700405 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700406 return -1;
407 }
408
Weidong Hanc4658b42009-05-23 00:41:14 +0800409 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700410
Yinghai Lue420dfb2008-08-19 20:50:21 -0700411 irq_iommu->iommu = NULL;
412 irq_iommu->irte_index = 0;
413 irq_iommu->sub_handle = 0;
414 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700415
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700416 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700417
Yu Zhao704126a2009-01-04 16:28:52 +0800418 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700419}
420
Suresh Siddha2ae21012008-07-10 11:16:43 -0700421static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
422{
423 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100424 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700425 unsigned long flags;
426
427 addr = virt_to_phys((void *)iommu->ir_table->base);
428
429 spin_lock_irqsave(&iommu->register_lock, flags);
430
431 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
432 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
433
434 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800435 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100436 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700437
438 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
439 readl, (sts & DMA_GSTS_IRTPS), sts);
440 spin_unlock_irqrestore(&iommu->register_lock, flags);
441
442 /*
443 * global invalidation of interrupt entry cache before enabling
444 * interrupt-remapping.
445 */
446 qi_global_iec(iommu);
447
448 spin_lock_irqsave(&iommu->register_lock, flags);
449
450 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700451 iommu->gcmd |= DMA_GCMD_IRE;
David Woodhousec416daa2009-05-10 20:30:58 +0100452 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700453
454 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
455 readl, (sts & DMA_GSTS_IRES), sts);
456
457 spin_unlock_irqrestore(&iommu->register_lock, flags);
458}
459
460
461static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
462{
463 struct ir_table *ir_table;
464 struct page *pages;
465
466 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700467 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700468
469 if (!iommu->ir_table)
470 return -ENOMEM;
471
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700472 pages = alloc_pages(GFP_ATOMIC | __GFP_ZERO, INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700473
474 if (!pages) {
475 printk(KERN_ERR "failed to allocate pages of order %d\n",
476 INTR_REMAP_PAGE_ORDER);
477 kfree(iommu->ir_table);
478 return -ENOMEM;
479 }
480
481 ir_table->base = page_address(pages);
482
483 iommu_set_intr_remapping(iommu, mode);
484 return 0;
485}
486
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700487/*
488 * Disable Interrupt Remapping.
489 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700490static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700491{
492 unsigned long flags;
493 u32 sts;
494
495 if (!ecap_ir_support(iommu->ecap))
496 return;
497
Fenghua Yub24696b2009-03-27 14:22:44 -0700498 /*
499 * global invalidation of interrupt entry cache before disabling
500 * interrupt-remapping.
501 */
502 qi_global_iec(iommu);
503
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700504 spin_lock_irqsave(&iommu->register_lock, flags);
505
506 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
507 if (!(sts & DMA_GSTS_IRES))
508 goto end;
509
510 iommu->gcmd &= ~DMA_GCMD_IRE;
511 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
512
513 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
514 readl, !(sts & DMA_GSTS_IRES), sts);
515
516end:
517 spin_unlock_irqrestore(&iommu->register_lock, flags);
518}
519
Weidong Han93758232009-04-17 16:42:14 +0800520int __init intr_remapping_supported(void)
521{
522 struct dmar_drhd_unit *drhd;
523
Weidong Han03ea8152009-04-17 16:42:15 +0800524 if (disable_intremap)
525 return 0;
526
Weidong Han93758232009-04-17 16:42:14 +0800527 for_each_drhd_unit(drhd) {
528 struct intel_iommu *iommu = drhd->iommu;
529
530 if (!ecap_ir_support(iommu->ecap))
531 return 0;
532 }
533
534 return 1;
535}
536
Suresh Siddha2ae21012008-07-10 11:16:43 -0700537int __init enable_intr_remapping(int eim)
538{
539 struct dmar_drhd_unit *drhd;
540 int setup = 0;
541
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700542 for_each_drhd_unit(drhd) {
543 struct intel_iommu *iommu = drhd->iommu;
544
545 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800546 * If the queued invalidation is already initialized,
547 * shouldn't disable it.
548 */
549 if (iommu->qi)
550 continue;
551
552 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700553 * Clear previous faults.
554 */
555 dmar_fault(-1, iommu);
556
557 /*
558 * Disable intr remapping and queued invalidation, if already
559 * enabled prior to OS handover.
560 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700561 iommu_disable_intr_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700562
563 dmar_disable_qi(iommu);
564 }
565
Suresh Siddha2ae21012008-07-10 11:16:43 -0700566 /*
567 * check for the Interrupt-remapping support
568 */
569 for_each_drhd_unit(drhd) {
570 struct intel_iommu *iommu = drhd->iommu;
571
572 if (!ecap_ir_support(iommu->ecap))
573 continue;
574
575 if (eim && !ecap_eim_support(iommu->ecap)) {
576 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
577 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
578 return -1;
579 }
580 }
581
582 /*
583 * Enable queued invalidation for all the DRHD's.
584 */
585 for_each_drhd_unit(drhd) {
586 int ret;
587 struct intel_iommu *iommu = drhd->iommu;
588 ret = dmar_enable_qi(iommu);
589
590 if (ret) {
591 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
592 " invalidation, ecap %Lx, ret %d\n",
593 drhd->reg_base_addr, iommu->ecap, ret);
594 return -1;
595 }
596 }
597
598 /*
599 * Setup Interrupt-remapping for all the DRHD's now.
600 */
601 for_each_drhd_unit(drhd) {
602 struct intel_iommu *iommu = drhd->iommu;
603
604 if (!ecap_ir_support(iommu->ecap))
605 continue;
606
607 if (setup_intr_remapping(iommu, eim))
608 goto error;
609
610 setup = 1;
611 }
612
613 if (!setup)
614 goto error;
615
616 intr_remapping_enabled = 1;
617
618 return 0;
619
620error:
621 /*
622 * handle error condition gracefully here!
623 */
624 return -1;
625}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700626
627static int ir_parse_ioapic_scope(struct acpi_dmar_header *header,
628 struct intel_iommu *iommu)
629{
630 struct acpi_dmar_hardware_unit *drhd;
631 struct acpi_dmar_device_scope *scope;
632 void *start, *end;
633
634 drhd = (struct acpi_dmar_hardware_unit *)header;
635
636 start = (void *)(drhd + 1);
637 end = ((void *)drhd) + header->length;
638
639 while (start < end) {
640 scope = start;
641 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
642 if (ir_ioapic_num == MAX_IO_APICS) {
643 printk(KERN_WARNING "Exceeded Max IO APICS\n");
644 return -1;
645 }
646
647 printk(KERN_INFO "IOAPIC id %d under DRHD base"
648 " 0x%Lx\n", scope->enumeration_id,
649 drhd->address);
650
651 ir_ioapic[ir_ioapic_num].iommu = iommu;
652 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
653 ir_ioapic_num++;
654 }
655 start += scope->length;
656 }
657
658 return 0;
659}
660
661/*
662 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
663 * hardware unit.
664 */
665int __init parse_ioapics_under_ir(void)
666{
667 struct dmar_drhd_unit *drhd;
668 int ir_supported = 0;
669
670 for_each_drhd_unit(drhd) {
671 struct intel_iommu *iommu = drhd->iommu;
672
673 if (ecap_ir_support(iommu->ecap)) {
674 if (ir_parse_ioapic_scope(drhd->hdr, iommu))
675 return -1;
676
677 ir_supported = 1;
678 }
679 }
680
681 if (ir_supported && ir_ioapic_num != nr_ioapics) {
682 printk(KERN_WARNING
683 "Not all IO-APIC's listed under remapping hardware\n");
684 return -1;
685 }
686
687 return ir_supported;
688}
Fenghua Yub24696b2009-03-27 14:22:44 -0700689
690void disable_intr_remapping(void)
691{
692 struct dmar_drhd_unit *drhd;
693 struct intel_iommu *iommu = NULL;
694
695 /*
696 * Disable Interrupt-remapping for all the DRHD's now.
697 */
698 for_each_iommu(iommu, drhd) {
699 if (!ecap_ir_support(iommu->ecap))
700 continue;
701
702 iommu_disable_intr_remapping(iommu);
703 }
704}
705
706int reenable_intr_remapping(int eim)
707{
708 struct dmar_drhd_unit *drhd;
709 int setup = 0;
710 struct intel_iommu *iommu = NULL;
711
712 for_each_iommu(iommu, drhd)
713 if (iommu->qi)
714 dmar_reenable_qi(iommu);
715
716 /*
717 * Setup Interrupt-remapping for all the DRHD's now.
718 */
719 for_each_iommu(iommu, drhd) {
720 if (!ecap_ir_support(iommu->ecap))
721 continue;
722
723 /* Set up interrupt remapping for iommu.*/
724 iommu_set_intr_remapping(iommu, eim);
725 setup = 1;
726 }
727
728 if (!setup)
729 goto error;
730
731 return 0;
732
733error:
734 /*
735 * handle error condition gracefully here!
736 */
737 return -1;
738}
739