2 * arch/arm/common/dmabounce.c
4 * Special dma_{map/unmap/dma_sync}_* routines for systems that have
5 * limited DMA windows. These functions utilize bounce buffers to
6 * copy data to/from buffers located outside the DMA region. This
7 * only works for systems in which DMA memory is at the bottom of
8 * RAM, the remainder of memory is at the top and the DMA memory
9 * can be marked as ZONE_DMA. Anything beyond that such as discontigous
10 * DMA windows will require custom implementations that reserve memory
11 * areas at early bootup.
13 * Original version by Brad Parker (brad@heeltoe.com)
14 * Re-written by Christopher Hoover <ch@murgatroid.com>
15 * Made generic by Deepak Saxena <dsaxena@plexity.net>
17 * Copyright (C) 2002 Hewlett Packard Company.
18 * Copyright (C) 2004 MontaVista Software, Inc.
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * version 2 as published by the Free Software Foundation.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/device.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/dmapool.h>
31 #include <linux/list.h>
33 #include <asm/cacheflush.h>
39 #define DO_STATS(X) do { X ; } while (0)
41 #define DO_STATS(X) do { } while (0)
44 /* ************************************************** */
47 struct list_head node;
49 /* original request */
54 /* safe buffer info */
55 struct dmabounce_pool *pool;
57 dma_addr_t safe_dma_addr;
60 struct dmabounce_pool {
62 struct dma_pool *pool;
68 struct dmabounce_device_info {
69 struct list_head node;
72 struct list_head safe_buffers;
74 unsigned long total_allocs;
75 unsigned long map_op_count;
76 unsigned long bounce_count;
78 struct dmabounce_pool small;
79 struct dmabounce_pool large;
84 static LIST_HEAD(dmabounce_devs);
87 static void print_alloc_stats(struct dmabounce_device_info *device_info)
90 "%s: dmabounce: sbp: %lu, lbp: %lu, other: %lu, total: %lu\n",
91 device_info->dev->bus_id,
92 device_info->small.allocs, device_info->large.allocs,
93 device_info->total_allocs - device_info->small.allocs -
94 device_info->large.allocs,
95 device_info->total_allocs);
99 /* find the given device in the dmabounce device list */
100 static inline struct dmabounce_device_info *
101 find_dmabounce_dev(struct device *dev)
103 struct dmabounce_device_info *d;
105 list_for_each_entry(d, &dmabounce_devs, node)
113 /* allocate a 'safe' buffer and keep track of it */
114 static inline struct safe_buffer *
115 alloc_safe_buffer(struct dmabounce_device_info *device_info, void *ptr,
116 size_t size, enum dma_data_direction dir)
118 struct safe_buffer *buf;
119 struct dmabounce_pool *pool;
120 struct device *dev = device_info->dev;
123 dev_dbg(dev, "%s(ptr=%p, size=%d, dir=%d)\n",
124 __func__, ptr, size, dir);
126 if (size <= device_info->small.size) {
127 pool = &device_info->small;
128 } else if (size <= device_info->large.size) {
129 pool = &device_info->large;
134 buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC);
136 dev_warn(dev, "%s: kmalloc failed\n", __func__);
142 buf->direction = dir;
146 buf->safe = dma_pool_alloc(pool->pool, GFP_ATOMIC,
147 &buf->safe_dma_addr);
149 buf->safe = dma_alloc_coherent(dev, size, &buf->safe_dma_addr,
153 if (buf->safe == NULL) {
155 "%s: could not alloc dma memory (size=%d)\n",
164 device_info->total_allocs++;
165 if (device_info->total_allocs % 1000 == 0)
166 print_alloc_stats(device_info);
169 write_lock_irqsave(&device_info->lock, flags);
171 list_add(&buf->node, &device_info->safe_buffers);
173 write_unlock_irqrestore(&device_info->lock, flags);
178 /* determine if a buffer is from our "safe" pool */
179 static inline struct safe_buffer *
180 find_safe_buffer(struct dmabounce_device_info *device_info, dma_addr_t safe_dma_addr)
182 struct safe_buffer *b = NULL;
185 read_lock_irqsave(&device_info->lock, flags);
187 list_for_each_entry(b, &device_info->safe_buffers, node)
188 if (b->safe_dma_addr == safe_dma_addr)
191 read_unlock_irqrestore(&device_info->lock, flags);
196 free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf)
200 dev_dbg(device_info->dev, "%s(buf=%p)\n", __func__, buf);
202 write_lock_irqsave(&device_info->lock, flags);
204 list_del(&buf->node);
206 write_unlock_irqrestore(&device_info->lock, flags);
209 dma_pool_free(buf->pool->pool, buf->safe, buf->safe_dma_addr);
211 dma_free_coherent(device_info->dev, buf->size, buf->safe,
217 /* ************************************************** */
220 static void print_map_stats(struct dmabounce_device_info *device_info)
222 dev_info(device_info->dev,
223 "dmabounce: map_op_count=%lu, bounce_count=%lu\n",
224 device_info->map_op_count, device_info->bounce_count);
228 static inline dma_addr_t
229 map_single(struct device *dev, void *ptr, size_t size,
230 enum dma_data_direction dir)
232 struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
234 int needs_bounce = 0;
237 DO_STATS ( device_info->map_op_count++ );
239 dma_addr = virt_to_dma(dev, ptr);
242 unsigned long mask = *dev->dma_mask;
245 limit = (mask + 1) & ~mask;
246 if (limit && size > limit) {
247 dev_err(dev, "DMA mapping too big (requested %#x "
248 "mask %#Lx)\n", size, *dev->dma_mask);
253 * Figure out if we need to bounce from the DMA mask.
255 needs_bounce = (dma_addr | (dma_addr + size - 1)) & ~mask;
258 if (device_info && (needs_bounce || dma_needs_bounce(dev, dma_addr, size))) {
259 struct safe_buffer *buf;
261 buf = alloc_safe_buffer(device_info, ptr, size, dir);
263 dev_err(dev, "%s: unable to map unsafe buffer %p!\n",
269 "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
270 __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
271 buf->safe, (void *) buf->safe_dma_addr);
273 if ((dir == DMA_TO_DEVICE) ||
274 (dir == DMA_BIDIRECTIONAL)) {
275 dev_dbg(dev, "%s: copy unsafe %p to safe %p, size %d\n",
276 __func__, ptr, buf->safe, size);
277 memcpy(buf->safe, ptr, size);
281 dma_addr = buf->safe_dma_addr;
284 consistent_sync(ptr, size, dir);
290 unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
291 enum dma_data_direction dir)
293 struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
294 struct safe_buffer *buf = NULL;
297 * Trying to unmap an invalid mapping
299 if (dma_mapping_error(dma_addr)) {
300 dev_err(dev, "Trying to unmap invalid mapping\n");
305 buf = find_safe_buffer(device_info, dma_addr);
308 BUG_ON(buf->size != size);
311 "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
312 __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
313 buf->safe, (void *) buf->safe_dma_addr);
315 DO_STATS ( device_info->bounce_count++ );
317 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
321 "%s: copy back safe %p to unsafe %p size %d\n",
322 __func__, buf->safe, buf->ptr, size);
323 memcpy(buf->ptr, buf->safe, size);
326 * DMA buffers must have the same cache properties
327 * as if they were really used for DMA - which means
328 * data must be written back to RAM. Note that
329 * we don't use dmac_flush_range() here for the
330 * bidirectional case because we know the cache
331 * lines will be coherent with the data written.
333 ptr = (unsigned long)buf->ptr;
334 dmac_clean_range(ptr, ptr + size);
336 free_safe_buffer(device_info, buf);
341 sync_single(struct device *dev, dma_addr_t dma_addr, size_t size,
342 enum dma_data_direction dir)
344 struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
345 struct safe_buffer *buf = NULL;
348 buf = find_safe_buffer(device_info, dma_addr);
352 * Both of these checks from original code need to be
353 * commented out b/c some drivers rely on the following:
355 * 1) Drivers may map a large chunk of memory into DMA space
356 * but only sync a small portion of it. Good example is
357 * allocating a large buffer, mapping it, and then
358 * breaking it up into small descriptors. No point
359 * in syncing the whole buffer if you only have to
360 * touch one descriptor.
362 * 2) Buffers that are mapped as DMA_BIDIRECTIONAL are
363 * usually only synced in one dir at a time.
365 * See drivers/net/eepro100.c for examples of both cases.
369 * BUG_ON(buf->size != size);
370 * BUG_ON(buf->direction != dir);
374 "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
375 __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
376 buf->safe, (void *) buf->safe_dma_addr);
378 DO_STATS ( device_info->bounce_count++ );
381 case DMA_FROM_DEVICE:
383 "%s: copy back safe %p to unsafe %p size %d\n",
384 __func__, buf->safe, buf->ptr, size);
385 memcpy(buf->ptr, buf->safe, size);
389 "%s: copy out unsafe %p to safe %p, size %d\n",
390 __func__,buf->ptr, buf->safe, size);
391 memcpy(buf->safe, buf->ptr, size);
393 case DMA_BIDIRECTIONAL:
394 BUG(); /* is this allowed? what does it mean? */
398 consistent_sync(buf->safe, size, dir);
400 consistent_sync(dma_to_virt(dev, dma_addr), size, dir);
404 /* ************************************************** */
407 * see if a buffer address is in an 'unsafe' range. if it is
408 * allocate a 'safe' buffer and copy the unsafe buffer into it.
409 * substitute the safe buffer for the unsafe one.
410 * (basically move the buffer from an unsafe area to a safe one)
413 dma_map_single(struct device *dev, void *ptr, size_t size,
414 enum dma_data_direction dir)
418 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
419 __func__, ptr, size, dir);
421 BUG_ON(dir == DMA_NONE);
423 dma_addr = map_single(dev, ptr, size, dir);
429 * see if a mapped address was really a "safe" buffer and if so, copy
430 * the data from the safe buffer back to the unsafe buffer and free up
431 * the safe buffer. (basically return things back to the way they
436 dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
437 enum dma_data_direction dir)
439 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
440 __func__, (void *) dma_addr, size, dir);
442 BUG_ON(dir == DMA_NONE);
444 unmap_single(dev, dma_addr, size, dir);
448 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
449 enum dma_data_direction dir)
453 dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
454 __func__, sg, nents, dir);
456 BUG_ON(dir == DMA_NONE);
458 for (i = 0; i < nents; i++, sg++) {
459 struct page *page = sg->page;
460 unsigned int offset = sg->offset;
461 unsigned int length = sg->length;
462 void *ptr = page_address(page) + offset;
465 map_single(dev, ptr, length, dir);
472 dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
473 enum dma_data_direction dir)
477 dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
478 __func__, sg, nents, dir);
480 BUG_ON(dir == DMA_NONE);
482 for (i = 0; i < nents; i++, sg++) {
483 dma_addr_t dma_addr = sg->dma_address;
484 unsigned int length = sg->length;
486 unmap_single(dev, dma_addr, length, dir);
491 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, size_t size,
492 enum dma_data_direction dir)
494 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
495 __func__, (void *) dma_addr, size, dir);
497 sync_single(dev, dma_addr, size, dir);
501 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, size_t size,
502 enum dma_data_direction dir)
504 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
505 __func__, (void *) dma_addr, size, dir);
507 sync_single(dev, dma_addr, size, dir);
511 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
512 enum dma_data_direction dir)
516 dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
517 __func__, sg, nents, dir);
519 BUG_ON(dir == DMA_NONE);
521 for (i = 0; i < nents; i++, sg++) {
522 dma_addr_t dma_addr = sg->dma_address;
523 unsigned int length = sg->length;
525 sync_single(dev, dma_addr, length, dir);
530 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
531 enum dma_data_direction dir)
535 dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
536 __func__, sg, nents, dir);
538 BUG_ON(dir == DMA_NONE);
540 for (i = 0; i < nents; i++, sg++) {
541 dma_addr_t dma_addr = sg->dma_address;
542 unsigned int length = sg->length;
544 sync_single(dev, dma_addr, length, dir);
549 dmabounce_init_pool(struct dmabounce_pool *pool, struct device *dev, const char *name,
553 DO_STATS(pool->allocs = 0);
554 pool->pool = dma_pool_create(name, dev, size,
555 0 /* byte alignment */,
556 0 /* no page-crossing issues */);
558 return pool->pool ? 0 : -ENOMEM;
562 dmabounce_register_dev(struct device *dev, unsigned long small_buffer_size,
563 unsigned long large_buffer_size)
565 struct dmabounce_device_info *device_info;
568 device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
571 "Could not allocated dmabounce_device_info for %s",
576 ret = dmabounce_init_pool(&device_info->small, dev,
577 "small_dmabounce_pool", small_buffer_size);
580 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
585 if (large_buffer_size) {
586 ret = dmabounce_init_pool(&device_info->large, dev,
587 "large_dmabounce_pool",
591 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
597 device_info->dev = dev;
598 INIT_LIST_HEAD(&device_info->safe_buffers);
599 rwlock_init(&device_info->lock);
602 device_info->total_allocs = 0;
603 device_info->map_op_count = 0;
604 device_info->bounce_count = 0;
607 list_add(&device_info->node, &dmabounce_devs);
609 printk(KERN_INFO "dmabounce: registered device %s on %s bus\n",
610 dev->bus_id, dev->bus->name);
615 dma_pool_destroy(device_info->small.pool);
622 dmabounce_unregister_dev(struct device *dev)
624 struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
628 "%s: Never registered with dmabounce but attempting" \
629 "to unregister!\n", dev->bus_id);
633 if (!list_empty(&device_info->safe_buffers)) {
635 "%s: Removing from dmabounce with pending buffers!\n",
640 if (device_info->small.pool)
641 dma_pool_destroy(device_info->small.pool);
642 if (device_info->large.pool)
643 dma_pool_destroy(device_info->large.pool);
646 print_alloc_stats(device_info);
647 print_map_stats(device_info);
650 list_del(&device_info->node);
654 printk(KERN_INFO "dmabounce: device %s on %s bus unregistered\n",
655 dev->bus_id, dev->bus->name);
659 EXPORT_SYMBOL(dma_map_single);
660 EXPORT_SYMBOL(dma_unmap_single);
661 EXPORT_SYMBOL(dma_map_sg);
662 EXPORT_SYMBOL(dma_unmap_sg);
663 EXPORT_SYMBOL(dma_sync_single);
664 EXPORT_SYMBOL(dma_sync_sg);
665 EXPORT_SYMBOL(dmabounce_register_dev);
666 EXPORT_SYMBOL(dmabounce_unregister_dev);
668 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
669 MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
670 MODULE_LICENSE("GPL");