xref: /freebsd/sys/compat/linuxkpi/common/include/linux/dma-mapping.h (revision 2c7dd66d09a1b92a4698232996cded6e5315b3bd)
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  */
29307f78f3SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_DMA_MAPPING_H_
30307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_DMA_MAPPING_H_
318d59ecb2SHans Petter Selasky 
328d59ecb2SHans Petter Selasky #include <linux/types.h>
338d59ecb2SHans Petter Selasky #include <linux/device.h>
348d59ecb2SHans Petter Selasky #include <linux/err.h>
358d59ecb2SHans Petter Selasky #include <linux/dma-attrs.h>
368d59ecb2SHans Petter Selasky #include <linux/scatterlist.h>
378d59ecb2SHans Petter Selasky #include <linux/mm.h>
388d59ecb2SHans Petter Selasky #include <linux/page.h>
3938ba9c8bSEmmanuel Vadot #include <linux/sizes.h>
408d59ecb2SHans Petter Selasky 
418d59ecb2SHans Petter Selasky #include <sys/systm.h>
428d59ecb2SHans Petter Selasky #include <sys/malloc.h>
438d59ecb2SHans Petter Selasky 
448d59ecb2SHans Petter Selasky #include <vm/vm.h>
458d59ecb2SHans Petter Selasky #include <vm/vm_page.h>
46dc8f7692SOlivier Certner #include <vm/uma_align_mask.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,
6598a6984aSVladimir Kondratyev 	    unsigned long attrs);
668d59ecb2SHans Petter Selasky 	void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
6798a6984aSVladimir Kondratyev 	    size_t size, enum dma_data_direction dir, unsigned long attrs);
688d59ecb2SHans Petter Selasky 	int (*map_sg)(struct device *dev, struct scatterlist *sg,
6998a6984aSVladimir Kondratyev 	    int nents, enum dma_data_direction dir, unsigned long attrs);
708d59ecb2SHans Petter Selasky 	void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
7198a6984aSVladimir Kondratyev 	    enum dma_data_direction dir, unsigned long 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 
9372c89ce9SBjoern A. Zeeb int linux_dma_tag_init(struct device *, u64);
94c39eefe7SBjoern A. Zeeb int linux_dma_tag_init_coherent(struct device *, u64);
95f211d536STycho Nightingale void *linux_dma_alloc_coherent(struct device *dev, size_t size,
96f211d536STycho Nightingale     dma_addr_t *dma_handle, gfp_t flag);
977105f0d9SBjoern A. Zeeb void *linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size,
987105f0d9SBjoern A. Zeeb     dma_addr_t *dma_handle, gfp_t flag);
99f211d536STycho Nightingale dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len);
100f211d536STycho Nightingale void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size);
101f211d536STycho Nightingale int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
10298a6984aSVladimir Kondratyev     int nents, enum dma_data_direction dir __unused,
10398a6984aSVladimir Kondratyev     unsigned long attrs __unused);
104f211d536STycho Nightingale void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
10598a6984aSVladimir Kondratyev     int nents __unused, enum dma_data_direction dir __unused,
10698a6984aSVladimir Kondratyev     unsigned long attrs __unused);
10795edb10bSBjoern A. Zeeb void linuxkpi_dma_sync(struct device *, dma_addr_t, size_t, bus_dmasync_op_t);
108f211d536STycho Nightingale 
1098d59ecb2SHans Petter Selasky static inline int
dma_supported(struct device * dev,u64 dma_mask)11072c89ce9SBjoern A. Zeeb dma_supported(struct device *dev, u64 dma_mask)
1118d59ecb2SHans Petter Selasky {
1128d59ecb2SHans Petter Selasky 
1138d59ecb2SHans Petter Selasky 	/* XXX busdma takes care of this elsewhere. */
1148d59ecb2SHans Petter Selasky 	return (1);
1158d59ecb2SHans Petter Selasky }
1168d59ecb2SHans Petter Selasky 
1178d59ecb2SHans Petter Selasky static inline int
dma_set_mask(struct device * dev,u64 dma_mask)1188d59ecb2SHans Petter Selasky dma_set_mask(struct device *dev, u64 dma_mask)
1198d59ecb2SHans Petter Selasky {
1208d59ecb2SHans Petter Selasky 
121f211d536STycho Nightingale 	if (!dev->dma_priv || !dma_supported(dev, dma_mask))
1228d59ecb2SHans Petter Selasky 		return -EIO;
1238d59ecb2SHans Petter Selasky 
124f211d536STycho Nightingale 	return (linux_dma_tag_init(dev, dma_mask));
1258d59ecb2SHans Petter Selasky }
1268d59ecb2SHans Petter Selasky 
1278d59ecb2SHans Petter Selasky static inline int
dma_set_coherent_mask(struct device * dev,u64 dma_mask)12872c89ce9SBjoern A. Zeeb dma_set_coherent_mask(struct device *dev, u64 dma_mask)
1298d59ecb2SHans Petter Selasky {
1308d59ecb2SHans Petter Selasky 
131c39eefe7SBjoern A. Zeeb 	if (!dev->dma_priv || !dma_supported(dev, dma_mask))
1328d59ecb2SHans Petter Selasky 		return -EIO;
133c39eefe7SBjoern A. Zeeb 
134c39eefe7SBjoern A. Zeeb 	return (linux_dma_tag_init_coherent(dev, dma_mask));
1358d59ecb2SHans Petter Selasky }
1368d59ecb2SHans Petter Selasky 
1378fdb5febSHans Petter Selasky static inline int
dma_set_mask_and_coherent(struct device * dev,u64 dma_mask)13872c89ce9SBjoern A. Zeeb dma_set_mask_and_coherent(struct device *dev, u64 dma_mask)
1398fdb5febSHans Petter Selasky {
1408fdb5febSHans Petter Selasky 	int r;
1418fdb5febSHans Petter Selasky 
14272c89ce9SBjoern A. Zeeb 	r = dma_set_mask(dev, dma_mask);
1438fdb5febSHans Petter Selasky 	if (r == 0)
14472c89ce9SBjoern A. Zeeb 		dma_set_coherent_mask(dev, dma_mask);
1458fdb5febSHans Petter Selasky 	return (r);
1468fdb5febSHans Petter Selasky }
1478fdb5febSHans Petter Selasky 
1488d59ecb2SHans Petter Selasky static inline void *
dma_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag)1498d59ecb2SHans Petter Selasky dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
1508d59ecb2SHans Petter Selasky     gfp_t flag)
1518d59ecb2SHans Petter Selasky {
152f211d536STycho Nightingale 	return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));
1538d59ecb2SHans Petter Selasky }
1548d59ecb2SHans Petter Selasky 
1558d59ecb2SHans Petter Selasky static inline void *
dma_zalloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag)1568d59ecb2SHans Petter Selasky dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
1578d59ecb2SHans Petter Selasky     gfp_t flag)
1588d59ecb2SHans Petter Selasky {
1598d59ecb2SHans Petter Selasky 
1608d59ecb2SHans Petter Selasky 	return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
1618d59ecb2SHans Petter Selasky }
1628d59ecb2SHans Petter Selasky 
1637105f0d9SBjoern A. Zeeb static inline void *
dmam_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag)1647105f0d9SBjoern A. Zeeb dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
1657105f0d9SBjoern A. Zeeb     gfp_t flag)
1667105f0d9SBjoern A. Zeeb {
1677105f0d9SBjoern A. Zeeb 
1687105f0d9SBjoern A. Zeeb 	return (linuxkpi_dmam_alloc_coherent(dev, size, dma_handle, flag));
1697105f0d9SBjoern A. Zeeb }
1707105f0d9SBjoern A. Zeeb 
1718d59ecb2SHans Petter Selasky static inline void
dma_free_coherent(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_addr)1728d59ecb2SHans Petter Selasky dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
173f211d536STycho Nightingale     dma_addr_t dma_addr)
1748d59ecb2SHans Petter Selasky {
1758d59ecb2SHans Petter Selasky 
176f211d536STycho Nightingale 	linux_dma_unmap(dev, dma_addr, size);
177f49fd63aSJohn Baldwin 	kmem_free(cpu_addr, size);
1788d59ecb2SHans Petter Selasky }
1798d59ecb2SHans Petter Selasky 
1804f081c4fSHans Petter Selasky static inline dma_addr_t
dma_map_page_attrs(struct device * dev,struct page * page,size_t offset,size_t size,enum dma_data_direction dir,unsigned long attrs)1814f081c4fSHans Petter Selasky dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,
1824f081c4fSHans Petter Selasky     size_t size, enum dma_data_direction dir, unsigned long attrs)
1834f081c4fSHans Petter Selasky {
1844f081c4fSHans Petter Selasky 
185d1ea0764SBjoern A. Zeeb 	return (linux_dma_map_phys(dev, page_to_phys(page) + offset, size));
1864f081c4fSHans Petter Selasky }
1874f081c4fSHans Petter Selasky 
18898a6984aSVladimir Kondratyev /* linux_dma_(un)map_sg_attrs does not support attrs yet */
18998a6984aSVladimir Kondratyev #define	dma_map_sg_attrs(dev, sgl, nents, dir, attrs)	\
19098a6984aSVladimir Kondratyev 	linux_dma_map_sg_attrs(dev, sgl, nents, dir, 0)
1918d59ecb2SHans Petter Selasky 
19298a6984aSVladimir Kondratyev #define	dma_unmap_sg_attrs(dev, sg, nents, dir, attrs)	\
19398a6984aSVladimir Kondratyev 	linux_dma_unmap_sg_attrs(dev, sg, nents, dir, 0)
1948d59ecb2SHans Petter Selasky 
1958d59ecb2SHans Petter Selasky static inline dma_addr_t
dma_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction direction)1968d59ecb2SHans Petter Selasky dma_map_page(struct device *dev, struct page *page,
1978d59ecb2SHans Petter Selasky     unsigned long offset, size_t size, enum dma_data_direction direction)
1988d59ecb2SHans Petter Selasky {
1998d59ecb2SHans Petter Selasky 
200d1ea0764SBjoern A. Zeeb 	return (linux_dma_map_phys(dev, page_to_phys(page) + offset, size));
2018d59ecb2SHans Petter Selasky }
2028d59ecb2SHans Petter Selasky 
2038d59ecb2SHans Petter Selasky static inline void
dma_unmap_page(struct device * dev,dma_addr_t dma_address,size_t size,enum dma_data_direction direction)2048d59ecb2SHans Petter Selasky dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
2058d59ecb2SHans Petter Selasky     enum dma_data_direction direction)
2068d59ecb2SHans Petter Selasky {
207f211d536STycho Nightingale 
208f211d536STycho Nightingale 	linux_dma_unmap(dev, dma_address, size);
2098d59ecb2SHans Petter Selasky }
2108d59ecb2SHans Petter Selasky 
2118d59ecb2SHans Petter Selasky static inline void
dma_sync_single_for_cpu(struct device * dev,dma_addr_t dma,size_t size,enum dma_data_direction direction)21295edb10bSBjoern A. Zeeb dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma, size_t size,
2138d59ecb2SHans Petter Selasky     enum dma_data_direction direction)
2148d59ecb2SHans Petter Selasky {
21595edb10bSBjoern A. Zeeb 	bus_dmasync_op_t op;
21695edb10bSBjoern A. Zeeb 
21795edb10bSBjoern A. Zeeb 	switch (direction) {
21895edb10bSBjoern A. Zeeb 	case DMA_BIDIRECTIONAL:
21995edb10bSBjoern A. Zeeb 		op = BUS_DMASYNC_POSTREAD;
22095edb10bSBjoern A. Zeeb 		linuxkpi_dma_sync(dev, dma, size, op);
22195edb10bSBjoern A. Zeeb 		op = BUS_DMASYNC_PREREAD;
22295edb10bSBjoern A. Zeeb 		break;
22395edb10bSBjoern A. Zeeb 	case DMA_TO_DEVICE:
22495edb10bSBjoern A. Zeeb 		op = BUS_DMASYNC_POSTWRITE;
22595edb10bSBjoern A. Zeeb 		break;
22695edb10bSBjoern A. Zeeb 	case DMA_FROM_DEVICE:
22795edb10bSBjoern A. Zeeb 		op = BUS_DMASYNC_POSTREAD;
22895edb10bSBjoern A. Zeeb 		break;
22995edb10bSBjoern A. Zeeb 	default:
23095edb10bSBjoern A. Zeeb 		return;
23195edb10bSBjoern A. Zeeb 	}
23295edb10bSBjoern A. Zeeb 
23395edb10bSBjoern A. Zeeb 	linuxkpi_dma_sync(dev, dma, size, op);
2348d59ecb2SHans Petter Selasky }
2358d59ecb2SHans Petter Selasky 
2368d59ecb2SHans Petter Selasky static inline void
dma_sync_single(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)2378d59ecb2SHans Petter Selasky dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
2388d59ecb2SHans Petter Selasky     enum dma_data_direction dir)
2398d59ecb2SHans Petter Selasky {
2408d59ecb2SHans Petter Selasky 	dma_sync_single_for_cpu(dev, addr, size, dir);
2418d59ecb2SHans Petter Selasky }
2428d59ecb2SHans Petter Selasky 
2438d59ecb2SHans Petter Selasky static inline void
dma_sync_single_for_device(struct device * dev,dma_addr_t dma,size_t size,enum dma_data_direction direction)24495edb10bSBjoern A. Zeeb dma_sync_single_for_device(struct device *dev, dma_addr_t dma,
2458d59ecb2SHans Petter Selasky     size_t size, enum dma_data_direction direction)
2468d59ecb2SHans Petter Selasky {
24795edb10bSBjoern A. Zeeb 	bus_dmasync_op_t op;
24895edb10bSBjoern A. Zeeb 
24995edb10bSBjoern A. Zeeb 	switch (direction) {
25095edb10bSBjoern A. Zeeb 	case DMA_BIDIRECTIONAL:
25195edb10bSBjoern A. Zeeb 		op = BUS_DMASYNC_PREWRITE;
25295edb10bSBjoern A. Zeeb 		break;
25395edb10bSBjoern A. Zeeb 	case DMA_TO_DEVICE:
25495edb10bSBjoern A. Zeeb 		op = BUS_DMASYNC_PREREAD;
25595edb10bSBjoern A. Zeeb 		break;
25695edb10bSBjoern A. Zeeb 	case DMA_FROM_DEVICE:
25795edb10bSBjoern A. Zeeb 		op = BUS_DMASYNC_PREWRITE;
25895edb10bSBjoern A. Zeeb 		break;
25995edb10bSBjoern A. Zeeb 	default:
26095edb10bSBjoern A. Zeeb 		return;
26195edb10bSBjoern A. Zeeb 	}
26295edb10bSBjoern A. Zeeb 
26395edb10bSBjoern A. Zeeb 	linuxkpi_dma_sync(dev, dma, size, op);
2648d59ecb2SHans Petter Selasky }
2658d59ecb2SHans Petter Selasky 
2668d59ecb2SHans Petter Selasky static inline void
dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction direction)2678d59ecb2SHans Petter Selasky dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
2688d59ecb2SHans Petter Selasky     enum dma_data_direction direction)
2698d59ecb2SHans Petter Selasky {
2708d59ecb2SHans Petter Selasky }
2718d59ecb2SHans Petter Selasky 
2728d59ecb2SHans Petter Selasky static inline void
dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction direction)2738d59ecb2SHans Petter Selasky dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
2748d59ecb2SHans Petter Selasky     enum dma_data_direction direction)
2758d59ecb2SHans Petter Selasky {
2768d59ecb2SHans Petter Selasky }
2778d59ecb2SHans Petter Selasky 
2788d59ecb2SHans Petter Selasky static inline void
dma_sync_single_range_for_cpu(struct device * dev,dma_addr_t dma_handle,unsigned long offset,size_t size,int direction)2798d59ecb2SHans Petter Selasky dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
2808d59ecb2SHans Petter Selasky     unsigned long offset, size_t size, int direction)
2818d59ecb2SHans Petter Selasky {
2828d59ecb2SHans Petter Selasky }
2838d59ecb2SHans Petter Selasky 
2848d59ecb2SHans Petter Selasky static inline void
dma_sync_single_range_for_device(struct device * dev,dma_addr_t dma_handle,unsigned long offset,size_t size,int direction)2858d59ecb2SHans Petter Selasky dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
2868d59ecb2SHans Petter Selasky     unsigned long offset, size_t size, int direction)
2878d59ecb2SHans Petter Selasky {
2888d59ecb2SHans Petter Selasky }
2898d59ecb2SHans Petter Selasky 
2900b9bc973SBjoern A. Zeeb #define	DMA_MAPPING_ERROR	(~(dma_addr_t)0)
2910b9bc973SBjoern A. Zeeb 
2928d59ecb2SHans Petter Selasky static inline int
dma_mapping_error(struct device * dev,dma_addr_t dma_addr)2938d59ecb2SHans Petter Selasky dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
2948d59ecb2SHans Petter Selasky {
2958d59ecb2SHans Petter Selasky 
2960b9bc973SBjoern A. Zeeb 	if (dma_addr == 0 || dma_addr == DMA_MAPPING_ERROR)
2970b9bc973SBjoern A. Zeeb 		return (-ENOMEM);
2980b9bc973SBjoern A. Zeeb 	return (0);
2998d59ecb2SHans Petter Selasky }
3008d59ecb2SHans Petter Selasky 
dma_set_max_seg_size(struct device * dev,unsigned int size)3018d59ecb2SHans Petter Selasky static inline unsigned int dma_set_max_seg_size(struct device *dev,
3028d59ecb2SHans Petter Selasky     unsigned int size)
3038d59ecb2SHans Petter Selasky {
3048d59ecb2SHans Petter Selasky 	return (0);
3058d59ecb2SHans Petter Selasky }
3068d59ecb2SHans Petter Selasky 
30795edb10bSBjoern A. Zeeb static inline dma_addr_t
_dma_map_single_attrs(struct device * dev,void * ptr,size_t size,enum dma_data_direction direction,unsigned long attrs __unused)30895edb10bSBjoern A. Zeeb _dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
30995edb10bSBjoern A. Zeeb     enum dma_data_direction direction, unsigned long attrs __unused)
31095edb10bSBjoern A. Zeeb {
31195edb10bSBjoern A. Zeeb 	dma_addr_t dma;
31295edb10bSBjoern A. Zeeb 
31395edb10bSBjoern A. Zeeb 	dma = linux_dma_map_phys(dev, vtophys(ptr), size);
31495edb10bSBjoern A. Zeeb 	if (!dma_mapping_error(dev, dma))
31595edb10bSBjoern A. Zeeb 		dma_sync_single_for_device(dev, dma, size, direction);
31695edb10bSBjoern A. Zeeb 
31795edb10bSBjoern A. Zeeb 	return (dma);
31895edb10bSBjoern A. Zeeb }
31995edb10bSBjoern A. Zeeb 
32095edb10bSBjoern A. Zeeb static inline void
_dma_unmap_single_attrs(struct device * dev,dma_addr_t dma,size_t size,enum dma_data_direction direction,unsigned long attrs __unused)32195edb10bSBjoern A. Zeeb _dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size,
32295edb10bSBjoern A. Zeeb     enum dma_data_direction direction, unsigned long attrs __unused)
32395edb10bSBjoern A. Zeeb {
32495edb10bSBjoern A. Zeeb 
32595edb10bSBjoern A. Zeeb 	dma_sync_single_for_cpu(dev, dma, size, direction);
32695edb10bSBjoern A. Zeeb 	linux_dma_unmap(dev, dma, size);
32795edb10bSBjoern A. Zeeb }
32895edb10bSBjoern A. Zeeb 
3291acf9b27SEmmanuel Vadot static inline size_t
dma_max_mapping_size(struct device * dev)3301acf9b27SEmmanuel Vadot dma_max_mapping_size(struct device *dev)
3311acf9b27SEmmanuel Vadot {
3321acf9b27SEmmanuel Vadot 
3331acf9b27SEmmanuel Vadot 	return (SCATTERLIST_MAX_SEGMENT);
3341acf9b27SEmmanuel Vadot }
3351acf9b27SEmmanuel Vadot 
33695edb10bSBjoern A. Zeeb #define	dma_map_single_attrs(dev, ptr, size, dir, attrs)	\
33795edb10bSBjoern A. Zeeb 	_dma_map_single_attrs(dev, ptr, size, dir, 0)
33895edb10bSBjoern A. Zeeb 
33995edb10bSBjoern A. Zeeb #define	dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs)	\
34095edb10bSBjoern A. Zeeb 	_dma_unmap_single_attrs(dev, dma_addr, size, dir, 0)
34195edb10bSBjoern A. Zeeb 
34298a6984aSVladimir Kondratyev #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
34398a6984aSVladimir Kondratyev #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
34498a6984aSVladimir Kondratyev #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
34598a6984aSVladimir Kondratyev #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
3468d59ecb2SHans Petter Selasky 
3478d59ecb2SHans Petter Selasky #define	DEFINE_DMA_UNMAP_ADDR(name)		dma_addr_t name
3488d59ecb2SHans Petter Selasky #define	DEFINE_DMA_UNMAP_LEN(name)		__u32 name
3498d59ecb2SHans Petter Selasky #define	dma_unmap_addr(p, name)			((p)->name)
3508d59ecb2SHans Petter Selasky #define	dma_unmap_addr_set(p, name, v)		(((p)->name) = (v))
3518d59ecb2SHans Petter Selasky #define	dma_unmap_len(p, name)			((p)->name)
3528d59ecb2SHans Petter Selasky #define	dma_unmap_len_set(p, name, v)		(((p)->name) = (v))
3538d59ecb2SHans Petter Selasky 
354*2c7dd66dSOlivier Certner #define	dma_get_cache_alignment()	(uma_get_cache_align_mask() + 1)
3558d59ecb2SHans Petter Selasky 
3569202c95fSEmmanuel Vadot 
3579202c95fSEmmanuel Vadot static inline int
dma_map_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)3589202c95fSEmmanuel Vadot dma_map_sgtable(struct device *dev, struct sg_table *sgt,
3599202c95fSEmmanuel Vadot     enum dma_data_direction dir,
3609202c95fSEmmanuel Vadot     unsigned long attrs)
3619202c95fSEmmanuel Vadot {
36272621b54SEmmanuel Vadot 	int nents;
3639202c95fSEmmanuel Vadot 
36472621b54SEmmanuel Vadot 	nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
36572621b54SEmmanuel Vadot 	if (nents < 0)
36672621b54SEmmanuel Vadot 		return (nents);
3674085bde9SAustin Shafer 	sgt->nents = nents;
36872621b54SEmmanuel Vadot 	return (0);
3699202c95fSEmmanuel Vadot }
3709202c95fSEmmanuel Vadot 
3719202c95fSEmmanuel Vadot static inline void
dma_unmap_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)3729202c95fSEmmanuel Vadot dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
3739202c95fSEmmanuel Vadot     enum dma_data_direction dir,
3749202c95fSEmmanuel Vadot     unsigned long attrs)
3759202c95fSEmmanuel Vadot {
3769202c95fSEmmanuel Vadot 
3779202c95fSEmmanuel Vadot 	dma_unmap_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
3789202c95fSEmmanuel Vadot }
3799202c95fSEmmanuel Vadot 
3809202c95fSEmmanuel Vadot 
381307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_DMA_MAPPING_H_ */
382