blob: 29a46bb198ccaf834398212b25c3a1c670ba2763 [file] [log] [blame]
Vineet Gupta1162b072013-01-18 15:12:20 +05301/*
2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9/*
10 * DMA Coherent API Notes
11 *
12 * I/O is inherently non-coherent on ARC. So a coherent DMA buffer is
13 * implemented by accessintg it using a kernel virtual address, with
14 * Cache bit off in the TLB entry.
15 *
16 * The default DMA address == Phy address which is 0x8000_0000 based.
Vineet Gupta1162b072013-01-18 15:12:20 +053017 */
18
19#include <linux/dma-mapping.h>
20#include <linux/dma-debug.h>
21#include <linux/export.h>
Alexey Brodkinf2b0b252015-05-25 19:54:28 +030022#include <asm/cache.h>
Vineet Gupta1162b072013-01-18 15:12:20 +053023#include <asm/cacheflush.h>
24
25/*
26 * Helpers for Coherent DMA API.
27 */
28void *dma_alloc_noncoherent(struct device *dev, size_t size,
29 dma_addr_t *dma_handle, gfp_t gfp)
30{
31 void *paddr;
32
33 /* This is linear addr (0x8000_0000 based) */
34 paddr = alloc_pages_exact(size, gfp);
35 if (!paddr)
36 return NULL;
37
38 /* This is bus address, platform dependent */
Vineet Gupta454bfda2015-04-16 21:04:49 +053039 *dma_handle = (dma_addr_t)paddr;
Vineet Gupta1162b072013-01-18 15:12:20 +053040
41 return paddr;
42}
43EXPORT_SYMBOL(dma_alloc_noncoherent);
44
45void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
46 dma_addr_t dma_handle)
47{
Vineet Gupta454bfda2015-04-16 21:04:49 +053048 free_pages_exact((void *)dma_handle, size);
Vineet Gupta1162b072013-01-18 15:12:20 +053049}
50EXPORT_SYMBOL(dma_free_noncoherent);
51
52void *dma_alloc_coherent(struct device *dev, size_t size,
53 dma_addr_t *dma_handle, gfp_t gfp)
54{
55 void *paddr, *kvaddr;
56
Alexey Brodkinf2b0b252015-05-25 19:54:28 +030057 /*
58 * IOC relies on all data (even coherent DMA data) being in cache
59 * Thus allocate normal cached memory
60 *
61 * The gains with IOC are two pronged:
62 * -For streaming data, elides needs for cache maintenance, saving
63 * cycles in flush code, and bus bandwidth as all the lines of a
64 * buffer need to be flushed out to memory
65 * -For coherent data, Read/Write to buffers terminate early in cache
66 * (vs. always going to memory - thus are faster)
67 */
Vineet Guptafd0881a22015-08-21 15:06:43 +053068 if (is_isa_arcv2() && ioc_exists)
Alexey Brodkinf2b0b252015-05-25 19:54:28 +030069 return dma_alloc_noncoherent(dev, size, dma_handle, gfp);
70
Vineet Gupta1162b072013-01-18 15:12:20 +053071 /* This is linear addr (0x8000_0000 based) */
72 paddr = alloc_pages_exact(size, gfp);
73 if (!paddr)
74 return NULL;
75
76 /* This is kernel Virtual address (0x7000_0000 based) */
77 kvaddr = ioremap_nocache((unsigned long)paddr, size);
Vineet Guptaf718c2e2015-07-03 10:40:43 +053078 if (kvaddr == NULL)
79 return NULL;
Vineet Gupta1162b072013-01-18 15:12:20 +053080
81 /* This is bus address, platform dependent */
Vineet Gupta454bfda2015-04-16 21:04:49 +053082 *dma_handle = (dma_addr_t)paddr;
Vineet Gupta1162b072013-01-18 15:12:20 +053083
Vineet Gupta795f4552015-04-03 12:37:07 +030084 /*
85 * Evict any existing L1 and/or L2 lines for the backing page
86 * in case it was used earlier as a normal "cached" page.
87 * Yeah this bit us - STAR 9000898266
88 *
89 * Although core does call flush_cache_vmap(), it gets kvaddr hence
90 * can't be used to efficiently flush L1 and/or L2 which need paddr
91 * Currently flush_cache_vmap nukes the L1 cache completely which
92 * will be optimized as a separate commit
93 */
94 dma_cache_wback_inv((unsigned long)paddr, size);
95
Vineet Gupta1162b072013-01-18 15:12:20 +053096 return kvaddr;
97}
98EXPORT_SYMBOL(dma_alloc_coherent);
99
100void dma_free_coherent(struct device *dev, size_t size, void *kvaddr,
101 dma_addr_t dma_handle)
102{
Vineet Guptafd0881a22015-08-21 15:06:43 +0530103 if (is_isa_arcv2() && ioc_exists)
Alexey Brodkinf2b0b252015-05-25 19:54:28 +0300104 return dma_free_noncoherent(dev, size, kvaddr, dma_handle);
105
Vineet Gupta1162b072013-01-18 15:12:20 +0530106 iounmap((void __force __iomem *)kvaddr);
107
Vineet Gupta454bfda2015-04-16 21:04:49 +0530108 free_pages_exact((void *)dma_handle, size);
Vineet Gupta1162b072013-01-18 15:12:20 +0530109}
110EXPORT_SYMBOL(dma_free_coherent);
111
112/*
113 * Helper for streaming DMA...
114 */
115void __arc_dma_cache_sync(unsigned long paddr, size_t size,
116 enum dma_data_direction dir)
117{
118 __inline_dma_cache_sync(paddr, size, dir);
119}
120EXPORT_SYMBOL(__arc_dma_cache_sync);