powerpc/mm: e300c2/c3/c4 TLB errata workaround
[safe/jmp/linux-2.6] / arch / powerpc / kernel / iommu.c
index 4eba60a..fd51578 100644 (file)
@@ -23,7 +23,6 @@
  */
 
 
-#include <linux/config.h>
 #include <linux/init.h>
 #include <linux/types.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/string.h>
 #include <linux/dma-mapping.h>
-#include <linux/init.h>
 #include <linux/bitops.h>
+#include <linux/iommu-helper.h>
+#include <linux/crash_dump.h>
 #include <asm/io.h>
 #include <asm/prom.h>
 #include <asm/iommu.h>
 #include <asm/pci-bridge.h>
 #include <asm/machdep.h>
+#include <asm/kdump.h>
 
 #define DBG(...)
 
@@ -47,6 +48,20 @@ static int novmerge = 0;
 static int novmerge = 1;
 #endif
 
+static int protect4gb = 1;
+
+static void __iommu_free(struct iommu_table *, dma_addr_t, unsigned int);
+
+static int __init setup_protect4gb(char *str)
+{
+       if (strcmp(str, "on") == 0)
+               protect4gb = 1;
+       else if (strcmp(str, "off") == 0)
+               protect4gb = 0;
+
+       return 1;
+}
+
 static int __init setup_iommu(char *str)
 {
        if (!strcmp(str, "novmerge"))
@@ -56,26 +71,29 @@ static int __init setup_iommu(char *str)
        return 1;
 }
 
+__setup("protect4gb=", setup_protect4gb);
 __setup("iommu=", setup_iommu);
 
