Back to home page

LXR

 
 

    


0001 /*
0002  * DMA Pool allocator
0003  *
0004  * Copyright 2001 David Brownell
0005  * Copyright 2007 Intel Corporation
0006  *   Author: Matthew Wilcox <willy@linux.intel.com>
0007  *
0008  * This software may be redistributed and/or modified under the terms of
0009  * the GNU General Public License ("GPL") version 2 as published by the
0010  * Free Software Foundation.
0011  *
0012  * This allocator returns small blocks of a given size which are DMA-able by
0013  * the given device.  It uses the dma_alloc_coherent page allocator to get
0014  * new pages, then splits them up into blocks of the required size.
0015  * Many older drivers still have their own code to do this.
0016  *
0017  * The current design of this allocator is fairly simple.  The pool is
0018  * represented by the 'struct dma_pool' which keeps a doubly-linked list of
0019  * allocated pages.  Each page in the page_list is split into blocks of at
0020  * least 'size' bytes.  Free blocks are tracked in an unsorted singly-linked
0021  * list of free blocks within the page.  Used blocks aren't tracked, but we
0022  * keep a count of how many are currently allocated from each page.
0023  */
0024 
0025 #include <linux/device.h>
0026 #include <linux/dma-mapping.h>
0027 #include <linux/dmapool.h>
0028 #include <linux/kernel.h>
0029 #include <linux/list.h>
0030 #include <linux/export.h>
0031 #include <linux/mutex.h>
0032 #include <linux/poison.h>
0033 #include <linux/sched.h>
0034 #include <linux/slab.h>
0035 #include <linux/stat.h>
0036 #include <linux/spinlock.h>
0037 #include <linux/string.h>
0038 #include <linux/types.h>
0039 #include <linux/wait.h>
0040 
0041 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON)
0042 #define DMAPOOL_DEBUG 1
0043 #endif
0044 
0045 struct dma_pool {       /* the pool */
0046     struct list_head page_list;
0047     spinlock_t lock;
0048     size_t size;
0049     struct device *dev;
0050     size_t allocation;
0051     size_t boundary;
0052     char name[32];
0053     struct list_head pools;
0054 };
0055 
0056 struct dma_page {       /* cacheable header for 'allocation' bytes */
0057     struct list_head page_list;
0058     void *vaddr;
0059     dma_addr_t dma;
0060     unsigned int in_use;
0061     unsigned int offset;
0062 };
0063 
0064 static DEFINE_MUTEX(pools_lock);
0065 static DEFINE_MUTEX(pools_reg_lock);
0066 
0067 static ssize_t
0068 show_pools(struct device *dev, struct device_attribute *attr, char *buf)
0069 {
0070     unsigned temp;
0071     unsigned size;
0072     char *next;
0073     struct dma_page *page;
0074     struct dma_pool *pool;
0075 
0076     next = buf;
0077     size = PAGE_SIZE;
0078 
0079     temp = scnprintf(next, size, "poolinfo - 0.1\n");
0080     size -= temp;
0081     next += temp;
0082 
0083     mutex_lock(&pools_lock);
0084     list_for_each_entry(pool, &dev->dma_pools, pools) {
0085         unsigned pages = 0;
0086         unsigned blocks = 0;
0087 
0088         spin_lock_irq(&pool->lock);
0089         list_for_each_entry(page, &pool->page_list, page_list) {
0090             pages++;
0091             blocks += page->in_use;
0092         }
0093         spin_unlock_irq(&pool->lock);
0094 
0095         /* per-pool info, no real statistics yet */
0096         temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
0097                  pool->name, blocks,
0098                  pages * (pool->allocation / pool->size),
0099                  pool->size, pages);
0100         size -= temp;
0101         next += temp;
0102     }
0103     mutex_unlock(&pools_lock);
0104 
0105     return PAGE_SIZE - size;
0106 }
0107 
0108 static DEVICE_ATTR(pools, S_IRUGO, show_pools, NULL);
0109 
0110 /**
0111  * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
0112  * @name: name of pool, for diagnostics
0113  * @dev: device that will be doing the DMA
0114  * @size: size of the blocks in this pool.
0115  * @align: alignment requirement for blocks; must be a power of two
0116  * @boundary: returned blocks won't cross this power of two boundary
0117  * Context: !in_interrupt()
0118  *
0119  * Returns a dma allocation pool with the requested characteristics, or
0120  * null if one can't be created.  Given one of these pools, dma_pool_alloc()
0121  * may be used to allocate memory.  Such memory will all have "consistent"
0122  * DMA mappings, accessible by the device and its driver without using
0123  * cache flushing primitives.  The actual size of blocks allocated may be
0124  * larger than requested because of alignment.
0125  *
0126  * If @boundary is nonzero, objects returned from dma_pool_alloc() won't
0127  * cross that size boundary.  This is useful for devices which have
0128  * addressing restrictions on individual DMA transfers, such as not crossing
0129  * boundaries of 4KBytes.
0130  */
0131 struct dma_pool *dma_pool_create(const char *name, struct device *dev,
0132                  size_t size, size_t align, size_t boundary)
0133 {
0134     struct dma_pool *retval;
0135     size_t allocation;
0136     bool empty = false;
0137 
0138     if (align == 0)
0139         align = 1;
0140     else if (align & (align - 1))
0141         return NULL;
0142 
0143     if (size == 0)
0144         return NULL;
0145     else if (size < 4)
0146         size = 4;
0147 
0148     if ((size % align) != 0)
0149         size = ALIGN(size, align);
0150 
0151     allocation = max_t(size_t, size, PAGE_SIZE);
0152 
0153     if (!boundary)
0154         boundary = allocation;
0155     else if ((boundary < size) || (boundary & (boundary - 1)))
0156         return NULL;
0157 
0158     retval = kmalloc_node(sizeof(*retval), GFP_KERNEL, dev_to_node(dev));
0159     if (!retval)
0160         return retval;
0161 
0162     strlcpy(retval->name, name, sizeof(retval->name));
0163 
0164     retval->dev = dev;
0165 
0166     INIT_LIST_HEAD(&retval->page_list);
0167     spin_lock_init(&retval->lock);
0168     retval->size = size;
0169     retval->boundary = boundary;
0170     retval->allocation = allocation;
0171 
0172     INIT_LIST_HEAD(&retval->pools);
0173 
0174     /*
0175      * pools_lock ensures that the ->dma_pools list does not get corrupted.
0176      * pools_reg_lock ensures that there is not a race between
0177      * dma_pool_create() and dma_pool_destroy() or within dma_pool_create()
0178      * when the first invocation of dma_pool_create() failed on
0179      * device_create_file() and the second assumes that it has been done (I
0180      * know it is a short window).
0181      */
0182     mutex_lock(&pools_reg_lock);
0183     mutex_lock(&pools_lock);
0184     if (list_empty(&dev->dma_pools))
0185         empty = true;
0186     list_add(&retval->pools, &dev->dma_pools);
0187     mutex_unlock(&pools_lock);
0188     if (empty) {
0189         int err;
0190 
0191         err = device_create_file(dev, &dev_attr_pools);
0192         if (err) {
0193             mutex_lock(&pools_lock);
0194             list_del(&retval->pools);
0195             mutex_unlock(&pools_lock);
0196             mutex_unlock(&pools_reg_lock);
0197             kfree(retval);
0198             return NULL;
0199         }
0200     }
0201     mutex_unlock(&pools_reg_lock);
0202     return retval;
0203 }
0204 EXPORT_SYMBOL(dma_pool_create);
0205 
0206 static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page)
0207 {
0208     unsigned int offset = 0;
0209     unsigned int next_boundary = pool->boundary;
0210 
0211     do {
0212         unsigned int next = offset + pool->size;
0213         if (unlikely((next + pool->size) >= next_boundary)) {
0214             next = next_boundary;
0215             next_boundary += pool->boundary;
0216         }
0217         *(int *)(page->vaddr + offset) = next;
0218         offset = next;
0219     } while (offset < pool->allocation);
0220 }
0221 
0222 static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
0223 {
0224     struct dma_page *page;
0225 
0226     page = kmalloc(sizeof(*page), mem_flags);
0227     if (!page)
0228         return NULL;
0229     page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation,
0230                      &page->dma, mem_flags);
0231     if (page->vaddr) {
0232 #ifdef  DMAPOOL_DEBUG
0233         memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
0234 #endif
0235         pool_initialise_page(pool, page);
0236         page->in_use = 0;
0237         page->offset = 0;
0238     } else {
0239         kfree(page);
0240         page = NULL;
0241     }
0242     return page;
0243 }
0244 
0245 static inline bool is_page_busy(struct dma_page *page)
0246 {
0247     return page->in_use != 0;
0248 }
0249 
0250 static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
0251 {
0252     dma_addr_t dma = page->dma;
0253 
0254 #ifdef  DMAPOOL_DEBUG
0255     memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
0256 #endif
0257     dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
0258     list_del(&page->page_list);
0259     kfree(page);
0260 }
0261 
0262 /**
0263  * dma_pool_destroy - destroys a pool of dma memory blocks.
0264  * @pool: dma pool that will be destroyed
0265  * Context: !in_interrupt()
0266  *
0267  * Caller guarantees that no more memory from the pool is in use,
0268  * and that nothing will try to use the pool after this call.
0269  */
0270 void dma_pool_destroy(struct dma_pool *pool)
0271 {
0272     bool empty = false;
0273 
0274     if (unlikely(!pool))
0275         return;
0276 
0277     mutex_lock(&pools_reg_lock);
0278     mutex_lock(&pools_lock);
0279     list_del(&pool->pools);
0280     if (pool->dev && list_empty(&pool->dev->dma_pools))
0281         empty = true;
0282     mutex_unlock(&pools_lock);
0283     if (empty)
0284         device_remove_file(pool->dev, &dev_attr_pools);
0285     mutex_unlock(&pools_reg_lock);
0286 
0287     while (!list_empty(&pool->page_list)) {
0288         struct dma_page *page;
0289         page = list_entry(pool->page_list.next,
0290                   struct dma_page, page_list);
0291         if (is_page_busy(page)) {
0292             if (pool->dev)
0293                 dev_err(pool->dev,
0294                     "dma_pool_destroy %s, %p busy\n",
0295                     pool->name, page->vaddr);
0296             else
0297                 pr_err("dma_pool_destroy %s, %p busy\n",
0298                        pool->name, page->vaddr);
0299             /* leak the still-in-use consistent memory */
0300             list_del(&page->page_list);
0301             kfree(page);
0302         } else
0303             pool_free_page(pool, page);
0304     }
0305 
0306     kfree(pool);
0307 }
0308 EXPORT_SYMBOL(dma_pool_destroy);
0309 
0310 /**
0311  * dma_pool_alloc - get a block of consistent memory
0312  * @pool: dma pool that will produce the block
0313  * @mem_flags: GFP_* bitmask
0314  * @handle: pointer to dma address of block
0315  *
0316  * This returns the kernel virtual address of a currently unused block,
0317  * and reports its dma address through the handle.
0318  * If such a memory block can't be allocated, %NULL is returned.
0319  */
0320 void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
0321              dma_addr_t *handle)
0322 {
0323     unsigned long flags;
0324     struct dma_page *page;
0325     size_t offset;
0326     void *retval;
0327 
0328     might_sleep_if(gfpflags_allow_blocking(mem_flags));
0329 
0330     spin_lock_irqsave(&pool->lock, flags);
0331     list_for_each_entry(page, &pool->page_list, page_list) {
0332         if (page->offset < pool->allocation)
0333             goto ready;
0334     }
0335 
0336     /* pool_alloc_page() might sleep, so temporarily drop &pool->lock */
0337     spin_unlock_irqrestore(&pool->lock, flags);
0338 
0339     page = pool_alloc_page(pool, mem_flags & (~__GFP_ZERO));
0340     if (!page)
0341         return NULL;
0342 
0343     spin_lock_irqsave(&pool->lock, flags);
0344 
0345     list_add(&page->page_list, &pool->page_list);
0346  ready:
0347     page->in_use++;
0348     offset = page->offset;
0349     page->offset = *(int *)(page->vaddr + offset);
0350     retval = offset + page->vaddr;
0351     *handle = offset + page->dma;
0352 #ifdef  DMAPOOL_DEBUG
0353     {
0354         int i;
0355         u8 *data = retval;
0356         /* page->offset is stored in first 4 bytes */
0357         for (i = sizeof(page->offset); i < pool->size; i++) {
0358             if (data[i] == POOL_POISON_FREED)
0359                 continue;
0360             if (pool->dev)
0361                 dev_err(pool->dev,
0362                     "dma_pool_alloc %s, %p (corrupted)\n",
0363                     pool->name, retval);
0364             else
0365                 pr_err("dma_pool_alloc %s, %p (corrupted)\n",
0366                     pool->name, retval);
0367 
0368             /*
0369              * Dump the first 4 bytes even if they are not
0370              * POOL_POISON_FREED
0371              */
0372             print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1,
0373                     data, pool->size, 1);
0374             break;
0375         }
0376     }
0377     if (!(mem_flags & __GFP_ZERO))
0378         memset(retval, POOL_POISON_ALLOCATED, pool->size);
0379 #endif
0380     spin_unlock_irqrestore(&pool->lock, flags);
0381 
0382     if (mem_flags & __GFP_ZERO)
0383         memset(retval, 0, pool->size);
0384 
0385     return retval;
0386 }
0387 EXPORT_SYMBOL(dma_pool_alloc);
0388 
0389 static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
0390 {
0391     struct dma_page *page;
0392 
0393     list_for_each_entry(page, &pool->page_list, page_list) {
0394         if (dma < page->dma)
0395             continue;
0396         if ((dma - page->dma) < pool->allocation)
0397             return page;
0398     }
0399     return NULL;
0400 }
0401 
0402 /**
0403  * dma_pool_free - put block back into dma pool
0404  * @pool: the dma pool holding the block
0405  * @vaddr: virtual address of block
0406  * @dma: dma address of block
0407  *
0408  * Caller promises neither device nor driver will again touch this block
0409  * unless it is first re-allocated.
0410  */
0411 void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
0412 {
0413     struct dma_page *page;
0414     unsigned long flags;
0415     unsigned int offset;
0416 
0417     spin_lock_irqsave(&pool->lock, flags);
0418     page = pool_find_page(pool, dma);
0419     if (!page) {
0420         spin_unlock_irqrestore(&pool->lock, flags);
0421         if (pool->dev)
0422             dev_err(pool->dev,
0423                 "dma_pool_free %s, %p/%lx (bad dma)\n",
0424                 pool->name, vaddr, (unsigned long)dma);
0425         else
0426             pr_err("dma_pool_free %s, %p/%lx (bad dma)\n",
0427                    pool->name, vaddr, (unsigned long)dma);
0428         return;
0429     }
0430 
0431     offset = vaddr - page->vaddr;
0432 #ifdef  DMAPOOL_DEBUG
0433     if ((dma - page->dma) != offset) {
0434         spin_unlock_irqrestore(&pool->lock, flags);
0435         if (pool->dev)
0436             dev_err(pool->dev,
0437                 "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
0438                 pool->name, vaddr, (unsigned long long)dma);
0439         else
0440             pr_err("dma_pool_free %s, %p (bad vaddr)/%Lx\n",
0441                    pool->name, vaddr, (unsigned long long)dma);
0442         return;
0443     }
0444     {
0445         unsigned int chain = page->offset;
0446         while (chain < pool->allocation) {
0447             if (chain != offset) {
0448                 chain = *(int *)(page->vaddr + chain);
0449                 continue;
0450             }
0451             spin_unlock_irqrestore(&pool->lock, flags);
0452             if (pool->dev)
0453                 dev_err(pool->dev, "dma_pool_free %s, dma %Lx already free\n",
0454                     pool->name, (unsigned long long)dma);
0455             else
0456                 pr_err("dma_pool_free %s, dma %Lx already free\n",
0457                        pool->name, (unsigned long long)dma);
0458             return;
0459         }
0460     }
0461     memset(vaddr, POOL_POISON_FREED, pool->size);
0462 #endif
0463 
0464     page->in_use--;
0465     *(int *)vaddr = page->offset;
0466     page->offset = offset;
0467     /*
0468      * Resist a temptation to do
0469      *    if (!is_page_busy(page)) pool_free_page(pool, page);
0470      * Better have a few empty pages hang around.
0471      */
0472     spin_unlock_irqrestore(&pool->lock, flags);
0473 }
0474 EXPORT_SYMBOL(dma_pool_free);
0475 
0476 /*
0477  * Managed DMA pool
0478  */
0479 static void dmam_pool_release(struct device *dev, void *res)
0480 {
0481     struct dma_pool *pool = *(struct dma_pool **)res;
0482 
0483     dma_pool_destroy(pool);
0484 }
0485 
0486 static int dmam_pool_match(struct device *dev, void *res, void *match_data)
0487 {
0488     return *(struct dma_pool **)res == match_data;
0489 }
0490 
0491 /**
0492  * dmam_pool_create - Managed dma_pool_create()
0493  * @name: name of pool, for diagnostics
0494  * @dev: device that will be doing the DMA
0495  * @size: size of the blocks in this pool.
0496  * @align: alignment requirement for blocks; must be a power of two
0497  * @allocation: returned blocks won't cross this boundary (or zero)
0498  *
0499  * Managed dma_pool_create().  DMA pool created with this function is
0500  * automatically destroyed on driver detach.
0501  */
0502 struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
0503                   size_t size, size_t align, size_t allocation)
0504 {
0505     struct dma_pool **ptr, *pool;
0506 
0507     ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
0508     if (!ptr)
0509         return NULL;
0510 
0511     pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
0512     if (pool)
0513         devres_add(dev, ptr);
0514     else
0515         devres_free(ptr);
0516 
0517     return pool;
0518 }
0519 EXPORT_SYMBOL(dmam_pool_create);
0520 
0521 /**
0522  * dmam_pool_destroy - Managed dma_pool_destroy()
0523  * @pool: dma pool that will be destroyed
0524  *
0525  * Managed dma_pool_destroy().
0526  */
0527 void dmam_pool_destroy(struct dma_pool *pool)
0528 {
0529     struct device *dev = pool->dev;
0530 
0531     WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool));
0532 }
0533 EXPORT_SYMBOL(dmam_pool_destroy);