Linux 5.7.7
[linux/fpc-iii.git] / arch / arm / common / dmabounce.c
blobf4b719bde76367abe084f6331517c817fa25bf41
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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-mapping.h>
28 #include <linux/dmapool.h>
29 #include <linux/list.h>
30 #include <linux/scatterlist.h>
32 #include <asm/cacheflush.h>
33 #include <asm/dma-iommu.h>
35 #undef STATS
37 #ifdef STATS
38 #define DO_STATS(X) do { X ; } while (0)
39 #else
40 #define DO_STATS(X) do { } while (0)
41 #endif
43 /* ************************************************** */
45 struct safe_buffer {
46 struct list_head node;
48 /* original request */
49 void *ptr;
50 size_t size;
51 int direction;
53 /* safe buffer info */
54 struct dmabounce_pool *pool;
55 void *safe;
56 dma_addr_t safe_dma_addr;
59 struct dmabounce_pool {
60 unsigned long size;
61 struct dma_pool *pool;
62 #ifdef STATS
63 unsigned long allocs;
64 #endif
67 struct dmabounce_device_info {
68 struct device *dev;
69 struct list_head safe_buffers;
70 #ifdef STATS
71 unsigned long total_allocs;
72 unsigned long map_op_count;
73 unsigned long bounce_count;
74 int attr_res;
75 #endif
76 struct dmabounce_pool small;
77 struct dmabounce_pool large;
79 rwlock_t lock;
81 int (*needs_bounce)(struct device *, dma_addr_t, size_t);
84 #ifdef STATS
85 static ssize_t dmabounce_show(struct device *dev, struct device_attribute *attr,
86 char *buf)
88 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
89 return sprintf(buf, "%lu %lu %lu %lu %lu %lu\n",
90 device_info->small.allocs,
91 device_info->large.allocs,
92 device_info->total_allocs - device_info->small.allocs -
93 device_info->large.allocs,
94 device_info->total_allocs,
95 device_info->map_op_count,
96 device_info->bounce_count);
99 static DEVICE_ATTR(dmabounce_stats, 0400, dmabounce_show, NULL);
100 #endif
103 /* allocate a 'safe' buffer and keep track of it */
104 static inline struct safe_buffer *
105 alloc_safe_buffer(struct dmabounce_device_info *device_info, void *ptr,
106 size_t size, enum dma_data_direction dir)
108 struct safe_buffer *buf;
109 struct dmabounce_pool *pool;
110 struct device *dev = device_info->dev;
111 unsigned long flags;
113 dev_dbg(dev, "%s(ptr=%p, size=%d, dir=%d)\n",
114 __func__, ptr, size, dir);
116 if (size <= device_info->small.size) {
117 pool = &device_info->small;
118 } else if (size <= device_info->large.size) {
119 pool = &device_info->large;
120 } else {
121 pool = NULL;
124 buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC);
125 if (buf == NULL) {
126 dev_warn(dev, "%s: kmalloc failed\n", __func__);
127 return NULL;
130 buf->ptr = ptr;
131 buf->size = size;
132 buf->direction = dir;
133 buf->pool = pool;
135 if (pool) {
136 buf->safe = dma_pool_alloc(pool->pool, GFP_ATOMIC,
137 &buf->safe_dma_addr);
138 } else {
139 buf->safe = dma_alloc_coherent(dev, size, &buf->safe_dma_addr,
140 GFP_ATOMIC);
143 if (buf->safe == NULL) {
144 dev_warn(dev,
145 "%s: could not alloc dma memory (size=%d)\n",
146 __func__, size);
147 kfree(buf);
148 return NULL;
151 #ifdef STATS
152 if (pool)
153 pool->allocs++;
154 device_info->total_allocs++;
155 #endif
157 write_lock_irqsave(&device_info->lock, flags);
158 list_add(&buf->node, &device_info->safe_buffers);
159 write_unlock_irqrestore(&device_info->lock, flags);
161 return buf;
164 /* determine if a buffer is from our "safe" pool */
165 static inline struct safe_buffer *
166 find_safe_buffer(struct dmabounce_device_info *device_info, dma_addr_t safe_dma_addr)
168 struct safe_buffer *b, *rb = NULL;
169 unsigned long flags;
171 read_lock_irqsave(&device_info->lock, flags);
173 list_for_each_entry(b, &device_info->safe_buffers, node)
174 if (b->safe_dma_addr <= safe_dma_addr &&
175 b->safe_dma_addr + b->size > safe_dma_addr) {
176 rb = b;
177 break;
180 read_unlock_irqrestore(&device_info->lock, flags);
181 return rb;
184 static inline void
185 free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf)
187 unsigned long flags;
189 dev_dbg(device_info->dev, "%s(buf=%p)\n", __func__, buf);
191 write_lock_irqsave(&device_info->lock, flags);
193 list_del(&buf->node);
195 write_unlock_irqrestore(&device_info->lock, flags);
197 if (buf->pool)
198 dma_pool_free(buf->pool->pool, buf->safe, buf->safe_dma_addr);
199 else
200 dma_free_coherent(device_info->dev, buf->size, buf->safe,
201 buf->safe_dma_addr);
203 kfree(buf);
206 /* ************************************************** */
208 static struct safe_buffer *find_safe_buffer_dev(struct device *dev,
209 dma_addr_t dma_addr, const char *where)
211 if (!dev || !dev->archdata.dmabounce)
212 return NULL;
213 if (dma_mapping_error(dev, dma_addr)) {
214 dev_err(dev, "Trying to %s invalid mapping\n", where);
215 return NULL;
217 return find_safe_buffer(dev->archdata.dmabounce, dma_addr);
220 static int needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
222 if (!dev || !dev->archdata.dmabounce)
223 return 0;
225 if (dev->dma_mask) {
226 unsigned long limit, mask = *dev->dma_mask;
228 limit = (mask + 1) & ~mask;
229 if (limit && size > limit) {
230 dev_err(dev, "DMA mapping too big (requested %#x "
231 "mask %#Lx)\n", size, *dev->dma_mask);
232 return -E2BIG;
235 /* Figure out if we need to bounce from the DMA mask. */
236 if ((dma_addr | (dma_addr + size - 1)) & ~mask)
237 return 1;
240 return !!dev->archdata.dmabounce->needs_bounce(dev, dma_addr, size);
243 static inline dma_addr_t map_single(struct device *dev, void *ptr, size_t size,
244 enum dma_data_direction dir,
245 unsigned long attrs)
247 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
248 struct safe_buffer *buf;
250 if (device_info)
251 DO_STATS ( device_info->map_op_count++ );
253 buf = alloc_safe_buffer(device_info, ptr, size, dir);
254 if (buf == NULL) {
255 dev_err(dev, "%s: unable to map unsafe buffer %p!\n",
256 __func__, ptr);
257 return DMA_MAPPING_ERROR;
260 dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
261 __func__, buf->ptr, virt_to_dma(dev, buf->ptr),
262 buf->safe, buf->safe_dma_addr);
264 if ((dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) &&
265 !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
266 dev_dbg(dev, "%s: copy unsafe %p to safe %p, size %d\n",
267 __func__, ptr, buf->safe, size);
268 memcpy(buf->safe, ptr, size);
271 return buf->safe_dma_addr;
274 static inline void unmap_single(struct device *dev, struct safe_buffer *buf,
275 size_t size, enum dma_data_direction dir,
276 unsigned long attrs)
278 BUG_ON(buf->size != size);
279 BUG_ON(buf->direction != dir);
281 dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
282 __func__, buf->ptr, virt_to_dma(dev, buf->ptr),
283 buf->safe, buf->safe_dma_addr);
285 DO_STATS(dev->archdata.dmabounce->bounce_count++);
287 if ((dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) &&
288 !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
289 void *ptr = buf->ptr;
291 dev_dbg(dev, "%s: copy back safe %p to unsafe %p size %d\n",
292 __func__, buf->safe, ptr, size);
293 memcpy(ptr, buf->safe, size);
296 * Since we may have written to a page cache page,
297 * we need to ensure that the data will be coherent
298 * with user mappings.
300 __cpuc_flush_dcache_area(ptr, size);
302 free_safe_buffer(dev->archdata.dmabounce, buf);
305 /* ************************************************** */
308 * see if a buffer address is in an 'unsafe' range. if it is
309 * allocate a 'safe' buffer and copy the unsafe buffer into it.
310 * substitute the safe buffer for the unsafe one.
311 * (basically move the buffer from an unsafe area to a safe one)
313 static dma_addr_t dmabounce_map_page(struct device *dev, struct page *page,
314 unsigned long offset, size_t size, enum dma_data_direction dir,
315 unsigned long attrs)
317 dma_addr_t dma_addr;
318 int ret;
320 dev_dbg(dev, "%s(page=%p,off=%#lx,size=%zx,dir=%x)\n",
321 __func__, page, offset, size, dir);
323 dma_addr = pfn_to_dma(dev, page_to_pfn(page)) + offset;
325 ret = needs_bounce(dev, dma_addr, size);
326 if (ret < 0)
327 return DMA_MAPPING_ERROR;
329 if (ret == 0) {
330 arm_dma_ops.sync_single_for_device(dev, dma_addr, size, dir);
331 return dma_addr;
334 if (PageHighMem(page)) {
335 dev_err(dev, "DMA buffer bouncing of HIGHMEM pages is not supported\n");
336 return DMA_MAPPING_ERROR;
339 return map_single(dev, page_address(page) + offset, size, dir, attrs);
343 * see if a mapped address was really a "safe" buffer and if so, copy
344 * the data from the safe buffer back to the unsafe buffer and free up
345 * the safe buffer. (basically return things back to the way they
346 * should be)
348 static void dmabounce_unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
349 enum dma_data_direction dir, unsigned long attrs)
351 struct safe_buffer *buf;
353 dev_dbg(dev, "%s(dma=%#x,size=%d,dir=%x)\n",
354 __func__, dma_addr, size, dir);
356 buf = find_safe_buffer_dev(dev, dma_addr, __func__);
357 if (!buf) {
358 arm_dma_ops.sync_single_for_cpu(dev, dma_addr, size, dir);
359 return;
362 unmap_single(dev, buf, size, dir, attrs);
365 static int __dmabounce_sync_for_cpu(struct device *dev, dma_addr_t addr,
366 size_t sz, enum dma_data_direction dir)
368 struct safe_buffer *buf;
369 unsigned long off;
371 dev_dbg(dev, "%s(dma=%#x,sz=%zx,dir=%x)\n",
372 __func__, addr, sz, dir);
374 buf = find_safe_buffer_dev(dev, addr, __func__);
375 if (!buf)
376 return 1;
378 off = addr - buf->safe_dma_addr;
380 BUG_ON(buf->direction != dir);
382 dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x off=%#lx) mapped to %p (dma=%#x)\n",
383 __func__, buf->ptr, virt_to_dma(dev, buf->ptr), off,
384 buf->safe, buf->safe_dma_addr);
386 DO_STATS(dev->archdata.dmabounce->bounce_count++);
388 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
389 dev_dbg(dev, "%s: copy back safe %p to unsafe %p size %d\n",
390 __func__, buf->safe + off, buf->ptr + off, sz);
391 memcpy(buf->ptr + off, buf->safe + off, sz);
393 return 0;
396 static void dmabounce_sync_for_cpu(struct device *dev,
397 dma_addr_t handle, size_t size, enum dma_data_direction dir)
399 if (!__dmabounce_sync_for_cpu(dev, handle, size, dir))
400 return;
402 arm_dma_ops.sync_single_for_cpu(dev, handle, size, dir);
405 static int __dmabounce_sync_for_device(struct device *dev, dma_addr_t addr,
406 size_t sz, enum dma_data_direction dir)
408 struct safe_buffer *buf;
409 unsigned long off;
411 dev_dbg(dev, "%s(dma=%#x,sz=%zx,dir=%x)\n",
412 __func__, addr, sz, dir);
414 buf = find_safe_buffer_dev(dev, addr, __func__);
415 if (!buf)
416 return 1;
418 off = addr - buf->safe_dma_addr;
420 BUG_ON(buf->direction != dir);
422 dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x off=%#lx) mapped to %p (dma=%#x)\n",
423 __func__, buf->ptr, virt_to_dma(dev, buf->ptr), off,
424 buf->safe, buf->safe_dma_addr);
426 DO_STATS(dev->archdata.dmabounce->bounce_count++);
428 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) {
429 dev_dbg(dev, "%s: copy out unsafe %p to safe %p, size %d\n",
430 __func__,buf->ptr + off, buf->safe + off, sz);
431 memcpy(buf->safe + off, buf->ptr + off, sz);
433 return 0;
436 static void dmabounce_sync_for_device(struct device *dev,
437 dma_addr_t handle, size_t size, enum dma_data_direction dir)
439 if (!__dmabounce_sync_for_device(dev, handle, size, dir))
440 return;
442 arm_dma_ops.sync_single_for_device(dev, handle, size, dir);
445 static int dmabounce_dma_supported(struct device *dev, u64 dma_mask)
447 if (dev->archdata.dmabounce)
448 return 0;
450 return arm_dma_ops.dma_supported(dev, dma_mask);
453 static const struct dma_map_ops dmabounce_ops = {
454 .alloc = arm_dma_alloc,
455 .free = arm_dma_free,
456 .mmap = arm_dma_mmap,
457 .get_sgtable = arm_dma_get_sgtable,
458 .map_page = dmabounce_map_page,
459 .unmap_page = dmabounce_unmap_page,
460 .sync_single_for_cpu = dmabounce_sync_for_cpu,
461 .sync_single_for_device = dmabounce_sync_for_device,
462 .map_sg = arm_dma_map_sg,
463 .unmap_sg = arm_dma_unmap_sg,
464 .sync_sg_for_cpu = arm_dma_sync_sg_for_cpu,
465 .sync_sg_for_device = arm_dma_sync_sg_for_device,
466 .dma_supported = dmabounce_dma_supported,
469 static int dmabounce_init_pool(struct dmabounce_pool *pool, struct device *dev,
470 const char *name, unsigned long size)
472 pool->size = size;
473 DO_STATS(pool->allocs = 0);
474 pool->pool = dma_pool_create(name, dev, size,
475 0 /* byte alignment */,
476 0 /* no page-crossing issues */);
478 return pool->pool ? 0 : -ENOMEM;
481 int dmabounce_register_dev(struct device *dev, unsigned long small_buffer_size,
482 unsigned long large_buffer_size,
483 int (*needs_bounce_fn)(struct device *, dma_addr_t, size_t))
485 struct dmabounce_device_info *device_info;
486 int ret;
488 device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
489 if (!device_info) {
490 dev_err(dev,
491 "Could not allocated dmabounce_device_info\n");
492 return -ENOMEM;
495 ret = dmabounce_init_pool(&device_info->small, dev,
496 "small_dmabounce_pool", small_buffer_size);
497 if (ret) {
498 dev_err(dev,
499 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
500 small_buffer_size);
501 goto err_free;
504 if (large_buffer_size) {
505 ret = dmabounce_init_pool(&device_info->large, dev,
506 "large_dmabounce_pool",
507 large_buffer_size);
508 if (ret) {
509 dev_err(dev,
510 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
511 large_buffer_size);
512 goto err_destroy;
516 device_info->dev = dev;
517 INIT_LIST_HEAD(&device_info->safe_buffers);
518 rwlock_init(&device_info->lock);
519 device_info->needs_bounce = needs_bounce_fn;
521 #ifdef STATS
522 device_info->total_allocs = 0;
523 device_info->map_op_count = 0;
524 device_info->bounce_count = 0;
525 device_info->attr_res = device_create_file(dev, &dev_attr_dmabounce_stats);
526 #endif
528 dev->archdata.dmabounce = device_info;
529 set_dma_ops(dev, &dmabounce_ops);
531 dev_info(dev, "dmabounce: registered device\n");
533 return 0;
535 err_destroy:
536 dma_pool_destroy(device_info->small.pool);
537 err_free:
538 kfree(device_info);
539 return ret;
541 EXPORT_SYMBOL(dmabounce_register_dev);
543 void dmabounce_unregister_dev(struct device *dev)
545 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
547 dev->archdata.dmabounce = NULL;
548 set_dma_ops(dev, NULL);
550 if (!device_info) {
551 dev_warn(dev,
552 "Never registered with dmabounce but attempting"
553 "to unregister!\n");
554 return;
557 if (!list_empty(&device_info->safe_buffers)) {
558 dev_err(dev,
559 "Removing from dmabounce with pending buffers!\n");
560 BUG();
563 if (device_info->small.pool)
564 dma_pool_destroy(device_info->small.pool);
565 if (device_info->large.pool)
566 dma_pool_destroy(device_info->large.pool);
568 #ifdef STATS
569 if (device_info->attr_res == 0)
570 device_remove_file(dev, &dev_attr_dmabounce_stats);
571 #endif
573 kfree(device_info);
575 dev_info(dev, "dmabounce: device unregistered\n");
577 EXPORT_SYMBOL(dmabounce_unregister_dev);
579 MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
580 MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
581 MODULE_LICENSE("GPL");