blob: 57706a9c69489791df22419ec069c1970d1aa634 [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>
22#include <asm/cacheflush.h>
23
24/*
25 * Helpers for Coherent DMA API.
26 */
27void *dma_alloc_noncoherent(struct device *dev, size_t size,
28 dma_addr_t *dma_handle, gfp_t gfp)
29{
30 void *paddr;
31
32 /* This is linear addr (0x8000_0000 based) */
33 paddr = alloc_pages_exact(size, gfp);
34 if (!paddr)
35 return NULL;
36
37 /* This is bus address, platform dependent */
Vineet Gupta454bfda2015-04-16 21:04:49 +053038 *dma_handle = (dma_addr_t)paddr;
Vineet Gupta1162b072013-01-18 15:12:20 +053039
40 return paddr;
41}
42EXPORT_SYMBOL(dma_alloc_noncoherent);
43
44void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
45 dma_addr_t dma_handle)
46{
Vineet Gupta454bfda2015-04-16 21:04:49 +053047 free_pages_exact((void *)dma_handle, size);
Vineet Gupta1162b072013-01-18 15:12:20 +053048}
49EXPORT_SYMBOL(dma_free_noncoherent);
50
51void *dma_alloc_coherent(struct device *dev, size_t size,
52 dma_addr_t *dma_handle, gfp_t gfp)
53{
54 void *paddr, *kvaddr;
55
56 /* This is linear addr (0x8000_0000 based) */
57 paddr = alloc_pages_exact(size, gfp);
58 if (!paddr)
59 return NULL;
60
61 /* This is kernel Virtual address (0x7000_0000 based) */
62 kvaddr = ioremap_nocache((unsigned long)paddr, size);
Vineet Guptaf718c2e2015-07-03 10:40:43 +053063 if (kvaddr == NULL)
64 return NULL;
Vineet Gupta1162b072013-01-18 15:12:20 +053065
66 /* This is bus address, platform dependent */
Vineet Gupta454bfda2015-04-16 21:04:49 +053067 *dma_handle = (dma_addr_t)paddr;
Vineet Gupta1162b072013-01-18 15:12:20 +053068
Vineet Gupta795f4552015-04-03 12:37:07 +030069 /*
70 * Evict any existing L1 and/or L2 lines for the backing page
71 * in case it was used earlier as a normal "cached" page.
72 * Yeah this bit us - STAR 9000898266
73 *
74 * Although core does call flush_cache_vmap(), it gets kvaddr hence
75 * can't be used to efficiently flush L1 and/or L2 which need paddr
76 * Currently flush_cache_vmap nukes the L1 cache completely which
77 * will be optimized as a separate commit
78 */
79 dma_cache_wback_inv((unsigned long)paddr, size);
80
Vineet Gupta1162b072013-01-18 15:12:20 +053081 return kvaddr;
82}
83EXPORT_SYMBOL(dma_alloc_coherent);
84
85void dma_free_coherent(struct device *dev, size_t size, void *kvaddr,
86 dma_addr_t dma_handle)
87{
88 iounmap((void __force __iomem *)kvaddr);
89
Vineet Gupta454bfda2015-04-16 21:04:49 +053090 free_pages_exact((void *)dma_handle, size);
Vineet Gupta1162b072013-01-18 15:12:20 +053091}
92EXPORT_SYMBOL(dma_free_coherent);
93
94/*
95 * Helper for streaming DMA...
96 */
97void __arc_dma_cache_sync(unsigned long paddr, size_t size,
98 enum dma_data_direction dir)
99{
100 __inline_dma_cache_sync(paddr, size, dir);
101}
102EXPORT_SYMBOL(__arc_dma_cache_sync);