2 * Copyright (c) 2006, Intel Corporation.
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.
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
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.
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>
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/intel-iommu.h>
38 #include <asm/cacheflush.h>
39 #include <asm/iommu.h>
42 #define ROOT_SIZE VTD_PAGE_SIZE
43 #define CONTEXT_SIZE VTD_PAGE_SIZE
45 #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
46 #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
48 #define IOAPIC_RANGE_START (0xfee00000)
49 #define IOAPIC_RANGE_END (0xfeefffff)
50 #define IOVA_START_ADDR (0x1000)
52 #define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
54 #define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
56 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
57 #define DMA_32BIT_PFN IOVA_PFN(DMA_32BIT_MASK)
58 #define DMA_64BIT_PFN IOVA_PFN(DMA_64BIT_MASK)
60 /* global iommu list, set NULL for ignored DMAR units */
61 static struct intel_iommu **g_iommus;
66 * 12-63: Context Ptr (12 - (haw-1))
73 #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
74 static inline bool root_present(struct root_entry *root)
76 return (root->val & 1);
78 static inline void set_root_present(struct root_entry *root)
82 static inline void set_root_value(struct root_entry *root, unsigned long value)
84 root->val |= value & VTD_PAGE_MASK;
87 static inline struct context_entry *
88 get_context_addr_from_root(struct root_entry *root)
90 return (struct context_entry *)
91 (root_present(root)?phys_to_virt(
92 root->val & VTD_PAGE_MASK) :
99 * 1: fault processing disable
100 * 2-3: translation type
101 * 12-63: address space root
107 struct context_entry {
112 static inline bool context_present(struct context_entry *context)
114 return (context->lo & 1);
116 static inline void context_set_present(struct context_entry *context)
121 static inline void context_set_fault_enable(struct context_entry *context)
123 context->lo &= (((u64)-1) << 2) | 1;
126 #define CONTEXT_TT_MULTI_LEVEL 0
128 static inline void context_set_translation_type(struct context_entry *context,
131 context->lo &= (((u64)-1) << 4) | 3;
132 context->lo |= (value & 3) << 2;
135 static inline void context_set_address_root(struct context_entry *context,
138 context->lo |= value & VTD_PAGE_MASK;
141 static inline void context_set_address_width(struct context_entry *context,
144 context->hi |= value & 7;
147 static inline void context_set_domain_id(struct context_entry *context,
150 context->hi |= (value & ((1 << 16) - 1)) << 8;
153 static inline void context_clear_entry(struct context_entry *context)
165 * 12-63: Host physcial address
171 static inline void dma_clear_pte(struct dma_pte *pte)
176 static inline void dma_set_pte_readable(struct dma_pte *pte)
178 pte->val |= DMA_PTE_READ;
181 static inline void dma_set_pte_writable(struct dma_pte *pte)
183 pte->val |= DMA_PTE_WRITE;
186 static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
188 pte->val = (pte->val & ~3) | (prot & 3);
191 static inline u64 dma_pte_addr(struct dma_pte *pte)
193 return (pte->val & VTD_PAGE_MASK);
196 static inline void dma_set_pte_addr(struct dma_pte *pte, u64 addr)
198 pte->val |= (addr & VTD_PAGE_MASK);
201 static inline bool dma_pte_present(struct dma_pte *pte)
203 return (pte->val & 3) != 0;
206 /* devices under the same p2p bridge are owned in one domain */
207 #define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 < 0)
209 /* domain represents a virtual machine, more than one devices
210 * across iommus may be owned in one domain, e.g. kvm guest.
212 #define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
215 int id; /* domain id */
216 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
218 struct list_head devices; /* all devices' list */
219 struct iova_domain iovad; /* iova's that belong to this domain */
221 struct dma_pte *pgd; /* virtual address */
222 spinlock_t mapping_lock; /* page table lock */
223 int gaw; /* max guest address width */
225 /* adjusted guest address width, 0 is level 2 30-bit */
228 int flags; /* flags to find out type of domain */
230 int iommu_coherency;/* indicate coherency of iommu access */
233 /* PCI domain-device relationship */
234 struct device_domain_info {
235 struct list_head link; /* link to domain siblings */
236 struct list_head global; /* link to global list */
237 u8 bus; /* PCI bus numer */
238 u8 devfn; /* PCI devfn number */
239 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
240 struct dmar_domain *domain; /* pointer to domain */
243 static void flush_unmaps_timeout(unsigned long data);
245 DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
247 #define HIGH_WATER_MARK 250
248 struct deferred_flush_tables {
250 struct iova *iova[HIGH_WATER_MARK];
251 struct dmar_domain *domain[HIGH_WATER_MARK];
254 static struct deferred_flush_tables *deferred_flush;
256 /* bitmap for indexing intel_iommus */
257 static int g_num_of_iommus;
259 static DEFINE_SPINLOCK(async_umap_flush_lock);
260 static LIST_HEAD(unmaps_to_do);
263 static long list_size;
265 static void domain_remove_dev_info(struct dmar_domain *domain);
268 static int __initdata dmar_map_gfx = 1;
269 static int dmar_forcedac;
270 static int intel_iommu_strict;
272 #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
273 static DEFINE_SPINLOCK(device_domain_lock);
274 static LIST_HEAD(device_domain_list);
276 static int __init intel_iommu_setup(char *str)
281 if (!strncmp(str, "off", 3)) {
283 printk(KERN_INFO"Intel-IOMMU: disabled\n");
284 } else if (!strncmp(str, "igfx_off", 8)) {
287 "Intel-IOMMU: disable GFX device mapping\n");
288 } else if (!strncmp(str, "forcedac", 8)) {
290 "Intel-IOMMU: Forcing DAC for PCI devices\n");
292 } else if (!strncmp(str, "strict", 6)) {
294 "Intel-IOMMU: disable batched IOTLB flush\n");
295 intel_iommu_strict = 1;
298 str += strcspn(str, ",");
304 __setup("intel_iommu=", intel_iommu_setup);
306 static struct kmem_cache *iommu_domain_cache;
307 static struct kmem_cache *iommu_devinfo_cache;
308 static struct kmem_cache *iommu_iova_cache;
310 static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
315 /* trying to avoid low memory issues */
316 flags = current->flags & PF_MEMALLOC;
317 current->flags |= PF_MEMALLOC;
318 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
319 current->flags &= (~PF_MEMALLOC | flags);
324 static inline void *alloc_pgtable_page(void)
329 /* trying to avoid low memory issues */
330 flags = current->flags & PF_MEMALLOC;
331 current->flags |= PF_MEMALLOC;
332 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
333 current->flags &= (~PF_MEMALLOC | flags);
337 static inline void free_pgtable_page(void *vaddr)
339 free_page((unsigned long)vaddr);
342 static inline void *alloc_domain_mem(void)
344 return iommu_kmem_cache_alloc(iommu_domain_cache);
347 static void free_domain_mem(void *vaddr)
349 kmem_cache_free(iommu_domain_cache, vaddr);
352 static inline void * alloc_devinfo_mem(void)
354 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
357 static inline void free_devinfo_mem(void *vaddr)
359 kmem_cache_free(iommu_devinfo_cache, vaddr);
362 struct iova *alloc_iova_mem(void)
364 return iommu_kmem_cache_alloc(iommu_iova_cache);
367 void free_iova_mem(struct iova *iova)
369 kmem_cache_free(iommu_iova_cache, iova);
373 static inline int width_to_agaw(int width);
375 /* calculate agaw for each iommu.
376 * "SAGAW" may be different across iommus, use a default agaw, and
377 * get a supported less agaw for iommus that don't support the default agaw.
379 int iommu_calculate_agaw(struct intel_iommu *iommu)
384 sagaw = cap_sagaw(iommu->cap);
385 for (agaw = width_to_agaw(DEFAULT_DOMAIN_ADDRESS_WIDTH);
387 if (test_bit(agaw, &sagaw))
394 /* in native case, each domain is related to only one iommu */
395 static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
399 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
401 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
402 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
405 return g_iommus[iommu_id];
408 /* "Coherency" capability may be different across iommus */
409 static void domain_update_iommu_coherency(struct dmar_domain *domain)
413 domain->iommu_coherency = 1;
415 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
416 for (; i < g_num_of_iommus; ) {
417 if (!ecap_coherent(g_iommus[i]->ecap)) {
418 domain->iommu_coherency = 0;
421 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
425 /* Gets context entry for a given bus and devfn */
426 static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
429 struct root_entry *root;
430 struct context_entry *context;
431 unsigned long phy_addr;
434 spin_lock_irqsave(&iommu->lock, flags);
435 root = &iommu->root_entry[bus];
436 context = get_context_addr_from_root(root);
438 context = (struct context_entry *)alloc_pgtable_page();
440 spin_unlock_irqrestore(&iommu->lock, flags);
443 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
444 phy_addr = virt_to_phys((void *)context);
445 set_root_value(root, phy_addr);
446 set_root_present(root);
447 __iommu_flush_cache(iommu, root, sizeof(*root));
449 spin_unlock_irqrestore(&iommu->lock, flags);
450 return &context[devfn];
453 static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
455 struct root_entry *root;
456 struct context_entry *context;
460 spin_lock_irqsave(&iommu->lock, flags);
461 root = &iommu->root_entry[bus];
462 context = get_context_addr_from_root(root);
467 ret = context_present(&context[devfn]);
469 spin_unlock_irqrestore(&iommu->lock, flags);
473 static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
475 struct root_entry *root;
476 struct context_entry *context;
479 spin_lock_irqsave(&iommu->lock, flags);
480 root = &iommu->root_entry[bus];
481 context = get_context_addr_from_root(root);
483 context_clear_entry(&context[devfn]);
484 __iommu_flush_cache(iommu, &context[devfn], \
487 spin_unlock_irqrestore(&iommu->lock, flags);
490 static void free_context_table(struct intel_iommu *iommu)
492 struct root_entry *root;
495 struct context_entry *context;
497 spin_lock_irqsave(&iommu->lock, flags);
498 if (!iommu->root_entry) {
501 for (i = 0; i < ROOT_ENTRY_NR; i++) {
502 root = &iommu->root_entry[i];
503 context = get_context_addr_from_root(root);
505 free_pgtable_page(context);
507 free_pgtable_page(iommu->root_entry);
508 iommu->root_entry = NULL;
510 spin_unlock_irqrestore(&iommu->lock, flags);
513 /* page table handling */
514 #define LEVEL_STRIDE (9)
515 #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
517 static inline int agaw_to_level(int agaw)
522 static inline int agaw_to_width(int agaw)
524 return 30 + agaw * LEVEL_STRIDE;
528 static inline int width_to_agaw(int width)
530 return (width - 30) / LEVEL_STRIDE;
533 static inline unsigned int level_to_offset_bits(int level)
535 return (12 + (level - 1) * LEVEL_STRIDE);
538 static inline int address_level_offset(u64 addr, int level)
540 return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
543 static inline u64 level_mask(int level)
545 return ((u64)-1 << level_to_offset_bits(level));
548 static inline u64 level_size(int level)
550 return ((u64)1 << level_to_offset_bits(level));
553 static inline u64 align_to_level(u64 addr, int level)
555 return ((addr + level_size(level) - 1) & level_mask(level));
558 static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
560 int addr_width = agaw_to_width(domain->agaw);
561 struct dma_pte *parent, *pte = NULL;
562 int level = agaw_to_level(domain->agaw);
565 struct intel_iommu *iommu = domain_get_iommu(domain);
567 BUG_ON(!domain->pgd);
569 addr &= (((u64)1) << addr_width) - 1;
570 parent = domain->pgd;
572 spin_lock_irqsave(&domain->mapping_lock, flags);
576 offset = address_level_offset(addr, level);
577 pte = &parent[offset];
581 if (!dma_pte_present(pte)) {
582 tmp_page = alloc_pgtable_page();
585 spin_unlock_irqrestore(&domain->mapping_lock,
589 __iommu_flush_cache(iommu, tmp_page,
591 dma_set_pte_addr(pte, virt_to_phys(tmp_page));
593 * high level table always sets r/w, last level page
594 * table control read/write
596 dma_set_pte_readable(pte);
597 dma_set_pte_writable(pte);
598 __iommu_flush_cache(iommu, pte, sizeof(*pte));
600 parent = phys_to_virt(dma_pte_addr(pte));
604 spin_unlock_irqrestore(&domain->mapping_lock, flags);
608 /* return address's pte at specific level */
609 static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
612 struct dma_pte *parent, *pte = NULL;
613 int total = agaw_to_level(domain->agaw);
616 parent = domain->pgd;
617 while (level <= total) {
618 offset = address_level_offset(addr, total);
619 pte = &parent[offset];
623 if (!dma_pte_present(pte))
625 parent = phys_to_virt(dma_pte_addr(pte));
631 /* clear one page's page table */
632 static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
634 struct dma_pte *pte = NULL;
635 struct intel_iommu *iommu = domain_get_iommu(domain);
637 /* get last level pte */
638 pte = dma_addr_level_pte(domain, addr, 1);
642 __iommu_flush_cache(iommu, pte, sizeof(*pte));
646 /* clear last level pte, a tlb flush should be followed */
647 static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
649 int addr_width = agaw_to_width(domain->agaw);
651 start &= (((u64)1) << addr_width) - 1;
652 end &= (((u64)1) << addr_width) - 1;
653 /* in case it's partial page */
654 start = PAGE_ALIGN(start);
657 /* we don't need lock here, nobody else touches the iova range */
658 while (start < end) {
659 dma_pte_clear_one(domain, start);
660 start += VTD_PAGE_SIZE;
664 /* free page table pages. last level pte should already be cleared */
665 static void dma_pte_free_pagetable(struct dmar_domain *domain,
668 int addr_width = agaw_to_width(domain->agaw);
670 int total = agaw_to_level(domain->agaw);
673 struct intel_iommu *iommu = domain_get_iommu(domain);
675 start &= (((u64)1) << addr_width) - 1;
676 end &= (((u64)1) << addr_width) - 1;
678 /* we don't need lock here, nobody else touches the iova range */
680 while (level <= total) {
681 tmp = align_to_level(start, level);
682 if (tmp >= end || (tmp + level_size(level) > end))
686 pte = dma_addr_level_pte(domain, tmp, level);
689 phys_to_virt(dma_pte_addr(pte)));
691 __iommu_flush_cache(iommu,
694 tmp += level_size(level);
699 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
700 free_pgtable_page(domain->pgd);
706 static int iommu_alloc_root_entry(struct intel_iommu *iommu)
708 struct root_entry *root;
711 root = (struct root_entry *)alloc_pgtable_page();
715 __iommu_flush_cache(iommu, root, ROOT_SIZE);
717 spin_lock_irqsave(&iommu->lock, flags);
718 iommu->root_entry = root;
719 spin_unlock_irqrestore(&iommu->lock, flags);
724 static void iommu_set_root_entry(struct intel_iommu *iommu)
730 addr = iommu->root_entry;
732 spin_lock_irqsave(&iommu->register_lock, flag);
733 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
735 cmd = iommu->gcmd | DMA_GCMD_SRTP;
736 writel(cmd, iommu->reg + DMAR_GCMD_REG);
738 /* Make sure hardware complete it */
739 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
740 readl, (sts & DMA_GSTS_RTPS), sts);
742 spin_unlock_irqrestore(&iommu->register_lock, flag);
745 static void iommu_flush_write_buffer(struct intel_iommu *iommu)
750 if (!cap_rwbf(iommu->cap))
752 val = iommu->gcmd | DMA_GCMD_WBF;
754 spin_lock_irqsave(&iommu->register_lock, flag);
755 writel(val, iommu->reg + DMAR_GCMD_REG);
757 /* Make sure hardware complete it */
758 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
759 readl, (!(val & DMA_GSTS_WBFS)), val);
761 spin_unlock_irqrestore(&iommu->register_lock, flag);
764 /* return value determine if we need a write buffer flush */
765 static int __iommu_flush_context(struct intel_iommu *iommu,
766 u16 did, u16 source_id, u8 function_mask, u64 type,
767 int non_present_entry_flush)
773 * In the non-present entry flush case, if hardware doesn't cache
774 * non-present entry we do nothing and if hardware cache non-present
775 * entry, we flush entries of domain 0 (the domain id is used to cache
776 * any non-present entries)
778 if (non_present_entry_flush) {
779 if (!cap_caching_mode(iommu->cap))
786 case DMA_CCMD_GLOBAL_INVL:
787 val = DMA_CCMD_GLOBAL_INVL;
789 case DMA_CCMD_DOMAIN_INVL:
790 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
792 case DMA_CCMD_DEVICE_INVL:
793 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
794 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
801 spin_lock_irqsave(&iommu->register_lock, flag);
802 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
804 /* Make sure hardware complete it */
805 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
806 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
808 spin_unlock_irqrestore(&iommu->register_lock, flag);
810 /* flush context entry will implicitly flush write buffer */
814 /* return value determine if we need a write buffer flush */
815 static int __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
816 u64 addr, unsigned int size_order, u64 type,
817 int non_present_entry_flush)
819 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
820 u64 val = 0, val_iva = 0;
824 * In the non-present entry flush case, if hardware doesn't cache
825 * non-present entry we do nothing and if hardware cache non-present
826 * entry, we flush entries of domain 0 (the domain id is used to cache
827 * any non-present entries)
829 if (non_present_entry_flush) {
830 if (!cap_caching_mode(iommu->cap))
837 case DMA_TLB_GLOBAL_FLUSH:
838 /* global flush doesn't need set IVA_REG */
839 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
841 case DMA_TLB_DSI_FLUSH:
842 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
844 case DMA_TLB_PSI_FLUSH:
845 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
846 /* Note: always flush non-leaf currently */
847 val_iva = size_order | addr;
852 /* Note: set drain read/write */
855 * This is probably to be super secure.. Looks like we can
856 * ignore it without any impact.
858 if (cap_read_drain(iommu->cap))
859 val |= DMA_TLB_READ_DRAIN;
861 if (cap_write_drain(iommu->cap))
862 val |= DMA_TLB_WRITE_DRAIN;
864 spin_lock_irqsave(&iommu->register_lock, flag);
865 /* Note: Only uses first TLB reg currently */
867 dmar_writeq(iommu->reg + tlb_offset, val_iva);
868 dmar_writeq(iommu->reg + tlb_offset + 8, val);
870 /* Make sure hardware complete it */
871 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
872 dmar_readq, (!(val & DMA_TLB_IVT)), val);
874 spin_unlock_irqrestore(&iommu->register_lock, flag);
876 /* check IOTLB invalidation granularity */
877 if (DMA_TLB_IAIG(val) == 0)
878 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
879 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
880 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
881 (unsigned long long)DMA_TLB_IIRG(type),
882 (unsigned long long)DMA_TLB_IAIG(val));
883 /* flush iotlb entry will implicitly flush write buffer */
887 static int iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
888 u64 addr, unsigned int pages, int non_present_entry_flush)
892 BUG_ON(addr & (~VTD_PAGE_MASK));
895 /* Fallback to domain selective flush if no PSI support */
896 if (!cap_pgsel_inv(iommu->cap))
897 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
899 non_present_entry_flush);
902 * PSI requires page size to be 2 ^ x, and the base address is naturally
903 * aligned to the size
905 mask = ilog2(__roundup_pow_of_two(pages));
906 /* Fallback to domain selective flush if size is too big */
907 if (mask > cap_max_amask_val(iommu->cap))
908 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
909 DMA_TLB_DSI_FLUSH, non_present_entry_flush);
911 return iommu->flush.flush_iotlb(iommu, did, addr, mask,
913 non_present_entry_flush);
916 static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
921 spin_lock_irqsave(&iommu->register_lock, flags);
922 pmen = readl(iommu->reg + DMAR_PMEN_REG);
923 pmen &= ~DMA_PMEN_EPM;
924 writel(pmen, iommu->reg + DMAR_PMEN_REG);
926 /* wait for the protected region status bit to clear */
927 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
928 readl, !(pmen & DMA_PMEN_PRS), pmen);
930 spin_unlock_irqrestore(&iommu->register_lock, flags);
933 static int iommu_enable_translation(struct intel_iommu *iommu)
938 spin_lock_irqsave(&iommu->register_lock, flags);
939 writel(iommu->gcmd|DMA_GCMD_TE, iommu->reg + DMAR_GCMD_REG);
941 /* Make sure hardware complete it */
942 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
943 readl, (sts & DMA_GSTS_TES), sts);
945 iommu->gcmd |= DMA_GCMD_TE;
946 spin_unlock_irqrestore(&iommu->register_lock, flags);
950 static int iommu_disable_translation(struct intel_iommu *iommu)
955 spin_lock_irqsave(&iommu->register_lock, flag);
956 iommu->gcmd &= ~DMA_GCMD_TE;
957 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
959 /* Make sure hardware complete it */
960 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
961 readl, (!(sts & DMA_GSTS_TES)), sts);
963 spin_unlock_irqrestore(&iommu->register_lock, flag);
967 /* iommu interrupt handling. Most stuff are MSI-like. */
969 static const char *fault_reason_strings[] =
972 "Present bit in root entry is clear",
973 "Present bit in context entry is clear",
974 "Invalid context entry",
975 "Access beyond MGAW",
976 "PTE Write access is not set",
977 "PTE Read access is not set",
978 "Next page table ptr is invalid",
979 "Root table address invalid",
980 "Context table ptr is invalid",
981 "non-zero reserved fields in RTP",
982 "non-zero reserved fields in CTP",
983 "non-zero reserved fields in PTE",
985 #define MAX_FAULT_REASON_IDX (ARRAY_SIZE(fault_reason_strings) - 1)
987 const char *dmar_get_fault_reason(u8 fault_reason)
989 if (fault_reason > MAX_FAULT_REASON_IDX)
992 return fault_reason_strings[fault_reason];
995 void dmar_msi_unmask(unsigned int irq)
997 struct intel_iommu *iommu = get_irq_data(irq);
1001 spin_lock_irqsave(&iommu->register_lock, flag);
1002 writel(0, iommu->reg + DMAR_FECTL_REG);
1003 /* Read a reg to force flush the post write */
1004 readl(iommu->reg + DMAR_FECTL_REG);
1005 spin_unlock_irqrestore(&iommu->register_lock, flag);
1008 void dmar_msi_mask(unsigned int irq)
1011 struct intel_iommu *iommu = get_irq_data(irq);
1014 spin_lock_irqsave(&iommu->register_lock, flag);
1015 writel(DMA_FECTL_IM, iommu->reg + DMAR_FECTL_REG);
1016 /* Read a reg to force flush the post write */
1017 readl(iommu->reg + DMAR_FECTL_REG);
1018 spin_unlock_irqrestore(&iommu->register_lock, flag);
1021 void dmar_msi_write(int irq, struct msi_msg *msg)
1023 struct intel_iommu *iommu = get_irq_data(irq);
1026 spin_lock_irqsave(&iommu->register_lock, flag);
1027 writel(msg->data, iommu->reg + DMAR_FEDATA_REG);
1028 writel(msg->address_lo, iommu->reg + DMAR_FEADDR_REG);
1029 writel(msg->address_hi, iommu->reg + DMAR_FEUADDR_REG);
1030 spin_unlock_irqrestore(&iommu->register_lock, flag);
1033 void dmar_msi_read(int irq, struct msi_msg *msg)
1035 struct intel_iommu *iommu = get_irq_data(irq);
1038 spin_lock_irqsave(&iommu->register_lock, flag);
1039 msg->data = readl(iommu->reg + DMAR_FEDATA_REG);
1040 msg->address_lo = readl(iommu->reg + DMAR_FEADDR_REG);
1041 msg->address_hi = readl(iommu->reg + DMAR_FEUADDR_REG);
1042 spin_unlock_irqrestore(&iommu->register_lock, flag);
1045 static int iommu_page_fault_do_one(struct intel_iommu *iommu, int type,
1046 u8 fault_reason, u16 source_id, unsigned long long addr)
1050 reason = dmar_get_fault_reason(fault_reason);
1053 "DMAR:[%s] Request device [%02x:%02x.%d] "
1054 "fault addr %llx \n"
1055 "DMAR:[fault reason %02d] %s\n",
1056 (type ? "DMA Read" : "DMA Write"),
1057 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1058 PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason);
1062 #define PRIMARY_FAULT_REG_LEN (16)
1063 static irqreturn_t iommu_page_fault(int irq, void *dev_id)
1065 struct intel_iommu *iommu = dev_id;
1066 int reg, fault_index;
1070 spin_lock_irqsave(&iommu->register_lock, flag);
1071 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1073 /* TBD: ignore advanced fault log currently */
1074 if (!(fault_status & DMA_FSTS_PPF))
1075 goto clear_overflow;
1077 fault_index = dma_fsts_fault_record_index(fault_status);
1078 reg = cap_fault_reg_offset(iommu->cap);
1086 /* highest 32 bits */
1087 data = readl(iommu->reg + reg +
1088 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1089 if (!(data & DMA_FRCD_F))
1092 fault_reason = dma_frcd_fault_reason(data);
1093 type = dma_frcd_type(data);
1095 data = readl(iommu->reg + reg +
1096 fault_index * PRIMARY_FAULT_REG_LEN + 8);
1097 source_id = dma_frcd_source_id(data);
1099 guest_addr = dmar_readq(iommu->reg + reg +
1100 fault_index * PRIMARY_FAULT_REG_LEN);
1101 guest_addr = dma_frcd_page_addr(guest_addr);
1102 /* clear the fault */
1103 writel(DMA_FRCD_F, iommu->reg + reg +
1104 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1106 spin_unlock_irqrestore(&iommu->register_lock, flag);
1108 iommu_page_fault_do_one(iommu, type, fault_reason,
1109 source_id, guest_addr);
1112 if (fault_index > cap_num_fault_regs(iommu->cap))
1114 spin_lock_irqsave(&iommu->register_lock, flag);
1117 /* clear primary fault overflow */
1118 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1119 if (fault_status & DMA_FSTS_PFO)
1120 writel(DMA_FSTS_PFO, iommu->reg + DMAR_FSTS_REG);
1122 spin_unlock_irqrestore(&iommu->register_lock, flag);
1126 int dmar_set_interrupt(struct intel_iommu *iommu)
1132 printk(KERN_ERR "IOMMU: no free vectors\n");
1136 set_irq_data(irq, iommu);
1139 ret = arch_setup_dmar_msi(irq);
1141 set_irq_data(irq, NULL);
1147 /* Force fault register is cleared */
1148 iommu_page_fault(irq, iommu);
1150 ret = request_irq(irq, iommu_page_fault, 0, iommu->name, iommu);
1152 printk(KERN_ERR "IOMMU: can't request irq\n");
1156 static int iommu_init_domains(struct intel_iommu *iommu)
1158 unsigned long ndomains;
1159 unsigned long nlongs;
1161 ndomains = cap_ndoms(iommu->cap);
1162 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1163 nlongs = BITS_TO_LONGS(ndomains);
1165 /* TBD: there might be 64K domains,
1166 * consider other allocation for future chip
1168 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1169 if (!iommu->domain_ids) {
1170 printk(KERN_ERR "Allocating domain id array failed\n");
1173 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1175 if (!iommu->domains) {
1176 printk(KERN_ERR "Allocating domain array failed\n");
1177 kfree(iommu->domain_ids);
1181 spin_lock_init(&iommu->lock);
1184 * if Caching mode is set, then invalid translations are tagged
1185 * with domainid 0. Hence we need to pre-allocate it.
1187 if (cap_caching_mode(iommu->cap))
1188 set_bit(0, iommu->domain_ids);
1193 static void domain_exit(struct dmar_domain *domain);
1195 void free_dmar_iommu(struct intel_iommu *iommu)
1197 struct dmar_domain *domain;
1200 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1201 for (; i < cap_ndoms(iommu->cap); ) {
1202 domain = iommu->domains[i];
1203 clear_bit(i, iommu->domain_ids);
1204 domain_exit(domain);
1205 i = find_next_bit(iommu->domain_ids,
1206 cap_ndoms(iommu->cap), i+1);
1209 if (iommu->gcmd & DMA_GCMD_TE)
1210 iommu_disable_translation(iommu);
1213 set_irq_data(iommu->irq, NULL);
1214 /* This will mask the irq */
1215 free_irq(iommu->irq, iommu);
1216 destroy_irq(iommu->irq);
1219 kfree(iommu->domains);
1220 kfree(iommu->domain_ids);
1222 g_iommus[iommu->seq_id] = NULL;
1224 /* if all iommus are freed, free g_iommus */
1225 for (i = 0; i < g_num_of_iommus; i++) {
1230 if (i == g_num_of_iommus)
1233 /* free context mapping */
1234 free_context_table(iommu);
1237 static struct dmar_domain * iommu_alloc_domain(struct intel_iommu *iommu)
1240 unsigned long ndomains;
1241 struct dmar_domain *domain;
1242 unsigned long flags;
1244 domain = alloc_domain_mem();
1248 ndomains = cap_ndoms(iommu->cap);
1250 spin_lock_irqsave(&iommu->lock, flags);
1251 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1252 if (num >= ndomains) {
1253 spin_unlock_irqrestore(&iommu->lock, flags);
1254 free_domain_mem(domain);
1255 printk(KERN_ERR "IOMMU: no free domain ids\n");
1259 set_bit(num, iommu->domain_ids);
1261 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
1262 set_bit(iommu->seq_id, &domain->iommu_bmp);
1264 iommu->domains[num] = domain;
1265 spin_unlock_irqrestore(&iommu->lock, flags);
1270 static void iommu_free_domain(struct dmar_domain *domain)
1272 unsigned long flags;
1273 struct intel_iommu *iommu;
1275 iommu = domain_get_iommu(domain);
1277 spin_lock_irqsave(&iommu->lock, flags);
1278 clear_bit(domain->id, iommu->domain_ids);
1279 spin_unlock_irqrestore(&iommu->lock, flags);
1282 static struct iova_domain reserved_iova_list;
1283 static struct lock_class_key reserved_alloc_key;
1284 static struct lock_class_key reserved_rbtree_key;
1286 static void dmar_init_reserved_ranges(void)
1288 struct pci_dev *pdev = NULL;
1293 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
1295 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1296 &reserved_alloc_key);
1297 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1298 &reserved_rbtree_key);
1300 /* IOAPIC ranges shouldn't be accessed by DMA */
1301 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1302 IOVA_PFN(IOAPIC_RANGE_END));
1304 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1306 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1307 for_each_pci_dev(pdev) {
1310 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1311 r = &pdev->resource[i];
1312 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1316 size = r->end - addr;
1317 size = PAGE_ALIGN(size);
1318 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1319 IOVA_PFN(size + addr) - 1);
1321 printk(KERN_ERR "Reserve iova failed\n");
1327 static void domain_reserve_special_ranges(struct dmar_domain *domain)
1329 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1332 static inline int guestwidth_to_adjustwidth(int gaw)
1335 int r = (gaw - 12) % 9;
1346 static int domain_init(struct dmar_domain *domain, int guest_width)
1348 struct intel_iommu *iommu;
1349 int adjust_width, agaw;
1350 unsigned long sagaw;
1352 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
1353 spin_lock_init(&domain->mapping_lock);
1355 domain_reserve_special_ranges(domain);
1357 /* calculate AGAW */
1358 iommu = domain_get_iommu(domain);
1359 if (guest_width > cap_mgaw(iommu->cap))
1360 guest_width = cap_mgaw(iommu->cap);
1361 domain->gaw = guest_width;
1362 adjust_width = guestwidth_to_adjustwidth(guest_width);
1363 agaw = width_to_agaw(adjust_width);
1364 sagaw = cap_sagaw(iommu->cap);
1365 if (!test_bit(agaw, &sagaw)) {
1366 /* hardware doesn't support it, choose a bigger one */
1367 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1368 agaw = find_next_bit(&sagaw, 5, agaw);
1372 domain->agaw = agaw;
1373 INIT_LIST_HEAD(&domain->devices);
1375 if (ecap_coherent(iommu->ecap))
1376 domain->iommu_coherency = 1;
1378 domain->iommu_coherency = 0;
1380 /* always allocate the top pgd */
1381 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1384 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
1388 static void domain_exit(struct dmar_domain *domain)
1392 /* Domain 0 is reserved, so dont process it */
1396 domain_remove_dev_info(domain);
1398 put_iova_domain(&domain->iovad);
1399 end = DOMAIN_MAX_ADDR(domain->gaw);
1400 end = end & (~PAGE_MASK);
1403 dma_pte_clear_range(domain, 0, end);
1405 /* free page tables */
1406 dma_pte_free_pagetable(domain, 0, end);
1408 iommu_free_domain(domain);
1409 free_domain_mem(domain);
1412 static int domain_context_mapping_one(struct dmar_domain *domain,
1415 struct context_entry *context;
1416 struct intel_iommu *iommu = domain_get_iommu(domain);
1417 unsigned long flags;
1419 pr_debug("Set context mapping for %02x:%02x.%d\n",
1420 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1421 BUG_ON(!domain->pgd);
1422 context = device_to_context_entry(iommu, bus, devfn);
1425 spin_lock_irqsave(&iommu->lock, flags);
1426 if (context_present(context)) {
1427 spin_unlock_irqrestore(&iommu->lock, flags);
1431 context_set_domain_id(context, domain->id);
1432 context_set_address_width(context, domain->agaw);
1433 context_set_address_root(context, virt_to_phys(domain->pgd));
1434 context_set_translation_type(context, CONTEXT_TT_MULTI_LEVEL);
1435 context_set_fault_enable(context);
1436 context_set_present(context);
1437 __iommu_flush_cache(iommu, context, sizeof(*context));
1439 /* it's a non-present to present mapping */
1440 if (iommu->flush.flush_context(iommu, domain->id,
1441 (((u16)bus) << 8) | devfn, DMA_CCMD_MASK_NOBIT,
1442 DMA_CCMD_DEVICE_INVL, 1))
1443 iommu_flush_write_buffer(iommu);
1445 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH, 0);
1447 spin_unlock_irqrestore(&iommu->lock, flags);
1452 domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev)
1455 struct pci_dev *tmp, *parent;
1457 ret = domain_context_mapping_one(domain, pdev->bus->number,
1462 /* dependent device mapping */
1463 tmp = pci_find_upstream_pcie_bridge(pdev);
1466 /* Secondary interface's bus number and devfn 0 */
1467 parent = pdev->bus->self;
1468 while (parent != tmp) {
1469 ret = domain_context_mapping_one(domain, parent->bus->number,
1473 parent = parent->bus->self;
1475 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1476 return domain_context_mapping_one(domain,
1477 tmp->subordinate->number, 0);
1478 else /* this is a legacy PCI bridge */
1479 return domain_context_mapping_one(domain,
1480 tmp->bus->number, tmp->devfn);
1483 static int domain_context_mapped(struct dmar_domain *domain,
1484 struct pci_dev *pdev)
1487 struct pci_dev *tmp, *parent;
1488 struct intel_iommu *iommu = domain_get_iommu(domain);
1490 ret = device_context_mapped(iommu,
1491 pdev->bus->number, pdev->devfn);
1494 /* dependent device mapping */
1495 tmp = pci_find_upstream_pcie_bridge(pdev);
1498 /* Secondary interface's bus number and devfn 0 */
1499 parent = pdev->bus->self;
1500 while (parent != tmp) {
1501 ret = device_context_mapped(iommu, parent->bus->number,
1505 parent = parent->bus->self;
1508 return device_context_mapped(iommu,
1509 tmp->subordinate->number, 0);
1511 return device_context_mapped(iommu,
1512 tmp->bus->number, tmp->devfn);
1516 domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1517 u64 hpa, size_t size, int prot)
1519 u64 start_pfn, end_pfn;
1520 struct dma_pte *pte;
1522 int addr_width = agaw_to_width(domain->agaw);
1523 struct intel_iommu *iommu = domain_get_iommu(domain);
1525 hpa &= (((u64)1) << addr_width) - 1;
1527 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1530 start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1531 end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
1533 while (start_pfn < end_pfn) {
1534 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
1537 /* We don't need lock here, nobody else
1538 * touches the iova range
1540 BUG_ON(dma_pte_addr(pte));
1541 dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT);
1542 dma_set_pte_prot(pte, prot);
1543 __iommu_flush_cache(iommu, pte, sizeof(*pte));
1550 static void detach_domain_for_dev(struct dmar_domain *domain, u8 bus, u8 devfn)
1552 struct intel_iommu *iommu = domain_get_iommu(domain);
1554 clear_context_table(iommu, bus, devfn);
1555 iommu->flush.flush_context(iommu, 0, 0, 0,
1556 DMA_CCMD_GLOBAL_INVL, 0);
1557 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
1558 DMA_TLB_GLOBAL_FLUSH, 0);
1561 static void domain_remove_dev_info(struct dmar_domain *domain)
1563 struct device_domain_info *info;
1564 unsigned long flags;
1566 spin_lock_irqsave(&device_domain_lock, flags);
1567 while (!list_empty(&domain->devices)) {
1568 info = list_entry(domain->devices.next,
1569 struct device_domain_info, link);
1570 list_del(&info->link);
1571 list_del(&info->global);
1573 info->dev->dev.archdata.iommu = NULL;
1574 spin_unlock_irqrestore(&device_domain_lock, flags);
1576 detach_domain_for_dev(info->domain, info->bus, info->devfn);
1577 free_devinfo_mem(info);
1579 spin_lock_irqsave(&device_domain_lock, flags);
1581 spin_unlock_irqrestore(&device_domain_lock, flags);
1586 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
1588 static struct dmar_domain *
1589 find_domain(struct pci_dev *pdev)
1591 struct device_domain_info *info;
1593 /* No lock here, assumes no domain exit in normal case */
1594 info = pdev->dev.archdata.iommu;
1596 return info->domain;
1600 /* domain is initialized */
1601 static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1603 struct dmar_domain *domain, *found = NULL;
1604 struct intel_iommu *iommu;
1605 struct dmar_drhd_unit *drhd;
1606 struct device_domain_info *info, *tmp;
1607 struct pci_dev *dev_tmp;
1608 unsigned long flags;
1609 int bus = 0, devfn = 0;
1611 domain = find_domain(pdev);
1615 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1617 if (dev_tmp->is_pcie) {
1618 bus = dev_tmp->subordinate->number;
1621 bus = dev_tmp->bus->number;
1622 devfn = dev_tmp->devfn;
1624 spin_lock_irqsave(&device_domain_lock, flags);
1625 list_for_each_entry(info, &device_domain_list, global) {
1626 if (info->bus == bus && info->devfn == devfn) {
1627 found = info->domain;
1631 spin_unlock_irqrestore(&device_domain_lock, flags);
1632 /* pcie-pci bridge already has a domain, uses it */
1639 /* Allocate new domain for the device */
1640 drhd = dmar_find_matched_drhd_unit(pdev);
1642 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1646 iommu = drhd->iommu;
1648 domain = iommu_alloc_domain(iommu);
1652 if (domain_init(domain, gaw)) {
1653 domain_exit(domain);
1657 /* register pcie-to-pci device */
1659 info = alloc_devinfo_mem();
1661 domain_exit(domain);
1665 info->devfn = devfn;
1667 info->domain = domain;
1668 /* This domain is shared by devices under p2p bridge */
1669 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
1671 /* pcie-to-pci bridge already has a domain, uses it */
1673 spin_lock_irqsave(&device_domain_lock, flags);
1674 list_for_each_entry(tmp, &device_domain_list, global) {
1675 if (tmp->bus == bus && tmp->devfn == devfn) {
1676 found = tmp->domain;
1681 free_devinfo_mem(info);
1682 domain_exit(domain);
1685 list_add(&info->link, &domain->devices);
1686 list_add(&info->global, &device_domain_list);
1688 spin_unlock_irqrestore(&device_domain_lock, flags);
1692 info = alloc_devinfo_mem();
1695 info->bus = pdev->bus->number;
1696 info->devfn = pdev->devfn;
1698 info->domain = domain;
1699 spin_lock_irqsave(&device_domain_lock, flags);
1700 /* somebody is fast */
1701 found = find_domain(pdev);
1702 if (found != NULL) {
1703 spin_unlock_irqrestore(&device_domain_lock, flags);
1704 if (found != domain) {
1705 domain_exit(domain);
1708 free_devinfo_mem(info);
1711 list_add(&info->link, &domain->devices);
1712 list_add(&info->global, &device_domain_list);
1713 pdev->dev.archdata.iommu = info;
1714 spin_unlock_irqrestore(&device_domain_lock, flags);
1717 /* recheck it here, maybe others set it */
1718 return find_domain(pdev);
1721 static int iommu_prepare_identity_map(struct pci_dev *pdev,
1722 unsigned long long start,
1723 unsigned long long end)
1725 struct dmar_domain *domain;
1727 unsigned long long base;
1731 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1732 pci_name(pdev), start, end);
1733 /* page table init */
1734 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1738 /* The address might not be aligned */
1739 base = start & PAGE_MASK;
1741 size = PAGE_ALIGN(size);
1742 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1743 IOVA_PFN(base + size) - 1)) {
1744 printk(KERN_ERR "IOMMU: reserve iova failed\n");
1749 pr_debug("Mapping reserved region %lx@%llx for %s\n",
1750 size, base, pci_name(pdev));
1752 * RMRR range might have overlap with physical memory range,
1755 dma_pte_clear_range(domain, base, base + size);
1757 ret = domain_page_mapping(domain, base, base, size,
1758 DMA_PTE_READ|DMA_PTE_WRITE);
1762 /* context entry init */
1763 ret = domain_context_mapping(domain, pdev);
1767 domain_exit(domain);
1772 static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1773 struct pci_dev *pdev)
1775 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
1777 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1778 rmrr->end_address + 1);
1781 #ifdef CONFIG_DMAR_GFX_WA
1782 struct iommu_prepare_data {
1783 struct pci_dev *pdev;
1787 static int __init iommu_prepare_work_fn(unsigned long start_pfn,
1788 unsigned long end_pfn, void *datax)
1790 struct iommu_prepare_data *data;
1792 data = (struct iommu_prepare_data *)datax;
1794 data->ret = iommu_prepare_identity_map(data->pdev,
1795 start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
1800 static int __init iommu_prepare_with_active_regions(struct pci_dev *pdev)
1803 struct iommu_prepare_data data;
1808 for_each_online_node(nid) {
1809 work_with_active_regions(nid, iommu_prepare_work_fn, &data);
1816 static void __init iommu_prepare_gfx_mapping(void)
1818 struct pci_dev *pdev = NULL;
1821 for_each_pci_dev(pdev) {
1822 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO ||
1823 !IS_GFX_DEVICE(pdev))
1825 printk(KERN_INFO "IOMMU: gfx device %s 1-1 mapping\n",
1827 ret = iommu_prepare_with_active_regions(pdev);
1829 printk(KERN_ERR "IOMMU: mapping reserved region failed\n");
1832 #else /* !CONFIG_DMAR_GFX_WA */
1833 static inline void iommu_prepare_gfx_mapping(void)
1839 #ifdef CONFIG_DMAR_FLOPPY_WA
1840 static inline void iommu_prepare_isa(void)
1842 struct pci_dev *pdev;
1845 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1849 printk(KERN_INFO "IOMMU: Prepare 0-16M unity mapping for LPC\n");
1850 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1853 printk("IOMMU: Failed to create 0-64M identity map, "
1854 "floppy might not work\n");
1858 static inline void iommu_prepare_isa(void)
1862 #endif /* !CONFIG_DMAR_FLPY_WA */
1864 static int __init init_dmars(void)
1866 struct dmar_drhd_unit *drhd;
1867 struct dmar_rmrr_unit *rmrr;
1868 struct pci_dev *pdev;
1869 struct intel_iommu *iommu;
1870 int i, ret, unit = 0;
1875 * initialize and program root entry to not present
1878 for_each_drhd_unit(drhd) {
1881 * lock not needed as this is only incremented in the single
1882 * threaded kernel __init code path all other access are read
1887 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
1890 printk(KERN_ERR "Allocating global iommu array failed\n");
1895 deferred_flush = kzalloc(g_num_of_iommus *
1896 sizeof(struct deferred_flush_tables), GFP_KERNEL);
1897 if (!deferred_flush) {
1903 for_each_drhd_unit(drhd) {
1907 iommu = drhd->iommu;
1908 g_iommus[iommu->seq_id] = iommu;
1910 ret = iommu_init_domains(iommu);
1916 * we could share the same root & context tables
1917 * amoung all IOMMU's. Need to Split it later.
1919 ret = iommu_alloc_root_entry(iommu);
1921 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
1926 for_each_drhd_unit(drhd) {
1930 iommu = drhd->iommu;
1931 if (dmar_enable_qi(iommu)) {
1933 * Queued Invalidate not enabled, use Register Based
1936 iommu->flush.flush_context = __iommu_flush_context;
1937 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
1938 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
1940 (unsigned long long)drhd->reg_base_addr);
1942 iommu->flush.flush_context = qi_flush_context;
1943 iommu->flush.flush_iotlb = qi_flush_iotlb;
1944 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
1946 (unsigned long long)drhd->reg_base_addr);
1952 * for each dev attached to rmrr
1954 * locate drhd for dev, alloc domain for dev
1955 * allocate free domain
1956 * allocate page table entries for rmrr
1957 * if context not allocated for bus
1958 * allocate and init context
1959 * set present in root table for this bus
1960 * init context with domain, translation etc
1964 for_each_rmrr_units(rmrr) {
1965 for (i = 0; i < rmrr->devices_cnt; i++) {
1966 pdev = rmrr->devices[i];
1967 /* some BIOS lists non-exist devices in DMAR table */
1970 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
1973 "IOMMU: mapping reserved region failed\n");
1977 iommu_prepare_gfx_mapping();
1979 iommu_prepare_isa();
1984 * global invalidate context cache
1985 * global invalidate iotlb
1986 * enable translation
1988 for_each_drhd_unit(drhd) {
1991 iommu = drhd->iommu;
1992 sprintf (iommu->name, "dmar%d", unit++);
1994 iommu_flush_write_buffer(iommu);
1996 ret = dmar_set_interrupt(iommu);
2000 iommu_set_root_entry(iommu);
2002 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL,
2004 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH,
2006 iommu_disable_protect_mem_regions(iommu);
2008 ret = iommu_enable_translation(iommu);
2015 for_each_drhd_unit(drhd) {
2018 iommu = drhd->iommu;
2025 static inline u64 aligned_size(u64 host_addr, size_t size)
2028 addr = (host_addr & (~PAGE_MASK)) + size;
2029 return PAGE_ALIGN(addr);
2033 iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
2037 /* Make sure it's in range */
2038 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
2039 if (!size || (IOVA_START_ADDR + size > end))
2042 piova = alloc_iova(&domain->iovad,
2043 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
2047 static struct iova *
2048 __intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
2049 size_t size, u64 dma_mask)
2051 struct pci_dev *pdev = to_pci_dev(dev);
2052 struct iova *iova = NULL;
2054 if (dma_mask <= DMA_32BIT_MASK || dmar_forcedac)
2055 iova = iommu_alloc_iova(domain, size, dma_mask);
2058 * First try to allocate an io virtual address in
2059 * DMA_32BIT_MASK and if that fails then try allocating
2062 iova = iommu_alloc_iova(domain, size, DMA_32BIT_MASK);
2064 iova = iommu_alloc_iova(domain, size, dma_mask);
2068 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2075 static struct dmar_domain *
2076 get_valid_domain_for_dev(struct pci_dev *pdev)
2078 struct dmar_domain *domain;
2081 domain = get_domain_for_dev(pdev,
2082 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2085 "Allocating domain for %s failed", pci_name(pdev));
2089 /* make sure context mapping is ok */
2090 if (unlikely(!domain_context_mapped(domain, pdev))) {
2091 ret = domain_context_mapping(domain, pdev);
2094 "Domain context map for %s failed",
2103 static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2104 size_t size, int dir, u64 dma_mask)
2106 struct pci_dev *pdev = to_pci_dev(hwdev);
2107 struct dmar_domain *domain;
2108 phys_addr_t start_paddr;
2112 struct intel_iommu *iommu;
2114 BUG_ON(dir == DMA_NONE);
2115 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2118 domain = get_valid_domain_for_dev(pdev);
2122 iommu = domain_get_iommu(domain);
2123 size = aligned_size((u64)paddr, size);
2125 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
2129 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
2132 * Check if DMAR supports zero-length reads on write only
2135 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
2136 !cap_zlr(iommu->cap))
2137 prot |= DMA_PTE_READ;
2138 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2139 prot |= DMA_PTE_WRITE;
2141 * paddr - (paddr + size) might be partial page, we should map the whole
2142 * page. Note: if two part of one page are separately mapped, we
2143 * might have two guest_addr mapping to the same host paddr, but this
2144 * is not a big problem
2146 ret = domain_page_mapping(domain, start_paddr,
2147 ((u64)paddr) & PAGE_MASK, size, prot);
2151 /* it's a non-present to present mapping */
2152 ret = iommu_flush_iotlb_psi(iommu, domain->id,
2153 start_paddr, size >> VTD_PAGE_SHIFT, 1);
2155 iommu_flush_write_buffer(iommu);
2157 return start_paddr + ((u64)paddr & (~PAGE_MASK));
2161 __free_iova(&domain->iovad, iova);
2162 printk(KERN_ERR"Device %s request: %lx@%llx dir %d --- failed\n",
2163 pci_name(pdev), size, (unsigned long long)paddr, dir);
2167 dma_addr_t intel_map_single(struct device *hwdev, phys_addr_t paddr,
2168 size_t size, int dir)
2170 return __intel_map_single(hwdev, paddr, size, dir,
2171 to_pci_dev(hwdev)->dma_mask);
2174 static void flush_unmaps(void)
2180 /* just flush them all */
2181 for (i = 0; i < g_num_of_iommus; i++) {
2182 struct intel_iommu *iommu = g_iommus[i];
2186 if (deferred_flush[i].next) {
2187 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2188 DMA_TLB_GLOBAL_FLUSH, 0);
2189 for (j = 0; j < deferred_flush[i].next; j++) {
2190 __free_iova(&deferred_flush[i].domain[j]->iovad,
2191 deferred_flush[i].iova[j]);
2193 deferred_flush[i].next = 0;
2200 static void flush_unmaps_timeout(unsigned long data)
2202 unsigned long flags;
2204 spin_lock_irqsave(&async_umap_flush_lock, flags);
2206 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2209 static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2211 unsigned long flags;
2213 struct intel_iommu *iommu;
2215 spin_lock_irqsave(&async_umap_flush_lock, flags);
2216 if (list_size == HIGH_WATER_MARK)
2219 iommu = domain_get_iommu(dom);
2220 iommu_id = iommu->seq_id;
2222 next = deferred_flush[iommu_id].next;
2223 deferred_flush[iommu_id].domain[next] = dom;
2224 deferred_flush[iommu_id].iova[next] = iova;
2225 deferred_flush[iommu_id].next++;
2228 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2232 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2235 void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2238 struct pci_dev *pdev = to_pci_dev(dev);
2239 struct dmar_domain *domain;
2240 unsigned long start_addr;
2242 struct intel_iommu *iommu;
2244 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2246 domain = find_domain(pdev);
2249 iommu = domain_get_iommu(domain);
2251 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2255 start_addr = iova->pfn_lo << PAGE_SHIFT;
2256 size = aligned_size((u64)dev_addr, size);
2258 pr_debug("Device %s unmapping: %lx@%llx\n",
2259 pci_name(pdev), size, (unsigned long long)start_addr);
2261 /* clear the whole page */
2262 dma_pte_clear_range(domain, start_addr, start_addr + size);
2263 /* free page tables */
2264 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2265 if (intel_iommu_strict) {
2266 if (iommu_flush_iotlb_psi(iommu,
2267 domain->id, start_addr, size >> VTD_PAGE_SHIFT, 0))
2268 iommu_flush_write_buffer(iommu);
2270 __free_iova(&domain->iovad, iova);
2272 add_unmap(domain, iova);
2274 * queue up the release of the unmap to save the 1/6th of the
2275 * cpu used up by the iotlb flush operation...
2280 void *intel_alloc_coherent(struct device *hwdev, size_t size,
2281 dma_addr_t *dma_handle, gfp_t flags)
2286 size = PAGE_ALIGN(size);
2287 order = get_order(size);
2288 flags &= ~(GFP_DMA | GFP_DMA32);
2290 vaddr = (void *)__get_free_pages(flags, order);
2293 memset(vaddr, 0, size);
2295 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2297 hwdev->coherent_dma_mask);
2300 free_pages((unsigned long)vaddr, order);
2304 void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2305 dma_addr_t dma_handle)
2309 size = PAGE_ALIGN(size);
2310 order = get_order(size);
2312 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2313 free_pages((unsigned long)vaddr, order);
2316 #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
2318 void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2319 int nelems, int dir)
2322 struct pci_dev *pdev = to_pci_dev(hwdev);
2323 struct dmar_domain *domain;
2324 unsigned long start_addr;
2328 struct scatterlist *sg;
2329 struct intel_iommu *iommu;
2331 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2334 domain = find_domain(pdev);
2337 iommu = domain_get_iommu(domain);
2339 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
2342 for_each_sg(sglist, sg, nelems, i) {
2343 addr = SG_ENT_VIRT_ADDRESS(sg);
2344 size += aligned_size((u64)addr, sg->length);
2347 start_addr = iova->pfn_lo << PAGE_SHIFT;
2349 /* clear the whole page */
2350 dma_pte_clear_range(domain, start_addr, start_addr + size);
2351 /* free page tables */
2352 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2354 if (iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2355 size >> VTD_PAGE_SHIFT, 0))
2356 iommu_flush_write_buffer(iommu);
2359 __free_iova(&domain->iovad, iova);
2362 static int intel_nontranslate_map_sg(struct device *hddev,
2363 struct scatterlist *sglist, int nelems, int dir)
2366 struct scatterlist *sg;
2368 for_each_sg(sglist, sg, nelems, i) {
2369 BUG_ON(!sg_page(sg));
2370 sg->dma_address = virt_to_bus(SG_ENT_VIRT_ADDRESS(sg));
2371 sg->dma_length = sg->length;
2376 int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2381 struct pci_dev *pdev = to_pci_dev(hwdev);
2382 struct dmar_domain *domain;
2386 struct iova *iova = NULL;
2388 struct scatterlist *sg;
2389 unsigned long start_addr;
2390 struct intel_iommu *iommu;
2392 BUG_ON(dir == DMA_NONE);
2393 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2394 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
2396 domain = get_valid_domain_for_dev(pdev);
2400 iommu = domain_get_iommu(domain);
2402 for_each_sg(sglist, sg, nelems, i) {
2403 addr = SG_ENT_VIRT_ADDRESS(sg);
2404 addr = (void *)virt_to_phys(addr);
2405 size += aligned_size((u64)addr, sg->length);
2408 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
2410 sglist->dma_length = 0;
2415 * Check if DMAR supports zero-length reads on write only
2418 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
2419 !cap_zlr(iommu->cap))
2420 prot |= DMA_PTE_READ;
2421 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2422 prot |= DMA_PTE_WRITE;
2424 start_addr = iova->pfn_lo << PAGE_SHIFT;
2426 for_each_sg(sglist, sg, nelems, i) {
2427 addr = SG_ENT_VIRT_ADDRESS(sg);
2428 addr = (void *)virt_to_phys(addr);
2429 size = aligned_size((u64)addr, sg->length);
2430 ret = domain_page_mapping(domain, start_addr + offset,
2431 ((u64)addr) & PAGE_MASK,
2434 /* clear the page */
2435 dma_pte_clear_range(domain, start_addr,
2436 start_addr + offset);
2437 /* free page tables */
2438 dma_pte_free_pagetable(domain, start_addr,
2439 start_addr + offset);
2441 __free_iova(&domain->iovad, iova);
2444 sg->dma_address = start_addr + offset +
2445 ((u64)addr & (~PAGE_MASK));
2446 sg->dma_length = sg->length;
2450 /* it's a non-present to present mapping */
2451 if (iommu_flush_iotlb_psi(iommu, domain->id,
2452 start_addr, offset >> VTD_PAGE_SHIFT, 1))
2453 iommu_flush_write_buffer(iommu);
2457 static struct dma_mapping_ops intel_dma_ops = {
2458 .alloc_coherent = intel_alloc_coherent,
2459 .free_coherent = intel_free_coherent,
2460 .map_single = intel_map_single,
2461 .unmap_single = intel_unmap_single,
2462 .map_sg = intel_map_sg,
2463 .unmap_sg = intel_unmap_sg,
2466 static inline int iommu_domain_cache_init(void)
2470 iommu_domain_cache = kmem_cache_create("iommu_domain",
2471 sizeof(struct dmar_domain),
2476 if (!iommu_domain_cache) {
2477 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2484 static inline int iommu_devinfo_cache_init(void)
2488 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2489 sizeof(struct device_domain_info),
2493 if (!iommu_devinfo_cache) {
2494 printk(KERN_ERR "Couldn't create devinfo cache\n");
2501 static inline int iommu_iova_cache_init(void)
2505 iommu_iova_cache = kmem_cache_create("iommu_iova",
2506 sizeof(struct iova),
2510 if (!iommu_iova_cache) {
2511 printk(KERN_ERR "Couldn't create iova cache\n");
2518 static int __init iommu_init_mempool(void)
2521 ret = iommu_iova_cache_init();
2525 ret = iommu_domain_cache_init();
2529 ret = iommu_devinfo_cache_init();
2533 kmem_cache_destroy(iommu_domain_cache);
2535 kmem_cache_destroy(iommu_iova_cache);
2540 static void __init iommu_exit_mempool(void)
2542 kmem_cache_destroy(iommu_devinfo_cache);
2543 kmem_cache_destroy(iommu_domain_cache);
2544 kmem_cache_destroy(iommu_iova_cache);
2548 static void __init init_no_remapping_devices(void)
2550 struct dmar_drhd_unit *drhd;
2552 for_each_drhd_unit(drhd) {
2553 if (!drhd->include_all) {
2555 for (i = 0; i < drhd->devices_cnt; i++)
2556 if (drhd->devices[i] != NULL)
2558 /* ignore DMAR unit if no pci devices exist */
2559 if (i == drhd->devices_cnt)
2567 for_each_drhd_unit(drhd) {
2569 if (drhd->ignored || drhd->include_all)
2572 for (i = 0; i < drhd->devices_cnt; i++)
2573 if (drhd->devices[i] &&
2574 !IS_GFX_DEVICE(drhd->devices[i]))
2577 if (i < drhd->devices_cnt)
2580 /* bypass IOMMU if it is just for gfx devices */
2582 for (i = 0; i < drhd->devices_cnt; i++) {
2583 if (!drhd->devices[i])
2585 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
2590 int __init intel_iommu_init(void)
2594 if (dmar_table_init())
2597 if (dmar_dev_scope_init())
2601 * Check the need for DMA-remapping initialization now.
2602 * Above initialization will also be used by Interrupt-remapping.
2604 if (no_iommu || swiotlb || dmar_disabled)
2607 iommu_init_mempool();
2608 dmar_init_reserved_ranges();
2610 init_no_remapping_devices();
2614 printk(KERN_ERR "IOMMU: dmar init failed\n");
2615 put_iova_domain(&reserved_iova_list);
2616 iommu_exit_mempool();
2620 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
2622 init_timer(&unmap_timer);
2624 dma_ops = &intel_dma_ops;
2628 void intel_iommu_domain_exit(struct dmar_domain *domain)
2632 /* Domain 0 is reserved, so dont process it */
2636 end = DOMAIN_MAX_ADDR(domain->gaw);
2637 end = end & (~VTD_PAGE_MASK);
2640 dma_pte_clear_range(domain, 0, end);
2642 /* free page tables */
2643 dma_pte_free_pagetable(domain, 0, end);
2645 iommu_free_domain(domain);
2646 free_domain_mem(domain);
2648 EXPORT_SYMBOL_GPL(intel_iommu_domain_exit);
2650 struct dmar_domain *intel_iommu_domain_alloc(struct pci_dev *pdev)
2652 struct dmar_drhd_unit *drhd;
2653 struct dmar_domain *domain;
2654 struct intel_iommu *iommu;
2656 drhd = dmar_find_matched_drhd_unit(pdev);
2658 printk(KERN_ERR "intel_iommu_domain_alloc: drhd == NULL\n");
2662 iommu = drhd->iommu;
2665 "intel_iommu_domain_alloc: iommu == NULL\n");
2668 domain = iommu_alloc_domain(iommu);
2671 "intel_iommu_domain_alloc: domain == NULL\n");
2674 if (domain_init(domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2676 "intel_iommu_domain_alloc: domain_init() failed\n");
2677 intel_iommu_domain_exit(domain);
2682 EXPORT_SYMBOL_GPL(intel_iommu_domain_alloc);
2684 int intel_iommu_context_mapping(
2685 struct dmar_domain *domain, struct pci_dev *pdev)
2688 rc = domain_context_mapping(domain, pdev);
2691 EXPORT_SYMBOL_GPL(intel_iommu_context_mapping);
2693 int intel_iommu_page_mapping(
2694 struct dmar_domain *domain, dma_addr_t iova,
2695 u64 hpa, size_t size, int prot)
2698 rc = domain_page_mapping(domain, iova, hpa, size, prot);
2701 EXPORT_SYMBOL_GPL(intel_iommu_page_mapping);
2703 void intel_iommu_detach_dev(struct dmar_domain *domain, u8 bus, u8 devfn)
2705 detach_domain_for_dev(domain, bus, devfn);
2707 EXPORT_SYMBOL_GPL(intel_iommu_detach_dev);
2709 struct dmar_domain *
2710 intel_iommu_find_domain(struct pci_dev *pdev)
2712 return find_domain(pdev);
2714 EXPORT_SYMBOL_GPL(intel_iommu_find_domain);
2716 int intel_iommu_found(void)
2718 return g_num_of_iommus;
2720 EXPORT_SYMBOL_GPL(intel_iommu_found);
2722 u64 intel_iommu_iova_to_pfn(struct dmar_domain *domain, u64 iova)
2724 struct dma_pte *pte;
2728 pte = addr_to_dma_pte(domain, iova);
2731 pfn = dma_pte_addr(pte);
2733 return pfn >> VTD_PAGE_SHIFT;
2735 EXPORT_SYMBOL_GPL(intel_iommu_iova_to_pfn);