blob: 03ba90ffc0f8f1130dd56fef97ecb9bf7a058a44 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef ASMARM_DMA_MAPPING_H
3#define ASMARM_DMA_MAPPING_H
4
5#ifdef __KERNEL__
6
Russell King98ed7d42008-08-10 12:10:49 +01007#include <linux/mm_types.h>
Jens Axboedee9ba82007-10-23 12:37:59 +02008#include <linux/scatterlist.h>
Russell King24056f52011-01-03 11:29:28 +00009#include <linux/dma-debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Russell King98ed7d42008-08-10 12:10:49 +010011#include <asm/memory.h>
12
Stefano Stabellinic7e9bc52013-10-18 16:01:26 +000013#include <xen/xen.h>
14#include <asm/xen/hypervisor.h>
15
Bart Van Assche52997092017-01-20 13:04:01 -080016extern const struct dma_map_ops arm_dma_ops;
17extern const struct dma_map_ops arm_coherent_dma_ops;
Marek Szyprowski2dc6a012012-02-10 19:55:20 +010018
Bart Van Assche815dd182017-01-20 13:04:04 -080019static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
Stefano Stabellinic7e9bc52013-10-18 16:01:26 +000020{
Christoph Hellwig356da6d2018-12-06 13:39:32 -080021 return IS_ENABLED(CONFIG_MMU) ? &arm_dma_ops : NULL;
Stefano Stabellinic7e9bc52013-10-18 16:01:26 +000022}
23
Russell King9eedd962011-01-03 00:00:17 +000024#ifdef __arch_page_to_dma
25#error Please update to __arch_pfn_to_dma
26#endif
27
Russell King98ed7d42008-08-10 12:10:49 +010028/*
Russell King9eedd962011-01-03 00:00:17 +000029 * 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.
Russell King98ed7d42008-08-10 12:10:49 +010032 */
Russell King9eedd962011-01-03 00:00:17 +000033#ifndef __arch_pfn_to_dma
34static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
Nicolas Pitre58edb512008-09-09 15:54:13 -040035{
Grygorii Strashko6ce0d202014-04-24 11:30:05 -040036 if (dev)
37 pfn -= dev->dma_pfn_offset;
Russell King9eedd962011-01-03 00:00:17 +000038 return (dma_addr_t)__pfn_to_bus(pfn);
Nicolas Pitre58edb512008-09-09 15:54:13 -040039}
Russell King98ed7d42008-08-10 12:10:49 +010040
Russell King9eedd962011-01-03 00:00:17 +000041static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
Russell Kingef1baed2009-10-31 16:07:16 +000042{
Grygorii Strashko6ce0d202014-04-24 11:30:05 -040043 unsigned long pfn = __bus_to_pfn(addr);
44
45 if (dev)
46 pfn += dev->dma_pfn_offset;
47
48 return pfn;
Russell Kingef1baed2009-10-31 16:07:16 +000049}
50
Russell King98ed7d42008-08-10 12:10:49 +010051static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
52{
Grygorii Strashko6ce0d202014-04-24 11:30:05 -040053 if (dev) {
54 unsigned long pfn = dma_to_pfn(dev, addr);
55
56 return phys_to_virt(__pfn_to_phys(pfn));
57 }
58
Catalin Marinas01f461a2011-08-23 13:59:14 +010059 return (void *)__bus_to_virt((unsigned long)addr);
Russell King98ed7d42008-08-10 12:10:49 +010060}
61
62static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
63{
Grygorii Strashko6ce0d202014-04-24 11:30:05 -040064 if (dev)
65 return pfn_to_dma(dev, virt_to_pfn(addr));
66
Russell King98ed7d42008-08-10 12:10:49 +010067 return (dma_addr_t)__virt_to_bus((unsigned long)(addr));
68}
Santosh Shilimkar26ba47b2013-08-01 03:12:01 +010069
Russell King98ed7d42008-08-10 12:10:49 +010070#else
Russell King9eedd962011-01-03 00:00:17 +000071static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
Russell King98ed7d42008-08-10 12:10:49 +010072{
Russell King9eedd962011-01-03 00:00:17 +000073 return __arch_pfn_to_dma(dev, pfn);
Russell King98ed7d42008-08-10 12:10:49 +010074}
75
Russell King9eedd962011-01-03 00:00:17 +000076static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
Russell Kingef1baed2009-10-31 16:07:16 +000077{
Russell King9eedd962011-01-03 00:00:17 +000078 return __arch_dma_to_pfn(dev, addr);
Russell Kingef1baed2009-10-31 16:07:16 +000079}
80
Russell King98ed7d42008-08-10 12:10:49 +010081static 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
Dmitry Baryshkov1fe53262008-07-18 13:30:14 +040091
Santosh Shilimkar26ba47b2013-08-01 03:12:01 +010092/* The ARM override for dma_max_pfn() */
93static inline unsigned long dma_max_pfn(struct device *dev)
94{
Roger Quadrosd2482202016-09-29 08:32:55 +010095 return dma_to_pfn(dev, *dev->dma_mask);
Santosh Shilimkar26ba47b2013-08-01 03:12:01 +010096}
97#define dma_max_pfn(dev) dma_max_pfn(dev)
98
Stefano Stabellini3d5391a2014-11-20 10:41:40 +000099/* do not use this function in a driver */
100static inline bool is_device_dma_coherent(struct device *dev)
101{
102 return dev->archdata.dma_coherent;
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/**
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200106 * arm_dma_alloc - allocate consistent memory for DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
108 * @size: required memory size
109 * @handle: bus-specific DMA address
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200110 * @attrs: optinal attributes that specific mapping properties
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200112 * Allocate some memory for a device for performing DMA. This function
113 * allocates pages, and will return the CPU-viewed address, and sets @handle
114 * to be the device-viewed address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200116extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700117 gfp_t gfp, unsigned long attrs);
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/**
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200120 * arm_dma_free - free memory allocated by arm_dma_alloc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
122 * @size: size of memory originally requested in dma_alloc_coherent
123 * @cpu_addr: CPU-view address returned from dma_alloc_coherent
124 * @handle: device-view address returned from dma_alloc_coherent
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200125 * @attrs: optinal attributes that specific mapping properties
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
127 * Free (and unmap) a DMA buffer previously allocated by
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200128 * arm_dma_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
130 * References to memory and mappings associated with cpu_addr/handle
131 * during and after this call executing are illegal.
132 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200133extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700134 dma_addr_t handle, unsigned long attrs);
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/**
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200137 * arm_dma_mmap - map a coherent DMA allocation into user space
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
139 * @vma: vm_area_struct describing requested user mapping
140 * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
141 * @handle: device-view address returned from dma_alloc_coherent
142 * @size: size of memory originally requested in dma_alloc_coherent
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200143 * @attrs: optinal attributes that specific mapping properties
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 *
145 * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
146 * into user space. The coherent DMA buffer must not be freed by the
147 * driver until the user space mapping has been released.
148 */
Marek Szyprowskif99d6032012-05-16 18:31:23 +0200149extern int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
150 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700151 unsigned long attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jon Medhurst99d17172011-08-02 17:28:27 +0100153/*
Russell King8c8a0ec52008-09-25 21:52:49 +0100154 * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
155 * and utilize bounce buffers as needed to work around limited DMA windows.
156 *
157 * On the SA-1111, a bug limits DMA to only certain regions of RAM.
158 * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
159 * On some ADI engineering systems, PCI inbound window is 32MB (12MB total RAM)
160 *
161 * The following are helper functions used by the dmabounce subystem
162 *
163 */
164
165/**
166 * dmabounce_register_dev
167 *
168 * @dev: valid struct device pointer
169 * @small_buf_size: size of buffers to use with small buffer pool
170 * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
Russell King0703ed22011-07-04 08:32:21 +0100171 * @needs_bounce_fn: called to determine whether buffer needs bouncing
Russell King8c8a0ec52008-09-25 21:52:49 +0100172 *
173 * This function should be called by low-level platform code to register
174 * a device as requireing DMA buffer bouncing. The function will allocate
175 * appropriate DMA pools for the device.
Russell King8c8a0ec52008-09-25 21:52:49 +0100176 */
Russell King3216a972008-09-25 22:23:31 +0100177extern int dmabounce_register_dev(struct device *, unsigned long,
Russell King0703ed22011-07-04 08:32:21 +0100178 unsigned long, int (*)(struct device *, dma_addr_t, size_t));
Russell King8c8a0ec52008-09-25 21:52:49 +0100179
180/**
181 * dmabounce_unregister_dev
182 *
183 * @dev: valid struct device pointer
184 *
185 * This function should be called by low-level platform code when device
186 * that was previously registered with dmabounce_register_dev is removed
187 * from the system.
188 *
189 */
190extern void dmabounce_unregister_dev(struct device *);
191
Russell King125ab122008-09-25 22:16:22 +0100192
Russell King24056f52011-01-03 11:29:28 +0000193
Russell Kingafd1a322008-09-25 16:30:57 +0100194/*
195 * The scatter list versions of the above methods.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100197extern int arm_dma_map_sg(struct device *, struct scatterlist *, int,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700198 enum dma_data_direction, unsigned long attrs);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100199extern void arm_dma_unmap_sg(struct device *, struct scatterlist *, int,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700200 enum dma_data_direction, unsigned long attrs);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100201extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int,
Russell King3216a972008-09-25 22:23:31 +0100202 enum dma_data_direction);
Marek Szyprowski2dc6a012012-02-10 19:55:20 +0100203extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int,
Russell King3216a972008-09-25 22:23:31 +0100204 enum dma_data_direction);
Marek Szyprowskidc2832e2012-06-13 10:01:15 +0200205extern int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
206 void *cpu_addr, dma_addr_t dma_addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700207 unsigned long attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#endif /* __KERNEL__ */
210#endif