-static unsigned long iommu_range_alloc(struct iommu_table *tbl,
+static unsigned long iommu_range_alloc(struct device *dev,
+                                      struct iommu_table *tbl,
                                        unsigned long npages,
                                        unsigned long *handle,
                                        unsigned long mask,
                                        unsigned int align_order)
 { 
-       unsigned long n, end, i, start;
+       unsigned long n, end, start;
        unsigned long limit;
        int largealloc = npages > 15;
        int pass = 0;
        unsigned long align_mask;
+       unsigned long boundary_size;
 
        align_mask = 0xffffffffffffffffl >> (64 - align_order);
 
        /* This allocator was derived from x86_64's bit string search */
 
        /* Sanity check */
-       if (unlikely(npages) == 0) {
+       if (unlikely(npages == 0)) {
                if (printk_ratelimit())
                        WARN_ON(1);
                return DMA_ERROR_CODE;
@@ -113,14 +131,17 @@ static unsigned long iommu_range_alloc(struct iommu_table *tbl,
                        start &= mask;
        }
 
-       n = find_next_zero_bit(tbl->it_map, limit, start);
-
-       /* Align allocation */
-       n = (n + align_mask) & ~align_mask;
-
-       end = n + npages;
+       if (dev)
+               boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
+                                     1 << IOMMU_PAGE_SHIFT);
+       else
+               boundary_size = ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT);
+       /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
 
-       if (unlikely(end >= limit)) {
+       n = iommu_area_alloc(tbl->it_map, limit, start, npages,
+                            tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
+                            align_mask);
+       if (n == -1) {
                if (likely(pass < 2)) {
                        /* First failure, just rescan the half of the table.
                         * Second failure, rescan the other half of the table.
@@ -135,14 +156,7 @@ static unsigned long iommu_range_alloc(struct iommu_table *tbl,
                }
        }
 
-       for (i = n; i < end; i++)
-               if (test_bit(i, tbl->it_map)) {
-                       start = i+1;
-                       goto again;
-               }
-
-       for (i = n; i < end; i++)
-               __set_bit(i, tbl->it_map);
+       end = n + npages;
 
        /* Bump the hint to a new block for small allocs. */
        if (largealloc) {
@@ -161,16 +175,19 @@ static unsigned long iommu_range_alloc(struct iommu_table *tbl,
        return n;
 }
 
-static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *page,
-                      unsigned int npages, enum dma_data_direction direction,
-                      unsigned long mask, unsigned int align_order)
+static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
+                             void *page, unsigned int npages,
+                             enum dma_data_direction direction,
+                             unsigned long mask, unsigned int align_order,
+                             struct dma_attrs *attrs)
 {
        unsigned long entry, flags;
        dma_addr_t ret = DMA_ERROR_CODE;
+       int build_fail;
 
        spin_lock_irqsave(&(tbl->it_lock), flags);
 
-       entry = iommu_range_alloc(tbl, npages, NULL, mask, align_order);
+       entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
 
        if (unlikely(entry == DMA_ERROR_CODE)) {
                spin_unlock_irqrestore(&(tbl->it_lock), flags);
@@ -178,12 +195,24 @@ static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *page,
        }
 
        entry += tbl->it_offset;        /* Offset into real TCE table */
-       ret = entry << PAGE_SHIFT;      /* Set the return dma address */
+       ret = entry << IOMMU_PAGE_SHIFT;        /* Set the return dma address */
 
        /* Put the TCEs in the HW table */
-       ppc_md.tce_build(tbl, entry, npages, (unsigned long)page & PAGE_MASK,
-                        direction);
+       build_fail = ppc_md.tce_build(tbl, entry, npages,
+                                     (unsigned long)page & IOMMU_PAGE_MASK,
+                                     direction, attrs);
+
+       /* ppc_md.tce_build() only returns non-zero for transient errors.
+        * Clean up the table bitmap in this case and return
+        * DMA_ERROR_CODE. For all other errors the functionality is
+        * not altered.
+        */
+       if (unlikely(build_fail)) {
+               __iommu_free(tbl, ret, npages);
 
+               spin_unlock_irqrestore(&(tbl->it_lock), flags);
+               return DMA_ERROR_CODE;
+       }
 
        /* Flush/invalidate TLB caches if necessary */
        if (ppc_md.tce_flush)
@@ -201,9 +230,8 @@ static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
                         unsigned int npages)
 {
        unsigned long entry, free_entry;
-       unsigned long i;
 
-       entry = dma_addr >> PAGE_SHIFT;
+       entry = dma_addr >> IOMMU_PAGE_SHIFT;
        free_entry = entry - tbl->it_offset;
 
        if (((free_entry + npages) > tbl->it_size) ||
@@ -211,21 +239,19 @@ static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
                if (printk_ratelimit()) {
                        printk(KERN_INFO "iommu_free: invalid entry\n");
                        printk(KERN_INFO "\tentry     = 0x%lx\n", entry); 
-                       printk(KERN_INFO "\tdma_addr  = 0x%lx\n", (u64)dma_addr);
-                       printk(KERN_INFO "\tTable     = 0x%lx\n", (u64)tbl);
-                       printk(KERN_INFO "\tbus#      = 0x%lx\n", (u64)tbl->it_busno);
-                       printk(KERN_INFO "\tsize      = 0x%lx\n", (u64)tbl->it_size);
-                       printk(KERN_INFO "\tstartOff  = 0x%lx\n", (u64)tbl->it_offset);
-                       printk(KERN_INFO "\tindex     = 0x%lx\n", (u64)tbl->it_index);
+                       printk(KERN_INFO "\tdma_addr  = 0x%llx\n", (u64)dma_addr);
+                       printk(KERN_INFO "\tTable     = 0x%llx\n", (u64)tbl);
+                       printk(KERN_INFO "\tbus#      = 0x%llx\n", (u64)tbl->it_busno);
+                       printk(KERN_INFO "\tsize      = 0x%llx\n", (u64)tbl->it_size);
+                       printk(KERN_INFO "\tstartOff  = 0x%llx\n", (u64)tbl->it_offset);
+                       printk(KERN_INFO "\tindex     = 0x%llx\n", (u64)tbl->it_index);
                        WARN_ON(1);
                }
                return;
        }
 
        ppc_md.tce_free(tbl, entry, npages);
-       
-       for (i = 0; i < npages; i++)
-               __clear_bit(free_entry+i, tbl->it_map);
+       iommu_area_free(tbl->it_map, free_entry, npages);
 }
 
 static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
@@ -248,14 +274,17 @@ static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
 }
 
 int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
-               struct scatterlist *sglist, int nelems,
-               unsigned long mask, enum dma_data_direction direction)
+                struct scatterlist *sglist, int nelems,
+                unsigned long mask, enum dma_data_direction direction,
+                struct dma_attrs *attrs)
 {
        dma_addr_t dma_next = 0, dma_addr;
        unsigned long flags;
        struct scatterlist *s, *outs, *segstart;
-       int outcount, incount;
+       int outcount, incount, i, build_fail = 0;
+       unsigned int align;
        unsigned long handle;
+       unsigned int max_seg_size;
 
        BUG_ON(direction == DMA_NONE);
 
@@ -270,11 +299,12 @@ int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
        /* Init first segment length for backout at failure */
        outs->dma_length = 0;
 
-       DBG("mapping %d elements:\n", nelems);
+       DBG("sg mapping %d elements:\n", nelems);
 
        spin_lock_irqsave(&(tbl->it_lock), flags);
 
-       for (s = outs; nelems; nelems--, s++) {
+       max_seg_size = dma_get_max_seg_size(dev);
+       for_each_sg(sglist, s, nelems, i) {
                unsigned long vaddr, npages, entry, slen;
 
                slen = s->length;
@@ -284,10 +314,14 @@ int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
                        continue;
                }
                /* Allocate iommu entries for that segment */
-               vaddr = (unsigned long)page_address(s->page) + s->offset;
-               npages = PAGE_ALIGN(vaddr + slen) - (vaddr & PAGE_MASK);
-               npages >>= PAGE_SHIFT;
-               entry = iommu_range_alloc(tbl, npages, &handle, mask >> PAGE_SHIFT, 0);
+               vaddr = (unsigned long) sg_virt(s);
+               npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE);
+               align = 0;
+               if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && slen >= PAGE_SIZE &&
+                   (vaddr & ~PAGE_MASK) == 0)
+                       align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
+               entry = iommu_range_alloc(dev, tbl, npages, &handle,
+                                         mask >> IOMMU_PAGE_SHIFT, align);
 
                DBG("  - vaddr: %lx, size: %lx\n", vaddr, slen);
 
@@ -301,14 +335,18 @@ int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
 
                /* Convert entry to a dma_addr_t */
                entry += tbl->it_offset;
-               dma_addr = entry << PAGE_SHIFT;
-               dma_addr |= s->offset;
+               dma_addr = entry << IOMMU_PAGE_SHIFT;
+               dma_addr |= (s->offset & ~IOMMU_PAGE_MASK);
 
-               DBG("  - %lx pages, entry: %lx, dma_addr: %lx\n",
+               DBG("  - %lu pages, entry: %lx, dma_addr: %lx\n",
                            npages, entry, dma_addr);
 
                /* Insert into HW table */
-               ppc_md.tce_build(tbl, entry, npages, vaddr & PAGE_MASK, direction);
+               build_fail = ppc_md.tce_build(tbl, entry, npages,
+                                             vaddr & IOMMU_PAGE_MASK,
+                                             direction, attrs);
+               if(unlikely(build_fail))
+                       goto failure;
 
                /* If we are in an open segment, try merging */
                if (segstart != s) {
@@ -316,14 +354,16 @@ int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
                        /* We cannot merge if:
                         * - allocated dma_addr isn't contiguous to previous allocation
                         */
-                       if (novmerge || (dma_addr != dma_next)) {
+                       if (novmerge || (dma_addr != dma_next) ||
+                           (outs->dma_length + s->length > max_seg_size)) {
                                /* Can't merge: create a new segment */
                                segstart = s;
-                               outcount++; outs++;
+                               outcount++;
+                               outs = sg_next(outs);
                                DBG("    can't merge, new segment.\n");
                        } else {
                                outs->dma_length += s->length;
-                               DBG("    merged, new len: %lx\n", outs->dma_length);
+                               DBG("    merged, new len: %ux\n", outs->dma_length);
                        }
                }
 
@@ -352,7 +392,7 @@ int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
         * next entry of the sglist if we didn't fill the list completely
         */
        if (outcount < incount) {
-               outs++;
+               outs = sg_next(outs);
                outs->dma_address = DMA_ERROR_CODE;
                outs->dma_length = 0;
        }
@@ -363,17 +403,19 @@ int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
        return outcount;
 
  failure:
-       for (s = &sglist[0]; s <= outs; s++) {
+       for_each_sg(sglist, s, nelems, i) {
                if (s->dma_length != 0) {
                        unsigned long vaddr, npages;
 
-                       vaddr = s->dma_address & PAGE_MASK;
-                       npages = (PAGE_ALIGN(s->dma_address + s->dma_length) - vaddr)
-                               >> PAGE_SHIFT;
+                       vaddr = s->dma_address & IOMMU_PAGE_MASK;
+                       npages = iommu_num_pages(s->dma_address, s->dma_length,
+                                                IOMMU_PAGE_SIZE);
                        __iommu_free(tbl, vaddr, npages);
                        s->dma_address = DMA_ERROR_CODE;
                        s->dma_length = 0;
                }
+               if (s == outs)
+                       break;
        }
        spin_unlock_irqrestore(&(tbl->it_lock), flags);
        return 0;
@@ -381,8 +423,10 @@ int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
 
 
 void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
-               int nelems, enum dma_data_direction direction)
+               int nelems, enum dma_data_direction direction,
+               struct dma_attrs *attrs)
 {
+       struct scatterlist *sg;
        unsigned long flags;
 
        BUG_ON(direction == DMA_NONE);
@@ -392,16 +436,17 @@ void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
 
        spin_lock_irqsave(&(tbl->it_lock), flags);
 
+       sg = sglist;
        while (nelems--) {
                unsigned int npages;
-               dma_addr_t dma_handle = sglist->dma_address;
+               dma_addr_t dma_handle = sg->dma_address;
 
-               if (sglist->dma_length == 0)
+               if (sg->dma_length == 0)
                        break;
-               npages = (PAGE_ALIGN(dma_handle + sglist->dma_length)
-                         - (dma_handle & PAGE_MASK)) >> PAGE_SHIFT;
+               npages = iommu_num_pages(dma_handle, sg->dma_length,
+                                        IOMMU_PAGE_SIZE);
                __iommu_free(tbl, dma_handle, npages);
-               sglist++;
+               sg = sg_next(sg);
        }
 
        /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
@@ -414,14 +459,51 @@ void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
        spin_unlock_irqrestore(&(tbl->it_lock), flags);
 }
 
+static void iommu_table_clear(struct iommu_table *tbl)
+{
+       if (!is_kdump_kernel()) {
+               /* Clear the table in case firmware left allocations in it */
+               ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
+               return;
+       }
+
+#ifdef CONFIG_CRASH_DUMP
+       if (ppc_md.tce_get) {
+               unsigned long index, tceval, tcecount = 0;
+
+               /* Reserve the existing mappings left by the first kernel. */
+               for (index = 0; index < tbl->it_size; index++) {
+                       tceval = ppc_md.tce_get(tbl, index + tbl->it_offset);
+                       /*
+                        * Freed TCE entry contains 0x7fffffffffffffff on JS20
+                        */
+                       if (tceval && (tceval != 0x7fffffffffffffffUL)) {
+                               __set_bit(index, tbl->it_map);
+                               tcecount++;
+                       }
+               }
+
+               if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {
+                       printk(KERN_WARNING "TCE table is full; freeing ");
+                       printk(KERN_WARNING "%d entries for the kdump boot\n",
+                               KDUMP_MIN_TCE_ENTRIES);
+                       for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;
+                               index < tbl->it_size; index++)
+                               __clear_bit(index, tbl->it_map);
+               }
+       }
+#endif
+}
+
 /*
  * Build a iommu_table structure.  This contains a bit map which
  * is used to manage allocation of the tce space.
  */
-struct iommu_table *iommu_init_table(struct iommu_table *tbl)
+struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
 {
        unsigned long sz;
        static int welcomed = 0;
+       struct page *page;
 
        /* Set aside 1/4 of the table for large allocations. */
        tbl->it_halfpoint = tbl->it_size * 3 / 4;
@@ -429,18 +511,17 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl)
        /* number of bytes needed for the bitmap */
        sz = (tbl->it_size + 7) >> 3;
 
-       tbl->it_map = (unsigned long *)__get_free_pages(GFP_ATOMIC, get_order(sz));
-       if (!tbl->it_map)
+       page = alloc_pages_node(nid, GFP_ATOMIC, get_order(sz));
+       if (!page)
                panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
-
+       tbl->it_map = page_address(page);
        memset(tbl->it_map, 0, sz);
 
        tbl->it_hint = 0;
        tbl->it_largehint = tbl->it_halfpoint;
        spin_lock_init(&tbl->it_lock);
 
-       /* Clear the hardware table in case firmware left allocations in it */
-       ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
+       iommu_table_clear(tbl);
 
        if (!welcomed) {
                printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
@@ -451,16 +532,14 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl)
        return tbl;
 }
 
