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> |
| 10 | |
Christoph Hellwig | 0b1abd1 | 2020-09-11 10:56:52 +0200 | [diff] [blame] | 11 | struct cma; |
| 12 | |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 13 | struct dma_map_ops { |
| 14 | void *(*alloc)(struct device *dev, size_t size, |
| 15 | dma_addr_t *dma_handle, gfp_t gfp, |
| 16 | unsigned long attrs); |
| 17 | void (*free)(struct device *dev, size_t size, void *vaddr, |
| 18 | dma_addr_t dma_handle, unsigned long attrs); |
| 19 | struct page *(*alloc_pages)(struct device *dev, size_t size, |
| 20 | dma_addr_t *dma_handle, enum dma_data_direction dir, |
| 21 | gfp_t gfp); |
| 22 | void (*free_pages)(struct device *dev, size_t size, struct page *vaddr, |
| 23 | dma_addr_t dma_handle, enum dma_data_direction dir); |
| 24 | void *(*alloc_noncoherent)(struct device *dev, size_t size, |
| 25 | dma_addr_t *dma_handle, enum dma_data_direction dir, |
| 26 | gfp_t gfp); |
| 27 | void (*free_noncoherent)(struct device *dev, size_t size, void *vaddr, |
| 28 | dma_addr_t dma_handle, enum dma_data_direction dir); |
| 29 | int (*mmap)(struct device *, struct vm_area_struct *, |
| 30 | void *, dma_addr_t, size_t, unsigned long attrs); |
| 31 | |
| 32 | int (*get_sgtable)(struct device *dev, struct sg_table *sgt, |
| 33 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 34 | unsigned long attrs); |
| 35 | |
| 36 | dma_addr_t (*map_page)(struct device *dev, struct page *page, |
| 37 | unsigned long offset, size_t size, |
| 38 | enum dma_data_direction dir, unsigned long attrs); |
| 39 | void (*unmap_page)(struct device *dev, dma_addr_t dma_handle, |
| 40 | size_t size, enum dma_data_direction dir, |
| 41 | unsigned long attrs); |
| 42 | /* |
| 43 | * map_sg returns 0 on error and a value > 0 on success. |
| 44 | * It should never return a value < 0. |
| 45 | */ |
| 46 | int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents, |
| 47 | enum dma_data_direction dir, unsigned long attrs); |
| 48 | void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents, |
| 49 | enum dma_data_direction dir, unsigned long attrs); |
| 50 | dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr, |
| 51 | size_t size, enum dma_data_direction dir, |
| 52 | unsigned long attrs); |
| 53 | void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle, |
| 54 | size_t size, enum dma_data_direction dir, |
| 55 | unsigned long attrs); |
| 56 | void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle, |
| 57 | size_t size, enum dma_data_direction dir); |
| 58 | void (*sync_single_for_device)(struct device *dev, |
| 59 | dma_addr_t dma_handle, size_t size, |
| 60 | enum dma_data_direction dir); |
| 61 | void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, |
| 62 | int nents, enum dma_data_direction dir); |
| 63 | void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg, |
| 64 | int nents, enum dma_data_direction dir); |
| 65 | void (*cache_sync)(struct device *dev, void *vaddr, size_t size, |
| 66 | enum dma_data_direction direction); |
| 67 | int (*dma_supported)(struct device *dev, u64 mask); |
| 68 | u64 (*get_required_mask)(struct device *dev); |
| 69 | size_t (*max_mapping_size)(struct device *dev); |
| 70 | unsigned long (*get_merge_boundary)(struct device *dev); |
| 71 | }; |
| 72 | |
| 73 | #ifdef CONFIG_DMA_OPS |
| 74 | #include <asm/dma-mapping.h> |
| 75 | |
| 76 | static 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(dev->bus); |
| 81 | } |
| 82 | |
| 83 | static 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_DMA_OPS */ |
| 89 | static inline const struct dma_map_ops *get_dma_ops(struct device *dev) |
| 90 | { |
| 91 | return NULL; |
| 92 | } |
| 93 | static inline void set_dma_ops(struct device *dev, |
| 94 | const struct dma_map_ops *dma_ops) |
| 95 | { |
| 96 | } |
| 97 | #endif /* CONFIG_DMA_OPS */ |
| 98 | |
Christoph Hellwig | 0b1abd1 | 2020-09-11 10:56:52 +0200 | [diff] [blame] | 99 | #ifdef CONFIG_DMA_CMA |
| 100 | extern struct cma *dma_contiguous_default_area; |
| 101 | |
| 102 | static 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 | |
| 109 | void dma_contiguous_reserve(phys_addr_t addr_limit); |
| 110 | int __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 | |
| 113 | struct page *dma_alloc_from_contiguous(struct device *dev, size_t count, |
| 114 | unsigned int order, bool no_warn); |
| 115 | bool dma_release_from_contiguous(struct device *dev, struct page *pages, |
| 116 | int count); |
| 117 | struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp); |
| 118 | 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] | 119 | |
| 120 | void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size); |
Christoph Hellwig | 0b1abd1 | 2020-09-11 10:56:52 +0200 | [diff] [blame] | 121 | #else /* CONFIG_DMA_CMA */ |
| 122 | static inline struct cma *dev_get_cma_area(struct device *dev) |
| 123 | { |
| 124 | return NULL; |
| 125 | } |
| 126 | static inline void dma_contiguous_reserve(phys_addr_t limit) |
| 127 | { |
| 128 | } |
| 129 | static 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 | } |
| 135 | static 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 | } |
| 140 | static 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 */ |
| 146 | static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size, |
| 147 | gfp_t gfp) |
| 148 | { |
| 149 | return NULL; |
| 150 | } |
| 151 | static inline void dma_free_contiguous(struct device *dev, struct page *page, |
| 152 | size_t size) |
| 153 | { |
| 154 | __free_pages(page, get_order(size)); |
| 155 | } |
| 156 | #endif /* CONFIG_DMA_CMA*/ |
| 157 | |
| 158 | #ifdef CONFIG_DMA_PERNUMA_CMA |
| 159 | void dma_pernuma_cma_reserve(void); |
| 160 | #else |
| 161 | static inline void dma_pernuma_cma_reserve(void) { } |
| 162 | #endif /* CONFIG_DMA_PERNUMA_CMA */ |
| 163 | |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 164 | #ifdef CONFIG_DMA_DECLARE_COHERENT |
| 165 | int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, |
| 166 | dma_addr_t device_addr, size_t size); |
| 167 | int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size, |
| 168 | dma_addr_t *dma_handle, void **ret); |
| 169 | int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr); |
| 170 | int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma, |
| 171 | void *cpu_addr, size_t size, int *ret); |
| 172 | |
| 173 | void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size, |
| 174 | dma_addr_t *dma_handle); |
| 175 | int dma_release_from_global_coherent(int order, void *vaddr); |
| 176 | int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr, |
| 177 | size_t size, int *ret); |
| 178 | |
| 179 | #else |
| 180 | static inline int dma_declare_coherent_memory(struct device *dev, |
| 181 | phys_addr_t phys_addr, dma_addr_t device_addr, size_t size) |
| 182 | { |
| 183 | return -ENOSYS; |
| 184 | } |
| 185 | #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0) |
| 186 | #define dma_release_from_dev_coherent(dev, order, vaddr) (0) |
| 187 | #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0) |
| 188 | |
| 189 | static inline void *dma_alloc_from_global_coherent(struct device *dev, |
| 190 | ssize_t size, dma_addr_t *dma_handle) |
| 191 | { |
| 192 | return NULL; |
| 193 | } |
| 194 | static inline int dma_release_from_global_coherent(int order, void *vaddr) |
| 195 | { |
| 196 | return 0; |
| 197 | } |
| 198 | static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma, |
| 199 | void *cpu_addr, size_t size, int *ret) |
| 200 | { |
| 201 | return 0; |
| 202 | } |
| 203 | #endif /* CONFIG_DMA_DECLARE_COHERENT */ |
| 204 | |
| 205 | #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS |
| 206 | void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, |
| 207 | const struct iommu_ops *iommu, bool coherent); |
| 208 | #else |
| 209 | static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base, |
| 210 | u64 size, const struct iommu_ops *iommu, bool coherent) |
| 211 | { |
| 212 | } |
| 213 | #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */ |
| 214 | |
| 215 | #ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS |
| 216 | void arch_teardown_dma_ops(struct device *dev); |
| 217 | #else |
| 218 | static inline void arch_teardown_dma_ops(struct device *dev) |
| 219 | { |
| 220 | } |
| 221 | #endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */ |
| 222 | |
Christoph Hellwig | a1fd09e | 2020-09-11 10:12:44 +0200 | [diff] [blame^] | 223 | #ifdef CONFIG_DMA_API_DEBUG |
| 224 | void dma_debug_add_bus(struct bus_type *bus); |
| 225 | void debug_dma_dump_mappings(struct device *dev); |
| 226 | #else |
| 227 | static inline void dma_debug_add_bus(struct bus_type *bus) |
| 228 | { |
| 229 | } |
| 230 | static inline void debug_dma_dump_mappings(struct device *dev) |
| 231 | { |
| 232 | } |
| 233 | #endif /* CONFIG_DMA_API_DEBUG */ |
| 234 | |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 235 | extern const struct dma_map_ops dma_dummy_ops; |
| 236 | |
| 237 | #endif /* _LINUX_DMA_MAP_OPS_H */ |