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> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 15 | |
| 16 | static spinlock_t dma_page_lock; |
Michael Hennerich | cb5ae60 | 2009-09-23 11:32:52 +0000 | [diff] [blame] | 17 | static unsigned long *dma_page; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 18 | static unsigned int dma_pages; |
| 19 | static unsigned long dma_base; |
| 20 | static unsigned long dma_size; |
| 21 | static unsigned int dma_initialized; |
| 22 | |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 23 | static void dma_alloc_init(unsigned long start, unsigned long end) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 24 | { |
| 25 | spin_lock_init(&dma_page_lock); |
| 26 | dma_initialized = 0; |
| 27 | |
Michael Hennerich | cb5ae60 | 2009-09-23 11:32:52 +0000 | [diff] [blame] | 28 | dma_page = (unsigned long *)__get_free_page(GFP_KERNEL); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 29 | memset(dma_page, 0, PAGE_SIZE); |
| 30 | dma_base = PAGE_ALIGN(start); |
| 31 | dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start); |
| 32 | dma_pages = dma_size >> PAGE_SHIFT; |
| 33 | memset((void *)dma_base, 0, DMA_UNCACHED_REGION); |
| 34 | dma_initialized = 1; |
| 35 | |
Harvey Harrison | b85d858 | 2008-04-23 09:39:01 +0800 | [diff] [blame] | 36 | 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] | 37 | dma_page, dma_pages, dma_base); |
| 38 | } |
| 39 | |
| 40 | static inline unsigned int get_pages(size_t size) |
| 41 | { |
| 42 | return ((size - 1) >> PAGE_SHIFT) + 1; |
| 43 | } |
| 44 | |
| 45 | static unsigned long __alloc_dma_pages(unsigned int pages) |
| 46 | { |
| 47 | unsigned long ret = 0, flags; |
| 48 | int i, count = 0; |
| 49 | |
| 50 | if (dma_initialized == 0) |
| 51 | dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend); |
| 52 | |
| 53 | spin_lock_irqsave(&dma_page_lock, flags); |
| 54 | |
| 55 | for (i = 0; i < dma_pages;) { |
Michael Hennerich | cb5ae60 | 2009-09-23 11:32:52 +0000 | [diff] [blame] | 56 | if (test_bit(i++, dma_page) == 0) { |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 57 | if (++count == pages) { |
| 58 | while (count--) |
Michael Hennerich | cb5ae60 | 2009-09-23 11:32:52 +0000 | [diff] [blame] | 59 | __set_bit(--i, dma_page); |
| 60 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 61 | ret = dma_base + (i << PAGE_SHIFT); |
| 62 | break; |
| 63 | } |
| 64 | } else |
| 65 | count = 0; |
| 66 | } |
| 67 | spin_unlock_irqrestore(&dma_page_lock, flags); |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | static void __free_dma_pages(unsigned long addr, unsigned int pages) |
| 72 | { |
| 73 | unsigned long page = (addr - dma_base) >> PAGE_SHIFT; |
| 74 | unsigned long flags; |
| 75 | int i; |
| 76 | |
| 77 | if ((page + pages) > dma_pages) { |
Harvey Harrison | b85d858 | 2008-04-23 09:39:01 +0800 | [diff] [blame] | 78 | printk(KERN_ERR "%s: freeing outside range.\n", __func__); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 79 | BUG(); |
| 80 | } |
| 81 | |
| 82 | spin_lock_irqsave(&dma_page_lock, flags); |
Michael Hennerich | cb5ae60 | 2009-09-23 11:32:52 +0000 | [diff] [blame] | 83 | for (i = page; i < page + pages; i++) |
| 84 | __clear_bit(i, dma_page); |
| 85 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 86 | spin_unlock_irqrestore(&dma_page_lock, flags); |
| 87 | } |
| 88 | |
| 89 | void *dma_alloc_coherent(struct device *dev, size_t size, |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 90 | dma_addr_t *dma_handle, gfp_t gfp) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 91 | { |
| 92 | void *ret; |
| 93 | |
| 94 | ret = (void *)__alloc_dma_pages(get_pages(size)); |
| 95 | |
| 96 | if (ret) { |
| 97 | memset(ret, 0, size); |
| 98 | *dma_handle = virt_to_phys(ret); |
| 99 | } |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | EXPORT_SYMBOL(dma_alloc_coherent); |
| 104 | |
| 105 | void |
| 106 | dma_free_coherent(struct device *dev, size_t size, void *vaddr, |
| 107 | dma_addr_t dma_handle) |
| 108 | { |
| 109 | __free_dma_pages((unsigned long)vaddr, get_pages(size)); |
| 110 | } |
| 111 | EXPORT_SYMBOL(dma_free_coherent); |
| 112 | |
| 113 | /* |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 114 | * Streaming DMA mappings |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 115 | */ |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 116 | void __dma_sync(dma_addr_t addr, size_t size, |
| 117 | enum dma_data_direction dir) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 118 | { |
Sonic Zhang | a3a6a59 | 2009-12-16 07:52:52 +0000 | [diff] [blame] | 119 | __dma_sync_inline(addr, size, dir); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 120 | } |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 121 | EXPORT_SYMBOL(__dma_sync); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 122 | |
| 123 | int |
| 124 | dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 125 | enum dma_data_direction direction) |
| 126 | { |
| 127 | int i; |
| 128 | |
Sonic Zhang | b07af76 | 2007-07-12 12:18:08 +0800 | [diff] [blame] | 129 | for (i = 0; i < nents; i++, sg++) { |
Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 130 | sg->dma_address = (dma_addr_t) sg_virt(sg); |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 131 | __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction); |
Sonic Zhang | b07af76 | 2007-07-12 12:18:08 +0800 | [diff] [blame] | 132 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 133 | |
| 134 | return nents; |
| 135 | } |
| 136 | EXPORT_SYMBOL(dma_map_sg); |
| 137 | |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 138 | void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
| 139 | int nelems, enum dma_data_direction direction) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 140 | { |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 141 | int i; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 142 | |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 143 | for (i = 0; i < nelems; i++, sg++) { |
| 144 | sg->dma_address = (dma_addr_t) sg_virt(sg); |
| 145 | __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction); |
| 146 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 147 | } |
Barry Song | dd3b0e3 | 2009-11-23 03:47:24 +0000 | [diff] [blame] | 148 | EXPORT_SYMBOL(dma_sync_sg_for_device); |