1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef ASMARM_DMA_MAPPING_H
3*4882a593Smuzhiyun #define ASMARM_DMA_MAPPING_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #ifdef __KERNEL__
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/mm_types.h>
8*4882a593Smuzhiyun #include <linux/scatterlist.h>
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <xen/xen.h>
11*4882a593Smuzhiyun #include <asm/xen/hypervisor.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun extern const struct dma_map_ops arm_dma_ops;
14*4882a593Smuzhiyun extern const struct dma_map_ops arm_coherent_dma_ops;
15*4882a593Smuzhiyun
get_arch_dma_ops(struct bus_type * bus)16*4882a593Smuzhiyun static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_MMU) && !IS_ENABLED(CONFIG_ARM_LPAE))
19*4882a593Smuzhiyun return &arm_dma_ops;
20*4882a593Smuzhiyun return NULL;
21*4882a593Smuzhiyun }
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /**
24*4882a593Smuzhiyun * arm_dma_alloc - allocate consistent memory for DMA
25*4882a593Smuzhiyun * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
26*4882a593Smuzhiyun * @size: required memory size
27*4882a593Smuzhiyun * @handle: bus-specific DMA address
28*4882a593Smuzhiyun * @attrs: optinal attributes that specific mapping properties
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * Allocate some memory for a device for performing DMA. This function
31*4882a593Smuzhiyun * allocates pages, and will return the CPU-viewed address, and sets @handle
32*4882a593Smuzhiyun * to be the device-viewed address.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
35*4882a593Smuzhiyun gfp_t gfp, unsigned long attrs);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /**
38*4882a593Smuzhiyun * arm_dma_free - free memory allocated by arm_dma_alloc
39*4882a593Smuzhiyun * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
40*4882a593Smuzhiyun * @size: size of memory originally requested in dma_alloc_coherent
41*4882a593Smuzhiyun * @cpu_addr: CPU-view address returned from dma_alloc_coherent
42*4882a593Smuzhiyun * @handle: device-view address returned from dma_alloc_coherent
43*4882a593Smuzhiyun * @attrs: optinal attributes that specific mapping properties
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * Free (and unmap) a DMA buffer previously allocated by
46*4882a593Smuzhiyun * arm_dma_alloc().
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * References to memory and mappings associated with cpu_addr/handle
49*4882a593Smuzhiyun * during and after this call executing are illegal.
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
52*4882a593Smuzhiyun dma_addr_t handle, unsigned long attrs);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun * arm_dma_mmap - map a coherent DMA allocation into user space
56*4882a593Smuzhiyun * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
57*4882a593Smuzhiyun * @vma: vm_area_struct describing requested user mapping
58*4882a593Smuzhiyun * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
59*4882a593Smuzhiyun * @handle: device-view address returned from dma_alloc_coherent
60*4882a593Smuzhiyun * @size: size of memory originally requested in dma_alloc_coherent
61*4882a593Smuzhiyun * @attrs: optinal attributes that specific mapping properties
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
64*4882a593Smuzhiyun * into user space. The coherent DMA buffer must not be freed by the
65*4882a593Smuzhiyun * driver until the user space mapping has been released.
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun extern int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
68*4882a593Smuzhiyun void *cpu_addr, dma_addr_t dma_addr, size_t size,
69*4882a593Smuzhiyun unsigned long attrs);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
73*4882a593Smuzhiyun * and utilize bounce buffers as needed to work around limited DMA windows.
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * On the SA-1111, a bug limits DMA to only certain regions of RAM.
76*4882a593Smuzhiyun * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
77*4882a593Smuzhiyun * On some ADI engineering systems, PCI inbound window is 32MB (12MB total RAM)
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * The following are helper functions used by the dmabounce subystem
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun * dmabounce_register_dev
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * @dev: valid struct device pointer
87*4882a593Smuzhiyun * @small_buf_size: size of buffers to use with small buffer pool
88*4882a593Smuzhiyun * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
89*4882a593Smuzhiyun * @needs_bounce_fn: called to determine whether buffer needs bouncing
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * This function should be called by low-level platform code to register
92*4882a593Smuzhiyun * a device as requireing DMA buffer bouncing. The function will allocate
93*4882a593Smuzhiyun * appropriate DMA pools for the device.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun extern int dmabounce_register_dev(struct device *, unsigned long,
96*4882a593Smuzhiyun unsigned long, int (*)(struct device *, dma_addr_t, size_t));
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun * dmabounce_unregister_dev
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * @dev: valid struct device pointer
102*4882a593Smuzhiyun *
103*4882a593Smuzhiyun * This function should be called by low-level platform code when device
104*4882a593Smuzhiyun * that was previously registered with dmabounce_register_dev is removed
105*4882a593Smuzhiyun * from the system.
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun extern void dmabounce_unregister_dev(struct device *);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun * The scatter list versions of the above methods.
114*4882a593Smuzhiyun */
115*4882a593Smuzhiyun extern int arm_dma_map_sg(struct device *, struct scatterlist *, int,
116*4882a593Smuzhiyun enum dma_data_direction, unsigned long attrs);
117*4882a593Smuzhiyun extern void arm_dma_unmap_sg(struct device *, struct scatterlist *, int,
118*4882a593Smuzhiyun enum dma_data_direction, unsigned long attrs);
119*4882a593Smuzhiyun extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int,
120*4882a593Smuzhiyun enum dma_data_direction);
121*4882a593Smuzhiyun extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int,
122*4882a593Smuzhiyun enum dma_data_direction);
123*4882a593Smuzhiyun extern int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
124*4882a593Smuzhiyun void *cpu_addr, dma_addr_t dma_addr, size_t size,
125*4882a593Smuzhiyun unsigned long attrs);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun #endif /* __KERNEL__ */
128*4882a593Smuzhiyun #endif
129