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/pmap.h> 47 48 #include <machine/bus.h> 49 50 enum dma_data_direction { 51 DMA_BIDIRECTIONAL = 0, 52 DMA_TO_DEVICE = 1, 53 DMA_FROM_DEVICE = 2, 54 DMA_NONE = 3, 55 }; 56 57 struct dma_map_ops { 58 void* (*alloc_coherent)(struct device *dev, size_t size, 59 dma_addr_t *dma_handle, gfp_t gfp); 60 void (*free_coherent)(struct device *dev, size_t size, 61 void *vaddr, dma_addr_t dma_handle); 62 dma_addr_t (*map_page)(struct device *dev, struct page *page, 63 unsigned long offset, size_t size, enum dma_data_direction dir, 64 unsigned long attrs); 65 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle, 66 size_t size, enum dma_data_direction dir, unsigned long attrs); 67 int (*map_sg)(struct device *dev, struct scatterlist *sg, 68 int nents, enum dma_data_direction dir, unsigned long attrs); 69 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents, 70 enum dma_data_direction dir, unsigned long attrs); 71 void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle, 72 size_t size, enum dma_data_direction dir); 73 void (*sync_single_for_device)(struct device *dev, 74 dma_addr_t dma_handle, size_t size, enum dma_data_direction dir); 75 void (*sync_single_range_for_cpu)(struct device *dev, 76 dma_addr_t dma_handle, unsigned long offset, size_t size, 77 enum dma_data_direction dir); 78 void (*sync_single_range_for_device)(struct device *dev, 79 dma_addr_t dma_handle, unsigned long offset, size_t size, 80 enum dma_data_direction dir); 81 void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, 82 int nents, enum dma_data_direction dir); 83 void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg, 84 int nents, enum dma_data_direction dir); 85 int (*mapping_error)(struct device *dev, dma_addr_t dma_addr); 86 int (*dma_supported)(struct device *dev, u64 mask); 87 int is_phys; 88 }; 89 90 #define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL) 91 92 int linux_dma_tag_init(struct device *, u64); 93 int linux_dma_tag_init_coherent(struct device *, u64); 94 void *linux_dma_alloc_coherent(struct device *dev, size_t size, 95 dma_addr_t *dma_handle, gfp_t flag); 96 void *linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size, 97 dma_addr_t *dma_handle, gfp_t flag); 98 dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len); 99 void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size); 100 int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, 101 int nents, enum dma_data_direction dir __unused, 102 unsigned long attrs __unused); 103 void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, 104 int nents __unused, enum dma_data_direction dir __unused, 105 unsigned long attrs __unused); 106 void linuxkpi_dma_sync(struct device *, dma_addr_t, size_t, bus_dmasync_op_t); 107 108 static inline int 109 dma_supported(struct device *dev, u64 dma_mask) 110 { 111 112 /* XXX busdma takes care of this elsewhere. */ 113 return (1); 114 } 115 116 static inline int 117 dma_set_mask(struct device *dev, u64 dma_mask) 118 { 119 120 if (!dev->dma_priv || !dma_supported(dev, dma_mask)) 121 return -EIO; 122 123 return (linux_dma_tag_init(dev, dma_mask)); 124 } 125 126 static inline int 127 dma_set_coherent_mask(struct device *dev, u64 dma_mask) 128 { 129 130 if (!dev->dma_priv || !dma_supported(dev, dma_mask)) 131 return -EIO; 132 133 return (linux_dma_tag_init_coherent(dev, dma_mask)); 134 } 135 136 static inline int 137 dma_set_mask_and_coherent(struct device *dev, u64 dma_mask) 138 { 139 int r; 140 141 r = dma_set_mask(dev, dma_mask); 142 if (r == 0) 143 dma_set_coherent_mask(dev, dma_mask); 144 return (r); 145 } 146 147 static inline void * 148 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 149 gfp_t flag) 150 { 151 return (linux_dma_alloc_coherent(dev, size, dma_handle, flag)); 152 } 153 154 static inline void * 155 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 156 gfp_t flag) 157 { 158 159 return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO)); 160 } 161 162 static inline void * 163 dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 164 gfp_t flag) 165 { 166 167 return (linuxkpi_dmam_alloc_coherent(dev, size, dma_handle, flag)); 168 } 169 170 static inline void 171 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, 172 dma_addr_t dma_addr) 173 { 174 175 linux_dma_unmap(dev, dma_addr, size); 176 kmem_free(cpu_addr, size); 177 } 178 179 static inline dma_addr_t 180 dma_map_page_attrs(struct device *dev, struct page *page, size_t offset, 181 size_t size, enum dma_data_direction dir, unsigned long attrs) 182 { 183 184 return (linux_dma_map_phys(dev, page_to_phys(page) + offset, size)); 185 } 186 187 /* linux_dma_(un)map_sg_attrs does not support attrs yet */ 188 #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \ 189 linux_dma_map_sg_attrs(dev, sgl, nents, dir, 0) 190 191 #define dma_unmap_sg_attrs(dev, sg, nents, dir, attrs) \ 192 linux_dma_unmap_sg_attrs(dev, sg, nents, dir, 0) 193 194 static inline dma_addr_t 195 dma_map_page(struct device *dev, struct page *page, 196 unsigned long offset, size_t size, enum dma_data_direction direction) 197 { 198 199 return (linux_dma_map_phys(dev, page_to_phys(page) + offset, size)); 200 } 201 202 static inline void 203 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size, 204 enum dma_data_direction direction) 205 { 206 207 linux_dma_unmap(dev, dma_address, size); 208 } 209 210 static inline void 211 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma, size_t size, 212 enum dma_data_direction direction) 213 { 214 bus_dmasync_op_t op; 215 216 switch (direction) { 217 case DMA_BIDIRECTIONAL: 218 op = BUS_DMASYNC_POSTREAD; 219 linuxkpi_dma_sync(dev, dma, size, op); 220 op = BUS_DMASYNC_PREREAD; 221 break; 222 case DMA_TO_DEVICE: 223 op = BUS_DMASYNC_POSTWRITE; 224 break; 225 case DMA_FROM_DEVICE: 226 op = BUS_DMASYNC_POSTREAD; 227 break; 228 default: 229 return; 230 } 231 232 linuxkpi_dma_sync(dev, dma, size, op); 233 } 234 235 static inline void 236 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size, 237 enum dma_data_direction dir) 238 { 239 dma_sync_single_for_cpu(dev, addr, size, dir); 240 } 241 242 static inline void 243 dma_sync_single_for_device(struct device *dev, dma_addr_t dma, 244 size_t size, enum dma_data_direction direction) 245 { 246 bus_dmasync_op_t op; 247 248 switch (direction) { 249 case DMA_BIDIRECTIONAL: 250 op = BUS_DMASYNC_PREWRITE; 251 break; 252 case DMA_TO_DEVICE: 253 op = BUS_DMASYNC_PREREAD; 254 break; 255 case DMA_FROM_DEVICE: 256 op = BUS_DMASYNC_PREWRITE; 257 break; 258 default: 259 return; 260 } 261 262 linuxkpi_dma_sync(dev, dma, size, op); 263 } 264 265 static inline void 266 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems, 267 enum dma_data_direction direction) 268 { 269 } 270 271 static inline void 272 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems, 273 enum dma_data_direction direction) 274 { 275 } 276 277 static inline void 278 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle, 279 unsigned long offset, size_t size, int direction) 280 { 281 } 282 283 static inline void 284 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle, 285 unsigned long offset, size_t size, int direction) 286 { 287 } 288 289 #define DMA_MAPPING_ERROR (~(dma_addr_t)0) 290 291 static inline int 292 dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 293 { 294 295 if (dma_addr == 0 || dma_addr == DMA_MAPPING_ERROR) 296 return (-ENOMEM); 297 return (0); 298 } 299 300 static inline unsigned int dma_set_max_seg_size(struct device *dev, 301 unsigned int size) 302 { 303 return (0); 304 } 305 306 static inline dma_addr_t 307 _dma_map_single_attrs(struct device *dev, void *ptr, size_t size, 308 enum dma_data_direction direction, unsigned long attrs __unused) 309 { 310 dma_addr_t dma; 311 312 dma = linux_dma_map_phys(dev, vtophys(ptr), size); 313 if (!dma_mapping_error(dev, dma)) 314 dma_sync_single_for_device(dev, dma, size, direction); 315 316 return (dma); 317 } 318 319 static inline void 320 _dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size, 321 enum dma_data_direction direction, unsigned long attrs __unused) 322 { 323 324 dma_sync_single_for_cpu(dev, dma, size, direction); 325 linux_dma_unmap(dev, dma, size); 326 } 327 328 static inline size_t 329 dma_max_mapping_size(struct device *dev) 330 { 331 332 return (SCATTERLIST_MAX_SEGMENT); 333 } 334 335 #define dma_map_single_attrs(dev, ptr, size, dir, attrs) \ 336 _dma_map_single_attrs(dev, ptr, size, dir, 0) 337 338 #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \ 339 _dma_unmap_single_attrs(dev, dma_addr, size, dir, 0) 340 341 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0) 342 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0) 343 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0) 344 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0) 345 346 #define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name 347 #define DEFINE_DMA_UNMAP_LEN(name) __u32 name 348 #define dma_unmap_addr(p, name) ((p)->name) 349 #define dma_unmap_addr_set(p, name, v) (((p)->name) = (v)) 350 #define dma_unmap_len(p, name) ((p)->name) 351 #define dma_unmap_len_set(p, name, v) (((p)->name) = (v)) 352 353 extern int uma_align_cache; 354 #define dma_get_cache_alignment() uma_align_cache 355 356 357 static inline int 358 dma_map_sgtable(struct device *dev, struct sg_table *sgt, 359 enum dma_data_direction dir, 360 unsigned long attrs) 361 { 362 int nents; 363 364 nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs); 365 if (nents < 0) 366 return (nents); 367 sgt->nents = nents; 368 return (0); 369 } 370 371 static inline void 372 dma_unmap_sgtable(struct device *dev, struct sg_table *sgt, 373 enum dma_data_direction dir, 374 unsigned long attrs) 375 { 376 377 dma_unmap_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs); 378 } 379 380 381 #endif /* _LINUXKPI_LINUX_DMA_MAPPING_H_ */ 382