Intel IOMMU Pass Through Support
[safe/jmp/linux-2.6] / drivers / pci / intel-iommu.c
1 /*
2  * Copyright (c) 2006, Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Copyright (C) 2006-2008 Intel Corporation
18  * Author: Ashok Raj <ashok.raj@intel.com>
19  * Author: Shaohua Li <shaohua.li@intel.com>
20  * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
21  * Author: Fenghua Yu <fenghua.yu@intel.com>
22  */
23
24 #include <linux/init.h>
25 #include <linux/bitmap.h>
26 #include <linux/debugfs.h>
27 #include <linux/slab.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/spinlock.h>
31 #include <linux/pci.h>
32 #include <linux/dmar.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/mempool.h>
35 #include <linux/timer.h>
36 #include <linux/iova.h>
37 #include <linux/iommu.h>
38 #include <linux/intel-iommu.h>
39 #include <linux/sysdev.h>
40 #include <asm/cacheflush.h>
41 #include <asm/iommu.h>
42 #include "pci.h"
43
44 #define ROOT_SIZE               VTD_PAGE_SIZE
45 #define CONTEXT_SIZE            VTD_PAGE_SIZE
46
47 #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
48 #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
49
50 #define IOAPIC_RANGE_START      (0xfee00000)
51 #define IOAPIC_RANGE_END        (0xfeefffff)
52 #define IOVA_START_ADDR         (0x1000)
53
54 #define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
55
56 #define MAX_AGAW_WIDTH 64
57
58 #define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
59
60 #define IOVA_PFN(addr)          ((addr) >> PAGE_SHIFT)
61 #define DMA_32BIT_PFN           IOVA_PFN(DMA_BIT_MASK(32))
62 #define DMA_64BIT_PFN           IOVA_PFN(DMA_BIT_MASK(64))
63
64 /* global iommu list, set NULL for ignored DMAR units */
65 static struct intel_iommu **g_iommus;
66
67 static int rwbf_quirk;
68
69 /*
70  * 0: Present
71  * 1-11: Reserved
72  * 12-63: Context Ptr (12 - (haw-1))
73  * 64-127: Reserved
74  */
75 struct root_entry {
76         u64     val;
77         u64     rsvd1;
78 };
79 #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
80 static inline bool root_present(struct root_entry *root)
81 {
82         return (root->val & 1);
83 }
84 static inline void set_root_present(struct root_entry *root)
85 {
86         root->val |= 1;
87 }
88 static inline void set_root_value(struct root_entry *root, unsigned long value)
89 {
90         root->val |= value & VTD_PAGE_MASK;
91 }
92
93 static inline struct context_entry *
94 get_context_addr_from_root(struct root_entry *root)
95 {
96         return (struct context_entry *)
97                 (root_present(root)?phys_to_virt(
98                 root->val & VTD_PAGE_MASK) :
99                 NULL);
100 }
101
102 /*
103  * low 64 bits:
104  * 0: present
105  * 1: fault processing disable
106  * 2-3: translation type
107  * 12-63: address space root
108  * high 64 bits:
109  * 0-2: address width
110  * 3-6: aval
111  * 8-23: domain id
112  */
113 struct context_entry {
114         u64 lo;
115         u64 hi;
116 };
117
118 static inline bool context_present(struct context_entry *context)
119 {
120         return (context->lo & 1);
121 }
122 static inline void context_set_present(struct context_entry *context)
123 {
124         context->lo |= 1;
125 }
126
127 static inline void context_set_fault_enable(struct context_entry *context)
128 {
129         context->lo &= (((u64)-1) << 2) | 1;
130 }
131
132 static inline void context_set_translation_type(struct context_entry *context,
133                                                 unsigned long value)
134 {
135         context->lo &= (((u64)-1) << 4) | 3;
136         context->lo |= (value & 3) << 2;
137 }
138
139 static inline void context_set_address_root(struct context_entry *context,
140                                             unsigned long value)
141 {
142         context->lo |= value & VTD_PAGE_MASK;
143 }
144
145 static inline void context_set_address_width(struct context_entry *context,
146                                              unsigned long value)
147 {
148         context->hi |= value & 7;
149 }
150
151 static inline void context_set_domain_id(struct context_entry *context,
152                                          unsigned long value)
153 {
154         context->hi |= (value & ((1 << 16) - 1)) << 8;
155 }
156
157 static inline void context_clear_entry(struct context_entry *context)
158 {
159         context->lo = 0;
160         context->hi = 0;
161 }
162
163 /*
164  * 0: readable
165  * 1: writable
166  * 2-6: reserved
167  * 7: super page
168  * 8-10: available
169  * 11: snoop behavior
170  * 12-63: Host physcial address
171  */
172 struct dma_pte {
173         u64 val;
174 };
175
176 static inline void dma_clear_pte(struct dma_pte *pte)
177 {
178         pte->val = 0;
179 }
180
181 static inline void dma_set_pte_readable(struct dma_pte *pte)
182 {
183         pte->val |= DMA_PTE_READ;
184 }
185
186 static inline void dma_set_pte_writable(struct dma_pte *pte)
187 {
188         pte->val |= DMA_PTE_WRITE;
189 }
190
191 static inline void dma_set_pte_snp(struct dma_pte *pte)
192 {
193         pte->val |= DMA_PTE_SNP;
194 }
195
196 static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
197 {
198         pte->val = (pte->val & ~3) | (prot & 3);
199 }
200
201 static inline u64 dma_pte_addr(struct dma_pte *pte)
202 {
203         return (pte->val & VTD_PAGE_MASK);
204 }
205
206 static inline void dma_set_pte_addr(struct dma_pte *pte, u64 addr)
207 {
208         pte->val |= (addr & VTD_PAGE_MASK);
209 }
210
211 static inline bool dma_pte_present(struct dma_pte *pte)
212 {
213         return (pte->val & 3) != 0;
214 }
215
216 /* devices under the same p2p bridge are owned in one domain */
217 #define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
218
219 /* domain represents a virtual machine, more than one devices
220  * across iommus may be owned in one domain, e.g. kvm guest.
221  */
222 #define DOMAIN_FLAG_VIRTUAL_MACHINE     (1 << 1)
223
224 struct dmar_domain {
225         int     id;                     /* domain id */
226         unsigned long iommu_bmp;        /* bitmap of iommus this domain uses*/
227
228         struct list_head devices;       /* all devices' list */
229         struct iova_domain iovad;       /* iova's that belong to this domain */
230
231         struct dma_pte  *pgd;           /* virtual address */
232         spinlock_t      mapping_lock;   /* page table lock */
233         int             gaw;            /* max guest address width */
234
235         /* adjusted guest address width, 0 is level 2 30-bit */
236         int             agaw;
237
238         int             flags;          /* flags to find out type of domain */
239
240         int             iommu_coherency;/* indicate coherency of iommu access */
241         int             iommu_snooping; /* indicate snooping control feature*/
242         int             iommu_count;    /* reference count of iommu */
243         spinlock_t      iommu_lock;     /* protect iommu set in domain */
244         u64             max_addr;       /* maximum mapped address */
245 };
246
247 /* PCI domain-device relationship */
248 struct device_domain_info {
249         struct list_head link;  /* link to domain siblings */
250         struct list_head global; /* link to global list */
251         int segment;            /* PCI domain */
252         u8 bus;                 /* PCI bus number */
253         u8 devfn;               /* PCI devfn number */
254         struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
255         struct dmar_domain *domain; /* pointer to domain */
256 };
257
258 static void flush_unmaps_timeout(unsigned long data);
259
260 DEFINE_TIMER(unmap_timer,  flush_unmaps_timeout, 0, 0);
261
262 #define HIGH_WATER_MARK 250
263 struct deferred_flush_tables {
264         int next;
265         struct iova *iova[HIGH_WATER_MARK];
266         struct dmar_domain *domain[HIGH_WATER_MARK];
267 };
268
269 static struct deferred_flush_tables *deferred_flush;
270
271 /* bitmap for indexing intel_iommus */
272 static int g_num_of_iommus;
273
274 static DEFINE_SPINLOCK(async_umap_flush_lock);
275 static LIST_HEAD(unmaps_to_do);
276
277 static int timer_on;
278 static long list_size;
279
280 static void domain_remove_dev_info(struct dmar_domain *domain);
281
282 #ifdef CONFIG_DMAR_DEFAULT_ON
283 int dmar_disabled = 0;
284 #else
285 int dmar_disabled = 1;
286 #endif /*CONFIG_DMAR_DEFAULT_ON*/
287
288 static int __initdata dmar_map_gfx = 1;
289 static int dmar_forcedac;
290 static int intel_iommu_strict;
291 int iommu_pass_through;
292
293 #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
294 static DEFINE_SPINLOCK(device_domain_lock);
295 static LIST_HEAD(device_domain_list);
296
297 static struct iommu_ops intel_iommu_ops;
298
299 static int __init intel_iommu_setup(char *str)
300 {
301         if (!str)
302                 return -EINVAL;
303         while (*str) {
304                 if (!strncmp(str, "on", 2)) {
305                         dmar_disabled = 0;
306                         printk(KERN_INFO "Intel-IOMMU: enabled\n");
307                 } else if (!strncmp(str, "off", 3)) {
308                         dmar_disabled = 1;
309                         printk(KERN_INFO "Intel-IOMMU: disabled\n");
310                 } else if (!strncmp(str, "igfx_off", 8)) {
311                         dmar_map_gfx = 0;
312                         printk(KERN_INFO
313                                 "Intel-IOMMU: disable GFX device mapping\n");
314                 } else if (!strncmp(str, "forcedac", 8)) {
315                         printk(KERN_INFO
316                                 "Intel-IOMMU: Forcing DAC for PCI devices\n");
317                         dmar_forcedac = 1;
318                 } else if (!strncmp(str, "strict", 6)) {
319                         printk(KERN_INFO
320                                 "Intel-IOMMU: disable batched IOTLB flush\n");
321                         intel_iommu_strict = 1;
322                 }
323
324                 str += strcspn(str, ",");
325                 while (*str == ',')
326                         str++;
327         }
328         return 0;
329 }
330 __setup("intel_iommu=", intel_iommu_setup);
331
332 static struct kmem_cache *iommu_domain_cache;
333 static struct kmem_cache *iommu_devinfo_cache;
334 static struct kmem_cache *iommu_iova_cache;
335
336 static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
337 {
338         unsigned int flags;
339         void *vaddr;
340
341         /* trying to avoid low memory issues */
342         flags = current->flags & PF_MEMALLOC;
343         current->flags |= PF_MEMALLOC;
344         vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
345         current->flags &= (~PF_MEMALLOC | flags);
346         return vaddr;
347 }
348
349
350 static inline void *alloc_pgtable_page(void)
351 {
352         unsigned int flags;
353         void *vaddr;
354
355         /* trying to avoid low memory issues */
356         flags = current->flags & PF_MEMALLOC;
357         current->flags |= PF_MEMALLOC;
358         vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
359         current->flags &= (~PF_MEMALLOC | flags);
360         return vaddr;
361 }
362
363 static inline void free_pgtable_page(void *vaddr)
364 {
365         free_page((unsigned long)vaddr);
366 }
367
368 static inline void *alloc_domain_mem(void)
369 {
370         return iommu_kmem_cache_alloc(iommu_domain_cache);
371 }
372
373 static void free_domain_mem(void *vaddr)
374 {
375         kmem_cache_free(iommu_domain_cache, vaddr);
376 }
377
378 static inline void * alloc_devinfo_mem(void)
379 {
380         return iommu_kmem_cache_alloc(iommu_devinfo_cache);
381 }
382
383 static inline void free_devinfo_mem(void *vaddr)
384 {
385         kmem_cache_free(iommu_devinfo_cache, vaddr);
386 }
387
388 struct iova *alloc_iova_mem(void)
389 {
390         return iommu_kmem_cache_alloc(iommu_iova_cache);
391 }
392
393 void free_iova_mem(struct iova *iova)
394 {
395         kmem_cache_free(iommu_iova_cache, iova);
396 }
397
398
399 static inline int width_to_agaw(int width);
400
401 static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
402 {
403         unsigned long sagaw;
404         int agaw = -1;
405
406         sagaw = cap_sagaw(iommu->cap);
407         for (agaw = width_to_agaw(max_gaw);
408              agaw >= 0; agaw--) {
409                 if (test_bit(agaw, &sagaw))
410                         break;
411         }
412
413         return agaw;
414 }
415
416 /*
417  * Calculate max SAGAW for each iommu.
418  */
419 int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
420 {
421         return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
422 }
423
424 /*
425  * calculate agaw for each iommu.
426  * "SAGAW" may be different across iommus, use a default agaw, and
427  * get a supported less agaw for iommus that don't support the default agaw.
428  */
429 int iommu_calculate_agaw(struct intel_iommu *iommu)
430 {
431         return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
432 }
433
434 /* in native case, each domain is related to only one iommu */
435 static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
436 {
437         int iommu_id;
438
439         BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
440
441         iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
442         if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
443                 return NULL;
444
445         return g_iommus[iommu_id];
446 }
447
448 static void domain_update_iommu_coherency(struct dmar_domain *domain)
449 {
450         int i;
451
452         domain->iommu_coherency = 1;
453
454         i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
455         for (; i < g_num_of_iommus; ) {
456                 if (!ecap_coherent(g_iommus[i]->ecap)) {
457                         domain->iommu_coherency = 0;
458                         break;
459                 }
460                 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
461         }
462 }
463
464 static void domain_update_iommu_snooping(struct dmar_domain *domain)
465 {
466         int i;
467
468         domain->iommu_snooping = 1;
469
470         i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
471         for (; i < g_num_of_iommus; ) {
472                 if (!ecap_sc_support(g_iommus[i]->ecap)) {
473                         domain->iommu_snooping = 0;
474                         break;
475                 }
476                 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
477         }
478 }
479
480 /* Some capabilities may be different across iommus */
481 static void domain_update_iommu_cap(struct dmar_domain *domain)
482 {
483         domain_update_iommu_coherency(domain);
484         domain_update_iommu_snooping(domain);
485 }
486
487 static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
488 {
489         struct dmar_drhd_unit *drhd = NULL;
490         int i;
491
492         for_each_drhd_unit(drhd) {
493                 if (drhd->ignored)
494                         continue;
495                 if (segment != drhd->segment)
496                         continue;
497
498                 for (i = 0; i < drhd->devices_cnt; i++) {
499                         if (drhd->devices[i] &&
500                             drhd->devices[i]->bus->number == bus &&
501                             drhd->devices[i]->devfn == devfn)
502                                 return drhd->iommu;
503                         if (drhd->devices[i] &&
504                             drhd->devices[i]->subordinate &&
505                             drhd->devices[i]->subordinate->number <= bus &&
506                             drhd->devices[i]->subordinate->subordinate >= bus)
507                                 return drhd->iommu;
508                 }
509
510                 if (drhd->include_all)
511                         return drhd->iommu;
512         }
513
514         return NULL;
515 }
516
517 static void domain_flush_cache(struct dmar_domain *domain,
518                                void *addr, int size)
519 {
520         if (!domain->iommu_coherency)
521                 clflush_cache_range(addr, size);
522 }
523
524 /* Gets context entry for a given bus and devfn */
525 static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
526                 u8 bus, u8 devfn)
527 {
528         struct root_entry *root;
529         struct context_entry *context;
530         unsigned long phy_addr;
531         unsigned long flags;
532
533         spin_lock_irqsave(&iommu->lock, flags);
534         root = &iommu->root_entry[bus];
535         context = get_context_addr_from_root(root);
536         if (!context) {
537                 context = (struct context_entry *)alloc_pgtable_page();
538                 if (!context) {
539                         spin_unlock_irqrestore(&iommu->lock, flags);
540                         return NULL;
541                 }
542                 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
543                 phy_addr = virt_to_phys((void *)context);
544                 set_root_value(root, phy_addr);
545                 set_root_present(root);
546                 __iommu_flush_cache(iommu, root, sizeof(*root));
547         }
548         spin_unlock_irqrestore(&iommu->lock, flags);
549         return &context[devfn];
550 }
551
552 static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
553 {
554         struct root_entry *root;
555         struct context_entry *context;
556         int ret;
557         unsigned long flags;
558
559         spin_lock_irqsave(&iommu->lock, flags);
560         root = &iommu->root_entry[bus];
561         context = get_context_addr_from_root(root);
562         if (!context) {
563                 ret = 0;
564                 goto out;
565         }
566         ret = context_present(&context[devfn]);
567 out:
568         spin_unlock_irqrestore(&iommu->lock, flags);
569         return ret;
570 }
571
572 static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
573 {
574         struct root_entry *root;
575         struct context_entry *context;
576         unsigned long flags;
577
578         spin_lock_irqsave(&iommu->lock, flags);
579         root = &iommu->root_entry[bus];
580         context = get_context_addr_from_root(root);
581         if (context) {
582                 context_clear_entry(&context[devfn]);
583                 __iommu_flush_cache(iommu, &context[devfn], \
584                         sizeof(*context));
585         }
586         spin_unlock_irqrestore(&iommu->lock, flags);
587 }
588
589 static void free_context_table(struct intel_iommu *iommu)
590 {
591         struct root_entry *root;
592         int i;
593         unsigned long flags;
594         struct context_entry *context;
595
596         spin_lock_irqsave(&iommu->lock, flags);
597         if (!iommu->root_entry) {
598                 goto out;
599         }
600         for (i = 0; i < ROOT_ENTRY_NR; i++) {
601                 root = &iommu->root_entry[i];
602                 context = get_context_addr_from_root(root);
603                 if (context)
604                         free_pgtable_page(context);
605         }
606         free_pgtable_page(iommu->root_entry);
607         iommu->root_entry = NULL;
608 out:
609         spin_unlock_irqrestore(&iommu->lock, flags);
610 }
611
612 /* page table handling */
613 #define LEVEL_STRIDE            (9)
614 #define LEVEL_MASK              (((u64)1 << LEVEL_STRIDE) - 1)
615
616 static inline int agaw_to_level(int agaw)
617 {
618         return agaw + 2;
619 }
620
621 static inline int agaw_to_width(int agaw)
622 {
623         return 30 + agaw * LEVEL_STRIDE;
624
625 }
626
627 static inline int width_to_agaw(int width)
628 {
629         return (width - 30) / LEVEL_STRIDE;
630 }
631
632 static inline unsigned int level_to_offset_bits(int level)
633 {
634         return (12 + (level - 1) * LEVEL_STRIDE);
635 }
636
637 static inline int address_level_offset(u64 addr, int level)
638 {
639         return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
640 }
641
642 static inline u64 level_mask(int level)
643 {
644         return ((u64)-1 << level_to_offset_bits(level));
645 }
646
647 static inline u64 level_size(int level)
648 {
649         return ((u64)1 << level_to_offset_bits(level));
650 }
651
652 static inline u64 align_to_level(u64 addr, int level)
653 {
654         return ((addr + level_size(level) - 1) & level_mask(level));
655 }
656
657 static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
658 {
659         int addr_width = agaw_to_width(domain->agaw);
660         struct dma_pte *parent, *pte = NULL;
661         int level = agaw_to_level(domain->agaw);
662         int offset;
663         unsigned long flags;
664
665         BUG_ON(!domain->pgd);
666
667         addr &= (((u64)1) << addr_width) - 1;
668         parent = domain->pgd;
669
670         spin_lock_irqsave(&domain->mapping_lock, flags);
671         while (level > 0) {
672                 void *tmp_page;
673
674                 offset = address_level_offset(addr, level);
675                 pte = &parent[offset];
676                 if (level == 1)
677                         break;
678
679                 if (!dma_pte_present(pte)) {
680                         tmp_page = alloc_pgtable_page();
681
682                         if (!tmp_page) {
683                                 spin_unlock_irqrestore(&domain->mapping_lock,
684                                         flags);
685                                 return NULL;
686                         }
687                         domain_flush_cache(domain, tmp_page, PAGE_SIZE);
688                         dma_set_pte_addr(pte, virt_to_phys(tmp_page));
689                         /*
690                          * high level table always sets r/w, last level page
691                          * table control read/write
692                          */
693                         dma_set_pte_readable(pte);
694                         dma_set_pte_writable(pte);
695                         domain_flush_cache(domain, pte, sizeof(*pte));
696                 }
697                 parent = phys_to_virt(dma_pte_addr(pte));
698                 level--;
699         }
700
701         spin_unlock_irqrestore(&domain->mapping_lock, flags);
702         return pte;
703 }
704
705 /* return address's pte at specific level */
706 static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
707                 int level)
708 {
709         struct dma_pte *parent, *pte = NULL;
710         int total = agaw_to_level(domain->agaw);
711         int offset;
712
713         parent = domain->pgd;
714         while (level <= total) {
715                 offset = address_level_offset(addr, total);
716                 pte = &parent[offset];
717                 if (level == total)
718                         return pte;
719
720                 if (!dma_pte_present(pte))
721                         break;
722                 parent = phys_to_virt(dma_pte_addr(pte));
723                 total--;
724         }
725         return NULL;
726 }
727
728 /* clear one page's page table */
729 static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
730 {
731         struct dma_pte *pte = NULL;
732
733         /* get last level pte */
734         pte = dma_addr_level_pte(domain, addr, 1);
735
736         if (pte) {
737                 dma_clear_pte(pte);
738                 domain_flush_cache(domain, pte, sizeof(*pte));
739         }
740 }
741
742 /* clear last level pte, a tlb flush should be followed */
743 static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
744 {
745         int addr_width = agaw_to_width(domain->agaw);
746         int npages;
747
748         start &= (((u64)1) << addr_width) - 1;
749         end &= (((u64)1) << addr_width) - 1;
750         /* in case it's partial page */
751         start &= PAGE_MASK;
752         end = PAGE_ALIGN(end);
753         npages = (end - start) / VTD_PAGE_SIZE;
754
755         /* we don't need lock here, nobody else touches the iova range */
756         while (npages--) {
757                 dma_pte_clear_one(domain, start);
758                 start += VTD_PAGE_SIZE;
759         }
760 }
761
762 /* free page table pages. last level pte should already be cleared */
763 static void dma_pte_free_pagetable(struct dmar_domain *domain,
764         u64 start, u64 end)
765 {
766         int addr_width = agaw_to_width(domain->agaw);
767         struct dma_pte *pte;
768         int total = agaw_to_level(domain->agaw);
769         int level;
770         u64 tmp;
771
772         start &= (((u64)1) << addr_width) - 1;
773         end &= (((u64)1) << addr_width) - 1;
774
775         /* we don't need lock here, nobody else touches the iova range */
776         level = 2;
777         while (level <= total) {
778                 tmp = align_to_level(start, level);
779                 if (tmp >= end || (tmp + level_size(level) > end))
780                         return;
781
782                 while (tmp < end) {
783                         pte = dma_addr_level_pte(domain, tmp, level);
784                         if (pte) {
785                                 free_pgtable_page(
786                                         phys_to_virt(dma_pte_addr(pte)));
787                                 dma_clear_pte(pte);
788                                 domain_flush_cache(domain, pte, sizeof(*pte));
789                         }
790                         tmp += level_size(level);
791                 }
792                 level++;
793         }
794         /* free pgd */
795         if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
796                 free_pgtable_page(domain->pgd);
797                 domain->pgd = NULL;
798         }
799 }
800
801 /* iommu handling */
802 static int iommu_alloc_root_entry(struct intel_iommu *iommu)
803 {
804         struct root_entry *root;
805         unsigned long flags;
806
807         root = (struct root_entry *)alloc_pgtable_page();
808         if (!root)
809                 return -ENOMEM;
810
811         __iommu_flush_cache(iommu, root, ROOT_SIZE);
812
813         spin_lock_irqsave(&iommu->lock, flags);
814         iommu->root_entry = root;
815         spin_unlock_irqrestore(&iommu->lock, flags);
816
817         return 0;
818 }
819
820 static void iommu_set_root_entry(struct intel_iommu *iommu)
821 {
822         void *addr;
823         u32 cmd, sts;
824         unsigned long flag;
825
826         addr = iommu->root_entry;
827
828         spin_lock_irqsave(&iommu->register_lock, flag);
829         dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
830
831         cmd = iommu->gcmd | DMA_GCMD_SRTP;
832         writel(cmd, iommu->reg + DMAR_GCMD_REG);
833
834         /* Make sure hardware complete it */
835         IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
836                 readl, (sts & DMA_GSTS_RTPS), sts);
837
838         spin_unlock_irqrestore(&iommu->register_lock, flag);
839 }
840
841 static void iommu_flush_write_buffer(struct intel_iommu *iommu)
842 {
843         u32 val;
844         unsigned long flag;
845
846         if (!rwbf_quirk && !cap_rwbf(iommu->cap))
847                 return;
848         val = iommu->gcmd | DMA_GCMD_WBF;
849
850         spin_lock_irqsave(&iommu->register_lock, flag);
851         writel(val, iommu->reg + DMAR_GCMD_REG);
852
853         /* Make sure hardware complete it */
854         IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
855                         readl, (!(val & DMA_GSTS_WBFS)), val);
856
857         spin_unlock_irqrestore(&iommu->register_lock, flag);
858 }
859
860 /* return value determine if we need a write buffer flush */
861 static int __iommu_flush_context(struct intel_iommu *iommu,
862         u16 did, u16 source_id, u8 function_mask, u64 type,
863         int non_present_entry_flush)
864 {
865         u64 val = 0;
866         unsigned long flag;
867
868         /*
869          * In the non-present entry flush case, if hardware doesn't cache
870          * non-present entry we do nothing and if hardware cache non-present
871          * entry, we flush entries of domain 0 (the domain id is used to cache
872          * any non-present entries)
873          */
874         if (non_present_entry_flush) {
875                 if (!cap_caching_mode(iommu->cap))
876                         return 1;
877                 else
878                         did = 0;
879         }
880
881         switch (type) {
882         case DMA_CCMD_GLOBAL_INVL:
883                 val = DMA_CCMD_GLOBAL_INVL;
884                 break;
885         case DMA_CCMD_DOMAIN_INVL:
886                 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
887                 break;
888         case DMA_CCMD_DEVICE_INVL:
889                 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
890                         | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
891                 break;
892         default:
893                 BUG();
894         }
895         val |= DMA_CCMD_ICC;
896
897         spin_lock_irqsave(&iommu->register_lock, flag);
898         dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
899
900         /* Make sure hardware complete it */
901         IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
902                 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
903
904         spin_unlock_irqrestore(&iommu->register_lock, flag);
905
906         /* flush context entry will implicitly flush write buffer */
907         return 0;
908 }
909
910 /* return value determine if we need a write buffer flush */
911 static int __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
912         u64 addr, unsigned int size_order, u64 type,
913         int non_present_entry_flush)
914 {
915         int tlb_offset = ecap_iotlb_offset(iommu->ecap);
916         u64 val = 0, val_iva = 0;
917         unsigned long flag;
918
919         /*
920          * In the non-present entry flush case, if hardware doesn't cache
921          * non-present entry we do nothing and if hardware cache non-present
922          * entry, we flush entries of domain 0 (the domain id is used to cache
923          * any non-present entries)
924          */
925         if (non_present_entry_flush) {
926                 if (!cap_caching_mode(iommu->cap))
927                         return 1;
928                 else
929                         did = 0;
930         }
931
932         switch (type) {
933         case DMA_TLB_GLOBAL_FLUSH:
934                 /* global flush doesn't need set IVA_REG */
935                 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
936                 break;
937         case DMA_TLB_DSI_FLUSH:
938                 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
939                 break;
940         case DMA_TLB_PSI_FLUSH:
941                 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
942                 /* Note: always flush non-leaf currently */
943                 val_iva = size_order | addr;
944                 break;
945         default:
946                 BUG();
947         }
948         /* Note: set drain read/write */
949 #if 0
950         /*
951          * This is probably to be super secure.. Looks like we can
952          * ignore it without any impact.
953          */
954         if (cap_read_drain(iommu->cap))
955                 val |= DMA_TLB_READ_DRAIN;
956 #endif
957         if (cap_write_drain(iommu->cap))
958                 val |= DMA_TLB_WRITE_DRAIN;
959
960         spin_lock_irqsave(&iommu->register_lock, flag);
961         /* Note: Only uses first TLB reg currently */
962         if (val_iva)
963                 dmar_writeq(iommu->reg + tlb_offset, val_iva);
964         dmar_writeq(iommu->reg + tlb_offset + 8, val);
965
966         /* Make sure hardware complete it */
967         IOMMU_WAIT_OP(iommu, tlb_offset + 8,
968                 dmar_readq, (!(val & DMA_TLB_IVT)), val);
969
970         spin_unlock_irqrestore(&iommu->register_lock, flag);
971
972         /* check IOTLB invalidation granularity */
973         if (DMA_TLB_IAIG(val) == 0)
974                 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
975         if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
976                 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
977                         (unsigned long long)DMA_TLB_IIRG(type),
978                         (unsigned long long)DMA_TLB_IAIG(val));
979         /* flush iotlb entry will implicitly flush write buffer */
980         return 0;
981 }
982
983 static int iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
984         u64 addr, unsigned int pages, int non_present_entry_flush)
985 {
986         unsigned int mask;
987
988         BUG_ON(addr & (~VTD_PAGE_MASK));
989         BUG_ON(pages == 0);
990
991         /* Fallback to domain selective flush if no PSI support */
992         if (!cap_pgsel_inv(iommu->cap))
993                 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
994                                                 DMA_TLB_DSI_FLUSH,
995                                                 non_present_entry_flush);
996
997         /*
998          * PSI requires page size to be 2 ^ x, and the base address is naturally
999          * aligned to the size
1000          */
1001         mask = ilog2(__roundup_pow_of_two(pages));
1002         /* Fallback to domain selective flush if size is too big */
1003         if (mask > cap_max_amask_val(iommu->cap))
1004                 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
1005                         DMA_TLB_DSI_FLUSH, non_present_entry_flush);
1006
1007         return iommu->flush.flush_iotlb(iommu, did, addr, mask,
1008                                         DMA_TLB_PSI_FLUSH,
1009                                         non_present_entry_flush);
1010 }
1011
1012 static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1013 {
1014         u32 pmen;
1015         unsigned long flags;
1016
1017         spin_lock_irqsave(&iommu->register_lock, flags);
1018         pmen = readl(iommu->reg + DMAR_PMEN_REG);
1019         pmen &= ~DMA_PMEN_EPM;
1020         writel(pmen, iommu->reg + DMAR_PMEN_REG);
1021
1022         /* wait for the protected region status bit to clear */
1023         IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1024                 readl, !(pmen & DMA_PMEN_PRS), pmen);
1025
1026         spin_unlock_irqrestore(&iommu->register_lock, flags);
1027 }
1028
1029 static int iommu_enable_translation(struct intel_iommu *iommu)
1030 {
1031         u32 sts;
1032         unsigned long flags;
1033
1034         spin_lock_irqsave(&iommu->register_lock, flags);
1035         writel(iommu->gcmd|DMA_GCMD_TE, iommu->reg + DMAR_GCMD_REG);
1036
1037         /* Make sure hardware complete it */
1038         IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1039                 readl, (sts & DMA_GSTS_TES), sts);
1040
1041         iommu->gcmd |= DMA_GCMD_TE;
1042         spin_unlock_irqrestore(&iommu->register_lock, flags);
1043         return 0;
1044 }
1045
1046 static int iommu_disable_translation(struct intel_iommu *iommu)
1047 {
1048         u32 sts;
1049         unsigned long flag;
1050
1051         spin_lock_irqsave(&iommu->register_lock, flag);
1052         iommu->gcmd &= ~DMA_GCMD_TE;
1053         writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1054
1055         /* Make sure hardware complete it */
1056         IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1057                 readl, (!(sts & DMA_GSTS_TES)), sts);
1058
1059         spin_unlock_irqrestore(&iommu->register_lock, flag);
1060         return 0;
1061 }
1062
1063
1064 static int iommu_init_domains(struct intel_iommu *iommu)
1065 {
1066         unsigned long ndomains;
1067         unsigned long nlongs;
1068
1069         ndomains = cap_ndoms(iommu->cap);
1070         pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1071         nlongs = BITS_TO_LONGS(ndomains);
1072
1073         /* TBD: there might be 64K domains,
1074          * consider other allocation for future chip
1075          */
1076         iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1077         if (!iommu->domain_ids) {
1078                 printk(KERN_ERR "Allocating domain id array failed\n");
1079                 return -ENOMEM;
1080         }
1081         iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1082                         GFP_KERNEL);
1083         if (!iommu->domains) {
1084                 printk(KERN_ERR "Allocating domain array failed\n");
1085                 kfree(iommu->domain_ids);
1086                 return -ENOMEM;
1087         }
1088
1089         spin_lock_init(&iommu->lock);
1090
1091         /*
1092          * if Caching mode is set, then invalid translations are tagged
1093          * with domainid 0. Hence we need to pre-allocate it.
1094          */
1095         if (cap_caching_mode(iommu->cap))
1096                 set_bit(0, iommu->domain_ids);
1097         return 0;
1098 }
1099
1100
1101 static void domain_exit(struct dmar_domain *domain);
1102 static void vm_domain_exit(struct dmar_domain *domain);
1103
1104 void free_dmar_iommu(struct intel_iommu *iommu)
1105 {
1106         struct dmar_domain *domain;
1107         int i;
1108         unsigned long flags;
1109
1110         i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1111         for (; i < cap_ndoms(iommu->cap); ) {
1112                 domain = iommu->domains[i];
1113                 clear_bit(i, iommu->domain_ids);
1114
1115                 spin_lock_irqsave(&domain->iommu_lock, flags);
1116                 if (--domain->iommu_count == 0) {
1117                         if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1118                                 vm_domain_exit(domain);
1119                         else
1120                                 domain_exit(domain);
1121                 }
1122                 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1123
1124                 i = find_next_bit(iommu->domain_ids,
1125                         cap_ndoms(iommu->cap), i+1);
1126         }
1127
1128         if (iommu->gcmd & DMA_GCMD_TE)
1129                 iommu_disable_translation(iommu);
1130
1131         if (iommu->irq) {
1132                 set_irq_data(iommu->irq, NULL);
1133                 /* This will mask the irq */
1134                 free_irq(iommu->irq, iommu);
1135                 destroy_irq(iommu->irq);
1136         }
1137
1138         kfree(iommu->domains);
1139         kfree(iommu->domain_ids);
1140
1141         g_iommus[iommu->seq_id] = NULL;
1142
1143         /* if all iommus are freed, free g_iommus */
1144         for (i = 0; i < g_num_of_iommus; i++) {
1145                 if (g_iommus[i])
1146                         break;
1147         }
1148
1149         if (i == g_num_of_iommus)
1150                 kfree(g_iommus);
1151
1152         /* free context mapping */
1153         free_context_table(iommu);
1154 }
1155
1156 static struct dmar_domain * iommu_alloc_domain(struct intel_iommu *iommu)
1157 {
1158         unsigned long num;
1159         unsigned long ndomains;
1160         struct dmar_domain *domain;
1161         unsigned long flags;
1162
1163         domain = alloc_domain_mem();
1164         if (!domain)
1165                 return NULL;
1166
1167         ndomains = cap_ndoms(iommu->cap);
1168
1169         spin_lock_irqsave(&iommu->lock, flags);
1170         num = find_first_zero_bit(iommu->domain_ids, ndomains);
1171         if (num >= ndomains) {
1172                 spin_unlock_irqrestore(&iommu->lock, flags);
1173                 free_domain_mem(domain);
1174                 printk(KERN_ERR "IOMMU: no free domain ids\n");
1175                 return NULL;
1176         }
1177
1178         set_bit(num, iommu->domain_ids);
1179         domain->id = num;
1180         memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
1181         set_bit(iommu->seq_id, &domain->iommu_bmp);
1182         domain->flags = 0;
1183         iommu->domains[num] = domain;
1184         spin_unlock_irqrestore(&iommu->lock, flags);
1185
1186         return domain;
1187 }
1188
1189 static void iommu_free_domain(struct dmar_domain *domain)
1190 {
1191         unsigned long flags;
1192         struct intel_iommu *iommu;
1193
1194         iommu = domain_get_iommu(domain);
1195
1196         spin_lock_irqsave(&iommu->lock, flags);
1197         clear_bit(domain->id, iommu->domain_ids);
1198         spin_unlock_irqrestore(&iommu->lock, flags);
1199 }
1200
1201 static struct iova_domain reserved_iova_list;
1202 static struct lock_class_key reserved_alloc_key;
1203 static struct lock_class_key reserved_rbtree_key;
1204
1205 static void dmar_init_reserved_ranges(void)
1206 {
1207         struct pci_dev *pdev = NULL;
1208         struct iova *iova;
1209         int i;
1210         u64 addr, size;
1211
1212         init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
1213
1214         lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1215                 &reserved_alloc_key);
1216         lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1217                 &reserved_rbtree_key);
1218
1219         /* IOAPIC ranges shouldn't be accessed by DMA */
1220         iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1221                 IOVA_PFN(IOAPIC_RANGE_END));
1222         if (!iova)
1223                 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1224
1225         /* Reserve all PCI MMIO to avoid peer-to-peer access */
1226         for_each_pci_dev(pdev) {
1227                 struct resource *r;
1228
1229                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1230                         r = &pdev->resource[i];
1231                         if (!r->flags || !(r->flags & IORESOURCE_MEM))
1232                                 continue;
1233                         addr = r->start;
1234                         addr &= PAGE_MASK;
1235                         size = r->end - addr;
1236                         size = PAGE_ALIGN(size);
1237                         iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1238                                 IOVA_PFN(size + addr) - 1);
1239                         if (!iova)
1240                                 printk(KERN_ERR "Reserve iova failed\n");
1241                 }
1242         }
1243
1244 }
1245
1246 static void domain_reserve_special_ranges(struct dmar_domain *domain)
1247 {
1248         copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1249 }
1250
1251 static inline int guestwidth_to_adjustwidth(int gaw)
1252 {
1253         int agaw;
1254         int r = (gaw - 12) % 9;
1255
1256         if (r == 0)
1257                 agaw = gaw;
1258         else
1259                 agaw = gaw + 9 - r;
1260         if (agaw > 64)
1261                 agaw = 64;
1262         return agaw;
1263 }
1264
1265 static int domain_init(struct dmar_domain *domain, int guest_width)
1266 {
1267         struct intel_iommu *iommu;
1268         int adjust_width, agaw;
1269         unsigned long sagaw;
1270
1271         init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
1272         spin_lock_init(&domain->mapping_lock);
1273         spin_lock_init(&domain->iommu_lock);
1274
1275         domain_reserve_special_ranges(domain);
1276
1277         /* calculate AGAW */
1278         iommu = domain_get_iommu(domain);
1279         if (guest_width > cap_mgaw(iommu->cap))
1280                 guest_width = cap_mgaw(iommu->cap);
1281         domain->gaw = guest_width;
1282         adjust_width = guestwidth_to_adjustwidth(guest_width);
1283         agaw = width_to_agaw(adjust_width);
1284         sagaw = cap_sagaw(iommu->cap);
1285         if (!test_bit(agaw, &sagaw)) {
1286                 /* hardware doesn't support it, choose a bigger one */
1287                 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1288                 agaw = find_next_bit(&sagaw, 5, agaw);
1289                 if (agaw >= 5)
1290                         return -ENODEV;
1291         }
1292         domain->agaw = agaw;
1293         INIT_LIST_HEAD(&domain->devices);
1294
1295         if (ecap_coherent(iommu->ecap))
1296                 domain->iommu_coherency = 1;
1297         else
1298                 domain->iommu_coherency = 0;
1299
1300         if (ecap_sc_support(iommu->ecap))
1301                 domain->iommu_snooping = 1;
1302         else
1303                 domain->iommu_snooping = 0;
1304
1305         domain->iommu_count = 1;
1306
1307         /* always allocate the top pgd */
1308         domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1309         if (!domain->pgd)
1310                 return -ENOMEM;
1311         __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
1312         return 0;
1313 }
1314
1315 static void domain_exit(struct dmar_domain *domain)
1316 {
1317         u64 end;
1318
1319         /* Domain 0 is reserved, so dont process it */
1320         if (!domain)
1321                 return;
1322
1323         domain_remove_dev_info(domain);
1324         /* destroy iovas */
1325         put_iova_domain(&domain->iovad);
1326         end = DOMAIN_MAX_ADDR(domain->gaw);
1327         end = end & (~PAGE_MASK);
1328
1329         /* clear ptes */
1330         dma_pte_clear_range(domain, 0, end);
1331
1332         /* free page tables */
1333         dma_pte_free_pagetable(domain, 0, end);
1334
1335         iommu_free_domain(domain);
1336         free_domain_mem(domain);
1337 }
1338
1339 static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1340                                  u8 bus, u8 devfn, int translation)
1341 {
1342         struct context_entry *context;
1343         unsigned long flags;
1344         struct intel_iommu *iommu;
1345         struct dma_pte *pgd;
1346         unsigned long num;
1347         unsigned long ndomains;
1348         int id;
1349         int agaw;
1350
1351         pr_debug("Set context mapping for %02x:%02x.%d\n",
1352                 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1353
1354         BUG_ON(!domain->pgd);
1355         BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1356                translation != CONTEXT_TT_MULTI_LEVEL);
1357
1358         iommu = device_to_iommu(segment, bus, devfn);
1359         if (!iommu)
1360                 return -ENODEV;
1361
1362         context = device_to_context_entry(iommu, bus, devfn);
1363         if (!context)
1364                 return -ENOMEM;
1365         spin_lock_irqsave(&iommu->lock, flags);
1366         if (context_present(context)) {
1367                 spin_unlock_irqrestore(&iommu->lock, flags);
1368                 return 0;
1369         }
1370
1371         id = domain->id;
1372         pgd = domain->pgd;
1373
1374         if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) {
1375                 int found = 0;
1376
1377                 /* find an available domain id for this device in iommu */
1378                 ndomains = cap_ndoms(iommu->cap);
1379                 num = find_first_bit(iommu->domain_ids, ndomains);
1380                 for (; num < ndomains; ) {
1381                         if (iommu->domains[num] == domain) {
1382                                 id = num;
1383                                 found = 1;
1384                                 break;
1385                         }
1386                         num = find_next_bit(iommu->domain_ids,
1387                                             cap_ndoms(iommu->cap), num+1);
1388                 }
1389
1390                 if (found == 0) {
1391                         num = find_first_zero_bit(iommu->domain_ids, ndomains);
1392                         if (num >= ndomains) {
1393                                 spin_unlock_irqrestore(&iommu->lock, flags);
1394                                 printk(KERN_ERR "IOMMU: no free domain ids\n");
1395                                 return -EFAULT;
1396                         }
1397
1398                         set_bit(num, iommu->domain_ids);
1399                         iommu->domains[num] = domain;
1400                         id = num;
1401                 }
1402
1403                 /* Skip top levels of page tables for
1404                  * iommu which has less agaw than default.
1405                  */
1406                 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1407                         pgd = phys_to_virt(dma_pte_addr(pgd));
1408                         if (!dma_pte_present(pgd)) {
1409                                 spin_unlock_irqrestore(&iommu->lock, flags);
1410                                 return -ENOMEM;
1411                         }
1412                 }
1413         }
1414
1415         context_set_domain_id(context, id);
1416
1417         /*
1418          * In pass through mode, AW must be programmed to indicate the largest
1419          * AGAW value supported by hardware. And ASR is ignored by hardware.
1420          */
1421         if (likely(translation == CONTEXT_TT_MULTI_LEVEL)) {
1422                 context_set_address_width(context, iommu->agaw);
1423                 context_set_address_root(context, virt_to_phys(pgd));
1424         } else
1425                 context_set_address_width(context, iommu->msagaw);
1426
1427         context_set_translation_type(context, translation);
1428         context_set_fault_enable(context);
1429         context_set_present(context);
1430         domain_flush_cache(domain, context, sizeof(*context));
1431
1432         /* it's a non-present to present mapping */
1433         if (iommu->flush.flush_context(iommu, domain->id,
1434                 (((u16)bus) << 8) | devfn, DMA_CCMD_MASK_NOBIT,
1435                 DMA_CCMD_DEVICE_INVL, 1))
1436                 iommu_flush_write_buffer(iommu);
1437         else
1438                 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH, 0);
1439
1440         spin_unlock_irqrestore(&iommu->lock, flags);
1441
1442         spin_lock_irqsave(&domain->iommu_lock, flags);
1443         if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1444                 domain->iommu_count++;
1445                 domain_update_iommu_cap(domain);
1446         }
1447         spin_unlock_irqrestore(&domain->iommu_lock, flags);
1448         return 0;
1449 }
1450
1451 static int
1452 domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1453                         int translation)
1454 {
1455         int ret;
1456         struct pci_dev *tmp, *parent;
1457
1458         ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
1459                                          pdev->bus->number, pdev->devfn,
1460                                          translation);
1461         if (ret)
1462                 return ret;
1463
1464         /* dependent device mapping */
1465         tmp = pci_find_upstream_pcie_bridge(pdev);
1466         if (!tmp)
1467                 return 0;
1468         /* Secondary interface's bus number and devfn 0 */
1469         parent = pdev->bus->self;
1470         while (parent != tmp) {
1471                 ret = domain_context_mapping_one(domain,
1472                                                  pci_domain_nr(parent->bus),
1473                                                  parent->bus->number,
1474                                                  parent->devfn, translation);
1475                 if (ret)
1476                         return ret;
1477                 parent = parent->bus->self;
1478         }
1479         if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1480                 return domain_context_mapping_one(domain,
1481                                         pci_domain_nr(tmp->subordinate),
1482                                         tmp->subordinate->number, 0,
1483                                         translation);
1484         else /* this is a legacy PCI bridge */
1485                 return domain_context_mapping_one(domain,
1486                                                   pci_domain_nr(tmp->bus),
1487                                                   tmp->bus->number,
1488                                                   tmp->devfn,
1489                                                   translation);
1490 }
1491
1492 static int domain_context_mapped(struct pci_dev *pdev)
1493 {
1494         int ret;
1495         struct pci_dev *tmp, *parent;
1496         struct intel_iommu *iommu;
1497
1498         iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1499                                 pdev->devfn);
1500         if (!iommu)
1501                 return -ENODEV;
1502
1503         ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
1504         if (!ret)
1505                 return ret;
1506         /* dependent device mapping */
1507         tmp = pci_find_upstream_pcie_bridge(pdev);
1508         if (!tmp)
1509                 return ret;
1510         /* Secondary interface's bus number and devfn 0 */
1511         parent = pdev->bus->self;
1512         while (parent != tmp) {
1513                 ret = device_context_mapped(iommu, parent->bus->number,
1514                                             parent->devfn);
1515                 if (!ret)
1516                         return ret;
1517                 parent = parent->bus->self;
1518         }
1519         if (tmp->is_pcie)
1520                 return device_context_mapped(iommu, tmp->subordinate->number,
1521                                              0);
1522         else
1523                 return device_context_mapped(iommu, tmp->bus->number,
1524                                              tmp->devfn);
1525 }
1526
1527 static int
1528 domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1529                         u64 hpa, size_t size, int prot)
1530 {
1531         u64 start_pfn, end_pfn;
1532         struct dma_pte *pte;
1533         int index;
1534         int addr_width = agaw_to_width(domain->agaw);
1535
1536         hpa &= (((u64)1) << addr_width) - 1;
1537
1538         if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1539                 return -EINVAL;
1540         iova &= PAGE_MASK;
1541         start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1542         end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
1543         index = 0;
1544         while (start_pfn < end_pfn) {
1545                 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
1546                 if (!pte)
1547                         return -ENOMEM;
1548                 /* We don't need lock here, nobody else
1549                  * touches the iova range
1550                  */
1551                 BUG_ON(dma_pte_addr(pte));
1552                 dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT);
1553                 dma_set_pte_prot(pte, prot);
1554                 if (prot & DMA_PTE_SNP)
1555                         dma_set_pte_snp(pte);
1556                 domain_flush_cache(domain, pte, sizeof(*pte));
1557                 start_pfn++;
1558                 index++;
1559         }
1560         return 0;
1561 }
1562
1563 static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
1564 {
1565         if (!iommu)
1566                 return;
1567
1568         clear_context_table(iommu, bus, devfn);
1569         iommu->flush.flush_context(iommu, 0, 0, 0,
1570                                            DMA_CCMD_GLOBAL_INVL, 0);
1571         iommu->flush.flush_iotlb(iommu, 0, 0, 0,
1572                                          DMA_TLB_GLOBAL_FLUSH, 0);
1573 }
1574
1575 static void domain_remove_dev_info(struct dmar_domain *domain)
1576 {
1577         struct device_domain_info *info;
1578         unsigned long flags;
1579         struct intel_iommu *iommu;
1580
1581         spin_lock_irqsave(&device_domain_lock, flags);
1582         while (!list_empty(&domain->devices)) {
1583                 info = list_entry(domain->devices.next,
1584                         struct device_domain_info, link);
1585                 list_del(&info->link);
1586                 list_del(&info->global);
1587                 if (info->dev)
1588                         info->dev->dev.archdata.iommu = NULL;
1589                 spin_unlock_irqrestore(&device_domain_lock, flags);
1590
1591                 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
1592                 iommu_detach_dev(iommu, info->bus, info->devfn);
1593                 free_devinfo_mem(info);
1594
1595                 spin_lock_irqsave(&device_domain_lock, flags);
1596         }
1597         spin_unlock_irqrestore(&device_domain_lock, flags);
1598 }
1599
1600 /*
1601  * find_domain
1602  * Note: we use struct pci_dev->dev.archdata.iommu stores the info
1603  */
1604 static struct dmar_domain *
1605 find_domain(struct pci_dev *pdev)
1606 {
1607         struct device_domain_info *info;
1608
1609         /* No lock here, assumes no domain exit in normal case */
1610         info = pdev->dev.archdata.iommu;
1611         if (info)
1612                 return info->domain;
1613         return NULL;
1614 }
1615
1616 /* domain is initialized */
1617 static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1618 {
1619         struct dmar_domain *domain, *found = NULL;
1620         struct intel_iommu *iommu;
1621         struct dmar_drhd_unit *drhd;
1622         struct device_domain_info *info, *tmp;
1623         struct pci_dev *dev_tmp;
1624         unsigned long flags;
1625         int bus = 0, devfn = 0;
1626         int segment;
1627
1628         domain = find_domain(pdev);
1629         if (domain)
1630                 return domain;
1631
1632         segment = pci_domain_nr(pdev->bus);
1633
1634         dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1635         if (dev_tmp) {
1636                 if (dev_tmp->is_pcie) {
1637                         bus = dev_tmp->subordinate->number;
1638                         devfn = 0;
1639                 } else {
1640                         bus = dev_tmp->bus->number;
1641                         devfn = dev_tmp->devfn;
1642                 }
1643                 spin_lock_irqsave(&device_domain_lock, flags);
1644                 list_for_each_entry(info, &device_domain_list, global) {
1645                         if (info->segment == segment &&
1646                             info->bus == bus && info->devfn == devfn) {
1647                                 found = info->domain;
1648                                 break;
1649                         }
1650                 }
1651                 spin_unlock_irqrestore(&device_domain_lock, flags);
1652                 /* pcie-pci bridge already has a domain, uses it */
1653                 if (found) {
1654                         domain = found;
1655                         goto found_domain;
1656                 }
1657         }
1658
1659         /* Allocate new domain for the device */
1660         drhd = dmar_find_matched_drhd_unit(pdev);
1661         if (!drhd) {
1662                 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1663                         pci_name(pdev));
1664                 return NULL;
1665         }
1666         iommu = drhd->iommu;
1667
1668         domain = iommu_alloc_domain(iommu);
1669         if (!domain)
1670                 goto error;
1671
1672         if (domain_init(domain, gaw)) {
1673                 domain_exit(domain);
1674                 goto error;
1675         }
1676
1677         /* register pcie-to-pci device */
1678         if (dev_tmp) {
1679                 info = alloc_devinfo_mem();
1680                 if (!info) {
1681                         domain_exit(domain);
1682                         goto error;
1683                 }
1684                 info->segment = segment;
1685                 info->bus = bus;
1686                 info->devfn = devfn;
1687                 info->dev = NULL;
1688                 info->domain = domain;
1689                 /* This domain is shared by devices under p2p bridge */
1690                 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
1691
1692                 /* pcie-to-pci bridge already has a domain, uses it */
1693                 found = NULL;
1694                 spin_lock_irqsave(&device_domain_lock, flags);
1695                 list_for_each_entry(tmp, &device_domain_list, global) {
1696                         if (tmp->segment == segment &&
1697                             tmp->bus == bus && tmp->devfn == devfn) {
1698                                 found = tmp->domain;
1699                                 break;
1700                         }
1701                 }
1702                 if (found) {
1703                         free_devinfo_mem(info);
1704                         domain_exit(domain);
1705                         domain = found;
1706                 } else {
1707                         list_add(&info->link, &domain->devices);
1708                         list_add(&info->global, &device_domain_list);
1709                 }
1710                 spin_unlock_irqrestore(&device_domain_lock, flags);
1711         }
1712
1713 found_domain:
1714         info = alloc_devinfo_mem();
1715         if (!info)
1716                 goto error;
1717         info->segment = segment;
1718         info->bus = pdev->bus->number;
1719         info->devfn = pdev->devfn;
1720         info->dev = pdev;
1721         info->domain = domain;
1722         spin_lock_irqsave(&device_domain_lock, flags);
1723         /* somebody is fast */
1724         found = find_domain(pdev);
1725         if (found != NULL) {
1726                 spin_unlock_irqrestore(&device_domain_lock, flags);
1727                 if (found != domain) {
1728                         domain_exit(domain);
1729                         domain = found;
1730                 }
1731                 free_devinfo_mem(info);
1732                 return domain;
1733         }
1734         list_add(&info->link, &domain->devices);
1735         list_add(&info->global, &device_domain_list);
1736         pdev->dev.archdata.iommu = info;
1737         spin_unlock_irqrestore(&device_domain_lock, flags);
1738         return domain;
1739 error:
1740         /* recheck it here, maybe others set it */
1741         return find_domain(pdev);
1742 }
1743
1744 static int iommu_prepare_identity_map(struct pci_dev *pdev,
1745                                       unsigned long long start,
1746                                       unsigned long long end)
1747 {
1748         struct dmar_domain *domain;
1749         unsigned long size;
1750         unsigned long long base;
1751         int ret;
1752
1753         printk(KERN_INFO
1754                 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1755                 pci_name(pdev), start, end);
1756         /* page table init */
1757         domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1758         if (!domain)
1759                 return -ENOMEM;
1760
1761         /* The address might not be aligned */
1762         base = start & PAGE_MASK;
1763         size = end - base;
1764         size = PAGE_ALIGN(size);
1765         if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1766                         IOVA_PFN(base + size) - 1)) {
1767                 printk(KERN_ERR "IOMMU: reserve iova failed\n");
1768                 ret = -ENOMEM;
1769                 goto error;
1770         }
1771
1772         pr_debug("Mapping reserved region %lx@%llx for %s\n",
1773                 size, base, pci_name(pdev));
1774         /*
1775          * RMRR range might have overlap with physical memory range,
1776          * clear it first
1777          */
1778         dma_pte_clear_range(domain, base, base + size);
1779
1780         ret = domain_page_mapping(domain, base, base, size,
1781                 DMA_PTE_READ|DMA_PTE_WRITE);
1782         if (ret)
1783                 goto error;
1784
1785         /* context entry init */
1786         ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
1787         if (!ret)
1788                 return 0;
1789 error:
1790         domain_exit(domain);
1791         return ret;
1792
1793 }
1794
1795 static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1796         struct pci_dev *pdev)
1797 {
1798         if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
1799                 return 0;
1800         return iommu_prepare_identity_map(pdev, rmrr->base_address,
1801                 rmrr->end_address + 1);
1802 }
1803
1804 #ifdef CONFIG_DMAR_GFX_WA
1805 struct iommu_prepare_data {
1806         struct pci_dev *pdev;
1807         int ret;
1808 };
1809
1810 static int __init iommu_prepare_work_fn(unsigned long start_pfn,
1811                                          unsigned long end_pfn, void *datax)
1812 {
1813         struct iommu_prepare_data *data;
1814
1815         data = (struct iommu_prepare_data *)datax;
1816
1817         data->ret = iommu_prepare_identity_map(data->pdev,
1818                                 start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
1819         return data->ret;
1820
1821 }
1822
1823 static int __init iommu_prepare_with_active_regions(struct pci_dev *pdev)
1824 {
1825         int nid;
1826         struct iommu_prepare_data data;
1827
1828         data.pdev = pdev;
1829         data.ret = 0;
1830
1831         for_each_online_node(nid) {
1832                 work_with_active_regions(nid, iommu_prepare_work_fn, &data);
1833                 if (data.ret)
1834                         return data.ret;
1835         }
1836         return data.ret;
1837 }
1838
1839 static void __init iommu_prepare_gfx_mapping(void)
1840 {
1841         struct pci_dev *pdev = NULL;
1842         int ret;
1843
1844         for_each_pci_dev(pdev) {
1845                 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO ||
1846                                 !IS_GFX_DEVICE(pdev))
1847                         continue;
1848                 printk(KERN_INFO "IOMMU: gfx device %s 1-1 mapping\n",
1849                         pci_name(pdev));
1850                 ret = iommu_prepare_with_active_regions(pdev);
1851                 if (ret)
1852                         printk(KERN_ERR "IOMMU: mapping reserved region failed\n");
1853         }
1854 }
1855 #else /* !CONFIG_DMAR_GFX_WA */
1856 static inline void iommu_prepare_gfx_mapping(void)
1857 {
1858         return;
1859 }
1860 #endif
1861
1862 #ifdef CONFIG_DMAR_FLOPPY_WA
1863 static inline void iommu_prepare_isa(void)
1864 {
1865         struct pci_dev *pdev;
1866         int ret;
1867
1868         pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1869         if (!pdev)
1870                 return;
1871
1872         printk(KERN_INFO "IOMMU: Prepare 0-16M unity mapping for LPC\n");
1873         ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1874
1875         if (ret)
1876                 printk(KERN_ERR "IOMMU: Failed to create 0-64M identity map, "
1877                         "floppy might not work\n");
1878
1879 }
1880 #else
1881 static inline void iommu_prepare_isa(void)
1882 {
1883         return;
1884 }
1885 #endif /* !CONFIG_DMAR_FLPY_WA */
1886
1887 /* Initialize each context entry as pass through.*/
1888 static int __init init_context_pass_through(void)
1889 {
1890         struct pci_dev *pdev = NULL;
1891         struct dmar_domain *domain;
1892         int ret;
1893
1894         for_each_pci_dev(pdev) {
1895                 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1896                 ret = domain_context_mapping(domain, pdev,
1897                                              CONTEXT_TT_PASS_THROUGH);
1898                 if (ret)
1899                         return ret;
1900         }
1901         return 0;
1902 }
1903
1904 static int __init init_dmars(void)
1905 {
1906         struct dmar_drhd_unit *drhd;
1907         struct dmar_rmrr_unit *rmrr;
1908         struct pci_dev *pdev;
1909         struct intel_iommu *iommu;
1910         int i, ret;
1911         int pass_through = 1;
1912
1913         /*
1914          * for each drhd
1915          *    allocate root
1916          *    initialize and program root entry to not present
1917          * endfor
1918          */
1919         for_each_drhd_unit(drhd) {
1920                 g_num_of_iommus++;
1921                 /*
1922                  * lock not needed as this is only incremented in the single
1923                  * threaded kernel __init code path all other access are read
1924                  * only
1925                  */
1926         }
1927
1928         g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
1929                         GFP_KERNEL);
1930         if (!g_iommus) {
1931                 printk(KERN_ERR "Allocating global iommu array failed\n");
1932                 ret = -ENOMEM;
1933                 goto error;
1934         }
1935
1936         deferred_flush = kzalloc(g_num_of_iommus *
1937                 sizeof(struct deferred_flush_tables), GFP_KERNEL);
1938         if (!deferred_flush) {
1939                 kfree(g_iommus);
1940                 ret = -ENOMEM;
1941                 goto error;
1942         }
1943
1944         for_each_drhd_unit(drhd) {
1945                 if (drhd->ignored)
1946                         continue;
1947
1948                 iommu = drhd->iommu;
1949                 g_iommus[iommu->seq_id] = iommu;
1950
1951                 ret = iommu_init_domains(iommu);
1952                 if (ret)
1953                         goto error;
1954
1955                 /*
1956                  * TBD:
1957                  * we could share the same root & context tables
1958                  * amoung all IOMMU's. Need to Split it later.
1959                  */
1960                 ret = iommu_alloc_root_entry(iommu);
1961                 if (ret) {
1962                         printk(KERN_ERR "IOMMU: allocate root entry failed\n");
1963                         goto error;
1964                 }
1965                 if (!ecap_pass_through(iommu->ecap))
1966                         pass_through = 0;
1967         }
1968         if (iommu_pass_through)
1969                 if (!pass_through) {
1970                         printk(KERN_INFO
1971                                "Pass Through is not supported by hardware.\n");
1972                         iommu_pass_through = 0;
1973                 }
1974
1975         /*
1976          * Start from the sane iommu hardware state.
1977          */
1978         for_each_drhd_unit(drhd) {
1979                 if (drhd->ignored)
1980                         continue;
1981
1982                 iommu = drhd->iommu;
1983
1984                 /*
1985                  * If the queued invalidation is already initialized by us
1986                  * (for example, while enabling interrupt-remapping) then
1987                  * we got the things already rolling from a sane state.
1988                  */
1989                 if (iommu->qi)
1990                         continue;
1991
1992                 /*
1993                  * Clear any previous faults.
1994                  */
1995                 dmar_fault(-1, iommu);
1996                 /*
1997                  * Disable queued invalidation if supported and already enabled
1998                  * before OS handover.
1999                  */
2000                 dmar_disable_qi(iommu);
2001         }
2002
2003         for_each_drhd_unit(drhd) {
2004                 if (drhd->ignored)
2005                         continue;
2006
2007                 iommu = drhd->iommu;
2008
2009                 if (dmar_enable_qi(iommu)) {
2010                         /*
2011                          * Queued Invalidate not enabled, use Register Based
2012                          * Invalidate
2013                          */
2014                         iommu->flush.flush_context = __iommu_flush_context;
2015                         iommu->flush.flush_iotlb = __iommu_flush_iotlb;
2016                         printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
2017                                "invalidation\n",
2018                                (unsigned long long)drhd->reg_base_addr);
2019                 } else {
2020                         iommu->flush.flush_context = qi_flush_context;
2021                         iommu->flush.flush_iotlb = qi_flush_iotlb;
2022                         printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
2023                                "invalidation\n",
2024                                (unsigned long long)drhd->reg_base_addr);
2025                 }
2026         }
2027
2028 #ifdef CONFIG_INTR_REMAP
2029         if (!intr_remapping_enabled) {
2030                 ret = enable_intr_remapping(0);
2031                 if (ret)
2032                         printk(KERN_ERR
2033                                "IOMMU: enable interrupt remapping failed\n");
2034         }
2035 #endif
2036         /*
2037          * If pass through is set and enabled, context entries of all pci
2038          * devices are intialized by pass through translation type.
2039          */
2040         if (iommu_pass_through) {
2041                 ret = init_context_pass_through();
2042                 if (ret) {
2043                         printk(KERN_ERR "IOMMU: Pass through init failed.\n");
2044                         iommu_pass_through = 0;
2045                 }
2046         }
2047
2048         /*
2049          * If pass through is not set or not enabled, setup context entries for
2050          * identity mappings for rmrr, gfx, and isa.
2051          */
2052         if (!iommu_pass_through) {
2053                 /*
2054                  * For each rmrr
2055                  *   for each dev attached to rmrr
2056                  *   do
2057                  *     locate drhd for dev, alloc domain for dev
2058                  *     allocate free domain
2059                  *     allocate page table entries for rmrr
2060                  *     if context not allocated for bus
2061                  *           allocate and init context
2062                  *           set present in root table for this bus
2063                  *     init context with domain, translation etc
2064                  *    endfor
2065                  * endfor
2066                  */
2067                 for_each_rmrr_units(rmrr) {
2068                         for (i = 0; i < rmrr->devices_cnt; i++) {
2069                                 pdev = rmrr->devices[i];
2070                                 /*
2071                                  * some BIOS lists non-exist devices in DMAR
2072                                  * table.
2073                                  */
2074                                 if (!pdev)
2075                                         continue;
2076                                 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2077                                 if (ret)
2078                                         printk(KERN_ERR
2079                                  "IOMMU: mapping reserved region failed\n");
2080                         }
2081                 }
2082
2083                 iommu_prepare_gfx_mapping();
2084
2085                 iommu_prepare_isa();
2086         }
2087
2088         /*
2089          * for each drhd
2090          *   enable fault log
2091          *   global invalidate context cache
2092          *   global invalidate iotlb
2093          *   enable translation
2094          */
2095         for_each_drhd_unit(drhd) {
2096                 if (drhd->ignored)
2097                         continue;
2098                 iommu = drhd->iommu;
2099
2100                 iommu_flush_write_buffer(iommu);
2101
2102                 ret = dmar_set_interrupt(iommu);
2103                 if (ret)
2104                         goto error;
2105
2106                 iommu_set_root_entry(iommu);
2107
2108                 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL,
2109                                            0);
2110                 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH,
2111                                          0);
2112                 iommu_disable_protect_mem_regions(iommu);
2113
2114                 ret = iommu_enable_translation(iommu);
2115                 if (ret)
2116                         goto error;
2117         }
2118
2119         return 0;
2120 error:
2121         for_each_drhd_unit(drhd) {
2122                 if (drhd->ignored)
2123                         continue;
2124                 iommu = drhd->iommu;
2125                 free_iommu(iommu);
2126         }
2127         kfree(g_iommus);
2128         return ret;
2129 }
2130
2131 static inline u64 aligned_size(u64 host_addr, size_t size)
2132 {
2133         u64 addr;
2134         addr = (host_addr & (~PAGE_MASK)) + size;
2135         return PAGE_ALIGN(addr);
2136 }
2137
2138 struct iova *
2139 iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
2140 {
2141         struct iova *piova;
2142
2143         /* Make sure it's in range */
2144         end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
2145         if (!size || (IOVA_START_ADDR + size > end))
2146                 return NULL;
2147
2148         piova = alloc_iova(&domain->iovad,
2149                         size >> PAGE_SHIFT, IOVA_PFN(end), 1);
2150         return piova;
2151 }
2152
2153 static struct iova *
2154 __intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
2155                    size_t size, u64 dma_mask)
2156 {
2157         struct pci_dev *pdev = to_pci_dev(dev);
2158         struct iova *iova = NULL;
2159
2160         if (dma_mask <= DMA_BIT_MASK(32) || dmar_forcedac)
2161                 iova = iommu_alloc_iova(domain, size, dma_mask);
2162         else {
2163                 /*
2164                  * First try to allocate an io virtual address in
2165                  * DMA_BIT_MASK(32) and if that fails then try allocating
2166                  * from higher range
2167                  */
2168                 iova = iommu_alloc_iova(domain, size, DMA_BIT_MASK(32));
2169                 if (!iova)
2170                         iova = iommu_alloc_iova(domain, size, dma_mask);
2171         }
2172
2173         if (!iova) {
2174                 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2175                 return NULL;
2176         }
2177
2178         return iova;
2179 }
2180
2181 static struct dmar_domain *
2182 get_valid_domain_for_dev(struct pci_dev *pdev)
2183 {
2184         struct dmar_domain *domain;
2185         int ret;
2186
2187         domain = get_domain_for_dev(pdev,
2188                         DEFAULT_DOMAIN_ADDRESS_WIDTH);
2189         if (!domain) {
2190                 printk(KERN_ERR
2191                         "Allocating domain for %s failed", pci_name(pdev));
2192                 return NULL;
2193         }
2194
2195         /* make sure context mapping is ok */
2196         if (unlikely(!domain_context_mapped(pdev))) {
2197                 ret = domain_context_mapping(domain, pdev,
2198                                              CONTEXT_TT_MULTI_LEVEL);
2199                 if (ret) {
2200                         printk(KERN_ERR
2201                                 "Domain context map for %s failed",
2202                                 pci_name(pdev));
2203                         return NULL;
2204                 }
2205         }
2206
2207         return domain;
2208 }
2209
2210 static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2211                                      size_t size, int dir, u64 dma_mask)
2212 {
2213         struct pci_dev *pdev = to_pci_dev(hwdev);
2214         struct dmar_domain *domain;
2215         phys_addr_t start_paddr;
2216         struct iova *iova;
2217         int prot = 0;
2218         int ret;
2219         struct intel_iommu *iommu;
2220
2221         BUG_ON(dir == DMA_NONE);
2222         if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2223                 return paddr;
2224
2225         domain = get_valid_domain_for_dev(pdev);
2226         if (!domain)
2227                 return 0;
2228
2229         iommu = domain_get_iommu(domain);
2230         size = aligned_size((u64)paddr, size);
2231
2232         iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
2233         if (!iova)
2234                 goto error;
2235
2236         start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
2237
2238         /*
2239          * Check if DMAR supports zero-length reads on write only
2240          * mappings..
2241          */
2242         if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
2243                         !cap_zlr(iommu->cap))
2244                 prot |= DMA_PTE_READ;
2245         if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2246                 prot |= DMA_PTE_WRITE;
2247         /*
2248          * paddr - (paddr + size) might be partial page, we should map the whole
2249          * page.  Note: if two part of one page are separately mapped, we
2250          * might have two guest_addr mapping to the same host paddr, but this
2251          * is not a big problem
2252          */
2253         ret = domain_page_mapping(domain, start_paddr,
2254                 ((u64)paddr) & PAGE_MASK, size, prot);
2255         if (ret)
2256                 goto error;
2257
2258         /* it's a non-present to present mapping */
2259         ret = iommu_flush_iotlb_psi(iommu, domain->id,
2260                         start_paddr, size >> VTD_PAGE_SHIFT, 1);
2261         if (ret)
2262                 iommu_flush_write_buffer(iommu);
2263
2264         return start_paddr + ((u64)paddr & (~PAGE_MASK));
2265
2266 error:
2267         if (iova)
2268                 __free_iova(&domain->iovad, iova);
2269         printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
2270                 pci_name(pdev), size, (unsigned long long)paddr, dir);
2271         return 0;
2272 }
2273
2274 static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2275                                  unsigned long offset, size_t size,
2276                                  enum dma_data_direction dir,
2277                                  struct dma_attrs *attrs)
2278 {
2279         return __intel_map_single(dev, page_to_phys(page) + offset, size,
2280                                   dir, to_pci_dev(dev)->dma_mask);
2281 }
2282
2283 static void flush_unmaps(void)
2284 {
2285         int i, j;
2286
2287         timer_on = 0;
2288
2289         /* just flush them all */
2290         for (i = 0; i < g_num_of_iommus; i++) {
2291                 struct intel_iommu *iommu = g_iommus[i];
2292                 if (!iommu)
2293                         continue;
2294
2295                 if (deferred_flush[i].next) {
2296                         iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2297                                                  DMA_TLB_GLOBAL_FLUSH, 0);
2298                         for (j = 0; j < deferred_flush[i].next; j++) {
2299                                 __free_iova(&deferred_flush[i].domain[j]->iovad,
2300                                                 deferred_flush[i].iova[j]);
2301                         }
2302                         deferred_flush[i].next = 0;
2303                 }
2304         }
2305
2306         list_size = 0;
2307 }
2308
2309 static void flush_unmaps_timeout(unsigned long data)
2310 {
2311         unsigned long flags;
2312
2313         spin_lock_irqsave(&async_umap_flush_lock, flags);
2314         flush_unmaps();
2315         spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2316 }
2317
2318 static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2319 {
2320         unsigned long flags;
2321         int next, iommu_id;
2322         struct intel_iommu *iommu;
2323
2324         spin_lock_irqsave(&async_umap_flush_lock, flags);
2325         if (list_size == HIGH_WATER_MARK)
2326                 flush_unmaps();
2327
2328         iommu = domain_get_iommu(dom);
2329         iommu_id = iommu->seq_id;
2330
2331         next = deferred_flush[iommu_id].next;
2332         deferred_flush[iommu_id].domain[next] = dom;
2333         deferred_flush[iommu_id].iova[next] = iova;
2334         deferred_flush[iommu_id].next++;
2335
2336         if (!timer_on) {
2337                 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2338                 timer_on = 1;
2339         }
2340         list_size++;
2341         spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2342 }
2343
2344 static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2345                              size_t size, enum dma_data_direction dir,
2346                              struct dma_attrs *attrs)
2347 {
2348         struct pci_dev *pdev = to_pci_dev(dev);
2349         struct dmar_domain *domain;
2350         unsigned long start_addr;
2351         struct iova *iova;
2352         struct intel_iommu *iommu;
2353
2354         if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2355                 return;
2356         domain = find_domain(pdev);
2357         BUG_ON(!domain);
2358
2359         iommu = domain_get_iommu(domain);
2360
2361         iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2362         if (!iova)
2363                 return;
2364
2365         start_addr = iova->pfn_lo << PAGE_SHIFT;
2366         size = aligned_size((u64)dev_addr, size);
2367
2368         pr_debug("Device %s unmapping: %zx@%llx\n",
2369                 pci_name(pdev), size, (unsigned long long)start_addr);
2370
2371         /*  clear the whole page */
2372         dma_pte_clear_range(domain, start_addr, start_addr + size);
2373         /* free page tables */
2374         dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2375         if (intel_iommu_strict) {
2376                 if (iommu_flush_iotlb_psi(iommu,
2377                         domain->id, start_addr, size >> VTD_PAGE_SHIFT, 0))
2378                         iommu_flush_write_buffer(iommu);
2379                 /* free iova */
2380                 __free_iova(&domain->iovad, iova);
2381         } else {
2382                 add_unmap(domain, iova);
2383                 /*
2384                  * queue up the release of the unmap to save the 1/6th of the
2385                  * cpu used up by the iotlb flush operation...
2386                  */
2387         }
2388 }
2389
2390 static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2391                                int dir)
2392 {
2393         intel_unmap_page(dev, dev_addr, size, dir, NULL);
2394 }
2395
2396 static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2397                                   dma_addr_t *dma_handle, gfp_t flags)
2398 {
2399         void *vaddr;
2400         int order;
2401
2402         size = PAGE_ALIGN(size);
2403         order = get_order(size);
2404         flags &= ~(GFP_DMA | GFP_DMA32);
2405
2406         vaddr = (void *)__get_free_pages(flags, order);
2407         if (!vaddr)
2408                 return NULL;
2409         memset(vaddr, 0, size);
2410
2411         *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2412                                          DMA_BIDIRECTIONAL,
2413                                          hwdev->coherent_dma_mask);
2414         if (*dma_handle)
2415                 return vaddr;
2416         free_pages((unsigned long)vaddr, order);
2417         return NULL;
2418 }
2419
2420 static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2421                                 dma_addr_t dma_handle)
2422 {
2423         int order;
2424
2425         size = PAGE_ALIGN(size);
2426         order = get_order(size);
2427
2428         intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2429         free_pages((unsigned long)vaddr, order);
2430 }
2431
2432 static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2433                            int nelems, enum dma_data_direction dir,
2434                            struct dma_attrs *attrs)
2435 {
2436         int i;
2437         struct pci_dev *pdev = to_pci_dev(hwdev);
2438         struct dmar_domain *domain;
2439         unsigned long start_addr;
2440         struct iova *iova;
2441         size_t size = 0;
2442         phys_addr_t addr;
2443         struct scatterlist *sg;
2444         struct intel_iommu *iommu;
2445
2446         if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2447                 return;
2448
2449         domain = find_domain(pdev);
2450         BUG_ON(!domain);
2451
2452         iommu = domain_get_iommu(domain);
2453
2454         iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
2455         if (!iova)
2456                 return;
2457         for_each_sg(sglist, sg, nelems, i) {
2458                 addr = page_to_phys(sg_page(sg)) + sg->offset;
2459                 size += aligned_size((u64)addr, sg->length);
2460         }
2461
2462         start_addr = iova->pfn_lo << PAGE_SHIFT;
2463
2464         /*  clear the whole page */
2465         dma_pte_clear_range(domain, start_addr, start_addr + size);
2466         /* free page tables */
2467         dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2468
2469         if (iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2470                         size >> VTD_PAGE_SHIFT, 0))
2471                 iommu_flush_write_buffer(iommu);
2472
2473         /* free iova */
2474         __free_iova(&domain->iovad, iova);
2475 }
2476
2477 static int intel_nontranslate_map_sg(struct device *hddev,
2478         struct scatterlist *sglist, int nelems, int dir)
2479 {
2480         int i;
2481         struct scatterlist *sg;
2482
2483         for_each_sg(sglist, sg, nelems, i) {
2484                 BUG_ON(!sg_page(sg));
2485                 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
2486                 sg->dma_length = sg->length;
2487         }
2488         return nelems;
2489 }
2490
2491 static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2492                         enum dma_data_direction dir, struct dma_attrs *attrs)
2493 {
2494         phys_addr_t addr;
2495         int i;
2496         struct pci_dev *pdev = to_pci_dev(hwdev);
2497         struct dmar_domain *domain;
2498         size_t size = 0;
2499         int prot = 0;
2500         size_t offset = 0;
2501         struct iova *iova = NULL;
2502         int ret;
2503         struct scatterlist *sg;
2504         unsigned long start_addr;
2505         struct intel_iommu *iommu;
2506
2507         BUG_ON(dir == DMA_NONE);
2508         if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2509                 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
2510
2511         domain = get_valid_domain_for_dev(pdev);
2512         if (!domain)
2513                 return 0;
2514
2515         iommu = domain_get_iommu(domain);
2516
2517         for_each_sg(sglist, sg, nelems, i) {
2518                 addr = page_to_phys(sg_page(sg)) + sg->offset;
2519                 size += aligned_size((u64)addr, sg->length);
2520         }
2521
2522         iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
2523         if (!iova) {
2524                 sglist->dma_length = 0;
2525                 return 0;
2526         }
2527
2528         /*
2529          * Check if DMAR supports zero-length reads on write only
2530          * mappings..
2531          */
2532         if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
2533                         !cap_zlr(iommu->cap))
2534                 prot |= DMA_PTE_READ;
2535         if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2536                 prot |= DMA_PTE_WRITE;
2537
2538         start_addr = iova->pfn_lo << PAGE_SHIFT;
2539         offset = 0;
2540         for_each_sg(sglist, sg, nelems, i) {
2541                 addr = page_to_phys(sg_page(sg)) + sg->offset;
2542                 size = aligned_size((u64)addr, sg->length);
2543                 ret = domain_page_mapping(domain, start_addr + offset,
2544                         ((u64)addr) & PAGE_MASK,
2545                         size, prot);
2546                 if (ret) {
2547                         /*  clear the page */
2548                         dma_pte_clear_range(domain, start_addr,
2549                                   start_addr + offset);
2550                         /* free page tables */
2551                         dma_pte_free_pagetable(domain, start_addr,
2552                                   start_addr + offset);
2553                         /* free iova */
2554                         __free_iova(&domain->iovad, iova);
2555                         return 0;
2556                 }
2557                 sg->dma_address = start_addr + offset +
2558                                 ((u64)addr & (~PAGE_MASK));
2559                 sg->dma_length = sg->length;
2560                 offset += size;
2561         }
2562
2563         /* it's a non-present to present mapping */
2564         if (iommu_flush_iotlb_psi(iommu, domain->id,
2565                         start_addr, offset >> VTD_PAGE_SHIFT, 1))
2566                 iommu_flush_write_buffer(iommu);
2567         return nelems;
2568 }
2569
2570 static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2571 {
2572         return !dma_addr;
2573 }
2574
2575 struct dma_map_ops intel_dma_ops = {
2576         .alloc_coherent = intel_alloc_coherent,
2577         .free_coherent = intel_free_coherent,
2578         .map_sg = intel_map_sg,
2579         .unmap_sg = intel_unmap_sg,
2580         .map_page = intel_map_page,
2581         .unmap_page = intel_unmap_page,
2582         .mapping_error = intel_mapping_error,
2583 };
2584
2585 static inline int iommu_domain_cache_init(void)
2586 {
2587         int ret = 0;
2588
2589         iommu_domain_cache = kmem_cache_create("iommu_domain",
2590                                          sizeof(struct dmar_domain),
2591                                          0,
2592                                          SLAB_HWCACHE_ALIGN,
2593
2594                                          NULL);
2595         if (!iommu_domain_cache) {
2596                 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2597                 ret = -ENOMEM;
2598         }
2599
2600         return ret;
2601 }
2602
2603 static inline int iommu_devinfo_cache_init(void)
2604 {
2605         int ret = 0;
2606
2607         iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2608                                          sizeof(struct device_domain_info),
2609                                          0,
2610                                          SLAB_HWCACHE_ALIGN,
2611                                          NULL);
2612         if (!iommu_devinfo_cache) {
2613                 printk(KERN_ERR "Couldn't create devinfo cache\n");
2614                 ret = -ENOMEM;
2615         }
2616
2617         return ret;
2618 }
2619
2620 static inline int iommu_iova_cache_init(void)
2621 {
2622         int ret = 0;
2623
2624         iommu_iova_cache = kmem_cache_create("iommu_iova",
2625                                          sizeof(struct iova),
2626                                          0,
2627                                          SLAB_HWCACHE_ALIGN,
2628                                          NULL);
2629         if (!iommu_iova_cache) {
2630                 printk(KERN_ERR "Couldn't create iova cache\n");
2631                 ret = -ENOMEM;
2632         }
2633
2634         return ret;
2635 }
2636
2637 static int __init iommu_init_mempool(void)
2638 {
2639         int ret;
2640         ret = iommu_iova_cache_init();
2641         if (ret)
2642                 return ret;
2643
2644         ret = iommu_domain_cache_init();
2645         if (ret)
2646                 goto domain_error;
2647
2648         ret = iommu_devinfo_cache_init();
2649         if (!ret)
2650                 return ret;
2651
2652         kmem_cache_destroy(iommu_domain_cache);
2653 domain_error:
2654         kmem_cache_destroy(iommu_iova_cache);
2655
2656         return -ENOMEM;
2657 }
2658
2659 static void __init iommu_exit_mempool(void)
2660 {
2661         kmem_cache_destroy(iommu_devinfo_cache);
2662         kmem_cache_destroy(iommu_domain_cache);
2663         kmem_cache_destroy(iommu_iova_cache);
2664
2665 }
2666
2667 static void __init init_no_remapping_devices(void)
2668 {
2669         struct dmar_drhd_unit *drhd;
2670
2671         for_each_drhd_unit(drhd) {
2672                 if (!drhd->include_all) {
2673                         int i;
2674                         for (i = 0; i < drhd->devices_cnt; i++)
2675                                 if (drhd->devices[i] != NULL)
2676                                         break;
2677                         /* ignore DMAR unit if no pci devices exist */
2678                         if (i == drhd->devices_cnt)
2679                                 drhd->ignored = 1;
2680                 }
2681         }
2682
2683         if (dmar_map_gfx)
2684                 return;
2685
2686         for_each_drhd_unit(drhd) {
2687                 int i;
2688                 if (drhd->ignored || drhd->include_all)
2689                         continue;
2690
2691                 for (i = 0; i < drhd->devices_cnt; i++)
2692                         if (drhd->devices[i] &&
2693                                 !IS_GFX_DEVICE(drhd->devices[i]))
2694                                 break;
2695
2696                 if (i < drhd->devices_cnt)
2697                         continue;
2698
2699                 /* bypass IOMMU if it is just for gfx devices */
2700                 drhd->ignored = 1;
2701                 for (i = 0; i < drhd->devices_cnt; i++) {
2702                         if (!drhd->devices[i])
2703                                 continue;
2704                         drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
2705                 }
2706         }
2707 }
2708
2709 #ifdef CONFIG_SUSPEND
2710 static int init_iommu_hw(void)
2711 {
2712         struct dmar_drhd_unit *drhd;
2713         struct intel_iommu *iommu = NULL;
2714
2715         for_each_active_iommu(iommu, drhd)
2716                 if (iommu->qi)
2717                         dmar_reenable_qi(iommu);
2718
2719         for_each_active_iommu(iommu, drhd) {
2720                 iommu_flush_write_buffer(iommu);
2721
2722                 iommu_set_root_entry(iommu);
2723
2724                 iommu->flush.flush_context(iommu, 0, 0, 0,
2725                                                 DMA_CCMD_GLOBAL_INVL, 0);
2726                 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2727                                                 DMA_TLB_GLOBAL_FLUSH, 0);
2728                 iommu_disable_protect_mem_regions(iommu);
2729                 iommu_enable_translation(iommu);
2730         }
2731
2732         return 0;
2733 }
2734
2735 static void iommu_flush_all(void)
2736 {
2737         struct dmar_drhd_unit *drhd;
2738         struct intel_iommu *iommu;
2739
2740         for_each_active_iommu(iommu, drhd) {
2741                 iommu->flush.flush_context(iommu, 0, 0, 0,
2742                                                 DMA_CCMD_GLOBAL_INVL, 0);
2743                 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2744                                                 DMA_TLB_GLOBAL_FLUSH, 0);
2745         }
2746 }
2747
2748 static int iommu_suspend(struct sys_device *dev, pm_message_t state)
2749 {
2750         struct dmar_drhd_unit *drhd;
2751         struct intel_iommu *iommu = NULL;
2752         unsigned long flag;
2753
2754         for_each_active_iommu(iommu, drhd) {
2755                 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
2756                                                  GFP_ATOMIC);
2757                 if (!iommu->iommu_state)
2758                         goto nomem;
2759         }
2760
2761         iommu_flush_all();
2762
2763         for_each_active_iommu(iommu, drhd) {
2764                 iommu_disable_translation(iommu);
2765
2766                 spin_lock_irqsave(&iommu->register_lock, flag);
2767
2768                 iommu->iommu_state[SR_DMAR_FECTL_REG] =
2769                         readl(iommu->reg + DMAR_FECTL_REG);
2770                 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
2771                         readl(iommu->reg + DMAR_FEDATA_REG);
2772                 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
2773                         readl(iommu->reg + DMAR_FEADDR_REG);
2774                 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
2775                         readl(iommu->reg + DMAR_FEUADDR_REG);
2776
2777                 spin_unlock_irqrestore(&iommu->register_lock, flag);
2778         }
2779         return 0;
2780
2781 nomem:
2782         for_each_active_iommu(iommu, drhd)
2783                 kfree(iommu->iommu_state);
2784
2785         return -ENOMEM;
2786 }
2787
2788 static int iommu_resume(struct sys_device *dev)
2789 {
2790         struct dmar_drhd_unit *drhd;
2791         struct intel_iommu *iommu = NULL;
2792         unsigned long flag;
2793
2794         if (init_iommu_hw()) {
2795                 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
2796                 return -EIO;
2797         }
2798
2799         for_each_active_iommu(iommu, drhd) {
2800
2801                 spin_lock_irqsave(&iommu->register_lock, flag);
2802
2803                 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
2804                         iommu->reg + DMAR_FECTL_REG);
2805                 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
2806                         iommu->reg + DMAR_FEDATA_REG);
2807                 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
2808                         iommu->reg + DMAR_FEADDR_REG);
2809                 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
2810                         iommu->reg + DMAR_FEUADDR_REG);
2811
2812                 spin_unlock_irqrestore(&iommu->register_lock, flag);
2813         }
2814
2815         for_each_active_iommu(iommu, drhd)
2816                 kfree(iommu->iommu_state);
2817
2818         return 0;
2819 }
2820
2821 static struct sysdev_class iommu_sysclass = {
2822         .name           = "iommu",
2823         .resume         = iommu_resume,
2824         .suspend        = iommu_suspend,
2825 };
2826
2827 static struct sys_device device_iommu = {
2828         .cls    = &iommu_sysclass,
2829 };
2830
2831 static int __init init_iommu_sysfs(void)
2832 {
2833         int error;
2834
2835         error = sysdev_class_register(&iommu_sysclass);
2836         if (error)
2837                 return error;
2838
2839         error = sysdev_register(&device_iommu);
2840         if (error)
2841                 sysdev_class_unregister(&iommu_sysclass);
2842
2843         return error;
2844 }
2845
2846 #else
2847 static int __init init_iommu_sysfs(void)
2848 {
2849         return 0;
2850 }
2851 #endif  /* CONFIG_PM */
2852
2853 int __init intel_iommu_init(void)
2854 {
2855         int ret = 0;
2856
2857         if (dmar_table_init())
2858                 return  -ENODEV;
2859
2860         if (dmar_dev_scope_init())
2861                 return  -ENODEV;
2862
2863         /*
2864          * Check the need for DMA-remapping initialization now.
2865          * Above initialization will also be used by Interrupt-remapping.
2866          */
2867         if (no_iommu || (swiotlb && !iommu_pass_through) || dmar_disabled)
2868                 return -ENODEV;
2869
2870         iommu_init_mempool();
2871         dmar_init_reserved_ranges();
2872
2873         init_no_remapping_devices();
2874
2875         ret = init_dmars();
2876         if (ret) {
2877                 printk(KERN_ERR "IOMMU: dmar init failed\n");
2878                 put_iova_domain(&reserved_iova_list);
2879                 iommu_exit_mempool();
2880                 return ret;
2881         }
2882         printk(KERN_INFO
2883         "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
2884
2885         init_timer(&unmap_timer);
2886         force_iommu = 1;
2887
2888         if (!iommu_pass_through) {
2889                 printk(KERN_INFO
2890                        "Multi-level page-table translation for DMAR.\n");
2891                 dma_ops = &intel_dma_ops;
2892         } else
2893                 printk(KERN_INFO
2894                        "DMAR: Pass through translation for DMAR.\n");
2895
2896         init_iommu_sysfs();
2897
2898         register_iommu(&intel_iommu_ops);
2899
2900         return 0;
2901 }
2902
2903 static int vm_domain_add_dev_info(struct dmar_domain *domain,
2904                                   struct pci_dev *pdev)
2905 {
2906         struct device_domain_info *info;
2907         unsigned long flags;
2908
2909         info = alloc_devinfo_mem();
2910         if (!info)
2911                 return -ENOMEM;
2912
2913         info->segment = pci_domain_nr(pdev->bus);
2914         info->bus = pdev->bus->number;
2915         info->devfn = pdev->devfn;
2916         info->dev = pdev;
2917         info->domain = domain;
2918
2919         spin_lock_irqsave(&device_domain_lock, flags);
2920         list_add(&info->link, &domain->devices);
2921         list_add(&info->global, &device_domain_list);
2922         pdev->dev.archdata.iommu = info;
2923         spin_unlock_irqrestore(&device_domain_lock, flags);
2924
2925         return 0;
2926 }
2927
2928 static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
2929                                            struct pci_dev *pdev)
2930 {
2931         struct pci_dev *tmp, *parent;
2932
2933         if (!iommu || !pdev)
2934                 return;
2935
2936         /* dependent device detach */
2937         tmp = pci_find_upstream_pcie_bridge(pdev);
2938         /* Secondary interface's bus number and devfn 0 */
2939         if (tmp) {
2940                 parent = pdev->bus->self;
2941                 while (parent != tmp) {
2942                         iommu_detach_dev(iommu, parent->bus->number,
2943                                          parent->devfn);
2944                         parent = parent->bus->self;
2945                 }
2946                 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
2947                         iommu_detach_dev(iommu,
2948                                 tmp->subordinate->number, 0);
2949                 else /* this is a legacy PCI bridge */
2950                         iommu_detach_dev(iommu, tmp->bus->number,
2951                                          tmp->devfn);
2952         }
2953 }
2954
2955 static void vm_domain_remove_one_dev_info(struct dmar_domain *domain,
2956                                           struct pci_dev *pdev)
2957 {
2958         struct device_domain_info *info;
2959         struct intel_iommu *iommu;
2960         unsigned long flags;
2961         int found = 0;
2962         struct list_head *entry, *tmp;
2963
2964         iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
2965                                 pdev->devfn);
2966         if (!iommu)
2967                 return;
2968
2969         spin_lock_irqsave(&device_domain_lock, flags);
2970         list_for_each_safe(entry, tmp, &domain->devices) {
2971                 info = list_entry(entry, struct device_domain_info, link);
2972                 /* No need to compare PCI domain; it has to be the same */
2973                 if (info->bus == pdev->bus->number &&
2974                     info->devfn == pdev->devfn) {
2975                         list_del(&info->link);
2976                         list_del(&info->global);
2977                         if (info->dev)
2978                                 info->dev->dev.archdata.iommu = NULL;
2979                         spin_unlock_irqrestore(&device_domain_lock, flags);
2980
2981                         iommu_detach_dev(iommu, info->bus, info->devfn);
2982                         iommu_detach_dependent_devices(iommu, pdev);
2983                         free_devinfo_mem(info);
2984
2985                         spin_lock_irqsave(&device_domain_lock, flags);
2986
2987                         if (found)
2988                                 break;
2989                         else
2990                                 continue;
2991                 }
2992
2993                 /* if there is no other devices under the same iommu
2994                  * owned by this domain, clear this iommu in iommu_bmp
2995                  * update iommu count and coherency
2996                  */
2997                 if (iommu == device_to_iommu(info->segment, info->bus,
2998                                             info->devfn))
2999                         found = 1;
3000         }
3001
3002         if (found == 0) {
3003                 unsigned long tmp_flags;
3004                 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
3005                 clear_bit(iommu->seq_id, &domain->iommu_bmp);
3006                 domain->iommu_count--;
3007                 domain_update_iommu_cap(domain);
3008                 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
3009         }
3010
3011         spin_unlock_irqrestore(&device_domain_lock, flags);
3012 }
3013
3014 static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
3015 {
3016         struct device_domain_info *info;
3017         struct intel_iommu *iommu;
3018         unsigned long flags1, flags2;
3019
3020         spin_lock_irqsave(&device_domain_lock, flags1);
3021         while (!list_empty(&domain->devices)) {
3022                 info = list_entry(domain->devices.next,
3023                         struct device_domain_info, link);
3024                 list_del(&info->link);
3025                 list_del(&info->global);
3026                 if (info->dev)
3027                         info->dev->dev.archdata.iommu = NULL;
3028
3029                 spin_unlock_irqrestore(&device_domain_lock, flags1);
3030
3031                 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
3032                 iommu_detach_dev(iommu, info->bus, info->devfn);
3033                 iommu_detach_dependent_devices(iommu, info->dev);
3034
3035                 /* clear this iommu in iommu_bmp, update iommu count
3036                  * and capabilities
3037                  */
3038                 spin_lock_irqsave(&domain->iommu_lock, flags2);
3039                 if (test_and_clear_bit(iommu->seq_id,
3040                                        &domain->iommu_bmp)) {
3041                         domain->iommu_count--;
3042                         domain_update_iommu_cap(domain);
3043                 }
3044                 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3045
3046                 free_devinfo_mem(info);
3047                 spin_lock_irqsave(&device_domain_lock, flags1);
3048         }
3049         spin_unlock_irqrestore(&device_domain_lock, flags1);
3050 }
3051
3052 /* domain id for virtual machine, it won't be set in context */
3053 static unsigned long vm_domid;
3054
3055 static int vm_domain_min_agaw(struct dmar_domain *domain)
3056 {
3057         int i;
3058         int min_agaw = domain->agaw;
3059
3060         i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
3061         for (; i < g_num_of_iommus; ) {
3062                 if (min_agaw > g_iommus[i]->agaw)
3063                         min_agaw = g_iommus[i]->agaw;
3064
3065                 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
3066         }
3067
3068         return min_agaw;
3069 }
3070
3071 static struct dmar_domain *iommu_alloc_vm_domain(void)
3072 {
3073         struct dmar_domain *domain;
3074
3075         domain = alloc_domain_mem();
3076         if (!domain)
3077                 return NULL;
3078
3079         domain->id = vm_domid++;
3080         memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3081         domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3082
3083         return domain;
3084 }
3085
3086 static int vm_domain_init(struct dmar_domain *domain, int guest_width)
3087 {
3088         int adjust_width;
3089
3090         init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3091         spin_lock_init(&domain->mapping_lock);
3092         spin_lock_init(&domain->iommu_lock);
3093
3094         domain_reserve_special_ranges(domain);
3095
3096         /* calculate AGAW */
3097         domain->gaw = guest_width;
3098         adjust_width = guestwidth_to_adjustwidth(guest_width);
3099         domain->agaw = width_to_agaw(adjust_width);
3100
3101         INIT_LIST_HEAD(&domain->devices);
3102
3103         domain->iommu_count = 0;
3104         domain->iommu_coherency = 0;
3105         domain->max_addr = 0;
3106
3107         /* always allocate the top pgd */
3108         domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3109         if (!domain->pgd)
3110                 return -ENOMEM;
3111         domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3112         return 0;
3113 }
3114
3115 static void iommu_free_vm_domain(struct dmar_domain *domain)
3116 {
3117         unsigned long flags;
3118         struct dmar_drhd_unit *drhd;
3119         struct intel_iommu *iommu;
3120         unsigned long i;
3121         unsigned long ndomains;
3122
3123         for_each_drhd_unit(drhd) {
3124                 if (drhd->ignored)
3125                         continue;
3126                 iommu = drhd->iommu;
3127
3128                 ndomains = cap_ndoms(iommu->cap);
3129                 i = find_first_bit(iommu->domain_ids, ndomains);
3130                 for (; i < ndomains; ) {
3131                         if (iommu->domains[i] == domain) {
3132                                 spin_lock_irqsave(&iommu->lock, flags);
3133                                 clear_bit(i, iommu->domain_ids);
3134                                 iommu->domains[i] = NULL;
3135                                 spin_unlock_irqrestore(&iommu->lock, flags);
3136                                 break;
3137                         }
3138                         i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3139                 }
3140         }
3141 }
3142
3143 static void vm_domain_exit(struct dmar_domain *domain)
3144 {
3145         u64 end;
3146
3147         /* Domain 0 is reserved, so dont process it */
3148         if (!domain)
3149                 return;
3150
3151         vm_domain_remove_all_dev_info(domain);
3152         /* destroy iovas */
3153         put_iova_domain(&domain->iovad);
3154         end = DOMAIN_MAX_ADDR(domain->gaw);
3155         end = end & (~VTD_PAGE_MASK);
3156
3157         /* clear ptes */
3158         dma_pte_clear_range(domain, 0, end);
3159
3160         /* free page tables */
3161         dma_pte_free_pagetable(domain, 0, end);
3162
3163         iommu_free_vm_domain(domain);
3164         free_domain_mem(domain);
3165 }
3166
3167 static int intel_iommu_domain_init(struct iommu_domain *domain)
3168 {
3169         struct dmar_domain *dmar_domain;
3170
3171         dmar_domain = iommu_alloc_vm_domain();
3172         if (!dmar_domain) {
3173                 printk(KERN_ERR
3174                         "intel_iommu_domain_init: dmar_domain == NULL\n");
3175                 return -ENOMEM;
3176         }
3177         if (vm_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
3178                 printk(KERN_ERR
3179                         "intel_iommu_domain_init() failed\n");
3180                 vm_domain_exit(dmar_domain);
3181                 return -ENOMEM;
3182         }
3183         domain->priv = dmar_domain;
3184
3185         return 0;
3186 }
3187
3188 static void intel_iommu_domain_destroy(struct iommu_domain *domain)
3189 {
3190         struct dmar_domain *dmar_domain = domain->priv;
3191
3192         domain->priv = NULL;
3193         vm_domain_exit(dmar_domain);
3194 }
3195
3196 static int intel_iommu_attach_device(struct iommu_domain *domain,
3197                                      struct device *dev)
3198 {
3199         struct dmar_domain *dmar_domain = domain->priv;
3200         struct pci_dev *pdev = to_pci_dev(dev);
3201         struct intel_iommu *iommu;
3202         int addr_width;
3203         u64 end;
3204         int ret;
3205
3206         /* normally pdev is not mapped */
3207         if (unlikely(domain_context_mapped(pdev))) {
3208                 struct dmar_domain *old_domain;
3209
3210                 old_domain = find_domain(pdev);
3211                 if (old_domain) {
3212                         if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
3213                                 vm_domain_remove_one_dev_info(old_domain, pdev);
3214                         else
3215                                 domain_remove_dev_info(old_domain);
3216                 }
3217         }
3218
3219         iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3220                                 pdev->devfn);
3221         if (!iommu)
3222                 return -ENODEV;
3223
3224         /* check if this iommu agaw is sufficient for max mapped address */
3225         addr_width = agaw_to_width(iommu->agaw);
3226         end = DOMAIN_MAX_ADDR(addr_width);
3227         end = end & VTD_PAGE_MASK;
3228         if (end < dmar_domain->max_addr) {
3229                 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3230                        "sufficient for the mapped address (%llx)\n",
3231                        __func__, iommu->agaw, dmar_domain->max_addr);
3232                 return -EFAULT;
3233         }
3234
3235         ret = domain_context_mapping(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
3236         if (ret)
3237                 return ret;
3238
3239         ret = vm_domain_add_dev_info(dmar_domain, pdev);
3240         return ret;
3241 }
3242
3243 static void intel_iommu_detach_device(struct iommu_domain *domain,
3244                                       struct device *dev)
3245 {
3246         struct dmar_domain *dmar_domain = domain->priv;
3247         struct pci_dev *pdev = to_pci_dev(dev);
3248
3249         vm_domain_remove_one_dev_info(dmar_domain, pdev);
3250 }
3251
3252 static int intel_iommu_map_range(struct iommu_domain *domain,
3253                                  unsigned long iova, phys_addr_t hpa,
3254                                  size_t size, int iommu_prot)
3255 {
3256         struct dmar_domain *dmar_domain = domain->priv;
3257         u64 max_addr;
3258         int addr_width;
3259         int prot = 0;
3260         int ret;
3261
3262         if (iommu_prot & IOMMU_READ)
3263                 prot |= DMA_PTE_READ;
3264         if (iommu_prot & IOMMU_WRITE)
3265                 prot |= DMA_PTE_WRITE;
3266         if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3267                 prot |= DMA_PTE_SNP;
3268
3269         max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
3270         if (dmar_domain->max_addr < max_addr) {
3271                 int min_agaw;
3272                 u64 end;
3273
3274                 /* check if minimum agaw is sufficient for mapped address */
3275                 min_agaw = vm_domain_min_agaw(dmar_domain);
3276                 addr_width = agaw_to_width(min_agaw);
3277                 end = DOMAIN_MAX_ADDR(addr_width);
3278                 end = end & VTD_PAGE_MASK;
3279                 if (end < max_addr) {
3280                         printk(KERN_ERR "%s: iommu agaw (%d) is not "
3281                                "sufficient for the mapped address (%llx)\n",
3282                                __func__, min_agaw, max_addr);
3283                         return -EFAULT;
3284                 }
3285                 dmar_domain->max_addr = max_addr;
3286         }
3287
3288         ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
3289         return ret;
3290 }
3291
3292 static void intel_iommu_unmap_range(struct iommu_domain *domain,
3293                                     unsigned long iova, size_t size)
3294 {
3295         struct dmar_domain *dmar_domain = domain->priv;
3296         dma_addr_t base;
3297
3298         /* The address might not be aligned */
3299         base = iova & VTD_PAGE_MASK;
3300         size = VTD_PAGE_ALIGN(size);
3301         dma_pte_clear_range(dmar_domain, base, base + size);
3302
3303         if (dmar_domain->max_addr == base + size)
3304                 dmar_domain->max_addr = base;
3305 }
3306
3307 static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3308                                             unsigned long iova)
3309 {
3310         struct dmar_domain *dmar_domain = domain->priv;
3311         struct dma_pte *pte;
3312         u64 phys = 0;
3313
3314         pte = addr_to_dma_pte(dmar_domain, iova);
3315         if (pte)
3316                 phys = dma_pte_addr(pte);
3317
3318         return phys;
3319 }
3320
3321 static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3322                                       unsigned long cap)
3323 {
3324         struct dmar_domain *dmar_domain = domain->priv;
3325
3326         if (cap == IOMMU_CAP_CACHE_COHERENCY)
3327                 return dmar_domain->iommu_snooping;
3328
3329         return 0;
3330 }
3331
3332 static struct iommu_ops intel_iommu_ops = {
3333         .domain_init    = intel_iommu_domain_init,
3334         .domain_destroy = intel_iommu_domain_destroy,
3335         .attach_dev     = intel_iommu_attach_device,
3336         .detach_dev     = intel_iommu_detach_device,
3337         .map            = intel_iommu_map_range,
3338         .unmap          = intel_iommu_unmap_range,
3339         .iova_to_phys   = intel_iommu_iova_to_phys,
3340         .domain_has_cap = intel_iommu_domain_has_cap,
3341 };
3342
3343 static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3344 {
3345         /*
3346          * Mobile 4 Series Chipset neglects to set RWBF capability,
3347          * but needs it:
3348          */
3349         printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3350         rwbf_quirk = 1;
3351 }
3352
3353 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);