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