aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips
diff options
context:
space:
mode:
authorDezhong Diao <dediao@cisco.com>2010-10-13 19:57:35 -0400
committerRalf Baechle <ralf@linux-mips.org>2011-07-25 12:26:52 -0400
commite36863a550da44595b155c6b86ff46b50cbff5c0 (patch)
tree62afb6746e304136bddec487a1361de22c48cc21 /arch/mips
parentd0be89f6c2570a63ac44ccdd12473a54243cd296 (diff)
MIPS: HIGHMEM DMA on noncoherent MIPS32 processors
[v4: Patch applies to linux-queue.git with kmap_atomic patches: https://patchwork.kernel.org/patch/189932/ https://patchwork.kernel.org/patch/194552/ https://patchwork.kernel.org/patch/189912/ ] The MIPS DMA coherency functions do not work properly (i.e. kernel oops) when HIGHMEM pages are passed in as arguments. Use kmap_atomic() to temporarily map high pages for cache maintenance operations. Tested on a 2.6.36-rc7 1GB HIGHMEM SMP no-alias system. Signed-off-by: Dezhong Diao <dediao@cisco.com> Signed-off-by: Kevin Cernekee <cernekee@gmail.com> Cc: Dezhong Diao <dediao@cisco.com> Cc: David Daney <ddaney@caviumnetworks.com> Cc: David VomLehn <dvomlehn@cisco.com> Cc: Sergei Shtylyov <sshtylyov@mvista.com> Cc: linux-mips@linux-mips.org Cc: linux-kernel@vger.kernel.org Patchwork: https://patchwork.linux-mips.org/patch/1695/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Diffstat (limited to 'arch/mips')
-rw-r--r--arch/mips/mm/dma-default.c114
1 files changed, 68 insertions, 46 deletions
diff --git a/arch/mips/mm/dma-default.c b/arch/mips/mm/dma-default.c
index 21ea14efb837..46084912e588 100644
--- a/arch/mips/mm/dma-default.c
+++ b/arch/mips/mm/dma-default.c
@@ -15,18 +15,18 @@
15#include <linux/scatterlist.h> 15#include <linux/scatterlist.h>
16#include <linux/string.h> 16#include <linux/string.h>
17#include <linux/gfp.h> 17#include <linux/gfp.h>
18#include <linux/highmem.h>
18 19
19#include <asm/cache.h> 20#include <asm/cache.h>
20#include <asm/io.h> 21#include <asm/io.h>
21 22
22#include <dma-coherence.h> 23#include <dma-coherence.h>
23 24
24static inline unsigned long dma_addr_to_virt(struct device *dev, 25static inline struct page *dma_addr_to_page(struct device *dev,
25 dma_addr_t dma_addr) 26 dma_addr_t dma_addr)
26{ 27{
27 unsigned long addr = plat_dma_addr_to_phys(dev, dma_addr); 28 return pfn_to_page(
28 29 plat_dma_addr_to_phys(dev, dma_addr) >> PAGE_SHIFT);
29 return (unsigned long)phys_to_virt(addr);
30} 30}
31 31
32/* 32/*
@@ -148,20 +148,20 @@ static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
148 free_pages(addr, get_order(size)); 148 free_pages(addr, get_order(size));
149} 149}
150 150
151static inline void __dma_sync(unsigned long addr, size_t size, 151static inline void __dma_sync_virtual(void *addr, size_t size,
152 enum dma_data_direction direction) 152 enum dma_data_direction direction)
153{ 153{
154 switch (direction) { 154 switch (direction) {
155 case DMA_TO_DEVICE: 155 case DMA_TO_DEVICE:
156 dma_cache_wback(addr, size); 156 dma_cache_wback((unsigned long)addr, size);
157 break; 157 break;
158 158
159 case DMA_FROM_DEVICE: 159 case DMA_FROM_DEVICE:
160 dma_cache_inv(addr, size); 160 dma_cache_inv((unsigned long)addr, size);
161 break; 161 break;
162 162
163 case DMA_BIDIRECTIONAL: 163 case DMA_BIDIRECTIONAL:
164 dma_cache_wback_inv(addr, size); 164 dma_cache_wback_inv((unsigned long)addr, size);
165 break; 165 break;
166 166
167 default: 167 default:
@@ -169,12 +169,49 @@ static inline void __dma_sync(unsigned long addr, size_t size,
169 } 169 }
170} 170}
171 171
172/*
173 * A single sg entry may refer to multiple physically contiguous
174 * pages. But we still need to process highmem pages individually.
175 * If highmem is not configured then the bulk of this loop gets
176 * optimized out.
177 */
178static inline void __dma_sync(struct page *page,
179 unsigned long offset, size_t size, enum dma_data_direction direction)
180{
181 size_t left = size;
182
183 do {
184 size_t len = left;
185
186 if (PageHighMem(page)) {
187 void *addr;
188
189 if (offset + len > PAGE_SIZE) {
190 if (offset >= PAGE_SIZE) {
191 page += offset >> PAGE_SHIFT;
192 offset &= ~PAGE_MASK;
193 }
194 len = PAGE_SIZE - offset;
195 }
196
197 addr = kmap_atomic(page);
198 __dma_sync_virtual(addr + offset, len, direction);
199 kunmap_atomic(addr);
200 } else
201 __dma_sync_virtual(page_address(page) + offset,
202 size, direction);
203 offset = 0;
204 page++;
205 left -= len;
206 } while (left);
207}
208
172static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr, 209static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
173 size_t size, enum dma_data_direction direction, struct dma_attrs *attrs) 210 size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
174{ 211{
175 if (cpu_is_noncoherent_r10000(dev)) 212 if (cpu_is_noncoherent_r10000(dev))
176 __dma_sync(dma_addr_to_virt(dev, dma_addr), size, 213 __dma_sync(dma_addr_to_page(dev, dma_addr),
177 direction); 214 dma_addr & ~PAGE_MASK, size, direction);
178 215
179 plat_unmap_dma_mem(dev, dma_addr, size, direction); 216 plat_unmap_dma_mem(dev, dma_addr, size, direction);
180} 217}
@@ -185,13 +222,11 @@ static int mips_dma_map_sg(struct device *dev, struct scatterlist *sg,
185 int i; 222 int i;
186 223
187 for (i = 0; i < nents; i++, sg++) { 224 for (i = 0; i < nents; i++, sg++) {
188 unsigned long addr; 225 if (!plat_device_is_coherent(dev))
189 226 __dma_sync(sg_page(sg), sg->offset, sg->length,
190 addr = (unsigned long) sg_virt(sg); 227 direction);
191 if (!plat_device_is_coherent(dev) && addr) 228 sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
192 __dma_sync(addr, sg->length, direction); 229 sg->offset;
193 sg->dma_address = plat_map_dma_mem(dev,
194 (void *)addr, sg->length);
195 } 230 }
196 231
197 return nents; 232 return nents;
@@ -201,30 +236,23 @@ static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
201 unsigned long offset, size_t size, enum dma_data_direction direction, 236 unsigned long offset, size_t size, enum dma_data_direction direction,
202 struct dma_attrs *attrs) 237 struct dma_attrs *attrs)
203{ 238{
204 unsigned long addr;
205
206 addr = (unsigned long) page_address(page) + offset;
207
208 if (!plat_device_is_coherent(dev)) 239 if (!plat_device_is_coherent(dev))
209 __dma_sync(addr, size, direction); 240 __dma_sync(page, offset, size, direction);
210 241
211 return plat_map_dma_mem(dev, (void *)addr, size); 242 return plat_map_dma_mem_page(dev, page) + offset;
212} 243}
213 244
214static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sg, 245static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
215 int nhwentries, enum dma_data_direction direction, 246 int nhwentries, enum dma_data_direction direction,
216 struct dma_attrs *attrs) 247 struct dma_attrs *attrs)
217{ 248{
218 unsigned long addr;
219 int i; 249 int i;
220 250
221 for (i = 0; i < nhwentries; i++, sg++) { 251 for (i = 0; i < nhwentries; i++, sg++) {
222 if (!plat_device_is_coherent(dev) && 252 if (!plat_device_is_coherent(dev) &&
223 direction != DMA_TO_DEVICE) { 253 direction != DMA_TO_DEVICE)
224 addr = (unsigned long) sg_virt(sg); 254 __dma_sync(sg_page(sg), sg->offset, sg->length,
225 if (addr) 255 direction);
226 __dma_sync(addr, sg->length, direction);
227 }
228 plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction); 256 plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
229 } 257 }
230} 258}
@@ -232,24 +260,18 @@ static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
232static void mips_dma_sync_single_for_cpu(struct device *dev, 260static void mips_dma_sync_single_for_cpu(struct device *dev,
233 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction) 261 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
234{ 262{
235 if (cpu_is_noncoherent_r10000(dev)) { 263 if (cpu_is_noncoherent_r10000(dev))
236 unsigned long addr; 264 __dma_sync(dma_addr_to_page(dev, dma_handle),
237 265 dma_handle & ~PAGE_MASK, size, direction);
238 addr = dma_addr_to_virt(dev, dma_handle);
239 __dma_sync(addr, size, direction);
240 }
241} 266}
242 267
243static void mips_dma_sync_single_for_device(struct device *dev, 268static void mips_dma_sync_single_for_device(struct device *dev,
244 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction) 269 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
245{ 270{
246 plat_extra_sync_for_device(dev); 271 plat_extra_sync_for_device(dev);
247 if (!plat_device_is_coherent(dev)) { 272 if (!plat_device_is_coherent(dev))
248 unsigned long addr; 273 __dma_sync(dma_addr_to_page(dev, dma_handle),
249 274 dma_handle & ~PAGE_MASK, size, direction);
250 addr = dma_addr_to_virt(dev, dma_handle);
251 __dma_sync(addr, size, direction);
252 }
253} 275}
254 276
255static void mips_dma_sync_sg_for_cpu(struct device *dev, 277static void mips_dma_sync_sg_for_cpu(struct device *dev,
@@ -260,8 +282,8 @@ static void mips_dma_sync_sg_for_cpu(struct device *dev,
260 /* Make sure that gcc doesn't leave the empty loop body. */ 282 /* Make sure that gcc doesn't leave the empty loop body. */
261 for (i = 0; i < nelems; i++, sg++) { 283 for (i = 0; i < nelems; i++, sg++) {
262 if (cpu_is_noncoherent_r10000(dev)) 284 if (cpu_is_noncoherent_r10000(dev))
263 __dma_sync((unsigned long)page_address(sg_page(sg)), 285 __dma_sync(sg_page(sg), sg->offset, sg->length,
264 sg->length, direction); 286 direction);
265 } 287 }
266} 288}
267 289
@@ -273,8 +295,8 @@ static void mips_dma_sync_sg_for_device(struct device *dev,
273 /* Make sure that gcc doesn't leave the empty loop body. */ 295 /* Make sure that gcc doesn't leave the empty loop body. */
274 for (i = 0; i < nelems; i++, sg++) { 296 for (i = 0; i < nelems; i++, sg++) {
275 if (!plat_device_is_coherent(dev)) 297 if (!plat_device_is_coherent(dev))
276 __dma_sync((unsigned long)page_address(sg_page(sg)), 298 __dma_sync(sg_page(sg), sg->offset, sg->length,
277 sg->length, direction); 299 direction);
278 } 300 }
279} 301}
280 302
@@ -295,7 +317,7 @@ void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
295 317
296 plat_extra_sync_for_device(dev); 318 plat_extra_sync_for_device(dev);
297 if (!plat_device_is_coherent(dev)) 319 if (!plat_device_is_coherent(dev))
298 __dma_sync((unsigned long)vaddr, size, direction); 320 __dma_sync_virtual(vaddr, size, direction);
299} 321}
300 322
301EXPORT_SYMBOL(dma_cache_sync); 323EXPORT_SYMBOL(dma_cache_sync);