Fedora kernel-2.6.17-1.2142_FC4
[linux-2.6.git] / arch / ppc64 / kernel / iommu.c
index 381fb93..3441646 100644 (file)
 #include <linux/string.h>
 #include <linux/dma-mapping.h>
 #include <linux/init.h>
+#include <linux/bitops.h>
 #include <asm/io.h>
 #include <asm/prom.h>
 #include <asm/iommu.h>
 #include <asm/pci-bridge.h>
 #include <asm/machdep.h>
-#include <asm/bitops.h>
 
 #define DBG(...)
 
@@ -87,7 +87,7 @@ static unsigned long iommu_range_alloc(struct iommu_table *tbl,
                start = largealloc ? tbl->it_largehint : tbl->it_hint;
 
        /* Use only half of the table for small allocs (15 pages or less) */
-       limit = largealloc ? tbl->it_mapsize : tbl->it_halfpoint;
+       limit = largealloc ? tbl->it_size : tbl->it_halfpoint;
 
        if (largealloc && start < tbl->it_halfpoint)
                start = tbl->it_halfpoint;
@@ -114,7 +114,7 @@ static unsigned long iommu_range_alloc(struct iommu_table *tbl,
                         * Second failure, rescan the other half of the table.
                         */
                        start = (largealloc ^ pass) ? tbl->it_halfpoint : 0;
-                       limit = pass ? tbl->it_mapsize : limit;
+                       limit = pass ? tbl->it_size : limit;
                        pass++;
                        goto again;
                } else {
@@ -194,7 +194,7 @@ static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
        entry = dma_addr >> PAGE_SHIFT;
        free_entry = entry - tbl->it_offset;
 
-       if (((free_entry + npages) > tbl->it_mapsize) ||
+       if (((free_entry + npages) > tbl->it_size) ||
            (entry < tbl->it_offset)) {
                if (printk_ratelimit()) {
                        printk(KERN_INFO "iommu_free: invalid entry\n");
@@ -202,7 +202,7 @@ static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
                        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 "\tmapsize   = 0x%lx\n", (u64)tbl->it_mapsize);
+                       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);
                        WARN_ON(1);
@@ -407,14 +407,11 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl)
        unsigned long sz;
        static int welcomed = 0;
 
-       /* it_size is in pages, it_mapsize in number of entries */
-       tbl->it_mapsize = (tbl->it_size << PAGE_SHIFT) / tbl->it_entrysize;
-
        /* Set aside 1/4 of the table for large allocations. */
-       tbl->it_halfpoint = tbl->it_mapsize * 3 / 4;
+       tbl->it_halfpoint = tbl->it_size * 3 / 4;
 
        /* number of bytes needed for the bitmap */
-       sz = (tbl->it_mapsize + 7) >> 3;
+       sz = (tbl->it_size + 7) >> 3;
 
        tbl->it_map = (unsigned long *)__get_free_pages(GFP_ATOMIC, get_order(sz));
        if (!tbl->it_map)
@@ -435,6 +432,39 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl)
        return tbl;
 }
 
+void iommu_free_table(struct device_node *dn)
+{
+       struct iommu_table *tbl = dn->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);
+               return;
+       }
+
+       /* verify that table contains no entries */
+       /* it_size is in entries, and we're examining 64 at a time */
+       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);
+                       break;
+               }
+       }
+
+       /* calculate bitmap size in bytes */
+       bitmap_sz = (tbl->it_size + 7) / 8;
+
+       /* free bitmap */
+       order = get_order(bitmap_sz);
+       free_pages((unsigned long) tbl->it_map, order);
+
+       /* free table */
+       kfree(tbl);
+}
+
 /* 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
@@ -483,8 +513,8 @@ void iommu_unmap_single(struct iommu_table *tbl, dma_addr_t dma_handle,
  * Returns the virtual address of the buffer and sets dma_handle
  * to the dma address (mapping) of the first page.
  */
-void *iommu_alloc_consistent(struct iommu_table *tbl, size_t size,
-               dma_addr_t *dma_handle)
+void *iommu_alloc_coherent(struct iommu_table *tbl, size_t size,
+               dma_addr_t *dma_handle, unsigned int __nocast flag)
 {
        void *ret = NULL;
        dma_addr_t mapping;
@@ -508,7 +538,7 @@ void *iommu_alloc_consistent(struct iommu_table *tbl, size_t size,
                return NULL;
 
        /* Alloc enough pages (and possibly more) */
-       ret = (void *)__get_free_pages(GFP_ATOMIC, order);
+       ret = (void *)__get_free_pages(flag, order);
        if (!ret)
                return NULL;
        memset(ret, 0, size);
@@ -523,7 +553,7 @@ void *iommu_alloc_consistent(struct iommu_table *tbl, size_t size,
        return ret;
 }
 
-void iommu_free_consistent(struct iommu_table *tbl, size_t size,
+void iommu_free_coherent(struct iommu_table *tbl, size_t size,
                         void *vaddr, dma_addr_t dma_handle)
 {
        unsigned int npages;