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