xref: /linux-6.15/include/linux/dmaengine.h (revision fc44ff0a)
19ab65affSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2c13c8260SChris Leech /*
3c13c8260SChris Leech  * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
4c13c8260SChris Leech  */
5d2ebfb33SRussell King - ARM Linux #ifndef LINUX_DMAENGINE_H
6d2ebfb33SRussell King - ARM Linux #define LINUX_DMAENGINE_H
71c0f16e5SDavid Woodhouse 
8c13c8260SChris Leech #include <linux/device.h>
90ad7c000SStephen Warren #include <linux/err.h>
10c13c8260SChris Leech #include <linux/uio.h>
11187f1882SPaul Gortmaker #include <linux/bug.h>
1290b44f8fSVinod Koul #include <linux/scatterlist.h>
13a8efa9d6SPaul Gortmaker #include <linux/bitmap.h>
14dcc043dcSViresh Kumar #include <linux/types.h>
15a8efa9d6SPaul Gortmaker #include <asm/page.h>
16b7f080cfSAlexey Dobriyan 
17c13c8260SChris Leech /**
18fe4ada2dSRandy Dunlap  * typedef dma_cookie_t - an opaque DMA cookie
19c13c8260SChris Leech  *
20c13c8260SChris Leech  * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
21c13c8260SChris Leech  */
22c13c8260SChris Leech typedef s32 dma_cookie_t;
2376bd061fSSteven J. Magnani #define DMA_MIN_COOKIE	1
24c13c8260SChris Leech 
2571ea1483SDan Carpenter static inline int dma_submit_error(dma_cookie_t cookie)
2671ea1483SDan Carpenter {
2771ea1483SDan Carpenter 	return cookie < 0 ? cookie : 0;
2871ea1483SDan Carpenter }
29c13c8260SChris Leech 
30c13c8260SChris Leech /**
31c13c8260SChris Leech  * enum dma_status - DMA transaction status
32adfedd9aSVinod Koul  * @DMA_COMPLETE: transaction completed
33c13c8260SChris Leech  * @DMA_IN_PROGRESS: transaction not yet processed
3407934481SLinus Walleij  * @DMA_PAUSED: transaction is paused
35c13c8260SChris Leech  * @DMA_ERROR: transaction failed
36c13c8260SChris Leech  */
37c13c8260SChris Leech enum dma_status {
387db5f727SVinod Koul 	DMA_COMPLETE,
39c13c8260SChris Leech 	DMA_IN_PROGRESS,
4007934481SLinus Walleij 	DMA_PAUSED,
41c13c8260SChris Leech 	DMA_ERROR,
4247ec7f09SDave Jiang 	DMA_OUT_OF_ORDER,
43c13c8260SChris Leech };
44c13c8260SChris Leech 
45c13c8260SChris Leech /**
467405f74bSDan Williams  * enum dma_transaction_type - DMA transaction types/indexes
47138f4c35SDan Williams  *
48138f4c35SDan Williams  * Note: The DMA_ASYNC_TX capability is not to be set by drivers.  It is
49138f4c35SDan Williams  * automatically set as dma devices are registered.
507405f74bSDan Williams  */
517405f74bSDan Williams enum dma_transaction_type {
527405f74bSDan Williams 	DMA_MEMCPY,
533218910fSAdrian Larumbe 	DMA_MEMCPY_SG,
547405f74bSDan Williams 	DMA_XOR,
55b2f46fd8SDan Williams 	DMA_PQ,
56099f53cbSDan Williams 	DMA_XOR_VAL,
57099f53cbSDan Williams 	DMA_PQ_VAL,
584983a501SMaxime Ripard 	DMA_MEMSET,
5950c7cd2bSMaxime Ripard 	DMA_MEMSET_SG,
607405f74bSDan Williams 	DMA_INTERRUPT,
6159b5ec21SDan Williams 	DMA_PRIVATE,
62138f4c35SDan Williams 	DMA_ASYNC_TX,
63dc0ee643SHaavard Skinnemoen 	DMA_SLAVE,
64782bc950SSascha Hauer 	DMA_CYCLIC,
65b14dab79SJassi Brar 	DMA_INTERLEAVE,
6647ec7f09SDave Jiang 	DMA_COMPLETION_NO_ORDER,
679c8ebd8bSLaurent Pinchart 	DMA_REPEAT,
689c8ebd8bSLaurent Pinchart 	DMA_LOAD_EOT,
697405f74bSDan Williams /* last transaction type for creation of the capabilities mask */
70b14dab79SJassi Brar 	DMA_TX_TYPE_END,
71b14dab79SJassi Brar };
72dc0ee643SHaavard Skinnemoen 
7349920bc6SVinod Koul /**
7449920bc6SVinod Koul  * enum dma_transfer_direction - dma transfer mode and direction indicator
7549920bc6SVinod Koul  * @DMA_MEM_TO_MEM: Async/Memcpy mode
7649920bc6SVinod Koul  * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device
7749920bc6SVinod Koul  * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory
7849920bc6SVinod Koul  * @DMA_DEV_TO_DEV: Slave mode & From Device to Device
7949920bc6SVinod Koul  */
8049920bc6SVinod Koul enum dma_transfer_direction {
8149920bc6SVinod Koul 	DMA_MEM_TO_MEM,
8249920bc6SVinod Koul 	DMA_MEM_TO_DEV,
8349920bc6SVinod Koul 	DMA_DEV_TO_MEM,
8449920bc6SVinod Koul 	DMA_DEV_TO_DEV,
8562268ce9SShawn Guo 	DMA_TRANS_NONE,
8649920bc6SVinod Koul };
877405f74bSDan Williams 
887405f74bSDan Williams /**
89b14dab79SJassi Brar  * Interleaved Transfer Request
90b14dab79SJassi Brar  * ----------------------------
9120d60f63SMaciej Grochowski  * A chunk is collection of contiguous bytes to be transferred.
92b14dab79SJassi Brar  * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG).
93b14dab79SJassi Brar  * ICGs may or may not change between chunks.
94b14dab79SJassi Brar  * A FRAME is the smallest series of contiguous {chunk,icg} pairs,
95b14dab79SJassi Brar  *  that when repeated an integral number of times, specifies the transfer.
96b14dab79SJassi Brar  * A transfer template is specification of a Frame, the number of times
97b14dab79SJassi Brar  *  it is to be repeated and other per-transfer attributes.
98b14dab79SJassi Brar  *
99b14dab79SJassi Brar  * Practically, a client driver would have ready a template for each
100b14dab79SJassi Brar  *  type of transfer it is going to need during its lifetime and
101b14dab79SJassi Brar  *  set only 'src_start' and 'dst_start' before submitting the requests.
102b14dab79SJassi Brar  *
103b14dab79SJassi Brar  *
104b14dab79SJassi Brar  *  |      Frame-1        |       Frame-2       | ~ |       Frame-'numf'  |
105b14dab79SJassi Brar  *  |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...|
106b14dab79SJassi Brar  *
107b14dab79SJassi Brar  *    ==  Chunk size
108b14dab79SJassi Brar  *    ... ICG
109b14dab79SJassi Brar  */
110b14dab79SJassi Brar 
111b14dab79SJassi Brar /**
112b14dab79SJassi Brar  * struct data_chunk - Element of scatter-gather list that makes a frame.
113b14dab79SJassi Brar  * @size: Number of bytes to read from source.
114b14dab79SJassi Brar  *	  size_dst := fn(op, size_src), so doesn't mean much for destination.
115b14dab79SJassi Brar  * @icg: Number of bytes to jump after last src/dst address of this
116b14dab79SJassi Brar  *	 chunk and before first src/dst address for next chunk.
117b14dab79SJassi Brar  *	 Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false.
118b14dab79SJassi Brar  *	 Ignored for src(assumed 0), if src_inc is true and src_sgl is false.
119e1031dc1SMaxime Ripard  * @dst_icg: Number of bytes to jump after last dst address of this
120e1031dc1SMaxime Ripard  *	 chunk and before the first dst address for next chunk.
121e1031dc1SMaxime Ripard  *	 Ignored if dst_inc is true and dst_sgl is false.
122e1031dc1SMaxime Ripard  * @src_icg: Number of bytes to jump after last src address of this
123e1031dc1SMaxime Ripard  *	 chunk and before the first src address for next chunk.
124e1031dc1SMaxime Ripard  *	 Ignored if src_inc is true and src_sgl is false.
125b14dab79SJassi Brar  */
126b14dab79SJassi Brar struct data_chunk {
127b14dab79SJassi Brar 	size_t size;
128b14dab79SJassi Brar 	size_t icg;
129e1031dc1SMaxime Ripard 	size_t dst_icg;
130e1031dc1SMaxime Ripard 	size_t src_icg;
131b14dab79SJassi Brar };
132b14dab79SJassi Brar 
133b14dab79SJassi Brar /**
134b14dab79SJassi Brar  * struct dma_interleaved_template - Template to convey DMAC the transfer pattern
135b14dab79SJassi Brar  *	 and attributes.
136b14dab79SJassi Brar  * @src_start: Bus address of source for the first chunk.
137b14dab79SJassi Brar  * @dst_start: Bus address of destination for the first chunk.
138b14dab79SJassi Brar  * @dir: Specifies the type of Source and Destination.
139b14dab79SJassi Brar  * @src_inc: If the source address increments after reading from it.
140b14dab79SJassi Brar  * @dst_inc: If the destination address increments after writing to it.
141b14dab79SJassi Brar  * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read).
142b14dab79SJassi Brar  *		Otherwise, source is read contiguously (icg ignored).
143b14dab79SJassi Brar  *		Ignored if src_inc is false.
144b14dab79SJassi Brar  * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write).
145b14dab79SJassi Brar  *		Otherwise, destination is filled contiguously (icg ignored).
146b14dab79SJassi Brar  *		Ignored if dst_inc is false.
147b14dab79SJassi Brar  * @numf: Number of frames in this template.
148b14dab79SJassi Brar  * @frame_size: Number of chunks in a frame i.e, size of sgl[].
149b14dab79SJassi Brar  * @sgl: Array of {chunk,icg} pairs that make up a frame.
150b14dab79SJassi Brar  */
151b14dab79SJassi Brar struct dma_interleaved_template {
152b14dab79SJassi Brar 	dma_addr_t src_start;
153b14dab79SJassi Brar 	dma_addr_t dst_start;
154b14dab79SJassi Brar 	enum dma_transfer_direction dir;
155b14dab79SJassi Brar 	bool src_inc;
156b14dab79SJassi Brar 	bool dst_inc;
157b14dab79SJassi Brar 	bool src_sgl;
158b14dab79SJassi Brar 	bool dst_sgl;
159b14dab79SJassi Brar 	size_t numf;
160b14dab79SJassi Brar 	size_t frame_size;
161466f966bSGustavo A. R. Silva 	struct data_chunk sgl[];
162b14dab79SJassi Brar };
163b14dab79SJassi Brar 
164b14dab79SJassi Brar /**
165636bdeaaSDan Williams  * enum dma_ctrl_flags - DMA flags to augment operation preparation,
166636bdeaaSDan Williams  *  control completion, and communicate status.
167d4c56f97SDan Williams  * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
168d4c56f97SDan Williams  *  this transaction
169a88f6667SGuennadi Liakhovetski  * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
1702be90e91SRandy Dunlap  *  acknowledges receipt, i.e. has a chance to establish any dependency
171b2f46fd8SDan Williams  *  chains
172b2f46fd8SDan Williams  * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
173b2f46fd8SDan Williams  * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
174b2f46fd8SDan Williams  * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
175b2f46fd8SDan Williams  *  sources that were the result of a previous operation, in the case of a PQ
176b2f46fd8SDan Williams  *  operation it continues the calculation with new sources
1770403e382SDan Williams  * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
1780403e382SDan Williams  *  on the result of this operation
17927242021SVinod Koul  * @DMA_CTRL_REUSE: client can reuse the descriptor and submit again till
18027242021SVinod Koul  *  cleared or freed
1813e00ab4aSAbhishek Sahu  * @DMA_PREP_CMD: tell the driver that the data passed to DMA API is command
1823e00ab4aSAbhishek Sahu  *  data and the descriptor should be in different format from normal
1833e00ab4aSAbhishek Sahu  *  data descriptors.
1849c8ebd8bSLaurent Pinchart  * @DMA_PREP_REPEAT: tell the driver that the transaction shall be automatically
1859c8ebd8bSLaurent Pinchart  *  repeated when it ends until a transaction is issued on the same channel
1869c8ebd8bSLaurent Pinchart  *  with the DMA_PREP_LOAD_EOT flag set. This flag is only applicable to
1879c8ebd8bSLaurent Pinchart  *  interleaved transactions and is ignored for all other transaction types.
1889c8ebd8bSLaurent Pinchart  * @DMA_PREP_LOAD_EOT: tell the driver that the transaction shall replace any
1899c8ebd8bSLaurent Pinchart  *  active repeated (as indicated by DMA_PREP_REPEAT) transaction when the
1909c8ebd8bSLaurent Pinchart  *  repeated transaction ends. Not setting this flag when the previously queued
1919c8ebd8bSLaurent Pinchart  *  transaction is marked with DMA_PREP_REPEAT will cause the new transaction
1929c8ebd8bSLaurent Pinchart  *  to never be processed and stay in the issued queue forever. The flag is
1939c8ebd8bSLaurent Pinchart  *  ignored if the previous transaction is not a repeated transaction.
194d4c56f97SDan Williams  */
195636bdeaaSDan Williams enum dma_ctrl_flags {
196d4c56f97SDan Williams 	DMA_PREP_INTERRUPT = (1 << 0),
197636bdeaaSDan Williams 	DMA_CTRL_ACK = (1 << 1),
1980776ae7bSBartlomiej Zolnierkiewicz 	DMA_PREP_PQ_DISABLE_P = (1 << 2),
1990776ae7bSBartlomiej Zolnierkiewicz 	DMA_PREP_PQ_DISABLE_Q = (1 << 3),
2000776ae7bSBartlomiej Zolnierkiewicz 	DMA_PREP_CONTINUE = (1 << 4),
2010776ae7bSBartlomiej Zolnierkiewicz 	DMA_PREP_FENCE = (1 << 5),
20227242021SVinod Koul 	DMA_CTRL_REUSE = (1 << 6),
2033e00ab4aSAbhishek Sahu 	DMA_PREP_CMD = (1 << 7),
2049c8ebd8bSLaurent Pinchart 	DMA_PREP_REPEAT = (1 << 8),
2059c8ebd8bSLaurent Pinchart 	DMA_PREP_LOAD_EOT = (1 << 9),
206d4c56f97SDan Williams };
207d4c56f97SDan Williams 
208d4c56f97SDan Williams /**
209ad283ea4SDan Williams  * enum sum_check_bits - bit position of pq_check_flags
210ad283ea4SDan Williams  */
211ad283ea4SDan Williams enum sum_check_bits {
212ad283ea4SDan Williams 	SUM_CHECK_P = 0,
213ad283ea4SDan Williams 	SUM_CHECK_Q = 1,
214ad283ea4SDan Williams };
215ad283ea4SDan Williams 
216ad283ea4SDan Williams /**
217ad283ea4SDan Williams  * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
218ad283ea4SDan Williams  * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
219ad283ea4SDan Williams  * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
220ad283ea4SDan Williams  */
221ad283ea4SDan Williams enum sum_check_flags {
222ad283ea4SDan Williams 	SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
223ad283ea4SDan Williams 	SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
224ad283ea4SDan Williams };
225ad283ea4SDan Williams 
226ad283ea4SDan Williams 
227ad283ea4SDan Williams /**
2287405f74bSDan Williams  * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
2297405f74bSDan Williams  * See linux/cpumask.h
2307405f74bSDan Williams  */
2317405f74bSDan Williams typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
2327405f74bSDan Williams 
2337405f74bSDan Williams /**
2344db8fd32SPeter Ujfalusi  * enum dma_desc_metadata_mode - per descriptor metadata mode types supported
2354db8fd32SPeter Ujfalusi  * @DESC_METADATA_CLIENT - the metadata buffer is allocated/provided by the
2364db8fd32SPeter Ujfalusi  *  client driver and it is attached (via the dmaengine_desc_attach_metadata()
2374db8fd32SPeter Ujfalusi  *  helper) to the descriptor.
2384db8fd32SPeter Ujfalusi  *
2394db8fd32SPeter Ujfalusi  * Client drivers interested to use this mode can follow:
2404db8fd32SPeter Ujfalusi  * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
2414db8fd32SPeter Ujfalusi  *   1. prepare the descriptor (dmaengine_prep_*)
2424db8fd32SPeter Ujfalusi  *	construct the metadata in the client's buffer
2434db8fd32SPeter Ujfalusi  *   2. use dmaengine_desc_attach_metadata() to attach the buffer to the
2444db8fd32SPeter Ujfalusi  *	descriptor
2454db8fd32SPeter Ujfalusi  *   3. submit the transfer
2464db8fd32SPeter Ujfalusi  * - DMA_DEV_TO_MEM:
2474db8fd32SPeter Ujfalusi  *   1. prepare the descriptor (dmaengine_prep_*)
2484db8fd32SPeter Ujfalusi  *   2. use dmaengine_desc_attach_metadata() to attach the buffer to the
2494db8fd32SPeter Ujfalusi  *	descriptor
2504db8fd32SPeter Ujfalusi  *   3. submit the transfer
2514db8fd32SPeter Ujfalusi  *   4. when the transfer is completed, the metadata should be available in the
2524db8fd32SPeter Ujfalusi  *	attached buffer
2534db8fd32SPeter Ujfalusi  *
2544db8fd32SPeter Ujfalusi  * @DESC_METADATA_ENGINE - the metadata buffer is allocated/managed by the DMA
2554db8fd32SPeter Ujfalusi  *  driver. The client driver can ask for the pointer, maximum size and the
2564db8fd32SPeter Ujfalusi  *  currently used size of the metadata and can directly update or read it.
2574db8fd32SPeter Ujfalusi  *  dmaengine_desc_get_metadata_ptr() and dmaengine_desc_set_metadata_len() is
2584db8fd32SPeter Ujfalusi  *  provided as helper functions.
2594db8fd32SPeter Ujfalusi  *
2604db8fd32SPeter Ujfalusi  *  Note: the metadata area for the descriptor is no longer valid after the
2614db8fd32SPeter Ujfalusi  *  transfer has been completed (valid up to the point when the completion
2624db8fd32SPeter Ujfalusi  *  callback returns if used).
2634db8fd32SPeter Ujfalusi  *
2644db8fd32SPeter Ujfalusi  * Client drivers interested to use this mode can follow:
2654db8fd32SPeter Ujfalusi  * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
2664db8fd32SPeter Ujfalusi  *   1. prepare the descriptor (dmaengine_prep_*)
2674db8fd32SPeter Ujfalusi  *   2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the engine's
2684db8fd32SPeter Ujfalusi  *	metadata area
2694db8fd32SPeter Ujfalusi  *   3. update the metadata at the pointer
2704db8fd32SPeter Ujfalusi  *   4. use dmaengine_desc_set_metadata_len()  to tell the DMA engine the amount
2714db8fd32SPeter Ujfalusi  *	of data the client has placed into the metadata buffer
2724db8fd32SPeter Ujfalusi  *   5. submit the transfer
2734db8fd32SPeter Ujfalusi  * - DMA_DEV_TO_MEM:
2744db8fd32SPeter Ujfalusi  *   1. prepare the descriptor (dmaengine_prep_*)
2754db8fd32SPeter Ujfalusi  *   2. submit the transfer
2764db8fd32SPeter Ujfalusi  *   3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get the
2774db8fd32SPeter Ujfalusi  *	pointer to the engine's metadata area
2784db8fd32SPeter Ujfalusi  *   4. Read out the metadata from the pointer
2794db8fd32SPeter Ujfalusi  *
2804db8fd32SPeter Ujfalusi  * Note: the two mode is not compatible and clients must use one mode for a
2814db8fd32SPeter Ujfalusi  * descriptor.
2824db8fd32SPeter Ujfalusi  */
2834db8fd32SPeter Ujfalusi enum dma_desc_metadata_mode {
2844db8fd32SPeter Ujfalusi 	DESC_METADATA_NONE = 0,
2854db8fd32SPeter Ujfalusi 	DESC_METADATA_CLIENT = BIT(0),
2864db8fd32SPeter Ujfalusi 	DESC_METADATA_ENGINE = BIT(1),
2874db8fd32SPeter Ujfalusi };
2884db8fd32SPeter Ujfalusi 
289acfbb191SAndy Shevchenko /**
290acfbb191SAndy Shevchenko  * struct dma_chan_percpu - the per-CPU part of struct dma_chan
291acfbb191SAndy Shevchenko  * @memcpy_count: transaction counter
292acfbb191SAndy Shevchenko  * @bytes_transferred: byte counter
293acfbb191SAndy Shevchenko  */
294c13c8260SChris Leech struct dma_chan_percpu {
295c13c8260SChris Leech 	/* stats */
296c13c8260SChris Leech 	unsigned long memcpy_count;
297c13c8260SChris Leech 	unsigned long bytes_transferred;
298c13c8260SChris Leech };
299c13c8260SChris Leech 
300c13c8260SChris Leech /**
30156f13c0dSPeter Ujfalusi  * struct dma_router - DMA router structure
30256f13c0dSPeter Ujfalusi  * @dev: pointer to the DMA router device
30356f13c0dSPeter Ujfalusi  * @route_free: function to be called when the route can be disconnected
30456f13c0dSPeter Ujfalusi  */
30556f13c0dSPeter Ujfalusi struct dma_router {
30656f13c0dSPeter Ujfalusi 	struct device *dev;
30756f13c0dSPeter Ujfalusi 	void (*route_free)(struct device *dev, void *route_data);
30856f13c0dSPeter Ujfalusi };
30956f13c0dSPeter Ujfalusi 
31056f13c0dSPeter Ujfalusi /**
311c13c8260SChris Leech  * struct dma_chan - devices supply DMA channels, clients use them
312fe4ada2dSRandy Dunlap  * @device: ptr to the dma device who supplies this channel, always !%NULL
31371723a96SGeert Uytterhoeven  * @slave: ptr to the device using this channel
314c13c8260SChris Leech  * @cookie: last cookie value returned to client
3154d4e58deSRussell King - ARM Linux  * @completed_cookie: last completed cookie for this channel
316fe4ada2dSRandy Dunlap  * @chan_id: channel ID for sysfs
31741d5e59cSDan Williams  * @dev: class device for sysfs
31871723a96SGeert Uytterhoeven  * @name: backlink name for sysfs
319e937cc1dSPeter Ujfalusi  * @dbg_client_name: slave name for debugfs in format:
320e937cc1dSPeter Ujfalusi  *	dev_name(requester's dev):channel name, for example: "2b00000.mcasp:tx"
321c13c8260SChris Leech  * @device_node: used to add this to the device chan list
322c13c8260SChris Leech  * @local: per-cpu pointer to a struct dma_chan_percpu
323868d2ee2SVinod Koul  * @client_count: how many clients are using this channel
324bec08513SDan Williams  * @table_count: number of appearances in the mem-to-mem allocation table
32556f13c0dSPeter Ujfalusi  * @router: pointer to the DMA router structure
32656f13c0dSPeter Ujfalusi  * @route_data: channel specific data for the router
327287d8592SDan Williams  * @private: private data for certain client-channel associations
328c13c8260SChris Leech  */
329c13c8260SChris Leech struct dma_chan {
330c13c8260SChris Leech 	struct dma_device *device;
33171723a96SGeert Uytterhoeven 	struct device *slave;
332c13c8260SChris Leech 	dma_cookie_t cookie;
3334d4e58deSRussell King - ARM Linux 	dma_cookie_t completed_cookie;
334c13c8260SChris Leech 
335c13c8260SChris Leech 	/* sysfs */
336c13c8260SChris Leech 	int chan_id;
33741d5e59cSDan Williams 	struct dma_chan_dev *dev;
33871723a96SGeert Uytterhoeven 	const char *name;
339e937cc1dSPeter Ujfalusi #ifdef CONFIG_DEBUG_FS
340e937cc1dSPeter Ujfalusi 	char *dbg_client_name;
341e937cc1dSPeter Ujfalusi #endif
342c13c8260SChris Leech 
343c13c8260SChris Leech 	struct list_head device_node;
344a29d8b8eSTejun Heo 	struct dma_chan_percpu __percpu *local;
3457cc5bf9aSDan Williams 	int client_count;
346bec08513SDan Williams 	int table_count;
34756f13c0dSPeter Ujfalusi 
34856f13c0dSPeter Ujfalusi 	/* DMA router */
34956f13c0dSPeter Ujfalusi 	struct dma_router *router;
35056f13c0dSPeter Ujfalusi 	void *route_data;
35156f13c0dSPeter Ujfalusi 
352287d8592SDan Williams 	void *private;
353c13c8260SChris Leech };
354c13c8260SChris Leech 
35541d5e59cSDan Williams /**
35641d5e59cSDan Williams  * struct dma_chan_dev - relate sysfs device node to backing channel device
357868d2ee2SVinod Koul  * @chan: driver channel device
358868d2ee2SVinod Koul  * @device: sysfs device
359868d2ee2SVinod Koul  * @dev_id: parent dma_device dev_id
360ab650ef6SPeter Ujfalusi  * @chan_dma_dev: The channel is using custom/different dma-mapping
361ab650ef6SPeter Ujfalusi  * compared to the parent dma_device
36241d5e59cSDan Williams  */
36341d5e59cSDan Williams struct dma_chan_dev {
36441d5e59cSDan Williams 	struct dma_chan *chan;
36541d5e59cSDan Williams 	struct device device;
366864498aaSDan Williams 	int dev_id;
367ab650ef6SPeter Ujfalusi 	bool chan_dma_dev;
36841d5e59cSDan Williams };
36941d5e59cSDan Williams 
370c156d0a5SLinus Walleij /**
371ba730340SAlexander Popov  * enum dma_slave_buswidth - defines bus width of the DMA slave
372c156d0a5SLinus Walleij  * device, source or target buses
373c156d0a5SLinus Walleij  */
374c156d0a5SLinus Walleij enum dma_slave_buswidth {
375c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
376c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
377c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
37893c6ee94SPeter Ujfalusi 	DMA_SLAVE_BUSWIDTH_3_BYTES = 3,
379c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
380c156d0a5SLinus Walleij 	DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
381534a7298SLaurent Pinchart 	DMA_SLAVE_BUSWIDTH_16_BYTES = 16,
382534a7298SLaurent Pinchart 	DMA_SLAVE_BUSWIDTH_32_BYTES = 32,
383534a7298SLaurent Pinchart 	DMA_SLAVE_BUSWIDTH_64_BYTES = 64,
384ab959c7dSBiju Das 	DMA_SLAVE_BUSWIDTH_128_BYTES = 128,
385c156d0a5SLinus Walleij };
386c156d0a5SLinus Walleij 
387c156d0a5SLinus Walleij /**
388c156d0a5SLinus Walleij  * struct dma_slave_config - dma slave channel runtime config
389c156d0a5SLinus Walleij  * @direction: whether the data shall go in or out on this slave
390397321f4SAlexander Popov  * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are
391d9ff958bSLaurent Pinchart  * legal values. DEPRECATED, drivers should use the direction argument
392d9ff958bSLaurent Pinchart  * to the device_prep_slave_sg and device_prep_dma_cyclic functions or
393d9ff958bSLaurent Pinchart  * the dir field in the dma_interleaved_template structure.
394c156d0a5SLinus Walleij  * @src_addr: this is the physical address where DMA slave data
395c156d0a5SLinus Walleij  * should be read (RX), if the source is memory this argument is
396c156d0a5SLinus Walleij  * ignored.
397c156d0a5SLinus Walleij  * @dst_addr: this is the physical address where DMA slave data
398c156d0a5SLinus Walleij  * should be written (TX), if the source is memory this argument
399c156d0a5SLinus Walleij  * is ignored.
400c156d0a5SLinus Walleij  * @src_addr_width: this is the width in bytes of the source (RX)
401c156d0a5SLinus Walleij  * register where DMA data shall be read. If the source
402c156d0a5SLinus Walleij  * is memory this may be ignored depending on architecture.
403ab959c7dSBiju Das  * Legal values: 1, 2, 3, 4, 8, 16, 32, 64, 128.
404c156d0a5SLinus Walleij  * @dst_addr_width: same as src_addr_width but for destination
405c156d0a5SLinus Walleij  * target (TX) mutatis mutandis.
406c156d0a5SLinus Walleij  * @src_maxburst: the maximum number of words (note: words, as in
407c156d0a5SLinus Walleij  * units of the src_addr_width member, not bytes) that can be sent
408c156d0a5SLinus Walleij  * in one burst to the device. Typically something like half the
409c156d0a5SLinus Walleij  * FIFO depth on I/O peripherals so you don't overflow it. This
410c156d0a5SLinus Walleij  * may or may not be applicable on memory sources.
411c156d0a5SLinus Walleij  * @dst_maxburst: same as src_maxburst but for destination target
412c156d0a5SLinus Walleij  * mutatis mutandis.
41354cd2558SPeter Ujfalusi  * @src_port_window_size: The length of the register area in words the data need
41454cd2558SPeter Ujfalusi  * to be accessed on the device side. It is only used for devices which is using
41554cd2558SPeter Ujfalusi  * an area instead of a single register to receive the data. Typically the DMA
41654cd2558SPeter Ujfalusi  * loops in this area in order to transfer the data.
41754cd2558SPeter Ujfalusi  * @dst_port_window_size: same as src_port_window_size but for the destination
41854cd2558SPeter Ujfalusi  * port.
419dcc043dcSViresh Kumar  * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill
420dcc043dcSViresh Kumar  * with 'true' if peripheral should be flow controller. Direction will be
421dcc043dcSViresh Kumar  * selected at Runtime.
422e7bbb7acSVinod Koul  * @peripheral_config: peripheral configuration for programming peripheral
423e7bbb7acSVinod Koul  * for dmaengine transfer
424e7bbb7acSVinod Koul  * @peripheral_size: peripheral configuration buffer size
425c156d0a5SLinus Walleij  *
426c156d0a5SLinus Walleij  * This struct is passed in as configuration data to a DMA engine
427c156d0a5SLinus Walleij  * in order to set up a certain channel for DMA transport at runtime.
428c156d0a5SLinus Walleij  * The DMA device/engine has to provide support for an additional
4292c44ad91SMaxime Ripard  * callback in the dma_device structure, device_config and this struct
4302c44ad91SMaxime Ripard  * will then be passed in as an argument to the function.
431c156d0a5SLinus Walleij  *
4327cbccb55SLars-Peter Clausen  * The rationale for adding configuration information to this struct is as
4337cbccb55SLars-Peter Clausen  * follows: if it is likely that more than one DMA slave controllers in
4347cbccb55SLars-Peter Clausen  * the world will support the configuration option, then make it generic.
4357cbccb55SLars-Peter Clausen  * If not: if it is fixed so that it be sent in static from the platform
4367cbccb55SLars-Peter Clausen  * data, then prefer to do that.
437c156d0a5SLinus Walleij  */
438c156d0a5SLinus Walleij struct dma_slave_config {
43949920bc6SVinod Koul 	enum dma_transfer_direction direction;
44095756320SVinod Koul 	phys_addr_t src_addr;
44195756320SVinod Koul 	phys_addr_t dst_addr;
442c156d0a5SLinus Walleij 	enum dma_slave_buswidth src_addr_width;
443c156d0a5SLinus Walleij 	enum dma_slave_buswidth dst_addr_width;
444c156d0a5SLinus Walleij 	u32 src_maxburst;
445c156d0a5SLinus Walleij 	u32 dst_maxburst;
44654cd2558SPeter Ujfalusi 	u32 src_port_window_size;
44754cd2558SPeter Ujfalusi 	u32 dst_port_window_size;
448dcc043dcSViresh Kumar 	bool device_fc;
449e7bbb7acSVinod Koul 	void *peripheral_config;
450e7bbb7acSVinod Koul 	size_t peripheral_size;
451c156d0a5SLinus Walleij };
452c156d0a5SLinus Walleij 
45350720563SLars-Peter Clausen /**
45450720563SLars-Peter Clausen  * enum dma_residue_granularity - Granularity of the reported transfer residue
45550720563SLars-Peter Clausen  * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The
45650720563SLars-Peter Clausen  *  DMA channel is only able to tell whether a descriptor has been completed or
45750720563SLars-Peter Clausen  *  not, which means residue reporting is not supported by this channel. The
45850720563SLars-Peter Clausen  *  residue field of the dma_tx_state field will always be 0.
45950720563SLars-Peter Clausen  * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully
46050720563SLars-Peter Clausen  *  completed segment of the transfer (For cyclic transfers this is after each
46150720563SLars-Peter Clausen  *  period). This is typically implemented by having the hardware generate an
46250720563SLars-Peter Clausen  *  interrupt after each transferred segment and then the drivers updates the
46350720563SLars-Peter Clausen  *  outstanding residue by the size of the segment. Another possibility is if
46450720563SLars-Peter Clausen  *  the hardware supports scatter-gather and the segment descriptor has a field
46550720563SLars-Peter Clausen  *  which gets set after the segment has been completed. The driver then counts
46650720563SLars-Peter Clausen  *  the number of segments without the flag set to compute the residue.
46750720563SLars-Peter Clausen  * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred
46850720563SLars-Peter Clausen  *  burst. This is typically only supported if the hardware has a progress
46950720563SLars-Peter Clausen  *  register of some sort (E.g. a register with the current read/write address
47050720563SLars-Peter Clausen  *  or a register with the amount of bursts/beats/bytes that have been
47150720563SLars-Peter Clausen  *  transferred or still need to be transferred).
47250720563SLars-Peter Clausen  */
47350720563SLars-Peter Clausen enum dma_residue_granularity {
47450720563SLars-Peter Clausen 	DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0,
47550720563SLars-Peter Clausen 	DMA_RESIDUE_GRANULARITY_SEGMENT = 1,
47650720563SLars-Peter Clausen 	DMA_RESIDUE_GRANULARITY_BURST = 2,
47750720563SLars-Peter Clausen };
47850720563SLars-Peter Clausen 
479c2cbd427SStefan Brüns /**
480c2cbd427SStefan Brüns  * struct dma_slave_caps - expose capabilities of a slave channel only
481c2cbd427SStefan Brüns  * @src_addr_widths: bit mask of src addr widths the channel supports.
482c2cbd427SStefan Brüns  *	Width is specified in bytes, e.g. for a channel supporting
483c2cbd427SStefan Brüns  *	a width of 4 the mask should have BIT(4) set.
484c2cbd427SStefan Brüns  * @dst_addr_widths: bit mask of dst addr widths the channel supports
485c2cbd427SStefan Brüns  * @directions: bit mask of slave directions the channel supports.
486c2cbd427SStefan Brüns  *	Since the enum dma_transfer_direction is not defined as bit flag for
487c2cbd427SStefan Brüns  *	each type, the dma controller should set BIT(<TYPE>) and same
488221a27c7SVinod Koul  *	should be checked by controller as well
489d97758e0SSerge Semin  * @min_burst: min burst capability per-transfer
4906d5bbed3SShawn Lin  * @max_burst: max burst capability per-transfer
491b1b40b8fSSerge Semin  * @max_sg_burst: max number of SG list entries executed in a single burst
492b1b40b8fSSerge Semin  *	DMA tansaction with no software intervention for reinitialization.
493b1b40b8fSSerge Semin  *	Zero value means unlimited number of entries.
494d8095f94SMarek Szyprowski  * @cmd_pause: true, if pause is supported (i.e. for reading residue or
495d8095f94SMarek Szyprowski  *	       for resume later)
496d8095f94SMarek Szyprowski  * @cmd_resume: true, if resume is supported
497221a27c7SVinod Koul  * @cmd_terminate: true, if terminate cmd is supported
49850720563SLars-Peter Clausen  * @residue_granularity: granularity of the reported transfer residue
49927242021SVinod Koul  * @descriptor_reuse: if a descriptor can be reused by client and
50027242021SVinod Koul  * resubmitted multiple times
501221a27c7SVinod Koul  */
502221a27c7SVinod Koul struct dma_slave_caps {
503221a27c7SVinod Koul 	u32 src_addr_widths;
504ceacbdbfSMaxime Ripard 	u32 dst_addr_widths;
505221a27c7SVinod Koul 	u32 directions;
506d97758e0SSerge Semin 	u32 min_burst;
5076d5bbed3SShawn Lin 	u32 max_burst;
508b1b40b8fSSerge Semin 	u32 max_sg_burst;
509221a27c7SVinod Koul 	bool cmd_pause;
510d8095f94SMarek Szyprowski 	bool cmd_resume;
511221a27c7SVinod Koul 	bool cmd_terminate;
51250720563SLars-Peter Clausen 	enum dma_residue_granularity residue_granularity;
51327242021SVinod Koul 	bool descriptor_reuse;
514221a27c7SVinod Koul };
515221a27c7SVinod Koul 
51641d5e59cSDan Williams static inline const char *dma_chan_name(struct dma_chan *chan)
51741d5e59cSDan Williams {
51841d5e59cSDan Williams 	return dev_name(&chan->dev->device);
51941d5e59cSDan Williams }
520d379b01eSDan Williams 
521c13c8260SChris Leech void dma_chan_cleanup(struct kref *kref);
522c13c8260SChris Leech 
523c13c8260SChris Leech /**
52459b5ec21SDan Williams  * typedef dma_filter_fn - callback filter for dma_request_channel
52559b5ec21SDan Williams  * @chan: channel to be reviewed
52659b5ec21SDan Williams  * @filter_param: opaque parameter passed through dma_request_channel
52759b5ec21SDan Williams  *
52859b5ec21SDan Williams  * When this optional parameter is specified in a call to dma_request_channel a
52959b5ec21SDan Williams  * suitable channel is passed to this routine for further dispositioning before
53059b5ec21SDan Williams  * being returned.  Where 'suitable' indicates a non-busy channel that
5317dd60251SDan Williams  * satisfies the given capability mask.  It returns 'true' to indicate that the
5327dd60251SDan Williams  * channel is suitable.
53359b5ec21SDan Williams  */
5347dd60251SDan Williams typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
53559b5ec21SDan Williams 
5367405f74bSDan Williams typedef void (*dma_async_tx_callback)(void *dma_async_param);
537d38a8c62SDan Williams 
538f067025bSDave Jiang enum dmaengine_tx_result {
539f067025bSDave Jiang 	DMA_TRANS_NOERROR = 0,		/* SUCCESS */
540f067025bSDave Jiang 	DMA_TRANS_READ_FAILED,		/* Source DMA read failed */
541f067025bSDave Jiang 	DMA_TRANS_WRITE_FAILED,		/* Destination DMA write failed */
542f067025bSDave Jiang 	DMA_TRANS_ABORTED,		/* Op never submitted / aborted */
543f067025bSDave Jiang };
544f067025bSDave Jiang 
545f067025bSDave Jiang struct dmaengine_result {
546f067025bSDave Jiang 	enum dmaengine_tx_result result;
547f067025bSDave Jiang 	u32 residue;
548f067025bSDave Jiang };
549f067025bSDave Jiang 
550f067025bSDave Jiang typedef void (*dma_async_tx_callback_result)(void *dma_async_param,
551f067025bSDave Jiang 				const struct dmaengine_result *result);
552f067025bSDave Jiang 
553d38a8c62SDan Williams struct dmaengine_unmap_data {
5540c0eb4caSZi Yan #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
5550c0eb4caSZi Yan 	u16 map_cnt;
5560c0eb4caSZi Yan #else
557c1f43dd9SXuelin Shi 	u8 map_cnt;
5580c0eb4caSZi Yan #endif
559d38a8c62SDan Williams 	u8 to_cnt;
560d38a8c62SDan Williams 	u8 from_cnt;
561d38a8c62SDan Williams 	u8 bidi_cnt;
562d38a8c62SDan Williams 	struct device *dev;
563d38a8c62SDan Williams 	struct kref kref;
564d38a8c62SDan Williams 	size_t len;
565466f966bSGustavo A. R. Silva 	dma_addr_t addr[];
566d38a8c62SDan Williams };
567d38a8c62SDan Williams 
5684db8fd32SPeter Ujfalusi struct dma_async_tx_descriptor;
5694db8fd32SPeter Ujfalusi 
5704db8fd32SPeter Ujfalusi struct dma_descriptor_metadata_ops {
5714db8fd32SPeter Ujfalusi 	int (*attach)(struct dma_async_tx_descriptor *desc, void *data,
5724db8fd32SPeter Ujfalusi 		      size_t len);
5734db8fd32SPeter Ujfalusi 
5744db8fd32SPeter Ujfalusi 	void *(*get_ptr)(struct dma_async_tx_descriptor *desc,
5754db8fd32SPeter Ujfalusi 			 size_t *payload_len, size_t *max_len);
5764db8fd32SPeter Ujfalusi 	int (*set_len)(struct dma_async_tx_descriptor *desc,
5774db8fd32SPeter Ujfalusi 		       size_t payload_len);
5784db8fd32SPeter Ujfalusi };
5794db8fd32SPeter Ujfalusi 
5807405f74bSDan Williams /**
5817405f74bSDan Williams  * struct dma_async_tx_descriptor - async transaction descriptor
5827405f74bSDan Williams  * ---dma generic offload fields---
5837405f74bSDan Williams  * @cookie: tracking cookie for this transaction, set to -EBUSY if
5847405f74bSDan Williams  *	this tx is sitting on a dependency list
585636bdeaaSDan Williams  * @flags: flags to augment operation preparation, control completion, and
586636bdeaaSDan Williams  *	communicate status
5877405f74bSDan Williams  * @phys: physical address of the descriptor
5887405f74bSDan Williams  * @chan: target channel for this operation
589aba96badSVinod Koul  * @tx_submit: accept the descriptor, assign ordered cookie and mark the
590aba96badSVinod Koul  * descriptor pending. To be pushed on .issue_pending() call
5917405f74bSDan Williams  * @callback: routine to call after this operation is complete
5927405f74bSDan Williams  * @callback_param: general parameter to pass to the callback routine
5934db8fd32SPeter Ujfalusi  * @desc_metadata_mode: core managed metadata mode to protect mixed use of
5944db8fd32SPeter Ujfalusi  *	DESC_METADATA_CLIENT or DESC_METADATA_ENGINE. Otherwise
5954db8fd32SPeter Ujfalusi  *	DESC_METADATA_NONE
5964db8fd32SPeter Ujfalusi  * @metadata_ops: DMA driver provided metadata mode ops, need to be set by the
5974db8fd32SPeter Ujfalusi  *	DMA driver if metadata mode is supported with the descriptor
5987405f74bSDan Williams  * ---async_tx api specific fields---
59919242d72SDan Williams  * @next: at completion submit this descriptor
6007405f74bSDan Williams  * @parent: pointer to the next level up in the dependency chain
60119242d72SDan Williams  * @lock: protect the parent and next pointers
6027405f74bSDan Williams  */
6037405f74bSDan Williams struct dma_async_tx_descriptor {
6047405f74bSDan Williams 	dma_cookie_t cookie;
605636bdeaaSDan Williams 	enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
6067405f74bSDan Williams 	dma_addr_t phys;
6077405f74bSDan Williams 	struct dma_chan *chan;
6087405f74bSDan Williams 	dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
60927242021SVinod Koul 	int (*desc_free)(struct dma_async_tx_descriptor *tx);
6107405f74bSDan Williams 	dma_async_tx_callback callback;
611f067025bSDave Jiang 	dma_async_tx_callback_result callback_result;
6127405f74bSDan Williams 	void *callback_param;
613d38a8c62SDan Williams 	struct dmaengine_unmap_data *unmap;
6144db8fd32SPeter Ujfalusi 	enum dma_desc_metadata_mode desc_metadata_mode;
6154db8fd32SPeter Ujfalusi 	struct dma_descriptor_metadata_ops *metadata_ops;
6165fc6d897SDan Williams #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
61719242d72SDan Williams 	struct dma_async_tx_descriptor *next;
6187405f74bSDan Williams 	struct dma_async_tx_descriptor *parent;
6197405f74bSDan Williams 	spinlock_t lock;
620caa20d97SDan Williams #endif
6217405f74bSDan Williams };
6227405f74bSDan Williams 
62389716462SDan Williams #ifdef CONFIG_DMA_ENGINE
624d38a8c62SDan Williams static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
625d38a8c62SDan Williams 				 struct dmaengine_unmap_data *unmap)
626d38a8c62SDan Williams {
627d38a8c62SDan Williams 	kref_get(&unmap->kref);
628d38a8c62SDan Williams 	tx->unmap = unmap;
629d38a8c62SDan Williams }
630d38a8c62SDan Williams 
63189716462SDan Williams struct dmaengine_unmap_data *
63289716462SDan Williams dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
63345c463aeSDan Williams void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
63489716462SDan Williams #else
63589716462SDan Williams static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
63689716462SDan Williams 				 struct dmaengine_unmap_data *unmap)
63789716462SDan Williams {
63889716462SDan Williams }
63989716462SDan Williams static inline struct dmaengine_unmap_data *
64089716462SDan Williams dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
64189716462SDan Williams {
64289716462SDan Williams 	return NULL;
64389716462SDan Williams }
64489716462SDan Williams static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
64589716462SDan Williams {
64689716462SDan Williams }
64789716462SDan Williams #endif
64845c463aeSDan Williams 
649d38a8c62SDan Williams static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
650d38a8c62SDan Williams {
6513a92063bSAndy Shevchenko 	if (!tx->unmap)
6523a92063bSAndy Shevchenko 		return;
6533a92063bSAndy Shevchenko 
65445c463aeSDan Williams 	dmaengine_unmap_put(tx->unmap);
655d38a8c62SDan Williams 	tx->unmap = NULL;
656d38a8c62SDan Williams }
657d38a8c62SDan Williams 
6585fc6d897SDan Williams #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
659caa20d97SDan Williams static inline void txd_lock(struct dma_async_tx_descriptor *txd)
660caa20d97SDan Williams {
661caa20d97SDan Williams }
662caa20d97SDan Williams static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
663caa20d97SDan Williams {
664caa20d97SDan Williams }
665caa20d97SDan Williams static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
666caa20d97SDan Williams {
667caa20d97SDan Williams 	BUG();
668caa20d97SDan Williams }
669caa20d97SDan Williams static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
670caa20d97SDan Williams {
671caa20d97SDan Williams }
672caa20d97SDan Williams static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
673caa20d97SDan Williams {
674caa20d97SDan Williams }
675caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
676caa20d97SDan Williams {
677caa20d97SDan Williams 	return NULL;
678caa20d97SDan Williams }
679caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
680caa20d97SDan Williams {
681caa20d97SDan Williams 	return NULL;
682caa20d97SDan Williams }
683caa20d97SDan Williams 
684caa20d97SDan Williams #else
685caa20d97SDan Williams static inline void txd_lock(struct dma_async_tx_descriptor *txd)
686caa20d97SDan Williams {
687caa20d97SDan Williams 	spin_lock_bh(&txd->lock);
688caa20d97SDan Williams }
689caa20d97SDan Williams static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
690caa20d97SDan Williams {
691caa20d97SDan Williams 	spin_unlock_bh(&txd->lock);
692caa20d97SDan Williams }
693caa20d97SDan Williams static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
694caa20d97SDan Williams {
695caa20d97SDan Williams 	txd->next = next;
696caa20d97SDan Williams 	next->parent = txd;
697caa20d97SDan Williams }
698caa20d97SDan Williams static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
699caa20d97SDan Williams {
700caa20d97SDan Williams 	txd->parent = NULL;
701caa20d97SDan Williams }
702caa20d97SDan Williams static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
703caa20d97SDan Williams {
704caa20d97SDan Williams 	txd->next = NULL;
705caa20d97SDan Williams }
706caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
707caa20d97SDan Williams {
708caa20d97SDan Williams 	return txd->parent;
709caa20d97SDan Williams }
710caa20d97SDan Williams static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
711caa20d97SDan Williams {
712caa20d97SDan Williams 	return txd->next;
713caa20d97SDan Williams }
714caa20d97SDan Williams #endif
715caa20d97SDan Williams 
716c13c8260SChris Leech /**
71707934481SLinus Walleij  * struct dma_tx_state - filled in to report the status of
71807934481SLinus Walleij  * a transfer.
71907934481SLinus Walleij  * @last: last completed DMA cookie
72007934481SLinus Walleij  * @used: last issued DMA cookie (i.e. the one in progress)
72107934481SLinus Walleij  * @residue: the remaining number of bytes left to transmit
72207934481SLinus Walleij  *	on the selected transfer for states DMA_IN_PROGRESS and
72307934481SLinus Walleij  *	DMA_PAUSED if this is implemented in the driver, else 0
7246755ec06SPeter Ujfalusi  * @in_flight_bytes: amount of data in bytes cached by the DMA.
72507934481SLinus Walleij  */
72607934481SLinus Walleij struct dma_tx_state {
72707934481SLinus Walleij 	dma_cookie_t last;
72807934481SLinus Walleij 	dma_cookie_t used;
72907934481SLinus Walleij 	u32 residue;
7306755ec06SPeter Ujfalusi 	u32 in_flight_bytes;
73107934481SLinus Walleij };
73207934481SLinus Walleij 
73307934481SLinus Walleij /**
73477a68e56SMaxime Ripard  * enum dmaengine_alignment - defines alignment of the DMA async tx
73577a68e56SMaxime Ripard  * buffers
73677a68e56SMaxime Ripard  */
73777a68e56SMaxime Ripard enum dmaengine_alignment {
73877a68e56SMaxime Ripard 	DMAENGINE_ALIGN_1_BYTE = 0,
73977a68e56SMaxime Ripard 	DMAENGINE_ALIGN_2_BYTES = 1,
74077a68e56SMaxime Ripard 	DMAENGINE_ALIGN_4_BYTES = 2,
74177a68e56SMaxime Ripard 	DMAENGINE_ALIGN_8_BYTES = 3,
74277a68e56SMaxime Ripard 	DMAENGINE_ALIGN_16_BYTES = 4,
74377a68e56SMaxime Ripard 	DMAENGINE_ALIGN_32_BYTES = 5,
74477a68e56SMaxime Ripard 	DMAENGINE_ALIGN_64_BYTES = 6,
745660343d0SPeter Ujfalusi 	DMAENGINE_ALIGN_128_BYTES = 7,
746660343d0SPeter Ujfalusi 	DMAENGINE_ALIGN_256_BYTES = 8,
74777a68e56SMaxime Ripard };
74877a68e56SMaxime Ripard 
74977a68e56SMaxime Ripard /**
750a8135d0dSPeter Ujfalusi  * struct dma_slave_map - associates slave device and it's slave channel with
751a8135d0dSPeter Ujfalusi  * parameter to be used by a filter function
752a8135d0dSPeter Ujfalusi  * @devname: name of the device
753a8135d0dSPeter Ujfalusi  * @slave: slave channel name
754a8135d0dSPeter Ujfalusi  * @param: opaque parameter to pass to struct dma_filter.fn
755a8135d0dSPeter Ujfalusi  */
756a8135d0dSPeter Ujfalusi struct dma_slave_map {
757a8135d0dSPeter Ujfalusi 	const char *devname;
758a8135d0dSPeter Ujfalusi 	const char *slave;
759a8135d0dSPeter Ujfalusi 	void *param;
760a8135d0dSPeter Ujfalusi };
761a8135d0dSPeter Ujfalusi 
762a8135d0dSPeter Ujfalusi /**
763a8135d0dSPeter Ujfalusi  * struct dma_filter - information for slave device/channel to filter_fn/param
764a8135d0dSPeter Ujfalusi  * mapping
765a8135d0dSPeter Ujfalusi  * @fn: filter function callback
766a8135d0dSPeter Ujfalusi  * @mapcnt: number of slave device/channel in the map
767a8135d0dSPeter Ujfalusi  * @map: array of channel to filter mapping data
768a8135d0dSPeter Ujfalusi  */
769a8135d0dSPeter Ujfalusi struct dma_filter {
770a8135d0dSPeter Ujfalusi 	dma_filter_fn fn;
771a8135d0dSPeter Ujfalusi 	int mapcnt;
772a8135d0dSPeter Ujfalusi 	const struct dma_slave_map *map;
773a8135d0dSPeter Ujfalusi };
774a8135d0dSPeter Ujfalusi 
775a8135d0dSPeter Ujfalusi /**
776c13c8260SChris Leech  * struct dma_device - info on the entity supplying DMA services
777c13c8260SChris Leech  * @chancnt: how many DMA channels are supported
7780f571515SAtsushi Nemoto  * @privatecnt: how many DMA channels are requested by dma_request_channel
779c13c8260SChris Leech  * @channels: the list of struct dma_chan
780c13c8260SChris Leech  * @global_node: list_head for global dma_device_list
781a8135d0dSPeter Ujfalusi  * @filter: information for device/slave to filter function/param mapping
7827405f74bSDan Williams  * @cap_mask: one or more dma_capability flags
7834db8fd32SPeter Ujfalusi  * @desc_metadata_modes: supported metadata modes by the DMA device
7847405f74bSDan Williams  * @max_xor: maximum number of xor sources, 0 if no capability
785b2f46fd8SDan Williams  * @max_pq: maximum number of PQ sources and PQ-continue capability
78683544ae9SDan Williams  * @copy_align: alignment shift for memcpy operations
78783544ae9SDan Williams  * @xor_align: alignment shift for xor operations
78883544ae9SDan Williams  * @pq_align: alignment shift for pq operations
7894983a501SMaxime Ripard  * @fill_align: alignment shift for memset operations
790fe4ada2dSRandy Dunlap  * @dev_id: unique device ID
7917405f74bSDan Williams  * @dev: struct device reference for dma mapping api
792dae7a589SLogan Gunthorpe  * @owner: owner module (automatically set based on the provided dev)
793cb8cea51SMaxime Ripard  * @src_addr_widths: bit mask of src addr widths the device supports
794c2cbd427SStefan Brüns  *	Width is specified in bytes, e.g. for a device supporting
795c2cbd427SStefan Brüns  *	a width of 4 the mask should have BIT(4) set.
796cb8cea51SMaxime Ripard  * @dst_addr_widths: bit mask of dst addr widths the device supports
797c2cbd427SStefan Brüns  * @directions: bit mask of slave directions the device supports.
798c2cbd427SStefan Brüns  *	Since the enum dma_transfer_direction is not defined as bit flag for
799c2cbd427SStefan Brüns  *	each type, the dma controller should set BIT(<TYPE>) and same
800c2cbd427SStefan Brüns  *	should be checked by controller as well
801d97758e0SSerge Semin  * @min_burst: min burst capability per-transfer
8026d5bbed3SShawn Lin  * @max_burst: max burst capability per-transfer
803b1b40b8fSSerge Semin  * @max_sg_burst: max number of SG list entries executed in a single burst
804b1b40b8fSSerge Semin  *	DMA tansaction with no software intervention for reinitialization.
805b1b40b8fSSerge Semin  *	Zero value means unlimited number of entries.
806cb8cea51SMaxime Ripard  * @residue_granularity: granularity of the transfer residue reported
807cb8cea51SMaxime Ripard  *	by tx_status
808fe4ada2dSRandy Dunlap  * @device_alloc_chan_resources: allocate resources and return the
809fe4ada2dSRandy Dunlap  *	number of allocated descriptors
8104f910c03SPeter Ujfalusi  * @device_router_config: optional callback for DMA router configuration
811fe4ada2dSRandy Dunlap  * @device_free_chan_resources: release DMA channel's resources
8127405f74bSDan Williams  * @device_prep_dma_memcpy: prepares a memcpy operation
8137405f74bSDan Williams  * @device_prep_dma_xor: prepares a xor operation
814099f53cbSDan Williams  * @device_prep_dma_xor_val: prepares a xor validation operation
815b2f46fd8SDan Williams  * @device_prep_dma_pq: prepares a pq operation
816b2f46fd8SDan Williams  * @device_prep_dma_pq_val: prepares a pqzero_sum operation
8174983a501SMaxime Ripard  * @device_prep_dma_memset: prepares a memset operation
81850c7cd2bSMaxime Ripard  * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list
8197405f74bSDan Williams  * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
820dc0ee643SHaavard Skinnemoen  * @device_prep_slave_sg: prepares a slave dma operation
821782bc950SSascha Hauer  * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
822782bc950SSascha Hauer  *	The function takes a buffer of size buf_len. The callback function will
823782bc950SSascha Hauer  *	be called after period_len bytes have been transferred.
824b14dab79SJassi Brar  * @device_prep_interleaved_dma: Transfer expression in a generic way.
825ff39988aSSiva Yerramreddy  * @device_prep_dma_imm_data: DMA's 8 byte immediate data to the dst address
8263b6d694eSSerge Semin  * @device_caps: May be used to override the generic DMA slave capabilities
8273b6d694eSSerge Semin  *	with per-channel specific ones
82894a73e30SMaxime Ripard  * @device_config: Pushes a new configuration to a channel, return 0 or an error
82994a73e30SMaxime Ripard  *	code
83023a3ea2fSMaxime Ripard  * @device_pause: Pauses any transfer happening on a channel. Returns
83123a3ea2fSMaxime Ripard  *	0 or an error code
83223a3ea2fSMaxime Ripard  * @device_resume: Resumes any transfer on a channel previously
83323a3ea2fSMaxime Ripard  *	paused. Returns 0 or an error code
8347fa0cf46SMaxime Ripard  * @device_terminate_all: Aborts all transfers on a channel. Returns 0
8357fa0cf46SMaxime Ripard  *	or an error code
836b36f09c3SLars-Peter Clausen  * @device_synchronize: Synchronizes the termination of a transfers to the
837b36f09c3SLars-Peter Clausen  *  current context.
83807934481SLinus Walleij  * @device_tx_status: poll for transaction completion, the optional
83907934481SLinus Walleij  *	txstate parameter can be supplied with a pointer to get a
84025985edcSLucas De Marchi  *	struct with auxiliary transfer status information, otherwise the call
84107934481SLinus Walleij  *	will just return a simple status code
8427405f74bSDan Williams  * @device_issue_pending: push pending transactions to hardware
8439eeacd3aSRobert Jarzmik  * @descriptor_reuse: a submitted transfer can be resubmitted after completion
8448ad342a8SLogan Gunthorpe  * @device_release: called sometime atfer dma_async_device_unregister() is
8458ad342a8SLogan Gunthorpe  *     called and there are no further references to this structure. This
8468ad342a8SLogan Gunthorpe  *     must be implemented to free resources however many existing drivers
8478ad342a8SLogan Gunthorpe  *     do not and are therefore not safe to unbind while in use.
848e937cc1dSPeter Ujfalusi  * @dbg_summary_show: optional routine to show contents in debugfs; default code
849e937cc1dSPeter Ujfalusi  *     will be used when this is omitted, but custom code can show extra,
850e937cc1dSPeter Ujfalusi  *     controller specific information.
851c13c8260SChris Leech  */
852c13c8260SChris Leech struct dma_device {
8538ad342a8SLogan Gunthorpe 	struct kref ref;
854c13c8260SChris Leech 	unsigned int chancnt;
8550f571515SAtsushi Nemoto 	unsigned int privatecnt;
856c13c8260SChris Leech 	struct list_head channels;
857c13c8260SChris Leech 	struct list_head global_node;
858a8135d0dSPeter Ujfalusi 	struct dma_filter filter;
8597405f74bSDan Williams 	dma_cap_mask_t  cap_mask;
8604db8fd32SPeter Ujfalusi 	enum dma_desc_metadata_mode desc_metadata_modes;
861b2f46fd8SDan Williams 	unsigned short max_xor;
862b2f46fd8SDan Williams 	unsigned short max_pq;
86377a68e56SMaxime Ripard 	enum dmaengine_alignment copy_align;
86477a68e56SMaxime Ripard 	enum dmaengine_alignment xor_align;
86577a68e56SMaxime Ripard 	enum dmaengine_alignment pq_align;
86677a68e56SMaxime Ripard 	enum dmaengine_alignment fill_align;
867b2f46fd8SDan Williams 	#define DMA_HAS_PQ_CONTINUE (1 << 15)
868c13c8260SChris Leech 
869c13c8260SChris Leech 	int dev_id;
8707405f74bSDan Williams 	struct device *dev;
871dae7a589SLogan Gunthorpe 	struct module *owner;
87208210094SDave Jiang 	struct ida chan_ida;
873c13c8260SChris Leech 
874cb8cea51SMaxime Ripard 	u32 src_addr_widths;
875cb8cea51SMaxime Ripard 	u32 dst_addr_widths;
876cb8cea51SMaxime Ripard 	u32 directions;
877d97758e0SSerge Semin 	u32 min_burst;
8786d5bbed3SShawn Lin 	u32 max_burst;
879b1b40b8fSSerge Semin 	u32 max_sg_burst;
8809eeacd3aSRobert Jarzmik 	bool descriptor_reuse;
881cb8cea51SMaxime Ripard 	enum dma_residue_granularity residue_granularity;
882cb8cea51SMaxime Ripard 
883aa1e6f1aSDan Williams 	int (*device_alloc_chan_resources)(struct dma_chan *chan);
8844f910c03SPeter Ujfalusi 	int (*device_router_config)(struct dma_chan *chan);
885c13c8260SChris Leech 	void (*device_free_chan_resources)(struct dma_chan *chan);
8867405f74bSDan Williams 
8877405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
888ceacbdbfSMaxime Ripard 		struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
889d4c56f97SDan Williams 		size_t len, unsigned long flags);
8903218910fSAdrian Larumbe 	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy_sg)(
8913218910fSAdrian Larumbe 		struct dma_chan *chan,
8923218910fSAdrian Larumbe 		struct scatterlist *dst_sg, unsigned int dst_nents,
8933218910fSAdrian Larumbe 		struct scatterlist *src_sg, unsigned int src_nents,
8943218910fSAdrian Larumbe 		unsigned long flags);
8957405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
896ceacbdbfSMaxime Ripard 		struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
897d4c56f97SDan Williams 		unsigned int src_cnt, size_t len, unsigned long flags);
898099f53cbSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
8990036731cSDan Williams 		struct dma_chan *chan, dma_addr_t *src,	unsigned int src_cnt,
900ad283ea4SDan Williams 		size_t len, enum sum_check_flags *result, unsigned long flags);
901b2f46fd8SDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
902b2f46fd8SDan Williams 		struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
903b2f46fd8SDan Williams 		unsigned int src_cnt, const unsigned char *scf,
904b2f46fd8SDan Williams 		size_t len, unsigned long flags);
905b2f46fd8SDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
906b2f46fd8SDan Williams 		struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
907b2f46fd8SDan Williams 		unsigned int src_cnt, const unsigned char *scf, size_t len,
908b2f46fd8SDan Williams 		enum sum_check_flags *pqres, unsigned long flags);
9094983a501SMaxime Ripard 	struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
9104983a501SMaxime Ripard 		struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
9114983a501SMaxime Ripard 		unsigned long flags);
91250c7cd2bSMaxime Ripard 	struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)(
91350c7cd2bSMaxime Ripard 		struct dma_chan *chan, struct scatterlist *sg,
91450c7cd2bSMaxime Ripard 		unsigned int nents, int value, unsigned long flags);
9157405f74bSDan Williams 	struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
916636bdeaaSDan Williams 		struct dma_chan *chan, unsigned long flags);
9177405f74bSDan Williams 
918dc0ee643SHaavard Skinnemoen 	struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
919dc0ee643SHaavard Skinnemoen 		struct dma_chan *chan, struct scatterlist *sgl,
92049920bc6SVinod Koul 		unsigned int sg_len, enum dma_transfer_direction direction,
921185ecb5fSAlexandre Bounine 		unsigned long flags, void *context);
922782bc950SSascha Hauer 	struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
923782bc950SSascha Hauer 		struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
924185ecb5fSAlexandre Bounine 		size_t period_len, enum dma_transfer_direction direction,
92531c1e5a1SLaurent Pinchart 		unsigned long flags);
926b14dab79SJassi Brar 	struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
927b14dab79SJassi Brar 		struct dma_chan *chan, struct dma_interleaved_template *xt,
928b14dab79SJassi Brar 		unsigned long flags);
929ff39988aSSiva Yerramreddy 	struct dma_async_tx_descriptor *(*device_prep_dma_imm_data)(
930ff39988aSSiva Yerramreddy 		struct dma_chan *chan, dma_addr_t dst, u64 data,
931ff39988aSSiva Yerramreddy 		unsigned long flags);
93294a73e30SMaxime Ripard 
9333b6d694eSSerge Semin 	void (*device_caps)(struct dma_chan *chan,
9343b6d694eSSerge Semin 			    struct dma_slave_caps *caps);
93594a73e30SMaxime Ripard 	int (*device_config)(struct dma_chan *chan,
93694a73e30SMaxime Ripard 			     struct dma_slave_config *config);
93723a3ea2fSMaxime Ripard 	int (*device_pause)(struct dma_chan *chan);
93823a3ea2fSMaxime Ripard 	int (*device_resume)(struct dma_chan *chan);
9397fa0cf46SMaxime Ripard 	int (*device_terminate_all)(struct dma_chan *chan);
940b36f09c3SLars-Peter Clausen 	void (*device_synchronize)(struct dma_chan *chan);
941dc0ee643SHaavard Skinnemoen 
94207934481SLinus Walleij 	enum dma_status (*device_tx_status)(struct dma_chan *chan,
94307934481SLinus Walleij 					    dma_cookie_t cookie,
94407934481SLinus Walleij 					    struct dma_tx_state *txstate);
9457405f74bSDan Williams 	void (*device_issue_pending)(struct dma_chan *chan);
9468ad342a8SLogan Gunthorpe 	void (*device_release)(struct dma_device *dev);
947e937cc1dSPeter Ujfalusi 	/* debugfs support */
948e937cc1dSPeter Ujfalusi 	void (*dbg_summary_show)(struct seq_file *s, struct dma_device *dev);
94926cf132dSPeter Ujfalusi 	struct dentry *dbg_dev_root;
950c13c8260SChris Leech };
951c13c8260SChris Leech 
9526e3ecaf0SSascha Hauer static inline int dmaengine_slave_config(struct dma_chan *chan,
9536e3ecaf0SSascha Hauer 					  struct dma_slave_config *config)
9546e3ecaf0SSascha Hauer {
95594a73e30SMaxime Ripard 	if (chan->device->device_config)
95694a73e30SMaxime Ripard 		return chan->device->device_config(chan, config);
95794a73e30SMaxime Ripard 
9582c44ad91SMaxime Ripard 	return -ENOSYS;
9596e3ecaf0SSascha Hauer }
9606e3ecaf0SSascha Hauer 
96161cc13a5SAndy Shevchenko static inline bool is_slave_direction(enum dma_transfer_direction direction)
96261cc13a5SAndy Shevchenko {
96361cc13a5SAndy Shevchenko 	return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM);
96461cc13a5SAndy Shevchenko }
96561cc13a5SAndy Shevchenko 
96690b44f8fSVinod Koul static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
967922ee08bSKuninori Morimoto 	struct dma_chan *chan, dma_addr_t buf, size_t len,
96849920bc6SVinod Koul 	enum dma_transfer_direction dir, unsigned long flags)
96990b44f8fSVinod Koul {
97090b44f8fSVinod Koul 	struct scatterlist sg;
971922ee08bSKuninori Morimoto 	sg_init_table(&sg, 1);
972922ee08bSKuninori Morimoto 	sg_dma_address(&sg) = buf;
973922ee08bSKuninori Morimoto 	sg_dma_len(&sg) = len;
97490b44f8fSVinod Koul 
975757d12e5SVinod Koul 	if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
976757d12e5SVinod Koul 		return NULL;
977757d12e5SVinod Koul 
978185ecb5fSAlexandre Bounine 	return chan->device->device_prep_slave_sg(chan, &sg, 1,
979185ecb5fSAlexandre Bounine 						  dir, flags, NULL);
98090b44f8fSVinod Koul }
98190b44f8fSVinod Koul 
98216052827SAlexandre Bounine static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
98316052827SAlexandre Bounine 	struct dma_chan *chan, struct scatterlist *sgl,	unsigned int sg_len,
98416052827SAlexandre Bounine 	enum dma_transfer_direction dir, unsigned long flags)
98516052827SAlexandre Bounine {
986757d12e5SVinod Koul 	if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
987757d12e5SVinod Koul 		return NULL;
988757d12e5SVinod Koul 
98916052827SAlexandre Bounine 	return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
990185ecb5fSAlexandre Bounine 						  dir, flags, NULL);
99116052827SAlexandre Bounine }
99216052827SAlexandre Bounine 
993e42d98ebSAlexandre Bounine #ifdef CONFIG_RAPIDIO_DMA_ENGINE
994e42d98ebSAlexandre Bounine struct rio_dma_ext;
995e42d98ebSAlexandre Bounine static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
996e42d98ebSAlexandre Bounine 	struct dma_chan *chan, struct scatterlist *sgl,	unsigned int sg_len,
997e42d98ebSAlexandre Bounine 	enum dma_transfer_direction dir, unsigned long flags,
998e42d98ebSAlexandre Bounine 	struct rio_dma_ext *rio_ext)
999e42d98ebSAlexandre Bounine {
1000757d12e5SVinod Koul 	if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
1001757d12e5SVinod Koul 		return NULL;
1002757d12e5SVinod Koul 
1003e42d98ebSAlexandre Bounine 	return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
1004e42d98ebSAlexandre Bounine 						  dir, flags, rio_ext);
1005e42d98ebSAlexandre Bounine }
1006e42d98ebSAlexandre Bounine #endif
1007e42d98ebSAlexandre Bounine 
100816052827SAlexandre Bounine static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
100916052827SAlexandre Bounine 		struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1010e7736cdeSPeter Ujfalusi 		size_t period_len, enum dma_transfer_direction dir,
1011e7736cdeSPeter Ujfalusi 		unsigned long flags)
101216052827SAlexandre Bounine {
1013757d12e5SVinod Koul 	if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic)
1014757d12e5SVinod Koul 		return NULL;
1015757d12e5SVinod Koul 
101616052827SAlexandre Bounine 	return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
101731c1e5a1SLaurent Pinchart 						period_len, dir, flags);
10186e3ecaf0SSascha Hauer }
10196e3ecaf0SSascha Hauer 
1020a14acb4aSBarry Song static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
1021a14acb4aSBarry Song 		struct dma_chan *chan, struct dma_interleaved_template *xt,
1022a14acb4aSBarry Song 		unsigned long flags)
1023a14acb4aSBarry Song {
1024757d12e5SVinod Koul 	if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma)
1025757d12e5SVinod Koul 		return NULL;
10269c8ebd8bSLaurent Pinchart 	if (flags & DMA_PREP_REPEAT &&
10279c8ebd8bSLaurent Pinchart 	    !test_bit(DMA_REPEAT, chan->device->cap_mask.bits))
10289c8ebd8bSLaurent Pinchart 		return NULL;
1029757d12e5SVinod Koul 
1030a14acb4aSBarry Song 	return chan->device->device_prep_interleaved_dma(chan, xt, flags);
1031a14acb4aSBarry Song }
1032a14acb4aSBarry Song 
1033*fc44ff0aSBen Walker /**
1034*fc44ff0aSBen Walker  * dmaengine_prep_dma_memset() - Prepare a DMA memset descriptor.
1035*fc44ff0aSBen Walker  * @chan: The channel to be used for this descriptor
1036*fc44ff0aSBen Walker  * @dest: Address of buffer to be set
1037*fc44ff0aSBen Walker  * @value: Treated as a single byte value that fills the destination buffer
1038*fc44ff0aSBen Walker  * @len: The total size of dest
1039*fc44ff0aSBen Walker  * @flags: DMA engine flags
1040*fc44ff0aSBen Walker  */
10414983a501SMaxime Ripard static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
10424983a501SMaxime Ripard 		struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
10434983a501SMaxime Ripard 		unsigned long flags)
10444983a501SMaxime Ripard {
1045757d12e5SVinod Koul 	if (!chan || !chan->device || !chan->device->device_prep_dma_memset)
10464983a501SMaxime Ripard 		return NULL;
10474983a501SMaxime Ripard 
10484983a501SMaxime Ripard 	return chan->device->device_prep_dma_memset(chan, dest, value,
10494983a501SMaxime Ripard 						    len, flags);
10504983a501SMaxime Ripard }
10514983a501SMaxime Ripard 
105277d65d6fSBoris Brezillon static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
105377d65d6fSBoris Brezillon 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
105477d65d6fSBoris Brezillon 		size_t len, unsigned long flags)
105577d65d6fSBoris Brezillon {
105677d65d6fSBoris Brezillon 	if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy)
105777d65d6fSBoris Brezillon 		return NULL;
105877d65d6fSBoris Brezillon 
105977d65d6fSBoris Brezillon 	return chan->device->device_prep_dma_memcpy(chan, dest, src,
106077d65d6fSBoris Brezillon 						    len, flags);
106177d65d6fSBoris Brezillon }
106277d65d6fSBoris Brezillon 
10633218910fSAdrian Larumbe static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy_sg(
10643218910fSAdrian Larumbe 		struct dma_chan *chan,
10653218910fSAdrian Larumbe 		struct scatterlist *dst_sg, unsigned int dst_nents,
10663218910fSAdrian Larumbe 		struct scatterlist *src_sg, unsigned int src_nents,
10673218910fSAdrian Larumbe 		unsigned long flags)
10683218910fSAdrian Larumbe {
10693218910fSAdrian Larumbe 	if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy_sg)
10703218910fSAdrian Larumbe 		return NULL;
10713218910fSAdrian Larumbe 
10723218910fSAdrian Larumbe 	return chan->device->device_prep_dma_memcpy_sg(chan, dst_sg, dst_nents,
10733218910fSAdrian Larumbe 						       src_sg, src_nents,
10743218910fSAdrian Larumbe 						       flags);
10753218910fSAdrian Larumbe }
10763218910fSAdrian Larumbe 
10774db8fd32SPeter Ujfalusi static inline bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan,
10784db8fd32SPeter Ujfalusi 		enum dma_desc_metadata_mode mode)
10794db8fd32SPeter Ujfalusi {
10804db8fd32SPeter Ujfalusi 	if (!chan)
10814db8fd32SPeter Ujfalusi 		return false;
10824db8fd32SPeter Ujfalusi 
10834db8fd32SPeter Ujfalusi 	return !!(chan->device->desc_metadata_modes & mode);
10844db8fd32SPeter Ujfalusi }
10854db8fd32SPeter Ujfalusi 
10864db8fd32SPeter Ujfalusi #ifdef CONFIG_DMA_ENGINE
10874db8fd32SPeter Ujfalusi int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
10884db8fd32SPeter Ujfalusi 				   void *data, size_t len);
10894db8fd32SPeter Ujfalusi void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
10904db8fd32SPeter Ujfalusi 				      size_t *payload_len, size_t *max_len);
10914db8fd32SPeter Ujfalusi int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
10924db8fd32SPeter Ujfalusi 				    size_t payload_len);
10934db8fd32SPeter Ujfalusi #else /* CONFIG_DMA_ENGINE */
10944db8fd32SPeter Ujfalusi static inline int dmaengine_desc_attach_metadata(
10954db8fd32SPeter Ujfalusi 		struct dma_async_tx_descriptor *desc, void *data, size_t len)
10964db8fd32SPeter Ujfalusi {
10974db8fd32SPeter Ujfalusi 	return -EINVAL;
10984db8fd32SPeter Ujfalusi }
10994db8fd32SPeter Ujfalusi static inline void *dmaengine_desc_get_metadata_ptr(
11004db8fd32SPeter Ujfalusi 		struct dma_async_tx_descriptor *desc, size_t *payload_len,
11014db8fd32SPeter Ujfalusi 		size_t *max_len)
11024db8fd32SPeter Ujfalusi {
11034db8fd32SPeter Ujfalusi 	return NULL;
11044db8fd32SPeter Ujfalusi }
11054db8fd32SPeter Ujfalusi static inline int dmaengine_desc_set_metadata_len(
11064db8fd32SPeter Ujfalusi 		struct dma_async_tx_descriptor *desc, size_t payload_len)
11074db8fd32SPeter Ujfalusi {
11084db8fd32SPeter Ujfalusi 	return -EINVAL;
11094db8fd32SPeter Ujfalusi }
11104db8fd32SPeter Ujfalusi #endif /* CONFIG_DMA_ENGINE */
11114db8fd32SPeter Ujfalusi 
1112b36f09c3SLars-Peter Clausen /**
1113b36f09c3SLars-Peter Clausen  * dmaengine_terminate_all() - Terminate all active DMA transfers
1114b36f09c3SLars-Peter Clausen  * @chan: The channel for which to terminate the transfers
1115b36f09c3SLars-Peter Clausen  *
1116b36f09c3SLars-Peter Clausen  * This function is DEPRECATED use either dmaengine_terminate_sync() or
1117b36f09c3SLars-Peter Clausen  * dmaengine_terminate_async() instead.
1118b36f09c3SLars-Peter Clausen  */
11196e3ecaf0SSascha Hauer static inline int dmaengine_terminate_all(struct dma_chan *chan)
11206e3ecaf0SSascha Hauer {
11217fa0cf46SMaxime Ripard 	if (chan->device->device_terminate_all)
11227fa0cf46SMaxime Ripard 		return chan->device->device_terminate_all(chan);
11237fa0cf46SMaxime Ripard 
11242c44ad91SMaxime Ripard 	return -ENOSYS;
11256e3ecaf0SSascha Hauer }
11266e3ecaf0SSascha Hauer 
1127b36f09c3SLars-Peter Clausen /**
1128b36f09c3SLars-Peter Clausen  * dmaengine_terminate_async() - Terminate all active DMA transfers
1129b36f09c3SLars-Peter Clausen  * @chan: The channel for which to terminate the transfers
1130b36f09c3SLars-Peter Clausen  *
1131b36f09c3SLars-Peter Clausen  * Calling this function will terminate all active and pending descriptors
1132b36f09c3SLars-Peter Clausen  * that have previously been submitted to the channel. It is not guaranteed
1133b36f09c3SLars-Peter Clausen  * though that the transfer for the active descriptor has stopped when the
1134b36f09c3SLars-Peter Clausen  * function returns. Furthermore it is possible the complete callback of a
1135b36f09c3SLars-Peter Clausen  * submitted transfer is still running when this function returns.
1136b36f09c3SLars-Peter Clausen  *
1137b36f09c3SLars-Peter Clausen  * dmaengine_synchronize() needs to be called before it is safe to free
1138b36f09c3SLars-Peter Clausen  * any memory that is accessed by previously submitted descriptors or before
1139b36f09c3SLars-Peter Clausen  * freeing any resources accessed from within the completion callback of any
114020d60f63SMaciej Grochowski  * previously submitted descriptors.
1141b36f09c3SLars-Peter Clausen  *
1142b36f09c3SLars-Peter Clausen  * This function can be called from atomic context as well as from within a
1143b36f09c3SLars-Peter Clausen  * complete callback of a descriptor submitted on the same channel.
1144b36f09c3SLars-Peter Clausen  *
1145b36f09c3SLars-Peter Clausen  * If none of the two conditions above apply consider using
1146b36f09c3SLars-Peter Clausen  * dmaengine_terminate_sync() instead.
1147b36f09c3SLars-Peter Clausen  */
1148b36f09c3SLars-Peter Clausen static inline int dmaengine_terminate_async(struct dma_chan *chan)
1149b36f09c3SLars-Peter Clausen {
1150b36f09c3SLars-Peter Clausen 	if (chan->device->device_terminate_all)
1151b36f09c3SLars-Peter Clausen 		return chan->device->device_terminate_all(chan);
1152b36f09c3SLars-Peter Clausen 
1153b36f09c3SLars-Peter Clausen 	return -EINVAL;
1154b36f09c3SLars-Peter Clausen }
1155b36f09c3SLars-Peter Clausen 
1156b36f09c3SLars-Peter Clausen /**
1157b36f09c3SLars-Peter Clausen  * dmaengine_synchronize() - Synchronize DMA channel termination
1158b36f09c3SLars-Peter Clausen  * @chan: The channel to synchronize
1159b36f09c3SLars-Peter Clausen  *
1160b36f09c3SLars-Peter Clausen  * Synchronizes to the DMA channel termination to the current context. When this
1161b36f09c3SLars-Peter Clausen  * function returns it is guaranteed that all transfers for previously issued
116220d60f63SMaciej Grochowski  * descriptors have stopped and it is safe to free the memory associated
1163b36f09c3SLars-Peter Clausen  * with them. Furthermore it is guaranteed that all complete callback functions
1164b36f09c3SLars-Peter Clausen  * for a previously submitted descriptor have finished running and it is safe to
1165b36f09c3SLars-Peter Clausen  * free resources accessed from within the complete callbacks.
1166b36f09c3SLars-Peter Clausen  *
1167b36f09c3SLars-Peter Clausen  * The behavior of this function is undefined if dma_async_issue_pending() has
1168b36f09c3SLars-Peter Clausen  * been called between dmaengine_terminate_async() and this function.
1169b36f09c3SLars-Peter Clausen  *
1170b36f09c3SLars-Peter Clausen  * This function must only be called from non-atomic context and must not be
1171b36f09c3SLars-Peter Clausen  * called from within a complete callback of a descriptor submitted on the same
1172b36f09c3SLars-Peter Clausen  * channel.
1173b36f09c3SLars-Peter Clausen  */
1174b36f09c3SLars-Peter Clausen static inline void dmaengine_synchronize(struct dma_chan *chan)
1175b36f09c3SLars-Peter Clausen {
1176b1d6ab1aSLars-Peter Clausen 	might_sleep();
1177b1d6ab1aSLars-Peter Clausen 
1178b36f09c3SLars-Peter Clausen 	if (chan->device->device_synchronize)
1179b36f09c3SLars-Peter Clausen 		chan->device->device_synchronize(chan);
1180b36f09c3SLars-Peter Clausen }
1181b36f09c3SLars-Peter Clausen 
1182b36f09c3SLars-Peter Clausen /**
1183b36f09c3SLars-Peter Clausen  * dmaengine_terminate_sync() - Terminate all active DMA transfers
1184b36f09c3SLars-Peter Clausen  * @chan: The channel for which to terminate the transfers
1185b36f09c3SLars-Peter Clausen  *
1186b36f09c3SLars-Peter Clausen  * Calling this function will terminate all active and pending transfers
1187b36f09c3SLars-Peter Clausen  * that have previously been submitted to the channel. It is similar to
1188b36f09c3SLars-Peter Clausen  * dmaengine_terminate_async() but guarantees that the DMA transfer has actually
1189b36f09c3SLars-Peter Clausen  * stopped and that all complete callbacks have finished running when the
1190b36f09c3SLars-Peter Clausen  * function returns.
1191b36f09c3SLars-Peter Clausen  *
1192b36f09c3SLars-Peter Clausen  * This function must only be called from non-atomic context and must not be
1193b36f09c3SLars-Peter Clausen  * called from within a complete callback of a descriptor submitted on the same
1194b36f09c3SLars-Peter Clausen  * channel.
1195b36f09c3SLars-Peter Clausen  */
1196b36f09c3SLars-Peter Clausen static inline int dmaengine_terminate_sync(struct dma_chan *chan)
1197b36f09c3SLars-Peter Clausen {
1198b36f09c3SLars-Peter Clausen 	int ret;
1199b36f09c3SLars-Peter Clausen 
1200b36f09c3SLars-Peter Clausen 	ret = dmaengine_terminate_async(chan);
1201b36f09c3SLars-Peter Clausen 	if (ret)
1202b36f09c3SLars-Peter Clausen 		return ret;
1203b36f09c3SLars-Peter Clausen 
1204b36f09c3SLars-Peter Clausen 	dmaengine_synchronize(chan);
1205b36f09c3SLars-Peter Clausen 
1206b36f09c3SLars-Peter Clausen 	return 0;
1207b36f09c3SLars-Peter Clausen }
1208b36f09c3SLars-Peter Clausen 
12096e3ecaf0SSascha Hauer static inline int dmaengine_pause(struct dma_chan *chan)
12106e3ecaf0SSascha Hauer {
121123a3ea2fSMaxime Ripard 	if (chan->device->device_pause)
121223a3ea2fSMaxime Ripard 		return chan->device->device_pause(chan);
121323a3ea2fSMaxime Ripard 
12142c44ad91SMaxime Ripard 	return -ENOSYS;
12156e3ecaf0SSascha Hauer }
12166e3ecaf0SSascha Hauer 
12176e3ecaf0SSascha Hauer static inline int dmaengine_resume(struct dma_chan *chan)
12186e3ecaf0SSascha Hauer {
121923a3ea2fSMaxime Ripard 	if (chan->device->device_resume)
122023a3ea2fSMaxime Ripard 		return chan->device->device_resume(chan);
122123a3ea2fSMaxime Ripard 
12222c44ad91SMaxime Ripard 	return -ENOSYS;
12236e3ecaf0SSascha Hauer }
12246e3ecaf0SSascha Hauer 
12253052cc2cSLars-Peter Clausen static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
12263052cc2cSLars-Peter Clausen 	dma_cookie_t cookie, struct dma_tx_state *state)
12273052cc2cSLars-Peter Clausen {
12283052cc2cSLars-Peter Clausen 	return chan->device->device_tx_status(chan, cookie, state);
12293052cc2cSLars-Peter Clausen }
12303052cc2cSLars-Peter Clausen 
123198d530feSRussell King - ARM Linux static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
12326e3ecaf0SSascha Hauer {
12336e3ecaf0SSascha Hauer 	return desc->tx_submit(desc);
12346e3ecaf0SSascha Hauer }
12356e3ecaf0SSascha Hauer 
123677a68e56SMaxime Ripard static inline bool dmaengine_check_align(enum dmaengine_alignment align,
123777a68e56SMaxime Ripard 					 size_t off1, size_t off2, size_t len)
123883544ae9SDan Williams {
123988ac039cSAndy Shevchenko 	return !(((1 << align) - 1) & (off1 | off2 | len));
124083544ae9SDan Williams }
124183544ae9SDan Williams 
124283544ae9SDan Williams static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
124383544ae9SDan Williams 				       size_t off2, size_t len)
124483544ae9SDan Williams {
124583544ae9SDan Williams 	return dmaengine_check_align(dev->copy_align, off1, off2, len);
124683544ae9SDan Williams }
124783544ae9SDan Williams 
124883544ae9SDan Williams static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
124983544ae9SDan Williams 				      size_t off2, size_t len)
125083544ae9SDan Williams {
125183544ae9SDan Williams 	return dmaengine_check_align(dev->xor_align, off1, off2, len);
125283544ae9SDan Williams }
125383544ae9SDan Williams 
125483544ae9SDan Williams static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
125583544ae9SDan Williams 				     size_t off2, size_t len)
125683544ae9SDan Williams {
125783544ae9SDan Williams 	return dmaengine_check_align(dev->pq_align, off1, off2, len);
125883544ae9SDan Williams }
125983544ae9SDan Williams 
12604983a501SMaxime Ripard static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
12614983a501SMaxime Ripard 				       size_t off2, size_t len)
12624983a501SMaxime Ripard {
12634983a501SMaxime Ripard 	return dmaengine_check_align(dev->fill_align, off1, off2, len);
12644983a501SMaxime Ripard }
12654983a501SMaxime Ripard 
1266b2f46fd8SDan Williams static inline void
1267b2f46fd8SDan Williams dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
1268b2f46fd8SDan Williams {
1269b2f46fd8SDan Williams 	dma->max_pq = maxpq;
1270b2f46fd8SDan Williams 	if (has_pq_continue)
1271b2f46fd8SDan Williams 		dma->max_pq |= DMA_HAS_PQ_CONTINUE;
1272b2f46fd8SDan Williams }
1273b2f46fd8SDan Williams 
1274b2f46fd8SDan Williams static inline bool dmaf_continue(enum dma_ctrl_flags flags)
1275b2f46fd8SDan Williams {
1276b2f46fd8SDan Williams 	return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
1277b2f46fd8SDan Williams }
1278b2f46fd8SDan Williams 
1279b2f46fd8SDan Williams static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
1280b2f46fd8SDan Williams {
1281b2f46fd8SDan Williams 	enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
1282b2f46fd8SDan Williams 
1283b2f46fd8SDan Williams 	return (flags & mask) == mask;
1284b2f46fd8SDan Williams }
1285b2f46fd8SDan Williams 
1286b2f46fd8SDan Williams static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
1287b2f46fd8SDan Williams {
1288b2f46fd8SDan Williams 	return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
1289b2f46fd8SDan Williams }
1290b2f46fd8SDan Williams 
1291d3f3cf85SMathieu Lacage static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
1292b2f46fd8SDan Williams {
1293b2f46fd8SDan Williams 	return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
1294b2f46fd8SDan Williams }
1295b2f46fd8SDan Williams 
1296b2f46fd8SDan Williams /* dma_maxpq - reduce maxpq in the face of continued operations
1297b2f46fd8SDan Williams  * @dma - dma device with PQ capability
1298b2f46fd8SDan Williams  * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
1299b2f46fd8SDan Williams  *
1300b2f46fd8SDan Williams  * When an engine does not support native continuation we need 3 extra
1301b2f46fd8SDan Williams  * source slots to reuse P and Q with the following coefficients:
1302b2f46fd8SDan Williams  * 1/ {00} * P : remove P from Q', but use it as a source for P'
1303b2f46fd8SDan Williams  * 2/ {01} * Q : use Q to continue Q' calculation
1304b2f46fd8SDan Williams  * 3/ {00} * Q : subtract Q from P' to cancel (2)
1305b2f46fd8SDan Williams  *
1306b2f46fd8SDan Williams  * In the case where P is disabled we only need 1 extra source:
1307b2f46fd8SDan Williams  * 1/ {01} * Q : use Q to continue Q' calculation
1308b2f46fd8SDan Williams  */
1309b2f46fd8SDan Williams static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
1310b2f46fd8SDan Williams {
1311b2f46fd8SDan Williams 	if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
1312b2f46fd8SDan Williams 		return dma_dev_to_maxpq(dma);
13135f77dd85SAndy Shevchenko 	if (dmaf_p_disabled_continue(flags))
1314b2f46fd8SDan Williams 		return dma_dev_to_maxpq(dma) - 1;
13155f77dd85SAndy Shevchenko 	if (dmaf_continue(flags))
1316b2f46fd8SDan Williams 		return dma_dev_to_maxpq(dma) - 3;
1317b2f46fd8SDan Williams 	BUG();
1318b2f46fd8SDan Williams }
1319b2f46fd8SDan Williams 
132087d001efSMaxime Ripard static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
132187d001efSMaxime Ripard 				      size_t dir_icg)
132287d001efSMaxime Ripard {
132387d001efSMaxime Ripard 	if (inc) {
132487d001efSMaxime Ripard 		if (dir_icg)
132587d001efSMaxime Ripard 			return dir_icg;
13265f77dd85SAndy Shevchenko 		if (sgl)
132787d001efSMaxime Ripard 			return icg;
132887d001efSMaxime Ripard 	}
132987d001efSMaxime Ripard 
133087d001efSMaxime Ripard 	return 0;
133187d001efSMaxime Ripard }
133287d001efSMaxime Ripard 
133387d001efSMaxime Ripard static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt,
133487d001efSMaxime Ripard 					   struct data_chunk *chunk)
133587d001efSMaxime Ripard {
133687d001efSMaxime Ripard 	return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl,
133787d001efSMaxime Ripard 				 chunk->icg, chunk->dst_icg);
133887d001efSMaxime Ripard }
133987d001efSMaxime Ripard 
134087d001efSMaxime Ripard static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt,
134187d001efSMaxime Ripard 					   struct data_chunk *chunk)
134287d001efSMaxime Ripard {
134387d001efSMaxime Ripard 	return dmaengine_get_icg(xt->src_inc, xt->src_sgl,
134487d001efSMaxime Ripard 				 chunk->icg, chunk->src_icg);
134587d001efSMaxime Ripard }
134687d001efSMaxime Ripard 
1347c13c8260SChris Leech /* --- public DMA engine API --- */
1348c13c8260SChris Leech 
1349649274d9SDan Williams #ifdef CONFIG_DMA_ENGINE
1350209b84a8SDan Williams void dmaengine_get(void);
1351209b84a8SDan Williams void dmaengine_put(void);
1352649274d9SDan Williams #else
1353649274d9SDan Williams static inline void dmaengine_get(void)
1354649274d9SDan Williams {
1355649274d9SDan Williams }
1356649274d9SDan Williams static inline void dmaengine_put(void)
1357649274d9SDan Williams {
1358649274d9SDan Williams }
1359649274d9SDan Williams #endif
1360649274d9SDan Williams 
1361729b5d1bSDan Williams #ifdef CONFIG_ASYNC_TX_DMA
1362729b5d1bSDan Williams #define async_dmaengine_get()	dmaengine_get()
1363729b5d1bSDan Williams #define async_dmaengine_put()	dmaengine_put()
13645fc6d897SDan Williams #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
1365138f4c35SDan Williams #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
1366138f4c35SDan Williams #else
1367729b5d1bSDan Williams #define async_dma_find_channel(type) dma_find_channel(type)
13685fc6d897SDan Williams #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
1369729b5d1bSDan Williams #else
1370729b5d1bSDan Williams static inline void async_dmaengine_get(void)
1371729b5d1bSDan Williams {
1372729b5d1bSDan Williams }
1373729b5d1bSDan Williams static inline void async_dmaengine_put(void)
1374729b5d1bSDan Williams {
1375729b5d1bSDan Williams }
1376729b5d1bSDan Williams static inline struct dma_chan *
1377729b5d1bSDan Williams async_dma_find_channel(enum dma_transaction_type type)
1378729b5d1bSDan Williams {
1379729b5d1bSDan Williams 	return NULL;
1380729b5d1bSDan Williams }
1381138f4c35SDan Williams #endif /* CONFIG_ASYNC_TX_DMA */
13827405f74bSDan Williams void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
13837405f74bSDan Williams 				  struct dma_chan *chan);
1384c13c8260SChris Leech 
13850839875eSDan Williams static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
13867405f74bSDan Williams {
1387636bdeaaSDan Williams 	tx->flags |= DMA_CTRL_ACK;
1388636bdeaaSDan Williams }
1389636bdeaaSDan Williams 
1390ef560682SGuennadi Liakhovetski static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
1391ef560682SGuennadi Liakhovetski {
1392ef560682SGuennadi Liakhovetski 	tx->flags &= ~DMA_CTRL_ACK;
1393ef560682SGuennadi Liakhovetski }
1394ef560682SGuennadi Liakhovetski 
13950839875eSDan Williams static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
1396636bdeaaSDan Williams {
13970839875eSDan Williams 	return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
1398c13c8260SChris Leech }
1399c13c8260SChris Leech 
14007405f74bSDan Williams #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
14017405f74bSDan Williams static inline void
14027405f74bSDan Williams __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
14037405f74bSDan Williams {
14047405f74bSDan Williams 	set_bit(tx_type, dstp->bits);
14057405f74bSDan Williams }
14067405f74bSDan Williams 
14070f571515SAtsushi Nemoto #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
14080f571515SAtsushi Nemoto static inline void
14090f571515SAtsushi Nemoto __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
14100f571515SAtsushi Nemoto {
14110f571515SAtsushi Nemoto 	clear_bit(tx_type, dstp->bits);
14120f571515SAtsushi Nemoto }
14130f571515SAtsushi Nemoto 
141433df8ca0SDan Williams #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
141533df8ca0SDan Williams static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
141633df8ca0SDan Williams {
141733df8ca0SDan Williams 	bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
141833df8ca0SDan Williams }
141933df8ca0SDan Williams 
14207405f74bSDan Williams #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
14217405f74bSDan Williams static inline int
14227405f74bSDan Williams __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
14237405f74bSDan Williams {
14247405f74bSDan Williams 	return test_bit(tx_type, srcp->bits);
14257405f74bSDan Williams }
14267405f74bSDan Williams 
14277405f74bSDan Williams #define for_each_dma_cap_mask(cap, mask) \
1428e5a087fdSAkinobu Mita 	for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
14297405f74bSDan Williams 
1430c13c8260SChris Leech /**
14317405f74bSDan Williams  * dma_async_issue_pending - flush pending transactions to HW
1432fe4ada2dSRandy Dunlap  * @chan: target DMA channel
1433c13c8260SChris Leech  *
1434c13c8260SChris Leech  * This allows drivers to push copies to HW in batches,
1435c13c8260SChris Leech  * reducing MMIO writes where possible.
1436c13c8260SChris Leech  */
14377405f74bSDan Williams static inline void dma_async_issue_pending(struct dma_chan *chan)
1438c13c8260SChris Leech {
1439ec8670f1SDan Williams 	chan->device->device_issue_pending(chan);
1440c13c8260SChris Leech }
1441c13c8260SChris Leech 
1442c13c8260SChris Leech /**
14437405f74bSDan Williams  * dma_async_is_tx_complete - poll for transaction completion
1444c13c8260SChris Leech  * @chan: DMA channel
1445c13c8260SChris Leech  * @cookie: transaction identifier to check status of
1446c13c8260SChris Leech  * @last: returns last completed cookie, can be NULL
1447c13c8260SChris Leech  * @used: returns last issued cookie, can be NULL
1448c13c8260SChris Leech  *
1449c13c8260SChris Leech  * If @last and @used are passed in, upon return they reflect the driver
1450c13c8260SChris Leech  * internal state and can be used with dma_async_is_complete() to check
1451c13c8260SChris Leech  * the status of multiple cookies without re-checking hardware state.
1452c13c8260SChris Leech  */
14537405f74bSDan Williams static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
1454c13c8260SChris Leech 	dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
1455c13c8260SChris Leech {
145607934481SLinus Walleij 	struct dma_tx_state state;
145707934481SLinus Walleij 	enum dma_status status;
145807934481SLinus Walleij 
145907934481SLinus Walleij 	status = chan->device->device_tx_status(chan, cookie, &state);
146007934481SLinus Walleij 	if (last)
146107934481SLinus Walleij 		*last = state.last;
146207934481SLinus Walleij 	if (used)
146307934481SLinus Walleij 		*used = state.used;
146407934481SLinus Walleij 	return status;
1465c13c8260SChris Leech }
1466c13c8260SChris Leech 
1467c13c8260SChris Leech /**
1468c13c8260SChris Leech  * dma_async_is_complete - test a cookie against chan state
1469c13c8260SChris Leech  * @cookie: transaction identifier to test status of
1470c13c8260SChris Leech  * @last_complete: last know completed transaction
1471c13c8260SChris Leech  * @last_used: last cookie value handed out
1472c13c8260SChris Leech  *
1473e239345fSBartlomiej Zolnierkiewicz  * dma_async_is_complete() is used in dma_async_is_tx_complete()
14748a5703f8SSebastian Siewior  * the test logic is separated for lightweight testing of multiple cookies
1475c13c8260SChris Leech  */
1476c13c8260SChris Leech static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
1477c13c8260SChris Leech 			dma_cookie_t last_complete, dma_cookie_t last_used)
1478c13c8260SChris Leech {
1479c13c8260SChris Leech 	if (last_complete <= last_used) {
1480c13c8260SChris Leech 		if ((cookie <= last_complete) || (cookie > last_used))
1481adfedd9aSVinod Koul 			return DMA_COMPLETE;
1482c13c8260SChris Leech 	} else {
1483c13c8260SChris Leech 		if ((cookie <= last_complete) && (cookie > last_used))
1484adfedd9aSVinod Koul 			return DMA_COMPLETE;
1485c13c8260SChris Leech 	}
1486c13c8260SChris Leech 	return DMA_IN_PROGRESS;
1487c13c8260SChris Leech }
1488c13c8260SChris Leech 
1489bca34692SDan Williams static inline void
1490bca34692SDan Williams dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
1491bca34692SDan Williams {
14923a92063bSAndy Shevchenko 	if (!st)
14933a92063bSAndy Shevchenko 		return;
14943a92063bSAndy Shevchenko 
1495bca34692SDan Williams 	st->last = last;
1496bca34692SDan Williams 	st->used = used;
1497bca34692SDan Williams 	st->residue = residue;
1498bca34692SDan Williams }
1499bca34692SDan Williams 
150007f2211eSDan Williams #ifdef CONFIG_DMA_ENGINE
15014a43f394SJon Mason struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
15024a43f394SJon Mason enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
150307f2211eSDan Williams enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
1504c50331e8SDan Williams void dma_issue_pending_all(void);
1505a53e28daSLars-Peter Clausen struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1506f5151311SBaolin Wang 				       dma_filter_fn fn, void *fn_param,
1507f5151311SBaolin Wang 				       struct device_node *np);
1508a8135d0dSPeter Ujfalusi 
1509a8135d0dSPeter Ujfalusi struct dma_chan *dma_request_chan(struct device *dev, const char *name);
1510a8135d0dSPeter Ujfalusi struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
1511a8135d0dSPeter Ujfalusi 
15128f33d527SGuennadi Liakhovetski void dma_release_channel(struct dma_chan *chan);
1513fdb8df99SLaurent Pinchart int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
151407f2211eSDan Williams #else
15154a43f394SJon Mason static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
15164a43f394SJon Mason {
15174a43f394SJon Mason 	return NULL;
15184a43f394SJon Mason }
15194a43f394SJon Mason static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
15204a43f394SJon Mason {
1521adfedd9aSVinod Koul 	return DMA_COMPLETE;
15224a43f394SJon Mason }
152307f2211eSDan Williams static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
152407f2211eSDan Williams {
1525adfedd9aSVinod Koul 	return DMA_COMPLETE;
152607f2211eSDan Williams }
1527c50331e8SDan Williams static inline void dma_issue_pending_all(void)
1528c50331e8SDan Williams {
15298f33d527SGuennadi Liakhovetski }
1530a53e28daSLars-Peter Clausen static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1531f5151311SBaolin Wang 						     dma_filter_fn fn,
1532f5151311SBaolin Wang 						     void *fn_param,
1533f5151311SBaolin Wang 						     struct device_node *np)
15348f33d527SGuennadi Liakhovetski {
15358f33d527SGuennadi Liakhovetski 	return NULL;
15368f33d527SGuennadi Liakhovetski }
1537a8135d0dSPeter Ujfalusi static inline struct dma_chan *dma_request_chan(struct device *dev,
1538a8135d0dSPeter Ujfalusi 						const char *name)
1539a8135d0dSPeter Ujfalusi {
1540a8135d0dSPeter Ujfalusi 	return ERR_PTR(-ENODEV);
1541a8135d0dSPeter Ujfalusi }
1542a8135d0dSPeter Ujfalusi static inline struct dma_chan *dma_request_chan_by_mask(
1543a8135d0dSPeter Ujfalusi 						const dma_cap_mask_t *mask)
1544a8135d0dSPeter Ujfalusi {
1545a8135d0dSPeter Ujfalusi 	return ERR_PTR(-ENODEV);
1546a8135d0dSPeter Ujfalusi }
15478f33d527SGuennadi Liakhovetski static inline void dma_release_channel(struct dma_chan *chan)
15488f33d527SGuennadi Liakhovetski {
1549c50331e8SDan Williams }
1550fdb8df99SLaurent Pinchart static inline int dma_get_slave_caps(struct dma_chan *chan,
1551fdb8df99SLaurent Pinchart 				     struct dma_slave_caps *caps)
1552fdb8df99SLaurent Pinchart {
1553fdb8df99SLaurent Pinchart 	return -ENXIO;
1554fdb8df99SLaurent Pinchart }
155507f2211eSDan Williams #endif
1556c13c8260SChris Leech 
155727242021SVinod Koul static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
155827242021SVinod Koul {
155927242021SVinod Koul 	struct dma_slave_caps caps;
156053a256a9SLukas Wunner 	int ret;
156127242021SVinod Koul 
156253a256a9SLukas Wunner 	ret = dma_get_slave_caps(tx->chan, &caps);
156353a256a9SLukas Wunner 	if (ret)
156453a256a9SLukas Wunner 		return ret;
156527242021SVinod Koul 
15663a92063bSAndy Shevchenko 	if (!caps.descriptor_reuse)
15673a92063bSAndy Shevchenko 		return -EPERM;
15683a92063bSAndy Shevchenko 
156927242021SVinod Koul 	tx->flags |= DMA_CTRL_REUSE;
157027242021SVinod Koul 	return 0;
157127242021SVinod Koul }
157227242021SVinod Koul 
157327242021SVinod Koul static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
157427242021SVinod Koul {
157527242021SVinod Koul 	tx->flags &= ~DMA_CTRL_REUSE;
157627242021SVinod Koul }
157727242021SVinod Koul 
157827242021SVinod Koul static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
157927242021SVinod Koul {
158027242021SVinod Koul 	return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE;
158127242021SVinod Koul }
158227242021SVinod Koul 
158327242021SVinod Koul static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
158427242021SVinod Koul {
158527242021SVinod Koul 	/* this is supported for reusable desc, so check that */
15863a92063bSAndy Shevchenko 	if (!dmaengine_desc_test_reuse(desc))
158727242021SVinod Koul 		return -EPERM;
15883a92063bSAndy Shevchenko 
15893a92063bSAndy Shevchenko 	return desc->desc_free(desc);
159027242021SVinod Koul }
159127242021SVinod Koul 
1592c13c8260SChris Leech /* --- DMA device --- */
1593c13c8260SChris Leech 
1594c13c8260SChris Leech int dma_async_device_register(struct dma_device *device);
1595f39b948dSHuang Shijie int dmaenginem_async_device_register(struct dma_device *device);
1596c13c8260SChris Leech void dma_async_device_unregister(struct dma_device *device);
1597e81274cdSDave Jiang int dma_async_device_channel_register(struct dma_device *device,
1598e81274cdSDave Jiang 				      struct dma_chan *chan);
1599e81274cdSDave Jiang void dma_async_device_channel_unregister(struct dma_device *device,
1600e81274cdSDave Jiang 					 struct dma_chan *chan);
160107f2211eSDan Williams void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
1602f5151311SBaolin Wang #define dma_request_channel(mask, x, y) \
1603f5151311SBaolin Wang 	__dma_request_channel(&(mask), x, y, NULL)
1604864ef69bSMatt Porter 
16057547dbd3SPeter Ujfalusi /* Deprecated, please use dma_request_chan() directly */
16067547dbd3SPeter Ujfalusi static inline struct dma_chan * __deprecated
16077547dbd3SPeter Ujfalusi dma_request_slave_channel(struct device *dev, const char *name)
16087547dbd3SPeter Ujfalusi {
16097547dbd3SPeter Ujfalusi 	struct dma_chan *ch = dma_request_chan(dev, name);
16107547dbd3SPeter Ujfalusi 
16117547dbd3SPeter Ujfalusi 	return IS_ERR(ch) ? NULL : ch;
16127547dbd3SPeter Ujfalusi }
16137547dbd3SPeter Ujfalusi 
1614864ef69bSMatt Porter static inline struct dma_chan
161571ca5b78SGeert Uytterhoeven *dma_request_slave_channel_compat(const dma_cap_mask_t mask,
1616a53e28daSLars-Peter Clausen 				  dma_filter_fn fn, void *fn_param,
16171dc04288SJarkko Nikula 				  struct device *dev, const char *name)
1618864ef69bSMatt Porter {
1619864ef69bSMatt Porter 	struct dma_chan *chan;
1620864ef69bSMatt Porter 
1621864ef69bSMatt Porter 	chan = dma_request_slave_channel(dev, name);
1622864ef69bSMatt Porter 	if (chan)
1623864ef69bSMatt Porter 		return chan;
1624864ef69bSMatt Porter 
16257dfffb95SGeert Uytterhoeven 	if (!fn || !fn_param)
16267dfffb95SGeert Uytterhoeven 		return NULL;
16277dfffb95SGeert Uytterhoeven 
162871ca5b78SGeert Uytterhoeven 	return __dma_request_channel(&mask, fn, fn_param, NULL);
1629864ef69bSMatt Porter }
1630816ebf48SPeter Ujfalusi 
1631816ebf48SPeter Ujfalusi static inline char *
1632816ebf48SPeter Ujfalusi dmaengine_get_direction_text(enum dma_transfer_direction dir)
1633816ebf48SPeter Ujfalusi {
1634816ebf48SPeter Ujfalusi 	switch (dir) {
1635816ebf48SPeter Ujfalusi 	case DMA_DEV_TO_MEM:
1636816ebf48SPeter Ujfalusi 		return "DEV_TO_MEM";
1637816ebf48SPeter Ujfalusi 	case DMA_MEM_TO_DEV:
1638816ebf48SPeter Ujfalusi 		return "MEM_TO_DEV";
1639816ebf48SPeter Ujfalusi 	case DMA_MEM_TO_MEM:
1640816ebf48SPeter Ujfalusi 		return "MEM_TO_MEM";
1641816ebf48SPeter Ujfalusi 	case DMA_DEV_TO_DEV:
1642816ebf48SPeter Ujfalusi 		return "DEV_TO_DEV";
1643816ebf48SPeter Ujfalusi 	default:
1644816ebf48SPeter Ujfalusi 		return "invalid";
1645de5506e1SChris Leech 	}
16461873300aSAndy Shevchenko }
1647ab650ef6SPeter Ujfalusi 
1648ab650ef6SPeter Ujfalusi static inline struct device *dmaengine_get_dma_device(struct dma_chan *chan)
1649ab650ef6SPeter Ujfalusi {
1650ab650ef6SPeter Ujfalusi 	if (chan->dev->chan_dma_dev)
1651ab650ef6SPeter Ujfalusi 		return &chan->dev->device;
1652ab650ef6SPeter Ujfalusi 
1653ab650ef6SPeter Ujfalusi 	return chan->device->dev;
1654ab650ef6SPeter Ujfalusi }
1655ab650ef6SPeter Ujfalusi 
1656c13c8260SChris Leech #endif /* DMAENGINE_H */
1657