xref: /linux-6.15/include/linux/dmaengine.h (revision a86ee03c)
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;
3476bd061fSSteven J. Magnani #define DMA_MIN_COOKIE	1
3576bd061fSSteven J. Magnani #define DMA_MAX_COOKIE	INT_MAX
36c13c8260SChris Leech 
37c13c8260SChris Leech #define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
38c13c8260SChris Leech 
39c13c8260SChris Leech /**
40c13c8260SChris Leech  * enum dma_status - DMA transaction status
41c13c8260SChris Leech  * @DMA_SUCCESS: transaction completed successfully
42c13c8260SChris Leech  * @DMA_IN_PROGRESS: transaction not yet processed
4307934481SLinus Walleij  * @DMA_PAUSED: transaction is paused
44c13c8260SChris Leech  * @DMA_ERROR: transaction failed
45c13c8260SChris Leech  */
46c13c8260SChris Leech enum dma_status {
47c13c8260SChris Leech 	DMA_SUCCESS,
48c13c8260SChris Leech 	DMA_IN_PROGRESS,
4907934481SLinus Walleij 	DMA_PAUSED,
50c13c8260SChris Leech 	DMA_ERROR,
51c13c8260SChris Leech };
52c13c8260SChris Leech 
53c13c8260SChris Leech /**
547405f74bSDan Williams  * enum dma_transaction_type - DMA transaction types/indexes
55138f4c35SDan Williams  *
56138f4c35SDan Williams  * Note: The DMA_ASYNC_TX capability is not to be set by drivers.  It is
57138f4c35SDan Williams  * automatically set as dma devices are registered.
587405f74bSDan Williams  */
597405f74bSDan Williams enum dma_transaction_type {
607405f74bSDan Williams 	DMA_MEMCPY,
617405f74bSDan Williams 	DMA_XOR,
62b2f46fd8SDan Williams 	DMA_PQ,
63099f53cbSDan Williams 	DMA_XOR_VAL,
64099f53cbSDan Williams 	DMA_PQ_VAL,
657405f74bSDan Williams 	DMA_MEMSET,
667405f74bSDan Williams 	DMA_INTERRUPT,
67*a86ee03cSIra Snyder 	DMA_SG,
6859b5ec21SDan Williams 	DMA_PRIVATE,
69138f4c35SDan Williams 	DMA_ASYNC_TX,
70dc0ee643SHaavard Skinnemoen 	DMA_SLAVE,
717405f74bSDan Williams };
727405f74bSDan Williams 
737405f74bSDan Williams /* last transaction type for creation of the capabilities mask */
74dc0ee643SHaavard Skinnemoen #define DMA_TX_TYPE_END (DMA_SLAVE + 1)
75dc0ee643SHaavard Skinnemoen 
767405f74bSDan Williams 
777405f74bSDan Williams /**
78636bdeaaSDan Williams  * enum dma_ctrl_flags - DMA flags to augment operation preparation,
79636bdeaaSDan Williams  *  control completion, and communicate status.
80d4c56f97SDan Williams  * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
81d4c56f97SDan Williams  *  this transaction
82a88f6667SGuennadi Liakhovetski  * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
83b2f46fd8SDan Williams  *  acknowledges receipt, i.e. has has a chance to establish any dependency
84b2f46fd8SDan Williams  *  chains
85e1d181efSDan Williams  * @DMA_COMPL_SKIP_SRC_UNMAP - set to disable dma-unmapping the source buffer(s)
86e1d181efSDan Williams  * @DMA_COMPL_SKIP_DEST_UNMAP - set to disable dma-unmapping the destination(s)
874f005dbeSMaciej Sosnowski  * @DMA_COMPL_SRC_UNMAP_SINGLE - set to do the source dma-unmapping as single
884f005dbeSMaciej Sosnowski  * 	(if not set, do the source dma-unmapping as page)
894f005dbeSMaciej Sosnowski  * @DMA_COMPL_DEST_UNMAP_SINGLE - set to do the destination dma-unmapping as single
904f005dbeSMaciej Sosnowski  * 	(if not set, do the destination dma-unmapping as page)
91b2f46fd8SDan Williams  * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
92b2f46fd8SDan Williams  * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
93b2f46fd8SDan Williams  * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
94b2f46fd8SDan Williams  *  sources that were the result of a previous operation, in the case of a PQ
95b2f46fd8SDan Williams  *  operation it continues the calculation with new sources
960403e382SDan Williams  * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
970403e382SDan Williams  *  on the result of this operation
98d4c56f97SDan Williams  */
99636bdeaaSDan Williams enum dma_ctrl_flags {
100d4c56f97SDan Williams 	DMA_PREP_INTERRUPT = (1 << 0),
101636bdeaaSDan Williams 	DMA_CTRL_ACK = (1 << 1),
102e1d181efSDan Williams 	DMA_COMPL_SKIP_SRC_UNMAP = (1 << 2),
103e1d181efSDan Williams 	DMA_COMPL_SKIP_DEST_UNMAP = (1 << 3),
1044f005dbeSMaciej Sosnowski 	DMA_COMPL_SRC_UNMAP_SINGLE = (1 << 4),
1054f005dbeSMaciej Sosnowski 	DMA_COMPL_DEST_UNMAP_SINGLE = (1 << 5),
106f9dd2134SDan Williams 	DMA_PREP_PQ_DISABLE_P = (1 << 6),
107f9dd2134SDan Williams 	DMA_PREP_PQ_DISABLE_Q = (1 << 7),
108f9dd2134SDan Williams 	DMA_PREP_CONTINUE = (1 << 8),
1090403e382SDan Williams 	DMA_PREP_FENCE = (1 << 9),
110d4c56f97SDan Williams };
111d4c56f97SDan Williams 
112d4c56f97SDan Williams /**
113c3635c78SLinus Walleij  * enum dma_ctrl_cmd - DMA operations that can optionally be exercised
114c3635c78SLinus Walleij  * on a running channel.
115c3635c78SLinus Walleij  * @DMA_TERMINATE_ALL: terminate all ongoing transfers
116c3635c78SLinus Walleij  * @DMA_PAUSE: pause ongoing transfers
117c3635c78SLinus Walleij  * @DMA_RESUME: resume paused transfer
118c156d0a5SLinus Walleij  * @DMA_SLAVE_CONFIG: this command is only implemented by DMA controllers
119c156d0a5SLinus Walleij  * that need to runtime reconfigure the slave channels (as opposed to passing
120c156d0a5SLinus Walleij  * configuration data in statically from the platform). An additional
121c156d0a5SLinus Walleij  * argument of struct dma_slave_config must be passed in with this
122c156d0a5SLinus Walleij  * command.
123c3635c78SLinus Walleij  */
124c3635c78SLinus Walleij enum dma_ctrl_cmd {
125c3635c78SLinus Walleij 	DMA_TERMINATE_ALL,
126c3635c78SLinus Walleij 	DMA_PAUSE,
127c3635c78SLinus Walleij 	DMA_RESUME,
128c156d0a5SLinus Walleij 	DMA_SLAVE_CONFIG,
129c3635c78SLinus Walleij };
130c3635c78SLinus Walleij 
131c3635c78SLinus Walleij /**
132ad283ea4SDan Williams  * enum sum_check_bits - bit position of pq_check_flags
133ad283ea4SDan Williams  */
134ad283ea4SDan Williams enum sum_check_bits {
135ad283ea4SDan Williams 	SUM_CHECK_P = 0,
136ad283ea4SDan Williams 	SUM_CHECK_Q = 1,
137ad283ea4SDan Williams };
138ad283ea4SDan Williams 
139ad283ea4SDan Williams /**
140ad283ea4SDan Williams  * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
141ad283ea4SDan Williams  * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
142ad283ea4SDan Williams  * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
143ad283ea4SDan Williams  */
144ad283ea4SDan Williams enum sum_check_flags {
145ad283ea4SDan Williams 	SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
146ad283ea4SDan Williams 	SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
147ad283ea4SDan Williams };
148ad283ea4SDan Williams 
149ad283ea4SDan Williams 
150ad283ea4SDan Williams /**
1517405f74bSDan Williams  * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
1527405f74bSDan Williams  * See linux/cpumask.h
1537405f74bSDan Williams  */
1547405f74bSDan Williams typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
1557405f74bSDan Williams 
1567405f74bSDan Williams /**
157c13c8260SChris Leech  * struct dma_chan_percpu - the per-CPU part of struct dma_chan
158c13c8260SChris Leech  * @memcpy_count: transaction counter
159c13c8260SChris Leech  * @bytes_transferred: byte counter
160c13c8260SChris Leech  */
161c13c8260SChris Leech 
162c13c8260SChris Leech struct dma_chan_percpu {
163c13c8260SChris Leech 	/* stats */
164c13c8260SChris Leech 	unsigned long memcpy_count;
165c13c8260SChris Leech 	unsigned long bytes_transferred;
166c13c8260SChris Leech };
167c13c8260SChris Leech 
168c13c8260SChris Leech /**
169c13c8260SChris Leech  * struct dma_chan - devices supply DMA channels, clients use them
170fe4ada2dSRandy Dunlap  * @device: ptr to the dma device who supplies this channel, always !%NULL
171c13c8260SChris Leech  * @cookie: last cookie value returned to client
172fe4ada2dSRandy Dunlap  * @chan_id: channel ID for sysfs
17341d5e59cSDan Williams  * @dev: class device for sysfs
174c13c8260SChris Leech  * @device_node: used to add this to the device chan list
175c13c8260SChris Leech  * @local: per-cpu pointer to a struct dma_chan_percpu
1767cc5bf9aSDan Williams  * @client-count: how many clients are using this channel
177bec08513SDan Williams  * @table_count: number of appearances in the mem-to-mem allocation table
178287d8592SDan Williams  * @private: private data for certain client-channel associations
179c13c8260SChris Leech  */
180c13c8260SChris Leech struct dma_chan {
181c13c8260SChris Leech 	struct dma_device *device;
182c13c8260SChris Leech 	dma_cookie_t cookie;
183c13c8260SChris Leech 
184c13c8260SChris Leech 	/* sysfs */
185c13c8260SChris Leech 	int chan_id;
18641d5e59cSDan Williams 	struct dma_chan_dev *dev;
187c13c8260SChris Leech 
188c13c8260SChris Leech 	struct list_head device_node;
189a29d8b8eSTejun Heo 	struct dma_chan_percpu __percpu *local;
1907cc5bf9aSDan Williams 	int client_count;
191bec08513SDan Williams 	int table_count;
192287d8592SDan Williams 	void *private;
193c13c8260SChris Leech };
194c13c8260SChris Leech 
19541d5e59cSDan Williams /**
19641d5e59cSDan Williams  * struct dma_chan_dev - relate sysfs device node to backing channel device
19741d5e59cSDan Williams  * @chan - driver channel device
19841d5e59cSDan Williams  * @device - sysfs device
199864498aaSDan Williams  * @dev_id - parent dma_device dev_id
200864498aaSDan Williams  * @idr_ref - reference count to gate release of dma_device dev_id
20141d5e59cSDan Williams  */
20241d5e59cSDan Williams struct dma_chan_dev {
20341d5e59cSDan Williams 	struct dma_chan *chan;
20441d5e59cSDan Williams 	struct device device;
205864498aaSDan Williams 	int dev_id;
206864498aaSDan Williams 	atomic_t *idr_ref;
20741d5e59cSDan Williams };
20841d5e59cSDan Williams 
209c156d0a5SLinus Walleij /**
210c156d0a5SLinus Walleij  * enum dma_slave_buswidth - defines bus with of the DMA slave
211c156d0a5SLinus Walleij  * device, source or target buses
212c156d0a5SLinus Walleij  */
213c156d0a5SLinus Walleij enum dma_slave_buswidth {
214c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
215c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
216c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
217c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
218c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
219c156d0a5SLinus Walleij };
220c156d0a5SLinus Walleij 
221c156d0a5SLinus Walleij /**
222c156d0a5SLinus Walleij  * struct dma_slave_config - dma slave channel runtime config
223c156d0a5SLinus Walleij  * @direction: whether the data shall go in or out on this slave
224c156d0a5SLinus Walleij  * channel, right now. DMA_TO_DEVICE and DMA_FROM_DEVICE are
225c156d0a5SLinus Walleij  * legal values, DMA_BIDIRECTIONAL is not acceptable since we
226c156d0a5SLinus Walleij  * need to differentiate source and target addresses.
227c156d0a5SLinus Walleij  * @src_addr: this is the physical address where DMA slave data
228c156d0a5SLinus Walleij  * should be read (RX), if the source is memory this argument is
229c156d0a5SLinus Walleij  * ignored.
230c156d0a5SLinus Walleij  * @dst_addr: this is the physical address where DMA slave data
231c156d0a5SLinus Walleij  * should be written (TX), if the source is memory this argument
232c156d0a5SLinus Walleij  * is ignored.
233c156d0a5SLinus Walleij  * @src_addr_width: this is the width in bytes of the source (RX)
234c156d0a5SLinus Walleij  * register where DMA data shall be read. If the source
235c156d0a5SLinus Walleij  * is memory this may be ignored depending on architecture.
236c156d0a5SLinus Walleij  * Legal values: 1, 2, 4, 8.
237c156d0a5SLinus Walleij  * @dst_addr_width: same as src_addr_width but for destination
238c156d0a5SLinus Walleij  * target (TX) mutatis mutandis.
239c156d0a5SLinus Walleij  * @src_maxburst: the maximum number of words (note: words, as in
240c156d0a5SLinus Walleij  * units of the src_addr_width member, not bytes) that can be sent
241c156d0a5SLinus Walleij  * in one burst to the device. Typically something like half the
242c156d0a5SLinus Walleij  * FIFO depth on I/O peripherals so you don't overflow it. This
243c156d0a5SLinus Walleij  * may or may not be applicable on memory sources.
244c156d0a5SLinus Walleij  * @dst_maxburst: same as src_maxburst but for destination target
245c156d0a5SLinus Walleij  * mutatis mutandis.
246c156d0a5SLinus Walleij  *
247c156d0a5SLinus Walleij  * This struct is passed in as configuration data to a DMA engine
248c156d0a5SLinus Walleij  * in order to set up a certain channel for DMA transport at runtime.
249c156d0a5SLinus Walleij  * The DMA device/engine has to provide support for an additional
250c156d0a5SLinus Walleij  * command in the channel config interface, DMA_SLAVE_CONFIG
251c156d0a5SLinus Walleij  * and this struct will then be passed in as an argument to the
252c156d0a5SLinus Walleij  * DMA engine device_control() function.
253c156d0a5SLinus Walleij  *
254c156d0a5SLinus Walleij  * The rationale for adding configuration information to this struct
255c156d0a5SLinus Walleij  * is as follows: if it is likely that most DMA slave controllers in
256c156d0a5SLinus Walleij  * the world will support the configuration option, then make it
257c156d0a5SLinus Walleij  * generic. If not: if it is fixed so that it be sent in static from
258c156d0a5SLinus Walleij  * the platform data, then prefer to do that. Else, if it is neither
259c156d0a5SLinus Walleij  * fixed at runtime, nor generic enough (such as bus mastership on
260c156d0a5SLinus Walleij  * some CPU family and whatnot) then create a custom slave config
261c156d0a5SLinus Walleij  * struct and pass that, then make this config a member of that
262c156d0a5SLinus Walleij  * struct, if applicable.
263c156d0a5SLinus Walleij  */
264c156d0a5SLinus Walleij struct dma_slave_config {
265c156d0a5SLinus Walleij 	enum dma_data_direction direction;
266c156d0a5SLinus Walleij 	dma_addr_t src_addr;
267c156d0a5SLinus Walleij 	dma_addr_t dst_addr;
268c156d0a5SLinus Walleij 	enum dma_slave_buswidth src_addr_width;
269c156d0a5SLinus Walleij 	enum dma_slave_buswidth dst_addr_width;
270c156d0a5SLinus Walleij 	u32 src_maxburst;
271c156d0a5SLinus Walleij 	u32 dst_maxburst;
272c156d0a5SLinus Walleij };
273c156d0a5SLinus Walleij 
27441d5e59cSDan Williams static inline const char *dma_chan_name(struct dma_chan *chan)
27541d5e59cSDan Williams {
27641d5e59cSDan Williams 	return dev_name(&chan->dev->device);
27741d5e59cSDan Williams }
278d379b01eSDan Williams 
279c13c8260SChris Leech void dma_chan_cleanup(struct kref *kref);
280c13c8260SChris Leech 
281c13c8260SChris Leech /**
28259b5ec21SDan Williams  * typedef dma_filter_fn - callback filter for dma_request_channel
28359b5ec21SDan Williams  * @chan: channel to be reviewed
28459b5ec21SDan Williams  * @filter_param: opaque parameter passed through dma_request_channel
28559b5ec21SDan Williams  *
28659b5ec21SDan Williams  * When this optional parameter is specified in a call to dma_request_channel a
28759b5ec21SDan Williams  * suitable channel is passed to this routine for further dispositioning before
28859b5ec21SDan Williams  * being returned.  Where 'suitable' indicates a non-busy channel that
2897dd60251SDan Williams  * satisfies the given capability mask.  It returns 'true' to indicate that the
2907dd60251SDan Williams  * channel is suitable.
29159b5ec21SDan Williams  */
2927dd60251SDan Williams typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
29359b5ec21SDan Williams 
2947405f74bSDan Williams typedef void (*dma_async_tx_callback)(void *dma_async_param);
2957405f74bSDan Williams /**
2967405f74bSDan Williams  * struct dma_async_tx_descriptor - async transaction descriptor
2977405f74bSDan Williams  * ---dma generic offload fields---
2987405f74bSDan Williams  * @cookie: tracking cookie for this transaction, set to -EBUSY if
2997405f74bSDan Williams  *	this tx is sitting on a dependency list
300636bdeaaSDan Williams  * @flags: flags to augment operation preparation, control completion, and
301636bdeaaSDan Williams  * 	communicate status
3027405f74bSDan Williams  * @phys: physical address of the descriptor
3037405f74bSDan Williams  * @chan: target channel for this operation
3047405f74bSDan Williams  * @tx_submit: set the prepared descriptor(s) to be executed by the engine
3057405f74bSDan Williams  * @callback: routine to call after this operation is complete
3067405f74bSDan Williams  * @callback_param: general parameter to pass to the callback routine
3077405f74bSDan Williams  * ---async_tx api specific fields---
30819242d72SDan Williams  * @next: at completion submit this descriptor
3097405f74bSDan Williams  * @parent: pointer to the next level up in the dependency chain
31019242d72SDan Williams  * @lock: protect the parent and next pointers
3117405f74bSDan Williams  */
3127405f74bSDan Williams struct dma_async_tx_descriptor {
3137405f74bSDan Williams 	dma_cookie_t cookie;
314636bdeaaSDan Williams 	enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
3157405f74bSDan Williams 	dma_addr_t phys;
3167405f74bSDan Williams 	struct dma_chan *chan;
3177405f74bSDan Williams 	dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
3187405f74bSDan Williams 	dma_async_tx_callback callback;
3197405f74bSDan Williams 	void *callback_param;
320caa20d97SDan Williams #ifndef CONFIG_ASYNC_TX_DISABLE_CHANNEL_SWITCH
32119242d72SDan Williams 	struct dma_async_tx_descriptor *next;
3227405f74bSDan Williams 	struct dma_async_tx_descriptor *parent;
3237405f74bSDan Williams 	spinlock_t lock;
324caa20d97SDan Williams #endif
3257405f74bSDan Williams };
3267405f74bSDan Williams 
327caa20d97SDan Williams #ifdef CONFIG_ASYNC_TX_DISABLE_CHANNEL_SWITCH
328caa20d97SDan Williams static inline void txd_lock(struct dma_async_tx_descriptor *txd)
329caa20d97SDan Williams {
330caa20d97SDan Williams }
331caa20d97SDan Williams static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
332caa20d97SDan Williams {
333caa20d97SDan Williams }
334caa20d97SDan Williams static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
335caa20d97SDan Williams {
336caa20d97SDan Williams 	BUG();
337caa20d97SDan Williams }
338caa20d97SDan Williams static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
339caa20d97SDan Williams {
340caa20d97SDan Williams }
341caa20d97SDan Williams static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
342caa20d97SDan Williams {
343caa20d97SDan Williams }
344caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
345caa20d97SDan Williams {
346caa20d97SDan Williams 	return NULL;
347caa20d97SDan Williams }
348caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
349caa20d97SDan Williams {
350caa20d97SDan Williams 	return NULL;
351caa20d97SDan Williams }
352caa20d97SDan Williams 
353caa20d97SDan Williams #else
354caa20d97SDan Williams static inline void txd_lock(struct dma_async_tx_descriptor *txd)
355caa20d97SDan Williams {
356caa20d97SDan Williams 	spin_lock_bh(&txd->lock);
357caa20d97SDan Williams }
358caa20d97SDan Williams static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
359caa20d97SDan Williams {
360caa20d97SDan Williams 	spin_unlock_bh(&txd->lock);
361caa20d97SDan Williams }
362caa20d97SDan Williams static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
363caa20d97SDan Williams {
364caa20d97SDan Williams 	txd->next = next;
365caa20d97SDan Williams 	next->parent = txd;
366caa20d97SDan Williams }
367caa20d97SDan Williams static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
368caa20d97SDan Williams {
369caa20d97SDan Williams 	txd->parent = NULL;
370caa20d97SDan Williams }
371caa20d97SDan Williams static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
372caa20d97SDan Williams {
373caa20d97SDan Williams 	txd->next = NULL;
374caa20d97SDan Williams }
375caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
376caa20d97SDan Williams {
377caa20d97SDan Williams 	return txd->parent;
378caa20d97SDan Williams }
379caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
380caa20d97SDan Williams {
381caa20d97SDan Williams 	return txd->next;
382caa20d97SDan Williams }
383caa20d97SDan Williams #endif
384caa20d97SDan Williams 
385c13c8260SChris Leech /**
38607934481SLinus Walleij  * struct dma_tx_state - filled in to report the status of
38707934481SLinus Walleij  * a transfer.
38807934481SLinus Walleij  * @last: last completed DMA cookie
38907934481SLinus Walleij  * @used: last issued DMA cookie (i.e. the one in progress)
39007934481SLinus Walleij  * @residue: the remaining number of bytes left to transmit
39107934481SLinus Walleij  *	on the selected transfer for states DMA_IN_PROGRESS and
39207934481SLinus Walleij  *	DMA_PAUSED if this is implemented in the driver, else 0
39307934481SLinus Walleij  */
39407934481SLinus Walleij struct dma_tx_state {
39507934481SLinus Walleij 	dma_cookie_t last;
39607934481SLinus Walleij 	dma_cookie_t used;
39707934481SLinus Walleij 	u32 residue;
39807934481SLinus Walleij };
39907934481SLinus Walleij 
40007934481SLinus Walleij /**
401c13c8260SChris Leech  * struct dma_device - info on the entity supplying DMA services
402c13c8260SChris Leech  * @chancnt: how many DMA channels are supported
4030f571515SAtsushi Nemoto  * @privatecnt: how many DMA channels are requested by dma_request_channel
404c13c8260SChris Leech  * @channels: the list of struct dma_chan
405c13c8260SChris Leech  * @global_node: list_head for global dma_device_list
4067405f74bSDan Williams  * @cap_mask: one or more dma_capability flags
4077405f74bSDan Williams  * @max_xor: maximum number of xor sources, 0 if no capability
408b2f46fd8SDan Williams  * @max_pq: maximum number of PQ sources and PQ-continue capability
40983544ae9SDan Williams  * @copy_align: alignment shift for memcpy operations
41083544ae9SDan Williams  * @xor_align: alignment shift for xor operations
41183544ae9SDan Williams  * @pq_align: alignment shift for pq operations
41283544ae9SDan Williams  * @fill_align: alignment shift for memset operations
413fe4ada2dSRandy Dunlap  * @dev_id: unique device ID
4147405f74bSDan Williams  * @dev: struct device reference for dma mapping api
415fe4ada2dSRandy Dunlap  * @device_alloc_chan_resources: allocate resources and return the
416fe4ada2dSRandy Dunlap  *	number of allocated descriptors
417fe4ada2dSRandy Dunlap  * @device_free_chan_resources: release DMA channel's resources
4187405f74bSDan Williams  * @device_prep_dma_memcpy: prepares a memcpy operation
4197405f74bSDan Williams  * @device_prep_dma_xor: prepares a xor operation
420099f53cbSDan Williams  * @device_prep_dma_xor_val: prepares a xor validation operation
421b2f46fd8SDan Williams  * @device_prep_dma_pq: prepares a pq operation
422b2f46fd8SDan Williams  * @device_prep_dma_pq_val: prepares a pqzero_sum operation
4237405f74bSDan Williams  * @device_prep_dma_memset: prepares a memset operation
4247405f74bSDan Williams  * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
425dc0ee643SHaavard Skinnemoen  * @device_prep_slave_sg: prepares a slave dma operation
426c3635c78SLinus Walleij  * @device_control: manipulate all pending operations on a channel, returns
427c3635c78SLinus Walleij  *	zero or error code
42807934481SLinus Walleij  * @device_tx_status: poll for transaction completion, the optional
42907934481SLinus Walleij  *	txstate parameter can be supplied with a pointer to get a
43007934481SLinus Walleij  *	struct with auxilary transfer status information, otherwise the call
43107934481SLinus Walleij  *	will just return a simple status code
4327405f74bSDan Williams  * @device_issue_pending: push pending transactions to hardware
433c13c8260SChris Leech  */
434c13c8260SChris Leech struct dma_device {
435c13c8260SChris Leech 
436c13c8260SChris Leech 	unsigned int chancnt;
4370f571515SAtsushi Nemoto 	unsigned int privatecnt;
438c13c8260SChris Leech 	struct list_head channels;
439c13c8260SChris Leech 	struct list_head global_node;
4407405f74bSDan Williams 	dma_cap_mask_t  cap_mask;
441b2f46fd8SDan Williams 	unsigned short max_xor;
442b2f46fd8SDan Williams 	unsigned short max_pq;
44383544ae9SDan Williams 	u8 copy_align;
44483544ae9SDan Williams 	u8 xor_align;
44583544ae9SDan Williams 	u8 pq_align;
44683544ae9SDan Williams 	u8 fill_align;
447b2f46fd8SDan Williams 	#define DMA_HAS_PQ_CONTINUE (1 << 15)
448c13c8260SChris Leech 
449c13c8260SChris Leech 	int dev_id;
4507405f74bSDan Williams 	struct device *dev;
451c13c8260SChris Leech 
452aa1e6f1aSDan Williams 	int (*device_alloc_chan_resources)(struct dma_chan *chan);
453c13c8260SChris Leech 	void (*device_free_chan_resources)(struct dma_chan *chan);
4547405f74bSDan Williams 
4557405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
4560036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
457d4c56f97SDan Williams 		size_t len, unsigned long flags);
4587405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
4590036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
460d4c56f97SDan Williams 		unsigned int src_cnt, size_t len, unsigned long flags);
461099f53cbSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
4620036731cSDan Williams 		struct dma_chan *chan, dma_addr_t *src,	unsigned int src_cnt,
463ad283ea4SDan Williams 		size_t len, enum sum_check_flags *result, unsigned long flags);
464b2f46fd8SDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
465b2f46fd8SDan Williams 		struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
466b2f46fd8SDan Williams 		unsigned int src_cnt, const unsigned char *scf,
467b2f46fd8SDan Williams 		size_t len, unsigned long flags);
468b2f46fd8SDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
469b2f46fd8SDan Williams 		struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
470b2f46fd8SDan Williams 		unsigned int src_cnt, const unsigned char *scf, size_t len,
471b2f46fd8SDan Williams 		enum sum_check_flags *pqres, unsigned long flags);
4727405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
4730036731cSDan Williams 		struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
474d4c56f97SDan Williams 		unsigned long flags);
4757405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
476636bdeaaSDan Williams 		struct dma_chan *chan, unsigned long flags);
477*a86ee03cSIra Snyder 	struct dma_async_tx_descriptor *(*device_prep_dma_sg)(
478*a86ee03cSIra Snyder 		struct dma_chan *chan,
479*a86ee03cSIra Snyder 		struct scatterlist *dst_sg, unsigned int dst_nents,
480*a86ee03cSIra Snyder 		struct scatterlist *src_sg, unsigned int src_nents,
481*a86ee03cSIra Snyder 		unsigned long flags);
4827405f74bSDan Williams 
483dc0ee643SHaavard Skinnemoen 	struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
484dc0ee643SHaavard Skinnemoen 		struct dma_chan *chan, struct scatterlist *sgl,
485dc0ee643SHaavard Skinnemoen 		unsigned int sg_len, enum dma_data_direction direction,
486dc0ee643SHaavard Skinnemoen 		unsigned long flags);
48705827630SLinus Walleij 	int (*device_control)(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
48805827630SLinus Walleij 		unsigned long arg);
489dc0ee643SHaavard Skinnemoen 
49007934481SLinus Walleij 	enum dma_status (*device_tx_status)(struct dma_chan *chan,
49107934481SLinus Walleij 					    dma_cookie_t cookie,
49207934481SLinus Walleij 					    struct dma_tx_state *txstate);
4937405f74bSDan Williams 	void (*device_issue_pending)(struct dma_chan *chan);
494c13c8260SChris Leech };
495c13c8260SChris Leech 
49683544ae9SDan Williams static inline bool dmaengine_check_align(u8 align, size_t off1, size_t off2, size_t len)
49783544ae9SDan Williams {
49883544ae9SDan Williams 	size_t mask;
49983544ae9SDan Williams 
50083544ae9SDan Williams 	if (!align)
50183544ae9SDan Williams 		return true;
50283544ae9SDan Williams 	mask = (1 << align) - 1;
50383544ae9SDan Williams 	if (mask & (off1 | off2 | len))
50483544ae9SDan Williams 		return false;
50583544ae9SDan Williams 	return true;
50683544ae9SDan Williams }
50783544ae9SDan Williams 
50883544ae9SDan Williams static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
50983544ae9SDan Williams 				       size_t off2, size_t len)
51083544ae9SDan Williams {
51183544ae9SDan Williams 	return dmaengine_check_align(dev->copy_align, off1, off2, len);
51283544ae9SDan Williams }
51383544ae9SDan Williams 
51483544ae9SDan Williams static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
51583544ae9SDan Williams 				      size_t off2, size_t len)
51683544ae9SDan Williams {
51783544ae9SDan Williams 	return dmaengine_check_align(dev->xor_align, off1, off2, len);
51883544ae9SDan Williams }
51983544ae9SDan Williams 
52083544ae9SDan Williams static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
52183544ae9SDan Williams 				     size_t off2, size_t len)
52283544ae9SDan Williams {
52383544ae9SDan Williams 	return dmaengine_check_align(dev->pq_align, off1, off2, len);
52483544ae9SDan Williams }
52583544ae9SDan Williams 
52683544ae9SDan Williams static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
52783544ae9SDan Williams 				       size_t off2, size_t len)
52883544ae9SDan Williams {
52983544ae9SDan Williams 	return dmaengine_check_align(dev->fill_align, off1, off2, len);
53083544ae9SDan Williams }
53183544ae9SDan Williams 
532b2f46fd8SDan Williams static inline void
533b2f46fd8SDan Williams dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
534b2f46fd8SDan Williams {
535b2f46fd8SDan Williams 	dma->max_pq = maxpq;
536b2f46fd8SDan Williams 	if (has_pq_continue)
537b2f46fd8SDan Williams 		dma->max_pq |= DMA_HAS_PQ_CONTINUE;
538b2f46fd8SDan Williams }
539b2f46fd8SDan Williams 
540b2f46fd8SDan Williams static inline bool dmaf_continue(enum dma_ctrl_flags flags)
541b2f46fd8SDan Williams {
542b2f46fd8SDan Williams 	return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
543b2f46fd8SDan Williams }
544b2f46fd8SDan Williams 
545b2f46fd8SDan Williams static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
546b2f46fd8SDan Williams {
547b2f46fd8SDan Williams 	enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
548b2f46fd8SDan Williams 
549b2f46fd8SDan Williams 	return (flags & mask) == mask;
550b2f46fd8SDan Williams }
551b2f46fd8SDan Williams 
552b2f46fd8SDan Williams static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
553b2f46fd8SDan Williams {
554b2f46fd8SDan Williams 	return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
555b2f46fd8SDan Williams }
556b2f46fd8SDan Williams 
557d3f3cf85SMathieu Lacage static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
558b2f46fd8SDan Williams {
559b2f46fd8SDan Williams 	return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
560b2f46fd8SDan Williams }
561b2f46fd8SDan Williams 
562b2f46fd8SDan Williams /* dma_maxpq - reduce maxpq in the face of continued operations
563b2f46fd8SDan Williams  * @dma - dma device with PQ capability
564b2f46fd8SDan Williams  * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
565b2f46fd8SDan Williams  *
566b2f46fd8SDan Williams  * When an engine does not support native continuation we need 3 extra
567b2f46fd8SDan Williams  * source slots to reuse P and Q with the following coefficients:
568b2f46fd8SDan Williams  * 1/ {00} * P : remove P from Q', but use it as a source for P'
569b2f46fd8SDan Williams  * 2/ {01} * Q : use Q to continue Q' calculation
570b2f46fd8SDan Williams  * 3/ {00} * Q : subtract Q from P' to cancel (2)
571b2f46fd8SDan Williams  *
572b2f46fd8SDan Williams  * In the case where P is disabled we only need 1 extra source:
573b2f46fd8SDan Williams  * 1/ {01} * Q : use Q to continue Q' calculation
574b2f46fd8SDan Williams  */
575b2f46fd8SDan Williams static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
576b2f46fd8SDan Williams {
577b2f46fd8SDan Williams 	if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
578b2f46fd8SDan Williams 		return dma_dev_to_maxpq(dma);
579b2f46fd8SDan Williams 	else if (dmaf_p_disabled_continue(flags))
580b2f46fd8SDan Williams 		return dma_dev_to_maxpq(dma) - 1;
581b2f46fd8SDan Williams 	else if (dmaf_continue(flags))
582b2f46fd8SDan Williams 		return dma_dev_to_maxpq(dma) - 3;
583b2f46fd8SDan Williams 	BUG();
584b2f46fd8SDan Williams }
585b2f46fd8SDan Williams 
586c13c8260SChris Leech /* --- public DMA engine API --- */
587c13c8260SChris Leech 
588649274d9SDan Williams #ifdef CONFIG_DMA_ENGINE
589209b84a8SDan Williams void dmaengine_get(void);
590209b84a8SDan Williams void dmaengine_put(void);
591649274d9SDan Williams #else
592649274d9SDan Williams static inline void dmaengine_get(void)
593649274d9SDan Williams {
594649274d9SDan Williams }
595649274d9SDan Williams static inline void dmaengine_put(void)
596649274d9SDan Williams {
597649274d9SDan Williams }
598649274d9SDan Williams #endif
599649274d9SDan Williams 
600b4bd07c2SDavid S. Miller #ifdef CONFIG_NET_DMA
601b4bd07c2SDavid S. Miller #define net_dmaengine_get()	dmaengine_get()
602b4bd07c2SDavid S. Miller #define net_dmaengine_put()	dmaengine_put()
603b4bd07c2SDavid S. Miller #else
604b4bd07c2SDavid S. Miller static inline void net_dmaengine_get(void)
605b4bd07c2SDavid S. Miller {
606b4bd07c2SDavid S. Miller }
607b4bd07c2SDavid S. Miller static inline void net_dmaengine_put(void)
608b4bd07c2SDavid S. Miller {
609b4bd07c2SDavid S. Miller }
610b4bd07c2SDavid S. Miller #endif
611b4bd07c2SDavid S. Miller 
612729b5d1bSDan Williams #ifdef CONFIG_ASYNC_TX_DMA
613729b5d1bSDan Williams #define async_dmaengine_get()	dmaengine_get()
614729b5d1bSDan Williams #define async_dmaengine_put()	dmaengine_put()
615138f4c35SDan Williams #ifdef CONFIG_ASYNC_TX_DISABLE_CHANNEL_SWITCH
616138f4c35SDan Williams #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
617138f4c35SDan Williams #else
618729b5d1bSDan Williams #define async_dma_find_channel(type) dma_find_channel(type)
619138f4c35SDan Williams #endif /* CONFIG_ASYNC_TX_DISABLE_CHANNEL_SWITCH */
620729b5d1bSDan Williams #else
621729b5d1bSDan Williams static inline void async_dmaengine_get(void)
622729b5d1bSDan Williams {
623729b5d1bSDan Williams }
624729b5d1bSDan Williams static inline void async_dmaengine_put(void)
625729b5d1bSDan Williams {
626729b5d1bSDan Williams }
627729b5d1bSDan Williams static inline struct dma_chan *
628729b5d1bSDan Williams async_dma_find_channel(enum dma_transaction_type type)
629729b5d1bSDan Williams {
630729b5d1bSDan Williams 	return NULL;
631729b5d1bSDan Williams }
632138f4c35SDan Williams #endif /* CONFIG_ASYNC_TX_DMA */
633729b5d1bSDan Williams 
6347405f74bSDan Williams dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
6357405f74bSDan Williams 	void *dest, void *src, size_t len);
6367405f74bSDan Williams dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
6377405f74bSDan Williams 	struct page *page, unsigned int offset, void *kdata, size_t len);
6387405f74bSDan Williams dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
639c13c8260SChris Leech 	struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
6407405f74bSDan Williams 	unsigned int src_off, size_t len);
6417405f74bSDan Williams void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
6427405f74bSDan Williams 	struct dma_chan *chan);
643c13c8260SChris Leech 
6440839875eSDan Williams static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
6457405f74bSDan Williams {
646636bdeaaSDan Williams 	tx->flags |= DMA_CTRL_ACK;
647636bdeaaSDan Williams }
648636bdeaaSDan Williams 
649ef560682SGuennadi Liakhovetski static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
650ef560682SGuennadi Liakhovetski {
651ef560682SGuennadi Liakhovetski 	tx->flags &= ~DMA_CTRL_ACK;
652ef560682SGuennadi Liakhovetski }
653ef560682SGuennadi Liakhovetski 
6540839875eSDan Williams static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
655636bdeaaSDan Williams {
6560839875eSDan Williams 	return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
657c13c8260SChris Leech }
658c13c8260SChris Leech 
6597405f74bSDan Williams #define first_dma_cap(mask) __first_dma_cap(&(mask))
6607405f74bSDan Williams static inline int __first_dma_cap(const dma_cap_mask_t *srcp)
6617405f74bSDan Williams {
6627405f74bSDan Williams 	return min_t(int, DMA_TX_TYPE_END,
6637405f74bSDan Williams 		find_first_bit(srcp->bits, DMA_TX_TYPE_END));
6647405f74bSDan Williams }
6657405f74bSDan Williams 
6667405f74bSDan Williams #define next_dma_cap(n, mask) __next_dma_cap((n), &(mask))
6677405f74bSDan Williams static inline int __next_dma_cap(int n, const dma_cap_mask_t *srcp)
6687405f74bSDan Williams {
6697405f74bSDan Williams 	return min_t(int, DMA_TX_TYPE_END,
6707405f74bSDan Williams 		find_next_bit(srcp->bits, DMA_TX_TYPE_END, n+1));
6717405f74bSDan Williams }
6727405f74bSDan Williams 
6737405f74bSDan Williams #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
6747405f74bSDan Williams static inline void
6757405f74bSDan Williams __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
6767405f74bSDan Williams {
6777405f74bSDan Williams 	set_bit(tx_type, dstp->bits);
6787405f74bSDan Williams }
6797405f74bSDan Williams 
6800f571515SAtsushi Nemoto #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
6810f571515SAtsushi Nemoto static inline void
6820f571515SAtsushi Nemoto __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
6830f571515SAtsushi Nemoto {
6840f571515SAtsushi Nemoto 	clear_bit(tx_type, dstp->bits);
6850f571515SAtsushi Nemoto }
6860f571515SAtsushi Nemoto 
68733df8ca0SDan Williams #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
68833df8ca0SDan Williams static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
68933df8ca0SDan Williams {
69033df8ca0SDan Williams 	bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
69133df8ca0SDan Williams }
69233df8ca0SDan Williams 
6937405f74bSDan Williams #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
6947405f74bSDan Williams static inline int
6957405f74bSDan Williams __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
6967405f74bSDan Williams {
6977405f74bSDan Williams 	return test_bit(tx_type, srcp->bits);
6987405f74bSDan Williams }
6997405f74bSDan Williams 
7007405f74bSDan Williams #define for_each_dma_cap_mask(cap, mask) \
7017405f74bSDan Williams 	for ((cap) = first_dma_cap(mask);	\
7027405f74bSDan Williams 		(cap) < DMA_TX_TYPE_END;	\
7037405f74bSDan Williams 		(cap) = next_dma_cap((cap), (mask)))
7047405f74bSDan Williams 
705c13c8260SChris Leech /**
7067405f74bSDan Williams  * dma_async_issue_pending - flush pending transactions to HW
707fe4ada2dSRandy Dunlap  * @chan: target DMA channel
708c13c8260SChris Leech  *
709c13c8260SChris Leech  * This allows drivers to push copies to HW in batches,
710c13c8260SChris Leech  * reducing MMIO writes where possible.
711c13c8260SChris Leech  */
7127405f74bSDan Williams static inline void dma_async_issue_pending(struct dma_chan *chan)
713c13c8260SChris Leech {
714ec8670f1SDan Williams 	chan->device->device_issue_pending(chan);
715c13c8260SChris Leech }
716c13c8260SChris Leech 
7177405f74bSDan Williams #define dma_async_memcpy_issue_pending(chan) dma_async_issue_pending(chan)
7187405f74bSDan Williams 
719c13c8260SChris Leech /**
7207405f74bSDan Williams  * dma_async_is_tx_complete - poll for transaction completion
721c13c8260SChris Leech  * @chan: DMA channel
722c13c8260SChris Leech  * @cookie: transaction identifier to check status of
723c13c8260SChris Leech  * @last: returns last completed cookie, can be NULL
724c13c8260SChris Leech  * @used: returns last issued cookie, can be NULL
725c13c8260SChris Leech  *
726c13c8260SChris Leech  * If @last and @used are passed in, upon return they reflect the driver
727c13c8260SChris Leech  * internal state and can be used with dma_async_is_complete() to check
728c13c8260SChris Leech  * the status of multiple cookies without re-checking hardware state.
729c13c8260SChris Leech  */
7307405f74bSDan Williams static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
731c13c8260SChris Leech 	dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
732c13c8260SChris Leech {
73307934481SLinus Walleij 	struct dma_tx_state state;
73407934481SLinus Walleij 	enum dma_status status;
73507934481SLinus Walleij 
73607934481SLinus Walleij 	status = chan->device->device_tx_status(chan, cookie, &state);
73707934481SLinus Walleij 	if (last)
73807934481SLinus Walleij 		*last = state.last;
73907934481SLinus Walleij 	if (used)
74007934481SLinus Walleij 		*used = state.used;
74107934481SLinus Walleij 	return status;
742c13c8260SChris Leech }
743c13c8260SChris Leech 
7447405f74bSDan Williams #define dma_async_memcpy_complete(chan, cookie, last, used)\
7457405f74bSDan Williams 	dma_async_is_tx_complete(chan, cookie, last, used)
7467405f74bSDan Williams 
747c13c8260SChris Leech /**
748c13c8260SChris Leech  * dma_async_is_complete - test a cookie against chan state
749c13c8260SChris Leech  * @cookie: transaction identifier to test status of
750c13c8260SChris Leech  * @last_complete: last know completed transaction
751c13c8260SChris Leech  * @last_used: last cookie value handed out
752c13c8260SChris Leech  *
753c13c8260SChris Leech  * dma_async_is_complete() is used in dma_async_memcpy_complete()
7548a5703f8SSebastian Siewior  * the test logic is separated for lightweight testing of multiple cookies
755c13c8260SChris Leech  */
756c13c8260SChris Leech static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
757c13c8260SChris Leech 			dma_cookie_t last_complete, dma_cookie_t last_used)
758c13c8260SChris Leech {
759c13c8260SChris Leech 	if (last_complete <= last_used) {
760c13c8260SChris Leech 		if ((cookie <= last_complete) || (cookie > last_used))
761c13c8260SChris Leech 			return DMA_SUCCESS;
762c13c8260SChris Leech 	} else {
763c13c8260SChris Leech 		if ((cookie <= last_complete) && (cookie > last_used))
764c13c8260SChris Leech 			return DMA_SUCCESS;
765c13c8260SChris Leech 	}
766c13c8260SChris Leech 	return DMA_IN_PROGRESS;
767c13c8260SChris Leech }
768c13c8260SChris Leech 
769bca34692SDan Williams static inline void
770bca34692SDan Williams dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
771bca34692SDan Williams {
772bca34692SDan Williams 	if (st) {
773bca34692SDan Williams 		st->last = last;
774bca34692SDan Williams 		st->used = used;
775bca34692SDan Williams 		st->residue = residue;
776bca34692SDan Williams 	}
777bca34692SDan Williams }
778bca34692SDan Williams 
7797405f74bSDan Williams enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
78007f2211eSDan Williams #ifdef CONFIG_DMA_ENGINE
78107f2211eSDan Williams enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
782c50331e8SDan Williams void dma_issue_pending_all(void);
78307f2211eSDan Williams #else
78407f2211eSDan Williams static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
78507f2211eSDan Williams {
78607f2211eSDan Williams 	return DMA_SUCCESS;
78707f2211eSDan Williams }
788c50331e8SDan Williams static inline void dma_issue_pending_all(void)
789c50331e8SDan Williams {
790c50331e8SDan Williams 	do { } while (0);
791c50331e8SDan Williams }
79207f2211eSDan Williams #endif
793c13c8260SChris Leech 
794c13c8260SChris Leech /* --- DMA device --- */
795c13c8260SChris Leech 
796c13c8260SChris Leech int dma_async_device_register(struct dma_device *device);
797c13c8260SChris Leech void dma_async_device_unregister(struct dma_device *device);
79807f2211eSDan Williams void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
799bec08513SDan Williams struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
80059b5ec21SDan Williams #define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
80159b5ec21SDan Williams struct dma_chan *__dma_request_channel(dma_cap_mask_t *mask, dma_filter_fn fn, void *fn_param);
80259b5ec21SDan Williams void dma_release_channel(struct dma_chan *chan);
803c13c8260SChris Leech 
804de5506e1SChris Leech /* --- Helper iov-locking functions --- */
805de5506e1SChris Leech 
806de5506e1SChris Leech struct dma_page_list {
807b2ddb901SAl Viro 	char __user *base_address;
808de5506e1SChris Leech 	int nr_pages;
809de5506e1SChris Leech 	struct page **pages;
810de5506e1SChris Leech };
811de5506e1SChris Leech 
812de5506e1SChris Leech struct dma_pinned_list {
813de5506e1SChris Leech 	int nr_iovecs;
814de5506e1SChris Leech 	struct dma_page_list page_list[0];
815de5506e1SChris Leech };
816de5506e1SChris Leech 
817de5506e1SChris Leech struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
818de5506e1SChris Leech void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
819de5506e1SChris Leech 
820de5506e1SChris Leech dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
821de5506e1SChris Leech 	struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
822de5506e1SChris Leech dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
823de5506e1SChris Leech 	struct dma_pinned_list *pinned_list, struct page *page,
824de5506e1SChris Leech 	unsigned int offset, size_t len);
825de5506e1SChris Leech 
826c13c8260SChris Leech #endif /* DMAENGINE_H */
827