Merge remote-tracking branch 'origin/dev/sumit-linux-3.10.96' into TOT-merge
[linux-3.10.git] / arch / arm / mm / dma-mapping.c
index cbdeaf4..ff01976 100644 (file)
 #include <linux/iommu.h>
 #include <linux/io.h>
 #include <linux/vmalloc.h>
+#include <linux/sizes.h>
+#include <linux/seq_file.h>
+#include <linux/debugfs.h>
 
 #include <asm/memory.h>
 #include <asm/highmem.h>
 #include <asm/cacheflush.h>
 #include <asm/tlbflush.h>
-#include <asm/sizes.h>
 #include <asm/mach/arch.h>
 #include <asm/dma-iommu.h>
 #include <asm/mach/map.h>
@@ -38,6 +40,9 @@
 
 #include "mm.h"
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/dmadebug.h>
+
 /*
  * The DMA API is built upon the notion of "buffer ownership".  A buffer
  * is either exclusively owned by the CPU (and therefore may be accessed
@@ -73,11 +78,18 @@ static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
             unsigned long offset, size_t size, enum dma_data_direction dir,
             struct dma_attrs *attrs)
 {
-       if (!arch_is_coherent())
+       if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
                __dma_page_cpu_to_dev(page, offset, size, dir);
        return pfn_to_dma(dev, page_to_pfn(page)) + offset;
 }
 
+static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
+            unsigned long offset, size_t size, enum dma_data_direction dir,
+            struct dma_attrs *attrs)
+{
+       return pfn_to_dma(dev, page_to_pfn(page)) + offset;
+}
+
 /**
  * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
@@ -96,7 +108,7 @@ static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
                size_t size, enum dma_data_direction dir,
                struct dma_attrs *attrs)
 {
-       if (!arch_is_coherent())
+       if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
                __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
                                      handle & ~PAGE_MASK, size, dir);
 }
@@ -106,8 +118,7 @@ static void arm_dma_sync_single_for_cpu(struct device *dev,
 {
        unsigned int offset = handle & (PAGE_SIZE - 1);
        struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
-       if (!arch_is_coherent())
-               __dma_page_dev_to_cpu(page, offset, size, dir);
+       __dma_page_dev_to_cpu(page, offset, size, dir);
 }
 
 static void arm_dma_sync_single_for_device(struct device *dev,
@@ -115,16 +126,14 @@ static void arm_dma_sync_single_for_device(struct device *dev,
 {
        unsigned int offset = handle & (PAGE_SIZE - 1);
        struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
-       if (!arch_is_coherent())
-               __dma_page_cpu_to_dev(page, offset, size, dir);
+       __dma_page_cpu_to_dev(page, offset, size, dir);
 }
 
-static int arm_dma_set_mask(struct device *dev, u64 dma_mask);
-
 struct dma_map_ops arm_dma_ops = {
        .alloc                  = arm_dma_alloc,
        .free                   = arm_dma_free,
        .mmap                   = arm_dma_mmap,
+       .get_sgtable            = arm_dma_get_sgtable,
        .map_page               = arm_dma_map_page,
        .unmap_page             = arm_dma_unmap_page,
        .map_sg                 = arm_dma_map_sg,
@@ -137,6 +146,22 @@ struct dma_map_ops arm_dma_ops = {
 };
 EXPORT_SYMBOL(arm_dma_ops);
 
+static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
+       dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs);
+static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
+                                 dma_addr_t handle, struct dma_attrs *attrs);
+
+struct dma_map_ops arm_coherent_dma_ops = {
+       .alloc                  = arm_coherent_dma_alloc,
+       .free                   = arm_coherent_dma_free,
+       .mmap                   = arm_dma_mmap,
+       .get_sgtable            = arm_dma_get_sgtable,
+       .map_page               = arm_coherent_dma_map_page,
+       .map_sg                 = arm_dma_map_sg,
+       .set_dma_mask           = arm_dma_set_mask,
+};
+EXPORT_SYMBOL(arm_coherent_dma_ops);
+
 static u64 get_coherent_dma_mask(struct device *dev)
 {
        u64 mask = (u64)arm_dma_limit;
@@ -166,13 +191,24 @@ static u64 get_coherent_dma_mask(struct device *dev)
 
 static void __dma_clear_buffer(struct page *page, size_t size)
 {
-       void *ptr;
        /*
         * Ensure that the allocated pages are zeroed, and that any data
         * lurking in the kernel direct-mapped region is invalidated.
         */
-       ptr = page_address(page);
-       if (ptr) {
+       if (PageHighMem(page)) {
+               phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
+               phys_addr_t end = base + size;
+               while (size > 0) {
+                       void *ptr = kmap_atomic(page);
+                       memset(ptr, 0, PAGE_SIZE);
+                       dmac_flush_range(ptr, ptr + PAGE_SIZE);
+                       kunmap_atomic(ptr);
+                       page++;
+                       size -= PAGE_SIZE;
+               }
+               outer_flush_range(base, end);
+       } else {
+               void *ptr = page_address(page);
                memset(ptr, 0, size);
                dmac_flush_range(ptr, ptr + size);
                outer_flush_range(__pa(ptr), __pa(ptr) + size);
@@ -223,7 +259,8 @@ static void __dma_free_buffer(struct page *page, size_t size)
 #endif
 
 static void *__alloc_from_contiguous(struct device *dev, size_t size,
-                                    pgprot_t prot, struct page **ret_page);
+                                    pgprot_t prot, struct page **ret_page,
+                                    const void *caller);
 
 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
                                 pgprot_t prot, struct page **ret_page,
@@ -266,17 +303,19 @@ static void __dma_free_remap(void *cpu_addr, size_t size)
        vunmap(cpu_addr);
 }
 
+#define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
+
 struct dma_pool {
        size_t size;
        spinlock_t lock;
        unsigned long *bitmap;
        unsigned long nr_pages;
        void *vaddr;
-       struct page *page;
+       struct page **pages;
 };
 
 static struct dma_pool atomic_pool = {
-       .size = SZ_256K,
+       .size = DEFAULT_DMA_COHERENT_POOL_SIZE,
 };
 
 static int __init early_coherent_pool(char *p)
@@ -286,6 +325,21 @@ static int __init early_coherent_pool(char *p)
 }
 early_param("coherent_pool", early_coherent_pool);
 
+void __init init_dma_coherent_pool_size(unsigned long size)
+{
+       /*
+        * Catch any attempt to set the pool size too late.
+        */
+       BUG_ON(atomic_pool.vaddr);
+
+       /*
+        * Set architecture specific coherent pool size only if
+        * it has not been changed by kernel command line parameter.
+        */
+       if (atomic_pool.size == DEFAULT_DMA_COHERENT_POOL_SIZE)
+               atomic_pool.size = size;
+}
+
 /*
  * Initialise the coherent pool for atomic allocations.
  */
@@ -293,9 +347,11 @@ static int __init atomic_pool_init(void)
 {
        struct dma_pool *pool = &atomic_pool;
        pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
+       gfp_t gfp = GFP_KERNEL | GFP_DMA;
        unsigned long nr_pages = pool->size >> PAGE_SHIFT;
        unsigned long *bitmap;
        struct page *page;
+       struct page **pages;
        void *ptr;
        int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long);
 
@@ -303,21 +359,34 @@ static int __init atomic_pool_init(void)
        if (!bitmap)
                goto no_bitmap;
 
+       pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
+       if (!pages)
+               goto no_pages;
+
        if (IS_ENABLED(CONFIG_CMA))
-               ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page);
+               ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page,
+                                             atomic_pool_init);
        else
-               ptr = __alloc_remap_buffer(NULL, pool->size, GFP_KERNEL, prot,
-                                          &page, NULL);
+               ptr = __alloc_remap_buffer(NULL, pool->size, gfp, prot, &page,
+                                          atomic_pool_init);
        if (ptr) {
+               int i;
+
+               for (i = 0; i < nr_pages; i++)
+                       pages[i] = page + i;
+
                spin_lock_init(&pool->lock);
                pool->vaddr = ptr;
-               pool->page = page;
+               pool->pages = pages;
                pool->bitmap = bitmap;
                pool->nr_pages = nr_pages;
                pr_info("DMA: preallocated %u KiB pool for atomic coherent allocations\n",
                       (unsigned)pool->size / 1024);
                return 0;
        }
+
+       kfree(pages);
+no_pages:
        kfree(bitmap);
 no_bitmap:
        pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
@@ -345,6 +414,9 @@ void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
        dma_mmu_remap_num++;
 }
 
