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