Path: blob/main/sys/compat/linuxkpi/common/include/linux/dma-mapping.h
103096 views
/*-1* Copyright (c) 2010 Isilon Systems, Inc.2* Copyright (c) 2010 iX Systems, Inc.3* Copyright (c) 2010 Panasas, Inc.4* Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice unmodified, this list of conditions, and the following12* disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR18* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES19* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.20* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,21* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT22* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF26* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/28#ifndef _LINUXKPI_LINUX_DMA_MAPPING_H_29#define _LINUXKPI_LINUX_DMA_MAPPING_H_3031#include <linux/types.h>32#include <linux/device.h>33#include <linux/err.h>34#include <linux/dma-attrs.h>35#include <linux/scatterlist.h>36#include <linux/mm.h>37#include <linux/page.h>38#include <linux/sizes.h>3940#include <sys/systm.h>41#include <sys/malloc.h>4243#include <vm/vm.h>44#include <vm/vm_page.h>45#include <vm/uma_align_mask.h>46#include <vm/pmap.h>4748#include <machine/bus.h>4950enum dma_data_direction {51DMA_BIDIRECTIONAL = 0,52DMA_TO_DEVICE = 1,53DMA_FROM_DEVICE = 2,54DMA_NONE = 3,55};5657struct dma_map_ops {58void* (*alloc_coherent)(struct device *dev, size_t size,59dma_addr_t *dma_handle, gfp_t gfp);60void (*free_coherent)(struct device *dev, size_t size,61void *vaddr, dma_addr_t dma_handle);62dma_addr_t (*map_page)(struct device *dev, struct page *page,63unsigned long offset, size_t size, enum dma_data_direction dir,64unsigned long attrs);65void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,66size_t size, enum dma_data_direction dir, unsigned long attrs);67int (*map_sg)(struct device *dev, struct scatterlist *sg,68int nents, enum dma_data_direction dir, unsigned long attrs);69void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,70enum dma_data_direction dir, unsigned long attrs);71void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,72size_t size, enum dma_data_direction dir);73void (*sync_single_for_device)(struct device *dev,74dma_addr_t dma_handle, size_t size, enum dma_data_direction dir);75void (*sync_single_range_for_cpu)(struct device *dev,76dma_addr_t dma_handle, unsigned long offset, size_t size,77enum dma_data_direction dir);78void (*sync_single_range_for_device)(struct device *dev,79dma_addr_t dma_handle, unsigned long offset, size_t size,80enum dma_data_direction dir);81void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,82int nents, enum dma_data_direction dir);83void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,84int nents, enum dma_data_direction dir);85int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);86int (*dma_supported)(struct device *dev, u64 mask);87int is_phys;88};8990#define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL)9192int linux_dma_tag_init(struct device *, u64);93int linux_dma_tag_init_coherent(struct device *, u64);94void *linux_dma_alloc_coherent(struct device *dev, size_t size,95dma_addr_t *dma_handle, gfp_t flag);96void *linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size,97dma_addr_t *dma_handle, gfp_t flag);98void linuxkpi_dmam_free_coherent(struct device *dev, size_t size,99void *addr, dma_addr_t dma_handle);100dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len); /* backward compat */101dma_addr_t lkpi_dma_map_phys(struct device *, vm_paddr_t, size_t,102enum dma_data_direction, unsigned long);103void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size); /* backward compat */104void lkpi_dma_unmap(struct device *, dma_addr_t, size_t,105enum dma_data_direction, unsigned long);106int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,107int nents, enum dma_data_direction direction,108unsigned long attrs __unused);109void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,110int nents __unused, enum dma_data_direction direction,111unsigned long attrs __unused);112void linuxkpi_dma_sync(struct device *, dma_addr_t, size_t, bus_dmasync_op_t);113114static inline int115dma_supported(struct device *dev, u64 dma_mask)116{117118/* XXX busdma takes care of this elsewhere. */119return (1);120}121122static inline int123dma_set_mask(struct device *dev, u64 dma_mask)124{125126if (!dev->dma_priv || !dma_supported(dev, dma_mask))127return -EIO;128129return (linux_dma_tag_init(dev, dma_mask));130}131132static inline int133dma_set_coherent_mask(struct device *dev, u64 dma_mask)134{135136if (!dev->dma_priv || !dma_supported(dev, dma_mask))137return -EIO;138139return (linux_dma_tag_init_coherent(dev, dma_mask));140}141142static inline int143dma_set_mask_and_coherent(struct device *dev, u64 dma_mask)144{145int r;146147r = dma_set_mask(dev, dma_mask);148if (r == 0)149dma_set_coherent_mask(dev, dma_mask);150return (r);151}152153static inline void *154dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,155gfp_t flag)156{157return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));158}159160static inline void *161dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,162gfp_t flag)163{164165return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));166}167168static inline void *169dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,170gfp_t flag)171{172173return (linuxkpi_dmam_alloc_coherent(dev, size, dma_handle, flag));174}175176static inline void177dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,178dma_addr_t dma_addr)179{180181lkpi_dma_unmap(dev, dma_addr, size, DMA_BIDIRECTIONAL, 0);182kmem_free(cpu_addr, size);183}184185static inline void186dmam_free_coherent(struct device *dev, size_t size, void *addr,187dma_addr_t dma_handle)188{189linuxkpi_dmam_free_coherent(dev, size, addr, dma_handle);190}191192static inline dma_addr_t193dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,194size_t size, enum dma_data_direction direction, unsigned long attrs)195{196197return (lkpi_dma_map_phys(dev, page_to_phys(page) + offset, size,198direction, attrs));199}200201/* linux_dma_(un)map_sg_attrs does not support attrs yet */202#define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \203linux_dma_map_sg_attrs(dev, sgl, nents, dir, 0)204205#define dma_unmap_sg_attrs(dev, sg, nents, dir, attrs) \206linux_dma_unmap_sg_attrs(dev, sg, nents, dir, 0)207208static inline dma_addr_t209dma_map_page(struct device *dev, struct page *page,210unsigned long offset, size_t size, enum dma_data_direction direction)211{212213return (lkpi_dma_map_phys(dev, page_to_phys(page) + offset, size,214direction, 0));215}216217static inline void218dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,219enum dma_data_direction direction)220{221222lkpi_dma_unmap(dev, dma_address, size, direction, 0);223}224225static inline dma_addr_t226dma_map_resource(struct device *dev, phys_addr_t paddr, size_t size,227enum dma_data_direction direction, unsigned long attrs)228{229return (lkpi_dma_map_phys(dev, paddr, size, direction, attrs));230}231232static inline void233dma_unmap_resource(struct device *dev, dma_addr_t dma, size_t size,234enum dma_data_direction direction, unsigned long attrs)235{236lkpi_dma_unmap(dev, dma, size, direction, attrs);237}238239static inline void240dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma, size_t size,241enum dma_data_direction direction)242{243bus_dmasync_op_t op;244245switch (direction) {246case DMA_BIDIRECTIONAL:247op = BUS_DMASYNC_POSTREAD;248linuxkpi_dma_sync(dev, dma, size, op);249op = BUS_DMASYNC_PREREAD;250break;251case DMA_TO_DEVICE:252op = BUS_DMASYNC_POSTWRITE;253break;254case DMA_FROM_DEVICE:255op = BUS_DMASYNC_POSTREAD;256break;257default:258return;259}260261linuxkpi_dma_sync(dev, dma, size, op);262}263264static inline void265dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,266enum dma_data_direction dir)267{268dma_sync_single_for_cpu(dev, addr, size, dir);269}270271static inline void272dma_sync_single_for_device(struct device *dev, dma_addr_t dma,273size_t size, enum dma_data_direction direction)274{275bus_dmasync_op_t op;276277switch (direction) {278case DMA_BIDIRECTIONAL:279op = BUS_DMASYNC_PREWRITE;280break;281case DMA_TO_DEVICE:282op = BUS_DMASYNC_PREREAD;283break;284case DMA_FROM_DEVICE:285op = BUS_DMASYNC_PREWRITE;286break;287default:288return;289}290291linuxkpi_dma_sync(dev, dma, size, op);292}293294/* (20250329) These four seem to be unused code. */295static inline void296dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,297enum dma_data_direction direction)298{299pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);300}301302static inline void303dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,304enum dma_data_direction direction)305{306pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);307}308309static inline void310dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,311unsigned long offset, size_t size, enum dma_data_direction direction)312{313pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);314}315316static inline void317dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,318unsigned long offset, size_t size, enum dma_data_direction direction)319{320pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);321}322323#define DMA_MAPPING_ERROR (~(dma_addr_t)0)324325static inline int326dma_mapping_error(struct device *dev, dma_addr_t dma_addr)327{328329if (dma_addr == 0 || dma_addr == DMA_MAPPING_ERROR)330return (-ENOMEM);331return (0);332}333334static inline unsigned int dma_set_max_seg_size(struct device *dev,335unsigned int size)336{337return (0);338}339340static inline dma_addr_t341_dma_map_single_attrs(struct device *dev, void *ptr, size_t size,342enum dma_data_direction direction, unsigned long attrs)343{344return (lkpi_dma_map_phys(dev, vtophys(ptr), size,345direction, attrs));346}347348static inline void349_dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size,350enum dma_data_direction direction, unsigned long attrs)351{352lkpi_dma_unmap(dev, dma, size, direction, attrs);353}354355static inline size_t356dma_max_mapping_size(struct device *dev)357{358359return (SCATTERLIST_MAX_SEGMENT);360}361362#define dma_map_single_attrs(dev, ptr, size, dir, attrs) \363_dma_map_single_attrs(dev, ptr, size, dir, 0)364365#define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \366_dma_unmap_single_attrs(dev, dma_addr, size, dir, 0)367368#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)369#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)370#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)371#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)372373#define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name374#define DEFINE_DMA_UNMAP_LEN(name) __u32 name375#define dma_unmap_addr(p, name) ((p)->name)376#define dma_unmap_addr_set(p, name, v) (((p)->name) = (v))377#define dma_unmap_len(p, name) ((p)->name)378#define dma_unmap_len_set(p, name, v) (((p)->name) = (v))379380#define dma_get_cache_alignment() (uma_get_cache_align_mask() + 1)381382383static inline int384dma_map_sgtable(struct device *dev, struct sg_table *sgt,385enum dma_data_direction dir,386unsigned long attrs)387{388int nents;389390nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);391if (nents < 0)392return (nents);393sgt->nents = nents;394return (0);395}396397static inline void398dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,399enum dma_data_direction dir,400unsigned long attrs)401{402403dma_unmap_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);404}405406407#endif /* _LINUXKPI_LINUX_DMA_MAPPING_H_ */408409410