at v6.18 15 kB view raw
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> 10#include <linux/pgtable.h> 11#include <linux/slab.h> 12 13struct cma; 14struct iommu_ops; 15 16struct dma_map_ops { 17 void *(*alloc)(struct device *dev, size_t size, 18 dma_addr_t *dma_handle, gfp_t gfp, 19 unsigned long attrs); 20 void (*free)(struct device *dev, size_t size, void *vaddr, 21 dma_addr_t dma_handle, unsigned long attrs); 22 struct page *(*alloc_pages_op)(struct device *dev, size_t size, 23 dma_addr_t *dma_handle, enum dma_data_direction dir, 24 gfp_t gfp); 25 void (*free_pages)(struct device *dev, size_t size, struct page *vaddr, 26 dma_addr_t dma_handle, enum dma_data_direction dir); 27 int (*mmap)(struct device *, struct vm_area_struct *, 28 void *, dma_addr_t, size_t, unsigned long attrs); 29 30 int (*get_sgtable)(struct device *dev, struct sg_table *sgt, 31 void *cpu_addr, dma_addr_t dma_addr, size_t size, 32 unsigned long attrs); 33 34 dma_addr_t (*map_page)(struct device *dev, struct page *page, 35 unsigned long offset, size_t size, 36 enum dma_data_direction dir, unsigned long attrs); 37 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle, 38 size_t size, enum dma_data_direction dir, 39 unsigned long attrs); 40 /* 41 * map_sg should return a negative error code on error. See 42 * dma_map_sgtable() for a list of appropriate error codes 43 * and their meanings. 44 */ 45 int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents, 46 enum dma_data_direction dir, unsigned long attrs); 47 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents, 48 enum dma_data_direction dir, unsigned long attrs); 49 dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr, 50 size_t size, enum dma_data_direction dir, 51 unsigned long attrs); 52 void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle, 53 size_t size, enum dma_data_direction dir, 54 unsigned long attrs); 55 void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle, 56 size_t size, enum dma_data_direction dir); 57 void (*sync_single_for_device)(struct device *dev, 58 dma_addr_t dma_handle, size_t size, 59 enum dma_data_direction dir); 60 void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, 61 int nents, enum dma_data_direction dir); 62 void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg, 63 int nents, enum dma_data_direction dir); 64 void (*cache_sync)(struct device *dev, void *vaddr, size_t size, 65 enum dma_data_direction direction); 66 int (*dma_supported)(struct device *dev, u64 mask); 67 u64 (*get_required_mask)(struct device *dev); 68 size_t (*max_mapping_size)(struct device *dev); 69 size_t (*opt_mapping_size)(void); 70 unsigned long (*get_merge_boundary)(struct device *dev); 71}; 72 73#ifdef CONFIG_ARCH_HAS_DMA_OPS 74#include <asm/dma-mapping.h> 75 76static inline const struct dma_map_ops *get_dma_ops(struct device *dev) 77{ 78 if (dev->dma_ops) 79 return dev->dma_ops; 80 return get_arch_dma_ops(); 81} 82 83static inline void set_dma_ops(struct device *dev, 84 const struct dma_map_ops *dma_ops) 85{ 86 dev->dma_ops = dma_ops; 87} 88#else /* CONFIG_ARCH_HAS_DMA_OPS */ 89static inline const struct dma_map_ops *get_dma_ops(struct device *dev) 90{ 91 return NULL; 92} 93static inline void set_dma_ops(struct device *dev, 94 const struct dma_map_ops *dma_ops) 95{ 96} 97#endif /* CONFIG_ARCH_HAS_DMA_OPS */ 98 99#ifdef CONFIG_DMA_CMA 100extern struct cma *dma_contiguous_default_area; 101 102static inline struct cma *dev_get_cma_area(struct device *dev) 103{ 104 if (dev && dev->cma_area) 105 return dev->cma_area; 106 return dma_contiguous_default_area; 107} 108 109void dma_contiguous_reserve(phys_addr_t addr_limit); 110int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, 111 phys_addr_t limit, struct cma **res_cma, bool fixed); 112 113struct page *dma_alloc_from_contiguous(struct device *dev, size_t count, 114 unsigned int order, bool no_warn); 115bool dma_release_from_contiguous(struct device *dev, struct page *pages, 116 int count); 117struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp); 118void dma_free_contiguous(struct device *dev, struct page *page, size_t size); 119 120void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size); 121#else /* CONFIG_DMA_CMA */ 122static inline struct cma *dev_get_cma_area(struct device *dev) 123{ 124 return NULL; 125} 126static inline void dma_contiguous_reserve(phys_addr_t limit) 127{ 128} 129static inline int dma_contiguous_reserve_area(phys_addr_t size, 130 phys_addr_t base, phys_addr_t limit, struct cma **res_cma, 131 bool fixed) 132{ 133 return -ENOSYS; 134} 135static inline struct page *dma_alloc_from_contiguous(struct device *dev, 136 size_t count, unsigned int order, bool no_warn) 137{ 138 return NULL; 139} 140static inline bool dma_release_from_contiguous(struct device *dev, 141 struct page *pages, int count) 142{ 143 return false; 144} 145/* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */ 146static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size, 147 gfp_t gfp) 148{ 149 return NULL; 150} 151static inline void dma_free_contiguous(struct device *dev, struct page *page, 152 size_t size) 153{ 154 __free_pages(page, get_order(size)); 155} 156static inline void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) 157{ 158} 159#endif /* CONFIG_DMA_CMA*/ 160 161#ifdef CONFIG_DMA_DECLARE_COHERENT 162int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, 163 dma_addr_t device_addr, size_t size); 164void dma_release_coherent_memory(struct device *dev); 165int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size, 166 dma_addr_t *dma_handle, void **ret); 167int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr); 168int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma, 169 void *cpu_addr, size_t size, int *ret); 170#else 171static inline int dma_declare_coherent_memory(struct device *dev, 172 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size) 173{ 174 return -ENOSYS; 175} 176 177#define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0) 178#define dma_release_from_dev_coherent(dev, order, vaddr) (0) 179#define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0) 180static inline void dma_release_coherent_memory(struct device *dev) { } 181#endif /* CONFIG_DMA_DECLARE_COHERENT */ 182 183#ifdef CONFIG_DMA_GLOBAL_POOL 184void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size, 185 dma_addr_t *dma_handle); 186int dma_release_from_global_coherent(int order, void *vaddr); 187int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr, 188 size_t size, int *ret); 189int dma_init_global_coherent(phys_addr_t phys_addr, size_t size); 190#else 191static inline void *dma_alloc_from_global_coherent(struct device *dev, 192 ssize_t size, dma_addr_t *dma_handle) 193{ 194 return NULL; 195} 196static inline int dma_release_from_global_coherent(int order, void *vaddr) 197{ 198 return 0; 199} 200static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma, 201 void *cpu_addr, size_t size, int *ret) 202{ 203 return 0; 204} 205#endif /* CONFIG_DMA_GLOBAL_POOL */ 206 207int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, 208 void *cpu_addr, dma_addr_t dma_addr, size_t size, 209 unsigned long attrs); 210int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, 211 void *cpu_addr, dma_addr_t dma_addr, size_t size, 212 unsigned long attrs); 213struct page *dma_common_alloc_pages(struct device *dev, size_t size, 214 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp); 215void dma_common_free_pages(struct device *dev, size_t size, struct page *vaddr, 216 dma_addr_t dma_handle, enum dma_data_direction dir); 217 218struct page **dma_common_find_pages(void *cpu_addr); 219void *dma_common_contiguous_remap(struct page *page, size_t size, pgprot_t prot, 220 const void *caller); 221void *dma_common_pages_remap(struct page **pages, size_t size, pgprot_t prot, 222 const void *caller); 223void dma_common_free_remap(void *cpu_addr, size_t size); 224 225struct page *dma_alloc_from_pool(struct device *dev, size_t size, 226 void **cpu_addr, gfp_t flags, 227 bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t)); 228bool dma_free_from_pool(struct device *dev, void *start, size_t size); 229 230int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start, 231 dma_addr_t dma_start, u64 size); 232 233#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ 234 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ 235 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) 236extern bool dma_default_coherent; 237static inline bool dev_is_dma_coherent(struct device *dev) 238{ 239 return dev->dma_coherent; 240} 241#else 242#define dma_default_coherent true 243 244static inline bool dev_is_dma_coherent(struct device *dev) 245{ 246 return true; 247} 248#endif 249 250static inline void dma_reset_need_sync(struct device *dev) 251{ 252#ifdef CONFIG_DMA_NEED_SYNC 253 /* Reset it only once so that the function can be called on hotpath */ 254 if (unlikely(dev->dma_skip_sync)) 255 dev->dma_skip_sync = false; 256#endif 257} 258 259/* 260 * Check whether potential kmalloc() buffers are safe for non-coherent DMA. 261 */ 262static inline bool dma_kmalloc_safe(struct device *dev, 263 enum dma_data_direction dir) 264{ 265 /* 266 * If DMA bouncing of kmalloc() buffers is disabled, the kmalloc() 267 * caches have already been aligned to a DMA-safe size. 268 */ 269 if (!IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC)) 270 return true; 271 272 /* 273 * kmalloc() buffers are DMA-safe irrespective of size if the device 274 * is coherent or the direction is DMA_TO_DEVICE (non-desctructive 275 * cache maintenance and benign cache line evictions). 276 */ 277 if (dev_is_dma_coherent(dev) || dir == DMA_TO_DEVICE) 278 return true; 279 280 return false; 281} 282 283/* 284 * Check whether the given size, assuming it is for a kmalloc()'ed buffer, is 285 * sufficiently aligned for non-coherent DMA. 286 */ 287static inline bool dma_kmalloc_size_aligned(size_t size) 288{ 289 /* 290 * Larger kmalloc() sizes are guaranteed to be aligned to 291 * ARCH_DMA_MINALIGN. 292 */ 293 if (size >= 2 * ARCH_DMA_MINALIGN || 294 IS_ALIGNED(kmalloc_size_roundup(size), dma_get_cache_alignment())) 295 return true; 296 297 return false; 298} 299 300/* 301 * Check whether the given object size may have originated from a kmalloc() 302 * buffer with a slab alignment below the DMA-safe alignment and needs 303 * bouncing for non-coherent DMA. The pointer alignment is not considered and 304 * in-structure DMA-safe offsets are the responsibility of the caller. Such 305 * code should use the static ARCH_DMA_MINALIGN for compiler annotations. 306 * 307 * The heuristics can have false positives, bouncing unnecessarily, though the 308 * buffers would be small. False negatives are theoretically possible if, for 309 * example, multiple small kmalloc() buffers are coalesced into a larger 310 * buffer that passes the alignment check. There are no such known constructs 311 * in the kernel. 312 */ 313static inline bool dma_kmalloc_needs_bounce(struct device *dev, size_t size, 314 enum dma_data_direction dir) 315{ 316 return !dma_kmalloc_safe(dev, dir) && !dma_kmalloc_size_aligned(size); 317} 318 319void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, 320 gfp_t gfp, unsigned long attrs); 321void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, 322 dma_addr_t dma_addr, unsigned long attrs); 323 324#ifdef CONFIG_ARCH_HAS_DMA_SET_MASK 325void arch_dma_set_mask(struct device *dev, u64 mask); 326#else 327#define arch_dma_set_mask(dev, mask) do { } while (0) 328#endif 329 330#ifdef CONFIG_MMU 331/* 332 * Page protection so that devices that can't snoop CPU caches can use the 333 * memory coherently. We default to pgprot_noncached which is usually used 334 * for ioremap as a safe bet, but architectures can override this with less 335 * strict semantics if possible. 336 */ 337#ifndef pgprot_dmacoherent 338#define pgprot_dmacoherent(prot) pgprot_noncached(prot) 339#endif 340 341pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs); 342#else 343static inline pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, 344 unsigned long attrs) 345{ 346 return prot; /* no protection bits supported without page tables */ 347} 348#endif /* CONFIG_MMU */ 349 350#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE 351void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, 352 enum dma_data_direction dir); 353#else 354static inline void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, 355 enum dma_data_direction dir) 356{ 357} 358#endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */ 359 360#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU 361void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, 362 enum dma_data_direction dir); 363#else 364static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, 365 enum dma_data_direction dir) 366{ 367} 368#endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */ 369 370#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL 371void arch_sync_dma_for_cpu_all(void); 372#else 373static inline void arch_sync_dma_for_cpu_all(void) 374{ 375} 376#endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */ 377 378#ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT 379void arch_dma_prep_coherent(struct page *page, size_t size); 380#else 381static inline void arch_dma_prep_coherent(struct page *page, size_t size) 382{ 383} 384#endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */ 385 386#ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN 387void arch_dma_mark_clean(phys_addr_t paddr, size_t size); 388#else 389static inline void arch_dma_mark_clean(phys_addr_t paddr, size_t size) 390{ 391} 392#endif /* ARCH_HAS_DMA_MARK_CLEAN */ 393 394void *arch_dma_set_uncached(void *addr, size_t size); 395void arch_dma_clear_uncached(void *addr, size_t size); 396 397#ifdef CONFIG_ARCH_HAS_DMA_MAP_DIRECT 398bool arch_dma_map_phys_direct(struct device *dev, phys_addr_t addr); 399bool arch_dma_unmap_phys_direct(struct device *dev, dma_addr_t dma_handle); 400bool arch_dma_map_sg_direct(struct device *dev, struct scatterlist *sg, 401 int nents); 402bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg, 403 int nents); 404#else 405#define arch_dma_map_phys_direct(d, a) (false) 406#define arch_dma_unmap_phys_direct(d, a) (false) 407#define arch_dma_map_sg_direct(d, s, n) (false) 408#define arch_dma_unmap_sg_direct(d, s, n) (false) 409#endif 410 411#ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS 412void arch_setup_dma_ops(struct device *dev, bool coherent); 413#else 414static inline void arch_setup_dma_ops(struct device *dev, bool coherent) 415{ 416} 417#endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */ 418 419#ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS 420void arch_teardown_dma_ops(struct device *dev); 421#else 422static inline void arch_teardown_dma_ops(struct device *dev) 423{ 424} 425#endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */ 426 427#ifdef CONFIG_DMA_API_DEBUG 428void dma_debug_add_bus(const struct bus_type *bus); 429void debug_dma_dump_mappings(struct device *dev); 430#else 431static inline void dma_debug_add_bus(const struct bus_type *bus) 432{ 433} 434static inline void debug_dma_dump_mappings(struct device *dev) 435{ 436} 437#endif /* CONFIG_DMA_API_DEBUG */ 438 439extern const struct dma_map_ops dma_dummy_ops; 440#endif /* _LINUX_DMA_MAP_OPS_H */