Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[pandora-kernel.git] / arch / mips / mm / dma-coherent.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2000  Ani Joshi <ajoshi@unixbox.com>
7  * Copyright (C) 2000, 2001  Ralf Baechle <ralf@gnu.org>
8  * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
9  */
10 #include <linux/config.h>
11 #include <linux/types.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16
17 #include <asm/cache.h>
18 #include <asm/io.h>
19
20 void *dma_alloc_noncoherent(struct device *dev, size_t size,
21         dma_addr_t * dma_handle, gfp_t gfp)
22 {
23         void *ret;
24         /* ignore region specifiers */
25         gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
26
27         if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
28                 gfp |= GFP_DMA;
29         ret = (void *) __get_free_pages(gfp, get_order(size));
30
31         if (ret != NULL) {
32                 memset(ret, 0, size);
33                 *dma_handle = virt_to_phys(ret);
34         }
35
36         return ret;
37 }
38
39 EXPORT_SYMBOL(dma_alloc_noncoherent);
40
41 void *dma_alloc_coherent(struct device *dev, size_t size,
42         dma_addr_t * dma_handle, gfp_t gfp)
43         __attribute__((alias("dma_alloc_noncoherent")));
44
45 EXPORT_SYMBOL(dma_alloc_coherent);
46
47 void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
48         dma_addr_t dma_handle)
49 {
50         unsigned long addr = (unsigned long) vaddr;
51
52         free_pages(addr, get_order(size));
53 }
54
55 EXPORT_SYMBOL(dma_free_noncoherent);
56
57 void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
58         dma_addr_t dma_handle) __attribute__((alias("dma_free_noncoherent")));
59
60 EXPORT_SYMBOL(dma_free_coherent);
61
62 dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
63         enum dma_data_direction direction)
64 {
65         BUG_ON(direction == DMA_NONE);
66
67         return __pa(ptr);
68 }
69
70 EXPORT_SYMBOL(dma_map_single);
71
72 void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
73                  enum dma_data_direction direction)
74 {
75         BUG_ON(direction == DMA_NONE);
76 }
77
78 EXPORT_SYMBOL(dma_unmap_single);
79
80 int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
81         enum dma_data_direction direction)
82 {
83         int i;
84
85         BUG_ON(direction == DMA_NONE);
86
87         for (i = 0; i < nents; i++, sg++) {
88                 sg->dma_address = (dma_addr_t)page_to_phys(sg->page) + sg->offset;
89         }
90
91         return nents;
92 }
93
94 EXPORT_SYMBOL(dma_map_sg);
95
96 dma_addr_t dma_map_page(struct device *dev, struct page *page,
97         unsigned long offset, size_t size, enum dma_data_direction direction)
98 {
99         BUG_ON(direction == DMA_NONE);
100
101         return page_to_phys(page) + offset;
102 }
103
104 EXPORT_SYMBOL(dma_map_page);
105
106 void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
107                enum dma_data_direction direction)
108 {
109         BUG_ON(direction == DMA_NONE);
110 }
111
112 EXPORT_SYMBOL(dma_unmap_page);
113
114 void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
115              enum dma_data_direction direction)
116 {
117         BUG_ON(direction == DMA_NONE);
118 }
119
120 EXPORT_SYMBOL(dma_unmap_sg);
121
122 void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
123         size_t size, enum dma_data_direction direction)
124 {
125         BUG_ON(direction == DMA_NONE);
126 }
127
128 EXPORT_SYMBOL(dma_sync_single_for_cpu);
129
130 void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
131         size_t size, enum dma_data_direction direction)
132 {
133         BUG_ON(direction == DMA_NONE);
134 }
135
136 EXPORT_SYMBOL(dma_sync_single_for_device);
137
138 void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
139                       unsigned long offset, size_t size,
140                       enum dma_data_direction direction)
141 {
142         BUG_ON(direction == DMA_NONE);
143 }
144
145 EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
146
147 void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
148                       unsigned long offset, size_t size,
149                       enum dma_data_direction direction)
150 {
151         BUG_ON(direction == DMA_NONE);
152 }
153
154 EXPORT_SYMBOL(dma_sync_single_range_for_device);
155
156 void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
157                  enum dma_data_direction direction)
158 {
159         BUG_ON(direction == DMA_NONE);
160 }
161
162 EXPORT_SYMBOL(dma_sync_sg_for_cpu);
163
164 void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
165                  enum dma_data_direction direction)
166 {
167         BUG_ON(direction == DMA_NONE);
168 }
169
170 EXPORT_SYMBOL(dma_sync_sg_for_device);
171
172 int dma_mapping_error(dma_addr_t dma_addr)
173 {
174         return 0;
175 }
176
177 EXPORT_SYMBOL(dma_mapping_error);
178
179 int dma_supported(struct device *dev, u64 mask)
180 {
181         /*
182          * we fall back to GFP_DMA when the mask isn't all 1s,
183          * so we can't guarantee allocations that must be
184          * within a tighter range than GFP_DMA..
185          */
186         if (mask < 0x00ffffff)
187                 return 0;
188
189         return 1;
190 }
191
192 EXPORT_SYMBOL(dma_supported);
193
194 int dma_is_consistent(dma_addr_t dma_addr)
195 {
196         return 1;
197 }
198
199 EXPORT_SYMBOL(dma_is_consistent);
200
201 void dma_cache_sync(void *vaddr, size_t size,
202                enum dma_data_direction direction)
203 {
204         BUG_ON(direction == DMA_NONE);
205 }
206
207 EXPORT_SYMBOL(dma_cache_sync);
208
209 /* The DAC routines are a PCIism.. */
210
211 #ifdef CONFIG_PCI
212
213 #include <linux/pci.h>
214
215 dma64_addr_t pci_dac_page_to_dma(struct pci_dev *pdev,
216         struct page *page, unsigned long offset, int direction)
217 {
218         return (dma64_addr_t)page_to_phys(page) + offset;
219 }
220
221 EXPORT_SYMBOL(pci_dac_page_to_dma);
222
223 struct page *pci_dac_dma_to_page(struct pci_dev *pdev,
224         dma64_addr_t dma_addr)
225 {
226         return mem_map + (dma_addr >> PAGE_SHIFT);
227 }
228
229 EXPORT_SYMBOL(pci_dac_dma_to_page);
230
231 unsigned long pci_dac_dma_to_offset(struct pci_dev *pdev,
232         dma64_addr_t dma_addr)
233 {
234         return dma_addr & ~PAGE_MASK;
235 }
236
237 EXPORT_SYMBOL(pci_dac_dma_to_offset);
238
239 void pci_dac_dma_sync_single_for_cpu(struct pci_dev *pdev,
240         dma64_addr_t dma_addr, size_t len, int direction)
241 {
242         BUG_ON(direction == PCI_DMA_NONE);
243 }
244
245 EXPORT_SYMBOL(pci_dac_dma_sync_single_for_cpu);
246
247 void pci_dac_dma_sync_single_for_device(struct pci_dev *pdev,
248         dma64_addr_t dma_addr, size_t len, int direction)
249 {
250         BUG_ON(direction == PCI_DMA_NONE);
251 }
252
253 EXPORT_SYMBOL(pci_dac_dma_sync_single_for_device);
254
255 #endif /* CONFIG_PCI */