blob: b437f2c780b83f6c84d427f18af4416fa9e42fd7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SH_DMA_MAPPING_H
2#define __ASM_SH_DMA_MAPPING_H
3
Paul Mundt73c926b2009-10-20 12:55:56 +09004extern struct dma_map_ops *dma_ops;
5extern void no_iommu_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Paul Mundt73c926b2009-10-20 12:55:56 +09007static inline struct dma_map_ops *get_dma_ops(struct device *dev)
8{
9 return dma_ops;
10}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Paul Mundtf32154c92009-10-26 09:50:51 +090012#include <asm-generic/dma-coherent.h>
13#include <asm-generic/dma-mapping-common.h>
14
Paul Mundt73c926b2009-10-20 12:55:56 +090015static inline int dma_supported(struct device *dev, u64 mask)
16{
17 struct dma_map_ops *ops = get_dma_ops(dev);
18
19 if (ops->dma_supported)
20 return ops->dma_supported(dev, mask);
21
22 return 1;
23}
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static inline int dma_set_mask(struct device *dev, u64 mask)
26{
Paul Mundt73c926b2009-10-20 12:55:56 +090027 struct dma_map_ops *ops = get_dma_ops(dev);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 if (!dev->dma_mask || !dma_supported(dev, mask))
30 return -EIO;
Paul Mundt73c926b2009-10-20 12:55:56 +090031 if (ops->set_dma_mask)
32 return ops->set_dma_mask(dev, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 *dev->dma_mask = mask;
35
36 return 0;
37}
38
Magnus Dammf93e97e2008-01-24 18:35:10 +090039void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
40 enum dma_data_direction dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Paul Mundtc7666e72007-02-13 11:11:22 +090042#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
43#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
Paul Mundt478fb152009-10-27 10:41:58 +090044
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070045static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Paul Mundt73c926b2009-10-20 12:55:56 +090047 struct dma_map_ops *ops = get_dma_ops(dev);
48
Shuah Khan50af1e92012-11-23 14:36:33 -070049 debug_dma_mapping_error(dev, dma_addr);
Paul Mundt73c926b2009-10-20 12:55:56 +090050 if (ops->mapping_error)
51 return ops->mapping_error(dev, dma_addr);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return dma_addr == 0;
54}
Magnus Dammf93e97e2008-01-24 18:35:10 +090055
Andrzej Pietrasiewicz552c0d32011-12-14 12:11:13 +010056#define dma_alloc_coherent(d,s,h,f) dma_alloc_attrs(d,s,h,f,NULL)
57
58static inline void *dma_alloc_attrs(struct device *dev, size_t size,
59 dma_addr_t *dma_handle, gfp_t gfp,
60 struct dma_attrs *attrs)
Paul Mundtf32154c92009-10-26 09:50:51 +090061{
62 struct dma_map_ops *ops = get_dma_ops(dev);
63 void *memory;
64
65 if (dma_alloc_from_coherent(dev, size, dma_handle, &memory))
66 return memory;
Andrzej Pietrasiewicz552c0d32011-12-14 12:11:13 +010067 if (!ops->alloc)
Paul Mundtf32154c92009-10-26 09:50:51 +090068 return NULL;
69
Andrzej Pietrasiewicz552c0d32011-12-14 12:11:13 +010070 memory = ops->alloc(dev, size, dma_handle, gfp, attrs);
Paul Mundtf32154c92009-10-26 09:50:51 +090071 debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
72
73 return memory;
74}
75
Andrzej Pietrasiewicz552c0d32011-12-14 12:11:13 +010076#define dma_free_coherent(d,s,c,h) dma_free_attrs(d,s,c,h,NULL)
77
78static inline void dma_free_attrs(struct device *dev, size_t size,
79 void *vaddr, dma_addr_t dma_handle,
80 struct dma_attrs *attrs)
Paul Mundtf32154c92009-10-26 09:50:51 +090081{
82 struct dma_map_ops *ops = get_dma_ops(dev);
83
Paul Mundtf32154c92009-10-26 09:50:51 +090084 if (dma_release_from_coherent(dev, get_order(size), vaddr))
85 return;
86
87 debug_dma_free_coherent(dev, size, vaddr, dma_handle);
Andrzej Pietrasiewicz552c0d32011-12-14 12:11:13 +010088 if (ops->free)
89 ops->free(dev, size, vaddr, dma_handle, attrs);
Paul Mundtf32154c92009-10-26 09:50:51 +090090}
91
92/* arch/sh/mm/consistent.c */
93extern void *dma_generic_alloc_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewicz552c0d32011-12-14 12:11:13 +010094 dma_addr_t *dma_addr, gfp_t flag,
95 struct dma_attrs *attrs);
Paul Mundtf32154c92009-10-26 09:50:51 +090096extern void dma_generic_free_coherent(struct device *dev, size_t size,
Andrzej Pietrasiewicz552c0d32011-12-14 12:11:13 +010097 void *vaddr, dma_addr_t dma_handle,
98 struct dma_attrs *attrs);
Magnus Dammf93e97e2008-01-24 18:35:10 +090099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#endif /* __ASM_SH_DMA_MAPPING_H */