~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/mips/mm/dma-default.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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, 06  Ralf Baechle <ralf@linux-mips.org>
  8  * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
  9  */
 10 
 11 #include <linux/types.h>
 12 #include <linux/dma-mapping.h>
 13 #include <linux/mm.h>
 14 #include <linux/module.h>
 15 #include <linux/scatterlist.h>
 16 #include <linux/string.h>
 17 #include <linux/gfp.h>
 18 #include <linux/highmem.h>
 19 
 20 #include <asm/cache.h>
 21 #include <asm/io.h>
 22 
 23 #include <dma-coherence.h>
 24 
 25 static inline struct page *dma_addr_to_page(struct device *dev,
 26         dma_addr_t dma_addr)
 27 {
 28         return pfn_to_page(
 29                 plat_dma_addr_to_phys(dev, dma_addr) >> PAGE_SHIFT);
 30 }
 31 
 32 /*
 33  * The affected CPUs below in 'cpu_needs_post_dma_flush()' can
 34  * speculatively fill random cachelines with stale data at any time,
 35  * requiring an extra flush post-DMA.
 36  *
 37  * Warning on the terminology - Linux calls an uncached area coherent;
 38  * MIPS terminology calls memory areas with hardware maintained coherency
 39  * coherent.
 40  */
 41 static inline int cpu_needs_post_dma_flush(struct device *dev)
 42 {
 43         return !plat_device_is_coherent(dev) &&
 44                (current_cpu_type() == CPU_R10000 ||
 45                 current_cpu_type() == CPU_R12000 ||
 46                 current_cpu_type() == CPU_BMIPS5000);
 47 }
 48 
 49 static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
 50 {
 51         gfp_t dma_flag;
 52 
 53         /* ignore region specifiers */
 54         gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
 55 
 56 #ifdef CONFIG_ISA
 57         if (dev == NULL)
 58                 dma_flag = __GFP_DMA;
 59         else
 60 #endif
 61 #if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA)
 62              if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
 63                         dma_flag = __GFP_DMA;
 64         else if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
 65                         dma_flag = __GFP_DMA32;
 66         else
 67 #endif
 68 #if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA)
 69              if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
 70                 dma_flag = __GFP_DMA32;
 71         else
 72 #endif
 73 #if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32)
 74              if (dev->coherent_dma_mask < DMA_BIT_MASK(sizeof(phys_addr_t) * 8))
 75                 dma_flag = __GFP_DMA;
 76         else
 77 #endif
 78                 dma_flag = 0;
 79 
 80         /* Don't invoke OOM killer */
 81         gfp |= __GFP_NORETRY;
 82 
 83         return gfp | dma_flag;
 84 }
 85 
 86 void *dma_alloc_noncoherent(struct device *dev, size_t size,
 87         dma_addr_t * dma_handle, gfp_t gfp)
 88 {
 89         void *ret;
 90 
 91         gfp = massage_gfp_flags(dev, gfp);
 92 
 93         ret = (void *) __get_free_pages(gfp, get_order(size));
 94 
 95         if (ret != NULL) {
 96                 memset(ret, 0, size);
 97                 *dma_handle = plat_map_dma_mem(dev, ret, size);
 98         }
 99 
100         return ret;
101 }
102 EXPORT_SYMBOL(dma_alloc_noncoherent);
103 
104 static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
105         dma_addr_t * dma_handle, gfp_t gfp, struct dma_attrs *attrs)
106 {
107         void *ret;
108 
109         if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
110                 return ret;
111 
112         gfp = massage_gfp_flags(dev, gfp);
113 
114         ret = (void *) __get_free_pages(gfp, get_order(size));
115 
116         if (ret) {
117                 memset(ret, 0, size);
118                 *dma_handle = plat_map_dma_mem(dev, ret, size);
119 
120                 if (!plat_device_is_coherent(dev)) {
121                         dma_cache_wback_inv((unsigned long) ret, size);
122                         ret = UNCAC_ADDR(ret);
123                 }
124         }
125 
126         return ret;
127 }
128 
129 
130 void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
131         dma_addr_t dma_handle)
132 {
133         plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
134         free_pages((unsigned long) vaddr, get_order(size));
135 }
136 EXPORT_SYMBOL(dma_free_noncoherent);
137 
138 static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
139         dma_addr_t dma_handle, struct dma_attrs *attrs)
140 {
141         unsigned long addr = (unsigned long) vaddr;
142         int order = get_order(size);
143 
144         if (dma_release_from_coherent(dev, order, vaddr))
145                 return;
146 
147         plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
148 
149         if (!plat_device_is_coherent(dev))
150                 addr = CAC_ADDR(addr);
151 
152         free_pages(addr, get_order(size));
153 }
154 
155 static inline void __dma_sync_virtual(void *addr, size_t size,
156         enum dma_data_direction direction)
157 {
158         switch (direction) {
159         case DMA_TO_DEVICE:
160                 dma_cache_wback((unsigned long)addr, size);
161                 break;
162 
163         case DMA_FROM_DEVICE:
164                 dma_cache_inv((unsigned long)addr, size);
165                 break;
166 
167         case DMA_BIDIRECTIONAL:
168                 dma_cache_wback_inv((unsigned long)addr, size);
169                 break;
170 
171         default:
172                 BUG();
173         }
174 }
175 
176 /*
177  * A single sg entry may refer to multiple physically contiguous
178  * pages. But we still need to process highmem pages individually.
179  * If highmem is not configured then the bulk of this loop gets
180  * optimized out.
181  */
182 static inline void __dma_sync(struct page *page,
183         unsigned long offset, size_t size, enum dma_data_direction direction)
184 {
185         size_t left = size;
186 
187         do {
188                 size_t len = left;
189 
190                 if (PageHighMem(page)) {
191                         void *addr;
192 
193                         if (offset + len > PAGE_SIZE) {
194                                 if (offset >= PAGE_SIZE) {
195                                         page += offset >> PAGE_SHIFT;
196                                         offset &= ~PAGE_MASK;
197                                 }
198                                 len = PAGE_SIZE - offset;
199                         }
200 
201                         addr = kmap_atomic(page);
202                         __dma_sync_virtual(addr + offset, len, direction);
203                         kunmap_atomic(addr);
204                 } else
205                         __dma_sync_virtual(page_address(page) + offset,
206                                            size, direction);
207                 offset = 0;
208                 page++;
209                 left -= len;
210         } while (left);
211 }
212 
213 static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
214         size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
215 {
216         if (cpu_needs_post_dma_flush(dev))
217                 __dma_sync(dma_addr_to_page(dev, dma_addr),
218                            dma_addr & ~PAGE_MASK, size, direction);
219 
220         plat_unmap_dma_mem(dev, dma_addr, size, direction);
221 }
222 
223 static int mips_dma_map_sg(struct device *dev, struct scatterlist *sg,
224         int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
225 {
226         int i;
227 
228         for (i = 0; i < nents; i++, sg++) {
229                 if (!plat_device_is_coherent(dev))
230                         __dma_sync(sg_page(sg), sg->offset, sg->length,
231                                    direction);
232                 sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
233                                   sg->offset;
234         }
235 
236         return nents;
237 }
238 
239 static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
240         unsigned long offset, size_t size, enum dma_data_direction direction,
241         struct dma_attrs *attrs)
242 {
243         if (!plat_device_is_coherent(dev))
244                 __dma_sync(page, offset, size, direction);
245 
246         return plat_map_dma_mem_page(dev, page) + offset;
247 }
248 
249 static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
250         int nhwentries, enum dma_data_direction direction,
251         struct dma_attrs *attrs)
252 {
253         int i;
254 
255         for (i = 0; i < nhwentries; i++, sg++) {
256                 if (!plat_device_is_coherent(dev) &&
257                     direction != DMA_TO_DEVICE)
258                         __dma_sync(sg_page(sg), sg->offset, sg->length,
259                                    direction);
260                 plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
261         }
262 }
263 
264 static void mips_dma_sync_single_for_cpu(struct device *dev,
265         dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
266 {
267         if (cpu_needs_post_dma_flush(dev))
268                 __dma_sync(dma_addr_to_page(dev, dma_handle),
269                            dma_handle & ~PAGE_MASK, size, direction);
270 }
271 
272 static void mips_dma_sync_single_for_device(struct device *dev,
273         dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
274 {
275         plat_extra_sync_for_device(dev);
276         if (!plat_device_is_coherent(dev))
277                 __dma_sync(dma_addr_to_page(dev, dma_handle),
278                            dma_handle & ~PAGE_MASK, size, direction);
279 }
280 
281 static void mips_dma_sync_sg_for_cpu(struct device *dev,
282         struct scatterlist *sg, int nelems, enum dma_data_direction direction)
283 {
284         int i;
285 
286         /* Make sure that gcc doesn't leave the empty loop body.  */
287         for (i = 0; i < nelems; i++, sg++) {
288                 if (cpu_needs_post_dma_flush(dev))
289                         __dma_sync(sg_page(sg), sg->offset, sg->length,
290                                    direction);
291         }
292 }
293 
294 static void mips_dma_sync_sg_for_device(struct device *dev,
295         struct scatterlist *sg, int nelems, enum dma_data_direction direction)
296 {
297         int i;
298 
299         /* Make sure that gcc doesn't leave the empty loop body.  */
300         for (i = 0; i < nelems; i++, sg++) {
301                 if (!plat_device_is_coherent(dev))
302                         __dma_sync(sg_page(sg), sg->offset, sg->length,
303                                    direction);
304         }
305 }
306 
307 int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
308 {
309         return plat_dma_mapping_error(dev, dma_addr);
310 }
311 
312 int mips_dma_supported(struct device *dev, u64 mask)
313 {
314         return plat_dma_supported(dev, mask);
315 }
316 
317 void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
318                          enum dma_data_direction direction)
319 {
320         BUG_ON(direction == DMA_NONE);
321 
322         plat_extra_sync_for_device(dev);
323         if (!plat_device_is_coherent(dev))
324                 __dma_sync_virtual(vaddr, size, direction);
325 }
326 
327 EXPORT_SYMBOL(dma_cache_sync);
328 
329 static struct dma_map_ops mips_default_dma_map_ops = {
330         .alloc = mips_dma_alloc_coherent,
331         .free = mips_dma_free_coherent,
332         .map_page = mips_dma_map_page,
333         .unmap_page = mips_dma_unmap_page,
334         .map_sg = mips_dma_map_sg,
335         .unmap_sg = mips_dma_unmap_sg,
336         .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
337         .sync_single_for_device = mips_dma_sync_single_for_device,
338         .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
339         .sync_sg_for_device = mips_dma_sync_sg_for_device,
340         .mapping_error = mips_dma_mapping_error,
341         .dma_supported = mips_dma_supported
342 };
343 
344 struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
345 EXPORT_SYMBOL(mips_dma_map_ops);
346 
347 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
348 
349 static int __init mips_dma_init(void)
350 {
351         dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
352 
353         return 0;
354 }
355 fs_initcall(mips_dma_init);
356 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp