blob: ea46935422fffd72afe399df4c04931ec6ed2c8f [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>
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070013#include "intr_remapping.h"
Alexander Beregalov46f06b722009-04-06 16:45:28 +010014#include <acpi/acpi.h>
Weidong Hanf007e992009-05-23 00:41:15 +080015#include <asm/pci-direct.h>
16#include "pci.h"
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -070017
18static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
Suresh Siddha20f30972009-08-04 12:07:08 -070019static struct hpet_scope ir_hpet[MAX_HPET_TBS];
20static int ir_ioapic_num, ir_hpet_num;
Suresh Siddha2ae21012008-07-10 11:16:43 -070021int intr_remapping_enabled;
22
Weidong Han03ea8152009-04-17 16:42:15 +080023static int disable_intremap;
Chris Wrightd1423d52010-07-20 11:06:49 -070024static int disable_sourceid_checking;
25
Weidong Han03ea8152009-04-17 16:42:15 +080026static __init int setup_nointremap(char *str)
27{
28 disable_intremap = 1;
29 return 0;
30}
31early_param("nointremap", setup_nointremap);
32
Chris Wrightd1423d52010-07-20 11:06:49 -070033static __init int setup_intremap(char *str)
34{
35 if (!str)
36 return -EINVAL;
37
38 if (!strncmp(str, "on", 2))
39 disable_intremap = 0;
40 else if (!strncmp(str, "off", 3))
41 disable_intremap = 1;
42 else if (!strncmp(str, "nosid", 5))
43 disable_sourceid_checking = 1;
44
45 return 0;
46}
47early_param("intremap", setup_intremap);
48
Yinghai Lue420dfb2008-08-19 20:50:21 -070049static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
50{
Thomas Gleixner349d6762010-10-10 12:29:27 +020051 struct irq_cfg *cfg = get_irq_chip_data(irq);
52 return cfg ? &cfg->irq_2_iommu : NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080053}
54
Yinghai Lu70590ea2009-08-26 16:21:54 -070055static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -080056{
Yinghai Lue420dfb2008-08-19 20:50:21 -070057 return irq_2_iommu(irq);
58}
Thomas Gleixner0e1e3672010-10-04 16:20:16 +020059
Thomas Gleixner349d6762010-10-10 12:29:27 +020060static void irq_2_iommu_free(unsigned int irq)
61{
62}
Suresh Siddhab6fcb332008-07-10 11:16:44 -070063
64static DEFINE_SPINLOCK(irq_2_ir_lock);
65
Yinghai Lue420dfb2008-08-19 20:50:21 -070066static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq)
67{
68 struct irq_2_iommu *irq_iommu;
69
70 irq_iommu = irq_2_iommu(irq);
71
72 if (!irq_iommu)
73 return NULL;
74
75 if (!irq_iommu->iommu)
76 return NULL;
77
78 return irq_iommu;
79}
80
Suresh Siddhab6fcb332008-07-10 11:16:44 -070081int irq_remapped(int irq)
82{
Yinghai Lue420dfb2008-08-19 20:50:21 -070083 return valid_irq_2_iommu(irq) != NULL;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070084}
85
86int get_irte(int irq, struct irte *entry)
87{
88 int index;
Yinghai Lue420dfb2008-08-19 20:50:21 -070089 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -070090 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -070091
Yinghai Lue420dfb2008-08-19 20:50:21 -070092 if (!entry)
Suresh Siddhab6fcb332008-07-10 11:16:44 -070093 return -1;
94
Suresh Siddha4c5502b2009-03-16 17:04:53 -070095 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -070096 irq_iommu = valid_irq_2_iommu(irq);
97 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -070098 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -070099 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
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700105 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700106 return 0;
107}
108
109int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
110{
111 struct ir_table *table = iommu->ir_table;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700112 struct irq_2_iommu *irq_iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700113 u16 index, start_index;
114 unsigned int mask = 0;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700115 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700116 int i;
117
118 if (!count)
119 return -1;
120
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800121#ifndef CONFIG_SPARSE_IRQ
Yinghai Lue420dfb2008-08-19 20:50:21 -0700122 /* protect irq_2_iommu_alloc later */
123 if (irq >= nr_irqs)
124 return -1;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800125#endif
Yinghai Lue420dfb2008-08-19 20:50:21 -0700126
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700127 /*
128 * start the IRTE search from index 0.
129 */
130 index = start_index = 0;
131
132 if (count > 1) {
133 count = __roundup_pow_of_two(count);
134 mask = ilog2(count);
135 }
136
137 if (mask > ecap_max_handle_mask(iommu->ecap)) {
138 printk(KERN_ERR
139 "Requested mask %x exceeds the max invalidation handle"
140 " mask value %Lx\n", mask,
141 ecap_max_handle_mask(iommu->ecap));
142 return -1;
143 }
144
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700145 spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700146 do {
147 for (i = index; i < index + count; i++)
148 if (table->base[i].present)
149 break;
150 /* empty index found */
151 if (i == index + count)
152 break;
153
154 index = (index + count) % INTR_REMAP_TABLE_ENTRIES;
155
156 if (index == start_index) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700157 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700158 printk(KERN_ERR "can't allocate an IRTE\n");
159 return -1;
160 }
161 } while (1);
162
163 for (i = index; i < index + count; i++)
164 table->base[i].present = 1;
165
Yinghai Lue420dfb2008-08-19 20:50:21 -0700166 irq_iommu = irq_2_iommu_alloc(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800167 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700168 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800169 printk(KERN_ERR "can't allocate irq_2_iommu\n");
170 return -1;
171 }
172
Yinghai Lue420dfb2008-08-19 20:50:21 -0700173 irq_iommu->iommu = iommu;
174 irq_iommu->irte_index = index;
175 irq_iommu->sub_handle = 0;
176 irq_iommu->irte_mask = mask;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700177
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700178 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700179
180 return index;
181}
182
Yu Zhao704126a2009-01-04 16:28:52 +0800183static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700184{
185 struct qi_desc desc;
186
187 desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
188 | QI_IEC_SELECTIVE;
189 desc.high = 0;
190
Yu Zhao704126a2009-01-04 16:28:52 +0800191 return qi_submit_sync(&desc, iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700192}
193
194int map_irq_to_irte_handle(int irq, u16 *sub_handle)
195{
196 int index;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700197 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700198 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700199
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700200 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700201 irq_iommu = valid_irq_2_iommu(irq);
202 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700203 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700204 return -1;
205 }
206
Yinghai Lue420dfb2008-08-19 20:50:21 -0700207 *sub_handle = irq_iommu->sub_handle;
208 index = irq_iommu->irte_index;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700209 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700210 return index;
211}
212
213int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
214{
Yinghai Lue420dfb2008-08-19 20:50:21 -0700215 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700216 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700217
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700218 spin_lock_irqsave(&irq_2_ir_lock, flags);
Suresh Siddha7ddfb652008-08-20 17:22:51 -0700219
220 irq_iommu = irq_2_iommu_alloc(irq);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700221
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800222 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700223 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800224 printk(KERN_ERR "can't allocate irq_2_iommu\n");
225 return -1;
226 }
227
Yinghai Lue420dfb2008-08-19 20:50:21 -0700228 irq_iommu->iommu = iommu;
229 irq_iommu->irte_index = index;
230 irq_iommu->sub_handle = subhandle;
231 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700232
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700233 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700234
235 return 0;
236}
237
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700238int modify_irte(int irq, struct irte *irte_modified)
239{
Yu Zhao704126a2009-01-04 16:28:52 +0800240 int rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700241 int index;
242 struct irte *irte;
243 struct intel_iommu *iommu;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700244 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700245 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700246
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700247 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700248 irq_iommu = valid_irq_2_iommu(irq);
249 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700250 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700251 return -1;
252 }
253
Yinghai Lue420dfb2008-08-19 20:50:21 -0700254 iommu = irq_iommu->iommu;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700255
Yinghai Lue420dfb2008-08-19 20:50:21 -0700256 index = irq_iommu->irte_index + irq_iommu->sub_handle;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700257 irte = &iommu->ir_table->base[index];
258
Linus Torvaldsc513b672010-08-06 11:02:31 -0700259 set_64bit(&irte->low, irte_modified->low);
260 set_64bit(&irte->high, irte_modified->high);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700261 __iommu_flush_cache(iommu, irte, sizeof(*irte));
262
Yu Zhao704126a2009-01-04 16:28:52 +0800263 rc = qi_flush_iec(iommu, index, 0);
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700264 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Yu Zhao704126a2009-01-04 16:28:52 +0800265
266 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700267}
268
Suresh Siddha20f30972009-08-04 12:07:08 -0700269struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
270{
271 int i;
272
273 for (i = 0; i < MAX_HPET_TBS; i++)
274 if (ir_hpet[i].id == hpet_id)
275 return ir_hpet[i].iommu;
276 return NULL;
277}
278
Suresh Siddha89027d32008-07-10 11:16:56 -0700279struct intel_iommu *map_ioapic_to_ir(int apic)
280{
281 int i;
282
283 for (i = 0; i < MAX_IO_APICS; i++)
284 if (ir_ioapic[i].id == apic)
285 return ir_ioapic[i].iommu;
286 return NULL;
287}
288
Suresh Siddha75c46fa2008-07-10 11:16:57 -0700289struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
290{
291 struct dmar_drhd_unit *drhd;
292
293 drhd = dmar_find_matched_drhd_unit(dev);
294 if (!drhd)
295 return NULL;
296
297 return drhd->iommu;
298}
299
Weidong Hanc4658b42009-05-23 00:41:14 +0800300static int clear_entries(struct irq_2_iommu *irq_iommu)
301{
302 struct irte *start, *entry, *end;
303 struct intel_iommu *iommu;
304 int index;
305
306 if (irq_iommu->sub_handle)
307 return 0;
308
309 iommu = irq_iommu->iommu;
310 index = irq_iommu->irte_index + irq_iommu->sub_handle;
311
312 start = iommu->ir_table->base + index;
313 end = start + (1 << irq_iommu->irte_mask);
314
315 for (entry = start; entry < end; entry++) {
Linus Torvaldsc513b672010-08-06 11:02:31 -0700316 set_64bit(&entry->low, 0);
317 set_64bit(&entry->high, 0);
Weidong Hanc4658b42009-05-23 00:41:14 +0800318 }
319
320 return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
321}
322
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700323int free_irte(int irq)
324{
Yu Zhao704126a2009-01-04 16:28:52 +0800325 int rc = 0;
Yinghai Lue420dfb2008-08-19 20:50:21 -0700326 struct irq_2_iommu *irq_iommu;
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700327 unsigned long flags;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700328
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700329 spin_lock_irqsave(&irq_2_ir_lock, flags);
Yinghai Lue420dfb2008-08-19 20:50:21 -0700330 irq_iommu = valid_irq_2_iommu(irq);
331 if (!irq_iommu) {
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700332 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700333 return -1;
334 }
335
Weidong Hanc4658b42009-05-23 00:41:14 +0800336 rc = clear_entries(irq_iommu);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700337
Yinghai Lue420dfb2008-08-19 20:50:21 -0700338 irq_iommu->iommu = NULL;
339 irq_iommu->irte_index = 0;
340 irq_iommu->sub_handle = 0;
341 irq_iommu->irte_mask = 0;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700342
Suresh Siddha4c5502b2009-03-16 17:04:53 -0700343 spin_unlock_irqrestore(&irq_2_ir_lock, flags);
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700344
Thomas Gleixner0e1e3672010-10-04 16:20:16 +0200345 irq_2_iommu_free(irq);
346
Yu Zhao704126a2009-01-04 16:28:52 +0800347 return rc;
Suresh Siddhab6fcb332008-07-10 11:16:44 -0700348}
349
Weidong Hanf007e992009-05-23 00:41:15 +0800350/*
351 * source validation type
352 */
353#define SVT_NO_VERIFY 0x0 /* no verification is required */
354#define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fiels */
355#define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */
356
357/*
358 * source-id qualifier
359 */
360#define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */
361#define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore
362 * the third least significant bit
363 */
364#define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore
365 * the second and third least significant bits
366 */
367#define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore
368 * the least three significant bits
369 */
370
371/*
372 * set SVT, SQ and SID fields of irte to verify
373 * source ids of interrupt requests
374 */
375static void set_irte_sid(struct irte *irte, unsigned int svt,
376 unsigned int sq, unsigned int sid)
377{
Chris Wrightd1423d52010-07-20 11:06:49 -0700378 if (disable_sourceid_checking)
379 svt = SVT_NO_VERIFY;
Weidong Hanf007e992009-05-23 00:41:15 +0800380 irte->svt = svt;
381 irte->sq = sq;
382 irte->sid = sid;
383}
384
385int set_ioapic_sid(struct irte *irte, int apic)
386{
387 int i;
388 u16 sid = 0;
389
390 if (!irte)
391 return -1;
392
393 for (i = 0; i < MAX_IO_APICS; i++) {
394 if (ir_ioapic[i].id == apic) {
395 sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
396 break;
397 }
398 }
399
400 if (sid == 0) {
401 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
402 return -1;
403 }
404
405 set_irte_sid(irte, 1, 0, sid);
406
407 return 0;
408}
409
Suresh Siddha20f30972009-08-04 12:07:08 -0700410int set_hpet_sid(struct irte *irte, u8 id)
411{
412 int i;
413 u16 sid = 0;
414
415 if (!irte)
416 return -1;
417
418 for (i = 0; i < MAX_HPET_TBS; i++) {
419 if (ir_hpet[i].id == id) {
420 sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
421 break;
422 }
423 }
424
425 if (sid == 0) {
426 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
427 return -1;
428 }
429
430 /*
431 * Should really use SQ_ALL_16. Some platforms are broken.
432 * While we figure out the right quirks for these broken platforms, use
433 * SQ_13_IGNORE_3 for now.
434 */
435 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
436
437 return 0;
438}
439
Weidong Hanf007e992009-05-23 00:41:15 +0800440int set_msi_sid(struct irte *irte, struct pci_dev *dev)
441{
442 struct pci_dev *bridge;
443
444 if (!irte || !dev)
445 return -1;
446
447 /* PCIe device or Root Complex integrated PCI device */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900448 if (pci_is_pcie(dev) || !dev->bus->parent) {
Weidong Hanf007e992009-05-23 00:41:15 +0800449 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
450 (dev->bus->number << 8) | dev->devfn);
451 return 0;
452 }
453
454 bridge = pci_find_upstream_pcie_bridge(dev);
455 if (bridge) {
Stefan Assmann45e829e2009-12-03 06:49:24 -0500456 if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */
Weidong Hanf007e992009-05-23 00:41:15 +0800457 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
458 (bridge->bus->number << 8) | dev->bus->number);
459 else /* this is a legacy PCI bridge */
460 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
461 (bridge->bus->number << 8) | bridge->devfn);
462 }
463
464 return 0;
465}
466
Suresh Siddha2ae21012008-07-10 11:16:43 -0700467static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode)
468{
469 u64 addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100470 u32 sts;
Suresh Siddha2ae21012008-07-10 11:16:43 -0700471 unsigned long flags;
472
473 addr = virt_to_phys((void *)iommu->ir_table->base);
474
475 spin_lock_irqsave(&iommu->register_lock, flags);
476
477 dmar_writeq(iommu->reg + DMAR_IRTA_REG,
478 (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
479
480 /* Set interrupt-remapping table pointer */
Han, Weidong161fde02009-04-03 17:15:47 +0800481 iommu->gcmd |= DMA_GCMD_SIRTP;
David Woodhousec416daa2009-05-10 20:30:58 +0100482 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700483
484 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
485 readl, (sts & DMA_GSTS_IRTPS), sts);
486 spin_unlock_irqrestore(&iommu->register_lock, flags);
487
488 /*
489 * global invalidation of interrupt entry cache before enabling
490 * interrupt-remapping.
491 */
492 qi_global_iec(iommu);
493
494 spin_lock_irqsave(&iommu->register_lock, flags);
495
496 /* Enable interrupt-remapping */
Suresh Siddha2ae21012008-07-10 11:16:43 -0700497 iommu->gcmd |= DMA_GCMD_IRE;
David Woodhousec416daa2009-05-10 20:30:58 +0100498 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700499
500 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
501 readl, (sts & DMA_GSTS_IRES), sts);
502
503 spin_unlock_irqrestore(&iommu->register_lock, flags);
504}
505
506
507static int setup_intr_remapping(struct intel_iommu *iommu, int mode)
508{
509 struct ir_table *ir_table;
510 struct page *pages;
511
512 ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table),
Suresh Siddhafa4b57c2009-03-16 17:05:05 -0700513 GFP_ATOMIC);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700514
515 if (!iommu->ir_table)
516 return -ENOMEM;
517
Suresh Siddha824cd752009-10-02 11:01:23 -0700518 pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
519 INTR_REMAP_PAGE_ORDER);
Suresh Siddha2ae21012008-07-10 11:16:43 -0700520
521 if (!pages) {
522 printk(KERN_ERR "failed to allocate pages of order %d\n",
523 INTR_REMAP_PAGE_ORDER);
524 kfree(iommu->ir_table);
525 return -ENOMEM;
526 }
527
528 ir_table->base = page_address(pages);
529
530 iommu_set_intr_remapping(iommu, mode);
531 return 0;
532}
533
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700534/*
535 * Disable Interrupt Remapping.
536 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700537static void iommu_disable_intr_remapping(struct intel_iommu *iommu)
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700538{
539 unsigned long flags;
540 u32 sts;
541
542 if (!ecap_ir_support(iommu->ecap))
543 return;
544
Fenghua Yub24696b2009-03-27 14:22:44 -0700545 /*
546 * global invalidation of interrupt entry cache before disabling
547 * interrupt-remapping.
548 */
549 qi_global_iec(iommu);
550
Suresh Siddhaeba67e52009-03-16 17:04:56 -0700551 spin_lock_irqsave(&iommu->register_lock, flags);
552
553 sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
554 if (!(sts & DMA_GSTS_IRES))
555 goto end;
556
557 iommu->gcmd &= ~DMA_GCMD_IRE;
558 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
559
560 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
561 readl, !(sts & DMA_GSTS_IRES), sts);
562
563end:
564 spin_unlock_irqrestore(&iommu->register_lock, flags);
565}
566
Weidong Han93758232009-04-17 16:42:14 +0800567int __init intr_remapping_supported(void)
568{
569 struct dmar_drhd_unit *drhd;
570
Weidong Han03ea8152009-04-17 16:42:15 +0800571 if (disable_intremap)
572 return 0;
573
Youquan Song074835f2009-09-09 12:05:39 -0400574 if (!dmar_ir_support())
575 return 0;
576
Weidong Han93758232009-04-17 16:42:14 +0800577 for_each_drhd_unit(drhd) {
578 struct intel_iommu *iommu = drhd->iommu;
579
580 if (!ecap_ir_support(iommu->ecap))
581 return 0;
582 }
583
584 return 1;
585}
586
Suresh Siddha2ae21012008-07-10 11:16:43 -0700587int __init enable_intr_remapping(int eim)
588{
589 struct dmar_drhd_unit *drhd;
590 int setup = 0;
591
Youquan Songe936d072009-09-07 10:58:07 -0400592 if (parse_ioapics_under_ir() != 1) {
593 printk(KERN_INFO "Not enable interrupt remapping\n");
594 return -1;
595 }
596
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700597 for_each_drhd_unit(drhd) {
598 struct intel_iommu *iommu = drhd->iommu;
599
600 /*
Han, Weidong34aaaa92009-04-04 17:21:26 +0800601 * If the queued invalidation is already initialized,
602 * shouldn't disable it.
603 */
604 if (iommu->qi)
605 continue;
606
607 /*
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700608 * Clear previous faults.
609 */
610 dmar_fault(-1, iommu);
611
612 /*
613 * Disable intr remapping and queued invalidation, if already
614 * enabled prior to OS handover.
615 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700616 iommu_disable_intr_remapping(iommu);
Suresh Siddha1531a6a2009-03-16 17:04:57 -0700617
618 dmar_disable_qi(iommu);
619 }
620
Suresh Siddha2ae21012008-07-10 11:16:43 -0700621 /*
622 * check for the Interrupt-remapping support
623 */
624 for_each_drhd_unit(drhd) {
625 struct intel_iommu *iommu = drhd->iommu;
626
627 if (!ecap_ir_support(iommu->ecap))
628 continue;
629
630 if (eim && !ecap_eim_support(iommu->ecap)) {
631 printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
632 " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
633 return -1;
634 }
635 }
636
637 /*
638 * Enable queued invalidation for all the DRHD's.
639 */
640 for_each_drhd_unit(drhd) {
641 int ret;
642 struct intel_iommu *iommu = drhd->iommu;
643 ret = dmar_enable_qi(iommu);
644
645 if (ret) {
646 printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
647 " invalidation, ecap %Lx, ret %d\n",
648 drhd->reg_base_addr, iommu->ecap, ret);
649 return -1;
650 }
651 }
652
653 /*
654 * Setup Interrupt-remapping for all the DRHD's now.
655 */
656 for_each_drhd_unit(drhd) {
657 struct intel_iommu *iommu = drhd->iommu;
658
659 if (!ecap_ir_support(iommu->ecap))
660 continue;
661
662 if (setup_intr_remapping(iommu, eim))
663 goto error;
664
665 setup = 1;
666 }
667
668 if (!setup)
669 goto error;
670
671 intr_remapping_enabled = 1;
672
673 return 0;
674
675error:
676 /*
677 * handle error condition gracefully here!
678 */
679 return -1;
680}
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700681
Suresh Siddha20f30972009-08-04 12:07:08 -0700682static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
683 struct intel_iommu *iommu)
684{
685 struct acpi_dmar_pci_path *path;
686 u8 bus;
687 int count;
688
689 bus = scope->bus;
690 path = (struct acpi_dmar_pci_path *)(scope + 1);
691 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
692 / sizeof(struct acpi_dmar_pci_path);
693
694 while (--count > 0) {
695 /*
696 * Access PCI directly due to the PCI
697 * subsystem isn't initialized yet.
698 */
699 bus = read_pci_config_byte(bus, path->dev, path->fn,
700 PCI_SECONDARY_BUS);
701 path++;
702 }
703 ir_hpet[ir_hpet_num].bus = bus;
704 ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn);
705 ir_hpet[ir_hpet_num].iommu = iommu;
706 ir_hpet[ir_hpet_num].id = scope->enumeration_id;
707 ir_hpet_num++;
708}
709
Weidong Hanf007e992009-05-23 00:41:15 +0800710static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
711 struct intel_iommu *iommu)
712{
713 struct acpi_dmar_pci_path *path;
714 u8 bus;
715 int count;
716
717 bus = scope->bus;
718 path = (struct acpi_dmar_pci_path *)(scope + 1);
719 count = (scope->length - sizeof(struct acpi_dmar_device_scope))
720 / sizeof(struct acpi_dmar_pci_path);
721
722 while (--count > 0) {
723 /*
724 * Access PCI directly due to the PCI
725 * subsystem isn't initialized yet.
726 */
727 bus = read_pci_config_byte(bus, path->dev, path->fn,
728 PCI_SECONDARY_BUS);
729 path++;
730 }
731
732 ir_ioapic[ir_ioapic_num].bus = bus;
733 ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn);
734 ir_ioapic[ir_ioapic_num].iommu = iommu;
735 ir_ioapic[ir_ioapic_num].id = scope->enumeration_id;
736 ir_ioapic_num++;
737}
738
Suresh Siddha20f30972009-08-04 12:07:08 -0700739static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
740 struct intel_iommu *iommu)
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700741{
742 struct acpi_dmar_hardware_unit *drhd;
743 struct acpi_dmar_device_scope *scope;
744 void *start, *end;
745
746 drhd = (struct acpi_dmar_hardware_unit *)header;
747
748 start = (void *)(drhd + 1);
749 end = ((void *)drhd) + header->length;
750
751 while (start < end) {
752 scope = start;
753 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
754 if (ir_ioapic_num == MAX_IO_APICS) {
755 printk(KERN_WARNING "Exceeded Max IO APICS\n");
756 return -1;
757 }
758
Yinghai Lu680a7522010-04-08 19:58:23 +0100759 printk(KERN_INFO "IOAPIC id %d under DRHD base "
760 " 0x%Lx IOMMU %d\n", scope->enumeration_id,
761 drhd->address, iommu->seq_id);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700762
Weidong Hanf007e992009-05-23 00:41:15 +0800763 ir_parse_one_ioapic_scope(scope, iommu);
Suresh Siddha20f30972009-08-04 12:07:08 -0700764 } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) {
765 if (ir_hpet_num == MAX_HPET_TBS) {
766 printk(KERN_WARNING "Exceeded Max HPET blocks\n");
767 return -1;
768 }
769
770 printk(KERN_INFO "HPET id %d under DRHD base"
771 " 0x%Lx\n", scope->enumeration_id,
772 drhd->address);
773
774 ir_parse_one_hpet_scope(scope, iommu);
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700775 }
776 start += scope->length;
777 }
778
779 return 0;
780}
781
782/*
783 * Finds the assocaition between IOAPIC's and its Interrupt-remapping
784 * hardware unit.
785 */
786int __init parse_ioapics_under_ir(void)
787{
788 struct dmar_drhd_unit *drhd;
789 int ir_supported = 0;
790
791 for_each_drhd_unit(drhd) {
792 struct intel_iommu *iommu = drhd->iommu;
793
794 if (ecap_ir_support(iommu->ecap)) {
Suresh Siddha20f30972009-08-04 12:07:08 -0700795 if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
Suresh Siddhaad3ad3f2008-07-10 11:16:40 -0700796 return -1;
797
798 ir_supported = 1;
799 }
800 }
801
802 if (ir_supported && ir_ioapic_num != nr_ioapics) {
803 printk(KERN_WARNING
804 "Not all IO-APIC's listed under remapping hardware\n");
805 return -1;
806 }
807
808 return ir_supported;
809}
Fenghua Yub24696b2009-03-27 14:22:44 -0700810
811void disable_intr_remapping(void)
812{
813 struct dmar_drhd_unit *drhd;
814 struct intel_iommu *iommu = NULL;
815
816 /*
817 * Disable Interrupt-remapping for all the DRHD's now.
818 */
819 for_each_iommu(iommu, drhd) {
820 if (!ecap_ir_support(iommu->ecap))
821 continue;
822
823 iommu_disable_intr_remapping(iommu);
824 }
825}
826
827int reenable_intr_remapping(int eim)
828{
829 struct dmar_drhd_unit *drhd;
830 int setup = 0;
831 struct intel_iommu *iommu = NULL;
832
833 for_each_iommu(iommu, drhd)
834 if (iommu->qi)
835 dmar_reenable_qi(iommu);
836
837 /*
838 * Setup Interrupt-remapping for all the DRHD's now.
839 */
840 for_each_iommu(iommu, drhd) {
841 if (!ecap_ir_support(iommu->ecap))
842 continue;
843
844 /* Set up interrupt remapping for iommu.*/
845 iommu_set_intr_remapping(iommu, eim);
846 setup = 1;
847 }
848
849 if (!setup)
850 goto error;
851
852 return 0;
853
854error:
855 /*
856 * handle error condition gracefully here!
857 */
858 return -1;
859}
860