xref: /linux-6.15/include/linux/dmaengine.h (revision 4f005dbe)
1c13c8260SChris Leech /*
2c13c8260SChris Leech  * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
3c13c8260SChris Leech  *
4c13c8260SChris Leech  * This program is free software; you can redistribute it and/or modify it
5c13c8260SChris Leech  * under the terms of the GNU General Public License as published by the Free
6c13c8260SChris Leech  * Software Foundation; either version 2 of the License, or (at your option)
7c13c8260SChris Leech  * any later version.
8c13c8260SChris Leech  *
9c13c8260SChris Leech  * This program is distributed in the hope that it will be useful, but WITHOUT
10c13c8260SChris Leech  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11c13c8260SChris Leech  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12c13c8260SChris Leech  * more details.
13c13c8260SChris Leech  *
14c13c8260SChris Leech  * You should have received a copy of the GNU General Public License along with
15c13c8260SChris Leech  * this program; if not, write to the Free Software Foundation, Inc., 59
16c13c8260SChris Leech  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17c13c8260SChris Leech  *
18c13c8260SChris Leech  * The full GNU General Public License is included in this distribution in the
19c13c8260SChris Leech  * file called COPYING.
20c13c8260SChris Leech  */
21c13c8260SChris Leech #ifndef DMAENGINE_H
22c13c8260SChris Leech #define DMAENGINE_H
231c0f16e5SDavid Woodhouse 
24c13c8260SChris Leech #include <linux/device.h>
25c13c8260SChris Leech #include <linux/uio.h>
267405f74bSDan Williams #include <linux/dma-mapping.h>
27c13c8260SChris Leech 
28c13c8260SChris Leech /**
29fe4ada2dSRandy Dunlap  * typedef dma_cookie_t - an opaque DMA cookie
30c13c8260SChris Leech  *
31c13c8260SChris Leech  * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
32c13c8260SChris Leech  */
33c13c8260SChris Leech typedef s32 dma_cookie_t;
34c13c8260SChris Leech 
35c13c8260SChris Leech #define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
36c13c8260SChris Leech 
37c13c8260SChris Leech /**
38c13c8260SChris Leech  * enum dma_status - DMA transaction status
39c13c8260SChris Leech  * @DMA_SUCCESS: transaction completed successfully
40c13c8260SChris Leech  * @DMA_IN_PROGRESS: transaction not yet processed
41c13c8260SChris Leech  * @DMA_ERROR: transaction failed
42c13c8260SChris Leech  */
43c13c8260SChris Leech enum dma_status {
44c13c8260SChris Leech 	DMA_SUCCESS,
45c13c8260SChris Leech 	DMA_IN_PROGRESS,
46c13c8260SChris Leech 	DMA_ERROR,
47c13c8260SChris Leech };
48c13c8260SChris Leech 
49c13c8260SChris Leech /**
507405f74bSDan Williams  * enum dma_transaction_type - DMA transaction types/indexes
517405f74bSDan Williams  */
527405f74bSDan Williams enum dma_transaction_type {
537405f74bSDan Williams 	DMA_MEMCPY,
547405f74bSDan Williams 	DMA_XOR,
557405f74bSDan Williams 	DMA_PQ_XOR,
567405f74bSDan Williams 	DMA_DUAL_XOR,
577405f74bSDan Williams 	DMA_PQ_UPDATE,
587405f74bSDan Williams 	DMA_ZERO_SUM,
597405f74bSDan Williams 	DMA_PQ_ZERO_SUM,
607405f74bSDan Williams 	DMA_MEMSET,
617405f74bSDan Williams 	DMA_MEMCPY_CRC32C,
627405f74bSDan Williams 	DMA_INTERRUPT,
6359b5ec21SDan Williams 	DMA_PRIVATE,
64dc0ee643SHaavard Skinnemoen 	DMA_SLAVE,
657405f74bSDan Williams };
667405f74bSDan Williams 
677405f74bSDan Williams /* last transaction type for creation of the capabilities mask */
68dc0ee643SHaavard Skinnemoen #define DMA_TX_TYPE_END (DMA_SLAVE + 1)
69dc0ee643SHaavard Skinnemoen 
707405f74bSDan Williams 
717405f74bSDan Williams /**
72636bdeaaSDan Williams  * enum dma_ctrl_flags - DMA flags to augment operation preparation,
73636bdeaaSDan Williams  * 	control completion, and communicate status.
74d4c56f97SDan Williams  * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
75d4c56f97SDan Williams  * 	this transaction
76636bdeaaSDan Williams  * @DMA_CTRL_ACK - the descriptor cannot be reused until the client
77636bdeaaSDan Williams  * 	acknowledges receipt, i.e. has has a chance to establish any
78636bdeaaSDan Williams  * 	dependency chains
79e1d181efSDan Williams  * @DMA_COMPL_SKIP_SRC_UNMAP - set to disable dma-unmapping the source buffer(s)
80e1d181efSDan Williams  * @DMA_COMPL_SKIP_DEST_UNMAP - set to disable dma-unmapping the destination(s)
81*4f005dbeSMaciej Sosnowski  * @DMA_COMPL_SRC_UNMAP_SINGLE - set to do the source dma-unmapping as single
82*4f005dbeSMaciej Sosnowski  * 	(if not set, do the source dma-unmapping as page)
83*4f005dbeSMaciej Sosnowski  * @DMA_COMPL_DEST_UNMAP_SINGLE - set to do the destination dma-unmapping as single
84*4f005dbeSMaciej Sosnowski  * 	(if not set, do the destination dma-unmapping as page)
85d4c56f97SDan Williams  */
86636bdeaaSDan Williams enum dma_ctrl_flags {
87d4c56f97SDan Williams 	DMA_PREP_INTERRUPT = (1 << 0),
88636bdeaaSDan Williams 	DMA_CTRL_ACK = (1 << 1),
89e1d181efSDan Williams 	DMA_COMPL_SKIP_SRC_UNMAP = (1 << 2),
90e1d181efSDan Williams 	DMA_COMPL_SKIP_DEST_UNMAP = (1 << 3),
91*4f005dbeSMaciej Sosnowski 	DMA_COMPL_SRC_UNMAP_SINGLE = (1 << 4),
92*4f005dbeSMaciej Sosnowski 	DMA_COMPL_DEST_UNMAP_SINGLE = (1 << 5),
93d4c56f97SDan Williams };
94d4c56f97SDan Williams 
95d4c56f97SDan Williams /**
967405f74bSDan Williams  * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
977405f74bSDan Williams  * See linux/cpumask.h
987405f74bSDan Williams  */
997405f74bSDan Williams typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
1007405f74bSDan Williams 
1017405f74bSDan Williams /**
102c13c8260SChris Leech  * struct dma_chan_percpu - the per-CPU part of struct dma_chan
103c13c8260SChris Leech  * @memcpy_count: transaction counter
104c13c8260SChris Leech  * @bytes_transferred: byte counter
105c13c8260SChris Leech  */
106c13c8260SChris Leech 
107c13c8260SChris Leech struct dma_chan_percpu {
108c13c8260SChris Leech 	/* stats */
109c13c8260SChris Leech 	unsigned long memcpy_count;
110c13c8260SChris Leech 	unsigned long bytes_transferred;
111c13c8260SChris Leech };
112c13c8260SChris Leech 
113c13c8260SChris Leech /**
114c13c8260SChris Leech  * struct dma_chan - devices supply DMA channels, clients use them
115fe4ada2dSRandy Dunlap  * @device: ptr to the dma device who supplies this channel, always !%NULL
116c13c8260SChris Leech  * @cookie: last cookie value returned to client
117fe4ada2dSRandy Dunlap  * @chan_id: channel ID for sysfs
11841d5e59cSDan Williams  * @dev: class device for sysfs
119c13c8260SChris Leech  * @device_node: used to add this to the device chan list
120c13c8260SChris Leech  * @local: per-cpu pointer to a struct dma_chan_percpu
1217cc5bf9aSDan Williams  * @client-count: how many clients are using this channel
122bec08513SDan Williams  * @table_count: number of appearances in the mem-to-mem allocation table
123287d8592SDan Williams  * @private: private data for certain client-channel associations
124c13c8260SChris Leech  */
125c13c8260SChris Leech struct dma_chan {
126c13c8260SChris Leech 	struct dma_device *device;
127c13c8260SChris Leech 	dma_cookie_t cookie;
128c13c8260SChris Leech 
129c13c8260SChris Leech 	/* sysfs */
130c13c8260SChris Leech 	int chan_id;
13141d5e59cSDan Williams 	struct dma_chan_dev *dev;
132c13c8260SChris Leech 
133c13c8260SChris Leech 	struct list_head device_node;
134c13c8260SChris Leech 	struct dma_chan_percpu *local;
1357cc5bf9aSDan Williams 	int client_count;
136bec08513SDan Williams 	int table_count;
137287d8592SDan Williams 	void *private;
138c13c8260SChris Leech };
139c13c8260SChris Leech 
14041d5e59cSDan Williams /**
14141d5e59cSDan Williams  * struct dma_chan_dev - relate sysfs device node to backing channel device
14241d5e59cSDan Williams  * @chan - driver channel device
14341d5e59cSDan Williams  * @device - sysfs device
144864498aaSDan Williams  * @dev_id - parent dma_device dev_id
145864498aaSDan Williams  * @idr_ref - reference count to gate release of dma_device dev_id
14641d5e59cSDan Williams  */
14741d5e59cSDan Williams struct dma_chan_dev {
14841d5e59cSDan Williams 	struct dma_chan *chan;
14941d5e59cSDan Williams 	struct device device;
150864498aaSDan Williams 	int dev_id;
151864498aaSDan Williams 	atomic_t *idr_ref;
15241d5e59cSDan Williams };
15341d5e59cSDan Williams 
15441d5e59cSDan Williams static inline const char *dma_chan_name(struct dma_chan *chan)
15541d5e59cSDan Williams {
15641d5e59cSDan Williams 	return dev_name(&chan->dev->device);
15741d5e59cSDan Williams }
158d379b01eSDan Williams 
159c13c8260SChris Leech void dma_chan_cleanup(struct kref *kref);
160c13c8260SChris Leech 
161c13c8260SChris Leech /**
16259b5ec21SDan Williams  * typedef dma_filter_fn - callback filter for dma_request_channel
16359b5ec21SDan Williams  * @chan: channel to be reviewed
16459b5ec21SDan Williams  * @filter_param: opaque parameter passed through dma_request_channel
16559b5ec21SDan Williams  *
16659b5ec21SDan Williams  * When this optional parameter is specified in a call to dma_request_channel a
16759b5ec21SDan Williams  * suitable channel is passed to this routine for further dispositioning before
16859b5ec21SDan Williams  * being returned.  Where 'suitable' indicates a non-busy channel that
1697dd60251SDan Williams  * satisfies the given capability mask.  It returns 'true' to indicate that the
1707dd60251SDan Williams  * channel is suitable.
17159b5ec21SDan Williams  */
1727dd60251SDan Williams typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
17359b5ec21SDan Williams 
1747405f74bSDan Williams typedef void (*dma_async_tx_callback)(void *dma_async_param);
1757405f74bSDan Williams /**
1767405f74bSDan Williams  * struct dma_async_tx_descriptor - async transaction descriptor
1777405f74bSDan Williams  * ---dma generic offload fields---
1787405f74bSDan Williams  * @cookie: tracking cookie for this transaction, set to -EBUSY if
1797405f74bSDan Williams  *	this tx is sitting on a dependency list
180636bdeaaSDan Williams  * @flags: flags to augment operation preparation, control completion, and
181636bdeaaSDan Williams  * 	communicate status
1827405f74bSDan Williams  * @phys: physical address of the descriptor
1837405f74bSDan Williams  * @tx_list: driver common field for operations that require multiple
1847405f74bSDan Williams  *	descriptors
1857405f74bSDan Williams  * @chan: target channel for this operation
1867405f74bSDan Williams  * @tx_submit: set the prepared descriptor(s) to be executed by the engine
1877405f74bSDan Williams  * @callback: routine to call after this operation is complete
1887405f74bSDan Williams  * @callback_param: general parameter to pass to the callback routine
1897405f74bSDan Williams  * ---async_tx api specific fields---
19019242d72SDan Williams  * @next: at completion submit this descriptor
1917405f74bSDan Williams  * @parent: pointer to the next level up in the dependency chain
19219242d72SDan Williams  * @lock: protect the parent and next pointers
1937405f74bSDan Williams  */
1947405f74bSDan Williams struct dma_async_tx_descriptor {
1957405f74bSDan Williams 	dma_cookie_t cookie;
196636bdeaaSDan Williams 	enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
1977405f74bSDan Williams 	dma_addr_t phys;
1987405f74bSDan Williams 	struct list_head tx_list;
1997405f74bSDan Williams 	struct dma_chan *chan;
2007405f74bSDan Williams 	dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
2017405f74bSDan Williams 	dma_async_tx_callback callback;
2027405f74bSDan Williams 	void *callback_param;
20319242d72SDan Williams 	struct dma_async_tx_descriptor *next;
2047405f74bSDan Williams 	struct dma_async_tx_descriptor *parent;
2057405f74bSDan Williams 	spinlock_t lock;
2067405f74bSDan Williams };
2077405f74bSDan Williams 
208c13c8260SChris Leech /**
209c13c8260SChris Leech  * struct dma_device - info on the entity supplying DMA services
210c13c8260SChris Leech  * @chancnt: how many DMA channels are supported
2110f571515SAtsushi Nemoto  * @privatecnt: how many DMA channels are requested by dma_request_channel
212c13c8260SChris Leech  * @channels: the list of struct dma_chan
213c13c8260SChris Leech  * @global_node: list_head for global dma_device_list
2147405f74bSDan Williams  * @cap_mask: one or more dma_capability flags
2157405f74bSDan Williams  * @max_xor: maximum number of xor sources, 0 if no capability
216fe4ada2dSRandy Dunlap  * @dev_id: unique device ID
2177405f74bSDan Williams  * @dev: struct device reference for dma mapping api
218fe4ada2dSRandy Dunlap  * @device_alloc_chan_resources: allocate resources and return the
219fe4ada2dSRandy Dunlap  *	number of allocated descriptors
220fe4ada2dSRandy Dunlap  * @device_free_chan_resources: release DMA channel's resources
2217405f74bSDan Williams  * @device_prep_dma_memcpy: prepares a memcpy operation
2227405f74bSDan Williams  * @device_prep_dma_xor: prepares a xor operation
2237405f74bSDan Williams  * @device_prep_dma_zero_sum: prepares a zero_sum operation
2247405f74bSDan Williams  * @device_prep_dma_memset: prepares a memset operation
2257405f74bSDan Williams  * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
226dc0ee643SHaavard Skinnemoen  * @device_prep_slave_sg: prepares a slave dma operation
227dc0ee643SHaavard Skinnemoen  * @device_terminate_all: terminate all pending operations
2281d93e52eSJohannes Weiner  * @device_is_tx_complete: poll for transaction completion
2297405f74bSDan Williams  * @device_issue_pending: push pending transactions to hardware
230c13c8260SChris Leech  */
231c13c8260SChris Leech struct dma_device {
232c13c8260SChris Leech 
233c13c8260SChris Leech 	unsigned int chancnt;
2340f571515SAtsushi Nemoto 	unsigned int privatecnt;
235c13c8260SChris Leech 	struct list_head channels;
236c13c8260SChris Leech 	struct list_head global_node;
2377405f74bSDan Williams 	dma_cap_mask_t  cap_mask;
2387405f74bSDan Williams 	int max_xor;
239c13c8260SChris Leech 
240c13c8260SChris Leech 	int dev_id;
2417405f74bSDan Williams 	struct device *dev;
242c13c8260SChris Leech 
243aa1e6f1aSDan Williams 	int (*device_alloc_chan_resources)(struct dma_chan *chan);
244c13c8260SChris Leech 	void (*device_free_chan_resources)(struct dma_chan *chan);
2457405f74bSDan Williams 
2467405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
2470036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
248d4c56f97SDan Williams 		size_t len, unsigned long flags);
2497405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
2500036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
251d4c56f97SDan Williams 		unsigned int src_cnt, size_t len, unsigned long flags);
2527405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_zero_sum)(
2530036731cSDan Williams 		struct dma_chan *chan, dma_addr_t *src,	unsigned int src_cnt,
254d4c56f97SDan Williams 		size_t len, u32 *result, unsigned long flags);
2557405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
2560036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
257d4c56f97SDan Williams 		unsigned long flags);
2587405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
259636bdeaaSDan Williams 		struct dma_chan *chan, unsigned long flags);
2607405f74bSDan Williams 
261dc0ee643SHaavard Skinnemoen 	struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
262dc0ee643SHaavard Skinnemoen 		struct dma_chan *chan, struct scatterlist *sgl,
263dc0ee643SHaavard Skinnemoen 		unsigned int sg_len, enum dma_data_direction direction,
264dc0ee643SHaavard Skinnemoen 		unsigned long flags);
265dc0ee643SHaavard Skinnemoen 	void (*device_terminate_all)(struct dma_chan *chan);
266dc0ee643SHaavard Skinnemoen 
2677405f74bSDan Williams 	enum dma_status (*device_is_tx_complete)(struct dma_chan *chan,
268c13c8260SChris Leech 			dma_cookie_t cookie, dma_cookie_t *last,
269c13c8260SChris Leech 			dma_cookie_t *used);
2707405f74bSDan Williams 	void (*device_issue_pending)(struct dma_chan *chan);
271c13c8260SChris Leech };
272c13c8260SChris Leech 
273c13c8260SChris Leech /* --- public DMA engine API --- */
274c13c8260SChris Leech 
275649274d9SDan Williams #ifdef CONFIG_DMA_ENGINE
276209b84a8SDan Williams void dmaengine_get(void);
277209b84a8SDan Williams void dmaengine_put(void);
278649274d9SDan Williams #else
279649274d9SDan Williams static inline void dmaengine_get(void)
280649274d9SDan Williams {
281649274d9SDan Williams }
282649274d9SDan Williams static inline void dmaengine_put(void)
283649274d9SDan Williams {
284649274d9SDan Williams }
285649274d9SDan Williams #endif
286649274d9SDan Williams 
287b4bd07c2SDavid S. Miller #ifdef CONFIG_NET_DMA
288b4bd07c2SDavid S. Miller #define net_dmaengine_get()	dmaengine_get()
289b4bd07c2SDavid S. Miller #define net_dmaengine_put()	dmaengine_put()
290b4bd07c2SDavid S. Miller #else
291b4bd07c2SDavid S. Miller static inline void net_dmaengine_get(void)
292b4bd07c2SDavid S. Miller {
293b4bd07c2SDavid S. Miller }
294b4bd07c2SDavid S. Miller static inline void net_dmaengine_put(void)
295b4bd07c2SDavid S. Miller {
296b4bd07c2SDavid S. Miller }
297b4bd07c2SDavid S. Miller #endif
298b4bd07c2SDavid S. Miller 
299729b5d1bSDan Williams #ifdef CONFIG_ASYNC_TX_DMA
300729b5d1bSDan Williams #define async_dmaengine_get()	dmaengine_get()
301729b5d1bSDan Williams #define async_dmaengine_put()	dmaengine_put()
302729b5d1bSDan Williams #define async_dma_find_channel(type) dma_find_channel(type)
303729b5d1bSDan Williams #else
304729b5d1bSDan Williams static inline void async_dmaengine_get(void)
305729b5d1bSDan Williams {
306729b5d1bSDan Williams }
307729b5d1bSDan Williams static inline void async_dmaengine_put(void)
308729b5d1bSDan Williams {
309729b5d1bSDan Williams }
310729b5d1bSDan Williams static inline struct dma_chan *
311729b5d1bSDan Williams async_dma_find_channel(enum dma_transaction_type type)
312729b5d1bSDan Williams {
313729b5d1bSDan Williams 	return NULL;
314729b5d1bSDan Williams }
315729b5d1bSDan Williams #endif
316729b5d1bSDan Williams 
3177405f74bSDan Williams dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
3187405f74bSDan Williams 	void *dest, void *src, size_t len);
3197405f74bSDan Williams dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
3207405f74bSDan Williams 	struct page *page, unsigned int offset, void *kdata, size_t len);
3217405f74bSDan Williams dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
322c13c8260SChris Leech 	struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
3237405f74bSDan Williams 	unsigned int src_off, size_t len);
3247405f74bSDan Williams void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
3257405f74bSDan Williams 	struct dma_chan *chan);
326c13c8260SChris Leech 
3270839875eSDan Williams static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
3287405f74bSDan Williams {
329636bdeaaSDan Williams 	tx->flags |= DMA_CTRL_ACK;
330636bdeaaSDan Williams }
331636bdeaaSDan Williams 
332ef560682SGuennadi Liakhovetski static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
333ef560682SGuennadi Liakhovetski {
334ef560682SGuennadi Liakhovetski 	tx->flags &= ~DMA_CTRL_ACK;
335ef560682SGuennadi Liakhovetski }
336ef560682SGuennadi Liakhovetski 
3370839875eSDan Williams static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
338636bdeaaSDan Williams {
3390839875eSDan Williams 	return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
340c13c8260SChris Leech }
341c13c8260SChris Leech 
3427405f74bSDan Williams #define first_dma_cap(mask) __first_dma_cap(&(mask))
3437405f74bSDan Williams static inline int __first_dma_cap(const dma_cap_mask_t *srcp)
3447405f74bSDan Williams {
3457405f74bSDan Williams 	return min_t(int, DMA_TX_TYPE_END,
3467405f74bSDan Williams 		find_first_bit(srcp->bits, DMA_TX_TYPE_END));
3477405f74bSDan Williams }
3487405f74bSDan Williams 
3497405f74bSDan Williams #define next_dma_cap(n, mask) __next_dma_cap((n), &(mask))
3507405f74bSDan Williams static inline int __next_dma_cap(int n, const dma_cap_mask_t *srcp)
3517405f74bSDan Williams {
3527405f74bSDan Williams 	return min_t(int, DMA_TX_TYPE_END,
3537405f74bSDan Williams 		find_next_bit(srcp->bits, DMA_TX_TYPE_END, n+1));
3547405f74bSDan Williams }
3557405f74bSDan Williams 
3567405f74bSDan Williams #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
3577405f74bSDan Williams static inline void
3587405f74bSDan Williams __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
3597405f74bSDan Williams {
3607405f74bSDan Williams 	set_bit(tx_type, dstp->bits);
3617405f74bSDan Williams }
3627405f74bSDan Williams 
3630f571515SAtsushi Nemoto #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
3640f571515SAtsushi Nemoto static inline void
3650f571515SAtsushi Nemoto __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
3660f571515SAtsushi Nemoto {
3670f571515SAtsushi Nemoto 	clear_bit(tx_type, dstp->bits);
3680f571515SAtsushi Nemoto }
3690f571515SAtsushi Nemoto 
37033df8ca0SDan Williams #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
37133df8ca0SDan Williams static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
37233df8ca0SDan Williams {
37333df8ca0SDan Williams 	bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
37433df8ca0SDan Williams }
37533df8ca0SDan Williams 
3767405f74bSDan Williams #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
3777405f74bSDan Williams static inline int
3787405f74bSDan Williams __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
3797405f74bSDan Williams {
3807405f74bSDan Williams 	return test_bit(tx_type, srcp->bits);
3817405f74bSDan Williams }
3827405f74bSDan Williams 
3837405f74bSDan Williams #define for_each_dma_cap_mask(cap, mask) \
3847405f74bSDan Williams 	for ((cap) = first_dma_cap(mask);	\
3857405f74bSDan Williams 		(cap) < DMA_TX_TYPE_END;	\
3867405f74bSDan Williams 		(cap) = next_dma_cap((cap), (mask)))
3877405f74bSDan Williams 
388c13c8260SChris Leech /**
3897405f74bSDan Williams  * dma_async_issue_pending - flush pending transactions to HW
390fe4ada2dSRandy Dunlap  * @chan: target DMA channel
391c13c8260SChris Leech  *
392c13c8260SChris Leech  * This allows drivers to push copies to HW in batches,
393c13c8260SChris Leech  * reducing MMIO writes where possible.
394c13c8260SChris Leech  */
3957405f74bSDan Williams static inline void dma_async_issue_pending(struct dma_chan *chan)
396c13c8260SChris Leech {
397ec8670f1SDan Williams 	chan->device->device_issue_pending(chan);
398c13c8260SChris Leech }
399c13c8260SChris Leech 
4007405f74bSDan Williams #define dma_async_memcpy_issue_pending(chan) dma_async_issue_pending(chan)
4017405f74bSDan Williams 
402c13c8260SChris Leech /**
4037405f74bSDan Williams  * dma_async_is_tx_complete - poll for transaction completion
404c13c8260SChris Leech  * @chan: DMA channel
405c13c8260SChris Leech  * @cookie: transaction identifier to check status of
406c13c8260SChris Leech  * @last: returns last completed cookie, can be NULL
407c13c8260SChris Leech  * @used: returns last issued cookie, can be NULL
408c13c8260SChris Leech  *
409c13c8260SChris Leech  * If @last and @used are passed in, upon return they reflect the driver
410c13c8260SChris Leech  * internal state and can be used with dma_async_is_complete() to check
411c13c8260SChris Leech  * the status of multiple cookies without re-checking hardware state.
412c13c8260SChris Leech  */
4137405f74bSDan Williams static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
414c13c8260SChris Leech 	dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
415c13c8260SChris Leech {
4167405f74bSDan Williams 	return chan->device->device_is_tx_complete(chan, cookie, last, used);
417c13c8260SChris Leech }
418c13c8260SChris Leech 
4197405f74bSDan Williams #define dma_async_memcpy_complete(chan, cookie, last, used)\
4207405f74bSDan Williams 	dma_async_is_tx_complete(chan, cookie, last, used)
4217405f74bSDan Williams 
422c13c8260SChris Leech /**
423c13c8260SChris Leech  * dma_async_is_complete - test a cookie against chan state
424c13c8260SChris Leech  * @cookie: transaction identifier to test status of
425c13c8260SChris Leech  * @last_complete: last know completed transaction
426c13c8260SChris Leech  * @last_used: last cookie value handed out
427c13c8260SChris Leech  *
428c13c8260SChris Leech  * dma_async_is_complete() is used in dma_async_memcpy_complete()
4298a5703f8SSebastian Siewior  * the test logic is separated for lightweight testing of multiple cookies
430c13c8260SChris Leech  */
431c13c8260SChris Leech static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
432c13c8260SChris Leech 			dma_cookie_t last_complete, dma_cookie_t last_used)
433c13c8260SChris Leech {
434c13c8260SChris Leech 	if (last_complete <= last_used) {
435c13c8260SChris Leech 		if ((cookie <= last_complete) || (cookie > last_used))
436c13c8260SChris Leech 			return DMA_SUCCESS;
437c13c8260SChris Leech 	} else {
438c13c8260SChris Leech 		if ((cookie <= last_complete) && (cookie > last_used))
439c13c8260SChris Leech 			return DMA_SUCCESS;
440c13c8260SChris Leech 	}
441c13c8260SChris Leech 	return DMA_IN_PROGRESS;
442c13c8260SChris Leech }
443c13c8260SChris Leech 
4447405f74bSDan Williams enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
44507f2211eSDan Williams #ifdef CONFIG_DMA_ENGINE
44607f2211eSDan Williams enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
447c50331e8SDan Williams void dma_issue_pending_all(void);
44807f2211eSDan Williams #else
44907f2211eSDan Williams static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
45007f2211eSDan Williams {
45107f2211eSDan Williams 	return DMA_SUCCESS;
45207f2211eSDan Williams }
453c50331e8SDan Williams static inline void dma_issue_pending_all(void)
454c50331e8SDan Williams {
455c50331e8SDan Williams 	do { } while (0);
456c50331e8SDan Williams }
45707f2211eSDan Williams #endif
458c13c8260SChris Leech 
459c13c8260SChris Leech /* --- DMA device --- */
460c13c8260SChris Leech 
461c13c8260SChris Leech int dma_async_device_register(struct dma_device *device);
462c13c8260SChris Leech void dma_async_device_unregister(struct dma_device *device);
46307f2211eSDan Williams void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
464bec08513SDan Williams struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
46559b5ec21SDan Williams #define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
46659b5ec21SDan Williams struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param);
46759b5ec21SDan Williams void dma_release_channel(struct dma_chan *chan);
468c13c8260SChris Leech 
469de5506e1SChris Leech /* --- Helper iov-locking functions --- */
470de5506e1SChris Leech 
471de5506e1SChris Leech struct dma_page_list {
472b2ddb901SAl Viro 	char __user *base_address;
473de5506e1SChris Leech 	int nr_pages;
474de5506e1SChris Leech 	struct page **pages;
475de5506e1SChris Leech };
476de5506e1SChris Leech 
477de5506e1SChris Leech struct dma_pinned_list {
478de5506e1SChris Leech 	int nr_iovecs;
479de5506e1SChris Leech 	struct dma_page_list page_list[0];
480de5506e1SChris Leech };
481de5506e1SChris Leech 
482de5506e1SChris Leech struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
483de5506e1SChris Leech void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
484de5506e1SChris Leech 
485de5506e1SChris Leech dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
486de5506e1SChris Leech 	struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
487de5506e1SChris Leech dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
488de5506e1SChris Leech 	struct dma_pinned_list *pinned_list, struct page *page,
489de5506e1SChris Leech 	unsigned int offset, size_t len);
490de5506e1SChris Leech 
491c13c8260SChris Leech #endif /* DMAENGINE_H */
492