-void iommu_free_table(struct device_node *dn)
+void iommu_free_table(struct iommu_table *tbl, const char *node_name)
 {
-       struct pci_dn *pdn = dn->data;
-       struct iommu_table *tbl = pdn->iommu_table;
        unsigned long bitmap_sz, i;
        unsigned int order;
 
        if (!tbl || !tbl->it_map) {
-               printk(KERN_ERR "%s: expected TCE map for %s\n", __FUNCTION__,
-                               dn->full_name);
+               printk(KERN_ERR "%s: expected TCE map for %s\n", __func__,
+                               node_name);
                return;
        }
 
@@ -469,7 +548,7 @@ void iommu_free_table(struct device_node *dn)
        for (i = 0; i < (tbl->it_size/64); i++) {
                if (tbl->it_map[i] != 0) {
                        printk(KERN_WARNING "%s: Unexpected TCEs for %s\n",
-                               __FUNCTION__, dn->full_name);
+                               __func__, node_name);
                        break;
                }
        }
@@ -486,28 +565,35 @@ void iommu_free_table(struct device_node *dn)
 }
 
 /* Creates TCEs for a user provided buffer.  The user buffer must be
- * contiguous real kernel storage (not vmalloc).  The address of the buffer
- * passed here is the kernel (virtual) address of the buffer.  The buffer
- * need not be page aligned, the dma_addr_t returned will point to the same
- * byte within the page as vaddr.
+ * contiguous real kernel storage (not vmalloc).  The address passed here
+ * comprises a page address and offset into that page. The dma_addr_t
+ * returned will point to the same byte within the page as was passed in.
  */
