xref: /openbmc/linux/mm/dmapool.c (revision a35a3455142976e3fffdf27027f3082cbaba6e8c)
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
20*a35a3455SMatthew Wilcox  * least 'size' bytes.  Free blocks are tracked in an unsorted singly-linked
21*a35a3455SMatthew Wilcox  * list of free blocks within the page.  Used blocks aren't tracked, but we
22*a35a3455SMatthew 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>
30141e9d4bSMatthew Wilcox #include <linux/module.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>
356182a094SMatthew Wilcox #include <linux/spinlock.h>
366182a094SMatthew Wilcox #include <linux/string.h>
376182a094SMatthew Wilcox #include <linux/types.h>
386182a094SMatthew Wilcox #include <linux/wait.h>
39141e9d4bSMatthew Wilcox 
40141e9d4bSMatthew Wilcox struct dma_pool {		/* the pool */
41141e9d4bSMatthew Wilcox 	struct list_head page_list;
42141e9d4bSMatthew Wilcox 	spinlock_t lock;
43141e9d4bSMatthew Wilcox 	size_t size;
44141e9d4bSMatthew Wilcox 	struct device *dev;
45141e9d4bSMatthew Wilcox 	size_t allocation;
46141e9d4bSMatthew Wilcox 	char name[32];
47141e9d4bSMatthew Wilcox 	wait_queue_head_t waitq;
48141e9d4bSMatthew Wilcox 	struct list_head pools;
49141e9d4bSMatthew Wilcox };
50141e9d4bSMatthew Wilcox 
51141e9d4bSMatthew Wilcox struct dma_page {		/* cacheable header for 'allocation' bytes */
52141e9d4bSMatthew Wilcox 	struct list_head page_list;
53141e9d4bSMatthew Wilcox 	void *vaddr;
54141e9d4bSMatthew Wilcox 	dma_addr_t dma;
55*a35a3455SMatthew Wilcox 	unsigned int in_use;
56*a35a3455SMatthew Wilcox 	unsigned int offset;
57141e9d4bSMatthew Wilcox };
58141e9d4bSMatthew Wilcox 
59141e9d4bSMatthew Wilcox #define	POOL_TIMEOUT_JIFFIES	((100 /* msec */ * HZ) / 1000)
60141e9d4bSMatthew Wilcox 
61141e9d4bSMatthew Wilcox static DEFINE_MUTEX(pools_lock);
62141e9d4bSMatthew Wilcox 
63141e9d4bSMatthew Wilcox static ssize_t
64141e9d4bSMatthew Wilcox show_pools(struct device *dev, struct device_attribute *attr, char *buf)
65141e9d4bSMatthew Wilcox {
66141e9d4bSMatthew Wilcox 	unsigned temp;
67141e9d4bSMatthew Wilcox 	unsigned size;
68141e9d4bSMatthew Wilcox 	char *next;
69141e9d4bSMatthew Wilcox 	struct dma_page *page;
70141e9d4bSMatthew Wilcox 	struct dma_pool *pool;
71141e9d4bSMatthew Wilcox 
72141e9d4bSMatthew Wilcox 	next = buf;
73141e9d4bSMatthew Wilcox 	size = PAGE_SIZE;
74141e9d4bSMatthew Wilcox 
75141e9d4bSMatthew Wilcox 	temp = scnprintf(next, size, "poolinfo - 0.1\n");
76141e9d4bSMatthew Wilcox 	size -= temp;
77141e9d4bSMatthew Wilcox 	next += temp;
78141e9d4bSMatthew Wilcox 
79141e9d4bSMatthew Wilcox 	mutex_lock(&pools_lock);
80141e9d4bSMatthew Wilcox 	list_for_each_entry(pool, &dev->dma_pools, pools) {
81141e9d4bSMatthew Wilcox 		unsigned pages = 0;
82141e9d4bSMatthew Wilcox 		unsigned blocks = 0;
83141e9d4bSMatthew Wilcox 
84141e9d4bSMatthew Wilcox 		list_for_each_entry(page, &pool->page_list, page_list) {
85141e9d4bSMatthew Wilcox 			pages++;
86141e9d4bSMatthew Wilcox 			blocks += page->in_use;
87141e9d4bSMatthew Wilcox 		}
88141e9d4bSMatthew Wilcox 
89141e9d4bSMatthew Wilcox 		/* per-pool info, no real statistics yet */
90141e9d4bSMatthew Wilcox 		temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
91*a35a3455SMatthew Wilcox 				 pool->name, blocks,
92*a35a3455SMatthew Wilcox 				 pages * (pool->allocation / pool->size),
93141e9d4bSMatthew Wilcox 				 pool->size, pages);
94141e9d4bSMatthew Wilcox 		size -= temp;
95141e9d4bSMatthew Wilcox 		next += temp;
96141e9d4bSMatthew Wilcox 	}
97141e9d4bSMatthew Wilcox 	mutex_unlock(&pools_lock);
98141e9d4bSMatthew Wilcox 
99141e9d4bSMatthew Wilcox 	return PAGE_SIZE - size;
100141e9d4bSMatthew Wilcox }
101e87aa773SMatthew Wilcox 
102141e9d4bSMatthew Wilcox static DEVICE_ATTR(pools, S_IRUGO, show_pools, NULL);
103141e9d4bSMatthew Wilcox 
104141e9d4bSMatthew Wilcox /**
105141e9d4bSMatthew Wilcox  * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
106141e9d4bSMatthew Wilcox  * @name: name of pool, for diagnostics
107141e9d4bSMatthew Wilcox  * @dev: device that will be doing the DMA
108141e9d4bSMatthew Wilcox  * @size: size of the blocks in this pool.
109141e9d4bSMatthew Wilcox  * @align: alignment requirement for blocks; must be a power of two
110141e9d4bSMatthew Wilcox  * @allocation: returned blocks won't cross this boundary (or zero)
111141e9d4bSMatthew Wilcox  * Context: !in_interrupt()
112141e9d4bSMatthew Wilcox  *
113141e9d4bSMatthew Wilcox  * Returns a dma allocation pool with the requested characteristics, or
114141e9d4bSMatthew Wilcox  * null if one can't be created.  Given one of these pools, dma_pool_alloc()
115141e9d4bSMatthew Wilcox  * may be used to allocate memory.  Such memory will all have "consistent"
116141e9d4bSMatthew Wilcox  * DMA mappings, accessible by the device and its driver without using
117141e9d4bSMatthew Wilcox  * cache flushing primitives.  The actual size of blocks allocated may be
118141e9d4bSMatthew Wilcox  * larger than requested because of alignment.
119141e9d4bSMatthew Wilcox  *
120141e9d4bSMatthew Wilcox  * If allocation is nonzero, objects returned from dma_pool_alloc() won't
121141e9d4bSMatthew Wilcox  * cross that size boundary.  This is useful for devices which have
122141e9d4bSMatthew Wilcox  * addressing restrictions on individual DMA transfers, such as not crossing
123141e9d4bSMatthew Wilcox  * boundaries of 4KBytes.
124141e9d4bSMatthew Wilcox  */
125e87aa773SMatthew Wilcox struct dma_pool *dma_pool_create(const char *name, struct device *dev,
126141e9d4bSMatthew Wilcox 				 size_t size, size_t align, size_t allocation)
127141e9d4bSMatthew Wilcox {
128141e9d4bSMatthew Wilcox 	struct dma_pool *retval;
129141e9d4bSMatthew Wilcox 
130399154beSMatthew Wilcox 	if (align == 0) {
131141e9d4bSMatthew Wilcox 		align = 1;
132399154beSMatthew Wilcox 	} else if (align & (align - 1)) {
133399154beSMatthew Wilcox 		return NULL;
134399154beSMatthew Wilcox 	}
135399154beSMatthew Wilcox 
136*a35a3455SMatthew Wilcox 	if (size == 0) {
137141e9d4bSMatthew Wilcox 		return NULL;
138*a35a3455SMatthew Wilcox 	} else if (size < 4) {
139*a35a3455SMatthew Wilcox 		size = 4;
140*a35a3455SMatthew Wilcox 	}
141399154beSMatthew Wilcox 
142399154beSMatthew Wilcox 	if ((size % align) != 0)
143399154beSMatthew Wilcox 		size = ALIGN(size, align);
144141e9d4bSMatthew Wilcox 
145141e9d4bSMatthew Wilcox 	if (allocation == 0) {
146141e9d4bSMatthew Wilcox 		if (PAGE_SIZE < size)
147141e9d4bSMatthew Wilcox 			allocation = size;
148141e9d4bSMatthew Wilcox 		else
149141e9d4bSMatthew Wilcox 			allocation = PAGE_SIZE;
150e87aa773SMatthew Wilcox 		/* FIXME: round up for less fragmentation */
151141e9d4bSMatthew Wilcox 	} else if (allocation < size)
152141e9d4bSMatthew Wilcox 		return NULL;
153141e9d4bSMatthew Wilcox 
154e87aa773SMatthew Wilcox 	if (!
155e87aa773SMatthew Wilcox 	    (retval =
156e87aa773SMatthew Wilcox 	     kmalloc_node(sizeof *retval, GFP_KERNEL, dev_to_node(dev))))
157141e9d4bSMatthew Wilcox 		return retval;
158141e9d4bSMatthew Wilcox 
159141e9d4bSMatthew Wilcox 	strlcpy(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;
166141e9d4bSMatthew Wilcox 	retval->allocation = allocation;
167141e9d4bSMatthew Wilcox 	init_waitqueue_head(&retval->waitq);
168141e9d4bSMatthew Wilcox 
169141e9d4bSMatthew Wilcox 	if (dev) {
170141e9d4bSMatthew Wilcox 		int ret;
171141e9d4bSMatthew Wilcox 
172141e9d4bSMatthew Wilcox 		mutex_lock(&pools_lock);
173141e9d4bSMatthew Wilcox 		if (list_empty(&dev->dma_pools))
174141e9d4bSMatthew Wilcox 			ret = device_create_file(dev, &dev_attr_pools);
175141e9d4bSMatthew Wilcox 		else
176141e9d4bSMatthew Wilcox 			ret = 0;
177141e9d4bSMatthew Wilcox 		/* note:  not currently insisting "name" be unique */
178141e9d4bSMatthew Wilcox 		if (!ret)
179141e9d4bSMatthew Wilcox 			list_add(&retval->pools, &dev->dma_pools);
180141e9d4bSMatthew Wilcox 		else {
181141e9d4bSMatthew Wilcox 			kfree(retval);
182141e9d4bSMatthew Wilcox 			retval = NULL;
183141e9d4bSMatthew Wilcox 		}
184141e9d4bSMatthew Wilcox 		mutex_unlock(&pools_lock);
185141e9d4bSMatthew Wilcox 	} else
186141e9d4bSMatthew Wilcox 		INIT_LIST_HEAD(&retval->pools);
187141e9d4bSMatthew Wilcox 
188141e9d4bSMatthew Wilcox 	return retval;
189141e9d4bSMatthew Wilcox }
190e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_create);
191141e9d4bSMatthew Wilcox 
192*a35a3455SMatthew Wilcox static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page)
193*a35a3455SMatthew Wilcox {
194*a35a3455SMatthew Wilcox 	unsigned int offset = 0;
195*a35a3455SMatthew Wilcox 
196*a35a3455SMatthew Wilcox 	do {
197*a35a3455SMatthew Wilcox 		unsigned int next = offset + pool->size;
198*a35a3455SMatthew Wilcox 		if (unlikely((next + pool->size) >= pool->allocation))
199*a35a3455SMatthew Wilcox 			next = pool->allocation;
200*a35a3455SMatthew Wilcox 		*(int *)(page->vaddr + offset) = next;
201*a35a3455SMatthew Wilcox 		offset = next;
202*a35a3455SMatthew Wilcox 	} while (offset < pool->allocation);
203*a35a3455SMatthew Wilcox }
204*a35a3455SMatthew Wilcox 
205e87aa773SMatthew Wilcox static struct dma_page *pool_alloc_page(struct dma_pool *pool, gfp_t mem_flags)
206141e9d4bSMatthew Wilcox {
207141e9d4bSMatthew Wilcox 	struct dma_page *page;
208141e9d4bSMatthew Wilcox 
209*a35a3455SMatthew Wilcox 	page = kmalloc(sizeof(*page), mem_flags);
210141e9d4bSMatthew Wilcox 	if (!page)
211141e9d4bSMatthew Wilcox 		return NULL;
212*a35a3455SMatthew Wilcox 	page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation,
213e87aa773SMatthew Wilcox 					 &page->dma, mem_flags);
214141e9d4bSMatthew Wilcox 	if (page->vaddr) {
215141e9d4bSMatthew Wilcox #ifdef	CONFIG_DEBUG_SLAB
216141e9d4bSMatthew Wilcox 		memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
217141e9d4bSMatthew Wilcox #endif
218*a35a3455SMatthew Wilcox 		pool_initialise_page(pool, page);
219141e9d4bSMatthew Wilcox 		list_add(&page->page_list, &pool->page_list);
220141e9d4bSMatthew Wilcox 		page->in_use = 0;
221*a35a3455SMatthew Wilcox 		page->offset = 0;
222141e9d4bSMatthew Wilcox 	} else {
223141e9d4bSMatthew Wilcox 		kfree(page);
224141e9d4bSMatthew Wilcox 		page = NULL;
225141e9d4bSMatthew Wilcox 	}
226141e9d4bSMatthew Wilcox 	return page;
227141e9d4bSMatthew Wilcox }
228141e9d4bSMatthew Wilcox 
229*a35a3455SMatthew Wilcox static inline int is_page_busy(struct dma_page *page)
230141e9d4bSMatthew Wilcox {
231*a35a3455SMatthew Wilcox 	return page->in_use != 0;
232141e9d4bSMatthew Wilcox }
233141e9d4bSMatthew Wilcox 
234e87aa773SMatthew Wilcox static void pool_free_page(struct dma_pool *pool, struct dma_page *page)
235141e9d4bSMatthew Wilcox {
236141e9d4bSMatthew Wilcox 	dma_addr_t dma = page->dma;
237141e9d4bSMatthew Wilcox 
238141e9d4bSMatthew Wilcox #ifdef	CONFIG_DEBUG_SLAB
239141e9d4bSMatthew Wilcox 	memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
240141e9d4bSMatthew Wilcox #endif
241141e9d4bSMatthew Wilcox 	dma_free_coherent(pool->dev, pool->allocation, page->vaddr, dma);
242141e9d4bSMatthew Wilcox 	list_del(&page->page_list);
243141e9d4bSMatthew Wilcox 	kfree(page);
244141e9d4bSMatthew Wilcox }
245141e9d4bSMatthew Wilcox 
246141e9d4bSMatthew Wilcox /**
247141e9d4bSMatthew Wilcox  * dma_pool_destroy - destroys a pool of dma memory blocks.
248141e9d4bSMatthew Wilcox  * @pool: dma pool that will be destroyed
249141e9d4bSMatthew Wilcox  * Context: !in_interrupt()
250141e9d4bSMatthew Wilcox  *
251141e9d4bSMatthew Wilcox  * Caller guarantees that no more memory from the pool is in use,
252141e9d4bSMatthew Wilcox  * and that nothing will try to use the pool after this call.
253141e9d4bSMatthew Wilcox  */
254e87aa773SMatthew Wilcox void dma_pool_destroy(struct dma_pool *pool)
255141e9d4bSMatthew Wilcox {
256141e9d4bSMatthew Wilcox 	mutex_lock(&pools_lock);
257141e9d4bSMatthew Wilcox 	list_del(&pool->pools);
258141e9d4bSMatthew Wilcox 	if (pool->dev && list_empty(&pool->dev->dma_pools))
259141e9d4bSMatthew Wilcox 		device_remove_file(pool->dev, &dev_attr_pools);
260141e9d4bSMatthew Wilcox 	mutex_unlock(&pools_lock);
261141e9d4bSMatthew Wilcox 
262141e9d4bSMatthew Wilcox 	while (!list_empty(&pool->page_list)) {
263141e9d4bSMatthew Wilcox 		struct dma_page *page;
264141e9d4bSMatthew Wilcox 		page = list_entry(pool->page_list.next,
265141e9d4bSMatthew Wilcox 				  struct dma_page, page_list);
266*a35a3455SMatthew Wilcox 		if (is_page_busy(page)) {
267141e9d4bSMatthew Wilcox 			if (pool->dev)
268e87aa773SMatthew Wilcox 				dev_err(pool->dev,
269e87aa773SMatthew Wilcox 					"dma_pool_destroy %s, %p busy\n",
270141e9d4bSMatthew Wilcox 					pool->name, page->vaddr);
271141e9d4bSMatthew Wilcox 			else
272e87aa773SMatthew Wilcox 				printk(KERN_ERR
273e87aa773SMatthew Wilcox 				       "dma_pool_destroy %s, %p busy\n",
274141e9d4bSMatthew Wilcox 				       pool->name, page->vaddr);
275141e9d4bSMatthew Wilcox 			/* leak the still-in-use consistent memory */
276141e9d4bSMatthew Wilcox 			list_del(&page->page_list);
277141e9d4bSMatthew Wilcox 			kfree(page);
278141e9d4bSMatthew Wilcox 		} else
279141e9d4bSMatthew Wilcox 			pool_free_page(pool, page);
280141e9d4bSMatthew Wilcox 	}
281141e9d4bSMatthew Wilcox 
282141e9d4bSMatthew Wilcox 	kfree(pool);
283141e9d4bSMatthew Wilcox }
284e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_destroy);
285141e9d4bSMatthew Wilcox 
286141e9d4bSMatthew Wilcox /**
287141e9d4bSMatthew Wilcox  * dma_pool_alloc - get a block of consistent memory
288141e9d4bSMatthew Wilcox  * @pool: dma pool that will produce the block
289141e9d4bSMatthew Wilcox  * @mem_flags: GFP_* bitmask
290141e9d4bSMatthew Wilcox  * @handle: pointer to dma address of block
291141e9d4bSMatthew Wilcox  *
292141e9d4bSMatthew Wilcox  * This returns the kernel virtual address of a currently unused block,
293141e9d4bSMatthew Wilcox  * and reports its dma address through the handle.
2946182a094SMatthew Wilcox  * If such a memory block can't be allocated, %NULL is returned.
295141e9d4bSMatthew Wilcox  */
296e87aa773SMatthew Wilcox void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
297e87aa773SMatthew Wilcox 		     dma_addr_t *handle)
298141e9d4bSMatthew Wilcox {
299141e9d4bSMatthew Wilcox 	unsigned long flags;
300141e9d4bSMatthew Wilcox 	struct dma_page *page;
301141e9d4bSMatthew Wilcox 	size_t offset;
302141e9d4bSMatthew Wilcox 	void *retval;
303141e9d4bSMatthew Wilcox 
304141e9d4bSMatthew Wilcox 	spin_lock_irqsave(&pool->lock, flags);
3052cae367eSMatthew Wilcox  restart:
306141e9d4bSMatthew Wilcox 	list_for_each_entry(page, &pool->page_list, page_list) {
307*a35a3455SMatthew Wilcox 		if (page->offset < pool->allocation)
308141e9d4bSMatthew Wilcox 			goto ready;
309141e9d4bSMatthew Wilcox 	}
310e87aa773SMatthew Wilcox 	page = pool_alloc_page(pool, GFP_ATOMIC);
311e87aa773SMatthew Wilcox 	if (!page) {
312141e9d4bSMatthew Wilcox 		if (mem_flags & __GFP_WAIT) {
313141e9d4bSMatthew Wilcox 			DECLARE_WAITQUEUE(wait, current);
314141e9d4bSMatthew Wilcox 
315141e9d4bSMatthew Wilcox 			__set_current_state(TASK_INTERRUPTIBLE);
3162cae367eSMatthew Wilcox 			__add_wait_queue(&pool->waitq, &wait);
317141e9d4bSMatthew Wilcox 			spin_unlock_irqrestore(&pool->lock, flags);
318141e9d4bSMatthew Wilcox 
319141e9d4bSMatthew Wilcox 			schedule_timeout(POOL_TIMEOUT_JIFFIES);
320141e9d4bSMatthew Wilcox 
3212cae367eSMatthew Wilcox 			spin_lock_irqsave(&pool->lock, flags);
3222cae367eSMatthew Wilcox 			__remove_wait_queue(&pool->waitq, &wait);
323141e9d4bSMatthew Wilcox 			goto restart;
324141e9d4bSMatthew Wilcox 		}
325141e9d4bSMatthew Wilcox 		retval = NULL;
326141e9d4bSMatthew Wilcox 		goto done;
327141e9d4bSMatthew Wilcox 	}
328141e9d4bSMatthew Wilcox 
329141e9d4bSMatthew Wilcox  ready:
330141e9d4bSMatthew Wilcox 	page->in_use++;
331*a35a3455SMatthew Wilcox 	offset = page->offset;
332*a35a3455SMatthew Wilcox 	page->offset = *(int *)(page->vaddr + offset);
333141e9d4bSMatthew Wilcox 	retval = offset + page->vaddr;
334141e9d4bSMatthew Wilcox 	*handle = offset + page->dma;
335141e9d4bSMatthew Wilcox #ifdef	CONFIG_DEBUG_SLAB
336141e9d4bSMatthew Wilcox 	memset(retval, POOL_POISON_ALLOCATED, pool->size);
337141e9d4bSMatthew Wilcox #endif
338141e9d4bSMatthew Wilcox  done:
339141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
340141e9d4bSMatthew Wilcox 	return retval;
341141e9d4bSMatthew Wilcox }
342e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_alloc);
343141e9d4bSMatthew Wilcox 
344e87aa773SMatthew Wilcox static struct dma_page *pool_find_page(struct dma_pool *pool, dma_addr_t dma)
345141e9d4bSMatthew Wilcox {
346141e9d4bSMatthew Wilcox 	unsigned long flags;
347141e9d4bSMatthew Wilcox 	struct dma_page *page;
348141e9d4bSMatthew Wilcox 
349141e9d4bSMatthew Wilcox 	spin_lock_irqsave(&pool->lock, flags);
350141e9d4bSMatthew Wilcox 	list_for_each_entry(page, &pool->page_list, page_list) {
351141e9d4bSMatthew Wilcox 		if (dma < page->dma)
352141e9d4bSMatthew Wilcox 			continue;
353141e9d4bSMatthew Wilcox 		if (dma < (page->dma + pool->allocation))
354141e9d4bSMatthew Wilcox 			goto done;
355141e9d4bSMatthew Wilcox 	}
356141e9d4bSMatthew Wilcox 	page = NULL;
357141e9d4bSMatthew Wilcox  done:
358141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
359141e9d4bSMatthew Wilcox 	return page;
360141e9d4bSMatthew Wilcox }
361141e9d4bSMatthew Wilcox 
362141e9d4bSMatthew Wilcox /**
363141e9d4bSMatthew Wilcox  * dma_pool_free - put block back into dma pool
364141e9d4bSMatthew Wilcox  * @pool: the dma pool holding the block
365141e9d4bSMatthew Wilcox  * @vaddr: virtual address of block
366141e9d4bSMatthew Wilcox  * @dma: dma address of block
367141e9d4bSMatthew Wilcox  *
368141e9d4bSMatthew Wilcox  * Caller promises neither device nor driver will again touch this block
369141e9d4bSMatthew Wilcox  * unless it is first re-allocated.
370141e9d4bSMatthew Wilcox  */
371e87aa773SMatthew Wilcox void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)
372141e9d4bSMatthew Wilcox {
373141e9d4bSMatthew Wilcox 	struct dma_page *page;
374141e9d4bSMatthew Wilcox 	unsigned long flags;
375*a35a3455SMatthew Wilcox 	unsigned int offset;
376141e9d4bSMatthew Wilcox 
377e87aa773SMatthew Wilcox 	page = pool_find_page(pool, dma);
378e87aa773SMatthew Wilcox 	if (!page) {
379141e9d4bSMatthew Wilcox 		if (pool->dev)
380e87aa773SMatthew Wilcox 			dev_err(pool->dev,
381e87aa773SMatthew Wilcox 				"dma_pool_free %s, %p/%lx (bad dma)\n",
382141e9d4bSMatthew Wilcox 				pool->name, vaddr, (unsigned long)dma);
383141e9d4bSMatthew Wilcox 		else
384141e9d4bSMatthew Wilcox 			printk(KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
385141e9d4bSMatthew Wilcox 			       pool->name, vaddr, (unsigned long)dma);
386141e9d4bSMatthew Wilcox 		return;
387141e9d4bSMatthew Wilcox 	}
388141e9d4bSMatthew Wilcox 
389*a35a3455SMatthew Wilcox 	offset = vaddr - page->vaddr;
390141e9d4bSMatthew Wilcox #ifdef	CONFIG_DEBUG_SLAB
391*a35a3455SMatthew Wilcox 	if ((dma - page->dma) != offset) {
392141e9d4bSMatthew Wilcox 		if (pool->dev)
393e87aa773SMatthew Wilcox 			dev_err(pool->dev,
394e87aa773SMatthew Wilcox 				"dma_pool_free %s, %p (bad vaddr)/%Lx\n",
395141e9d4bSMatthew Wilcox 				pool->name, vaddr, (unsigned long long)dma);
396141e9d4bSMatthew Wilcox 		else
397e87aa773SMatthew Wilcox 			printk(KERN_ERR
398e87aa773SMatthew Wilcox 			       "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
399141e9d4bSMatthew Wilcox 			       pool->name, vaddr, (unsigned long long)dma);
400141e9d4bSMatthew Wilcox 		return;
401141e9d4bSMatthew Wilcox 	}
402*a35a3455SMatthew Wilcox 	{
403*a35a3455SMatthew Wilcox 		unsigned int chain = page->offset;
404*a35a3455SMatthew Wilcox 		while (chain < pool->allocation) {
405*a35a3455SMatthew Wilcox 			if (chain != offset) {
406*a35a3455SMatthew Wilcox 				chain = *(int *)(page->vaddr + chain);
407*a35a3455SMatthew Wilcox 				continue;
408*a35a3455SMatthew Wilcox 			}
409141e9d4bSMatthew Wilcox 			if (pool->dev)
410*a35a3455SMatthew Wilcox 				dev_err(pool->dev, "dma_pool_free %s, dma %Lx "
411*a35a3455SMatthew Wilcox 					"already free\n", pool->name,
412*a35a3455SMatthew Wilcox 					(unsigned long long)dma);
413141e9d4bSMatthew Wilcox 			else
414*a35a3455SMatthew Wilcox 				printk(KERN_ERR "dma_pool_free %s, dma %Lx "
415*a35a3455SMatthew Wilcox 					"already free\n", pool->name,
416*a35a3455SMatthew Wilcox 					(unsigned long long)dma);
417141e9d4bSMatthew Wilcox 			return;
418141e9d4bSMatthew Wilcox 		}
419*a35a3455SMatthew Wilcox 	}
420141e9d4bSMatthew Wilcox 	memset(vaddr, POOL_POISON_FREED, pool->size);
421141e9d4bSMatthew Wilcox #endif
422141e9d4bSMatthew Wilcox 
423141e9d4bSMatthew Wilcox 	spin_lock_irqsave(&pool->lock, flags);
424141e9d4bSMatthew Wilcox 	page->in_use--;
425*a35a3455SMatthew Wilcox 	*(int *)vaddr = page->offset;
426*a35a3455SMatthew Wilcox 	page->offset = offset;
427141e9d4bSMatthew Wilcox 	if (waitqueue_active(&pool->waitq))
4282cae367eSMatthew Wilcox 		wake_up_locked(&pool->waitq);
429141e9d4bSMatthew Wilcox 	/*
430141e9d4bSMatthew Wilcox 	 * Resist a temptation to do
431*a35a3455SMatthew Wilcox 	 *    if (!is_page_busy(page)) pool_free_page(pool, page);
432141e9d4bSMatthew Wilcox 	 * Better have a few empty pages hang around.
433141e9d4bSMatthew Wilcox 	 */
434141e9d4bSMatthew Wilcox 	spin_unlock_irqrestore(&pool->lock, flags);
435141e9d4bSMatthew Wilcox }
436e87aa773SMatthew Wilcox EXPORT_SYMBOL(dma_pool_free);
437141e9d4bSMatthew Wilcox 
438141e9d4bSMatthew Wilcox /*
439141e9d4bSMatthew Wilcox  * Managed DMA pool
440141e9d4bSMatthew Wilcox  */
441141e9d4bSMatthew Wilcox static void dmam_pool_release(struct device *dev, void *res)
442141e9d4bSMatthew Wilcox {
443141e9d4bSMatthew Wilcox 	struct dma_pool *pool = *(struct dma_pool **)res;
444141e9d4bSMatthew Wilcox 
445141e9d4bSMatthew Wilcox 	dma_pool_destroy(pool);
446141e9d4bSMatthew Wilcox }
447141e9d4bSMatthew Wilcox 
448141e9d4bSMatthew Wilcox static int dmam_pool_match(struct device *dev, void *res, void *match_data)
449141e9d4bSMatthew Wilcox {
450141e9d4bSMatthew Wilcox 	return *(struct dma_pool **)res == match_data;
451141e9d4bSMatthew Wilcox }
452141e9d4bSMatthew Wilcox 
453141e9d4bSMatthew Wilcox /**
454141e9d4bSMatthew Wilcox  * dmam_pool_create - Managed dma_pool_create()
455141e9d4bSMatthew Wilcox  * @name: name of pool, for diagnostics
456141e9d4bSMatthew Wilcox  * @dev: device that will be doing the DMA
457141e9d4bSMatthew Wilcox  * @size: size of the blocks in this pool.
458141e9d4bSMatthew Wilcox  * @align: alignment requirement for blocks; must be a power of two
459141e9d4bSMatthew Wilcox  * @allocation: returned blocks won't cross this boundary (or zero)
460141e9d4bSMatthew Wilcox  *
461141e9d4bSMatthew Wilcox  * Managed dma_pool_create().  DMA pool created with this function is
462141e9d4bSMatthew Wilcox  * automatically destroyed on driver detach.
463141e9d4bSMatthew Wilcox  */
464141e9d4bSMatthew Wilcox struct dma_pool *dmam_pool_create(const char *name, struct device *dev,
465141e9d4bSMatthew Wilcox 				  size_t size, size_t align, size_t allocation)
466141e9d4bSMatthew Wilcox {
467141e9d4bSMatthew Wilcox 	struct dma_pool **ptr, *pool;
468141e9d4bSMatthew Wilcox 
469141e9d4bSMatthew Wilcox 	ptr = devres_alloc(dmam_pool_release, sizeof(*ptr), GFP_KERNEL);
470141e9d4bSMatthew Wilcox 	if (!ptr)
471141e9d4bSMatthew Wilcox 		return NULL;
472141e9d4bSMatthew Wilcox 
473141e9d4bSMatthew Wilcox 	pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
474141e9d4bSMatthew Wilcox 	if (pool)
475141e9d4bSMatthew Wilcox 		devres_add(dev, ptr);
476141e9d4bSMatthew Wilcox 	else
477141e9d4bSMatthew Wilcox 		devres_free(ptr);
478141e9d4bSMatthew Wilcox 
479141e9d4bSMatthew Wilcox 	return pool;
480141e9d4bSMatthew Wilcox }
481e87aa773SMatthew Wilcox EXPORT_SYMBOL(dmam_pool_create);
482141e9d4bSMatthew Wilcox 
483141e9d4bSMatthew Wilcox /**
484141e9d4bSMatthew Wilcox  * dmam_pool_destroy - Managed dma_pool_destroy()
485141e9d4bSMatthew Wilcox  * @pool: dma pool that will be destroyed
486141e9d4bSMatthew Wilcox  *
487141e9d4bSMatthew Wilcox  * Managed dma_pool_destroy().
488141e9d4bSMatthew Wilcox  */
489141e9d4bSMatthew Wilcox void dmam_pool_destroy(struct dma_pool *pool)
490141e9d4bSMatthew Wilcox {
491141e9d4bSMatthew Wilcox 	struct device *dev = pool->dev;
492141e9d4bSMatthew Wilcox 
493141e9d4bSMatthew Wilcox 	dma_pool_destroy(pool);
494141e9d4bSMatthew Wilcox 	WARN_ON(devres_destroy(dev, dmam_pool_release, dmam_pool_match, pool));
495141e9d4bSMatthew Wilcox }
496141e9d4bSMatthew Wilcox EXPORT_SYMBOL(dmam_pool_destroy);
497