blob: 2cfe81dca92a52a3a54f710f49de0a002a2ef8ed [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);
63 if (kvaddr != NULL)
64 memset(kvaddr, 0, size);
65
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
69 return kvaddr;
70}
71EXPORT_SYMBOL(dma_alloc_coherent);
72
73void dma_free_coherent(struct device *dev, size_t size, void *kvaddr,
74 dma_addr_t dma_handle)
75{
76 iounmap((void __force __iomem *)kvaddr);
77
Vineet Gupta454bfda2015-04-16 21:04:49 +053078 free_pages_exact((void *)dma_handle, size);
Vineet Gupta1162b072013-01-18 15:12:20 +053079}
80EXPORT_SYMBOL(dma_free_coherent);
81
82/*
83 * Helper for streaming DMA...
84 */
85void __arc_dma_cache_sync(unsigned long paddr, size_t size,
86 enum dma_data_direction dir)
87{
88 __inline_dma_cache_sync(paddr, size, dir);
89}
90EXPORT_SYMBOL(__arc_dma_cache_sync);