+__init void iotable_init_va(struct map_desc *io_desc, int nr);
+__init void iotable_init_mapping(struct map_desc *io_desc, int nr);
+
 void __init dma_contiguous_remap(void)
 {
        int i;
@@ -357,42 +429,35 @@ void __init dma_contiguous_remap(void)
                if (end > arm_lowmem_limit)
                        end = arm_lowmem_limit;
                if (start >= end)
-                       return;
+                       continue;
 
-               map.pfn = __phys_to_pfn(start);
-               map.virtual = __phys_to_virt(start);
-               map.length = end - start;
-               map.type = MT_MEMORY_DMA_READY;
+               map.type = MT_MEMORY;
 
                /*
-                * Clear previous low-memory mapping
+                * Clear previous low-memory mapping to ensure that the
+                * TLB does not see any conflicting entries, then flush
+                * the TLB of the old entries before creating new mappings.
+                *
+                * This ensures that any speculatively loaded TLB entries
+                * (even though they may be rare) can not cause any problems,
+                * and ensures that this code is architecturally compliant.
                 */
                for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
                     addr += PMD_SIZE)
                        pmd_clear(pmd_off_k(addr));
 
-               iotable_init(&map, 1);
-       }
-}
-
-static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
-                           void *data)
-{
-       struct page *page = virt_to_page(addr);
-       pgprot_t prot = *(pgprot_t *)data;
-
-       set_pte_ext(pte, mk_pte(page, prot), 0);
-       return 0;
-}
-
-static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
-{
-       unsigned long start = (unsigned long) page_address(page);
-       unsigned end = start + size;
+               for (addr = start; addr < end; addr += PAGE_SIZE) {
+                       map.pfn = __phys_to_pfn(addr);
+                       map.virtual = __phys_to_virt(addr);
+                       map.length = PAGE_SIZE;
+                       iotable_init_mapping(&map, 1);
+               }
 
-       apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
-       dsb();
-       flush_tlb_kernel_range(start, end);
+               map.pfn = __phys_to_pfn(start);
+               map.virtual = __phys_to_virt(start);
+               map.length = end - start;
+               iotable_init_va(&map, 1);
+       }
 }
 
 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
@@ -422,7 +487,7 @@ static void *__alloc_from_pool(size_t size, struct page **ret_page)
        unsigned int pageno;
        unsigned long flags;
        void *ptr = NULL;
-       size_t align;
+       unsigned long align_mask;
 
        if (!pool->vaddr) {
                WARN(1, "coherent pool not initialised!\n");
@@ -434,35 +499,53 @@ static void *__alloc_from_pool(size_t size, struct page **ret_page)
         * small, so align them to their order in pages, minimum is a page
         * size. This helps reduce fragmentation of the DMA space.
         */
-       align = PAGE_SIZE << get_order(size);
+       align_mask = (1 << get_order(size)) - 1;
 
        spin_lock_irqsave(&pool->lock, flags);
        pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages,
-                                           0, count, (1 << align) - 1);
+                                           0, count, align_mask);
        if (pageno < pool->nr_pages) {
                bitmap_set(pool->bitmap, pageno, count);
                ptr = pool->vaddr + PAGE_SIZE * pageno;
-               *ret_page = pool->page + pageno;
+               *ret_page = pool->pages[pageno];
+       } else {
+               pr_err_once("ERROR: %u KiB atomic DMA coherent pool is too small!\n"
+                           "Please increase it with coherent_pool= kernel parameter!\n",
+                           (unsigned)pool->size / 1024);
        }
        spin_unlock_irqrestore(&pool->lock, flags);
 
        return ptr;
 }
 
+static bool __in_atomic_pool(void *start, size_t size)
+{
+       struct dma_pool *pool = &atomic_pool;
+       void *end = start + size;
+       void *pool_start = pool->vaddr;
+       void *pool_end = pool->vaddr + pool->size;
+
+       if (start < pool_start || start >= pool_end)
+               return false;
+
+       if (end <= pool_end)
+               return true;
+
+       WARN(1, "Wrong coherent size(%p-%p) from atomic pool(%p-%p)\n",
+            start, end - 1, pool_start, pool_end - 1);
+
+       return false;
+}
+
 static int __free_from_pool(void *start, size_t size)
 {
        struct dma_pool *pool = &atomic_pool;
        unsigned long pageno, count;
        unsigned long flags;
 
-       if (start < pool->vaddr || start > pool->vaddr + pool->size)
+       if (!__in_atomic_pool(start, size))
                return 0;
 
-       if (start + size > pool->vaddr + pool->size) {
-               WARN(1, "freeing wrong coherent size from pool\n");
-               return 0;
-       }
-
        pageno = (start - pool->vaddr) >> PAGE_SHIFT;
        count = size >> PAGE_SHIFT;
 
@@ -474,27 +557,36 @@ static int __free_from_pool(void *start, size_t size)
 }
 
 static void *__alloc_from_contiguous(struct device *dev, size_t size,
-                                    pgprot_t prot, struct page **ret_page)
+                                    pgprot_t prot, struct page **ret_page,
+                                    const void *caller)
 {
        unsigned long order = get_order(size);
        size_t count = size >> PAGE_SHIFT;
        struct page *page;
+       void *ptr;
 
        page = dma_alloc_from_contiguous(dev, count, order);
        if (!page)
                return NULL;
 
-       __dma_clear_buffer(page, size);
-       __dma_remap(page, size, prot);
-
+       if (PageHighMem(page)) {
+               ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
+               if (!ptr) {
+                       dma_release_from_contiguous(dev, page, count);
+                       return NULL;
+               }
+       } else {
+               ptr = page_address(page);
+       }
        *ret_page = page;
-       return page_address(page);
+       return ptr;
 }
 
 static void __free_from_contiguous(struct device *dev, struct page *page,
-                                  size_t size)
+                                  void *cpu_addr, size_t size)
 {
-       __dma_remap(page, size, pgprot_kernel);
+       if (PageHighMem(page))
+               __dma_free_remap(cpu_addr, size);
        dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
 }
 
@@ -515,9 +607,9 @@ static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
 #define __get_dma_pgprot(attrs, prot)  __pgprot(0)
 #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c)     NULL
 #define __alloc_from_pool(size, ret_page)                      NULL
-#define __alloc_from_contiguous(dev, size, prot, ret)          NULL
+#define __alloc_from_contiguous(dev, size, prot, ret, c)       NULL
 #define __free_from_pool(cpu_addr, size)                       0
-#define __free_from_contiguous(dev, page, size)                        do { } while (0)
+#define __free_from_contiguous(dev, page, cpu_addr, size)      do { } while (0)
 #define __dma_free_remap(cpu_addr, size)                       do { } while (0)
 
 #endif /* CONFIG_MMU */
@@ -537,16 +629,16 @@ static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
 
 
 static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
