1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 #ifndef _LINUXKPI_LINUX_DMA_MAPPING_H_ 30 #define _LINUXKPI_LINUX_DMA_MAPPING_H_ 31 32 #include <linux/types.h> 33 #include <linux/device.h> 34 #include <linux/err.h> 35 #include <linux/dma-attrs.h> 36 #include <linux/scatterlist.h> 37 #include <linux/mm.h> 38 #include <linux/page.h> 39 #include <linux/sizes.h> 40 41 #include <sys/systm.h> 42 #include <sys/malloc.h> 43 44 #include <vm/vm.h> 45 #include <vm/vm_page.h> 46 #include <vm/uma_align_mask.h> 47 #include <vm/pmap.h> 48 49 #include <machine/bus.h> 50 51 enum dma_data_direction { 52 DMA_BIDIRECTIONAL = 0, 53 DMA_TO_DEVICE = 1, 54 DMA_FROM_DEVICE = 2, 55 DMA_NONE = 3, 56 }; 57 58 struct dma_map_ops { 59 void* (*alloc_coherent)(struct device *dev, size_t size, 60 dma_addr_t *dma_handle, gfp_t gfp); 61 void (*free_coherent)(struct device *dev, size_t size, 62 void *vaddr, dma_addr_t dma_handle); 63 dma_addr_t (*map_page)(struct device *dev, struct page *page, 64 unsigned long offset, size_t size, enum dma_data_direction dir, 65 unsigned long attrs); 66 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle, 67 size_t size, enum dma_data_direction dir, unsigned long attrs); 68 int (*map_sg)(struct device *dev, struct scatterlist *sg, 69 int nents, enum dma_data_direction dir, unsigned long attrs); 70 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents, 71 enum dma_data_direction dir, unsigned long attrs); 72 void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle, 73 size_t size, enum dma_data_direction dir); 74 void (*sync_single_for_device)(struct device *dev, 75 dma_addr_t dma_handle, size_t size, enum dma_data_direction dir); 76 void (*sync_single_range_for_cpu)(struct device *dev, 77 dma_addr_t dma_handle, unsigned long offset, size_t size, 78 enum dma_data_direction dir); 79 void (*sync_single_range_for_device)(struct device *dev, 80 dma_addr_t dma_handle, unsigned long offset, size_t size, 81 enum dma_data_direction dir); 82 void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, 83 int nents, enum dma_data_direction dir); 84 void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg, 85 int nents, enum dma_data_direction dir); 86 int (*mapping_error)(struct device *dev, dma_addr_t dma_addr); 87 int (*dma_supported)(struct device *dev, u64 mask); 88 int is_phys; 89 }; 90 91 #define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL) 92 93 int linux_dma_tag_init(struct device *, u64); 94 int linux_dma_tag_init_coherent(struct device *, u64); 95 void *linux_dma_alloc_coherent(struct device *dev, size_t size, 96 dma_addr_t *dma_handle, gfp_t flag); 97 void *linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, 98 dma_addr_t *dma_handle, gfp_t flag); 99 dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len); /* backward compat */ 100 dma_addr_t lkpi_dma_map_phys(struct device *, vm_paddr_t, size_t, 101 enum dma_data_direction, unsigned long); 102 void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size); /* backward compat */ 103 void lkpi_dma_unmap(struct device *, dma_addr_t, size_t, 104 enum dma_data_direction, unsigned long); 105 int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, 106 int nents, enum dma_data_direction direction, 107 unsigned long attrs __unused); 108 void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, 109 int nents __unused, enum dma_data_direction direction, 110 unsigned long attrs __unused); 111 void linuxkpi_dma_sync(struct device *, dma_addr_t, size_t, bus_dmasync_op_t); 112 113 static inline int 114 dma_supported(struct device *dev, u64 dma_mask) 115 { 116 117 /* XXX busdma takes care of this elsewhere. */ 118 return (1); 119 } 120 121 static inline int 122 dma_set_mask(struct device *dev, u64 dma_mask) 123 { 124 125 if (!dev->dma_priv || !dma_supported(dev, dma_mask)) 126 return -EIO; 127 128 return (linux_dma_tag_init(dev, dma_mask)); 129 } 130 131 static inline int 132 dma_set_coherent_mask(struct device *dev, u64 dma_mask) 133 { 134 135 if (!dev->dma_priv || !dma_supported(dev, dma_mask)) 136 return -EIO; 137 138 return (linux_dma_tag_init_coherent(dev, dma_mask)); 139 } 140 141 static inline int 142 dma_set_mask_and_coherent(struct device *dev, u64 dma_mask) 143 { 144 int r; 145 146 r = dma_set_mask(dev, dma_mask); 147 if (r == 0) 148 dma_set_coherent_mask(dev, dma_mask); 149 return (r); 150 } 151 152 static inline void * 153 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 154 gfp_t flag) 155 { 156 return (linux_dma_alloc_coherent(dev, size, dma_handle, flag)); 157 } 158 159 static inline void * 160 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 161 gfp_t flag) 162 { 163 164 return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO)); 165 } 166 167 static inline void * 168 dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 169 gfp_t flag) 170 { 171 172 return (linuxkpi_dmam_alloc_coherent(dev, size, dma_handle, flag)); 173 } 174 175 static inline void 176 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, 177 dma_addr_t dma_addr) 178 { 179 180 lkpi_dma_unmap(dev, dma_addr, size, DMA_BIDIRECTIONAL, 0); 181 kmem_free(cpu_addr, size); 182 } 183 184 static inline dma_addr_t 185 dma_map_page_attrs(struct device *dev, struct page *page, size_t offset, 186 size_t size, enum dma_data_direction direction, unsigned long attrs) 187 { 188 189 return (lkpi_dma_map_phys(dev, page_to_phys(page) + offset, size, 190 direction, attrs)); 191 } 192 193 /* linux_dma_(un)map_sg_attrs does not support attrs yet */ 194 #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \ 195 linux_dma_map_sg_attrs(dev, sgl, nents, dir, 0) 196 197 #define dma_unmap_sg_attrs(dev, sg, nents, dir, attrs) \ 198 linux_dma_unmap_sg_attrs(dev, sg, nents, dir, 0) 199 200 static inline dma_addr_t 201 dma_map_page(struct device *dev, struct page *page, 202 unsigned long offset, size_t size, enum dma_data_direction direction) 203 { 204 205 return (lkpi_dma_map_phys(dev, page_to_phys(page) + offset, size, 206 direction, 0)); 207 } 208 209 static inline void 210 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size, 211 enum dma_data_direction direction) 212 { 213 214 lkpi_dma_unmap(dev, dma_address, size, direction, 0); 215 } 216 217 static inline dma_addr_t 218 dma_map_resource(struct device *dev, phys_addr_t paddr, size_t size, 219 enum dma_data_direction direction, unsigned long attrs) 220 { 221 return (lkpi_dma_map_phys(dev, paddr, size, direction, attrs)); 222 } 223 224 static inline void 225 dma_unmap_resource(struct device *dev, dma_addr_t dma, size_t size, 226 enum dma_data_direction direction, unsigned long attrs) 227 { 228 lkpi_dma_unmap(dev, dma, size, direction, attrs); 229 } 230 231 static inline void 232 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma, size_t size, 233 enum dma_data_direction direction) 234 { 235 bus_dmasync_op_t op; 236 237 switch (direction) { 238 case DMA_BIDIRECTIONAL: 239 op = BUS_DMASYNC_POSTREAD; 240 linuxkpi_dma_sync(dev, dma, size, op); 241 op = BUS_DMASYNC_PREREAD; 242 break; 243 case DMA_TO_DEVICE: 244 op = BUS_DMASYNC_POSTWRITE; 245 break; 246 case DMA_FROM_DEVICE: 247 op = BUS_DMASYNC_POSTREAD; 248 break; 249 default: 250 return; 251 } 252 253 linuxkpi_dma_sync(dev, dma, size, op); 254 } 255 256 static inline void 257 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size, 258 enum dma_data_direction dir) 259 { 260 dma_sync_single_for_cpu(dev, addr, size, dir); 261 } 262 263 static inline void 264 dma_sync_single_for_device(struct device *dev, dma_addr_t dma, 265 size_t size, enum dma_data_direction direction) 266 { 267 bus_dmasync_op_t op; 268 269 switch (direction) { 270 case DMA_BIDIRECTIONAL: 271 op = BUS_DMASYNC_PREWRITE; 272 break; 273 case DMA_TO_DEVICE: 274 op = BUS_DMASYNC_PREREAD; 275 break; 276 case DMA_FROM_DEVICE: 277 op = BUS_DMASYNC_PREWRITE; 278 break; 279 default: 280 return; 281 } 282 283 linuxkpi_dma_sync(dev, dma, size, op); 284 } 285 286 /* (20250329) These four seem to be unused code. */ 287 static inline void 288 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems, 289 enum dma_data_direction direction) 290 { 291 pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction); 292 } 293 294 static inline void 295 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems, 296 enum dma_data_direction direction) 297 { 298 pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction); 299 } 300 301 static inline void 302 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle, 303 unsigned long offset, size_t size, enum dma_data_direction direction) 304 { 305 pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction); 306 } 307 308 static inline void 309 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle, 310 unsigned long offset, size_t size, enum dma_data_direction direction) 311 { 312 pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction); 313 } 314 315 #define DMA_MAPPING_ERROR (~(dma_addr_t)0) 316 317 static inline int 318 dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 319 { 320 321 if (dma_addr == 0 || dma_addr == DMA_MAPPING_ERROR) 322 return (-ENOMEM); 323 return (0); 324 } 325 326 static inline unsigned int dma_set_max_seg_size(struct device *dev, 327 unsigned int size) 328 { 329 return (0); 330 } 331 332 static inline dma_addr_t 333 _dma_map_single_attrs(struct device *dev, void *ptr, size_t size, 334 enum dma_data_direction direction, unsigned long attrs) 335 { 336 return (lkpi_dma_map_phys(dev, vtophys(ptr), size, 337 direction, attrs)); 338 } 339 340 static inline void 341 _dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size, 342 enum dma_data_direction direction, unsigned long attrs) 343 { 344 lkpi_dma_unmap(dev, dma, size, direction, attrs); 345 } 346 347 static inline size_t 348 dma_max_mapping_size(struct device *dev) 349 { 350 351 return (SCATTERLIST_MAX_SEGMENT); 352 } 353 354 #define dma_map_single_attrs(dev, ptr, size, dir, attrs) \ 355 _dma_map_single_attrs(dev, ptr, size, dir, 0) 356 357 #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \ 358 _dma_unmap_single_attrs(dev, dma_addr, size, dir, 0) 359 360 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0) 361 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0) 362 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0) 363 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0) 364 365 #define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name 366 #define DEFINE_DMA_UNMAP_LEN(name) __u32 name 367 #define dma_unmap_addr(p, name) ((p)->name) 368 #define dma_unmap_addr_set(p, name, v) (((p)->name) = (v)) 369 #define dma_unmap_len(p, name) ((p)->name) 370 #define dma_unmap_len_set(p, name, v) (((p)->name) = (v)) 371 372 #define dma_get_cache_alignment() (uma_get_cache_align_mask() + 1) 373 374 375 static inline int 376 dma_map_sgtable(struct device *dev, struct sg_table *sgt, 377 enum dma_data_direction dir, 378 unsigned long attrs) 379 { 380 int nents; 381 382 nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs); 383 if (nents < 0) 384 return (nents); 385 sgt->nents = nents; 386 return (0); 387 } 388 389 static inline void 390 dma_unmap_sgtable(struct device *dev, struct sg_table *sgt, 391 enum dma_data_direction dir, 392 unsigned long attrs) 393 { 394 395 dma_unmap_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs); 396 } 397 398 399 #endif /* _LINUXKPI_LINUX_DMA_MAPPING_H_ */ 400