[ARM] 3755/1: dmabounce: fix return value for find_safe_buffer
[linux-2.6/linux-mips.git] / arch / arm / common / dmabounce.c
blob028bdc9228fb00103bed548c148cb7fccc5268a8
1 /*
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>
35 #undef DEBUG
36 #undef STATS
38 #ifdef STATS
39 #define DO_STATS(X) do { X ; } while (0)
40 #else
41 #define DO_STATS(X) do { } while (0)
42 #endif
44 /* ************************************************** */
46 struct safe_buffer {
47 struct list_head node;
49 /* original request */
50 void *ptr;
51 size_t size;
52 int direction;
54 /* safe buffer info */
55 struct dmabounce_pool *pool;
56 void *safe;
57 dma_addr_t safe_dma_addr;
60 struct dmabounce_pool {
61 unsigned long size;
62 struct dma_pool *pool;
63 #ifdef STATS
64 unsigned long allocs;
65 #endif
68 struct dmabounce_device_info {
69 struct list_head node;
71 struct device *dev;
72 struct list_head safe_buffers;
73 #ifdef STATS
74 unsigned long total_allocs;
75 unsigned long map_op_count;
76 unsigned long bounce_count;
77 #endif
78 struct dmabounce_pool small;
79 struct dmabounce_pool large;
81 rwlock_t lock;
84 static LIST_HEAD(dmabounce_devs);
86 #ifdef STATS
87 static void print_alloc_stats(struct dmabounce_device_info *device_info)
89 printk(KERN_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);
97 #endif
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)
106 if (d->dev == dev)
107 return d;
109 return NULL;
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;
121 unsigned long flags;
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;
130 } else {
131 pool = NULL;
134 buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC);
135 if (buf == NULL) {
136 dev_warn(dev, "%s: kmalloc failed\n", __func__);
137 return NULL;
140 buf->ptr = ptr;
141 buf->size = size;
142 buf->direction = dir;
143 buf->pool = pool;
145 if (pool) {
146 buf->safe = dma_pool_alloc(pool->pool, GFP_ATOMIC,
147 &buf->safe_dma_addr);
148 } else {
149 buf->safe = dma_alloc_coherent(dev, size, &buf->safe_dma_addr,
150 GFP_ATOMIC);
153 if (buf->safe == NULL) {
154 dev_warn(dev,
155 "%s: could not alloc dma memory (size=%d)\n",
156 __func__, size);
157 kfree(buf);
158 return NULL;
161 #ifdef STATS
162 if (pool)
163 pool->allocs++;
164 device_info->total_allocs++;
165 if (device_info->total_allocs % 1000 == 0)
166 print_alloc_stats(device_info);
167 #endif
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);
175 return buf;
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, *rb = NULL;
183 unsigned long flags;
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) {
189 rb = b;
190 break;
193 read_unlock_irqrestore(&device_info->lock, flags);
194 return rb;
197 static inline void
198 free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf)
200 unsigned long flags;
202 dev_dbg(device_info->dev, "%s(buf=%p)\n", __func__, buf);
204 write_lock_irqsave(&device_info->lock, flags);
206 list_del(&buf->node);
208 write_unlock_irqrestore(&device_info->lock, flags);
210 if (buf->pool)
211 dma_pool_free(buf->pool->pool, buf->safe, buf->safe_dma_addr);
212 else
213 dma_free_coherent(device_info->dev, buf->size, buf->safe,
214 buf->safe_dma_addr);
216 kfree(buf);
219 /* ************************************************** */
221 #ifdef STATS
222 static void print_map_stats(struct dmabounce_device_info *device_info)
224 dev_info(device_info->dev,
225 "dmabounce: map_op_count=%lu, bounce_count=%lu\n",
226 device_info->map_op_count, device_info->bounce_count);
228 #endif
230 static inline dma_addr_t
231 map_single(struct device *dev, void *ptr, size_t size,
232 enum dma_data_direction dir)
234 struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
235 dma_addr_t dma_addr;
236 int needs_bounce = 0;
238 if (device_info)
239 DO_STATS ( device_info->map_op_count++ );
241 dma_addr = virt_to_dma(dev, ptr);
243 if (dev->dma_mask) {
244 unsigned long mask = *dev->dma_mask;
245 unsigned long limit;
247 limit = (mask + 1) & ~mask;
248 if (limit && size > limit) {
249 dev_err(dev, "DMA mapping too big (requested %#x "
250 "mask %#Lx)\n", size, *dev->dma_mask);
251 return ~0;
255 * Figure out if we need to bounce from the DMA mask.
257 needs_bounce = (dma_addr | (dma_addr + size - 1)) & ~mask;
260 if (device_info && (needs_bounce || dma_needs_bounce(dev, dma_addr, size))) {
261 struct safe_buffer *buf;
263 buf = alloc_safe_buffer(device_info, ptr, size, dir);
264 if (buf == 0) {
265 dev_err(dev, "%s: unable to map unsafe buffer %p!\n",
266 __func__, ptr);
267 return 0;
270 dev_dbg(dev,
271 "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
272 __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
273 buf->safe, (void *) buf->safe_dma_addr);
275 if ((dir == DMA_TO_DEVICE) ||
276 (dir == DMA_BIDIRECTIONAL)) {
277 dev_dbg(dev, "%s: copy unsafe %p to safe %p, size %d\n",
278 __func__, ptr, buf->safe, size);
279 memcpy(buf->safe, ptr, size);
281 ptr = buf->safe;
283 dma_addr = buf->safe_dma_addr;
286 consistent_sync(ptr, size, dir);
288 return dma_addr;
291 static inline void
292 unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
293 enum dma_data_direction dir)
295 struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
296 struct safe_buffer *buf = NULL;
299 * Trying to unmap an invalid mapping
301 if (dma_mapping_error(dma_addr)) {
302 dev_err(dev, "Trying to unmap invalid mapping\n");
303 return;
306 if (device_info)
307 buf = find_safe_buffer(device_info, dma_addr);
309 if (buf) {
310 BUG_ON(buf->size != size);
312 dev_dbg(dev,
313 "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
314 __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
315 buf->safe, (void *) buf->safe_dma_addr);
317 DO_STATS ( device_info->bounce_count++ );
319 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
320 unsigned long ptr;
322 dev_dbg(dev,
323 "%s: copy back safe %p to unsafe %p size %d\n",
324 __func__, buf->safe, buf->ptr, size);
325 memcpy(buf->ptr, buf->safe, size);
328 * DMA buffers must have the same cache properties
329 * as if they were really used for DMA - which means
330 * data must be written back to RAM. Note that
331 * we don't use dmac_flush_range() here for the
332 * bidirectional case because we know the cache
333 * lines will be coherent with the data written.
335 ptr = (unsigned long)buf->ptr;
336 dmac_clean_range(ptr, ptr + size);
338 free_safe_buffer(device_info, buf);
342 static inline void
343 sync_single(struct device *dev, dma_addr_t dma_addr, size_t size,
344 enum dma_data_direction dir)
346 struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
347 struct safe_buffer *buf = NULL;
349 if (device_info)
350 buf = find_safe_buffer(device_info, dma_addr);
352 if (buf) {
354 * Both of these checks from original code need to be
355 * commented out b/c some drivers rely on the following:
357 * 1) Drivers may map a large chunk of memory into DMA space
358 * but only sync a small portion of it. Good example is
359 * allocating a large buffer, mapping it, and then
360 * breaking it up into small descriptors. No point
361 * in syncing the whole buffer if you only have to
362 * touch one descriptor.
364 * 2) Buffers that are mapped as DMA_BIDIRECTIONAL are
365 * usually only synced in one dir at a time.
367 * See drivers/net/eepro100.c for examples of both cases.
369 * -ds
371 * BUG_ON(buf->size != size);
372 * BUG_ON(buf->direction != dir);
375 dev_dbg(dev,
376 "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
377 __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
378 buf->safe, (void *) buf->safe_dma_addr);
380 DO_STATS ( device_info->bounce_count++ );
382 switch (dir) {
383 case DMA_FROM_DEVICE:
384 dev_dbg(dev,
385 "%s: copy back safe %p to unsafe %p size %d\n",
386 __func__, buf->safe, buf->ptr, size);
387 memcpy(buf->ptr, buf->safe, size);
388 break;
389 case DMA_TO_DEVICE:
390 dev_dbg(dev,
391 "%s: copy out unsafe %p to safe %p, size %d\n",
392 __func__,buf->ptr, buf->safe, size);
393 memcpy(buf->safe, buf->ptr, size);
394 break;
395 case DMA_BIDIRECTIONAL:
396 BUG(); /* is this allowed? what does it mean? */
397 default:
398 BUG();
400 consistent_sync(buf->safe, size, dir);
401 } else {
402 consistent_sync(dma_to_virt(dev, dma_addr), size, dir);
406 /* ************************************************** */
409 * see if a buffer address is in an 'unsafe' range. if it is
410 * allocate a 'safe' buffer and copy the unsafe buffer into it.
411 * substitute the safe buffer for the unsafe one.
412 * (basically move the buffer from an unsafe area to a safe one)
414 dma_addr_t
415 dma_map_single(struct device *dev, void *ptr, size_t size,
416 enum dma_data_direction dir)
418 dma_addr_t dma_addr;
420 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
421 __func__, ptr, size, dir);
423 BUG_ON(dir == DMA_NONE);
425 dma_addr = map_single(dev, ptr, size, dir);
427 return dma_addr;
431 * see if a mapped address was really a "safe" buffer and if so, copy
432 * the data from the safe buffer back to the unsafe buffer and free up
433 * the safe buffer. (basically return things back to the way they
434 * should be)
437 void
438 dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
439 enum dma_data_direction dir)
441 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
442 __func__, (void *) dma_addr, size, dir);
444 BUG_ON(dir == DMA_NONE);
446 unmap_single(dev, dma_addr, size, dir);
450 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
451 enum dma_data_direction dir)
453 int i;
455 dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
456 __func__, sg, nents, dir);
458 BUG_ON(dir == DMA_NONE);
460 for (i = 0; i < nents; i++, sg++) {
461 struct page *page = sg->page;
462 unsigned int offset = sg->offset;
463 unsigned int length = sg->length;
464 void *ptr = page_address(page) + offset;
466 sg->dma_address =
467 map_single(dev, ptr, length, dir);
470 return nents;
473 void
474 dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
475 enum dma_data_direction dir)
477 int i;
479 dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
480 __func__, sg, nents, dir);
482 BUG_ON(dir == DMA_NONE);
484 for (i = 0; i < nents; i++, sg++) {
485 dma_addr_t dma_addr = sg->dma_address;
486 unsigned int length = sg->length;
488 unmap_single(dev, dma_addr, length, dir);
492 void
493 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, size_t size,
494 enum dma_data_direction dir)
496 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
497 __func__, (void *) dma_addr, size, dir);
499 sync_single(dev, dma_addr, size, dir);
502 void
503 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, size_t size,
504 enum dma_data_direction dir)
506 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
507 __func__, (void *) dma_addr, size, dir);
509 sync_single(dev, dma_addr, size, dir);
512 void
513 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
514 enum dma_data_direction dir)
516 int i;
518 dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
519 __func__, sg, nents, dir);
521 BUG_ON(dir == DMA_NONE);
523 for (i = 0; i < nents; i++, sg++) {
524 dma_addr_t dma_addr = sg->dma_address;
525 unsigned int length = sg->length;
527 sync_single(dev, dma_addr, length, dir);
531 void
532 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
533 enum dma_data_direction dir)
535 int i;
537 dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
538 __func__, sg, nents, dir);
540 BUG_ON(dir == DMA_NONE);
542 for (i = 0; i < nents; i++, sg++) {
543 dma_addr_t dma_addr = sg->dma_address;
544 unsigned int length = sg->length;
546 sync_single(dev, dma_addr, length, dir);
550 static int
551 dmabounce_init_pool(struct dmabounce_pool *pool, struct device *dev, const char *name,
552 unsigned long size)
554 pool->size = size;
555 DO_STATS(pool->allocs = 0);
556 pool->pool = dma_pool_create(name, dev, size,
557 0 /* byte alignment */,
558 0 /* no page-crossing issues */);
560 return pool->pool ? 0 : -ENOMEM;
564 dmabounce_register_dev(struct device *dev, unsigned long small_buffer_size,
565 unsigned long large_buffer_size)
567 struct dmabounce_device_info *device_info;
568 int ret;
570 device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
571 if (!device_info) {
572 printk(KERN_ERR
573 "Could not allocated dmabounce_device_info for %s",
574 dev->bus_id);
575 return -ENOMEM;
578 ret = dmabounce_init_pool(&device_info->small, dev,
579 "small_dmabounce_pool", small_buffer_size);
580 if (ret) {
581 dev_err(dev,
582 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
583 small_buffer_size);
584 goto err_free;
587 if (large_buffer_size) {
588 ret = dmabounce_init_pool(&device_info->large, dev,
589 "large_dmabounce_pool",
590 large_buffer_size);
591 if (ret) {
592 dev_err(dev,
593 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
594 large_buffer_size);
595 goto err_destroy;
599 device_info->dev = dev;
600 INIT_LIST_HEAD(&device_info->safe_buffers);
601 rwlock_init(&device_info->lock);
603 #ifdef STATS
604 device_info->total_allocs = 0;
605 device_info->map_op_count = 0;
606 device_info->bounce_count = 0;
607 #endif
609 list_add(&device_info->node, &dmabounce_devs);
611 printk(KERN_INFO "dmabounce: registered device %s on %s bus\n",
612 dev->bus_id, dev->bus->name);
614 return 0;
616 err_destroy:
617 dma_pool_destroy(device_info->small.pool);
618 err_free:
619 kfree(device_info);
620 return ret;
623 void
624 dmabounce_unregister_dev(struct device *dev)
626 struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
628 if (!device_info) {
629 printk(KERN_WARNING
630 "%s: Never registered with dmabounce but attempting" \
631 "to unregister!\n", dev->bus_id);
632 return;
635 if (!list_empty(&device_info->safe_buffers)) {
636 printk(KERN_ERR
637 "%s: Removing from dmabounce with pending buffers!\n",
638 dev->bus_id);
639 BUG();
642 if (device_info->small.pool)
643 dma_pool_destroy(device_info->small.pool);
644 if (device_info->large.pool)
645 dma_pool_destroy(device_info->large.pool);
647 #ifdef STATS
648 print_alloc_stats(device_info);
649 print_map_stats(device_info);
650 #endif
652 list_del(&device_info->node);
654 kfree(device_info);
656 printk(KERN_INFO "dmabounce: device %s on %s bus unregistered\n",
657 dev->bus_id, dev->bus->name);
661 EXPORT_SYMBOL(dma_map_single);
662 EXPORT_SYMBOL(dma_unmap_single);
663 EXPORT_SYMBOL(dma_map_sg);
664 EXPORT_SYMBOL(dma_unmap_sg);
665 EXPORT_SYMBOL(dma_sync_single);
666 EXPORT_SYMBOL(dma_sync_sg);
667 EXPORT_SYMBOL(dmabounce_register_dev);
668 EXPORT_SYMBOL(dmabounce_unregister_dev);
670 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
671 MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
672 MODULE_LICENSE("GPL");