xref: /linux-6.15/include/linux/dmaengine.h (revision e1d181ef)
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 /**
32fd3f8984SJoe Perches  * enum dma_state - resource PNP/power management state
33c13c8260SChris Leech  * @DMA_RESOURCE_SUSPEND: DMA device going into low power state
34c13c8260SChris Leech  * @DMA_RESOURCE_RESUME: DMA device returning to full power
35d379b01eSDan Williams  * @DMA_RESOURCE_AVAILABLE: DMA device available to the system
36c13c8260SChris Leech  * @DMA_RESOURCE_REMOVED: DMA device removed from the system
37c13c8260SChris Leech  */
38d379b01eSDan Williams enum dma_state {
39c13c8260SChris Leech 	DMA_RESOURCE_SUSPEND,
40c13c8260SChris Leech 	DMA_RESOURCE_RESUME,
41d379b01eSDan Williams 	DMA_RESOURCE_AVAILABLE,
42c13c8260SChris Leech 	DMA_RESOURCE_REMOVED,
43c13c8260SChris Leech };
44c13c8260SChris Leech 
45c13c8260SChris Leech /**
46d379b01eSDan Williams  * enum dma_state_client - state of the channel in the client
47d379b01eSDan Williams  * @DMA_ACK: client would like to use, or was using this channel
48d379b01eSDan Williams  * @DMA_DUP: client has already seen this channel, or is not using this channel
49d379b01eSDan Williams  * @DMA_NAK: client does not want to see any more channels
50d379b01eSDan Williams  */
51d379b01eSDan Williams enum dma_state_client {
52d379b01eSDan Williams 	DMA_ACK,
53d379b01eSDan Williams 	DMA_DUP,
54d379b01eSDan Williams 	DMA_NAK,
55d379b01eSDan Williams };
56d379b01eSDan Williams 
57d379b01eSDan Williams /**
58fe4ada2dSRandy Dunlap  * typedef dma_cookie_t - an opaque DMA cookie
59c13c8260SChris Leech  *
60c13c8260SChris Leech  * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
61c13c8260SChris Leech  */
62c13c8260SChris Leech typedef s32 dma_cookie_t;
63c13c8260SChris Leech 
64c13c8260SChris Leech #define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
65c13c8260SChris Leech 
66c13c8260SChris Leech /**
67c13c8260SChris Leech  * enum dma_status - DMA transaction status
68c13c8260SChris Leech  * @DMA_SUCCESS: transaction completed successfully
69c13c8260SChris Leech  * @DMA_IN_PROGRESS: transaction not yet processed
70c13c8260SChris Leech  * @DMA_ERROR: transaction failed
71c13c8260SChris Leech  */
72c13c8260SChris Leech enum dma_status {
73c13c8260SChris Leech 	DMA_SUCCESS,
74c13c8260SChris Leech 	DMA_IN_PROGRESS,
75c13c8260SChris Leech 	DMA_ERROR,
76c13c8260SChris Leech };
77c13c8260SChris Leech 
78c13c8260SChris Leech /**
797405f74bSDan Williams  * enum dma_transaction_type - DMA transaction types/indexes
807405f74bSDan Williams  */
817405f74bSDan Williams enum dma_transaction_type {
827405f74bSDan Williams 	DMA_MEMCPY,
837405f74bSDan Williams 	DMA_XOR,
847405f74bSDan Williams 	DMA_PQ_XOR,
857405f74bSDan Williams 	DMA_DUAL_XOR,
867405f74bSDan Williams 	DMA_PQ_UPDATE,
877405f74bSDan Williams 	DMA_ZERO_SUM,
887405f74bSDan Williams 	DMA_PQ_ZERO_SUM,
897405f74bSDan Williams 	DMA_MEMSET,
907405f74bSDan Williams 	DMA_MEMCPY_CRC32C,
917405f74bSDan Williams 	DMA_INTERRUPT,
927405f74bSDan Williams };
937405f74bSDan Williams 
947405f74bSDan Williams /* last transaction type for creation of the capabilities mask */
957405f74bSDan Williams #define DMA_TX_TYPE_END (DMA_INTERRUPT + 1)
967405f74bSDan Williams 
977405f74bSDan Williams /**
98636bdeaaSDan Williams  * enum dma_ctrl_flags - DMA flags to augment operation preparation,
99636bdeaaSDan Williams  * 	control completion, and communicate status.
100d4c56f97SDan Williams  * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
101d4c56f97SDan Williams  * 	this transaction
102636bdeaaSDan Williams  * @DMA_CTRL_ACK - the descriptor cannot be reused until the client
103636bdeaaSDan Williams  * 	acknowledges receipt, i.e. has has a chance to establish any
104636bdeaaSDan Williams  * 	dependency chains
105*e1d181efSDan Williams  * @DMA_COMPL_SKIP_SRC_UNMAP - set to disable dma-unmapping the source buffer(s)
106*e1d181efSDan Williams  * @DMA_COMPL_SKIP_DEST_UNMAP - set to disable dma-unmapping the destination(s)
107d4c56f97SDan Williams  */
108636bdeaaSDan Williams enum dma_ctrl_flags {
109d4c56f97SDan Williams 	DMA_PREP_INTERRUPT = (1 << 0),
110636bdeaaSDan Williams 	DMA_CTRL_ACK = (1 << 1),
111*e1d181efSDan Williams 	DMA_COMPL_SKIP_SRC_UNMAP = (1 << 2),
112*e1d181efSDan Williams 	DMA_COMPL_SKIP_DEST_UNMAP = (1 << 3),
113d4c56f97SDan Williams };
114d4c56f97SDan Williams 
115d4c56f97SDan Williams /**
1167405f74bSDan Williams  * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
1177405f74bSDan Williams  * See linux/cpumask.h
1187405f74bSDan Williams  */
1197405f74bSDan Williams typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
1207405f74bSDan Williams 
1217405f74bSDan Williams /**
122c13c8260SChris Leech  * struct dma_chan_percpu - the per-CPU part of struct dma_chan
123c13c8260SChris Leech  * @refcount: local_t used for open-coded "bigref" counting
124c13c8260SChris Leech  * @memcpy_count: transaction counter
125c13c8260SChris Leech  * @bytes_transferred: byte counter
126c13c8260SChris Leech  */
127c13c8260SChris Leech 
128c13c8260SChris Leech struct dma_chan_percpu {
129c13c8260SChris Leech 	local_t refcount;
130c13c8260SChris Leech 	/* stats */
131c13c8260SChris Leech 	unsigned long memcpy_count;
132c13c8260SChris Leech 	unsigned long bytes_transferred;
133c13c8260SChris Leech };
134c13c8260SChris Leech 
135c13c8260SChris Leech /**
136c13c8260SChris Leech  * struct dma_chan - devices supply DMA channels, clients use them
137fe4ada2dSRandy Dunlap  * @device: ptr to the dma device who supplies this channel, always !%NULL
138c13c8260SChris Leech  * @cookie: last cookie value returned to client
139fe4ada2dSRandy Dunlap  * @chan_id: channel ID for sysfs
140fe4ada2dSRandy Dunlap  * @class_dev: class device for sysfs
141c13c8260SChris Leech  * @refcount: kref, used in "bigref" slow-mode
142fe4ada2dSRandy Dunlap  * @slow_ref: indicates that the DMA channel is free
143fe4ada2dSRandy Dunlap  * @rcu: the DMA channel's RCU head
144c13c8260SChris Leech  * @device_node: used to add this to the device chan list
145c13c8260SChris Leech  * @local: per-cpu pointer to a struct dma_chan_percpu
1467cc5bf9aSDan Williams  * @client-count: how many clients are using this channel
147c13c8260SChris Leech  */
148c13c8260SChris Leech struct dma_chan {
149c13c8260SChris Leech 	struct dma_device *device;
150c13c8260SChris Leech 	dma_cookie_t cookie;
151c13c8260SChris Leech 
152c13c8260SChris Leech 	/* sysfs */
153c13c8260SChris Leech 	int chan_id;
154891f78eaSTony Jones 	struct device dev;
155c13c8260SChris Leech 
156c13c8260SChris Leech 	struct kref refcount;
157c13c8260SChris Leech 	int slow_ref;
158c13c8260SChris Leech 	struct rcu_head rcu;
159c13c8260SChris Leech 
160c13c8260SChris Leech 	struct list_head device_node;
161c13c8260SChris Leech 	struct dma_chan_percpu *local;
1627cc5bf9aSDan Williams 	int client_count;
163c13c8260SChris Leech };
164c13c8260SChris Leech 
165891f78eaSTony Jones #define to_dma_chan(p) container_of(p, struct dma_chan, dev)
166d379b01eSDan Williams 
167c13c8260SChris Leech void dma_chan_cleanup(struct kref *kref);
168c13c8260SChris Leech 
169c13c8260SChris Leech static inline void dma_chan_get(struct dma_chan *chan)
170c13c8260SChris Leech {
171c13c8260SChris Leech 	if (unlikely(chan->slow_ref))
172c13c8260SChris Leech 		kref_get(&chan->refcount);
173c13c8260SChris Leech 	else {
174c13c8260SChris Leech 		local_inc(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
175c13c8260SChris Leech 		put_cpu();
176c13c8260SChris Leech 	}
177c13c8260SChris Leech }
178c13c8260SChris Leech 
179c13c8260SChris Leech static inline void dma_chan_put(struct dma_chan *chan)
180c13c8260SChris Leech {
181c13c8260SChris Leech 	if (unlikely(chan->slow_ref))
182c13c8260SChris Leech 		kref_put(&chan->refcount, dma_chan_cleanup);
183c13c8260SChris Leech 	else {
184c13c8260SChris Leech 		local_dec(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
185c13c8260SChris Leech 		put_cpu();
186c13c8260SChris Leech 	}
187c13c8260SChris Leech }
188c13c8260SChris Leech 
189c13c8260SChris Leech /*
190c13c8260SChris Leech  * typedef dma_event_callback - function pointer to a DMA event callback
191d379b01eSDan Williams  * For each channel added to the system this routine is called for each client.
192d379b01eSDan Williams  * If the client would like to use the channel it returns '1' to signal (ack)
193d379b01eSDan Williams  * the dmaengine core to take out a reference on the channel and its
194d379b01eSDan Williams  * corresponding device.  A client must not 'ack' an available channel more
195d379b01eSDan Williams  * than once.  When a channel is removed all clients are notified.  If a client
196d379b01eSDan Williams  * is using the channel it must 'ack' the removal.  A client must not 'ack' a
197d379b01eSDan Williams  * removed channel more than once.
198d379b01eSDan Williams  * @client - 'this' pointer for the client context
199d379b01eSDan Williams  * @chan - channel to be acted upon
200d379b01eSDan Williams  * @state - available or removed
201c13c8260SChris Leech  */
202d379b01eSDan Williams struct dma_client;
203d379b01eSDan Williams typedef enum dma_state_client (*dma_event_callback) (struct dma_client *client,
204d379b01eSDan Williams 		struct dma_chan *chan, enum dma_state state);
205c13c8260SChris Leech 
206c13c8260SChris Leech /**
207c13c8260SChris Leech  * struct dma_client - info on the entity making use of DMA services
208c13c8260SChris Leech  * @event_callback: func ptr to call when something happens
209d379b01eSDan Williams  * @cap_mask: only return channels that satisfy the requested capabilities
210d379b01eSDan Williams  *  a value of zero corresponds to any capability
211c13c8260SChris Leech  * @global_node: list_head for global dma_client_list
212c13c8260SChris Leech  */
213c13c8260SChris Leech struct dma_client {
214c13c8260SChris Leech 	dma_event_callback	event_callback;
215d379b01eSDan Williams 	dma_cap_mask_t		cap_mask;
216c13c8260SChris Leech 	struct list_head	global_node;
217c13c8260SChris Leech };
218c13c8260SChris Leech 
2197405f74bSDan Williams typedef void (*dma_async_tx_callback)(void *dma_async_param);
2207405f74bSDan Williams /**
2217405f74bSDan Williams  * struct dma_async_tx_descriptor - async transaction descriptor
2227405f74bSDan Williams  * ---dma generic offload fields---
2237405f74bSDan Williams  * @cookie: tracking cookie for this transaction, set to -EBUSY if
2247405f74bSDan Williams  *	this tx is sitting on a dependency list
225636bdeaaSDan Williams  * @flags: flags to augment operation preparation, control completion, and
226636bdeaaSDan Williams  * 	communicate status
2277405f74bSDan Williams  * @phys: physical address of the descriptor
2287405f74bSDan Williams  * @tx_list: driver common field for operations that require multiple
2297405f74bSDan Williams  *	descriptors
2307405f74bSDan Williams  * @chan: target channel for this operation
2317405f74bSDan Williams  * @tx_submit: set the prepared descriptor(s) to be executed by the engine
2327405f74bSDan Williams  * @callback: routine to call after this operation is complete
2337405f74bSDan Williams  * @callback_param: general parameter to pass to the callback routine
2347405f74bSDan Williams  * ---async_tx api specific fields---
23519242d72SDan Williams  * @next: at completion submit this descriptor
2367405f74bSDan Williams  * @parent: pointer to the next level up in the dependency chain
23719242d72SDan Williams  * @lock: protect the parent and next pointers
2387405f74bSDan Williams  */
2397405f74bSDan Williams struct dma_async_tx_descriptor {
2407405f74bSDan Williams 	dma_cookie_t cookie;
241636bdeaaSDan Williams 	enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
2427405f74bSDan Williams 	dma_addr_t phys;
2437405f74bSDan Williams 	struct list_head tx_list;
2447405f74bSDan Williams 	struct dma_chan *chan;
2457405f74bSDan Williams 	dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
2467405f74bSDan Williams 	dma_async_tx_callback callback;
2477405f74bSDan Williams 	void *callback_param;
24819242d72SDan Williams 	struct dma_async_tx_descriptor *next;
2497405f74bSDan Williams 	struct dma_async_tx_descriptor *parent;
2507405f74bSDan Williams 	spinlock_t lock;
2517405f74bSDan Williams };
2527405f74bSDan Williams 
253c13c8260SChris Leech /**
254c13c8260SChris Leech  * struct dma_device - info on the entity supplying DMA services
255c13c8260SChris Leech  * @chancnt: how many DMA channels are supported
256c13c8260SChris Leech  * @channels: the list of struct dma_chan
257c13c8260SChris Leech  * @global_node: list_head for global dma_device_list
2587405f74bSDan Williams  * @cap_mask: one or more dma_capability flags
2597405f74bSDan Williams  * @max_xor: maximum number of xor sources, 0 if no capability
260fe4ada2dSRandy Dunlap  * @refcount: reference count
261fe4ada2dSRandy Dunlap  * @done: IO completion struct
262fe4ada2dSRandy Dunlap  * @dev_id: unique device ID
2637405f74bSDan Williams  * @dev: struct device reference for dma mapping api
264fe4ada2dSRandy Dunlap  * @device_alloc_chan_resources: allocate resources and return the
265fe4ada2dSRandy Dunlap  *	number of allocated descriptors
266fe4ada2dSRandy Dunlap  * @device_free_chan_resources: release DMA channel's resources
2677405f74bSDan Williams  * @device_prep_dma_memcpy: prepares a memcpy operation
2687405f74bSDan Williams  * @device_prep_dma_xor: prepares a xor operation
2697405f74bSDan Williams  * @device_prep_dma_zero_sum: prepares a zero_sum operation
2707405f74bSDan Williams  * @device_prep_dma_memset: prepares a memset operation
2717405f74bSDan Williams  * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
2727405f74bSDan Williams  * @device_issue_pending: push pending transactions to hardware
273c13c8260SChris Leech  */
274c13c8260SChris Leech struct dma_device {
275c13c8260SChris Leech 
276c13c8260SChris Leech 	unsigned int chancnt;
277c13c8260SChris Leech 	struct list_head channels;
278c13c8260SChris Leech 	struct list_head global_node;
2797405f74bSDan Williams 	dma_cap_mask_t  cap_mask;
2807405f74bSDan Williams 	int max_xor;
281c13c8260SChris Leech 
282c13c8260SChris Leech 	struct kref refcount;
283c13c8260SChris Leech 	struct completion done;
284c13c8260SChris Leech 
285c13c8260SChris Leech 	int dev_id;
2867405f74bSDan Williams 	struct device *dev;
287c13c8260SChris Leech 
288848c536aSHaavard Skinnemoen 	int (*device_alloc_chan_resources)(struct dma_chan *chan,
289848c536aSHaavard Skinnemoen 			struct dma_client *client);
290c13c8260SChris Leech 	void (*device_free_chan_resources)(struct dma_chan *chan);
2917405f74bSDan Williams 
2927405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
2930036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
294d4c56f97SDan Williams 		size_t len, unsigned long flags);
2957405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
2960036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
297d4c56f97SDan Williams 		unsigned int src_cnt, size_t len, unsigned long flags);
2987405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_zero_sum)(
2990036731cSDan Williams 		struct dma_chan *chan, dma_addr_t *src,	unsigned int src_cnt,
300d4c56f97SDan Williams 		size_t len, u32 *result, unsigned long flags);
3017405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
3020036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
303d4c56f97SDan Williams 		unsigned long flags);
3047405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
305636bdeaaSDan Williams 		struct dma_chan *chan, unsigned long flags);
3067405f74bSDan Williams 
3077405f74bSDan Williams 	enum dma_status (*device_is_tx_complete)(struct dma_chan *chan,
308c13c8260SChris Leech 			dma_cookie_t cookie, dma_cookie_t *last,
309c13c8260SChris Leech 			dma_cookie_t *used);
3107405f74bSDan Williams 	void (*device_issue_pending)(struct dma_chan *chan);
311c13c8260SChris Leech };
312c13c8260SChris Leech 
313c13c8260SChris Leech /* --- public DMA engine API --- */
314c13c8260SChris Leech 
315d379b01eSDan Williams void dma_async_client_register(struct dma_client *client);
316c13c8260SChris Leech void dma_async_client_unregister(struct dma_client *client);
317d379b01eSDan Williams void dma_async_client_chan_request(struct dma_client *client);
3187405f74bSDan Williams dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
3197405f74bSDan Williams 	void *dest, void *src, size_t len);
3207405f74bSDan Williams dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
3217405f74bSDan Williams 	struct page *page, unsigned int offset, void *kdata, size_t len);
3227405f74bSDan Williams dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
323c13c8260SChris Leech 	struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
3247405f74bSDan Williams 	unsigned int src_off, size_t len);
3257405f74bSDan Williams void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
3267405f74bSDan Williams 	struct dma_chan *chan);
327c13c8260SChris Leech 
3287405f74bSDan Williams static inline void
3297405f74bSDan Williams async_tx_ack(struct dma_async_tx_descriptor *tx)
3307405f74bSDan Williams {
331636bdeaaSDan Williams 	tx->flags |= DMA_CTRL_ACK;
332636bdeaaSDan Williams }
333636bdeaaSDan Williams 
334636bdeaaSDan Williams static inline int
335636bdeaaSDan Williams async_tx_test_ack(struct dma_async_tx_descriptor *tx)
336636bdeaaSDan Williams {
337636bdeaaSDan Williams 	return tx->flags & DMA_CTRL_ACK;
338c13c8260SChris Leech }
339c13c8260SChris Leech 
3407405f74bSDan Williams #define first_dma_cap(mask) __first_dma_cap(&(mask))
3417405f74bSDan Williams static inline int __first_dma_cap(const dma_cap_mask_t *srcp)
3427405f74bSDan Williams {
3437405f74bSDan Williams 	return min_t(int, DMA_TX_TYPE_END,
3447405f74bSDan Williams 		find_first_bit(srcp->bits, DMA_TX_TYPE_END));
3457405f74bSDan Williams }
3467405f74bSDan Williams 
3477405f74bSDan Williams #define next_dma_cap(n, mask) __next_dma_cap((n), &(mask))
3487405f74bSDan Williams static inline int __next_dma_cap(int n, const dma_cap_mask_t *srcp)
3497405f74bSDan Williams {
3507405f74bSDan Williams 	return min_t(int, DMA_TX_TYPE_END,
3517405f74bSDan Williams 		find_next_bit(srcp->bits, DMA_TX_TYPE_END, n+1));
3527405f74bSDan Williams }
3537405f74bSDan Williams 
3547405f74bSDan Williams #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
3557405f74bSDan Williams static inline void
3567405f74bSDan Williams __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
3577405f74bSDan Williams {
3587405f74bSDan Williams 	set_bit(tx_type, dstp->bits);
3597405f74bSDan Williams }
3607405f74bSDan Williams 
3617405f74bSDan Williams #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
3627405f74bSDan Williams static inline int
3637405f74bSDan Williams __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
3647405f74bSDan Williams {
3657405f74bSDan Williams 	return test_bit(tx_type, srcp->bits);
3667405f74bSDan Williams }
3677405f74bSDan Williams 
3687405f74bSDan Williams #define for_each_dma_cap_mask(cap, mask) \
3697405f74bSDan Williams 	for ((cap) = first_dma_cap(mask);	\
3707405f74bSDan Williams 		(cap) < DMA_TX_TYPE_END;	\
3717405f74bSDan Williams 		(cap) = next_dma_cap((cap), (mask)))
3727405f74bSDan Williams 
373c13c8260SChris Leech /**
3747405f74bSDan Williams  * dma_async_issue_pending - flush pending transactions to HW
375fe4ada2dSRandy Dunlap  * @chan: target DMA channel
376c13c8260SChris Leech  *
377c13c8260SChris Leech  * This allows drivers to push copies to HW in batches,
378c13c8260SChris Leech  * reducing MMIO writes where possible.
379c13c8260SChris Leech  */
3807405f74bSDan Williams static inline void dma_async_issue_pending(struct dma_chan *chan)
381c13c8260SChris Leech {
382ec8670f1SDan Williams 	chan->device->device_issue_pending(chan);
383c13c8260SChris Leech }
384c13c8260SChris Leech 
3857405f74bSDan Williams #define dma_async_memcpy_issue_pending(chan) dma_async_issue_pending(chan)
3867405f74bSDan Williams 
387c13c8260SChris Leech /**
3887405f74bSDan Williams  * dma_async_is_tx_complete - poll for transaction completion
389c13c8260SChris Leech  * @chan: DMA channel
390c13c8260SChris Leech  * @cookie: transaction identifier to check status of
391c13c8260SChris Leech  * @last: returns last completed cookie, can be NULL
392c13c8260SChris Leech  * @used: returns last issued cookie, can be NULL
393c13c8260SChris Leech  *
394c13c8260SChris Leech  * If @last and @used are passed in, upon return they reflect the driver
395c13c8260SChris Leech  * internal state and can be used with dma_async_is_complete() to check
396c13c8260SChris Leech  * the status of multiple cookies without re-checking hardware state.
397c13c8260SChris Leech  */
3987405f74bSDan Williams static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
399c13c8260SChris Leech 	dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
400c13c8260SChris Leech {
4017405f74bSDan Williams 	return chan->device->device_is_tx_complete(chan, cookie, last, used);
402c13c8260SChris Leech }
403c13c8260SChris Leech 
4047405f74bSDan Williams #define dma_async_memcpy_complete(chan, cookie, last, used)\
4057405f74bSDan Williams 	dma_async_is_tx_complete(chan, cookie, last, used)
4067405f74bSDan Williams 
407c13c8260SChris Leech /**
408c13c8260SChris Leech  * dma_async_is_complete - test a cookie against chan state
409c13c8260SChris Leech  * @cookie: transaction identifier to test status of
410c13c8260SChris Leech  * @last_complete: last know completed transaction
411c13c8260SChris Leech  * @last_used: last cookie value handed out
412c13c8260SChris Leech  *
413c13c8260SChris Leech  * dma_async_is_complete() is used in dma_async_memcpy_complete()
4148a5703f8SSebastian Siewior  * the test logic is separated for lightweight testing of multiple cookies
415c13c8260SChris Leech  */
416c13c8260SChris Leech static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
417c13c8260SChris Leech 			dma_cookie_t last_complete, dma_cookie_t last_used)
418c13c8260SChris Leech {
419c13c8260SChris Leech 	if (last_complete <= last_used) {
420c13c8260SChris Leech 		if ((cookie <= last_complete) || (cookie > last_used))
421c13c8260SChris Leech 			return DMA_SUCCESS;
422c13c8260SChris Leech 	} else {
423c13c8260SChris Leech 		if ((cookie <= last_complete) && (cookie > last_used))
424c13c8260SChris Leech 			return DMA_SUCCESS;
425c13c8260SChris Leech 	}
426c13c8260SChris Leech 	return DMA_IN_PROGRESS;
427c13c8260SChris Leech }
428c13c8260SChris Leech 
4297405f74bSDan Williams enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
430c13c8260SChris Leech 
431c13c8260SChris Leech /* --- DMA device --- */
432c13c8260SChris Leech 
433c13c8260SChris Leech int dma_async_device_register(struct dma_device *device);
434c13c8260SChris Leech void dma_async_device_unregister(struct dma_device *device);
435c13c8260SChris Leech 
436de5506e1SChris Leech /* --- Helper iov-locking functions --- */
437de5506e1SChris Leech 
438de5506e1SChris Leech struct dma_page_list {
439b2ddb901SAl Viro 	char __user *base_address;
440de5506e1SChris Leech 	int nr_pages;
441de5506e1SChris Leech 	struct page **pages;
442de5506e1SChris Leech };
443de5506e1SChris Leech 
444de5506e1SChris Leech struct dma_pinned_list {
445de5506e1SChris Leech 	int nr_iovecs;
446de5506e1SChris Leech 	struct dma_page_list page_list[0];
447de5506e1SChris Leech };
448de5506e1SChris Leech 
449de5506e1SChris Leech struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
450de5506e1SChris Leech void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
451de5506e1SChris Leech 
452de5506e1SChris Leech dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
453de5506e1SChris Leech 	struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
454de5506e1SChris Leech dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
455de5506e1SChris Leech 	struct dma_pinned_list *pinned_list, struct page *page,
456de5506e1SChris Leech 	unsigned int offset, size_t len);
457de5506e1SChris Leech 
458c13c8260SChris Leech #endif /* DMAENGINE_H */
459