16182a094SMatthew Wilcox /* 26182a094SMatthew Wilcox * DMA Pool allocator 36182a094SMatthew Wilcox * 46182a094SMatthew Wilcox * Copyright 2001 David Brownell 56182a094SMatthew Wilcox * Copyright 2007 Intel Corporation 66182a094SMatthew Wilcox * Author: Matthew Wilcox <willy@linux.intel.com> 76182a094SMatthew Wilcox * 86182a094SMatthew Wilcox * This software may be redistributed and/or modified under the terms of 96182a094SMatthew Wilcox * the GNU General Public License ("GPL") version 2 as published by the 106182a094SMatthew Wilcox * Free Software Foundation. 116182a094SMatthew Wilcox * 126182a094SMatthew Wilcox * This allocator returns small blocks of a given size which are DMA-able by 136182a094SMatthew Wilcox * the given device. It uses the dma_alloc_coherent page allocator to get 146182a094SMatthew Wilcox * new pages, then splits them up into blocks of the required size. 156182a094SMatthew Wilcox * Many older drivers still have their own code to do this. 166182a094SMatthew Wilcox * 176182a094SMatthew Wilcox * The current design of this allocator is fairly simple. The pool is 186182a094SMatthew Wilcox * represented by the 'struct dma_pool' which keeps a doubly-linked list of 196182a094SMatthew Wilcox * allocated pages. Each page in the page_list is split into blocks of at 20a35a3455SMatthew Wilcox * least 'size' bytes. Free blocks are tracked in an unsorted singly-linked 21a35a3455SMatthew Wilcox * list of free blocks within the page. Used blocks aren't tracked, but we 22a35a3455SMatthew Wilcox * keep a count of how many are currently allocated from each page. 236182a094SMatthew Wilcox */ 24141e9d4bSMatthew Wilcox 25141e9d4bSMatthew Wilcox #include <linux/device.h> 26141e9d4bSMatthew Wilcox #include <linux/dma-mapping.h> 27141e9d4bSMatthew Wilcox #include <linux/dmapool.h> 286182a094SMatthew Wilcox #include <linux/kernel.h> 296182a094SMatthew Wilcox #include <linux/list.h> 30b95f1b31SPaul Gortmaker #include <linux/export.h> 316182a094SMatthew Wilcox #include <linux/mutex.h> 32141e9d4bSMatthew Wilcox #include <linux/poison.h> 33141e9d4bSMatthew Wilcox #include <linux/sched.h> 346182a094SMatthew Wilcox #include <linux/slab.h> 357c77509cSPaul Gortmaker #include <linux/stat.h> 366182a094SMatthew Wilcox #include <linux/spinlock.h> 376182a094SMatthew Wilcox #include <linux/string.h> 386182a094SMatthew Wilcox #include <linux/types.h> 396182a094SMatthew Wilcox #include <linux/wait.h> 40141e9d4bSMatthew Wilcox 41b5ee5befSAndi Kleen #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON) 42b5ee5befSAndi Kleen #define DMAPOOL_DEBUG 1 43b5ee5befSAndi Kleen #endif 44b5ee5befSAndi Kleen 45141e9d4bSMatthew Wilcox struct dma_pool { /* the pool */ 46141e9d4bSMatthew Wilcox struct list_head page_list; 47141e9d4bSMatthew Wilcox spinlock_t lock; 48141e9d4bSMatthew Wilcox size_t size; 49141e9d4bSMatthew Wilcox struct device *dev; 50141e9d4bSMatthew Wilcox size_t allocation; 51e34f44b3SMatthew Wilcox size_t boundary; 52141e9d4bSMatthew Wilcox char name[32]; 53141e9d4bSMatthew Wilcox struct list_head pools; 54141e9d4bSMatthew Wilcox }; 55141e9d4bSMatthew Wilcox 56141e9d4bSMatthew Wilcox struct dma_page { /* cacheable header for 'allocation' bytes */ 57141e9d4bSMatthew Wilcox struct list_head page_list; 58141e9d4bSMatthew Wilcox void *vaddr; 59141e9d4bSMatthew Wilcox dma_addr_t dma; 60a35a3455SMatthew Wilcox unsigned int in_use; 61a35a3455SMatthew Wilcox unsigned int offset; 62141e9d4bSMatthew Wilcox }; 63141e9d4bSMatthew Wilcox 64141e9d4bSMatthew Wilcox static DEFINE_MUTEX(pools_lock); 6501c2965fSSebastian Andrzej Siewior static DEFINE_MUTEX(pools_reg_lock); 66141e9d4bSMatthew Wilcox 67141e9d4bSMatthew Wilcox static ssize_t 68141e9d4bSMatthew Wilcox show_pools(struct device *dev, struct device_attribute *attr, char *buf) 69141e9d4bSMatthew Wilcox { 70141e9d4bSMatthew Wilcox unsigned temp; 71141e9d4bSMatthew Wilcox unsigned size; 72141e9d4bSMatthew Wilcox char *next; 73141e9d4bSMatthew Wilcox struct dma_page *page; 74141e9d4bSMatthew Wilcox struct dma_pool *pool; 75141e9d4bSMatthew Wilcox 76141e9d4bSMatthew Wilcox next = buf; 77141e9d4bSMatthew Wilcox size = PAGE_SIZE; 78141e9d4bSMatthew Wilcox 79141e9d4bSMatthew Wilcox temp = scnprintf(next, size, "poolinfo - 0.1\n"); 80141e9d4bSMatthew Wilcox size -= temp; 81141e9d4bSMatthew Wilcox next += temp; 82141e9d4bSMatthew Wilcox 83141e9d4bSMatthew Wilcox mutex_lock(&pools_lock); 84141e9d4bSMatthew Wilcox list_for_each_entry(pool, &dev->dma_pools, pools) { 85141e9d4bSMatthew Wilcox unsigned pages = 0; 86141e9d4bSMatthew Wilcox unsigned blocks = 0; 87141e9d4bSMatthew Wilcox 88c4956823SThomas Gleixner spin_lock_irq(&pool->lock); 89141e9d4bSMatthew Wilcox list_for_each_entry(page, &pool->page_list, page_list) { 90141e9d4bSMatthew Wilcox pages++; 91141e9d4bSMatthew Wilcox blocks += page->in_use; 92141e9d4bSMatthew Wilcox } 93c4956823SThomas Gleixner spin_unlock_irq(&pool->lock); 94141e9d4bSMatthew Wilcox 95141e9d4bSMatthew Wilcox /* per-pool info, no real statistics yet */ 965b5e0928SAlexey Dobriyan temp = scnprintf(next, size, "%-16s %4u %4zu %4zu %2u\n", 97a35a3455SMatthew Wilcox pool->name, blocks, 98a35a3455SMatthew Wilcox pages * (pool->allocation / pool->size), 99141e9d4bSMatthew Wilcox pool->size, pages); 100141e9d4bSMatthew Wilcox size -= temp; 101141e9d4bSMatthew Wilcox next += temp; 102141e9d4bSMatthew Wilcox } 103141e9d4bSMatthew Wilcox mutex_unlock(&pools_lock); 104141e9d4bSMatthew Wilcox 105141e9d4bSMatthew Wilcox return PAGE_SIZE - size; 106141e9d4bSMatthew Wilcox } 107e87aa773SMatthew Wilcox 1080825a6f9SJoe Perches static DEVICE_ATTR(pools, 0444, show_pools, NULL); 109141e9d4bSMatthew Wilcox 110141e9d4bSMatthew Wilcox /** 111141e9d4bSMatthew Wilcox * dma_pool_create - Creates a pool of consistent memory blocks, for dma. 112141e9d4bSMatthew Wilcox * @name: name of pool, for diagnostics 113141e9d4bSMatthew Wilcox * @dev: device that will be doing the DMA 114141e9d4bSMatthew Wilcox * @size: size of the blocks in this pool. 115141e9d4bSMatthew Wilcox * @align: alignment requirement for blocks; must be a power of two 116e34f44b3SMatthew Wilcox * @boundary: returned blocks won't cross this power of two boundary 117*a862f68aSMike Rapoport * Context: not in_interrupt() 118141e9d4bSMatthew Wilcox * 119*a862f68aSMike Rapoport * Given one of these pools, dma_pool_alloc() 120141e9d4bSMatthew Wilcox * may be used to allocate memory. Such memory will all have "consistent" 121141e9d4bSMatthew Wilcox * DMA mappings, accessible by the device and its driver without using 122141e9d4bSMatthew Wilcox * cache flushing primitives. The actual size of blocks allocated may be 123141e9d4bSMatthew Wilcox * larger than requested because of alignment. 124141e9d4bSMatthew Wilcox * 125e34f44b3SMatthew Wilcox * If @boundary is nonzero, objects returned from dma_pool_alloc() won't 126141e9d4bSMatthew Wilcox * cross that size boundary. This is useful for devices which have 127141e9d4bSMatthew Wilcox * addressing restrictions on individual DMA transfers, such as not crossing 128141e9d4bSMatthew Wilcox * boundaries of 4KBytes. 129*a862f68aSMike Rapoport * 130*a862f68aSMike Rapoport * Return: a dma allocation pool with the requested characteristics, or 131*a862f68aSMike Rapoport * %NULL if one can't be created. 132141e9d4bSMatthew Wilcox */ 133e87aa773SMatthew Wilcox struct dma_pool *dma_pool_create(const char *name, struct device *dev, 134e34f44b3SMatthew Wilcox size_t size, size_t align, size_t boundary) 135141e9d4bSMatthew Wilcox { 136141e9d4bSMatthew Wilcox struct dma_pool *retval; 137e34f44b3SMatthew Wilcox size_t allocation; 13801c2965fSSebastian Andrzej Siewior bool empty = false; 139141e9d4bSMatthew Wilcox 140baa2ef83SPaul McQuade if (align == 0) 141141e9d4bSMatthew Wilcox align = 1; 142baa2ef83SPaul McQuade else if (align & (align - 1)) 143399154beSMatthew Wilcox return NULL; 144399154beSMatthew Wilcox 145baa2ef83SPaul McQuade if (size == 0) 146141e9d4bSMatthew Wilcox return NULL; 147baa2ef83SPaul McQuade else if (size < 4) 148a35a3455SMatthew Wilcox size = 4; 149399154beSMatthew Wilcox 150399154beSMatthew Wilcox if ((size % align) != 0) 151399154beSMatthew Wilcox size = ALIGN(size, align); 152141e9d4bSMatthew Wilcox 153e34f44b3SMatthew Wilcox allocation = max_t(size_t, size, PAGE_SIZE); 154141e9d4bSMatthew Wilcox 155baa2ef83SPaul McQuade if (!boundary) 156e34f44b3SMatthew Wilcox boundary = allocation; 157baa2ef83SPaul McQuade else if ((boundary < size) || (boundary & (boundary - 1))) 158e34f44b3SMatthew Wilcox return NULL; 159e34f44b3SMatthew Wilcox 160e34f44b3SMatthew Wilcox retval = kmalloc_node(sizeof(*retval), GFP_KERNEL, dev_to_node(dev)); 161e34f44b3SMatthew Wilcox if (!retval) 162141e9d4bSMatthew Wilcox return retval; 163141e9d4bSMatthew Wilcox 164e34f44b3SMatthew Wilcox strlcpy(retval->name, name, sizeof(retval->name)); 165141e9d4bSMatthew Wilcox 166141e9d4bSMatthew Wilcox retval->dev = dev; 167141e9d4bSMatthew Wilcox 168141e9d4bSMatthew Wilcox INIT_LIST_HEAD(&retval->page_list); 169141e9d4bSMatthew Wilcox spin_lock_init(&retval->lock); 170141e9d4bSMatthew Wilcox retval->size = size; 171e34f44b3SMatthew Wilcox retval->boundary = boundary; 172141e9d4bSMatthew Wilcox retval->allocation = allocation; 173141e9d4bSMatthew Wilcox 174cc6b664aSDaeseok Youn INIT_LIST_HEAD(&retval->pools); 175141e9d4bSMatthew Wilcox 17601c2965fSSebastian Andrzej Siewior /* 17701c2965fSSebastian Andrzej Siewior * pools_lock ensures that the ->dma_pools list does not get corrupted. 17801c2965fSSebastian Andrzej Siewior * pools_reg_lock ensures that there is not a race between 17901c2965fSSebastian Andrzej Siewior * dma_pool_create() and dma_pool_destroy() or within dma_pool_create() 18001c2965fSSebastian Andrzej Siewior * when the first invocation of dma_pool_create() failed on 18101c2965fSSebastian Andrzej Siewior * device_create_file() and the second assumes that it has been done (I 18201c2965fSSebastian Andrzej Siewior * know it is a short window). 18301c2965fSSebastian Andrzej Siewior */ 18401c2965fSSebastian Andrzej Siewior mutex_lock(&pools_reg_lock); 185141e9d4bSMatthew Wilcox mutex_lock(&pools_lock); 18601c2965fSSebastian Andrzej Siewior if (list_empty(&dev->dma_pools)) 18701c2965fSSebastian Andrzej Siewior empty = true; 188cc6b664aSDaeseok Youn list_add(&retval->pools, &dev->dma_pools); 189cc6b664aSDaeseok Youn mutex_unlock(&pools_lock); 19001c2965fSSebastian Andrzej Siewior if (empty) { 19101c2965fSSebastian Andrzej Siewior int err; 192141e9d4bSMatthew Wilcox 19301c2965fSSebastian Andrzej Siewior err = device_create_file(dev, &dev_attr_pools); 19401c2965fSSebastian Andrzej Siewior if (err) { 19501c2965fSSebastian Andrzej Siewior mutex_lock(&pools_lock); 19601c2965fSSebastian Andrzej Siewior list_del(&retval->pools); 19701c2965fSSebastian Andrzej Siewior mutex_unlock(&pools_lock); 19801c2965fSSebastian Andrzej Siewior mutex_unlock(&pools_reg_lock); 19901c2965fSSebastian Andrzej Siewior kfree(retval); 20001c2965fSSebastian Andrzej Siewior return NULL; 20101c2965fSSebastian Andrzej Siewior } 20201c2965fSSebastian Andrzej Siewior } 20301c2965fSSebastian Andrzej Siewior mutex_unlock(&pools_reg_lock); 204141e9d4bSMatthew Wilcox return retval; 205141e9d4bSMatthew Wilcox } 206e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_create); 207141e9d4bSMatthew Wilcox 208a35a3455SMatthew Wilcox static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page) 209a35a3455SMatthew Wilcox { 210a35a3455SMatthew Wilcox unsigned int offset = 0; 211e34f44b3SMatthew Wilcox unsigned int next_boundary = pool->boundary; 212a35a3455SMatthew Wilcox 213a35a3455SMatthew Wilcox do { 214a35a3455SMatthew Wilcox unsigned int next = offset + pool->size; 215e34f44b3SMatthew Wilcox if (unlikely((next + pool->size) >= next_boundary)) { 216e34f44b3SMatthew Wilcox next = next_boundary; 217e34f44b3SMatthew Wilcox next_boundary += pool->boundary; 218e34f44b3SMatthew Wilcox } 219a35a3455SMatthew Wilcox *(int *)(page->vaddr + offset) = next; 220a35a3455SMatthew Wilcox offset = next; 221a35a3455SMatthew Wilcox } while (offset < pool->allocation); 222a35a3455SMatthew Wilcox } 223a35a3455SMatthew Wilcox 224e87aa773SMatthew Wilcox static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags) 225141e9d4bSMatthew Wilcox { 226141e9d4bSMatthew Wilcox struct dma_page *page; 227141e9d4bSMatthew Wilcox 228a35a3455SMatthew Wilcox page = kmalloc(sizeof(*page), mem_flags); 229141e9d4bSMatthew Wilcox if (!page) 230141e9d4bSMatthew Wilcox return NULL; 231a35a3455SMatthew Wilcox page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation, 232e87aa773SMatthew Wilcox &page->dma, mem_flags); 233141e9d4bSMatthew Wilcox if (page->vaddr) { 234b5ee5befSAndi Kleen #ifdef DMAPOOL_DEBUG 235141e9d4bSMatthew Wilcox memset(page->vaddr, POOL_POISON_FREED, pool->allocation); 236141e9d4bSMatthew Wilcox #endif 237a35a3455SMatthew Wilcox pool_initialise_page(pool, page); 238141e9d4bSMatthew Wilcox page->in_use = 0; 239a35a3455SMatthew Wilcox page->offset = 0; 240141e9d4bSMatthew Wilcox } else { 241141e9d4bSMatthew Wilcox kfree(page); 242141e9d4bSMatthew Wilcox page = NULL; 243141e9d4bSMatthew Wilcox } 244141e9d4bSMatthew Wilcox return page; 245141e9d4bSMatthew Wilcox } 246141e9d4bSMatthew Wilcox 247d9e7e37bSNicholas Krause static inline bool is_page_busy(struct dma_page *page) 248141e9d4bSMatthew Wilcox { 249a35a3455SMatthew Wilcox return page->in_use != 0; 250141e9d4bSMatthew Wilcox } 251141e9d4bSMatthew Wilcox 252e87aa773SMatthew Wilcox static void pool_free_page(struct dma_pool *pool, struct dma_page *page) 253141e9d4bSMatthew Wilcox { 254141e9d4bSMatthew Wilcox dma_addr_t dma = page->dma; 255141e9d4bSMatthew Wilcox 256b5ee5befSAndi Kleen #ifdef DMAPOOL_DEBUG 257141e9d4bSMatthew Wilcox memset(page->vaddr, POOL_POISON_FREED, pool->allocation); 258141e9d4bSMatthew Wilcox #endif 259141e9d4bSMatthew Wilcox dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma); 260141e9d4bSMatthew Wilcox list_del(&page->page_list); 261141e9d4bSMatthew Wilcox kfree(page); 262141e9d4bSMatthew Wilcox } 263141e9d4bSMatthew Wilcox 264141e9d4bSMatthew Wilcox /** 265141e9d4bSMatthew Wilcox * dma_pool_destroy - destroys a pool of dma memory blocks. 266141e9d4bSMatthew Wilcox * @pool: dma pool that will be destroyed 267141e9d4bSMatthew Wilcox * Context: !in_interrupt() 268141e9d4bSMatthew Wilcox * 269141e9d4bSMatthew Wilcox * Caller guarantees that no more memory from the pool is in use, 270141e9d4bSMatthew Wilcox * and that nothing will try to use the pool after this call. 271141e9d4bSMatthew Wilcox */ 272e87aa773SMatthew Wilcox void dma_pool_destroy(struct dma_pool *pool) 273141e9d4bSMatthew Wilcox { 27401c2965fSSebastian Andrzej Siewior bool empty = false; 27501c2965fSSebastian Andrzej Siewior 27644d7175dSSergey Senozhatsky if (unlikely(!pool)) 27744d7175dSSergey Senozhatsky return; 27844d7175dSSergey Senozhatsky 27901c2965fSSebastian Andrzej Siewior mutex_lock(&pools_reg_lock); 280141e9d4bSMatthew Wilcox mutex_lock(&pools_lock); 281141e9d4bSMatthew Wilcox list_del(&pool->pools); 282141e9d4bSMatthew Wilcox if (pool->dev && list_empty(&pool->dev->dma_pools)) 28301c2965fSSebastian Andrzej Siewior empty = true; 284141e9d4bSMatthew Wilcox mutex_unlock(&pools_lock); 28501c2965fSSebastian Andrzej Siewior if (empty) 28601c2965fSSebastian Andrzej Siewior device_remove_file(pool->dev, &dev_attr_pools); 28701c2965fSSebastian Andrzej Siewior mutex_unlock(&pools_reg_lock); 288141e9d4bSMatthew Wilcox 289141e9d4bSMatthew Wilcox while (!list_empty(&pool->page_list)) { 290141e9d4bSMatthew Wilcox struct dma_page *page; 291141e9d4bSMatthew Wilcox page = list_entry(pool->page_list.next, 292141e9d4bSMatthew Wilcox struct dma_page, page_list); 293a35a3455SMatthew Wilcox if (is_page_busy(page)) { 294141e9d4bSMatthew Wilcox if (pool->dev) 295e87aa773SMatthew Wilcox dev_err(pool->dev, 296e87aa773SMatthew Wilcox "dma_pool_destroy %s, %p busy\n", 297141e9d4bSMatthew Wilcox pool->name, page->vaddr); 298141e9d4bSMatthew Wilcox else 2991170532bSJoe Perches pr_err("dma_pool_destroy %s, %p busy\n", 300141e9d4bSMatthew Wilcox pool->name, page->vaddr); 301141e9d4bSMatthew Wilcox /* leak the still-in-use consistent memory */ 302141e9d4bSMatthew Wilcox list_del(&page->page_list); 303141e9d4bSMatthew Wilcox kfree(page); 304141e9d4bSMatthew Wilcox } else 305141e9d4bSMatthew Wilcox pool_free_page(pool, page); 306141e9d4bSMatthew Wilcox } 307141e9d4bSMatthew Wilcox 308141e9d4bSMatthew Wilcox kfree(pool); 309141e9d4bSMatthew Wilcox } 310e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_destroy); 311141e9d4bSMatthew Wilcox 312141e9d4bSMatthew Wilcox /** 313141e9d4bSMatthew Wilcox * dma_pool_alloc - get a block of consistent memory 314141e9d4bSMatthew Wilcox * @pool: dma pool that will produce the block 315141e9d4bSMatthew Wilcox * @mem_flags: GFP_* bitmask 316141e9d4bSMatthew Wilcox * @handle: pointer to dma address of block 317141e9d4bSMatthew Wilcox * 318*a862f68aSMike Rapoport * Return: the kernel virtual address of a currently unused block, 319141e9d4bSMatthew Wilcox * and reports its dma address through the handle. 3206182a094SMatthew Wilcox * If such a memory block can't be allocated, %NULL is returned. 321141e9d4bSMatthew Wilcox */ 322e87aa773SMatthew Wilcox void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags, 323e87aa773SMatthew Wilcox dma_addr_t *handle) 324141e9d4bSMatthew Wilcox { 325141e9d4bSMatthew Wilcox unsigned long flags; 326141e9d4bSMatthew Wilcox struct dma_page *page; 327141e9d4bSMatthew Wilcox size_t offset; 328141e9d4bSMatthew Wilcox void *retval; 329141e9d4bSMatthew Wilcox 330d0164adcSMel Gorman might_sleep_if(gfpflags_allow_blocking(mem_flags)); 331ea05c844SDima Zavin 332141e9d4bSMatthew Wilcox spin_lock_irqsave(&pool->lock, flags); 333141e9d4bSMatthew Wilcox list_for_each_entry(page, &pool->page_list, page_list) { 334a35a3455SMatthew Wilcox if (page->offset < pool->allocation) 335141e9d4bSMatthew Wilcox goto ready; 336141e9d4bSMatthew Wilcox } 337141e9d4bSMatthew Wilcox 338387870f2SMarek Szyprowski /* pool_alloc_page() might sleep, so temporarily drop &pool->lock */ 339141e9d4bSMatthew Wilcox spin_unlock_irqrestore(&pool->lock, flags); 340141e9d4bSMatthew Wilcox 341fa23f56dSSean O. Stalley page = pool_alloc_page(pool, mem_flags & (~__GFP_ZERO)); 342387870f2SMarek Szyprowski if (!page) 343387870f2SMarek Szyprowski return NULL; 344141e9d4bSMatthew Wilcox 3452cae367eSMatthew Wilcox spin_lock_irqsave(&pool->lock, flags); 346141e9d4bSMatthew Wilcox 347387870f2SMarek Szyprowski list_add(&page->page_list, &pool->page_list); 348141e9d4bSMatthew Wilcox ready: 349141e9d4bSMatthew Wilcox page->in_use++; 350a35a3455SMatthew Wilcox offset = page->offset; 351a35a3455SMatthew Wilcox page->offset = *(int *)(page->vaddr + offset); 352141e9d4bSMatthew Wilcox retval = offset + page->vaddr; 353141e9d4bSMatthew Wilcox *handle = offset + page->dma; 354b5ee5befSAndi Kleen #ifdef DMAPOOL_DEBUG 3555de55b26SMatthieu CASTET { 3565de55b26SMatthieu CASTET int i; 3575de55b26SMatthieu CASTET u8 *data = retval; 3585de55b26SMatthieu CASTET /* page->offset is stored in first 4 bytes */ 3595de55b26SMatthieu CASTET for (i = sizeof(page->offset); i < pool->size; i++) { 3605de55b26SMatthieu CASTET if (data[i] == POOL_POISON_FREED) 3615de55b26SMatthieu CASTET continue; 3625de55b26SMatthieu CASTET if (pool->dev) 3635de55b26SMatthieu CASTET dev_err(pool->dev, 3645835f251SHiroshige Sato "dma_pool_alloc %s, %p (corrupted)\n", 3655de55b26SMatthieu CASTET pool->name, retval); 3665de55b26SMatthieu CASTET else 3675835f251SHiroshige Sato pr_err("dma_pool_alloc %s, %p (corrupted)\n", 3685de55b26SMatthieu CASTET pool->name, retval); 3695de55b26SMatthieu CASTET 3705de55b26SMatthieu CASTET /* 3715de55b26SMatthieu CASTET * Dump the first 4 bytes even if they are not 3725de55b26SMatthieu CASTET * POOL_POISON_FREED 3735de55b26SMatthieu CASTET */ 3745de55b26SMatthieu CASTET print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, 3755de55b26SMatthieu CASTET data, pool->size, 1); 3765de55b26SMatthieu CASTET break; 3775de55b26SMatthieu CASTET } 3785de55b26SMatthieu CASTET } 379fa23f56dSSean O. Stalley if (!(mem_flags & __GFP_ZERO)) 380141e9d4bSMatthew Wilcox memset(retval, POOL_POISON_ALLOCATED, pool->size); 381141e9d4bSMatthew Wilcox #endif 382141e9d4bSMatthew Wilcox spin_unlock_irqrestore(&pool->lock, flags); 383fa23f56dSSean O. Stalley 384fa23f56dSSean O. Stalley if (mem_flags & __GFP_ZERO) 385fa23f56dSSean O. Stalley memset(retval, 0, pool->size); 386fa23f56dSSean O. Stalley 387141e9d4bSMatthew Wilcox return retval; 388141e9d4bSMatthew Wilcox } 389e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_alloc); 390141e9d4bSMatthew Wilcox 391e87aa773SMatthew Wilcox static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma) 392141e9d4bSMatthew Wilcox { 393141e9d4bSMatthew Wilcox struct dma_page *page; 394141e9d4bSMatthew Wilcox 395141e9d4bSMatthew Wilcox list_for_each_entry(page, &pool->page_list, page_list) { 396141e9d4bSMatthew Wilcox if (dma < page->dma) 397141e9d4bSMatthew Wilcox continue; 398676bd991SRobin Murphy if ((dma - page->dma) < pool->allocation) 399141e9d4bSMatthew Wilcox return page; 400141e9d4bSMatthew Wilcox } 40184bc227dSRolf Eike Beer return NULL; 40284bc227dSRolf Eike Beer } 403141e9d4bSMatthew Wilcox 404141e9d4bSMatthew Wilcox /** 405141e9d4bSMatthew Wilcox * dma_pool_free - put block back into dma pool 406141e9d4bSMatthew Wilcox * @pool: the dma pool holding the block 407141e9d4bSMatthew Wilcox * @vaddr: virtual address of block 408141e9d4bSMatthew Wilcox * @dma: dma address of block 409141e9d4bSMatthew Wilcox * 410141e9d4bSMatthew Wilcox * Caller promises neither device nor driver will again touch this block 411141e9d4bSMatthew Wilcox * unless it is first re-allocated. 412141e9d4bSMatthew Wilcox */ 413e87aa773SMatthew Wilcox void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma) 414141e9d4bSMatthew Wilcox { 415141e9d4bSMatthew Wilcox struct dma_page *page; 416141e9d4bSMatthew Wilcox unsigned long flags; 417a35a3455SMatthew Wilcox unsigned int offset; 418141e9d4bSMatthew Wilcox 41984bc227dSRolf Eike Beer spin_lock_irqsave(&pool->lock, flags); 420e87aa773SMatthew Wilcox page = pool_find_page(pool, dma); 421e87aa773SMatthew Wilcox if (!page) { 42284bc227dSRolf Eike Beer spin_unlock_irqrestore(&pool->lock, flags); 423141e9d4bSMatthew Wilcox if (pool->dev) 424e87aa773SMatthew Wilcox dev_err(pool->dev, 425e87aa773SMatthew Wilcox "dma_pool_free %s, %p/%lx (bad dma)\n", 426141e9d4bSMatthew Wilcox pool->name, vaddr, (unsigned long)dma); 427141e9d4bSMatthew Wilcox else 4281170532bSJoe Perches pr_err("dma_pool_free %s, %p/%lx (bad dma)\n", 429141e9d4bSMatthew Wilcox pool->name, vaddr, (unsigned long)dma); 430141e9d4bSMatthew Wilcox return; 431141e9d4bSMatthew Wilcox } 432141e9d4bSMatthew Wilcox 433a35a3455SMatthew Wilcox offset = vaddr - page->vaddr; 434b5ee5befSAndi Kleen #ifdef DMAPOOL_DEBUG 435a35a3455SMatthew Wilcox if ((dma - page->dma) != offset) { 43684bc227dSRolf Eike Beer spin_unlock_irqrestore(&pool->lock, flags); 437141e9d4bSMatthew Wilcox if (pool->dev) 438e87aa773SMatthew Wilcox dev_err(pool->dev, 439199eaa05SMiles Chen "dma_pool_free %s, %p (bad vaddr)/%pad\n", 440199eaa05SMiles Chen pool->name, vaddr, &dma); 441141e9d4bSMatthew Wilcox else 442199eaa05SMiles Chen pr_err("dma_pool_free %s, %p (bad vaddr)/%pad\n", 443199eaa05SMiles Chen pool->name, vaddr, &dma); 444141e9d4bSMatthew Wilcox return; 445141e9d4bSMatthew Wilcox } 446a35a3455SMatthew Wilcox { 447a35a3455SMatthew Wilcox unsigned int chain = page->offset; 448a35a3455SMatthew Wilcox while (chain < pool->allocation) { 449a35a3455SMatthew Wilcox if (chain != offset) { 450a35a3455SMatthew Wilcox chain = *(int *)(page->vaddr + chain); 451a35a3455SMatthew Wilcox continue; 452a35a3455SMatthew Wilcox } 45384bc227dSRolf Eike Beer spin_unlock_irqrestore(&pool->lock, flags); 454141e9d4bSMatthew Wilcox if (pool->dev) 455199eaa05SMiles Chen dev_err(pool->dev, "dma_pool_free %s, dma %pad already free\n", 456199eaa05SMiles Chen pool->name, &dma); 457141e9d4bSMatthew Wilcox else 458199eaa05SMiles Chen pr_err("dma_pool_free %s, dma %pad already free\n", 459199eaa05SMiles Chen pool->name, &dma); 460141e9d4bSMatthew Wilcox return; 461141e9d4bSMatthew Wilcox } 462a35a3455SMatthew Wilcox } 463141e9d4bSMatthew Wilcox memset(vaddr, POOL_POISON_FREED, pool->size); 464141e9d4bSMatthew Wilcox #endif 465141e9d4bSMatthew Wilcox 466141e9d4bSMatthew Wilcox page->in_use--; 467a35a3455SMatthew Wilcox *(int *)vaddr = page->offset; 468a35a3455SMatthew Wilcox page->offset = offset; 469141e9d4bSMatthew Wilcox /* 470141e9d4bSMatthew Wilcox * Resist a temptation to do 471a35a3455SMatthew Wilcox * if (!is_page_busy(page)) pool_free_page(pool, page); 472141e9d4bSMatthew Wilcox * Better have a few empty pages hang around. 473141e9d4bSMatthew Wilcox */ 474141e9d4bSMatthew Wilcox spin_unlock_irqrestore(&pool->lock, flags); 475141e9d4bSMatthew Wilcox } 476e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_free); 477141e9d4bSMatthew Wilcox 478141e9d4bSMatthew Wilcox /* 479141e9d4bSMatthew Wilcox * Managed DMA pool 480141e9d4bSMatthew Wilcox */ 481141e9d4bSMatthew Wilcox static void dmam_pool_release(struct device *dev, void *res) 482141e9d4bSMatthew Wilcox { 483141e9d4bSMatthew Wilcox struct dma_pool *pool = *(struct dma_pool **)res; 484141e9d4bSMatthew Wilcox 485141e9d4bSMatthew Wilcox dma_pool_destroy(pool); 486141e9d4bSMatthew Wilcox } 487141e9d4bSMatthew Wilcox 488141e9d4bSMatthew Wilcox static int dmam_pool_match(struct device *dev, void *res, void *match_data) 489141e9d4bSMatthew Wilcox { 490141e9d4bSMatthew Wilcox return *(struct dma_pool **)res == match_data; 491141e9d4bSMatthew Wilcox } 492141e9d4bSMatthew Wilcox 493141e9d4bSMatthew Wilcox /** 494141e9d4bSMatthew Wilcox * dmam_pool_create - Managed dma_pool_create() 495141e9d4bSMatthew Wilcox * @name: name of pool, for diagnostics 496141e9d4bSMatthew Wilcox * @dev: device that will be doing the DMA 497141e9d4bSMatthew Wilcox * @size: size of the blocks in this pool. 498141e9d4bSMatthew Wilcox * @align: alignment requirement for blocks; must be a power of two 499141e9d4bSMatthew Wilcox * @allocation: returned blocks won't cross this boundary (or zero) 500141e9d4bSMatthew Wilcox * 501141e9d4bSMatthew Wilcox * Managed dma_pool_create(). DMA pool created with this function is 502141e9d4bSMatthew Wilcox * automatically destroyed on driver detach. 503*a862f68aSMike Rapoport * 504*a862f68aSMike Rapoport * Return: a managed dma allocation pool with the requested 505*a862f68aSMike Rapoport * characteristics, or %NULL if one can't be created. 506141e9d4bSMatthew Wilcox */ 507141e9d4bSMatthew Wilcox struct dma_pool *dmam_pool_create(const char *name, struct device *dev, 508141e9d4bSMatthew Wilcox size_t size, size_t align, size_t allocation) 509141e9d4bSMatthew Wilcox { 510141e9d4bSMatthew Wilcox struct dma_pool **ptr, *pool; 511141e9d4bSMatthew Wilcox 512141e9d4bSMatthew Wilcox ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL); 513141e9d4bSMatthew Wilcox if (!ptr) 514141e9d4bSMatthew Wilcox return NULL; 515141e9d4bSMatthew Wilcox 516141e9d4bSMatthew Wilcox pool = *ptr = dma_pool_create(name, dev, size, align, allocation); 517141e9d4bSMatthew Wilcox if (pool) 518141e9d4bSMatthew Wilcox devres_add(dev, ptr); 519141e9d4bSMatthew Wilcox else 520141e9d4bSMatthew Wilcox devres_free(ptr); 521141e9d4bSMatthew Wilcox 522141e9d4bSMatthew Wilcox return pool; 523141e9d4bSMatthew Wilcox } 524e87aa773SMatthew Wilcox EXPORT_SYMBOL(dmam_pool_create); 525141e9d4bSMatthew Wilcox 526141e9d4bSMatthew Wilcox /** 527141e9d4bSMatthew Wilcox * dmam_pool_destroy - Managed dma_pool_destroy() 528141e9d4bSMatthew Wilcox * @pool: dma pool that will be destroyed 529141e9d4bSMatthew Wilcox * 530141e9d4bSMatthew Wilcox * Managed dma_pool_destroy(). 531141e9d4bSMatthew Wilcox */ 532141e9d4bSMatthew Wilcox void dmam_pool_destroy(struct dma_pool *pool) 533141e9d4bSMatthew Wilcox { 534141e9d4bSMatthew Wilcox struct device *dev = pool->dev; 535141e9d4bSMatthew Wilcox 536172cb4b3SAndy Shevchenko WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool)); 537141e9d4bSMatthew Wilcox } 538141e9d4bSMatthew Wilcox EXPORT_SYMBOL(dmam_pool_destroy); 539