Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / arm / common / dmabounce.c
blob7996c04393d501a35085b5338971d3f5744a1528
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-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>
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 device *dev;
70 struct list_head safe_buffers;
71 #ifdef STATS
72 unsigned long total_allocs;
73 unsigned long map_op_count;
74 unsigned long bounce_count;
75 int attr_res;
76 #endif
77 struct dmabounce_pool small;
78 struct dmabounce_pool large;
80 rwlock_t lock;
82 int (*needs_bounce)(struct device *, dma_addr_t, size_t);
85 #ifdef STATS
86 static ssize_t dmabounce_show(struct device *dev, struct device_attribute *attr,
87 char *buf)
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);
101 #endif
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;
112 unsigned long flags;
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;
121 } else {
122 pool = NULL;
125 buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC);
126 if (buf == NULL) {
127 dev_warn(dev, "%s: kmalloc failed\n", __func__);
128 return NULL;
131 buf->ptr = ptr;
132 buf->size = size;
133 buf->direction = dir;
134 buf->pool = pool;
136 if (pool) {
137 buf->safe = dma_pool_alloc(pool->pool, GFP_ATOMIC,
138 &buf->safe_dma_addr);
139 } else {
140 buf->safe = dma_alloc_coherent(dev, size, &buf->safe_dma_addr,
141 GFP_ATOMIC);
144 if (buf->safe == NULL) {
145 dev_warn(dev,
146 "%s: could not alloc dma memory (size=%d)\n",
147 __func__, size);
148 kfree(buf);
149 return NULL;
152 #ifdef STATS
153 if (pool)
154 pool->allocs++;
155 device_info->total_allocs++;
156 #endif
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);
162 return buf;
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;
170 unsigned long flags;
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) {
177 rb = b;
178 break;
181 read_unlock_irqrestore(&device_info->lock, flags);
182 return rb;
185 static inline void
186 free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf)
188 unsigned long flags;
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);
198 if (buf->pool)
199 dma_pool_free(buf->pool->pool, buf->safe, buf->safe_dma_addr);
200 else
201 dma_free_coherent(device_info->dev, buf->size, buf->safe,
202 buf->safe_dma_addr);
204 kfree(buf);
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)
213 return NULL;
214 if (dma_mapping_error(dev, dma_addr)) {
215 dev_err(dev, "Trying to %s invalid mapping\n", where);
216 return NULL;
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)
224 return 0;
226 if (dev->dma_mask) {
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);
233 return -E2BIG;
236 /* Figure out if we need to bounce from the DMA mask. */
237 if ((dma_addr | (dma_addr + size - 1)) & ~mask)
238 return 1;
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,
246 unsigned long attrs)
248 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
249 struct safe_buffer *buf;
251 if (device_info)
252 DO_STATS ( device_info->map_op_count++ );
254 buf = alloc_safe_buffer(device_info, ptr, size, dir);
255 if (buf == NULL) {
256 dev_err(dev, "%s: unable to map unsafe buffer %p!\n",
257 __func__, ptr);
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,
277 unsigned long attrs)
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,
316 unsigned long attrs)
318 dma_addr_t dma_addr;
319 int ret;
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);
327 if (ret < 0)
328 return DMA_MAPPING_ERROR;
330 if (ret == 0) {
331 arm_dma_ops.sync_single_for_device(dev, dma_addr, size, dir);
332 return dma_addr;
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
347 * should be)
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__);
358 if (!buf) {
359 arm_dma_ops.sync_single_for_cpu(dev, dma_addr, size, dir);
360 return;
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;
370 unsigned long off;
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__);
376 if (!buf)
377 return 1;
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);
394 return 0;
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))
401 return;
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;
410 unsigned long off;
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__);
416 if (!buf)
417 return 1;
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);
434 return 0;
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))
441 return;
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)
449 return 0;
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)
473 pool->size = 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;
487 int ret;
489 device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
490 if (!device_info) {
491 dev_err(dev,
492 "Could not allocated dmabounce_device_info\n");
493 return -ENOMEM;
496 ret = dmabounce_init_pool(&device_info->small, dev,
497 "small_dmabounce_pool", small_buffer_size);
498 if (ret) {
499 dev_err(dev,
500 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
501 small_buffer_size);
502 goto err_free;
505 if (large_buffer_size) {
506 ret = dmabounce_init_pool(&device_info->large, dev,
507 "large_dmabounce_pool",
508 large_buffer_size);
509 if (ret) {
510 dev_err(dev,
511 "dmabounce: could not allocate DMA pool for %ld byte objects\n",
512 large_buffer_size);
513 goto err_destroy;
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;
522 #ifdef STATS
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);
527 #endif
529 dev->archdata.dmabounce = device_info;
530 set_dma_ops(dev, &dmabounce_ops);
532 dev_info(dev, "dmabounce: registered device\n");
534 return 0;
536 err_destroy:
537 dma_pool_destroy(device_info->small.pool);
538 err_free:
539 kfree(device_info);
540 return ret;
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);
551 if (!device_info) {
552 dev_warn(dev,
553 "Never registered with dmabounce but attempting"
554 "to unregister!\n");
555 return;
558 if (!list_empty(&device_info->safe_buffers)) {
559 dev_err(dev,
560 "Removing from dmabounce with pending buffers!\n");
561 BUG();
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);
569 #ifdef STATS
570 if (device_info->attr_res == 0)
571 device_remove_file(dev, &dev_attr_dmabounce_stats);
572 #endif
574 kfree(device_info);
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");