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