patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / arm / common / dmabounce.c
1 /*
2  *  arch/arm/common/dmabounce.c
3  *
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 and the remainder of memory is at the top an 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.
12  *
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>
16  *
17  *  Copyright (C) 2002 Hewlett Packard Company.
18  *  Copyright (C) 2004 MontaVista Software, Inc.
19  *
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.
23  */
24
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>
32
33 #undef DEBUG
34
35 #undef STATS
36 #ifdef STATS
37 #define DO_STATS(X) do { X ; } while (0)
38 #else
39 #define DO_STATS(X) do { } while (0)
40 #endif
41
42 /* ************************************************** */
43
44 struct safe_buffer {
45         struct list_head node;
46
47         /* original request */
48         void            *ptr;
49         size_t          size;
50         int             direction;
51
52         /* safe buffer info */
53         struct dma_pool *pool;
54         void            *safe;
55         dma_addr_t      safe_dma_addr;
56 };
57
58 struct dmabounce_device_info {
59         struct list_head node;
60
61         struct device *dev;
62         struct dma_pool *small_buffer_pool;
63         struct dma_pool *large_buffer_pool;
64         struct list_head safe_buffers;
65         unsigned long small_buffer_size, large_buffer_size;
66 #ifdef STATS
67         unsigned long sbp_allocs;
68         unsigned long lbp_allocs;
69         unsigned long total_allocs;
70         unsigned long map_op_count;
71         unsigned long bounce_count;
72 #endif
73 };
74
75 static LIST_HEAD(dmabounce_devs);
76
77 #ifdef STATS
78 static void print_alloc_stats(struct dmabounce_device_info *device_info)
79 {
80         printk(KERN_INFO
81                 "%s: dmabounce: sbp: %lu, lbp: %lu, other: %lu, total: %lu\n",
82                 device_info->dev->bus_id,
83                 device_info->sbp_allocs, device_info->lbp_allocs,
84                 device_info->total_allocs - device_info->sbp_allocs -
85                         device_info->lbp_allocs,
86                 device_info->total_allocs);
87 }
88 #endif
89
90 /* find the given device in the dmabounce device list */
91 static inline struct dmabounce_device_info *
92 find_dmabounce_dev(struct device *dev)
93 {
94         struct list_head *entry;
95
96         list_for_each(entry, &dmabounce_devs) {
97                 struct dmabounce_device_info *d =
98                         list_entry(entry, struct dmabounce_device_info, node);
99
100                 if (d->dev == dev)
101                         return d;
102         }
103         return NULL;
104 }
105
106
107 /* allocate a 'safe' buffer and keep track of it */
108 static inline struct safe_buffer *
109 alloc_safe_buffer(struct dmabounce_device_info *device_info, void *ptr,
110                         size_t size, enum dma_data_direction dir)
111 {
112         struct safe_buffer *buf;
113         struct dma_pool *pool;
114         struct device *dev = device_info->dev;
115         void *safe;
116         dma_addr_t safe_dma_addr;
117
118         dev_dbg(dev, "%s(ptr=%p, size=%d, dir=%d)\n",
119                 __func__, ptr, size, dir);
120
121         DO_STATS ( device_info->total_allocs++ );
122
123         buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC);
124         if (buf == 0) {
125                 dev_warn(dev, "%s: kmalloc failed\n", __func__);
126                 return 0;
127         }
128
129         if (size <= device_info->small_buffer_size) {
130                 pool = device_info->small_buffer_pool;
131                 safe = dma_pool_alloc(pool, GFP_ATOMIC, &safe_dma_addr);
132
133                 DO_STATS ( device_info->sbp_allocs++ );
134         } else if (size <= device_info->large_buffer_size) {
135                 pool = device_info->large_buffer_pool;
136                 safe = dma_pool_alloc(pool, GFP_ATOMIC, &safe_dma_addr);
137
138                 DO_STATS ( device_info->lbp_allocs++ );
139         } else {
140                 pool = 0;
141                 safe = dma_alloc_coherent(dev, size, &safe_dma_addr, GFP_ATOMIC);
142         }
143
144         if (safe == 0) {
145                 dev_warn(device_info->dev,
146                         "%s: could not alloc dma memory (size=%d)\n",
147                        __func__, size);
148                 kfree(buf);
149                 return 0;
150         }
151
152 #ifdef STATS
153         if (device_info->total_allocs % 1000 == 0)
154                 print_alloc_stats(device_info);
155 #endif
156
157         buf->ptr = ptr;
158         buf->size = size;
159         buf->direction = dir;
160         buf->pool = pool;
161         buf->safe = safe;
162         buf->safe_dma_addr = safe_dma_addr;
163
164         list_add(&buf->node, &device_info->safe_buffers);
165
166         return buf;
167 }
168
169 /* determine if a buffer is from our "safe" pool */
170 static inline struct safe_buffer *
171 find_safe_buffer(struct dmabounce_device_info *device_info, dma_addr_t safe_dma_addr)
172 {
173         struct list_head *entry;
174
175         list_for_each(entry, &device_info->safe_buffers) {
176                 struct safe_buffer *b =
177                         list_entry(entry, struct safe_buffer, node);
178
179                 if (b->safe_dma_addr == safe_dma_addr)
180                         return b;
181         }
182
183         return NULL;
184 }
185
186 static inline void
187 free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf)
188 {
189         dev_dbg(device_info->dev, "%s(buf=%p)\n", __func__, buf);
190
191         list_del(&buf->node);
192
193         if (buf->pool)
194                 dma_pool_free(buf->pool, buf->safe, buf->safe_dma_addr);
195         else
196                 dma_free_coherent(device_info->dev, buf->size, buf->safe,
197                                     buf->safe_dma_addr);
198
199         kfree(buf);
200 }
201
202 /* ************************************************** */
203
204 #ifdef STATS
205
206 static void print_map_stats(struct dmabounce_device_info *device_info)
207 {
208         printk(KERN_INFO
209                 "%s: dmabounce: map_op_count=%lu, bounce_count=%lu\n",
210                 device_info->dev->bus_id,
211                 device_info->map_op_count, device_info->bounce_count);
212 }
213 #endif
214
215 static inline dma_addr_t
216 map_single(struct device *dev, void *ptr, size_t size,
217                 enum dma_data_direction dir)
218 {
219         dma_addr_t dma_addr;
220         struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
221
222         if (device_info)
223                 DO_STATS ( device_info->map_op_count++ );
224
225         if (dev->dma_mask) {
226                 unsigned long limit;
227
228                 limit = (*dev->dma_mask + 1) & ~(*dev->dma_mask);
229                 if (limit && (size > limit)) {
230                         dev_err(dev, "DMA mapping too big "
231                                 "(requested %#x mask %#Lx)\n",
232                                 size, *dev->dma_mask);
233                         return ~0;
234                 }
235         }
236
237         dma_addr = virt_to_bus(ptr);
238
239         if (device_info && dma_needs_bounce(dev, dma_addr, size)) {
240                 struct safe_buffer *buf;
241
242                 buf = alloc_safe_buffer(device_info, ptr, size, dir);
243                 if (buf == 0) {
244                         dev_err(dev, "%s: unable to map unsafe buffer %p!\n",
245                                __func__, ptr);
246                         return 0;
247                 }
248
249                 dev_dbg(dev,
250                         "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
251                         __func__, buf->ptr, (void *) virt_to_bus(buf->ptr),
252                         buf->safe, (void *) buf->safe_dma_addr);
253
254                 if ((dir == DMA_TO_DEVICE) ||
255                     (dir == DMA_BIDIRECTIONAL)) {
256                         dev_dbg(dev, "%s: copy unsafe %p to safe %p, size %d\n",
257                                 __func__, ptr, buf->safe, size);
258                         memcpy(buf->safe, ptr, size);
259                 }
260                 consistent_sync(buf->safe, size, dir);
261
262                 dma_addr = buf->safe_dma_addr;
263         } else {
264                 consistent_sync(ptr, size, dir);
265         }
266
267         return dma_addr;
268 }
269
270 static inline void
271 unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
272                 enum dma_data_direction dir)
273 {
274         struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
275         struct safe_buffer *buf = NULL;
276
277         /*
278          * Trying to unmap an invalid mapping
279          */
280         if (dma_addr == ~0) {
281                 dev_err(dev, "Trying to unmap invalid mapping\n");
282                 return;
283         }
284
285         if (device_info)
286                 buf = find_safe_buffer(device_info, dma_addr);
287
288         if (buf) {
289                 BUG_ON(buf->size != size);
290
291                 dev_dbg(dev,
292                         "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
293                         __func__, buf->ptr, (void *) virt_to_bus(buf->ptr),
294                         buf->safe, (void *) buf->safe_dma_addr);
295
296
297                 DO_STATS ( device_info->bounce_count++ );
298
299                 if ((dir == DMA_FROM_DEVICE) ||
300                     (dir == DMA_BIDIRECTIONAL)) {
301                         dev_dbg(dev,
302                                 "%s: copy back safe %p to unsafe %p size %d\n",
303                                 __func__, buf->safe, buf->ptr, size);
304                         memcpy(buf->ptr, buf->safe, size);
305                 }
306                 free_safe_buffer(device_info, buf);
307         }
308 }
309
310 static inline void
311 sync_single(struct device *dev, dma_addr_t dma_addr, size_t size,
312                 enum dma_data_direction dir)
313 {
314         struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
315         struct safe_buffer *buf = NULL;
316
317         if (device_info)
318                 buf = find_safe_buffer(device_info, dma_addr);
319
320         if (buf) {
321                 /*
322                  * Both of these checks from original code need to be
323                  * commented out b/c some drivers rely on the following:
324                  *
325                  * 1) Drivers may map a large chunk of memory into DMA space
326                  *    but only sync a small portion of it. Good example is
327                  *    allocating a large buffer, mapping it, and then
328                  *    breaking it up into small descriptors. No point
329                  *    in syncing the whole buffer if you only have to
330                  *    touch one descriptor.
331                  *
332                  * 2) Buffers that are mapped as DMA_BIDIRECTIONAL are
333                  *    usually only synced in one dir at a time.
334                  *
335                  * See drivers/net/eepro100.c for examples of both cases.
336                  *
337                  * -ds
338                  *
339                  * BUG_ON(buf->size != size);
340                  * BUG_ON(buf->direction != dir);
341                  */
342
343                 dev_dbg(dev,
344                         "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
345                         __func__, buf->ptr, (void *) virt_to_bus(buf->ptr),
346                         buf->safe, (void *) buf->safe_dma_addr);
347
348                 DO_STATS ( device_info->bounce_count++ );
349
350                 switch (dir) {
351                 case DMA_FROM_DEVICE:
352                         dev_dbg(dev,
353                                 "%s: copy back safe %p to unsafe %p size %d\n",
354                                 __func__, buf->safe, buf->ptr, size);
355                         memcpy(buf->ptr, buf->safe, size);
356                         break;
357                 case DMA_TO_DEVICE:
358                         dev_dbg(dev,
359                                 "%s: copy out unsafe %p to safe %p, size %d\n",
360                                 __func__,buf->ptr, buf->safe, size);
361                         memcpy(buf->safe, buf->ptr, size);
362                         break;
363                 case DMA_BIDIRECTIONAL:
364                         BUG();  /* is this allowed?  what does it mean? */
365                 default:
366                         BUG();
367                 }
368                 consistent_sync(buf->safe, size, dir);
369         } else {
370                 consistent_sync(bus_to_virt(dma_addr), size, dir);
371         }
372 }
373
374 /* ************************************************** */
375
376 /*
377  * see if a buffer address is in an 'unsafe' range.  if it is
378  * allocate a 'safe' buffer and copy the unsafe buffer into it.
379  * substitute the safe buffer for the unsafe one.
380  * (basically move the buffer from an unsafe area to a safe one)
381  */
382 dma_addr_t
383 dma_map_single(struct device *dev, void *ptr, size_t size,
384                 enum dma_data_direction dir)
385 {
386         unsigned long flags;
387         dma_addr_t dma_addr;
388
389         dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
390                 __func__, ptr, size, dir);
391
392         BUG_ON(dir == DMA_NONE);
393
394         local_irq_save(flags);
395
396         dma_addr = map_single(dev, ptr, size, dir);
397
398         local_irq_restore(flags);
399
400         return dma_addr;
401 }
402
403 /*
404  * see if a mapped address was really a "safe" buffer and if so, copy
405  * the data from the safe buffer back to the unsafe buffer and free up
406  * the safe buffer.  (basically return things back to the way they
407  * should be)
408  */
409
410 void
411 dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
412                         enum dma_data_direction dir)
413 {
414         unsigned long flags;
415
416         dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
417                 __func__, (void *) dma_addr, size, dir);
418
419         BUG_ON(dir == DMA_NONE);
420
421         local_irq_save(flags);
422
423         unmap_single(dev, dma_addr, size, dir);
424
425         local_irq_restore(flags);
426 }
427
428 int
429 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
430                 enum dma_data_direction dir)
431 {
432         unsigned long flags;
433         int i;
434
435         dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
436                 __func__, sg, nents, dir);
437
438         BUG_ON(dir == DMA_NONE);
439
440         local_irq_save(flags);
441
442         for (i = 0; i < nents; i++, sg++) {
443                 struct page *page = sg->page;
444                 unsigned int offset = sg->offset;
445                 unsigned int length = sg->length;
446                 void *ptr = page_address(page) + offset;
447
448                 sg->dma_address =
449                         map_single(dev, ptr, length, dir);
450         }
451
452         local_irq_restore(flags);
453
454         return nents;
455 }
456
457 void
458 dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
459                 enum dma_data_direction dir)
460 {
461         unsigned long flags;
462         int i;
463
464         dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
465                 __func__, sg, nents, dir);
466
467         BUG_ON(dir == DMA_NONE);
468
469         local_irq_save(flags);
470
471         for (i = 0; i < nents; i++, sg++) {
472                 dma_addr_t dma_addr = sg->dma_address;
473                 unsigned int length = sg->length;
474
475                 unmap_single(dev, dma_addr, length, dir);
476         }
477
478         local_irq_restore(flags);
479 }
480
481 void
482 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, size_t size,
483                                 enum dma_data_direction dir)
484 {
485         unsigned long flags;
486
487         dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
488                 __func__, (void *) dma_addr, size, dir);
489
490         local_irq_save(flags);
491
492         sync_single(dev, dma_addr, size, dir);
493
494         local_irq_restore(flags);
495 }
496
497 void
498 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, size_t size,
499                                 enum dma_data_direction dir)
500 {
501         unsigned long flags;
502
503         dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
504                 __func__, (void *) dma_addr, size, dir);
505
506         local_irq_save(flags);
507
508         sync_single(dev, dma_addr, size, dir);
509
510         local_irq_restore(flags);
511 }
512
513 void
514 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
515                         enum dma_data_direction dir)
516 {
517         unsigned long flags;
518         int i;
519
520         dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
521                 __func__, sg, nents, dir);
522
523         BUG_ON(dir == DMA_NONE);
524
525         local_irq_save(flags);
526
527         for (i = 0; i < nents; i++, sg++) {
528                 dma_addr_t dma_addr = sg->dma_address;
529                 unsigned int length = sg->length;
530
531                 sync_single(dev, dma_addr, length, dir);
532         }
533
534         local_irq_restore(flags);
535 }
536
537 void
538 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
539                         enum dma_data_direction dir)
540 {
541         unsigned long flags;
542         int i;
543
544         dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
545                 __func__, sg, nents, dir);
546
547         BUG_ON(dir == DMA_NONE);
548
549         local_irq_save(flags);
550
551         for (i = 0; i < nents; i++, sg++) {
552                 dma_addr_t dma_addr = sg->dma_address;
553                 unsigned int length = sg->length;
554
555                 sync_single(dev, dma_addr, length, dir);
556         }
557
558         local_irq_restore(flags);
559 }
560
561 int
562 dmabounce_register_dev(struct device *dev, unsigned long small_buffer_size,
563                         unsigned long large_buffer_size)
564 {
565         struct dmabounce_device_info *device_info;
566
567         device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
568         if (!device_info) {
569                 printk(KERN_ERR
570                         "Could not allocated dmabounce_device_info for %s",
571                         dev->bus_id);
572                 return -ENOMEM;
573         }
574
575         device_info->small_buffer_pool =
576                 dma_pool_create("small_dmabounce_pool",
577                                 dev,
578                                 small_buffer_size,
579                                 0 /* byte alignment */,
580                                 0 /* no page-crossing issues */);
581         if (!device_info->small_buffer_pool) {
582                 printk(KERN_ERR
583                         "dmabounce: could not allocate small DMA pool for %s\n",
584                         dev->bus_id);
585                 kfree(device_info);
586                 return -ENOMEM;
587         }
588
589         if (large_buffer_size) {
590                 device_info->large_buffer_pool =
591                         dma_pool_create("large_dmabounce_pool",
592                                         dev,
593                                         large_buffer_size,
594                                         0 /* byte alignment */,
595                                         0 /* no page-crossing issues */);
596                 if (!device_info->large_buffer_pool) {
597                 printk(KERN_ERR
598                         "dmabounce: could not allocate large DMA pool for %s\n",
599                         dev->bus_id);
600                         dma_pool_destroy(device_info->small_buffer_pool);
601
602                         return -ENOMEM;
603                 }
604         }
605
606         device_info->dev = dev;
607         device_info->small_buffer_size = small_buffer_size;
608         device_info->large_buffer_size = large_buffer_size;
609         INIT_LIST_HEAD(&device_info->safe_buffers);
610
611 #ifdef STATS
612         device_info->sbp_allocs = 0;
613         device_info->lbp_allocs = 0;
614         device_info->total_allocs = 0;
615         device_info->map_op_count = 0;
616         device_info->bounce_count = 0;
617 #endif
618
619         list_add(&device_info->node, &dmabounce_devs);
620
621         printk(KERN_INFO "dmabounce: registered device %s on %s bus\n",
622                 dev->bus_id, dev->bus->name);
623
624         return 0;
625 }
626
627 void
628 dmabounce_unregister_dev(struct device *dev)
629 {
630         struct dmabounce_device_info *device_info = find_dmabounce_dev(dev);
631
632         if (!device_info) {
633                 printk(KERN_WARNING
634                         "%s: Never registered with dmabounce but attempting" \
635                         "to unregister!\n", dev->bus_id);
636                 return;
637         }
638
639         if (!list_empty(&device_info->safe_buffers)) {
640                 printk(KERN_ERR
641                         "%s: Removing from dmabounce with pending buffers!\n",
642                         dev->bus_id);
643                 BUG();
644         }
645
646         if (device_info->small_buffer_pool)
647                 dma_pool_destroy(device_info->small_buffer_pool);
648         if (device_info->large_buffer_pool)
649                 dma_pool_destroy(device_info->large_buffer_pool);
650
651 #ifdef STATS
652         print_alloc_stats(device_info);
653         print_map_stats(device_info);
654 #endif
655
656         list_del(&device_info->node);
657
658         kfree(device_info);
659
660         printk(KERN_INFO "dmabounce: device %s on %s bus unregistered\n",
661                 dev->bus_id, dev->bus->name);
662 }
663
664
665 EXPORT_SYMBOL(dma_map_single);
666 EXPORT_SYMBOL(dma_unmap_single);
667 EXPORT_SYMBOL(dma_map_sg);
668 EXPORT_SYMBOL(dma_unmap_sg);
669 EXPORT_SYMBOL(dma_sync_single);
670 EXPORT_SYMBOL(dma_sync_sg);
671 EXPORT_SYMBOL(dmabounce_register_dev);
672 EXPORT_SYMBOL(dmabounce_unregister_dev);
673
674 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
675 MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
676 MODULE_LICENSE("GPL");