18d59ecb2SHans Petter Selasky /*- 28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc. 38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc. 48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc. 58d59ecb2SHans Petter Selasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 68d59ecb2SHans Petter Selasky * All rights reserved. 78d59ecb2SHans Petter Selasky * 88d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 98d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions 108d59ecb2SHans Petter Selasky * are met: 118d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 128d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following 138d59ecb2SHans Petter Selasky * disclaimer. 148d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 158d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 168d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution. 178d59ecb2SHans Petter Selasky * 188d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 198d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 208d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 218d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 228d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 238d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 248d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 258d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 268d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 278d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 288d59ecb2SHans Petter Selasky * 298d59ecb2SHans Petter Selasky * $FreeBSD$ 308d59ecb2SHans Petter Selasky */ 318d59ecb2SHans Petter Selasky #ifndef _LINUX_DMA_MAPPING_H_ 328d59ecb2SHans Petter Selasky #define _LINUX_DMA_MAPPING_H_ 338d59ecb2SHans Petter Selasky 348d59ecb2SHans Petter Selasky #include <linux/types.h> 358d59ecb2SHans Petter Selasky #include <linux/device.h> 368d59ecb2SHans Petter Selasky #include <linux/err.h> 378d59ecb2SHans Petter Selasky #include <linux/dma-attrs.h> 388d59ecb2SHans Petter Selasky #include <linux/scatterlist.h> 398d59ecb2SHans Petter Selasky #include <linux/mm.h> 408d59ecb2SHans Petter Selasky #include <linux/page.h> 418d59ecb2SHans Petter Selasky 428d59ecb2SHans Petter Selasky #include <sys/systm.h> 438d59ecb2SHans Petter Selasky #include <sys/malloc.h> 448d59ecb2SHans Petter Selasky 458d59ecb2SHans Petter Selasky #include <vm/vm.h> 468d59ecb2SHans Petter Selasky #include <vm/vm_page.h> 478d59ecb2SHans Petter Selasky #include <vm/pmap.h> 488d59ecb2SHans Petter Selasky 498d59ecb2SHans Petter Selasky #include <machine/bus.h> 508d59ecb2SHans Petter Selasky 518d59ecb2SHans Petter Selasky enum dma_data_direction { 528d59ecb2SHans Petter Selasky DMA_BIDIRECTIONAL = 0, 538d59ecb2SHans Petter Selasky DMA_TO_DEVICE = 1, 548d59ecb2SHans Petter Selasky DMA_FROM_DEVICE = 2, 558d59ecb2SHans Petter Selasky DMA_NONE = 3, 568d59ecb2SHans Petter Selasky }; 578d59ecb2SHans Petter Selasky 588d59ecb2SHans Petter Selasky struct dma_map_ops { 598d59ecb2SHans Petter Selasky void* (*alloc_coherent)(struct device *dev, size_t size, 608d59ecb2SHans Petter Selasky dma_addr_t *dma_handle, gfp_t gfp); 618d59ecb2SHans Petter Selasky void (*free_coherent)(struct device *dev, size_t size, 628d59ecb2SHans Petter Selasky void *vaddr, dma_addr_t dma_handle); 638d59ecb2SHans Petter Selasky dma_addr_t (*map_page)(struct device *dev, struct page *page, 648d59ecb2SHans Petter Selasky unsigned long offset, size_t size, enum dma_data_direction dir, 658d59ecb2SHans Petter Selasky struct dma_attrs *attrs); 668d59ecb2SHans Petter Selasky void (*unmap_page)(struct device *dev, dma_addr_t dma_handle, 678d59ecb2SHans Petter Selasky size_t size, enum dma_data_direction dir, struct dma_attrs *attrs); 688d59ecb2SHans Petter Selasky int (*map_sg)(struct device *dev, struct scatterlist *sg, 698d59ecb2SHans Petter Selasky int nents, enum dma_data_direction dir, struct dma_attrs *attrs); 708d59ecb2SHans Petter Selasky void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents, 718d59ecb2SHans Petter Selasky enum dma_data_direction dir, struct dma_attrs *attrs); 728d59ecb2SHans Petter Selasky void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle, 738d59ecb2SHans Petter Selasky size_t size, enum dma_data_direction dir); 748d59ecb2SHans Petter Selasky void (*sync_single_for_device)(struct device *dev, 758d59ecb2SHans Petter Selasky dma_addr_t dma_handle, size_t size, enum dma_data_direction dir); 768d59ecb2SHans Petter Selasky void (*sync_single_range_for_cpu)(struct device *dev, 778d59ecb2SHans Petter Selasky dma_addr_t dma_handle, unsigned long offset, size_t size, 788d59ecb2SHans Petter Selasky enum dma_data_direction dir); 798d59ecb2SHans Petter Selasky void (*sync_single_range_for_device)(struct device *dev, 808d59ecb2SHans Petter Selasky dma_addr_t dma_handle, unsigned long offset, size_t size, 818d59ecb2SHans Petter Selasky enum dma_data_direction dir); 828d59ecb2SHans Petter Selasky void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, 838d59ecb2SHans Petter Selasky int nents, enum dma_data_direction dir); 848d59ecb2SHans Petter Selasky void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg, 858d59ecb2SHans Petter Selasky int nents, enum dma_data_direction dir); 868d59ecb2SHans Petter Selasky int (*mapping_error)(struct device *dev, dma_addr_t dma_addr); 878d59ecb2SHans Petter Selasky int (*dma_supported)(struct device *dev, u64 mask); 888d59ecb2SHans Petter Selasky int is_phys; 898d59ecb2SHans Petter Selasky }; 908d59ecb2SHans Petter Selasky 918d59ecb2SHans Petter Selasky #define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL) 928d59ecb2SHans Petter Selasky 93*f211d536STycho Nightingale int linux_dma_tag_init(struct device *dev, u64 mask); 94*f211d536STycho Nightingale void *linux_dma_alloc_coherent(struct device *dev, size_t size, 95*f211d536STycho Nightingale dma_addr_t *dma_handle, gfp_t flag); 96*f211d536STycho Nightingale dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len); 97*f211d536STycho Nightingale void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size); 98*f211d536STycho Nightingale int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, 99*f211d536STycho Nightingale int nents, enum dma_data_direction dir, struct dma_attrs *attrs); 100*f211d536STycho Nightingale void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, 101*f211d536STycho Nightingale int nents, enum dma_data_direction dir, struct dma_attrs *attrs); 102*f211d536STycho Nightingale 1038d59ecb2SHans Petter Selasky static inline int 1048d59ecb2SHans Petter Selasky dma_supported(struct device *dev, u64 mask) 1058d59ecb2SHans Petter Selasky { 1068d59ecb2SHans Petter Selasky 1078d59ecb2SHans Petter Selasky /* XXX busdma takes care of this elsewhere. */ 1088d59ecb2SHans Petter Selasky return (1); 1098d59ecb2SHans Petter Selasky } 1108d59ecb2SHans Petter Selasky 1118d59ecb2SHans Petter Selasky static inline int 1128d59ecb2SHans Petter Selasky dma_set_mask(struct device *dev, u64 dma_mask) 1138d59ecb2SHans Petter Selasky { 1148d59ecb2SHans Petter Selasky 115*f211d536STycho Nightingale if (!dev->dma_priv || !dma_supported(dev, dma_mask)) 1168d59ecb2SHans Petter Selasky return -EIO; 1178d59ecb2SHans Petter Selasky 118*f211d536STycho Nightingale return (linux_dma_tag_init(dev, dma_mask)); 1198d59ecb2SHans Petter Selasky } 1208d59ecb2SHans Petter Selasky 1218d59ecb2SHans Petter Selasky static inline int 1228d59ecb2SHans Petter Selasky dma_set_coherent_mask(struct device *dev, u64 mask) 1238d59ecb2SHans Petter Selasky { 1248d59ecb2SHans Petter Selasky 1258d59ecb2SHans Petter Selasky if (!dma_supported(dev, mask)) 1268d59ecb2SHans Petter Selasky return -EIO; 1271ce4275dSPedro F. Giffuni /* XXX Currently we don't support a separate coherent mask. */ 1288d59ecb2SHans Petter Selasky return 0; 1298d59ecb2SHans Petter Selasky } 1308d59ecb2SHans Petter Selasky 1318fdb5febSHans Petter Selasky static inline int 1328fdb5febSHans Petter Selasky dma_set_mask_and_coherent(struct device *dev, u64 mask) 1338fdb5febSHans Petter Selasky { 1348fdb5febSHans Petter Selasky int r; 1358fdb5febSHans Petter Selasky 1368fdb5febSHans Petter Selasky r = dma_set_mask(dev, mask); 1378fdb5febSHans Petter Selasky if (r == 0) 1388fdb5febSHans Petter Selasky dma_set_coherent_mask(dev, mask); 1398fdb5febSHans Petter Selasky return (r); 1408fdb5febSHans Petter Selasky } 1418fdb5febSHans Petter Selasky 1428d59ecb2SHans Petter Selasky static inline void * 1438d59ecb2SHans Petter Selasky dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 1448d59ecb2SHans Petter Selasky gfp_t flag) 1458d59ecb2SHans Petter Selasky { 146*f211d536STycho Nightingale return (linux_dma_alloc_coherent(dev, size, dma_handle, flag)); 1478d59ecb2SHans Petter Selasky } 1488d59ecb2SHans Petter Selasky 1498d59ecb2SHans Petter Selasky static inline void * 1508d59ecb2SHans Petter Selasky dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 1518d59ecb2SHans Petter Selasky gfp_t flag) 1528d59ecb2SHans Petter Selasky { 1538d59ecb2SHans Petter Selasky 1548d59ecb2SHans Petter Selasky return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO)); 1558d59ecb2SHans Petter Selasky } 1568d59ecb2SHans Petter Selasky 1578d59ecb2SHans Petter Selasky static inline void 1588d59ecb2SHans Petter Selasky dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, 159*f211d536STycho Nightingale dma_addr_t dma_addr) 1608d59ecb2SHans Petter Selasky { 1618d59ecb2SHans Petter Selasky 162*f211d536STycho Nightingale linux_dma_unmap(dev, dma_addr, size); 16349bfa624SAlan Cox kmem_free((vm_offset_t)cpu_addr, size); 1648d59ecb2SHans Petter Selasky } 1658d59ecb2SHans Petter Selasky 1668d59ecb2SHans Petter Selasky static inline dma_addr_t 1678d59ecb2SHans Petter Selasky dma_map_single_attrs(struct device *dev, void *ptr, size_t size, 1688d59ecb2SHans Petter Selasky enum dma_data_direction dir, struct dma_attrs *attrs) 1698d59ecb2SHans Petter Selasky { 1708d59ecb2SHans Petter Selasky 171*f211d536STycho Nightingale return (linux_dma_map_phys(dev, vtophys(ptr), size)); 1728d59ecb2SHans Petter Selasky } 1738d59ecb2SHans Petter Selasky 1748d59ecb2SHans Petter Selasky static inline void 175*f211d536STycho Nightingale dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr, size_t size, 1768d59ecb2SHans Petter Selasky enum dma_data_direction dir, struct dma_attrs *attrs) 1778d59ecb2SHans Petter Selasky { 178*f211d536STycho Nightingale 179*f211d536STycho Nightingale linux_dma_unmap(dev, dma_addr, size); 1808d59ecb2SHans Petter Selasky } 1818d59ecb2SHans Petter Selasky 1824f081c4fSHans Petter Selasky static inline dma_addr_t 1834f081c4fSHans Petter Selasky dma_map_page_attrs(struct device *dev, struct page *page, size_t offset, 1844f081c4fSHans Petter Selasky size_t size, enum dma_data_direction dir, unsigned long attrs) 1854f081c4fSHans Petter Selasky { 1864f081c4fSHans Petter Selasky 187*f211d536STycho Nightingale return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size)); 1884f081c4fSHans Petter Selasky } 1894f081c4fSHans Petter Selasky 1908d59ecb2SHans Petter Selasky static inline int 1918d59ecb2SHans Petter Selasky dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents, 1928d59ecb2SHans Petter Selasky enum dma_data_direction dir, struct dma_attrs *attrs) 1938d59ecb2SHans Petter Selasky { 1948d59ecb2SHans Petter Selasky 195*f211d536STycho Nightingale return (linux_dma_map_sg_attrs(dev, sgl, nents, dir, attrs)); 1968d59ecb2SHans Petter Selasky } 1978d59ecb2SHans Petter Selasky 1988d59ecb2SHans Petter Selasky static inline void 1998d59ecb2SHans Petter Selasky dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, int nents, 2008d59ecb2SHans Petter Selasky enum dma_data_direction dir, struct dma_attrs *attrs) 2018d59ecb2SHans Petter Selasky { 202*f211d536STycho Nightingale 203*f211d536STycho Nightingale linux_dma_unmap_sg_attrs(dev, sg, nents, dir, attrs); 2048d59ecb2SHans Petter Selasky } 2058d59ecb2SHans Petter Selasky 2068d59ecb2SHans Petter Selasky static inline dma_addr_t 2078d59ecb2SHans Petter Selasky dma_map_page(struct device *dev, struct page *page, 2088d59ecb2SHans Petter Selasky unsigned long offset, size_t size, enum dma_data_direction direction) 2098d59ecb2SHans Petter Selasky { 2108d59ecb2SHans Petter Selasky 211*f211d536STycho Nightingale return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size)); 2128d59ecb2SHans Petter Selasky } 2138d59ecb2SHans Petter Selasky 2148d59ecb2SHans Petter Selasky static inline void 2158d59ecb2SHans Petter Selasky dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size, 2168d59ecb2SHans Petter Selasky enum dma_data_direction direction) 2178d59ecb2SHans Petter Selasky { 218*f211d536STycho Nightingale 219*f211d536STycho Nightingale linux_dma_unmap(dev, dma_address, size); 2208d59ecb2SHans Petter Selasky } 2218d59ecb2SHans Petter Selasky 2228d59ecb2SHans Petter Selasky static inline void 2238d59ecb2SHans Petter Selasky dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size, 2248d59ecb2SHans Petter Selasky enum dma_data_direction direction) 2258d59ecb2SHans Petter Selasky { 2268d59ecb2SHans Petter Selasky } 2278d59ecb2SHans Petter Selasky 2288d59ecb2SHans Petter Selasky static inline void 2298d59ecb2SHans Petter Selasky dma_sync_single(struct device *dev, dma_addr_t addr, size_t size, 2308d59ecb2SHans Petter Selasky enum dma_data_direction dir) 2318d59ecb2SHans Petter Selasky { 2328d59ecb2SHans Petter Selasky dma_sync_single_for_cpu(dev, addr, size, dir); 2338d59ecb2SHans Petter Selasky } 2348d59ecb2SHans Petter Selasky 2358d59ecb2SHans Petter Selasky static inline void 2368d59ecb2SHans Petter Selasky dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, 2378d59ecb2SHans Petter Selasky size_t size, enum dma_data_direction direction) 2388d59ecb2SHans Petter Selasky { 2398d59ecb2SHans Petter Selasky } 2408d59ecb2SHans Petter Selasky 2418d59ecb2SHans Petter Selasky static inline void 2428d59ecb2SHans Petter Selasky dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems, 2438d59ecb2SHans Petter Selasky enum dma_data_direction direction) 2448d59ecb2SHans Petter Selasky { 2458d59ecb2SHans Petter Selasky } 2468d59ecb2SHans Petter Selasky 2478d59ecb2SHans Petter Selasky static inline void 2488d59ecb2SHans Petter Selasky dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems, 2498d59ecb2SHans Petter Selasky enum dma_data_direction direction) 2508d59ecb2SHans Petter Selasky { 2518d59ecb2SHans Petter Selasky } 2528d59ecb2SHans Petter Selasky 2538d59ecb2SHans Petter Selasky static inline void 2548d59ecb2SHans Petter Selasky dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle, 2558d59ecb2SHans Petter Selasky unsigned long offset, size_t size, int direction) 2568d59ecb2SHans Petter Selasky { 2578d59ecb2SHans Petter Selasky } 2588d59ecb2SHans Petter Selasky 2598d59ecb2SHans Petter Selasky static inline void 2608d59ecb2SHans Petter Selasky dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle, 2618d59ecb2SHans Petter Selasky unsigned long offset, size_t size, int direction) 2628d59ecb2SHans Petter Selasky { 2638d59ecb2SHans Petter Selasky } 2648d59ecb2SHans Petter Selasky 2658d59ecb2SHans Petter Selasky static inline int 2668d59ecb2SHans Petter Selasky dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 2678d59ecb2SHans Petter Selasky { 2688d59ecb2SHans Petter Selasky 269*f211d536STycho Nightingale return (dma_addr == 0); 2708d59ecb2SHans Petter Selasky } 2718d59ecb2SHans Petter Selasky 2728d59ecb2SHans Petter Selasky static inline unsigned int dma_set_max_seg_size(struct device *dev, 2738d59ecb2SHans Petter Selasky unsigned int size) 2748d59ecb2SHans Petter Selasky { 2758d59ecb2SHans Petter Selasky return (0); 2768d59ecb2SHans Petter Selasky } 2778d59ecb2SHans Petter Selasky 2788d59ecb2SHans Petter Selasky 2798d59ecb2SHans Petter Selasky #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL) 2808d59ecb2SHans Petter Selasky #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL) 2818d59ecb2SHans Petter Selasky #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL) 2828d59ecb2SHans Petter Selasky #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL) 2838d59ecb2SHans Petter Selasky 2848d59ecb2SHans Petter Selasky #define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name 2858d59ecb2SHans Petter Selasky #define DEFINE_DMA_UNMAP_LEN(name) __u32 name 2868d59ecb2SHans Petter Selasky #define dma_unmap_addr(p, name) ((p)->name) 2878d59ecb2SHans Petter Selasky #define dma_unmap_addr_set(p, name, v) (((p)->name) = (v)) 2888d59ecb2SHans Petter Selasky #define dma_unmap_len(p, name) ((p)->name) 2898d59ecb2SHans Petter Selasky #define dma_unmap_len_set(p, name, v) (((p)->name) = (v)) 2908d59ecb2SHans Petter Selasky 2918d59ecb2SHans Petter Selasky extern int uma_align_cache; 2928d59ecb2SHans Petter Selasky #define dma_get_cache_alignment() uma_align_cache 2938d59ecb2SHans Petter Selasky 2948d59ecb2SHans Petter Selasky #endif /* _LINUX_DMA_MAPPING_H_ */ 295