blob: 8e78251eccc25e0a981ede0b89d1df907f9e896a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Ralf Baechle70342282013-01-22 12:59:30 +01007 * Copyright (C) 2000, 2001, 06 Ralf Baechle <ralf@linux-mips.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
9 */
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000012#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
Paul Gortmakerd9ba5772016-08-21 15:58:14 -040014#include <linux/export.h>
Jens Axboe4fcc47a2007-10-23 12:32:34 +020015#include <linux/scatterlist.h>
Ralf Baechle6e86b0b2007-10-29 19:35:33 +000016#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Dezhong Diaoe36863a2010-10-13 16:57:35 -070018#include <linux/highmem.h>
Zubair Lutfullah Kakakhelf4649382014-07-16 16:51:32 +010019#include <linux/dma-contiguous.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/cache.h>
Ralf Baechle69f24d12013-09-17 10:25:47 +020022#include <asm/cpu-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/io.h>
24
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000025#include <dma-coherence.h>
26
Paul Burton20d33062016-10-05 18:18:16 +010027#if defined(CONFIG_DMA_MAYBE_COHERENT) && !defined(CONFIG_DMA_PERDEV_COHERENT)
Paul Burtonf2302022016-10-05 18:18:14 +010028/* User defined DMA coherency from command line. */
29enum coherent_io_user_state coherentio = IO_COHERENCE_DEFAULT;
Steven J. Hillb6d92b42013-03-25 13:47:29 -050030EXPORT_SYMBOL_GPL(coherentio);
31int hw_coherentio = 0; /* Actual hardware supported DMA coherency setting. */
32
33static int __init setcoherentio(char *str)
34{
Paul Burtonf2302022016-10-05 18:18:14 +010035 coherentio = IO_COHERENCE_ENABLED;
Steven J. Hillb6d92b42013-03-25 13:47:29 -050036 pr_info("Hardware DMA cache coherency (command line)\n");
37 return 0;
38}
39early_param("coherentio", setcoherentio);
40
41static int __init setnocoherentio(char *str)
42{
Paul Burtonf2302022016-10-05 18:18:14 +010043 coherentio = IO_COHERENCE_DISABLED;
Steven J. Hillb6d92b42013-03-25 13:47:29 -050044 pr_info("Software DMA cache coherency (command line)\n");
45 return 0;
46}
47early_param("nocoherentio", setnocoherentio);
Felix Fietkau885014b2013-09-27 14:41:44 +020048#endif
Steven J. Hillb6d92b42013-03-25 13:47:29 -050049
Dezhong Diaoe36863a2010-10-13 16:57:35 -070050static inline struct page *dma_addr_to_page(struct device *dev,
Kevin Cernekee3807ef3f62009-04-23 17:25:12 -070051 dma_addr_t dma_addr)
Franck Bui-Huuc9d06962007-03-19 17:36:42 +010052{
Dezhong Diaoe36863a2010-10-13 16:57:35 -070053 return pfn_to_page(
54 plat_dma_addr_to_phys(dev, dma_addr) >> PAGE_SHIFT);
Franck Bui-Huuc9d06962007-03-19 17:36:42 +010055}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Jim Quinlanf86f55d2013-08-27 16:57:51 -040058 * 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.
61 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * Warning on the terminology - Linux calls an uncached area coherent;
63 * MIPS terminology calls memory areas with hardware maintained coherency
64 * coherent.
Ralf Baechle0dc294c2014-11-11 22:22:03 +010065 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
Paul Burtoncad482c2017-06-13 10:01:08 -070071static inline bool cpu_needs_post_dma_flush(struct device *dev)
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000072{
Paul Burtoncad482c2017-06-13 10:01:08 -070073 if (plat_device_is_coherent(dev))
74 return false;
75
76 switch (boot_cpu_type()) {
77 case CPU_R10000:
78 case CPU_R12000:
79 case CPU_BMIPS5000:
80 return true;
81
82 default:
83 /*
84 * Presence of MAARs suggests that the CPU supports
85 * speculatively prefetching data, and therefore requires
86 * the post-DMA flush/invalidate.
87 */
88 return cpu_has_maar;
89 }
Ralf Baechle9a88cbb2006-11-16 02:56:12 +000090}
91
Ralf Baechlecce335a2007-11-03 02:05:43 +000092static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
93{
Ralf Baechlea2e715a2010-09-02 23:22:23 +020094 gfp_t dma_flag;
95
Ralf Baechlecce335a2007-11-03 02:05:43 +000096 /* ignore region specifiers */
97 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
98
Ralf Baechlea2e715a2010-09-02 23:22:23 +020099#ifdef CONFIG_ISA
Ralf Baechlecce335a2007-11-03 02:05:43 +0000100 if (dev == NULL)
Ralf Baechlea2e715a2010-09-02 23:22:23 +0200101 dma_flag = __GFP_DMA;
Ralf Baechlecce335a2007-11-03 02:05:43 +0000102 else
103#endif
Ralf Baechlea2e715a2010-09-02 23:22:23 +0200104#if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA)
Matt Redfearn8d4925e2015-12-21 15:21:42 +0000105 if (dev == NULL || dev->coherent_dma_mask < DMA_BIT_MASK(32))
Ralf Baechlea2e715a2010-09-02 23:22:23 +0200106 dma_flag = __GFP_DMA;
107 else if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
108 dma_flag = __GFP_DMA32;
Ralf Baechlecce335a2007-11-03 02:05:43 +0000109 else
110#endif
Ralf Baechlea2e715a2010-09-02 23:22:23 +0200111#if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA)
Matt Redfearn8d4925e2015-12-21 15:21:42 +0000112 if (dev == NULL || dev->coherent_dma_mask < DMA_BIT_MASK(64))
Ralf Baechlea2e715a2010-09-02 23:22:23 +0200113 dma_flag = __GFP_DMA32;
114 else
115#endif
116#if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32)
Matt Redfearn8d4925e2015-12-21 15:21:42 +0000117 if (dev == NULL ||
118 dev->coherent_dma_mask < DMA_BIT_MASK(sizeof(phys_addr_t) * 8))
Ralf Baechlea2e715a2010-09-02 23:22:23 +0200119 dma_flag = __GFP_DMA;
120 else
121#endif
122 dma_flag = 0;
Ralf Baechlecce335a2007-11-03 02:05:43 +0000123
124 /* Don't invoke OOM killer */
125 gfp |= __GFP_NORETRY;
126
Ralf Baechlea2e715a2010-09-02 23:22:23 +0200127 return gfp | dma_flag;
Ralf Baechlecce335a2007-11-03 02:05:43 +0000128}
129
Christoph Hellwig1e893752015-09-09 15:39:42 -0700130static void *mips_dma_alloc_noncoherent(struct device *dev, size_t size,
Al Viro185a8ff2005-10-21 03:21:23 -0400131 dma_addr_t * dma_handle, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 void *ret;
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000134
Ralf Baechlecce335a2007-11-03 02:05:43 +0000135 gfp = massage_gfp_flags(dev, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 ret = (void *) __get_free_pages(gfp, get_order(size));
138
139 if (ret != NULL) {
140 memset(ret, 0, size);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000141 *dma_handle = plat_map_dma_mem(dev, ret, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
144 return ret;
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
David Daney48e1fd52010-10-01 13:27:32 -0700147static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700148 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 void *ret;
Zubair Lutfullah Kakakhelf4649382014-07-16 16:51:32 +0100151 struct page *page = NULL;
152 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Christoph Hellwig1e893752015-09-09 15:39:42 -0700154 /*
155 * XXX: seems like the coherent and non-coherent implementations could
156 * be consolidated.
157 */
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700158 if (attrs & DMA_ATTR_NON_CONSISTENT)
Christoph Hellwig1e893752015-09-09 15:39:42 -0700159 return mips_dma_alloc_noncoherent(dev, size, dma_handle, gfp);
160
Ralf Baechlecce335a2007-11-03 02:05:43 +0000161 gfp = massage_gfp_flags(dev, gfp);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000162
Qais Yousef9530d0f2015-12-11 13:41:09 -0800163 if (IS_ENABLED(CONFIG_DMA_CMA) && gfpflags_allow_blocking(gfp))
Lucas Stach712c6042017-02-24 14:58:44 -0800164 page = dma_alloc_from_contiguous(dev, count, get_order(size),
165 gfp);
Zubair Lutfullah Kakakhelf4649382014-07-16 16:51:32 +0100166 if (!page)
167 page = alloc_pages(gfp, get_order(size));
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000168
Zubair Lutfullah Kakakhelf4649382014-07-16 16:51:32 +0100169 if (!page)
170 return NULL;
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000171
Zubair Lutfullah Kakakhelf4649382014-07-16 16:51:32 +0100172 ret = page_address(page);
173 memset(ret, 0, size);
174 *dma_handle = plat_map_dma_mem(dev, ret, size);
175 if (!plat_device_is_coherent(dev)) {
176 dma_cache_wback_inv((unsigned long) ret, size);
Paul Burtoncfa93fb2016-10-05 18:18:15 +0100177 ret = UNCAC_ADDR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
180 return ret;
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Christoph Hellwig1e893752015-09-09 15:39:42 -0700184static void mips_dma_free_noncoherent(struct device *dev, size_t size,
185 void *vaddr, dma_addr_t dma_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700187 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 free_pages((unsigned long) vaddr, get_order(size));
189}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David Daney48e1fd52010-10-01 13:27:32 -0700191static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700192 dma_addr_t dma_handle, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 unsigned long addr = (unsigned long) vaddr;
Zubair Lutfullah Kakakhelf4649382014-07-16 16:51:32 +0100195 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
196 struct page *page = NULL;
Yoichi Yuasaf8ac0422009-06-04 00:16:04 +0900197
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700198 if (attrs & DMA_ATTR_NON_CONSISTENT) {
Christoph Hellwig1e893752015-09-09 15:39:42 -0700199 mips_dma_free_noncoherent(dev, size, vaddr, dma_handle);
200 return;
201 }
202
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700203 plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
David Daney11531ac2008-12-10 18:14:45 -0800204
Paul Burtoncfa93fb2016-10-05 18:18:15 +0100205 if (!plat_device_is_coherent(dev))
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000206 addr = CAC_ADDR(addr);
207
Zubair Lutfullah Kakakhelf4649382014-07-16 16:51:32 +0100208 page = virt_to_page((void *) addr);
209
210 if (!dma_release_from_contiguous(dev, page, count))
211 __free_pages(page, get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Alex Smith8c172462015-07-30 12:03:42 +0100214static int mips_dma_mmap(struct device *dev, struct vm_area_struct *vma,
215 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700216 unsigned long attrs)
Alex Smith8c172462015-07-30 12:03:42 +0100217{
218 unsigned long user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
219 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
220 unsigned long addr = (unsigned long)cpu_addr;
221 unsigned long off = vma->vm_pgoff;
222 unsigned long pfn;
223 int ret = -ENXIO;
224
Paul Burtoncfa93fb2016-10-05 18:18:15 +0100225 if (!plat_device_is_coherent(dev))
Alex Smith8c172462015-07-30 12:03:42 +0100226 addr = CAC_ADDR(addr);
227
228 pfn = page_to_pfn(virt_to_page((void *)addr));
229
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700230 if (attrs & DMA_ATTR_WRITE_COMBINE)
Alex Smith8c172462015-07-30 12:03:42 +0100231 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
232 else
233 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
234
Vladimir Murzin43fc5092017-07-20 11:19:58 +0100235 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
Alex Smith8c172462015-07-30 12:03:42 +0100236 return ret;
237
238 if (off < count && user_count <= (count - off)) {
239 ret = remap_pfn_range(vma, vma->vm_start,
240 pfn + off,
241 user_count << PAGE_SHIFT,
242 vma->vm_page_prot);
243 }
244
245 return ret;
246}
247
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700248static inline void __dma_sync_virtual(void *addr, size_t size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 enum dma_data_direction direction)
250{
251 switch (direction) {
252 case DMA_TO_DEVICE:
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700253 dma_cache_wback((unsigned long)addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
255
256 case DMA_FROM_DEVICE:
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700257 dma_cache_inv((unsigned long)addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
259
260 case DMA_BIDIRECTIONAL:
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700261 dma_cache_wback_inv((unsigned long)addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
263
264 default:
265 BUG();
266 }
267}
268
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700269/*
270 * A single sg entry may refer to multiple physically contiguous
271 * pages. But we still need to process highmem pages individually.
272 * If highmem is not configured then the bulk of this loop gets
273 * optimized out.
274 */
275static inline void __dma_sync(struct page *page,
276 unsigned long offset, size_t size, enum dma_data_direction direction)
277{
278 size_t left = size;
279
280 do {
281 size_t len = left;
282
283 if (PageHighMem(page)) {
284 void *addr;
285
286 if (offset + len > PAGE_SIZE) {
287 if (offset >= PAGE_SIZE) {
288 page += offset >> PAGE_SHIFT;
289 offset &= ~PAGE_MASK;
290 }
291 len = PAGE_SIZE - offset;
292 }
293
294 addr = kmap_atomic(page);
295 __dma_sync_virtual(addr + offset, len, direction);
296 kunmap_atomic(addr);
297 } else
298 __dma_sync_virtual(page_address(page) + offset,
299 size, direction);
300 offset = 0;
301 page++;
302 left -= len;
303 } while (left);
304}
305
David Daney48e1fd52010-10-01 13:27:32 -0700306static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700307 size_t size, enum dma_data_direction direction, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Alexander Duyck9f318d42016-12-14 15:04:58 -0800309 if (cpu_needs_post_dma_flush(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700310 __dma_sync(dma_addr_to_page(dev, dma_addr),
311 dma_addr & ~PAGE_MASK, size, direction);
Ralf Baechle0acbfc62015-03-27 15:10:30 +0100312 plat_post_dma_flush(dev);
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700313 plat_unmap_dma_mem(dev, dma_addr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900316static int mips_dma_map_sg(struct device *dev, struct scatterlist *sglist,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700317 int nents, enum dma_data_direction direction, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int i;
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900320 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900322 for_each_sg(sglist, sg, nents, i) {
Alexander Duyck9f318d42016-12-14 15:04:58 -0800323 if (!plat_device_is_coherent(dev) &&
324 !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700325 __dma_sync(sg_page(sg), sg->offset, sg->length,
326 direction);
Jayachandran C4954a9a2013-06-10 06:28:08 +0000327#ifdef CONFIG_NEED_SG_DMA_LENGTH
328 sg->dma_length = sg->length;
329#endif
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700330 sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
331 sg->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 return nents;
335}
336
David Daney48e1fd52010-10-01 13:27:32 -0700337static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
338 unsigned long offset, size_t size, enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700339 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Alexander Duyck9f318d42016-12-14 15:04:58 -0800341 if (!plat_device_is_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700342 __dma_sync(page, offset, size, direction);
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000343
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700344 return plat_map_dma_mem_page(dev, page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900347static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
David Daney48e1fd52010-10-01 13:27:32 -0700348 int nhwentries, enum dma_data_direction direction,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700349 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int i;
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900352 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900354 for_each_sg(sglist, sg, nhwentries, i) {
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000355 if (!plat_device_is_coherent(dev) &&
Alexander Duyck9f318d42016-12-14 15:04:58 -0800356 !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700357 direction != DMA_TO_DEVICE)
358 __dma_sync(sg_page(sg), sg->offset, sg->length,
359 direction);
Kevin Cernekeed3f634b2009-04-23 17:03:43 -0700360 plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362}
363
David Daney48e1fd52010-10-01 13:27:32 -0700364static void mips_dma_sync_single_for_cpu(struct device *dev,
365 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Jim Quinlanf86f55d2013-08-27 16:57:51 -0400367 if (cpu_needs_post_dma_flush(dev))
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700368 __dma_sync(dma_addr_to_page(dev, dma_handle),
369 dma_handle & ~PAGE_MASK, size, direction);
Ralf Baechle0acbfc62015-03-27 15:10:30 +0100370 plat_post_dma_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
David Daney48e1fd52010-10-01 13:27:32 -0700373static void mips_dma_sync_single_for_device(struct device *dev,
374 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700376 if (!plat_device_is_coherent(dev))
377 __dma_sync(dma_addr_to_page(dev, dma_handle),
378 dma_handle & ~PAGE_MASK, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
David Daney48e1fd52010-10-01 13:27:32 -0700381static void mips_dma_sync_sg_for_cpu(struct device *dev,
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900382 struct scatterlist *sglist, int nelems,
383 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 int i;
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900386 struct scatterlist *sg;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700387
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900388 if (cpu_needs_post_dma_flush(dev)) {
389 for_each_sg(sglist, sg, nelems, i) {
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700390 __dma_sync(sg_page(sg), sg->offset, sg->length,
391 direction);
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900392 }
393 }
Ralf Baechle0acbfc62015-03-27 15:10:30 +0100394 plat_post_dma_flush(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
David Daney48e1fd52010-10-01 13:27:32 -0700397static void mips_dma_sync_sg_for_device(struct device *dev,
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900398 struct scatterlist *sglist, int nelems,
399 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 int i;
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900402 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900404 if (!plat_device_is_coherent(dev)) {
405 for_each_sg(sglist, sg, nelems, i) {
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700406 __dma_sync(sg_page(sg), sg->offset, sg->length,
407 direction);
Akinobu Mita1e51714c2015-05-01 22:56:38 +0900408 }
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
David Daney48e1fd52010-10-01 13:27:32 -0700412int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Felix Fietkau4e7f7262013-08-15 11:28:30 +0200414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
David Daney48e1fd52010-10-01 13:27:32 -0700417int mips_dma_supported(struct device *dev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
David Daney843aef42008-12-11 15:33:36 -0800419 return plat_dma_supported(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Ralf Baechlea3aad4a2010-12-09 19:14:09 +0000422void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
David Daney48e1fd52010-10-01 13:27:32 -0700423 enum dma_data_direction direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000425 BUG_ON(direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Ralf Baechle9a88cbb2006-11-16 02:56:12 +0000427 if (!plat_device_is_coherent(dev))
Dezhong Diaoe36863a2010-10-13 16:57:35 -0700428 __dma_sync_virtual(vaddr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Ralf Baechlea3aad4a2010-12-09 19:14:09 +0000431EXPORT_SYMBOL(dma_cache_sync);
432
Bart Van Assche52997092017-01-20 13:04:01 -0800433static const struct dma_map_ops mips_default_dma_map_ops = {
Andrzej Pietrasiewicze8d51e542012-03-27 14:32:21 +0200434 .alloc = mips_dma_alloc_coherent,
435 .free = mips_dma_free_coherent,
Alex Smith8c172462015-07-30 12:03:42 +0100436 .mmap = mips_dma_mmap,
David Daney48e1fd52010-10-01 13:27:32 -0700437 .map_page = mips_dma_map_page,
438 .unmap_page = mips_dma_unmap_page,
439 .map_sg = mips_dma_map_sg,
440 .unmap_sg = mips_dma_unmap_sg,
441 .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
442 .sync_single_for_device = mips_dma_sync_single_for_device,
443 .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
444 .sync_sg_for_device = mips_dma_sync_sg_for_device,
445 .mapping_error = mips_dma_mapping_error,
446 .dma_supported = mips_dma_supported
447};
448
Bart Van Assche52997092017-01-20 13:04:01 -0800449const struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
David Daney48e1fd52010-10-01 13:27:32 -0700450EXPORT_SYMBOL(mips_dma_map_ops);
451
452#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
453
454static int __init mips_dma_init(void)
455{
456 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
457
458 return 0;
459}
460fs_initcall(mips_dma_init);