blob: 04ddcfeb798140a3ffb465157948395577874ebd [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * Dynamic DMA mapping support
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Copyright 2005-2009 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -07005 *
Robin Getz96f10502009-09-24 14:11:24 +00006 * Licensed under the GPL-2 or later
Bryan Wu1394f032007-05-06 14:50:22 -07007 */
8
9#include <linux/types.h>
Barry Songdd3b0e32009-11-23 03:47:24 +000010#include <linux/gfp.h>
Bryan Wu1394f032007-05-06 14:50:22 -070011#include <linux/string.h>
Bryan Wu1394f032007-05-06 14:50:22 -070012#include <linux/spinlock.h>
Bryan Wu1394f032007-05-06 14:50:22 -070013#include <linux/dma-mapping.h>
Adrian Bunk82861922007-10-24 14:09:25 +020014#include <linux/scatterlist.h>
Bryan Wu1394f032007-05-06 14:50:22 -070015
16static spinlock_t dma_page_lock;
Michael Hennerichcb5ae602009-09-23 11:32:52 +000017static unsigned long *dma_page;
Bryan Wu1394f032007-05-06 14:50:22 -070018static unsigned int dma_pages;
19static unsigned long dma_base;
20static unsigned long dma_size;
21static unsigned int dma_initialized;
22
Barry Songdd3b0e32009-11-23 03:47:24 +000023static void dma_alloc_init(unsigned long start, unsigned long end)
Bryan Wu1394f032007-05-06 14:50:22 -070024{
25 spin_lock_init(&dma_page_lock);
26 dma_initialized = 0;
27
Michael Hennerichcb5ae602009-09-23 11:32:52 +000028 dma_page = (unsigned long *)__get_free_page(GFP_KERNEL);
Bryan Wu1394f032007-05-06 14:50:22 -070029 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 Harrisonb85d8582008-04-23 09:39:01 +080036 printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __func__,
Bryan Wu1394f032007-05-06 14:50:22 -070037 dma_page, dma_pages, dma_base);
38}
39
40static inline unsigned int get_pages(size_t size)
41{
42 return ((size - 1) >> PAGE_SHIFT) + 1;
43}
44
45static 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 Hennerichcb5ae602009-09-23 11:32:52 +000056 if (test_bit(i++, dma_page) == 0) {
Bryan Wu1394f032007-05-06 14:50:22 -070057 if (++count == pages) {
58 while (count--)
Michael Hennerichcb5ae602009-09-23 11:32:52 +000059 __set_bit(--i, dma_page);
60
Bryan Wu1394f032007-05-06 14:50:22 -070061 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
71static 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 Harrisonb85d8582008-04-23 09:39:01 +080078 printk(KERN_ERR "%s: freeing outside range.\n", __func__);
Bryan Wu1394f032007-05-06 14:50:22 -070079 BUG();
80 }
81
82 spin_lock_irqsave(&dma_page_lock, flags);
Michael Hennerichcb5ae602009-09-23 11:32:52 +000083 for (i = page; i < page + pages; i++)
84 __clear_bit(i, dma_page);
85
Bryan Wu1394f032007-05-06 14:50:22 -070086 spin_unlock_irqrestore(&dma_page_lock, flags);
87}
88
89void *dma_alloc_coherent(struct device *dev, size_t size,
Barry Songdd3b0e32009-11-23 03:47:24 +000090 dma_addr_t *dma_handle, gfp_t gfp)
Bryan Wu1394f032007-05-06 14:50:22 -070091{
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}
103EXPORT_SYMBOL(dma_alloc_coherent);
104
105void
106dma_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}
111EXPORT_SYMBOL(dma_free_coherent);
112
113/*
Barry Songdd3b0e32009-11-23 03:47:24 +0000114 * Streaming DMA mappings
Bryan Wu1394f032007-05-06 14:50:22 -0700115 */
Barry Songdd3b0e32009-11-23 03:47:24 +0000116void __dma_sync(dma_addr_t addr, size_t size,
117 enum dma_data_direction dir)
Bryan Wu1394f032007-05-06 14:50:22 -0700118{
Sonic Zhanga3a6a592009-12-16 07:52:52 +0000119 __dma_sync_inline(addr, size, dir);
Bryan Wu1394f032007-05-06 14:50:22 -0700120}
Barry Songdd3b0e32009-11-23 03:47:24 +0000121EXPORT_SYMBOL(__dma_sync);
Bryan Wu1394f032007-05-06 14:50:22 -0700122
123int
124dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
125 enum dma_data_direction direction)
126{
127 int i;
128
Sonic Zhangb07af762007-07-12 12:18:08 +0800129 for (i = 0; i < nents; i++, sg++) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200130 sg->dma_address = (dma_addr_t) sg_virt(sg);
Barry Songdd3b0e32009-11-23 03:47:24 +0000131 __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
Sonic Zhangb07af762007-07-12 12:18:08 +0800132 }
Bryan Wu1394f032007-05-06 14:50:22 -0700133
134 return nents;
135}
136EXPORT_SYMBOL(dma_map_sg);
137
Barry Songdd3b0e32009-11-23 03:47:24 +0000138void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
139 int nelems, enum dma_data_direction direction)
Bryan Wu1394f032007-05-06 14:50:22 -0700140{
Barry Songdd3b0e32009-11-23 03:47:24 +0000141 int i;
Bryan Wu1394f032007-05-06 14:50:22 -0700142
Barry Songdd3b0e32009-11-23 03:47:24 +0000143 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 Wu1394f032007-05-06 14:50:22 -0700147}
Barry Songdd3b0e32009-11-23 03:47:24 +0000148EXPORT_SYMBOL(dma_sync_sg_for_device);