blob: c69f2b5f0cc40035877ccea1f363c3de6711fc61 [file] [log] [blame]
Stephen Rothwell78b09732005-11-19 01:40:46 +11001/*
2 * Copyright (C) 2004 IBM
3 *
4 * Implements the generic device dma API for powerpc.
5 * the pci and vio busses
6 */
7#ifndef _ASM_DMA_MAPPING_H
8#define _ASM_DMA_MAPPING_H
Anton Blanchard33ff9102007-10-16 14:54:33 -05009#ifdef __KERNEL__
10
11#include <linux/types.h>
12#include <linux/cache.h>
13/* need struct page definitions */
14#include <linux/mm.h>
15#include <linux/scatterlist.h>
Mark Nelson3affedc2008-07-05 05:05:42 +100016#include <linux/dma-attrs.h>
Anton Blanchard33ff9102007-10-16 14:54:33 -050017#include <asm/io.h>
18
19#define DMA_ERROR_CODE (~(dma_addr_t)0x0)
20
21#ifdef CONFIG_NOT_COHERENT_CACHE
22/*
23 * DMA-consistent mapping functions for PowerPCs that don't support
24 * cache snooping. These allocate/free a region of uncached mapped
25 * memory space for use with DMA devices. Alternatively, you could
26 * allocate the space "normally" and use the cache management functions
27 * to ensure it is consistent.
28 */
29extern void *__dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp);
30extern void __dma_free_coherent(size_t size, void *vaddr);
31extern void __dma_sync(void *vaddr, size_t size, int direction);
32extern void __dma_sync_page(struct page *page, unsigned long offset,
33 size_t size, int direction);
34
35#else /* ! CONFIG_NOT_COHERENT_CACHE */
36/*
37 * Cache coherent cores.
38 */
39
40#define __dma_alloc_coherent(gfp, size, handle) NULL
41#define __dma_free_coherent(size, addr) ((void)0)
42#define __dma_sync(addr, size, rw) ((void)0)
43#define __dma_sync_page(pg, off, sz, rw) ((void)0)
44
45#endif /* ! CONFIG_NOT_COHERENT_CACHE */
46
Mark Nelson3a4c6f02008-07-05 05:05:45 +100047static inline unsigned long device_to_mask(struct device *dev)
48{
49 if (dev->dma_mask && *dev->dma_mask)
50 return *dev->dma_mask;
51 /* Assume devices without mask can take 32 bit addresses */
52 return 0xfffffffful;
53}
54
Anton Blanchard33ff9102007-10-16 14:54:33 -050055/*
56 * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
57 */
58struct dma_mapping_ops {
59 void * (*alloc_coherent)(struct device *dev, size_t size,
60 dma_addr_t *dma_handle, gfp_t flag);
61 void (*free_coherent)(struct device *dev, size_t size,
62 void *vaddr, dma_addr_t dma_handle);
Anton Blanchard33ff9102007-10-16 14:54:33 -050063 int (*map_sg)(struct device *dev, struct scatterlist *sg,
Mark Nelson3affedc2008-07-05 05:05:42 +100064 int nents, enum dma_data_direction direction,
65 struct dma_attrs *attrs);
Anton Blanchard33ff9102007-10-16 14:54:33 -050066 void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
Mark Nelson3affedc2008-07-05 05:05:42 +100067 int nents, enum dma_data_direction direction,
68 struct dma_attrs *attrs);
Anton Blanchard33ff9102007-10-16 14:54:33 -050069 int (*dma_supported)(struct device *dev, u64 mask);
70 int (*set_dma_mask)(struct device *dev, u64 dma_mask);
Becky Bruce4fc665b2008-09-12 10:34:46 +000071 dma_addr_t (*map_page)(struct device *dev, struct page *page,
72 unsigned long offset, size_t size,
73 enum dma_data_direction direction,
74 struct dma_attrs *attrs);
75 void (*unmap_page)(struct device *dev,
76 dma_addr_t dma_address, size_t size,
77 enum dma_data_direction direction,
78 struct dma_attrs *attrs);
Becky Bruce15e09c02008-11-20 06:49:16 +000079#ifdef CONFIG_PPC_NEED_DMA_SYNC_OPS
80 void (*sync_single_range_for_cpu)(struct device *hwdev,
81 dma_addr_t dma_handle, unsigned long offset,
82 size_t size,
83 enum dma_data_direction direction);
84 void (*sync_single_range_for_device)(struct device *hwdev,
85 dma_addr_t dma_handle, unsigned long offset,
86 size_t size,
87 enum dma_data_direction direction);
88 void (*sync_sg_for_cpu)(struct device *hwdev,
89 struct scatterlist *sg, int nelems,
90 enum dma_data_direction direction);
91 void (*sync_sg_for_device)(struct device *hwdev,
92 struct scatterlist *sg, int nelems,
93 enum dma_data_direction direction);
94#endif
Anton Blanchard33ff9102007-10-16 14:54:33 -050095};
96
Becky Bruce4fc665b2008-09-12 10:34:46 +000097/*
98 * Available generic sets of operations
99 */
100#ifdef CONFIG_PPC64
101extern struct dma_mapping_ops dma_iommu_ops;
102#endif
103extern struct dma_mapping_ops dma_direct_ops;
104
Anton Blanchard33ff9102007-10-16 14:54:33 -0500105static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
106{
107 /* We don't handle the NULL dev case for ISA for now. We could
108 * do it via an out of line call but it is not needed for now. The
109 * only ISA DMA device we support is the floppy and we have a hack
110 * in the floppy driver directly to get a device for us.
111 */
Kumar Gala4ae0ff62009-03-19 03:40:52 +0000112 if (unlikely(dev == NULL))
Anton Blanchard33ff9102007-10-16 14:54:33 -0500113 return NULL;
Becky Bruce4fc665b2008-09-12 10:34:46 +0000114
Anton Blanchard33ff9102007-10-16 14:54:33 -0500115 return dev->archdata.dma_ops;
116}
117
Michael Ellerman1f62a162008-01-30 01:13:58 +1100118static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
119{
120 dev->archdata.dma_ops = ops;
121}
122
Anton Blanchard33ff9102007-10-16 14:54:33 -0500123static inline int dma_supported(struct device *dev, u64 mask)
124{
125 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
126
127 if (unlikely(dma_ops == NULL))
128 return 0;
129 if (dma_ops->dma_supported == NULL)
130 return 1;
131 return dma_ops->dma_supported(dev, mask);
132}
133
Michael Ellerman84631f32007-12-17 17:35:53 +1100134/* We have our own implementation of pci_set_dma_mask() */
135#define HAVE_ARCH_PCI_SET_DMA_MASK
136
Anton Blanchard33ff9102007-10-16 14:54:33 -0500137static inline int dma_set_mask(struct device *dev, u64 dma_mask)
138{
139 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
140
141 if (unlikely(dma_ops == NULL))
142 return -EIO;
143 if (dma_ops->set_dma_mask != NULL)
144 return dma_ops->set_dma_mask(dev, dma_mask);
145 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
146 return -EIO;
147 *dev->dma_mask = dma_mask;
148 return 0;
149}
150
Becky Bruce4fc665b2008-09-12 10:34:46 +0000151/*
Mark Nelsonc73049f2008-10-27 20:38:14 +0000152 * map_/unmap_single actually call through to map/unmap_page now that all the
153 * dma_mapping_ops have been converted over. We just have to get the page and
154 * offset to pass through to map_page
Becky Bruce4fc665b2008-09-12 10:34:46 +0000155 */
Mark Nelson3affedc2008-07-05 05:05:42 +1000156static inline dma_addr_t dma_map_single_attrs(struct device *dev,
157 void *cpu_addr,
158 size_t size,
159 enum dma_data_direction direction,
160 struct dma_attrs *attrs)
161{
162 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
163
164 BUG_ON(!dma_ops);
Becky Bruce4fc665b2008-09-12 10:34:46 +0000165
Becky Bruce4fc665b2008-09-12 10:34:46 +0000166 return dma_ops->map_page(dev, virt_to_page(cpu_addr),
167 (unsigned long)cpu_addr % PAGE_SIZE, size,
168 direction, attrs);
Mark Nelson3affedc2008-07-05 05:05:42 +1000169}
170
171static inline void dma_unmap_single_attrs(struct device *dev,
172 dma_addr_t dma_addr,
173 size_t size,
174 enum dma_data_direction direction,
175 struct dma_attrs *attrs)
176{
177 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
178
179 BUG_ON(!dma_ops);
Becky Bruce4fc665b2008-09-12 10:34:46 +0000180
Becky Bruce4fc665b2008-09-12 10:34:46 +0000181 dma_ops->unmap_page(dev, dma_addr, size, direction, attrs);
Mark Nelson3affedc2008-07-05 05:05:42 +1000182}
183
184static inline dma_addr_t dma_map_page_attrs(struct device *dev,
185 struct page *page,
186 unsigned long offset, size_t size,
187 enum dma_data_direction direction,
188 struct dma_attrs *attrs)
189{
190 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
191
192 BUG_ON(!dma_ops);
Becky Bruce4fc665b2008-09-12 10:34:46 +0000193
Mark Nelsonc73049f2008-10-27 20:38:14 +0000194 return dma_ops->map_page(dev, page, offset, size, direction, attrs);
Mark Nelson3affedc2008-07-05 05:05:42 +1000195}
196
197static inline void dma_unmap_page_attrs(struct device *dev,
198 dma_addr_t dma_address,
199 size_t size,
200 enum dma_data_direction direction,
201 struct dma_attrs *attrs)
202{
203 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
204
205 BUG_ON(!dma_ops);
Becky Bruce4fc665b2008-09-12 10:34:46 +0000206
Mark Nelsonc73049f2008-10-27 20:38:14 +0000207 dma_ops->unmap_page(dev, dma_address, size, direction, attrs);
Mark Nelson3affedc2008-07-05 05:05:42 +1000208}
209
210static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
211 int nents, enum dma_data_direction direction,
212 struct dma_attrs *attrs)
213{
214 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
215
216 BUG_ON(!dma_ops);
217 return dma_ops->map_sg(dev, sg, nents, direction, attrs);
218}
219
220static inline void dma_unmap_sg_attrs(struct device *dev,
221 struct scatterlist *sg,
222 int nhwentries,
223 enum dma_data_direction direction,
224 struct dma_attrs *attrs)
225{
226 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
227
228 BUG_ON(!dma_ops);
229 dma_ops->unmap_sg(dev, sg, nhwentries, direction, attrs);
230}
231
Anton Blanchard33ff9102007-10-16 14:54:33 -0500232static inline void *dma_alloc_coherent(struct device *dev, size_t size,
233 dma_addr_t *dma_handle, gfp_t flag)
234{
235 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
236
237 BUG_ON(!dma_ops);
238 return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
239}
240
241static inline void dma_free_coherent(struct device *dev, size_t size,
242 void *cpu_addr, dma_addr_t dma_handle)
243{
244 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
245
246 BUG_ON(!dma_ops);
247 dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
248}
249
250static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
251 size_t size,
252 enum dma_data_direction direction)
253{
Mark Nelson3affedc2008-07-05 05:05:42 +1000254 return dma_map_single_attrs(dev, cpu_addr, size, direction, NULL);
Anton Blanchard33ff9102007-10-16 14:54:33 -0500255}
256
257static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
258 size_t size,
259 enum dma_data_direction direction)
260{
Mark Nelson3affedc2008-07-05 05:05:42 +1000261 dma_unmap_single_attrs(dev, dma_addr, size, direction, NULL);
Anton Blanchard33ff9102007-10-16 14:54:33 -0500262}
263
264static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
265 unsigned long offset, size_t size,
266 enum dma_data_direction direction)
267{
Mark Nelson3affedc2008-07-05 05:05:42 +1000268 return dma_map_page_attrs(dev, page, offset, size, direction, NULL);
Anton Blanchard33ff9102007-10-16 14:54:33 -0500269}
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100270
271static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
272 size_t size,
273 enum dma_data_direction direction)
274{
Mark Nelson3affedc2008-07-05 05:05:42 +1000275 dma_unmap_page_attrs(dev, dma_address, size, direction, NULL);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100276}
277
278static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
279 int nents, enum dma_data_direction direction)
280{
Mark Nelson3affedc2008-07-05 05:05:42 +1000281 return dma_map_sg_attrs(dev, sg, nents, direction, NULL);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100282}
283
284static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
285 int nhwentries,
286 enum dma_data_direction direction)
287{
Mark Nelson3affedc2008-07-05 05:05:42 +1000288 dma_unmap_sg_attrs(dev, sg, nhwentries, direction, NULL);
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +1100289}
290
Becky Bruce15e09c02008-11-20 06:49:16 +0000291#ifdef CONFIG_PPC_NEED_DMA_SYNC_OPS
Stephen Rothwell78b09732005-11-19 01:40:46 +1100292static inline void dma_sync_single_for_cpu(struct device *dev,
293 dma_addr_t dma_handle, size_t size,
294 enum dma_data_direction direction)
295{
Becky Bruce15e09c02008-11-20 06:49:16 +0000296 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
297
298 BUG_ON(!dma_ops);
299 dma_ops->sync_single_range_for_cpu(dev, dma_handle, 0,
300 size, direction);
Stephen Rothwell78b09732005-11-19 01:40:46 +1100301}
302
303static inline void dma_sync_single_for_device(struct device *dev,
304 dma_addr_t dma_handle, size_t size,
305 enum dma_data_direction direction)
306{
Becky Bruce15e09c02008-11-20 06:49:16 +0000307 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
308
309 BUG_ON(!dma_ops);
310 dma_ops->sync_single_range_for_device(dev, dma_handle,
311 0, size, direction);
Stephen Rothwell78b09732005-11-19 01:40:46 +1100312}
313
314static inline void dma_sync_sg_for_cpu(struct device *dev,
Jens Axboe78bdc312007-10-12 13:44:12 +0200315 struct scatterlist *sgl, int nents,
Stephen Rothwell78b09732005-11-19 01:40:46 +1100316 enum dma_data_direction direction)
317{
Becky Bruce15e09c02008-11-20 06:49:16 +0000318 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
Stephen Rothwell78b09732005-11-19 01:40:46 +1100319
Becky Bruce15e09c02008-11-20 06:49:16 +0000320 BUG_ON(!dma_ops);
321 dma_ops->sync_sg_for_cpu(dev, sgl, nents, direction);
Stephen Rothwell78b09732005-11-19 01:40:46 +1100322}
323
324static inline void dma_sync_sg_for_device(struct device *dev,
Jens Axboe78bdc312007-10-12 13:44:12 +0200325 struct scatterlist *sgl, int nents,
Stephen Rothwell78b09732005-11-19 01:40:46 +1100326 enum dma_data_direction direction)
327{
Becky Bruce15e09c02008-11-20 06:49:16 +0000328 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
Stephen Rothwell78b09732005-11-19 01:40:46 +1100329
Becky Bruce15e09c02008-11-20 06:49:16 +0000330 BUG_ON(!dma_ops);
331 dma_ops->sync_sg_for_device(dev, sgl, nents, direction);
Stephen Rothwell78b09732005-11-19 01:40:46 +1100332}
333
Becky Bruce15e09c02008-11-20 06:49:16 +0000334static inline void dma_sync_single_range_for_cpu(struct device *dev,
335 dma_addr_t dma_handle, unsigned long offset, size_t size,
336 enum dma_data_direction direction)
337{
338 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
339
340 BUG_ON(!dma_ops);
341 dma_ops->sync_single_range_for_cpu(dev, dma_handle,
342 offset, size, direction);
343}
344
345static inline void dma_sync_single_range_for_device(struct device *dev,
346 dma_addr_t dma_handle, unsigned long offset, size_t size,
347 enum dma_data_direction direction)
348{
349 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
350
351 BUG_ON(!dma_ops);
352 dma_ops->sync_single_range_for_device(dev, dma_handle, offset,
353 size, direction);
354}
355#else /* CONFIG_PPC_NEED_DMA_SYNC_OPS */
Becky Bruce0efbb572008-12-04 08:12:40 +0000356static inline void dma_sync_single_for_cpu(struct device *dev,
357 dma_addr_t dma_handle, size_t size,
358 enum dma_data_direction direction)
359{
360}
361
362static inline void dma_sync_single_for_device(struct device *dev,
363 dma_addr_t dma_handle, size_t size,
364 enum dma_data_direction direction)
365{
366}
367
368static inline void dma_sync_sg_for_cpu(struct device *dev,
369 struct scatterlist *sgl, int nents,
370 enum dma_data_direction direction)
371{
372}
373
374static inline void dma_sync_sg_for_device(struct device *dev,
375 struct scatterlist *sgl, int nents,
376 enum dma_data_direction direction)
377{
378}
379
380static inline void dma_sync_single_range_for_cpu(struct device *dev,
381 dma_addr_t dma_handle, unsigned long offset, size_t size,
382 enum dma_data_direction direction)
383{
384}
385
386static inline void dma_sync_single_range_for_device(struct device *dev,
387 dma_addr_t dma_handle, unsigned long offset, size_t size,
388 enum dma_data_direction direction)
389{
390}
Becky Bruce15e09c02008-11-20 06:49:16 +0000391#endif
392
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700393static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Stephen Rothwell78b09732005-11-19 01:40:46 +1100394{
395#ifdef CONFIG_PPC64
396 return (dma_addr == DMA_ERROR_CODE);
397#else
398 return 0;
399#endif
400}
401
402#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
403#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
404#ifdef CONFIG_NOT_COHERENT_CACHE
Ralf Baechlef67637e2006-12-06 20:38:54 -0800405#define dma_is_consistent(d, h) (0)
Stephen Rothwell78b09732005-11-19 01:40:46 +1100406#else
Ralf Baechlef67637e2006-12-06 20:38:54 -0800407#define dma_is_consistent(d, h) (1)
Stephen Rothwell78b09732005-11-19 01:40:46 +1100408#endif
409
410static inline int dma_get_cache_alignment(void)
411{
412#ifdef CONFIG_PPC64
413 /* no easy way to get cache size on all processors, so return
414 * the maximum possible, to be safe */
Ravikiran G Thirumalai1fd73c62006-01-08 01:01:28 -0800415 return (1 << INTERNODE_CACHE_SHIFT);
Stephen Rothwell78b09732005-11-19 01:40:46 +1100416#else
417 /*
418 * Each processor family will define its own L1_CACHE_SHIFT,
419 * L1_CACHE_BYTES wraps to this, so this is always safe.
420 */
421 return L1_CACHE_BYTES;
422#endif
423}
424
Ralf Baechled3fa72e2006-12-06 20:38:56 -0800425static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
Stephen Rothwell78b09732005-11-19 01:40:46 +1100426 enum dma_data_direction direction)
427{
428 BUG_ON(direction == DMA_NONE);
429 __dma_sync(vaddr, size, (int)direction);
430}
431
Arnd Bergmann88ced032005-12-16 22:43:46 +0100432#endif /* __KERNEL__ */
Stephen Rothwell78b09732005-11-19 01:40:46 +1100433#endif /* _ASM_DMA_MAPPING_H */