xref: /openbmc/linux/mm/dmapool.c (revision 172cb4b3d49a1339dd67ee05e3f47972a70f556f)
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);
65141e9d4bSMatthew Wilcox 
66141e9d4bSMatthew Wilcox static ssize_t
67141e9d4bSMatthew Wilcox show_pools(struct device *dev, struct device_attribute *attr, char *buf)
68141e9d4bSMatthew Wilcox {
69141e9d4bSMatthew Wilcox 	unsigned temp;
70141e9d4bSMatthew Wilcox 	unsigned size;
71141e9d4bSMatthew Wilcox 	char *next;
72141e9d4bSMatthew Wilcox 	struct dma_page *page;
73141e9d4bSMatthew Wilcox 	struct dma_pool *pool;
74141e9d4bSMatthew Wilcox 
75141e9d4bSMatthew Wilcox 	next = buf;
76141e9d4bSMatthew Wilcox 	size = PAGE_SIZE;
77141e9d4bSMatthew Wilcox 
78141e9d4bSMatthew Wilcox 	temp = scnprintf(next, size, "poolinfo - 0.1\n");
79141e9d4bSMatthew Wilcox 	size -= temp;
80141e9d4bSMatthew Wilcox 	next += temp;
81141e9d4bSMatthew Wilcox 
82141e9d4bSMatthew Wilcox 	mutex_lock(&pools_lock);
83141e9d4bSMatthew Wilcox 	list_for_each_entry(pool, &dev->dma_pools, pools) {
84141e9d4bSMatthew Wilcox 		unsigned pages = 0;
85141e9d4bSMatthew Wilcox 		unsigned blocks = 0;
86141e9d4bSMatthew Wilcox 
87c4956823SThomas Gleixner 		spin_lock_irq(&pool->lock);
88141e9d4bSMatthew Wilcox 		list_for_each_entry(page, &pool->page_list, page_list) {
89141e9d4bSMatthew Wilcox 			pages++;
90141e9d4bSMatthew Wilcox 			blocks += page->in_use;
91141e9d4bSMatthew Wilcox 		}
92c4956823SThomas Gleixner 		spin_unlock_irq(&pool->lock);
93141e9d4bSMatthew Wilcox 
94141e9d4bSMatthew Wilcox 		/* per-pool info, no real statistics yet */
95141e9d4bSMatthew Wilcox 		temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
96a35a3455SMatthew Wilcox 				 pool->name, blocks,
97a35a3455SMatthew Wilcox 				 pages * (pool->allocation / pool->size),
98141e9d4bSMatthew Wilcox 				 pool->size, pages);
99141e9d4bSMatthew Wilcox 		size -= temp;
100141e9d4bSMatthew Wilcox 		next += temp;
101141e9d4bSMatthew Wilcox 	}
102141e9d4bSMatthew Wilcox 	mutex_unlock(&pools_lock);
103141e9d4bSMatthew Wilcox 
104141e9d4bSMatthew Wilcox 	return PAGE_SIZE - size;
105141e9d4bSMatthew Wilcox }
106e87aa773SMatthew Wilcox 
107141e9d4bSMatthew Wilcox static DEVICE_ATTR(pools, S_IRUGO, show_pools, NULL);
108141e9d4bSMatthew Wilcox 
109141e9d4bSMatthew Wilcox /**
110141e9d4bSMatthew Wilcox  * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
111141e9d4bSMatthew Wilcox  * @name: name of pool, for diagnostics
112141e9d4bSMatthew Wilcox  * @dev: device that will be doing the DMA
113141e9d4bSMatthew Wilcox  * @size: size of the blocks in this pool.
114141e9d4bSMatthew Wilcox  * @align: alignment requirement for blocks; must be a power of two
115e34f44b3SMatthew Wilcox  * @boundary: returned blocks won't cross this power of two boundary
116141e9d4bSMatthew Wilcox  * Context: !in_interrupt()
117141e9d4bSMatthew Wilcox  *
118141e9d4bSMatthew Wilcox  * Returns a dma allocation pool with the requested characteristics, or
119141e9d4bSMatthew Wilcox  * null if one can't be created.  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.
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;
135141e9d4bSMatthew Wilcox 
136399154beSMatthew Wilcox 	if (align == 0) {
137141e9d4bSMatthew Wilcox 		align = 1;
138399154beSMatthew Wilcox 	} else if (align & (align - 1)) {
139399154beSMatthew Wilcox 		return NULL;
140399154beSMatthew Wilcox 	}
141399154beSMatthew Wilcox 
142a35a3455SMatthew Wilcox 	if (size == 0) {
143141e9d4bSMatthew Wilcox 		return NULL;
144a35a3455SMatthew Wilcox 	} else if (size < 4) {
145a35a3455SMatthew Wilcox 		size = 4;
146a35a3455SMatthew Wilcox 	}
147399154beSMatthew Wilcox 
148399154beSMatthew Wilcox 	if ((size % align) != 0)
149399154beSMatthew Wilcox 		size = ALIGN(size, align);
150141e9d4bSMatthew Wilcox 
151e34f44b3SMatthew Wilcox 	allocation = max_t(size_t, size, PAGE_SIZE);
152141e9d4bSMatthew Wilcox 
153e34f44b3SMatthew Wilcox 	if (!boundary) {
154e34f44b3SMatthew Wilcox 		boundary = allocation;
155e34f44b3SMatthew Wilcox 	} else if ((boundary < size) || (boundary & (boundary - 1))) {
156e34f44b3SMatthew Wilcox 		return NULL;
157e34f44b3SMatthew Wilcox 	}
158e34f44b3SMatthew Wilcox 
159e34f44b3SMatthew Wilcox 	retval = kmalloc_node(sizeof(*retval), GFP_KERNEL, dev_to_node(dev));
160e34f44b3SMatthew Wilcox 	if (!retval)
161141e9d4bSMatthew Wilcox 		return retval;
162141e9d4bSMatthew Wilcox 
163e34f44b3SMatthew Wilcox 	strlcpy(retval->name, name, sizeof(retval->name));
164141e9d4bSMatthew Wilcox 
165141e9d4bSMatthew Wilcox 	retval->dev = dev;
166141e9d4bSMatthew Wilcox 
167141e9d4bSMatthew Wilcox 	INIT_LIST_HEAD(&retval->page_list);
168141e9d4bSMatthew Wilcox 	spin_lock_init(&retval->lock);
169141e9d4bSMatthew Wilcox 	retval->size = size;
170e34f44b3SMatthew Wilcox 	retval->boundary = boundary;
171141e9d4bSMatthew Wilcox 	retval->allocation = allocation;
172141e9d4bSMatthew Wilcox 
173cc6b664aSDaeseok Youn 	INIT_LIST_HEAD(&retval->pools);
174141e9d4bSMatthew Wilcox 
175141e9d4bSMatthew Wilcox 	mutex_lock(&pools_lock);
176cc6b664aSDaeseok Youn 	if (list_empty(&dev->dma_pools) &&
177cc6b664aSDaeseok Youn 	    device_create_file(dev, &dev_attr_pools)) {
178141e9d4bSMatthew Wilcox 		kfree(retval);
179cc6b664aSDaeseok Youn 		return NULL;
180141e9d4bSMatthew Wilcox 	} else
181cc6b664aSDaeseok Youn 		list_add(&retval->pools, &dev->dma_pools);
182cc6b664aSDaeseok Youn 	mutex_unlock(&pools_lock);
183141e9d4bSMatthew Wilcox 
184141e9d4bSMatthew Wilcox 	return retval;
185141e9d4bSMatthew Wilcox }
186e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_create);
187141e9d4bSMatthew Wilcox 
188a35a3455SMatthew Wilcox static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page)
189a35a3455SMatthew Wilcox {
190a35a3455SMatthew Wilcox 	unsigned int offset = 0;
191e34f44b3SMatthew Wilcox 	unsigned int next_boundary = pool->boundary;
192a35a3455SMatthew Wilcox 
193a35a3455SMatthew Wilcox 	do {
194a35a3455SMatthew Wilcox 		unsigned int next = offset + pool->size;
195e34f44b3SMatthew Wilcox 		if (unlikely((next + pool->size) >= next_boundary)) {
196e34f44b3SMatthew Wilcox 			next = next_boundary;
197e34f44b3SMatthew Wilcox 			next_boundary += pool->boundary;
198e34f44b3SMatthew Wilcox 		}
199a35a3455SMatthew Wilcox 		*(int *)(page->vaddr + offset) = next;
200a35a3455SMatthew Wilcox 		offset = next;
201a35a3455SMatthew Wilcox 	} while (offset < pool->allocation);
202a35a3455SMatthew Wilcox }
203a35a3455SMatthew Wilcox 
204e87aa773SMatthew Wilcox static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
205141e9d4bSMatthew Wilcox {
206141e9d4bSMatthew Wilcox 	struct dma_page *page;
207141e9d4bSMatthew Wilcox 
208a35a3455SMatthew Wilcox 	page = kmalloc(sizeof(*page), mem_flags);
209141e9d4bSMatthew Wilcox 	if (!page)
210141e9d4bSMatthew Wilcox 		return NULL;
211a35a3455SMatthew Wilcox 	page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation,
212e87aa773SMatthew Wilcox 					 &page->dma, mem_flags);
213141e9d4bSMatthew Wilcox 	if (page->vaddr) {
214b5ee5befSAndi Kleen #ifdef	DMAPOOL_DEBUG
215141e9d4bSMatthew Wilcox 		memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
216141e9d4bSMatthew Wilcox #endif
217a35a3455SMatthew Wilcox 		pool_initialise_page(pool, page);
218141e9d4bSMatthew Wilcox 		page->in_use = 0;
219a35a3455SMatthew Wilcox 		page->offset = 0;
220141e9d4bSMatthew Wilcox 	} else {
221141e9d4bSMatthew Wilcox 		kfree(page);
222141e9d4bSMatthew Wilcox 		page = NULL;
223141e9d4bSMatthew Wilcox 	}
224141e9d4bSMatthew Wilcox 	return page;
225141e9d4bSMatthew Wilcox }
226141e9d4bSMatthew Wilcox 
227a35a3455SMatthew Wilcox static inline int is_page_busy(struct dma_page *page)
228141e9d4bSMatthew Wilcox {
229a35a3455SMatthew Wilcox 	return page->in_use != 0;
230141e9d4bSMatthew Wilcox }
231141e9d4bSMatthew Wilcox 
232e87aa773SMatthew Wilcox static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
233141e9d4bSMatthew Wilcox {
234141e9d4bSMatthew Wilcox 	dma_addr_t dma = page->dma;
235141e9d4bSMatthew Wilcox 
236b5ee5befSAndi Kleen #ifdef	DMAPOOL_DEBUG
237141e9d4bSMatthew Wilcox 	memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
238141e9d4bSMatthew Wilcox #endif
239141e9d4bSMatthew Wilcox 	dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
240141e9d4bSMatthew Wilcox 	list_del(&page->page_list);
241141e9d4bSMatthew Wilcox 	kfree(page);
242141e9d4bSMatthew Wilcox }
243141e9d4bSMatthew Wilcox 
244141e9d4bSMatthew Wilcox /**
245141e9d4bSMatthew Wilcox  * dma_pool_destroy - destroys a pool of dma memory blocks.
246141e9d4bSMatthew Wilcox  * @pool: dma pool that will be destroyed
247141e9d4bSMatthew Wilcox  * Context: !in_interrupt()
248141e9d4bSMatthew Wilcox  *
249141e9d4bSMatthew Wilcox  * Caller guarantees that no more memory from the pool is in use,
250141e9d4bSMatthew Wilcox  * and that nothing will try to use the pool after this call.
251141e9d4bSMatthew Wilcox  */
252e87aa773SMatthew Wilcox void dma_pool_destroy(struct dma_pool *pool)
253141e9d4bSMatthew Wilcox {
254141e9d4bSMatthew Wilcox 	mutex_lock(&pools_lock);
255141e9d4bSMatthew Wilcox 	list_del(&pool->pools);
256141e9d4bSMatthew Wilcox 	if (pool->dev && list_empty(&pool->dev->dma_pools))
257141e9d4bSMatthew Wilcox 		device_remove_file(pool->dev, &dev_attr_pools);
258141e9d4bSMatthew Wilcox 	mutex_unlock(&pools_lock);
259141e9d4bSMatthew Wilcox 
260141e9d4bSMatthew Wilcox 	while (!list_empty(&pool->page_list)) {
261141e9d4bSMatthew Wilcox 		struct dma_page *page;
262141e9d4bSMatthew Wilcox 		page = list_entry(pool->page_list.next,
263141e9d4bSMatthew Wilcox 				  struct dma_page, page_list);
264a35a3455SMatthew Wilcox 		if (is_page_busy(page)) {
265141e9d4bSMatthew Wilcox 			if (pool->dev)
266e87aa773SMatthew Wilcox 				dev_err(pool->dev,
267e87aa773SMatthew Wilcox 					"dma_pool_destroy %s, %p busy\n",
268141e9d4bSMatthew Wilcox 					pool->name, page->vaddr);
269141e9d4bSMatthew Wilcox 			else
270e87aa773SMatthew Wilcox 				printk(KERN_ERR
271e87aa773SMatthew Wilcox 				       "dma_pool_destroy %s, %p busy\n",
272141e9d4bSMatthew Wilcox 				       pool->name, page->vaddr);
273141e9d4bSMatthew Wilcox 			/* leak the still-in-use consistent memory */
274141e9d4bSMatthew Wilcox 			list_del(&page->page_list);
275141e9d4bSMatthew Wilcox 			kfree(page);
276141e9d4bSMatthew Wilcox 		} else
277141e9d4bSMatthew Wilcox 			pool_free_page(pool, page);
278141e9d4bSMatthew Wilcox 	}
279141e9d4bSMatthew Wilcox 
280141e9d4bSMatthew Wilcox 	kfree(pool);
281141e9d4bSMatthew Wilcox }
282e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_destroy);
283141e9d4bSMatthew Wilcox 
284141e9d4bSMatthew Wilcox /**
285141e9d4bSMatthew Wilcox  * dma_pool_alloc - get a block of consistent memory
286141e9d4bSMatthew Wilcox  * @pool: dma pool that will produce the block
287141e9d4bSMatthew Wilcox  * @mem_flags: GFP_* bitmask
288141e9d4bSMatthew Wilcox  * @handle: pointer to dma address of block
289141e9d4bSMatthew Wilcox  *
290141e9d4bSMatthew Wilcox  * This returns the kernel virtual address of a currently unused block,
291141e9d4bSMatthew Wilcox  * and reports its dma address through the handle.
2926182a094SMatthew Wilcox  * If such a memory block can't be allocated, %NULL is returned.
293141e9d4bSMatthew Wilcox  */
294e87aa773SMatthew Wilcox void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
295e87aa773SMatthew Wilcox 		     dma_addr_t *handle)
296141e9d4bSMatthew Wilcox {
297141e9d4bSMatthew Wilcox 	unsigned long flags;
298141e9d4bSMatthew Wilcox 	struct dma_page *page;
299141e9d4bSMatthew Wilcox 	size_t offset;
300141e9d4bSMatthew Wilcox 	void *retval;
301141e9d4bSMatthew Wilcox 
302ea05c844SDima Zavin 	might_sleep_if(mem_flags & __GFP_WAIT);
303ea05c844SDima Zavin 
304141e9d4bSMatthew Wilcox 	spin_lock_irqsave(&pool->lock, flags);
305141e9d4bSMatthew Wilcox 	list_for_each_entry(page, &pool->page_list, page_list) {
306a35a3455SMatthew Wilcox 		if (page->offset < pool->allocation)
307141e9d4bSMatthew Wilcox 			goto ready;
308141e9d4bSMatthew Wilcox 	}
309141e9d4bSMatthew Wilcox 
310387870f2SMarek Szyprowski 	/* pool_alloc_page() might sleep, so temporarily drop &pool->lock */
311141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
312141e9d4bSMatthew Wilcox 
313387870f2SMarek Szyprowski 	page = pool_alloc_page(pool, mem_flags);
314387870f2SMarek Szyprowski 	if (!page)
315387870f2SMarek Szyprowski 		return NULL;
316141e9d4bSMatthew Wilcox 
3172cae367eSMatthew Wilcox 	spin_lock_irqsave(&pool->lock, flags);
318141e9d4bSMatthew Wilcox 
319387870f2SMarek Szyprowski 	list_add(&page->page_list, &pool->page_list);
320141e9d4bSMatthew Wilcox  ready:
321141e9d4bSMatthew Wilcox 	page->in_use++;
322a35a3455SMatthew Wilcox 	offset = page->offset;
323a35a3455SMatthew Wilcox 	page->offset = *(int *)(page->vaddr + offset);
324141e9d4bSMatthew Wilcox 	retval = offset + page->vaddr;
325141e9d4bSMatthew Wilcox 	*handle = offset + page->dma;
326b5ee5befSAndi Kleen #ifdef	DMAPOOL_DEBUG
3275de55b26SMatthieu CASTET 	{
3285de55b26SMatthieu CASTET 		int i;
3295de55b26SMatthieu CASTET 		u8 *data = retval;
3305de55b26SMatthieu CASTET 		/* page->offset is stored in first 4 bytes */
3315de55b26SMatthieu CASTET 		for (i = sizeof(page->offset); i < pool->size; i++) {
3325de55b26SMatthieu CASTET 			if (data[i] == POOL_POISON_FREED)
3335de55b26SMatthieu CASTET 				continue;
3345de55b26SMatthieu CASTET 			if (pool->dev)
3355de55b26SMatthieu CASTET 				dev_err(pool->dev,
3365835f251SHiroshige Sato 					"dma_pool_alloc %s, %p (corrupted)\n",
3375de55b26SMatthieu CASTET 					pool->name, retval);
3385de55b26SMatthieu CASTET 			else
3395835f251SHiroshige Sato 				pr_err("dma_pool_alloc %s, %p (corrupted)\n",
3405de55b26SMatthieu CASTET 					pool->name, retval);
3415de55b26SMatthieu CASTET 
3425de55b26SMatthieu CASTET 			/*
3435de55b26SMatthieu CASTET 			 * Dump the first 4 bytes even if they are not
3445de55b26SMatthieu CASTET 			 * POOL_POISON_FREED
3455de55b26SMatthieu CASTET 			 */
3465de55b26SMatthieu CASTET 			print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1,
3475de55b26SMatthieu CASTET 					data, pool->size, 1);
3485de55b26SMatthieu CASTET 			break;
3495de55b26SMatthieu CASTET 		}
3505de55b26SMatthieu CASTET 	}
351141e9d4bSMatthew Wilcox 	memset(retval, POOL_POISON_ALLOCATED, pool->size);
352141e9d4bSMatthew Wilcox #endif
353141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
354141e9d4bSMatthew Wilcox 	return retval;
355141e9d4bSMatthew Wilcox }
356e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_alloc);
357141e9d4bSMatthew Wilcox 
358e87aa773SMatthew Wilcox static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
359141e9d4bSMatthew Wilcox {
360141e9d4bSMatthew Wilcox 	struct dma_page *page;
361141e9d4bSMatthew Wilcox 
362141e9d4bSMatthew Wilcox 	list_for_each_entry(page, &pool->page_list, page_list) {
363141e9d4bSMatthew Wilcox 		if (dma < page->dma)
364141e9d4bSMatthew Wilcox 			continue;
365141e9d4bSMatthew Wilcox 		if (dma < (page->dma + pool->allocation))
366141e9d4bSMatthew Wilcox 			return page;
367141e9d4bSMatthew Wilcox 	}
36884bc227dSRolf Eike Beer 	return NULL;
36984bc227dSRolf Eike Beer }
370141e9d4bSMatthew Wilcox 
371141e9d4bSMatthew Wilcox /**
372141e9d4bSMatthew Wilcox  * dma_pool_free - put block back into dma pool
373141e9d4bSMatthew Wilcox  * @pool: the dma pool holding the block
374141e9d4bSMatthew Wilcox  * @vaddr: virtual address of block
375141e9d4bSMatthew Wilcox  * @dma: dma address of block
376141e9d4bSMatthew Wilcox  *
377141e9d4bSMatthew Wilcox  * Caller promises neither device nor driver will again touch this block
378141e9d4bSMatthew Wilcox  * unless it is first re-allocated.
379141e9d4bSMatthew Wilcox  */
380e87aa773SMatthew Wilcox void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
381141e9d4bSMatthew Wilcox {
382141e9d4bSMatthew Wilcox 	struct dma_page *page;
383141e9d4bSMatthew Wilcox 	unsigned long flags;
384a35a3455SMatthew Wilcox 	unsigned int offset;
385141e9d4bSMatthew Wilcox 
38684bc227dSRolf Eike Beer 	spin_lock_irqsave(&pool->lock, flags);
387e87aa773SMatthew Wilcox 	page = pool_find_page(pool, dma);
388e87aa773SMatthew Wilcox 	if (!page) {
38984bc227dSRolf Eike Beer 		spin_unlock_irqrestore(&pool->lock, flags);
390141e9d4bSMatthew Wilcox 		if (pool->dev)
391e87aa773SMatthew Wilcox 			dev_err(pool->dev,
392e87aa773SMatthew Wilcox 				"dma_pool_free %s, %p/%lx (bad dma)\n",
393141e9d4bSMatthew Wilcox 				pool->name, vaddr, (unsigned long)dma);
394141e9d4bSMatthew Wilcox 		else
395141e9d4bSMatthew Wilcox 			printk(KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
396141e9d4bSMatthew Wilcox 			       pool->name, vaddr, (unsigned long)dma);
397141e9d4bSMatthew Wilcox 		return;
398141e9d4bSMatthew Wilcox 	}
399141e9d4bSMatthew Wilcox 
400a35a3455SMatthew Wilcox 	offset = vaddr - page->vaddr;
401b5ee5befSAndi Kleen #ifdef	DMAPOOL_DEBUG
402a35a3455SMatthew Wilcox 	if ((dma - page->dma) != offset) {
40384bc227dSRolf Eike Beer 		spin_unlock_irqrestore(&pool->lock, flags);
404141e9d4bSMatthew Wilcox 		if (pool->dev)
405e87aa773SMatthew Wilcox 			dev_err(pool->dev,
406e87aa773SMatthew Wilcox 				"dma_pool_free %s, %p (bad vaddr)/%Lx\n",
407141e9d4bSMatthew Wilcox 				pool->name, vaddr, (unsigned long long)dma);
408141e9d4bSMatthew Wilcox 		else
409e87aa773SMatthew Wilcox 			printk(KERN_ERR
410e87aa773SMatthew Wilcox 			       "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
411141e9d4bSMatthew Wilcox 			       pool->name, vaddr, (unsigned long long)dma);
412141e9d4bSMatthew Wilcox 		return;
413141e9d4bSMatthew Wilcox 	}
414a35a3455SMatthew Wilcox 	{
415a35a3455SMatthew Wilcox 		unsigned int chain = page->offset;
416a35a3455SMatthew Wilcox 		while (chain < pool->allocation) {
417a35a3455SMatthew Wilcox 			if (chain != offset) {
418a35a3455SMatthew Wilcox 				chain = *(int *)(page->vaddr + chain);
419a35a3455SMatthew Wilcox 				continue;
420a35a3455SMatthew Wilcox 			}
42184bc227dSRolf Eike Beer 			spin_unlock_irqrestore(&pool->lock, flags);
422141e9d4bSMatthew Wilcox 			if (pool->dev)
423a35a3455SMatthew Wilcox 				dev_err(pool->dev, "dma_pool_free %s, dma %Lx "
424a35a3455SMatthew Wilcox 					"already free\n", pool->name,
425a35a3455SMatthew Wilcox 					(unsigned long long)dma);
426141e9d4bSMatthew Wilcox 			else
427a35a3455SMatthew Wilcox 				printk(KERN_ERR "dma_pool_free %s, dma %Lx "
428a35a3455SMatthew Wilcox 					"already free\n", pool->name,
429a35a3455SMatthew Wilcox 					(unsigned long long)dma);
430141e9d4bSMatthew Wilcox 			return;
431141e9d4bSMatthew Wilcox 		}
432a35a3455SMatthew Wilcox 	}
433141e9d4bSMatthew Wilcox 	memset(vaddr, POOL_POISON_FREED, pool->size);
434141e9d4bSMatthew Wilcox #endif
435141e9d4bSMatthew Wilcox 
436141e9d4bSMatthew Wilcox 	page->in_use--;
437a35a3455SMatthew Wilcox 	*(int *)vaddr = page->offset;
438a35a3455SMatthew Wilcox 	page->offset = offset;
439141e9d4bSMatthew Wilcox 	/*
440141e9d4bSMatthew Wilcox 	 * Resist a temptation to do
441a35a3455SMatthew Wilcox 	 *    if (!is_page_busy(page)) pool_free_page(pool, page);
442141e9d4bSMatthew Wilcox 	 * Better have a few empty pages hang around.
443141e9d4bSMatthew Wilcox 	 */
444141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
445141e9d4bSMatthew Wilcox }
446e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_free);
447141e9d4bSMatthew Wilcox 
448141e9d4bSMatthew Wilcox /*
449141e9d4bSMatthew Wilcox  * Managed DMA pool
450141e9d4bSMatthew Wilcox  */
451141e9d4bSMatthew Wilcox static void dmam_pool_release(struct device *dev, void *res)
452141e9d4bSMatthew Wilcox {
453141e9d4bSMatthew Wilcox 	struct dma_pool *pool = *(struct dma_pool **)res;
454141e9d4bSMatthew Wilcox 
455141e9d4bSMatthew Wilcox 	dma_pool_destroy(pool);
456141e9d4bSMatthew Wilcox }
457141e9d4bSMatthew Wilcox 
458141e9d4bSMatthew Wilcox static int dmam_pool_match(struct device *dev, void *res, void *match_data)
459141e9d4bSMatthew Wilcox {
460141e9d4bSMatthew Wilcox 	return *(struct dma_pool **)res == match_data;
461141e9d4bSMatthew Wilcox }
462141e9d4bSMatthew Wilcox 
463141e9d4bSMatthew Wilcox /**
464141e9d4bSMatthew Wilcox  * dmam_pool_create - Managed dma_pool_create()
465141e9d4bSMatthew Wilcox  * @name: name of pool, for diagnostics
466141e9d4bSMatthew Wilcox  * @dev: device that will be doing the DMA
467141e9d4bSMatthew Wilcox  * @size: size of the blocks in this pool.
468141e9d4bSMatthew Wilcox  * @align: alignment requirement for blocks; must be a power of two
469141e9d4bSMatthew Wilcox  * @allocation: returned blocks won't cross this boundary (or zero)
470141e9d4bSMatthew Wilcox  *
471141e9d4bSMatthew Wilcox  * Managed dma_pool_create().  DMA pool created with this function is
472141e9d4bSMatthew Wilcox  * automatically destroyed on driver detach.
473141e9d4bSMatthew Wilcox  */
474141e9d4bSMatthew Wilcox struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
475141e9d4bSMatthew Wilcox 				  size_t size, size_t align, size_t allocation)
476141e9d4bSMatthew Wilcox {
477141e9d4bSMatthew Wilcox 	struct dma_pool **ptr, *pool;
478141e9d4bSMatthew Wilcox 
479141e9d4bSMatthew Wilcox 	ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
480141e9d4bSMatthew Wilcox 	if (!ptr)
481141e9d4bSMatthew Wilcox 		return NULL;
482141e9d4bSMatthew Wilcox 
483141e9d4bSMatthew Wilcox 	pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
484141e9d4bSMatthew Wilcox 	if (pool)
485141e9d4bSMatthew Wilcox 		devres_add(dev, ptr);
486141e9d4bSMatthew Wilcox 	else
487141e9d4bSMatthew Wilcox 		devres_free(ptr);
488141e9d4bSMatthew Wilcox 
489141e9d4bSMatthew Wilcox 	return pool;
490141e9d4bSMatthew Wilcox }
491e87aa773SMatthew Wilcox EXPORT_SYMBOL(dmam_pool_create);
492141e9d4bSMatthew Wilcox 
493141e9d4bSMatthew Wilcox /**
494141e9d4bSMatthew Wilcox  * dmam_pool_destroy - Managed dma_pool_destroy()
495141e9d4bSMatthew Wilcox  * @pool: dma pool that will be destroyed
496141e9d4bSMatthew Wilcox  *
497141e9d4bSMatthew Wilcox  * Managed dma_pool_destroy().
498141e9d4bSMatthew Wilcox  */
499141e9d4bSMatthew Wilcox void dmam_pool_destroy(struct dma_pool *pool)
500141e9d4bSMatthew Wilcox {
501141e9d4bSMatthew Wilcox 	struct device *dev = pool->dev;
502141e9d4bSMatthew Wilcox 
503*172cb4b3SAndy Shevchenko 	WARN_ON(devres_release(dev, dmam_pool_release, dmam_pool_match, pool));
504141e9d4bSMatthew Wilcox }
505141e9d4bSMatthew Wilcox EXPORT_SYMBOL(dmam_pool_destroy);
506