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