Merge 5.0-rc6 into driver-core-next
[linux/fpc-iii.git] / arch / arm / common / dmabounce.c
blob5ba4622030cacb49dae81a1c6eb9416b66787aa2
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 discontiguous
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/page-flags.h>
29 #include <linux/device.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/dmapool.h>
32 #include <linux/list.h>
33 #include <linux/scatterlist.h>
35 #include <asm/cacheflush.h>
36 #include <asm/dma-iommu.h>
38 #undef STATS
40 #ifdef STATS
41 #define DO_STATS(X) do { X ; } while (0)
42 #else
43 #define DO_STATS(X) do { } while (0)
44 #endif
46 /* ************************************************** */
48 struct safe_buffer {
49 struct list_head node;
51 /* original request */
52 void *ptr;
53 size_t size;
54 int direction;
56 /* safe buffer info */
57 struct dmabounce_pool *pool;
58 void *safe;
59 dma_addr_t safe_dma_addr;
62 struct dmabounce_pool {
63 unsigned long size;
64 struct dma_pool *pool;
65 #ifdef STATS
66 unsigned long allocs;
67 #endif
70 struct dmabounce_device_info {
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 int attr_res;
78 #endif
79 struct dmabounce_pool small;
80 struct dmabounce_pool large;
82 rwlock_t lock;
84 int (*needs_bounce)(struct device *, dma_addr_t, size_t);
87 #ifdef STATS
88 static ssize_t dmabounce_show(struct device *dev, struct device_attribute *attr,
89 char *buf)
91 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
92 return sprintf(buf, "%lu %lu %lu %lu %lu %lu\n",
93 device_info->small.allocs,
94 device_info->large.allocs,
95 device_info->total_allocs - device_info->small.allocs -
96 device_info->large.allocs,
97 device_info->total_allocs,
98 device_info->map_op_count,
99 device_info->bounce_count);
102 static DEVICE_ATTR(dmabounce_stats, 0400, dmabounce_show, NULL);
103 #endif
106 /* allocate a 'safe' buffer and keep track of it */
107 static inline struct safe_buffer *
108 alloc_safe_buffer(struct dmabounce_device_info *device_info, void *ptr,
109 size_t size, enum dma_data_direction dir)
111 struct safe_buffer *buf;
112 struct dmabounce_pool *pool;
113 struct device *dev = device_info->dev;
114 unsigned long flags;
116 dev_dbg(dev, "%s(ptr=%p, size=%d, dir=%d)\n",
117 __func__, ptr, size, dir);
119 if (size <= device_info->small.size) {
120 pool = &device_info->small;
121 } else if (size <= device_info->large.size) {
122 pool = &device_info->large;
123 } else {
124 pool = NULL;
127 buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC);
128 if (buf == NULL) {
129 dev_warn(dev, "%s: kmalloc failed\n", __func__);
130 return NULL;
133 buf->ptr = ptr;
134 buf->size = size;
135 buf->direction = dir;
136 buf->pool = pool;
138 if (pool) {
139 buf->safe = dma_pool_alloc(pool->pool, GFP_ATOMIC,
140 &buf->safe_dma_addr);
141 } else {
142 buf->safe = dma_alloc_coherent(dev, size, &buf->safe_dma_addr,
143 GFP_ATOMIC);
146 if (buf->safe == NULL) {
147 dev_warn(dev,
148 "%s: could not alloc dma memory (size=%d)\n",
149 __func__, size);
150 kfree(buf);
151 return NULL;
154 #ifdef STATS
155 if (pool)
156 pool->allocs++;
157 device_info->total_allocs++;
158 #endif
160 write_lock_irqsave(&device_info->lock, flags);
161 list_add(&buf->node, &device_info->safe_buffers);
162 write_unlock_irqrestore(&device_info->lock, flags);
164 return buf;
167 /* determine if a buffer is from our "safe" pool */
168 static inline struct safe_buffer *
169 find_safe_buffer(struct dmabounce_device_info *device_info, dma_addr_t safe_dma_addr)
171 struct safe_buffer *b, *rb = NULL;
172 unsigned long flags;
174 read_lock_irqsave(&device_info->lock, flags);
176 list_for_each_entry(b, &device_info->safe_buffers, node)
177 if (b->safe_dma_addr <= safe_dma_addr &&
178 b->safe_dma_addr + b->size > safe_dma_addr) {
179 rb = b;
180 break;
183 read_unlock_irqrestore(&device_info->lock, flags);
184 return rb;
187 static inline void
188 free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf)
190 unsigned long flags;
192 dev_dbg(device_info->dev, "%s(buf=%p)\n", __func__, buf);
194 write_lock_irqsave(&device_info->lock, flags);
196 list_del(&buf->node);
198 write_unlock_irqrestore(&device_info->lock, flags);
200 if (buf->pool)
201 dma_pool_free(buf->pool->pool, buf->safe, buf->safe_dma_addr);
202 else
203 dma_free_coherent(device_info->dev, buf->size, buf->safe,
204 buf->safe_dma_addr);
206 kfree(buf);
209 /* ************************************************** */
211 static struct safe_buffer *find_safe_buffer_dev(struct device *dev,
212 dma_addr_t dma_addr, const char *where)
214 if (!dev || !dev->archdata.dmabounce)
215 return NULL;
216 if (dma_mapping_error(dev, dma_addr)) {
217 dev_err(dev, "Trying to %s invalid mapping\n", where);
218 return NULL;
220 return find_safe_buffer(dev->archdata.dmabounce, dma_addr);
223 static int needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
225 if (!dev || !dev->archdata.dmabounce)
226 return 0;
228 if (dev->dma_mask) {
229 unsigned long limit, mask = *dev->dma_mask;
231 limit = (mask + 1) & ~mask;
232 if (limit && size > limit) {
233 dev_err(dev, "DMA mapping too big (requested %#x "
234 "mask %#Lx)\n", size, *dev->dma_mask);
235 return -E2BIG;
238 /* Figure out if we need to bounce from the DMA mask. */
239 if ((dma_addr | (dma_addr + size - 1)) & ~mask)
240 return 1;
243 return !!dev->archdata.dmabounce->needs_bounce(dev, dma_addr, size);
246 static inline dma_addr_t map_single(struct device *dev, void *ptr, size_t size,
247 enum dma_data_direction dir,
248 unsigned long attrs)
250 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
251 struct safe_buffer *buf;
253 if (device_info)
254 DO_STATS ( device_info->map_op_count++ );
256 buf = alloc_safe_buffer(device_info, ptr, size, dir);
257 if (buf == NULL) {
258 dev_err(dev, "%s: unable to map unsafe buffer %p!\n",
259 __func__, ptr);
260 return DMA_MAPPING_ERROR;
263 dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
264 __func__, buf->ptr, virt_to_dma(dev, buf->ptr),
265 buf->safe, buf->safe_dma_addr);
267 if ((dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) &&
268 !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
269 dev_dbg(dev, "%s: copy unsafe %p to safe %p, size %d\n",
270 __func__, ptr, buf->safe, size);
271 memcpy(buf->safe, ptr, size);
274 return buf->safe_dma_addr;
277 static inline void unmap_single(struct device *dev, struct safe_buffer *buf,
278 size_t size, enum dma_data_direction dir,
279 unsigned long attrs)
281 BUG_ON(buf->size != size);
282 BUG_ON(buf->direction != dir);
284 dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
285 __func__, buf->ptr, virt_to_dma(dev, buf->ptr),
286 buf->safe, buf->safe_dma_addr);
288 DO_STATS(dev->archdata.dmabounce->bounce_count++);
290 if ((dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) &&
291 !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
292 void *ptr = buf->ptr;
294 dev_dbg(dev, "%s: copy back safe %p to unsafe %p size %d\n",
295 __func__, buf->safe, ptr, size);
296 memcpy(ptr, buf->safe, size);
299 * Since we may have written to a page cache page,
300 * we need to ensure that the data will be coherent
301 * with user mappings.
303 __cpuc_flush_dcache_area(ptr, size);
305 free_safe_buffer(dev->archdata.dmabounce, buf);
308 /* ************************************************** */
311 * see if a buffer address is in an 'unsafe' range. if it is
312 * allocate a 'safe' buffer and copy the unsafe buffer into it.
313 * substitute the safe buffer for the unsafe one.
314 * (basically move the buffer from an unsafe area to a safe one)
316 static dma_addr_t dmabounce_map_page(struct device *dev, struct page *page,
317 unsigned long offset, size_t size, enum dma_data_direction dir,
318 unsigned long attrs)
320 dma_addr_t dma_addr;
321 int ret;
323 dev_dbg(dev, "%s(page=%p,off=%#lx,size=%zx,dir=%x)\n",
324 __func__, page, offset, size, dir);
326 dma_addr = pfn_to_dma(dev, page_to_pfn(page)) + offset;
328 ret = needs_bounce(dev, dma_addr, size);
329 if (ret < 0)
330 return DMA_MAPPING_ERROR;
332 if (ret == 0) {
333 arm_dma_ops.sync_single_for_device(dev, dma_addr, size, dir);
334 return dma_addr;
337 if (PageHighMem(page)) {
338 dev_err(dev, "DMA buffer bouncing of HIGHMEM pages is not supported\n");
339 return DMA_MAPPING_ERROR;
342 return map_single(dev, page_address(page) + offset, size, dir, attrs);
346 * see if a mapped address was really a "safe" buffer and if so, copy
347 * the data from the safe buffer back to the unsafe buffer and free up
348 * the safe buffer. (basically return things back to the way they
349 * should be)
351 static void dmabounce_unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
352 enum dma_data_direction dir, unsigned long attrs)
354 struct safe_buffer *buf;
356 dev_dbg(dev, "%s(dma=%#x,size=%d,dir=%x)\n",
357 __func__, dma_addr, size, dir);
359 buf = find_safe_buffer_dev(dev, dma_addr, __func__);
360 if (!buf) {
361 arm_dma_ops.sync_single_for_cpu(dev, dma_addr, size, dir);
362 return;
365 unmap_single(dev, buf, size, dir, attrs);
368 static int __dmabounce_sync_for_cpu(struct device *dev, dma_addr_t addr,
369 size_t sz, enum dma_data_direction dir)
371 struct safe_buffer *buf;
372 unsigned long off;
374 dev_dbg(dev, "%s(dma=%#x,sz=%zx,dir=%x)\n",
375 __func__, addr, sz, dir);
377 buf = find_safe_buffer_dev(dev, addr, __func__);
378 if (!buf)
379 return 1;
381 off = addr - buf->safe_dma_addr;
383 BUG_ON(buf->direction != dir);
385 dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x off=%#lx) mapped to %p (dma=%#x)\n",
386 __func__, buf->ptr, virt_to_dma(dev, buf->ptr), off,
387 buf->safe, buf->safe_dma_addr);
389 DO_STATS(dev->archdata.dmabounce->bounce_count++);
391 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
392 dev_dbg(dev, "%s: copy back safe %p to unsafe %p size %d\n",
393 __func__, buf->safe + off, buf->ptr + off, sz);
394 memcpy(buf->ptr + off, buf->safe + off, sz);
396 return 0;
399 static void dmabounce_sync_for_cpu(struct device *dev,
400 dma_addr_t handle, size_t size, enum dma_data_direction dir)
402 if (!__dmabounce_sync_for_cpu(dev, handle, size, dir))
403 return;
405 arm_dma_ops.sync_single_for_cpu(dev, handle, size, dir);
408 static int __dmabounce_sync_for_device(struct device *dev, dma_addr_t addr,
409 size_t sz, enum dma_data_direction dir)
411 struct safe_buffer *buf;
412 unsigned long off;
414 dev_dbg(dev, "%s(dma=%#x,sz=%zx,dir=%x)\n",
415 __func__, addr, sz, dir);
417 buf = find_safe_buffer_dev(dev, addr, __func__);
418 if (!buf)
419 return 1;
421 off = addr - buf->safe_dma_addr;
423 BUG_ON(buf->direction != dir);
425 dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x off=%#lx) mapped to %p (dma=%#x)\n",
426 __func__, buf->ptr, virt_to_dma(dev, buf->ptr), off,
427 buf->safe, buf->safe_dma_addr);
429 DO_STATS(dev->archdata.dmabounce->bounce_count++);
431 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) {
432 dev_dbg(dev, "%s: copy out unsafe %p to safe %p, size %d\n",
433 __func__,buf->ptr + off, buf->safe + off, sz);
434 memcpy(buf->safe + off, buf->ptr + off, sz);
436 return 0;
439 static void dmabounce_sync_for_device(struct device *dev,
440 dma_addr_t handle, size_t size, enum dma_data_direction dir)
442 if (!__dmabounce_sync_for_device(dev, handle, size, dir))
443 return;
445 arm_dma_ops.sync_single_for_device(dev, handle, size, dir);
448 static int dmabounce_dma_supported(struct device *dev, u64 dma_mask)
450 if (dev->archdata.dmabounce)
451 return 0;
453 return arm_dma_ops.dma_supported(dev, dma_mask);
456 static const struct dma_map_ops dmabounce_ops = {
457 .alloc = arm_dma_alloc,
458 .free = arm_dma_free,
459 .mmap = arm_dma_mmap,
460 .get_sgtable = arm_dma_get_sgtable,
461 .map_page = dmabounce_map_page,
462 .unmap_page = dmabounce_unmap_page,
463 .sync_single_for_cpu = dmabounce_sync_for_cpu,
464 .sync_single_for_device = dmabounce_sync_for_device,
465 .map_sg = arm_dma_map_sg,
466 .unmap_sg = arm_dma_unmap_sg,
467 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
468 .sync_sg_for_device = arm_dma_sync_sg_for_device,
469 .dma_supported = dmabounce_dma_supported,
472 static int dmabounce_init_pool(struct dmabounce_pool *pool, struct device *dev,
473 const char *name, unsigned long size)
475 pool->size = size;
476 DO_STATS(pool->allocs = 0);
477 pool->pool = dma_pool_create(name, dev, size,
478 0 /* byte alignment */,
479 0 /* no page-crossing issues */);
481 return pool->pool ? 0 : -ENOMEM;
484 int dmabounce_register_dev(struct device *dev, unsigned long small_buffer_size,
485 unsigned long large_buffer_size,
486 int (*needs_bounce_fn)(struct device *, dma_addr_t, size_t))
488 struct dmabounce_device_info *device_info;
489 int ret;
491 device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
492 if (!device_info) {
493 dev_err(dev,
494 "Could not allocated dmabounce_device_info\n");
495 return -ENOMEM;
498 ret = dmabounce_init_pool(&device_info->small, dev,
499 "small_dmabounce_pool", small_buffer_size);
500 if (ret) {
501 dev_err(dev,
502 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
503 small_buffer_size);
504 goto err_free;
507 if (large_buffer_size) {
508 ret = dmabounce_init_pool(&device_info->large, dev,
509 "large_dmabounce_pool",
510 large_buffer_size);
511 if (ret) {
512 dev_err(dev,
513 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
514 large_buffer_size);
515 goto err_destroy;
519 device_info->dev = dev;
520 INIT_LIST_HEAD(&device_info->safe_buffers);
521 rwlock_init(&device_info->lock);
522 device_info->needs_bounce = needs_bounce_fn;
524 #ifdef STATS
525 device_info->total_allocs = 0;
526 device_info->map_op_count = 0;
527 device_info->bounce_count = 0;
528 device_info->attr_res = device_create_file(dev, &dev_attr_dmabounce_stats);
529 #endif
531 dev->archdata.dmabounce = device_info;
532 set_dma_ops(dev, &dmabounce_ops);
534 dev_info(dev, "dmabounce: registered device\n");
536 return 0;
538 err_destroy:
539 dma_pool_destroy(device_info->small.pool);
540 err_free:
541 kfree(device_info);
542 return ret;
544 EXPORT_SYMBOL(dmabounce_register_dev);
546 void dmabounce_unregister_dev(struct device *dev)
548 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
550 dev->archdata.dmabounce = NULL;
551 set_dma_ops(dev, NULL);
553 if (!device_info) {
554 dev_warn(dev,
555 "Never registered with dmabounce but attempting"
556 "to unregister!\n");
557 return;
560 if (!list_empty(&device_info->safe_buffers)) {
561 dev_err(dev,
562 "Removing from dmabounce with pending buffers!\n");
563 BUG();
566 if (device_info->small.pool)
567 dma_pool_destroy(device_info->small.pool);
568 if (device_info->large.pool)
569 dma_pool_destroy(device_info->large.pool);
571 #ifdef STATS
572 if (device_info->attr_res == 0)
573 device_remove_file(dev, &dev_attr_dmabounce_stats);
574 #endif
576 kfree(device_info);
578 dev_info(dev, "dmabounce: device unregistered\n");
580 EXPORT_SYMBOL(dmabounce_unregister_dev);
582 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
583 MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
584 MODULE_LICENSE("GPL");