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>
10 #include <linux/poison.h>
11 #include <linux/sched.h>
14 * Pool allocator ... wraps the dma_alloc_coherent page allocator, so
15 * small blocks are easily used by drivers for bus mastering controllers.
16 * This should probably be sharing the guts of the slab allocator.
19 struct dma_pool
{ /* the pool */
20 struct list_head page_list
;
22 size_t blocks_per_page
;
27 wait_queue_head_t waitq
;
28 struct list_head pools
;
31 struct dma_page
{ /* cacheable header for 'allocation' bytes */
32 struct list_head page_list
;
36 unsigned long bitmap
[0];
39 #define POOL_TIMEOUT_JIFFIES ((100 /* msec */ * HZ) / 1000)
41 static DEFINE_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");
59 mutex_lock(&pools_lock
);
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
,
77 mutex_unlock(&pools_lock
);
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_node (sizeof *retval
, GFP_KERNEL
, dev_to_node(dev
))))
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
);
147 mutex_lock(&pools_lock
);
148 if (list_empty (&dev
->dma_pools
))
149 ret
= device_create_file (dev
, &dev_attr_pools
);
152 /* note: not currently insisting "name" be unique */
154 list_add (&retval
->pools
, &dev
->dma_pools
);
159 mutex_unlock(&pools_lock
);
161 INIT_LIST_HEAD (&retval
->pools
);
167 static struct dma_page
*
168 pool_alloc_page (struct dma_pool
*pool
, gfp_t mem_flags
)
170 struct dma_page
*page
;
173 mapsize
= pool
->blocks_per_page
;
174 mapsize
= (mapsize
+ BITS_PER_LONG
- 1) / BITS_PER_LONG
;
175 mapsize
*= sizeof (long);
177 page
= kmalloc(mapsize
+ sizeof *page
, mem_flags
);
180 page
->vaddr
= dma_alloc_coherent (pool
->dev
,
185 memset (page
->bitmap
, 0xff, mapsize
); // bit set == free
186 #ifdef CONFIG_DEBUG_SLAB
187 memset (page
->vaddr
, POOL_POISON_FREED
, pool
->allocation
);
189 list_add (&page
->page_list
, &pool
->page_list
);
200 is_page_busy (int blocks
, unsigned long *bitmap
)
203 if (*bitmap
++ != ~0UL)
205 blocks
-= BITS_PER_LONG
;
211 pool_free_page (struct dma_pool
*pool
, struct dma_page
*page
)
213 dma_addr_t dma
= page
->dma
;
215 #ifdef CONFIG_DEBUG_SLAB
216 memset (page
->vaddr
, POOL_POISON_FREED
, pool
->allocation
);
218 dma_free_coherent (pool
->dev
, pool
->allocation
, page
->vaddr
, dma
);
219 list_del (&page
->page_list
);
225 * dma_pool_destroy - destroys a pool of dma memory blocks.
226 * @pool: dma pool that will be destroyed
227 * Context: !in_interrupt()
229 * Caller guarantees that no more memory from the pool is in use,
230 * and that nothing will try to use the pool after this call.
233 dma_pool_destroy (struct dma_pool
*pool
)
235 mutex_lock(&pools_lock
);
236 list_del (&pool
->pools
);
237 if (pool
->dev
&& list_empty (&pool
->dev
->dma_pools
))
238 device_remove_file (pool
->dev
, &dev_attr_pools
);
239 mutex_unlock(&pools_lock
);
241 while (!list_empty (&pool
->page_list
)) {
242 struct dma_page
*page
;
243 page
= list_entry (pool
->page_list
.next
,
244 struct dma_page
, page_list
);
245 if (is_page_busy (pool
->blocks_per_page
, page
->bitmap
)) {
247 dev_err(pool
->dev
, "dma_pool_destroy %s, %p busy\n",
248 pool
->name
, page
->vaddr
);
250 printk (KERN_ERR
"dma_pool_destroy %s, %p busy\n",
251 pool
->name
, page
->vaddr
);
252 /* leak the still-in-use consistent memory */
253 list_del (&page
->page_list
);
256 pool_free_page (pool
, page
);
264 * dma_pool_alloc - get a block of consistent memory
265 * @pool: dma pool that will produce the block
266 * @mem_flags: GFP_* bitmask
267 * @handle: pointer to dma address of block
269 * This returns the kernel virtual address of a currently unused block,
270 * and reports its dma address through the handle.
271 * If such a memory block can't be allocated, null is returned.
274 dma_pool_alloc (struct dma_pool
*pool
, gfp_t mem_flags
, dma_addr_t
*handle
)
277 struct dma_page
*page
;
283 spin_lock_irqsave (&pool
->lock
, flags
);
284 list_for_each_entry(page
, &pool
->page_list
, page_list
) {
286 /* only cachable accesses here ... */
288 i
< pool
->blocks_per_page
;
289 i
+= BITS_PER_LONG
, map
++) {
290 if (page
->bitmap
[map
] == 0)
292 block
= ffz (~ page
->bitmap
[map
]);
293 if ((i
+ block
) < pool
->blocks_per_page
) {
294 clear_bit (block
, &page
->bitmap
[map
]);
295 offset
= (BITS_PER_LONG
* map
) + block
;
296 offset
*= pool
->size
;
301 if (!(page
= pool_alloc_page (pool
, GFP_ATOMIC
))) {
302 if (mem_flags
& __GFP_WAIT
) {
303 DECLARE_WAITQUEUE (wait
, current
);
305 __set_current_state(TASK_INTERRUPTIBLE
);
306 add_wait_queue (&pool
->waitq
, &wait
);
307 spin_unlock_irqrestore (&pool
->lock
, flags
);
309 schedule_timeout (POOL_TIMEOUT_JIFFIES
);
311 remove_wait_queue (&pool
->waitq
, &wait
);
318 clear_bit (0, &page
->bitmap
[0]);
322 retval
= offset
+ page
->vaddr
;
323 *handle
= offset
+ page
->dma
;
324 #ifdef CONFIG_DEBUG_SLAB
325 memset (retval
, POOL_POISON_ALLOCATED
, pool
->size
);
328 spin_unlock_irqrestore (&pool
->lock
, flags
);
333 static struct dma_page
*
334 pool_find_page (struct dma_pool
*pool
, dma_addr_t dma
)
337 struct dma_page
*page
;
339 spin_lock_irqsave (&pool
->lock
, flags
);
340 list_for_each_entry(page
, &pool
->page_list
, page_list
) {
343 if (dma
< (page
->dma
+ pool
->allocation
))
348 spin_unlock_irqrestore (&pool
->lock
, flags
);
354 * dma_pool_free - put block back into dma pool
355 * @pool: the dma pool holding the block
356 * @vaddr: virtual address of block
357 * @dma: dma address of block
359 * Caller promises neither device nor driver will again touch this block
360 * unless it is first re-allocated.
363 dma_pool_free (struct dma_pool
*pool
, void *vaddr
, dma_addr_t dma
)
365 struct dma_page
*page
;
369 if ((page
= pool_find_page(pool
, dma
)) == NULL
) {
371 dev_err(pool
->dev
, "dma_pool_free %s, %p/%lx (bad dma)\n",
372 pool
->name
, vaddr
, (unsigned long) dma
);
374 printk (KERN_ERR
"dma_pool_free %s, %p/%lx (bad dma)\n",
375 pool
->name
, vaddr
, (unsigned long) dma
);
379 block
= dma
- page
->dma
;
381 map
= block
/ BITS_PER_LONG
;
382 block
%= BITS_PER_LONG
;
384 #ifdef CONFIG_DEBUG_SLAB
385 if (((dma
- page
->dma
) + (void *)page
->vaddr
) != vaddr
) {
387 dev_err(pool
->dev
, "dma_pool_free %s, %p (bad vaddr)/%Lx\n",
388 pool
->name
, vaddr
, (unsigned long long) dma
);
390 printk (KERN_ERR
"dma_pool_free %s, %p (bad vaddr)/%Lx\n",
391 pool
->name
, vaddr
, (unsigned long long) dma
);
394 if (page
->bitmap
[map
] & (1UL << block
)) {
396 dev_err(pool
->dev
, "dma_pool_free %s, dma %Lx already free\n",
397 pool
->name
, (unsigned long long)dma
);
399 printk (KERN_ERR
"dma_pool_free %s, dma %Lx already free\n",
400 pool
->name
, (unsigned long long)dma
);
403 memset (vaddr
, POOL_POISON_FREED
, pool
->size
);
406 spin_lock_irqsave (&pool
->lock
, flags
);
408 set_bit (block
, &page
->bitmap
[map
]);
409 if (waitqueue_active (&pool
->waitq
))
410 wake_up (&pool
->waitq
);
412 * Resist a temptation to do
413 * if (!is_page_busy(bpp, page->bitmap)) pool_free_page(pool, page);
414 * Better have a few empty pages hang around.
416 spin_unlock_irqrestore (&pool
->lock
, flags
);
422 static void dmam_pool_release(struct device
*dev
, void *res
)
424 struct dma_pool
*pool
= *(struct dma_pool
**)res
;
426 dma_pool_destroy(pool
);
429 static int dmam_pool_match(struct device
*dev
, void *res
, void *match_data
)
431 return *(struct dma_pool
**)res
== match_data
;
435 * dmam_pool_create - Managed dma_pool_create()
436 * @name: name of pool, for diagnostics
437 * @dev: device that will be doing the DMA
438 * @size: size of the blocks in this pool.
439 * @align: alignment requirement for blocks; must be a power of two
440 * @allocation: returned blocks won't cross this boundary (or zero)
442 * Managed dma_pool_create(). DMA pool created with this function is
443 * automatically destroyed on driver detach.
445 struct dma_pool
*dmam_pool_create(const char *name
, struct device
*dev
,
446 size_t size
, size_t align
, size_t allocation
)
448 struct dma_pool
**ptr
, *pool
;
450 ptr
= devres_alloc(dmam_pool_release
, sizeof(*ptr
), GFP_KERNEL
);
454 pool
= *ptr
= dma_pool_create(name
, dev
, size
, align
, allocation
);
456 devres_add(dev
, ptr
);
464 * dmam_pool_destroy - Managed dma_pool_destroy()
465 * @pool: dma pool that will be destroyed
467 * Managed dma_pool_destroy().
469 void dmam_pool_destroy(struct dma_pool
*pool
)
471 struct device
*dev
= pool
->dev
;
473 dma_pool_destroy(pool
);
474 WARN_ON(devres_destroy(dev
, dmam_pool_release
, dmam_pool_match
, pool
));
477 EXPORT_SYMBOL (dma_pool_create
);
478 EXPORT_SYMBOL (dma_pool_destroy
);
479 EXPORT_SYMBOL (dma_pool_alloc
);
480 EXPORT_SYMBOL (dma_pool_free
);
481 EXPORT_SYMBOL (dmam_pool_create
);
482 EXPORT_SYMBOL (dmam_pool_destroy
);