Yinghai Lu | 5aeecaf | 2008-08-19 20:49:59 -0700 | [diff] [blame] | 1 | #include <linux/interrupt.h> |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 2 | #include <linux/dmar.h> |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 3 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 4 | #include <linux/slab.h> |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 5 | #include <linux/jiffies.h> |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 6 | #include <linux/hpet.h> |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 7 | #include <linux/pci.h> |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 8 | #include <linux/irq.h> |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 9 | #include <asm/io_apic.h> |
Yinghai Lu | 17483a1 | 2008-12-12 13:14:18 -0800 | [diff] [blame] | 10 | #include <asm/smp.h> |
Jaswinder Singh Rajput | 6d652ea | 2009-01-07 21:38:59 +0530 | [diff] [blame] | 11 | #include <asm/cpu.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 12 | #include <linux/intel-iommu.h> |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 13 | #include "intr_remapping.h" |
Alexander Beregalov | 46f06b72 | 2009-04-06 16:45:28 +0100 | [diff] [blame] | 14 | #include <acpi/acpi.h> |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 15 | #include <asm/pci-direct.h> |
| 16 | #include "pci.h" |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 17 | |
| 18 | static struct ioapic_scope ir_ioapic[MAX_IO_APICS]; |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 19 | static struct hpet_scope ir_hpet[MAX_HPET_TBS]; |
| 20 | static int ir_ioapic_num, ir_hpet_num; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 21 | int intr_remapping_enabled; |
| 22 | |
Weidong Han | 03ea815 | 2009-04-17 16:42:15 +0800 | [diff] [blame] | 23 | static int disable_intremap; |
| 24 | static __init int setup_nointremap(char *str) |
| 25 | { |
| 26 | disable_intremap = 1; |
| 27 | return 0; |
| 28 | } |
| 29 | early_param("nointremap", setup_nointremap); |
| 30 | |
Yinghai Lu | 5aeecaf | 2008-08-19 20:49:59 -0700 | [diff] [blame] | 31 | struct irq_2_iommu { |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 32 | struct intel_iommu *iommu; |
| 33 | u16 irte_index; |
| 34 | u16 sub_handle; |
| 35 | u8 irte_mask; |
Yinghai Lu | 5aeecaf | 2008-08-19 20:49:59 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Yinghai Lu | d7e51e6 | 2009-01-07 15:03:13 -0800 | [diff] [blame] | 38 | #ifdef CONFIG_GENERIC_HARDIRQS |
Yinghai Lu | 85ac16d | 2009-04-27 18:00:38 -0700 | [diff] [blame] | 39 | static struct irq_2_iommu *get_one_free_irq_2_iommu(int node) |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 40 | { |
| 41 | struct irq_2_iommu *iommu; |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 42 | |
| 43 | iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node); |
Yinghai Lu | 85ac16d | 2009-04-27 18:00:38 -0700 | [diff] [blame] | 44 | printk(KERN_DEBUG "alloc irq_2_iommu on node %d\n", node); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 45 | |
| 46 | return iommu; |
| 47 | } |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 48 | |
| 49 | static struct irq_2_iommu *irq_2_iommu(unsigned int irq) |
| 50 | { |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 51 | struct irq_desc *desc; |
| 52 | |
| 53 | desc = irq_to_desc(irq); |
| 54 | |
| 55 | if (WARN_ON_ONCE(!desc)) |
| 56 | return NULL; |
| 57 | |
| 58 | return desc->irq_2_iommu; |
| 59 | } |
| 60 | |
Yinghai Lu | 70590ea | 2009-08-26 16:21:54 -0700 | [diff] [blame] | 61 | static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq) |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 62 | { |
| 63 | struct irq_desc *desc; |
| 64 | struct irq_2_iommu *irq_iommu; |
| 65 | |
Yinghai Lu | 70590ea | 2009-08-26 16:21:54 -0700 | [diff] [blame] | 66 | desc = irq_to_desc(irq); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 67 | if (!desc) { |
| 68 | printk(KERN_INFO "can not get irq_desc for %d\n", irq); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | irq_iommu = desc->irq_2_iommu; |
| 73 | |
| 74 | if (!irq_iommu) |
Yinghai Lu | 70590ea | 2009-08-26 16:21:54 -0700 | [diff] [blame] | 75 | desc->irq_2_iommu = get_one_free_irq_2_iommu(irq_node(irq)); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 76 | |
| 77 | return desc->irq_2_iommu; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 78 | } |
Thomas Gleixner | d6c88a5 | 2008-10-15 15:27:23 +0200 | [diff] [blame] | 79 | |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 80 | #else /* !CONFIG_SPARSE_IRQ */ |
| 81 | |
| 82 | static struct irq_2_iommu irq_2_iommuX[NR_IRQS]; |
| 83 | |
| 84 | static struct irq_2_iommu *irq_2_iommu(unsigned int irq) |
| 85 | { |
| 86 | if (irq < nr_irqs) |
| 87 | return &irq_2_iommuX[irq]; |
| 88 | |
| 89 | return NULL; |
| 90 | } |
| 91 | static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq) |
| 92 | { |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 93 | return irq_2_iommu(irq); |
| 94 | } |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 95 | #endif |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 96 | |
| 97 | static DEFINE_SPINLOCK(irq_2_ir_lock); |
| 98 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 99 | static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq) |
| 100 | { |
| 101 | struct irq_2_iommu *irq_iommu; |
| 102 | |
| 103 | irq_iommu = irq_2_iommu(irq); |
| 104 | |
| 105 | if (!irq_iommu) |
| 106 | return NULL; |
| 107 | |
| 108 | if (!irq_iommu->iommu) |
| 109 | return NULL; |
| 110 | |
| 111 | return irq_iommu; |
| 112 | } |
| 113 | |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 114 | int irq_remapped(int irq) |
| 115 | { |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 116 | return valid_irq_2_iommu(irq) != NULL; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int get_irte(int irq, struct irte *entry) |
| 120 | { |
| 121 | int index; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 122 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 123 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 124 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 125 | if (!entry) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 126 | return -1; |
| 127 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 128 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 129 | irq_iommu = valid_irq_2_iommu(irq); |
| 130 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 131 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 132 | return -1; |
| 133 | } |
| 134 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 135 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
| 136 | *entry = *(irq_iommu->iommu->ir_table->base + index); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 137 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 138 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int alloc_irte(struct intel_iommu *iommu, int irq, u16 count) |
| 143 | { |
| 144 | struct ir_table *table = iommu->ir_table; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 145 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 146 | u16 index, start_index; |
| 147 | unsigned int mask = 0; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 148 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 149 | int i; |
| 150 | |
| 151 | if (!count) |
| 152 | return -1; |
| 153 | |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 154 | #ifndef CONFIG_SPARSE_IRQ |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 155 | /* protect irq_2_iommu_alloc later */ |
| 156 | if (irq >= nr_irqs) |
| 157 | return -1; |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 158 | #endif |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 159 | |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 160 | /* |
| 161 | * start the IRTE search from index 0. |
| 162 | */ |
| 163 | index = start_index = 0; |
| 164 | |
| 165 | if (count > 1) { |
| 166 | count = __roundup_pow_of_two(count); |
| 167 | mask = ilog2(count); |
| 168 | } |
| 169 | |
| 170 | if (mask > ecap_max_handle_mask(iommu->ecap)) { |
| 171 | printk(KERN_ERR |
| 172 | "Requested mask %x exceeds the max invalidation handle" |
| 173 | " mask value %Lx\n", mask, |
| 174 | ecap_max_handle_mask(iommu->ecap)); |
| 175 | return -1; |
| 176 | } |
| 177 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 178 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 179 | do { |
| 180 | for (i = index; i < index + count; i++) |
| 181 | if (table->base[i].present) |
| 182 | break; |
| 183 | /* empty index found */ |
| 184 | if (i == index + count) |
| 185 | break; |
| 186 | |
| 187 | index = (index + count) % INTR_REMAP_TABLE_ENTRIES; |
| 188 | |
| 189 | if (index == start_index) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 190 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 191 | printk(KERN_ERR "can't allocate an IRTE\n"); |
| 192 | return -1; |
| 193 | } |
| 194 | } while (1); |
| 195 | |
| 196 | for (i = index; i < index + count; i++) |
| 197 | table->base[i].present = 1; |
| 198 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 199 | irq_iommu = irq_2_iommu_alloc(irq); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 200 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 201 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 202 | printk(KERN_ERR "can't allocate irq_2_iommu\n"); |
| 203 | return -1; |
| 204 | } |
| 205 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 206 | irq_iommu->iommu = iommu; |
| 207 | irq_iommu->irte_index = index; |
| 208 | irq_iommu->sub_handle = 0; |
| 209 | irq_iommu->irte_mask = mask; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 210 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 211 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 212 | |
| 213 | return index; |
| 214 | } |
| 215 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 216 | static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 217 | { |
| 218 | struct qi_desc desc; |
| 219 | |
| 220 | desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask) |
| 221 | | QI_IEC_SELECTIVE; |
| 222 | desc.high = 0; |
| 223 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 224 | return qi_submit_sync(&desc, iommu); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int map_irq_to_irte_handle(int irq, u16 *sub_handle) |
| 228 | { |
| 229 | int index; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 230 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 231 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 232 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 233 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 234 | irq_iommu = valid_irq_2_iommu(irq); |
| 235 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 236 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 237 | return -1; |
| 238 | } |
| 239 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 240 | *sub_handle = irq_iommu->sub_handle; |
| 241 | index = irq_iommu->irte_index; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 242 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 243 | return index; |
| 244 | } |
| 245 | |
| 246 | int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle) |
| 247 | { |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 248 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 249 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 250 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 251 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Suresh Siddha | 7ddfb65 | 2008-08-20 17:22:51 -0700 | [diff] [blame] | 252 | |
| 253 | irq_iommu = irq_2_iommu_alloc(irq); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 254 | |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 255 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 256 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 257 | printk(KERN_ERR "can't allocate irq_2_iommu\n"); |
| 258 | return -1; |
| 259 | } |
| 260 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 261 | irq_iommu->iommu = iommu; |
| 262 | irq_iommu->irte_index = index; |
| 263 | irq_iommu->sub_handle = subhandle; |
| 264 | irq_iommu->irte_mask = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 265 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 266 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index) |
| 272 | { |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 273 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 274 | unsigned long flags; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 275 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 276 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 277 | irq_iommu = valid_irq_2_iommu(irq); |
| 278 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 279 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 283 | irq_iommu->iommu = NULL; |
| 284 | irq_iommu->irte_index = 0; |
| 285 | irq_iommu->sub_handle = 0; |
| 286 | irq_2_iommu(irq)->irte_mask = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 287 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 288 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | int modify_irte(int irq, struct irte *irte_modified) |
| 294 | { |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 295 | int rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 296 | int index; |
| 297 | struct irte *irte; |
| 298 | struct intel_iommu *iommu; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 299 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 300 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 301 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 302 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 303 | irq_iommu = valid_irq_2_iommu(irq); |
| 304 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 305 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 306 | return -1; |
| 307 | } |
| 308 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 309 | iommu = irq_iommu->iommu; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 310 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 311 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 312 | irte = &iommu->ir_table->base[index]; |
| 313 | |
Weidong Han | c4658b4 | 2009-05-23 00:41:14 +0800 | [diff] [blame] | 314 | set_64bit((unsigned long *)&irte->low, irte_modified->low); |
| 315 | set_64bit((unsigned long *)&irte->high, irte_modified->high); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 316 | __iommu_flush_cache(iommu, irte, sizeof(*irte)); |
| 317 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 318 | rc = qi_flush_iec(iommu, index, 0); |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 319 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 320 | |
| 321 | return rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | int flush_irte(int irq) |
| 325 | { |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 326 | int rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 327 | int index; |
| 328 | struct intel_iommu *iommu; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 329 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 330 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 331 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 332 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 333 | irq_iommu = valid_irq_2_iommu(irq); |
| 334 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 335 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 336 | return -1; |
| 337 | } |
| 338 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 339 | iommu = irq_iommu->iommu; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 340 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 341 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 342 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 343 | rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask); |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 344 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 345 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 346 | return rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 349 | struct intel_iommu *map_hpet_to_ir(u8 hpet_id) |
| 350 | { |
| 351 | int i; |
| 352 | |
| 353 | for (i = 0; i < MAX_HPET_TBS; i++) |
| 354 | if (ir_hpet[i].id == hpet_id) |
| 355 | return ir_hpet[i].iommu; |
| 356 | return NULL; |
| 357 | } |
| 358 | |
Suresh Siddha | 89027d3 | 2008-07-10 11:16:56 -0700 | [diff] [blame] | 359 | struct intel_iommu *map_ioapic_to_ir(int apic) |
| 360 | { |
| 361 | int i; |
| 362 | |
| 363 | for (i = 0; i < MAX_IO_APICS; i++) |
| 364 | if (ir_ioapic[i].id == apic) |
| 365 | return ir_ioapic[i].iommu; |
| 366 | return NULL; |
| 367 | } |
| 368 | |
Suresh Siddha | 75c46fa | 2008-07-10 11:16:57 -0700 | [diff] [blame] | 369 | struct intel_iommu *map_dev_to_ir(struct pci_dev *dev) |
| 370 | { |
| 371 | struct dmar_drhd_unit *drhd; |
| 372 | |
| 373 | drhd = dmar_find_matched_drhd_unit(dev); |
| 374 | if (!drhd) |
| 375 | return NULL; |
| 376 | |
| 377 | return drhd->iommu; |
| 378 | } |
| 379 | |
Weidong Han | c4658b4 | 2009-05-23 00:41:14 +0800 | [diff] [blame] | 380 | static int clear_entries(struct irq_2_iommu *irq_iommu) |
| 381 | { |
| 382 | struct irte *start, *entry, *end; |
| 383 | struct intel_iommu *iommu; |
| 384 | int index; |
| 385 | |
| 386 | if (irq_iommu->sub_handle) |
| 387 | return 0; |
| 388 | |
| 389 | iommu = irq_iommu->iommu; |
| 390 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
| 391 | |
| 392 | start = iommu->ir_table->base + index; |
| 393 | end = start + (1 << irq_iommu->irte_mask); |
| 394 | |
| 395 | for (entry = start; entry < end; entry++) { |
| 396 | set_64bit((unsigned long *)&entry->low, 0); |
| 397 | set_64bit((unsigned long *)&entry->high, 0); |
| 398 | } |
| 399 | |
| 400 | return qi_flush_iec(iommu, index, irq_iommu->irte_mask); |
| 401 | } |
| 402 | |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 403 | int free_irte(int irq) |
| 404 | { |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 405 | int rc = 0; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 406 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 407 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 408 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 409 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 410 | irq_iommu = valid_irq_2_iommu(irq); |
| 411 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 412 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 413 | return -1; |
| 414 | } |
| 415 | |
Weidong Han | c4658b4 | 2009-05-23 00:41:14 +0800 | [diff] [blame] | 416 | rc = clear_entries(irq_iommu); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 417 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 418 | irq_iommu->iommu = NULL; |
| 419 | irq_iommu->irte_index = 0; |
| 420 | irq_iommu->sub_handle = 0; |
| 421 | irq_iommu->irte_mask = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 422 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 423 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 424 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 425 | return rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 428 | /* |
| 429 | * source validation type |
| 430 | */ |
| 431 | #define SVT_NO_VERIFY 0x0 /* no verification is required */ |
| 432 | #define SVT_VERIFY_SID_SQ 0x1 /* verify using SID and SQ fiels */ |
| 433 | #define SVT_VERIFY_BUS 0x2 /* verify bus of request-id */ |
| 434 | |
| 435 | /* |
| 436 | * source-id qualifier |
| 437 | */ |
| 438 | #define SQ_ALL_16 0x0 /* verify all 16 bits of request-id */ |
| 439 | #define SQ_13_IGNORE_1 0x1 /* verify most significant 13 bits, ignore |
| 440 | * the third least significant bit |
| 441 | */ |
| 442 | #define SQ_13_IGNORE_2 0x2 /* verify most significant 13 bits, ignore |
| 443 | * the second and third least significant bits |
| 444 | */ |
| 445 | #define SQ_13_IGNORE_3 0x3 /* verify most significant 13 bits, ignore |
| 446 | * the least three significant bits |
| 447 | */ |
| 448 | |
| 449 | /* |
| 450 | * set SVT, SQ and SID fields of irte to verify |
| 451 | * source ids of interrupt requests |
| 452 | */ |
| 453 | static void set_irte_sid(struct irte *irte, unsigned int svt, |
| 454 | unsigned int sq, unsigned int sid) |
| 455 | { |
| 456 | irte->svt = svt; |
| 457 | irte->sq = sq; |
| 458 | irte->sid = sid; |
| 459 | } |
| 460 | |
| 461 | int set_ioapic_sid(struct irte *irte, int apic) |
| 462 | { |
| 463 | int i; |
| 464 | u16 sid = 0; |
| 465 | |
| 466 | if (!irte) |
| 467 | return -1; |
| 468 | |
| 469 | for (i = 0; i < MAX_IO_APICS; i++) { |
| 470 | if (ir_ioapic[i].id == apic) { |
| 471 | sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn; |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (sid == 0) { |
| 477 | pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic); |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | set_irte_sid(irte, 1, 0, sid); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 486 | int set_hpet_sid(struct irte *irte, u8 id) |
| 487 | { |
| 488 | int i; |
| 489 | u16 sid = 0; |
| 490 | |
| 491 | if (!irte) |
| 492 | return -1; |
| 493 | |
| 494 | for (i = 0; i < MAX_HPET_TBS; i++) { |
| 495 | if (ir_hpet[i].id == id) { |
| 496 | sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn; |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (sid == 0) { |
| 502 | pr_warning("Failed to set source-id of HPET block (%d)\n", id); |
| 503 | return -1; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Should really use SQ_ALL_16. Some platforms are broken. |
| 508 | * While we figure out the right quirks for these broken platforms, use |
| 509 | * SQ_13_IGNORE_3 for now. |
| 510 | */ |
| 511 | set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid); |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 516 | int set_msi_sid(struct irte *irte, struct pci_dev *dev) |
| 517 | { |
| 518 | struct pci_dev *bridge; |
| 519 | |
| 520 | if (!irte || !dev) |
| 521 | return -1; |
| 522 | |
| 523 | /* PCIe device or Root Complex integrated PCI device */ |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 524 | if (pci_is_pcie(dev) || !dev->bus->parent) { |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 525 | set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, |
| 526 | (dev->bus->number << 8) | dev->devfn); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | bridge = pci_find_upstream_pcie_bridge(dev); |
| 531 | if (bridge) { |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 532 | if (pci_is_pcie(bridge))/* this is a PCIe-to-PCI/PCIX bridge */ |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 533 | set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16, |
| 534 | (bridge->bus->number << 8) | dev->bus->number); |
| 535 | else /* this is a legacy PCI bridge */ |
| 536 | set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, |
| 537 | (bridge->bus->number << 8) | bridge->devfn); |
| 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 543 | static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode) |
| 544 | { |
| 545 | u64 addr; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 546 | u32 sts; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 547 | unsigned long flags; |
| 548 | |
| 549 | addr = virt_to_phys((void *)iommu->ir_table->base); |
| 550 | |
| 551 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 552 | |
| 553 | dmar_writeq(iommu->reg + DMAR_IRTA_REG, |
| 554 | (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE); |
| 555 | |
| 556 | /* Set interrupt-remapping table pointer */ |
Han, Weidong | 161fde0 | 2009-04-03 17:15:47 +0800 | [diff] [blame] | 557 | iommu->gcmd |= DMA_GCMD_SIRTP; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 558 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 559 | |
| 560 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 561 | readl, (sts & DMA_GSTS_IRTPS), sts); |
| 562 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 563 | |
| 564 | /* |
| 565 | * global invalidation of interrupt entry cache before enabling |
| 566 | * interrupt-remapping. |
| 567 | */ |
| 568 | qi_global_iec(iommu); |
| 569 | |
| 570 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 571 | |
| 572 | /* Enable interrupt-remapping */ |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 573 | iommu->gcmd |= DMA_GCMD_IRE; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 574 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 575 | |
| 576 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 577 | readl, (sts & DMA_GSTS_IRES), sts); |
| 578 | |
| 579 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 580 | } |
| 581 | |
| 582 | |
| 583 | static int setup_intr_remapping(struct intel_iommu *iommu, int mode) |
| 584 | { |
| 585 | struct ir_table *ir_table; |
| 586 | struct page *pages; |
| 587 | |
| 588 | ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table), |
Suresh Siddha | fa4b57c | 2009-03-16 17:05:05 -0700 | [diff] [blame] | 589 | GFP_ATOMIC); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 590 | |
| 591 | if (!iommu->ir_table) |
| 592 | return -ENOMEM; |
| 593 | |
Suresh Siddha | 824cd75 | 2009-10-02 11:01:23 -0700 | [diff] [blame] | 594 | pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO, |
| 595 | INTR_REMAP_PAGE_ORDER); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 596 | |
| 597 | if (!pages) { |
| 598 | printk(KERN_ERR "failed to allocate pages of order %d\n", |
| 599 | INTR_REMAP_PAGE_ORDER); |
| 600 | kfree(iommu->ir_table); |
| 601 | return -ENOMEM; |
| 602 | } |
| 603 | |
| 604 | ir_table->base = page_address(pages); |
| 605 | |
| 606 | iommu_set_intr_remapping(iommu, mode); |
| 607 | return 0; |
| 608 | } |
| 609 | |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 610 | /* |
| 611 | * Disable Interrupt Remapping. |
| 612 | */ |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 613 | static void iommu_disable_intr_remapping(struct intel_iommu *iommu) |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 614 | { |
| 615 | unsigned long flags; |
| 616 | u32 sts; |
| 617 | |
| 618 | if (!ecap_ir_support(iommu->ecap)) |
| 619 | return; |
| 620 | |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 621 | /* |
| 622 | * global invalidation of interrupt entry cache before disabling |
| 623 | * interrupt-remapping. |
| 624 | */ |
| 625 | qi_global_iec(iommu); |
| 626 | |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 627 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 628 | |
| 629 | sts = dmar_readq(iommu->reg + DMAR_GSTS_REG); |
| 630 | if (!(sts & DMA_GSTS_IRES)) |
| 631 | goto end; |
| 632 | |
| 633 | iommu->gcmd &= ~DMA_GCMD_IRE; |
| 634 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 635 | |
| 636 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 637 | readl, !(sts & DMA_GSTS_IRES), sts); |
| 638 | |
| 639 | end: |
| 640 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 641 | } |
| 642 | |
Weidong Han | 9375823 | 2009-04-17 16:42:14 +0800 | [diff] [blame] | 643 | int __init intr_remapping_supported(void) |
| 644 | { |
| 645 | struct dmar_drhd_unit *drhd; |
| 646 | |
Weidong Han | 03ea815 | 2009-04-17 16:42:15 +0800 | [diff] [blame] | 647 | if (disable_intremap) |
| 648 | return 0; |
| 649 | |
Youquan Song | 074835f | 2009-09-09 12:05:39 -0400 | [diff] [blame] | 650 | if (!dmar_ir_support()) |
| 651 | return 0; |
| 652 | |
Weidong Han | 9375823 | 2009-04-17 16:42:14 +0800 | [diff] [blame] | 653 | for_each_drhd_unit(drhd) { |
| 654 | struct intel_iommu *iommu = drhd->iommu; |
| 655 | |
| 656 | if (!ecap_ir_support(iommu->ecap)) |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | return 1; |
| 661 | } |
| 662 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 663 | int __init enable_intr_remapping(int eim) |
| 664 | { |
| 665 | struct dmar_drhd_unit *drhd; |
| 666 | int setup = 0; |
| 667 | |
Youquan Song | e936d07 | 2009-09-07 10:58:07 -0400 | [diff] [blame] | 668 | if (parse_ioapics_under_ir() != 1) { |
| 669 | printk(KERN_INFO "Not enable interrupt remapping\n"); |
| 670 | return -1; |
| 671 | } |
| 672 | |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 673 | for_each_drhd_unit(drhd) { |
| 674 | struct intel_iommu *iommu = drhd->iommu; |
| 675 | |
| 676 | /* |
Han, Weidong | 34aaaa9 | 2009-04-04 17:21:26 +0800 | [diff] [blame] | 677 | * If the queued invalidation is already initialized, |
| 678 | * shouldn't disable it. |
| 679 | */ |
| 680 | if (iommu->qi) |
| 681 | continue; |
| 682 | |
| 683 | /* |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 684 | * Clear previous faults. |
| 685 | */ |
| 686 | dmar_fault(-1, iommu); |
| 687 | |
| 688 | /* |
| 689 | * Disable intr remapping and queued invalidation, if already |
| 690 | * enabled prior to OS handover. |
| 691 | */ |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 692 | iommu_disable_intr_remapping(iommu); |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 693 | |
| 694 | dmar_disable_qi(iommu); |
| 695 | } |
| 696 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 697 | /* |
| 698 | * check for the Interrupt-remapping support |
| 699 | */ |
| 700 | for_each_drhd_unit(drhd) { |
| 701 | struct intel_iommu *iommu = drhd->iommu; |
| 702 | |
| 703 | if (!ecap_ir_support(iommu->ecap)) |
| 704 | continue; |
| 705 | |
| 706 | if (eim && !ecap_eim_support(iommu->ecap)) { |
| 707 | printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, " |
| 708 | " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap); |
| 709 | return -1; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Enable queued invalidation for all the DRHD's. |
| 715 | */ |
| 716 | for_each_drhd_unit(drhd) { |
| 717 | int ret; |
| 718 | struct intel_iommu *iommu = drhd->iommu; |
| 719 | ret = dmar_enable_qi(iommu); |
| 720 | |
| 721 | if (ret) { |
| 722 | printk(KERN_ERR "DRHD %Lx: failed to enable queued, " |
| 723 | " invalidation, ecap %Lx, ret %d\n", |
| 724 | drhd->reg_base_addr, iommu->ecap, ret); |
| 725 | return -1; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Setup Interrupt-remapping for all the DRHD's now. |
| 731 | */ |
| 732 | for_each_drhd_unit(drhd) { |
| 733 | struct intel_iommu *iommu = drhd->iommu; |
| 734 | |
| 735 | if (!ecap_ir_support(iommu->ecap)) |
| 736 | continue; |
| 737 | |
| 738 | if (setup_intr_remapping(iommu, eim)) |
| 739 | goto error; |
| 740 | |
| 741 | setup = 1; |
| 742 | } |
| 743 | |
| 744 | if (!setup) |
| 745 | goto error; |
| 746 | |
| 747 | intr_remapping_enabled = 1; |
| 748 | |
| 749 | return 0; |
| 750 | |
| 751 | error: |
| 752 | /* |
| 753 | * handle error condition gracefully here! |
| 754 | */ |
| 755 | return -1; |
| 756 | } |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 757 | |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 758 | static void ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope, |
| 759 | struct intel_iommu *iommu) |
| 760 | { |
| 761 | struct acpi_dmar_pci_path *path; |
| 762 | u8 bus; |
| 763 | int count; |
| 764 | |
| 765 | bus = scope->bus; |
| 766 | path = (struct acpi_dmar_pci_path *)(scope + 1); |
| 767 | count = (scope->length - sizeof(struct acpi_dmar_device_scope)) |
| 768 | / sizeof(struct acpi_dmar_pci_path); |
| 769 | |
| 770 | while (--count > 0) { |
| 771 | /* |
| 772 | * Access PCI directly due to the PCI |
| 773 | * subsystem isn't initialized yet. |
| 774 | */ |
| 775 | bus = read_pci_config_byte(bus, path->dev, path->fn, |
| 776 | PCI_SECONDARY_BUS); |
| 777 | path++; |
| 778 | } |
| 779 | ir_hpet[ir_hpet_num].bus = bus; |
| 780 | ir_hpet[ir_hpet_num].devfn = PCI_DEVFN(path->dev, path->fn); |
| 781 | ir_hpet[ir_hpet_num].iommu = iommu; |
| 782 | ir_hpet[ir_hpet_num].id = scope->enumeration_id; |
| 783 | ir_hpet_num++; |
| 784 | } |
| 785 | |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 786 | static void ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope, |
| 787 | struct intel_iommu *iommu) |
| 788 | { |
| 789 | struct acpi_dmar_pci_path *path; |
| 790 | u8 bus; |
| 791 | int count; |
| 792 | |
| 793 | bus = scope->bus; |
| 794 | path = (struct acpi_dmar_pci_path *)(scope + 1); |
| 795 | count = (scope->length - sizeof(struct acpi_dmar_device_scope)) |
| 796 | / sizeof(struct acpi_dmar_pci_path); |
| 797 | |
| 798 | while (--count > 0) { |
| 799 | /* |
| 800 | * Access PCI directly due to the PCI |
| 801 | * subsystem isn't initialized yet. |
| 802 | */ |
| 803 | bus = read_pci_config_byte(bus, path->dev, path->fn, |
| 804 | PCI_SECONDARY_BUS); |
| 805 | path++; |
| 806 | } |
| 807 | |
| 808 | ir_ioapic[ir_ioapic_num].bus = bus; |
| 809 | ir_ioapic[ir_ioapic_num].devfn = PCI_DEVFN(path->dev, path->fn); |
| 810 | ir_ioapic[ir_ioapic_num].iommu = iommu; |
| 811 | ir_ioapic[ir_ioapic_num].id = scope->enumeration_id; |
| 812 | ir_ioapic_num++; |
| 813 | } |
| 814 | |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 815 | static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header, |
| 816 | struct intel_iommu *iommu) |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 817 | { |
| 818 | struct acpi_dmar_hardware_unit *drhd; |
| 819 | struct acpi_dmar_device_scope *scope; |
| 820 | void *start, *end; |
| 821 | |
| 822 | drhd = (struct acpi_dmar_hardware_unit *)header; |
| 823 | |
| 824 | start = (void *)(drhd + 1); |
| 825 | end = ((void *)drhd) + header->length; |
| 826 | |
| 827 | while (start < end) { |
| 828 | scope = start; |
| 829 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) { |
| 830 | if (ir_ioapic_num == MAX_IO_APICS) { |
| 831 | printk(KERN_WARNING "Exceeded Max IO APICS\n"); |
| 832 | return -1; |
| 833 | } |
| 834 | |
Yinghai Lu | 680a752 | 2010-04-08 19:58:23 +0100 | [diff] [blame] | 835 | printk(KERN_INFO "IOAPIC id %d under DRHD base " |
| 836 | " 0x%Lx IOMMU %d\n", scope->enumeration_id, |
| 837 | drhd->address, iommu->seq_id); |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 838 | |
Weidong Han | f007e99 | 2009-05-23 00:41:15 +0800 | [diff] [blame] | 839 | ir_parse_one_ioapic_scope(scope, iommu); |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 840 | } else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET) { |
| 841 | if (ir_hpet_num == MAX_HPET_TBS) { |
| 842 | printk(KERN_WARNING "Exceeded Max HPET blocks\n"); |
| 843 | return -1; |
| 844 | } |
| 845 | |
| 846 | printk(KERN_INFO "HPET id %d under DRHD base" |
| 847 | " 0x%Lx\n", scope->enumeration_id, |
| 848 | drhd->address); |
| 849 | |
| 850 | ir_parse_one_hpet_scope(scope, iommu); |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 851 | } |
| 852 | start += scope->length; |
| 853 | } |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Finds the assocaition between IOAPIC's and its Interrupt-remapping |
| 860 | * hardware unit. |
| 861 | */ |
| 862 | int __init parse_ioapics_under_ir(void) |
| 863 | { |
| 864 | struct dmar_drhd_unit *drhd; |
| 865 | int ir_supported = 0; |
| 866 | |
| 867 | for_each_drhd_unit(drhd) { |
| 868 | struct intel_iommu *iommu = drhd->iommu; |
| 869 | |
| 870 | if (ecap_ir_support(iommu->ecap)) { |
Suresh Siddha | 20f3097 | 2009-08-04 12:07:08 -0700 | [diff] [blame] | 871 | if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu)) |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 872 | return -1; |
| 873 | |
| 874 | ir_supported = 1; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | if (ir_supported && ir_ioapic_num != nr_ioapics) { |
| 879 | printk(KERN_WARNING |
| 880 | "Not all IO-APIC's listed under remapping hardware\n"); |
| 881 | return -1; |
| 882 | } |
| 883 | |
| 884 | return ir_supported; |
| 885 | } |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 886 | |
| 887 | void disable_intr_remapping(void) |
| 888 | { |
| 889 | struct dmar_drhd_unit *drhd; |
| 890 | struct intel_iommu *iommu = NULL; |
| 891 | |
| 892 | /* |
| 893 | * Disable Interrupt-remapping for all the DRHD's now. |
| 894 | */ |
| 895 | for_each_iommu(iommu, drhd) { |
| 896 | if (!ecap_ir_support(iommu->ecap)) |
| 897 | continue; |
| 898 | |
| 899 | iommu_disable_intr_remapping(iommu); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | int reenable_intr_remapping(int eim) |
| 904 | { |
| 905 | struct dmar_drhd_unit *drhd; |
| 906 | int setup = 0; |
| 907 | struct intel_iommu *iommu = NULL; |
| 908 | |
| 909 | for_each_iommu(iommu, drhd) |
| 910 | if (iommu->qi) |
| 911 | dmar_reenable_qi(iommu); |
| 912 | |
| 913 | /* |
| 914 | * Setup Interrupt-remapping for all the DRHD's now. |
| 915 | */ |
| 916 | for_each_iommu(iommu, drhd) { |
| 917 | if (!ecap_ir_support(iommu->ecap)) |
| 918 | continue; |
| 919 | |
| 920 | /* Set up interrupt remapping for iommu.*/ |
| 921 | iommu_set_intr_remapping(iommu, eim); |
| 922 | setup = 1; |
| 923 | } |
| 924 | |
| 925 | if (!setup) |
| 926 | goto error; |
| 927 | |
| 928 | return 0; |
| 929 | |
| 930 | error: |
| 931 | /* |
| 932 | * handle error condition gracefully here! |
| 933 | */ |
| 934 | return -1; |
| 935 | } |
| 936 | |