1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm/common/dmabounce.c
5 * Special dma_{map/unmap/dma_sync}_* routines for systems that have
6 * limited DMA windows. These functions utilize bounce buffers to
7 * copy data to/from buffers located outside the DMA region. This
8 * only works for systems in which DMA memory is at the bottom of
9 * RAM, the remainder of memory is at the top and the DMA memory
10 * can be marked as ZONE_DMA. Anything beyond that such as discontiguous
11 * DMA windows will require custom implementations that reserve memory
12 * areas at early bootup.
14 * Original version by Brad Parker (brad@heeltoe.com)
15 * Re-written by Christopher Hoover <ch@murgatroid.com>
16 * Made generic by Deepak Saxena <dsaxena@plexity.net>
18 * Copyright (C) 2002 Hewlett Packard Company.
19 * Copyright (C) 2004 MontaVista Software, Inc.
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/page-flags.h>
26 #include <linux/device.h>
27 #include <linux/dma-direct.h>
28 #include <linux/dma-map-ops.h>
29 #include <linux/dmapool.h>
30 #include <linux/list.h>
31 #include <linux/scatterlist.h>
33 #include <asm/cacheflush.h>
34 #include <asm/dma-iommu.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
{
70 struct list_head safe_buffers
;
72 unsigned long total_allocs
;
73 unsigned long map_op_count
;
74 unsigned long bounce_count
;
77 struct dmabounce_pool small
;
78 struct dmabounce_pool large
;
82 int (*needs_bounce
)(struct device
*, dma_addr_t
, size_t);
86 static ssize_t
dmabounce_show(struct device
*dev
, struct device_attribute
*attr
,
89 struct dmabounce_device_info
*device_info
= dev
->archdata
.dmabounce
;
90 return sprintf(buf
, "%lu %lu %lu %lu %lu %lu\n",
91 device_info
->small
.allocs
,
92 device_info
->large
.allocs
,
93 device_info
->total_allocs
- device_info
->small
.allocs
-
94 device_info
->large
.allocs
,
95 device_info
->total_allocs
,
96 device_info
->map_op_count
,
97 device_info
->bounce_count
);
100 static DEVICE_ATTR(dmabounce_stats
, 0400, dmabounce_show
, NULL
);
104 /* allocate a 'safe' buffer and keep track of it */
105 static inline struct safe_buffer
*
106 alloc_safe_buffer(struct dmabounce_device_info
*device_info
, void *ptr
,
107 size_t size
, enum dma_data_direction dir
)
109 struct safe_buffer
*buf
;
110 struct dmabounce_pool
*pool
;
111 struct device
*dev
= device_info
->dev
;
114 dev_dbg(dev
, "%s(ptr=%p, size=%d, dir=%d)\n",
115 __func__
, ptr
, size
, dir
);
117 if (size
<= device_info
->small
.size
) {
118 pool
= &device_info
->small
;
119 } else if (size
<= device_info
->large
.size
) {
120 pool
= &device_info
->large
;
125 buf
= kmalloc(sizeof(struct safe_buffer
), GFP_ATOMIC
);
127 dev_warn(dev
, "%s: kmalloc failed\n", __func__
);
133 buf
->direction
= dir
;
137 buf
->safe
= dma_pool_alloc(pool
->pool
, GFP_ATOMIC
,
138 &buf
->safe_dma_addr
);
140 buf
->safe
= dma_alloc_coherent(dev
, size
, &buf
->safe_dma_addr
,
144 if (buf
->safe
== NULL
) {
146 "%s: could not alloc dma memory (size=%d)\n",
155 device_info
->total_allocs
++;
158 write_lock_irqsave(&device_info
->lock
, flags
);
159 list_add(&buf
->node
, &device_info
->safe_buffers
);
160 write_unlock_irqrestore(&device_info
->lock
, flags
);
165 /* determine if a buffer is from our "safe" pool */
166 static inline struct safe_buffer
*
167 find_safe_buffer(struct dmabounce_device_info
*device_info
, dma_addr_t safe_dma_addr
)
169 struct safe_buffer
*b
, *rb
= NULL
;
172 read_lock_irqsave(&device_info
->lock
, flags
);
174 list_for_each_entry(b
, &device_info
->safe_buffers
, node
)
175 if (b
->safe_dma_addr
<= safe_dma_addr
&&
176 b
->safe_dma_addr
+ b
->size
> safe_dma_addr
) {
181 read_unlock_irqrestore(&device_info
->lock
, flags
);
186 free_safe_buffer(struct dmabounce_device_info
*device_info
, struct safe_buffer
*buf
)
190 dev_dbg(device_info
->dev
, "%s(buf=%p)\n", __func__
, buf
);
192 write_lock_irqsave(&device_info
->lock
, flags
);
194 list_del(&buf
->node
);
196 write_unlock_irqrestore(&device_info
->lock
, flags
);
199 dma_pool_free(buf
->pool
->pool
, buf
->safe
, buf
->safe_dma_addr
);
201 dma_free_coherent(device_info
->dev
, buf
->size
, buf
->safe
,
207 /* ************************************************** */
209 static struct safe_buffer
*find_safe_buffer_dev(struct device
*dev
,
210 dma_addr_t dma_addr
, const char *where
)
212 if (!dev
|| !dev
->archdata
.dmabounce
)
214 if (dma_mapping_error(dev
, dma_addr
)) {
215 dev_err(dev
, "Trying to %s invalid mapping\n", where
);
218 return find_safe_buffer(dev
->archdata
.dmabounce
, dma_addr
);
221 static int needs_bounce(struct device
*dev
, dma_addr_t dma_addr
, size_t size
)
223 if (!dev
|| !dev
->archdata
.dmabounce
)
227 unsigned long limit
, mask
= *dev
->dma_mask
;
229 limit
= (mask
+ 1) & ~mask
;
230 if (limit
&& size
> limit
) {
231 dev_err(dev
, "DMA mapping too big (requested %#x "
232 "mask %#Lx)\n", size
, *dev
->dma_mask
);
236 /* Figure out if we need to bounce from the DMA mask. */
237 if ((dma_addr
| (dma_addr
+ size
- 1)) & ~mask
)
241 return !!dev
->archdata
.dmabounce
->needs_bounce(dev
, dma_addr
, size
);
244 static inline dma_addr_t
map_single(struct device
*dev
, void *ptr
, size_t size
,
245 enum dma_data_direction dir
,
248 struct dmabounce_device_info
*device_info
= dev
->archdata
.dmabounce
;
249 struct safe_buffer
*buf
;
252 DO_STATS ( device_info
->map_op_count
++ );
254 buf
= alloc_safe_buffer(device_info
, ptr
, size
, dir
);
256 dev_err(dev
, "%s: unable to map unsafe buffer %p!\n",
258 return DMA_MAPPING_ERROR
;
261 dev_dbg(dev
, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
262 __func__
, buf
->ptr
, virt_to_dma(dev
, buf
->ptr
),
263 buf
->safe
, buf
->safe_dma_addr
);
265 if ((dir
== DMA_TO_DEVICE
|| dir
== DMA_BIDIRECTIONAL
) &&
266 !(attrs
& DMA_ATTR_SKIP_CPU_SYNC
)) {
267 dev_dbg(dev
, "%s: copy unsafe %p to safe %p, size %d\n",
268 __func__
, ptr
, buf
->safe
, size
);
269 memcpy(buf
->safe
, ptr
, size
);
272 return buf
->safe_dma_addr
;
275 static inline void unmap_single(struct device
*dev
, struct safe_buffer
*buf
,
276 size_t size
, enum dma_data_direction dir
,
279 BUG_ON(buf
->size
!= size
);
280 BUG_ON(buf
->direction
!= dir
);
282 dev_dbg(dev
, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
283 __func__
, buf
->ptr
, virt_to_dma(dev
, buf
->ptr
),
284 buf
->safe
, buf
->safe_dma_addr
);
286 DO_STATS(dev
->archdata
.dmabounce
->bounce_count
++);
288 if ((dir
== DMA_FROM_DEVICE
|| dir
== DMA_BIDIRECTIONAL
) &&
289 !(attrs
& DMA_ATTR_SKIP_CPU_SYNC
)) {
290 void *ptr
= buf
->ptr
;
292 dev_dbg(dev
, "%s: copy back safe %p to unsafe %p size %d\n",
293 __func__
, buf
->safe
, ptr
, size
);
294 memcpy(ptr
, buf
->safe
, size
);
297 * Since we may have written to a page cache page,
298 * we need to ensure that the data will be coherent
299 * with user mappings.
301 __cpuc_flush_dcache_area(ptr
, size
);
303 free_safe_buffer(dev
->archdata
.dmabounce
, buf
);
306 /* ************************************************** */
309 * see if a buffer address is in an 'unsafe' range. if it is
310 * allocate a 'safe' buffer and copy the unsafe buffer into it.
311 * substitute the safe buffer for the unsafe one.
312 * (basically move the buffer from an unsafe area to a safe one)
314 static dma_addr_t
dmabounce_map_page(struct device
*dev
, struct page
*page
,
315 unsigned long offset
, size_t size
, enum dma_data_direction dir
,
321 dev_dbg(dev
, "%s(page=%p,off=%#lx,size=%zx,dir=%x)\n",
322 __func__
, page
, offset
, size
, dir
);
324 dma_addr
= pfn_to_dma(dev
, page_to_pfn(page
)) + offset
;
326 ret
= needs_bounce(dev
, dma_addr
, size
);
328 return DMA_MAPPING_ERROR
;
331 arm_dma_ops
.sync_single_for_device(dev
, dma_addr
, size
, dir
);
335 if (PageHighMem(page
)) {
336 dev_err(dev
, "DMA buffer bouncing of HIGHMEM pages is not supported\n");
337 return DMA_MAPPING_ERROR
;
340 return map_single(dev
, page_address(page
) + offset
, size
, dir
, attrs
);
344 * see if a mapped address was really a "safe" buffer and if so, copy
345 * the data from the safe buffer back to the unsafe buffer and free up
346 * the safe buffer. (basically return things back to the way they
349 static void dmabounce_unmap_page(struct device
*dev
, dma_addr_t dma_addr
, size_t size
,
350 enum dma_data_direction dir
, unsigned long attrs
)
352 struct safe_buffer
*buf
;
354 dev_dbg(dev
, "%s(dma=%#x,size=%d,dir=%x)\n",
355 __func__
, dma_addr
, size
, dir
);
357 buf
= find_safe_buffer_dev(dev
, dma_addr
, __func__
);
359 arm_dma_ops
.sync_single_for_cpu(dev
, dma_addr
, size
, dir
);
363 unmap_single(dev
, buf
, size
, dir
, attrs
);
366 static int __dmabounce_sync_for_cpu(struct device
*dev
, dma_addr_t addr
,
367 size_t sz
, enum dma_data_direction dir
)
369 struct safe_buffer
*buf
;
372 dev_dbg(dev
, "%s(dma=%#x,sz=%zx,dir=%x)\n",
373 __func__
, addr
, sz
, dir
);
375 buf
= find_safe_buffer_dev(dev
, addr
, __func__
);
379 off
= addr
- buf
->safe_dma_addr
;
381 BUG_ON(buf
->direction
!= dir
);
383 dev_dbg(dev
, "%s: unsafe buffer %p (dma=%#x off=%#lx) mapped to %p (dma=%#x)\n",
384 __func__
, buf
->ptr
, virt_to_dma(dev
, buf
->ptr
), off
,
385 buf
->safe
, buf
->safe_dma_addr
);
387 DO_STATS(dev
->archdata
.dmabounce
->bounce_count
++);
389 if (dir
== DMA_FROM_DEVICE
|| dir
== DMA_BIDIRECTIONAL
) {
390 dev_dbg(dev
, "%s: copy back safe %p to unsafe %p size %d\n",
391 __func__
, buf
->safe
+ off
, buf
->ptr
+ off
, sz
);
392 memcpy(buf
->ptr
+ off
, buf
->safe
+ off
, sz
);
397 static void dmabounce_sync_for_cpu(struct device
*dev
,
398 dma_addr_t handle
, size_t size
, enum dma_data_direction dir
)
400 if (!__dmabounce_sync_for_cpu(dev
, handle
, size
, dir
))
403 arm_dma_ops
.sync_single_for_cpu(dev
, handle
, size
, dir
);
406 static int __dmabounce_sync_for_device(struct device
*dev
, dma_addr_t addr
,
407 size_t sz
, enum dma_data_direction dir
)
409 struct safe_buffer
*buf
;
412 dev_dbg(dev
, "%s(dma=%#x,sz=%zx,dir=%x)\n",
413 __func__
, addr
, sz
, dir
);
415 buf
= find_safe_buffer_dev(dev
, addr
, __func__
);
419 off
= addr
- buf
->safe_dma_addr
;
421 BUG_ON(buf
->direction
!= dir
);
423 dev_dbg(dev
, "%s: unsafe buffer %p (dma=%#x off=%#lx) mapped to %p (dma=%#x)\n",
424 __func__
, buf
->ptr
, virt_to_dma(dev
, buf
->ptr
), off
,
425 buf
->safe
, buf
->safe_dma_addr
);
427 DO_STATS(dev
->archdata
.dmabounce
->bounce_count
++);
429 if (dir
== DMA_TO_DEVICE
|| dir
== DMA_BIDIRECTIONAL
) {
430 dev_dbg(dev
, "%s: copy out unsafe %p to safe %p, size %d\n",
431 __func__
,buf
->ptr
+ off
, buf
->safe
+ off
, sz
);
432 memcpy(buf
->safe
+ off
, buf
->ptr
+ off
, sz
);
437 static void dmabounce_sync_for_device(struct device
*dev
,
438 dma_addr_t handle
, size_t size
, enum dma_data_direction dir
)
440 if (!__dmabounce_sync_for_device(dev
, handle
, size
, dir
))
443 arm_dma_ops
.sync_single_for_device(dev
, handle
, size
, dir
);
446 static int dmabounce_dma_supported(struct device
*dev
, u64 dma_mask
)
448 if (dev
->archdata
.dmabounce
)
451 return arm_dma_ops
.dma_supported(dev
, dma_mask
);
454 static const struct dma_map_ops dmabounce_ops
= {
455 .alloc
= arm_dma_alloc
,
456 .free
= arm_dma_free
,
457 .mmap
= arm_dma_mmap
,
458 .get_sgtable
= arm_dma_get_sgtable
,
459 .map_page
= dmabounce_map_page
,
460 .unmap_page
= dmabounce_unmap_page
,
461 .sync_single_for_cpu
= dmabounce_sync_for_cpu
,
462 .sync_single_for_device
= dmabounce_sync_for_device
,
463 .map_sg
= arm_dma_map_sg
,
464 .unmap_sg
= arm_dma_unmap_sg
,
465 .sync_sg_for_cpu
= arm_dma_sync_sg_for_cpu
,
466 .sync_sg_for_device
= arm_dma_sync_sg_for_device
,
467 .dma_supported
= dmabounce_dma_supported
,
470 static int dmabounce_init_pool(struct dmabounce_pool
*pool
, struct device
*dev
,
471 const char *name
, unsigned long size
)
474 DO_STATS(pool
->allocs
= 0);
475 pool
->pool
= dma_pool_create(name
, dev
, size
,
476 0 /* byte alignment */,
477 0 /* no page-crossing issues */);
479 return pool
->pool
? 0 : -ENOMEM
;
482 int dmabounce_register_dev(struct device
*dev
, unsigned long small_buffer_size
,
483 unsigned long large_buffer_size
,
484 int (*needs_bounce_fn
)(struct device
*, dma_addr_t
, size_t))
486 struct dmabounce_device_info
*device_info
;
489 device_info
= kmalloc(sizeof(struct dmabounce_device_info
), GFP_ATOMIC
);
492 "Could not allocated dmabounce_device_info\n");
496 ret
= dmabounce_init_pool(&device_info
->small
, dev
,
497 "small_dmabounce_pool", small_buffer_size
);
500 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
505 if (large_buffer_size
) {
506 ret
= dmabounce_init_pool(&device_info
->large
, dev
,
507 "large_dmabounce_pool",
511 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
517 device_info
->dev
= dev
;
518 INIT_LIST_HEAD(&device_info
->safe_buffers
);
519 rwlock_init(&device_info
->lock
);
520 device_info
->needs_bounce
= needs_bounce_fn
;
523 device_info
->total_allocs
= 0;
524 device_info
->map_op_count
= 0;
525 device_info
->bounce_count
= 0;
526 device_info
->attr_res
= device_create_file(dev
, &dev_attr_dmabounce_stats
);
529 dev
->archdata
.dmabounce
= device_info
;
530 set_dma_ops(dev
, &dmabounce_ops
);
532 dev_info(dev
, "dmabounce: registered device\n");
537 dma_pool_destroy(device_info
->small
.pool
);
542 EXPORT_SYMBOL(dmabounce_register_dev
);
544 void dmabounce_unregister_dev(struct device
*dev
)
546 struct dmabounce_device_info
*device_info
= dev
->archdata
.dmabounce
;
548 dev
->archdata
.dmabounce
= NULL
;
549 set_dma_ops(dev
, NULL
);
553 "Never registered with dmabounce but attempting"
558 if (!list_empty(&device_info
->safe_buffers
)) {
560 "Removing from dmabounce with pending buffers!\n");
564 if (device_info
->small
.pool
)
565 dma_pool_destroy(device_info
->small
.pool
);
566 if (device_info
->large
.pool
)
567 dma_pool_destroy(device_info
->large
.pool
);
570 if (device_info
->attr_res
== 0)
571 device_remove_file(dev
, &dev_attr_dmabounce_stats
);
576 dev_info(dev
, "dmabounce: device unregistered\n");
578 EXPORT_SYMBOL(dmabounce_unregister_dev
);
580 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
581 MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
582 MODULE_LICENSE("GPL");