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) Ashok Raj <ashok.raj@intel.com>
18 * Copyright (C) Shaohua Li <shaohua.li@intel.com>
19 * Copyright (C) Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
22 #include <linux/init.h>
23 #include <linux/bitmap.h>
24 #include <linux/slab.h>
25 #include <linux/irq.h>
26 #include <linux/interrupt.h>
27 #include <linux/sysdev.h>
28 #include <linux/spinlock.h>
29 #include <linux/pci.h>
30 #include <linux/dmar.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/mempool.h>
34 #include "intel-iommu.h"
35 #include <asm/proto.h> /* force_iommu in this header in x86-64*/
36 #include <asm/cacheflush.h>
37 #include <asm/iommu.h>
40 #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
41 #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
43 #define IOAPIC_RANGE_START (0xfee00000)
44 #define IOAPIC_RANGE_END (0xfeefffff)
45 #define IOVA_START_ADDR (0x1000)
47 #define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
49 #define DMAR_OPERATION_TIMEOUT (HZ*60) /* 1m */
51 #define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
53 static void domain_remove_dev_info(struct dmar_domain *domain);
55 static int dmar_disabled;
56 static int __initdata dmar_map_gfx = 1;
58 #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
59 static DEFINE_SPINLOCK(device_domain_lock);
60 static LIST_HEAD(device_domain_list);
62 static int __init intel_iommu_setup(char *str)
67 if (!strncmp(str, "off", 3)) {
69 printk(KERN_INFO"Intel-IOMMU: disabled\n");
70 } else if (!strncmp(str, "igfx_off", 8)) {
73 "Intel-IOMMU: disable GFX device mapping\n");
76 str += strcspn(str, ",");
82 __setup("intel_iommu=", intel_iommu_setup);
84 static struct kmem_cache *iommu_domain_cache;
85 static struct kmem_cache *iommu_devinfo_cache;
86 static struct kmem_cache *iommu_iova_cache;
88 static inline void *alloc_pgtable_page(void)
90 return (void *)get_zeroed_page(GFP_ATOMIC);
93 static inline void free_pgtable_page(void *vaddr)
95 free_page((unsigned long)vaddr);
98 static inline void *alloc_domain_mem(void)
100 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
103 static inline void free_domain_mem(void *vaddr)
105 kmem_cache_free(iommu_domain_cache, vaddr);
108 static inline void * alloc_devinfo_mem(void)
110 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
113 static inline void free_devinfo_mem(void *vaddr)
115 kmem_cache_free(iommu_devinfo_cache, vaddr);
118 struct iova *alloc_iova_mem(void)
120 return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC);
123 void free_iova_mem(struct iova *iova)
125 kmem_cache_free(iommu_iova_cache, iova);
128 static inline void __iommu_flush_cache(
129 struct intel_iommu *iommu, void *addr, int size)
131 if (!ecap_coherent(iommu->ecap))
132 clflush_cache_range(addr, size);
135 /* Gets context entry for a given bus and devfn */
136 static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
139 struct root_entry *root;
140 struct context_entry *context;
141 unsigned long phy_addr;
144 spin_lock_irqsave(&iommu->lock, flags);
145 root = &iommu->root_entry[bus];
146 context = get_context_addr_from_root(root);
148 context = (struct context_entry *)alloc_pgtable_page();
150 spin_unlock_irqrestore(&iommu->lock, flags);
153 __iommu_flush_cache(iommu, (void *)context, PAGE_SIZE_4K);
154 phy_addr = virt_to_phys((void *)context);
155 set_root_value(root, phy_addr);
156 set_root_present(root);
157 __iommu_flush_cache(iommu, root, sizeof(*root));
159 spin_unlock_irqrestore(&iommu->lock, flags);
160 return &context[devfn];
163 static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
165 struct root_entry *root;
166 struct context_entry *context;
170 spin_lock_irqsave(&iommu->lock, flags);
171 root = &iommu->root_entry[bus];
172 context = get_context_addr_from_root(root);
177 ret = context_present(context[devfn]);
179 spin_unlock_irqrestore(&iommu->lock, flags);
183 static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
185 struct root_entry *root;
186 struct context_entry *context;
189 spin_lock_irqsave(&iommu->lock, flags);
190 root = &iommu->root_entry[bus];
191 context = get_context_addr_from_root(root);
193 context_clear_entry(context[devfn]);
194 __iommu_flush_cache(iommu, &context[devfn], \
197 spin_unlock_irqrestore(&iommu->lock, flags);
200 static void free_context_table(struct intel_iommu *iommu)
202 struct root_entry *root;
205 struct context_entry *context;
207 spin_lock_irqsave(&iommu->lock, flags);
208 if (!iommu->root_entry) {
211 for (i = 0; i < ROOT_ENTRY_NR; i++) {
212 root = &iommu->root_entry[i];
213 context = get_context_addr_from_root(root);
215 free_pgtable_page(context);
217 free_pgtable_page(iommu->root_entry);
218 iommu->root_entry = NULL;
220 spin_unlock_irqrestore(&iommu->lock, flags);
223 /* page table handling */
224 #define LEVEL_STRIDE (9)
225 #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
227 static inline int agaw_to_level(int agaw)
232 static inline int agaw_to_width(int agaw)
234 return 30 + agaw * LEVEL_STRIDE;
238 static inline int width_to_agaw(int width)
240 return (width - 30) / LEVEL_STRIDE;
243 static inline unsigned int level_to_offset_bits(int level)
245 return (12 + (level - 1) * LEVEL_STRIDE);
248 static inline int address_level_offset(u64 addr, int level)
250 return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
253 static inline u64 level_mask(int level)
255 return ((u64)-1 << level_to_offset_bits(level));
258 static inline u64 level_size(int level)
260 return ((u64)1 << level_to_offset_bits(level));
263 static inline u64 align_to_level(u64 addr, int level)
265 return ((addr + level_size(level) - 1) & level_mask(level));
268 static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
270 int addr_width = agaw_to_width(domain->agaw);
271 struct dma_pte *parent, *pte = NULL;
272 int level = agaw_to_level(domain->agaw);
276 BUG_ON(!domain->pgd);
278 addr &= (((u64)1) << addr_width) - 1;
279 parent = domain->pgd;
281 spin_lock_irqsave(&domain->mapping_lock, flags);
285 offset = address_level_offset(addr, level);
286 pte = &parent[offset];
290 if (!dma_pte_present(*pte)) {
291 tmp_page = alloc_pgtable_page();
294 spin_unlock_irqrestore(&domain->mapping_lock,
298 __iommu_flush_cache(domain->iommu, tmp_page,
300 dma_set_pte_addr(*pte, virt_to_phys(tmp_page));
302 * high level table always sets r/w, last level page
303 * table control read/write
305 dma_set_pte_readable(*pte);
306 dma_set_pte_writable(*pte);
307 __iommu_flush_cache(domain->iommu, pte, sizeof(*pte));
309 parent = phys_to_virt(dma_pte_addr(*pte));
313 spin_unlock_irqrestore(&domain->mapping_lock, flags);
317 /* return address's pte at specific level */
318 static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
321 struct dma_pte *parent, *pte = NULL;
322 int total = agaw_to_level(domain->agaw);
325 parent = domain->pgd;
326 while (level <= total) {
327 offset = address_level_offset(addr, total);
328 pte = &parent[offset];
332 if (!dma_pte_present(*pte))
334 parent = phys_to_virt(dma_pte_addr(*pte));
340 /* clear one page's page table */
341 static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
343 struct dma_pte *pte = NULL;
345 /* get last level pte */
346 pte = dma_addr_level_pte(domain, addr, 1);
350 __iommu_flush_cache(domain->iommu, pte, sizeof(*pte));
354 /* clear last level pte, a tlb flush should be followed */
355 static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
357 int addr_width = agaw_to_width(domain->agaw);
359 start &= (((u64)1) << addr_width) - 1;
360 end &= (((u64)1) << addr_width) - 1;
361 /* in case it's partial page */
362 start = PAGE_ALIGN_4K(start);
365 /* we don't need lock here, nobody else touches the iova range */
366 while (start < end) {
367 dma_pte_clear_one(domain, start);
368 start += PAGE_SIZE_4K;
372 /* free page table pages. last level pte should already be cleared */
373 static void dma_pte_free_pagetable(struct dmar_domain *domain,
376 int addr_width = agaw_to_width(domain->agaw);
378 int total = agaw_to_level(domain->agaw);
382 start &= (((u64)1) << addr_width) - 1;
383 end &= (((u64)1) << addr_width) - 1;
385 /* we don't need lock here, nobody else touches the iova range */
387 while (level <= total) {
388 tmp = align_to_level(start, level);
389 if (tmp >= end || (tmp + level_size(level) > end))
393 pte = dma_addr_level_pte(domain, tmp, level);
396 phys_to_virt(dma_pte_addr(*pte)));
398 __iommu_flush_cache(domain->iommu,
401 tmp += level_size(level);
406 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
407 free_pgtable_page(domain->pgd);
413 static int iommu_alloc_root_entry(struct intel_iommu *iommu)
415 struct root_entry *root;
418 root = (struct root_entry *)alloc_pgtable_page();
422 __iommu_flush_cache(iommu, root, PAGE_SIZE_4K);
424 spin_lock_irqsave(&iommu->lock, flags);
425 iommu->root_entry = root;
426 spin_unlock_irqrestore(&iommu->lock, flags);
431 #define IOMMU_WAIT_OP(iommu, offset, op, cond, sts) \
433 unsigned long start_time = jiffies;\
435 sts = op (iommu->reg + offset);\
438 if (time_after(jiffies, start_time + DMAR_OPERATION_TIMEOUT))\
439 panic("DMAR hardware is malfunctioning\n");\
444 static void iommu_set_root_entry(struct intel_iommu *iommu)
450 addr = iommu->root_entry;
452 spin_lock_irqsave(&iommu->register_lock, flag);
453 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
455 cmd = iommu->gcmd | DMA_GCMD_SRTP;
456 writel(cmd, iommu->reg + DMAR_GCMD_REG);
458 /* Make sure hardware complete it */
459 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
460 readl, (sts & DMA_GSTS_RTPS), sts);
462 spin_unlock_irqrestore(&iommu->register_lock, flag);
465 static void iommu_flush_write_buffer(struct intel_iommu *iommu)
470 if (!cap_rwbf(iommu->cap))
472 val = iommu->gcmd | DMA_GCMD_WBF;
474 spin_lock_irqsave(&iommu->register_lock, flag);
475 writel(val, iommu->reg + DMAR_GCMD_REG);
477 /* Make sure hardware complete it */
478 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
479 readl, (!(val & DMA_GSTS_WBFS)), val);
481 spin_unlock_irqrestore(&iommu->register_lock, flag);
484 /* return value determine if we need a write buffer flush */
485 static int __iommu_flush_context(struct intel_iommu *iommu,
486 u16 did, u16 source_id, u8 function_mask, u64 type,
487 int non_present_entry_flush)
493 * In the non-present entry flush case, if hardware doesn't cache
494 * non-present entry we do nothing and if hardware cache non-present
495 * entry, we flush entries of domain 0 (the domain id is used to cache
496 * any non-present entries)
498 if (non_present_entry_flush) {
499 if (!cap_caching_mode(iommu->cap))
506 case DMA_CCMD_GLOBAL_INVL:
507 val = DMA_CCMD_GLOBAL_INVL;
509 case DMA_CCMD_DOMAIN_INVL:
510 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
512 case DMA_CCMD_DEVICE_INVL:
513 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
514 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
521 spin_lock_irqsave(&iommu->register_lock, flag);
522 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
524 /* Make sure hardware complete it */
525 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
526 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
528 spin_unlock_irqrestore(&iommu->register_lock, flag);
530 /* flush context entry will implictly flush write buffer */
534 static int inline iommu_flush_context_global(struct intel_iommu *iommu,
535 int non_present_entry_flush)
537 return __iommu_flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL,
538 non_present_entry_flush);
541 static int inline iommu_flush_context_domain(struct intel_iommu *iommu, u16 did,
542 int non_present_entry_flush)
544 return __iommu_flush_context(iommu, did, 0, 0, DMA_CCMD_DOMAIN_INVL,
545 non_present_entry_flush);
548 static int inline iommu_flush_context_device(struct intel_iommu *iommu,
549 u16 did, u16 source_id, u8 function_mask, int non_present_entry_flush)
551 return __iommu_flush_context(iommu, did, source_id, function_mask,
552 DMA_CCMD_DEVICE_INVL, non_present_entry_flush);
555 /* return value determine if we need a write buffer flush */
556 static int __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
557 u64 addr, unsigned int size_order, u64 type,
558 int non_present_entry_flush)
560 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
561 u64 val = 0, val_iva = 0;
565 * In the non-present entry flush case, if hardware doesn't cache
566 * non-present entry we do nothing and if hardware cache non-present
567 * entry, we flush entries of domain 0 (the domain id is used to cache
568 * any non-present entries)
570 if (non_present_entry_flush) {
571 if (!cap_caching_mode(iommu->cap))
578 case DMA_TLB_GLOBAL_FLUSH:
579 /* global flush doesn't need set IVA_REG */
580 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
582 case DMA_TLB_DSI_FLUSH:
583 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
585 case DMA_TLB_PSI_FLUSH:
586 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
587 /* Note: always flush non-leaf currently */
588 val_iva = size_order | addr;
593 /* Note: set drain read/write */
596 * This is probably to be super secure.. Looks like we can
597 * ignore it without any impact.
599 if (cap_read_drain(iommu->cap))
600 val |= DMA_TLB_READ_DRAIN;
602 if (cap_write_drain(iommu->cap))
603 val |= DMA_TLB_WRITE_DRAIN;
605 spin_lock_irqsave(&iommu->register_lock, flag);
606 /* Note: Only uses first TLB reg currently */
608 dmar_writeq(iommu->reg + tlb_offset, val_iva);
609 dmar_writeq(iommu->reg + tlb_offset + 8, val);
611 /* Make sure hardware complete it */
612 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
613 dmar_readq, (!(val & DMA_TLB_IVT)), val);
615 spin_unlock_irqrestore(&iommu->register_lock, flag);
617 /* check IOTLB invalidation granularity */
618 if (DMA_TLB_IAIG(val) == 0)
619 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
620 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
621 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
622 DMA_TLB_IIRG(type), DMA_TLB_IAIG(val));
623 /* flush context entry will implictly flush write buffer */
627 static int inline iommu_flush_iotlb_global(struct intel_iommu *iommu,
628 int non_present_entry_flush)
630 return __iommu_flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH,
631 non_present_entry_flush);
634 static int inline iommu_flush_iotlb_dsi(struct intel_iommu *iommu, u16 did,
635 int non_present_entry_flush)
637 return __iommu_flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH,
638 non_present_entry_flush);
641 static int iommu_get_alignment(u64 base, unsigned int size)
646 end = base + size - 1;
647 while (base != end) {
655 static int iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
656 u64 addr, unsigned int pages, int non_present_entry_flush)
660 BUG_ON(addr & (~PAGE_MASK_4K));
663 /* Fallback to domain selective flush if no PSI support */
664 if (!cap_pgsel_inv(iommu->cap))
665 return iommu_flush_iotlb_dsi(iommu, did,
666 non_present_entry_flush);
669 * PSI requires page size to be 2 ^ x, and the base address is naturally
670 * aligned to the size
672 align = iommu_get_alignment(addr >> PAGE_SHIFT_4K, pages);
673 /* Fallback to domain selective flush if size is too big */
674 if (align > cap_max_amask_val(iommu->cap))
675 return iommu_flush_iotlb_dsi(iommu, did,
676 non_present_entry_flush);
678 addr >>= PAGE_SHIFT_4K + align;
679 addr <<= PAGE_SHIFT_4K + align;
681 return __iommu_flush_iotlb(iommu, did, addr, align,
682 DMA_TLB_PSI_FLUSH, non_present_entry_flush);
685 static int iommu_enable_translation(struct intel_iommu *iommu)
690 spin_lock_irqsave(&iommu->register_lock, flags);
691 writel(iommu->gcmd|DMA_GCMD_TE, iommu->reg + DMAR_GCMD_REG);
693 /* Make sure hardware complete it */
694 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
695 readl, (sts & DMA_GSTS_TES), sts);
697 iommu->gcmd |= DMA_GCMD_TE;
698 spin_unlock_irqrestore(&iommu->register_lock, flags);
702 static int iommu_disable_translation(struct intel_iommu *iommu)
707 spin_lock_irqsave(&iommu->register_lock, flag);
708 iommu->gcmd &= ~DMA_GCMD_TE;
709 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
711 /* Make sure hardware complete it */
712 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
713 readl, (!(sts & DMA_GSTS_TES)), sts);
715 spin_unlock_irqrestore(&iommu->register_lock, flag);
719 static int iommu_init_domains(struct intel_iommu *iommu)
721 unsigned long ndomains;
722 unsigned long nlongs;
724 ndomains = cap_ndoms(iommu->cap);
725 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
726 nlongs = BITS_TO_LONGS(ndomains);
728 /* TBD: there might be 64K domains,
729 * consider other allocation for future chip
731 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
732 if (!iommu->domain_ids) {
733 printk(KERN_ERR "Allocating domain id array failed\n");
736 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
738 if (!iommu->domains) {
739 printk(KERN_ERR "Allocating domain array failed\n");
740 kfree(iommu->domain_ids);
745 * if Caching mode is set, then invalid translations are tagged
746 * with domainid 0. Hence we need to pre-allocate it.
748 if (cap_caching_mode(iommu->cap))
749 set_bit(0, iommu->domain_ids);
753 static struct intel_iommu *alloc_iommu(struct dmar_drhd_unit *drhd)
755 struct intel_iommu *iommu;
760 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
763 iommu->reg = ioremap(drhd->reg_base_addr, PAGE_SIZE_4K);
765 printk(KERN_ERR "IOMMU: can't map the region\n");
768 iommu->cap = dmar_readq(iommu->reg + DMAR_CAP_REG);
769 iommu->ecap = dmar_readq(iommu->reg + DMAR_ECAP_REG);
771 /* the registers might be more than one page */
772 map_size = max_t(int, ecap_max_iotlb_offset(iommu->ecap),
773 cap_max_fault_reg_offset(iommu->cap));
774 map_size = PAGE_ALIGN_4K(map_size);
775 if (map_size > PAGE_SIZE_4K) {
777 iommu->reg = ioremap(drhd->reg_base_addr, map_size);
779 printk(KERN_ERR "IOMMU: can't map the region\n");
784 ver = readl(iommu->reg + DMAR_VER_REG);
785 pr_debug("IOMMU %llx: ver %d:%d cap %llx ecap %llx\n",
786 drhd->reg_base_addr, DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver),
787 iommu->cap, iommu->ecap);
788 ret = iommu_init_domains(iommu);
791 spin_lock_init(&iommu->lock);
792 spin_lock_init(&iommu->register_lock);
804 static void domain_exit(struct dmar_domain *domain);
805 static void free_iommu(struct intel_iommu *iommu)
807 struct dmar_domain *domain;
813 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
814 for (; i < cap_ndoms(iommu->cap); ) {
815 domain = iommu->domains[i];
816 clear_bit(i, iommu->domain_ids);
818 i = find_next_bit(iommu->domain_ids,
819 cap_ndoms(iommu->cap), i+1);
822 if (iommu->gcmd & DMA_GCMD_TE)
823 iommu_disable_translation(iommu);
826 set_irq_data(iommu->irq, NULL);
827 /* This will mask the irq */
828 free_irq(iommu->irq, iommu);
829 destroy_irq(iommu->irq);
832 kfree(iommu->domains);
833 kfree(iommu->domain_ids);
835 /* free context mapping */
836 free_context_table(iommu);
843 static struct dmar_domain * iommu_alloc_domain(struct intel_iommu *iommu)
846 unsigned long ndomains;
847 struct dmar_domain *domain;
850 domain = alloc_domain_mem();
854 ndomains = cap_ndoms(iommu->cap);
856 spin_lock_irqsave(&iommu->lock, flags);
857 num = find_first_zero_bit(iommu->domain_ids, ndomains);
858 if (num >= ndomains) {
859 spin_unlock_irqrestore(&iommu->lock, flags);
860 free_domain_mem(domain);
861 printk(KERN_ERR "IOMMU: no free domain ids\n");
865 set_bit(num, iommu->domain_ids);
867 domain->iommu = iommu;
868 iommu->domains[num] = domain;
869 spin_unlock_irqrestore(&iommu->lock, flags);
874 static void iommu_free_domain(struct dmar_domain *domain)
878 spin_lock_irqsave(&domain->iommu->lock, flags);
879 clear_bit(domain->id, domain->iommu->domain_ids);
880 spin_unlock_irqrestore(&domain->iommu->lock, flags);
883 static struct iova_domain reserved_iova_list;
885 static void dmar_init_reserved_ranges(void)
887 struct pci_dev *pdev = NULL;
892 init_iova_domain(&reserved_iova_list);
894 /* IOAPIC ranges shouldn't be accessed by DMA */
895 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
896 IOVA_PFN(IOAPIC_RANGE_END));
898 printk(KERN_ERR "Reserve IOAPIC range failed\n");
900 /* Reserve all PCI MMIO to avoid peer-to-peer access */
901 for_each_pci_dev(pdev) {
904 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
905 r = &pdev->resource[i];
906 if (!r->flags || !(r->flags & IORESOURCE_MEM))
909 addr &= PAGE_MASK_4K;
910 size = r->end - addr;
911 size = PAGE_ALIGN_4K(size);
912 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
913 IOVA_PFN(size + addr) - 1);
915 printk(KERN_ERR "Reserve iova failed\n");
921 static void domain_reserve_special_ranges(struct dmar_domain *domain)
923 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
926 static inline int guestwidth_to_adjustwidth(int gaw)
929 int r = (gaw - 12) % 9;
940 static int domain_init(struct dmar_domain *domain, int guest_width)
942 struct intel_iommu *iommu;
943 int adjust_width, agaw;
946 init_iova_domain(&domain->iovad);
947 spin_lock_init(&domain->mapping_lock);
949 domain_reserve_special_ranges(domain);
952 iommu = domain->iommu;
953 if (guest_width > cap_mgaw(iommu->cap))
954 guest_width = cap_mgaw(iommu->cap);
955 domain->gaw = guest_width;
956 adjust_width = guestwidth_to_adjustwidth(guest_width);
957 agaw = width_to_agaw(adjust_width);
958 sagaw = cap_sagaw(iommu->cap);
959 if (!test_bit(agaw, &sagaw)) {
960 /* hardware doesn't support it, choose a bigger one */
961 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
962 agaw = find_next_bit(&sagaw, 5, agaw);
967 INIT_LIST_HEAD(&domain->devices);
969 /* always allocate the top pgd */
970 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
973 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE_4K);
977 static void domain_exit(struct dmar_domain *domain)
981 /* Domain 0 is reserved, so dont process it */
985 domain_remove_dev_info(domain);
987 put_iova_domain(&domain->iovad);
988 end = DOMAIN_MAX_ADDR(domain->gaw);
989 end = end & (~PAGE_MASK_4K);
992 dma_pte_clear_range(domain, 0, end);
994 /* free page tables */
995 dma_pte_free_pagetable(domain, 0, end);
997 iommu_free_domain(domain);
998 free_domain_mem(domain);
1001 static int domain_context_mapping_one(struct dmar_domain *domain,
1004 struct context_entry *context;
1005 struct intel_iommu *iommu = domain->iommu;
1006 unsigned long flags;
1008 pr_debug("Set context mapping for %02x:%02x.%d\n",
1009 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1010 BUG_ON(!domain->pgd);
1011 context = device_to_context_entry(iommu, bus, devfn);
1014 spin_lock_irqsave(&iommu->lock, flags);
1015 if (context_present(*context)) {
1016 spin_unlock_irqrestore(&iommu->lock, flags);
1020 context_set_domain_id(*context, domain->id);
1021 context_set_address_width(*context, domain->agaw);
1022 context_set_address_root(*context, virt_to_phys(domain->pgd));
1023 context_set_translation_type(*context, CONTEXT_TT_MULTI_LEVEL);
1024 context_set_fault_enable(*context);
1025 context_set_present(*context);
1026 __iommu_flush_cache(iommu, context, sizeof(*context));
1028 /* it's a non-present to present mapping */
1029 if (iommu_flush_context_device(iommu, domain->id,
1030 (((u16)bus) << 8) | devfn, DMA_CCMD_MASK_NOBIT, 1))
1031 iommu_flush_write_buffer(iommu);
1033 iommu_flush_iotlb_dsi(iommu, 0, 0);
1034 spin_unlock_irqrestore(&iommu->lock, flags);
1039 domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev)
1042 struct pci_dev *tmp, *parent;
1044 ret = domain_context_mapping_one(domain, pdev->bus->number,
1049 /* dependent device mapping */
1050 tmp = pci_find_upstream_pcie_bridge(pdev);
1053 /* Secondary interface's bus number and devfn 0 */
1054 parent = pdev->bus->self;
1055 while (parent != tmp) {
1056 ret = domain_context_mapping_one(domain, parent->bus->number,
1060 parent = parent->bus->self;
1062 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1063 return domain_context_mapping_one(domain,
1064 tmp->subordinate->number, 0);
1065 else /* this is a legacy PCI bridge */
1066 return domain_context_mapping_one(domain,
1067 tmp->bus->number, tmp->devfn);
1070 static int domain_context_mapped(struct dmar_domain *domain,
1071 struct pci_dev *pdev)
1074 struct pci_dev *tmp, *parent;
1076 ret = device_context_mapped(domain->iommu,
1077 pdev->bus->number, pdev->devfn);
1080 /* dependent device mapping */
1081 tmp = pci_find_upstream_pcie_bridge(pdev);
1084 /* Secondary interface's bus number and devfn 0 */
1085 parent = pdev->bus->self;
1086 while (parent != tmp) {
1087 ret = device_context_mapped(domain->iommu, parent->bus->number,
1091 parent = parent->bus->self;
1094 return device_context_mapped(domain->iommu,
1095 tmp->subordinate->number, 0);
1097 return device_context_mapped(domain->iommu,
1098 tmp->bus->number, tmp->devfn);
1102 domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1103 u64 hpa, size_t size, int prot)
1105 u64 start_pfn, end_pfn;
1106 struct dma_pte *pte;
1109 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1111 iova &= PAGE_MASK_4K;
1112 start_pfn = ((u64)hpa) >> PAGE_SHIFT_4K;
1113 end_pfn = (PAGE_ALIGN_4K(((u64)hpa) + size)) >> PAGE_SHIFT_4K;
1115 while (start_pfn < end_pfn) {
1116 pte = addr_to_dma_pte(domain, iova + PAGE_SIZE_4K * index);
1119 /* We don't need lock here, nobody else
1120 * touches the iova range
1122 BUG_ON(dma_pte_addr(*pte));
1123 dma_set_pte_addr(*pte, start_pfn << PAGE_SHIFT_4K);
1124 dma_set_pte_prot(*pte, prot);
1125 __iommu_flush_cache(domain->iommu, pte, sizeof(*pte));
1132 static void detach_domain_for_dev(struct dmar_domain *domain, u8 bus, u8 devfn)
1134 clear_context_table(domain->iommu, bus, devfn);
1135 iommu_flush_context_global(domain->iommu, 0);
1136 iommu_flush_iotlb_global(domain->iommu, 0);
1139 static void domain_remove_dev_info(struct dmar_domain *domain)
1141 struct device_domain_info *info;
1142 unsigned long flags;
1144 spin_lock_irqsave(&device_domain_lock, flags);
1145 while (!list_empty(&domain->devices)) {
1146 info = list_entry(domain->devices.next,
1147 struct device_domain_info, link);
1148 list_del(&info->link);
1149 list_del(&info->global);
1151 info->dev->sysdata = NULL;
1152 spin_unlock_irqrestore(&device_domain_lock, flags);
1154 detach_domain_for_dev(info->domain, info->bus, info->devfn);
1155 free_devinfo_mem(info);
1157 spin_lock_irqsave(&device_domain_lock, flags);
1159 spin_unlock_irqrestore(&device_domain_lock, flags);
1164 * Note: we use struct pci_dev->sysdata stores the info
1166 struct dmar_domain *
1167 find_domain(struct pci_dev *pdev)
1169 struct device_domain_info *info;
1171 /* No lock here, assumes no domain exit in normal case */
1172 info = pdev->sysdata;
1174 return info->domain;
1178 static int dmar_pci_device_match(struct pci_dev *devices[], int cnt,
1179 struct pci_dev *dev)
1184 for (index = 0; index < cnt; index ++)
1185 if (dev == devices[index])
1188 /* Check our parent */
1189 dev = dev->bus->self;
1195 static struct dmar_drhd_unit *
1196 dmar_find_matched_drhd_unit(struct pci_dev *dev)
1198 struct dmar_drhd_unit *drhd = NULL;
1200 list_for_each_entry(drhd, &dmar_drhd_units, list) {
1201 if (drhd->include_all || dmar_pci_device_match(drhd->devices,
1202 drhd->devices_cnt, dev))
1209 /* domain is initialized */
1210 static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1212 struct dmar_domain *domain, *found = NULL;
1213 struct intel_iommu *iommu;
1214 struct dmar_drhd_unit *drhd;
1215 struct device_domain_info *info, *tmp;
1216 struct pci_dev *dev_tmp;
1217 unsigned long flags;
1218 int bus = 0, devfn = 0;
1220 domain = find_domain(pdev);
1224 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1226 if (dev_tmp->is_pcie) {
1227 bus = dev_tmp->subordinate->number;
1230 bus = dev_tmp->bus->number;
1231 devfn = dev_tmp->devfn;
1233 spin_lock_irqsave(&device_domain_lock, flags);
1234 list_for_each_entry(info, &device_domain_list, global) {
1235 if (info->bus == bus && info->devfn == devfn) {
1236 found = info->domain;
1240 spin_unlock_irqrestore(&device_domain_lock, flags);
1241 /* pcie-pci bridge already has a domain, uses it */
1248 /* Allocate new domain for the device */
1249 drhd = dmar_find_matched_drhd_unit(pdev);
1251 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1255 iommu = drhd->iommu;
1257 domain = iommu_alloc_domain(iommu);
1261 if (domain_init(domain, gaw)) {
1262 domain_exit(domain);
1266 /* register pcie-to-pci device */
1268 info = alloc_devinfo_mem();
1270 domain_exit(domain);
1274 info->devfn = devfn;
1276 info->domain = domain;
1277 /* This domain is shared by devices under p2p bridge */
1278 domain->flags |= DOMAIN_FLAG_MULTIPLE_DEVICES;
1280 /* pcie-to-pci bridge already has a domain, uses it */
1282 spin_lock_irqsave(&device_domain_lock, flags);
1283 list_for_each_entry(tmp, &device_domain_list, global) {
1284 if (tmp->bus == bus && tmp->devfn == devfn) {
1285 found = tmp->domain;
1290 free_devinfo_mem(info);
1291 domain_exit(domain);
1294 list_add(&info->link, &domain->devices);
1295 list_add(&info->global, &device_domain_list);
1297 spin_unlock_irqrestore(&device_domain_lock, flags);
1301 info = alloc_devinfo_mem();
1304 info->bus = pdev->bus->number;
1305 info->devfn = pdev->devfn;
1307 info->domain = domain;
1308 spin_lock_irqsave(&device_domain_lock, flags);
1309 /* somebody is fast */
1310 found = find_domain(pdev);
1311 if (found != NULL) {
1312 spin_unlock_irqrestore(&device_domain_lock, flags);
1313 if (found != domain) {
1314 domain_exit(domain);
1317 free_devinfo_mem(info);
1320 list_add(&info->link, &domain->devices);
1321 list_add(&info->global, &device_domain_list);
1322 pdev->sysdata = info;
1323 spin_unlock_irqrestore(&device_domain_lock, flags);
1326 /* recheck it here, maybe others set it */
1327 return find_domain(pdev);
1330 static int iommu_prepare_identity_map(struct pci_dev *pdev, u64 start, u64 end)
1332 struct dmar_domain *domain;
1338 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1339 pci_name(pdev), start, end);
1340 /* page table init */
1341 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1345 /* The address might not be aligned */
1346 base = start & PAGE_MASK_4K;
1348 size = PAGE_ALIGN_4K(size);
1349 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1350 IOVA_PFN(base + size) - 1)) {
1351 printk(KERN_ERR "IOMMU: reserve iova failed\n");
1356 pr_debug("Mapping reserved region %lx@%llx for %s\n",
1357 size, base, pci_name(pdev));
1359 * RMRR range might have overlap with physical memory range,
1362 dma_pte_clear_range(domain, base, base + size);
1364 ret = domain_page_mapping(domain, base, base, size,
1365 DMA_PTE_READ|DMA_PTE_WRITE);
1369 /* context entry init */
1370 ret = domain_context_mapping(domain, pdev);
1374 domain_exit(domain);
1379 static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1380 struct pci_dev *pdev)
1382 if (pdev->sysdata == DUMMY_DEVICE_DOMAIN_INFO)
1384 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1385 rmrr->end_address + 1);
1388 int __init init_dmars(void)
1390 struct dmar_drhd_unit *drhd;
1391 struct dmar_rmrr_unit *rmrr;
1392 struct pci_dev *pdev;
1393 struct intel_iommu *iommu;
1399 * initialize and program root entry to not present
1402 for_each_drhd_unit(drhd) {
1405 iommu = alloc_iommu(drhd);
1413 * we could share the same root & context tables
1414 * amoung all IOMMU's. Need to Split it later.
1416 ret = iommu_alloc_root_entry(iommu);
1418 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
1425 * for each dev attached to rmrr
1427 * locate drhd for dev, alloc domain for dev
1428 * allocate free domain
1429 * allocate page table entries for rmrr
1430 * if context not allocated for bus
1431 * allocate and init context
1432 * set present in root table for this bus
1433 * init context with domain, translation etc
1437 for_each_rmrr_units(rmrr) {
1439 for (i = 0; i < rmrr->devices_cnt; i++) {
1440 pdev = rmrr->devices[i];
1441 /* some BIOS lists non-exist devices in DMAR table */
1444 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
1447 "IOMMU: mapping reserved region failed\n");
1454 * global invalidate context cache
1455 * global invalidate iotlb
1456 * enable translation
1458 for_each_drhd_unit(drhd) {
1461 iommu = drhd->iommu;
1462 sprintf (iommu->name, "dmar%d", unit++);
1464 iommu_flush_write_buffer(iommu);
1466 iommu_set_root_entry(iommu);
1468 iommu_flush_context_global(iommu, 0);
1469 iommu_flush_iotlb_global(iommu, 0);
1471 ret = iommu_enable_translation(iommu);
1478 for_each_drhd_unit(drhd) {
1481 iommu = drhd->iommu;
1487 static inline u64 aligned_size(u64 host_addr, size_t size)
1490 addr = (host_addr & (~PAGE_MASK_4K)) + size;
1491 return PAGE_ALIGN_4K(addr);
1495 iommu_alloc_iova(struct dmar_domain *domain, void *host_addr, size_t size,
1501 /* Make sure it's in range */
1502 if ((start > DOMAIN_MAX_ADDR(domain->gaw)) || end < start)
1505 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
1506 start_addr = PAGE_ALIGN_4K(start);
1507 size = aligned_size((u64)host_addr, size);
1508 if (!size || (start_addr + size > end))
1511 piova = alloc_iova(&domain->iovad,
1512 size >> PAGE_SHIFT_4K, IOVA_PFN(end));
1517 static dma_addr_t __intel_map_single(struct device *dev, void *addr,
1518 size_t size, int dir, u64 *flush_addr, unsigned int *flush_size)
1520 struct dmar_domain *domain;
1521 struct pci_dev *pdev = to_pci_dev(dev);
1524 struct iova *iova = NULL;
1527 addr = (void *)virt_to_phys(addr);
1529 domain = get_domain_for_dev(pdev,
1530 DEFAULT_DOMAIN_ADDRESS_WIDTH);
1533 "Allocating domain for %s failed", pci_name(pdev));
1537 start_addr = IOVA_START_ADDR;
1539 if (pdev->dma_mask <= DMA_32BIT_MASK) {
1540 iova = iommu_alloc_iova(domain, addr, size, start_addr,
1544 * First try to allocate an io virtual address in
1545 * DMA_32BIT_MASK and if that fails then try allocating
1548 iova = iommu_alloc_iova(domain, addr, size, start_addr,
1551 iova = iommu_alloc_iova(domain, addr, size, start_addr,
1556 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
1560 /* make sure context mapping is ok */
1561 if (unlikely(!domain_context_mapped(domain, pdev))) {
1562 ret = domain_context_mapping(domain, pdev);
1568 * Check if DMAR supports zero-length reads on write only
1571 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
1572 !cap_zlr(domain->iommu->cap))
1573 prot |= DMA_PTE_READ;
1574 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
1575 prot |= DMA_PTE_WRITE;
1577 * addr - (addr + size) might be partial page, we should map the whole
1578 * page. Note: if two part of one page are separately mapped, we
1579 * might have two guest_addr mapping to the same host addr, but this
1580 * is not a big problem
1582 ret = domain_page_mapping(domain, iova->pfn_lo << PAGE_SHIFT_4K,
1583 ((u64)addr) & PAGE_MASK_4K,
1584 (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT_4K, prot);
1588 pr_debug("Device %s request: %lx@%llx mapping: %lx@%llx, dir %d\n",
1589 pci_name(pdev), size, (u64)addr,
1590 (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT_4K,
1591 (u64)(iova->pfn_lo << PAGE_SHIFT_4K), dir);
1593 *flush_addr = iova->pfn_lo << PAGE_SHIFT_4K;
1594 *flush_size = (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT_4K;
1595 return (iova->pfn_lo << PAGE_SHIFT_4K) + ((u64)addr & (~PAGE_MASK_4K));
1597 __free_iova(&domain->iovad, iova);
1598 printk(KERN_ERR"Device %s request: %lx@%llx dir %d --- failed\n",
1599 pci_name(pdev), size, (u64)addr, dir);
1603 static dma_addr_t intel_map_single(struct device *hwdev, void *addr,
1604 size_t size, int dir)
1606 struct pci_dev *pdev = to_pci_dev(hwdev);
1608 struct dmar_domain *domain;
1610 unsigned int flush_size;
1612 BUG_ON(dir == DMA_NONE);
1613 if (pdev->sysdata == DUMMY_DEVICE_DOMAIN_INFO)
1614 return virt_to_bus(addr);
1616 ret = __intel_map_single(hwdev, addr, size,
1617 dir, &flush_addr, &flush_size);
1619 domain = find_domain(pdev);
1620 /* it's a non-present to present mapping */
1621 if (iommu_flush_iotlb_psi(domain->iommu, domain->id,
1622 flush_addr, flush_size >> PAGE_SHIFT_4K, 1))
1623 iommu_flush_write_buffer(domain->iommu);
1628 static void __intel_unmap_single(struct device *dev, dma_addr_t dev_addr,
1629 size_t size, int dir, u64 *flush_addr, unsigned int *flush_size)
1631 struct dmar_domain *domain;
1632 struct pci_dev *pdev = to_pci_dev(dev);
1635 domain = find_domain(pdev);
1638 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
1643 pr_debug("Device %s unmapping: %lx@%llx\n",
1645 (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT_4K,
1646 (u64)(iova->pfn_lo << PAGE_SHIFT_4K));
1648 *flush_addr = iova->pfn_lo << PAGE_SHIFT_4K;
1649 *flush_size = (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT_4K;
1650 /* clear the whole page, not just dev_addr - (dev_addr + size) */
1651 dma_pte_clear_range(domain, *flush_addr, *flush_addr + *flush_size);
1652 /* free page tables */
1653 dma_pte_free_pagetable(domain, *flush_addr, *flush_addr + *flush_size);
1655 __free_iova(&domain->iovad, iova);
1658 static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr,
1659 size_t size, int dir)
1661 struct pci_dev *pdev = to_pci_dev(dev);
1662 struct dmar_domain *domain;
1664 unsigned int flush_size;
1666 if (pdev->sysdata == DUMMY_DEVICE_DOMAIN_INFO)
1669 domain = find_domain(pdev);
1670 __intel_unmap_single(dev, dev_addr, size,
1671 dir, &flush_addr, &flush_size);
1672 if (flush_size == 0)
1674 if (iommu_flush_iotlb_psi(domain->iommu, domain->id, flush_addr,
1675 flush_size >> PAGE_SHIFT_4K, 0))
1676 iommu_flush_write_buffer(domain->iommu);
1679 static void * intel_alloc_coherent(struct device *hwdev, size_t size,
1680 dma_addr_t *dma_handle, gfp_t flags)
1685 size = PAGE_ALIGN_4K(size);
1686 order = get_order(size);
1687 flags &= ~(GFP_DMA | GFP_DMA32);
1689 vaddr = (void *)__get_free_pages(flags, order);
1692 memset(vaddr, 0, size);
1694 *dma_handle = intel_map_single(hwdev, vaddr, size, DMA_BIDIRECTIONAL);
1697 free_pages((unsigned long)vaddr, order);
1701 static void intel_free_coherent(struct device *hwdev, size_t size,
1702 void *vaddr, dma_addr_t dma_handle)
1706 size = PAGE_ALIGN_4K(size);
1707 order = get_order(size);
1709 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
1710 free_pages((unsigned long)vaddr, order);
1713 static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sg,
1714 int nelems, int dir)
1717 struct pci_dev *pdev = to_pci_dev(hwdev);
1718 struct dmar_domain *domain;
1720 unsigned int flush_size;
1722 if (pdev->sysdata == DUMMY_DEVICE_DOMAIN_INFO)
1725 domain = find_domain(pdev);
1726 for (i = 0; i < nelems; i++, sg++)
1727 __intel_unmap_single(hwdev, sg->dma_address,
1728 sg->dma_length, dir, &flush_addr, &flush_size);
1730 if (iommu_flush_iotlb_dsi(domain->iommu, domain->id, 0))
1731 iommu_flush_write_buffer(domain->iommu);
1734 #define SG_ENT_VIRT_ADDRESS(sg) (page_address((sg)->page) + (sg)->offset)
1735 static int intel_nontranslate_map_sg(struct device *hddev,
1736 struct scatterlist *sg, int nelems, int dir)
1740 for (i = 0; i < nelems; i++) {
1741 struct scatterlist *s = &sg[i];
1743 s->dma_address = virt_to_bus(SG_ENT_VIRT_ADDRESS(s));
1744 s->dma_length = s->length;
1749 static int intel_map_sg(struct device *hwdev, struct scatterlist *sg,
1750 int nelems, int dir)
1754 dma_addr_t dma_handle;
1755 struct pci_dev *pdev = to_pci_dev(hwdev);
1756 struct dmar_domain *domain;
1758 unsigned int flush_size;
1760 BUG_ON(dir == DMA_NONE);
1761 if (pdev->sysdata == DUMMY_DEVICE_DOMAIN_INFO)
1762 return intel_nontranslate_map_sg(hwdev, sg, nelems, dir);
1764 for (i = 0; i < nelems; i++, sg++) {
1765 addr = SG_ENT_VIRT_ADDRESS(sg);
1766 dma_handle = __intel_map_single(hwdev, addr,
1767 sg->length, dir, &flush_addr, &flush_size);
1769 intel_unmap_sg(hwdev, sg - i, i, dir);
1770 sg[0].dma_length = 0;
1773 sg->dma_address = dma_handle;
1774 sg->dma_length = sg->length;
1777 domain = find_domain(pdev);
1779 /* it's a non-present to present mapping */
1780 if (iommu_flush_iotlb_dsi(domain->iommu, domain->id, 1))
1781 iommu_flush_write_buffer(domain->iommu);
1785 static struct dma_mapping_ops intel_dma_ops = {
1786 .alloc_coherent = intel_alloc_coherent,
1787 .free_coherent = intel_free_coherent,
1788 .map_single = intel_map_single,
1789 .unmap_single = intel_unmap_single,
1790 .map_sg = intel_map_sg,
1791 .unmap_sg = intel_unmap_sg,
1794 static inline int iommu_domain_cache_init(void)
1798 iommu_domain_cache = kmem_cache_create("iommu_domain",
1799 sizeof(struct dmar_domain),
1804 if (!iommu_domain_cache) {
1805 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
1812 static inline int iommu_devinfo_cache_init(void)
1816 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
1817 sizeof(struct device_domain_info),
1822 if (!iommu_devinfo_cache) {
1823 printk(KERN_ERR "Couldn't create devinfo cache\n");
1830 static inline int iommu_iova_cache_init(void)
1834 iommu_iova_cache = kmem_cache_create("iommu_iova",
1835 sizeof(struct iova),
1840 if (!iommu_iova_cache) {
1841 printk(KERN_ERR "Couldn't create iova cache\n");
1848 static int __init iommu_init_mempool(void)
1851 ret = iommu_iova_cache_init();
1855 ret = iommu_domain_cache_init();
1859 ret = iommu_devinfo_cache_init();
1863 kmem_cache_destroy(iommu_domain_cache);
1865 kmem_cache_destroy(iommu_iova_cache);
1870 static void __init iommu_exit_mempool(void)
1872 kmem_cache_destroy(iommu_devinfo_cache);
1873 kmem_cache_destroy(iommu_domain_cache);
1874 kmem_cache_destroy(iommu_iova_cache);
1878 void __init detect_intel_iommu(void)
1880 if (swiotlb || no_iommu || iommu_detected || dmar_disabled)
1882 if (early_dmar_detect()) {
1887 static void __init init_no_remapping_devices(void)
1889 struct dmar_drhd_unit *drhd;
1891 for_each_drhd_unit(drhd) {
1892 if (!drhd->include_all) {
1894 for (i = 0; i < drhd->devices_cnt; i++)
1895 if (drhd->devices[i] != NULL)
1897 /* ignore DMAR unit if no pci devices exist */
1898 if (i == drhd->devices_cnt)
1906 for_each_drhd_unit(drhd) {
1908 if (drhd->ignored || drhd->include_all)
1911 for (i = 0; i < drhd->devices_cnt; i++)
1912 if (drhd->devices[i] &&
1913 !IS_GFX_DEVICE(drhd->devices[i]))
1916 if (i < drhd->devices_cnt)
1919 /* bypass IOMMU if it is just for gfx devices */
1921 for (i = 0; i < drhd->devices_cnt; i++) {
1922 if (!drhd->devices[i])
1924 drhd->devices[i]->sysdata = DUMMY_DEVICE_DOMAIN_INFO;
1929 int __init intel_iommu_init(void)
1933 if (no_iommu || swiotlb || dmar_disabled)
1936 if (dmar_table_init())
1939 iommu_init_mempool();
1940 dmar_init_reserved_ranges();
1942 init_no_remapping_devices();
1946 printk(KERN_ERR "IOMMU: dmar init failed\n");
1947 put_iova_domain(&reserved_iova_list);
1948 iommu_exit_mempool();
1952 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
1955 dma_ops = &intel_dma_ops;