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> |
| 4 | #include <linux/jiffies.h> |
| 5 | #include <linux/pci.h> |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 6 | #include <linux/irq.h> |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 7 | #include <asm/io_apic.h> |
Yinghai Lu | 17483a1 | 2008-12-12 13:14:18 -0800 | [diff] [blame] | 8 | #include <asm/smp.h> |
Jaswinder Singh Rajput | 6d652ea | 2009-01-07 21:38:59 +0530 | [diff] [blame] | 9 | #include <asm/cpu.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 10 | #include <linux/intel-iommu.h> |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 11 | #include "intr_remapping.h" |
| 12 | |
| 13 | static struct ioapic_scope ir_ioapic[MAX_IO_APICS]; |
| 14 | static int ir_ioapic_num; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 15 | int intr_remapping_enabled; |
| 16 | |
Yinghai Lu | 5aeecaf | 2008-08-19 20:49:59 -0700 | [diff] [blame] | 17 | struct irq_2_iommu { |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 18 | struct intel_iommu *iommu; |
| 19 | u16 irte_index; |
| 20 | u16 sub_handle; |
| 21 | u8 irte_mask; |
Yinghai Lu | 5aeecaf | 2008-08-19 20:49:59 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
Yinghai Lu | d7e51e6 | 2009-01-07 15:03:13 -0800 | [diff] [blame] | 24 | #ifdef CONFIG_GENERIC_HARDIRQS |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 25 | static struct irq_2_iommu *get_one_free_irq_2_iommu(int cpu) |
| 26 | { |
| 27 | struct irq_2_iommu *iommu; |
| 28 | int node; |
| 29 | |
| 30 | node = cpu_to_node(cpu); |
| 31 | |
| 32 | iommu = kzalloc_node(sizeof(*iommu), GFP_ATOMIC, node); |
| 33 | printk(KERN_DEBUG "alloc irq_2_iommu on cpu %d node %d\n", cpu, node); |
| 34 | |
| 35 | return iommu; |
| 36 | } |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 37 | |
| 38 | static struct irq_2_iommu *irq_2_iommu(unsigned int irq) |
| 39 | { |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 40 | struct irq_desc *desc; |
| 41 | |
| 42 | desc = irq_to_desc(irq); |
| 43 | |
| 44 | if (WARN_ON_ONCE(!desc)) |
| 45 | return NULL; |
| 46 | |
| 47 | return desc->irq_2_iommu; |
| 48 | } |
| 49 | |
| 50 | static struct irq_2_iommu *irq_2_iommu_alloc_cpu(unsigned int irq, int cpu) |
| 51 | { |
| 52 | struct irq_desc *desc; |
| 53 | struct irq_2_iommu *irq_iommu; |
| 54 | |
| 55 | /* |
| 56 | * alloc irq desc if not allocated already. |
| 57 | */ |
| 58 | desc = irq_to_desc_alloc_cpu(irq, cpu); |
| 59 | if (!desc) { |
| 60 | printk(KERN_INFO "can not get irq_desc for %d\n", irq); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | irq_iommu = desc->irq_2_iommu; |
| 65 | |
| 66 | if (!irq_iommu) |
| 67 | desc->irq_2_iommu = get_one_free_irq_2_iommu(cpu); |
| 68 | |
| 69 | return desc->irq_2_iommu; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 70 | } |
Thomas Gleixner | d6c88a5 | 2008-10-15 15:27:23 +0200 | [diff] [blame] | 71 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 72 | static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq) |
| 73 | { |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 74 | return irq_2_iommu_alloc_cpu(irq, boot_cpu_id); |
| 75 | } |
| 76 | |
| 77 | #else /* !CONFIG_SPARSE_IRQ */ |
| 78 | |
| 79 | static struct irq_2_iommu irq_2_iommuX[NR_IRQS]; |
| 80 | |
| 81 | static struct irq_2_iommu *irq_2_iommu(unsigned int irq) |
| 82 | { |
| 83 | if (irq < nr_irqs) |
| 84 | return &irq_2_iommuX[irq]; |
| 85 | |
| 86 | return NULL; |
| 87 | } |
| 88 | static struct irq_2_iommu *irq_2_iommu_alloc(unsigned int irq) |
| 89 | { |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 90 | return irq_2_iommu(irq); |
| 91 | } |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 92 | #endif |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 93 | |
| 94 | static DEFINE_SPINLOCK(irq_2_ir_lock); |
| 95 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 96 | static struct irq_2_iommu *valid_irq_2_iommu(unsigned int irq) |
| 97 | { |
| 98 | struct irq_2_iommu *irq_iommu; |
| 99 | |
| 100 | irq_iommu = irq_2_iommu(irq); |
| 101 | |
| 102 | if (!irq_iommu) |
| 103 | return NULL; |
| 104 | |
| 105 | if (!irq_iommu->iommu) |
| 106 | return NULL; |
| 107 | |
| 108 | return irq_iommu; |
| 109 | } |
| 110 | |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 111 | int irq_remapped(int irq) |
| 112 | { |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 113 | return valid_irq_2_iommu(irq) != NULL; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int get_irte(int irq, struct irte *entry) |
| 117 | { |
| 118 | int index; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 119 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 120 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 121 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 122 | if (!entry) |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 123 | return -1; |
| 124 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 125 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 126 | irq_iommu = valid_irq_2_iommu(irq); |
| 127 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 128 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 132 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
| 133 | *entry = *(irq_iommu->iommu->ir_table->base + index); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 134 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 135 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | int alloc_irte(struct intel_iommu *iommu, int irq, u16 count) |
| 140 | { |
| 141 | struct ir_table *table = iommu->ir_table; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 142 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 143 | u16 index, start_index; |
| 144 | unsigned int mask = 0; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 145 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 146 | int i; |
| 147 | |
| 148 | if (!count) |
| 149 | return -1; |
| 150 | |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 151 | #ifndef CONFIG_SPARSE_IRQ |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 152 | /* protect irq_2_iommu_alloc later */ |
| 153 | if (irq >= nr_irqs) |
| 154 | return -1; |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 155 | #endif |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 156 | |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 157 | /* |
| 158 | * start the IRTE search from index 0. |
| 159 | */ |
| 160 | index = start_index = 0; |
| 161 | |
| 162 | if (count > 1) { |
| 163 | count = __roundup_pow_of_two(count); |
| 164 | mask = ilog2(count); |
| 165 | } |
| 166 | |
| 167 | if (mask > ecap_max_handle_mask(iommu->ecap)) { |
| 168 | printk(KERN_ERR |
| 169 | "Requested mask %x exceeds the max invalidation handle" |
| 170 | " mask value %Lx\n", mask, |
| 171 | ecap_max_handle_mask(iommu->ecap)); |
| 172 | return -1; |
| 173 | } |
| 174 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 175 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 176 | do { |
| 177 | for (i = index; i < index + count; i++) |
| 178 | if (table->base[i].present) |
| 179 | break; |
| 180 | /* empty index found */ |
| 181 | if (i == index + count) |
| 182 | break; |
| 183 | |
| 184 | index = (index + count) % INTR_REMAP_TABLE_ENTRIES; |
| 185 | |
| 186 | if (index == start_index) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 187 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 188 | printk(KERN_ERR "can't allocate an IRTE\n"); |
| 189 | return -1; |
| 190 | } |
| 191 | } while (1); |
| 192 | |
| 193 | for (i = index; i < index + count; i++) |
| 194 | table->base[i].present = 1; |
| 195 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 196 | irq_iommu = irq_2_iommu_alloc(irq); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 197 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 198 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 199 | printk(KERN_ERR "can't allocate irq_2_iommu\n"); |
| 200 | return -1; |
| 201 | } |
| 202 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 203 | irq_iommu->iommu = iommu; |
| 204 | irq_iommu->irte_index = index; |
| 205 | irq_iommu->sub_handle = 0; |
| 206 | irq_iommu->irte_mask = mask; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 207 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 208 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 209 | |
| 210 | return index; |
| 211 | } |
| 212 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 213 | 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] | 214 | { |
| 215 | struct qi_desc desc; |
| 216 | |
| 217 | desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask) |
| 218 | | QI_IEC_SELECTIVE; |
| 219 | desc.high = 0; |
| 220 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 221 | return qi_submit_sync(&desc, iommu); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | int map_irq_to_irte_handle(int irq, u16 *sub_handle) |
| 225 | { |
| 226 | int index; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 227 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 228 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 229 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 230 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 231 | irq_iommu = valid_irq_2_iommu(irq); |
| 232 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 233 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 234 | return -1; |
| 235 | } |
| 236 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 237 | *sub_handle = irq_iommu->sub_handle; |
| 238 | index = irq_iommu->irte_index; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 239 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 240 | return index; |
| 241 | } |
| 242 | |
| 243 | int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle) |
| 244 | { |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 245 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 246 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 247 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 248 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Suresh Siddha | 7ddfb65 | 2008-08-20 17:22:51 -0700 | [diff] [blame] | 249 | |
| 250 | irq_iommu = irq_2_iommu_alloc(irq); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 251 | |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 252 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 253 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 254 | printk(KERN_ERR "can't allocate irq_2_iommu\n"); |
| 255 | return -1; |
| 256 | } |
| 257 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 258 | irq_iommu->iommu = iommu; |
| 259 | irq_iommu->irte_index = index; |
| 260 | irq_iommu->sub_handle = subhandle; |
| 261 | irq_iommu->irte_mask = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 262 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 263 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | int clear_irte_irq(int irq, struct intel_iommu *iommu, u16 index) |
| 269 | { |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 270 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 271 | unsigned long flags; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 272 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 273 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 274 | irq_iommu = valid_irq_2_iommu(irq); |
| 275 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 276 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 277 | return -1; |
| 278 | } |
| 279 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 280 | irq_iommu->iommu = NULL; |
| 281 | irq_iommu->irte_index = 0; |
| 282 | irq_iommu->sub_handle = 0; |
| 283 | irq_2_iommu(irq)->irte_mask = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 284 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 285 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | int modify_irte(int irq, struct irte *irte_modified) |
| 291 | { |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 292 | int rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 293 | int index; |
| 294 | struct irte *irte; |
| 295 | struct intel_iommu *iommu; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 296 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 297 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 298 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 299 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 300 | irq_iommu = valid_irq_2_iommu(irq); |
| 301 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 302 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 303 | return -1; |
| 304 | } |
| 305 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 306 | iommu = irq_iommu->iommu; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 307 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 308 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 309 | irte = &iommu->ir_table->base[index]; |
| 310 | |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 311 | set_64bit((unsigned long *)irte, irte_modified->low); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 312 | __iommu_flush_cache(iommu, irte, sizeof(*irte)); |
| 313 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 314 | rc = qi_flush_iec(iommu, index, 0); |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 315 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 316 | |
| 317 | return rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | int flush_irte(int irq) |
| 321 | { |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 322 | int rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 323 | int index; |
| 324 | struct intel_iommu *iommu; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 325 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 326 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 327 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 328 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 329 | irq_iommu = valid_irq_2_iommu(irq); |
| 330 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 331 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 332 | return -1; |
| 333 | } |
| 334 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 335 | iommu = irq_iommu->iommu; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 336 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 337 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 338 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 339 | rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask); |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 340 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 341 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 342 | return rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Suresh Siddha | 89027d3 | 2008-07-10 11:16:56 -0700 | [diff] [blame] | 345 | struct intel_iommu *map_ioapic_to_ir(int apic) |
| 346 | { |
| 347 | int i; |
| 348 | |
| 349 | for (i = 0; i < MAX_IO_APICS; i++) |
| 350 | if (ir_ioapic[i].id == apic) |
| 351 | return ir_ioapic[i].iommu; |
| 352 | return NULL; |
| 353 | } |
| 354 | |
Suresh Siddha | 75c46fa | 2008-07-10 11:16:57 -0700 | [diff] [blame] | 355 | struct intel_iommu *map_dev_to_ir(struct pci_dev *dev) |
| 356 | { |
| 357 | struct dmar_drhd_unit *drhd; |
| 358 | |
| 359 | drhd = dmar_find_matched_drhd_unit(dev); |
| 360 | if (!drhd) |
| 361 | return NULL; |
| 362 | |
| 363 | return drhd->iommu; |
| 364 | } |
| 365 | |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 366 | int free_irte(int irq) |
| 367 | { |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 368 | int rc = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 369 | int index, i; |
| 370 | struct irte *irte; |
| 371 | struct intel_iommu *iommu; |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 372 | struct irq_2_iommu *irq_iommu; |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 373 | unsigned long flags; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 374 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 375 | spin_lock_irqsave(&irq_2_ir_lock, flags); |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 376 | irq_iommu = valid_irq_2_iommu(irq); |
| 377 | if (!irq_iommu) { |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 378 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 382 | iommu = irq_iommu->iommu; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 383 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 384 | index = irq_iommu->irte_index + irq_iommu->sub_handle; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 385 | irte = &iommu->ir_table->base[index]; |
| 386 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 387 | if (!irq_iommu->sub_handle) { |
| 388 | for (i = 0; i < (1 << irq_iommu->irte_mask); i++) |
Suresh Siddha | 2e93456 | 2009-03-16 17:04:58 -0700 | [diff] [blame] | 389 | set_64bit((unsigned long *)(irte + i), 0); |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 390 | rc = qi_flush_iec(iommu, index, irq_iommu->irte_mask); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Yinghai Lu | e420dfb | 2008-08-19 20:50:21 -0700 | [diff] [blame] | 393 | irq_iommu->iommu = NULL; |
| 394 | irq_iommu->irte_index = 0; |
| 395 | irq_iommu->sub_handle = 0; |
| 396 | irq_iommu->irte_mask = 0; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 397 | |
Suresh Siddha | 4c5502b | 2009-03-16 17:04:53 -0700 | [diff] [blame] | 398 | spin_unlock_irqrestore(&irq_2_ir_lock, flags); |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 399 | |
Yu Zhao | 704126a | 2009-01-04 16:28:52 +0800 | [diff] [blame] | 400 | return rc; |
Suresh Siddha | b6fcb33 | 2008-07-10 11:16:44 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 403 | static void iommu_set_intr_remapping(struct intel_iommu *iommu, int mode) |
| 404 | { |
| 405 | u64 addr; |
| 406 | u32 cmd, sts; |
| 407 | unsigned long flags; |
| 408 | |
| 409 | addr = virt_to_phys((void *)iommu->ir_table->base); |
| 410 | |
| 411 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 412 | |
| 413 | dmar_writeq(iommu->reg + DMAR_IRTA_REG, |
| 414 | (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE); |
| 415 | |
| 416 | /* Set interrupt-remapping table pointer */ |
| 417 | cmd = iommu->gcmd | DMA_GCMD_SIRTP; |
Han, Weidong | 161fde0 | 2009-04-03 17:15:47 +0800 | [diff] [blame] | 418 | iommu->gcmd |= DMA_GCMD_SIRTP; |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 419 | writel(cmd, iommu->reg + DMAR_GCMD_REG); |
| 420 | |
| 421 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 422 | readl, (sts & DMA_GSTS_IRTPS), sts); |
| 423 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 424 | |
Han, Weidong | 161fde0 | 2009-04-03 17:15:47 +0800 | [diff] [blame] | 425 | if (mode == 0) { |
| 426 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 427 | |
| 428 | /* enable comaptiblity format interrupt pass through */ |
| 429 | cmd = iommu->gcmd | DMA_GCMD_CFI; |
| 430 | iommu->gcmd |= DMA_GCMD_CFI; |
| 431 | writel(cmd, iommu->reg + DMAR_GCMD_REG); |
| 432 | |
| 433 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 434 | readl, (sts & DMA_GSTS_CFIS), sts); |
| 435 | |
| 436 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 437 | } |
| 438 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 439 | /* |
| 440 | * global invalidation of interrupt entry cache before enabling |
| 441 | * interrupt-remapping. |
| 442 | */ |
| 443 | qi_global_iec(iommu); |
| 444 | |
| 445 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 446 | |
| 447 | /* Enable interrupt-remapping */ |
| 448 | cmd = iommu->gcmd | DMA_GCMD_IRE; |
| 449 | iommu->gcmd |= DMA_GCMD_IRE; |
| 450 | writel(cmd, iommu->reg + DMAR_GCMD_REG); |
| 451 | |
| 452 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 453 | readl, (sts & DMA_GSTS_IRES), sts); |
| 454 | |
| 455 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | static int setup_intr_remapping(struct intel_iommu *iommu, int mode) |
| 460 | { |
| 461 | struct ir_table *ir_table; |
| 462 | struct page *pages; |
| 463 | |
| 464 | ir_table = iommu->ir_table = kzalloc(sizeof(struct ir_table), |
Suresh Siddha | fa4b57c | 2009-03-16 17:05:05 -0700 | [diff] [blame] | 465 | GFP_ATOMIC); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 466 | |
| 467 | if (!iommu->ir_table) |
| 468 | return -ENOMEM; |
| 469 | |
Suresh Siddha | fa4b57c | 2009-03-16 17:05:05 -0700 | [diff] [blame] | 470 | pages = alloc_pages(GFP_ATOMIC | __GFP_ZERO, INTR_REMAP_PAGE_ORDER); |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 471 | |
| 472 | if (!pages) { |
| 473 | printk(KERN_ERR "failed to allocate pages of order %d\n", |
| 474 | INTR_REMAP_PAGE_ORDER); |
| 475 | kfree(iommu->ir_table); |
| 476 | return -ENOMEM; |
| 477 | } |
| 478 | |
| 479 | ir_table->base = page_address(pages); |
| 480 | |
| 481 | iommu_set_intr_remapping(iommu, mode); |
| 482 | return 0; |
| 483 | } |
| 484 | |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 485 | /* |
| 486 | * Disable Interrupt Remapping. |
| 487 | */ |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 488 | static void iommu_disable_intr_remapping(struct intel_iommu *iommu) |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 489 | { |
| 490 | unsigned long flags; |
| 491 | u32 sts; |
| 492 | |
| 493 | if (!ecap_ir_support(iommu->ecap)) |
| 494 | return; |
| 495 | |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 496 | /* |
| 497 | * global invalidation of interrupt entry cache before disabling |
| 498 | * interrupt-remapping. |
| 499 | */ |
| 500 | qi_global_iec(iommu); |
| 501 | |
Suresh Siddha | eba67e5 | 2009-03-16 17:04:56 -0700 | [diff] [blame] | 502 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 503 | |
| 504 | sts = dmar_readq(iommu->reg + DMAR_GSTS_REG); |
| 505 | if (!(sts & DMA_GSTS_IRES)) |
| 506 | goto end; |
| 507 | |
| 508 | iommu->gcmd &= ~DMA_GCMD_IRE; |
| 509 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 510 | |
| 511 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
| 512 | readl, !(sts & DMA_GSTS_IRES), sts); |
| 513 | |
| 514 | end: |
| 515 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 516 | } |
| 517 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 518 | int __init enable_intr_remapping(int eim) |
| 519 | { |
| 520 | struct dmar_drhd_unit *drhd; |
| 521 | int setup = 0; |
| 522 | |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 523 | for_each_drhd_unit(drhd) { |
| 524 | struct intel_iommu *iommu = drhd->iommu; |
| 525 | |
| 526 | /* |
Han, Weidong | 34aaaa9 | 2009-04-04 17:21:26 +0800 | [diff] [blame^] | 527 | * If the queued invalidation is already initialized, |
| 528 | * shouldn't disable it. |
| 529 | */ |
| 530 | if (iommu->qi) |
| 531 | continue; |
| 532 | |
| 533 | /* |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 534 | * Clear previous faults. |
| 535 | */ |
| 536 | dmar_fault(-1, iommu); |
| 537 | |
| 538 | /* |
| 539 | * Disable intr remapping and queued invalidation, if already |
| 540 | * enabled prior to OS handover. |
| 541 | */ |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 542 | iommu_disable_intr_remapping(iommu); |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 543 | |
| 544 | dmar_disable_qi(iommu); |
| 545 | } |
| 546 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 547 | /* |
| 548 | * check for the Interrupt-remapping support |
| 549 | */ |
| 550 | for_each_drhd_unit(drhd) { |
| 551 | struct intel_iommu *iommu = drhd->iommu; |
| 552 | |
| 553 | if (!ecap_ir_support(iommu->ecap)) |
| 554 | continue; |
| 555 | |
| 556 | if (eim && !ecap_eim_support(iommu->ecap)) { |
| 557 | printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, " |
| 558 | " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap); |
| 559 | return -1; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Enable queued invalidation for all the DRHD's. |
| 565 | */ |
| 566 | for_each_drhd_unit(drhd) { |
| 567 | int ret; |
| 568 | struct intel_iommu *iommu = drhd->iommu; |
| 569 | ret = dmar_enable_qi(iommu); |
| 570 | |
| 571 | if (ret) { |
| 572 | printk(KERN_ERR "DRHD %Lx: failed to enable queued, " |
| 573 | " invalidation, ecap %Lx, ret %d\n", |
| 574 | drhd->reg_base_addr, iommu->ecap, ret); |
| 575 | return -1; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * Setup Interrupt-remapping for all the DRHD's now. |
| 581 | */ |
| 582 | for_each_drhd_unit(drhd) { |
| 583 | struct intel_iommu *iommu = drhd->iommu; |
| 584 | |
| 585 | if (!ecap_ir_support(iommu->ecap)) |
| 586 | continue; |
| 587 | |
| 588 | if (setup_intr_remapping(iommu, eim)) |
| 589 | goto error; |
| 590 | |
| 591 | setup = 1; |
| 592 | } |
| 593 | |
| 594 | if (!setup) |
| 595 | goto error; |
| 596 | |
| 597 | intr_remapping_enabled = 1; |
| 598 | |
| 599 | return 0; |
| 600 | |
| 601 | error: |
| 602 | /* |
| 603 | * handle error condition gracefully here! |
| 604 | */ |
| 605 | return -1; |
| 606 | } |
Suresh Siddha | ad3ad3f | 2008-07-10 11:16:40 -0700 | [diff] [blame] | 607 | |
| 608 | static int ir_parse_ioapic_scope(struct acpi_dmar_header *header, |
| 609 | struct intel_iommu *iommu) |
| 610 | { |
| 611 | struct acpi_dmar_hardware_unit *drhd; |
| 612 | struct acpi_dmar_device_scope *scope; |
| 613 | void *start, *end; |
| 614 | |
| 615 | drhd = (struct acpi_dmar_hardware_unit *)header; |
| 616 | |
| 617 | start = (void *)(drhd + 1); |
| 618 | end = ((void *)drhd) + header->length; |
| 619 | |
| 620 | while (start < end) { |
| 621 | scope = start; |
| 622 | if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) { |
| 623 | if (ir_ioapic_num == MAX_IO_APICS) { |
| 624 | printk(KERN_WARNING "Exceeded Max IO APICS\n"); |
| 625 | return -1; |
| 626 | } |
| 627 | |
| 628 | printk(KERN_INFO "IOAPIC id %d under DRHD base" |
| 629 | " 0x%Lx\n", scope->enumeration_id, |
| 630 | drhd->address); |
| 631 | |
| 632 | ir_ioapic[ir_ioapic_num].iommu = iommu; |
| 633 | ir_ioapic[ir_ioapic_num].id = scope->enumeration_id; |
| 634 | ir_ioapic_num++; |
| 635 | } |
| 636 | start += scope->length; |
| 637 | } |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Finds the assocaition between IOAPIC's and its Interrupt-remapping |
| 644 | * hardware unit. |
| 645 | */ |
| 646 | int __init parse_ioapics_under_ir(void) |
| 647 | { |
| 648 | struct dmar_drhd_unit *drhd; |
| 649 | int ir_supported = 0; |
| 650 | |
| 651 | for_each_drhd_unit(drhd) { |
| 652 | struct intel_iommu *iommu = drhd->iommu; |
| 653 | |
| 654 | if (ecap_ir_support(iommu->ecap)) { |
| 655 | if (ir_parse_ioapic_scope(drhd->hdr, iommu)) |
| 656 | return -1; |
| 657 | |
| 658 | ir_supported = 1; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (ir_supported && ir_ioapic_num != nr_ioapics) { |
| 663 | printk(KERN_WARNING |
| 664 | "Not all IO-APIC's listed under remapping hardware\n"); |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | return ir_supported; |
| 669 | } |
Fenghua Yu | b24696b | 2009-03-27 14:22:44 -0700 | [diff] [blame] | 670 | |
| 671 | void disable_intr_remapping(void) |
| 672 | { |
| 673 | struct dmar_drhd_unit *drhd; |
| 674 | struct intel_iommu *iommu = NULL; |
| 675 | |
| 676 | /* |
| 677 | * Disable Interrupt-remapping for all the DRHD's now. |
| 678 | */ |
| 679 | for_each_iommu(iommu, drhd) { |
| 680 | if (!ecap_ir_support(iommu->ecap)) |
| 681 | continue; |
| 682 | |
| 683 | iommu_disable_intr_remapping(iommu); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | int reenable_intr_remapping(int eim) |
| 688 | { |
| 689 | struct dmar_drhd_unit *drhd; |
| 690 | int setup = 0; |
| 691 | struct intel_iommu *iommu = NULL; |
| 692 | |
| 693 | for_each_iommu(iommu, drhd) |
| 694 | if (iommu->qi) |
| 695 | dmar_reenable_qi(iommu); |
| 696 | |
| 697 | /* |
| 698 | * Setup Interrupt-remapping for all the DRHD's now. |
| 699 | */ |
| 700 | for_each_iommu(iommu, drhd) { |
| 701 | if (!ecap_ir_support(iommu->ecap)) |
| 702 | continue; |
| 703 | |
| 704 | /* Set up interrupt remapping for iommu.*/ |
| 705 | iommu_set_intr_remapping(iommu, eim); |
| 706 | setup = 1; |
| 707 | } |
| 708 | |
| 709 | if (!setup) |
| 710 | goto error; |
| 711 | |
| 712 | return 0; |
| 713 | |
| 714 | error: |
| 715 | /* |
| 716 | * handle error condition gracefully here! |
| 717 | */ |
| 718 | return -1; |
| 719 | } |
| 720 | |