1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef __ASM_DMA_MAPPING_H
17#define __ASM_DMA_MAPPING_H
18
19#ifdef __KERNEL__
20
21#include <linux/types.h>
22#include <linux/vmalloc.h>
23
24#include <asm-generic/dma-coherent.h>
25
26#include <xen/xen.h>
27#include <asm/xen/hypervisor.h>
28
29#define DMA_ERROR_CODE (~(dma_addr_t)0)
30extern struct dma_map_ops *dma_ops;
31
32static inline struct dma_map_ops *__generic_dma_ops(struct device *dev)
33{
34 if (unlikely(!dev) || !dev->archdata.dma_ops)
35 return dma_ops;
36 else
37 return dev->archdata.dma_ops;
38}
39
40static inline struct dma_map_ops *get_dma_ops(struct device *dev)
41{
42 if (xen_initial_domain())
43 return xen_dma_ops;
44 else
45 return __generic_dma_ops(dev);
46}
47
48static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
49 struct iommu_ops *iommu, bool coherent)
50{
51 dev->archdata.dma_coherent = coherent;
52}
53#define arch_setup_dma_ops arch_setup_dma_ops
54
55
56static inline bool is_device_dma_coherent(struct device *dev)
57{
58 if (!dev)
59 return false;
60 return dev->archdata.dma_coherent;
61}
62
63#include <asm-generic/dma-mapping-common.h>
64
65static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
66{
67 return (dma_addr_t)paddr;
68}
69
70static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
71{
72 return (phys_addr_t)dev_addr;
73}
74
75static inline int dma_mapping_error(struct device *dev, dma_addr_t dev_addr)
76{
77 struct dma_map_ops *ops = get_dma_ops(dev);
78 debug_dma_mapping_error(dev, dev_addr);
79 return ops->mapping_error(dev, dev_addr);
80}
81
82static inline int dma_supported(struct device *dev, u64 mask)
83{
84 struct dma_map_ops *ops = get_dma_ops(dev);
85 return ops->dma_supported(dev, mask);
86}
87
88static inline int dma_set_mask(struct device *dev, u64 mask)
89{
90 if (!dev->dma_mask || !dma_supported(dev, mask))
91 return -EIO;
92 *dev->dma_mask = mask;
93
94 return 0;
95}
96
97static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
98{
99 if (!dev->dma_mask)
100 return false;
101
102 return addr + size - 1 <= *dev->dma_mask;
103}
104
105static inline void dma_mark_clean(void *addr, size_t size)
106{
107}
108
109#define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
110#define dma_free_coherent(d, s, h, f) dma_free_attrs(d, s, h, f, NULL)
111
112static inline void *dma_alloc_attrs(struct device *dev, size_t size,
113 dma_addr_t *dma_handle, gfp_t flags,
114 struct dma_attrs *attrs)
115{
116 struct dma_map_ops *ops = get_dma_ops(dev);
117 void *vaddr;
118
119 if (dma_alloc_from_coherent(dev, size, dma_handle, &vaddr))
120 return vaddr;
121
122 vaddr = ops->alloc(dev, size, dma_handle, flags, attrs);
123 debug_dma_alloc_coherent(dev, size, *dma_handle, vaddr);
124 return vaddr;
125}
126
127static inline void dma_free_attrs(struct device *dev, size_t size,
128 void *vaddr, dma_addr_t dev_addr,
129 struct dma_attrs *attrs)
130{
131 struct dma_map_ops *ops = get_dma_ops(dev);
132
133 if (dma_release_from_coherent(dev, get_order(size), vaddr))
134 return;
135
136 debug_dma_free_coherent(dev, size, vaddr, dev_addr);
137 ops->free(dev, size, vaddr, dev_addr, attrs);
138}
139
140
141
142
143static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
144 dma_addr_t *handle, gfp_t flags)
145{
146 return NULL;
147}
148
149static inline void dma_free_noncoherent(struct device *dev, size_t size,
150 void *cpu_addr, dma_addr_t handle)
151{
152}
153
154#endif
155#endif
156