-                        gfp_t gfp, pgprot_t prot, const void *caller)
+                        gfp_t gfp, pgprot_t prot, bool is_coherent, const void *caller)
 {
        u64 mask = get_coherent_dma_mask(dev);
-       struct page *page;
+       struct page *page = NULL;
        void *addr;
 
 #ifdef CONFIG_DMA_API_DEBUG
        u64 limit = (mask + 1) & ~mask;
        if (limit && size >= limit) {
-               dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
+               dev_warn(dev, "coherent allocation too big (requested %#zx mask %#llx)\n",
                        size, mask);
                return NULL;
        }
@@ -570,14 +662,14 @@ static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
        *handle = DMA_ERROR_CODE;
        size = PAGE_ALIGN(size);
 
-       if (arch_is_coherent() || nommu())
+       if (is_coherent || nommu())
                addr = __alloc_simple_buffer(dev, size, gfp, &page);
-       else if (gfp & GFP_ATOMIC)
+       else if (!(gfp & __GFP_WAIT))
                addr = __alloc_from_pool(size, &page);
        else if (!IS_ENABLED(CONFIG_CMA))
                addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
        else
-               addr = __alloc_from_contiguous(dev, size, prot, &page);
+               addr = __alloc_from_contiguous(dev, size, prot, &page, caller);
 
        if (addr)
                *handle = pfn_to_dma(dev, page_to_pfn(page));
@@ -595,10 +687,25 @@ void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
        pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
        void *memory;
 
-       if (dma_alloc_from_coherent(dev, size, handle, &memory))
+       if (dma_alloc_from_coherent_attr(dev, size, handle,
+                       &memory, attrs))
                return memory;
 
-       return __dma_alloc(dev, size, handle, gfp, prot,
+       return __dma_alloc(dev, size, handle, gfp, prot, false,
+                          __builtin_return_address(0));
+}
+
+static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
+       dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
+{
+       pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
+       void *memory;
+
+       if (dma_alloc_from_coherent_attr(dev, size, handle,
+                       &memory, attrs))
+                       return memory;
+
+       return __dma_alloc(dev, size, handle, gfp, prot, true,
                           __builtin_return_address(0));
 }
 
@@ -635,72 +742,103 @@ int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
 /*
  * Free a buffer as defined by the above mapping.
  */
-void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
-                 dma_addr_t handle, struct dma_attrs *attrs)
+static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
+                          dma_addr_t handle, struct dma_attrs *attrs,
+                          bool is_coherent)
 {
        struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
 
-       if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
+       if (dma_release_from_coherent_attr(dev, size, cpu_addr, attrs))
                return;
 
        size = PAGE_ALIGN(size);
 
-       if (arch_is_coherent() || nommu()) {
+       if (is_coherent || nommu()) {
                __dma_free_buffer(page, size);
+       } else if (__free_from_pool(cpu_addr, size)) {
+               return;
        } else if (!IS_ENABLED(CONFIG_CMA)) {
                __dma_free_remap(cpu_addr, size);
                __dma_free_buffer(page, size);
        } else {
-               if (__free_from_pool(cpu_addr, size))
-                       return;
                /*
                 * Non-atomic allocations cannot be freed with IRQs disabled
                 */
                WARN_ON(irqs_disabled());
-               __free_from_contiguous(dev, page, size);
+               __free_from_contiguous(dev, page, cpu_addr, size);
        }
 }
 
+void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
+                 dma_addr_t handle, struct dma_attrs *attrs)
+{
+       __arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
+}
+
+static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
+                                 dma_addr_t handle, struct dma_attrs *attrs)
+{
+       __arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
+}
+
+int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
+                void *cpu_addr, dma_addr_t handle, size_t size,
+                struct dma_attrs *attrs)
+{
+       struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
+       int ret;
+
+       ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
+       if (unlikely(ret))
+               return ret;
+
+       sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
+       return 0;
+}
+
 static void dma_cache_maint_page(struct page *page, unsigned long offset,
        size_t size, enum dma_data_direction dir,
        void (*op)(const void *, size_t, int))
 {
+       unsigned long pfn;
+       size_t left = size;
+
+       pfn = page_to_pfn(page) + offset / PAGE_SIZE;
+       offset %= PAGE_SIZE;
+
        /*
         * A single sg entry may refer to multiple physically contiguous
         * pages.  But we still need to process highmem pages individually.
         * If highmem is not configured then the bulk of this loop gets
         * optimized out.
         */
-       size_t left = size;
        do {
                size_t len = left;
                void *vaddr;
 
+               page = pfn_to_page(pfn);
+
                if (PageHighMem(page)) {
-                       if (len + offset > PAGE_SIZE) {
-                               if (offset >= PAGE_SIZE) {
-                                       page += offset / PAGE_SIZE;
-                                       offset %= PAGE_SIZE;
-                               }
+                       if (len + offset > PAGE_SIZE)
                                len = PAGE_SIZE - offset;
-                       }
-                       vaddr = kmap_high_get(page);
-                       if (vaddr) {
-                               vaddr += offset;
-                               op(vaddr, len, dir);
-                               kunmap_high(page);
-                       } else if (cache_is_vipt()) {
-                               /* unmapped pages might still be cached */
+
+                       if (cache_is_vipt_nonaliasing()) {
                                vaddr = kmap_atomic(page);
                                op(vaddr + offset, len, dir);
                                kunmap_atomic(vaddr);
+                       } else {
+                               vaddr = kmap_high_get(page);
+                               if (vaddr) {
+                                       op(vaddr + offset, len, dir);
+                                       kunmap_high(page);
+                               }
                        }
                } else {
                        vaddr = page_address(page) + offset;
                        op(vaddr, len, dir);
                }
                offset = 0;
-               page++;
+               pfn++;
                left -= len;
        } while (left);
 }
@@ -860,7 +998,7 @@ int dma_supported(struct device *dev, u64 mask)
 }
 EXPORT_SYMBOL(dma_supported);
 
-static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
+int arm_dma_set_mask(struct device *dev, u64 dma_mask)
 {
        if (!dev->dma_mask || !dma_supported(dev, dma_mask))
                return -EIO;
@@ -870,30 +1008,325 @@ static int arm_dma_set_mask(struct device *dev, u64 dma_mask)
        return 0;
 }
 
+#if defined(CONFIG_ARM_DMA_USE_IOMMU)
+
+static LIST_HEAD(iommu_mapping_list);
+static DEFINE_SPINLOCK(iommu_mapping_list_lock);
+
+#if defined(CONFIG_DEBUG_FS)
+static dma_addr_t bit_to_addr(size_t pos, dma_addr_t base, size_t order)
+{
+       return base + pos * (1 << (PAGE_SHIFT + order));
+}
+
+static void seq_print_dma_areas(struct seq_file *s, void *bitmap,
+                               dma_addr_t base, size_t bits, size_t order)
+{
+       /* one bit = one (page + order) sized block */
+       size_t pos = find_first_bit(bitmap, bits), end;
+
+       for (; pos < bits; pos = find_next_bit(bitmap, bits, end + 1)) {
+               dma_addr_t start_addr, end_addr;
+
+               end = find_next_zero_bit(bitmap, bits, pos);
+               start_addr = bit_to_addr(pos, base, order);
+               end_addr = bit_to_addr(end, base, order) - 1;
+               seq_printf(s, "    %pa-%pa pages=%zu\n",
+                          &start_addr, &end_addr, (end - pos) << order);
+       }
+}
+
+static void seq_print_mapping(struct seq_file *s,
+                             struct dma_iommu_mapping *mapping)
+{
+       seq_printf(s, "  memory map: base=%pa size=%lld order=%d domain=%p\n",
+                  &mapping->base, (u64)(mapping->end - mapping->base),
+                  mapping->order, mapping->domain);
+
+       seq_print_dma_areas(s, mapping->bitmap, mapping->base, mapping->bits,
+                           mapping->order);
+}
+
+static void debug_dma_seq_print_mappings(struct seq_file *s)
+{
+       struct dma_iommu_mapping *mapping;
+       int i = 0;
+
+       list_for_each_entry(mapping, &iommu_mapping_list, list) {
+               seq_printf(s, "Map %d (%p):\n", i, mapping);
+               seq_print_mapping(s, mapping);
+               i++;
+       }
+}
+
+static int dump_iommu_mappings(struct seq_file *s, void *data)
+{
+       debug_dma_seq_print_mappings(s);
+       return 0;
+}
+
+static int dump_iommu_mappings_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, dump_iommu_mappings, NULL);
+}
+
+static const struct file_operations dump_iommu_mappings_fops = {
+       .open           = dump_iommu_mappings_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+};
+
+#endif /* CONFIG_DEBUG_FS */
+
+void dma_debugfs_platform_info(struct dentry *dent)
+{
+       debugfs_create_file("dump_mappings", S_IRUGO, dent, NULL,
+                           &dump_iommu_mappings_fops);
+}
+
+#else /* !CONFIG_ARM_DMA_USE_IOMMU */
+static inline void dma_debugfs_platform_info(struct dentry *dent)
+{
+}
+#endif /* !CONFIG_ARM_DMA_USE_IOMMU */
+
+#if defined(CONFIG_DMA_API_DEBUG)
+static inline void dma_debug_platform(void)
+{
+}
+#else /* !CONFIG_DMA_API_DEBUG */
+static void dma_debug_platform(void)
+{
+       struct dentry *dent;
+
+       dent = debugfs_create_dir("dma-api", NULL);
+       if (dent)
+               dma_debugfs_platform_info(dent);
+}
+#endif /* !CONFIG_DMA_API_DEBUG */
+
 #define PREALLOC_DMA_DEBUG_ENTRIES     4096
 
 static int __init dma_debug_do_init(void)
 {
        dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
+       dma_debug_platform();
        return 0;
 }
 fs_initcall(dma_debug_do_init);
 
+char *__weak debug_dma_platformdata(struct device *dev)
+{
+       /* empty string by default */
+       static char buf[1];
+
+       return buf;
+}
+
 #ifdef CONFIG_ARM_DMA_USE_IOMMU
 
 /* IOMMU */
 
