xref: /openbmc/linux/mm/dmapool.c (revision e8df2c703d5d1a99cfc45124bfa6f5e1982e0166)
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 
65*e8df2c70SYueHaibing static ssize_t pools_show(struct device *dev, struct device_attribute *attr, char *buf)
66141e9d4bSMatthew Wilcox {
67141e9d4bSMatthew Wilcox 	unsigned temp;
68141e9d4bSMatthew Wilcox 	unsigned size;
69141e9d4bSMatthew Wilcox 	char *next;
70141e9d4bSMatthew Wilcox 	struct dma_page *page;
71141e9d4bSMatthew Wilcox 	struct dma_pool *pool;
72141e9d4bSMatthew Wilcox 
73141e9d4bSMatthew Wilcox 	next = buf;
74141e9d4bSMatthew Wilcox 	size = PAGE_SIZE;
75141e9d4bSMatthew Wilcox 
76141e9d4bSMatthew Wilcox 	temp = scnprintf(next, size, "poolinfo - 0.1\n");
77141e9d4bSMatthew Wilcox 	size -= temp;
78141e9d4bSMatthew Wilcox 	next += temp;
79141e9d4bSMatthew Wilcox 
80141e9d4bSMatthew Wilcox 	mutex_lock(&pools_lock);
81141e9d4bSMatthew Wilcox 	list_for_each_entry(pool, &dev->dma_pools, pools) {
82141e9d4bSMatthew Wilcox 		unsigned pages = 0;
83141e9d4bSMatthew Wilcox 		unsigned blocks = 0;
84141e9d4bSMatthew Wilcox 
85c4956823SThomas Gleixner 		spin_lock_irq(&pool->lock);
86141e9d4bSMatthew Wilcox 		list_for_each_entry(page, &pool->page_list, page_list) {
87141e9d4bSMatthew Wilcox 			pages++;
88141e9d4bSMatthew Wilcox 			blocks += page->in_use;
89141e9d4bSMatthew Wilcox 		}
90c4956823SThomas Gleixner 		spin_unlock_irq(&pool->lock);
91141e9d4bSMatthew Wilcox 
92141e9d4bSMatthew Wilcox 		/* per-pool info, no real statistics yet */
935b5e0928SAlexey Dobriyan 		temp = scnprintf(next, size, "%-16s %4u %4zu %4zu %2u\n",
94a35a3455SMatthew Wilcox 				 pool->name, blocks,
95a35a3455SMatthew Wilcox 				 pages * (pool->allocation / pool->size),
96141e9d4bSMatthew Wilcox 				 pool->size, pages);
97141e9d4bSMatthew Wilcox 		size -= temp;
98141e9d4bSMatthew Wilcox 		next += temp;
99141e9d4bSMatthew Wilcox 	}
100141e9d4bSMatthew Wilcox 	mutex_unlock(&pools_lock);
101141e9d4bSMatthew Wilcox 
102141e9d4bSMatthew Wilcox 	return PAGE_SIZE - size;
103141e9d4bSMatthew Wilcox }
104e87aa773SMatthew Wilcox 
105*e8df2c70SYueHaibing static DEVICE_ATTR_RO(pools);
106141e9d4bSMatthew Wilcox 
107141e9d4bSMatthew Wilcox /**
108141e9d4bSMatthew Wilcox  * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
109141e9d4bSMatthew Wilcox  * @name: name of pool, for diagnostics
110141e9d4bSMatthew Wilcox  * @dev: device that will be doing the DMA
111141e9d4bSMatthew Wilcox  * @size: size of the blocks in this pool.
112141e9d4bSMatthew Wilcox  * @align: alignment requirement for blocks; must be a power of two
113e34f44b3SMatthew Wilcox  * @boundary: returned blocks won't cross this power of two boundary
114a862f68aSMike Rapoport  * Context: not in_interrupt()
115141e9d4bSMatthew Wilcox  *
116a862f68aSMike Rapoport  * Given one of these pools, dma_pool_alloc()
117141e9d4bSMatthew Wilcox  * may be used to allocate memory.  Such memory will all have "consistent"
118141e9d4bSMatthew Wilcox  * DMA mappings, accessible by the device and its driver without using
119141e9d4bSMatthew Wilcox  * cache flushing primitives.  The actual size of blocks allocated may be
120141e9d4bSMatthew Wilcox  * larger than requested because of alignment.
121141e9d4bSMatthew Wilcox  *
122e34f44b3SMatthew Wilcox  * If @boundary is nonzero, objects returned from dma_pool_alloc() won't
123141e9d4bSMatthew Wilcox  * cross that size boundary.  This is useful for devices which have
124141e9d4bSMatthew Wilcox  * addressing restrictions on individual DMA transfers, such as not crossing
125141e9d4bSMatthew Wilcox  * boundaries of 4KBytes.
126a862f68aSMike Rapoport  *
127a862f68aSMike Rapoport  * Return: a dma allocation pool with the requested characteristics, or
128a862f68aSMike Rapoport  * %NULL if one can't be created.
129141e9d4bSMatthew Wilcox  */
130e87aa773SMatthew Wilcox struct dma_pool *dma_pool_create(const char *name, struct device *dev,
131e34f44b3SMatthew Wilcox 				 size_t size, size_t align, size_t boundary)
132141e9d4bSMatthew Wilcox {
133141e9d4bSMatthew Wilcox 	struct dma_pool *retval;
134e34f44b3SMatthew Wilcox 	size_t allocation;
13501c2965fSSebastian Andrzej Siewior 	bool empty = false;
136141e9d4bSMatthew Wilcox 
137baa2ef83SPaul McQuade 	if (align == 0)
138141e9d4bSMatthew Wilcox 		align = 1;
139baa2ef83SPaul McQuade 	else if (align & (align - 1))
140399154beSMatthew Wilcox 		return NULL;
141399154beSMatthew Wilcox 
142baa2ef83SPaul McQuade 	if (size == 0)
143141e9d4bSMatthew Wilcox 		return NULL;
144baa2ef83SPaul McQuade 	else if (size < 4)
145a35a3455SMatthew Wilcox 		size = 4;
146399154beSMatthew Wilcox 
147399154beSMatthew Wilcox 	size = ALIGN(size, align);
148e34f44b3SMatthew Wilcox 	allocation = max_t(size_t, size, PAGE_SIZE);
149141e9d4bSMatthew Wilcox 
150baa2ef83SPaul McQuade 	if (!boundary)
151e34f44b3SMatthew Wilcox 		boundary = allocation;
152baa2ef83SPaul McQuade 	else if ((boundary < size) || (boundary & (boundary - 1)))
153e34f44b3SMatthew Wilcox 		return NULL;
154e34f44b3SMatthew Wilcox 
155e34f44b3SMatthew Wilcox 	retval = kmalloc_node(sizeof(*retval), GFP_KERNEL, dev_to_node(dev));
156e34f44b3SMatthew Wilcox 	if (!retval)
157141e9d4bSMatthew Wilcox 		return retval;
158141e9d4bSMatthew Wilcox 
159943f229eSZhiyuan Dai 	strscpy(retval->name, name, sizeof(retval->name));
160141e9d4bSMatthew Wilcox 
161141e9d4bSMatthew Wilcox 	retval->dev = dev;
162141e9d4bSMatthew Wilcox 
163141e9d4bSMatthew Wilcox 	INIT_LIST_HEAD(&retval->page_list);
164141e9d4bSMatthew Wilcox 	spin_lock_init(&retval->lock);
165141e9d4bSMatthew Wilcox 	retval->size = size;
166e34f44b3SMatthew Wilcox 	retval->boundary = boundary;
167141e9d4bSMatthew Wilcox 	retval->allocation = allocation;
168141e9d4bSMatthew Wilcox 
169cc6b664aSDaeseok Youn 	INIT_LIST_HEAD(&retval->pools);
170141e9d4bSMatthew Wilcox 
17101c2965fSSebastian Andrzej Siewior 	/*
17201c2965fSSebastian Andrzej Siewior 	 * pools_lock ensures that the ->dma_pools list does not get corrupted.
17301c2965fSSebastian Andrzej Siewior 	 * pools_reg_lock ensures that there is not a race between
17401c2965fSSebastian Andrzej Siewior 	 * dma_pool_create() and dma_pool_destroy() or within dma_pool_create()
17501c2965fSSebastian Andrzej Siewior 	 * when the first invocation of dma_pool_create() failed on
17601c2965fSSebastian Andrzej Siewior 	 * device_create_file() and the second assumes that it has been done (I
17701c2965fSSebastian Andrzej Siewior 	 * know it is a short window).
17801c2965fSSebastian Andrzej Siewior 	 */
17901c2965fSSebastian Andrzej Siewior 	mutex_lock(&pools_reg_lock);
180141e9d4bSMatthew Wilcox 	mutex_lock(&pools_lock);
18101c2965fSSebastian Andrzej Siewior 	if (list_empty(&dev->dma_pools))
18201c2965fSSebastian Andrzej Siewior 		empty = true;
183cc6b664aSDaeseok Youn 	list_add(&retval->pools, &dev->dma_pools);
184cc6b664aSDaeseok Youn 	mutex_unlock(&pools_lock);
18501c2965fSSebastian Andrzej Siewior 	if (empty) {
18601c2965fSSebastian Andrzej Siewior 		int err;
187141e9d4bSMatthew Wilcox 
18801c2965fSSebastian Andrzej Siewior 		err = device_create_file(dev, &dev_attr_pools);
18901c2965fSSebastian Andrzej Siewior 		if (err) {
19001c2965fSSebastian Andrzej Siewior 			mutex_lock(&pools_lock);
19101c2965fSSebastian Andrzej Siewior 			list_del(&retval->pools);
19201c2965fSSebastian Andrzej Siewior 			mutex_unlock(&pools_lock);
19301c2965fSSebastian Andrzej Siewior 			mutex_unlock(&pools_reg_lock);
19401c2965fSSebastian Andrzej Siewior 			kfree(retval);
19501c2965fSSebastian Andrzej Siewior 			return NULL;
19601c2965fSSebastian Andrzej Siewior 		}
19701c2965fSSebastian Andrzej Siewior 	}
19801c2965fSSebastian Andrzej Siewior 	mutex_unlock(&pools_reg_lock);
199141e9d4bSMatthew Wilcox 	return retval;
200141e9d4bSMatthew Wilcox }
201e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_create);
202141e9d4bSMatthew Wilcox 
203a35a3455SMatthew Wilcox static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page)
204a35a3455SMatthew Wilcox {
205a35a3455SMatthew Wilcox 	unsigned int offset = 0;
206e34f44b3SMatthew Wilcox 	unsigned int next_boundary = pool->boundary;
207a35a3455SMatthew Wilcox 
208a35a3455SMatthew Wilcox 	do {
209a35a3455SMatthew Wilcox 		unsigned int next = offset + pool->size;
210e34f44b3SMatthew Wilcox 		if (unlikely((next + pool->size) >= next_boundary)) {
211e34f44b3SMatthew Wilcox 			next = next_boundary;
212e34f44b3SMatthew Wilcox 			next_boundary += pool->boundary;
213e34f44b3SMatthew Wilcox 		}
214a35a3455SMatthew Wilcox 		*(int *)(page->vaddr + offset) = next;
215a35a3455SMatthew Wilcox 		offset = next;
216a35a3455SMatthew Wilcox 	} while (offset < pool->allocation);
217a35a3455SMatthew Wilcox }
218a35a3455SMatthew Wilcox 
219e87aa773SMatthew Wilcox static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
220141e9d4bSMatthew Wilcox {
221141e9d4bSMatthew Wilcox 	struct dma_page *page;
222141e9d4bSMatthew Wilcox 
223a35a3455SMatthew Wilcox 	page = kmalloc(sizeof(*page), mem_flags);
224141e9d4bSMatthew Wilcox 	if (!page)
225141e9d4bSMatthew Wilcox 		return NULL;
226a35a3455SMatthew Wilcox 	page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation,
227e87aa773SMatthew Wilcox 					 &page->dma, mem_flags);
228141e9d4bSMatthew Wilcox 	if (page->vaddr) {
229b5ee5befSAndi Kleen #ifdef	DMAPOOL_DEBUG
230141e9d4bSMatthew Wilcox 		memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
231141e9d4bSMatthew Wilcox #endif
232a35a3455SMatthew Wilcox 		pool_initialise_page(pool, page);
233141e9d4bSMatthew Wilcox 		page->in_use = 0;
234a35a3455SMatthew Wilcox 		page->offset = 0;
235141e9d4bSMatthew Wilcox 	} else {
236141e9d4bSMatthew Wilcox 		kfree(page);
237141e9d4bSMatthew Wilcox 		page = NULL;
238141e9d4bSMatthew Wilcox 	}
239141e9d4bSMatthew Wilcox 	return page;
240141e9d4bSMatthew Wilcox }
241141e9d4bSMatthew Wilcox 
242d9e7e37bSNicholas Krause static inline bool is_page_busy(struct dma_page *page)
243141e9d4bSMatthew Wilcox {
244a35a3455SMatthew Wilcox 	return page->in_use != 0;
245141e9d4bSMatthew Wilcox }
246141e9d4bSMatthew Wilcox 
247e87aa773SMatthew Wilcox static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
248141e9d4bSMatthew Wilcox {
249141e9d4bSMatthew Wilcox 	dma_addr_t dma = page->dma;
250141e9d4bSMatthew Wilcox 
251b5ee5befSAndi Kleen #ifdef	DMAPOOL_DEBUG
252141e9d4bSMatthew Wilcox 	memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
253141e9d4bSMatthew Wilcox #endif
254141e9d4bSMatthew Wilcox 	dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
255141e9d4bSMatthew Wilcox 	list_del(&page->page_list);
256141e9d4bSMatthew Wilcox 	kfree(page);
257141e9d4bSMatthew Wilcox }
258141e9d4bSMatthew Wilcox 
259141e9d4bSMatthew Wilcox /**
260141e9d4bSMatthew Wilcox  * dma_pool_destroy - destroys a pool of dma memory blocks.
261141e9d4bSMatthew Wilcox  * @pool: dma pool that will be destroyed
262141e9d4bSMatthew Wilcox  * Context: !in_interrupt()
263141e9d4bSMatthew Wilcox  *
264141e9d4bSMatthew Wilcox  * Caller guarantees that no more memory from the pool is in use,
265141e9d4bSMatthew Wilcox  * and that nothing will try to use the pool after this call.
266141e9d4bSMatthew Wilcox  */
267e87aa773SMatthew Wilcox void dma_pool_destroy(struct dma_pool *pool)
268141e9d4bSMatthew Wilcox {
26942286f83SAndy Shevchenko 	struct dma_page *page, *tmp;
27001c2965fSSebastian Andrzej Siewior 	bool empty = false;
27101c2965fSSebastian Andrzej Siewior 
27244d7175dSSergey Senozhatsky 	if (unlikely(!pool))
27344d7175dSSergey Senozhatsky 		return;
27444d7175dSSergey Senozhatsky 
27501c2965fSSebastian Andrzej Siewior 	mutex_lock(&pools_reg_lock);
276141e9d4bSMatthew Wilcox 	mutex_lock(&pools_lock);
277141e9d4bSMatthew Wilcox 	list_del(&pool->pools);
278141e9d4bSMatthew Wilcox 	if (pool->dev && list_empty(&pool->dev->dma_pools))
27901c2965fSSebastian Andrzej Siewior 		empty = true;
280141e9d4bSMatthew Wilcox 	mutex_unlock(&pools_lock);
28101c2965fSSebastian Andrzej Siewior 	if (empty)
28201c2965fSSebastian Andrzej Siewior 		device_remove_file(pool->dev, &dev_attr_pools);
28301c2965fSSebastian Andrzej Siewior 	mutex_unlock(&pools_reg_lock);
284141e9d4bSMatthew Wilcox 
28542286f83SAndy Shevchenko 	list_for_each_entry_safe(page, tmp, &pool->page_list, page_list) {
286a35a3455SMatthew Wilcox 		if (is_page_busy(page)) {
287141e9d4bSMatthew Wilcox 			if (pool->dev)
28841a04814SAndy Shevchenko 				dev_err(pool->dev, "%s %s, %p busy\n", __func__,
289141e9d4bSMatthew Wilcox 					pool->name, page->vaddr);
290141e9d4bSMatthew Wilcox 			else
29141a04814SAndy Shevchenko 				pr_err("%s %s, %p busy\n", __func__,
292141e9d4bSMatthew Wilcox 				       pool->name, page->vaddr);
293141e9d4bSMatthew Wilcox 			/* leak the still-in-use consistent memory */
294141e9d4bSMatthew Wilcox 			list_del(&page->page_list);
295141e9d4bSMatthew Wilcox 			kfree(page);
296141e9d4bSMatthew Wilcox 		} else
297141e9d4bSMatthew Wilcox 			pool_free_page(pool, page);
298141e9d4bSMatthew Wilcox 	}
299141e9d4bSMatthew Wilcox 
300141e9d4bSMatthew Wilcox 	kfree(pool);
301141e9d4bSMatthew Wilcox }
302e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_destroy);
303141e9d4bSMatthew Wilcox 
304141e9d4bSMatthew Wilcox /**
305141e9d4bSMatthew Wilcox  * dma_pool_alloc - get a block of consistent memory
306141e9d4bSMatthew Wilcox  * @pool: dma pool that will produce the block
307141e9d4bSMatthew Wilcox  * @mem_flags: GFP_* bitmask
308141e9d4bSMatthew Wilcox  * @handle: pointer to dma address of block
309141e9d4bSMatthew Wilcox  *
310a862f68aSMike Rapoport  * Return: the kernel virtual address of a currently unused block,
311141e9d4bSMatthew Wilcox  * and reports its dma address through the handle.
3126182a094SMatthew Wilcox  * If such a memory block can't be allocated, %NULL is returned.
313141e9d4bSMatthew Wilcox  */
314e87aa773SMatthew Wilcox void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
315e87aa773SMatthew Wilcox 		     dma_addr_t *handle)
316141e9d4bSMatthew Wilcox {
317141e9d4bSMatthew Wilcox 	unsigned long flags;
318141e9d4bSMatthew Wilcox 	struct dma_page *page;
319141e9d4bSMatthew Wilcox 	size_t offset;
320141e9d4bSMatthew Wilcox 	void *retval;
321141e9d4bSMatthew Wilcox 
3220f2f89b6SDaniel Vetter 	might_alloc(mem_flags);
323ea05c844SDima Zavin 
324141e9d4bSMatthew Wilcox 	spin_lock_irqsave(&pool->lock, flags);
325141e9d4bSMatthew Wilcox 	list_for_each_entry(page, &pool->page_list, page_list) {
326a35a3455SMatthew Wilcox 		if (page->offset < pool->allocation)
327141e9d4bSMatthew Wilcox 			goto ready;
328141e9d4bSMatthew Wilcox 	}
329141e9d4bSMatthew Wilcox 
330387870f2SMarek Szyprowski 	/* pool_alloc_page() might sleep, so temporarily drop &pool->lock */
331141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
332141e9d4bSMatthew Wilcox 
333fa23f56dSSean O. Stalley 	page = pool_alloc_page(pool, mem_flags & (~__GFP_ZERO));
334387870f2SMarek Szyprowski 	if (!page)
335387870f2SMarek Szyprowski 		return NULL;
336141e9d4bSMatthew Wilcox 
3372cae367eSMatthew Wilcox 	spin_lock_irqsave(&pool->lock, flags);
338141e9d4bSMatthew Wilcox 
339387870f2SMarek Szyprowski 	list_add(&page->page_list, &pool->page_list);
340141e9d4bSMatthew Wilcox  ready:
341141e9d4bSMatthew Wilcox 	page->in_use++;
342a35a3455SMatthew Wilcox 	offset = page->offset;
343a35a3455SMatthew Wilcox 	page->offset = *(int *)(page->vaddr + offset);
344141e9d4bSMatthew Wilcox 	retval = offset + page->vaddr;
345141e9d4bSMatthew Wilcox 	*handle = offset + page->dma;
346b5ee5befSAndi Kleen #ifdef	DMAPOOL_DEBUG
3475de55b26SMatthieu CASTET 	{
3485de55b26SMatthieu CASTET 		int i;
3495de55b26SMatthieu CASTET 		u8 *data = retval;
3505de55b26SMatthieu CASTET 		/* page->offset is stored in first 4 bytes */
3515de55b26SMatthieu CASTET 		for (i = sizeof(page->offset); i < pool->size; i++) {
3525de55b26SMatthieu CASTET 			if (data[i] == POOL_POISON_FREED)
3535de55b26SMatthieu CASTET 				continue;
3545de55b26SMatthieu CASTET 			if (pool->dev)
35541a04814SAndy Shevchenko 				dev_err(pool->dev, "%s %s, %p (corrupted)\n",
35641a04814SAndy Shevchenko 					__func__, pool->name, retval);
3575de55b26SMatthieu CASTET 			else
35841a04814SAndy Shevchenko 				pr_err("%s %s, %p (corrupted)\n",
35941a04814SAndy Shevchenko 					__func__, pool->name, retval);
3605de55b26SMatthieu CASTET 
3615de55b26SMatthieu CASTET 			/*
3625de55b26SMatthieu CASTET 			 * Dump the first 4 bytes even if they are not
3635de55b26SMatthieu CASTET 			 * POOL_POISON_FREED
3645de55b26SMatthieu CASTET 			 */
3655de55b26SMatthieu CASTET 			print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1,
3665de55b26SMatthieu CASTET 					data, pool->size, 1);
3675de55b26SMatthieu CASTET 			break;
3685de55b26SMatthieu CASTET 		}
3695de55b26SMatthieu CASTET 	}
370fa23f56dSSean O. Stalley 	if (!(mem_flags & __GFP_ZERO))
371141e9d4bSMatthew Wilcox 		memset(retval, POOL_POISON_ALLOCATED, pool->size);
372141e9d4bSMatthew Wilcox #endif
373141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
374fa23f56dSSean O. Stalley 
3756471384aSAlexander Potapenko 	if (want_init_on_alloc(mem_flags))
376fa23f56dSSean O. Stalley 		memset(retval, 0, pool->size);
377fa23f56dSSean O. Stalley 
378141e9d4bSMatthew Wilcox 	return retval;
379141e9d4bSMatthew Wilcox }
380e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_alloc);
381141e9d4bSMatthew Wilcox 
382e87aa773SMatthew Wilcox static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
383141e9d4bSMatthew Wilcox {
384141e9d4bSMatthew Wilcox 	struct dma_page *page;
385141e9d4bSMatthew Wilcox 
386141e9d4bSMatthew Wilcox 	list_for_each_entry(page, &pool->page_list, page_list) {
387141e9d4bSMatthew Wilcox 		if (dma < page->dma)
388141e9d4bSMatthew Wilcox 			continue;
389676bd991SRobin Murphy 		if ((dma - page->dma) < pool->allocation)
390141e9d4bSMatthew Wilcox 			return page;
391141e9d4bSMatthew Wilcox 	}
39284bc227dSRolf Eike Beer 	return NULL;
39384bc227dSRolf Eike Beer }
394141e9d4bSMatthew Wilcox 
395141e9d4bSMatthew Wilcox /**
396141e9d4bSMatthew Wilcox  * dma_pool_free - put block back into dma pool
397141e9d4bSMatthew Wilcox  * @pool: the dma pool holding the block
398141e9d4bSMatthew Wilcox  * @vaddr: virtual address of block
399141e9d4bSMatthew Wilcox  * @dma: dma address of block
400141e9d4bSMatthew Wilcox  *
401141e9d4bSMatthew Wilcox  * Caller promises neither device nor driver will again touch this block
402141e9d4bSMatthew Wilcox  * unless it is first re-allocated.
403141e9d4bSMatthew Wilcox  */
404e87aa773SMatthew Wilcox void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
405141e9d4bSMatthew Wilcox {
406141e9d4bSMatthew Wilcox 	struct dma_page *page;
407141e9d4bSMatthew Wilcox 	unsigned long flags;
408a35a3455SMatthew Wilcox 	unsigned int offset;
409141e9d4bSMatthew Wilcox 
41084bc227dSRolf Eike Beer 	spin_lock_irqsave(&pool->lock, flags);
411e87aa773SMatthew Wilcox 	page = pool_find_page(pool, dma);
412e87aa773SMatthew Wilcox 	if (!page) {
41384bc227dSRolf Eike Beer 		spin_unlock_irqrestore(&pool->lock, flags);
414141e9d4bSMatthew Wilcox 		if (pool->dev)
41541a04814SAndy Shevchenko 			dev_err(pool->dev, "%s %s, %p/%pad (bad dma)\n",
41641a04814SAndy Shevchenko 				__func__, pool->name, vaddr, &dma);
417141e9d4bSMatthew Wilcox 		else
41841a04814SAndy Shevchenko 			pr_err("%s %s, %p/%pad (bad dma)\n",
41941a04814SAndy Shevchenko 			       __func__, pool->name, vaddr, &dma);
420141e9d4bSMatthew Wilcox 		return;
421141e9d4bSMatthew Wilcox 	}
422141e9d4bSMatthew Wilcox 
423a35a3455SMatthew Wilcox 	offset = vaddr - page->vaddr;
4246471384aSAlexander Potapenko 	if (want_init_on_free())
4256471384aSAlexander Potapenko 		memset(vaddr, 0, pool->size);
426b5ee5befSAndi Kleen #ifdef	DMAPOOL_DEBUG
427a35a3455SMatthew Wilcox 	if ((dma - page->dma) != offset) {
42884bc227dSRolf Eike Beer 		spin_unlock_irqrestore(&pool->lock, flags);
429141e9d4bSMatthew Wilcox 		if (pool->dev)
43041a04814SAndy Shevchenko 			dev_err(pool->dev, "%s %s, %p (bad vaddr)/%pad\n",
43141a04814SAndy Shevchenko 				__func__, pool->name, vaddr, &dma);
432141e9d4bSMatthew Wilcox 		else
43341a04814SAndy Shevchenko 			pr_err("%s %s, %p (bad vaddr)/%pad\n",
43441a04814SAndy Shevchenko 			       __func__, pool->name, vaddr, &dma);
435141e9d4bSMatthew Wilcox 		return;
436141e9d4bSMatthew Wilcox 	}
437a35a3455SMatthew Wilcox 	{
438a35a3455SMatthew Wilcox 		unsigned int chain = page->offset;
439a35a3455SMatthew Wilcox 		while (chain < pool->allocation) {
440a35a3455SMatthew Wilcox 			if (chain != offset) {
441a35a3455SMatthew Wilcox 				chain = *(int *)(page->vaddr + chain);
442a35a3455SMatthew Wilcox 				continue;
443a35a3455SMatthew Wilcox 			}
44484bc227dSRolf Eike Beer 			spin_unlock_irqrestore(&pool->lock, flags);
445141e9d4bSMatthew Wilcox 			if (pool->dev)
44641a04814SAndy Shevchenko 				dev_err(pool->dev, "%s %s, dma %pad already free\n",
44741a04814SAndy Shevchenko 					__func__, pool->name, &dma);
448141e9d4bSMatthew Wilcox 			else
44941a04814SAndy Shevchenko 				pr_err("%s %s, dma %pad already free\n",
45041a04814SAndy Shevchenko 				       __func__, pool->name, &dma);
451141e9d4bSMatthew Wilcox 			return;
452141e9d4bSMatthew Wilcox 		}
453a35a3455SMatthew Wilcox 	}
454141e9d4bSMatthew Wilcox 	memset(vaddr, POOL_POISON_FREED, pool->size);
455141e9d4bSMatthew Wilcox #endif
456141e9d4bSMatthew Wilcox 
457141e9d4bSMatthew Wilcox 	page->in_use--;
458a35a3455SMatthew Wilcox 	*(int *)vaddr = page->offset;
459a35a3455SMatthew Wilcox 	page->offset = offset;
460141e9d4bSMatthew Wilcox 	/*
461141e9d4bSMatthew Wilcox 	 * Resist a temptation to do
462a35a3455SMatthew Wilcox 	 *    if (!is_page_busy(page)) pool_free_page(pool, page);
463141e9d4bSMatthew Wilcox 	 * Better have a few empty pages hang around.
464141e9d4bSMatthew Wilcox 	 */
465141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
466141e9d4bSMatthew Wilcox }
467e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_free);
468141e9d4bSMatthew Wilcox 
469141e9d4bSMatthew Wilcox /*
470141e9d4bSMatthew Wilcox  * Managed DMA pool
471141e9d4bSMatthew Wilcox  */
472141e9d4bSMatthew Wilcox static void dmam_pool_release(struct device *dev, void *res)
473141e9d4bSMatthew Wilcox {
474141e9d4bSMatthew Wilcox 	struct dma_pool *pool = *(struct dma_pool **)res;
475141e9d4bSMatthew Wilcox 
476141e9d4bSMatthew Wilcox 	dma_pool_destroy(pool);
477141e9d4bSMatthew Wilcox }
478141e9d4bSMatthew Wilcox 
479141e9d4bSMatthew Wilcox static int dmam_pool_match(struct device *dev, void *res, void *match_data)
480141e9d4bSMatthew Wilcox {
481141e9d4bSMatthew Wilcox 	return *(struct dma_pool **)res == match_data;
482141e9d4bSMatthew Wilcox }
483141e9d4bSMatthew Wilcox 
484141e9d4bSMatthew Wilcox /**
485141e9d4bSMatthew Wilcox  * dmam_pool_create - Managed dma_pool_create()
486141e9d4bSMatthew Wilcox  * @name: name of pool, for diagnostics
487141e9d4bSMatthew Wilcox  * @dev: device that will be doing the DMA
488141e9d4bSMatthew Wilcox  * @size: size of the blocks in this pool.
489141e9d4bSMatthew Wilcox  * @align: alignment requirement for blocks; must be a power of two
490141e9d4bSMatthew Wilcox  * @allocation: returned blocks won't cross this boundary (or zero)
491141e9d4bSMatthew Wilcox  *
492141e9d4bSMatthew Wilcox  * Managed dma_pool_create().  DMA pool created with this function is
493141e9d4bSMatthew Wilcox  * automatically destroyed on driver detach.
494a862f68aSMike Rapoport  *
495a862f68aSMike Rapoport  * Return: a managed dma allocation pool with the requested
496a862f68aSMike Rapoport  * characteristics, or %NULL if one can't be created.
497141e9d4bSMatthew Wilcox  */
498141e9d4bSMatthew Wilcox struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
499141e9d4bSMatthew Wilcox 				  size_t size, size_t align, size_t allocation)
500141e9d4bSMatthew Wilcox {
501141e9d4bSMatthew Wilcox 	struct dma_pool **ptr, *pool;
502141e9d4bSMatthew Wilcox 
503141e9d4bSMatthew Wilcox 	ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
504141e9d4bSMatthew Wilcox 	if (!ptr)
505141e9d4bSMatthew Wilcox 		return NULL;
506141e9d4bSMatthew Wilcox 
507141e9d4bSMatthew Wilcox 	pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
508141e9d4bSMatthew Wilcox 	if (pool)
509141e9d4bSMatthew Wilcox 		devres_add(dev, ptr);
510141e9d4bSMatthew Wilcox 	else
511141e9d4bSMatthew Wilcox 		devres_free(ptr);
512141e9d4bSMatthew Wilcox 
513141e9d4bSMatthew Wilcox 	return pool;
514141e9d4bSMatthew Wilcox }
515e87aa773SMatthew Wilcox EXPORT_SYMBOL(dmam_pool_create);
516141e9d4bSMatthew Wilcox 
517141e9d4bSMatthew Wilcox /**
518141e9d4bSMatthew Wilcox  * dmam_pool_destroy - Managed dma_pool_destroy()
519141e9d4bSMatthew Wilcox  * @pool: dma pool that will be destroyed
520141e9d4bSMatthew Wilcox  *
521141e9d4bSMatthew Wilcox  * Managed dma_pool_destroy().
522141e9d4bSMatthew Wilcox  */
523141e9d4bSMatthew Wilcox void dmam_pool_destroy(struct dma_pool *pool)
524141e9d4bSMatthew Wilcox {
525141e9d4bSMatthew Wilcox 	struct device *dev = pool->dev;
526141e9d4bSMatthew Wilcox 
527172cb4b3SAndy Shevchenko 	WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool));
528141e9d4bSMatthew Wilcox }
529141e9d4bSMatthew Wilcox EXPORT_SYMBOL(dmam_pool_destroy);
530