Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * This header is for implementations of dma_map_ops and related code. |
| 4 | * It should not be included in drivers just using the DMA API. |
| 5 | */ |
| 6 | #ifndef _LINUX_DMA_MAP_OPS_H |
| 7 | #define _LINUX_DMA_MAP_OPS_H |
| 8 | |
| 9 | #include <linux/dma-mapping.h> |
Christoph Hellwig | 9f4df96 | 2020-09-22 15:36:11 +0200 | [diff] [blame] | 10 | #include <linux/pgtable.h> |
Greg Kroah-Hartman | 774168d | 2020-05-02 09:36:40 +0200 | [diff] [blame] | 11 | #include <linux/android_kabi.h> |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 12 | |
Christoph Hellwig | 0b1abd1 | 2020-09-11 10:56:52 +0200 | [diff] [blame] | 13 | struct cma; |
| 14 | |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 15 | struct dma_map_ops { |
| 16 | void *(*alloc)(struct device *dev, size_t size, |
| 17 | dma_addr_t *dma_handle, gfp_t gfp, |
| 18 | unsigned long attrs); |
| 19 | void (*free)(struct device *dev, size_t size, void *vaddr, |
| 20 | dma_addr_t dma_handle, unsigned long attrs); |
| 21 | struct page *(*alloc_pages)(struct device *dev, size_t size, |
| 22 | dma_addr_t *dma_handle, enum dma_data_direction dir, |
| 23 | gfp_t gfp); |
| 24 | void (*free_pages)(struct device *dev, size_t size, struct page *vaddr, |
| 25 | dma_addr_t dma_handle, enum dma_data_direction dir); |
| 26 | void *(*alloc_noncoherent)(struct device *dev, size_t size, |
| 27 | dma_addr_t *dma_handle, enum dma_data_direction dir, |
| 28 | gfp_t gfp); |
| 29 | void (*free_noncoherent)(struct device *dev, size_t size, void *vaddr, |
| 30 | dma_addr_t dma_handle, enum dma_data_direction dir); |
| 31 | int (*mmap)(struct device *, struct vm_area_struct *, |
| 32 | void *, dma_addr_t, size_t, unsigned long attrs); |
| 33 | |
| 34 | int (*get_sgtable)(struct device *dev, struct sg_table *sgt, |
| 35 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 36 | unsigned long attrs); |
| 37 | |
| 38 | dma_addr_t (*map_page)(struct device *dev, struct page *page, |
| 39 | unsigned long offset, size_t size, |
| 40 | enum dma_data_direction dir, unsigned long attrs); |
| 41 | void (*unmap_page)(struct device *dev, dma_addr_t dma_handle, |
| 42 | size_t size, enum dma_data_direction dir, |
| 43 | unsigned long attrs); |
| 44 | /* |
| 45 | * map_sg returns 0 on error and a value > 0 on success. |
| 46 | * It should never return a value < 0. |
| 47 | */ |
| 48 | int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents, |
| 49 | enum dma_data_direction dir, unsigned long attrs); |
| 50 | void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents, |
| 51 | enum dma_data_direction dir, unsigned long attrs); |
| 52 | dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr, |
| 53 | size_t size, enum dma_data_direction dir, |
| 54 | unsigned long attrs); |
| 55 | void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle, |
| 56 | size_t size, enum dma_data_direction dir, |
| 57 | unsigned long attrs); |
| 58 | void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle, |
| 59 | size_t size, enum dma_data_direction dir); |
| 60 | void (*sync_single_for_device)(struct device *dev, |
| 61 | dma_addr_t dma_handle, size_t size, |
| 62 | enum dma_data_direction dir); |
| 63 | void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, |
| 64 | int nents, enum dma_data_direction dir); |
| 65 | void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg, |
| 66 | int nents, enum dma_data_direction dir); |
| 67 | void (*cache_sync)(struct device *dev, void *vaddr, size_t size, |
| 68 | enum dma_data_direction direction); |
| 69 | int (*dma_supported)(struct device *dev, u64 mask); |
| 70 | u64 (*get_required_mask)(struct device *dev); |
| 71 | size_t (*max_mapping_size)(struct device *dev); |
| 72 | unsigned long (*get_merge_boundary)(struct device *dev); |
Greg Kroah-Hartman | 774168d | 2020-05-02 09:36:40 +0200 | [diff] [blame] | 73 | |
| 74 | ANDROID_KABI_RESERVE(1); |
| 75 | ANDROID_KABI_RESERVE(2); |
| 76 | ANDROID_KABI_RESERVE(3); |
| 77 | ANDROID_KABI_RESERVE(4); |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | #ifdef CONFIG_DMA_OPS |
| 81 | #include <asm/dma-mapping.h> |
| 82 | |
| 83 | static inline const struct dma_map_ops *get_dma_ops(struct device *dev) |
| 84 | { |
| 85 | if (dev->dma_ops) |
| 86 | return dev->dma_ops; |
| 87 | return get_arch_dma_ops(dev->bus); |
| 88 | } |
| 89 | |
| 90 | static inline void set_dma_ops(struct device *dev, |
| 91 | const struct dma_map_ops *dma_ops) |
| 92 | { |
| 93 | dev->dma_ops = dma_ops; |
| 94 | } |
| 95 | #else /* CONFIG_DMA_OPS */ |
| 96 | static inline const struct dma_map_ops *get_dma_ops(struct device *dev) |
| 97 | { |
| 98 | return NULL; |
| 99 | } |
| 100 | static inline void set_dma_ops(struct device *dev, |
| 101 | const struct dma_map_ops *dma_ops) |
| 102 | { |
| 103 | } |
| 104 | #endif /* CONFIG_DMA_OPS */ |
| 105 | |
Christoph Hellwig | 0b1abd1 | 2020-09-11 10:56:52 +0200 | [diff] [blame] | 106 | #ifdef CONFIG_DMA_CMA |
| 107 | extern struct cma *dma_contiguous_default_area; |
| 108 | |
| 109 | static inline struct cma *dev_get_cma_area(struct device *dev) |
| 110 | { |
| 111 | if (dev && dev->cma_area) |
| 112 | return dev->cma_area; |
| 113 | return dma_contiguous_default_area; |
| 114 | } |
| 115 | |
| 116 | void dma_contiguous_reserve(phys_addr_t addr_limit); |
| 117 | int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, |
| 118 | phys_addr_t limit, struct cma **res_cma, bool fixed); |
| 119 | |
| 120 | struct page *dma_alloc_from_contiguous(struct device *dev, size_t count, |
| 121 | unsigned int order, bool no_warn); |
| 122 | bool dma_release_from_contiguous(struct device *dev, struct page *pages, |
| 123 | int count); |
| 124 | struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp); |
| 125 | void dma_free_contiguous(struct device *dev, struct page *page, size_t size); |
Christoph Hellwig | 5db5d93 | 2020-09-11 11:04:43 +0200 | [diff] [blame] | 126 | |
| 127 | void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size); |
Christoph Hellwig | 0b1abd1 | 2020-09-11 10:56:52 +0200 | [diff] [blame] | 128 | #else /* CONFIG_DMA_CMA */ |
| 129 | static inline struct cma *dev_get_cma_area(struct device *dev) |
| 130 | { |
| 131 | return NULL; |
| 132 | } |
| 133 | static inline void dma_contiguous_reserve(phys_addr_t limit) |
| 134 | { |
| 135 | } |
| 136 | static inline int dma_contiguous_reserve_area(phys_addr_t size, |
| 137 | phys_addr_t base, phys_addr_t limit, struct cma **res_cma, |
| 138 | bool fixed) |
| 139 | { |
| 140 | return -ENOSYS; |
| 141 | } |
| 142 | static inline struct page *dma_alloc_from_contiguous(struct device *dev, |
| 143 | size_t count, unsigned int order, bool no_warn) |
| 144 | { |
| 145 | return NULL; |
| 146 | } |
| 147 | static inline bool dma_release_from_contiguous(struct device *dev, |
| 148 | struct page *pages, int count) |
| 149 | { |
| 150 | return false; |
| 151 | } |
| 152 | /* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */ |
| 153 | static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size, |
| 154 | gfp_t gfp) |
| 155 | { |
| 156 | return NULL; |
| 157 | } |
| 158 | static inline void dma_free_contiguous(struct device *dev, struct page *page, |
| 159 | size_t size) |
| 160 | { |
| 161 | __free_pages(page, get_order(size)); |
| 162 | } |
| 163 | #endif /* CONFIG_DMA_CMA*/ |
| 164 | |
| 165 | #ifdef CONFIG_DMA_PERNUMA_CMA |
| 166 | void dma_pernuma_cma_reserve(void); |
| 167 | #else |
| 168 | static inline void dma_pernuma_cma_reserve(void) { } |
| 169 | #endif /* CONFIG_DMA_PERNUMA_CMA */ |
| 170 | |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 171 | #ifdef CONFIG_DMA_DECLARE_COHERENT |
| 172 | int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, |
| 173 | dma_addr_t device_addr, size_t size); |
| 174 | int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size, |
| 175 | dma_addr_t *dma_handle, void **ret); |
| 176 | int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr); |
| 177 | int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma, |
| 178 | void *cpu_addr, size_t size, int *ret); |
| 179 | |
| 180 | void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size, |
| 181 | dma_addr_t *dma_handle); |
| 182 | int dma_release_from_global_coherent(int order, void *vaddr); |
| 183 | int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr, |
| 184 | size_t size, int *ret); |
| 185 | |
| 186 | #else |
| 187 | static inline int dma_declare_coherent_memory(struct device *dev, |
| 188 | phys_addr_t phys_addr, dma_addr_t device_addr, size_t size) |
| 189 | { |
| 190 | return -ENOSYS; |
| 191 | } |
| 192 | #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0) |
| 193 | #define dma_release_from_dev_coherent(dev, order, vaddr) (0) |
| 194 | #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0) |
| 195 | |
| 196 | static inline void *dma_alloc_from_global_coherent(struct device *dev, |
| 197 | ssize_t size, dma_addr_t *dma_handle) |
| 198 | { |
| 199 | return NULL; |
| 200 | } |
| 201 | static inline int dma_release_from_global_coherent(int order, void *vaddr) |
| 202 | { |
| 203 | return 0; |
| 204 | } |
| 205 | static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma, |
| 206 | void *cpu_addr, size_t size, int *ret) |
| 207 | { |
| 208 | return 0; |
| 209 | } |
| 210 | #endif /* CONFIG_DMA_DECLARE_COHERENT */ |
| 211 | |
Christoph Hellwig | 695cebe | 2020-10-20 10:41:07 +0200 | [diff] [blame] | 212 | int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 213 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 214 | unsigned long attrs); |
| 215 | int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, |
| 216 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 217 | unsigned long attrs); |
| 218 | struct page *dma_common_alloc_pages(struct device *dev, size_t size, |
| 219 | dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp); |
| 220 | void dma_common_free_pages(struct device *dev, size_t size, struct page *vaddr, |
| 221 | dma_addr_t dma_handle, enum dma_data_direction dir); |
| 222 | |
| 223 | struct page **dma_common_find_pages(void *cpu_addr); |
| 224 | void *dma_common_contiguous_remap(struct page *page, size_t size, pgprot_t prot, |
| 225 | const void *caller); |
| 226 | void *dma_common_pages_remap(struct page **pages, size_t size, pgprot_t prot, |
| 227 | const void *caller); |
| 228 | void dma_common_free_remap(void *cpu_addr, size_t size); |
| 229 | |
| 230 | struct page *dma_alloc_from_pool(struct device *dev, size_t size, |
| 231 | void **cpu_addr, gfp_t flags, |
| 232 | bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t)); |
| 233 | bool dma_free_from_pool(struct device *dev, void *start, size_t size); |
| 234 | |
Christoph Hellwig | 9f4df96 | 2020-09-22 15:36:11 +0200 | [diff] [blame] | 235 | #ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H |
| 236 | #include <asm/dma-coherence.h> |
| 237 | #elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ |
| 238 | defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ |
| 239 | defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) |
| 240 | static inline bool dev_is_dma_coherent(struct device *dev) |
| 241 | { |
| 242 | return dev->dma_coherent; |
| 243 | } |
| 244 | #else |
| 245 | static inline bool dev_is_dma_coherent(struct device *dev) |
| 246 | { |
| 247 | return true; |
| 248 | } |
| 249 | #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */ |
| 250 | |
Christoph Hellwig | 9f4df96 | 2020-09-22 15:36:11 +0200 | [diff] [blame] | 251 | void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, |
| 252 | gfp_t gfp, unsigned long attrs); |
| 253 | void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 254 | dma_addr_t dma_addr, unsigned long attrs); |
| 255 | |
| 256 | #ifdef CONFIG_MMU |
| 257 | /* |
| 258 | * Page protection so that devices that can't snoop CPU caches can use the |
| 259 | * memory coherently. We default to pgprot_noncached which is usually used |
| 260 | * for ioremap as a safe bet, but architectures can override this with less |
| 261 | * strict semantics if possible. |
| 262 | */ |
| 263 | #ifndef pgprot_dmacoherent |
| 264 | #define pgprot_dmacoherent(prot) pgprot_noncached(prot) |
| 265 | #endif |
| 266 | |
Isaac J. Manjarres | 261c508 | 2021-01-12 14:17:41 -0800 | [diff] [blame] | 267 | /* |
| 268 | * If there is no system cache pgprot, then fallback to dmacoherent |
| 269 | * pgprot, as the expectation is that the device is not coherent. |
| 270 | */ |
| 271 | #ifndef pgprot_syscached |
| 272 | #define pgprot_syscached(prot) pgprot_dmacoherent(prot) |
| 273 | #endif |
| 274 | |
Christoph Hellwig | 9f4df96 | 2020-09-22 15:36:11 +0200 | [diff] [blame] | 275 | pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs); |
| 276 | #else |
| 277 | static inline pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, |
| 278 | unsigned long attrs) |
| 279 | { |
| 280 | return prot; /* no protection bits supported without page tables */ |
| 281 | } |
| 282 | #endif /* CONFIG_MMU */ |
| 283 | |
| 284 | #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE |
| 285 | void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, |
| 286 | enum dma_data_direction dir); |
| 287 | #else |
| 288 | static inline void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, |
| 289 | enum dma_data_direction dir) |
| 290 | { |
| 291 | } |
| 292 | #endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */ |
| 293 | |
| 294 | #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU |
| 295 | void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, |
| 296 | enum dma_data_direction dir); |
| 297 | #else |
| 298 | static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, |
| 299 | enum dma_data_direction dir) |
| 300 | { |
| 301 | } |
| 302 | #endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */ |
| 303 | |
| 304 | #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL |
| 305 | void arch_sync_dma_for_cpu_all(void); |
| 306 | #else |
| 307 | static inline void arch_sync_dma_for_cpu_all(void) |
| 308 | { |
| 309 | } |
| 310 | #endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */ |
| 311 | |
| 312 | #ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT |
| 313 | void arch_dma_prep_coherent(struct page *page, size_t size); |
| 314 | #else |
| 315 | static inline void arch_dma_prep_coherent(struct page *page, size_t size) |
| 316 | { |
| 317 | } |
| 318 | #endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */ |
| 319 | |
| 320 | #ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN |
| 321 | void arch_dma_mark_clean(phys_addr_t paddr, size_t size); |
| 322 | #else |
| 323 | static inline void arch_dma_mark_clean(phys_addr_t paddr, size_t size) |
| 324 | { |
| 325 | } |
| 326 | #endif /* ARCH_HAS_DMA_MARK_CLEAN */ |
| 327 | |
| 328 | void *arch_dma_set_uncached(void *addr, size_t size); |
| 329 | void arch_dma_clear_uncached(void *addr, size_t size); |
| 330 | |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 331 | #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS |
| 332 | void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, |
| 333 | const struct iommu_ops *iommu, bool coherent); |
| 334 | #else |
| 335 | static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, |
| 336 | u64 size, const struct iommu_ops *iommu, bool coherent) |
| 337 | { |
| 338 | } |
| 339 | #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */ |
| 340 | |
| 341 | #ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS |
| 342 | void arch_teardown_dma_ops(struct device *dev); |
| 343 | #else |
| 344 | static inline void arch_teardown_dma_ops(struct device *dev) |
| 345 | { |
| 346 | } |
| 347 | #endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */ |
| 348 | |
Christoph Hellwig | a1fd09e | 2020-09-11 10:12:44 +0200 | [diff] [blame] | 349 | #ifdef CONFIG_DMA_API_DEBUG |
| 350 | void dma_debug_add_bus(struct bus_type *bus); |
| 351 | void debug_dma_dump_mappings(struct device *dev); |
| 352 | #else |
| 353 | static inline void dma_debug_add_bus(struct bus_type *bus) |
| 354 | { |
| 355 | } |
| 356 | static inline void debug_dma_dump_mappings(struct device *dev) |
| 357 | { |
| 358 | } |
| 359 | #endif /* CONFIG_DMA_API_DEBUG */ |
| 360 | |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 361 | extern const struct dma_map_ops dma_dummy_ops; |
| 362 | |
| 363 | #endif /* _LINUX_DMA_MAP_OPS_H */ |