+static unsigned int prefetch_page_count = 1;
+static unsigned int gap_page_count = 1;
+
+#define PF_PAGES_SIZE (prefetch_page_count << PAGE_SHIFT)
+#define PG_PAGES (prefetch_page_count + gap_page_count)
+
+static struct page *iova_gap_pages;
+static phys_addr_t iova_gap_phys;
+
+static int __init iova_gap_pages_init(void)
+{
+       unsigned long order = get_order(PF_PAGES_SIZE);
+
+       iova_gap_pages = alloc_pages(GFP_KERNEL, order);
+       if (WARN_ON(!iova_gap_pages)) {
+               prefetch_page_count = 0;
+               return 0;
+       }
+
+       __dma_clear_buffer(iova_gap_pages, PAGE_SIZE << order);
+       iova_gap_phys = page_to_phys(iova_gap_pages);
+       return 0;
+}
+core_initcall(iova_gap_pages_init);
+
+static void iommu_mapping_list_add(struct dma_iommu_mapping *mapping)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&iommu_mapping_list_lock, flags);
+       list_add_tail(&mapping->list, &iommu_mapping_list);
+       spin_unlock_irqrestore(&iommu_mapping_list_lock, flags);
+}
+
+static void iommu_mapping_list_del(struct dma_iommu_mapping *mapping)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&iommu_mapping_list_lock, flags);
+       list_del(&mapping->list);
+       spin_unlock_irqrestore(&iommu_mapping_list_lock, flags);
+}
+
+static inline int iommu_get_num_pf_pages(struct dma_iommu_mapping *mapping,
+                                        struct dma_attrs *attrs)
+{
+       /* XXX: give priority to DMA_ATTR_SKIP_IOVA_GAP */
+       if (dma_get_attr(DMA_ATTR_SKIP_IOVA_GAP, attrs))
+                       return 0;
+
+       /* XXX: currently we support only 1 prefetch page */
+       WARN_ON(mapping->num_pf_page > prefetch_page_count);
+
+       return mapping->num_pf_page;
+}
+
+static inline int iommu_gap_pg_count(struct dma_iommu_mapping *mapping,
+                                    struct dma_attrs *attrs)
+{
+       if (dma_get_attr(DMA_ATTR_SKIP_IOVA_GAP, attrs))
+               return 0;
+
+       return mapping->gap_page ? gap_page_count : 0;
+}
+
+static int pg_iommu_map(struct dma_iommu_mapping *mapping, unsigned long iova,
+                       phys_addr_t phys, size_t len, int prot)
+{
+       int err;
+       struct iommu_domain *domain = mapping->domain;
+       struct dma_attrs *attrs = (struct dma_attrs *)prot;
+       bool need_prefetch_page = !!iommu_get_num_pf_pages(mapping, attrs);
+
+       if (need_prefetch_page) {
+               err = iommu_map(domain, iova + len, iova_gap_phys,
+                               PF_PAGES_SIZE, prot);
+               if (err)
+                       return err;
+       }
+
+       err = iommu_map(domain, iova, phys, len, prot);
+       if (err && need_prefetch_page)
+               iommu_unmap(domain, iova + len, PF_PAGES_SIZE);
+
+       return err;
+}
+
+static size_t pg_iommu_unmap(struct dma_iommu_mapping *mapping,
+                            unsigned long iova, size_t len, int prot)
+{
+       struct dma_attrs *attrs = (struct dma_attrs *)prot;
+       struct iommu_domain *domain = mapping->domain;
+       bool need_prefetch_page = !!iommu_get_num_pf_pages(mapping, attrs);
+
+       if (need_prefetch_page) {
+               phys_addr_t phys_addr;
+
+               phys_addr = iommu_iova_to_phys(domain, iova + len);
+               BUG_ON(phys_addr != iova_gap_phys);
+               iommu_unmap(domain, iova + len, PF_PAGES_SIZE);
+       }
+
+       return iommu_unmap(domain, iova, len);
+}
+
+static int pg_iommu_map_sg(struct dma_iommu_mapping *mapping,
+                          unsigned long iova, struct scatterlist *sgl,
+                          int nents, int prot)
+{
+       int err;
+       struct dma_attrs *attrs = (struct dma_attrs *)prot;
+       struct iommu_domain *domain = mapping->domain;
+       bool need_prefetch_page = !!iommu_get_num_pf_pages(mapping, attrs);
+
+       if (need_prefetch_page) {
+               err = iommu_map(domain, iova + (nents << PAGE_SHIFT),
+                               iova_gap_phys, PF_PAGES_SIZE, prot);
+               if (err)
+                       return err;
+       }
+
+       err = iommu_map_sg(domain, iova, sgl, nents, prot);
+       if (err && need_prefetch_page)
+               iommu_unmap(domain, iova + (nents << PAGE_SHIFT), PF_PAGES_SIZE);
+
+       return err;
+}
+
+static size_t arm_iommu_iova_get_free_total(struct device *dev)
+{
+       struct dma_iommu_mapping *mapping = dev->archdata.mapping;
+       unsigned long flags;
+       size_t size = 0;
+       unsigned long start = 0;
+
+       BUG_ON(!dev);
+       BUG_ON(!mapping);
+
+       spin_lock_irqsave(&mapping->lock, flags);
+       while (1) {
+               unsigned long end;
+
+               start = bitmap_find_next_zero_area(mapping->bitmap,
+                                                  mapping->bits, start, 1, 0);
+               if (start > mapping->bits)
+                       break;
+
+               end = find_next_bit(mapping->bitmap, mapping->bits, start);
+               size += end - start;
+               start = end;
+       }
+       spin_unlock_irqrestore(&mapping->lock, flags);
+       return size << (mapping->order + PAGE_SHIFT);
+}
+
+static size_t arm_iommu_iova_get_free_max(struct device *dev)
+{
+       struct dma_iommu_mapping *mapping = dev->archdata.mapping;
+       unsigned long flags;
+       size_t max_free = 0;
+       unsigned long start = 0;
+
+       spin_lock_irqsave(&mapping->lock, flags);
+       while (1) {
+               unsigned long end;
+
+               start = bitmap_find_next_zero_area(mapping->bitmap,
+                                                  mapping->bits, start, 1, 0);
+               if (start > mapping->bits)
+                       break;
+
+               end = find_next_bit(mapping->bitmap, mapping->bits, start);
+               max_free = max_t(size_t, max_free, end - start);
+               start = end;
+       }
+       spin_unlock_irqrestore(&mapping->lock, flags);
+       return max_free << (mapping->order + PAGE_SHIFT);
+}
+
 static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
-                                     size_t size)
+                                     size_t size, struct dma_attrs *attrs)
 {
        unsigned int order = get_order(size);
        unsigned int align = 0;
        unsigned int count, start;
        unsigned long flags;
 
+       if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
+               order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
+
+       if (mapping->alignment && order > get_order(mapping->alignment))
+               order = get_order(mapping->alignment);
+
        count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
                 (1 << mapping->order) - 1) >> mapping->order;
 
+       count += iommu_get_num_pf_pages(mapping, attrs);
+       count += iommu_gap_pg_count(mapping, attrs);
+
        if (order > mapping->order)
                align = (1 << (order - mapping->order)) - 1;
 
@@ -911,8 +1344,67 @@ static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
        return mapping->base + (start << (mapping->order + PAGE_SHIFT));
 }
 
+static dma_addr_t __alloc_iova_at(struct dma_iommu_mapping *mapping,
+                                 dma_addr_t *iova, size_t size,
+                                 struct dma_attrs *attrs)
+{
+       unsigned int count, start, orig;
+       unsigned long flags;
+       size_t bytes;
+
+       count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
+                (1 << mapping->order) - 1) >> mapping->order;
+
+       count += iommu_get_num_pf_pages(mapping, attrs);
+       count += iommu_gap_pg_count(mapping, attrs);
+
+       bytes = count << (mapping->order + PAGE_SHIFT);
+
+       spin_lock_irqsave(&mapping->lock, flags);
+
+       if ((*iova < mapping->base) || (bytes > mapping->end - *iova)) {
+               *iova = -ENXIO;
+               goto err_out;
+       }
+
+       orig = (*iova - mapping->base) >> (mapping->order + PAGE_SHIFT);
+       start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits,
+                                          orig, count, 0);
+
+       if ((start > mapping->bits) || (orig != start)) {
+               *iova = -EINVAL;
+               goto err_out;
+       }
+
+       bitmap_set(mapping->bitmap, start, count);
+       spin_unlock_irqrestore(&mapping->lock, flags);
+
+       return mapping->base + (start << (mapping->order + PAGE_SHIFT));
+
+err_out:
+       spin_unlock_irqrestore(&mapping->lock, flags);
+       return DMA_ERROR_CODE;
+}
+
+static dma_addr_t arm_iommu_iova_alloc_at(struct device *dev, dma_addr_t *iova,
+                                         size_t size, struct dma_attrs *attrs)
+{
+       struct dma_iommu_mapping *mapping = dev->archdata.mapping;
+
+       return __alloc_iova_at(mapping, iova, size, attrs);
+}
+
+static dma_addr_t arm_iommu_iova_alloc(struct device *dev, size_t size,
+                                      struct dma_attrs *attrs)
+{
+       struct dma_iommu_mapping *mapping = dev->archdata.mapping;
+
+       return __alloc_iova(mapping, size, attrs);
+}
+
 static inline void __free_iova(struct dma_iommu_mapping *mapping,
-                              dma_addr_t addr, size_t size)
+                              dma_addr_t addr, size_t size,
+                              struct dma_attrs *attrs)
 {
        unsigned int start = (addr - mapping->base) >>
                             (mapping->order + PAGE_SHIFT);
@@ -920,12 +1412,24 @@ static inline void __free_iova(struct dma_iommu_mapping *mapping,
                              (1 << mapping->order) - 1) >> mapping->order;
        unsigned long flags;
 
+       count += iommu_get_num_pf_pages(mapping, attrs);
+       count += iommu_gap_pg_count(mapping, attrs);
+
        spin_lock_irqsave(&mapping->lock, flags);
        bitmap_clear(mapping->bitmap, start, count);
        spin_unlock_irqrestore(&mapping->lock, flags);
 }
 
