]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - arch/arm/include/asm/dma-mapping.h
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[thirdparty/kernel/linux.git] / arch / arm / include / asm / dma-mapping.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef ASMARM_DMA_MAPPING_H
3#define ASMARM_DMA_MAPPING_H
4
5#ifdef __KERNEL__
6
98ed7d4b 7#include <linux/mm_types.h>
dee9ba82 8#include <linux/scatterlist.h>
24056f52 9#include <linux/dma-debug.h>
1da177e4 10
98ed7d4b
RK
11#include <asm/memory.h>
12
c7e9bc54
SS
13#include <xen/xen.h>
14#include <asm/xen/hypervisor.h>
15
5299709d
BVA
16extern const struct dma_map_ops arm_dma_ops;
17extern const struct dma_map_ops arm_coherent_dma_ops;
2dc6a016 18
815dd187 19static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
c7e9bc54 20{
1c51c429 21 return IS_ENABLED(CONFIG_MMU) ? &arm_dma_ops : &dma_noop_ops;
c7e9bc54
SS
22}
23
9eedd963
RK
24#ifdef __arch_page_to_dma
25#error Please update to __arch_pfn_to_dma
26#endif
27
98ed7d4b 28/*
9eedd963
RK
29 * dma_to_pfn/pfn_to_dma/dma_to_virt/virt_to_dma are architecture private
30 * functions used internally by the DMA-mapping API to provide DMA
31 * addresses. They must not be used by drivers.
98ed7d4b 32 */
9eedd963
RK
33#ifndef __arch_pfn_to_dma
34static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
58edb515 35{
6ce0d200
GS
36 if (dev)
37 pfn -= dev->dma_pfn_offset;
9eedd963 38 return (dma_addr_t)__pfn_to_bus(pfn);
58edb515 39}
98ed7d4b 40
9eedd963 41static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
ef1baed8 42{
6ce0d200
GS
43 unsigned long pfn = __bus_to_pfn(addr);
44
45 if (dev)
46 pfn += dev->dma_pfn_offset;
47
48 return pfn;
ef1baed8
RK
49}
50
98ed7d4b
RK
51static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
52{
6ce0d200
GS
53 if (dev) {
54 unsigned long pfn = dma_to_pfn(dev, addr);
55
56 return phys_to_virt(__pfn_to_phys(pfn));
57 }
58
01f461a3 59 return (void *)__bus_to_virt((unsigned long)addr);
98ed7d4b
RK
60}
61
62static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
63{
6ce0d200
GS
64 if (dev)
65 return pfn_to_dma(dev, virt_to_pfn(addr));
66
98ed7d4b
RK
67 return (dma_addr_t)__virt_to_bus((unsigned long)(addr));
68}
26ba47b1 69
98ed7d4b 70#else
9eedd963 71static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
98ed7d4b 72{
9eedd963 73 return __arch_pfn_to_dma(dev, pfn);
98ed7d4b
RK
74}
75
9eedd963 76static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
ef1baed8 77{
9eedd963 78 return __arch_dma_to_pfn(dev, addr);
ef1baed8
RK
79}
80
98ed7d4b
RK
81static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
82{
83 return __arch_dma_to_virt(dev, addr);
84}
85
86static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
87{
88 return __arch_virt_to_dma(dev, addr);
89}
90#endif
1fe53268 91
26ba47b1
SS
92/* The ARM override for dma_max_pfn() */
93static inline unsigned long dma_max_pfn(struct device *dev)
94{
d248220f 95 return dma_to_pfn(dev, *dev->dma_mask);
26ba47b1
SS
96}
97#define dma_max_pfn(dev) dma_max_pfn(dev)
98
a3a60f81 99#define arch_setup_dma_ops arch_setup_dma_ops
4bb25789 100extern void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
53c92d79 101 const struct iommu_ops *iommu, bool coherent);
4bb25789
WD
102
103#define arch_teardown_dma_ops arch_teardown_dma_ops
104extern void arch_teardown_dma_ops(struct device *dev);
812b99e4 105
3d5391ac
SS
106/* do not use this function in a driver */
107static inline bool is_device_dma_coherent(struct device *dev)
108{
109 return dev->archdata.dma_coherent;
110}
111
fbd989b1
SS
112static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
113{
114 unsigned int offset = paddr & ~PAGE_MASK;
115 return pfn_to_dma(dev, __phys_to_pfn(paddr)) + offset;
116}
117
118static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
119{
120 unsigned int offset = dev_addr & ~PAGE_MASK;
121 return __pfn_to_phys(dma_to_pfn(dev, dev_addr)) + offset;
122}
123
124static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
125{
126 u64 limit, mask;
127
128 if (!dev->dma_mask)
129 return 0;
130
131 mask = *dev->dma_mask;
132
133 limit = (mask + 1) & ~mask;
134 if (limit && size > limit)
135 return 0;
136
137 if ((addr | (addr + size - 1)) & ~mask)
138 return 0;
139
140 return 1;
141}
142
143static inline void dma_mark_clean(void *addr, size_t size) { }
144
1da177e4 145/**
f99d6034 146 * arm_dma_alloc - allocate consistent memory for DMA
1da177e4
LT
147 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
148 * @size: required memory size
149 * @handle: bus-specific DMA address
f99d6034 150 * @attrs: optinal attributes that specific mapping properties
1da177e4 151 *
f99d6034
MS
152 * Allocate some memory for a device for performing DMA. This function
153 * allocates pages, and will return the CPU-viewed address, and sets @handle
154 * to be the device-viewed address.
1da177e4 155 */
f99d6034 156extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
00085f1e 157 gfp_t gfp, unsigned long attrs);
f99d6034 158
1da177e4 159/**
f99d6034 160 * arm_dma_free - free memory allocated by arm_dma_alloc
1da177e4
LT
161 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
162 * @size: size of memory originally requested in dma_alloc_coherent
163 * @cpu_addr: CPU-view address returned from dma_alloc_coherent
164 * @handle: device-view address returned from dma_alloc_coherent
f99d6034 165 * @attrs: optinal attributes that specific mapping properties
1da177e4
LT
166 *
167 * Free (and unmap) a DMA buffer previously allocated by
f99d6034 168 * arm_dma_alloc().
1da177e4
LT
169 *
170 * References to memory and mappings associated with cpu_addr/handle
171 * during and after this call executing are illegal.
172 */
f99d6034 173extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
00085f1e 174 dma_addr_t handle, unsigned long attrs);
f99d6034 175
1da177e4 176/**
f99d6034 177 * arm_dma_mmap - map a coherent DMA allocation into user space
1da177e4
LT
178 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
179 * @vma: vm_area_struct describing requested user mapping
180 * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
181 * @handle: device-view address returned from dma_alloc_coherent
182 * @size: size of memory originally requested in dma_alloc_coherent
f99d6034 183 * @attrs: optinal attributes that specific mapping properties
1da177e4
LT
184 *
185 * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
186 * into user space. The coherent DMA buffer must not be freed by the
187 * driver until the user space mapping has been released.
188 */
f99d6034
MS
189extern int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
190 void *cpu_addr, dma_addr_t dma_addr, size_t size,
00085f1e 191 unsigned long attrs);
1da177e4 192
6e5267aa
MS
193/*
194 * This can be called during early boot to increase the size of the atomic
195 * coherent DMA pool above the default value of 256KiB. It must be called
196 * before postcore_initcall.
197 */
198extern void __init init_dma_coherent_pool_size(unsigned long size);
199
8c8a0ec5
RK
200/*
201 * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
202 * and utilize bounce buffers as needed to work around limited DMA windows.
203 *
204 * On the SA-1111, a bug limits DMA to only certain regions of RAM.
205 * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
206 * On some ADI engineering systems, PCI inbound window is 32MB (12MB total RAM)
207 *
208 * The following are helper functions used by the dmabounce subystem
209 *
210 */
211
212/**
213 * dmabounce_register_dev
214 *
215 * @dev: valid struct device pointer
216 * @small_buf_size: size of buffers to use with small buffer pool
217 * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
0703ed2a 218 * @needs_bounce_fn: called to determine whether buffer needs bouncing
8c8a0ec5
RK
219 *
220 * This function should be called by low-level platform code to register
221 * a device as requireing DMA buffer bouncing. The function will allocate
222 * appropriate DMA pools for the device.
8c8a0ec5 223 */
3216a97b 224extern int dmabounce_register_dev(struct device *, unsigned long,
0703ed2a 225 unsigned long, int (*)(struct device *, dma_addr_t, size_t));
8c8a0ec5
RK
226
227/**
228 * dmabounce_unregister_dev
229 *
230 * @dev: valid struct device pointer
231 *
232 * This function should be called by low-level platform code when device
233 * that was previously registered with dmabounce_register_dev is removed
234 * from the system.
235 *
236 */
237extern void dmabounce_unregister_dev(struct device *);
238
8c8a0ec5 239
24056f52 240
afd1a321
RK
241/*
242 * The scatter list versions of the above methods.
1da177e4 243 */
2dc6a016 244extern int arm_dma_map_sg(struct device *, struct scatterlist *, int,
00085f1e 245 enum dma_data_direction, unsigned long attrs);
2dc6a016 246extern void arm_dma_unmap_sg(struct device *, struct scatterlist *, int,
00085f1e 247 enum dma_data_direction, unsigned long attrs);
2dc6a016 248extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int,
3216a97b 249 enum dma_data_direction);
2dc6a016 250extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int,
3216a97b 251 enum dma_data_direction);
dc2832e1
MS
252extern int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
253 void *cpu_addr, dma_addr_t dma_addr, size_t size,
00085f1e 254 unsigned long attrs);
1da177e4 255
1da177e4
LT
256#endif /* __KERNEL__ */
257#endif