xref: /linux-6.15/include/linux/dmaengine.h (revision b4bd07c2)
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>
26c13c8260SChris Leech #include <linux/kref.h>
27c13c8260SChris Leech #include <linux/completion.h>
28c13c8260SChris Leech #include <linux/rcupdate.h>
297405f74bSDan Williams #include <linux/dma-mapping.h>
30c13c8260SChris Leech 
31c13c8260SChris Leech /**
32fe4ada2dSRandy Dunlap  * typedef dma_cookie_t - an opaque DMA cookie
33c13c8260SChris Leech  *
34c13c8260SChris Leech  * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
35c13c8260SChris Leech  */
36c13c8260SChris Leech typedef s32 dma_cookie_t;
37c13c8260SChris Leech 
38c13c8260SChris Leech #define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
39c13c8260SChris Leech 
40c13c8260SChris Leech /**
41c13c8260SChris Leech  * enum dma_status - DMA transaction status
42c13c8260SChris Leech  * @DMA_SUCCESS: transaction completed successfully
43c13c8260SChris Leech  * @DMA_IN_PROGRESS: transaction not yet processed
44c13c8260SChris Leech  * @DMA_ERROR: transaction failed
45c13c8260SChris Leech  */
46c13c8260SChris Leech enum dma_status {
47c13c8260SChris Leech 	DMA_SUCCESS,
48c13c8260SChris Leech 	DMA_IN_PROGRESS,
49c13c8260SChris Leech 	DMA_ERROR,
50c13c8260SChris Leech };
51c13c8260SChris Leech 
52c13c8260SChris Leech /**
537405f74bSDan Williams  * enum dma_transaction_type - DMA transaction types/indexes
547405f74bSDan Williams  */
557405f74bSDan Williams enum dma_transaction_type {
567405f74bSDan Williams 	DMA_MEMCPY,
577405f74bSDan Williams 	DMA_XOR,
587405f74bSDan Williams 	DMA_PQ_XOR,
597405f74bSDan Williams 	DMA_DUAL_XOR,
607405f74bSDan Williams 	DMA_PQ_UPDATE,
617405f74bSDan Williams 	DMA_ZERO_SUM,
627405f74bSDan Williams 	DMA_PQ_ZERO_SUM,
637405f74bSDan Williams 	DMA_MEMSET,
647405f74bSDan Williams 	DMA_MEMCPY_CRC32C,
657405f74bSDan Williams 	DMA_INTERRUPT,
6659b5ec21SDan Williams 	DMA_PRIVATE,
67dc0ee643SHaavard Skinnemoen 	DMA_SLAVE,
687405f74bSDan Williams };
697405f74bSDan Williams 
707405f74bSDan Williams /* last transaction type for creation of the capabilities mask */
71dc0ee643SHaavard Skinnemoen #define DMA_TX_TYPE_END (DMA_SLAVE + 1)
72dc0ee643SHaavard Skinnemoen 
737405f74bSDan Williams 
747405f74bSDan Williams /**
75636bdeaaSDan Williams  * enum dma_ctrl_flags - DMA flags to augment operation preparation,
76636bdeaaSDan Williams  * 	control completion, and communicate status.
77d4c56f97SDan Williams  * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
78d4c56f97SDan Williams  * 	this transaction
79636bdeaaSDan Williams  * @DMA_CTRL_ACK - the descriptor cannot be reused until the client
80636bdeaaSDan Williams  * 	acknowledges receipt, i.e. has has a chance to establish any
81636bdeaaSDan Williams  * 	dependency chains
82e1d181efSDan Williams  * @DMA_COMPL_SKIP_SRC_UNMAP - set to disable dma-unmapping the source buffer(s)
83e1d181efSDan Williams  * @DMA_COMPL_SKIP_DEST_UNMAP - set to disable dma-unmapping the destination(s)
84d4c56f97SDan Williams  */
85636bdeaaSDan Williams enum dma_ctrl_flags {
86d4c56f97SDan Williams 	DMA_PREP_INTERRUPT = (1 << 0),
87636bdeaaSDan Williams 	DMA_CTRL_ACK = (1 << 1),
88e1d181efSDan Williams 	DMA_COMPL_SKIP_SRC_UNMAP = (1 << 2),
89e1d181efSDan Williams 	DMA_COMPL_SKIP_DEST_UNMAP = (1 << 3),
90d4c56f97SDan Williams };
91d4c56f97SDan Williams 
92d4c56f97SDan Williams /**
937405f74bSDan Williams  * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
947405f74bSDan Williams  * See linux/cpumask.h
957405f74bSDan Williams  */
967405f74bSDan Williams typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
977405f74bSDan Williams 
987405f74bSDan Williams /**
99c13c8260SChris Leech  * struct dma_chan_percpu - the per-CPU part of struct dma_chan
100c13c8260SChris Leech  * @refcount: local_t used for open-coded "bigref" counting
101c13c8260SChris Leech  * @memcpy_count: transaction counter
102c13c8260SChris Leech  * @bytes_transferred: byte counter
103c13c8260SChris Leech  */
104c13c8260SChris Leech 
105c13c8260SChris Leech struct dma_chan_percpu {
106c13c8260SChris Leech 	/* stats */
107c13c8260SChris Leech 	unsigned long memcpy_count;
108c13c8260SChris Leech 	unsigned long bytes_transferred;
109c13c8260SChris Leech };
110c13c8260SChris Leech 
111c13c8260SChris Leech /**
112c13c8260SChris Leech  * struct dma_chan - devices supply DMA channels, clients use them
113fe4ada2dSRandy Dunlap  * @device: ptr to the dma device who supplies this channel, always !%NULL
114c13c8260SChris Leech  * @cookie: last cookie value returned to client
115fe4ada2dSRandy Dunlap  * @chan_id: channel ID for sysfs
11641d5e59cSDan Williams  * @dev: class device for sysfs
117c13c8260SChris Leech  * @refcount: kref, used in "bigref" slow-mode
118fe4ada2dSRandy Dunlap  * @slow_ref: indicates that the DMA channel is free
119fe4ada2dSRandy Dunlap  * @rcu: the DMA channel's RCU head
120c13c8260SChris Leech  * @device_node: used to add this to the device chan list
121c13c8260SChris Leech  * @local: per-cpu pointer to a struct dma_chan_percpu
1227cc5bf9aSDan Williams  * @client-count: how many clients are using this channel
123bec08513SDan Williams  * @table_count: number of appearances in the mem-to-mem allocation table
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;
137c13c8260SChris Leech };
138c13c8260SChris Leech 
13941d5e59cSDan Williams /**
14041d5e59cSDan Williams  * struct dma_chan_dev - relate sysfs device node to backing channel device
14141d5e59cSDan Williams  * @chan - driver channel device
14241d5e59cSDan Williams  * @device - sysfs device
143864498aaSDan Williams  * @dev_id - parent dma_device dev_id
144864498aaSDan Williams  * @idr_ref - reference count to gate release of dma_device dev_id
14541d5e59cSDan Williams  */
14641d5e59cSDan Williams struct dma_chan_dev {
14741d5e59cSDan Williams 	struct dma_chan *chan;
14841d5e59cSDan Williams 	struct device device;
149864498aaSDan Williams 	int dev_id;
150864498aaSDan Williams 	atomic_t *idr_ref;
15141d5e59cSDan Williams };
15241d5e59cSDan Williams 
15341d5e59cSDan Williams static inline const char *dma_chan_name(struct dma_chan *chan)
15441d5e59cSDan Williams {
15541d5e59cSDan Williams 	return dev_name(&chan->dev->device);
15641d5e59cSDan Williams }
157d379b01eSDan Williams 
158c13c8260SChris Leech void dma_chan_cleanup(struct kref *kref);
159c13c8260SChris Leech 
160c13c8260SChris Leech /**
16159b5ec21SDan Williams  * typedef dma_filter_fn - callback filter for dma_request_channel
16259b5ec21SDan Williams  * @chan: channel to be reviewed
16359b5ec21SDan Williams  * @filter_param: opaque parameter passed through dma_request_channel
16459b5ec21SDan Williams  *
16559b5ec21SDan Williams  * When this optional parameter is specified in a call to dma_request_channel a
16659b5ec21SDan Williams  * suitable channel is passed to this routine for further dispositioning before
16759b5ec21SDan Williams  * being returned.  Where 'suitable' indicates a non-busy channel that
1687dd60251SDan Williams  * satisfies the given capability mask.  It returns 'true' to indicate that the
1697dd60251SDan Williams  * channel is suitable.
17059b5ec21SDan Williams  */
1717dd60251SDan Williams typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
17259b5ec21SDan Williams 
1737405f74bSDan Williams typedef void (*dma_async_tx_callback)(void *dma_async_param);
1747405f74bSDan Williams /**
1757405f74bSDan Williams  * struct dma_async_tx_descriptor - async transaction descriptor
1767405f74bSDan Williams  * ---dma generic offload fields---
1777405f74bSDan Williams  * @cookie: tracking cookie for this transaction, set to -EBUSY if
1787405f74bSDan Williams  *	this tx is sitting on a dependency list
179636bdeaaSDan Williams  * @flags: flags to augment operation preparation, control completion, and
180636bdeaaSDan Williams  * 	communicate status
1817405f74bSDan Williams  * @phys: physical address of the descriptor
1827405f74bSDan Williams  * @tx_list: driver common field for operations that require multiple
1837405f74bSDan Williams  *	descriptors
1847405f74bSDan Williams  * @chan: target channel for this operation
1857405f74bSDan Williams  * @tx_submit: set the prepared descriptor(s) to be executed by the engine
1867405f74bSDan Williams  * @callback: routine to call after this operation is complete
1877405f74bSDan Williams  * @callback_param: general parameter to pass to the callback routine
1887405f74bSDan Williams  * ---async_tx api specific fields---
18919242d72SDan Williams  * @next: at completion submit this descriptor
1907405f74bSDan Williams  * @parent: pointer to the next level up in the dependency chain
19119242d72SDan Williams  * @lock: protect the parent and next pointers
1927405f74bSDan Williams  */
1937405f74bSDan Williams struct dma_async_tx_descriptor {
1947405f74bSDan Williams 	dma_cookie_t cookie;
195636bdeaaSDan Williams 	enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
1967405f74bSDan Williams 	dma_addr_t phys;
1977405f74bSDan Williams 	struct list_head tx_list;
1987405f74bSDan Williams 	struct dma_chan *chan;
1997405f74bSDan Williams 	dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
2007405f74bSDan Williams 	dma_async_tx_callback callback;
2017405f74bSDan Williams 	void *callback_param;
20219242d72SDan Williams 	struct dma_async_tx_descriptor *next;
2037405f74bSDan Williams 	struct dma_async_tx_descriptor *parent;
2047405f74bSDan Williams 	spinlock_t lock;
2057405f74bSDan Williams };
2067405f74bSDan Williams 
207c13c8260SChris Leech /**
208c13c8260SChris Leech  * struct dma_device - info on the entity supplying DMA services
209c13c8260SChris Leech  * @chancnt: how many DMA channels are supported
210c13c8260SChris Leech  * @channels: the list of struct dma_chan
211c13c8260SChris Leech  * @global_node: list_head for global dma_device_list
2127405f74bSDan Williams  * @cap_mask: one or more dma_capability flags
2137405f74bSDan Williams  * @max_xor: maximum number of xor sources, 0 if no capability
214fe4ada2dSRandy Dunlap  * @refcount: reference count
215fe4ada2dSRandy Dunlap  * @done: IO completion struct
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
2287405f74bSDan Williams  * @device_issue_pending: push pending transactions to hardware
229c13c8260SChris Leech  */
230c13c8260SChris Leech struct dma_device {
231c13c8260SChris Leech 
232c13c8260SChris Leech 	unsigned int chancnt;
233c13c8260SChris Leech 	struct list_head channels;
234c13c8260SChris Leech 	struct list_head global_node;
2357405f74bSDan Williams 	dma_cap_mask_t  cap_mask;
2367405f74bSDan Williams 	int max_xor;
237c13c8260SChris Leech 
238c13c8260SChris Leech 	int dev_id;
2397405f74bSDan Williams 	struct device *dev;
240c13c8260SChris Leech 
241aa1e6f1aSDan Williams 	int (*device_alloc_chan_resources)(struct dma_chan *chan);
242c13c8260SChris Leech 	void (*device_free_chan_resources)(struct dma_chan *chan);
2437405f74bSDan Williams 
2447405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
2450036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
246d4c56f97SDan Williams 		size_t len, unsigned long flags);
2477405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
2480036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
249d4c56f97SDan Williams 		unsigned int src_cnt, size_t len, unsigned long flags);
2507405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_zero_sum)(
2510036731cSDan Williams 		struct dma_chan *chan, dma_addr_t *src,	unsigned int src_cnt,
252d4c56f97SDan Williams 		size_t len, u32 *result, unsigned long flags);
2537405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
2540036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
255d4c56f97SDan Williams 		unsigned long flags);
2567405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
257636bdeaaSDan Williams 		struct dma_chan *chan, unsigned long flags);
2587405f74bSDan Williams 
259dc0ee643SHaavard Skinnemoen 	struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
260dc0ee643SHaavard Skinnemoen 		struct dma_chan *chan, struct scatterlist *sgl,
261dc0ee643SHaavard Skinnemoen 		unsigned int sg_len, enum dma_data_direction direction,
262dc0ee643SHaavard Skinnemoen 		unsigned long flags);
263dc0ee643SHaavard Skinnemoen 	void (*device_terminate_all)(struct dma_chan *chan);
264dc0ee643SHaavard Skinnemoen 
2657405f74bSDan Williams 	enum dma_status (*device_is_tx_complete)(struct dma_chan *chan,
266c13c8260SChris Leech 			dma_cookie_t cookie, dma_cookie_t *last,
267c13c8260SChris Leech 			dma_cookie_t *used);
2687405f74bSDan Williams 	void (*device_issue_pending)(struct dma_chan *chan);
269c13c8260SChris Leech };
270c13c8260SChris Leech 
271c13c8260SChris Leech /* --- public DMA engine API --- */
272c13c8260SChris Leech 
273649274d9SDan Williams #ifdef CONFIG_DMA_ENGINE
274209b84a8SDan Williams void dmaengine_get(void);
275209b84a8SDan Williams void dmaengine_put(void);
276649274d9SDan Williams #else
277649274d9SDan Williams static inline void dmaengine_get(void)
278649274d9SDan Williams {
279649274d9SDan Williams }
280649274d9SDan Williams static inline void dmaengine_put(void)
281649274d9SDan Williams {
282649274d9SDan Williams }
283649274d9SDan Williams #endif
284649274d9SDan Williams 
285*b4bd07c2SDavid S. Miller #ifdef CONFIG_NET_DMA
286*b4bd07c2SDavid S. Miller #define net_dmaengine_get()	dmaengine_get()
287*b4bd07c2SDavid S. Miller #define net_dmaengine_put()	dmaengine_put()
288*b4bd07c2SDavid S. Miller #else
289*b4bd07c2SDavid S. Miller static inline void net_dmaengine_get(void)
290*b4bd07c2SDavid S. Miller {
291*b4bd07c2SDavid S. Miller }
292*b4bd07c2SDavid S. Miller static inline void net_dmaengine_put(void)
293*b4bd07c2SDavid S. Miller {
294*b4bd07c2SDavid S. Miller }
295*b4bd07c2SDavid S. Miller #endif
296*b4bd07c2SDavid S. Miller 
2977405f74bSDan Williams dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
2987405f74bSDan Williams 	void *dest, void *src, size_t len);
2997405f74bSDan Williams dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
3007405f74bSDan Williams 	struct page *page, unsigned int offset, void *kdata, size_t len);
3017405f74bSDan Williams dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
302c13c8260SChris Leech 	struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
3037405f74bSDan Williams 	unsigned int src_off, size_t len);
3047405f74bSDan Williams void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
3057405f74bSDan Williams 	struct dma_chan *chan);
306c13c8260SChris Leech 
3070839875eSDan Williams static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
3087405f74bSDan Williams {
309636bdeaaSDan Williams 	tx->flags |= DMA_CTRL_ACK;
310636bdeaaSDan Williams }
311636bdeaaSDan Williams 
312ef560682SGuennadi Liakhovetski static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
313ef560682SGuennadi Liakhovetski {
314ef560682SGuennadi Liakhovetski 	tx->flags &= ~DMA_CTRL_ACK;
315ef560682SGuennadi Liakhovetski }
316ef560682SGuennadi Liakhovetski 
3170839875eSDan Williams static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
318636bdeaaSDan Williams {
3190839875eSDan Williams 	return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
320c13c8260SChris Leech }
321c13c8260SChris Leech 
3227405f74bSDan Williams #define first_dma_cap(mask) __first_dma_cap(&(mask))
3237405f74bSDan Williams static inline int __first_dma_cap(const dma_cap_mask_t *srcp)
3247405f74bSDan Williams {
3257405f74bSDan Williams 	return min_t(int, DMA_TX_TYPE_END,
3267405f74bSDan Williams 		find_first_bit(srcp->bits, DMA_TX_TYPE_END));
3277405f74bSDan Williams }
3287405f74bSDan Williams 
3297405f74bSDan Williams #define next_dma_cap(n, mask) __next_dma_cap((n), &(mask))
3307405f74bSDan Williams static inline int __next_dma_cap(int n, const dma_cap_mask_t *srcp)
3317405f74bSDan Williams {
3327405f74bSDan Williams 	return min_t(int, DMA_TX_TYPE_END,
3337405f74bSDan Williams 		find_next_bit(srcp->bits, DMA_TX_TYPE_END, n+1));
3347405f74bSDan Williams }
3357405f74bSDan Williams 
3367405f74bSDan Williams #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
3377405f74bSDan Williams static inline void
3387405f74bSDan Williams __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
3397405f74bSDan Williams {
3407405f74bSDan Williams 	set_bit(tx_type, dstp->bits);
3417405f74bSDan Williams }
3427405f74bSDan Williams 
34333df8ca0SDan Williams #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
34433df8ca0SDan Williams static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
34533df8ca0SDan Williams {
34633df8ca0SDan Williams 	bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
34733df8ca0SDan Williams }
34833df8ca0SDan Williams 
3497405f74bSDan Williams #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
3507405f74bSDan Williams static inline int
3517405f74bSDan Williams __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
3527405f74bSDan Williams {
3537405f74bSDan Williams 	return test_bit(tx_type, srcp->bits);
3547405f74bSDan Williams }
3557405f74bSDan Williams 
3567405f74bSDan Williams #define for_each_dma_cap_mask(cap, mask) \
3577405f74bSDan Williams 	for ((cap) = first_dma_cap(mask);	\
3587405f74bSDan Williams 		(cap) < DMA_TX_TYPE_END;	\
3597405f74bSDan Williams 		(cap) = next_dma_cap((cap), (mask)))
3607405f74bSDan Williams 
361c13c8260SChris Leech /**
3627405f74bSDan Williams  * dma_async_issue_pending - flush pending transactions to HW
363fe4ada2dSRandy Dunlap  * @chan: target DMA channel
364c13c8260SChris Leech  *
365c13c8260SChris Leech  * This allows drivers to push copies to HW in batches,
366c13c8260SChris Leech  * reducing MMIO writes where possible.
367c13c8260SChris Leech  */
3687405f74bSDan Williams static inline void dma_async_issue_pending(struct dma_chan *chan)
369c13c8260SChris Leech {
370ec8670f1SDan Williams 	chan->device->device_issue_pending(chan);
371c13c8260SChris Leech }
372c13c8260SChris Leech 
3737405f74bSDan Williams #define dma_async_memcpy_issue_pending(chan) dma_async_issue_pending(chan)
3747405f74bSDan Williams 
375c13c8260SChris Leech /**
3767405f74bSDan Williams  * dma_async_is_tx_complete - poll for transaction completion
377c13c8260SChris Leech  * @chan: DMA channel
378c13c8260SChris Leech  * @cookie: transaction identifier to check status of
379c13c8260SChris Leech  * @last: returns last completed cookie, can be NULL
380c13c8260SChris Leech  * @used: returns last issued cookie, can be NULL
381c13c8260SChris Leech  *
382c13c8260SChris Leech  * If @last and @used are passed in, upon return they reflect the driver
383c13c8260SChris Leech  * internal state and can be used with dma_async_is_complete() to check
384c13c8260SChris Leech  * the status of multiple cookies without re-checking hardware state.
385c13c8260SChris Leech  */
3867405f74bSDan Williams static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
387c13c8260SChris Leech 	dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
388c13c8260SChris Leech {
3897405f74bSDan Williams 	return chan->device->device_is_tx_complete(chan, cookie, last, used);
390c13c8260SChris Leech }
391c13c8260SChris Leech 
3927405f74bSDan Williams #define dma_async_memcpy_complete(chan, cookie, last, used)\
3937405f74bSDan Williams 	dma_async_is_tx_complete(chan, cookie, last, used)
3947405f74bSDan Williams 
395c13c8260SChris Leech /**
396c13c8260SChris Leech  * dma_async_is_complete - test a cookie against chan state
397c13c8260SChris Leech  * @cookie: transaction identifier to test status of
398c13c8260SChris Leech  * @last_complete: last know completed transaction
399c13c8260SChris Leech  * @last_used: last cookie value handed out
400c13c8260SChris Leech  *
401c13c8260SChris Leech  * dma_async_is_complete() is used in dma_async_memcpy_complete()
4028a5703f8SSebastian Siewior  * the test logic is separated for lightweight testing of multiple cookies
403c13c8260SChris Leech  */
404c13c8260SChris Leech static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
405c13c8260SChris Leech 			dma_cookie_t last_complete, dma_cookie_t last_used)
406c13c8260SChris Leech {
407c13c8260SChris Leech 	if (last_complete <= last_used) {
408c13c8260SChris Leech 		if ((cookie <= last_complete) || (cookie > last_used))
409c13c8260SChris Leech 			return DMA_SUCCESS;
410c13c8260SChris Leech 	} else {
411c13c8260SChris Leech 		if ((cookie <= last_complete) && (cookie > last_used))
412c13c8260SChris Leech 			return DMA_SUCCESS;
413c13c8260SChris Leech 	}
414c13c8260SChris Leech 	return DMA_IN_PROGRESS;
415c13c8260SChris Leech }
416c13c8260SChris Leech 
4177405f74bSDan Williams enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
41807f2211eSDan Williams #ifdef CONFIG_DMA_ENGINE
41907f2211eSDan Williams enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
420c50331e8SDan Williams void dma_issue_pending_all(void);
42107f2211eSDan Williams #else
42207f2211eSDan Williams static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
42307f2211eSDan Williams {
42407f2211eSDan Williams 	return DMA_SUCCESS;
42507f2211eSDan Williams }
426c50331e8SDan Williams static inline void dma_issue_pending_all(void)
427c50331e8SDan Williams {
428c50331e8SDan Williams 	do { } while (0);
429c50331e8SDan Williams }
43007f2211eSDan Williams #endif
431c13c8260SChris Leech 
432c13c8260SChris Leech /* --- DMA device --- */
433c13c8260SChris Leech 
434c13c8260SChris Leech int dma_async_device_register(struct dma_device *device);
435c13c8260SChris Leech void dma_async_device_unregister(struct dma_device *device);
43607f2211eSDan Williams void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
437bec08513SDan Williams struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
43859b5ec21SDan Williams #define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
43959b5ec21SDan Williams struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param);
44059b5ec21SDan Williams void dma_release_channel(struct dma_chan *chan);
441c13c8260SChris Leech 
442de5506e1SChris Leech /* --- Helper iov-locking functions --- */
443de5506e1SChris Leech 
444de5506e1SChris Leech struct dma_page_list {
445b2ddb901SAl Viro 	char __user *base_address;
446de5506e1SChris Leech 	int nr_pages;
447de5506e1SChris Leech 	struct page **pages;
448de5506e1SChris Leech };
449de5506e1SChris Leech 
450de5506e1SChris Leech struct dma_pinned_list {
451de5506e1SChris Leech 	int nr_iovecs;
452de5506e1SChris Leech 	struct dma_page_list page_list[0];
453de5506e1SChris Leech };
454de5506e1SChris Leech 
455de5506e1SChris Leech struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
456de5506e1SChris Leech void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
457de5506e1SChris Leech 
458de5506e1SChris Leech dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
459de5506e1SChris Leech 	struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
460de5506e1SChris Leech dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
461de5506e1SChris Leech 	struct dma_pinned_list *pinned_list, struct page *page,
462de5506e1SChris Leech 	unsigned int offset, size_t len);
463de5506e1SChris Leech 
464c13c8260SChris Leech #endif /* DMAENGINE_H */
465