x86/unwinder: Handle stack overflows more gracefully
[linux/fpc-iii.git] / arch / mips / mm / dma-default.c
blobc01bd20d020810901b9712dc652f283d37d976b3
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.
6 * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
7 * Copyright (C) 2000, 2001, 06 Ralf Baechle <ralf@linux-mips.org>
8 * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
9 */
11 #include <linux/types.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/mm.h>
14 #include <linux/export.h>
15 #include <linux/scatterlist.h>
16 #include <linux/string.h>
17 #include <linux/gfp.h>
18 #include <linux/highmem.h>
19 #include <linux/dma-contiguous.h>
21 #include <asm/cache.h>
22 #include <asm/cpu-type.h>
23 #include <asm/io.h>
25 #include <dma-coherence.h>
27 #if defined(CONFIG_DMA_MAYBE_COHERENT) && !defined(CONFIG_DMA_PERDEV_COHERENT)
28 /* User defined DMA coherency from command line. */
29 enum coherent_io_user_state coherentio = IO_COHERENCE_DEFAULT;
30 EXPORT_SYMBOL_GPL(coherentio);
31 int hw_coherentio = 0; /* Actual hardware supported DMA coherency setting. */
33 static int __init setcoherentio(char *str)
35 coherentio = IO_COHERENCE_ENABLED;
36 pr_info("Hardware DMA cache coherency (command line)\n");
37 return 0;
39 early_param("coherentio", setcoherentio);
41 static int __init setnocoherentio(char *str)
43 coherentio = IO_COHERENCE_DISABLED;
44 pr_info("Software DMA cache coherency (command line)\n");
45 return 0;
47 early_param("nocoherentio", setnocoherentio);
48 #endif
50 static inline struct page *dma_addr_to_page(struct device *dev,
51 dma_addr_t dma_addr)
53 return pfn_to_page(
54 plat_dma_addr_to_phys(dev, dma_addr) >> PAGE_SHIFT);
58 * The affected CPUs below in 'cpu_needs_post_dma_flush()' can
59 * speculatively fill random cachelines with stale data at any time,
60 * requiring an extra flush post-DMA.
62 * Warning on the terminology - Linux calls an uncached area coherent;
63 * MIPS terminology calls memory areas with hardware maintained coherency
64 * coherent.
66 * Note that the R14000 and R16000 should also be checked for in this
67 * condition. However this function is only called on non-I/O-coherent
68 * systems and only the R10000 and R12000 are used in such systems, the
69 * SGI IP28 Indigo² rsp. SGI IP32 aka O2.
71 static inline bool cpu_needs_post_dma_flush(struct device *dev)
73 if (plat_device_is_coherent(dev))
74 return false;
76 switch (boot_cpu_type()) {
77 case CPU_R10000:
78 case CPU_R12000:
79 case CPU_BMIPS5000:
80 return true;
82 default:
84 * Presence of MAARs suggests that the CPU supports
85 * speculatively prefetching data, and therefore requires
86 * the post-DMA flush/invalidate.
88 return cpu_has_maar;
92 static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
94 gfp_t dma_flag;
96 /* ignore region specifiers */
97 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
99 #ifdef CONFIG_ISA
100 if (dev == NULL)
101 dma_flag = __GFP_DMA;
102 else
103 #endif
104 #if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA)
105 if (dev == NULL || dev->coherent_dma_mask < DMA_BIT_MASK(32))
106 dma_flag = __GFP_DMA;
107 else if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
108 dma_flag = __GFP_DMA32;
109 else
110 #endif
111 #if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA)
112 if (dev == NULL || dev->coherent_dma_mask < DMA_BIT_MASK(64))
113 dma_flag = __GFP_DMA32;
114 else
115 #endif
116 #if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32)
117 if (dev == NULL ||
118 dev->coherent_dma_mask < DMA_BIT_MASK(sizeof(phys_addr_t) * 8))
119 dma_flag = __GFP_DMA;
120 else
121 #endif
122 dma_flag = 0;
124 /* Don't invoke OOM killer */
125 gfp |= __GFP_NORETRY;
127 return gfp | dma_flag;
130 static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
131 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
133 void *ret;
134 struct page *page = NULL;
135 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
137 gfp = massage_gfp_flags(dev, gfp);
139 if (IS_ENABLED(CONFIG_DMA_CMA) && gfpflags_allow_blocking(gfp))
140 page = dma_alloc_from_contiguous(dev, count, get_order(size),
141 gfp);
142 if (!page)
143 page = alloc_pages(gfp, get_order(size));
145 if (!page)
146 return NULL;
148 ret = page_address(page);
149 memset(ret, 0, size);
150 *dma_handle = plat_map_dma_mem(dev, ret, size);
151 if (!(attrs & DMA_ATTR_NON_CONSISTENT) &&
152 !plat_device_is_coherent(dev)) {
153 dma_cache_wback_inv((unsigned long) ret, size);
154 ret = UNCAC_ADDR(ret);
157 return ret;
160 static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
161 dma_addr_t dma_handle, unsigned long attrs)
163 unsigned long addr = (unsigned long) vaddr;
164 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
165 struct page *page = NULL;
167 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
169 if (!(attrs & DMA_ATTR_NON_CONSISTENT) && !plat_device_is_coherent(dev))
170 addr = CAC_ADDR(addr);
172 page = virt_to_page((void *) addr);
174 if (!dma_release_from_contiguous(dev, page, count))
175 __free_pages(page, get_order(size));
178 static int mips_dma_mmap(struct device *dev, struct vm_area_struct *vma,
179 void *cpu_addr, dma_addr_t dma_addr, size_t size,
180 unsigned long attrs)
182 unsigned long user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
183 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
184 unsigned long addr = (unsigned long)cpu_addr;
185 unsigned long off = vma->vm_pgoff;
186 unsigned long pfn;
187 int ret = -ENXIO;
189 if (!plat_device_is_coherent(dev))
190 addr = CAC_ADDR(addr);
192 pfn = page_to_pfn(virt_to_page((void *)addr));
194 if (attrs & DMA_ATTR_WRITE_COMBINE)
195 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
196 else
197 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
199 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
200 return ret;
202 if (off < count && user_count <= (count - off)) {
203 ret = remap_pfn_range(vma, vma->vm_start,
204 pfn + off,
205 user_count << PAGE_SHIFT,
206 vma->vm_page_prot);
209 return ret;
212 static inline void __dma_sync_virtual(void *addr, size_t size,
213 enum dma_data_direction direction)
215 switch (direction) {
216 case DMA_TO_DEVICE:
217 dma_cache_wback((unsigned long)addr, size);
218 break;
220 case DMA_FROM_DEVICE:
221 dma_cache_inv((unsigned long)addr, size);
222 break;
224 case DMA_BIDIRECTIONAL:
225 dma_cache_wback_inv((unsigned long)addr, size);
226 break;
228 default:
229 BUG();
234 * A single sg entry may refer to multiple physically contiguous
235 * pages. But we still need to process highmem pages individually.
236 * If highmem is not configured then the bulk of this loop gets
237 * optimized out.
239 static inline void __dma_sync(struct page *page,
240 unsigned long offset, size_t size, enum dma_data_direction direction)
242 size_t left = size;
244 do {
245 size_t len = left;
247 if (PageHighMem(page)) {
248 void *addr;
250 if (offset + len > PAGE_SIZE) {
251 if (offset >= PAGE_SIZE) {
252 page += offset >> PAGE_SHIFT;
253 offset &= ~PAGE_MASK;
255 len = PAGE_SIZE - offset;
258 addr = kmap_atomic(page);
259 __dma_sync_virtual(addr + offset, len, direction);
260 kunmap_atomic(addr);
261 } else
262 __dma_sync_virtual(page_address(page) + offset,
263 size, direction);
264 offset = 0;
265 page++;
266 left -= len;
267 } while (left);
270 static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
271 size_t size, enum dma_data_direction direction, unsigned long attrs)
273 if (cpu_needs_post_dma_flush(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
274 __dma_sync(dma_addr_to_page(dev, dma_addr),
275 dma_addr & ~PAGE_MASK, size, direction);
276 plat_post_dma_flush(dev);
277 plat_unmap_dma_mem(dev, dma_addr, size, direction);
280 static int mips_dma_map_sg(struct device *dev, struct scatterlist *sglist,
281 int nents, enum dma_data_direction direction, unsigned long attrs)
283 int i;
284 struct scatterlist *sg;
286 for_each_sg(sglist, sg, nents, i) {
287 if (!plat_device_is_coherent(dev) &&
288 !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
289 __dma_sync(sg_page(sg), sg->offset, sg->length,
290 direction);
291 #ifdef CONFIG_NEED_SG_DMA_LENGTH
292 sg->dma_length = sg->length;
293 #endif
294 sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
295 sg->offset;
298 return nents;
301 static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
302 unsigned long offset, size_t size, enum dma_data_direction direction,
303 unsigned long attrs)
305 if (!plat_device_is_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
306 __dma_sync(page, offset, size, direction);
308 return plat_map_dma_mem_page(dev, page) + offset;
311 static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
312 int nhwentries, enum dma_data_direction direction,
313 unsigned long attrs)
315 int i;
316 struct scatterlist *sg;
318 for_each_sg(sglist, sg, nhwentries, i) {
319 if (!plat_device_is_coherent(dev) &&
320 !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
321 direction != DMA_TO_DEVICE)
322 __dma_sync(sg_page(sg), sg->offset, sg->length,
323 direction);
324 plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
328 static void mips_dma_sync_single_for_cpu(struct device *dev,
329 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
331 if (cpu_needs_post_dma_flush(dev))
332 __dma_sync(dma_addr_to_page(dev, dma_handle),
333 dma_handle & ~PAGE_MASK, size, direction);
334 plat_post_dma_flush(dev);
337 static void mips_dma_sync_single_for_device(struct device *dev,
338 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
340 if (!plat_device_is_coherent(dev))
341 __dma_sync(dma_addr_to_page(dev, dma_handle),
342 dma_handle & ~PAGE_MASK, size, direction);
345 static void mips_dma_sync_sg_for_cpu(struct device *dev,
346 struct scatterlist *sglist, int nelems,
347 enum dma_data_direction direction)
349 int i;
350 struct scatterlist *sg;
352 if (cpu_needs_post_dma_flush(dev)) {
353 for_each_sg(sglist, sg, nelems, i) {
354 __dma_sync(sg_page(sg), sg->offset, sg->length,
355 direction);
358 plat_post_dma_flush(dev);
361 static void mips_dma_sync_sg_for_device(struct device *dev,
362 struct scatterlist *sglist, int nelems,
363 enum dma_data_direction direction)
365 int i;
366 struct scatterlist *sg;
368 if (!plat_device_is_coherent(dev)) {
369 for_each_sg(sglist, sg, nelems, i) {
370 __dma_sync(sg_page(sg), sg->offset, sg->length,
371 direction);
376 static int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
378 return 0;
381 static int mips_dma_supported(struct device *dev, u64 mask)
383 return plat_dma_supported(dev, mask);
386 void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
387 enum dma_data_direction direction)
389 BUG_ON(direction == DMA_NONE);
391 if (!plat_device_is_coherent(dev))
392 __dma_sync_virtual(vaddr, size, direction);
395 EXPORT_SYMBOL(dma_cache_sync);
397 static const struct dma_map_ops mips_default_dma_map_ops = {
398 .alloc = mips_dma_alloc_coherent,
399 .free = mips_dma_free_coherent,
400 .mmap = mips_dma_mmap,
401 .map_page = mips_dma_map_page,
402 .unmap_page = mips_dma_unmap_page,
403 .map_sg = mips_dma_map_sg,
404 .unmap_sg = mips_dma_unmap_sg,
405 .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
406 .sync_single_for_device = mips_dma_sync_single_for_device,
407 .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
408 .sync_sg_for_device = mips_dma_sync_sg_for_device,
409 .mapping_error = mips_dma_mapping_error,
410 .dma_supported = mips_dma_supported
413 const struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
414 EXPORT_SYMBOL(mips_dma_map_ops);
416 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
418 static int __init mips_dma_init(void)
420 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
422 return 0;
424 fs_initcall(mips_dma_init);