1b2139ce0SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 26182a094SMatthew Wilcox /* 36182a094SMatthew Wilcox * DMA Pool allocator 46182a094SMatthew Wilcox * 56182a094SMatthew Wilcox * Copyright 2001 David Brownell 66182a094SMatthew Wilcox * Copyright 2007 Intel Corporation 76182a094SMatthew Wilcox * Author: Matthew Wilcox <willy@linux.intel.com> 86182a094SMatthew Wilcox * 96182a094SMatthew Wilcox * This allocator returns small blocks of a given size which are DMA-able by 106182a094SMatthew Wilcox * the given device. It uses the dma_alloc_coherent page allocator to get 116182a094SMatthew Wilcox * new pages, then splits them up into blocks of the required size. 126182a094SMatthew Wilcox * Many older drivers still have their own code to do this. 136182a094SMatthew Wilcox * 146182a094SMatthew Wilcox * The current design of this allocator is fairly simple. The pool is 156182a094SMatthew Wilcox * represented by the 'struct dma_pool' which keeps a doubly-linked list of 166182a094SMatthew Wilcox * allocated pages. Each page in the page_list is split into blocks of at 17a35a3455SMatthew Wilcox * least 'size' bytes. Free blocks are tracked in an unsorted singly-linked 18a35a3455SMatthew Wilcox * list of free blocks within the page. Used blocks aren't tracked, but we 19a35a3455SMatthew Wilcox * keep a count of how many are currently allocated from each page. 206182a094SMatthew Wilcox */ 21141e9d4bSMatthew Wilcox 22141e9d4bSMatthew Wilcox #include <linux/device.h> 23141e9d4bSMatthew Wilcox #include <linux/dma-mapping.h> 24141e9d4bSMatthew Wilcox #include <linux/dmapool.h> 256182a094SMatthew Wilcox #include <linux/kernel.h> 266182a094SMatthew Wilcox #include <linux/list.h> 27b95f1b31SPaul Gortmaker #include <linux/export.h> 286182a094SMatthew Wilcox #include <linux/mutex.h> 29141e9d4bSMatthew Wilcox #include <linux/poison.h> 30141e9d4bSMatthew Wilcox #include <linux/sched.h> 310f2f89b6SDaniel Vetter #include <linux/sched/mm.h> 326182a094SMatthew Wilcox #include <linux/slab.h> 337c77509cSPaul Gortmaker #include <linux/stat.h> 346182a094SMatthew Wilcox #include <linux/spinlock.h> 356182a094SMatthew Wilcox #include <linux/string.h> 366182a094SMatthew Wilcox #include <linux/types.h> 376182a094SMatthew Wilcox #include <linux/wait.h> 38141e9d4bSMatthew Wilcox 39b5ee5befSAndi Kleen #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON) 40b5ee5befSAndi Kleen #define DMAPOOL_DEBUG 1 41b5ee5befSAndi Kleen #endif 42b5ee5befSAndi Kleen 43141e9d4bSMatthew Wilcox struct dma_pool { /* the pool */ 44141e9d4bSMatthew Wilcox struct list_head page_list; 45141e9d4bSMatthew Wilcox spinlock_t lock; 46141e9d4bSMatthew Wilcox size_t size; 47141e9d4bSMatthew Wilcox struct device *dev; 48141e9d4bSMatthew Wilcox size_t allocation; 49e34f44b3SMatthew Wilcox size_t boundary; 50141e9d4bSMatthew Wilcox char name[32]; 51141e9d4bSMatthew Wilcox struct list_head pools; 52141e9d4bSMatthew Wilcox }; 53141e9d4bSMatthew Wilcox 54141e9d4bSMatthew Wilcox struct dma_page { /* cacheable header for 'allocation' bytes */ 55141e9d4bSMatthew Wilcox struct list_head page_list; 56141e9d4bSMatthew Wilcox void *vaddr; 57141e9d4bSMatthew Wilcox dma_addr_t dma; 58a35a3455SMatthew Wilcox unsigned int in_use; 59a35a3455SMatthew Wilcox unsigned int offset; 60141e9d4bSMatthew Wilcox }; 61141e9d4bSMatthew Wilcox 62141e9d4bSMatthew Wilcox static DEFINE_MUTEX(pools_lock); 6301c2965fSSebastian Andrzej Siewior static DEFINE_MUTEX(pools_reg_lock); 64141e9d4bSMatthew Wilcox 65e8df2c70SYueHaibing static ssize_t pools_show(struct device *dev, struct device_attribute *attr, char *buf) 66141e9d4bSMatthew Wilcox { 67*08cc96c8STony Battersby int size; 68141e9d4bSMatthew Wilcox struct dma_page *page; 69141e9d4bSMatthew Wilcox struct dma_pool *pool; 70141e9d4bSMatthew Wilcox 71*08cc96c8STony Battersby size = sysfs_emit(buf, "poolinfo - 0.1\n"); 72141e9d4bSMatthew Wilcox 73141e9d4bSMatthew Wilcox mutex_lock(&pools_lock); 74141e9d4bSMatthew Wilcox list_for_each_entry(pool, &dev->dma_pools, pools) { 75141e9d4bSMatthew Wilcox unsigned pages = 0; 76141e9d4bSMatthew Wilcox unsigned blocks = 0; 77141e9d4bSMatthew Wilcox 78c4956823SThomas Gleixner spin_lock_irq(&pool->lock); 79141e9d4bSMatthew Wilcox list_for_each_entry(page, &pool->page_list, page_list) { 80141e9d4bSMatthew Wilcox pages++; 81141e9d4bSMatthew Wilcox blocks += page->in_use; 82141e9d4bSMatthew Wilcox } 83c4956823SThomas Gleixner spin_unlock_irq(&pool->lock); 84141e9d4bSMatthew Wilcox 85141e9d4bSMatthew Wilcox /* per-pool info, no real statistics yet */ 86*08cc96c8STony Battersby size += sysfs_emit_at(buf, size, "%-16s %4u %4zu %4zu %2u\n", 87a35a3455SMatthew Wilcox pool->name, blocks, 88a35a3455SMatthew Wilcox pages * (pool->allocation / pool->size), 89141e9d4bSMatthew Wilcox pool->size, pages); 90141e9d4bSMatthew Wilcox } 91141e9d4bSMatthew Wilcox mutex_unlock(&pools_lock); 92141e9d4bSMatthew Wilcox 93*08cc96c8STony Battersby return size; 94141e9d4bSMatthew Wilcox } 95e87aa773SMatthew Wilcox 96e8df2c70SYueHaibing static DEVICE_ATTR_RO(pools); 97141e9d4bSMatthew Wilcox 98141e9d4bSMatthew Wilcox /** 99141e9d4bSMatthew Wilcox * dma_pool_create - Creates a pool of consistent memory blocks, for dma. 100141e9d4bSMatthew Wilcox * @name: name of pool, for diagnostics 101141e9d4bSMatthew Wilcox * @dev: device that will be doing the DMA 102141e9d4bSMatthew Wilcox * @size: size of the blocks in this pool. 103141e9d4bSMatthew Wilcox * @align: alignment requirement for blocks; must be a power of two 104e34f44b3SMatthew Wilcox * @boundary: returned blocks won't cross this power of two boundary 105a862f68aSMike Rapoport * Context: not in_interrupt() 106141e9d4bSMatthew Wilcox * 107a862f68aSMike Rapoport * Given one of these pools, dma_pool_alloc() 108141e9d4bSMatthew Wilcox * may be used to allocate memory. Such memory will all have "consistent" 109141e9d4bSMatthew Wilcox * DMA mappings, accessible by the device and its driver without using 110141e9d4bSMatthew Wilcox * cache flushing primitives. The actual size of blocks allocated may be 111141e9d4bSMatthew Wilcox * larger than requested because of alignment. 112141e9d4bSMatthew Wilcox * 113e34f44b3SMatthew Wilcox * If @boundary is nonzero, objects returned from dma_pool_alloc() won't 114141e9d4bSMatthew Wilcox * cross that size boundary. This is useful for devices which have 115141e9d4bSMatthew Wilcox * addressing restrictions on individual DMA transfers, such as not crossing 116141e9d4bSMatthew Wilcox * boundaries of 4KBytes. 117a862f68aSMike Rapoport * 118a862f68aSMike Rapoport * Return: a dma allocation pool with the requested characteristics, or 119a862f68aSMike Rapoport * %NULL if one can't be created. 120141e9d4bSMatthew Wilcox */ 121e87aa773SMatthew Wilcox struct dma_pool *dma_pool_create(const char *name, struct device *dev, 122e34f44b3SMatthew Wilcox size_t size, size_t align, size_t boundary) 123141e9d4bSMatthew Wilcox { 124141e9d4bSMatthew Wilcox struct dma_pool *retval; 125e34f44b3SMatthew Wilcox size_t allocation; 12601c2965fSSebastian Andrzej Siewior bool empty = false; 127141e9d4bSMatthew Wilcox 12867a540c6STony Battersby if (!dev) 12967a540c6STony Battersby return NULL; 13067a540c6STony Battersby 131baa2ef83SPaul McQuade if (align == 0) 132141e9d4bSMatthew Wilcox align = 1; 133baa2ef83SPaul McQuade else if (align & (align - 1)) 134399154beSMatthew Wilcox return NULL; 135399154beSMatthew Wilcox 136baa2ef83SPaul McQuade if (size == 0) 137141e9d4bSMatthew Wilcox return NULL; 138baa2ef83SPaul McQuade else if (size < 4) 139a35a3455SMatthew Wilcox size = 4; 140399154beSMatthew Wilcox 141399154beSMatthew Wilcox size = ALIGN(size, align); 142e34f44b3SMatthew Wilcox allocation = max_t(size_t, size, PAGE_SIZE); 143141e9d4bSMatthew Wilcox 144baa2ef83SPaul McQuade if (!boundary) 145e34f44b3SMatthew Wilcox boundary = allocation; 146baa2ef83SPaul McQuade else if ((boundary < size) || (boundary & (boundary - 1))) 147e34f44b3SMatthew Wilcox return NULL; 148e34f44b3SMatthew Wilcox 149cc6266f0SChristian König retval = kmalloc(sizeof(*retval), GFP_KERNEL); 150e34f44b3SMatthew Wilcox if (!retval) 151141e9d4bSMatthew Wilcox return retval; 152141e9d4bSMatthew Wilcox 153943f229eSZhiyuan Dai strscpy(retval->name, name, sizeof(retval->name)); 154141e9d4bSMatthew Wilcox 155141e9d4bSMatthew Wilcox retval->dev = dev; 156141e9d4bSMatthew Wilcox 157141e9d4bSMatthew Wilcox INIT_LIST_HEAD(&retval->page_list); 158141e9d4bSMatthew Wilcox spin_lock_init(&retval->lock); 159141e9d4bSMatthew Wilcox retval->size = size; 160e34f44b3SMatthew Wilcox retval->boundary = boundary; 161141e9d4bSMatthew Wilcox retval->allocation = allocation; 162141e9d4bSMatthew Wilcox 163cc6b664aSDaeseok Youn INIT_LIST_HEAD(&retval->pools); 164141e9d4bSMatthew Wilcox 16501c2965fSSebastian Andrzej Siewior /* 16601c2965fSSebastian Andrzej Siewior * pools_lock ensures that the ->dma_pools list does not get corrupted. 16701c2965fSSebastian Andrzej Siewior * pools_reg_lock ensures that there is not a race between 16801c2965fSSebastian Andrzej Siewior * dma_pool_create() and dma_pool_destroy() or within dma_pool_create() 16901c2965fSSebastian Andrzej Siewior * when the first invocation of dma_pool_create() failed on 17001c2965fSSebastian Andrzej Siewior * device_create_file() and the second assumes that it has been done (I 17101c2965fSSebastian Andrzej Siewior * know it is a short window). 17201c2965fSSebastian Andrzej Siewior */ 17301c2965fSSebastian Andrzej Siewior mutex_lock(&pools_reg_lock); 174141e9d4bSMatthew Wilcox mutex_lock(&pools_lock); 17501c2965fSSebastian Andrzej Siewior if (list_empty(&dev->dma_pools)) 17601c2965fSSebastian Andrzej Siewior empty = true; 177cc6b664aSDaeseok Youn list_add(&retval->pools, &dev->dma_pools); 178cc6b664aSDaeseok Youn mutex_unlock(&pools_lock); 17901c2965fSSebastian Andrzej Siewior if (empty) { 18001c2965fSSebastian Andrzej Siewior int err; 181141e9d4bSMatthew Wilcox 18201c2965fSSebastian Andrzej Siewior err = device_create_file(dev, &dev_attr_pools); 18301c2965fSSebastian Andrzej Siewior if (err) { 18401c2965fSSebastian Andrzej Siewior mutex_lock(&pools_lock); 18501c2965fSSebastian Andrzej Siewior list_del(&retval->pools); 18601c2965fSSebastian Andrzej Siewior mutex_unlock(&pools_lock); 18701c2965fSSebastian Andrzej Siewior mutex_unlock(&pools_reg_lock); 18801c2965fSSebastian Andrzej Siewior kfree(retval); 18901c2965fSSebastian Andrzej Siewior return NULL; 19001c2965fSSebastian Andrzej Siewior } 19101c2965fSSebastian Andrzej Siewior } 19201c2965fSSebastian Andrzej Siewior mutex_unlock(&pools_reg_lock); 193141e9d4bSMatthew Wilcox return retval; 194141e9d4bSMatthew Wilcox } 195e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_create); 196141e9d4bSMatthew Wilcox 197a35a3455SMatthew Wilcox static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page) 198a35a3455SMatthew Wilcox { 199a35a3455SMatthew Wilcox unsigned int offset = 0; 200e34f44b3SMatthew Wilcox unsigned int next_boundary = pool->boundary; 201a35a3455SMatthew Wilcox 202a35a3455SMatthew Wilcox do { 203a35a3455SMatthew Wilcox unsigned int next = offset + pool->size; 204e34f44b3SMatthew Wilcox if (unlikely((next + pool->size) >= next_boundary)) { 205e34f44b3SMatthew Wilcox next = next_boundary; 206e34f44b3SMatthew Wilcox next_boundary += pool->boundary; 207e34f44b3SMatthew Wilcox } 208a35a3455SMatthew Wilcox *(int *)(page->vaddr + offset) = next; 209a35a3455SMatthew Wilcox offset = next; 210a35a3455SMatthew Wilcox } while (offset < pool->allocation); 211a35a3455SMatthew Wilcox } 212a35a3455SMatthew Wilcox 213e87aa773SMatthew Wilcox static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags) 214141e9d4bSMatthew Wilcox { 215141e9d4bSMatthew Wilcox struct dma_page *page; 216141e9d4bSMatthew Wilcox 217a35a3455SMatthew Wilcox page = kmalloc(sizeof(*page), mem_flags); 218141e9d4bSMatthew Wilcox if (!page) 219141e9d4bSMatthew Wilcox return NULL; 220a35a3455SMatthew Wilcox page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation, 221e87aa773SMatthew Wilcox &page->dma, mem_flags); 222141e9d4bSMatthew Wilcox if (page->vaddr) { 223b5ee5befSAndi Kleen #ifdef DMAPOOL_DEBUG 224141e9d4bSMatthew Wilcox memset(page->vaddr, POOL_POISON_FREED, pool->allocation); 225141e9d4bSMatthew Wilcox #endif 226a35a3455SMatthew Wilcox pool_initialise_page(pool, page); 227141e9d4bSMatthew Wilcox page->in_use = 0; 228a35a3455SMatthew Wilcox page->offset = 0; 229141e9d4bSMatthew Wilcox } else { 230141e9d4bSMatthew Wilcox kfree(page); 231141e9d4bSMatthew Wilcox page = NULL; 232141e9d4bSMatthew Wilcox } 233141e9d4bSMatthew Wilcox return page; 234141e9d4bSMatthew Wilcox } 235141e9d4bSMatthew Wilcox 236d9e7e37bSNicholas Krause static inline bool is_page_busy(struct dma_page *page) 237141e9d4bSMatthew Wilcox { 238a35a3455SMatthew Wilcox return page->in_use != 0; 239141e9d4bSMatthew Wilcox } 240141e9d4bSMatthew Wilcox 241e87aa773SMatthew Wilcox static void pool_free_page(struct dma_pool *pool, struct dma_page *page) 242141e9d4bSMatthew Wilcox { 243141e9d4bSMatthew Wilcox dma_addr_t dma = page->dma; 244141e9d4bSMatthew Wilcox 245b5ee5befSAndi Kleen #ifdef DMAPOOL_DEBUG 246141e9d4bSMatthew Wilcox memset(page->vaddr, POOL_POISON_FREED, pool->allocation); 247141e9d4bSMatthew Wilcox #endif 248141e9d4bSMatthew Wilcox dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma); 249141e9d4bSMatthew Wilcox list_del(&page->page_list); 250141e9d4bSMatthew Wilcox kfree(page); 251141e9d4bSMatthew Wilcox } 252141e9d4bSMatthew Wilcox 253141e9d4bSMatthew Wilcox /** 254141e9d4bSMatthew Wilcox * dma_pool_destroy - destroys a pool of dma memory blocks. 255141e9d4bSMatthew Wilcox * @pool: dma pool that will be destroyed 256141e9d4bSMatthew Wilcox * Context: !in_interrupt() 257141e9d4bSMatthew Wilcox * 258141e9d4bSMatthew Wilcox * Caller guarantees that no more memory from the pool is in use, 259141e9d4bSMatthew Wilcox * and that nothing will try to use the pool after this call. 260141e9d4bSMatthew Wilcox */ 261e87aa773SMatthew Wilcox void dma_pool_destroy(struct dma_pool *pool) 262141e9d4bSMatthew Wilcox { 26342286f83SAndy Shevchenko struct dma_page *page, *tmp; 26401c2965fSSebastian Andrzej Siewior bool empty = false; 26501c2965fSSebastian Andrzej Siewior 26644d7175dSSergey Senozhatsky if (unlikely(!pool)) 26744d7175dSSergey Senozhatsky return; 26844d7175dSSergey Senozhatsky 26901c2965fSSebastian Andrzej Siewior mutex_lock(&pools_reg_lock); 270141e9d4bSMatthew Wilcox mutex_lock(&pools_lock); 271141e9d4bSMatthew Wilcox list_del(&pool->pools); 27267a540c6STony Battersby if (list_empty(&pool->dev->dma_pools)) 27301c2965fSSebastian Andrzej Siewior empty = true; 274141e9d4bSMatthew Wilcox mutex_unlock(&pools_lock); 27501c2965fSSebastian Andrzej Siewior if (empty) 27601c2965fSSebastian Andrzej Siewior device_remove_file(pool->dev, &dev_attr_pools); 27701c2965fSSebastian Andrzej Siewior mutex_unlock(&pools_reg_lock); 278141e9d4bSMatthew Wilcox 27942286f83SAndy Shevchenko list_for_each_entry_safe(page, tmp, &pool->page_list, page_list) { 280a35a3455SMatthew Wilcox if (is_page_busy(page)) { 28141a04814SAndy Shevchenko dev_err(pool->dev, "%s %s, %p busy\n", __func__, 282141e9d4bSMatthew Wilcox pool->name, page->vaddr); 283141e9d4bSMatthew Wilcox /* leak the still-in-use consistent memory */ 284141e9d4bSMatthew Wilcox list_del(&page->page_list); 285141e9d4bSMatthew Wilcox kfree(page); 286141e9d4bSMatthew Wilcox } else 287141e9d4bSMatthew Wilcox pool_free_page(pool, page); 288141e9d4bSMatthew Wilcox } 289141e9d4bSMatthew Wilcox 290141e9d4bSMatthew Wilcox kfree(pool); 291141e9d4bSMatthew Wilcox } 292e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_destroy); 293141e9d4bSMatthew Wilcox 294141e9d4bSMatthew Wilcox /** 295141e9d4bSMatthew Wilcox * dma_pool_alloc - get a block of consistent memory 296141e9d4bSMatthew Wilcox * @pool: dma pool that will produce the block 297141e9d4bSMatthew Wilcox * @mem_flags: GFP_* bitmask 298141e9d4bSMatthew Wilcox * @handle: pointer to dma address of block 299141e9d4bSMatthew Wilcox * 300a862f68aSMike Rapoport * Return: the kernel virtual address of a currently unused block, 301141e9d4bSMatthew Wilcox * and reports its dma address through the handle. 3026182a094SMatthew Wilcox * If such a memory block can't be allocated, %NULL is returned. 303141e9d4bSMatthew Wilcox */ 304e87aa773SMatthew Wilcox void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags, 305e87aa773SMatthew Wilcox dma_addr_t *handle) 306141e9d4bSMatthew Wilcox { 307141e9d4bSMatthew Wilcox unsigned long flags; 308141e9d4bSMatthew Wilcox struct dma_page *page; 309141e9d4bSMatthew Wilcox size_t offset; 310141e9d4bSMatthew Wilcox void *retval; 311141e9d4bSMatthew Wilcox 3120f2f89b6SDaniel Vetter might_alloc(mem_flags); 313ea05c844SDima Zavin 314141e9d4bSMatthew Wilcox spin_lock_irqsave(&pool->lock, flags); 315141e9d4bSMatthew Wilcox list_for_each_entry(page, &pool->page_list, page_list) { 316a35a3455SMatthew Wilcox if (page->offset < pool->allocation) 317141e9d4bSMatthew Wilcox goto ready; 318141e9d4bSMatthew Wilcox } 319141e9d4bSMatthew Wilcox 320387870f2SMarek Szyprowski /* pool_alloc_page() might sleep, so temporarily drop &pool->lock */ 321141e9d4bSMatthew Wilcox spin_unlock_irqrestore(&pool->lock, flags); 322141e9d4bSMatthew Wilcox 323fa23f56dSSean O. Stalley page = pool_alloc_page(pool, mem_flags & (~__GFP_ZERO)); 324387870f2SMarek Szyprowski if (!page) 325387870f2SMarek Szyprowski return NULL; 326141e9d4bSMatthew Wilcox 3272cae367eSMatthew Wilcox spin_lock_irqsave(&pool->lock, flags); 328141e9d4bSMatthew Wilcox 329387870f2SMarek Szyprowski list_add(&page->page_list, &pool->page_list); 330141e9d4bSMatthew Wilcox ready: 331141e9d4bSMatthew Wilcox page->in_use++; 332a35a3455SMatthew Wilcox offset = page->offset; 333a35a3455SMatthew Wilcox page->offset = *(int *)(page->vaddr + offset); 334141e9d4bSMatthew Wilcox retval = offset + page->vaddr; 335141e9d4bSMatthew Wilcox *handle = offset + page->dma; 336b5ee5befSAndi Kleen #ifdef DMAPOOL_DEBUG 3375de55b26SMatthieu CASTET { 3385de55b26SMatthieu CASTET int i; 3395de55b26SMatthieu CASTET u8 *data = retval; 3405de55b26SMatthieu CASTET /* page->offset is stored in first 4 bytes */ 3415de55b26SMatthieu CASTET for (i = sizeof(page->offset); i < pool->size; i++) { 3425de55b26SMatthieu CASTET if (data[i] == POOL_POISON_FREED) 3435de55b26SMatthieu CASTET continue; 34441a04814SAndy Shevchenko dev_err(pool->dev, "%s %s, %p (corrupted)\n", 34541a04814SAndy Shevchenko __func__, pool->name, retval); 3465de55b26SMatthieu CASTET 3475de55b26SMatthieu CASTET /* 3485de55b26SMatthieu CASTET * Dump the first 4 bytes even if they are not 3495de55b26SMatthieu CASTET * POOL_POISON_FREED 3505de55b26SMatthieu CASTET */ 3515de55b26SMatthieu CASTET print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, 3525de55b26SMatthieu CASTET data, pool->size, 1); 3535de55b26SMatthieu CASTET break; 3545de55b26SMatthieu CASTET } 3555de55b26SMatthieu CASTET } 356fa23f56dSSean O. Stalley if (!(mem_flags & __GFP_ZERO)) 357141e9d4bSMatthew Wilcox memset(retval, POOL_POISON_ALLOCATED, pool->size); 358141e9d4bSMatthew Wilcox #endif 359141e9d4bSMatthew Wilcox spin_unlock_irqrestore(&pool->lock, flags); 360fa23f56dSSean O. Stalley 3616471384aSAlexander Potapenko if (want_init_on_alloc(mem_flags)) 362fa23f56dSSean O. Stalley memset(retval, 0, pool->size); 363fa23f56dSSean O. Stalley 364141e9d4bSMatthew Wilcox return retval; 365141e9d4bSMatthew Wilcox } 366e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_alloc); 367141e9d4bSMatthew Wilcox 368e87aa773SMatthew Wilcox static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma) 369141e9d4bSMatthew Wilcox { 370141e9d4bSMatthew Wilcox struct dma_page *page; 371141e9d4bSMatthew Wilcox 372141e9d4bSMatthew Wilcox list_for_each_entry(page, &pool->page_list, page_list) { 373141e9d4bSMatthew Wilcox if (dma < page->dma) 374141e9d4bSMatthew Wilcox continue; 375676bd991SRobin Murphy if ((dma - page->dma) < pool->allocation) 376141e9d4bSMatthew Wilcox return page; 377141e9d4bSMatthew Wilcox } 37884bc227dSRolf Eike Beer return NULL; 37984bc227dSRolf Eike Beer } 380141e9d4bSMatthew Wilcox 381141e9d4bSMatthew Wilcox /** 382141e9d4bSMatthew Wilcox * dma_pool_free - put block back into dma pool 383141e9d4bSMatthew Wilcox * @pool: the dma pool holding the block 384141e9d4bSMatthew Wilcox * @vaddr: virtual address of block 385141e9d4bSMatthew Wilcox * @dma: dma address of block 386141e9d4bSMatthew Wilcox * 387141e9d4bSMatthew Wilcox * Caller promises neither device nor driver will again touch this block 388141e9d4bSMatthew Wilcox * unless it is first re-allocated. 389141e9d4bSMatthew Wilcox */ 390e87aa773SMatthew Wilcox void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma) 391141e9d4bSMatthew Wilcox { 392141e9d4bSMatthew Wilcox struct dma_page *page; 393141e9d4bSMatthew Wilcox unsigned long flags; 394a35a3455SMatthew Wilcox unsigned int offset; 395141e9d4bSMatthew Wilcox 39684bc227dSRolf Eike Beer spin_lock_irqsave(&pool->lock, flags); 397e87aa773SMatthew Wilcox page = pool_find_page(pool, dma); 398e87aa773SMatthew Wilcox if (!page) { 39984bc227dSRolf Eike Beer spin_unlock_irqrestore(&pool->lock, flags); 40041a04814SAndy Shevchenko dev_err(pool->dev, "%s %s, %p/%pad (bad dma)\n", 40141a04814SAndy Shevchenko __func__, pool->name, vaddr, &dma); 402141e9d4bSMatthew Wilcox return; 403141e9d4bSMatthew Wilcox } 404141e9d4bSMatthew Wilcox 405a35a3455SMatthew Wilcox offset = vaddr - page->vaddr; 4066471384aSAlexander Potapenko if (want_init_on_free()) 4076471384aSAlexander Potapenko memset(vaddr, 0, pool->size); 408b5ee5befSAndi Kleen #ifdef DMAPOOL_DEBUG 409a35a3455SMatthew Wilcox if ((dma - page->dma) != offset) { 41084bc227dSRolf Eike Beer spin_unlock_irqrestore(&pool->lock, flags); 41141a04814SAndy Shevchenko dev_err(pool->dev, "%s %s, %p (bad vaddr)/%pad\n", 41241a04814SAndy Shevchenko __func__, pool->name, vaddr, &dma); 413141e9d4bSMatthew Wilcox return; 414141e9d4bSMatthew Wilcox } 415a35a3455SMatthew Wilcox { 416a35a3455SMatthew Wilcox unsigned int chain = page->offset; 417a35a3455SMatthew Wilcox while (chain < pool->allocation) { 418a35a3455SMatthew Wilcox if (chain != offset) { 419a35a3455SMatthew Wilcox chain = *(int *)(page->vaddr + chain); 420a35a3455SMatthew Wilcox continue; 421a35a3455SMatthew Wilcox } 42284bc227dSRolf Eike Beer spin_unlock_irqrestore(&pool->lock, flags); 42341a04814SAndy Shevchenko dev_err(pool->dev, "%s %s, dma %pad already free\n", 42441a04814SAndy Shevchenko __func__, pool->name, &dma); 425141e9d4bSMatthew Wilcox return; 426141e9d4bSMatthew Wilcox } 427a35a3455SMatthew Wilcox } 428141e9d4bSMatthew Wilcox memset(vaddr, POOL_POISON_FREED, pool->size); 429141e9d4bSMatthew Wilcox #endif 430141e9d4bSMatthew Wilcox 431141e9d4bSMatthew Wilcox page->in_use--; 432a35a3455SMatthew Wilcox *(int *)vaddr = page->offset; 433a35a3455SMatthew Wilcox page->offset = offset; 434141e9d4bSMatthew Wilcox /* 435141e9d4bSMatthew Wilcox * Resist a temptation to do 436a35a3455SMatthew Wilcox * if (!is_page_busy(page)) pool_free_page(pool, page); 437141e9d4bSMatthew Wilcox * Better have a few empty pages hang around. 438141e9d4bSMatthew Wilcox */ 439141e9d4bSMatthew Wilcox spin_unlock_irqrestore(&pool->lock, flags); 440141e9d4bSMatthew Wilcox } 441e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_free); 442141e9d4bSMatthew Wilcox 443141e9d4bSMatthew Wilcox /* 444141e9d4bSMatthew Wilcox * Managed DMA pool 445141e9d4bSMatthew Wilcox */ 446141e9d4bSMatthew Wilcox static void dmam_pool_release(struct device *dev, void *res) 447141e9d4bSMatthew Wilcox { 448141e9d4bSMatthew Wilcox struct dma_pool *pool = *(struct dma_pool **)res; 449141e9d4bSMatthew Wilcox 450141e9d4bSMatthew Wilcox dma_pool_destroy(pool); 451141e9d4bSMatthew Wilcox } 452141e9d4bSMatthew Wilcox 453141e9d4bSMatthew Wilcox static int dmam_pool_match(struct device *dev, void *res, void *match_data) 454141e9d4bSMatthew Wilcox { 455141e9d4bSMatthew Wilcox return *(struct dma_pool **)res == match_data; 456141e9d4bSMatthew Wilcox } 457141e9d4bSMatthew Wilcox 458141e9d4bSMatthew Wilcox /** 459141e9d4bSMatthew Wilcox * dmam_pool_create - Managed dma_pool_create() 460141e9d4bSMatthew Wilcox * @name: name of pool, for diagnostics 461141e9d4bSMatthew Wilcox * @dev: device that will be doing the DMA 462141e9d4bSMatthew Wilcox * @size: size of the blocks in this pool. 463141e9d4bSMatthew Wilcox * @align: alignment requirement for blocks; must be a power of two 464141e9d4bSMatthew Wilcox * @allocation: returned blocks won't cross this boundary (or zero) 465141e9d4bSMatthew Wilcox * 466141e9d4bSMatthew Wilcox * Managed dma_pool_create(). DMA pool created with this function is 467141e9d4bSMatthew Wilcox * automatically destroyed on driver detach. 468a862f68aSMike Rapoport * 469a862f68aSMike Rapoport * Return: a managed dma allocation pool with the requested 470a862f68aSMike Rapoport * characteristics, or %NULL if one can't be created. 471141e9d4bSMatthew Wilcox */ 472141e9d4bSMatthew Wilcox struct dma_pool *dmam_pool_create(const char *name, struct device *dev, 473141e9d4bSMatthew Wilcox size_t size, size_t align, size_t allocation) 474141e9d4bSMatthew Wilcox { 475141e9d4bSMatthew Wilcox struct dma_pool **ptr, *pool; 476141e9d4bSMatthew Wilcox 477141e9d4bSMatthew Wilcox ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL); 478141e9d4bSMatthew Wilcox if (!ptr) 479141e9d4bSMatthew Wilcox return NULL; 480141e9d4bSMatthew Wilcox 481141e9d4bSMatthew Wilcox pool = *ptr = dma_pool_create(name, dev, size, align, allocation); 482141e9d4bSMatthew Wilcox if (pool) 483141e9d4bSMatthew Wilcox devres_add(dev, ptr); 484141e9d4bSMatthew Wilcox else 485141e9d4bSMatthew Wilcox devres_free(ptr); 486141e9d4bSMatthew Wilcox 487141e9d4bSMatthew Wilcox return pool; 488141e9d4bSMatthew Wilcox } 489e87aa773SMatthew Wilcox EXPORT_SYMBOL(dmam_pool_create); 490141e9d4bSMatthew Wilcox 491141e9d4bSMatthew Wilcox /** 492141e9d4bSMatthew Wilcox * dmam_pool_destroy - Managed dma_pool_destroy() 493141e9d4bSMatthew Wilcox * @pool: dma pool that will be destroyed 494141e9d4bSMatthew Wilcox * 495141e9d4bSMatthew Wilcox * Managed dma_pool_destroy(). 496141e9d4bSMatthew Wilcox */ 497141e9d4bSMatthew Wilcox void dmam_pool_destroy(struct dma_pool *pool) 498141e9d4bSMatthew Wilcox { 499141e9d4bSMatthew Wilcox struct device *dev = pool->dev; 500141e9d4bSMatthew Wilcox 501172cb4b3SAndy Shevchenko WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool)); 502141e9d4bSMatthew Wilcox } 503141e9d4bSMatthew Wilcox EXPORT_SYMBOL(dmam_pool_destroy); 504