2 #include <linux/device.h>
4 #include <asm/io.h> /* Needed for i386 to build */
5 #include <asm/scatterlist.h> /* Needed for i386 to build */
6 #include <linux/dma-mapping.h>
7 #include <linux/dmapool.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
12 * Pool allocator ... wraps the dma_alloc_coherent page allocator, so
13 * small blocks are easily used by drivers for bus mastering controllers.
14 * This should probably be sharing the guts of the slab allocator.
17 struct dma_pool { /* the pool */
18 struct list_head page_list;
20 size_t blocks_per_page;
25 wait_queue_head_t waitq;
26 struct list_head pools;
29 struct dma_page { /* cacheable header for 'allocation' bytes */
30 struct list_head page_list;
34 unsigned long bitmap [0];
37 #define POOL_TIMEOUT_JIFFIES ((100 /* msec */ * HZ) / 1000)
38 #define POOL_POISON_FREED 0xa7 /* !inuse */
39 #define POOL_POISON_ALLOCATED 0xa9 /* !initted */
41 static DECLARE_MUTEX (pools_lock);
44 show_pools (struct device *dev, struct device_attribute *attr, char *buf)
49 struct dma_page *page;
50 struct dma_pool *pool;
55 temp = scnprintf(next, size, "poolinfo - 0.1\n");
60 list_for_each_entry(pool, &dev->dma_pools, pools) {
64 list_for_each_entry(page, &pool->page_list, page_list) {
66 blocks += page->in_use;
69 /* per-pool info, no real statistics yet */
70 temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
72 blocks, pages * pool->blocks_per_page,
79 return PAGE_SIZE - size;
81 static DEVICE_ATTR (pools, S_IRUGO, show_pools, NULL);
84 * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
85 * @name: name of pool, for diagnostics
86 * @dev: device that will be doing the DMA
87 * @size: size of the blocks in this pool.
88 * @align: alignment requirement for blocks; must be a power of two
89 * @allocation: returned blocks won't cross this boundary (or zero)
90 * Context: !in_interrupt()
92 * Returns a dma allocation pool with the requested characteristics, or
93 * null if one can't be created. Given one of these pools, dma_pool_alloc()
94 * may be used to allocate memory. Such memory will all have "consistent"
95 * DMA mappings, accessible by the device and its driver without using
96 * cache flushing primitives. The actual size of blocks allocated may be
97 * larger than requested because of alignment.
99 * If allocation is nonzero, objects returned from dma_pool_alloc() won't
100 * cross that size boundary. This is useful for devices which have
101 * addressing restrictions on individual DMA transfers, such as not crossing
102 * boundaries of 4KBytes.
105 dma_pool_create (const char *name, struct device *dev,
106 size_t size, size_t align, size_t allocation)
108 struct dma_pool *retval;
114 else if (size < align)
116 else if ((size % align) != 0) {
118 size &= ~(align - 1);
121 if (allocation == 0) {
122 if (PAGE_SIZE < size)
125 allocation = PAGE_SIZE;
126 // FIXME: round up for less fragmentation
127 } else if (allocation < size)
130 if (!(retval = kmalloc (sizeof *retval, SLAB_KERNEL)))
133 strlcpy (retval->name, name, sizeof retval->name);
137 INIT_LIST_HEAD (&retval->page_list);
138 spin_lock_init (&retval->lock);
140 retval->allocation = allocation;
141 retval->blocks_per_page = allocation / size;
142 init_waitqueue_head (&retval->waitq);
146 if (list_empty (&dev->dma_pools))
147 device_create_file (dev, &dev_attr_pools);
148 /* note: not currently insisting "name" be unique */
149 list_add (&retval->pools, &dev->dma_pools);
152 INIT_LIST_HEAD (&retval->pools);
158 static struct dma_page *
159 pool_alloc_page (struct dma_pool *pool, unsigned int __nocast mem_flags)
161 struct dma_page *page;
164 mapsize = pool->blocks_per_page;
165 mapsize = (mapsize + BITS_PER_LONG - 1) / BITS_PER_LONG;
166 mapsize *= sizeof (long);
168 page = (struct dma_page *) kmalloc (mapsize + sizeof *page, mem_flags);
171 page->vaddr = dma_alloc_coherent (pool->dev,
176 memset (page->bitmap, 0xff, mapsize); // bit set == free
177 #ifdef CONFIG_DEBUG_SLAB
178 memset (page->vaddr, POOL_POISON_FREED, pool->allocation);
180 list_add (&page->page_list, &pool->page_list);
191 is_page_busy (int blocks, unsigned long *bitmap)
194 if (*bitmap++ != ~0UL)
196 blocks -= BITS_PER_LONG;
202 pool_free_page (struct dma_pool *pool, struct dma_page *page)
204 dma_addr_t dma = page->dma;
206 #ifdef CONFIG_DEBUG_SLAB
207 memset (page->vaddr, POOL_POISON_FREED, pool->allocation);
209 dma_free_coherent (pool->dev, pool->allocation, page->vaddr, dma);
210 list_del (&page->page_list);
216 * dma_pool_destroy - destroys a pool of dma memory blocks.
217 * @pool: dma pool that will be destroyed
218 * Context: !in_interrupt()
220 * Caller guarantees that no more memory from the pool is in use,
221 * and that nothing will try to use the pool after this call.
224 dma_pool_destroy (struct dma_pool *pool)
227 list_del (&pool->pools);
228 if (pool->dev && list_empty (&pool->dev->dma_pools))
229 device_remove_file (pool->dev, &dev_attr_pools);
232 while (!list_empty (&pool->page_list)) {
233 struct dma_page *page;
234 page = list_entry (pool->page_list.next,
235 struct dma_page, page_list);
236 if (is_page_busy (pool->blocks_per_page, page->bitmap)) {
238 dev_err(pool->dev, "dma_pool_destroy %s, %p busy\n",
239 pool->name, page->vaddr);
241 printk (KERN_ERR "dma_pool_destroy %s, %p busy\n",
242 pool->name, page->vaddr);
243 /* leak the still-in-use consistent memory */
244 list_del (&page->page_list);
247 pool_free_page (pool, page);
255 * dma_pool_alloc - get a block of consistent memory
256 * @pool: dma pool that will produce the block
257 * @mem_flags: GFP_* bitmask
258 * @handle: pointer to dma address of block
260 * This returns the kernel virtual address of a currently unused block,
261 * and reports its dma address through the handle.
262 * If such a memory block can't be allocated, null is returned.
265 dma_pool_alloc (struct dma_pool *pool, unsigned int __nocast mem_flags,
269 struct dma_page *page;
275 spin_lock_irqsave (&pool->lock, flags);
276 list_for_each_entry(page, &pool->page_list, page_list) {
278 /* only cachable accesses here ... */
280 i < pool->blocks_per_page;
281 i += BITS_PER_LONG, map++) {
282 if (page->bitmap [map] == 0)
284 block = ffz (~ page->bitmap [map]);
285 if ((i + block) < pool->blocks_per_page) {
286 clear_bit (block, &page->bitmap [map]);
287 offset = (BITS_PER_LONG * map) + block;
288 offset *= pool->size;
293 if (!(page = pool_alloc_page (pool, SLAB_ATOMIC))) {
294 if (mem_flags & __GFP_WAIT) {
295 DECLARE_WAITQUEUE (wait, current);
297 current->state = TASK_INTERRUPTIBLE;
298 add_wait_queue (&pool->waitq, &wait);
299 spin_unlock_irqrestore (&pool->lock, flags);
301 schedule_timeout (POOL_TIMEOUT_JIFFIES);
303 remove_wait_queue (&pool->waitq, &wait);
310 clear_bit (0, &page->bitmap [0]);
314 retval = offset + page->vaddr;
315 *handle = offset + page->dma;
316 #ifdef CONFIG_DEBUG_SLAB
317 memset (retval, POOL_POISON_ALLOCATED, pool->size);
320 spin_unlock_irqrestore (&pool->lock, flags);
325 static struct dma_page *
326 pool_find_page (struct dma_pool *pool, dma_addr_t dma)
329 struct dma_page *page;
331 spin_lock_irqsave (&pool->lock, flags);
332 list_for_each_entry(page, &pool->page_list, page_list) {
335 if (dma < (page->dma + pool->allocation))
340 spin_unlock_irqrestore (&pool->lock, flags);
346 * dma_pool_free - put block back into dma pool
347 * @pool: the dma pool holding the block
348 * @vaddr: virtual address of block
349 * @dma: dma address of block
351 * Caller promises neither device nor driver will again touch this block
352 * unless it is first re-allocated.
355 dma_pool_free (struct dma_pool *pool, void *vaddr, dma_addr_t dma)
357 struct dma_page *page;
361 if ((page = pool_find_page (pool, dma)) == 0) {
363 dev_err(pool->dev, "dma_pool_free %s, %p/%lx (bad dma)\n",
364 pool->name, vaddr, (unsigned long) dma);
366 printk (KERN_ERR "dma_pool_free %s, %p/%lx (bad dma)\n",
367 pool->name, vaddr, (unsigned long) dma);
371 block = dma - page->dma;
373 map = block / BITS_PER_LONG;
374 block %= BITS_PER_LONG;
376 #ifdef CONFIG_DEBUG_SLAB
377 if (((dma - page->dma) + (void *)page->vaddr) != vaddr) {
379 dev_err(pool->dev, "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
380 pool->name, vaddr, (unsigned long long) dma);
382 printk (KERN_ERR "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
383 pool->name, vaddr, (unsigned long long) dma);
386 if (page->bitmap [map] & (1UL << block)) {
388 dev_err(pool->dev, "dma_pool_free %s, dma %Lx already free\n",
389 pool->name, (unsigned long long)dma);
391 printk (KERN_ERR "dma_pool_free %s, dma %Lx already free\n",
392 pool->name, (unsigned long long)dma);
395 memset (vaddr, POOL_POISON_FREED, pool->size);
398 spin_lock_irqsave (&pool->lock, flags);
400 set_bit (block, &page->bitmap [map]);
401 if (waitqueue_active (&pool->waitq))
402 wake_up (&pool->waitq);
404 * Resist a temptation to do
405 * if (!is_page_busy(bpp, page->bitmap)) pool_free_page(pool, page);
406 * Better have a few empty pages hang around.
408 spin_unlock_irqrestore (&pool->lock, flags);
412 EXPORT_SYMBOL (dma_pool_create);
413 EXPORT_SYMBOL (dma_pool_destroy);
414 EXPORT_SYMBOL (dma_pool_alloc);
415 EXPORT_SYMBOL (dma_pool_free);