aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLey Foon Tan <lftan@altera.com>2014-11-06 02:19:53 -0500
committerLey Foon Tan <lftan@altera.com>2014-12-07 23:55:56 -0500
commite23c621f983c576af48d4976ad1ed0d0eca2bd0e (patch)
tree409abc6f081d98c7da19f3c29409306c7f68beed
parentf27ffc751ca5d216a347084996c70452a4e185a4 (diff)
nios2: DMA mapping API
This patch adds support for the DMA mapping API. Signed-off-by: Ley Foon Tan <lftan@altera.com>
-rw-r--r--arch/nios2/include/asm/dma-mapping.h140
-rw-r--r--arch/nios2/mm/dma-mapping.c186
2 files changed, 326 insertions, 0 deletions
diff --git a/arch/nios2/include/asm/dma-mapping.h b/arch/nios2/include/asm/dma-mapping.h
new file mode 100644
index 000000000000..b5567233f7f1
--- /dev/null
+++ b/arch/nios2/include/asm/dma-mapping.h
@@ -0,0 +1,140 @@
1/*
2 * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
3 * Copyright (C) 2009 Wind River Systems Inc
4 *
5 * This file is subject to the terms and conditions of the GNU General
6 * Public License. See the file COPYING in the main directory of this
7 * archive for more details.
8 */
9
10#ifndef _ASM_NIOS2_DMA_MAPPING_H
11#define _ASM_NIOS2_DMA_MAPPING_H
12
13#include <linux/scatterlist.h>
14#include <linux/cache.h>
15#include <asm/cacheflush.h>
16
17static inline void __dma_sync_for_device(void *vaddr, size_t size,
18 enum dma_data_direction direction)
19{
20 switch (direction) {
21 case DMA_FROM_DEVICE:
22 invalidate_dcache_range((unsigned long)vaddr,
23 (unsigned long)(vaddr + size));
24 break;
25 case DMA_TO_DEVICE:
26 /*
27 * We just need to flush the caches here , but Nios2 flush
28 * instruction will do both writeback and invalidate.
29 */
30 case DMA_BIDIRECTIONAL: /* flush and invalidate */
31 flush_dcache_range((unsigned long)vaddr,
32 (unsigned long)(vaddr + size));
33 break;
34 default:
35 BUG();
36 }
37}
38
39static inline void __dma_sync_for_cpu(void *vaddr, size_t size,
40 enum dma_data_direction direction)
41{
42 switch (direction) {
43 case DMA_BIDIRECTIONAL:
44 case DMA_FROM_DEVICE:
45 invalidate_dcache_range((unsigned long)vaddr,
46 (unsigned long)(vaddr + size));
47 break;
48 case DMA_TO_DEVICE:
49 break;
50 default:
51 BUG();
52 }
53}
54
55#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
56#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
57
58void *dma_alloc_coherent(struct device *dev, size_t size,
59 dma_addr_t *dma_handle, gfp_t flag);
60
61void dma_free_coherent(struct device *dev, size_t size,
62 void *vaddr, dma_addr_t dma_handle);
63
64static inline dma_addr_t dma_map_single(struct device *dev, void *ptr,
65 size_t size,
66 enum dma_data_direction direction)
67{
68 BUG_ON(!valid_dma_direction(direction));
69 __dma_sync_for_device(ptr, size, direction);
70 return virt_to_phys(ptr);
71}
72
73static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
74 size_t size, enum dma_data_direction direction)
75{
76}
77
78extern int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
79 enum dma_data_direction direction);
80extern dma_addr_t dma_map_page(struct device *dev, struct page *page,
81 unsigned long offset, size_t size, enum dma_data_direction direction);
82extern void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
83 size_t size, enum dma_data_direction direction);
84extern void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
85 int nhwentries, enum dma_data_direction direction);
86extern void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
87 size_t size, enum dma_data_direction direction);
88extern void dma_sync_single_for_device(struct device *dev,
89 dma_addr_t dma_handle, size_t size, enum dma_data_direction direction);
90extern void dma_sync_single_range_for_cpu(struct device *dev,
91 dma_addr_t dma_handle, unsigned long offset, size_t size,
92 enum dma_data_direction direction);
93extern void dma_sync_single_range_for_device(struct device *dev,
94 dma_addr_t dma_handle, unsigned long offset, size_t size,
95 enum dma_data_direction direction);
96extern void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
97 int nelems, enum dma_data_direction direction);
98extern void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
99 int nelems, enum dma_data_direction direction);
100
101static inline int dma_supported(struct device *dev, u64 mask)
102{
103 return 1;
104}
105
106static inline int dma_set_mask(struct device *dev, u64 mask)
107{
108 if (!dev->dma_mask || !dma_supported(dev, mask))
109 return -EIO;
110
111 *dev->dma_mask = mask;
112
113 return 0;
114}
115
116static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
117{
118 return 0;
119}
120
121/*
122* dma_alloc_noncoherent() returns non-cacheable memory, so there's no need to
123* do any flushing here.
124*/
125static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
126 enum dma_data_direction direction)
127{
128}
129
130/* drivers/base/dma-mapping.c */
131extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
132 void *cpu_addr, dma_addr_t dma_addr, size_t size);
133extern int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
134 void *cpu_addr, dma_addr_t dma_addr,
135 size_t size);
136
137#define dma_mmap_coherent(d, v, c, h, s) dma_common_mmap(d, v, c, h, s)
138#define dma_get_sgtable(d, t, v, h, s) dma_common_get_sgtable(d, t, v, h, s)
139
140#endif /* _ASM_NIOS2_DMA_MAPPING_H */
diff --git a/arch/nios2/mm/dma-mapping.c b/arch/nios2/mm/dma-mapping.c
new file mode 100644
index 000000000000..ac5da7594f0b
--- /dev/null
+++ b/arch/nios2/mm/dma-mapping.c
@@ -0,0 +1,186 @@
1/*
2 * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
3 * Copyright (C) 2009 Wind River Systems Inc
4 * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
5 *
6 * Based on DMA code from MIPS.
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
11 */
12
13#include <linux/types.h>
14#include <linux/mm.h>
15#include <linux/export.h>
16#include <linux/string.h>
17#include <linux/scatterlist.h>
18#include <linux/dma-mapping.h>
19#include <linux/io.h>
20#include <linux/cache.h>
21#include <asm/cacheflush.h>
22
23
24void *dma_alloc_coherent(struct device *dev, size_t size,
25 dma_addr_t *dma_handle, gfp_t gfp)
26{
27 void *ret;
28
29 /* ignore region specifiers */
30 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
31
32 /* optimized page clearing */
33 gfp |= __GFP_ZERO;
34
35 if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
36 gfp |= GFP_DMA;
37
38 ret = (void *) __get_free_pages(gfp, get_order(size));
39 if (ret != NULL) {
40 *dma_handle = virt_to_phys(ret);
41 flush_dcache_range((unsigned long) ret,
42 (unsigned long) ret + size);
43 ret = UNCAC_ADDR(ret);
44 }
45
46 return ret;
47}
48EXPORT_SYMBOL(dma_alloc_coherent);
49
50void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
51 dma_addr_t dma_handle)
52{
53 unsigned long addr = (unsigned long) CAC_ADDR((unsigned long) vaddr);
54
55 free_pages(addr, get_order(size));
56}
57EXPORT_SYMBOL(dma_free_coherent);
58
59int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
60 enum dma_data_direction direction)
61{
62 int i;
63
64 BUG_ON(!valid_dma_direction(direction));
65
66 for_each_sg(sg, sg, nents, i) {
67 void *addr;
68
69 addr = sg_virt(sg);
70 if (addr) {
71 __dma_sync_for_device(addr, sg->length, direction);
72 sg->dma_address = sg_phys(sg);
73 }
74 }
75
76 return nents;
77}
78EXPORT_SYMBOL(dma_map_sg);
79
80dma_addr_t dma_map_page(struct device *dev, struct page *page,
81 unsigned long offset, size_t size,
82 enum dma_data_direction direction)
83{
84 void *addr;
85
86 BUG_ON(!valid_dma_direction(direction));
87
88 addr = page_address(page) + offset;
89 __dma_sync_for_device(addr, size, direction);
90
91 return page_to_phys(page) + offset;
92}
93EXPORT_SYMBOL(dma_map_page);
94
95void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
96 enum dma_data_direction direction)
97{
98 BUG_ON(!valid_dma_direction(direction));
99
100 __dma_sync_for_cpu(phys_to_virt(dma_address), size, direction);
101}
102EXPORT_SYMBOL(dma_unmap_page);
103
104void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
105 enum dma_data_direction direction)
106{
107 void *addr;
108 int i;
109
110 BUG_ON(!valid_dma_direction(direction));
111
112 if (direction == DMA_TO_DEVICE)
113 return;
114
115 for_each_sg(sg, sg, nhwentries, i) {
116 addr = sg_virt(sg);
117 if (addr)
118 __dma_sync_for_cpu(addr, sg->length, direction);
119 }
120}
121EXPORT_SYMBOL(dma_unmap_sg);
122
123void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
124 size_t size, enum dma_data_direction direction)
125{
126 BUG_ON(!valid_dma_direction(direction));
127
128 __dma_sync_for_cpu(phys_to_virt(dma_handle), size, direction);
129}
130EXPORT_SYMBOL(dma_sync_single_for_cpu);
131
132void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
133 size_t size, enum dma_data_direction direction)
134{
135 BUG_ON(!valid_dma_direction(direction));
136
137 __dma_sync_for_device(phys_to_virt(dma_handle), size, direction);
138}
139EXPORT_SYMBOL(dma_sync_single_for_device);
140
141void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
142 unsigned long offset, size_t size,
143 enum dma_data_direction direction)
144{
145 BUG_ON(!valid_dma_direction(direction));
146
147 __dma_sync_for_cpu(phys_to_virt(dma_handle), size, direction);
148}
149EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
150
151void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
152 unsigned long offset, size_t size,
153 enum dma_data_direction direction)
154{
155 BUG_ON(!valid_dma_direction(direction));
156
157 __dma_sync_for_device(phys_to_virt(dma_handle), size, direction);
158}
159EXPORT_SYMBOL(dma_sync_single_range_for_device);
160
161void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
162 enum dma_data_direction direction)
163{
164 int i;
165
166 BUG_ON(!valid_dma_direction(direction));
167
168 /* Make sure that gcc doesn't leave the empty loop body. */
169 for_each_sg(sg, sg, nelems, i)
170 __dma_sync_for_cpu(sg_virt(sg), sg->length, direction);
171}
172EXPORT_SYMBOL(dma_sync_sg_for_cpu);
173
174void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
175 int nelems, enum dma_data_direction direction)
176{
177 int i;
178
179 BUG_ON(!valid_dma_direction(direction));
180
181 /* Make sure that gcc doesn't leave the empty loop body. */
182 for_each_sg(sg, sg, nelems, i)
183 __dma_sync_for_device(sg_virt(sg), sg->length, direction);
184
185}
186EXPORT_SYMBOL(dma_sync_sg_for_device);