1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc. 3219820Sjeff * Copyright (c) 2010 iX Systems, Inc. 4219820Sjeff * Copyright (c) 2010 Panasas, Inc. 5270710Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 6219820Sjeff * All rights reserved. 7219820Sjeff * 8219820Sjeff * Redistribution and use in source and binary forms, with or without 9219820Sjeff * modification, are permitted provided that the following conditions 10219820Sjeff * are met: 11219820Sjeff * 1. Redistributions of source code must retain the above copyright 12219820Sjeff * notice unmodified, this list of conditions, and the following 13219820Sjeff * disclaimer. 14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 15219820Sjeff * notice, this list of conditions and the following disclaimer in the 16219820Sjeff * documentation and/or other materials provided with the distribution. 17219820Sjeff * 18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28289644Shselasky * 29289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/dma-mapping.h 345920 2019-04-05 11:16:06Z hselasky $ 30219820Sjeff */ 31219820Sjeff#ifndef _LINUX_DMA_MAPPING_H_ 32219820Sjeff#define _LINUX_DMA_MAPPING_H_ 33219820Sjeff 34219820Sjeff#include <linux/types.h> 35219820Sjeff#include <linux/device.h> 36219820Sjeff#include <linux/err.h> 37219820Sjeff#include <linux/dma-attrs.h> 38219820Sjeff#include <linux/scatterlist.h> 39219820Sjeff#include <linux/mm.h> 40219820Sjeff#include <linux/page.h> 41219820Sjeff 42219820Sjeff#include <sys/systm.h> 43219820Sjeff#include <sys/malloc.h> 44219820Sjeff 45219820Sjeff#include <vm/vm.h> 46219820Sjeff#include <vm/vm_page.h> 47219820Sjeff#include <vm/pmap.h> 48219820Sjeff 49219820Sjeff#include <machine/bus.h> 50219820Sjeff 51219820Sjeffenum dma_data_direction { 52219820Sjeff DMA_BIDIRECTIONAL = 0, 53219820Sjeff DMA_TO_DEVICE = 1, 54219820Sjeff DMA_FROM_DEVICE = 2, 55219820Sjeff DMA_NONE = 3, 56219820Sjeff}; 57219820Sjeff 58219820Sjeffstruct dma_map_ops { 59219820Sjeff void* (*alloc_coherent)(struct device *dev, size_t size, 60219820Sjeff dma_addr_t *dma_handle, gfp_t gfp); 61219820Sjeff void (*free_coherent)(struct device *dev, size_t size, 62219820Sjeff void *vaddr, dma_addr_t dma_handle); 63219820Sjeff dma_addr_t (*map_page)(struct device *dev, struct page *page, 64219820Sjeff unsigned long offset, size_t size, enum dma_data_direction dir, 65219820Sjeff struct dma_attrs *attrs); 66219820Sjeff void (*unmap_page)(struct device *dev, dma_addr_t dma_handle, 67219820Sjeff size_t size, enum dma_data_direction dir, struct dma_attrs *attrs); 68219820Sjeff int (*map_sg)(struct device *dev, struct scatterlist *sg, 69219820Sjeff int nents, enum dma_data_direction dir, struct dma_attrs *attrs); 70219820Sjeff void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents, 71219820Sjeff enum dma_data_direction dir, struct dma_attrs *attrs); 72219820Sjeff void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle, 73219820Sjeff size_t size, enum dma_data_direction dir); 74219820Sjeff void (*sync_single_for_device)(struct device *dev, 75219820Sjeff dma_addr_t dma_handle, size_t size, enum dma_data_direction dir); 76219820Sjeff void (*sync_single_range_for_cpu)(struct device *dev, 77219820Sjeff dma_addr_t dma_handle, unsigned long offset, size_t size, 78219820Sjeff enum dma_data_direction dir); 79219820Sjeff void (*sync_single_range_for_device)(struct device *dev, 80219820Sjeff dma_addr_t dma_handle, unsigned long offset, size_t size, 81219820Sjeff enum dma_data_direction dir); 82219820Sjeff void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg, 83219820Sjeff int nents, enum dma_data_direction dir); 84219820Sjeff void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg, 85219820Sjeff int nents, enum dma_data_direction dir); 86219820Sjeff int (*mapping_error)(struct device *dev, dma_addr_t dma_addr); 87219820Sjeff int (*dma_supported)(struct device *dev, u64 mask); 88219820Sjeff int is_phys; 89219820Sjeff}; 90219820Sjeff 91289571Shselasky#define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL) 92219820Sjeff 93219820Sjeffstatic inline int 94219820Sjeffdma_supported(struct device *dev, u64 mask) 95219820Sjeff{ 96219820Sjeff 97219820Sjeff /* XXX busdma takes care of this elsewhere. */ 98219820Sjeff return (1); 99219820Sjeff} 100331756Semaste 101219820Sjeffstatic inline int 102219820Sjeffdma_set_mask(struct device *dev, u64 dma_mask) 103219820Sjeff{ 104219820Sjeff 105219820Sjeff if (!dev->dma_mask || !dma_supported(dev, dma_mask)) 106219820Sjeff return -EIO; 107219820Sjeff 108219820Sjeff *dev->dma_mask = dma_mask; 109219820Sjeff return (0); 110219820Sjeff} 111219820Sjeff 112219820Sjeffstatic inline int 113219820Sjeffdma_set_coherent_mask(struct device *dev, u64 mask) 114219820Sjeff{ 115219820Sjeff 116219820Sjeff if (!dma_supported(dev, mask)) 117219820Sjeff return -EIO; 118298829Spfg /* XXX Currently we don't support a separate coherent mask. */ 119219820Sjeff return 0; 120219820Sjeff} 121219820Sjeff 122345907Shselaskystatic inline int 123345907Shselaskydma_set_mask_and_coherent(struct device *dev, u64 mask) 124345907Shselasky{ 125345907Shselasky int r; 126345907Shselasky 127345907Shselasky r = dma_set_mask(dev, mask); 128345907Shselasky if (r == 0) 129345907Shselasky dma_set_coherent_mask(dev, mask); 130345907Shselasky return (r); 131345907Shselasky} 132345907Shselasky 133219820Sjeffstatic inline void * 134219820Sjeffdma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 135219820Sjeff gfp_t flag) 136219820Sjeff{ 137219820Sjeff vm_paddr_t high; 138219820Sjeff size_t align; 139219820Sjeff void *mem; 140219820Sjeff 141325936Shselasky if (dev != NULL && dev->dma_mask) 142219820Sjeff high = *dev->dma_mask; 143328653Shselasky else if (flag & GFP_DMA32) 144328653Shselasky high = BUS_SPACE_MAXADDR_32BIT; 145219820Sjeff else 146328653Shselasky high = BUS_SPACE_MAXADDR; 147219820Sjeff align = PAGE_SIZE << get_order(size); 148254025Sjeff mem = (void *)kmem_alloc_contig(kmem_arena, size, flag, 0, high, align, 149219820Sjeff 0, VM_MEMATTR_DEFAULT); 150219820Sjeff if (mem) 151219820Sjeff *dma_handle = vtophys(mem); 152219820Sjeff else 153219820Sjeff *dma_handle = 0; 154219820Sjeff return (mem); 155219820Sjeff} 156277396Shselasky 157277396Shselaskystatic inline void * 158277396Shselaskydma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, 159277396Shselasky gfp_t flag) 160277396Shselasky{ 161277396Shselasky 162277396Shselasky return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO)); 163277396Shselasky} 164331756Semaste 165219820Sjeffstatic inline void 166219820Sjeffdma_free_coherent(struct device *dev, size_t size, void *cpu_addr, 167219820Sjeff dma_addr_t dma_handle) 168219820Sjeff{ 169219820Sjeff 170254025Sjeff kmem_free(kmem_arena, (vm_offset_t)cpu_addr, size); 171219820Sjeff} 172219820Sjeff 173219820Sjeff/* XXX This only works with no iommu. */ 174219820Sjeffstatic inline dma_addr_t 175219820Sjeffdma_map_single_attrs(struct device *dev, void *ptr, size_t size, 176219820Sjeff enum dma_data_direction dir, struct dma_attrs *attrs) 177219820Sjeff{ 178219820Sjeff 179219820Sjeff return vtophys(ptr); 180219820Sjeff} 181219820Sjeff 182219820Sjeffstatic inline void 183219820Sjeffdma_unmap_single_attrs(struct device *dev, dma_addr_t addr, size_t size, 184219820Sjeff enum dma_data_direction dir, struct dma_attrs *attrs) 185219820Sjeff{ 186219820Sjeff} 187219820Sjeff 188345920Shselaskystatic inline dma_addr_t 189345920Shselaskydma_map_page_attrs(struct device *dev, struct page *page, size_t offset, 190345920Shselasky size_t size, enum dma_data_direction dir, unsigned long attrs) 191345920Shselasky{ 192345920Shselasky 193345920Shselasky return (VM_PAGE_TO_PHYS(page) + offset); 194345920Shselasky} 195345920Shselasky 196219820Sjeffstatic inline int 197219820Sjeffdma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents, 198219820Sjeff enum dma_data_direction dir, struct dma_attrs *attrs) 199219820Sjeff{ 200219820Sjeff struct scatterlist *sg; 201219820Sjeff int i; 202331756Semaste 203219820Sjeff for_each_sg(sgl, sg, nents, i) 204219820Sjeff sg_dma_address(sg) = sg_phys(sg); 205219820Sjeff 206219820Sjeff return (nents); 207219820Sjeff} 208219820Sjeff 209219820Sjeffstatic inline void 210219820Sjeffdma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, int nents, 211219820Sjeff enum dma_data_direction dir, struct dma_attrs *attrs) 212219820Sjeff{ 213219820Sjeff} 214331756Semaste 215219820Sjeffstatic inline dma_addr_t 216219820Sjeffdma_map_page(struct device *dev, struct page *page, 217219820Sjeff unsigned long offset, size_t size, enum dma_data_direction direction) 218219820Sjeff{ 219219820Sjeff 220219820Sjeff return VM_PAGE_TO_PHYS(page) + offset; 221219820Sjeff} 222219820Sjeff 223219820Sjeffstatic inline void 224219820Sjeffdma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size, 225219820Sjeff enum dma_data_direction direction) 226219820Sjeff{ 227219820Sjeff} 228219820Sjeff 229219820Sjeffstatic inline void 230219820Sjeffdma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size, 231219820Sjeff enum dma_data_direction direction) 232219820Sjeff{ 233219820Sjeff} 234219820Sjeff 235219820Sjeffstatic inline void 236219820Sjeffdma_sync_single(struct device *dev, dma_addr_t addr, size_t size, 237219820Sjeff enum dma_data_direction dir) 238219820Sjeff{ 239219820Sjeff dma_sync_single_for_cpu(dev, addr, size, dir); 240219820Sjeff} 241219820Sjeff 242219820Sjeffstatic inline void 243219820Sjeffdma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, 244219820Sjeff size_t size, enum dma_data_direction direction) 245219820Sjeff{ 246219820Sjeff} 247219820Sjeff 248219820Sjeffstatic inline void 249219820Sjeffdma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems, 250219820Sjeff enum dma_data_direction direction) 251219820Sjeff{ 252219820Sjeff} 253219820Sjeff 254219820Sjeffstatic inline void 255219820Sjeffdma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems, 256219820Sjeff enum dma_data_direction direction) 257219820Sjeff{ 258219820Sjeff} 259219820Sjeff 260219820Sjeffstatic inline void 261219820Sjeffdma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle, 262219820Sjeff unsigned long offset, size_t size, int direction) 263219820Sjeff{ 264219820Sjeff} 265219820Sjeff 266219820Sjeffstatic inline void 267219820Sjeffdma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle, 268219820Sjeff unsigned long offset, size_t size, int direction) 269219820Sjeff{ 270219820Sjeff} 271219820Sjeff 272219820Sjeffstatic inline int 273219820Sjeffdma_mapping_error(struct device *dev, dma_addr_t dma_addr) 274219820Sjeff{ 275219820Sjeff 276219820Sjeff return (0); 277219820Sjeff} 278219820Sjeff 279255932Salfredstatic inline unsigned int dma_set_max_seg_size(struct device *dev, 280331756Semaste unsigned int size) 281255932Salfred{ 282331756Semaste return (0); 283255932Salfred} 284255932Salfred 285255932Salfred 286219820Sjeff#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL) 287219820Sjeff#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL) 288219820Sjeff#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL) 289219820Sjeff#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL) 290219820Sjeff 291219820Sjeff#define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name 292219820Sjeff#define DEFINE_DMA_UNMAP_LEN(name) __u32 name 293219820Sjeff#define dma_unmap_addr(p, name) ((p)->name) 294219820Sjeff#define dma_unmap_addr_set(p, name, v) (((p)->name) = (v)) 295219820Sjeff#define dma_unmap_len(p, name) ((p)->name) 296219820Sjeff#define dma_unmap_len_set(p, name, v) (((p)->name) = (v)) 297219820Sjeff 298219820Sjeffextern int uma_align_cache; 299219820Sjeff#define dma_get_cache_alignment() uma_align_cache 300219820Sjeff 301219820Sjeff#endif /* _LINUX_DMA_MAPPING_H_ */ 302