-dma_addr_t iommu_map_single(struct iommu_table *tbl, void *vaddr,
-               size_t size, unsigned long mask,
-               enum dma_data_direction direction)
+dma_addr_t iommu_map_page(struct device *dev, struct iommu_table *tbl,
+                         struct page *page, unsigned long offset, size_t size,
+                         unsigned long mask, enum dma_data_direction direction,
+                         struct dma_attrs *attrs)
 {
        dma_addr_t dma_handle = DMA_ERROR_CODE;
+       void *vaddr;
        unsigned long uaddr;
-       unsigned int npages;
+       unsigned int npages, align;
 
        BUG_ON(direction == DMA_NONE);
 
+       vaddr = page_address(page) + offset;
        uaddr = (unsigned long)vaddr;
-       npages = PAGE_ALIGN(uaddr + size) - (uaddr & PAGE_MASK);
-       npages >>= PAGE_SHIFT;
+       npages = iommu_num_pages(uaddr, size, IOMMU_PAGE_SIZE);
 
        if (tbl) {
-               dma_handle = iommu_alloc(tbl, vaddr, npages, direction,
-                                        mask >> PAGE_SHIFT, 0);
+               align = 0;
+               if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && size >= PAGE_SIZE &&
+                   ((unsigned long)vaddr & ~PAGE_MASK) == 0)
+                       align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
+
+               dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction,
+                                        mask >> IOMMU_PAGE_SHIFT, align,
+                                        attrs);
                if (dma_handle == DMA_ERROR_CODE) {
                        if (printk_ratelimit())  {
                                printk(KERN_INFO "iommu_alloc failed, "
@@ -515,35 +601,41 @@ dma_addr_t iommu_map_single(struct iommu_table *tbl, void *vaddr,
                                                tbl, vaddr, npages);
                        }
                } else
-                       dma_handle |= (uaddr & ~PAGE_MASK);
+                       dma_handle |= (uaddr & ~IOMMU_PAGE_MASK);
        }
 
        return dma_handle;
 }
 
