]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/hwspinlock.git/blob - include/linux/dmaengine.h
d33a953c9b55fcfe6ba9c4a0471fdabbc70051b2
[rpmsg/hwspinlock.git] / include / linux / dmaengine.h
1 /*
2  * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * The full GNU General Public License is included in this distribution in the
15  * file called COPYING.
16  */
17 #ifndef LINUX_DMAENGINE_H
18 #define LINUX_DMAENGINE_H
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/uio.h>
23 #include <linux/bug.h>
24 #include <linux/scatterlist.h>
25 #include <linux/bitmap.h>
26 #include <linux/types.h>
27 #include <asm/page.h>
29 /**
30  * typedef dma_cookie_t - an opaque DMA cookie
31  *
32  * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
33  */
34 typedef s32 dma_cookie_t;
35 #define DMA_MIN_COOKIE  1
37 static inline int dma_submit_error(dma_cookie_t cookie)
38 {
39         return cookie < 0 ? cookie : 0;
40 }
42 /**
43  * enum dma_status - DMA transaction status
44  * @DMA_COMPLETE: transaction completed
45  * @DMA_IN_PROGRESS: transaction not yet processed
46  * @DMA_PAUSED: transaction is paused
47  * @DMA_ERROR: transaction failed
48  */
49 enum dma_status {
50         DMA_COMPLETE,
51         DMA_IN_PROGRESS,
52         DMA_PAUSED,
53         DMA_ERROR,
54 };
56 /**
57  * enum dma_transaction_type - DMA transaction types/indexes
58  *
59  * Note: The DMA_ASYNC_TX capability is not to be set by drivers.  It is
60  * automatically set as dma devices are registered.
61  */
62 enum dma_transaction_type {
63         DMA_MEMCPY,
64         DMA_XOR,
65         DMA_PQ,
66         DMA_XOR_VAL,
67         DMA_PQ_VAL,
68         DMA_MEMSET,
69         DMA_MEMSET_SG,
70         DMA_INTERRUPT,
71         DMA_PRIVATE,
72         DMA_ASYNC_TX,
73         DMA_SLAVE,
74         DMA_CYCLIC,
75         DMA_INTERLEAVE,
76 /* last transaction type for creation of the capabilities mask */
77         DMA_TX_TYPE_END,
78 };
80 /**
81  * enum dma_transfer_direction - dma transfer mode and direction indicator
82  * @DMA_MEM_TO_MEM: Async/Memcpy mode
83  * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device
84  * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory
85  * @DMA_DEV_TO_DEV: Slave mode & From Device to Device
86  */
87 enum dma_transfer_direction {
88         DMA_MEM_TO_MEM,
89         DMA_MEM_TO_DEV,
90         DMA_DEV_TO_MEM,
91         DMA_DEV_TO_DEV,
92         DMA_TRANS_NONE,
93 };
95 /**
96  * Interleaved Transfer Request
97  * ----------------------------
98  * A chunk is collection of contiguous bytes to be transfered.
99  * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG).
100  * ICGs may or maynot change between chunks.
101  * A FRAME is the smallest series of contiguous {chunk,icg} pairs,
102  *  that when repeated an integral number of times, specifies the transfer.
103  * A transfer template is specification of a Frame, the number of times
104  *  it is to be repeated and other per-transfer attributes.
105  *
106  * Practically, a client driver would have ready a template for each
107  *  type of transfer it is going to need during its lifetime and
108  *  set only 'src_start' and 'dst_start' before submitting the requests.
109  *
110  *
111  *  |      Frame-1        |       Frame-2       | ~ |       Frame-'numf'  |
112  *  |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...|
113  *
114  *    ==  Chunk size
115  *    ... ICG
116  */
118 /**
119  * struct data_chunk - Element of scatter-gather list that makes a frame.
120  * @size: Number of bytes to read from source.
121  *        size_dst := fn(op, size_src), so doesn't mean much for destination.
122  * @icg: Number of bytes to jump after last src/dst address of this
123  *       chunk and before first src/dst address for next chunk.
124  *       Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false.
125  *       Ignored for src(assumed 0), if src_inc is true and src_sgl is false.
126  * @dst_icg: Number of bytes to jump after last dst address of this
127  *       chunk and before the first dst address for next chunk.
128  *       Ignored if dst_inc is true and dst_sgl is false.
129  * @src_icg: Number of bytes to jump after last src address of this
130  *       chunk and before the first src address for next chunk.
131  *       Ignored if src_inc is true and src_sgl is false.
132  */
133 struct data_chunk {
134         size_t size;
135         size_t icg;
136         size_t dst_icg;
137         size_t src_icg;
138 };
140 /**
141  * struct dma_interleaved_template - Template to convey DMAC the transfer pattern
142  *       and attributes.
143  * @src_start: Bus address of source for the first chunk.
144  * @dst_start: Bus address of destination for the first chunk.
145  * @dir: Specifies the type of Source and Destination.
146  * @src_inc: If the source address increments after reading from it.
147  * @dst_inc: If the destination address increments after writing to it.
148  * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read).
149  *              Otherwise, source is read contiguously (icg ignored).
150  *              Ignored if src_inc is false.
151  * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write).
152  *              Otherwise, destination is filled contiguously (icg ignored).
153  *              Ignored if dst_inc is false.
154  * @numf: Number of frames in this template.
155  * @frame_size: Number of chunks in a frame i.e, size of sgl[].
156  * @sgl: Array of {chunk,icg} pairs that make up a frame.
157  */
158 struct dma_interleaved_template {
159         dma_addr_t src_start;
160         dma_addr_t dst_start;
161         enum dma_transfer_direction dir;
162         bool src_inc;
163         bool dst_inc;
164         bool src_sgl;
165         bool dst_sgl;
166         size_t numf;
167         size_t frame_size;
168         struct data_chunk sgl[0];
169 };
171 /**
172  * enum dma_ctrl_flags - DMA flags to augment operation preparation,
173  *  control completion, and communicate status.
174  * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
175  *  this transaction
176  * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
177  *  acknowledges receipt, i.e. has has a chance to establish any dependency
178  *  chains
179  * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
180  * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
181  * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
182  *  sources that were the result of a previous operation, in the case of a PQ
183  *  operation it continues the calculation with new sources
184  * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
185  *  on the result of this operation
186  * @DMA_CTRL_REUSE: client can reuse the descriptor and submit again till
187  *  cleared or freed
188  * @DMA_PREP_CMD: tell the driver that the data passed to DMA API is command
189  *  data and the descriptor should be in different format from normal
190  *  data descriptors.
191  */
192 enum dma_ctrl_flags {
193         DMA_PREP_INTERRUPT = (1 << 0),
194         DMA_CTRL_ACK = (1 << 1),
195         DMA_PREP_PQ_DISABLE_P = (1 << 2),
196         DMA_PREP_PQ_DISABLE_Q = (1 << 3),
197         DMA_PREP_CONTINUE = (1 << 4),
198         DMA_PREP_FENCE = (1 << 5),
199         DMA_CTRL_REUSE = (1 << 6),
200         DMA_PREP_CMD = (1 << 7),
201 };
203 /**
204  * enum sum_check_bits - bit position of pq_check_flags
205  */
206 enum sum_check_bits {
207         SUM_CHECK_P = 0,
208         SUM_CHECK_Q = 1,
209 };
211 /**
212  * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
213  * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
214  * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
215  */
216 enum sum_check_flags {
217         SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
218         SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
219 };
222 /**
223  * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
224  * See linux/cpumask.h
225  */
226 typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
228 /**
229  * struct dma_chan_percpu - the per-CPU part of struct dma_chan
230  * @memcpy_count: transaction counter
231  * @bytes_transferred: byte counter
232  */
234 /**
235  * enum dma_desc_metadata_mode - per descriptor metadata mode types supported
236  * @DESC_METADATA_CLIENT - the metadata buffer is allocated/provided by the
237  *  client driver and it is attached (via the dmaengine_desc_attach_metadata()
238  *  helper) to the descriptor.
239  *
240  * Client drivers interested to use this mode can follow:
241  * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
242  *   1. prepare the descriptor (dmaengine_prep_*)
243  *      construct the metadata in the client's buffer
244  *   2. use dmaengine_desc_attach_metadata() to attach the buffer to the
245  *      descriptor
246  *   3. submit the transfer
247  * - DMA_DEV_TO_MEM:
248  *   1. prepare the descriptor (dmaengine_prep_*)
249  *   2. use dmaengine_desc_attach_metadata() to attach the buffer to the
250  *      descriptor
251  *   3. submit the transfer
252  *   4. when the transfer is completed, the metadata should be available in the
253  *      attached buffer
254  *
255  * @DESC_METADATA_ENGINE - the metadata buffer is allocated/managed by the DMA
256  *  driver. The client driver can ask for the pointer, maximum size and the
257  *  currently used size of the metadata and can directly update or read it.
258  *  dmaengine_desc_get_metadata_ptr() and dmaengine_desc_set_metadata_len() is
259  *  provided as helper functions.
260  *
261  * Client drivers interested to use this mode can follow:
262  * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
263  *   1. prepare the descriptor (dmaengine_prep_*)
264  *   2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the engine's
265  *      metadata area
266  *   3. update the metadata at the pointer
267  *   4. use dmaengine_desc_set_metadata_len()  to tell the DMA engine the amount
268  *      of data the client has placed into the metadata buffer
269  *   5. submit the transfer
270  * - DMA_DEV_TO_MEM:
271  *   1. prepare the descriptor (dmaengine_prep_*)
272  *   2. submit the transfer
273  *   3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get the
274  *      pointer to the engine's metadata are
275  *   4. Read out the metadate from the pointer
276  *
277  * Note: the two mode is not compatible and clients must use one mode for a
278  * descriptor.
279  */
280 enum dma_desc_metadata_mode {
281         DESC_METADATA_NONE = 0,
282         DESC_METADATA_CLIENT = BIT(0),
283         DESC_METADATA_ENGINE = BIT(1),
284 };
286 struct dma_chan_percpu {
287         /* stats */
288         unsigned long memcpy_count;
289         unsigned long bytes_transferred;
290 };
292 /**
293  * struct dma_router - DMA router structure
294  * @dev: pointer to the DMA router device
295  * @route_free: function to be called when the route can be disconnected
296  */
297 struct dma_router {
298         struct device *dev;
299         void (*route_free)(struct device *dev, void *route_data);
300 };
302 /**
303  * struct dma_chan - devices supply DMA channels, clients use them
304  * @device: ptr to the dma device who supplies this channel, always !%NULL
305  * @cookie: last cookie value returned to client
306  * @completed_cookie: last completed cookie for this channel
307  * @chan_id: channel ID for sysfs
308  * @dev: class device for sysfs
309  * @device_node: used to add this to the device chan list
310  * @local: per-cpu pointer to a struct dma_chan_percpu
311  * @client_count: how many clients are using this channel
312  * @table_count: number of appearances in the mem-to-mem allocation table
313  * @router: pointer to the DMA router structure
314  * @route_data: channel specific data for the router
315  * @private: private data for certain client-channel associations
316  */
317 struct dma_chan {
318         struct dma_device *device;
319         dma_cookie_t cookie;
320         dma_cookie_t completed_cookie;
322         /* sysfs */
323         int chan_id;
324         struct dma_chan_dev *dev;
326         struct list_head device_node;
327         struct dma_chan_percpu __percpu *local;
328         int client_count;
329         int table_count;
331         /* DMA router */
332         struct dma_router *router;
333         void *route_data;
335         void *private;
336 };
338 /**
339  * struct dma_chan_dev - relate sysfs device node to backing channel device
340  * @chan: driver channel device
341  * @device: sysfs device
342  * @dev_id: parent dma_device dev_id
343  * @idr_ref: reference count to gate release of dma_device dev_id
344  */
345 struct dma_chan_dev {
346         struct dma_chan *chan;
347         struct device device;
348         int dev_id;
349         atomic_t *idr_ref;
350 };
352 /**
353  * enum dma_slave_buswidth - defines bus width of the DMA slave
354  * device, source or target buses
355  */
356 enum dma_slave_buswidth {
357         DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
358         DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
359         DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
360         DMA_SLAVE_BUSWIDTH_3_BYTES = 3,
361         DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
362         DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
363         DMA_SLAVE_BUSWIDTH_16_BYTES = 16,
364         DMA_SLAVE_BUSWIDTH_32_BYTES = 32,
365         DMA_SLAVE_BUSWIDTH_64_BYTES = 64,
366 };
368 /**
369  * struct dma_slave_config - dma slave channel runtime config
370  * @direction: whether the data shall go in or out on this slave
371  * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are
372  * legal values. DEPRECATED, drivers should use the direction argument
373  * to the device_prep_slave_sg and device_prep_dma_cyclic functions or
374  * the dir field in the dma_interleaved_template structure.
375  * @src_addr: this is the physical address where DMA slave data
376  * should be read (RX), if the source is memory this argument is
377  * ignored.
378  * @dst_addr: this is the physical address where DMA slave data
379  * should be written (TX), if the source is memory this argument
380  * is ignored.
381  * @src_addr_width: this is the width in bytes of the source (RX)
382  * register where DMA data shall be read. If the source
383  * is memory this may be ignored depending on architecture.
384  * Legal values: 1, 2, 3, 4, 8, 16, 32, 64.
385  * @dst_addr_width: same as src_addr_width but for destination
386  * target (TX) mutatis mutandis.
387  * @src_maxburst: the maximum number of words (note: words, as in
388  * units of the src_addr_width member, not bytes) that can be sent
389  * in one burst to the device. Typically something like half the
390  * FIFO depth on I/O peripherals so you don't overflow it. This
391  * may or may not be applicable on memory sources.
392  * @dst_maxburst: same as src_maxburst but for destination target
393  * mutatis mutandis.
394  * @src_port_window_size: The length of the register area in words the data need
395  * to be accessed on the device side. It is only used for devices which is using
396  * an area instead of a single register to receive the data. Typically the DMA
397  * loops in this area in order to transfer the data.
398  * @dst_port_window_size: same as src_port_window_size but for the destination
399  * port.
400  * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill
401  * with 'true' if peripheral should be flow controller. Direction will be
402  * selected at Runtime.
403  * @slave_id: Slave requester id. Only valid for slave channels. The dma
404  * slave peripheral will have unique id as dma requester which need to be
405  * pass as slave config.
406  *
407  * This struct is passed in as configuration data to a DMA engine
408  * in order to set up a certain channel for DMA transport at runtime.
409  * The DMA device/engine has to provide support for an additional
410  * callback in the dma_device structure, device_config and this struct
411  * will then be passed in as an argument to the function.
412  *
413  * The rationale for adding configuration information to this struct is as
414  * follows: if it is likely that more than one DMA slave controllers in
415  * the world will support the configuration option, then make it generic.
416  * If not: if it is fixed so that it be sent in static from the platform
417  * data, then prefer to do that.
418  */
419 struct dma_slave_config {
420         enum dma_transfer_direction direction;
421         phys_addr_t src_addr;
422         phys_addr_t dst_addr;
423         enum dma_slave_buswidth src_addr_width;
424         enum dma_slave_buswidth dst_addr_width;
425         u32 src_maxburst;
426         u32 dst_maxburst;
427         u32 src_port_window_size;
428         u32 dst_port_window_size;
429         bool device_fc;
430         unsigned int slave_id;
431 };
433 /**
434  * enum dma_residue_granularity - Granularity of the reported transfer residue
435  * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The
436  *  DMA channel is only able to tell whether a descriptor has been completed or
437  *  not, which means residue reporting is not supported by this channel. The
438  *  residue field of the dma_tx_state field will always be 0.
439  * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully
440  *  completed segment of the transfer (For cyclic transfers this is after each
441  *  period). This is typically implemented by having the hardware generate an
442  *  interrupt after each transferred segment and then the drivers updates the
443  *  outstanding residue by the size of the segment. Another possibility is if
444  *  the hardware supports scatter-gather and the segment descriptor has a field
445  *  which gets set after the segment has been completed. The driver then counts
446  *  the number of segments without the flag set to compute the residue.
447  * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred
448  *  burst. This is typically only supported if the hardware has a progress
449  *  register of some sort (E.g. a register with the current read/write address
450  *  or a register with the amount of bursts/beats/bytes that have been
451  *  transferred or still need to be transferred).
452  */
453 enum dma_residue_granularity {
454         DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0,
455         DMA_RESIDUE_GRANULARITY_SEGMENT = 1,
456         DMA_RESIDUE_GRANULARITY_BURST = 2,
457 };
459 /**
460  * struct dma_slave_caps - expose capabilities of a slave channel only
461  * @src_addr_widths: bit mask of src addr widths the channel supports.
462  *      Width is specified in bytes, e.g. for a channel supporting
463  *      a width of 4 the mask should have BIT(4) set.
464  * @dst_addr_widths: bit mask of dst addr widths the channel supports
465  * @directions: bit mask of slave directions the channel supports.
466  *      Since the enum dma_transfer_direction is not defined as bit flag for
467  *      each type, the dma controller should set BIT(<TYPE>) and same
468  *      should be checked by controller as well
469  * @max_burst: max burst capability per-transfer
470  * @cmd_pause: true, if pause is supported (i.e. for reading residue or
471  *             for resume later)
472  * @cmd_resume: true, if resume is supported
473  * @cmd_terminate: true, if terminate cmd is supported
474  * @residue_granularity: granularity of the reported transfer residue
475  * @descriptor_reuse: if a descriptor can be reused by client and
476  * resubmitted multiple times
477  */
478 struct dma_slave_caps {
479         u32 src_addr_widths;
480         u32 dst_addr_widths;
481         u32 directions;
482         u32 max_burst;
483         bool cmd_pause;
484         bool cmd_resume;
485         bool cmd_terminate;
486         enum dma_residue_granularity residue_granularity;
487         bool descriptor_reuse;
488 };
490 static inline const char *dma_chan_name(struct dma_chan *chan)
492         return dev_name(&chan->dev->device);
495 void dma_chan_cleanup(struct kref *kref);
497 /**
498  * typedef dma_filter_fn - callback filter for dma_request_channel
499  * @chan: channel to be reviewed
500  * @filter_param: opaque parameter passed through dma_request_channel
501  *
502  * When this optional parameter is specified in a call to dma_request_channel a
503  * suitable channel is passed to this routine for further dispositioning before
504  * being returned.  Where 'suitable' indicates a non-busy channel that
505  * satisfies the given capability mask.  It returns 'true' to indicate that the
506  * channel is suitable.
507  */
508 typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
510 typedef void (*dma_async_tx_callback)(void *dma_async_param);
512 enum dmaengine_tx_result {
513         DMA_TRANS_NOERROR = 0,          /* SUCCESS */
514         DMA_TRANS_READ_FAILED,          /* Source DMA read failed */
515         DMA_TRANS_WRITE_FAILED,         /* Destination DMA write failed */
516         DMA_TRANS_ABORTED,              /* Op never submitted / aborted */
517 };
519 struct dmaengine_result {
520         enum dmaengine_tx_result result;
521         u32 residue;
522 };
524 typedef void (*dma_async_tx_callback_result)(void *dma_async_param,
525                                 const struct dmaengine_result *result);
527 struct dmaengine_unmap_data {
528 #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID)
529         u16 map_cnt;
530 #else
531         u8 map_cnt;
532 #endif
533         u8 to_cnt;
534         u8 from_cnt;
535         u8 bidi_cnt;
536         struct device *dev;
537         struct kref kref;
538         size_t len;
539         dma_addr_t addr[0];
540 };
542 struct dma_async_tx_descriptor;
544 struct dma_descriptor_metadata_ops {
545         int (*attach)(struct dma_async_tx_descriptor *desc, void *data,
546                       size_t len);
548         void *(*get_ptr)(struct dma_async_tx_descriptor *desc,
549                          size_t *payload_len, size_t *max_len);
550         int (*set_len)(struct dma_async_tx_descriptor *desc,
551                        size_t payload_len);
552 };
554 /**
555  * struct dma_async_tx_descriptor - async transaction descriptor
556  * ---dma generic offload fields---
557  * @cookie: tracking cookie for this transaction, set to -EBUSY if
558  *      this tx is sitting on a dependency list
559  * @flags: flags to augment operation preparation, control completion, and
560  *      communicate status
561  * @phys: physical address of the descriptor
562  * @chan: target channel for this operation
563  * @tx_submit: accept the descriptor, assign ordered cookie and mark the
564  * descriptor pending. To be pushed on .issue_pending() call
565  * @callback: routine to call after this operation is complete
566  * @callback_param: general parameter to pass to the callback routine
567  * @desc_metadata_mode: core managed metadata mode to protect mixed use of
568  *      DESC_METADATA_CLIENT or DESC_METADATA_ENGINE. Otherwise
569  *      DESC_METADATA_NONE
570  * @metadata_ops: DMA driver provided metadata mode ops, need to be set by the
571  *      DMA driver if metadata mode is supported with the descriptor
572  * ---async_tx api specific fields---
573  * @next: at completion submit this descriptor
574  * @parent: pointer to the next level up in the dependency chain
575  * @lock: protect the parent and next pointers
576  */
577 struct dma_async_tx_descriptor {
578         dma_cookie_t cookie;
579         enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
580         dma_addr_t phys;
581         struct dma_chan *chan;
582         dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
583         int (*desc_free)(struct dma_async_tx_descriptor *tx);
584         dma_async_tx_callback callback;
585         dma_async_tx_callback_result callback_result;
586         void *callback_param;
587         struct dmaengine_unmap_data *unmap;
588         enum dma_desc_metadata_mode desc_metadata_mode;
589         struct dma_descriptor_metadata_ops *metadata_ops;
590 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
591         struct dma_async_tx_descriptor *next;
592         struct dma_async_tx_descriptor *parent;
593         spinlock_t lock;
594 #endif
595 };
597 #ifdef CONFIG_DMA_ENGINE
598 static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
599                                  struct dmaengine_unmap_data *unmap)
601         kref_get(&unmap->kref);
602         tx->unmap = unmap;
605 struct dmaengine_unmap_data *
606 dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
607 void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
608 #else
609 static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
610                                  struct dmaengine_unmap_data *unmap)
613 static inline struct dmaengine_unmap_data *
614 dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
616         return NULL;
618 static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
621 #endif
623 static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
625         if (tx->unmap) {
626                 dmaengine_unmap_put(tx->unmap);
627                 tx->unmap = NULL;
628         }
631 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
632 static inline void txd_lock(struct dma_async_tx_descriptor *txd)
635 static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
638 static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
640         BUG();
642 static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
645 static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
648 static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
650         return NULL;
652 static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
654         return NULL;
657 #else
658 static inline void txd_lock(struct dma_async_tx_descriptor *txd)
660         spin_lock_bh(&txd->lock);
662 static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
664         spin_unlock_bh(&txd->lock);
666 static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
668         txd->next = next;
669         next->parent = txd;
671 static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
673         txd->parent = NULL;
675 static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
677         txd->next = NULL;
679 static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
681         return txd->parent;
683 static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
685         return txd->next;
687 #endif
689 /**
690  * struct dma_tx_state - filled in to report the status of
691  * a transfer.
692  * @last: last completed DMA cookie
693  * @used: last issued DMA cookie (i.e. the one in progress)
694  * @residue: the remaining number of bytes left to transmit
695  *      on the selected transfer for states DMA_IN_PROGRESS and
696  *      DMA_PAUSED if this is implemented in the driver, else 0
697  */
698 struct dma_tx_state {
699         dma_cookie_t last;
700         dma_cookie_t used;
701         u32 residue;
702 };
704 /**
705  * enum dmaengine_alignment - defines alignment of the DMA async tx
706  * buffers
707  */
708 enum dmaengine_alignment {
709         DMAENGINE_ALIGN_1_BYTE = 0,
710         DMAENGINE_ALIGN_2_BYTES = 1,
711         DMAENGINE_ALIGN_4_BYTES = 2,
712         DMAENGINE_ALIGN_8_BYTES = 3,
713         DMAENGINE_ALIGN_16_BYTES = 4,
714         DMAENGINE_ALIGN_32_BYTES = 5,
715         DMAENGINE_ALIGN_64_BYTES = 6,
716 };
718 /**
719  * struct dma_slave_map - associates slave device and it's slave channel with
720  * parameter to be used by a filter function
721  * @devname: name of the device
722  * @slave: slave channel name
723  * @param: opaque parameter to pass to struct dma_filter.fn
724  */
725 struct dma_slave_map {
726         const char *devname;
727         const char *slave;
728         void *param;
729 };
731 /**
732  * struct dma_filter - information for slave device/channel to filter_fn/param
733  * mapping
734  * @fn: filter function callback
735  * @mapcnt: number of slave device/channel in the map
736  * @map: array of channel to filter mapping data
737  */
738 struct dma_filter {
739         dma_filter_fn fn;
740         int mapcnt;
741         const struct dma_slave_map *map;
742 };
744 /**
745  * struct dma_device - info on the entity supplying DMA services
746  * @chancnt: how many DMA channels are supported
747  * @privatecnt: how many DMA channels are requested by dma_request_channel
748  * @channels: the list of struct dma_chan
749  * @global_node: list_head for global dma_device_list
750  * @filter: information for device/slave to filter function/param mapping
751  * @cap_mask: one or more dma_capability flags
752  * @desc_metadata_modes: supported metadata modes by the DMA device
753  * @max_xor: maximum number of xor sources, 0 if no capability
754  * @max_pq: maximum number of PQ sources and PQ-continue capability
755  * @copy_align: alignment shift for memcpy operations
756  * @xor_align: alignment shift for xor operations
757  * @pq_align: alignment shift for pq operations
758  * @fill_align: alignment shift for memset operations
759  * @dev_id: unique device ID
760  * @dev: struct device reference for dma mapping api
761  * @src_addr_widths: bit mask of src addr widths the device supports
762  *      Width is specified in bytes, e.g. for a device supporting
763  *      a width of 4 the mask should have BIT(4) set.
764  * @dst_addr_widths: bit mask of dst addr widths the device supports
765  * @directions: bit mask of slave directions the device supports.
766  *      Since the enum dma_transfer_direction is not defined as bit flag for
767  *      each type, the dma controller should set BIT(<TYPE>) and same
768  *      should be checked by controller as well
769  * @max_burst: max burst capability per-transfer
770  * @residue_granularity: granularity of the transfer residue reported
771  *      by tx_status
772  * @device_alloc_chan_resources: allocate resources and return the
773  *      number of allocated descriptors
774  * @device_free_chan_resources: release DMA channel's resources
775  * @device_prep_dma_memcpy: prepares a memcpy operation
776  * @device_prep_dma_xor: prepares a xor operation
777  * @device_prep_dma_xor_val: prepares a xor validation operation
778  * @device_prep_dma_pq: prepares a pq operation
779  * @device_prep_dma_pq_val: prepares a pqzero_sum operation
780  * @device_prep_dma_memset: prepares a memset operation
781  * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list
782  * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
783  * @device_prep_slave_sg: prepares a slave dma operation
784  * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
785  *      The function takes a buffer of size buf_len. The callback function will
786  *      be called after period_len bytes have been transferred.
787  * @device_prep_interleaved_dma: Transfer expression in a generic way.
788  * @device_prep_dma_imm_data: DMA's 8 byte immediate data to the dst address
789  * @device_config: Pushes a new configuration to a channel, return 0 or an error
790  *      code
791  * @device_pause: Pauses any transfer happening on a channel. Returns
792  *      0 or an error code
793  * @device_resume: Resumes any transfer on a channel previously
794  *      paused. Returns 0 or an error code
795  * @device_terminate_all: Aborts all transfers on a channel. Returns 0
796  *      or an error code
797  * @device_synchronize: Synchronizes the termination of a transfers to the
798  *  current context.
799  * @device_tx_status: poll for transaction completion, the optional
800  *      txstate parameter can be supplied with a pointer to get a
801  *      struct with auxiliary transfer status information, otherwise the call
802  *      will just return a simple status code
803  * @device_issue_pending: push pending transactions to hardware
804  * @descriptor_reuse: a submitted transfer can be resubmitted after completion
805  */
806 struct dma_device {
808         unsigned int chancnt;
809         unsigned int privatecnt;
810         struct list_head channels;
811         struct list_head global_node;
812         struct dma_filter filter;
813         dma_cap_mask_t  cap_mask;
814         enum dma_desc_metadata_mode desc_metadata_modes;
815         unsigned short max_xor;
816         unsigned short max_pq;
817         enum dmaengine_alignment copy_align;
818         enum dmaengine_alignment xor_align;
819         enum dmaengine_alignment pq_align;
820         enum dmaengine_alignment fill_align;
821         #define DMA_HAS_PQ_CONTINUE (1 << 15)
823         int dev_id;
824         struct device *dev;
826         u32 src_addr_widths;
827         u32 dst_addr_widths;
828         u32 directions;
829         u32 max_burst;
830         bool descriptor_reuse;
831         enum dma_residue_granularity residue_granularity;
833         int (*device_alloc_chan_resources)(struct dma_chan *chan);
834         void (*device_free_chan_resources)(struct dma_chan *chan);
836         struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
837                 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
838                 size_t len, unsigned long flags);
839         struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
840                 struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
841                 unsigned int src_cnt, size_t len, unsigned long flags);
842         struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
843                 struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
844                 size_t len, enum sum_check_flags *result, unsigned long flags);
845         struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
846                 struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
847                 unsigned int src_cnt, const unsigned char *scf,
848                 size_t len, unsigned long flags);
849         struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
850                 struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
851                 unsigned int src_cnt, const unsigned char *scf, size_t len,
852                 enum sum_check_flags *pqres, unsigned long flags);
853         struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
854                 struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
855                 unsigned long flags);
856         struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)(
857                 struct dma_chan *chan, struct scatterlist *sg,
858                 unsigned int nents, int value, unsigned long flags);
859         struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
860                 struct dma_chan *chan, unsigned long flags);
862         struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
863                 struct dma_chan *chan, struct scatterlist *sgl,
864                 unsigned int sg_len, enum dma_transfer_direction direction,
865                 unsigned long flags, void *context);
866         struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
867                 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
868                 size_t period_len, enum dma_transfer_direction direction,
869                 unsigned long flags);
870         struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
871                 struct dma_chan *chan, struct dma_interleaved_template *xt,
872                 unsigned long flags);
873         struct dma_async_tx_descriptor *(*device_prep_dma_imm_data)(
874                 struct dma_chan *chan, dma_addr_t dst, u64 data,
875                 unsigned long flags);
877         int (*device_config)(struct dma_chan *chan,
878                              struct dma_slave_config *config);
879         int (*device_pause)(struct dma_chan *chan);
880         int (*device_resume)(struct dma_chan *chan);
881         int (*device_terminate_all)(struct dma_chan *chan);
882         void (*device_synchronize)(struct dma_chan *chan);
884         enum dma_status (*device_tx_status)(struct dma_chan *chan,
885                                             dma_cookie_t cookie,
886                                             struct dma_tx_state *txstate);
887         void (*device_issue_pending)(struct dma_chan *chan);
888 };
890 static inline int dmaengine_slave_config(struct dma_chan *chan,
891                                           struct dma_slave_config *config)
893         if (chan->device->device_config)
894                 return chan->device->device_config(chan, config);
896         return -ENOSYS;
899 static inline bool is_slave_direction(enum dma_transfer_direction direction)
901         return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM);
904 static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
905         struct dma_chan *chan, dma_addr_t buf, size_t len,
906         enum dma_transfer_direction dir, unsigned long flags)
908         struct scatterlist sg;
909         sg_init_table(&sg, 1);
910         sg_dma_address(&sg) = buf;
911         sg_dma_len(&sg) = len;
913         if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
914                 return NULL;
916         return chan->device->device_prep_slave_sg(chan, &sg, 1,
917                                                   dir, flags, NULL);
920 static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
921         struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
922         enum dma_transfer_direction dir, unsigned long flags)
924         if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
925                 return NULL;
927         return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
928                                                   dir, flags, NULL);
931 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
932 struct rio_dma_ext;
933 static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
934         struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
935         enum dma_transfer_direction dir, unsigned long flags,
936         struct rio_dma_ext *rio_ext)
938         if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
939                 return NULL;
941         return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
942                                                   dir, flags, rio_ext);
944 #endif
946 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
947                 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
948                 size_t period_len, enum dma_transfer_direction dir,
949                 unsigned long flags)
951         if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic)
952                 return NULL;
954         return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
955                                                 period_len, dir, flags);
958 static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
959                 struct dma_chan *chan, struct dma_interleaved_template *xt,
960                 unsigned long flags)
962         if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma)
963                 return NULL;
965         return chan->device->device_prep_interleaved_dma(chan, xt, flags);
968 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
969                 struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
970                 unsigned long flags)
972         if (!chan || !chan->device || !chan->device->device_prep_dma_memset)
973                 return NULL;
975         return chan->device->device_prep_dma_memset(chan, dest, value,
976                                                     len, flags);
979 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
980                 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
981                 size_t len, unsigned long flags)
983         if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy)
984                 return NULL;
986         return chan->device->device_prep_dma_memcpy(chan, dest, src,
987                                                     len, flags);
990 static inline bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan,
991                 enum dma_desc_metadata_mode mode)
993         if (!chan)
994                 return false;
996         return !!(chan->device->desc_metadata_modes & mode);
999 #ifdef CONFIG_DMA_ENGINE
1000 int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
1001                                    void *data, size_t len);
1002 void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
1003                                       size_t *payload_len, size_t *max_len);
1004 int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
1005                                     size_t payload_len);
1006 #else /* CONFIG_DMA_ENGINE */
1007 static inline int dmaengine_desc_attach_metadata(
1008                 struct dma_async_tx_descriptor *desc, void *data, size_t len)
1010         return -EINVAL;
1012 static inline void *dmaengine_desc_get_metadata_ptr(
1013                 struct dma_async_tx_descriptor *desc, size_t *payload_len,
1014                 size_t *max_len)
1016         return NULL;
1018 static inline int dmaengine_desc_set_metadata_len(
1019                 struct dma_async_tx_descriptor *desc, size_t payload_len)
1021         return -EINVAL;
1023 #endif /* CONFIG_DMA_ENGINE */
1025 /**
1026  * dmaengine_terminate_all() - Terminate all active DMA transfers
1027  * @chan: The channel for which to terminate the transfers
1028  *
1029  * This function is DEPRECATED use either dmaengine_terminate_sync() or
1030  * dmaengine_terminate_async() instead.
1031  */
1032 static inline int dmaengine_terminate_all(struct dma_chan *chan)
1034         if (chan->device->device_terminate_all)
1035                 return chan->device->device_terminate_all(chan);
1037         return -ENOSYS;
1040 /**
1041  * dmaengine_terminate_async() - Terminate all active DMA transfers
1042  * @chan: The channel for which to terminate the transfers
1043  *
1044  * Calling this function will terminate all active and pending descriptors
1045  * that have previously been submitted to the channel. It is not guaranteed
1046  * though that the transfer for the active descriptor has stopped when the
1047  * function returns. Furthermore it is possible the complete callback of a
1048  * submitted transfer is still running when this function returns.
1049  *
1050  * dmaengine_synchronize() needs to be called before it is safe to free
1051  * any memory that is accessed by previously submitted descriptors or before
1052  * freeing any resources accessed from within the completion callback of any
1053  * perviously submitted descriptors.
1054  *
1055  * This function can be called from atomic context as well as from within a
1056  * complete callback of a descriptor submitted on the same channel.
1057  *
1058  * If none of the two conditions above apply consider using
1059  * dmaengine_terminate_sync() instead.
1060  */
1061 static inline int dmaengine_terminate_async(struct dma_chan *chan)
1063         if (chan->device->device_terminate_all)
1064                 return chan->device->device_terminate_all(chan);
1066         return -EINVAL;
1069 /**
1070  * dmaengine_synchronize() - Synchronize DMA channel termination
1071  * @chan: The channel to synchronize
1072  *
1073  * Synchronizes to the DMA channel termination to the current context. When this
1074  * function returns it is guaranteed that all transfers for previously issued
1075  * descriptors have stopped and and it is safe to free the memory assoicated
1076  * with them. Furthermore it is guaranteed that all complete callback functions
1077  * for a previously submitted descriptor have finished running and it is safe to
1078  * free resources accessed from within the complete callbacks.
1079  *
1080  * The behavior of this function is undefined if dma_async_issue_pending() has
1081  * been called between dmaengine_terminate_async() and this function.
1082  *
1083  * This function must only be called from non-atomic context and must not be
1084  * called from within a complete callback of a descriptor submitted on the same
1085  * channel.
1086  */
1087 static inline void dmaengine_synchronize(struct dma_chan *chan)
1089         might_sleep();
1091         if (chan->device->device_synchronize)
1092                 chan->device->device_synchronize(chan);
1095 /**
1096  * dmaengine_terminate_sync() - Terminate all active DMA transfers
1097  * @chan: The channel for which to terminate the transfers
1098  *
1099  * Calling this function will terminate all active and pending transfers
1100  * that have previously been submitted to the channel. It is similar to
1101  * dmaengine_terminate_async() but guarantees that the DMA transfer has actually
1102  * stopped and that all complete callbacks have finished running when the
1103  * function returns.
1104  *
1105  * This function must only be called from non-atomic context and must not be
1106  * called from within a complete callback of a descriptor submitted on the same
1107  * channel.
1108  */
1109 static inline int dmaengine_terminate_sync(struct dma_chan *chan)
1111         int ret;
1113         ret = dmaengine_terminate_async(chan);
1114         if (ret)
1115                 return ret;
1117         dmaengine_synchronize(chan);
1119         return 0;
1122 static inline int dmaengine_pause(struct dma_chan *chan)
1124         if (chan->device->device_pause)
1125                 return chan->device->device_pause(chan);
1127         return -ENOSYS;
1130 static inline int dmaengine_resume(struct dma_chan *chan)
1132         if (chan->device->device_resume)
1133                 return chan->device->device_resume(chan);
1135         return -ENOSYS;
1138 static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
1139         dma_cookie_t cookie, struct dma_tx_state *state)
1141         return chan->device->device_tx_status(chan, cookie, state);
1144 static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
1146         return desc->tx_submit(desc);
1149 static inline bool dmaengine_check_align(enum dmaengine_alignment align,
1150                                          size_t off1, size_t off2, size_t len)
1152         size_t mask;
1154         if (!align)
1155                 return true;
1156         mask = (1 << align) - 1;
1157         if (mask & (off1 | off2 | len))
1158                 return false;
1159         return true;
1162 static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
1163                                        size_t off2, size_t len)
1165         return dmaengine_check_align(dev->copy_align, off1, off2, len);
1168 static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
1169                                       size_t off2, size_t len)
1171         return dmaengine_check_align(dev->xor_align, off1, off2, len);
1174 static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
1175                                      size_t off2, size_t len)
1177         return dmaengine_check_align(dev->pq_align, off1, off2, len);
1180 static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
1181                                        size_t off2, size_t len)
1183         return dmaengine_check_align(dev->fill_align, off1, off2, len);
1186 static inline void
1187 dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
1189         dma->max_pq = maxpq;
1190         if (has_pq_continue)
1191                 dma->max_pq |= DMA_HAS_PQ_CONTINUE;
1194 static inline bool dmaf_continue(enum dma_ctrl_flags flags)
1196         return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
1199 static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
1201         enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
1203         return (flags & mask) == mask;
1206 static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
1208         return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
1211 static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
1213         return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
1216 /* dma_maxpq - reduce maxpq in the face of continued operations
1217  * @dma - dma device with PQ capability
1218  * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
1219  *
1220  * When an engine does not support native continuation we need 3 extra
1221  * source slots to reuse P and Q with the following coefficients:
1222  * 1/ {00} * P : remove P from Q', but use it as a source for P'
1223  * 2/ {01} * Q : use Q to continue Q' calculation
1224  * 3/ {00} * Q : subtract Q from P' to cancel (2)
1225  *
1226  * In the case where P is disabled we only need 1 extra source:
1227  * 1/ {01} * Q : use Q to continue Q' calculation
1228  */
1229 static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
1231         if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
1232                 return dma_dev_to_maxpq(dma);
1233         else if (dmaf_p_disabled_continue(flags))
1234                 return dma_dev_to_maxpq(dma) - 1;
1235         else if (dmaf_continue(flags))
1236                 return dma_dev_to_maxpq(dma) - 3;
1237         BUG();
1240 static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
1241                                       size_t dir_icg)
1243         if (inc) {
1244                 if (dir_icg)
1245                         return dir_icg;
1246                 else if (sgl)
1247                         return icg;
1248         }
1250         return 0;
1253 static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt,
1254                                            struct data_chunk *chunk)
1256         return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl,
1257                                  chunk->icg, chunk->dst_icg);
1260 static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt,
1261                                            struct data_chunk *chunk)
1263         return dmaengine_get_icg(xt->src_inc, xt->src_sgl,
1264                                  chunk->icg, chunk->src_icg);
1267 /* --- public DMA engine API --- */
1269 #ifdef CONFIG_DMA_ENGINE
1270 void dmaengine_get(void);
1271 void dmaengine_put(void);
1272 #else
1273 static inline void dmaengine_get(void)
1276 static inline void dmaengine_put(void)
1279 #endif
1281 #ifdef CONFIG_ASYNC_TX_DMA
1282 #define async_dmaengine_get()   dmaengine_get()
1283 #define async_dmaengine_put()   dmaengine_put()
1284 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
1285 #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
1286 #else
1287 #define async_dma_find_channel(type) dma_find_channel(type)
1288 #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
1289 #else
1290 static inline void async_dmaengine_get(void)
1293 static inline void async_dmaengine_put(void)
1296 static inline struct dma_chan *
1297 async_dma_find_channel(enum dma_transaction_type type)
1299         return NULL;
1301 #endif /* CONFIG_ASYNC_TX_DMA */
1302 void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
1303                                   struct dma_chan *chan);
1305 static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
1307         tx->flags |= DMA_CTRL_ACK;
1310 static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
1312         tx->flags &= ~DMA_CTRL_ACK;
1315 static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
1317         return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
1320 #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
1321 static inline void
1322 __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
1324         set_bit(tx_type, dstp->bits);
1327 #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
1328 static inline void
1329 __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
1331         clear_bit(tx_type, dstp->bits);
1334 #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
1335 static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
1337         bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
1340 #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
1341 static inline int
1342 __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
1344         return test_bit(tx_type, srcp->bits);
1347 #define for_each_dma_cap_mask(cap, mask) \
1348         for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
1350 /**
1351  * dma_async_issue_pending - flush pending transactions to HW
1352  * @chan: target DMA channel
1353  *
1354  * This allows drivers to push copies to HW in batches,
1355  * reducing MMIO writes where possible.
1356  */
1357 static inline void dma_async_issue_pending(struct dma_chan *chan)
1359         chan->device->device_issue_pending(chan);
1362 /**
1363  * dma_async_is_tx_complete - poll for transaction completion
1364  * @chan: DMA channel
1365  * @cookie: transaction identifier to check status of
1366  * @last: returns last completed cookie, can be NULL
1367  * @used: returns last issued cookie, can be NULL
1368  *
1369  * If @last and @used are passed in, upon return they reflect the driver
1370  * internal state and can be used with dma_async_is_complete() to check
1371  * the status of multiple cookies without re-checking hardware state.
1372  */
1373 static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
1374         dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
1376         struct dma_tx_state state;
1377         enum dma_status status;
1379         status = chan->device->device_tx_status(chan, cookie, &state);
1380         if (last)
1381                 *last = state.last;
1382         if (used)
1383                 *used = state.used;
1384         return status;
1387 /**
1388  * dma_async_is_complete - test a cookie against chan state
1389  * @cookie: transaction identifier to test status of
1390  * @last_complete: last know completed transaction
1391  * @last_used: last cookie value handed out
1392  *
1393  * dma_async_is_complete() is used in dma_async_is_tx_complete()
1394  * the test logic is separated for lightweight testing of multiple cookies
1395  */
1396 static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
1397                         dma_cookie_t last_complete, dma_cookie_t last_used)
1399         if (last_complete <= last_used) {
1400                 if ((cookie <= last_complete) || (cookie > last_used))
1401                         return DMA_COMPLETE;
1402         } else {
1403                 if ((cookie <= last_complete) && (cookie > last_used))
1404                         return DMA_COMPLETE;
1405         }
1406         return DMA_IN_PROGRESS;
1409 static inline void
1410 dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
1412         if (st) {
1413                 st->last = last;
1414                 st->used = used;
1415                 st->residue = residue;
1416         }
1419 #ifdef CONFIG_DMA_ENGINE
1420 struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
1421 enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
1422 enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
1423 void dma_issue_pending_all(void);
1424 struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1425                                         dma_filter_fn fn, void *fn_param);
1426 struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name);
1428 struct dma_chan *dma_request_chan(struct device *dev, const char *name);
1429 struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
1431 void dma_release_channel(struct dma_chan *chan);
1432 int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
1433 #else
1434 static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
1436         return NULL;
1438 static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
1440         return DMA_COMPLETE;
1442 static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
1444         return DMA_COMPLETE;
1446 static inline void dma_issue_pending_all(void)
1449 static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
1450                                               dma_filter_fn fn, void *fn_param)
1452         return NULL;
1454 static inline struct dma_chan *dma_request_slave_channel(struct device *dev,
1455                                                          const char *name)
1457         return NULL;
1459 static inline struct dma_chan *dma_request_chan(struct device *dev,
1460                                                 const char *name)
1462         return ERR_PTR(-ENODEV);
1464 static inline struct dma_chan *dma_request_chan_by_mask(
1465                                                 const dma_cap_mask_t *mask)
1467         return ERR_PTR(-ENODEV);
1469 static inline void dma_release_channel(struct dma_chan *chan)
1472 static inline int dma_get_slave_caps(struct dma_chan *chan,
1473                                      struct dma_slave_caps *caps)
1475         return -ENXIO;
1477 #endif
1479 #define dma_request_slave_channel_reason(dev, name) dma_request_chan(dev, name)
1481 static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
1483         struct dma_slave_caps caps;
1485         dma_get_slave_caps(tx->chan, &caps);
1487         if (caps.descriptor_reuse) {
1488                 tx->flags |= DMA_CTRL_REUSE;
1489                 return 0;
1490         } else {
1491                 return -EPERM;
1492         }
1495 static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
1497         tx->flags &= ~DMA_CTRL_REUSE;
1500 static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
1502         return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE;
1505 static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
1507         /* this is supported for reusable desc, so check that */
1508         if (dmaengine_desc_test_reuse(desc))
1509                 return desc->desc_free(desc);
1510         else
1511                 return -EPERM;
1514 /* --- DMA device --- */
1516 int dma_async_device_register(struct dma_device *device);
1517 int dmaenginem_async_device_register(struct dma_device *device);
1518 void dma_async_device_unregister(struct dma_device *device);
1519 void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
1520 struct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
1521 struct dma_chan *dma_get_any_slave_channel(struct dma_device *device);
1522 #define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
1523 #define dma_request_slave_channel_compat(mask, x, y, dev, name) \
1524         __dma_request_slave_channel_compat(&(mask), x, y, dev, name)
1526 static inline struct dma_chan
1527 *__dma_request_slave_channel_compat(const dma_cap_mask_t *mask,
1528                                   dma_filter_fn fn, void *fn_param,
1529                                   struct device *dev, const char *name)
1531         struct dma_chan *chan;
1533         chan = dma_request_slave_channel(dev, name);
1534         if (chan)
1535                 return chan;
1537         if (!fn || !fn_param)
1538                 return NULL;
1540         return __dma_request_channel(mask, fn, fn_param);
1542 #endif /* DMAENGINE_H */