Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 2 | * Dynamic DMA mapping support |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 3 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 4 | * Copyright 2005-2009 Analog Devices Inc. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 5 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 6 | * Licensed under the GPL-2 or later |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 10 | #include <linux/gfp.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 11 | #include <linux/string.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
Adrian Bunk | 8286192 | 2007-10-24 14:09:25 +0200 | [diff] [blame] | 14 | #include <linux/scatterlist.h> |
Paul Gortmaker | 8dc7a9c | 2011-08-09 11:05:22 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Akinobu Mita | c73bc70 | 2012-11-25 16:48:23 +0900 | [diff] [blame] | 16 | #include <linux/bitmap.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 17 | |
| 18 | static spinlock_t dma_page_lock; |
Michael Hennerich | cb5ae60 | 2009-09-23 11:32:52 +0000 | [diff] [blame] | 19 | static unsigned long *dma_page; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 20 | static unsigned int dma_pages; |
| 21 | static unsigned long dma_base; |
| 22 | static unsigned long dma_size; |
| 23 | static unsigned int dma_initialized; |
| 24 | |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 25 | static void dma_alloc_init(unsigned long start, unsigned long end) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 26 | { |
| 27 | spin_lock_init(&dma_page_lock); |
| 28 | dma_initialized = 0; |
| 29 | |
Michael Hennerich | cb5ae60 | 2009-09-23 11:32:52 +0000 | [diff] [blame] | 30 | dma_page = (unsigned long *)__get_free_page(GFP_KERNEL); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 31 | memset(dma_page, 0, PAGE_SIZE); |
| 32 | dma_base = PAGE_ALIGN(start); |
| 33 | dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start); |
| 34 | dma_pages = dma_size >> PAGE_SHIFT; |
| 35 | memset((void *)dma_base, 0, DMA_UNCACHED_REGION); |
| 36 | dma_initialized = 1; |
| 37 | |
Harvey Harrison | b85d858 | 2008-04-23 09:39:01 +0800 | [diff] [blame] | 38 | printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __func__, |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 39 | dma_page, dma_pages, dma_base); |
| 40 | } |
| 41 | |
| 42 | static inline unsigned int get_pages(size_t size) |
| 43 | { |
| 44 | return ((size - 1) >> PAGE_SHIFT) + 1; |
| 45 | } |
| 46 | |
| 47 | static unsigned long __alloc_dma_pages(unsigned int pages) |
| 48 | { |
| 49 | unsigned long ret = 0, flags; |
Akinobu Mita | c73bc70 | 2012-11-25 16:48:23 +0900 | [diff] [blame] | 50 | unsigned long start; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 51 | |
| 52 | if (dma_initialized == 0) |
| 53 | dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend); |
| 54 | |
| 55 | spin_lock_irqsave(&dma_page_lock, flags); |
| 56 | |
Akinobu Mita | c73bc70 | 2012-11-25 16:48:23 +0900 | [diff] [blame] | 57 | start = bitmap_find_next_zero_area(dma_page, dma_pages, 0, pages, 0); |
| 58 | if (start < dma_pages) { |
| 59 | ret = dma_base + (start << PAGE_SHIFT); |
| 60 | bitmap_set(dma_page, start, pages); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 61 | } |
| 62 | spin_unlock_irqrestore(&dma_page_lock, flags); |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | static void __free_dma_pages(unsigned long addr, unsigned int pages) |
| 67 | { |
| 68 | unsigned long page = (addr - dma_base) >> PAGE_SHIFT; |
| 69 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 70 | |
| 71 | if ((page + pages) > dma_pages) { |
Harvey Harrison | b85d858 | 2008-04-23 09:39:01 +0800 | [diff] [blame] | 72 | printk(KERN_ERR "%s: freeing outside range.\n", __func__); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 73 | BUG(); |
| 74 | } |
| 75 | |
| 76 | spin_lock_irqsave(&dma_page_lock, flags); |
Akinobu Mita | c73bc70 | 2012-11-25 16:48:23 +0900 | [diff] [blame] | 77 | bitmap_clear(dma_page, page, pages); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 78 | spin_unlock_irqrestore(&dma_page_lock, flags); |
| 79 | } |
| 80 | |
| 81 | void *dma_alloc_coherent(struct device *dev, size_t size, |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 82 | dma_addr_t *dma_handle, gfp_t gfp) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 83 | { |
| 84 | void *ret; |
| 85 | |
| 86 | ret = (void *)__alloc_dma_pages(get_pages(size)); |
| 87 | |
| 88 | if (ret) { |
| 89 | memset(ret, 0, size); |
| 90 | *dma_handle = virt_to_phys(ret); |
| 91 | } |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | EXPORT_SYMBOL(dma_alloc_coherent); |
| 96 | |
| 97 | void |
| 98 | dma_free_coherent(struct device *dev, size_t size, void *vaddr, |
| 99 | dma_addr_t dma_handle) |
| 100 | { |
| 101 | __free_dma_pages((unsigned long)vaddr, get_pages(size)); |
| 102 | } |
| 103 | EXPORT_SYMBOL(dma_free_coherent); |
| 104 | |
| 105 | /* |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 106 | * Streaming DMA mappings |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 107 | */ |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 108 | void __dma_sync(dma_addr_t addr, size_t size, |
| 109 | enum dma_data_direction dir) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 110 | { |
Sonic Zhang | a3a6a59 | 2009-12-16 07:52:52 +0000 | [diff] [blame] | 111 | __dma_sync_inline(addr, size, dir); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 112 | } |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 113 | EXPORT_SYMBOL(__dma_sync); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 114 | |
| 115 | int |
Sonic Zhang | 86d688a | 2012-05-22 18:23:40 +0800 | [diff] [blame] | 116 | dma_map_sg(struct device *dev, struct scatterlist *sg_list, int nents, |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 117 | enum dma_data_direction direction) |
| 118 | { |
Sonic Zhang | 86d688a | 2012-05-22 18:23:40 +0800 | [diff] [blame] | 119 | struct scatterlist *sg; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 120 | int i; |
| 121 | |
Sonic Zhang | 86d688a | 2012-05-22 18:23:40 +0800 | [diff] [blame] | 122 | for_each_sg(sg_list, sg, nents, i) { |
Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 123 | sg->dma_address = (dma_addr_t) sg_virt(sg); |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 124 | __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction); |
Sonic Zhang | b07af76 | 2007-07-12 12:18:08 +0800 | [diff] [blame] | 125 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 126 | |
| 127 | return nents; |
| 128 | } |
| 129 | EXPORT_SYMBOL(dma_map_sg); |
| 130 | |
Sonic Zhang | 86d688a | 2012-05-22 18:23:40 +0800 | [diff] [blame] | 131 | void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg_list, |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 132 | int nelems, enum dma_data_direction direction) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 133 | { |
Sonic Zhang | 86d688a | 2012-05-22 18:23:40 +0800 | [diff] [blame] | 134 | struct scatterlist *sg; |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 135 | int i; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 136 | |
Sonic Zhang | 86d688a | 2012-05-22 18:23:40 +0800 | [diff] [blame] | 137 | for_each_sg(sg_list, sg, nelems, i) { |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 138 | sg->dma_address = (dma_addr_t) sg_virt(sg); |
| 139 | __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction); |
| 140 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 141 | } |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 142 | EXPORT_SYMBOL(dma_sync_sg_for_device); |