-void iommu_unmap_single(struct iommu_table *tbl, dma_addr_t dma_handle,
-               size_t size, enum dma_data_direction direction)
+void iommu_unmap_page(struct iommu_table *tbl, dma_addr_t dma_handle,
+                     size_t size, enum dma_data_direction direction,
+                     struct dma_attrs *attrs)
 {
+       unsigned int npages;
+
        BUG_ON(direction == DMA_NONE);
 
-       if (tbl)
-               iommu_free(tbl, dma_handle, (PAGE_ALIGN(dma_handle + size) -
-                                       (dma_handle & PAGE_MASK)) >> PAGE_SHIFT);
+       if (tbl) {
+               npages = iommu_num_pages(dma_handle, size, IOMMU_PAGE_SIZE);
+               iommu_free(tbl, dma_handle, npages);
+       }
 }
 
 /* Allocates a contiguous real buffer and creates mappings over it.
  * Returns the virtual address of the buffer and sets dma_handle
  * to the dma address (mapping) of the first page.
  */
-void *iommu_alloc_coherent(struct iommu_table *tbl, size_t size,
-               dma_addr_t *dma_handle, unsigned long mask, gfp_t flag)
+void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,
+                          size_t size, dma_addr_t *dma_handle,
+                          unsigned long mask, gfp_t flag, int node)
 {
        void *ret = NULL;
        dma_addr_t mapping;
-       unsigned int npages, order;
+       unsigned int order;
+       unsigned int nio_pages, io_order;
+       struct page *page;
 
        size = PAGE_ALIGN(size);
-       npages = size >> PAGE_SHIFT;
        order = get_order(size);
 
        /*
@@ -560,31 +652,35 @@ void *iommu_alloc_coherent(struct iommu_table *tbl, size_t size,
                return NULL;
 
        /* Alloc enough pages (and possibly more) */
-       ret = (void *)__get_free_pages(flag, order);
-       if (!ret)
+       page = alloc_pages_node(node, flag, order);
+       if (!page)
                return NULL;
+       ret = page_address(page);
        memset(ret, 0, size);
 
        /* Set up tces to cover the allocated range */
-       mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL,
-                             mask >> PAGE_SHIFT, order);
+       nio_pages = size >> IOMMU_PAGE_SHIFT;
+       io_order = get_iommu_order(size);
+       mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
+                             mask >> IOMMU_PAGE_SHIFT, io_order, NULL);
        if (mapping == DMA_ERROR_CODE) {
                free_pages((unsigned long)ret, order);
-               ret = NULL;
-       } else
-               *dma_handle = mapping;
+               return NULL;
+       }
+       *dma_handle = mapping;
        return ret;
 }
 
 void iommu_free_coherent(struct iommu_table *tbl, size_t size,
                         void *vaddr, dma_addr_t dma_handle)
 {
-       unsigned int npages;
-
        if (tbl) {
+               unsigned int nio_pages;
+
+               size = PAGE_ALIGN(size);
+               nio_pages = size >> IOMMU_PAGE_SHIFT;
+               iommu_free(tbl, dma_handle, nio_pages);
                size = PAGE_ALIGN(size);
-               npages = size >> PAGE_SHIFT;
-               iommu_free(tbl, dma_handle, npages);
                free_pages((unsigned long)vaddr, get_order(size));
        }
 }