Robert P. J. Day | 96532ba | 2008-02-03 15:06:26 +0200 | [diff] [blame] | 1 | #ifndef _LINUX_DMA_MAPPING_H |
| 2 | #define _LINUX_DMA_MAPPING_H |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
Robin Murphy | 002edb6 | 2015-11-06 16:32:51 -0800 | [diff] [blame] | 4 | #include <linux/sizes.h> |
Andrew Morton | 842fa69 | 2011-11-02 13:39:33 -0700 | [diff] [blame] | 5 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/device.h> |
| 7 | #include <linux/err.h> |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 8 | #include <linux/dma-debug.h> |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 9 | #include <linux/dma-direction.h> |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 10 | #include <linux/scatterlist.h> |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 11 | #include <linux/kmemcheck.h> |
| 12 | #include <linux/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 14 | /** |
| 15 | * List of possible attributes associated with a DMA mapping. The semantics |
| 16 | * of each attribute should be defined in Documentation/DMA-attributes.txt. |
| 17 | * |
| 18 | * DMA_ATTR_WRITE_BARRIER: DMA to a memory region with this attribute |
| 19 | * forces all pending DMA writes to complete. |
| 20 | */ |
| 21 | #define DMA_ATTR_WRITE_BARRIER (1UL << 0) |
| 22 | /* |
| 23 | * DMA_ATTR_WEAK_ORDERING: Specifies that reads and writes to the mapping |
| 24 | * may be weakly ordered, that is that reads and writes may pass each other. |
| 25 | */ |
| 26 | #define DMA_ATTR_WEAK_ORDERING (1UL << 1) |
| 27 | /* |
| 28 | * DMA_ATTR_WRITE_COMBINE: Specifies that writes to the mapping may be |
| 29 | * buffered to improve performance. |
| 30 | */ |
| 31 | #define DMA_ATTR_WRITE_COMBINE (1UL << 2) |
| 32 | /* |
| 33 | * DMA_ATTR_NON_CONSISTENT: Lets the platform to choose to return either |
| 34 | * consistent or non-consistent memory as it sees fit. |
| 35 | */ |
| 36 | #define DMA_ATTR_NON_CONSISTENT (1UL << 3) |
| 37 | /* |
| 38 | * DMA_ATTR_NO_KERNEL_MAPPING: Lets the platform to avoid creating a kernel |
| 39 | * virtual mapping for the allocated buffer. |
| 40 | */ |
| 41 | #define DMA_ATTR_NO_KERNEL_MAPPING (1UL << 4) |
| 42 | /* |
| 43 | * DMA_ATTR_SKIP_CPU_SYNC: Allows platform code to skip synchronization of |
| 44 | * the CPU cache for the given buffer assuming that it has been already |
| 45 | * transferred to 'device' domain. |
| 46 | */ |
| 47 | #define DMA_ATTR_SKIP_CPU_SYNC (1UL << 5) |
| 48 | /* |
| 49 | * DMA_ATTR_FORCE_CONTIGUOUS: Forces contiguous allocation of the buffer |
| 50 | * in physical memory. |
| 51 | */ |
| 52 | #define DMA_ATTR_FORCE_CONTIGUOUS (1UL << 6) |
| 53 | /* |
| 54 | * DMA_ATTR_ALLOC_SINGLE_PAGES: This is a hint to the DMA-mapping subsystem |
| 55 | * that it's probably not worth the time to try to allocate memory to in a way |
| 56 | * that gives better TLB efficiency. |
| 57 | */ |
| 58 | #define DMA_ATTR_ALLOC_SINGLE_PAGES (1UL << 7) |
Mauricio Faria de Oliveira | a9a62c9 | 2016-10-11 13:54:14 -0700 | [diff] [blame] | 59 | /* |
| 60 | * DMA_ATTR_NO_WARN: This tells the DMA-mapping subsystem to suppress |
| 61 | * allocation failure reports (similarly to __GFP_NOWARN). |
| 62 | */ |
| 63 | #define DMA_ATTR_NO_WARN (1UL << 8) |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 64 | |
Bjorn Helgaas | 77f2ea2 | 2014-04-30 11:20:53 -0600 | [diff] [blame] | 65 | /* |
| 66 | * A dma_addr_t can hold any valid DMA or bus address for the platform. |
| 67 | * It can be given to a device to use as a DMA source or target. A CPU cannot |
| 68 | * reference a dma_addr_t directly because there may be translation between |
| 69 | * its physical address space and the bus address space. |
| 70 | */ |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 71 | struct dma_map_ops { |
Marek Szyprowski | 613c457 | 2012-03-28 16:36:27 +0200 | [diff] [blame] | 72 | void* (*alloc)(struct device *dev, size_t size, |
| 73 | dma_addr_t *dma_handle, gfp_t gfp, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 74 | unsigned long attrs); |
Marek Szyprowski | 613c457 | 2012-03-28 16:36:27 +0200 | [diff] [blame] | 75 | void (*free)(struct device *dev, size_t size, |
| 76 | void *vaddr, dma_addr_t dma_handle, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 77 | unsigned long attrs); |
Marek Szyprowski | 9adc537 | 2011-12-21 16:55:33 +0100 | [diff] [blame] | 78 | int (*mmap)(struct device *, struct vm_area_struct *, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 79 | void *, dma_addr_t, size_t, |
| 80 | unsigned long attrs); |
Marek Szyprowski | 9adc537 | 2011-12-21 16:55:33 +0100 | [diff] [blame] | 81 | |
Marek Szyprowski | d2b7428 | 2012-06-13 10:05:52 +0200 | [diff] [blame] | 82 | int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 83 | dma_addr_t, size_t, unsigned long attrs); |
Marek Szyprowski | d2b7428 | 2012-06-13 10:05:52 +0200 | [diff] [blame] | 84 | |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 85 | dma_addr_t (*map_page)(struct device *dev, struct page *page, |
| 86 | unsigned long offset, size_t size, |
| 87 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 88 | unsigned long attrs); |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 89 | void (*unmap_page)(struct device *dev, dma_addr_t dma_handle, |
| 90 | size_t size, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 91 | unsigned long attrs); |
Ricardo Ribalda Delgado | 04abab6 | 2015-02-11 13:53:15 +0100 | [diff] [blame] | 92 | /* |
| 93 | * map_sg returns 0 on error and a value > 0 on success. |
| 94 | * It should never return a value < 0. |
| 95 | */ |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 96 | int (*map_sg)(struct device *dev, struct scatterlist *sg, |
| 97 | int nents, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 98 | unsigned long attrs); |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 99 | void (*unmap_sg)(struct device *dev, |
| 100 | struct scatterlist *sg, int nents, |
| 101 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 102 | unsigned long attrs); |
Niklas Söderlund | ba409b3 | 2016-08-10 13:22:14 +0200 | [diff] [blame] | 103 | dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr, |
| 104 | size_t size, enum dma_data_direction dir, |
| 105 | unsigned long attrs); |
| 106 | void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle, |
| 107 | size_t size, enum dma_data_direction dir, |
| 108 | unsigned long attrs); |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 109 | void (*sync_single_for_cpu)(struct device *dev, |
| 110 | dma_addr_t dma_handle, size_t size, |
| 111 | enum dma_data_direction dir); |
| 112 | void (*sync_single_for_device)(struct device *dev, |
| 113 | dma_addr_t dma_handle, size_t size, |
| 114 | enum dma_data_direction dir); |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 115 | void (*sync_sg_for_cpu)(struct device *dev, |
| 116 | struct scatterlist *sg, int nents, |
| 117 | enum dma_data_direction dir); |
| 118 | void (*sync_sg_for_device)(struct device *dev, |
| 119 | struct scatterlist *sg, int nents, |
| 120 | enum dma_data_direction dir); |
| 121 | int (*mapping_error)(struct device *dev, dma_addr_t dma_addr); |
| 122 | int (*dma_supported)(struct device *dev, u64 mask); |
FUJITA Tomonori | f726f30e | 2009-08-04 19:08:24 +0000 | [diff] [blame] | 123 | int (*set_dma_mask)(struct device *dev, u64 mask); |
Milton Miller | 3a8f755 | 2011-06-24 09:05:23 +0000 | [diff] [blame] | 124 | #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK |
| 125 | u64 (*get_required_mask)(struct device *dev); |
| 126 | #endif |
FUJITA Tomonori | f0402a2 | 2009-01-05 23:59:01 +0900 | [diff] [blame] | 127 | int is_phys; |
| 128 | }; |
| 129 | |
Christian Borntraeger | a8463d4 | 2016-02-02 21:46:32 -0800 | [diff] [blame] | 130 | extern struct dma_map_ops dma_noop_ops; |
| 131 | |
Andrew Morton | 8f286c3 | 2007-10-18 03:05:07 -0700 | [diff] [blame] | 132 | #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) |
Borislav Petkov | 34c6538 | 2007-10-18 03:05:06 -0700 | [diff] [blame] | 133 | |
James Bottomley | 32e8f70 | 2007-10-16 01:23:55 -0700 | [diff] [blame] | 134 | #define DMA_MASK_NONE 0x0ULL |
| 135 | |
Rolf Eike Beer | d6bd3a3 | 2006-09-29 01:59:48 -0700 | [diff] [blame] | 136 | static inline int valid_dma_direction(int dma_direction) |
| 137 | { |
| 138 | return ((dma_direction == DMA_BIDIRECTIONAL) || |
| 139 | (dma_direction == DMA_TO_DEVICE) || |
| 140 | (dma_direction == DMA_FROM_DEVICE)); |
| 141 | } |
| 142 | |
James Bottomley | 32e8f70 | 2007-10-16 01:23:55 -0700 | [diff] [blame] | 143 | static inline int is_device_dma_capable(struct device *dev) |
| 144 | { |
| 145 | return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE; |
| 146 | } |
| 147 | |
Christoph Hellwig | 20d666e | 2016-01-20 15:02:09 -0800 | [diff] [blame] | 148 | #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT |
| 149 | /* |
| 150 | * These three functions are only for dma allocator. |
| 151 | * Don't use them in device drivers. |
| 152 | */ |
| 153 | int dma_alloc_from_coherent(struct device *dev, ssize_t size, |
| 154 | dma_addr_t *dma_handle, void **ret); |
| 155 | int dma_release_from_coherent(struct device *dev, int order, void *vaddr); |
| 156 | |
| 157 | int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma, |
| 158 | void *cpu_addr, size_t size, int *ret); |
| 159 | #else |
| 160 | #define dma_alloc_from_coherent(dev, size, handle, ret) (0) |
| 161 | #define dma_release_from_coherent(dev, order, vaddr) (0) |
| 162 | #define dma_mmap_from_coherent(dev, vma, vaddr, order, ret) (0) |
| 163 | #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */ |
| 164 | |
Dan Williams | 1b0fac4 | 2007-07-15 23:40:26 -0700 | [diff] [blame] | 165 | #ifdef CONFIG_HAS_DMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | #include <asm/dma-mapping.h> |
Dan Williams | 1b0fac4 | 2007-07-15 23:40:26 -0700 | [diff] [blame] | 167 | #else |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 168 | /* |
| 169 | * Define the dma api to allow compilation but not linking of |
| 170 | * dma dependent code. Code that depends on the dma-mapping |
| 171 | * API needs to set 'depends on HAS_DMA' in its Kconfig |
| 172 | */ |
| 173 | extern struct dma_map_ops bad_dma_ops; |
| 174 | static inline struct dma_map_ops *get_dma_ops(struct device *dev) |
| 175 | { |
| 176 | return &bad_dma_ops; |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr, |
| 181 | size_t size, |
| 182 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 183 | unsigned long attrs) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 184 | { |
| 185 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 186 | dma_addr_t addr; |
| 187 | |
| 188 | kmemcheck_mark_initialized(ptr, size); |
| 189 | BUG_ON(!valid_dma_direction(dir)); |
| 190 | addr = ops->map_page(dev, virt_to_page(ptr), |
Geliang Tang | 8e99469 | 2016-01-20 15:02:12 -0800 | [diff] [blame] | 191 | offset_in_page(ptr), size, |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 192 | dir, attrs); |
| 193 | debug_dma_map_page(dev, virt_to_page(ptr), |
Geliang Tang | 8e99469 | 2016-01-20 15:02:12 -0800 | [diff] [blame] | 194 | offset_in_page(ptr), size, |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 195 | dir, addr, true); |
| 196 | return addr; |
| 197 | } |
| 198 | |
| 199 | static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr, |
| 200 | size_t size, |
| 201 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 202 | unsigned long attrs) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 203 | { |
| 204 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 205 | |
| 206 | BUG_ON(!valid_dma_direction(dir)); |
| 207 | if (ops->unmap_page) |
| 208 | ops->unmap_page(dev, addr, size, dir, attrs); |
| 209 | debug_dma_unmap_page(dev, addr, size, dir, true); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * dma_maps_sg_attrs returns 0 on error and > 0 on success. |
| 214 | * It should never return a value < 0. |
| 215 | */ |
| 216 | static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, |
| 217 | int nents, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 218 | unsigned long attrs) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 219 | { |
| 220 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 221 | int i, ents; |
| 222 | struct scatterlist *s; |
| 223 | |
| 224 | for_each_sg(sg, s, nents, i) |
| 225 | kmemcheck_mark_initialized(sg_virt(s), s->length); |
| 226 | BUG_ON(!valid_dma_direction(dir)); |
| 227 | ents = ops->map_sg(dev, sg, nents, dir, attrs); |
| 228 | BUG_ON(ents < 0); |
| 229 | debug_dma_map_sg(dev, sg, nents, ents, dir); |
| 230 | |
| 231 | return ents; |
| 232 | } |
| 233 | |
| 234 | static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, |
| 235 | int nents, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 236 | unsigned long attrs) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 237 | { |
| 238 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 239 | |
| 240 | BUG_ON(!valid_dma_direction(dir)); |
| 241 | debug_dma_unmap_sg(dev, sg, nents, dir); |
| 242 | if (ops->unmap_sg) |
| 243 | ops->unmap_sg(dev, sg, nents, dir, attrs); |
| 244 | } |
| 245 | |
| 246 | static inline dma_addr_t dma_map_page(struct device *dev, struct page *page, |
| 247 | size_t offset, size_t size, |
| 248 | enum dma_data_direction dir) |
| 249 | { |
| 250 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 251 | dma_addr_t addr; |
| 252 | |
| 253 | kmemcheck_mark_initialized(page_address(page) + offset, size); |
| 254 | BUG_ON(!valid_dma_direction(dir)); |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 255 | addr = ops->map_page(dev, page, offset, size, dir, 0); |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 256 | debug_dma_map_page(dev, page, offset, size, dir, addr, false); |
| 257 | |
| 258 | return addr; |
| 259 | } |
| 260 | |
| 261 | static inline void dma_unmap_page(struct device *dev, dma_addr_t addr, |
| 262 | size_t size, enum dma_data_direction dir) |
| 263 | { |
| 264 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 265 | |
| 266 | BUG_ON(!valid_dma_direction(dir)); |
| 267 | if (ops->unmap_page) |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 268 | ops->unmap_page(dev, addr, size, dir, 0); |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 269 | debug_dma_unmap_page(dev, addr, size, dir, false); |
| 270 | } |
| 271 | |
Niklas Söderlund | 6f3d879 | 2016-08-10 13:22:16 +0200 | [diff] [blame] | 272 | static inline dma_addr_t dma_map_resource(struct device *dev, |
| 273 | phys_addr_t phys_addr, |
| 274 | size_t size, |
| 275 | enum dma_data_direction dir, |
| 276 | unsigned long attrs) |
| 277 | { |
| 278 | struct dma_map_ops *ops = get_dma_ops(dev); |
Niklas Söderlund | 6f3d879 | 2016-08-10 13:22:16 +0200 | [diff] [blame] | 279 | dma_addr_t addr; |
| 280 | |
| 281 | BUG_ON(!valid_dma_direction(dir)); |
| 282 | |
| 283 | /* Don't allow RAM to be mapped */ |
Niklas Söderlund | 3757dc4 | 2016-09-29 12:02:40 +0200 | [diff] [blame] | 284 | BUG_ON(pfn_valid(PHYS_PFN(phys_addr))); |
Niklas Söderlund | 6f3d879 | 2016-08-10 13:22:16 +0200 | [diff] [blame] | 285 | |
| 286 | addr = phys_addr; |
| 287 | if (ops->map_resource) |
| 288 | addr = ops->map_resource(dev, phys_addr, size, dir, attrs); |
| 289 | |
| 290 | debug_dma_map_resource(dev, phys_addr, size, dir, addr); |
| 291 | |
| 292 | return addr; |
| 293 | } |
| 294 | |
| 295 | static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr, |
| 296 | size_t size, enum dma_data_direction dir, |
| 297 | unsigned long attrs) |
| 298 | { |
| 299 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 300 | |
| 301 | BUG_ON(!valid_dma_direction(dir)); |
| 302 | if (ops->unmap_resource) |
| 303 | ops->unmap_resource(dev, addr, size, dir, attrs); |
| 304 | debug_dma_unmap_resource(dev, addr, size, dir); |
| 305 | } |
| 306 | |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 307 | static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, |
| 308 | size_t size, |
| 309 | enum dma_data_direction dir) |
| 310 | { |
| 311 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 312 | |
| 313 | BUG_ON(!valid_dma_direction(dir)); |
| 314 | if (ops->sync_single_for_cpu) |
| 315 | ops->sync_single_for_cpu(dev, addr, size, dir); |
| 316 | debug_dma_sync_single_for_cpu(dev, addr, size, dir); |
| 317 | } |
| 318 | |
| 319 | static inline void dma_sync_single_for_device(struct device *dev, |
| 320 | dma_addr_t addr, size_t size, |
| 321 | enum dma_data_direction dir) |
| 322 | { |
| 323 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 324 | |
| 325 | BUG_ON(!valid_dma_direction(dir)); |
| 326 | if (ops->sync_single_for_device) |
| 327 | ops->sync_single_for_device(dev, addr, size, dir); |
| 328 | debug_dma_sync_single_for_device(dev, addr, size, dir); |
| 329 | } |
| 330 | |
| 331 | static inline void dma_sync_single_range_for_cpu(struct device *dev, |
| 332 | dma_addr_t addr, |
| 333 | unsigned long offset, |
| 334 | size_t size, |
| 335 | enum dma_data_direction dir) |
| 336 | { |
| 337 | const struct dma_map_ops *ops = get_dma_ops(dev); |
| 338 | |
| 339 | BUG_ON(!valid_dma_direction(dir)); |
| 340 | if (ops->sync_single_for_cpu) |
| 341 | ops->sync_single_for_cpu(dev, addr + offset, size, dir); |
| 342 | debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir); |
| 343 | } |
| 344 | |
| 345 | static inline void dma_sync_single_range_for_device(struct device *dev, |
| 346 | dma_addr_t addr, |
| 347 | unsigned long offset, |
| 348 | size_t size, |
| 349 | enum dma_data_direction dir) |
| 350 | { |
| 351 | const struct dma_map_ops *ops = get_dma_ops(dev); |
| 352 | |
| 353 | BUG_ON(!valid_dma_direction(dir)); |
| 354 | if (ops->sync_single_for_device) |
| 355 | ops->sync_single_for_device(dev, addr + offset, size, dir); |
| 356 | debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir); |
| 357 | } |
| 358 | |
| 359 | static inline void |
| 360 | dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
| 361 | int nelems, enum dma_data_direction dir) |
| 362 | { |
| 363 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 364 | |
| 365 | BUG_ON(!valid_dma_direction(dir)); |
| 366 | if (ops->sync_sg_for_cpu) |
| 367 | ops->sync_sg_for_cpu(dev, sg, nelems, dir); |
| 368 | debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir); |
| 369 | } |
| 370 | |
| 371 | static inline void |
| 372 | dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
| 373 | int nelems, enum dma_data_direction dir) |
| 374 | { |
| 375 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 376 | |
| 377 | BUG_ON(!valid_dma_direction(dir)); |
| 378 | if (ops->sync_sg_for_device) |
| 379 | ops->sync_sg_for_device(dev, sg, nelems, dir); |
| 380 | debug_dma_sync_sg_for_device(dev, sg, nelems, dir); |
| 381 | |
| 382 | } |
| 383 | |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 384 | #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0) |
| 385 | #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0) |
| 386 | #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0) |
| 387 | #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 388 | |
| 389 | extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, |
| 390 | void *cpu_addr, dma_addr_t dma_addr, size_t size); |
| 391 | |
| 392 | void *dma_common_contiguous_remap(struct page *page, size_t size, |
| 393 | unsigned long vm_flags, |
| 394 | pgprot_t prot, const void *caller); |
| 395 | |
| 396 | void *dma_common_pages_remap(struct page **pages, size_t size, |
| 397 | unsigned long vm_flags, pgprot_t prot, |
| 398 | const void *caller); |
| 399 | void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags); |
| 400 | |
| 401 | /** |
| 402 | * dma_mmap_attrs - map a coherent DMA allocation into user space |
| 403 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 404 | * @vma: vm_area_struct describing requested user mapping |
| 405 | * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs |
| 406 | * @handle: device-view address returned from dma_alloc_attrs |
| 407 | * @size: size of memory originally requested in dma_alloc_attrs |
| 408 | * @attrs: attributes of mapping properties requested in dma_alloc_attrs |
| 409 | * |
| 410 | * Map a coherent DMA buffer previously allocated by dma_alloc_attrs |
| 411 | * into user space. The coherent DMA buffer must not be freed by the |
| 412 | * driver until the user space mapping has been released. |
| 413 | */ |
| 414 | static inline int |
| 415 | dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 416 | dma_addr_t dma_addr, size_t size, unsigned long attrs) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 417 | { |
| 418 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 419 | BUG_ON(!ops); |
| 420 | if (ops->mmap) |
| 421 | return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs); |
| 422 | return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size); |
| 423 | } |
| 424 | |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 425 | #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 426 | |
| 427 | int |
| 428 | dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 429 | void *cpu_addr, dma_addr_t dma_addr, size_t size); |
| 430 | |
| 431 | static inline int |
| 432 | dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 433 | dma_addr_t dma_addr, size_t size, |
| 434 | unsigned long attrs) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 435 | { |
| 436 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 437 | BUG_ON(!ops); |
| 438 | if (ops->get_sgtable) |
| 439 | return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, |
| 440 | attrs); |
| 441 | return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size); |
| 442 | } |
| 443 | |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 444 | #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 445 | |
| 446 | #ifndef arch_dma_alloc_attrs |
| 447 | #define arch_dma_alloc_attrs(dev, flag) (true) |
| 448 | #endif |
| 449 | |
| 450 | static inline void *dma_alloc_attrs(struct device *dev, size_t size, |
| 451 | dma_addr_t *dma_handle, gfp_t flag, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 452 | unsigned long attrs) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 453 | { |
| 454 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 455 | void *cpu_addr; |
| 456 | |
| 457 | BUG_ON(!ops); |
| 458 | |
| 459 | if (dma_alloc_from_coherent(dev, size, dma_handle, &cpu_addr)) |
| 460 | return cpu_addr; |
| 461 | |
| 462 | if (!arch_dma_alloc_attrs(&dev, &flag)) |
| 463 | return NULL; |
| 464 | if (!ops->alloc) |
| 465 | return NULL; |
| 466 | |
| 467 | cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs); |
| 468 | debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr); |
| 469 | return cpu_addr; |
| 470 | } |
| 471 | |
| 472 | static inline void dma_free_attrs(struct device *dev, size_t size, |
| 473 | void *cpu_addr, dma_addr_t dma_handle, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 474 | unsigned long attrs) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 475 | { |
| 476 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 477 | |
| 478 | BUG_ON(!ops); |
| 479 | WARN_ON(irqs_disabled()); |
| 480 | |
| 481 | if (dma_release_from_coherent(dev, get_order(size), cpu_addr)) |
| 482 | return; |
| 483 | |
Zhen Lei | d6b7eae | 2016-03-09 14:08:38 -0800 | [diff] [blame] | 484 | if (!ops->free || !cpu_addr) |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 485 | return; |
| 486 | |
| 487 | debug_dma_free_coherent(dev, size, cpu_addr, dma_handle); |
| 488 | ops->free(dev, size, cpu_addr, dma_handle, attrs); |
| 489 | } |
| 490 | |
| 491 | static inline void *dma_alloc_coherent(struct device *dev, size_t size, |
| 492 | dma_addr_t *dma_handle, gfp_t flag) |
| 493 | { |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 494 | return dma_alloc_attrs(dev, size, dma_handle, flag, 0); |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static inline void dma_free_coherent(struct device *dev, size_t size, |
| 498 | void *cpu_addr, dma_addr_t dma_handle) |
| 499 | { |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 500 | return dma_free_attrs(dev, size, cpu_addr, dma_handle, 0); |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static inline void *dma_alloc_noncoherent(struct device *dev, size_t size, |
| 504 | dma_addr_t *dma_handle, gfp_t gfp) |
| 505 | { |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 506 | return dma_alloc_attrs(dev, size, dma_handle, gfp, |
| 507 | DMA_ATTR_NON_CONSISTENT); |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static inline void dma_free_noncoherent(struct device *dev, size_t size, |
| 511 | void *cpu_addr, dma_addr_t dma_handle) |
| 512 | { |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 513 | dma_free_attrs(dev, size, cpu_addr, dma_handle, |
| 514 | DMA_ATTR_NON_CONSISTENT); |
Christoph Hellwig | e1c7e32 | 2016-01-20 15:02:05 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 518 | { |
| 519 | debug_dma_mapping_error(dev, dma_addr); |
| 520 | |
| 521 | if (get_dma_ops(dev)->mapping_error) |
| 522 | return get_dma_ops(dev)->mapping_error(dev, dma_addr); |
| 523 | |
| 524 | #ifdef DMA_ERROR_CODE |
| 525 | return dma_addr == DMA_ERROR_CODE; |
| 526 | #else |
| 527 | return 0; |
| 528 | #endif |
| 529 | } |
| 530 | |
| 531 | #ifndef HAVE_ARCH_DMA_SUPPORTED |
| 532 | static inline int dma_supported(struct device *dev, u64 mask) |
| 533 | { |
| 534 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 535 | |
| 536 | if (!ops) |
| 537 | return 0; |
| 538 | if (!ops->dma_supported) |
| 539 | return 1; |
| 540 | return ops->dma_supported(dev, mask); |
| 541 | } |
| 542 | #endif |
| 543 | |
| 544 | #ifndef HAVE_ARCH_DMA_SET_MASK |
| 545 | static inline int dma_set_mask(struct device *dev, u64 mask) |
| 546 | { |
| 547 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 548 | |
| 549 | if (ops->set_dma_mask) |
| 550 | return ops->set_dma_mask(dev, mask); |
| 551 | |
| 552 | if (!dev->dma_mask || !dma_supported(dev, mask)) |
| 553 | return -EIO; |
| 554 | *dev->dma_mask = mask; |
| 555 | return 0; |
| 556 | } |
Dan Williams | 1b0fac4 | 2007-07-15 23:40:26 -0700 | [diff] [blame] | 557 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
FUJITA Tomonori | 589fc9a | 2008-09-12 19:42:34 +0900 | [diff] [blame] | 559 | static inline u64 dma_get_mask(struct device *dev) |
| 560 | { |
FUJITA Tomonori | 07a2c01 | 2008-09-19 02:02:05 +0900 | [diff] [blame] | 561 | if (dev && dev->dma_mask && *dev->dma_mask) |
FUJITA Tomonori | 589fc9a | 2008-09-12 19:42:34 +0900 | [diff] [blame] | 562 | return *dev->dma_mask; |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 563 | return DMA_BIT_MASK(32); |
FUJITA Tomonori | 589fc9a | 2008-09-12 19:42:34 +0900 | [diff] [blame] | 564 | } |
| 565 | |
Rob Herring | 58af4a2 | 2012-03-20 14:33:01 -0500 | [diff] [blame] | 566 | #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK |
FUJITA Tomonori | 710224f | 2010-09-22 13:04:55 -0700 | [diff] [blame] | 567 | int dma_set_coherent_mask(struct device *dev, u64 mask); |
| 568 | #else |
FUJITA Tomonori | 6a1961f | 2010-03-10 15:23:39 -0800 | [diff] [blame] | 569 | static inline int dma_set_coherent_mask(struct device *dev, u64 mask) |
| 570 | { |
| 571 | if (!dma_supported(dev, mask)) |
| 572 | return -EIO; |
| 573 | dev->coherent_dma_mask = mask; |
| 574 | return 0; |
| 575 | } |
FUJITA Tomonori | 710224f | 2010-09-22 13:04:55 -0700 | [diff] [blame] | 576 | #endif |
FUJITA Tomonori | 6a1961f | 2010-03-10 15:23:39 -0800 | [diff] [blame] | 577 | |
Russell King | 4aa806b | 2013-06-26 13:49:44 +0100 | [diff] [blame] | 578 | /* |
| 579 | * Set both the DMA mask and the coherent DMA mask to the same thing. |
| 580 | * Note that we don't check the return value from dma_set_coherent_mask() |
| 581 | * as the DMA API guarantees that the coherent DMA mask can be set to |
| 582 | * the same or smaller than the streaming DMA mask. |
| 583 | */ |
| 584 | static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask) |
| 585 | { |
| 586 | int rc = dma_set_mask(dev, mask); |
| 587 | if (rc == 0) |
| 588 | dma_set_coherent_mask(dev, mask); |
| 589 | return rc; |
| 590 | } |
| 591 | |
Russell King | fa6a8d6 | 2013-06-27 12:21:45 +0100 | [diff] [blame] | 592 | /* |
| 593 | * Similar to the above, except it deals with the case where the device |
| 594 | * does not have dev->dma_mask appropriately setup. |
| 595 | */ |
| 596 | static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask) |
| 597 | { |
| 598 | dev->dma_mask = &dev->coherent_dma_mask; |
| 599 | return dma_set_mask_and_coherent(dev, mask); |
| 600 | } |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | extern u64 dma_get_required_mask(struct device *dev); |
| 603 | |
Will Deacon | a3a60f8 | 2014-08-27 15:49:10 +0100 | [diff] [blame] | 604 | #ifndef arch_setup_dma_ops |
Will Deacon | 97890ba | 2014-08-27 16:24:20 +0100 | [diff] [blame] | 605 | static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, |
Robin Murphy | 53c92d7 | 2016-04-07 18:42:05 +0100 | [diff] [blame] | 606 | u64 size, const struct iommu_ops *iommu, |
Will Deacon | 97890ba | 2014-08-27 16:24:20 +0100 | [diff] [blame] | 607 | bool coherent) { } |
| 608 | #endif |
| 609 | |
| 610 | #ifndef arch_teardown_dma_ops |
| 611 | static inline void arch_teardown_dma_ops(struct device *dev) { } |
Santosh Shilimkar | 591c1ee | 2014-04-24 11:30:04 -0400 | [diff] [blame] | 612 | #endif |
| 613 | |
FUJITA Tomonori | 6b7b651 | 2008-02-04 22:27:55 -0800 | [diff] [blame] | 614 | static inline unsigned int dma_get_max_seg_size(struct device *dev) |
| 615 | { |
Robin Murphy | 002edb6 | 2015-11-06 16:32:51 -0800 | [diff] [blame] | 616 | if (dev->dma_parms && dev->dma_parms->max_segment_size) |
| 617 | return dev->dma_parms->max_segment_size; |
| 618 | return SZ_64K; |
FUJITA Tomonori | 6b7b651 | 2008-02-04 22:27:55 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static inline unsigned int dma_set_max_seg_size(struct device *dev, |
| 622 | unsigned int size) |
| 623 | { |
| 624 | if (dev->dma_parms) { |
| 625 | dev->dma_parms->max_segment_size = size; |
| 626 | return 0; |
Robin Murphy | 002edb6 | 2015-11-06 16:32:51 -0800 | [diff] [blame] | 627 | } |
| 628 | return -EIO; |
FUJITA Tomonori | 6b7b651 | 2008-02-04 22:27:55 -0800 | [diff] [blame] | 629 | } |
| 630 | |
FUJITA Tomonori | d22a696 | 2008-02-04 22:28:13 -0800 | [diff] [blame] | 631 | static inline unsigned long dma_get_seg_boundary(struct device *dev) |
| 632 | { |
Robin Murphy | 002edb6 | 2015-11-06 16:32:51 -0800 | [diff] [blame] | 633 | if (dev->dma_parms && dev->dma_parms->segment_boundary_mask) |
| 634 | return dev->dma_parms->segment_boundary_mask; |
| 635 | return DMA_BIT_MASK(32); |
FUJITA Tomonori | d22a696 | 2008-02-04 22:28:13 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask) |
| 639 | { |
| 640 | if (dev->dma_parms) { |
| 641 | dev->dma_parms->segment_boundary_mask = mask; |
| 642 | return 0; |
Robin Murphy | 002edb6 | 2015-11-06 16:32:51 -0800 | [diff] [blame] | 643 | } |
| 644 | return -EIO; |
FUJITA Tomonori | d22a696 | 2008-02-04 22:28:13 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Santosh Shilimkar | 00c8f16 | 2013-07-29 14:18:48 +0100 | [diff] [blame] | 647 | #ifndef dma_max_pfn |
| 648 | static inline unsigned long dma_max_pfn(struct device *dev) |
| 649 | { |
| 650 | return *dev->dma_mask >> PAGE_SHIFT; |
| 651 | } |
| 652 | #endif |
| 653 | |
Andrew Morton | 842fa69 | 2011-11-02 13:39:33 -0700 | [diff] [blame] | 654 | static inline void *dma_zalloc_coherent(struct device *dev, size_t size, |
| 655 | dma_addr_t *dma_handle, gfp_t flag) |
| 656 | { |
Joe Perches | ede23fa | 2013-08-26 22:45:23 -0700 | [diff] [blame] | 657 | void *ret = dma_alloc_coherent(dev, size, dma_handle, |
| 658 | flag | __GFP_ZERO); |
Andrew Morton | 842fa69 | 2011-11-02 13:39:33 -0700 | [diff] [blame] | 659 | return ret; |
| 660 | } |
| 661 | |
Heiko Carstens | e259f19 | 2010-08-13 09:39:18 +0200 | [diff] [blame] | 662 | #ifdef CONFIG_HAS_DMA |
FUJITA Tomonori | 4565f01 | 2010-08-10 18:03:22 -0700 | [diff] [blame] | 663 | static inline int dma_get_cache_alignment(void) |
| 664 | { |
| 665 | #ifdef ARCH_DMA_MINALIGN |
| 666 | return ARCH_DMA_MINALIGN; |
| 667 | #endif |
| 668 | return 1; |
| 669 | } |
Heiko Carstens | e259f19 | 2010-08-13 09:39:18 +0200 | [diff] [blame] | 670 | #endif |
FUJITA Tomonori | 4565f01 | 2010-08-10 18:03:22 -0700 | [diff] [blame] | 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | /* flags for the coherent memory api */ |
| 673 | #define DMA_MEMORY_MAP 0x01 |
| 674 | #define DMA_MEMORY_IO 0x02 |
| 675 | #define DMA_MEMORY_INCLUDES_CHILDREN 0x04 |
| 676 | #define DMA_MEMORY_EXCLUSIVE 0x08 |
| 677 | |
Christoph Hellwig | 20d666e | 2016-01-20 15:02:09 -0800 | [diff] [blame] | 678 | #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT |
| 679 | int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, |
| 680 | dma_addr_t device_addr, size_t size, int flags); |
| 681 | void dma_release_declared_memory(struct device *dev); |
| 682 | void *dma_mark_declared_memory_occupied(struct device *dev, |
| 683 | dma_addr_t device_addr, size_t size); |
| 684 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | static inline int |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 686 | dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | dma_addr_t device_addr, size_t size, int flags) |
| 688 | { |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static inline void |
| 693 | dma_release_declared_memory(struct device *dev) |
| 694 | { |
| 695 | } |
| 696 | |
| 697 | static inline void * |
| 698 | dma_mark_declared_memory_occupied(struct device *dev, |
| 699 | dma_addr_t device_addr, size_t size) |
| 700 | { |
| 701 | return ERR_PTR(-EBUSY); |
| 702 | } |
Christoph Hellwig | 20d666e | 2016-01-20 15:02:09 -0800 | [diff] [blame] | 703 | #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 705 | /* |
| 706 | * Managed DMA API |
| 707 | */ |
| 708 | extern void *dmam_alloc_coherent(struct device *dev, size_t size, |
| 709 | dma_addr_t *dma_handle, gfp_t gfp); |
| 710 | extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr, |
| 711 | dma_addr_t dma_handle); |
| 712 | extern void *dmam_alloc_noncoherent(struct device *dev, size_t size, |
| 713 | dma_addr_t *dma_handle, gfp_t gfp); |
| 714 | extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr, |
| 715 | dma_addr_t dma_handle); |
Christoph Hellwig | 20d666e | 2016-01-20 15:02:09 -0800 | [diff] [blame] | 716 | #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 717 | extern int dmam_declare_coherent_memory(struct device *dev, |
| 718 | phys_addr_t phys_addr, |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 719 | dma_addr_t device_addr, size_t size, |
| 720 | int flags); |
| 721 | extern void dmam_release_declared_memory(struct device *dev); |
Christoph Hellwig | 20d666e | 2016-01-20 15:02:09 -0800 | [diff] [blame] | 722 | #else /* CONFIG_HAVE_GENERIC_DMA_COHERENT */ |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 723 | static inline int dmam_declare_coherent_memory(struct device *dev, |
Bjorn Helgaas | 88a984b | 2014-05-20 16:54:22 -0600 | [diff] [blame] | 724 | phys_addr_t phys_addr, dma_addr_t device_addr, |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 725 | size_t size, gfp_t gfp) |
| 726 | { |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static inline void dmam_release_declared_memory(struct device *dev) |
| 731 | { |
| 732 | } |
Christoph Hellwig | 20d666e | 2016-01-20 15:02:09 -0800 | [diff] [blame] | 733 | #endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */ |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 734 | |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 735 | static inline void *dma_alloc_wc(struct device *dev, size_t size, |
| 736 | dma_addr_t *dma_addr, gfp_t gfp) |
Thierry Reding | b4bbb10 | 2014-06-27 11:56:58 +0200 | [diff] [blame] | 737 | { |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 738 | return dma_alloc_attrs(dev, size, dma_addr, gfp, |
| 739 | DMA_ATTR_WRITE_COMBINE); |
Thierry Reding | b4bbb10 | 2014-06-27 11:56:58 +0200 | [diff] [blame] | 740 | } |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 741 | #ifndef dma_alloc_writecombine |
| 742 | #define dma_alloc_writecombine dma_alloc_wc |
| 743 | #endif |
Thierry Reding | b4bbb10 | 2014-06-27 11:56:58 +0200 | [diff] [blame] | 744 | |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 745 | static inline void dma_free_wc(struct device *dev, size_t size, |
| 746 | void *cpu_addr, dma_addr_t dma_addr) |
Thierry Reding | b4bbb10 | 2014-06-27 11:56:58 +0200 | [diff] [blame] | 747 | { |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 748 | return dma_free_attrs(dev, size, cpu_addr, dma_addr, |
| 749 | DMA_ATTR_WRITE_COMBINE); |
Thierry Reding | b4bbb10 | 2014-06-27 11:56:58 +0200 | [diff] [blame] | 750 | } |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 751 | #ifndef dma_free_writecombine |
| 752 | #define dma_free_writecombine dma_free_wc |
| 753 | #endif |
Thierry Reding | b4bbb10 | 2014-06-27 11:56:58 +0200 | [diff] [blame] | 754 | |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 755 | static inline int dma_mmap_wc(struct device *dev, |
| 756 | struct vm_area_struct *vma, |
| 757 | void *cpu_addr, dma_addr_t dma_addr, |
| 758 | size_t size) |
Thierry Reding | b4bbb10 | 2014-06-27 11:56:58 +0200 | [diff] [blame] | 759 | { |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 760 | return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, |
| 761 | DMA_ATTR_WRITE_COMBINE); |
Thierry Reding | b4bbb10 | 2014-06-27 11:56:58 +0200 | [diff] [blame] | 762 | } |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 763 | #ifndef dma_mmap_writecombine |
| 764 | #define dma_mmap_writecombine dma_mmap_wc |
| 765 | #endif |
Arthur Kepner | 74bc7ce | 2008-04-29 01:00:30 -0700 | [diff] [blame] | 766 | |
Andrey Smirnov | 2481366 | 2016-09-28 15:22:33 -0700 | [diff] [blame] | 767 | #if defined(CONFIG_NEED_DMA_MAP_STATE) || defined(CONFIG_DMA_API_DEBUG) |
FUJITA Tomonori | 0acedc1 | 2010-03-10 15:23:31 -0800 | [diff] [blame] | 768 | #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) dma_addr_t ADDR_NAME |
| 769 | #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) __u32 LEN_NAME |
| 770 | #define dma_unmap_addr(PTR, ADDR_NAME) ((PTR)->ADDR_NAME) |
| 771 | #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) (((PTR)->ADDR_NAME) = (VAL)) |
| 772 | #define dma_unmap_len(PTR, LEN_NAME) ((PTR)->LEN_NAME) |
| 773 | #define dma_unmap_len_set(PTR, LEN_NAME, VAL) (((PTR)->LEN_NAME) = (VAL)) |
| 774 | #else |
| 775 | #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME) |
| 776 | #define DEFINE_DMA_UNMAP_LEN(LEN_NAME) |
| 777 | #define dma_unmap_addr(PTR, ADDR_NAME) (0) |
| 778 | #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL) do { } while (0) |
| 779 | #define dma_unmap_len(PTR, LEN_NAME) (0) |
| 780 | #define dma_unmap_len_set(PTR, LEN_NAME, VAL) do { } while (0) |
| 781 | #endif |
| 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | #endif |