]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/arm64/include/asm/dma-mapping.h
Merge branch 'next' of git://git.infradead.org/users/pcmoore/selinux into next
[karo-tx-linux.git] / 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/acpi.h>
22 #include <linux/types.h>
23 #include <linux/vmalloc.h>
24
25 #include <asm-generic/dma-coherent.h>
26
27 #include <xen/xen.h>
28 #include <asm/xen/hypervisor.h>
29
30 #define DMA_ERROR_CODE  (~(dma_addr_t)0)
31 extern struct dma_map_ops *dma_ops;
32 extern struct dma_map_ops dummy_dma_ops;
33
34 static inline struct dma_map_ops *__generic_dma_ops(struct device *dev)
35 {
36         if (unlikely(!dev))
37                 return dma_ops;
38         else if (dev->archdata.dma_ops)
39                 return dev->archdata.dma_ops;
40         else if (acpi_disabled)
41                 return dma_ops;
42
43         /*
44          * When ACPI is enabled, if arch_set_dma_ops is not called,
45          * we will disable device DMA capability by setting it
46          * to dummy_dma_ops.
47          */
48         return &dummy_dma_ops;
49 }
50
51 static inline struct dma_map_ops *get_dma_ops(struct device *dev)
52 {
53         if (xen_initial_domain())
54                 return xen_dma_ops;
55         else
56                 return __generic_dma_ops(dev);
57 }
58
59 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
60                                       struct iommu_ops *iommu, bool coherent)
61 {
62         if (!acpi_disabled && !dev->archdata.dma_ops)
63                 dev->archdata.dma_ops = dma_ops;
64
65         dev->archdata.dma_coherent = coherent;
66 }
67 #define arch_setup_dma_ops      arch_setup_dma_ops
68
69 /* do not use this function in a driver */
70 static inline bool is_device_dma_coherent(struct device *dev)
71 {
72         if (!dev)
73                 return false;
74         return dev->archdata.dma_coherent;
75 }
76
77 #include <asm-generic/dma-mapping-common.h>
78
79 static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
80 {
81         return (dma_addr_t)paddr;
82 }
83
84 static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
85 {
86         return (phys_addr_t)dev_addr;
87 }
88
89 static inline int dma_mapping_error(struct device *dev, dma_addr_t dev_addr)
90 {
91         struct dma_map_ops *ops = get_dma_ops(dev);
92         debug_dma_mapping_error(dev, dev_addr);
93         return ops->mapping_error(dev, dev_addr);
94 }
95
96 static inline int dma_supported(struct device *dev, u64 mask)
97 {
98         struct dma_map_ops *ops = get_dma_ops(dev);
99         return ops->dma_supported(dev, mask);
100 }
101
102 static inline int dma_set_mask(struct device *dev, u64 mask)
103 {
104         if (!dev->dma_mask || !dma_supported(dev, mask))
105                 return -EIO;
106         *dev->dma_mask = mask;
107
108         return 0;
109 }
110
111 static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
112 {
113         if (!dev->dma_mask)
114                 return false;
115
116         return addr + size - 1 <= *dev->dma_mask;
117 }
118
119 static inline void dma_mark_clean(void *addr, size_t size)
120 {
121 }
122
123 #define dma_alloc_coherent(d, s, h, f)  dma_alloc_attrs(d, s, h, f, NULL)
124 #define dma_free_coherent(d, s, h, f)   dma_free_attrs(d, s, h, f, NULL)
125
126 static inline void *dma_alloc_attrs(struct device *dev, size_t size,
127                                     dma_addr_t *dma_handle, gfp_t flags,
128                                     struct dma_attrs *attrs)
129 {
130         struct dma_map_ops *ops = get_dma_ops(dev);
131         void *vaddr;
132
133         if (dma_alloc_from_coherent(dev, size, dma_handle, &vaddr))
134                 return vaddr;
135
136         vaddr = ops->alloc(dev, size, dma_handle, flags, attrs);
137         debug_dma_alloc_coherent(dev, size, *dma_handle, vaddr);
138         return vaddr;
139 }
140
141 static inline void dma_free_attrs(struct device *dev, size_t size,
142                                   void *vaddr, dma_addr_t dev_addr,
143                                   struct dma_attrs *attrs)
144 {
145         struct dma_map_ops *ops = get_dma_ops(dev);
146
147         if (dma_release_from_coherent(dev, get_order(size), vaddr))
148                 return;
149
150         debug_dma_free_coherent(dev, size, vaddr, dev_addr);
151         ops->free(dev, size, vaddr, dev_addr, attrs);
152 }
153
154 /*
155  * There is no dma_cache_sync() implementation, so just return NULL here.
156  */
157 static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
158                                           dma_addr_t *handle, gfp_t flags)
159 {
160         return NULL;
161 }
162
163 static inline void dma_free_noncoherent(struct device *dev, size_t size,
164                                         void *cpu_addr, dma_addr_t handle)
165 {
166 }
167
168 #endif  /* __KERNEL__ */
169 #endif  /* __ASM_DMA_MAPPING_H */