Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / arch / arm64 / include / asm / dma-mapping.h
1 /*
2  * Copyright (C) 2012 ARM Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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)
30 extern struct dma_map_ops *dma_ops;
31
32 static 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
40 static 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
48 static 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 /* do not use this function in a driver */
56 static 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
65 static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
66 {
67         return (dma_addr_t)paddr;
68 }
69
70 static 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
75 static 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
82 static 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
88 static 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
97 static 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
105 static 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
112 static 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
127 static 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  * There is no dma_cache_sync() implementation, so just return NULL here.
142  */
143 static 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
149 static inline void dma_free_noncoherent(struct device *dev, size_t size,
150                                         void *cpu_addr, dma_addr_t handle)
151 {
152 }
153
154 #endif  /* __KERNEL__ */
155 #endif  /* __ASM_DMA_MAPPING_H */