xref: /linux/include/linux/dmaengine.h (revision 66498c75b4f8017f62d720d9b59675bdf3abce91)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
4  */
5 #ifndef LINUX_DMAENGINE_H
6 #define LINUX_DMAENGINE_H
7 
8 #include <linux/device.h>
9 #include <linux/err.h>
10 #include <linux/uio.h>
11 #include <linux/bug.h>
12 #include <linux/scatterlist.h>
13 #include <linux/bitmap.h>
14 #include <linux/types.h>
15 #include <asm/page.h>
16 
17 /**
18  * typedef dma_cookie_t - an opaque DMA cookie
19  *
20  * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
21  */
22 typedef s32 dma_cookie_t;
23 #define DMA_MIN_COOKIE	1
24 
dma_submit_error(dma_cookie_t cookie)25 static inline int dma_submit_error(dma_cookie_t cookie)
26 {
27 	return cookie < 0 ? cookie : 0;
28 }
29 
30 /**
31  * enum dma_status - DMA transaction status
32  * @DMA_COMPLETE: transaction completed
33  * @DMA_IN_PROGRESS: transaction not yet processed
34  * @DMA_PAUSED: transaction is paused
35  * @DMA_ERROR: transaction failed
36  */
37 enum dma_status {
38 	DMA_COMPLETE,
39 	DMA_IN_PROGRESS,
40 	DMA_PAUSED,
41 	DMA_ERROR,
42 	DMA_OUT_OF_ORDER,
43 };
44 
45 /**
46  * enum dma_transaction_type - DMA transaction types/indexes
47  *
48  * Note: The DMA_ASYNC_TX capability is not to be set by drivers.  It is
49  * automatically set as dma devices are registered.
50  */
51 enum dma_transaction_type {
52 	DMA_MEMCPY,
53 	DMA_XOR,
54 	DMA_PQ,
55 	DMA_XOR_VAL,
56 	DMA_PQ_VAL,
57 	DMA_MEMSET,
58 	DMA_MEMSET_SG,
59 	DMA_INTERRUPT,
60 	DMA_PRIVATE,
61 	DMA_ASYNC_TX,
62 	DMA_SLAVE,
63 	DMA_CYCLIC,
64 	DMA_INTERLEAVE,
65 	DMA_COMPLETION_NO_ORDER,
66 	DMA_REPEAT,
67 	DMA_LOAD_EOT,
68 /* last transaction type for creation of the capabilities mask */
69 	DMA_TX_TYPE_END,
70 };
71 
72 /**
73  * enum dma_transfer_direction - dma transfer mode and direction indicator
74  * @DMA_MEM_TO_MEM: Async/Memcpy mode
75  * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device
76  * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory
77  * @DMA_DEV_TO_DEV: Slave mode & From Device to Device
78  */
79 enum dma_transfer_direction {
80 	DMA_MEM_TO_MEM,
81 	DMA_MEM_TO_DEV,
82 	DMA_DEV_TO_MEM,
83 	DMA_DEV_TO_DEV,
84 	DMA_TRANS_NONE,
85 };
86 
87 /*
88  * Interleaved Transfer Request
89  * ----------------------------
90  * A chunk is collection of contiguous bytes to be transferred.
91  * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG).
92  * ICGs may or may not change between chunks.
93  * A FRAME is the smallest series of contiguous {chunk,icg} pairs,
94  *  that when repeated an integral number of times, specifies the transfer.
95  * A transfer template is specification of a Frame, the number of times
96  *  it is to be repeated and other per-transfer attributes.
97  *
98  * Practically, a client driver would have ready a template for each
99  *  type of transfer it is going to need during its lifetime and
100  *  set only 'src_start' and 'dst_start' before submitting the requests.
101  *
102  *
103  *  |      Frame-1        |       Frame-2       | ~ |       Frame-'numf'  |
104  *  |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...|
105  *
106  *    ==  Chunk size
107  *    ... ICG
108  */
109 
110 /**
111  * struct data_chunk - Element of scatter-gather list that makes a frame.
112  * @size: Number of bytes to read from source.
113  *	  size_dst := fn(op, size_src), so doesn't mean much for destination.
114  * @icg: Number of bytes to jump after last src/dst address of this
115  *	 chunk and before first src/dst address for next chunk.
116  *	 Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false.
117  *	 Ignored for src(assumed 0), if src_inc is true and src_sgl is false.
118  * @dst_icg: Number of bytes to jump after last dst address of this
119  *	 chunk and before the first dst address for next chunk.
120  *	 Ignored if dst_inc is true and dst_sgl is false.
121  * @src_icg: Number of bytes to jump after last src address of this
122  *	 chunk and before the first src address for next chunk.
123  *	 Ignored if src_inc is true and src_sgl is false.
124  */
125 struct data_chunk {
126 	size_t size;
127 	size_t icg;
128 	size_t dst_icg;
129 	size_t src_icg;
130 };
131 
132 /**
133  * struct dma_interleaved_template - Template to convey DMAC the transfer pattern
134  *	 and attributes.
135  * @src_start: Bus address of source for the first chunk.
136  * @dst_start: Bus address of destination for the first chunk.
137  * @dir: Specifies the type of Source and Destination.
138  * @src_inc: If the source address increments after reading from it.
139  * @dst_inc: If the destination address increments after writing to it.
140  * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read).
141  *		Otherwise, source is read contiguously (icg ignored).
142  *		Ignored if src_inc is false.
143  * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write).
144  *		Otherwise, destination is filled contiguously (icg ignored).
145  *		Ignored if dst_inc is false.
146  * @numf: Number of frames in this template.
147  * @frame_size: Number of chunks in a frame i.e, size of sgl[].
148  * @sgl: Array of {chunk,icg} pairs that make up a frame.
149  */
150 struct dma_interleaved_template {
151 	dma_addr_t src_start;
152 	dma_addr_t dst_start;
153 	enum dma_transfer_direction dir;
154 	bool src_inc;
155 	bool dst_inc;
156 	bool src_sgl;
157 	bool dst_sgl;
158 	size_t numf;
159 	size_t frame_size;
160 	struct data_chunk sgl[];
161 };
162 
163 /**
164  * struct dma_vec - DMA vector
165  * @addr: Bus address of the start of the vector
166  * @len: Length in bytes of the DMA vector
167  */
168 struct dma_vec {
169 	dma_addr_t addr;
170 	size_t len;
171 };
172 
173 /**
174  * enum dma_ctrl_flags - DMA flags to augment operation preparation,
175  *  control completion, and communicate status.
176  * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
177  *  this transaction
178  * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
179  *  acknowledges receipt, i.e. has a chance to establish any dependency
180  *  chains
181  * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
182  * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
183  * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
184  *  sources that were the result of a previous operation, in the case of a PQ
185  *  operation it continues the calculation with new sources
186  * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
187  *  on the result of this operation
188  * @DMA_CTRL_REUSE: client can reuse the descriptor and submit again till
189  *  cleared or freed
190  * @DMA_PREP_CMD: tell the driver that the data passed to DMA API is command
191  *  data and the descriptor should be in different format from normal
192  *  data descriptors.
193  * @DMA_PREP_REPEAT: tell the driver that the transaction shall be automatically
194  *  repeated when it ends until a transaction is issued on the same channel
195  *  with the DMA_PREP_LOAD_EOT flag set. This flag is only applicable to
196  *  interleaved transactions and is ignored for all other transaction types.
197  * @DMA_PREP_LOAD_EOT: tell the driver that the transaction shall replace any
198  *  active repeated (as indicated by DMA_PREP_REPEAT) transaction when the
199  *  repeated transaction ends. Not setting this flag when the previously queued
200  *  transaction is marked with DMA_PREP_REPEAT will cause the new transaction
201  *  to never be processed and stay in the issued queue forever. The flag is
202  *  ignored if the previous transaction is not a repeated transaction.
203  */
204 enum dma_ctrl_flags {
205 	DMA_PREP_INTERRUPT = (1 << 0),
206 	DMA_CTRL_ACK = (1 << 1),
207 	DMA_PREP_PQ_DISABLE_P = (1 << 2),
208 	DMA_PREP_PQ_DISABLE_Q = (1 << 3),
209 	DMA_PREP_CONTINUE = (1 << 4),
210 	DMA_PREP_FENCE = (1 << 5),
211 	DMA_CTRL_REUSE = (1 << 6),
212 	DMA_PREP_CMD = (1 << 7),
213 	DMA_PREP_REPEAT = (1 << 8),
214 	DMA_PREP_LOAD_EOT = (1 << 9),
215 };
216 
217 /**
218  * enum sum_check_bits - bit position of pq_check_flags
219  */
220 enum sum_check_bits {
221 	SUM_CHECK_P = 0,
222 	SUM_CHECK_Q = 1,
223 };
224 
225 /**
226  * enum sum_check_flags - result of async_{xor,pq}_zero_sum operations
227  * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
228  * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
229  */
230 enum sum_check_flags {
231 	SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
232 	SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
233 };
234 
235 
236 /**
237  * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
238  * See linux/cpumask.h
239  */
240 typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
241 
242 /**
243  * enum dma_desc_metadata_mode - per descriptor metadata mode types supported
244  * @DESC_METADATA_CLIENT - the metadata buffer is allocated/provided by the
245  *  client driver and it is attached (via the dmaengine_desc_attach_metadata()
246  *  helper) to the descriptor.
247  *
248  * Client drivers interested to use this mode can follow:
249  * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
250  *   1. prepare the descriptor (dmaengine_prep_*)
251  *	construct the metadata in the client's buffer
252  *   2. use dmaengine_desc_attach_metadata() to attach the buffer to the
253  *	descriptor
254  *   3. submit the transfer
255  * - DMA_DEV_TO_MEM:
256  *   1. prepare the descriptor (dmaengine_prep_*)
257  *   2. use dmaengine_desc_attach_metadata() to attach the buffer to the
258  *	descriptor
259  *   3. submit the transfer
260  *   4. when the transfer is completed, the metadata should be available in the
261  *	attached buffer
262  *
263  * @DESC_METADATA_ENGINE - the metadata buffer is allocated/managed by the DMA
264  *  driver. The client driver can ask for the pointer, maximum size and the
265  *  currently used size of the metadata and can directly update or read it.
266  *  dmaengine_desc_get_metadata_ptr() and dmaengine_desc_set_metadata_len() is
267  *  provided as helper functions.
268  *
269  *  Note: the metadata area for the descriptor is no longer valid after the
270  *  transfer has been completed (valid up to the point when the completion
271  *  callback returns if used).
272  *
273  * Client drivers interested to use this mode can follow:
274  * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
275  *   1. prepare the descriptor (dmaengine_prep_*)
276  *   2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the engine's
277  *	metadata area
278  *   3. update the metadata at the pointer
279  *   4. use dmaengine_desc_set_metadata_len()  to tell the DMA engine the amount
280  *	of data the client has placed into the metadata buffer
281  *   5. submit the transfer
282  * - DMA_DEV_TO_MEM:
283  *   1. prepare the descriptor (dmaengine_prep_*)
284  *   2. submit the transfer
285  *   3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get the
286  *	pointer to the engine's metadata area
287  *   4. Read out the metadata from the pointer
288  *
289  * Warning: the two modes are not compatible and clients must use one mode for a
290  * descriptor.
291  */
292 enum dma_desc_metadata_mode {
293 	DESC_METADATA_NONE = 0,
294 	DESC_METADATA_CLIENT = BIT(0),
295 	DESC_METADATA_ENGINE = BIT(1),
296 };
297 
298 /**
299  * struct dma_chan_percpu - the per-CPU part of struct dma_chan
300  * @memcpy_count: transaction counter
301  * @bytes_transferred: byte counter
302  */
303 struct dma_chan_percpu {
304 	/* stats */
305 	unsigned long memcpy_count;
306 	unsigned long bytes_transferred;
307 };
308 
309 /**
310  * struct dma_router - DMA router structure
311  * @dev: pointer to the DMA router device
312  * @route_free: function to be called when the route can be disconnected
313  */
314 struct dma_router {
315 	struct device *dev;
316 	void (*route_free)(struct device *dev, void *route_data);
317 };
318 
319 /**
320  * struct dma_chan - devices supply DMA channels, clients use them
321  * @device: ptr to the dma device who supplies this channel, always !%NULL
322  * @slave: ptr to the device using this channel
323  * @cookie: last cookie value returned to client
324  * @completed_cookie: last completed cookie for this channel
325  * @lock: protect between config and prepare transfer when driver have not
326  *	  implemented callback device_prep_config_sg().
327  * @chan_id: channel ID for sysfs
328  * @dev: class device for sysfs
329  * @name: backlink name for sysfs
330  * @dbg_client_name: slave name for debugfs in format:
331  *	dev_name(requester's dev):channel name, for example: "2b00000.mcasp:tx"
332  * @device_node: used to add this to the device chan list
333  * @local: per-cpu pointer to a struct dma_chan_percpu
334  * @client_count: how many clients are using this channel
335  * @table_count: number of appearances in the mem-to-mem allocation table
336  * @router: pointer to the DMA router structure
337  * @route_data: channel specific data for the router
338  * @private: private data for certain client-channel associations
339  */
340 struct dma_chan {
341 	struct dma_device *device;
342 	struct device *slave;
343 	dma_cookie_t cookie;
344 	dma_cookie_t completed_cookie;
345 
346 	/*
347 	 * protect between config and prepare transfer because *_prep() may be
348 	 * called from complete callback, which is in GFP_NOSLEEP context.
349 	 */
350 	spinlock_t lock;
351 
352 	/* sysfs */
353 	int chan_id;
354 	struct dma_chan_dev *dev;
355 	const char *name;
356 #ifdef CONFIG_DEBUG_FS
357 	char *dbg_client_name;
358 #endif
359 
360 	struct list_head device_node;
361 	struct dma_chan_percpu __percpu *local;
362 	int client_count;
363 	int table_count;
364 
365 	/* DMA router */
366 	struct dma_router *router;
367 	void *route_data;
368 
369 	void *private;
370 };
371 
372 /**
373  * struct dma_chan_dev - relate sysfs device node to backing channel device
374  * @chan: driver channel device
375  * @device: sysfs device
376  * @dev_id: parent dma_device dev_id
377  * @chan_dma_dev: The channel is using custom/different dma-mapping
378  * compared to the parent dma_device
379  */
380 struct dma_chan_dev {
381 	struct dma_chan *chan;
382 	struct device device;
383 	int dev_id;
384 	bool chan_dma_dev;
385 };
386 
387 /**
388  * enum dma_slave_buswidth - defines bus width of the DMA slave
389  * device, source or target buses
390  */
391 enum dma_slave_buswidth {
392 	DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
393 	DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
394 	DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
395 	DMA_SLAVE_BUSWIDTH_3_BYTES = 3,
396 	DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
397 	DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
398 	DMA_SLAVE_BUSWIDTH_16_BYTES = 16,
399 	DMA_SLAVE_BUSWIDTH_32_BYTES = 32,
400 	DMA_SLAVE_BUSWIDTH_64_BYTES = 64,
401 	DMA_SLAVE_BUSWIDTH_128_BYTES = 128,
402 };
403 
404 /**
405  * struct dma_slave_config - dma slave channel runtime config
406  * @direction: whether the data shall go in or out on this slave
407  * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are
408  * legal values. DEPRECATED, drivers should use the direction argument
409  * to the device_prep_slave_sg and device_prep_dma_cyclic functions or
410  * the dir field in the dma_interleaved_template structure.
411  * @src_addr: this is the physical address where DMA slave data
412  * should be read (RX), if the source is memory this argument is
413  * ignored.
414  * @dst_addr: this is the physical address where DMA slave data
415  * should be written (TX), if the destination is memory this argument
416  * is ignored.
417  * @src_addr_width: this is the width in bytes of the source (RX)
418  * register where DMA data shall be read. If the source
419  * is memory this may be ignored depending on architecture.
420  * Legal values: 1, 2, 3, 4, 8, 16, 32, 64, 128.
421  * @dst_addr_width: same as src_addr_width but for destination
422  * target (TX) mutatis mutandis.
423  * @src_maxburst: the maximum number of words (note: words, as in
424  * units of the src_addr_width member, not bytes) that can be sent
425  * in one burst to the device. Typically something like half the
426  * FIFO depth on I/O peripherals so you don't overflow it. This
427  * may or may not be applicable on memory sources.
428  * @dst_maxburst: same as src_maxburst but for destination target
429  * mutatis mutandis.
430  * @src_port_window_size: The length of the register area in words the data need
431  * to be accessed on the device side. It is only used for devices which is using
432  * an area instead of a single register to receive the data. Typically the DMA
433  * loops in this area in order to transfer the data.
434  * @dst_port_window_size: same as src_port_window_size but for the destination
435  * port.
436  * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill
437  * with 'true' if peripheral should be flow controller. Direction will be
438  * selected at Runtime.
439  * @peripheral_config: peripheral configuration for programming peripheral
440  * for dmaengine transfer
441  * @peripheral_size: peripheral configuration buffer size
442  *
443  * This struct is passed in as configuration data to a DMA engine
444  * in order to set up a certain channel for DMA transport at runtime.
445  * The DMA device/engine has to provide support for an additional
446  * callback in the dma_device structure, device_config and this struct
447  * will then be passed in as an argument to the function.
448  *
449  * The rationale for adding configuration information to this struct is as
450  * follows: if it is likely that more than one DMA slave controllers in
451  * the world will support the configuration option, then make it generic.
452  * If not: if it is fixed so that it be sent in static from the platform
453  * data, then prefer to do that.
454  */
455 struct dma_slave_config {
456 	enum dma_transfer_direction direction;
457 	phys_addr_t src_addr;
458 	phys_addr_t dst_addr;
459 	enum dma_slave_buswidth src_addr_width;
460 	enum dma_slave_buswidth dst_addr_width;
461 	u32 src_maxburst;
462 	u32 dst_maxburst;
463 	u32 src_port_window_size;
464 	u32 dst_port_window_size;
465 	bool device_fc;
466 	void *peripheral_config;
467 	size_t peripheral_size;
468 };
469 
470 /**
471  * enum dma_residue_granularity - Granularity of the reported transfer residue
472  * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The
473  *  DMA channel is only able to tell whether a descriptor has been completed or
474  *  not, which means residue reporting is not supported by this channel. The
475  *  residue field of the dma_tx_state field will always be 0.
476  * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully
477  *  completed segment of the transfer (For cyclic transfers this is after each
478  *  period). This is typically implemented by having the hardware generate an
479  *  interrupt after each transferred segment and then the drivers updates the
480  *  outstanding residue by the size of the segment. Another possibility is if
481  *  the hardware supports scatter-gather and the segment descriptor has a field
482  *  which gets set after the segment has been completed. The driver then counts
483  *  the number of segments without the flag set to compute the residue.
484  * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred
485  *  burst. This is typically only supported if the hardware has a progress
486  *  register of some sort (E.g. a register with the current read/write address
487  *  or a register with the amount of bursts/beats/bytes that have been
488  *  transferred or still need to be transferred).
489  */
490 enum dma_residue_granularity {
491 	DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0,
492 	DMA_RESIDUE_GRANULARITY_SEGMENT = 1,
493 	DMA_RESIDUE_GRANULARITY_BURST = 2,
494 };
495 
496 /**
497  * struct dma_slave_caps - expose capabilities of a slave channel only
498  * @src_addr_widths: bit mask of src addr widths the channel supports.
499  *	Width is specified in bytes, e.g. for a channel supporting
500  *	a width of 4 the mask should have BIT(4) set.
501  * @dst_addr_widths: bit mask of dst addr widths the channel supports
502  * @directions: bit mask of slave directions the channel supports.
503  *	Since the enum dma_transfer_direction is not defined as bit flag for
504  *	each type, the dma controller should set BIT(<TYPE>) and same
505  *	should be checked by controller as well
506  * @min_burst: min burst capability per-transfer
507  * @max_burst: max burst capability per-transfer
508  * @max_sg_burst: max number of SG list entries executed in a single burst
509  *	DMA tansaction with no software intervention for reinitialization.
510  *	Zero value means unlimited number of entries.
511  * @cmd_pause: true, if pause is supported (i.e. for reading residue or
512  *	       for resume later)
513  * @cmd_resume: true, if resume is supported
514  * @cmd_terminate: true, if terminate cmd is supported
515  * @residue_granularity: granularity of the reported transfer residue
516  * @descriptor_reuse: if a descriptor can be reused by client and
517  * resubmitted multiple times
518  */
519 struct dma_slave_caps {
520 	u32 src_addr_widths;
521 	u32 dst_addr_widths;
522 	u32 directions;
523 	u32 min_burst;
524 	u32 max_burst;
525 	u32 max_sg_burst;
526 	bool cmd_pause;
527 	bool cmd_resume;
528 	bool cmd_terminate;
529 	enum dma_residue_granularity residue_granularity;
530 	bool descriptor_reuse;
531 };
532 
dma_chan_name(struct dma_chan * chan)533 static inline const char *dma_chan_name(struct dma_chan *chan)
534 {
535 	return dev_name(&chan->dev->device);
536 }
537 
538 /**
539  * typedef dma_filter_fn - callback filter for dma_request_channel
540  * @chan: channel to be reviewed
541  * @filter_param: opaque parameter passed through dma_request_channel
542  *
543  * When this optional parameter is specified in a call to dma_request_channel a
544  * suitable channel is passed to this routine for further dispositioning before
545  * being returned.  Where 'suitable' indicates a non-busy channel that
546  * satisfies the given capability mask.  It returns 'true' to indicate that the
547  * channel is suitable.
548  */
549 typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
550 
551 typedef void (*dma_async_tx_callback)(void *dma_async_param);
552 
553 enum dmaengine_tx_result {
554 	DMA_TRANS_NOERROR = 0,		/* SUCCESS */
555 	DMA_TRANS_READ_FAILED,		/* Source DMA read failed */
556 	DMA_TRANS_WRITE_FAILED,		/* Destination DMA write failed */
557 	DMA_TRANS_ABORTED,		/* Op never submitted / aborted */
558 };
559 
560 struct dmaengine_result {
561 	enum dmaengine_tx_result result;
562 	u32 residue;
563 };
564 
565 typedef void (*dma_async_tx_callback_result)(void *dma_async_param,
566 				const struct dmaengine_result *result);
567 
568 struct dmaengine_unmap_data {
569 #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
570 	u16 map_cnt;
571 #else
572 	u8 map_cnt;
573 #endif
574 	u8 to_cnt;
575 	u8 from_cnt;
576 	u8 bidi_cnt;
577 	struct device *dev;
578 	struct kref kref;
579 	size_t len;
580 	dma_addr_t addr[];
581 };
582 
583 struct dma_async_tx_descriptor;
584 
585 struct dma_descriptor_metadata_ops {
586 	int (*attach)(struct dma_async_tx_descriptor *desc, void *data,
587 		      size_t len);
588 
589 	void *(*get_ptr)(struct dma_async_tx_descriptor *desc,
590 			 size_t *payload_len, size_t *max_len);
591 	int (*set_len)(struct dma_async_tx_descriptor *desc,
592 		       size_t payload_len);
593 };
594 
595 /**
596  * struct dma_async_tx_descriptor - async transaction descriptor
597  * ---dma generic offload fields---
598  * @cookie: tracking cookie for this transaction, set to -EBUSY if
599  *	this tx is sitting on a dependency list
600  * @flags: flags to augment operation preparation, control completion, and
601  *	communicate status
602  * @phys: physical address of the descriptor
603  * @chan: target channel for this operation
604  * @tx_submit: accept the descriptor, assign ordered cookie and mark the
605  *	descriptor pending. To be pushed on .issue_pending() call
606  * @desc_free: driver's callback function to free a resusable descriptor
607  *	after completion
608  * @callback: routine to call after this operation is complete
609  * @callback_result: error result from a DMA transaction
610  * @callback_param: general parameter to pass to the callback routine
611  * @unmap: hook for generic DMA unmap data
612  * @desc_metadata_mode: core managed metadata mode to protect mixed use of
613  *	DESC_METADATA_CLIENT or DESC_METADATA_ENGINE. Otherwise
614  *	DESC_METADATA_NONE
615  * @metadata_ops: DMA driver provided metadata mode ops, need to be set by the
616  *	DMA driver if metadata mode is supported with the descriptor
617  * ---async_tx api specific fields---
618  * @next: at completion submit this descriptor
619  * @parent: pointer to the next level up in the dependency chain
620  * @lock: protect the parent and next pointers
621  */
622 struct dma_async_tx_descriptor {
623 	dma_cookie_t cookie;
624 	enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
625 	dma_addr_t phys;
626 	struct dma_chan *chan;
627 	dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
628 	int (*desc_free)(struct dma_async_tx_descriptor *tx);
629 	dma_async_tx_callback callback;
630 	dma_async_tx_callback_result callback_result;
631 	void *callback_param;
632 	struct dmaengine_unmap_data *unmap;
633 	enum dma_desc_metadata_mode desc_metadata_mode;
634 	const struct dma_descriptor_metadata_ops *metadata_ops;
635 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
636 	struct dma_async_tx_descriptor *next;
637 	struct dma_async_tx_descriptor *parent;
638 	spinlock_t lock;
639 #endif
640 };
641 
642 #ifdef CONFIG_DMA_ENGINE
dma_set_unmap(struct dma_async_tx_descriptor * tx,struct dmaengine_unmap_data * unmap)643 static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
644 				 struct dmaengine_unmap_data *unmap)
645 {
646 	kref_get(&unmap->kref);
647 	tx->unmap = unmap;
648 }
649 
650 struct dmaengine_unmap_data *
651 dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
652 void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
653 #else
dma_set_unmap(struct dma_async_tx_descriptor * tx,struct dmaengine_unmap_data * unmap)654 static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
655 				 struct dmaengine_unmap_data *unmap)
656 {
657 }
658 static inline struct dmaengine_unmap_data *
dmaengine_get_unmap_data(struct device * dev,int nr,gfp_t flags)659 dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
660 {
661 	return NULL;
662 }
dmaengine_unmap_put(struct dmaengine_unmap_data * unmap)663 static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
664 {
665 }
666 #endif
667 
dma_descriptor_unmap(struct dma_async_tx_descriptor * tx)668 static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
669 {
670 	if (!tx->unmap)
671 		return;
672 
673 	dmaengine_unmap_put(tx->unmap);
674 	tx->unmap = NULL;
675 }
676 
677 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
txd_lock(struct dma_async_tx_descriptor * txd)678 static inline void txd_lock(struct dma_async_tx_descriptor *txd)
679 {
680 }
txd_unlock(struct dma_async_tx_descriptor * txd)681 static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
682 {
683 }
txd_chain(struct dma_async_tx_descriptor * txd,struct dma_async_tx_descriptor * next)684 static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
685 {
686 	BUG();
687 }
txd_clear_parent(struct dma_async_tx_descriptor * txd)688 static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
689 {
690 }
txd_clear_next(struct dma_async_tx_descriptor * txd)691 static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
692 {
693 }
txd_next(struct dma_async_tx_descriptor * txd)694 static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
695 {
696 	return NULL;
697 }
txd_parent(struct dma_async_tx_descriptor * txd)698 static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
699 {
700 	return NULL;
701 }
702 
703 #else
txd_lock(struct dma_async_tx_descriptor * txd)704 static inline void txd_lock(struct dma_async_tx_descriptor *txd)
705 {
706 	spin_lock_bh(&txd->lock);
707 }
txd_unlock(struct dma_async_tx_descriptor * txd)708 static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
709 {
710 	spin_unlock_bh(&txd->lock);
711 }
txd_chain(struct dma_async_tx_descriptor * txd,struct dma_async_tx_descriptor * next)712 static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
713 {
714 	txd->next = next;
715 	next->parent = txd;
716 }
txd_clear_parent(struct dma_async_tx_descriptor * txd)717 static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
718 {
719 	txd->parent = NULL;
720 }
txd_clear_next(struct dma_async_tx_descriptor * txd)721 static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
722 {
723 	txd->next = NULL;
724 }
txd_parent(struct dma_async_tx_descriptor * txd)725 static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
726 {
727 	return txd->parent;
728 }
txd_next(struct dma_async_tx_descriptor * txd)729 static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
730 {
731 	return txd->next;
732 }
733 #endif
734 
735 /**
736  * struct dma_tx_state - filled in to report the status of
737  * a transfer.
738  * @last: last completed DMA cookie
739  * @used: last issued DMA cookie (i.e. the one in progress)
740  * @residue: the remaining number of bytes left to transmit
741  *	on the selected transfer for states DMA_IN_PROGRESS and
742  *	DMA_PAUSED if this is implemented in the driver, else 0
743  * @in_flight_bytes: amount of data in bytes cached by the DMA.
744  */
745 struct dma_tx_state {
746 	dma_cookie_t last;
747 	dma_cookie_t used;
748 	u32 residue;
749 	u32 in_flight_bytes;
750 };
751 
752 /**
753  * enum dmaengine_alignment - defines alignment of the DMA async tx
754  * buffers
755  */
756 enum dmaengine_alignment {
757 	DMAENGINE_ALIGN_1_BYTE = 0,
758 	DMAENGINE_ALIGN_2_BYTES = 1,
759 	DMAENGINE_ALIGN_4_BYTES = 2,
760 	DMAENGINE_ALIGN_8_BYTES = 3,
761 	DMAENGINE_ALIGN_16_BYTES = 4,
762 	DMAENGINE_ALIGN_32_BYTES = 5,
763 	DMAENGINE_ALIGN_64_BYTES = 6,
764 	DMAENGINE_ALIGN_128_BYTES = 7,
765 	DMAENGINE_ALIGN_256_BYTES = 8,
766 };
767 
768 /**
769  * struct dma_slave_map - associates slave device and it's slave channel with
770  * parameter to be used by a filter function
771  * @devname: name of the device
772  * @slave: slave channel name
773  * @param: opaque parameter to pass to struct dma_filter.fn
774  */
775 struct dma_slave_map {
776 	const char *devname;
777 	const char *slave;
778 	void *param;
779 };
780 
781 /**
782  * struct dma_filter - information for slave device/channel to filter_fn/param
783  * mapping
784  * @fn: filter function callback
785  * @mapcnt: number of slave device/channel in the map
786  * @map: array of channel to filter mapping data
787  */
788 struct dma_filter {
789 	dma_filter_fn fn;
790 	int mapcnt;
791 	const struct dma_slave_map *map;
792 };
793 
794 /**
795  * struct dma_device - info on the entity supplying DMA services
796  * @ref: reference is taken and put every time a channel is allocated or freed
797  * @chancnt: how many DMA channels are supported
798  * @privatecnt: how many DMA channels are requested by dma_request_channel
799  * @channels: the list of struct dma_chan
800  * @global_node: list_head for global dma_device_list
801  * @filter: information for device/slave to filter function/param mapping
802  * @cap_mask: one or more dma_capability flags
803  * @desc_metadata_modes: supported metadata modes by the DMA device
804  * @max_xor: maximum number of xor sources, 0 if no capability
805  * @max_pq: maximum number of PQ sources and PQ-continue capability
806  * @copy_align: alignment shift for memcpy operations
807  * @xor_align: alignment shift for xor operations
808  * @pq_align: alignment shift for pq operations
809  * @fill_align: alignment shift for memset operations
810  * @dev_id: unique device ID
811  * @dev: struct device reference for dma mapping api
812  * @owner: owner module (automatically set based on the provided dev)
813  * @chan_ida: unique channel ID
814  * @src_addr_widths: bit mask of src addr widths the device supports
815  *	Width is specified in bytes, e.g. for a device supporting
816  *	a width of 4 the mask should have BIT(4) set.
817  * @dst_addr_widths: bit mask of dst addr widths the device supports
818  * @directions: bit mask of slave directions the device supports.
819  *	Since the enum dma_transfer_direction is not defined as bit flag for
820  *	each type, the dma controller should set BIT(<TYPE>) and same
821  *	should be checked by controller as well
822  * @min_burst: min burst capability per-transfer
823  * @max_burst: max burst capability per-transfer
824  * @max_sg_burst: max number of SG list entries executed in a single burst
825  *	DMA tansaction with no software intervention for reinitialization.
826  *	Zero value means unlimited number of entries.
827  * @descriptor_reuse: a submitted transfer can be resubmitted after completion
828  * @residue_granularity: granularity of the transfer residue reported
829  *	by tx_status
830  * @device_alloc_chan_resources: allocate resources and return the
831  *	number of allocated descriptors
832  * @device_router_config: optional callback for DMA router configuration
833  * @device_free_chan_resources: release DMA channel's resources
834  * @device_prep_dma_memcpy: prepares a memcpy operation
835  * @device_prep_dma_xor: prepares a xor operation
836  * @device_prep_dma_xor_val: prepares a xor validation operation
837  * @device_prep_dma_pq: prepares a pq operation
838  * @device_prep_dma_pq_val: prepares a pqzero_sum operation
839  * @device_prep_dma_memset: prepares a memset operation
840  * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list
841  * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
842  * @device_prep_peripheral_dma_vec: prepares a scatter-gather DMA transfer,
843  *	where the address and size of each segment is located in one entry of
844  *	the dma_vec array.
845  * @device_prep_slave_sg: prepares a slave dma operation
846  * @device_prep_config_sg: prepares a slave DMA operation with dma_slave_config
847  * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
848  *	The function takes a buffer of size buf_len. The callback function will
849  *	be called after period_len bytes have been transferred.
850  * @device_prep_interleaved_dma: Transfer expression in a generic way.
851  * @device_caps: May be used to override the generic DMA slave capabilities
852  *	with per-channel specific ones
853  * @device_config: Pushes a new configuration to a channel, return 0 or an error
854  *	code
855  * @device_pause: Pauses any transfer happening on a channel. Returns
856  *	0 or an error code
857  * @device_resume: Resumes any transfer on a channel previously
858  *	paused. Returns 0 or an error code
859  * @device_terminate_all: Aborts all transfers on a channel. Returns 0
860  *	or an error code
861  * @device_synchronize: Synchronizes the termination of a transfers to the
862  *  current context.
863  * @device_tx_status: poll for transaction completion, the optional
864  *	txstate parameter can be supplied with a pointer to get a
865  *	struct with auxiliary transfer status information, otherwise the call
866  *	will just return a simple status code
867  * @device_issue_pending: push pending transactions to hardware
868  * @device_release: called sometime atfer dma_async_device_unregister() is
869  *     called and there are no further references to this structure. This
870  *     must be implemented to free resources however many existing drivers
871  *     do not and are therefore not safe to unbind while in use.
872  * @dbg_summary_show: optional routine to show contents in debugfs; default code
873  *     will be used when this is omitted, but custom code can show extra,
874  *     controller specific information.
875  * @dbg_dev_root: the root folder in debugfs for this device
876  */
877 struct dma_device {
878 	struct kref ref;
879 	unsigned int chancnt;
880 	unsigned int privatecnt;
881 	struct list_head channels;
882 	struct list_head global_node;
883 	struct dma_filter filter;
884 	dma_cap_mask_t cap_mask;
885 	enum dma_desc_metadata_mode desc_metadata_modes;
886 	unsigned short max_xor;
887 	unsigned short max_pq;
888 	enum dmaengine_alignment copy_align;
889 	enum dmaengine_alignment xor_align;
890 	enum dmaengine_alignment pq_align;
891 	enum dmaengine_alignment fill_align;
892 	#define DMA_HAS_PQ_CONTINUE (1 << 15)
893 
894 	int dev_id;
895 	struct device *dev;
896 	struct module *owner;
897 	struct ida chan_ida;
898 
899 	u32 src_addr_widths;
900 	u32 dst_addr_widths;
901 	u32 directions;
902 	u32 min_burst;
903 	u32 max_burst;
904 	u32 max_sg_burst;
905 	bool descriptor_reuse;
906 	enum dma_residue_granularity residue_granularity;
907 
908 	int (*device_alloc_chan_resources)(struct dma_chan *chan);
909 	int (*device_router_config)(struct dma_chan *chan);
910 	void (*device_free_chan_resources)(struct dma_chan *chan);
911 
912 	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
913 		struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
914 		size_t len, unsigned long flags);
915 	struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
916 		struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
917 		unsigned int src_cnt, size_t len, unsigned long flags);
918 	struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
919 		struct dma_chan *chan, dma_addr_t *src,	unsigned int src_cnt,
920 		size_t len, enum sum_check_flags *result, unsigned long flags);
921 	struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
922 		struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
923 		unsigned int src_cnt, const unsigned char *scf,
924 		size_t len, unsigned long flags);
925 	struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
926 		struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
927 		unsigned int src_cnt, const unsigned char *scf, size_t len,
928 		enum sum_check_flags *pqres, unsigned long flags);
929 	struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
930 		struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
931 		unsigned long flags);
932 	struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)(
933 		struct dma_chan *chan, struct scatterlist *sg,
934 		unsigned int nents, int value, unsigned long flags);
935 	struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
936 		struct dma_chan *chan, unsigned long flags);
937 
938 	struct dma_async_tx_descriptor *(*device_prep_peripheral_dma_vec)(
939 		struct dma_chan *chan, const struct dma_vec *vecs,
940 		size_t nents, enum dma_transfer_direction direction,
941 		unsigned long flags);
942 	struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
943 		struct dma_chan *chan, struct scatterlist *sgl,
944 		unsigned int sg_len, enum dma_transfer_direction direction,
945 		unsigned long flags, void *context);
946 	struct dma_async_tx_descriptor *(*device_prep_config_sg)(
947 		struct dma_chan *chan, struct scatterlist *sgl,
948 		unsigned int sg_len, enum dma_transfer_direction direction,
949 		unsigned long flags, struct dma_slave_config *config);
950 	struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
951 		struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
952 		size_t period_len, enum dma_transfer_direction direction,
953 		unsigned long flags);
954 	struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
955 		struct dma_chan *chan, struct dma_interleaved_template *xt,
956 		unsigned long flags);
957 
958 	void (*device_caps)(struct dma_chan *chan, struct dma_slave_caps *caps);
959 	int (*device_config)(struct dma_chan *chan, struct dma_slave_config *config);
960 	int (*device_pause)(struct dma_chan *chan);
961 	int (*device_resume)(struct dma_chan *chan);
962 	int (*device_terminate_all)(struct dma_chan *chan);
963 	void (*device_synchronize)(struct dma_chan *chan);
964 
965 	enum dma_status (*device_tx_status)(struct dma_chan *chan,
966 					    dma_cookie_t cookie,
967 					    struct dma_tx_state *txstate);
968 	void (*device_issue_pending)(struct dma_chan *chan);
969 	void (*device_release)(struct dma_device *dev);
970 	/* debugfs support */
971 	void (*dbg_summary_show)(struct seq_file *s, struct dma_device *dev);
972 	struct dentry *dbg_dev_root;
973 };
974 
dmaengine_slave_config(struct dma_chan * chan,struct dma_slave_config * config)975 static inline int dmaengine_slave_config(struct dma_chan *chan,
976 					  struct dma_slave_config *config)
977 {
978 	if (chan->device->device_config)
979 		return chan->device->device_config(chan, config);
980 
981 	return -ENOSYS;
982 }
983 
is_slave_direction(enum dma_transfer_direction direction)984 static inline bool is_slave_direction(enum dma_transfer_direction direction)
985 {
986 	return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM) ||
987 	       (direction == DMA_DEV_TO_DEV);
988 }
989 
990 static inline struct dma_async_tx_descriptor *
dmaengine_prep_config_single(struct dma_chan * chan,dma_addr_t buf,size_t len,enum dma_transfer_direction dir,unsigned long flags,struct dma_slave_config * config)991 dmaengine_prep_config_single(struct dma_chan *chan, dma_addr_t buf, size_t len,
992 			     enum dma_transfer_direction dir,
993 			     unsigned long flags,
994 			     struct dma_slave_config *config)
995 {
996 	struct scatterlist sg;
997 
998 	if (!chan || !chan->device)
999 		return NULL;
1000 
1001 	sg_init_table(&sg, 1);
1002 	sg_dma_address(&sg) = buf;
1003 	sg_dma_len(&sg) = len;
1004 
1005 	if (chan->device->device_prep_config_sg)
1006 		return chan->device->device_prep_config_sg(chan, &sg, 1, dir,
1007 							   flags, config);
1008 
1009 	if (config)
1010 		if (dmaengine_slave_config(chan, config))
1011 			return NULL;
1012 
1013 	if (!chan->device->device_prep_slave_sg)
1014 		return NULL;
1015 
1016 	return chan->device->device_prep_slave_sg(chan, &sg, 1,
1017 						  dir, flags, NULL);
1018 }
1019 
1020 static inline struct dma_async_tx_descriptor *
dmaengine_prep_slave_single(struct dma_chan * chan,dma_addr_t buf,size_t len,enum dma_transfer_direction dir,unsigned long flags)1021 dmaengine_prep_slave_single(struct dma_chan *chan, dma_addr_t buf, size_t len,
1022 			    enum dma_transfer_direction dir,
1023 			    unsigned long flags)
1024 {
1025 	return dmaengine_prep_config_single(chan, buf, len, dir, flags, NULL);
1026 }
1027 
1028 /**
1029  * dmaengine_prep_peripheral_dma_vec() - Prepare a DMA scatter-gather descriptor
1030  * @chan: The channel to be used for this descriptor
1031  * @vecs: The array of DMA vectors that should be transferred
1032  * @nents: The number of DMA vectors in the array
1033  * @dir: Specifies the direction of the data transfer
1034  * @flags: DMA engine flags - DMA_PREP_REPEAT can be used to mark a cyclic
1035  *         DMA transfer
1036  */
dmaengine_prep_peripheral_dma_vec(struct dma_chan * chan,const struct dma_vec * vecs,size_t nents,enum dma_transfer_direction dir,unsigned long flags)1037 static inline struct dma_async_tx_descriptor *dmaengine_prep_peripheral_dma_vec(
1038 	struct dma_chan *chan, const struct dma_vec *vecs, size_t nents,
1039 	enum dma_transfer_direction dir, unsigned long flags)
1040 {
1041 	if (!chan || !chan->device || !chan->device->device_prep_peripheral_dma_vec)
1042 		return NULL;
1043 
1044 	return chan->device->device_prep_peripheral_dma_vec(chan, vecs, nents,
1045 							    dir, flags);
1046 }
1047 
1048 static inline struct dma_async_tx_descriptor *
dmaengine_prep_config_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction dir,unsigned long flags,struct dma_slave_config * config)1049 dmaengine_prep_config_sg(struct dma_chan *chan, struct scatterlist *sgl,
1050 			 unsigned int sg_len, enum dma_transfer_direction dir,
1051 			 unsigned long flags, struct dma_slave_config *config)
1052 {
1053 	if (!chan || !chan->device)
1054 		return NULL;
1055 
1056 	if (chan->device->device_prep_config_sg)
1057 		return chan->device->device_prep_config_sg(chan, sgl, sg_len,
1058 				dir, flags, config);
1059 
1060 	if (config)
1061 		if (dmaengine_slave_config(chan, config))
1062 			return NULL;
1063 
1064 	if (!chan->device->device_prep_slave_sg)
1065 		return NULL;
1066 
1067 	return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
1068 						  dir, flags, NULL);
1069 }
1070 
1071 static inline struct dma_async_tx_descriptor *
dmaengine_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction dir,unsigned long flags)1072 dmaengine_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
1073 			unsigned int sg_len, enum dma_transfer_direction dir,
1074 			unsigned long flags)
1075 {
1076 	return dmaengine_prep_config_sg(chan, sgl, sg_len, dir, flags, NULL);
1077 }
1078 
1079 /**
1080  * dmaengine_prep_config_sg_safe - prepare a scatter-gather DMA transfer
1081  *                                 with atomic slave configuration update
1082  * @chan: DMA channel
1083  * @sgl: scatterlist for the transfer
1084  * @sg_len: number of entries in @sgl
1085  * @dir: DMA transfer direction
1086  * @flags: transfer preparation flags
1087  * @config: DMA slave configuration for this transfer
1088  *
1089  * Prepare a DMA scatter-gather transfer together with a corresponding slave
1090  * configuration update in a re-entrant and race-safe manner.
1091  *
1092  * DMA engine drivers may implement the optional
1093  * device_prep_config_sg() callback to perform both the slave configuration
1094  * and descriptor preparation atomically. In this case, the operation is
1095  * fully handled by the DMA engine driver.
1096  *
1097  * If the DMA engine driver does not implement device_prep_config_sg(), falls
1098  * back to calling dmaengine_slave_config() followed by dmaengine_prep_slave_sg().
1099  * The fallback path is protected by a per-channel spinlock to ensure that
1100  * concurrent callers cannot interleave configuration and descriptor preparation
1101  * on the same DMA channel.
1102  *
1103  * Return: Pointer to a prepared DMA async transaction descriptor on success,
1104  * or %NULL if the transfer could not be prepared.
1105  */
1106 static inline struct dma_async_tx_descriptor *
dmaengine_prep_config_sg_safe(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction dir,unsigned long flags,struct dma_slave_config * config)1107 dmaengine_prep_config_sg_safe(struct dma_chan *chan, struct scatterlist *sgl,
1108 			      unsigned int sg_len,
1109 			      enum dma_transfer_direction dir,
1110 			      unsigned long flags,
1111 			      struct dma_slave_config *config)
1112 {
1113 	struct dma_async_tx_descriptor *tx;
1114 	unsigned long spinlock_flags;
1115 
1116 	if (!chan || !chan->device)
1117 		return NULL;
1118 
1119 	if (!chan->device->device_prep_config_sg)
1120 		spin_lock_irqsave(&chan->lock, spinlock_flags);
1121 
1122 	tx = dmaengine_prep_config_sg(chan, sgl, sg_len, dir, flags, config);
1123 
1124 	if (!chan->device->device_prep_config_sg)
1125 		spin_unlock_irqrestore(&chan->lock, spinlock_flags);
1126 
1127 	return tx;
1128 }
1129 
1130 /**
1131  * dmaengine_prep_config_single_safe - prepare a single-buffer DMA transfer
1132  *                                     with atomic slave configuration update
1133  * @chan: DMA channel
1134  * @buf: DMA buffer address
1135  * @len: length of the transfer in bytes
1136  * @dir: DMA transfer direction
1137  * @flags: transfer preparation flags
1138  * @config: DMA slave configuration for this transfer
1139  *
1140  * Detail see dmaengine_prep_config_sg_safe().
1141  */
1142 static inline struct dma_async_tx_descriptor *
dmaengine_prep_config_single_safe(struct dma_chan * chan,dma_addr_t buf,size_t len,enum dma_transfer_direction dir,unsigned long flags,struct dma_slave_config * config)1143 dmaengine_prep_config_single_safe(struct dma_chan *chan, dma_addr_t buf,
1144 				  size_t len, enum dma_transfer_direction dir,
1145 				  unsigned long flags,
1146 				  struct dma_slave_config *config)
1147 {
1148 	struct scatterlist sg;
1149 
1150 	sg_init_table(&sg, 1);
1151 	sg_dma_address(&sg) = buf;
1152 	sg_dma_len(&sg) = len;
1153 
1154 	return dmaengine_prep_config_sg_safe(chan, &sg, 1, dir, flags, config);
1155 }
1156 
1157 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1158 struct rio_dma_ext;
dmaengine_prep_rio_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction dir,unsigned long flags,struct rio_dma_ext * rio_ext)1159 static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
1160 	struct dma_chan *chan, struct scatterlist *sgl,	unsigned int sg_len,
1161 	enum dma_transfer_direction dir, unsigned long flags,
1162 	struct rio_dma_ext *rio_ext)
1163 {
1164 	if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
1165 		return NULL;
1166 
1167 	return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
1168 						  dir, flags, rio_ext);
1169 }
1170 #endif
1171 
dmaengine_prep_dma_cyclic(struct dma_chan * chan,dma_addr_t buf_addr,size_t buf_len,size_t period_len,enum dma_transfer_direction dir,unsigned long flags)1172 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
1173 		struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1174 		size_t period_len, enum dma_transfer_direction dir,
1175 		unsigned long flags)
1176 {
1177 	if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic)
1178 		return NULL;
1179 
1180 	return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
1181 						period_len, dir, flags);
1182 }
1183 
dmaengine_prep_interleaved_dma(struct dma_chan * chan,struct dma_interleaved_template * xt,unsigned long flags)1184 static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
1185 		struct dma_chan *chan, struct dma_interleaved_template *xt,
1186 		unsigned long flags)
1187 {
1188 	if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma)
1189 		return NULL;
1190 	if (flags & DMA_PREP_REPEAT &&
1191 	    !test_bit(DMA_REPEAT, chan->device->cap_mask.bits))
1192 		return NULL;
1193 
1194 	return chan->device->device_prep_interleaved_dma(chan, xt, flags);
1195 }
1196 
1197 /**
1198  * dmaengine_prep_dma_memset() - Prepare a DMA memset descriptor.
1199  * @chan: The channel to be used for this descriptor
1200  * @dest: Address of buffer to be set
1201  * @value: Treated as a single byte value that fills the destination buffer
1202  * @len: The total size of dest
1203  * @flags: DMA engine flags
1204  */
dmaengine_prep_dma_memset(struct dma_chan * chan,dma_addr_t dest,int value,size_t len,unsigned long flags)1205 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
1206 		struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
1207 		unsigned long flags)
1208 {
1209 	if (!chan || !chan->device || !chan->device->device_prep_dma_memset)
1210 		return NULL;
1211 
1212 	return chan->device->device_prep_dma_memset(chan, dest, value,
1213 						    len, flags);
1214 }
1215 
dmaengine_prep_dma_memcpy(struct dma_chan * chan,dma_addr_t dest,dma_addr_t src,size_t len,unsigned long flags)1216 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
1217 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1218 		size_t len, unsigned long flags)
1219 {
1220 	if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy)
1221 		return NULL;
1222 
1223 	return chan->device->device_prep_dma_memcpy(chan, dest, src,
1224 						    len, flags);
1225 }
1226 
dmaengine_is_metadata_mode_supported(struct dma_chan * chan,enum dma_desc_metadata_mode mode)1227 static inline bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan,
1228 		enum dma_desc_metadata_mode mode)
1229 {
1230 	if (!chan)
1231 		return false;
1232 
1233 	return !!(chan->device->desc_metadata_modes & mode);
1234 }
1235 
1236 #ifdef CONFIG_DMA_ENGINE
1237 int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
1238 				   void *data, size_t len);
1239 void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
1240 				      size_t *payload_len, size_t *max_len);
1241 int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
1242 				    size_t payload_len);
1243 #else /* CONFIG_DMA_ENGINE */
dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor * desc,void * data,size_t len)1244 static inline int dmaengine_desc_attach_metadata(
1245 		struct dma_async_tx_descriptor *desc, void *data, size_t len)
1246 {
1247 	return -EINVAL;
1248 }
dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor * desc,size_t * payload_len,size_t * max_len)1249 static inline void *dmaengine_desc_get_metadata_ptr(
1250 		struct dma_async_tx_descriptor *desc, size_t *payload_len,
1251 		size_t *max_len)
1252 {
1253 	return NULL;
1254 }
dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor * desc,size_t payload_len)1255 static inline int dmaengine_desc_set_metadata_len(
1256 		struct dma_async_tx_descriptor *desc, size_t payload_len)
1257 {
1258 	return -EINVAL;
1259 }
1260 #endif /* CONFIG_DMA_ENGINE */
1261 
1262 /**
1263  * dmaengine_terminate_all() - Terminate all active DMA transfers
1264  * @chan: The channel for which to terminate the transfers
1265  *
1266  * This function is DEPRECATED use either dmaengine_terminate_sync() or
1267  * dmaengine_terminate_async() instead.
1268  */
dmaengine_terminate_all(struct dma_chan * chan)1269 static inline int dmaengine_terminate_all(struct dma_chan *chan)
1270 {
1271 	if (chan->device->device_terminate_all)
1272 		return chan->device->device_terminate_all(chan);
1273 
1274 	return -ENOSYS;
1275 }
1276 
1277 /**
1278  * dmaengine_terminate_async() - Terminate all active DMA transfers
1279  * @chan: The channel for which to terminate the transfers
1280  *
1281  * Calling this function will terminate all active and pending descriptors
1282  * that have previously been submitted to the channel. It is not guaranteed
1283  * though that the transfer for the active descriptor has stopped when the
1284  * function returns. Furthermore it is possible the complete callback of a
1285  * submitted transfer is still running when this function returns.
1286  *
1287  * dmaengine_synchronize() needs to be called before it is safe to free
1288  * any memory that is accessed by previously submitted descriptors or before
1289  * freeing any resources accessed from within the completion callback of any
1290  * previously submitted descriptors.
1291  *
1292  * This function can be called from atomic context as well as from within a
1293  * complete callback of a descriptor submitted on the same channel.
1294  *
1295  * If none of the two conditions above apply consider using
1296  * dmaengine_terminate_sync() instead.
1297  */
dmaengine_terminate_async(struct dma_chan * chan)1298 static inline int dmaengine_terminate_async(struct dma_chan *chan)
1299 {
1300 	if (chan->device->device_terminate_all)
1301 		return chan->device->device_terminate_all(chan);
1302 
1303 	return -EINVAL;
1304 }
1305 
1306 /**
1307  * dmaengine_synchronize() - Synchronize DMA channel termination
1308  * @chan: The channel to synchronize
1309  *
1310  * Synchronizes to the DMA channel termination to the current context. When this
1311  * function returns it is guaranteed that all transfers for previously issued
1312  * descriptors have stopped and it is safe to free the memory associated
1313  * with them. Furthermore it is guaranteed that all complete callback functions
1314  * for a previously submitted descriptor have finished running and it is safe to
1315  * free resources accessed from within the complete callbacks.
1316  *
1317  * The behavior of this function is undefined if dma_async_issue_pending() has
1318  * been called between dmaengine_terminate_async() and this function.
1319  *
1320  * This function must only be called from non-atomic context and must not be
1321  * called from within a complete callback of a descriptor submitted on the same
1322  * channel.
1323  */
dmaengine_synchronize(struct dma_chan * chan)1324 static inline void dmaengine_synchronize(struct dma_chan *chan)
1325 {
1326 	might_sleep();
1327 
1328 	if (chan->device->device_synchronize)
1329 		chan->device->device_synchronize(chan);
1330 }
1331 
1332 /**
1333  * dmaengine_terminate_sync() - Terminate all active DMA transfers
1334  * @chan: The channel for which to terminate the transfers
1335  *
1336  * Calling this function will terminate all active and pending transfers
1337  * that have previously been submitted to the channel. It is similar to
1338  * dmaengine_terminate_async() but guarantees that the DMA transfer has actually
1339  * stopped and that all complete callbacks have finished running when the
1340  * function returns.
1341  *
1342  * This function must only be called from non-atomic context and must not be
1343  * called from within a complete callback of a descriptor submitted on the same
1344  * channel.
1345  */
dmaengine_terminate_sync(struct dma_chan * chan)1346 static inline int dmaengine_terminate_sync(struct dma_chan *chan)
1347 {
1348 	int ret;
1349 
1350 	ret = dmaengine_terminate_async(chan);
1351 	if (ret)
1352 		return ret;
1353 
1354 	dmaengine_synchronize(chan);
1355 
1356 	return 0;
1357 }
1358 
dmaengine_pause(struct dma_chan * chan)1359 static inline int dmaengine_pause(struct dma_chan *chan)
1360 {
1361 	if (chan->device->device_pause)
1362 		return chan->device->device_pause(chan);
1363 
1364 	return -ENOSYS;
1365 }
1366 
dmaengine_resume(struct dma_chan * chan)1367 static inline int dmaengine_resume(struct dma_chan *chan)
1368 {
1369 	if (chan->device->device_resume)
1370 		return chan->device->device_resume(chan);
1371 
1372 	return -ENOSYS;
1373 }
1374 
dmaengine_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * state)1375 static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
1376 	dma_cookie_t cookie, struct dma_tx_state *state)
1377 {
1378 	return chan->device->device_tx_status(chan, cookie, state);
1379 }
1380 
dmaengine_submit(struct dma_async_tx_descriptor * desc)1381 static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
1382 {
1383 	return desc->tx_submit(desc);
1384 }
1385 
dmaengine_check_align(enum dmaengine_alignment align,size_t off1,size_t off2,size_t len)1386 static inline bool dmaengine_check_align(enum dmaengine_alignment align,
1387 					 size_t off1, size_t off2, size_t len)
1388 {
1389 	return !(((1 << align) - 1) & (off1 | off2 | len));
1390 }
1391 
is_dma_copy_aligned(struct dma_device * dev,size_t off1,size_t off2,size_t len)1392 static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
1393 				       size_t off2, size_t len)
1394 {
1395 	return dmaengine_check_align(dev->copy_align, off1, off2, len);
1396 }
1397 
is_dma_xor_aligned(struct dma_device * dev,size_t off1,size_t off2,size_t len)1398 static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
1399 				      size_t off2, size_t len)
1400 {
1401 	return dmaengine_check_align(dev->xor_align, off1, off2, len);
1402 }
1403 
is_dma_pq_aligned(struct dma_device * dev,size_t off1,size_t off2,size_t len)1404 static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
1405 				     size_t off2, size_t len)
1406 {
1407 	return dmaengine_check_align(dev->pq_align, off1, off2, len);
1408 }
1409 
is_dma_fill_aligned(struct dma_device * dev,size_t off1,size_t off2,size_t len)1410 static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
1411 				       size_t off2, size_t len)
1412 {
1413 	return dmaengine_check_align(dev->fill_align, off1, off2, len);
1414 }
1415 
1416 static inline void
dma_set_maxpq(struct dma_device * dma,int maxpq,int has_pq_continue)1417 dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
1418 {
1419 	dma->max_pq = maxpq;
1420 	if (has_pq_continue)
1421 		dma->max_pq |= DMA_HAS_PQ_CONTINUE;
1422 }
1423 
dmaf_continue(enum dma_ctrl_flags flags)1424 static inline bool dmaf_continue(enum dma_ctrl_flags flags)
1425 {
1426 	return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
1427 }
1428 
dmaf_p_disabled_continue(enum dma_ctrl_flags flags)1429 static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
1430 {
1431 	enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
1432 
1433 	return (flags & mask) == mask;
1434 }
1435 
dma_dev_has_pq_continue(struct dma_device * dma)1436 static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
1437 {
1438 	return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
1439 }
1440 
dma_dev_to_maxpq(struct dma_device * dma)1441 static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
1442 {
1443 	return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
1444 }
1445 
1446 /* dma_maxpq - reduce maxpq in the face of continued operations
1447  * @dma - dma device with PQ capability
1448  * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
1449  *
1450  * When an engine does not support native continuation we need 3 extra
1451  * source slots to reuse P and Q with the following coefficients:
1452  * 1/ {00} * P : remove P from Q', but use it as a source for P'
1453  * 2/ {01} * Q : use Q to continue Q' calculation
1454  * 3/ {00} * Q : subtract Q from P' to cancel (2)
1455  *
1456  * In the case where P is disabled we only need 1 extra source:
1457  * 1/ {01} * Q : use Q to continue Q' calculation
1458  */
dma_maxpq(struct dma_device * dma,enum dma_ctrl_flags flags)1459 static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
1460 {
1461 	if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
1462 		return dma_dev_to_maxpq(dma);
1463 	if (dmaf_p_disabled_continue(flags))
1464 		return dma_dev_to_maxpq(dma) - 1;
1465 	if (dmaf_continue(flags))
1466 		return dma_dev_to_maxpq(dma) - 3;
1467 	BUG();
1468 }
1469 
dmaengine_get_icg(bool inc,bool sgl,size_t icg,size_t dir_icg)1470 static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
1471 				      size_t dir_icg)
1472 {
1473 	if (inc) {
1474 		if (dir_icg)
1475 			return dir_icg;
1476 		if (sgl)
1477 			return icg;
1478 	}
1479 
1480 	return 0;
1481 }
1482 
dmaengine_get_dst_icg(struct dma_interleaved_template * xt,struct data_chunk * chunk)1483 static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt,
1484 					   struct data_chunk *chunk)
1485 {
1486 	return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl,
1487 				 chunk->icg, chunk->dst_icg);
1488 }
1489 
dmaengine_get_src_icg(struct dma_interleaved_template * xt,struct data_chunk * chunk)1490 static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt,
1491 					   struct data_chunk *chunk)
1492 {
1493 	return dmaengine_get_icg(xt->src_inc, xt->src_sgl,
1494 				 chunk->icg, chunk->src_icg);
1495 }
1496 
1497 /* --- public DMA engine API --- */
1498 
1499 #ifdef CONFIG_DMA_ENGINE
1500 void dmaengine_get(void);
1501 void dmaengine_put(void);
1502 #else
dmaengine_get(void)1503 static inline void dmaengine_get(void)
1504 {
1505 }
dmaengine_put(void)1506 static inline void dmaengine_put(void)
1507 {
1508 }
1509 #endif
1510 
1511 #ifdef CONFIG_ASYNC_TX_DMA
1512 #define async_dmaengine_get()	dmaengine_get()
1513 #define async_dmaengine_put()	dmaengine_put()
1514 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
1515 #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
1516 #else
1517 #define async_dma_find_channel(type) dma_find_channel(type)
1518 #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
1519 #else
async_dmaengine_get(void)1520 static inline void async_dmaengine_get(void)
1521 {
1522 }
async_dmaengine_put(void)1523 static inline void async_dmaengine_put(void)
1524 {
1525 }
1526 static inline struct dma_chan *
async_dma_find_channel(enum dma_transaction_type type)1527 async_dma_find_channel(enum dma_transaction_type type)
1528 {
1529 	return NULL;
1530 }
1531 #endif /* CONFIG_ASYNC_TX_DMA */
1532 void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1533 				  struct dma_chan *chan);
1534 
async_tx_ack(struct dma_async_tx_descriptor * tx)1535 static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
1536 {
1537 	tx->flags |= DMA_CTRL_ACK;
1538 }
1539 
async_tx_clear_ack(struct dma_async_tx_descriptor * tx)1540 static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
1541 {
1542 	tx->flags &= ~DMA_CTRL_ACK;
1543 }
1544 
async_tx_test_ack(struct dma_async_tx_descriptor * tx)1545 static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
1546 {
1547 	return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
1548 }
1549 
1550 #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
1551 static inline void
__dma_cap_set(enum dma_transaction_type tx_type,dma_cap_mask_t * dstp)1552 __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
1553 {
1554 	set_bit(tx_type, dstp->bits);
1555 }
1556 
1557 #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
1558 static inline void
__dma_cap_clear(enum dma_transaction_type tx_type,dma_cap_mask_t * dstp)1559 __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
1560 {
1561 	clear_bit(tx_type, dstp->bits);
1562 }
1563 
1564 #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
__dma_cap_zero(dma_cap_mask_t * dstp)1565 static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
1566 {
1567 	bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
1568 }
1569 
1570 #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
1571 static inline int
__dma_has_cap(enum dma_transaction_type tx_type,dma_cap_mask_t * srcp)1572 __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
1573 {
1574 	return test_bit(tx_type, srcp->bits);
1575 }
1576 
1577 #define for_each_dma_cap_mask(cap, mask) \
1578 	for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
1579 
1580 /**
1581  * dma_async_issue_pending - flush pending transactions to HW
1582  * @chan: target DMA channel
1583  *
1584  * This allows drivers to push copies to HW in batches,
1585  * reducing MMIO writes where possible.
1586  */
dma_async_issue_pending(struct dma_chan * chan)1587 static inline void dma_async_issue_pending(struct dma_chan *chan)
1588 {
1589 	chan->device->device_issue_pending(chan);
1590 }
1591 
1592 /**
1593  * dma_async_is_tx_complete - poll for transaction completion
1594  * @chan: DMA channel
1595  * @cookie: transaction identifier to check status of
1596  * @last: returns last completed cookie, can be NULL
1597  * @used: returns last issued cookie, can be NULL
1598  *
1599  * If @last and @used are passed in, upon return they reflect the driver
1600  * internal state and can be used with dma_async_is_complete() to check
1601  * the status of multiple cookies without re-checking hardware state.
1602  */
dma_async_is_tx_complete(struct dma_chan * chan,dma_cookie_t cookie,dma_cookie_t * last,dma_cookie_t * used)1603 static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
1604 	dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
1605 {
1606 	struct dma_tx_state state;
1607 	enum dma_status status;
1608 
1609 	status = chan->device->device_tx_status(chan, cookie, &state);
1610 	if (last)
1611 		*last = state.last;
1612 	if (used)
1613 		*used = state.used;
1614 	return status;
1615 }
1616 
1617 /**
1618  * dma_async_is_complete - test a cookie against chan state
1619  * @cookie: transaction identifier to test status of
1620  * @last_complete: last know completed transaction
1621  * @last_used: last cookie value handed out
1622  *
1623  * dma_async_is_complete() is used in dma_async_is_tx_complete()
1624  * the test logic is separated for lightweight testing of multiple cookies
1625  */
dma_async_is_complete(dma_cookie_t cookie,dma_cookie_t last_complete,dma_cookie_t last_used)1626 static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
1627 			dma_cookie_t last_complete, dma_cookie_t last_used)
1628 {
1629 	if (last_complete <= last_used) {
1630 		if ((cookie <= last_complete) || (cookie > last_used))
1631 			return DMA_COMPLETE;
1632 	} else {
1633 		if ((cookie <= last_complete) && (cookie > last_used))
1634 			return DMA_COMPLETE;
1635 	}
1636 	return DMA_IN_PROGRESS;
1637 }
1638 
1639 static inline void
dma_set_tx_state(struct dma_tx_state * st,dma_cookie_t last,dma_cookie_t used,u32 residue)1640 dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
1641 {
1642 	if (!st)
1643 		return;
1644 
1645 	st->last = last;
1646 	st->used = used;
1647 	st->residue = residue;
1648 }
1649 
1650 #ifdef CONFIG_DMA_ENGINE
1651 struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
1652 enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
1653 enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
1654 void dma_issue_pending_all(void);
1655 struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1656 				       dma_filter_fn fn, void *fn_param,
1657 				       struct device_node *np);
1658 
1659 struct dma_chan *dma_request_chan(struct device *dev, const char *name);
1660 struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
1661 struct dma_chan *devm_dma_request_chan(struct device *dev, const char *name);
1662 
1663 void dma_release_channel(struct dma_chan *chan);
1664 int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
1665 #else
dma_find_channel(enum dma_transaction_type tx_type)1666 static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
1667 {
1668 	return NULL;
1669 }
dma_sync_wait(struct dma_chan * chan,dma_cookie_t cookie)1670 static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
1671 {
1672 	return DMA_COMPLETE;
1673 }
dma_wait_for_async_tx(struct dma_async_tx_descriptor * tx)1674 static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1675 {
1676 	return DMA_COMPLETE;
1677 }
dma_issue_pending_all(void)1678 static inline void dma_issue_pending_all(void)
1679 {
1680 }
__dma_request_channel(const dma_cap_mask_t * mask,dma_filter_fn fn,void * fn_param,struct device_node * np)1681 static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1682 						     dma_filter_fn fn,
1683 						     void *fn_param,
1684 						     struct device_node *np)
1685 {
1686 	return NULL;
1687 }
dma_request_chan(struct device * dev,const char * name)1688 static inline struct dma_chan *dma_request_chan(struct device *dev,
1689 						const char *name)
1690 {
1691 	return ERR_PTR(-ENODEV);
1692 }
dma_request_chan_by_mask(const dma_cap_mask_t * mask)1693 static inline struct dma_chan *dma_request_chan_by_mask(
1694 						const dma_cap_mask_t *mask)
1695 {
1696 	return ERR_PTR(-ENODEV);
1697 }
1698 
devm_dma_request_chan(struct device * dev,const char * name)1699 static inline struct dma_chan *devm_dma_request_chan(struct device *dev, const char *name)
1700 {
1701 	return ERR_PTR(-ENODEV);
1702 }
1703 
dma_release_channel(struct dma_chan * chan)1704 static inline void dma_release_channel(struct dma_chan *chan)
1705 {
1706 }
dma_get_slave_caps(struct dma_chan * chan,struct dma_slave_caps * caps)1707 static inline int dma_get_slave_caps(struct dma_chan *chan,
1708 				     struct dma_slave_caps *caps)
1709 {
1710 	return -ENXIO;
1711 }
1712 #endif
1713 
dmaengine_desc_set_reuse(struct dma_async_tx_descriptor * tx)1714 static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
1715 {
1716 	struct dma_slave_caps caps;
1717 	int ret;
1718 
1719 	ret = dma_get_slave_caps(tx->chan, &caps);
1720 	if (ret)
1721 		return ret;
1722 
1723 	if (!caps.descriptor_reuse)
1724 		return -EPERM;
1725 
1726 	tx->flags |= DMA_CTRL_REUSE;
1727 	return 0;
1728 }
1729 
dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor * tx)1730 static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
1731 {
1732 	tx->flags &= ~DMA_CTRL_REUSE;
1733 }
1734 
dmaengine_desc_test_reuse(struct dma_async_tx_descriptor * tx)1735 static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
1736 {
1737 	return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE;
1738 }
1739 
dmaengine_desc_free(struct dma_async_tx_descriptor * desc)1740 static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
1741 {
1742 	/* this is supported for reusable desc, so check that */
1743 	if (!dmaengine_desc_test_reuse(desc))
1744 		return -EPERM;
1745 
1746 	return desc->desc_free(desc);
1747 }
1748 
1749 /* --- DMA device --- */
1750 
1751 int dma_async_device_register(struct dma_device *device);
1752 int dmaenginem_async_device_register(struct dma_device *device);
1753 void dma_async_device_unregister(struct dma_device *device);
1754 int dma_async_device_channel_register(struct dma_device *device,
1755 				      struct dma_chan *chan,
1756 				      const char *name);
1757 void dma_async_device_channel_unregister(struct dma_device *device,
1758 					 struct dma_chan *chan);
1759 void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
1760 #define dma_request_channel(mask, x, y) \
1761 	__dma_request_channel(&(mask), x, y, NULL)
1762 
1763 /* Deprecated, please use dma_request_chan() directly */
1764 static inline struct dma_chan * __deprecated
dma_request_slave_channel(struct device * dev,const char * name)1765 dma_request_slave_channel(struct device *dev, const char *name)
1766 {
1767 	struct dma_chan *ch = dma_request_chan(dev, name);
1768 
1769 	return IS_ERR(ch) ? NULL : ch;
1770 }
1771 
1772 static inline struct dma_chan
dma_request_slave_channel_compat(const dma_cap_mask_t mask,dma_filter_fn fn,void * fn_param,struct device * dev,const char * name)1773 *dma_request_slave_channel_compat(const dma_cap_mask_t mask,
1774 				  dma_filter_fn fn, void *fn_param,
1775 				  struct device *dev, const char *name)
1776 {
1777 	struct dma_chan *chan;
1778 
1779 	chan = dma_request_chan(dev, name);
1780 	if (!IS_ERR(chan))
1781 		return chan;
1782 
1783 	if (!fn || !fn_param)
1784 		return NULL;
1785 
1786 	return dma_request_channel(mask, fn, fn_param);
1787 }
1788 
1789 static inline char *
dmaengine_get_direction_text(enum dma_transfer_direction dir)1790 dmaengine_get_direction_text(enum dma_transfer_direction dir)
1791 {
1792 	switch (dir) {
1793 	case DMA_DEV_TO_MEM:
1794 		return "DEV_TO_MEM";
1795 	case DMA_MEM_TO_DEV:
1796 		return "MEM_TO_DEV";
1797 	case DMA_MEM_TO_MEM:
1798 		return "MEM_TO_MEM";
1799 	case DMA_DEV_TO_DEV:
1800 		return "DEV_TO_DEV";
1801 	default:
1802 		return "invalid";
1803 	}
1804 }
1805 
dmaengine_get_dma_device(struct dma_chan * chan)1806 static inline struct device *dmaengine_get_dma_device(struct dma_chan *chan)
1807 {
1808 	if (chan->dev->chan_dma_dev)
1809 		return &chan->dev->device;
1810 
1811 	return chan->device->dev;
1812 }
1813 
1814 #endif /* DMAENGINE_H */
1815