-static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
+static void arm_iommu_iova_free(struct device *dev, dma_addr_t addr,
+                               size_t size, struct dma_attrs *attrs)
+{
+       struct dma_iommu_mapping *mapping = dev->archdata.mapping;
+
+       __free_iova(mapping, addr, size, attrs);
+}
+
+static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
+                                         gfp_t gfp, struct dma_attrs *attrs)
 {
        struct page **pages;
        int count = size >> PAGE_SHIFT;
@@ -933,26 +1437,53 @@ static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, gfp_t
        int i = 0;
 
        if (array_size <= PAGE_SIZE)
-               pages = kzalloc(array_size, gfp);
+               pages = kzalloc(array_size,
+                               gfp & ~(__GFP_HIGHMEM | __GFP_DMA32));
        else
                pages = vzalloc(array_size);
        if (!pages)
                return NULL;
 
+       if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
+       {
+               unsigned long order = get_order(size);
+               struct page *page;
+
+               page = dma_alloc_from_contiguous(dev, count, order);
+               if (!page)
+                       goto error;
+
+               __dma_clear_buffer(page, size);
+
+               for (i = 0; i < count; i++)
+                       pages[i] = page + i;
+
+               return pages;
+       }
+
+       /*
+        * IOMMU can map any pages, so himem can also be used here
+        * unless some DMA'able area is explicitly required.
+        */
+       if (!(gfp & GFP_DMA) && !(gfp & GFP_DMA32))
+               gfp |= __GFP_HIGHMEM;
+
+       gfp |= __GFP_NOWARN;
        while (count) {
                int j, order = __fls(count);
 
-               pages[i] = alloc_pages(gfp | __GFP_NOWARN, order);
+               pages[i] = alloc_pages(gfp, order);
                while (!pages[i] && order)
-                       pages[i] = alloc_pages(gfp | __GFP_NOWARN, --order);
+                       pages[i] = alloc_pages(gfp, --order);
                if (!pages[i])
                        goto error;
 
-               if (order)
+               if (order) {
                        split_page(pages[i], order);
-               j = 1 << order;
-               while (--j)
-                       pages[i + j] = pages[i] + j;
+                       j = 1 << order;
+                       while (--j)
+                               pages[i + j] = pages[i] + j;
+               }
 
                __dma_clear_buffer(pages[i], PAGE_SIZE << order);
                i += 1 << order;
@@ -971,14 +1502,21 @@ error:
        return NULL;
 }
 
-static int __iommu_free_buffer(struct device *dev, struct page **pages, size_t size)
+static int __iommu_free_buffer(struct device *dev, struct page **pages,
+                              size_t size, struct dma_attrs *attrs)
 {
        int count = size >> PAGE_SHIFT;
        int array_size = count * sizeof(struct page *);
        int i;
-       for (i = 0; i < count; i++)
-               if (pages[i])
-                       __free_pages(pages[i], 0);
+
+       if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
+               dma_release_from_contiguous(dev, pages[0], count);
+       } else {
+               for (i = 0; i < count; i++)
+                       if (pages[i])
+                               __free_pages(pages[i], 0);
+       }
+
        if (array_size <= PAGE_SIZE)
                kfree(pages);
        else
@@ -1023,14 +1561,20 @@ err:
  * Create a mapping in device IO address space for specified pages
  */
 static dma_addr_t
-__iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
+____iommu_create_mapping(struct device *dev, dma_addr_t *req,
+                        struct page **pages, size_t size,
+                        struct dma_attrs *attrs)
 {
        struct dma_iommu_mapping *mapping = dev->archdata.mapping;
        unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
        dma_addr_t dma_addr, iova;
        int i, ret = DMA_ERROR_CODE;
 
-       dma_addr = __alloc_iova(mapping, size);
+       if (req)
+               dma_addr = __alloc_iova_at(mapping, req, size, attrs);
+       else
+               dma_addr = __alloc_iova(mapping, size, attrs);
+
        if (dma_addr == DMA_ERROR_CODE)
                return dma_addr;
 
@@ -1045,20 +1589,36 @@ __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
                                break;
 
                len = (j - i) << PAGE_SHIFT;
-               ret = iommu_map(mapping->domain, iova, phys, len, 0);
+               ret = iommu_map(mapping->domain, iova, phys, len, (int)attrs);
                if (ret < 0)
                        goto fail;
                iova += len;
                i = j;
        }
+
+       if (iommu_get_num_pf_pages(mapping, attrs)) {
+               int err = iommu_map(mapping->domain, iova, iova_gap_phys,
+                                   PF_PAGES_SIZE, (int)attrs);
+               if (err)
+                       goto fail;
+       }
+
        return dma_addr;
 fail:
-       iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
-       __free_iova(mapping, dma_addr, size);
+       iommu_unmap(mapping->domain, dma_addr, iova - dma_addr);
+       __free_iova(mapping, dma_addr, size, attrs);
        return DMA_ERROR_CODE;
 }
 
-static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
+static dma_addr_t
+__iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
+                      struct dma_attrs *attrs)
+{
+       return ____iommu_create_mapping(dev, NULL, pages, size, attrs);
+}
+
+static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova,
+                                 size_t size, struct dma_attrs *attrs)
 {
        struct dma_iommu_mapping *mapping = dev->archdata.mapping;
 
@@ -1069,21 +1629,67 @@ static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t si
        size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
        iova &= PAGE_MASK;
 
-       iommu_unmap(mapping->domain, iova, size);
-       __free_iova(mapping, iova, size);
+       pg_iommu_unmap(mapping, iova, size, (int)attrs);
+       __free_iova(mapping, iova, size, attrs);
        return 0;
 }
 
-static struct page **__iommu_get_pages(void *cpu_addr)
+static struct page **__atomic_get_pages(void *addr)
+{
+       struct dma_pool *pool = &atomic_pool;
+       struct page **pages = pool->pages;
+       int offs = (addr - pool->vaddr) >> PAGE_SHIFT;
+
+       return pages + offs;
+}
+
+static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
 {
        struct vm_struct *area;
 
+       if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
+               return __atomic_get_pages(cpu_addr);
+
+       if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
+               return cpu_addr;
+
        area = find_vm_area(cpu_addr);
        if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
                return area->pages;
        return NULL;
 }
 
+static void *__iommu_alloc_atomic(struct device *dev, size_t size,
+                                 dma_addr_t *handle, struct dma_attrs *attrs)
+{
+       struct page *page;
+       void *addr;
+
+       addr = __alloc_from_pool(size, &page);
+       if (!addr)
+               return NULL;
+
+       *handle = __iommu_create_mapping(dev, &page, size, attrs);
+       if (*handle == DMA_ERROR_CODE)
+               goto err_mapping;
+
+       dev_dbg(dev, "%s() %pa(%x)\n", __func__, handle, size);
+       return addr;
+
+err_mapping:
+       __free_from_pool(addr, size);
+       return NULL;
+}
+
+static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
+                               dma_addr_t handle, size_t size,
+                               struct dma_attrs *attrs)
+{
+       __iommu_remove_mapping(dev, handle, size, attrs);
+       __free_from_pool(cpu_addr, size);
+       dev_dbg(dev, "%s() %pa(%x)\n", __func__, &handle, size);
+}
+
 static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
            dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
 {
@@ -1091,28 +1697,49 @@ static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
        struct page **pages;
        void *addr = NULL;
 
-       *handle = DMA_ERROR_CODE;
+       /* Following is a work-around (a.k.a. hack) to prevent pages
+        * with __GFP_COMP being passed to split_page() which cannot
+        * handle them.  The real problem is that this flag probably
+        * should be 0 on ARM as it is not supported on this
+        * platform--see CONFIG_HUGETLB_PAGE. */
+       gfp &= ~(__GFP_COMP);
+
        size = PAGE_ALIGN(size);
 
-       pages = __iommu_alloc_buffer(dev, size, gfp);
+       if (!(gfp & __GFP_WAIT)) {
+               addr = __iommu_alloc_atomic(dev, size, handle, attrs);
+               trace_dmadebug_alloc_attrs(dev, *handle, size, NULL, addr);
+               return addr;
+       }
+
+       pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
        if (!pages)
                return NULL;
 
-       *handle = __iommu_create_mapping(dev, pages, size);
+       if (*handle == DMA_ERROR_CODE)
+               *handle = __iommu_create_mapping(dev, pages, size, attrs);
+       else
+               *handle = ____iommu_create_mapping(dev, handle, pages, size,
+                                                  attrs);
+
        if (*handle == DMA_ERROR_CODE)
                goto err_buffer;
 
+       if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
+               return pages;
+
        addr = __iommu_alloc_remap(pages, size, gfp, prot,
                                   __builtin_return_address(0));
        if (!addr)
                goto err_mapping;
 
+       trace_dmadebug_alloc_attrs(dev, *handle, size, pages, addr);
        return addr;
 
 err_mapping:
-       __iommu_remove_mapping(dev, *handle, size);
+       __iommu_remove_mapping(dev, *handle, size, attrs);
 err_buffer:
-       __iommu_free_buffer(dev, pages, size);
+       __iommu_free_buffer(dev, pages, size, attrs);
        return NULL;
 }
 
