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