iommu/vt-d: support extended root and context entries
[cascardo/linux.git] / drivers / iommu / intel_irq_remapping.c
1 #include <linux/interrupt.h>
2 #include <linux/dmar.h>
3 #include <linux/spinlock.h>
4 #include <linux/slab.h>
5 #include <linux/jiffies.h>
6 #include <linux/hpet.h>
7 #include <linux/pci.h>
8 #include <linux/irq.h>
9 #include <linux/intel-iommu.h>
10 #include <linux/acpi.h>
11 #include <asm/io_apic.h>
12 #include <asm/smp.h>
13 #include <asm/cpu.h>
14 #include <asm/irq_remapping.h>
15 #include <asm/pci-direct.h>
16 #include <asm/msidef.h>
17
18 #include "irq_remapping.h"
19
20 struct ioapic_scope {
21         struct intel_iommu *iommu;
22         unsigned int id;
23         unsigned int bus;       /* PCI bus number */
24         unsigned int devfn;     /* PCI devfn number */
25 };
26
27 struct hpet_scope {
28         struct intel_iommu *iommu;
29         u8 id;
30         unsigned int bus;
31         unsigned int devfn;
32 };
33
34 #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
35 #define IRTE_DEST(dest) ((x2apic_mode) ? dest : dest << 8)
36
37 static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
38 static struct hpet_scope ir_hpet[MAX_HPET_TBS];
39
40 /*
41  * Lock ordering:
42  * ->dmar_global_lock
43  *      ->irq_2_ir_lock
44  *              ->qi->q_lock
45  *      ->iommu->register_lock
46  * Note:
47  * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
48  * in single-threaded environment with interrupt disabled, so no need to tabke
49  * the dmar_global_lock.
50  */
51 static DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
52
53 static int __init parse_ioapics_under_ir(void);
54
55 static struct irq_2_iommu *irq_2_iommu(unsigned int irq)
56 {
57         struct irq_cfg *cfg = irq_cfg(irq);
58         return cfg ? &cfg->irq_2_iommu : NULL;
59 }
60
61 static int get_irte(int irq, struct irte *entry)
62 {
63         struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
64         unsigned long flags;
65         int index;
66
67         if (!entry || !irq_iommu)
68                 return -1;
69
70         raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
71
72         if (unlikely(!irq_iommu->iommu)) {
73                 raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
74                 return -1;
75         }
76
77         index = irq_iommu->irte_index + irq_iommu->sub_handle;
78         *entry = *(irq_iommu->iommu->ir_table->base + index);
79
80         raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
81         return 0;
82 }
83
84 static int alloc_irte(struct intel_iommu *iommu, int irq, u16 count)
85 {
86         struct ir_table *table = iommu->ir_table;
87         struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
88         struct irq_cfg *cfg = irq_cfg(irq);
89         unsigned int mask = 0;
90         unsigned long flags;
91         int index;
92
93         if (!count || !irq_iommu)
94                 return -1;
95
96         if (count > 1) {
97                 count = __roundup_pow_of_two(count);
98                 mask = ilog2(count);
99         }
100
101         if (mask > ecap_max_handle_mask(iommu->ecap)) {
102                 printk(KERN_ERR
103                        "Requested mask %x exceeds the max invalidation handle"
104                        " mask value %Lx\n", mask,
105                        ecap_max_handle_mask(iommu->ecap));
106                 return -1;
107         }
108
109         raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
110         index = bitmap_find_free_region(table->bitmap,
111                                         INTR_REMAP_TABLE_ENTRIES, mask);
112         if (index < 0) {
113                 pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
114         } else {
115                 cfg->remapped = 1;
116                 irq_iommu->iommu = iommu;
117                 irq_iommu->irte_index =  index;
118                 irq_iommu->sub_handle = 0;
119                 irq_iommu->irte_mask = mask;
120         }
121         raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
122
123         return index;
124 }
125
126 static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
127 {
128         struct qi_desc desc;
129
130         desc.low = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
131                    | QI_IEC_SELECTIVE;
132         desc.high = 0;
133
134         return qi_submit_sync(&desc, iommu);
135 }
136
137 static int map_irq_to_irte_handle(int irq, u16 *sub_handle)
138 {
139         struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
140         unsigned long flags;
141         int index;
142
143         if (!irq_iommu)
144                 return -1;
145
146         raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
147         *sub_handle = irq_iommu->sub_handle;
148         index = irq_iommu->irte_index;
149         raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
150         return index;
151 }
152
153 static int set_irte_irq(int irq, struct intel_iommu *iommu, u16 index, u16 subhandle)
154 {
155         struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
156         struct irq_cfg *cfg = irq_cfg(irq);
157         unsigned long flags;
158
159         if (!irq_iommu)
160                 return -1;
161
162         raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
163
164         cfg->remapped = 1;
165         irq_iommu->iommu = iommu;
166         irq_iommu->irte_index = index;
167         irq_iommu->sub_handle = subhandle;
168         irq_iommu->irte_mask = 0;
169
170         raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
171
172         return 0;
173 }
174
175 static int modify_irte(int irq, struct irte *irte_modified)
176 {
177         struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
178         struct intel_iommu *iommu;
179         unsigned long flags;
180         struct irte *irte;
181         int rc, index;
182
183         if (!irq_iommu)
184                 return -1;
185
186         raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
187
188         iommu = irq_iommu->iommu;
189
190         index = irq_iommu->irte_index + irq_iommu->sub_handle;
191         irte = &iommu->ir_table->base[index];
192
193         set_64bit(&irte->low, irte_modified->low);
194         set_64bit(&irte->high, irte_modified->high);
195         __iommu_flush_cache(iommu, irte, sizeof(*irte));
196
197         rc = qi_flush_iec(iommu, index, 0);
198         raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
199
200         return rc;
201 }
202
203 static struct intel_iommu *map_hpet_to_ir(u8 hpet_id)
204 {
205         int i;
206
207         for (i = 0; i < MAX_HPET_TBS; i++)
208                 if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
209                         return ir_hpet[i].iommu;
210         return NULL;
211 }
212
213 static struct intel_iommu *map_ioapic_to_ir(int apic)
214 {
215         int i;
216
217         for (i = 0; i < MAX_IO_APICS; i++)
218                 if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
219                         return ir_ioapic[i].iommu;
220         return NULL;
221 }
222
223 static struct intel_iommu *map_dev_to_ir(struct pci_dev *dev)
224 {
225         struct dmar_drhd_unit *drhd;
226
227         drhd = dmar_find_matched_drhd_unit(dev);
228         if (!drhd)
229                 return NULL;
230
231         return drhd->iommu;
232 }
233
234 static int clear_entries(struct irq_2_iommu *irq_iommu)
235 {
236         struct irte *start, *entry, *end;
237         struct intel_iommu *iommu;
238         int index;
239
240         if (irq_iommu->sub_handle)
241                 return 0;
242
243         iommu = irq_iommu->iommu;
244         index = irq_iommu->irte_index + irq_iommu->sub_handle;
245
246         start = iommu->ir_table->base + index;
247         end = start + (1 << irq_iommu->irte_mask);
248
249         for (entry = start; entry < end; entry++) {
250                 set_64bit(&entry->low, 0);
251                 set_64bit(&entry->high, 0);
252         }
253         bitmap_release_region(iommu->ir_table->bitmap, index,
254                               irq_iommu->irte_mask);
255
256         return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
257 }
258
259 static int free_irte(int irq)
260 {
261         struct irq_2_iommu *irq_iommu = irq_2_iommu(irq);
262         unsigned long flags;
263         int rc;
264
265         if (!irq_iommu)
266                 return -1;
267
268         raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
269
270         rc = clear_entries(irq_iommu);
271
272         irq_iommu->iommu = NULL;
273         irq_iommu->irte_index = 0;
274         irq_iommu->sub_handle = 0;
275         irq_iommu->irte_mask = 0;
276
277         raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
278
279         return rc;
280 }
281
282 /*
283  * source validation type
284  */
285 #define SVT_NO_VERIFY           0x0  /* no verification is required */
286 #define SVT_VERIFY_SID_SQ       0x1  /* verify using SID and SQ fields */
287 #define SVT_VERIFY_BUS          0x2  /* verify bus of request-id */
288
289 /*
290  * source-id qualifier
291  */
292 #define SQ_ALL_16       0x0  /* verify all 16 bits of request-id */
293 #define SQ_13_IGNORE_1  0x1  /* verify most significant 13 bits, ignore
294                               * the third least significant bit
295                               */
296 #define SQ_13_IGNORE_2  0x2  /* verify most significant 13 bits, ignore
297                               * the second and third least significant bits
298                               */
299 #define SQ_13_IGNORE_3  0x3  /* verify most significant 13 bits, ignore
300                               * the least three significant bits
301                               */
302
303 /*
304  * set SVT, SQ and SID fields of irte to verify
305  * source ids of interrupt requests
306  */
307 static void set_irte_sid(struct irte *irte, unsigned int svt,
308                          unsigned int sq, unsigned int sid)
309 {
310         if (disable_sourceid_checking)
311                 svt = SVT_NO_VERIFY;
312         irte->svt = svt;
313         irte->sq = sq;
314         irte->sid = sid;
315 }
316
317 static int set_ioapic_sid(struct irte *irte, int apic)
318 {
319         int i;
320         u16 sid = 0;
321
322         if (!irte)
323                 return -1;
324
325         down_read(&dmar_global_lock);
326         for (i = 0; i < MAX_IO_APICS; i++) {
327                 if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
328                         sid = (ir_ioapic[i].bus << 8) | ir_ioapic[i].devfn;
329                         break;
330                 }
331         }
332         up_read(&dmar_global_lock);
333
334         if (sid == 0) {
335                 pr_warning("Failed to set source-id of IOAPIC (%d)\n", apic);
336                 return -1;
337         }
338
339         set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
340
341         return 0;
342 }
343
344 static int set_hpet_sid(struct irte *irte, u8 id)
345 {
346         int i;
347         u16 sid = 0;
348
349         if (!irte)
350                 return -1;
351
352         down_read(&dmar_global_lock);
353         for (i = 0; i < MAX_HPET_TBS; i++) {
354                 if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
355                         sid = (ir_hpet[i].bus << 8) | ir_hpet[i].devfn;
356                         break;
357                 }
358         }
359         up_read(&dmar_global_lock);
360
361         if (sid == 0) {
362                 pr_warning("Failed to set source-id of HPET block (%d)\n", id);
363                 return -1;
364         }
365
366         /*
367          * Should really use SQ_ALL_16. Some platforms are broken.
368          * While we figure out the right quirks for these broken platforms, use
369          * SQ_13_IGNORE_3 for now.
370          */
371         set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
372
373         return 0;
374 }
375
376 struct set_msi_sid_data {
377         struct pci_dev *pdev;
378         u16 alias;
379 };
380
381 static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
382 {
383         struct set_msi_sid_data *data = opaque;
384
385         data->pdev = pdev;
386         data->alias = alias;
387
388         return 0;
389 }
390
391 static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
392 {
393         struct set_msi_sid_data data;
394
395         if (!irte || !dev)
396                 return -1;
397
398         pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
399
400         /*
401          * DMA alias provides us with a PCI device and alias.  The only case
402          * where the it will return an alias on a different bus than the
403          * device is the case of a PCIe-to-PCI bridge, where the alias is for
404          * the subordinate bus.  In this case we can only verify the bus.
405          *
406          * If the alias device is on a different bus than our source device
407          * then we have a topology based alias, use it.
408          *
409          * Otherwise, the alias is for a device DMA quirk and we cannot
410          * assume that MSI uses the same requester ID.  Therefore use the
411          * original device.
412          */
413         if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
414                 set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
415                              PCI_DEVID(PCI_BUS_NUM(data.alias),
416                                        dev->bus->number));
417         else if (data.pdev->bus->number != dev->bus->number)
418                 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
419         else
420                 set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
421                              PCI_DEVID(dev->bus->number, dev->devfn));
422
423         return 0;
424 }
425
426 static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
427 {
428         u64 addr;
429         u32 sts;
430         unsigned long flags;
431
432         addr = virt_to_phys((void *)iommu->ir_table->base);
433
434         raw_spin_lock_irqsave(&iommu->register_lock, flags);
435
436         dmar_writeq(iommu->reg + DMAR_IRTA_REG,
437                     (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
438
439         /* Set interrupt-remapping table pointer */
440         writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
441
442         IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
443                       readl, (sts & DMA_GSTS_IRTPS), sts);
444         raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
445
446         /*
447          * global invalidation of interrupt entry cache before enabling
448          * interrupt-remapping.
449          */
450         qi_global_iec(iommu);
451
452         raw_spin_lock_irqsave(&iommu->register_lock, flags);
453
454         /* Enable interrupt-remapping */
455         iommu->gcmd |= DMA_GCMD_IRE;
456         iommu->gcmd &= ~DMA_GCMD_CFI;  /* Block compatibility-format MSIs */
457         writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
458
459         IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
460                       readl, (sts & DMA_GSTS_IRES), sts);
461
462         /*
463          * With CFI clear in the Global Command register, we should be
464          * protected from dangerous (i.e. compatibility) interrupts
465          * regardless of x2apic status.  Check just to be sure.
466          */
467         if (sts & DMA_GSTS_CFIS)
468                 WARN(1, KERN_WARNING
469                         "Compatibility-format IRQs enabled despite intr remapping;\n"
470                         "you are vulnerable to IRQ injection.\n");
471
472         raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
473 }
474
475 static int intel_setup_irq_remapping(struct intel_iommu *iommu)
476 {
477         struct ir_table *ir_table;
478         struct page *pages;
479         unsigned long *bitmap;
480
481         if (iommu->ir_table)
482                 return 0;
483
484         ir_table = kzalloc(sizeof(struct ir_table), GFP_ATOMIC);
485         if (!ir_table)
486                 return -ENOMEM;
487
488         pages = alloc_pages_node(iommu->node, GFP_ATOMIC | __GFP_ZERO,
489                                  INTR_REMAP_PAGE_ORDER);
490
491         if (!pages) {
492                 pr_err("IR%d: failed to allocate pages of order %d\n",
493                        iommu->seq_id, INTR_REMAP_PAGE_ORDER);
494                 goto out_free_table;
495         }
496
497         bitmap = kcalloc(BITS_TO_LONGS(INTR_REMAP_TABLE_ENTRIES),
498                          sizeof(long), GFP_ATOMIC);
499         if (bitmap == NULL) {
500                 pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
501                 goto out_free_pages;
502         }
503
504         ir_table->base = page_address(pages);
505         ir_table->bitmap = bitmap;
506         iommu->ir_table = ir_table;
507         return 0;
508
509 out_free_pages:
510         __free_pages(pages, INTR_REMAP_PAGE_ORDER);
511 out_free_table:
512         kfree(ir_table);
513         return -ENOMEM;
514 }
515
516 static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
517 {
518         if (iommu && iommu->ir_table) {
519                 free_pages((unsigned long)iommu->ir_table->base,
520                            INTR_REMAP_PAGE_ORDER);
521                 kfree(iommu->ir_table->bitmap);
522                 kfree(iommu->ir_table);
523                 iommu->ir_table = NULL;
524         }
525 }
526
527 /*
528  * Disable Interrupt Remapping.
529  */
530 static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
531 {
532         unsigned long flags;
533         u32 sts;
534
535         if (!ecap_ir_support(iommu->ecap))
536                 return;
537
538         /*
539          * global invalidation of interrupt entry cache before disabling
540          * interrupt-remapping.
541          */
542         qi_global_iec(iommu);
543
544         raw_spin_lock_irqsave(&iommu->register_lock, flags);
545
546         sts = dmar_readq(iommu->reg + DMAR_GSTS_REG);
547         if (!(sts & DMA_GSTS_IRES))
548                 goto end;
549
550         iommu->gcmd &= ~DMA_GCMD_IRE;
551         writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
552
553         IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
554                       readl, !(sts & DMA_GSTS_IRES), sts);
555
556 end:
557         raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
558 }
559
560 static int __init dmar_x2apic_optout(void)
561 {
562         struct acpi_table_dmar *dmar;
563         dmar = (struct acpi_table_dmar *)dmar_tbl;
564         if (!dmar || no_x2apic_optout)
565                 return 0;
566         return dmar->flags & DMAR_X2APIC_OPT_OUT;
567 }
568
569 static int __init intel_irq_remapping_supported(void)
570 {
571         struct dmar_drhd_unit *drhd;
572         struct intel_iommu *iommu;
573
574         if (disable_irq_remap)
575                 return 0;
576         if (irq_remap_broken) {
577                 printk(KERN_WARNING
578                         "This system BIOS has enabled interrupt remapping\n"
579                         "on a chipset that contains an erratum making that\n"
580                         "feature unstable.  To maintain system stability\n"
581                         "interrupt remapping is being disabled.  Please\n"
582                         "contact your BIOS vendor for an update\n");
583                 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
584                 disable_irq_remap = 1;
585                 return 0;
586         }
587
588         if (!dmar_ir_support())
589                 return 0;
590
591         for_each_iommu(iommu, drhd)
592                 if (!ecap_ir_support(iommu->ecap))
593                         return 0;
594
595         return 1;
596 }
597
598 static int __init intel_enable_irq_remapping(void)
599 {
600         struct dmar_drhd_unit *drhd;
601         struct intel_iommu *iommu;
602         bool x2apic_present;
603         int setup = 0;
604         int eim = 0;
605
606         x2apic_present = x2apic_supported();
607
608         if (parse_ioapics_under_ir() != 1) {
609                 printk(KERN_INFO "Not enable interrupt remapping\n");
610                 goto error;
611         }
612
613         if (x2apic_present) {
614                 pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
615
616                 eim = !dmar_x2apic_optout();
617                 if (!eim)
618                         pr_info("x2apic is disabled because BIOS sets x2apic opt out bit. You can use 'intremap=no_x2apic_optout' to override the BIOS setting.\n");
619         }
620
621         for_each_iommu(iommu, drhd) {
622                 /*
623                  * If the queued invalidation is already initialized,
624                  * shouldn't disable it.
625                  */
626                 if (iommu->qi)
627                         continue;
628
629                 /*
630                  * Clear previous faults.
631                  */
632                 dmar_fault(-1, iommu);
633
634                 /*
635                  * Disable intr remapping and queued invalidation, if already
636                  * enabled prior to OS handover.
637                  */
638                 iommu_disable_irq_remapping(iommu);
639
640                 dmar_disable_qi(iommu);
641         }
642
643         /*
644          * check for the Interrupt-remapping support
645          */
646         for_each_iommu(iommu, drhd) {
647                 if (!ecap_ir_support(iommu->ecap))
648                         continue;
649
650                 if (eim && !ecap_eim_support(iommu->ecap)) {
651                         printk(KERN_INFO "DRHD %Lx: EIM not supported by DRHD, "
652                                " ecap %Lx\n", drhd->reg_base_addr, iommu->ecap);
653                         goto error;
654                 }
655         }
656
657         /*
658          * Enable queued invalidation for all the DRHD's.
659          */
660         for_each_iommu(iommu, drhd) {
661                 int ret = dmar_enable_qi(iommu);
662
663                 if (ret) {
664                         printk(KERN_ERR "DRHD %Lx: failed to enable queued, "
665                                " invalidation, ecap %Lx, ret %d\n",
666                                drhd->reg_base_addr, iommu->ecap, ret);
667                         goto error;
668                 }
669         }
670
671         /*
672          * Setup Interrupt-remapping for all the DRHD's now.
673          */
674         for_each_iommu(iommu, drhd) {
675                 if (!ecap_ir_support(iommu->ecap))
676                         continue;
677
678                 if (intel_setup_irq_remapping(iommu))
679                         goto error;
680
681                 iommu_set_irq_remapping(iommu, eim);
682                 setup = 1;
683         }
684
685         if (!setup)
686                 goto error;
687
688         irq_remapping_enabled = 1;
689
690         /*
691          * VT-d has a different layout for IO-APIC entries when
692          * interrupt remapping is enabled. So it needs a special routine
693          * to print IO-APIC entries for debugging purposes too.
694          */
695         x86_io_apic_ops.print_entries = intel_ir_io_apic_print_entries;
696
697         pr_info("Enabled IRQ remapping in %s mode\n", eim ? "x2apic" : "xapic");
698
699         return eim ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
700
701 error:
702         for_each_iommu(iommu, drhd)
703                 if (ecap_ir_support(iommu->ecap)) {
704                         iommu_disable_irq_remapping(iommu);
705                         intel_teardown_irq_remapping(iommu);
706                 }
707
708         if (x2apic_present)
709                 pr_warn("Failed to enable irq remapping.  You are vulnerable to irq-injection attacks.\n");
710
711         return -1;
712 }
713
714 static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
715                                    struct intel_iommu *iommu,
716                                    struct acpi_dmar_hardware_unit *drhd)
717 {
718         struct acpi_dmar_pci_path *path;
719         u8 bus;
720         int count, free = -1;
721
722         bus = scope->bus;
723         path = (struct acpi_dmar_pci_path *)(scope + 1);
724         count = (scope->length - sizeof(struct acpi_dmar_device_scope))
725                 / sizeof(struct acpi_dmar_pci_path);
726
727         while (--count > 0) {
728                 /*
729                  * Access PCI directly due to the PCI
730                  * subsystem isn't initialized yet.
731                  */
732                 bus = read_pci_config_byte(bus, path->device, path->function,
733                                            PCI_SECONDARY_BUS);
734                 path++;
735         }
736
737         for (count = 0; count < MAX_HPET_TBS; count++) {
738                 if (ir_hpet[count].iommu == iommu &&
739                     ir_hpet[count].id == scope->enumeration_id)
740                         return 0;
741                 else if (ir_hpet[count].iommu == NULL && free == -1)
742                         free = count;
743         }
744         if (free == -1) {
745                 pr_warn("Exceeded Max HPET blocks\n");
746                 return -ENOSPC;
747         }
748
749         ir_hpet[free].iommu = iommu;
750         ir_hpet[free].id    = scope->enumeration_id;
751         ir_hpet[free].bus   = bus;
752         ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
753         pr_info("HPET id %d under DRHD base 0x%Lx\n",
754                 scope->enumeration_id, drhd->address);
755
756         return 0;
757 }
758
759 static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
760                                      struct intel_iommu *iommu,
761                                      struct acpi_dmar_hardware_unit *drhd)
762 {
763         struct acpi_dmar_pci_path *path;
764         u8 bus;
765         int count, free = -1;
766
767         bus = scope->bus;
768         path = (struct acpi_dmar_pci_path *)(scope + 1);
769         count = (scope->length - sizeof(struct acpi_dmar_device_scope))
770                 / sizeof(struct acpi_dmar_pci_path);
771
772         while (--count > 0) {
773                 /*
774                  * Access PCI directly due to the PCI
775                  * subsystem isn't initialized yet.
776                  */
777                 bus = read_pci_config_byte(bus, path->device, path->function,
778                                            PCI_SECONDARY_BUS);
779                 path++;
780         }
781
782         for (count = 0; count < MAX_IO_APICS; count++) {
783                 if (ir_ioapic[count].iommu == iommu &&
784                     ir_ioapic[count].id == scope->enumeration_id)
785                         return 0;
786                 else if (ir_ioapic[count].iommu == NULL && free == -1)
787                         free = count;
788         }
789         if (free == -1) {
790                 pr_warn("Exceeded Max IO APICS\n");
791                 return -ENOSPC;
792         }
793
794         ir_ioapic[free].bus   = bus;
795         ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
796         ir_ioapic[free].iommu = iommu;
797         ir_ioapic[free].id    = scope->enumeration_id;
798         pr_info("IOAPIC id %d under DRHD base  0x%Lx IOMMU %d\n",
799                 scope->enumeration_id, drhd->address, iommu->seq_id);
800
801         return 0;
802 }
803
804 static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
805                                       struct intel_iommu *iommu)
806 {
807         int ret = 0;
808         struct acpi_dmar_hardware_unit *drhd;
809         struct acpi_dmar_device_scope *scope;
810         void *start, *end;
811
812         drhd = (struct acpi_dmar_hardware_unit *)header;
813         start = (void *)(drhd + 1);
814         end = ((void *)drhd) + header->length;
815
816         while (start < end && ret == 0) {
817                 scope = start;
818                 if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
819                         ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
820                 else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
821                         ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
822                 start += scope->length;
823         }
824
825         return ret;
826 }
827
828 static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
829 {
830         int i;
831
832         for (i = 0; i < MAX_HPET_TBS; i++)
833                 if (ir_hpet[i].iommu == iommu)
834                         ir_hpet[i].iommu = NULL;
835
836         for (i = 0; i < MAX_IO_APICS; i++)
837                 if (ir_ioapic[i].iommu == iommu)
838                         ir_ioapic[i].iommu = NULL;
839 }
840
841 /*
842  * Finds the assocaition between IOAPIC's and its Interrupt-remapping
843  * hardware unit.
844  */
845 static int __init parse_ioapics_under_ir(void)
846 {
847         struct dmar_drhd_unit *drhd;
848         struct intel_iommu *iommu;
849         int ir_supported = 0;
850         int ioapic_idx;
851
852         for_each_iommu(iommu, drhd)
853                 if (ecap_ir_support(iommu->ecap)) {
854                         if (ir_parse_ioapic_hpet_scope(drhd->hdr, iommu))
855                                 return -1;
856
857                         ir_supported = 1;
858                 }
859
860         if (!ir_supported)
861                 return 0;
862
863         for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
864                 int ioapic_id = mpc_ioapic_id(ioapic_idx);
865                 if (!map_ioapic_to_ir(ioapic_id)) {
866                         pr_err(FW_BUG "ioapic %d has no mapping iommu, "
867                                "interrupt remapping will be disabled\n",
868                                ioapic_id);
869                         return -1;
870                 }
871         }
872
873         return 1;
874 }
875
876 static int __init ir_dev_scope_init(void)
877 {
878         int ret;
879
880         if (!irq_remapping_enabled)
881                 return 0;
882
883         down_write(&dmar_global_lock);
884         ret = dmar_dev_scope_init();
885         up_write(&dmar_global_lock);
886
887         return ret;
888 }
889 rootfs_initcall(ir_dev_scope_init);
890
891 static void disable_irq_remapping(void)
892 {
893         struct dmar_drhd_unit *drhd;
894         struct intel_iommu *iommu = NULL;
895
896         /*
897          * Disable Interrupt-remapping for all the DRHD's now.
898          */
899         for_each_iommu(iommu, drhd) {
900                 if (!ecap_ir_support(iommu->ecap))
901                         continue;
902
903                 iommu_disable_irq_remapping(iommu);
904         }
905 }
906
907 static int reenable_irq_remapping(int eim)
908 {
909         struct dmar_drhd_unit *drhd;
910         int setup = 0;
911         struct intel_iommu *iommu = NULL;
912
913         for_each_iommu(iommu, drhd)
914                 if (iommu->qi)
915                         dmar_reenable_qi(iommu);
916
917         /*
918          * Setup Interrupt-remapping for all the DRHD's now.
919          */
920         for_each_iommu(iommu, drhd) {
921                 if (!ecap_ir_support(iommu->ecap))
922                         continue;
923
924                 /* Set up interrupt remapping for iommu.*/
925                 iommu_set_irq_remapping(iommu, eim);
926                 setup = 1;
927         }
928
929         if (!setup)
930                 goto error;
931
932         return 0;
933
934 error:
935         /*
936          * handle error condition gracefully here!
937          */
938         return -1;
939 }
940
941 static void prepare_irte(struct irte *irte, int vector,
942                          unsigned int dest)
943 {
944         memset(irte, 0, sizeof(*irte));
945
946         irte->present = 1;
947         irte->dst_mode = apic->irq_dest_mode;
948         /*
949          * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
950          * actual level or edge trigger will be setup in the IO-APIC
951          * RTE. This will help simplify level triggered irq migration.
952          * For more details, see the comments (in io_apic.c) explainig IO-APIC
953          * irq migration in the presence of interrupt-remapping.
954         */
955         irte->trigger_mode = 0;
956         irte->dlvry_mode = apic->irq_delivery_mode;
957         irte->vector = vector;
958         irte->dest_id = IRTE_DEST(dest);
959         irte->redir_hint = 1;
960 }
961
962 static int intel_setup_ioapic_entry(int irq,
963                                     struct IO_APIC_route_entry *route_entry,
964                                     unsigned int destination, int vector,
965                                     struct io_apic_irq_attr *attr)
966 {
967         int ioapic_id = mpc_ioapic_id(attr->ioapic);
968         struct intel_iommu *iommu;
969         struct IR_IO_APIC_route_entry *entry;
970         struct irte irte;
971         int index;
972
973         down_read(&dmar_global_lock);
974         iommu = map_ioapic_to_ir(ioapic_id);
975         if (!iommu) {
976                 pr_warn("No mapping iommu for ioapic %d\n", ioapic_id);
977                 index = -ENODEV;
978         } else {
979                 index = alloc_irte(iommu, irq, 1);
980                 if (index < 0) {
981                         pr_warn("Failed to allocate IRTE for ioapic %d\n",
982                                 ioapic_id);
983                         index = -ENOMEM;
984                 }
985         }
986         up_read(&dmar_global_lock);
987         if (index < 0)
988                 return index;
989
990         prepare_irte(&irte, vector, destination);
991
992         /* Set source-id of interrupt request */
993         set_ioapic_sid(&irte, ioapic_id);
994
995         modify_irte(irq, &irte);
996
997         apic_printk(APIC_VERBOSE, KERN_DEBUG "IOAPIC[%d]: "
998                 "Set IRTE entry (P:%d FPD:%d Dst_Mode:%d "
999                 "Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X "
1000                 "Avail:%X Vector:%02X Dest:%08X "
1001                 "SID:%04X SQ:%X SVT:%X)\n",
1002                 attr->ioapic, irte.present, irte.fpd, irte.dst_mode,
1003                 irte.redir_hint, irte.trigger_mode, irte.dlvry_mode,
1004                 irte.avail, irte.vector, irte.dest_id,
1005                 irte.sid, irte.sq, irte.svt);
1006
1007         entry = (struct IR_IO_APIC_route_entry *)route_entry;
1008         memset(entry, 0, sizeof(*entry));
1009
1010         entry->index2   = (index >> 15) & 0x1;
1011         entry->zero     = 0;
1012         entry->format   = 1;
1013         entry->index    = (index & 0x7fff);
1014         /*
1015          * IO-APIC RTE will be configured with virtual vector.
1016          * irq handler will do the explicit EOI to the io-apic.
1017          */
1018         entry->vector   = attr->ioapic_pin;
1019         entry->mask     = 0;                    /* enable IRQ */
1020         entry->trigger  = attr->trigger;
1021         entry->polarity = attr->polarity;
1022
1023         /* Mask level triggered irqs.
1024          * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1025          */
1026         if (attr->trigger)
1027                 entry->mask = 1;
1028
1029         return 0;
1030 }
1031
1032 /*
1033  * Migrate the IO-APIC irq in the presence of intr-remapping.
1034  *
1035  * For both level and edge triggered, irq migration is a simple atomic
1036  * update(of vector and cpu destination) of IRTE and flush the hardware cache.
1037  *
1038  * For level triggered, we eliminate the io-apic RTE modification (with the
1039  * updated vector information), by using a virtual vector (io-apic pin number).
1040  * Real vector that is used for interrupting cpu will be coming from
1041  * the interrupt-remapping table entry.
1042  *
1043  * As the migration is a simple atomic update of IRTE, the same mechanism
1044  * is used to migrate MSI irq's in the presence of interrupt-remapping.
1045  */
1046 static int
1047 intel_ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
1048                           bool force)
1049 {
1050         struct irq_cfg *cfg = irqd_cfg(data);
1051         unsigned int dest, irq = data->irq;
1052         struct irte irte;
1053         int err;
1054
1055         if (!config_enabled(CONFIG_SMP))
1056                 return -EINVAL;
1057
1058         if (!cpumask_intersects(mask, cpu_online_mask))
1059                 return -EINVAL;
1060
1061         if (get_irte(irq, &irte))
1062                 return -EBUSY;
1063
1064         err = assign_irq_vector(irq, cfg, mask);
1065         if (err)
1066                 return err;
1067
1068         err = apic->cpu_mask_to_apicid_and(cfg->domain, mask, &dest);
1069         if (err) {
1070                 if (assign_irq_vector(irq, cfg, data->affinity))
1071                         pr_err("Failed to recover vector for irq %d\n", irq);
1072                 return err;
1073         }
1074
1075         irte.vector = cfg->vector;
1076         irte.dest_id = IRTE_DEST(dest);
1077
1078         /*
1079          * Atomically updates the IRTE with the new destination, vector
1080          * and flushes the interrupt entry cache.
1081          */
1082         modify_irte(irq, &irte);
1083
1084         /*
1085          * After this point, all the interrupts will start arriving
1086          * at the new destination. So, time to cleanup the previous
1087          * vector allocation.
1088          */
1089         if (cfg->move_in_progress)
1090                 send_cleanup_vector(cfg);
1091
1092         cpumask_copy(data->affinity, mask);
1093         return 0;
1094 }
1095
1096 static void intel_compose_msi_msg(struct pci_dev *pdev,
1097                                   unsigned int irq, unsigned int dest,
1098                                   struct msi_msg *msg, u8 hpet_id)
1099 {
1100         struct irq_cfg *cfg;
1101         struct irte irte;
1102         u16 sub_handle = 0;
1103         int ir_index;
1104
1105         cfg = irq_cfg(irq);
1106
1107         ir_index = map_irq_to_irte_handle(irq, &sub_handle);
1108         BUG_ON(ir_index == -1);
1109
1110         prepare_irte(&irte, cfg->vector, dest);
1111
1112         /* Set source-id of interrupt request */
1113         if (pdev)
1114                 set_msi_sid(&irte, pdev);
1115         else
1116                 set_hpet_sid(&irte, hpet_id);
1117
1118         modify_irte(irq, &irte);
1119
1120         msg->address_hi = MSI_ADDR_BASE_HI;
1121         msg->data = sub_handle;
1122         msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
1123                           MSI_ADDR_IR_SHV |
1124                           MSI_ADDR_IR_INDEX1(ir_index) |
1125                           MSI_ADDR_IR_INDEX2(ir_index);
1126 }
1127
1128 /*
1129  * Map the PCI dev to the corresponding remapping hardware unit
1130  * and allocate 'nvec' consecutive interrupt-remapping table entries
1131  * in it.
1132  */
1133 static int intel_msi_alloc_irq(struct pci_dev *dev, int irq, int nvec)
1134 {
1135         struct intel_iommu *iommu;
1136         int index;
1137
1138         down_read(&dmar_global_lock);
1139         iommu = map_dev_to_ir(dev);
1140         if (!iommu) {
1141                 printk(KERN_ERR
1142                        "Unable to map PCI %s to iommu\n", pci_name(dev));
1143                 index = -ENOENT;
1144         } else {
1145                 index = alloc_irte(iommu, irq, nvec);
1146                 if (index < 0) {
1147                         printk(KERN_ERR
1148                                "Unable to allocate %d IRTE for PCI %s\n",
1149                                nvec, pci_name(dev));
1150                         index = -ENOSPC;
1151                 }
1152         }
1153         up_read(&dmar_global_lock);
1154
1155         return index;
1156 }
1157
1158 static int intel_msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
1159                                int index, int sub_handle)
1160 {
1161         struct intel_iommu *iommu;
1162         int ret = -ENOENT;
1163
1164         down_read(&dmar_global_lock);
1165         iommu = map_dev_to_ir(pdev);
1166         if (iommu) {
1167                 /*
1168                  * setup the mapping between the irq and the IRTE
1169                  * base index, the sub_handle pointing to the
1170                  * appropriate interrupt remap table entry.
1171                  */
1172                 set_irte_irq(irq, iommu, index, sub_handle);
1173                 ret = 0;
1174         }
1175         up_read(&dmar_global_lock);
1176
1177         return ret;
1178 }
1179
1180 static int intel_alloc_hpet_msi(unsigned int irq, unsigned int id)
1181 {
1182         int ret = -1;
1183         struct intel_iommu *iommu;
1184         int index;
1185
1186         down_read(&dmar_global_lock);
1187         iommu = map_hpet_to_ir(id);
1188         if (iommu) {
1189                 index = alloc_irte(iommu, irq, 1);
1190                 if (index >= 0)
1191                         ret = 0;
1192         }
1193         up_read(&dmar_global_lock);
1194
1195         return ret;
1196 }
1197
1198 struct irq_remap_ops intel_irq_remap_ops = {
1199         .supported              = intel_irq_remapping_supported,
1200         .prepare                = dmar_table_init,
1201         .enable                 = intel_enable_irq_remapping,
1202         .disable                = disable_irq_remapping,
1203         .reenable               = reenable_irq_remapping,
1204         .enable_faulting        = enable_drhd_fault_handling,
1205         .setup_ioapic_entry     = intel_setup_ioapic_entry,
1206         .set_affinity           = intel_ioapic_set_affinity,
1207         .free_irq               = free_irte,
1208         .compose_msi_msg        = intel_compose_msi_msg,
1209         .msi_alloc_irq          = intel_msi_alloc_irq,
1210         .msi_setup_irq          = intel_msi_setup_irq,
1211         .alloc_hpet_msi         = intel_alloc_hpet_msi,
1212 };
1213
1214 /*
1215  * Support of Interrupt Remapping Unit Hotplug
1216  */
1217 static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
1218 {
1219         int ret;
1220         int eim = x2apic_enabled();
1221
1222         if (eim && !ecap_eim_support(iommu->ecap)) {
1223                 pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
1224                         iommu->reg_phys, iommu->ecap);
1225                 return -ENODEV;
1226         }
1227
1228         if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
1229                 pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
1230                         iommu->reg_phys);
1231                 return -ENODEV;
1232         }
1233
1234         /* TODO: check all IOAPICs are covered by IOMMU */
1235
1236         /* Setup Interrupt-remapping now. */
1237         ret = intel_setup_irq_remapping(iommu);
1238         if (ret) {
1239                 pr_err("DRHD %Lx: failed to allocate resource\n",
1240                        iommu->reg_phys);
1241                 ir_remove_ioapic_hpet_scope(iommu);
1242                 return ret;
1243         }
1244
1245         if (!iommu->qi) {
1246                 /* Clear previous faults. */
1247                 dmar_fault(-1, iommu);
1248                 iommu_disable_irq_remapping(iommu);
1249                 dmar_disable_qi(iommu);
1250         }
1251
1252         /* Enable queued invalidation */
1253         ret = dmar_enable_qi(iommu);
1254         if (!ret) {
1255                 iommu_set_irq_remapping(iommu, eim);
1256         } else {
1257                 pr_err("DRHD %Lx: failed to enable queued invalidation, ecap %Lx, ret %d\n",
1258                        iommu->reg_phys, iommu->ecap, ret);
1259                 intel_teardown_irq_remapping(iommu);
1260                 ir_remove_ioapic_hpet_scope(iommu);
1261         }
1262
1263         return ret;
1264 }
1265
1266 int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
1267 {
1268         int ret = 0;
1269         struct intel_iommu *iommu = dmaru->iommu;
1270
1271         if (!irq_remapping_enabled)
1272                 return 0;
1273         if (iommu == NULL)
1274                 return -EINVAL;
1275         if (!ecap_ir_support(iommu->ecap))
1276                 return 0;
1277
1278         if (insert) {
1279                 if (!iommu->ir_table)
1280                         ret = dmar_ir_add(dmaru, iommu);
1281         } else {
1282                 if (iommu->ir_table) {
1283                         if (!bitmap_empty(iommu->ir_table->bitmap,
1284                                           INTR_REMAP_TABLE_ENTRIES)) {
1285                                 ret = -EBUSY;
1286                         } else {
1287                                 iommu_disable_irq_remapping(iommu);
1288                                 intel_teardown_irq_remapping(iommu);
1289                                 ir_remove_ioapic_hpet_scope(iommu);
1290                         }
1291                 }
1292         }
1293
1294         return ret;
1295 }