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