1 /*
2 * DMA Mapping glue for ARC
3 *
4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #ifndef ASM_ARC_DMA_MAPPING_H
12 #define ASM_ARC_DMA_MAPPING_H
13
14 #include <asm-generic/dma-coherent.h>
15 #include <asm/cacheflush.h>
16
17 void *dma_alloc_noncoherent(struct device *dev, size_t size,
18 dma_addr_t *dma_handle, gfp_t gfp);
19
20 void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
21 dma_addr_t dma_handle);
22
23 void *dma_alloc_coherent(struct device *dev, size_t size,
24 dma_addr_t *dma_handle, gfp_t gfp);
25
26 void dma_free_coherent(struct device *dev, size_t size, void *kvaddr,
27 dma_addr_t dma_handle);
28
29 /* drivers/base/dma-mapping.c */
30 extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
31 void *cpu_addr, dma_addr_t dma_addr, size_t size);
32 extern int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
33 void *cpu_addr, dma_addr_t dma_addr,
34 size_t size);
35
36 #define dma_mmap_coherent(d, v, c, h, s) dma_common_mmap(d, v, c, h, s)
37 #define dma_get_sgtable(d, t, v, h, s) dma_common_get_sgtable(d, t, v, h, s)
38
39 /*
40 * streaming DMA Mapping API...
41 * CPU accesses page via normal paddr, thus needs to explicitly made
42 * consistent before each use
43 */
44
__inline_dma_cache_sync(unsigned long paddr,size_t size,enum dma_data_direction dir)45 static inline void __inline_dma_cache_sync(unsigned long paddr, size_t size,
46 enum dma_data_direction dir)
47 {
48 switch (dir) {
49 case DMA_FROM_DEVICE:
50 dma_cache_inv(paddr, size);
51 break;
52 case DMA_TO_DEVICE:
53 dma_cache_wback(paddr, size);
54 break;
55 case DMA_BIDIRECTIONAL:
56 dma_cache_wback_inv(paddr, size);
57 break;
58 default:
59 pr_err("Invalid DMA dir [%d] for OP @ %lx\n", dir, paddr);
60 }
61 }
62
63 void __arc_dma_cache_sync(unsigned long paddr, size_t size,
64 enum dma_data_direction dir);
65
66 #define _dma_cache_sync(addr, sz, dir) \
67 do { \
68 if (__builtin_constant_p(dir)) \
69 __inline_dma_cache_sync(addr, sz, dir); \
70 else \
71 __arc_dma_cache_sync(addr, sz, dir); \
72 } \
73 while (0);
74
75 static inline dma_addr_t
dma_map_single(struct device * dev,void * cpu_addr,size_t size,enum dma_data_direction dir)76 dma_map_single(struct device *dev, void *cpu_addr, size_t size,
77 enum dma_data_direction dir)
78 {
79 _dma_cache_sync((unsigned long)cpu_addr, size, dir);
80 return (dma_addr_t)cpu_addr;
81 }
82
83 static inline void
dma_unmap_single(struct device * dev,dma_addr_t dma_addr,size_t size,enum dma_data_direction dir)84 dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
85 size_t size, enum dma_data_direction dir)
86 {
87 }
88
89 static inline dma_addr_t
dma_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir)90 dma_map_page(struct device *dev, struct page *page,
91 unsigned long offset, size_t size,
92 enum dma_data_direction dir)
93 {
94 unsigned long paddr = page_to_phys(page) + offset;
95 return dma_map_single(dev, (void *)paddr, size, dir);
96 }
97
98 static inline void
dma_unmap_page(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir)99 dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
100 size_t size, enum dma_data_direction dir)
101 {
102 }
103
104 static inline int
dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)105 dma_map_sg(struct device *dev, struct scatterlist *sg,
106 int nents, enum dma_data_direction dir)
107 {
108 struct scatterlist *s;
109 int i;
110
111 for_each_sg(sg, s, nents, i)
112 s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
113 s->length, dir);
114
115 return nents;
116 }
117
118 static inline void
dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir)119 dma_unmap_sg(struct device *dev, struct scatterlist *sg,
120 int nents, enum dma_data_direction dir)
121 {
122 struct scatterlist *s;
123 int i;
124
125 for_each_sg(sg, s, nents, i)
126 dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir);
127 }
128
129 static inline void
dma_sync_single_for_cpu(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir)130 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
131 size_t size, enum dma_data_direction dir)
132 {
133 _dma_cache_sync(dma_handle, size, DMA_FROM_DEVICE);
134 }
135
136 static inline void
dma_sync_single_for_device(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir)137 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
138 size_t size, enum dma_data_direction dir)
139 {
140 _dma_cache_sync(dma_handle, size, DMA_TO_DEVICE);
141 }
142
143 static inline void
dma_sync_single_range_for_cpu(struct device * dev,dma_addr_t dma_handle,unsigned long offset,size_t size,enum dma_data_direction direction)144 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
145 unsigned long offset, size_t size,
146 enum dma_data_direction direction)
147 {
148 _dma_cache_sync(dma_handle + offset, size, DMA_FROM_DEVICE);
149 }
150
151 static inline void
dma_sync_single_range_for_device(struct device * dev,dma_addr_t dma_handle,unsigned long offset,size_t size,enum dma_data_direction direction)152 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
153 unsigned long offset, size_t size,
154 enum dma_data_direction direction)
155 {
156 _dma_cache_sync(dma_handle + offset, size, DMA_TO_DEVICE);
157 }
158
159 static inline void
dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sglist,int nelems,enum dma_data_direction dir)160 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sglist, int nelems,
161 enum dma_data_direction dir)
162 {
163 int i;
164 struct scatterlist *sg;
165
166 for_each_sg(sglist, sg, nelems, i)
167 _dma_cache_sync((unsigned int)sg_virt(sg), sg->length, dir);
168 }
169
170 static inline void
dma_sync_sg_for_device(struct device * dev,struct scatterlist * sglist,int nelems,enum dma_data_direction dir)171 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sglist,
172 int nelems, enum dma_data_direction dir)
173 {
174 int i;
175 struct scatterlist *sg;
176
177 for_each_sg(sglist, sg, nelems, i)
178 _dma_cache_sync((unsigned int)sg_virt(sg), sg->length, dir);
179 }
180
dma_supported(struct device * dev,u64 dma_mask)181 static inline int dma_supported(struct device *dev, u64 dma_mask)
182 {
183 /* Support 32 bit DMA mask exclusively */
184 return dma_mask == DMA_BIT_MASK(32);
185 }
186
dma_mapping_error(struct device * dev,dma_addr_t dma_addr)187 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
188 {
189 return 0;
190 }
191
dma_set_mask(struct device * dev,u64 dma_mask)192 static inline int dma_set_mask(struct device *dev, u64 dma_mask)
193 {
194 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
195 return -EIO;
196
197 *dev->dma_mask = dma_mask;
198
199 return 0;
200 }
201
202 #endif
203