GNU Linux-libre 4.4.297-gnu1
[releases.git] / arch / parisc / include / asm / dma-mapping.h
1 #ifndef _PARISC_DMA_MAPPING_H
2 #define _PARISC_DMA_MAPPING_H
3
4 #include <linux/mm.h>
5 #include <linux/scatterlist.h>
6 #include <asm/cacheflush.h>
7
8 /* See Documentation/DMA-API-HOWTO.txt */
9 struct hppa_dma_ops {
10         int  (*dma_supported)(struct device *dev, u64 mask);
11         void *(*alloc_consistent)(struct device *dev, size_t size, dma_addr_t *iova, gfp_t flag);
12         void *(*alloc_noncoherent)(struct device *dev, size_t size, dma_addr_t *iova, gfp_t flag);
13         void (*free_consistent)(struct device *dev, size_t size, void *vaddr, dma_addr_t iova);
14         dma_addr_t (*map_single)(struct device *dev, void *addr, size_t size, enum dma_data_direction direction);
15         void (*unmap_single)(struct device *dev, dma_addr_t iova, size_t size, enum dma_data_direction direction);
16         int  (*map_sg)(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction direction);
17         void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nhwents, enum dma_data_direction direction);
18         void (*dma_sync_single_for_cpu)(struct device *dev, dma_addr_t iova, unsigned long offset, size_t size, enum dma_data_direction direction);
19         void (*dma_sync_single_for_device)(struct device *dev, dma_addr_t iova, unsigned long offset, size_t size, enum dma_data_direction direction);
20         void (*dma_sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, int nelems, enum dma_data_direction direction);
21         void (*dma_sync_sg_for_device)(struct device *dev, struct scatterlist *sg, int nelems, enum dma_data_direction direction);
22 };
23
24 /*
25 ** We could live without the hppa_dma_ops indirection if we didn't want
26 ** to support 4 different coherent dma models with one binary (they will
27 ** someday be loadable modules):
28 **     I/O MMU        consistent method           dma_sync behavior
29 **  =============   ======================       =======================
30 **  a) PA-7x00LC    uncachable host memory          flush/purge
31 **  b) U2/Uturn      cachable host memory              NOP
32 **  c) Ike/Astro     cachable host memory              NOP
33 **  d) EPIC/SAGA     memory on EPIC/SAGA         flush/reset DMA channel
34 **
35 ** PA-7[13]00LC processors have a GSC bus interface and no I/O MMU.
36 **
37 ** Systems (eg PCX-T workstations) that don't fall into the above
38 ** categories will need to modify the needed drivers to perform
39 ** flush/purge and allocate "regular" cacheable pages for everything.
40 */
41
42 #define DMA_ERROR_CODE  (~(dma_addr_t)0)
43
44 #ifdef CONFIG_PA11
45 extern struct hppa_dma_ops pcxl_dma_ops;
46 extern struct hppa_dma_ops pcx_dma_ops;
47 #endif
48
49 extern struct hppa_dma_ops *hppa_dma_ops;
50
51 #define dma_alloc_attrs(d, s, h, f, a) dma_alloc_coherent(d, s, h, f)
52 #define dma_free_attrs(d, s, h, f, a) dma_free_coherent(d, s, h, f)
53
54 static inline void *
55 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
56                    gfp_t flag)
57 {
58         return hppa_dma_ops->alloc_consistent(dev, size, dma_handle, flag);
59 }
60
61 static inline void *
62 dma_alloc_noncoherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
63                       gfp_t flag)
64 {
65         return hppa_dma_ops->alloc_noncoherent(dev, size, dma_handle, flag);
66 }
67
68 static inline void
69 dma_free_coherent(struct device *dev, size_t size, 
70                     void *vaddr, dma_addr_t dma_handle)
71 {
72         hppa_dma_ops->free_consistent(dev, size, vaddr, dma_handle);
73 }
74
75 static inline void
76 dma_free_noncoherent(struct device *dev, size_t size, 
77                     void *vaddr, dma_addr_t dma_handle)
78 {
79         hppa_dma_ops->free_consistent(dev, size, vaddr, dma_handle);
80 }
81
82 static inline dma_addr_t
83 dma_map_single(struct device *dev, void *ptr, size_t size,
84                enum dma_data_direction direction)
85 {
86         return hppa_dma_ops->map_single(dev, ptr, size, direction);
87 }
88
89 static inline void
90 dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
91                  enum dma_data_direction direction)
92 {
93         hppa_dma_ops->unmap_single(dev, dma_addr, size, direction);
94 }
95
96 static inline int
97 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
98            enum dma_data_direction direction)
99 {
100         return hppa_dma_ops->map_sg(dev, sg, nents, direction);
101 }
102
103 static inline void
104 dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
105              enum dma_data_direction direction)
106 {
107         hppa_dma_ops->unmap_sg(dev, sg, nhwentries, direction);
108 }
109
110 static inline dma_addr_t
111 dma_map_page(struct device *dev, struct page *page, unsigned long offset,
112              size_t size, enum dma_data_direction direction)
113 {
114         return dma_map_single(dev, (page_address(page) + (offset)), size, direction);
115 }
116
117 static inline void
118 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
119                enum dma_data_direction direction)
120 {
121         dma_unmap_single(dev, dma_address, size, direction);
122 }
123
124
125 static inline void
126 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
127                 enum dma_data_direction direction)
128 {
129         if(hppa_dma_ops->dma_sync_single_for_cpu)
130                 hppa_dma_ops->dma_sync_single_for_cpu(dev, dma_handle, 0, size, direction);
131 }
132
133 static inline void
134 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, size_t size,
135                 enum dma_data_direction direction)
136 {
137         if(hppa_dma_ops->dma_sync_single_for_device)
138                 hppa_dma_ops->dma_sync_single_for_device(dev, dma_handle, 0, size, direction);
139 }
140
141 static inline void
142 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
143                       unsigned long offset, size_t size,
144                       enum dma_data_direction direction)
145 {
146         if(hppa_dma_ops->dma_sync_single_for_cpu)
147                 hppa_dma_ops->dma_sync_single_for_cpu(dev, dma_handle, offset, size, direction);
148 }
149
150 static inline void
151 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         if(hppa_dma_ops->dma_sync_single_for_device)
156                 hppa_dma_ops->dma_sync_single_for_device(dev, dma_handle, offset, size, direction);
157 }
158
159 static inline void
160 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
161                  enum dma_data_direction direction)
162 {
163         if(hppa_dma_ops->dma_sync_sg_for_cpu)
164                 hppa_dma_ops->dma_sync_sg_for_cpu(dev, sg, nelems, direction);
165 }
166
167 static inline void
168 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
169                  enum dma_data_direction direction)
170 {
171         if(hppa_dma_ops->dma_sync_sg_for_device)
172                 hppa_dma_ops->dma_sync_sg_for_device(dev, sg, nelems, direction);
173 }
174
175 static inline int
176 dma_supported(struct device *dev, u64 mask)
177 {
178         return hppa_dma_ops->dma_supported(dev, mask);
179 }
180
181 static inline int
182 dma_set_mask(struct device *dev, u64 mask)
183 {
184         if(!dev->dma_mask || !dma_supported(dev, mask))
185                 return -EIO;
186
187         *dev->dma_mask = mask;
188
189         return 0;
190 }
191
192 static inline void
193 dma_cache_sync(struct device *dev, void *vaddr, size_t size,
194                enum dma_data_direction direction)
195 {
196         if(hppa_dma_ops->dma_sync_single_for_cpu)
197                 flush_kernel_dcache_range((unsigned long)vaddr, size);
198 }
199
200 static inline void *
201 parisc_walk_tree(struct device *dev)
202 {
203         struct device *otherdev;
204         if(likely(dev->platform_data != NULL))
205                 return dev->platform_data;
206         /* OK, just traverse the bus to find it */
207         for(otherdev = dev->parent; otherdev;
208             otherdev = otherdev->parent) {
209                 if(otherdev->platform_data) {
210                         dev->platform_data = otherdev->platform_data;
211                         break;
212                 }
213         }
214         return dev->platform_data;
215 }
216
217 #define GET_IOC(dev) ({                                 \
218         void *__pdata = parisc_walk_tree(dev);          \
219         __pdata ? HBA_DATA(__pdata)->iommu : NULL;      \
220 })
221
222 #ifdef CONFIG_IOMMU_CCIO
223 struct parisc_device;
224 struct ioc;
225 void * ccio_get_iommu(const struct parisc_device *dev);
226 int ccio_request_resource(const struct parisc_device *dev,
227                 struct resource *res);
228 int ccio_allocate_resource(const struct parisc_device *dev,
229                 struct resource *res, unsigned long size,
230                 unsigned long min, unsigned long max, unsigned long align);
231 #else /* !CONFIG_IOMMU_CCIO */
232 #define ccio_get_iommu(dev) NULL
233 #define ccio_request_resource(dev, res) insert_resource(&iomem_resource, res)
234 #define ccio_allocate_resource(dev, res, size, min, max, align) \
235                 allocate_resource(&iomem_resource, res, size, min, max, \
236                                 align, NULL, NULL)
237 #endif /* !CONFIG_IOMMU_CCIO */
238
239 #ifdef CONFIG_IOMMU_SBA
240 struct parisc_device;
241 void * sba_get_iommu(struct parisc_device *dev);
242 #endif
243
244 /* At the moment, we panic on error for IOMMU resource exaustion */
245 #define dma_mapping_error(dev, x)       0
246
247 /* This API cannot be supported on PA-RISC */
248 static inline int dma_mmap_coherent(struct device *dev,
249                                     struct vm_area_struct *vma, void *cpu_addr,
250                                     dma_addr_t dma_addr, size_t size)
251 {
252         return -EINVAL;
253 }
254
255 static inline int dma_get_sgtable(struct device *dev, struct sg_table *sgt,
256                                   void *cpu_addr, dma_addr_t dma_addr,
257                                   size_t size)
258 {
259         return -EINVAL;
260 }
261
262 #endif