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)
210 int id; /* domain id */
211 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
213 struct list_head devices; /* all devices' list */
214 struct iova_domain iovad; /* iova's that belong to this domain */
216 struct dma_pte *pgd; /* virtual address */
217 spinlock_t mapping_lock; /* page table lock */
218 int gaw; /* max guest address width */
220 /* adjusted guest address width, 0 is level 2 30-bit */
223 int flags; /* flags to find out type of domain */
226 /* PCI domain-device relationship */
227 struct device_domain_info {
228 struct list_head link; /* link to domain siblings */
229 struct list_head global; /* link to global list */
230 u8 bus; /* PCI bus numer */
231 u8 devfn; /* PCI devfn number */
232 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
233 struct dmar_domain *domain; /* pointer to domain */
236 static void flush_unmaps_timeout(unsigned long data);
238 DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
240 #define HIGH_WATER_MARK 250
241 struct deferred_flush_tables {
243 struct iova *iova[HIGH_WATER_MARK];
244 struct dmar_domain *domain[HIGH_WATER_MARK];
247 static struct deferred_flush_tables *deferred_flush;
249 /* bitmap for indexing intel_iommus */
250 static int g_num_of_iommus;
252 static DEFINE_SPINLOCK(async_umap_flush_lock);
253 static LIST_HEAD(unmaps_to_do);
256 static long list_size;
258 static void domain_remove_dev_info(struct dmar_domain *domain);
261 static int __initdata dmar_map_gfx = 1;
262 static int dmar_forcedac;
263 static int intel_iommu_strict;
265 #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
266 static DEFINE_SPINLOCK(device_domain_lock);
267 static LIST_HEAD(device_domain_list);
269 static int __init intel_iommu_setup(char *str)
274 if (!strncmp(str, "off", 3)) {
276 printk(KERN_INFO"Intel-IOMMU: disabled\n");
277 } else if (!strncmp(str, "igfx_off", 8)) {
280 "Intel-IOMMU: disable GFX device mapping\n");
281 } else if (!strncmp(str, "forcedac", 8)) {
283 "Intel-IOMMU: Forcing DAC for PCI devices\n");
285 } else if (!strncmp(str, "strict", 6)) {
287 "Intel-IOMMU: disable batched IOTLB flush\n");
288 intel_iommu_strict = 1;
291 str += strcspn(str, ",");
297 __setup("intel_iommu=", intel_iommu_setup);
299 static struct kmem_cache *iommu_domain_cache;
300 static struct kmem_cache *iommu_devinfo_cache;
301 static struct kmem_cache *iommu_iova_cache;
303 static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
308 /* trying to avoid low memory issues */
309 flags = current->flags & PF_MEMALLOC;
310 current->flags |= PF_MEMALLOC;
311 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
312 current->flags &= (~PF_MEMALLOC | flags);
317 static inline void *alloc_pgtable_page(void)
322 /* trying to avoid low memory issues */
323 flags = current->flags & PF_MEMALLOC;
324 current->flags |= PF_MEMALLOC;
325 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
326 current->flags &= (~PF_MEMALLOC | flags);
330 static inline void free_pgtable_page(void *vaddr)
332 free_page((unsigned long)vaddr);
335 static inline void *alloc_domain_mem(void)
337 return iommu_kmem_cache_alloc(iommu_domain_cache);
340 static void free_domain_mem(void *vaddr)
342 kmem_cache_free(iommu_domain_cache, vaddr);
345 static inline void * alloc_devinfo_mem(void)
347 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
350 static inline void free_devinfo_mem(void *vaddr)
352 kmem_cache_free(iommu_devinfo_cache, vaddr);
355 struct iova *alloc_iova_mem(void)
357 return iommu_kmem_cache_alloc(iommu_iova_cache);
360 void free_iova_mem(struct iova *iova)
362 kmem_cache_free(iommu_iova_cache, iova);
365 /* in native case, each domain is related to only one iommu */
366 static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
370 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
371 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
374 return g_iommus[iommu_id];
377 /* Gets context entry for a given bus and devfn */
378 static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
381 struct root_entry *root;
382 struct context_entry *context;
383 unsigned long phy_addr;
386 spin_lock_irqsave(&iommu->lock, flags);
387 root = &iommu->root_entry[bus];
388 context = get_context_addr_from_root(root);
390 context = (struct context_entry *)alloc_pgtable_page();
392 spin_unlock_irqrestore(&iommu->lock, flags);
395 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
396 phy_addr = virt_to_phys((void *)context);
397 set_root_value(root, phy_addr);
398 set_root_present(root);
399 __iommu_flush_cache(iommu, root, sizeof(*root));
401 spin_unlock_irqrestore(&iommu->lock, flags);
402 return &context[devfn];
405 static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
407 struct root_entry *root;
408 struct context_entry *context;
412 spin_lock_irqsave(&iommu->lock, flags);
413 root = &iommu->root_entry[bus];
414 context = get_context_addr_from_root(root);
419 ret = context_present(&context[devfn]);
421 spin_unlock_irqrestore(&iommu->lock, flags);
425 static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
427 struct root_entry *root;
428 struct context_entry *context;
431 spin_lock_irqsave(&iommu->lock, flags);
432 root = &iommu->root_entry[bus];
433 context = get_context_addr_from_root(root);
435 context_clear_entry(&context[devfn]);
436 __iommu_flush_cache(iommu, &context[devfn], \
439 spin_unlock_irqrestore(&iommu->lock, flags);
442 static void free_context_table(struct intel_iommu *iommu)
444 struct root_entry *root;
447 struct context_entry *context;
449 spin_lock_irqsave(&iommu->lock, flags);
450 if (!iommu->root_entry) {
453 for (i = 0; i < ROOT_ENTRY_NR; i++) {
454 root = &iommu->root_entry[i];
455 context = get_context_addr_from_root(root);
457 free_pgtable_page(context);
459 free_pgtable_page(iommu->root_entry);
460 iommu->root_entry = NULL;
462 spin_unlock_irqrestore(&iommu->lock, flags);
465 /* page table handling */
466 #define LEVEL_STRIDE (9)
467 #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
469 static inline int agaw_to_level(int agaw)
474 static inline int agaw_to_width(int agaw)
476 return 30 + agaw * LEVEL_STRIDE;
480 static inline int width_to_agaw(int width)
482 return (width - 30) / LEVEL_STRIDE;
485 static inline unsigned int level_to_offset_bits(int level)
487 return (12 + (level - 1) * LEVEL_STRIDE);
490 static inline int address_level_offset(u64 addr, int level)
492 return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
495 static inline u64 level_mask(int level)
497 return ((u64)-1 << level_to_offset_bits(level));
500 static inline u64 level_size(int level)
502 return ((u64)1 << level_to_offset_bits(level));
505 static inline u64 align_to_level(u64 addr, int level)
507 return ((addr + level_size(level) - 1) & level_mask(level));
510 static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
512 int addr_width = agaw_to_width(domain->agaw);
513 struct dma_pte *parent, *pte = NULL;
514 int level = agaw_to_level(domain->agaw);
517 struct intel_iommu *iommu = domain_get_iommu(domain);
519 BUG_ON(!domain->pgd);
521 addr &= (((u64)1) << addr_width) - 1;
522 parent = domain->pgd;
524 spin_lock_irqsave(&domain->mapping_lock, flags);
528 offset = address_level_offset(addr, level);
529 pte = &parent[offset];
533 if (!dma_pte_present(pte)) {
534 tmp_page = alloc_pgtable_page();
537 spin_unlock_irqrestore(&domain->mapping_lock,
541 __iommu_flush_cache(iommu, tmp_page,
543 dma_set_pte_addr(pte, virt_to_phys(tmp_page));
545 * high level table always sets r/w, last level page
546 * table control read/write
548 dma_set_pte_readable(pte);
549 dma_set_pte_writable(pte);
550 __iommu_flush_cache(iommu, pte, sizeof(*pte));
552 parent = phys_to_virt(dma_pte_addr(pte));
556 spin_unlock_irqrestore(&domain->mapping_lock, flags);
560 /* return address's pte at specific level */
561 static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
564 struct dma_pte *parent, *pte = NULL;
565 int total = agaw_to_level(domain->agaw);
568 parent = domain->pgd;
569 while (level <= total) {
570 offset = address_level_offset(addr, total);
571 pte = &parent[offset];
575 if (!dma_pte_present(pte))
577 parent = phys_to_virt(dma_pte_addr(pte));
583 /* clear one page's page table */
584 static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
586 struct dma_pte *pte = NULL;
587 struct intel_iommu *iommu = domain_get_iommu(domain);
589 /* get last level pte */
590 pte = dma_addr_level_pte(domain, addr, 1);
594 __iommu_flush_cache(iommu, pte, sizeof(*pte));
598 /* clear last level pte, a tlb flush should be followed */
599 static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
601 int addr_width = agaw_to_width(domain->agaw);
603 start &= (((u64)1) << addr_width) - 1;
604 end &= (((u64)1) << addr_width) - 1;
605 /* in case it's partial page */
606 start = PAGE_ALIGN(start);
609 /* we don't need lock here, nobody else touches the iova range */
610 while (start < end) {
611 dma_pte_clear_one(domain, start);
612 start += VTD_PAGE_SIZE;
616 /* free page table pages. last level pte should already be cleared */
617 static void dma_pte_free_pagetable(struct dmar_domain *domain,
620 int addr_width = agaw_to_width(domain->agaw);
622 int total = agaw_to_level(domain->agaw);
625 struct intel_iommu *iommu = domain_get_iommu(domain);
627 start &= (((u64)1) << addr_width) - 1;
628 end &= (((u64)1) << addr_width) - 1;
630 /* we don't need lock here, nobody else touches the iova range */
632 while (level <= total) {
633 tmp = align_to_level(start, level);
634 if (tmp >= end || (tmp + level_size(level) > end))
638 pte = dma_addr_level_pte(domain, tmp, level);
641 phys_to_virt(dma_pte_addr(pte)));
643 __iommu_flush_cache(iommu,
646 tmp += level_size(level);
651 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
652 free_pgtable_page(domain->pgd);
658 static int iommu_alloc_root_entry(struct intel_iommu *iommu)
660 struct root_entry *root;
663 root = (struct root_entry *)alloc_pgtable_page();
667 __iommu_flush_cache(iommu, root, ROOT_SIZE);
669 spin_lock_irqsave(&iommu->lock, flags);
670 iommu->root_entry = root;
671 spin_unlock_irqrestore(&iommu->lock, flags);
676 static void iommu_set_root_entry(struct intel_iommu *iommu)
682 addr = iommu->root_entry;
684 spin_lock_irqsave(&iommu->register_lock, flag);
685 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
687 cmd = iommu->gcmd | DMA_GCMD_SRTP;
688 writel(cmd, iommu->reg + DMAR_GCMD_REG);
690 /* Make sure hardware complete it */
691 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
692 readl, (sts & DMA_GSTS_RTPS), sts);
694 spin_unlock_irqrestore(&iommu->register_lock, flag);
697 static void iommu_flush_write_buffer(struct intel_iommu *iommu)
702 if (!cap_rwbf(iommu->cap))
704 val = iommu->gcmd | DMA_GCMD_WBF;
706 spin_lock_irqsave(&iommu->register_lock, flag);
707 writel(val, iommu->reg + DMAR_GCMD_REG);
709 /* Make sure hardware complete it */
710 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
711 readl, (!(val & DMA_GSTS_WBFS)), val);
713 spin_unlock_irqrestore(&iommu->register_lock, flag);
716 /* return value determine if we need a write buffer flush */
717 static int __iommu_flush_context(struct intel_iommu *iommu,
718 u16 did, u16 source_id, u8 function_mask, u64 type,
719 int non_present_entry_flush)
725 * In the non-present entry flush case, if hardware doesn't cache
726 * non-present entry we do nothing and if hardware cache non-present
727 * entry, we flush entries of domain 0 (the domain id is used to cache
728 * any non-present entries)
730 if (non_present_entry_flush) {
731 if (!cap_caching_mode(iommu->cap))
738 case DMA_CCMD_GLOBAL_INVL:
739 val = DMA_CCMD_GLOBAL_INVL;
741 case DMA_CCMD_DOMAIN_INVL:
742 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
744 case DMA_CCMD_DEVICE_INVL:
745 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
746 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
753 spin_lock_irqsave(&iommu->register_lock, flag);
754 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
756 /* Make sure hardware complete it */
757 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
758 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
760 spin_unlock_irqrestore(&iommu->register_lock, flag);
762 /* flush context entry will implicitly flush write buffer */
766 /* return value determine if we need a write buffer flush */
767 static int __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
768 u64 addr, unsigned int size_order, u64 type,
769 int non_present_entry_flush)
771 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
772 u64 val = 0, val_iva = 0;
776 * In the non-present entry flush case, if hardware doesn't cache
777 * non-present entry we do nothing and if hardware cache non-present
778 * entry, we flush entries of domain 0 (the domain id is used to cache
779 * any non-present entries)
781 if (non_present_entry_flush) {
782 if (!cap_caching_mode(iommu->cap))
789 case DMA_TLB_GLOBAL_FLUSH:
790 /* global flush doesn't need set IVA_REG */
791 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
793 case DMA_TLB_DSI_FLUSH:
794 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
796 case DMA_TLB_PSI_FLUSH:
797 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
798 /* Note: always flush non-leaf currently */
799 val_iva = size_order | addr;
804 /* Note: set drain read/write */
807 * This is probably to be super secure.. Looks like we can
808 * ignore it without any impact.
810 if (cap_read_drain(iommu->cap))
811 val |= DMA_TLB_READ_DRAIN;
813 if (cap_write_drain(iommu->cap))
814 val |= DMA_TLB_WRITE_DRAIN;
816 spin_lock_irqsave(&iommu->register_lock, flag);
817 /* Note: Only uses first TLB reg currently */
819 dmar_writeq(iommu->reg + tlb_offset, val_iva);
820 dmar_writeq(iommu->reg + tlb_offset + 8, val);
822 /* Make sure hardware complete it */
823 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
824 dmar_readq, (!(val & DMA_TLB_IVT)), val);
826 spin_unlock_irqrestore(&iommu->register_lock, flag);
828 /* check IOTLB invalidation granularity */
829 if (DMA_TLB_IAIG(val) == 0)
830 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
831 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
832 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
833 (unsigned long long)DMA_TLB_IIRG(type),
834 (unsigned long long)DMA_TLB_IAIG(val));
835 /* flush iotlb entry will implicitly flush write buffer */
839 static int iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
840 u64 addr, unsigned int pages, int non_present_entry_flush)
844 BUG_ON(addr & (~VTD_PAGE_MASK));
847 /* Fallback to domain selective flush if no PSI support */
848 if (!cap_pgsel_inv(iommu->cap))
849 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
851 non_present_entry_flush);
854 * PSI requires page size to be 2 ^ x, and the base address is naturally
855 * aligned to the size
857 mask = ilog2(__roundup_pow_of_two(pages));
858 /* Fallback to domain selective flush if size is too big */
859 if (mask > cap_max_amask_val(iommu->cap))
860 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
861 DMA_TLB_DSI_FLUSH, non_present_entry_flush);
863 return iommu->flush.flush_iotlb(iommu, did, addr, mask,
865 non_present_entry_flush);
868 static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
873 spin_lock_irqsave(&iommu->register_lock, flags);
874 pmen = readl(iommu->reg + DMAR_PMEN_REG);
875 pmen &= ~DMA_PMEN_EPM;
876 writel(pmen, iommu->reg + DMAR_PMEN_REG);
878 /* wait for the protected region status bit to clear */
879 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
880 readl, !(pmen & DMA_PMEN_PRS), pmen);
882 spin_unlock_irqrestore(&iommu->register_lock, flags);
885 static int iommu_enable_translation(struct intel_iommu *iommu)
890 spin_lock_irqsave(&iommu->register_lock, flags);
891 writel(iommu->gcmd|DMA_GCMD_TE, iommu->reg + DMAR_GCMD_REG);
893 /* Make sure hardware complete it */
894 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
895 readl, (sts & DMA_GSTS_TES), sts);
897 iommu->gcmd |= DMA_GCMD_TE;
898 spin_unlock_irqrestore(&iommu->register_lock, flags);
902 static int iommu_disable_translation(struct intel_iommu *iommu)
907 spin_lock_irqsave(&iommu->register_lock, flag);
908 iommu->gcmd &= ~DMA_GCMD_TE;
909 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
911 /* Make sure hardware complete it */
912 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
913 readl, (!(sts & DMA_GSTS_TES)), sts);
915 spin_unlock_irqrestore(&iommu->register_lock, flag);
919 /* iommu interrupt handling. Most stuff are MSI-like. */
921 static const char *fault_reason_strings[] =
924 "Present bit in root entry is clear",
925 "Present bit in context entry is clear",
926 "Invalid context entry",
927 "Access beyond MGAW",
928 "PTE Write access is not set",
929 "PTE Read access is not set",
930 "Next page table ptr is invalid",
931 "Root table address invalid",
932 "Context table ptr is invalid",
933 "non-zero reserved fields in RTP",
934 "non-zero reserved fields in CTP",
935 "non-zero reserved fields in PTE",
937 #define MAX_FAULT_REASON_IDX (ARRAY_SIZE(fault_reason_strings) - 1)
939 const char *dmar_get_fault_reason(u8 fault_reason)
941 if (fault_reason > MAX_FAULT_REASON_IDX)
944 return fault_reason_strings[fault_reason];
947 void dmar_msi_unmask(unsigned int irq)
949 struct intel_iommu *iommu = get_irq_data(irq);
953 spin_lock_irqsave(&iommu->register_lock, flag);
954 writel(0, iommu->reg + DMAR_FECTL_REG);
955 /* Read a reg to force flush the post write */
956 readl(iommu->reg + DMAR_FECTL_REG);
957 spin_unlock_irqrestore(&iommu->register_lock, flag);
960 void dmar_msi_mask(unsigned int irq)
963 struct intel_iommu *iommu = get_irq_data(irq);
966 spin_lock_irqsave(&iommu->register_lock, flag);
967 writel(DMA_FECTL_IM, iommu->reg + DMAR_FECTL_REG);
968 /* Read a reg to force flush the post write */
969 readl(iommu->reg + DMAR_FECTL_REG);
970 spin_unlock_irqrestore(&iommu->register_lock, flag);
973 void dmar_msi_write(int irq, struct msi_msg *msg)
975 struct intel_iommu *iommu = get_irq_data(irq);
978 spin_lock_irqsave(&iommu->register_lock, flag);
979 writel(msg->data, iommu->reg + DMAR_FEDATA_REG);
980 writel(msg->address_lo, iommu->reg + DMAR_FEADDR_REG);
981 writel(msg->address_hi, iommu->reg + DMAR_FEUADDR_REG);
982 spin_unlock_irqrestore(&iommu->register_lock, flag);
985 void dmar_msi_read(int irq, struct msi_msg *msg)
987 struct intel_iommu *iommu = get_irq_data(irq);
990 spin_lock_irqsave(&iommu->register_lock, flag);
991 msg->data = readl(iommu->reg + DMAR_FEDATA_REG);
992 msg->address_lo = readl(iommu->reg + DMAR_FEADDR_REG);
993 msg->address_hi = readl(iommu->reg + DMAR_FEUADDR_REG);
994 spin_unlock_irqrestore(&iommu->register_lock, flag);
997 static int iommu_page_fault_do_one(struct intel_iommu *iommu, int type,
998 u8 fault_reason, u16 source_id, unsigned long long addr)
1002 reason = dmar_get_fault_reason(fault_reason);
1005 "DMAR:[%s] Request device [%02x:%02x.%d] "
1006 "fault addr %llx \n"
1007 "DMAR:[fault reason %02d] %s\n",
1008 (type ? "DMA Read" : "DMA Write"),
1009 (source_id >> 8), PCI_SLOT(source_id & 0xFF),
1010 PCI_FUNC(source_id & 0xFF), addr, fault_reason, reason);
1014 #define PRIMARY_FAULT_REG_LEN (16)
1015 static irqreturn_t iommu_page_fault(int irq, void *dev_id)
1017 struct intel_iommu *iommu = dev_id;
1018 int reg, fault_index;
1022 spin_lock_irqsave(&iommu->register_lock, flag);
1023 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1025 /* TBD: ignore advanced fault log currently */
1026 if (!(fault_status & DMA_FSTS_PPF))
1027 goto clear_overflow;
1029 fault_index = dma_fsts_fault_record_index(fault_status);
1030 reg = cap_fault_reg_offset(iommu->cap);
1038 /* highest 32 bits */
1039 data = readl(iommu->reg + reg +
1040 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1041 if (!(data & DMA_FRCD_F))
1044 fault_reason = dma_frcd_fault_reason(data);
1045 type = dma_frcd_type(data);
1047 data = readl(iommu->reg + reg +
1048 fault_index * PRIMARY_FAULT_REG_LEN + 8);
1049 source_id = dma_frcd_source_id(data);
1051 guest_addr = dmar_readq(iommu->reg + reg +
1052 fault_index * PRIMARY_FAULT_REG_LEN);
1053 guest_addr = dma_frcd_page_addr(guest_addr);
1054 /* clear the fault */
1055 writel(DMA_FRCD_F, iommu->reg + reg +
1056 fault_index * PRIMARY_FAULT_REG_LEN + 12);
1058 spin_unlock_irqrestore(&iommu->register_lock, flag);
1060 iommu_page_fault_do_one(iommu, type, fault_reason,
1061 source_id, guest_addr);
1064 if (fault_index > cap_num_fault_regs(iommu->cap))
1066 spin_lock_irqsave(&iommu->register_lock, flag);
1069 /* clear primary fault overflow */
1070 fault_status = readl(iommu->reg + DMAR_FSTS_REG);
1071 if (fault_status & DMA_FSTS_PFO)
1072 writel(DMA_FSTS_PFO, iommu->reg + DMAR_FSTS_REG);
1074 spin_unlock_irqrestore(&iommu->register_lock, flag);
1078 int dmar_set_interrupt(struct intel_iommu *iommu)
1084 printk(KERN_ERR "IOMMU: no free vectors\n");
1088 set_irq_data(irq, iommu);
1091 ret = arch_setup_dmar_msi(irq);
1093 set_irq_data(irq, NULL);
1099 /* Force fault register is cleared */
1100 iommu_page_fault(irq, iommu);
1102 ret = request_irq(irq, iommu_page_fault, 0, iommu->name, iommu);
1104 printk(KERN_ERR "IOMMU: can't request irq\n");
1108 static int iommu_init_domains(struct intel_iommu *iommu)
1110 unsigned long ndomains;
1111 unsigned long nlongs;
1113 ndomains = cap_ndoms(iommu->cap);
1114 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1115 nlongs = BITS_TO_LONGS(ndomains);
1117 /* TBD: there might be 64K domains,
1118 * consider other allocation for future chip
1120 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1121 if (!iommu->domain_ids) {
1122 printk(KERN_ERR "Allocating domain id array failed\n");
1125 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1127 if (!iommu->domains) {
1128 printk(KERN_ERR "Allocating domain array failed\n");
1129 kfree(iommu->domain_ids);
1133 spin_lock_init(&iommu->lock);
1136 * if Caching mode is set, then invalid translations are tagged
1137 * with domainid 0. Hence we need to pre-allocate it.
1139 if (cap_caching_mode(iommu->cap))
1140 set_bit(0, iommu->domain_ids);
1145 static void domain_exit(struct dmar_domain *domain);
1147 void free_dmar_iommu(struct intel_iommu *iommu)
1149 struct dmar_domain *domain;
1152 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1153 for (; i < cap_ndoms(iommu->cap); ) {
1154 domain = iommu->domains[i];
1155 clear_bit(i, iommu->domain_ids);
1156 domain_exit(domain);
1157 i = find_next_bit(iommu->domain_ids,
1158 cap_ndoms(iommu->cap), i+1);
1161 if (iommu->gcmd & DMA_GCMD_TE)
1162 iommu_disable_translation(iommu);
1165 set_irq_data(iommu->irq, NULL);
1166 /* This will mask the irq */
1167 free_irq(iommu->irq, iommu);
1168 destroy_irq(iommu->irq);
1171 kfree(iommu->domains);
1172 kfree(iommu->domain_ids);
1174 g_iommus[iommu->seq_id] = NULL;
1176 /* if all iommus are freed, free g_iommus */
1177 for (i = 0; i < g_num_of_iommus; i++) {
1182 if (i == g_num_of_iommus)
1185 /* free context mapping */
1186 free_context_table(iommu);
1189 static struct dmar_domain * iommu_alloc_domain(struct intel_iommu *iommu)
1192 unsigned long ndomains;
1193 struct dmar_domain *domain;
1194 unsigned long flags;
1196 domain = alloc_domain_mem();
1200 ndomains = cap_ndoms(iommu->cap);
1202 spin_lock_irqsave(&iommu->lock, flags);
1203 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1204 if (num >= ndomains) {
1205 spin_unlock_irqrestore(&iommu->lock, flags);
1206 free_domain_mem(domain);
1207 printk(KERN_ERR "IOMMU: no free domain ids\n");
1211 set_bit(num, iommu->domain_ids);
1213 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
1214 set_bit(iommu->seq_id, &domain->iommu_bmp);
1216 iommu->domains[num] = domain;
1217 spin_unlock_irqrestore(&iommu->lock, flags);
1222 static void iommu_free_domain(struct dmar_domain *domain)
1224 unsigned long flags;
1225 struct intel_iommu *iommu;
1227 iommu = domain_get_iommu(domain);
1229 spin_lock_irqsave(&iommu->lock, flags);
1230 clear_bit(domain->id, iommu->domain_ids);
1231 spin_unlock_irqrestore(&iommu->lock, flags);
1234 static struct iova_domain reserved_iova_list;
1235 static struct lock_class_key reserved_alloc_key;
1236 static struct lock_class_key reserved_rbtree_key;
1238 static void dmar_init_reserved_ranges(void)
1240 struct pci_dev *pdev = NULL;
1245 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
1247 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1248 &reserved_alloc_key);
1249 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1250 &reserved_rbtree_key);
1252 /* IOAPIC ranges shouldn't be accessed by DMA */
1253 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1254 IOVA_PFN(IOAPIC_RANGE_END));
1256 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1258 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1259 for_each_pci_dev(pdev) {
1262 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1263 r = &pdev->resource[i];
1264 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1268 size = r->end - addr;
1269 size = PAGE_ALIGN(size);
1270 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1271 IOVA_PFN(size + addr) - 1);
1273 printk(KERN_ERR "Reserve iova failed\n");
1279 static void domain_reserve_special_ranges(struct dmar_domain *domain)
1281 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1284 static inline int guestwidth_to_adjustwidth(int gaw)
1287 int r = (gaw - 12) % 9;
1298 static int domain_init(struct dmar_domain *domain, int guest_width)
1300 struct intel_iommu *iommu;
1301 int adjust_width, agaw;
1302 unsigned long sagaw;
1304 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
1305 spin_lock_init(&domain->mapping_lock);
1307 domain_reserve_special_ranges(domain);
1309 /* calculate AGAW */
1310 iommu = domain_get_iommu(domain);
1311 if (guest_width > cap_mgaw(iommu->cap))
1312 guest_width = cap_mgaw(iommu->cap);
1313 domain->gaw = guest_width;
1314 adjust_width = guestwidth_to_adjustwidth(guest_width);
1315 agaw = width_to_agaw(adjust_width);
1316 sagaw = cap_sagaw(iommu->cap);
1317 if (!test_bit(agaw, &sagaw)) {
1318 /* hardware doesn't support it, choose a bigger one */
1319 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1320 agaw = find_next_bit(&sagaw, 5, agaw);
1324 domain->agaw = agaw;
1325 INIT_LIST_HEAD(&domain->devices);
1327 /* always allocate the top pgd */
1328 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1331 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
1335 static void domain_exit(struct dmar_domain *domain)
1339 /* Domain 0 is reserved, so dont process it */
1343 domain_remove_dev_info(domain);
1345 put_iova_domain(&domain->iovad);
1346 end = DOMAIN_MAX_ADDR(domain->gaw);
1347 end = end & (~PAGE_MASK);
1350 dma_pte_clear_range(domain, 0, end);
1352 /* free page tables */
1353 dma_pte_free_pagetable(domain, 0, end);
1355 iommu_free_domain(domain);
1356 free_domain_mem(domain);
1359 static int domain_context_mapping_one(struct dmar_domain *domain,
1362 struct context_entry *context;
1363 struct intel_iommu *iommu = domain_get_iommu(domain);
1364 unsigned long flags;
1366 pr_debug("Set context mapping for %02x:%02x.%d\n",
1367 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1368 BUG_ON(!domain->pgd);
1369 context = device_to_context_entry(iommu, bus, devfn);
1372 spin_lock_irqsave(&iommu->lock, flags);
1373 if (context_present(context)) {
1374 spin_unlock_irqrestore(&iommu->lock, flags);
1378 context_set_domain_id(context, domain->id);
1379 context_set_address_width(context, domain->agaw);
1380 context_set_address_root(context, virt_to_phys(domain->pgd));
1381 context_set_translation_type(context, CONTEXT_TT_MULTI_LEVEL);
1382 context_set_fault_enable(context);
1383 context_set_present(context);
1384 __iommu_flush_cache(iommu, context, sizeof(*context));
1386 /* it's a non-present to present mapping */
1387 if (iommu->flush.flush_context(iommu, domain->id,
1388 (((u16)bus) << 8) | devfn, DMA_CCMD_MASK_NOBIT,
1389 DMA_CCMD_DEVICE_INVL, 1))
1390 iommu_flush_write_buffer(iommu);
1392 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH, 0);
1394 spin_unlock_irqrestore(&iommu->lock, flags);
1399 domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev)
1402 struct pci_dev *tmp, *parent;
1404 ret = domain_context_mapping_one(domain, pdev->bus->number,
1409 /* dependent device mapping */
1410 tmp = pci_find_upstream_pcie_bridge(pdev);
1413 /* Secondary interface's bus number and devfn 0 */
1414 parent = pdev->bus->self;
1415 while (parent != tmp) {
1416 ret = domain_context_mapping_one(domain, parent->bus->number,
1420 parent = parent->bus->self;
1422 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1423 return domain_context_mapping_one(domain,
1424 tmp->subordinate->number, 0);
1425 else /* this is a legacy PCI bridge */
1426 return domain_context_mapping_one(domain,
1427 tmp->bus->number, tmp->devfn);
1430 static int domain_context_mapped(struct dmar_domain *domain,
1431 struct pci_dev *pdev)
1434 struct pci_dev *tmp, *parent;
1435 struct intel_iommu *iommu = domain_get_iommu(domain);
1437 ret = device_context_mapped(iommu,
1438 pdev->bus->number, pdev->devfn);
1441 /* dependent device mapping */
1442 tmp = pci_find_upstream_pcie_bridge(pdev);
1445 /* Secondary interface's bus number and devfn 0 */
1446 parent = pdev->bus->self;
1447 while (parent != tmp) {
1448 ret = device_context_mapped(iommu, parent->bus->number,
1452 parent = parent->bus->self;
1455 return device_context_mapped(iommu,
1456 tmp->subordinate->number, 0);
1458 return device_context_mapped(iommu,
1459 tmp->bus->number, tmp->devfn);
1463 domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1464 u64 hpa, size_t size, int prot)
1466 u64 start_pfn, end_pfn;
1467 struct dma_pte *pte;
1469 int addr_width = agaw_to_width(domain->agaw);
1470 struct intel_iommu *iommu = domain_get_iommu(domain);
1472 hpa &= (((u64)1) << addr_width) - 1;
1474 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1477 start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1478 end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
1480 while (start_pfn < end_pfn) {
1481 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
1484 /* We don't need lock here, nobody else
1485 * touches the iova range
1487 BUG_ON(dma_pte_addr(pte));
1488 dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT);
1489 dma_set_pte_prot(pte, prot);
1490 __iommu_flush_cache(iommu, pte, sizeof(*pte));
1497 static void detach_domain_for_dev(struct dmar_domain *domain, u8 bus, u8 devfn)
1499 struct intel_iommu *iommu = domain_get_iommu(domain);
1501 clear_context_table(iommu, bus, devfn);
1502 iommu->flush.flush_context(iommu, 0, 0, 0,
1503 DMA_CCMD_GLOBAL_INVL, 0);
1504 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
1505 DMA_TLB_GLOBAL_FLUSH, 0);
1508 static void domain_remove_dev_info(struct dmar_domain *domain)
1510 struct device_domain_info *info;
1511 unsigned long flags;
1513 spin_lock_irqsave(&device_domain_lock, flags);
1514 while (!list_empty(&domain->devices)) {
1515 info = list_entry(domain->devices.next,
1516 struct device_domain_info, link);
1517 list_del(&info->link);
1518 list_del(&info->global);
1520 info->dev->dev.archdata.iommu = NULL;
1521 spin_unlock_irqrestore(&device_domain_lock, flags);
1523 detach_domain_for_dev(info->domain, info->bus, info->devfn);
1524 free_devinfo_mem(info);
1526 spin_lock_irqsave(&device_domain_lock, flags);
1528 spin_unlock_irqrestore(&device_domain_lock, flags);
1533 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
1535 static struct dmar_domain *
1536 find_domain(struct pci_dev *pdev)
1538 struct device_domain_info *info;
1540 /* No lock here, assumes no domain exit in normal case */
1541 info = pdev->dev.archdata.iommu;
1543 return info->domain;
1547 /* domain is initialized */
1548 static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1550 struct dmar_domain *domain, *found = NULL;
1551 struct intel_iommu *iommu;
1552 struct dmar_drhd_unit *drhd;
1553 struct device_domain_info *info, *tmp;
1554 struct pci_dev *dev_tmp;
1555 unsigned long flags;
1556 int bus = 0, devfn = 0;
1558 domain = find_domain(pdev);
1562 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1564 if (dev_tmp->is_pcie) {
1565 bus = dev_tmp->subordinate->number;
1568 bus = dev_tmp->bus->number;
1569 devfn = dev_tmp->devfn;
1571 spin_lock_irqsave(&device_domain_lock, flags);
1572 list_for_each_entry(info, &device_domain_list, global) {
1573 if (info->bus == bus && info->devfn == devfn) {
1574 found = info->domain;
1578 spin_unlock_irqrestore(&device_domain_lock, flags);
1579 /* pcie-pci bridge already has a domain, uses it */
1586 /* Allocate new domain for the device */
1587 drhd = dmar_find_matched_drhd_unit(pdev);
1589 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1593 iommu = drhd->iommu;
1595 domain = iommu_alloc_domain(iommu);
1599 if (domain_init(domain, gaw)) {
1600 domain_exit(domain);
1604 /* register pcie-to-pci device */
1606 info = alloc_devinfo_mem();
1608 domain_exit(domain);
1612 info->devfn = devfn;
1614 info->domain = domain;
1615 /* This domain is shared by devices under p2p bridge */
1616 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
1618 /* pcie-to-pci bridge already has a domain, uses it */
1620 spin_lock_irqsave(&device_domain_lock, flags);
1621 list_for_each_entry(tmp, &device_domain_list, global) {
1622 if (tmp->bus == bus && tmp->devfn == devfn) {
1623 found = tmp->domain;
1628 free_devinfo_mem(info);
1629 domain_exit(domain);
1632 list_add(&info->link, &domain->devices);
1633 list_add(&info->global, &device_domain_list);
1635 spin_unlock_irqrestore(&device_domain_lock, flags);
1639 info = alloc_devinfo_mem();
1642 info->bus = pdev->bus->number;
1643 info->devfn = pdev->devfn;
1645 info->domain = domain;
1646 spin_lock_irqsave(&device_domain_lock, flags);
1647 /* somebody is fast */
1648 found = find_domain(pdev);
1649 if (found != NULL) {
1650 spin_unlock_irqrestore(&device_domain_lock, flags);
1651 if (found != domain) {
1652 domain_exit(domain);
1655 free_devinfo_mem(info);
1658 list_add(&info->link, &domain->devices);
1659 list_add(&info->global, &device_domain_list);
1660 pdev->dev.archdata.iommu = info;
1661 spin_unlock_irqrestore(&device_domain_lock, flags);
1664 /* recheck it here, maybe others set it */
1665 return find_domain(pdev);
1668 static int iommu_prepare_identity_map(struct pci_dev *pdev,
1669 unsigned long long start,
1670 unsigned long long end)
1672 struct dmar_domain *domain;
1674 unsigned long long base;
1678 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1679 pci_name(pdev), start, end);
1680 /* page table init */
1681 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1685 /* The address might not be aligned */
1686 base = start & PAGE_MASK;
1688 size = PAGE_ALIGN(size);
1689 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1690 IOVA_PFN(base + size) - 1)) {
1691 printk(KERN_ERR "IOMMU: reserve iova failed\n");
1696 pr_debug("Mapping reserved region %lx@%llx for %s\n",
1697 size, base, pci_name(pdev));
1699 * RMRR range might have overlap with physical memory range,
1702 dma_pte_clear_range(domain, base, base + size);
1704 ret = domain_page_mapping(domain, base, base, size,
1705 DMA_PTE_READ|DMA_PTE_WRITE);
1709 /* context entry init */
1710 ret = domain_context_mapping(domain, pdev);
1714 domain_exit(domain);
1719 static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1720 struct pci_dev *pdev)
1722 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
1724 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1725 rmrr->end_address + 1);
1728 #ifdef CONFIG_DMAR_GFX_WA
1729 struct iommu_prepare_data {
1730 struct pci_dev *pdev;
1734 static int __init iommu_prepare_work_fn(unsigned long start_pfn,
1735 unsigned long end_pfn, void *datax)
1737 struct iommu_prepare_data *data;
1739 data = (struct iommu_prepare_data *)datax;
1741 data->ret = iommu_prepare_identity_map(data->pdev,
1742 start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
1747 static int __init iommu_prepare_with_active_regions(struct pci_dev *pdev)
1750 struct iommu_prepare_data data;
1755 for_each_online_node(nid) {
1756 work_with_active_regions(nid, iommu_prepare_work_fn, &data);
1763 static void __init iommu_prepare_gfx_mapping(void)
1765 struct pci_dev *pdev = NULL;
1768 for_each_pci_dev(pdev) {
1769 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO ||
1770 !IS_GFX_DEVICE(pdev))
1772 printk(KERN_INFO "IOMMU: gfx device %s 1-1 mapping\n",
1774 ret = iommu_prepare_with_active_regions(pdev);
1776 printk(KERN_ERR "IOMMU: mapping reserved region failed\n");
1779 #else /* !CONFIG_DMAR_GFX_WA */
1780 static inline void iommu_prepare_gfx_mapping(void)
1786 #ifdef CONFIG_DMAR_FLOPPY_WA
1787 static inline void iommu_prepare_isa(void)
1789 struct pci_dev *pdev;
1792 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1796 printk(KERN_INFO "IOMMU: Prepare 0-16M unity mapping for LPC\n");
1797 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1800 printk("IOMMU: Failed to create 0-64M identity map, "
1801 "floppy might not work\n");
1805 static inline void iommu_prepare_isa(void)
1809 #endif /* !CONFIG_DMAR_FLPY_WA */
1811 static int __init init_dmars(void)
1813 struct dmar_drhd_unit *drhd;
1814 struct dmar_rmrr_unit *rmrr;
1815 struct pci_dev *pdev;
1816 struct intel_iommu *iommu;
1817 int i, ret, unit = 0;
1822 * initialize and program root entry to not present
1825 for_each_drhd_unit(drhd) {
1828 * lock not needed as this is only incremented in the single
1829 * threaded kernel __init code path all other access are read
1834 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
1837 printk(KERN_ERR "Allocating global iommu array failed\n");
1842 deferred_flush = kzalloc(g_num_of_iommus *
1843 sizeof(struct deferred_flush_tables), GFP_KERNEL);
1844 if (!deferred_flush) {
1850 for_each_drhd_unit(drhd) {
1854 iommu = drhd->iommu;
1855 g_iommus[iommu->seq_id] = iommu;
1857 ret = iommu_init_domains(iommu);
1863 * we could share the same root & context tables
1864 * amoung all IOMMU's. Need to Split it later.
1866 ret = iommu_alloc_root_entry(iommu);
1868 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
1873 for_each_drhd_unit(drhd) {
1877 iommu = drhd->iommu;
1878 if (dmar_enable_qi(iommu)) {
1880 * Queued Invalidate not enabled, use Register Based
1883 iommu->flush.flush_context = __iommu_flush_context;
1884 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
1885 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
1887 (unsigned long long)drhd->reg_base_addr);
1889 iommu->flush.flush_context = qi_flush_context;
1890 iommu->flush.flush_iotlb = qi_flush_iotlb;
1891 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
1893 (unsigned long long)drhd->reg_base_addr);
1899 * for each dev attached to rmrr
1901 * locate drhd for dev, alloc domain for dev
1902 * allocate free domain
1903 * allocate page table entries for rmrr
1904 * if context not allocated for bus
1905 * allocate and init context
1906 * set present in root table for this bus
1907 * init context with domain, translation etc
1911 for_each_rmrr_units(rmrr) {
1912 for (i = 0; i < rmrr->devices_cnt; i++) {
1913 pdev = rmrr->devices[i];
1914 /* some BIOS lists non-exist devices in DMAR table */
1917 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
1920 "IOMMU: mapping reserved region failed\n");
1924 iommu_prepare_gfx_mapping();
1926 iommu_prepare_isa();
1931 * global invalidate context cache
1932 * global invalidate iotlb
1933 * enable translation
1935 for_each_drhd_unit(drhd) {
1938 iommu = drhd->iommu;
1939 sprintf (iommu->name, "dmar%d", unit++);
1941 iommu_flush_write_buffer(iommu);
1943 ret = dmar_set_interrupt(iommu);
1947 iommu_set_root_entry(iommu);
1949 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL,
1951 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH,
1953 iommu_disable_protect_mem_regions(iommu);
1955 ret = iommu_enable_translation(iommu);
1962 for_each_drhd_unit(drhd) {
1965 iommu = drhd->iommu;
1972 static inline u64 aligned_size(u64 host_addr, size_t size)
1975 addr = (host_addr & (~PAGE_MASK)) + size;
1976 return PAGE_ALIGN(addr);
1980 iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
1984 /* Make sure it's in range */
1985 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
1986 if (!size || (IOVA_START_ADDR + size > end))
1989 piova = alloc_iova(&domain->iovad,
1990 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
1994 static struct iova *
1995 __intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
1996 size_t size, u64 dma_mask)
1998 struct pci_dev *pdev = to_pci_dev(dev);
1999 struct iova *iova = NULL;
2001 if (dma_mask <= DMA_32BIT_MASK || dmar_forcedac)
2002 iova = iommu_alloc_iova(domain, size, dma_mask);
2005 * First try to allocate an io virtual address in
2006 * DMA_32BIT_MASK and if that fails then try allocating
2009 iova = iommu_alloc_iova(domain, size, DMA_32BIT_MASK);
2011 iova = iommu_alloc_iova(domain, size, dma_mask);
2015 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2022 static struct dmar_domain *
2023 get_valid_domain_for_dev(struct pci_dev *pdev)
2025 struct dmar_domain *domain;
2028 domain = get_domain_for_dev(pdev,
2029 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2032 "Allocating domain for %s failed", pci_name(pdev));
2036 /* make sure context mapping is ok */
2037 if (unlikely(!domain_context_mapped(domain, pdev))) {
2038 ret = domain_context_mapping(domain, pdev);
2041 "Domain context map for %s failed",
2050 static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2051 size_t size, int dir, u64 dma_mask)
2053 struct pci_dev *pdev = to_pci_dev(hwdev);
2054 struct dmar_domain *domain;
2055 phys_addr_t start_paddr;
2059 struct intel_iommu *iommu;
2061 BUG_ON(dir == DMA_NONE);
2062 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2065 domain = get_valid_domain_for_dev(pdev);
2069 iommu = domain_get_iommu(domain);
2070 size = aligned_size((u64)paddr, size);
2072 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
2076 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
2079 * Check if DMAR supports zero-length reads on write only
2082 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
2083 !cap_zlr(iommu->cap))
2084 prot |= DMA_PTE_READ;
2085 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2086 prot |= DMA_PTE_WRITE;
2088 * paddr - (paddr + size) might be partial page, we should map the whole
2089 * page. Note: if two part of one page are separately mapped, we
2090 * might have two guest_addr mapping to the same host paddr, but this
2091 * is not a big problem
2093 ret = domain_page_mapping(domain, start_paddr,
2094 ((u64)paddr) & PAGE_MASK, size, prot);
2098 /* it's a non-present to present mapping */
2099 ret = iommu_flush_iotlb_psi(iommu, domain->id,
2100 start_paddr, size >> VTD_PAGE_SHIFT, 1);
2102 iommu_flush_write_buffer(iommu);
2104 return start_paddr + ((u64)paddr & (~PAGE_MASK));
2108 __free_iova(&domain->iovad, iova);
2109 printk(KERN_ERR"Device %s request: %lx@%llx dir %d --- failed\n",
2110 pci_name(pdev), size, (unsigned long long)paddr, dir);
2114 dma_addr_t intel_map_single(struct device *hwdev, phys_addr_t paddr,
2115 size_t size, int dir)
2117 return __intel_map_single(hwdev, paddr, size, dir,
2118 to_pci_dev(hwdev)->dma_mask);
2121 static void flush_unmaps(void)
2127 /* just flush them all */
2128 for (i = 0; i < g_num_of_iommus; i++) {
2129 struct intel_iommu *iommu = g_iommus[i];
2133 if (deferred_flush[i].next) {
2134 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2135 DMA_TLB_GLOBAL_FLUSH, 0);
2136 for (j = 0; j < deferred_flush[i].next; j++) {
2137 __free_iova(&deferred_flush[i].domain[j]->iovad,
2138 deferred_flush[i].iova[j]);
2140 deferred_flush[i].next = 0;
2147 static void flush_unmaps_timeout(unsigned long data)
2149 unsigned long flags;
2151 spin_lock_irqsave(&async_umap_flush_lock, flags);
2153 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2156 static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2158 unsigned long flags;
2160 struct intel_iommu *iommu;
2162 spin_lock_irqsave(&async_umap_flush_lock, flags);
2163 if (list_size == HIGH_WATER_MARK)
2166 iommu = domain_get_iommu(dom);
2167 iommu_id = iommu->seq_id;
2169 next = deferred_flush[iommu_id].next;
2170 deferred_flush[iommu_id].domain[next] = dom;
2171 deferred_flush[iommu_id].iova[next] = iova;
2172 deferred_flush[iommu_id].next++;
2175 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2179 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2182 void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2185 struct pci_dev *pdev = to_pci_dev(dev);
2186 struct dmar_domain *domain;
2187 unsigned long start_addr;
2189 struct intel_iommu *iommu;
2191 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2193 domain = find_domain(pdev);
2196 iommu = domain_get_iommu(domain);
2198 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2202 start_addr = iova->pfn_lo << PAGE_SHIFT;
2203 size = aligned_size((u64)dev_addr, size);
2205 pr_debug("Device %s unmapping: %lx@%llx\n",
2206 pci_name(pdev), size, (unsigned long long)start_addr);
2208 /* clear the whole page */
2209 dma_pte_clear_range(domain, start_addr, start_addr + size);
2210 /* free page tables */
2211 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2212 if (intel_iommu_strict) {
2213 if (iommu_flush_iotlb_psi(iommu,
2214 domain->id, start_addr, size >> VTD_PAGE_SHIFT, 0))
2215 iommu_flush_write_buffer(iommu);
2217 __free_iova(&domain->iovad, iova);
2219 add_unmap(domain, iova);
2221 * queue up the release of the unmap to save the 1/6th of the
2222 * cpu used up by the iotlb flush operation...
2227 void *intel_alloc_coherent(struct device *hwdev, size_t size,
2228 dma_addr_t *dma_handle, gfp_t flags)
2233 size = PAGE_ALIGN(size);
2234 order = get_order(size);
2235 flags &= ~(GFP_DMA | GFP_DMA32);
2237 vaddr = (void *)__get_free_pages(flags, order);
2240 memset(vaddr, 0, size);
2242 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2244 hwdev->coherent_dma_mask);
2247 free_pages((unsigned long)vaddr, order);
2251 void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2252 dma_addr_t dma_handle)
2256 size = PAGE_ALIGN(size);
2257 order = get_order(size);
2259 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2260 free_pages((unsigned long)vaddr, order);
2263 #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
2265 void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2266 int nelems, int dir)
2269 struct pci_dev *pdev = to_pci_dev(hwdev);
2270 struct dmar_domain *domain;
2271 unsigned long start_addr;
2275 struct scatterlist *sg;
2276 struct intel_iommu *iommu;
2278 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2281 domain = find_domain(pdev);
2284 iommu = domain_get_iommu(domain);
2286 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
2289 for_each_sg(sglist, sg, nelems, i) {
2290 addr = SG_ENT_VIRT_ADDRESS(sg);
2291 size += aligned_size((u64)addr, sg->length);
2294 start_addr = iova->pfn_lo << PAGE_SHIFT;
2296 /* clear the whole page */
2297 dma_pte_clear_range(domain, start_addr, start_addr + size);
2298 /* free page tables */
2299 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2301 if (iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2302 size >> VTD_PAGE_SHIFT, 0))
2303 iommu_flush_write_buffer(iommu);
2306 __free_iova(&domain->iovad, iova);
2309 static int intel_nontranslate_map_sg(struct device *hddev,
2310 struct scatterlist *sglist, int nelems, int dir)
2313 struct scatterlist *sg;
2315 for_each_sg(sglist, sg, nelems, i) {
2316 BUG_ON(!sg_page(sg));
2317 sg->dma_address = virt_to_bus(SG_ENT_VIRT_ADDRESS(sg));
2318 sg->dma_length = sg->length;
2323 int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2328 struct pci_dev *pdev = to_pci_dev(hwdev);
2329 struct dmar_domain *domain;
2333 struct iova *iova = NULL;
2335 struct scatterlist *sg;
2336 unsigned long start_addr;
2337 struct intel_iommu *iommu;
2339 BUG_ON(dir == DMA_NONE);
2340 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
2341 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
2343 domain = get_valid_domain_for_dev(pdev);
2347 iommu = domain_get_iommu(domain);
2349 for_each_sg(sglist, sg, nelems, i) {
2350 addr = SG_ENT_VIRT_ADDRESS(sg);
2351 addr = (void *)virt_to_phys(addr);
2352 size += aligned_size((u64)addr, sg->length);
2355 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
2357 sglist->dma_length = 0;
2362 * Check if DMAR supports zero-length reads on write only
2365 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
2366 !cap_zlr(iommu->cap))
2367 prot |= DMA_PTE_READ;
2368 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2369 prot |= DMA_PTE_WRITE;
2371 start_addr = iova->pfn_lo << PAGE_SHIFT;
2373 for_each_sg(sglist, sg, nelems, i) {
2374 addr = SG_ENT_VIRT_ADDRESS(sg);
2375 addr = (void *)virt_to_phys(addr);
2376 size = aligned_size((u64)addr, sg->length);
2377 ret = domain_page_mapping(domain, start_addr + offset,
2378 ((u64)addr) & PAGE_MASK,
2381 /* clear the page */
2382 dma_pte_clear_range(domain, start_addr,
2383 start_addr + offset);
2384 /* free page tables */
2385 dma_pte_free_pagetable(domain, start_addr,
2386 start_addr + offset);
2388 __free_iova(&domain->iovad, iova);
2391 sg->dma_address = start_addr + offset +
2392 ((u64)addr & (~PAGE_MASK));
2393 sg->dma_length = sg->length;
2397 /* it's a non-present to present mapping */
2398 if (iommu_flush_iotlb_psi(iommu, domain->id,
2399 start_addr, offset >> VTD_PAGE_SHIFT, 1))
2400 iommu_flush_write_buffer(iommu);
2404 static struct dma_mapping_ops intel_dma_ops = {
2405 .alloc_coherent = intel_alloc_coherent,
2406 .free_coherent = intel_free_coherent,
2407 .map_single = intel_map_single,
2408 .unmap_single = intel_unmap_single,
2409 .map_sg = intel_map_sg,
2410 .unmap_sg = intel_unmap_sg,
2413 static inline int iommu_domain_cache_init(void)
2417 iommu_domain_cache = kmem_cache_create("iommu_domain",
2418 sizeof(struct dmar_domain),
2423 if (!iommu_domain_cache) {
2424 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2431 static inline int iommu_devinfo_cache_init(void)
2435 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2436 sizeof(struct device_domain_info),
2440 if (!iommu_devinfo_cache) {
2441 printk(KERN_ERR "Couldn't create devinfo cache\n");
2448 static inline int iommu_iova_cache_init(void)
2452 iommu_iova_cache = kmem_cache_create("iommu_iova",
2453 sizeof(struct iova),
2457 if (!iommu_iova_cache) {
2458 printk(KERN_ERR "Couldn't create iova cache\n");
2465 static int __init iommu_init_mempool(void)
2468 ret = iommu_iova_cache_init();
2472 ret = iommu_domain_cache_init();
2476 ret = iommu_devinfo_cache_init();
2480 kmem_cache_destroy(iommu_domain_cache);
2482 kmem_cache_destroy(iommu_iova_cache);
2487 static void __init iommu_exit_mempool(void)
2489 kmem_cache_destroy(iommu_devinfo_cache);
2490 kmem_cache_destroy(iommu_domain_cache);
2491 kmem_cache_destroy(iommu_iova_cache);
2495 static void __init init_no_remapping_devices(void)
2497 struct dmar_drhd_unit *drhd;
2499 for_each_drhd_unit(drhd) {
2500 if (!drhd->include_all) {
2502 for (i = 0; i < drhd->devices_cnt; i++)
2503 if (drhd->devices[i] != NULL)
2505 /* ignore DMAR unit if no pci devices exist */
2506 if (i == drhd->devices_cnt)
2514 for_each_drhd_unit(drhd) {
2516 if (drhd->ignored || drhd->include_all)
2519 for (i = 0; i < drhd->devices_cnt; i++)
2520 if (drhd->devices[i] &&
2521 !IS_GFX_DEVICE(drhd->devices[i]))
2524 if (i < drhd->devices_cnt)
2527 /* bypass IOMMU if it is just for gfx devices */
2529 for (i = 0; i < drhd->devices_cnt; i++) {
2530 if (!drhd->devices[i])
2532 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
2537 int __init intel_iommu_init(void)
2541 if (dmar_table_init())
2544 if (dmar_dev_scope_init())
2548 * Check the need for DMA-remapping initialization now.
2549 * Above initialization will also be used by Interrupt-remapping.
2551 if (no_iommu || swiotlb || dmar_disabled)
2554 iommu_init_mempool();
2555 dmar_init_reserved_ranges();
2557 init_no_remapping_devices();
2561 printk(KERN_ERR "IOMMU: dmar init failed\n");
2562 put_iova_domain(&reserved_iova_list);
2563 iommu_exit_mempool();
2567 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
2569 init_timer(&unmap_timer);
2571 dma_ops = &intel_dma_ops;
2575 void intel_iommu_domain_exit(struct dmar_domain *domain)
2579 /* Domain 0 is reserved, so dont process it */
2583 end = DOMAIN_MAX_ADDR(domain->gaw);
2584 end = end & (~VTD_PAGE_MASK);
2587 dma_pte_clear_range(domain, 0, end);
2589 /* free page tables */
2590 dma_pte_free_pagetable(domain, 0, end);
2592 iommu_free_domain(domain);
2593 free_domain_mem(domain);
2595 EXPORT_SYMBOL_GPL(intel_iommu_domain_exit);
2597 struct dmar_domain *intel_iommu_domain_alloc(struct pci_dev *pdev)
2599 struct dmar_drhd_unit *drhd;
2600 struct dmar_domain *domain;
2601 struct intel_iommu *iommu;
2603 drhd = dmar_find_matched_drhd_unit(pdev);
2605 printk(KERN_ERR "intel_iommu_domain_alloc: drhd == NULL\n");
2609 iommu = drhd->iommu;
2612 "intel_iommu_domain_alloc: iommu == NULL\n");
2615 domain = iommu_alloc_domain(iommu);
2618 "intel_iommu_domain_alloc: domain == NULL\n");
2621 if (domain_init(domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2623 "intel_iommu_domain_alloc: domain_init() failed\n");
2624 intel_iommu_domain_exit(domain);
2629 EXPORT_SYMBOL_GPL(intel_iommu_domain_alloc);
2631 int intel_iommu_context_mapping(
2632 struct dmar_domain *domain, struct pci_dev *pdev)
2635 rc = domain_context_mapping(domain, pdev);
2638 EXPORT_SYMBOL_GPL(intel_iommu_context_mapping);
2640 int intel_iommu_page_mapping(
2641 struct dmar_domain *domain, dma_addr_t iova,
2642 u64 hpa, size_t size, int prot)
2645 rc = domain_page_mapping(domain, iova, hpa, size, prot);
2648 EXPORT_SYMBOL_GPL(intel_iommu_page_mapping);
2650 void intel_iommu_detach_dev(struct dmar_domain *domain, u8 bus, u8 devfn)
2652 detach_domain_for_dev(domain, bus, devfn);
2654 EXPORT_SYMBOL_GPL(intel_iommu_detach_dev);
2656 struct dmar_domain *
2657 intel_iommu_find_domain(struct pci_dev *pdev)
2659 return find_domain(pdev);
2661 EXPORT_SYMBOL_GPL(intel_iommu_find_domain);
2663 int intel_iommu_found(void)
2665 return g_num_of_iommus;
2667 EXPORT_SYMBOL_GPL(intel_iommu_found);
2669 u64 intel_iommu_iova_to_pfn(struct dmar_domain *domain, u64 iova)
2671 struct dma_pte *pte;
2675 pte = addr_to_dma_pte(domain, iova);
2678 pfn = dma_pte_addr(pte);
2680 return pfn >> VTD_PAGE_SHIFT;
2682 EXPORT_SYMBOL_GPL(intel_iommu_iova_to_pfn);