Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 7 | * Copyright (C) 2000, 2001, 06 Ralf Baechle <ralf@linux-mips.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * swiped from i386, and cloned for MIPS by Geert, polished by Ralf. |
| 9 | */ |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 12 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/mm.h> |
Paul Gortmaker | d9ba577 | 2016-08-21 15:58:14 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Jens Axboe | 4fcc47a | 2007-10-23 12:32:34 +0200 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
Ralf Baechle | 6e86b0b | 2007-10-29 19:35:33 +0000 | [diff] [blame] | 16 | #include <linux/string.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/gfp.h> |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 18 | #include <linux/highmem.h> |
Zubair Lutfullah Kakakhel | f464938 | 2014-07-16 16:51:32 +0100 | [diff] [blame] | 19 | #include <linux/dma-contiguous.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/cache.h> |
Ralf Baechle | 69f24d1 | 2013-09-17 10:25:47 +0200 | [diff] [blame] | 22 | #include <asm/cpu-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/io.h> |
| 24 | |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 25 | #include <dma-coherence.h> |
| 26 | |
Paul Burton | 20d3306 | 2016-10-05 18:18:16 +0100 | [diff] [blame] | 27 | #if defined(CONFIG_DMA_MAYBE_COHERENT) && !defined(CONFIG_DMA_PERDEV_COHERENT) |
Paul Burton | f230202 | 2016-10-05 18:18:14 +0100 | [diff] [blame] | 28 | /* User defined DMA coherency from command line. */ |
| 29 | enum coherent_io_user_state coherentio = IO_COHERENCE_DEFAULT; |
Steven J. Hill | b6d92b4 | 2013-03-25 13:47:29 -0500 | [diff] [blame] | 30 | EXPORT_SYMBOL_GPL(coherentio); |
| 31 | int hw_coherentio = 0; /* Actual hardware supported DMA coherency setting. */ |
| 32 | |
| 33 | static int __init setcoherentio(char *str) |
| 34 | { |
Paul Burton | f230202 | 2016-10-05 18:18:14 +0100 | [diff] [blame] | 35 | coherentio = IO_COHERENCE_ENABLED; |
Steven J. Hill | b6d92b4 | 2013-03-25 13:47:29 -0500 | [diff] [blame] | 36 | pr_info("Hardware DMA cache coherency (command line)\n"); |
| 37 | return 0; |
| 38 | } |
| 39 | early_param("coherentio", setcoherentio); |
| 40 | |
| 41 | static int __init setnocoherentio(char *str) |
| 42 | { |
Paul Burton | f230202 | 2016-10-05 18:18:14 +0100 | [diff] [blame] | 43 | coherentio = IO_COHERENCE_DISABLED; |
Steven J. Hill | b6d92b4 | 2013-03-25 13:47:29 -0500 | [diff] [blame] | 44 | pr_info("Software DMA cache coherency (command line)\n"); |
| 45 | return 0; |
| 46 | } |
| 47 | early_param("nocoherentio", setnocoherentio); |
Felix Fietkau | 885014b | 2013-09-27 14:41:44 +0200 | [diff] [blame] | 48 | #endif |
Steven J. Hill | b6d92b4 | 2013-03-25 13:47:29 -0500 | [diff] [blame] | 49 | |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 50 | static inline struct page *dma_addr_to_page(struct device *dev, |
Kevin Cernekee | 3807ef3f6 | 2009-04-23 17:25:12 -0700 | [diff] [blame] | 51 | dma_addr_t dma_addr) |
Franck Bui-Huu | c9d0696 | 2007-03-19 17:36:42 +0100 | [diff] [blame] | 52 | { |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 53 | return pfn_to_page( |
| 54 | plat_dma_addr_to_phys(dev, dma_addr) >> PAGE_SHIFT); |
Franck Bui-Huu | c9d0696 | 2007-03-19 17:36:42 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
Jim Quinlan | f86f55d | 2013-08-27 16:57:51 -0400 | [diff] [blame] | 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. |
| 61 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * Warning on the terminology - Linux calls an uncached area coherent; |
| 63 | * MIPS terminology calls memory areas with hardware maintained coherency |
| 64 | * coherent. |
Ralf Baechle | 0dc294c | 2014-11-11 22:22:03 +0100 | [diff] [blame] | 65 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | */ |
Paul Burton | cad482c | 2017-06-13 10:01:08 -0700 | [diff] [blame] | 71 | static inline bool cpu_needs_post_dma_flush(struct device *dev) |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 72 | { |
Paul Burton | cad482c | 2017-06-13 10:01:08 -0700 | [diff] [blame] | 73 | 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 Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 92 | static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp) |
| 93 | { |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 94 | gfp_t dma_flag; |
| 95 | |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 96 | /* ignore region specifiers */ |
| 97 | gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM); |
| 98 | |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 99 | #ifdef CONFIG_ISA |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 100 | if (dev == NULL) |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 101 | dma_flag = __GFP_DMA; |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 102 | else |
| 103 | #endif |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 104 | #if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA) |
Matt Redfearn | 8d4925e | 2015-12-21 15:21:42 +0000 | [diff] [blame] | 105 | if (dev == NULL || dev->coherent_dma_mask < DMA_BIT_MASK(32)) |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 106 | dma_flag = __GFP_DMA; |
| 107 | else if (dev->coherent_dma_mask < DMA_BIT_MASK(64)) |
| 108 | dma_flag = __GFP_DMA32; |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 109 | else |
| 110 | #endif |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 111 | #if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA) |
Matt Redfearn | 8d4925e | 2015-12-21 15:21:42 +0000 | [diff] [blame] | 112 | if (dev == NULL || dev->coherent_dma_mask < DMA_BIT_MASK(64)) |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 113 | dma_flag = __GFP_DMA32; |
| 114 | else |
| 115 | #endif |
| 116 | #if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32) |
Matt Redfearn | 8d4925e | 2015-12-21 15:21:42 +0000 | [diff] [blame] | 117 | if (dev == NULL || |
| 118 | dev->coherent_dma_mask < DMA_BIT_MASK(sizeof(phys_addr_t) * 8)) |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 119 | dma_flag = __GFP_DMA; |
| 120 | else |
| 121 | #endif |
| 122 | dma_flag = 0; |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 123 | |
| 124 | /* Don't invoke OOM killer */ |
| 125 | gfp |= __GFP_NORETRY; |
| 126 | |
Ralf Baechle | a2e715a | 2010-09-02 23:22:23 +0200 | [diff] [blame] | 127 | return gfp | dma_flag; |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Christoph Hellwig | 1e89375 | 2015-09-09 15:39:42 -0700 | [diff] [blame] | 130 | static void *mips_dma_alloc_noncoherent(struct device *dev, size_t size, |
Al Viro | 185a8ff | 2005-10-21 03:21:23 -0400 | [diff] [blame] | 131 | dma_addr_t * dma_handle, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | void *ret; |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 134 | |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 135 | gfp = massage_gfp_flags(dev, gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | ret = (void *) __get_free_pages(gfp, get_order(size)); |
| 138 | |
| 139 | if (ret != NULL) { |
| 140 | memset(ret, 0, size); |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 141 | *dma_handle = plat_map_dma_mem(dev, ret, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return ret; |
| 145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 147 | static void *mips_dma_alloc_coherent(struct device *dev, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 148 | dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
| 150 | void *ret; |
Zubair Lutfullah Kakakhel | f464938 | 2014-07-16 16:51:32 +0100 | [diff] [blame] | 151 | struct page *page = NULL; |
| 152 | unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Christoph Hellwig | 1e89375 | 2015-09-09 15:39:42 -0700 | [diff] [blame] | 154 | /* |
| 155 | * XXX: seems like the coherent and non-coherent implementations could |
| 156 | * be consolidated. |
| 157 | */ |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 158 | if (attrs & DMA_ATTR_NON_CONSISTENT) |
Christoph Hellwig | 1e89375 | 2015-09-09 15:39:42 -0700 | [diff] [blame] | 159 | return mips_dma_alloc_noncoherent(dev, size, dma_handle, gfp); |
| 160 | |
Ralf Baechle | cce335a | 2007-11-03 02:05:43 +0000 | [diff] [blame] | 161 | gfp = massage_gfp_flags(dev, gfp); |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 162 | |
Qais Yousef | 9530d0f | 2015-12-11 13:41:09 -0800 | [diff] [blame] | 163 | if (IS_ENABLED(CONFIG_DMA_CMA) && gfpflags_allow_blocking(gfp)) |
Lucas Stach | 712c604 | 2017-02-24 14:58:44 -0800 | [diff] [blame] | 164 | page = dma_alloc_from_contiguous(dev, count, get_order(size), |
| 165 | gfp); |
Zubair Lutfullah Kakakhel | f464938 | 2014-07-16 16:51:32 +0100 | [diff] [blame] | 166 | if (!page) |
| 167 | page = alloc_pages(gfp, get_order(size)); |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 168 | |
Zubair Lutfullah Kakakhel | f464938 | 2014-07-16 16:51:32 +0100 | [diff] [blame] | 169 | if (!page) |
| 170 | return NULL; |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 171 | |
Zubair Lutfullah Kakakhel | f464938 | 2014-07-16 16:51:32 +0100 | [diff] [blame] | 172 | 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 Burton | cfa93fb | 2016-10-05 18:18:15 +0100 | [diff] [blame] | 177 | ret = UNCAC_ADDR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Christoph Hellwig | 1e89375 | 2015-09-09 15:39:42 -0700 | [diff] [blame] | 184 | static void mips_dma_free_noncoherent(struct device *dev, size_t size, |
| 185 | void *vaddr, dma_addr_t dma_handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Kevin Cernekee | d3f634b | 2009-04-23 17:03:43 -0700 | [diff] [blame] | 187 | plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | free_pages((unsigned long) vaddr, get_order(size)); |
| 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 191 | static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 192 | dma_addr_t dma_handle, unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
| 194 | unsigned long addr = (unsigned long) vaddr; |
Zubair Lutfullah Kakakhel | f464938 | 2014-07-16 16:51:32 +0100 | [diff] [blame] | 195 | unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 196 | struct page *page = NULL; |
Yoichi Yuasa | f8ac042 | 2009-06-04 00:16:04 +0900 | [diff] [blame] | 197 | |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 198 | if (attrs & DMA_ATTR_NON_CONSISTENT) { |
Christoph Hellwig | 1e89375 | 2015-09-09 15:39:42 -0700 | [diff] [blame] | 199 | mips_dma_free_noncoherent(dev, size, vaddr, dma_handle); |
| 200 | return; |
| 201 | } |
| 202 | |
Kevin Cernekee | d3f634b | 2009-04-23 17:03:43 -0700 | [diff] [blame] | 203 | plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL); |
David Daney | 11531ac | 2008-12-10 18:14:45 -0800 | [diff] [blame] | 204 | |
Paul Burton | cfa93fb | 2016-10-05 18:18:15 +0100 | [diff] [blame] | 205 | if (!plat_device_is_coherent(dev)) |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 206 | addr = CAC_ADDR(addr); |
| 207 | |
Zubair Lutfullah Kakakhel | f464938 | 2014-07-16 16:51:32 +0100 | [diff] [blame] | 208 | page = virt_to_page((void *) addr); |
| 209 | |
| 210 | if (!dma_release_from_contiguous(dev, page, count)) |
| 211 | __free_pages(page, get_order(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Alex Smith | 8c17246 | 2015-07-30 12:03:42 +0100 | [diff] [blame] | 214 | static 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 Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 216 | unsigned long attrs) |
Alex Smith | 8c17246 | 2015-07-30 12:03:42 +0100 | [diff] [blame] | 217 | { |
| 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 Burton | cfa93fb | 2016-10-05 18:18:15 +0100 | [diff] [blame] | 225 | if (!plat_device_is_coherent(dev)) |
Alex Smith | 8c17246 | 2015-07-30 12:03:42 +0100 | [diff] [blame] | 226 | addr = CAC_ADDR(addr); |
| 227 | |
| 228 | pfn = page_to_pfn(virt_to_page((void *)addr)); |
| 229 | |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 230 | if (attrs & DMA_ATTR_WRITE_COMBINE) |
Alex Smith | 8c17246 | 2015-07-30 12:03:42 +0100 | [diff] [blame] | 231 | 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 Murzin | 43fc509 | 2017-07-20 11:19:58 +0100 | [diff] [blame] | 235 | if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret)) |
Alex Smith | 8c17246 | 2015-07-30 12:03:42 +0100 | [diff] [blame] | 236 | 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 Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 248 | static inline void __dma_sync_virtual(void *addr, size_t size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | enum dma_data_direction direction) |
| 250 | { |
| 251 | switch (direction) { |
| 252 | case DMA_TO_DEVICE: |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 253 | dma_cache_wback((unsigned long)addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | break; |
| 255 | |
| 256 | case DMA_FROM_DEVICE: |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 257 | dma_cache_inv((unsigned long)addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | break; |
| 259 | |
| 260 | case DMA_BIDIRECTIONAL: |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 261 | dma_cache_wback_inv((unsigned long)addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | break; |
| 263 | |
| 264 | default: |
| 265 | BUG(); |
| 266 | } |
| 267 | } |
| 268 | |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 269 | /* |
| 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 | */ |
| 275 | static 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 Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 306 | static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 307 | size_t size, enum dma_data_direction direction, unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
Alexander Duyck | 9f318d4 | 2016-12-14 15:04:58 -0800 | [diff] [blame] | 309 | if (cpu_needs_post_dma_flush(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 310 | __dma_sync(dma_addr_to_page(dev, dma_addr), |
| 311 | dma_addr & ~PAGE_MASK, size, direction); |
Ralf Baechle | 0acbfc6 | 2015-03-27 15:10:30 +0100 | [diff] [blame] | 312 | plat_post_dma_flush(dev); |
Kevin Cernekee | d3f634b | 2009-04-23 17:03:43 -0700 | [diff] [blame] | 313 | plat_unmap_dma_mem(dev, dma_addr, size, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 316 | static int mips_dma_map_sg(struct device *dev, struct scatterlist *sglist, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 317 | int nents, enum dma_data_direction direction, unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
| 319 | int i; |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 320 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 322 | for_each_sg(sglist, sg, nents, i) { |
Alexander Duyck | 9f318d4 | 2016-12-14 15:04:58 -0800 | [diff] [blame] | 323 | if (!plat_device_is_coherent(dev) && |
| 324 | !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 325 | __dma_sync(sg_page(sg), sg->offset, sg->length, |
| 326 | direction); |
Jayachandran C | 4954a9a | 2013-06-10 06:28:08 +0000 | [diff] [blame] | 327 | #ifdef CONFIG_NEED_SG_DMA_LENGTH |
| 328 | sg->dma_length = sg->length; |
| 329 | #endif |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 330 | sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) + |
| 331 | sg->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | return nents; |
| 335 | } |
| 336 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 337 | static 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 Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 339 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { |
Alexander Duyck | 9f318d4 | 2016-12-14 15:04:58 -0800 | [diff] [blame] | 341 | if (!plat_device_is_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 342 | __dma_sync(page, offset, size, direction); |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 343 | |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 344 | return plat_map_dma_mem_page(dev, page) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 347 | static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sglist, |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 348 | int nhwentries, enum dma_data_direction direction, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 349 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | int i; |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 352 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 354 | for_each_sg(sglist, sg, nhwentries, i) { |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 355 | if (!plat_device_is_coherent(dev) && |
Alexander Duyck | 9f318d4 | 2016-12-14 15:04:58 -0800 | [diff] [blame] | 356 | !(attrs & DMA_ATTR_SKIP_CPU_SYNC) && |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 357 | direction != DMA_TO_DEVICE) |
| 358 | __dma_sync(sg_page(sg), sg->offset, sg->length, |
| 359 | direction); |
Kevin Cernekee | d3f634b | 2009-04-23 17:03:43 -0700 | [diff] [blame] | 360 | plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 364 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
Jim Quinlan | f86f55d | 2013-08-27 16:57:51 -0400 | [diff] [blame] | 367 | if (cpu_needs_post_dma_flush(dev)) |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 368 | __dma_sync(dma_addr_to_page(dev, dma_handle), |
| 369 | dma_handle & ~PAGE_MASK, size, direction); |
Ralf Baechle | 0acbfc6 | 2015-03-27 15:10:30 +0100 | [diff] [blame] | 370 | plat_post_dma_flush(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 373 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 376 | if (!plat_device_is_coherent(dev)) |
| 377 | __dma_sync(dma_addr_to_page(dev, dma_handle), |
| 378 | dma_handle & ~PAGE_MASK, size, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 381 | static void mips_dma_sync_sg_for_cpu(struct device *dev, |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 382 | struct scatterlist *sglist, int nelems, |
| 383 | enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
| 385 | int i; |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 386 | struct scatterlist *sg; |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 387 | |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 388 | if (cpu_needs_post_dma_flush(dev)) { |
| 389 | for_each_sg(sglist, sg, nelems, i) { |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 390 | __dma_sync(sg_page(sg), sg->offset, sg->length, |
| 391 | direction); |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 392 | } |
| 393 | } |
Ralf Baechle | 0acbfc6 | 2015-03-27 15:10:30 +0100 | [diff] [blame] | 394 | plat_post_dma_flush(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 397 | static void mips_dma_sync_sg_for_device(struct device *dev, |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 398 | struct scatterlist *sglist, int nelems, |
| 399 | enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { |
| 401 | int i; |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 402 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 404 | if (!plat_device_is_coherent(dev)) { |
| 405 | for_each_sg(sglist, sg, nelems, i) { |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 406 | __dma_sync(sg_page(sg), sg->offset, sg->length, |
| 407 | direction); |
Akinobu Mita | 1e51714c | 2015-05-01 22:56:38 +0900 | [diff] [blame] | 408 | } |
| 409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 412 | int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
Felix Fietkau | 4e7f726 | 2013-08-15 11:28:30 +0200 | [diff] [blame] | 414 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 417 | int mips_dma_supported(struct device *dev, u64 mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
David Daney | 843aef4 | 2008-12-11 15:33:36 -0800 | [diff] [blame] | 419 | return plat_dma_supported(dev, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Ralf Baechle | a3aad4a | 2010-12-09 19:14:09 +0000 | [diff] [blame] | 422 | void dma_cache_sync(struct device *dev, void *vaddr, size_t size, |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 423 | enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 425 | BUG_ON(direction == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Ralf Baechle | 9a88cbb | 2006-11-16 02:56:12 +0000 | [diff] [blame] | 427 | if (!plat_device_is_coherent(dev)) |
Dezhong Diao | e36863a | 2010-10-13 16:57:35 -0700 | [diff] [blame] | 428 | __dma_sync_virtual(vaddr, size, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Ralf Baechle | a3aad4a | 2010-12-09 19:14:09 +0000 | [diff] [blame] | 431 | EXPORT_SYMBOL(dma_cache_sync); |
| 432 | |
Bart Van Assche | 5299709 | 2017-01-20 13:04:01 -0800 | [diff] [blame] | 433 | static const struct dma_map_ops mips_default_dma_map_ops = { |
Andrzej Pietrasiewicz | e8d51e54 | 2012-03-27 14:32:21 +0200 | [diff] [blame] | 434 | .alloc = mips_dma_alloc_coherent, |
| 435 | .free = mips_dma_free_coherent, |
Alex Smith | 8c17246 | 2015-07-30 12:03:42 +0100 | [diff] [blame] | 436 | .mmap = mips_dma_mmap, |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 437 | .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 Assche | 5299709 | 2017-01-20 13:04:01 -0800 | [diff] [blame] | 449 | const struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops; |
David Daney | 48e1fd5 | 2010-10-01 13:27:32 -0700 | [diff] [blame] | 450 | EXPORT_SYMBOL(mips_dma_map_ops); |
| 451 | |
| 452 | #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16) |
| 453 | |
| 454 | static int __init mips_dma_init(void) |
| 455 | { |
| 456 | dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | fs_initcall(mips_dma_init); |