@@ -1122,13 +1749,20 @@ static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
 {
        unsigned long uaddr = vma->vm_start;
        unsigned long usize = vma->vm_end - vma->vm_start;
-       struct page **pages = __iommu_get_pages(cpu_addr);
+       struct page **pages = __iommu_get_pages(cpu_addr, attrs);
+       unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
+       unsigned long off = vma->vm_pgoff;
 
        vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
 
        if (!pages)
                return -ENXIO;
 
+       if (off >= nr_pages || (usize >> PAGE_SHIFT) > nr_pages - off)
+               return -ENXIO;
+
+       pages += off;
+
        do {
                int ret = vm_insert_page(vma, uaddr, *pages++);
                if (ret) {
@@ -1149,7 +1783,7 @@ static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
 void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
                          dma_addr_t handle, struct dma_attrs *attrs)
 {
-       struct page **pages = __iommu_get_pages(cpu_addr);
+       struct page **pages = __iommu_get_pages(cpu_addr, attrs);
        size = PAGE_ALIGN(size);
 
        if (!pages) {
@@ -1157,11 +1791,34 @@ void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
                return;
        }
 
-       unmap_kernel_range((unsigned long)cpu_addr, size);
-       vunmap(cpu_addr);
+       if (__in_atomic_pool(cpu_addr, size)) {
+               trace_dmadebug_free_attrs(dev, handle, size, NULL, cpu_addr);
+               __iommu_free_atomic(dev, cpu_addr, handle, size, attrs);
+               return;
+       }
+
+       if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
+               unmap_kernel_range((unsigned long)cpu_addr, size);
+               vunmap(cpu_addr);
+       }
+
+       trace_dmadebug_free_attrs(dev, handle, size, pages, cpu_addr);
+       __iommu_remove_mapping(dev, handle, size, attrs);
+       __iommu_free_buffer(dev, pages, size, attrs);
+}
 
-       __iommu_remove_mapping(dev, handle, size);
-       __iommu_free_buffer(dev, pages, size);
+static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
+                                void *cpu_addr, dma_addr_t dma_addr,
+                                size_t size, struct dma_attrs *attrs)
+{
+       unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
+       struct page **pages = __iommu_get_pages(cpu_addr, attrs);
+
+       if (!pages)
+               return -ENXIO;
+
+       return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
+                                        GFP_KERNEL);
 }
 
 /*
@@ -1169,7 +1826,8 @@ void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
  */
 static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
                          size_t size, dma_addr_t *handle,
-                         enum dma_data_direction dir)
+                         enum dma_data_direction dir, struct dma_attrs *attrs,
+                         bool is_coherent)
 {
        struct dma_iommu_mapping *mapping = dev->archdata.mapping;
        dma_addr_t iova, iova_base;
@@ -1180,46 +1838,39 @@ static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
        size = PAGE_ALIGN(size);
        *handle = DMA_ERROR_CODE;
 
-       iova_base = iova = __alloc_iova(mapping, size);
+       iova_base = iova = __alloc_iova(mapping, size, attrs);
        if (iova == DMA_ERROR_CODE)
                return -ENOMEM;
 
+       if (is_coherent || dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
+               goto skip_cmaint;
+
        for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
-               phys_addr_t phys = page_to_phys(sg_page(s));
                unsigned int len = PAGE_ALIGN(s->offset + s->length);
 
-               if (!arch_is_coherent())
-                       __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
+               __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
 
-               ret = iommu_map(mapping->domain, iova, phys, len, 0);
-               if (ret < 0)
-                       goto fail;
                count += len >> PAGE_SHIFT;
                iova += len;
        }
+
+skip_cmaint:
+       count = size >> PAGE_SHIFT;
+       ret = pg_iommu_map_sg(mapping, iova_base, sg, count, (int)attrs);
+       if (WARN_ON(ret < 0))
+               goto fail;
+
        *handle = iova_base;
 
        return 0;
 fail:
-       iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
-       __free_iova(mapping, iova_base, size);
+       __free_iova(mapping, iova_base, size, attrs);
        return ret;
 }
 
-/**
- * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
- * @dev: valid struct device pointer
- * @sg: list of buffers
- * @nents: number of buffers to map
- * @dir: DMA transfer direction
- *
- * Map a set of buffers described by scatterlist in streaming mode for DMA.
- * The scatter gather list elements are merged together (if possible) and
- * tagged with the appropriate dma address and length. They are obtained via
- * sg_dma_{address,length}.
- */
-int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
-                    enum dma_data_direction dir, struct dma_attrs *attrs)
+static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
+                    enum dma_data_direction dir, struct dma_attrs *attrs,
+                    bool is_coherent)
 {
        struct scatterlist *s = sg, *dma = sg, *start = sg;
        int i, count = 0;
@@ -1235,7 +1886,7 @@ int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
 
                if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
                        if (__map_sg_chunk(dev, start, size, &dma->dma_address,
-                           dir) < 0)
+                           dir, attrs, is_coherent) < 0)
                                goto bad_mapping;
 
                        dma->dma_address += offset;
@@ -1248,32 +1899,63 @@ int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
                }
                size += s->length;
        }
-       if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir) < 0)
+       if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
+               is_coherent) < 0)
                goto bad_mapping;
 
        dma->dma_address += offset;
        dma->dma_length = size - offset;
 
+       trace_dmadebug_map_sg(dev, dma->dma_address, dma->dma_length,
+                             sg_page(sg));
        return count+1;
 
 bad_mapping:
        for_each_sg(sg, s, count, i)
-               __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
+               __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s),
+                       attrs);
        return 0;
 }
 
 /**
- * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
+ * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
  * @dev: valid struct device pointer
  * @sg: list of buffers
- * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
- * @dir: DMA transfer direction (same as was passed to dma_map_sg)
+ * @nents: number of buffers to map
+ * @dir: DMA transfer direction
  *
- * Unmap a set of streaming mode DMA translations.  Again, CPU access
- * rules concerning calls here are the same as for dma_unmap_single().
+ * Map a set of i/o coherent buffers described by scatterlist in streaming
+ * mode for DMA. The scatter gather list elements are merged together (if
+ * possible) and tagged with the appropriate dma address and length. They are
+ * obtained via sg_dma_{address,length}.
  */
-void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
-                       enum dma_data_direction dir, struct dma_attrs *attrs)
+int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
+               int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
+{
+       return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
+}
+
+/**
+ * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
+ * @dev: valid struct device pointer
+ * @sg: list of buffers
+ * @nents: number of buffers to map
+ * @dir: DMA transfer direction
+ *
+ * Map a set of buffers described by scatterlist in streaming mode for DMA.
+ * The scatter gather list elements are merged together (if possible) and
+ * tagged with the appropriate dma address and length. They are obtained via
+ * sg_dma_{address,length}.
+ */
+int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
+               int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
+{
+       return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
+}
+
+static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
+               int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
+               bool is_coherent)
 {
        struct scatterlist *s;
        int i;
@@ -1281,11 +1963,47 @@ void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
        for_each_sg(sg, s, nents, i) {
                if (sg_dma_len(s))
                        __iommu_remove_mapping(dev, sg_dma_address(s),
-                                              sg_dma_len(s));
-               if (!arch_is_coherent())
+                                              sg_dma_len(s), attrs);
+               if (!is_coherent &&
+                   !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
                        __dma_page_dev_to_cpu(sg_page(s), s->offset,
                                              s->length, dir);
        }
+
+       trace_dmadebug_unmap_sg(dev, sg_dma_address(sg), sg_dma_len(sg),
+                               sg_page(sg));
+}
+
+/**
+ * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
+ * @dev: valid struct device pointer
+ * @sg: list of buffers
+ * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
+ * @dir: DMA transfer direction (same as was passed to dma_map_sg)
+ *
+ * Unmap a set of streaming mode DMA translations.  Again, CPU access
+ * rules concerning calls here are the same as for dma_unmap_single().
+ */
+void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
+               int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
+{
+       __iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
+}
+
+/**
+ * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
+ * @dev: valid struct device pointer
+ * @sg: list of buffers
+ * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
+ * @dir: DMA transfer direction (same as was passed to dma_map_sg)
+ *
+ * Unmap a set of streaming mode DMA translations.  Again, CPU access
+ * rules concerning calls here are the same as for dma_unmap_single().
+ */
+void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
+                       enum dma_data_direction dir, struct dma_attrs *attrs)
+{
+       __iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
 }
 
 /**
@@ -1302,8 +2020,7 @@ void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
        int i;
 
        for_each_sg(sg, s, nents, i)
-               if (!arch_is_coherent())
-                       __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
+               __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
 
 }
 
@@ -1321,22 +2038,21 @@ void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
        int i;
 
        for_each_sg(sg, s, nents, i)
-               if (!arch_is_coherent())
-                       __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
+               __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
 }
 
 
 /**
- * arm_iommu_map_page
+ * arm_coherent_iommu_map_page
  * @dev: valid struct device pointer
  * @page: page that buffer resides in
  * @offset: offset into page for start of buffer
  * @size: size of buffer to map
  * @dir: DMA transfer direction
  *
- * IOMMU aware version of arm_dma_map_page()
+ * Coherent IOMMU aware version of arm_dma_map_page()
  */
-static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
+static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
             unsigned long offset, size_t size, enum dma_data_direction dir,
             struct dma_attrs *attrs)
 {
@@ -1344,24 +2060,90 @@ static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
        dma_addr_t dma_addr;
        int ret, len = PAGE_ALIGN(size + offset);
 
-       if (!arch_is_coherent())
-               __dma_page_cpu_to_dev(page, offset, size, dir);
-
-       dma_addr = __alloc_iova(mapping, len);
+       dma_addr = __alloc_iova(mapping, len, attrs);
        if (dma_addr == DMA_ERROR_CODE)
                return dma_addr;
 
-       ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, 0);
+       ret = pg_iommu_map(mapping, dma_addr,
+                          page_to_phys(page), len, (int)attrs);
        if (ret < 0)
                goto fail;
 
+       trace_dmadebug_map_page(dev, dma_addr + offset, size, page);
        return dma_addr + offset;
 fail:
-       __free_iova(mapping, dma_addr, len);
+       __free_iova(mapping, dma_addr, len, attrs);
        return DMA_ERROR_CODE;
 }
 
 /**
+ * arm_iommu_map_page
+ * @dev: valid struct device pointer
+ * @page: page that buffer resides in
+ * @offset: offset into page for start of buffer
+ * @size: size of buffer to map
+ * @dir: DMA transfer direction
+ *
+ * IOMMU aware version of arm_dma_map_page()
+ */
+static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
+            unsigned long offset, size_t size, enum dma_data_direction dir,
+            struct dma_attrs *attrs)
+{
+       if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
+               __dma_page_cpu_to_dev(page, offset, size, dir);
+
+       return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
+}
+
+static dma_addr_t arm_iommu_map_page_at(struct device *dev, struct page *page,
+                dma_addr_t dma_addr, unsigned long offset, size_t size,
+                enum dma_data_direction dir, struct dma_attrs *attrs)
+{
+       struct dma_iommu_mapping *mapping = dev->archdata.mapping;
+       int ret, len = PAGE_ALIGN(size + offset);
+
+       if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
+               __dma_page_cpu_to_dev(page, offset, size, dir);
+
+       ret = pg_iommu_map(mapping, dma_addr,
+                          page_to_phys(page), len, (int)attrs);
+       if (ret < 0)
+               return DMA_ERROR_CODE;
+
+       trace_dmadebug_map_page(dev, dma_addr, size, page);
+       return dma_addr + offset;
+}
+
+/**
+ * arm_coherent_iommu_unmap_page
+ * @dev: valid struct device pointer
+ * @handle: DMA address of buffer
+ * @size: size of buffer (same as passed to dma_map_page)
+ * @dir: DMA transfer direction (same as passed to dma_map_page)
+ *
+ * Coherent IOMMU aware version of arm_dma_unmap_page()
+ */
+static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
+               size_t size, enum dma_data_direction dir,
+               struct dma_attrs *attrs)
+{
+       struct dma_iommu_mapping *mapping = dev->archdata.mapping;
+       dma_addr_t iova = handle & PAGE_MASK;
+       int offset = handle & ~PAGE_MASK;
+       int len = PAGE_ALIGN(size + offset);
+
+       if (!iova)
+               return;
+
+       trace_dmadebug_unmap_page(dev, handle, size,
+                 phys_to_page(iommu_iova_to_phys(mapping->domain, handle)));
+       pg_iommu_unmap(mapping, iova, len, (int)attrs);
+       if (!dma_get_attr(DMA_ATTR_SKIP_FREE_IOVA, attrs))
+               __free_iova(mapping, iova, len, attrs);
+}
+
+/**
  * arm_iommu_unmap_page
  * @dev: valid struct device pointer
  * @handle: DMA address of buffer
@@ -1383,11 +2165,14 @@ static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
        if (!iova)
                return;
 
-       if (!arch_is_coherent())
+       if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
                __dma_page_dev_to_cpu(page, offset, size, dir);
 
-       iommu_unmap(mapping->domain, iova, len);
-       __free_iova(mapping, iova, len);
+       trace_dmadebug_unmap_page(dev, handle, size,
+                 phys_to_page(iommu_iova_to_phys(mapping->domain, handle)));
+       pg_iommu_unmap(mapping, iova, len, (int)attrs);
+       if (!dma_get_attr(DMA_ATTR_SKIP_FREE_IOVA, attrs))
+               __free_iova(mapping, iova, len, attrs);
 }
 
 static void arm_iommu_sync_single_for_cpu(struct device *dev,
@@ -1401,8 +2186,10 @@ static void arm_iommu_sync_single_for_cpu(struct device *dev,
        if (!iova)
                return;
 
-       if (!arch_is_coherent())
-               __dma_page_dev_to_cpu(page, offset, size, dir);
+       if (WARN_ON(!pfn_valid(page_to_pfn(page))))
+               return;
+
+       __dma_page_dev_to_cpu(page, offset, size, dir);
 }
 
 static void arm_iommu_sync_single_for_device(struct device *dev,
@@ -1416,15 +2203,27 @@ static void arm_iommu_sync_single_for_device(struct device *dev,
        if (!iova)
                return;
 
+       if (WARN_ON(!pfn_valid(page_to_pfn(page))))
+               return;
+
        __dma_page_cpu_to_dev(page, offset, size, dir);
 }
 
+static phys_addr_t arm_iommu_iova_to_phys(struct device *dev, dma_addr_t iova)
+{
+       struct dma_iommu_mapping *mapping = dev->archdata.mapping;
+
+       return iommu_iova_to_phys(mapping->domain, iova);
+}
+
 struct dma_map_ops iommu_ops = {
        .alloc          = arm_iommu_alloc_attrs,
        .free           = arm_iommu_free_attrs,
        .mmap           = arm_iommu_mmap_attrs,
+       .get_sgtable    = arm_iommu_get_sgtable,
 
        .map_page               = arm_iommu_map_page,
+       .map_page_at            = arm_iommu_map_page_at,
        .unmap_page             = arm_iommu_unmap_page,
        .sync_single_for_cpu    = arm_iommu_sync_single_for_cpu,
        .sync_single_for_device = arm_iommu_sync_single_for_device,
@@ -1433,8 +2232,158 @@ struct dma_map_ops iommu_ops = {
        .unmap_sg               = arm_iommu_unmap_sg,
        .sync_sg_for_cpu        = arm_iommu_sync_sg_for_cpu,
        .sync_sg_for_device     = arm_iommu_sync_sg_for_device,
+
+       .set_dma_mask           = arm_dma_set_mask,
+
+       .iova_alloc             = arm_iommu_iova_alloc,
+       .iova_alloc_at          = arm_iommu_iova_alloc_at,
+       .iova_free              = arm_iommu_iova_free,
+       .iova_get_free_total    = arm_iommu_iova_get_free_total,
+       .iova_get_free_max      = arm_iommu_iova_get_free_max,
+
+       .iova_to_phys           = arm_iommu_iova_to_phys,
+};
+
+struct dma_map_ops iommu_coherent_ops = {
+       .alloc          = arm_iommu_alloc_attrs,
+       .free           = arm_iommu_free_attrs,
+       .mmap           = arm_iommu_mmap_attrs,
+       .get_sgtable    = arm_iommu_get_sgtable,
+
+       .map_page       = arm_coherent_iommu_map_page,
+       .unmap_page     = arm_coherent_iommu_unmap_page,
+
+       .map_sg         = arm_coherent_iommu_map_sg,
+       .unmap_sg       = arm_coherent_iommu_unmap_sg,
+
+       .set_dma_mask   = arm_dma_set_mask,
 };
 
+bool device_is_iommuable(struct device *dev)
+{
+       return (dev->archdata.dma_ops == &iommu_ops) ||
+               (dev->archdata.dma_ops == &iommu_coherent_ops);
+}
+
+static inline void __dummy_common(void)
+{ WARN(1, "DMA API should be called after ->probe() is done.\n"); }
+
+static void *__dummy_alloc_attrs(struct device *dev, size_t size,
+                                dma_addr_t *dma_handle, gfp_t gfp,
+                                struct dma_attrs *attrs)
+{ __dummy_common(); return NULL; }
+
+static void __dummy_free_attrs(struct device *dev, size_t size,
+                              void *vaddr, dma_addr_t dma_handle,
+                              struct dma_attrs *attrs)
+{ __dummy_common(); }
+
+static int __dummy_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
+                             void *cpu_addr, dma_addr_t dma_addr, size_t size,
+                             struct dma_attrs *attrs)
+{ __dummy_common(); return -ENXIO; }
+
+static int __dummy_get_sgtable(struct device *dev, struct sg_table *sgt,
+                              void *cpu_addr, dma_addr_t dma_addr,
+                              size_t size, struct dma_attrs *attrs)
+{ __dummy_common(); return -ENXIO; }
+
+static dma_addr_t __dummy_map_page(struct device *dev, struct page *page,
+                                  unsigned long offset, size_t size,
+                                  enum dma_data_direction dir,
+                                  struct dma_attrs *attrs)
+{ __dummy_common(); return DMA_ERROR_CODE; }
+
+static dma_addr_t __dummy_map_page_at(struct device *dev, struct page *page,
+                                     dma_addr_t dma_handle,
+                                     unsigned long offset, size_t size,
+                                     enum dma_data_direction dir,
+                                     struct dma_attrs *attrs)
+{ __dummy_common(); return DMA_ERROR_CODE; }
+
+static void __dummy_unmap_page(struct device *dev, dma_addr_t dma_handle,
+                              size_t size, enum dma_data_direction dir,
+                              struct dma_attrs *attrs)
+{ __dummy_common(); }
+
+static int __dummy_map_sg(struct device *dev, struct scatterlist *sg,
+                         int nents, enum dma_data_direction dir,
+                         struct dma_attrs *attrs)
+{ __dummy_common(); return 0; }
+
+static void __dummy_unmap_sg(struct device *dev,
+                            struct scatterlist *sg, int nents,
+                            enum dma_data_direction dir,
+                            struct dma_attrs *attrs)
+{ __dummy_common(); }
+
+static void __dummy_sync_single_for_cpu(struct device *dev,
+                                       dma_addr_t dma_handle, size_t size,
+                                       enum dma_data_direction dir)
+{ __dummy_common(); }
+
+static void __dummy_sync_single_for_device(struct device *dev,
+                                          dma_addr_t dma_handle, size_t size,
+                                          enum dma_data_direction dir)
+{ __dummy_common(); }
+
+static void __dummy_sync_sg_for_cpu(struct device *dev,
+                                   struct scatterlist *sg, int nents,
+                                   enum dma_data_direction dir)
+{ __dummy_common(); }
+
+static void __dummy_sync_sg_for_device(struct device *dev,
+                                      struct scatterlist *sg, int nents,
+                                      enum dma_data_direction dir)
+{ __dummy_common(); }
+
+static dma_addr_t __dummy_iova_alloc(struct device *dev, size_t size,
+                                    struct dma_attrs *attrs)
+{ __dummy_common(); return DMA_ERROR_CODE; }
+
+static dma_addr_t __dummy_iova_alloc_at(struct device *dev, dma_addr_t *dma_addr,
+                                       size_t size, struct dma_attrs *attrs)
+{ __dummy_common(); return DMA_ERROR_CODE; }
+
+static void __dummy_iova_free(struct device *dev, dma_addr_t addr, size_t size,
+                             struct dma_attrs *attrs)
+{ __dummy_common(); }
+
+static size_t __dummy_iova_get_free_total(struct device *dev)
+{ __dummy_common(); return 0; }
+
+static size_t __dummy_iova_get_free_max(struct device *dev)
+{ __dummy_common(); return 0; }
+
+static struct dma_map_ops __dummy_ops = {
+       .alloc          = __dummy_alloc_attrs,
+       .free           = __dummy_free_attrs,
+       .mmap           = __dummy_mmap_attrs,
+       .get_sgtable    = __dummy_get_sgtable,
+
+       .map_page               = __dummy_map_page,
+       .map_page_at            = __dummy_map_page_at,
+       .unmap_page             = __dummy_unmap_page,
+       .sync_single_for_cpu    = __dummy_sync_single_for_cpu,
+       .sync_single_for_device = __dummy_sync_single_for_device,
+
+       .map_sg                 = __dummy_map_sg,
+       .unmap_sg               = __dummy_unmap_sg,
+       .sync_sg_for_cpu        = __dummy_sync_sg_for_cpu,
+       .sync_sg_for_device     = __dummy_sync_sg_for_device,
+
+       .iova_alloc             = __dummy_iova_alloc,
+       .iova_alloc_at          = __dummy_iova_alloc_at,
+       .iova_free              = __dummy_iova_free,
+       .iova_get_free_total    = __dummy_iova_get_free_total,
+       .iova_get_free_max      = __dummy_iova_get_free_max,
+};
+
+void set_dummy_dma_ops(struct device *dev)
+{
+       set_dma_ops(dev, &__dummy_ops);
+}
+
 /**
  * arm_iommu_create_mapping
  * @bus: pointer to the bus holding the client device (for IOMMU calls)
@@ -1469,7 +2418,9 @@ arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
        if (!mapping->bitmap)
                goto err2;
 
+       base = round_up(base, 1 << (order + PAGE_SHIFT));
        mapping->base = base;
+       mapping->end = base + size;
        mapping->bits = BITS_PER_BYTE * bitmap_size;
        mapping->order = order;
        spin_lock_init(&mapping->lock);
@@ -1479,6 +2430,8 @@ arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
                goto err3;
 
        kref_init(&mapping->kref);
+
+       iommu_mapping_list_add(mapping);
        return mapping;
 err3:
        kfree(mapping->bitmap);
@@ -1487,12 +2440,14 @@ err2:
 err:
        return ERR_PTR(err);
 }
+EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
 
 static void release_iommu_mapping(struct kref *kref)
 {
        struct dma_iommu_mapping *mapping =
                container_of(kref, struct dma_iommu_mapping, kref);
 
+       iommu_mapping_list_del(mapping);
        iommu_domain_free(mapping->domain);
        kfree(mapping->bitmap);
        kfree(mapping);
@@ -1503,6 +2458,7 @@ void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
        if (mapping)
                kref_put(&mapping->kref, release_iommu_mapping);
 }
+EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
 
 /**
  * arm_iommu_attach_device
@@ -1519,17 +2475,53 @@ int arm_iommu_attach_device(struct device *dev,
                            struct dma_iommu_mapping *mapping)
 {
        int err;
+       struct dma_map_ops *org_ops;
+       struct dma_iommu_mapping *org_map;
+
+       org_ops = get_dma_ops(dev);
+       set_dma_ops(dev, &iommu_ops);
+
+       org_map = dev->archdata.mapping;
+       dev->archdata.mapping = mapping;
 
        err = iommu_attach_device(mapping->domain, dev);
-       if (err)
+       if (err) {
+               set_dma_ops(dev, org_ops);
+               dev->archdata.mapping = org_map;
                return err;
+       }
 
        kref_get(&mapping->kref);
-       dev->archdata.mapping = mapping;
-       set_dma_ops(dev, &iommu_ops);
 
-       pr_info("Attached IOMMU controller to %s device.\n", dev_name(dev));
+       pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
        return 0;
 }
+EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
+
+/**
+ * arm_iommu_detach_device
+ * @dev: valid struct device pointer
+ *
+ * Detaches the provided device from a previously attached map.
+ * This voids the dma operations (dma_map_ops pointer)
+ */
+void arm_iommu_detach_device(struct device *dev)
+{
+       struct dma_iommu_mapping *mapping;
+
+       mapping = to_dma_iommu_mapping(dev);
+       if (!mapping) {
+               dev_warn(dev, "Not attached\n");
+               return;
+       }
+
+       iommu_detach_device(mapping->domain, dev);
+       kref_put(&mapping->kref, release_iommu_mapping);
+       mapping = NULL;
+       set_dma_ops(dev, NULL);
+
+       pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
+}
+EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
 
 #endif