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 * struct dma_vec - DMA vector 165 * @addr: Bus address of the start of the vector 166 * @len: Length in bytes of the DMA vector 167 */ 168 struct dma_vec { 169 dma_addr_t addr; 170 size_t len; 171 }; 172 173 /** 174 * enum dma_ctrl_flags - DMA flags to augment operation preparation, 175 * control completion, and communicate status. 176 * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of 177 * this transaction 178 * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client 179 * acknowledges receipt, i.e. has a chance to establish any dependency 180 * chains 181 * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q 182 * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P 183 * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as 184 * sources that were the result of a previous operation, in the case of a PQ 185 * operation it continues the calculation with new sources 186 * @DMA_PREP_FENCE - tell the driver that subsequent operations depend 187 * on the result of this operation 188 * @DMA_CTRL_REUSE: client can reuse the descriptor and submit again till 189 * cleared or freed 190 * @DMA_PREP_CMD: tell the driver that the data passed to DMA API is command 191 * data and the descriptor should be in different format from normal 192 * data descriptors. 193 * @DMA_PREP_REPEAT: tell the driver that the transaction shall be automatically 194 * repeated when it ends until a transaction is issued on the same channel 195 * with the DMA_PREP_LOAD_EOT flag set. This flag is only applicable to 196 * interleaved transactions and is ignored for all other transaction types. 197 * @DMA_PREP_LOAD_EOT: tell the driver that the transaction shall replace any 198 * active repeated (as indicated by DMA_PREP_REPEAT) transaction when the 199 * repeated transaction ends. Not setting this flag when the previously queued 200 * transaction is marked with DMA_PREP_REPEAT will cause the new transaction 201 * to never be processed and stay in the issued queue forever. The flag is 202 * ignored if the previous transaction is not a repeated transaction. 203 */ 204 enum dma_ctrl_flags { 205 DMA_PREP_INTERRUPT = (1 << 0), 206 DMA_CTRL_ACK = (1 << 1), 207 DMA_PREP_PQ_DISABLE_P = (1 << 2), 208 DMA_PREP_PQ_DISABLE_Q = (1 << 3), 209 DMA_PREP_CONTINUE = (1 << 4), 210 DMA_PREP_FENCE = (1 << 5), 211 DMA_CTRL_REUSE = (1 << 6), 212 DMA_PREP_CMD = (1 << 7), 213 DMA_PREP_REPEAT = (1 << 8), 214 DMA_PREP_LOAD_EOT = (1 << 9), 215 }; 216 217 /** 218 * enum sum_check_bits - bit position of pq_check_flags 219 */ 220 enum sum_check_bits { 221 SUM_CHECK_P = 0, 222 SUM_CHECK_Q = 1, 223 }; 224 225 /** 226 * enum sum_check_flags - result of async_{xor,pq}_zero_sum operations 227 * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise 228 * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise 229 */ 230 enum sum_check_flags { 231 SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P), 232 SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q), 233 }; 234 235 236 /** 237 * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t. 238 * See linux/cpumask.h 239 */ 240 typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t; 241 242 /** 243 * enum dma_desc_metadata_mode - per descriptor metadata mode types supported 244 * @DESC_METADATA_CLIENT - the metadata buffer is allocated/provided by the 245 * client driver and it is attached (via the dmaengine_desc_attach_metadata() 246 * helper) to the descriptor. 247 * 248 * Client drivers interested to use this mode can follow: 249 * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM: 250 * 1. prepare the descriptor (dmaengine_prep_*) 251 * construct the metadata in the client's buffer 252 * 2. use dmaengine_desc_attach_metadata() to attach the buffer to the 253 * descriptor 254 * 3. submit the transfer 255 * - DMA_DEV_TO_MEM: 256 * 1. prepare the descriptor (dmaengine_prep_*) 257 * 2. use dmaengine_desc_attach_metadata() to attach the buffer to the 258 * descriptor 259 * 3. submit the transfer 260 * 4. when the transfer is completed, the metadata should be available in the 261 * attached buffer 262 * 263 * @DESC_METADATA_ENGINE - the metadata buffer is allocated/managed by the DMA 264 * driver. The client driver can ask for the pointer, maximum size and the 265 * currently used size of the metadata and can directly update or read it. 266 * dmaengine_desc_get_metadata_ptr() and dmaengine_desc_set_metadata_len() is 267 * provided as helper functions. 268 * 269 * Note: the metadata area for the descriptor is no longer valid after the 270 * transfer has been completed (valid up to the point when the completion 271 * callback returns if used). 272 * 273 * Client drivers interested to use this mode can follow: 274 * - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM: 275 * 1. prepare the descriptor (dmaengine_prep_*) 276 * 2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the engine's 277 * metadata area 278 * 3. update the metadata at the pointer 279 * 4. use dmaengine_desc_set_metadata_len() to tell the DMA engine the amount 280 * of data the client has placed into the metadata buffer 281 * 5. submit the transfer 282 * - DMA_DEV_TO_MEM: 283 * 1. prepare the descriptor (dmaengine_prep_*) 284 * 2. submit the transfer 285 * 3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get the 286 * pointer to the engine's metadata area 287 * 4. Read out the metadata from the pointer 288 * 289 * Warning: the two modes are not compatible and clients must use one mode for a 290 * descriptor. 291 */ 292 enum dma_desc_metadata_mode { 293 DESC_METADATA_NONE = 0, 294 DESC_METADATA_CLIENT = BIT(0), 295 DESC_METADATA_ENGINE = BIT(1), 296 }; 297 298 /** 299 * struct dma_chan_percpu - the per-CPU part of struct dma_chan 300 * @memcpy_count: transaction counter 301 * @bytes_transferred: byte counter 302 */ 303 struct dma_chan_percpu { 304 /* stats */ 305 unsigned long memcpy_count; 306 unsigned long bytes_transferred; 307 }; 308 309 /** 310 * struct dma_router - DMA router structure 311 * @dev: pointer to the DMA router device 312 * @route_free: function to be called when the route can be disconnected 313 */ 314 struct dma_router { 315 struct device *dev; 316 void (*route_free)(struct device *dev, void *route_data); 317 }; 318 319 /** 320 * struct dma_chan - devices supply DMA channels, clients use them 321 * @device: ptr to the dma device who supplies this channel, always !%NULL 322 * @slave: ptr to the device using this channel 323 * @cookie: last cookie value returned to client 324 * @completed_cookie: last completed cookie for this channel 325 * @lock: protect between config and prepare transfer when driver have not 326 * implemented callback device_prep_config_sg(). 327 * @chan_id: channel ID for sysfs 328 * @chan_dev: class channel device for sysfs, some device use it for per-channel 329 * IOMMU mapping. 330 * @name: backlink name for sysfs 331 * @dbg_client_name: slave name for debugfs in format: 332 * dev_name(requester's dev):channel name, for example: "2b00000.mcasp:tx" 333 * @device_node: used to add this to the device chan list 334 * @local: per-cpu pointer to a struct dma_chan_percpu 335 * @client_count: how many clients are using this channel 336 * @table_count: number of appearances in the mem-to-mem allocation table 337 * @router: pointer to the DMA router structure 338 * @route_data: channel specific data for the router 339 * @private: private data for certain client-channel associations 340 */ 341 struct dma_chan { 342 struct dma_device *device; 343 struct device *slave; 344 dma_cookie_t cookie; 345 dma_cookie_t completed_cookie; 346 347 /* 348 * protect between config and prepare transfer because *_prep() may be 349 * called from complete callback, which is in GFP_NOSLEEP context. 350 */ 351 spinlock_t lock; 352 353 /* sysfs */ 354 int chan_id; 355 union { 356 struct dma_chan_dev *chan_dev; 357 /* 358 * Use chan_dev; dev will be removed once all users are 359 * converted. 360 */ 361 struct dma_chan_dev *dev; 362 }; 363 const char *name; 364 #ifdef CONFIG_DEBUG_FS 365 char *dbg_client_name; 366 #endif 367 368 struct list_head device_node; 369 struct dma_chan_percpu __percpu *local; 370 int client_count; 371 int table_count; 372 373 /* DMA router */ 374 struct dma_router *router; 375 void *route_data; 376 377 void *private; 378 }; 379 380 #define DMA_CHAN_ID_STATIC BIT(30) 381 382 /** 383 * dmaengine_set_static_chan_id - request an exact DMA engine channel ID 384 * @chan: DMA channel 385 * @id: channel ID, unique within the DMA device 386 * 387 * Drivers may call this after initializing @chan and before registering its 388 * DMA device. The dmaengine core reserves @id from the device IDA instead of 389 * assigning the next available ID. 390 */ 391 static inline void dmaengine_set_static_chan_id(struct dma_chan *chan, 392 unsigned int id) 393 { 394 if (WARN_ON_ONCE(id >= DMA_CHAN_ID_STATIC)) 395 return; 396 397 chan->chan_id = DMA_CHAN_ID_STATIC | id; 398 } 399 400 /** 401 * struct dma_chan_dev - relate sysfs device node to backing channel device 402 * @chan: driver channel device 403 * @device: sysfs device 404 * @dev_id: parent dma_device dev_id 405 * @chan_dma_dev: The channel is using custom/different dma-mapping 406 * compared to the parent dma_device 407 */ 408 struct dma_chan_dev { 409 struct dma_chan *chan; 410 struct device device; 411 int dev_id; 412 bool chan_dma_dev; 413 }; 414 415 /** 416 * enum dma_slave_buswidth - defines bus width of the DMA slave 417 * device, source or target buses 418 */ 419 enum dma_slave_buswidth { 420 DMA_SLAVE_BUSWIDTH_UNDEFINED = 0, 421 DMA_SLAVE_BUSWIDTH_1_BYTE = 1, 422 DMA_SLAVE_BUSWIDTH_2_BYTES = 2, 423 DMA_SLAVE_BUSWIDTH_3_BYTES = 3, 424 DMA_SLAVE_BUSWIDTH_4_BYTES = 4, 425 DMA_SLAVE_BUSWIDTH_8_BYTES = 8, 426 DMA_SLAVE_BUSWIDTH_16_BYTES = 16, 427 DMA_SLAVE_BUSWIDTH_32_BYTES = 32, 428 DMA_SLAVE_BUSWIDTH_64_BYTES = 64, 429 DMA_SLAVE_BUSWIDTH_128_BYTES = 128, 430 }; 431 432 /** 433 * struct dma_slave_config - dma slave channel runtime config 434 * @direction: whether the data shall go in or out on this slave 435 * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are 436 * legal values. DEPRECATED, drivers should use the direction argument 437 * to the device_prep_slave_sg and device_prep_dma_cyclic functions or 438 * the dir field in the dma_interleaved_template structure. 439 * @src_addr: this is the physical address where DMA slave data 440 * should be read (RX), if the source is memory this argument is 441 * ignored. 442 * @dst_addr: this is the physical address where DMA slave data 443 * should be written (TX), if the destination is memory this argument 444 * is ignored. 445 * @src_addr_width: this is the width in bytes of the source (RX) 446 * register where DMA data shall be read. If the source 447 * is memory this may be ignored depending on architecture. 448 * Legal values: 1, 2, 3, 4, 8, 16, 32, 64, 128. 449 * @dst_addr_width: same as src_addr_width but for destination 450 * target (TX) mutatis mutandis. 451 * @src_maxburst: the maximum number of words (note: words, as in 452 * units of the src_addr_width member, not bytes) that can be sent 453 * in one burst to the device. Typically something like half the 454 * FIFO depth on I/O peripherals so you don't overflow it. This 455 * may or may not be applicable on memory sources. 456 * @dst_maxburst: same as src_maxburst but for destination target 457 * mutatis mutandis. 458 * @src_port_window_size: The length of the register area in words the data need 459 * to be accessed on the device side. It is only used for devices which is using 460 * an area instead of a single register to receive the data. Typically the DMA 461 * loops in this area in order to transfer the data. 462 * @dst_port_window_size: same as src_port_window_size but for the destination 463 * port. 464 * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill 465 * with 'true' if peripheral should be flow controller. Direction will be 466 * selected at Runtime. 467 * @peripheral_config: peripheral configuration for programming peripheral 468 * for dmaengine transfer 469 * @peripheral_size: peripheral configuration buffer size 470 * 471 * This struct is passed in as configuration data to a DMA engine 472 * in order to set up a certain channel for DMA transport at runtime. 473 * The DMA device/engine has to provide support for an additional 474 * callback in the dma_device structure, device_config and this struct 475 * will then be passed in as an argument to the function. 476 * 477 * The rationale for adding configuration information to this struct is as 478 * follows: if it is likely that more than one DMA slave controllers in 479 * the world will support the configuration option, then make it generic. 480 * If not: if it is fixed so that it be sent in static from the platform 481 * data, then prefer to do that. 482 */ 483 struct dma_slave_config { 484 enum dma_transfer_direction direction; 485 phys_addr_t src_addr; 486 phys_addr_t dst_addr; 487 enum dma_slave_buswidth src_addr_width; 488 enum dma_slave_buswidth dst_addr_width; 489 u32 src_maxburst; 490 u32 dst_maxburst; 491 u32 src_port_window_size; 492 u32 dst_port_window_size; 493 bool device_fc; 494 void *peripheral_config; 495 size_t peripheral_size; 496 }; 497 498 /** 499 * enum dma_residue_granularity - Granularity of the reported transfer residue 500 * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The 501 * DMA channel is only able to tell whether a descriptor has been completed or 502 * not, which means residue reporting is not supported by this channel. The 503 * residue field of the dma_tx_state field will always be 0. 504 * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully 505 * completed segment of the transfer (For cyclic transfers this is after each 506 * period). This is typically implemented by having the hardware generate an 507 * interrupt after each transferred segment and then the drivers updates the 508 * outstanding residue by the size of the segment. Another possibility is if 509 * the hardware supports scatter-gather and the segment descriptor has a field 510 * which gets set after the segment has been completed. The driver then counts 511 * the number of segments without the flag set to compute the residue. 512 * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred 513 * burst. This is typically only supported if the hardware has a progress 514 * register of some sort (E.g. a register with the current read/write address 515 * or a register with the amount of bursts/beats/bytes that have been 516 * transferred or still need to be transferred). 517 */ 518 enum dma_residue_granularity { 519 DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0, 520 DMA_RESIDUE_GRANULARITY_SEGMENT = 1, 521 DMA_RESIDUE_GRANULARITY_BURST = 2, 522 }; 523 524 /** 525 * struct dma_slave_caps - expose capabilities of a slave channel only 526 * @src_addr_widths: bit mask of src addr widths the channel supports. 527 * Width is specified in bytes, e.g. for a channel supporting 528 * a width of 4 the mask should have BIT(4) set. 529 * @dst_addr_widths: bit mask of dst addr widths the channel supports 530 * @directions: bit mask of slave directions the channel supports. 531 * Since the enum dma_transfer_direction is not defined as bit flag for 532 * each type, the dma controller should set BIT(<TYPE>) and same 533 * should be checked by controller as well 534 * @min_burst: min burst capability per-transfer 535 * @max_burst: max burst capability per-transfer 536 * @max_sg_burst: max number of SG list entries executed in a single burst 537 * DMA tansaction with no software intervention for reinitialization. 538 * Zero value means unlimited number of entries. 539 * @cmd_pause: true, if pause is supported (i.e. for reading residue or 540 * for resume later) 541 * @cmd_resume: true, if resume is supported 542 * @cmd_terminate: true, if terminate cmd is supported 543 * @residue_granularity: granularity of the reported transfer residue 544 * @descriptor_reuse: if a descriptor can be reused by client and 545 * resubmitted multiple times 546 */ 547 struct dma_slave_caps { 548 u32 src_addr_widths; 549 u32 dst_addr_widths; 550 u32 directions; 551 u32 min_burst; 552 u32 max_burst; 553 u32 max_sg_burst; 554 bool cmd_pause; 555 bool cmd_resume; 556 bool cmd_terminate; 557 enum dma_residue_granularity residue_granularity; 558 bool descriptor_reuse; 559 }; 560 561 static inline const char *dma_chan_name(struct dma_chan *chan) 562 { 563 return dev_name(&chan->chan_dev->device); 564 } 565 566 /** 567 * typedef dma_filter_fn - callback filter for dma_request_channel 568 * @chan: channel to be reviewed 569 * @filter_param: opaque parameter passed through dma_request_channel 570 * 571 * When this optional parameter is specified in a call to dma_request_channel a 572 * suitable channel is passed to this routine for further dispositioning before 573 * being returned. Where 'suitable' indicates a non-busy channel that 574 * satisfies the given capability mask. It returns 'true' to indicate that the 575 * channel is suitable. 576 */ 577 typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param); 578 579 typedef void (*dma_async_tx_callback)(void *dma_async_param); 580 581 enum dmaengine_tx_result { 582 DMA_TRANS_NOERROR = 0, /* SUCCESS */ 583 DMA_TRANS_READ_FAILED, /* Source DMA read failed */ 584 DMA_TRANS_WRITE_FAILED, /* Destination DMA write failed */ 585 DMA_TRANS_ABORTED, /* Op never submitted / aborted */ 586 }; 587 588 struct dmaengine_result { 589 enum dmaengine_tx_result result; 590 u32 residue; 591 }; 592 593 typedef void (*dma_async_tx_callback_result)(void *dma_async_param, 594 const struct dmaengine_result *result); 595 596 struct dmaengine_unmap_data { 597 #if IS_ENABLED(CONFIG_DMA_ENGINE_RAID) 598 u16 map_cnt; 599 #else 600 u8 map_cnt; 601 #endif 602 u8 to_cnt; 603 u8 from_cnt; 604 u8 bidi_cnt; 605 struct device *dev; 606 struct kref kref; 607 size_t len; 608 dma_addr_t addr[]; 609 }; 610 611 struct dma_async_tx_descriptor; 612 613 struct dma_descriptor_metadata_ops { 614 int (*attach)(struct dma_async_tx_descriptor *desc, void *data, 615 size_t len); 616 617 void *(*get_ptr)(struct dma_async_tx_descriptor *desc, 618 size_t *payload_len, size_t *max_len); 619 int (*set_len)(struct dma_async_tx_descriptor *desc, 620 size_t payload_len); 621 }; 622 623 /** 624 * struct dma_async_tx_descriptor - async transaction descriptor 625 * ---dma generic offload fields--- 626 * @cookie: tracking cookie for this transaction, set to -EBUSY if 627 * this tx is sitting on a dependency list 628 * @flags: flags to augment operation preparation, control completion, and 629 * communicate status 630 * @phys: physical address of the descriptor 631 * @chan: target channel for this operation 632 * @tx_submit: accept the descriptor, assign ordered cookie and mark the 633 * descriptor pending. To be pushed on .issue_pending() call 634 * @desc_free: driver's callback function to free a resusable descriptor 635 * after completion 636 * @callback: routine to call after this operation is complete 637 * @callback_result: error result from a DMA transaction 638 * @callback_param: general parameter to pass to the callback routine 639 * @unmap: hook for generic DMA unmap data 640 * @desc_metadata_mode: core managed metadata mode to protect mixed use of 641 * DESC_METADATA_CLIENT or DESC_METADATA_ENGINE. Otherwise 642 * DESC_METADATA_NONE 643 * @metadata_ops: DMA driver provided metadata mode ops, need to be set by the 644 * DMA driver if metadata mode is supported with the descriptor 645 * ---async_tx api specific fields--- 646 * @next: at completion submit this descriptor 647 * @parent: pointer to the next level up in the dependency chain 648 * @lock: protect the parent and next pointers 649 */ 650 struct dma_async_tx_descriptor { 651 dma_cookie_t cookie; 652 enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */ 653 dma_addr_t phys; 654 struct dma_chan *chan; 655 dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx); 656 int (*desc_free)(struct dma_async_tx_descriptor *tx); 657 dma_async_tx_callback callback; 658 dma_async_tx_callback_result callback_result; 659 void *callback_param; 660 struct dmaengine_unmap_data *unmap; 661 enum dma_desc_metadata_mode desc_metadata_mode; 662 const struct dma_descriptor_metadata_ops *metadata_ops; 663 #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH 664 struct dma_async_tx_descriptor *next; 665 struct dma_async_tx_descriptor *parent; 666 spinlock_t lock; 667 #endif 668 }; 669 670 #ifdef CONFIG_DMA_ENGINE 671 static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx, 672 struct dmaengine_unmap_data *unmap) 673 { 674 kref_get(&unmap->kref); 675 tx->unmap = unmap; 676 } 677 678 struct dmaengine_unmap_data * 679 dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags); 680 void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap); 681 #else 682 static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx, 683 struct dmaengine_unmap_data *unmap) 684 { 685 } 686 static inline struct dmaengine_unmap_data * 687 dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags) 688 { 689 return NULL; 690 } 691 static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap) 692 { 693 } 694 #endif 695 696 static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx) 697 { 698 if (!tx->unmap) 699 return; 700 701 dmaengine_unmap_put(tx->unmap); 702 tx->unmap = NULL; 703 } 704 705 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH 706 static inline void txd_lock(struct dma_async_tx_descriptor *txd) 707 { 708 } 709 static inline void txd_unlock(struct dma_async_tx_descriptor *txd) 710 { 711 } 712 static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next) 713 { 714 BUG(); 715 } 716 static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd) 717 { 718 } 719 static inline void txd_clear_next(struct dma_async_tx_descriptor *txd) 720 { 721 } 722 static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd) 723 { 724 return NULL; 725 } 726 static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd) 727 { 728 return NULL; 729 } 730 731 #else 732 static inline void txd_lock(struct dma_async_tx_descriptor *txd) 733 { 734 spin_lock_bh(&txd->lock); 735 } 736 static inline void txd_unlock(struct dma_async_tx_descriptor *txd) 737 { 738 spin_unlock_bh(&txd->lock); 739 } 740 static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next) 741 { 742 txd->next = next; 743 next->parent = txd; 744 } 745 static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd) 746 { 747 txd->parent = NULL; 748 } 749 static inline void txd_clear_next(struct dma_async_tx_descriptor *txd) 750 { 751 txd->next = NULL; 752 } 753 static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd) 754 { 755 return txd->parent; 756 } 757 static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd) 758 { 759 return txd->next; 760 } 761 #endif 762 763 /** 764 * struct dma_tx_state - filled in to report the status of 765 * a transfer. 766 * @last: last completed DMA cookie 767 * @used: last issued DMA cookie (i.e. the one in progress) 768 * @residue: the remaining number of bytes left to transmit 769 * on the selected transfer for states DMA_IN_PROGRESS and 770 * DMA_PAUSED if this is implemented in the driver, else 0 771 * @in_flight_bytes: amount of data in bytes cached by the DMA. 772 */ 773 struct dma_tx_state { 774 dma_cookie_t last; 775 dma_cookie_t used; 776 u32 residue; 777 u32 in_flight_bytes; 778 }; 779 780 /** 781 * enum dmaengine_alignment - defines alignment of the DMA async tx 782 * buffers 783 */ 784 enum dmaengine_alignment { 785 DMAENGINE_ALIGN_1_BYTE = 0, 786 DMAENGINE_ALIGN_2_BYTES = 1, 787 DMAENGINE_ALIGN_4_BYTES = 2, 788 DMAENGINE_ALIGN_8_BYTES = 3, 789 DMAENGINE_ALIGN_16_BYTES = 4, 790 DMAENGINE_ALIGN_32_BYTES = 5, 791 DMAENGINE_ALIGN_64_BYTES = 6, 792 DMAENGINE_ALIGN_128_BYTES = 7, 793 DMAENGINE_ALIGN_256_BYTES = 8, 794 }; 795 796 /** 797 * struct dma_slave_map - associates slave device and it's slave channel with 798 * parameter to be used by a filter function 799 * @devname: name of the device 800 * @slave: slave channel name 801 * @param: opaque parameter to pass to struct dma_filter.fn 802 */ 803 struct dma_slave_map { 804 const char *devname; 805 const char *slave; 806 void *param; 807 }; 808 809 /** 810 * struct dma_filter - information for slave device/channel to filter_fn/param 811 * mapping 812 * @fn: filter function callback 813 * @mapcnt: number of slave device/channel in the map 814 * @map: array of channel to filter mapping data 815 */ 816 struct dma_filter { 817 dma_filter_fn fn; 818 int mapcnt; 819 const struct dma_slave_map *map; 820 }; 821 822 /** 823 * struct dma_device - info on the entity supplying DMA services 824 * @ref: reference is taken and put every time a channel is allocated or freed 825 * @chancnt: how many DMA channels are supported 826 * @privatecnt: how many DMA channels are requested by dma_request_channel 827 * @channels: the list of struct dma_chan 828 * @global_node: list_head for global dma_device_list 829 * @filter: information for device/slave to filter function/param mapping 830 * @cap_mask: one or more dma_capability flags 831 * @desc_metadata_modes: supported metadata modes by the DMA device 832 * @max_xor: maximum number of xor sources, 0 if no capability 833 * @max_pq: maximum number of PQ sources and PQ-continue capability 834 * @copy_align: alignment shift for memcpy operations 835 * @xor_align: alignment shift for xor operations 836 * @pq_align: alignment shift for pq operations 837 * @fill_align: alignment shift for memset operations 838 * @dev_id: unique device ID 839 * @dev: struct device reference for dma mapping api 840 * @owner: owner module (automatically set based on the provided dev) 841 * @chan_ida: unique channel ID 842 * @src_addr_widths: bit mask of src addr widths the device supports 843 * Width is specified in bytes, e.g. for a device supporting 844 * a width of 4 the mask should have BIT(4) set. 845 * @dst_addr_widths: bit mask of dst addr widths the device supports 846 * @directions: bit mask of slave directions the device supports. 847 * Since the enum dma_transfer_direction is not defined as bit flag for 848 * each type, the dma controller should set BIT(<TYPE>) and same 849 * should be checked by controller as well 850 * @min_burst: min burst capability per-transfer 851 * @max_burst: max burst capability per-transfer 852 * @max_sg_burst: max number of SG list entries executed in a single burst 853 * DMA tansaction with no software intervention for reinitialization. 854 * Zero value means unlimited number of entries. 855 * @descriptor_reuse: a submitted transfer can be resubmitted after completion 856 * @residue_granularity: granularity of the transfer residue reported 857 * by tx_status 858 * @device_alloc_chan_resources: allocate resources and return the 859 * number of allocated descriptors 860 * @device_router_config: optional callback for DMA router configuration 861 * @device_free_chan_resources: release DMA channel's resources 862 * @device_prep_dma_memcpy: prepares a memcpy operation 863 * @device_prep_dma_xor: prepares a xor operation 864 * @device_prep_dma_xor_val: prepares a xor validation operation 865 * @device_prep_dma_pq: prepares a pq operation 866 * @device_prep_dma_pq_val: prepares a pqzero_sum operation 867 * @device_prep_dma_memset: prepares a memset operation 868 * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list 869 * @device_prep_dma_interrupt: prepares an end of chain interrupt operation 870 * @device_prep_peripheral_dma_vec: prepares a scatter-gather DMA transfer, 871 * where the address and size of each segment is located in one entry of 872 * the dma_vec array. 873 * @device_prep_slave_sg: prepares a slave dma operation 874 * @device_prep_config_sg: prepares a slave DMA operation with dma_slave_config 875 * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio. 876 * The function takes a buffer of size buf_len. The callback function will 877 * be called after period_len bytes have been transferred. 878 * @device_prep_interleaved_dma: Transfer expression in a generic way. 879 * @device_caps: May be used to override the generic DMA slave capabilities 880 * with per-channel specific ones 881 * @device_config: Pushes a new configuration to a channel, return 0 or an error 882 * code 883 * @device_pause: Pauses any transfer happening on a channel. Returns 884 * 0 or an error code 885 * @device_resume: Resumes any transfer on a channel previously 886 * paused. Returns 0 or an error code 887 * @device_terminate_all: Aborts all transfers on a channel. Returns 0 888 * or an error code 889 * @device_synchronize: Synchronizes the termination of a transfers to the 890 * current context. 891 * @device_tx_status: poll for transaction completion, the optional 892 * txstate parameter can be supplied with a pointer to get a 893 * struct with auxiliary transfer status information, otherwise the call 894 * will just return a simple status code 895 * @device_issue_pending: push pending transactions to hardware 896 * @device_release: called sometime atfer dma_async_device_unregister() is 897 * called and there are no further references to this structure. This 898 * must be implemented to free resources however many existing drivers 899 * do not and are therefore not safe to unbind while in use. 900 * @dbg_summary_show: optional routine to show contents in debugfs; default code 901 * will be used when this is omitted, but custom code can show extra, 902 * controller specific information. 903 * @dbg_dev_root: the root folder in debugfs for this device 904 */ 905 struct dma_device { 906 struct kref ref; 907 unsigned int chancnt; 908 unsigned int privatecnt; 909 struct list_head channels; 910 struct list_head global_node; 911 struct dma_filter filter; 912 dma_cap_mask_t cap_mask; 913 enum dma_desc_metadata_mode desc_metadata_modes; 914 unsigned short max_xor; 915 unsigned short max_pq; 916 enum dmaengine_alignment copy_align; 917 enum dmaengine_alignment xor_align; 918 enum dmaengine_alignment pq_align; 919 enum dmaengine_alignment fill_align; 920 #define DMA_HAS_PQ_CONTINUE (1 << 15) 921 922 int dev_id; 923 struct device *dev; 924 struct module *owner; 925 struct ida chan_ida; 926 927 u32 src_addr_widths; 928 u32 dst_addr_widths; 929 u32 directions; 930 u32 min_burst; 931 u32 max_burst; 932 u32 max_sg_burst; 933 bool descriptor_reuse; 934 enum dma_residue_granularity residue_granularity; 935 936 int (*device_alloc_chan_resources)(struct dma_chan *chan); 937 int (*device_router_config)(struct dma_chan *chan); 938 void (*device_free_chan_resources)(struct dma_chan *chan); 939 940 struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)( 941 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src, 942 size_t len, unsigned long flags); 943 struct dma_async_tx_descriptor *(*device_prep_dma_xor)( 944 struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src, 945 unsigned int src_cnt, size_t len, unsigned long flags); 946 struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)( 947 struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt, 948 size_t len, enum sum_check_flags *result, unsigned long flags); 949 struct dma_async_tx_descriptor *(*device_prep_dma_pq)( 950 struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src, 951 unsigned int src_cnt, const unsigned char *scf, 952 size_t len, unsigned long flags); 953 struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)( 954 struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src, 955 unsigned int src_cnt, const unsigned char *scf, size_t len, 956 enum sum_check_flags *pqres, unsigned long flags); 957 struct dma_async_tx_descriptor *(*device_prep_dma_memset)( 958 struct dma_chan *chan, dma_addr_t dest, int value, size_t len, 959 unsigned long flags); 960 struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)( 961 struct dma_chan *chan, struct scatterlist *sg, 962 unsigned int nents, int value, unsigned long flags); 963 struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)( 964 struct dma_chan *chan, unsigned long flags); 965 966 struct dma_async_tx_descriptor *(*device_prep_peripheral_dma_vec)( 967 struct dma_chan *chan, const struct dma_vec *vecs, 968 size_t nents, enum dma_transfer_direction direction, 969 unsigned long flags); 970 struct dma_async_tx_descriptor *(*device_prep_slave_sg)( 971 struct dma_chan *chan, struct scatterlist *sgl, 972 unsigned int sg_len, enum dma_transfer_direction direction, 973 unsigned long flags, void *context); 974 struct dma_async_tx_descriptor *(*device_prep_config_sg)( 975 struct dma_chan *chan, struct scatterlist *sgl, 976 unsigned int sg_len, enum dma_transfer_direction direction, 977 unsigned long flags, struct dma_slave_config *config); 978 struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)( 979 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, 980 size_t period_len, enum dma_transfer_direction direction, 981 unsigned long flags); 982 struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)( 983 struct dma_chan *chan, struct dma_interleaved_template *xt, 984 unsigned long flags); 985 986 void (*device_caps)(struct dma_chan *chan, struct dma_slave_caps *caps); 987 int (*device_config)(struct dma_chan *chan, struct dma_slave_config *config); 988 int (*device_pause)(struct dma_chan *chan); 989 int (*device_resume)(struct dma_chan *chan); 990 int (*device_terminate_all)(struct dma_chan *chan); 991 void (*device_synchronize)(struct dma_chan *chan); 992 993 enum dma_status (*device_tx_status)(struct dma_chan *chan, 994 dma_cookie_t cookie, 995 struct dma_tx_state *txstate); 996 void (*device_issue_pending)(struct dma_chan *chan); 997 void (*device_release)(struct dma_device *dev); 998 /* debugfs support */ 999 void (*dbg_summary_show)(struct seq_file *s, struct dma_device *dev); 1000 struct dentry *dbg_dev_root; 1001 }; 1002 1003 static inline int dmaengine_slave_config(struct dma_chan *chan, 1004 struct dma_slave_config *config) 1005 { 1006 if (chan->device->device_config) 1007 return chan->device->device_config(chan, config); 1008 1009 return -ENOSYS; 1010 } 1011 1012 static inline struct dma_async_tx_descriptor * 1013 dmaengine_prep_config_single(struct dma_chan *chan, dma_addr_t buf, size_t len, 1014 enum dma_transfer_direction dir, 1015 unsigned long flags, 1016 struct dma_slave_config *config) 1017 { 1018 struct scatterlist sg; 1019 1020 if (!chan || !chan->device) 1021 return NULL; 1022 1023 sg_init_table(&sg, 1); 1024 sg_dma_address(&sg) = buf; 1025 sg_dma_len(&sg) = len; 1026 1027 if (chan->device->device_prep_config_sg) 1028 return chan->device->device_prep_config_sg(chan, &sg, 1, dir, 1029 flags, config); 1030 1031 if (config) 1032 if (dmaengine_slave_config(chan, config)) 1033 return NULL; 1034 1035 if (!chan->device->device_prep_slave_sg) 1036 return NULL; 1037 1038 return chan->device->device_prep_slave_sg(chan, &sg, 1, 1039 dir, flags, NULL); 1040 } 1041 1042 static inline struct dma_async_tx_descriptor * 1043 dmaengine_prep_slave_single(struct dma_chan *chan, dma_addr_t buf, size_t len, 1044 enum dma_transfer_direction dir, 1045 unsigned long flags) 1046 { 1047 return dmaengine_prep_config_single(chan, buf, len, dir, flags, NULL); 1048 } 1049 1050 /** 1051 * dmaengine_prep_peripheral_dma_vec() - Prepare a DMA scatter-gather descriptor 1052 * @chan: The channel to be used for this descriptor 1053 * @vecs: The array of DMA vectors that should be transferred 1054 * @nents: The number of DMA vectors in the array 1055 * @dir: Specifies the direction of the data transfer 1056 * @flags: DMA engine flags - DMA_PREP_REPEAT can be used to mark a cyclic 1057 * DMA transfer 1058 */ 1059 static inline struct dma_async_tx_descriptor *dmaengine_prep_peripheral_dma_vec( 1060 struct dma_chan *chan, const struct dma_vec *vecs, size_t nents, 1061 enum dma_transfer_direction dir, unsigned long flags) 1062 { 1063 if (!chan || !chan->device || !chan->device->device_prep_peripheral_dma_vec) 1064 return NULL; 1065 1066 return chan->device->device_prep_peripheral_dma_vec(chan, vecs, nents, 1067 dir, flags); 1068 } 1069 1070 static inline struct dma_async_tx_descriptor * 1071 dmaengine_prep_config_sg(struct dma_chan *chan, struct scatterlist *sgl, 1072 unsigned int sg_len, enum dma_transfer_direction dir, 1073 unsigned long flags, struct dma_slave_config *config) 1074 { 1075 if (!chan || !chan->device) 1076 return NULL; 1077 1078 if (chan->device->device_prep_config_sg) 1079 return chan->device->device_prep_config_sg(chan, sgl, sg_len, 1080 dir, flags, config); 1081 1082 if (config) 1083 if (dmaengine_slave_config(chan, config)) 1084 return NULL; 1085 1086 if (!chan->device->device_prep_slave_sg) 1087 return NULL; 1088 1089 return chan->device->device_prep_slave_sg(chan, sgl, sg_len, 1090 dir, flags, NULL); 1091 } 1092 1093 static inline struct dma_async_tx_descriptor * 1094 dmaengine_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, 1095 unsigned int sg_len, enum dma_transfer_direction dir, 1096 unsigned long flags) 1097 { 1098 return dmaengine_prep_config_sg(chan, sgl, sg_len, dir, flags, NULL); 1099 } 1100 1101 /** 1102 * dmaengine_prep_config_sg_safe - prepare a scatter-gather DMA transfer 1103 * with atomic slave configuration update 1104 * @chan: DMA channel 1105 * @sgl: scatterlist for the transfer 1106 * @sg_len: number of entries in @sgl 1107 * @dir: DMA transfer direction 1108 * @flags: transfer preparation flags 1109 * @config: DMA slave configuration for this transfer 1110 * 1111 * Prepare a DMA scatter-gather transfer together with a corresponding slave 1112 * configuration update in a re-entrant and race-safe manner. 1113 * 1114 * DMA engine drivers may implement the optional 1115 * device_prep_config_sg() callback to perform both the slave configuration 1116 * and descriptor preparation atomically. In this case, the operation is 1117 * fully handled by the DMA engine driver. 1118 * 1119 * If the DMA engine driver does not implement device_prep_config_sg(), falls 1120 * back to calling dmaengine_slave_config() followed by dmaengine_prep_slave_sg(). 1121 * The fallback path is protected by a per-channel spinlock to ensure that 1122 * concurrent callers cannot interleave configuration and descriptor preparation 1123 * on the same DMA channel. 1124 * 1125 * Return: Pointer to a prepared DMA async transaction descriptor on success, 1126 * or %NULL if the transfer could not be prepared. 1127 */ 1128 static inline struct dma_async_tx_descriptor * 1129 dmaengine_prep_config_sg_safe(struct dma_chan *chan, struct scatterlist *sgl, 1130 unsigned int sg_len, 1131 enum dma_transfer_direction dir, 1132 unsigned long flags, 1133 struct dma_slave_config *config) 1134 { 1135 struct dma_async_tx_descriptor *tx; 1136 unsigned long spinlock_flags; 1137 1138 if (!chan || !chan->device) 1139 return NULL; 1140 1141 if (!chan->device->device_prep_config_sg) 1142 spin_lock_irqsave(&chan->lock, spinlock_flags); 1143 1144 tx = dmaengine_prep_config_sg(chan, sgl, sg_len, dir, flags, config); 1145 1146 if (!chan->device->device_prep_config_sg) 1147 spin_unlock_irqrestore(&chan->lock, spinlock_flags); 1148 1149 return tx; 1150 } 1151 1152 /** 1153 * dmaengine_prep_config_single_safe - prepare a single-buffer DMA transfer 1154 * with atomic slave configuration update 1155 * @chan: DMA channel 1156 * @buf: DMA buffer address 1157 * @len: length of the transfer in bytes 1158 * @dir: DMA transfer direction 1159 * @flags: transfer preparation flags 1160 * @config: DMA slave configuration for this transfer 1161 * 1162 * Detail see dmaengine_prep_config_sg_safe(). 1163 */ 1164 static inline struct dma_async_tx_descriptor * 1165 dmaengine_prep_config_single_safe(struct dma_chan *chan, dma_addr_t buf, 1166 size_t len, enum dma_transfer_direction dir, 1167 unsigned long flags, 1168 struct dma_slave_config *config) 1169 { 1170 struct scatterlist sg; 1171 1172 sg_init_table(&sg, 1); 1173 sg_dma_address(&sg) = buf; 1174 sg_dma_len(&sg) = len; 1175 1176 return dmaengine_prep_config_sg_safe(chan, &sg, 1, dir, flags, config); 1177 } 1178 1179 #ifdef CONFIG_RAPIDIO_DMA_ENGINE 1180 struct rio_dma_ext; 1181 static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg( 1182 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len, 1183 enum dma_transfer_direction dir, unsigned long flags, 1184 struct rio_dma_ext *rio_ext) 1185 { 1186 if (!chan || !chan->device || !chan->device->device_prep_slave_sg) 1187 return NULL; 1188 1189 return chan->device->device_prep_slave_sg(chan, sgl, sg_len, 1190 dir, flags, rio_ext); 1191 } 1192 #endif 1193 1194 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic( 1195 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, 1196 size_t period_len, enum dma_transfer_direction dir, 1197 unsigned long flags) 1198 { 1199 if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic) 1200 return NULL; 1201 1202 return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len, 1203 period_len, dir, flags); 1204 } 1205 1206 static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma( 1207 struct dma_chan *chan, struct dma_interleaved_template *xt, 1208 unsigned long flags) 1209 { 1210 if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma) 1211 return NULL; 1212 if (flags & DMA_PREP_REPEAT && 1213 !test_bit(DMA_REPEAT, chan->device->cap_mask.bits)) 1214 return NULL; 1215 1216 return chan->device->device_prep_interleaved_dma(chan, xt, flags); 1217 } 1218 1219 /** 1220 * dmaengine_prep_dma_memset() - Prepare a DMA memset descriptor. 1221 * @chan: The channel to be used for this descriptor 1222 * @dest: Address of buffer to be set 1223 * @value: Treated as a single byte value that fills the destination buffer 1224 * @len: The total size of dest 1225 * @flags: DMA engine flags 1226 */ 1227 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset( 1228 struct dma_chan *chan, dma_addr_t dest, int value, size_t len, 1229 unsigned long flags) 1230 { 1231 if (!chan || !chan->device || !chan->device->device_prep_dma_memset) 1232 return NULL; 1233 1234 return chan->device->device_prep_dma_memset(chan, dest, value, 1235 len, flags); 1236 } 1237 1238 static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy( 1239 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 1240 size_t len, unsigned long flags) 1241 { 1242 if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy) 1243 return NULL; 1244 1245 return chan->device->device_prep_dma_memcpy(chan, dest, src, 1246 len, flags); 1247 } 1248 1249 /** 1250 * dmaengine_get_max_xor - get the maximum number of XOR sources for a channel 1251 * @chan: DMA channel 1252 * 1253 * Returns the maximum number of source buffers supported by the XOR engine 1254 * behind @chan, or 0 if the channel does not support XOR operations. 1255 */ 1256 static inline unsigned int dmaengine_get_max_xor(struct dma_chan *chan) 1257 { 1258 if (!chan || !chan->device) 1259 return 0; 1260 1261 return chan->device->max_xor; 1262 } 1263 1264 /** 1265 * dmaengine_prep_dma_xor - prepare a DMA XOR operation 1266 * @chan: the channel to use for this operation 1267 * @dst: destination buffer address 1268 * @src: array of source buffer addresses 1269 * @src_cnt: number of source buffers 1270 * @len: length in bytes of each source and destination buffer 1271 * @flags: DMA engine flags (e.g. DMA_PREP_INTERRUPT) 1272 * 1273 * Prepare an XOR parity generation transaction. The engine computes: 1274 * dst = src[0] XOR src[1] XOR ... XOR src[src_cnt - 1] 1275 * 1276 * Returns a descriptor on success, or NULL if the channel does not support 1277 * this operation or the request could not be queued. 1278 */ 1279 static inline struct dma_async_tx_descriptor * 1280 dmaengine_prep_dma_xor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src, 1281 unsigned int src_cnt, size_t len, unsigned long flags) 1282 { 1283 if (!chan || !chan->device || !chan->device->device_prep_dma_xor) 1284 return NULL; 1285 1286 return chan->device->device_prep_dma_xor(chan, dst, src, 1287 src_cnt, len, flags); 1288 } 1289 1290 /** 1291 * dmaengine_prep_dma_xor_val - prepare a DMA XOR zero-sum validation operation 1292 * @chan: the channel to use for this operation 1293 * @src: array of source buffer addresses 1294 * @src_cnt: number of source buffers 1295 * @len: length in bytes of each source buffer 1296 * @result: output flag set to SUM_CHECK_P_RESULT if the XOR of all sources 1297 * is non-zero (i.e. parity error), cleared otherwise 1298 * @flags: DMA engine flags (e.g. DMA_PREP_INTERRUPT) 1299 * 1300 * Prepare an XOR zero-sum validation transaction. The engine XORs all 1301 * source buffers and checks whether the result is zero. The outcome is 1302 * written to @result on completion. 1303 * 1304 * Returns a descriptor on success, or NULL if the channel does not support 1305 * this operation or the request could not be queued. 1306 */ 1307 static inline struct dma_async_tx_descriptor * 1308 dmaengine_prep_dma_xor_val(struct dma_chan *chan, dma_addr_t *src, 1309 unsigned int src_cnt, size_t len, 1310 enum sum_check_flags *result, unsigned long flags) 1311 { 1312 if (!chan || !chan->device || !chan->device->device_prep_dma_xor_val) 1313 return NULL; 1314 1315 return chan->device->device_prep_dma_xor_val(chan, src, src_cnt, 1316 len, result, flags); 1317 } 1318 1319 /** 1320 * dmaengine_prep_dma_pq - prepare a DMA PQ (RAID-6 P+Q) operation 1321 * @chan: the channel to use for this operation 1322 * @dst: array of two destination addresses: dst[0] for P, dst[1] for Q 1323 * @src: array of source buffer addresses 1324 * @src_cnt: number of source buffers 1325 * @scf: array of scaling coefficients, one per source buffer 1326 * @len: length in bytes of each source and destination buffer 1327 * @flags: DMA engine flags (e.g. DMA_PREP_INTERRUPT) 1328 * 1329 * Prepare a P+Q parity generation transaction. The engine computes: 1330 * P = XOR of all source buffers 1331 * Q = Galois field sum of (scf[i] * src[i]) over all sources 1332 * 1333 * Returns a descriptor on success, or NULL if the channel does not support 1334 * this operation or the request could not be queued. 1335 */ 1336 static inline struct dma_async_tx_descriptor * 1337 dmaengine_prep_dma_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src, 1338 unsigned int src_cnt, const unsigned char *scf, 1339 size_t len, unsigned long flags) 1340 { 1341 if (!chan || !chan->device || !chan->device->device_prep_dma_pq) 1342 return NULL; 1343 1344 return chan->device->device_prep_dma_pq(chan, dst, src, 1345 src_cnt, scf, len, flags); 1346 } 1347 1348 /** 1349 * dmaengine_prep_dma_pq_val - prepare a DMA PQ validation operation 1350 * @chan: the channel to use for this operation 1351 * @pq: array of two addresses holding existing P and Q parity buffers 1352 * @src: array of source buffer addresses 1353 * @src_cnt: number of source buffers 1354 * @scf: array of scaling coefficients, one per source buffer 1355 * @len: length in bytes of each buffer 1356 * @pqres: output flags indicating P and/or Q check results (SUM_CHECK_P_VALID, 1357 * SUM_CHECK_Q_VALID) 1358 * @flags: DMA engine flags (e.g. DMA_PREP_INTERRUPT) 1359 * 1360 * Prepare a PQ validation transaction. The engine recomputes P and Q from the 1361 * source buffers and compares them against the existing parity stored at @pq. 1362 * The result of each comparison is reported through @pqres. 1363 * 1364 * Returns a descriptor on success, or NULL if the channel does not support 1365 * this operation or the request could not be queued. 1366 */ 1367 static inline struct dma_async_tx_descriptor * 1368 dmaengine_prep_dma_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src, 1369 unsigned int src_cnt, const unsigned char *scf, 1370 size_t len, enum sum_check_flags *pqres, unsigned long flags) 1371 { 1372 if (!chan || !chan->device || !chan->device->device_prep_dma_pq_val) 1373 return NULL; 1374 1375 return chan->device->device_prep_dma_pq_val(chan, pq, src, 1376 src_cnt, scf, len, 1377 pqres, flags); 1378 } 1379 1380 static inline bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan, 1381 enum dma_desc_metadata_mode mode) 1382 { 1383 if (!chan) 1384 return false; 1385 1386 return !!(chan->device->desc_metadata_modes & mode); 1387 } 1388 1389 #ifdef CONFIG_DMA_ENGINE 1390 int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc, 1391 void *data, size_t len); 1392 void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc, 1393 size_t *payload_len, size_t *max_len); 1394 int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc, 1395 size_t payload_len); 1396 #else /* CONFIG_DMA_ENGINE */ 1397 static inline int dmaengine_desc_attach_metadata( 1398 struct dma_async_tx_descriptor *desc, void *data, size_t len) 1399 { 1400 return -EINVAL; 1401 } 1402 static inline void *dmaengine_desc_get_metadata_ptr( 1403 struct dma_async_tx_descriptor *desc, size_t *payload_len, 1404 size_t *max_len) 1405 { 1406 return NULL; 1407 } 1408 static inline int dmaengine_desc_set_metadata_len( 1409 struct dma_async_tx_descriptor *desc, size_t payload_len) 1410 { 1411 return -EINVAL; 1412 } 1413 #endif /* CONFIG_DMA_ENGINE */ 1414 1415 /** 1416 * dmaengine_terminate_all() - Terminate all active DMA transfers 1417 * @chan: The channel for which to terminate the transfers 1418 * 1419 * This function is DEPRECATED use either dmaengine_terminate_sync() or 1420 * dmaengine_terminate_async() instead. 1421 */ 1422 static inline int dmaengine_terminate_all(struct dma_chan *chan) 1423 { 1424 if (chan->device->device_terminate_all) 1425 return chan->device->device_terminate_all(chan); 1426 1427 return -ENOSYS; 1428 } 1429 1430 /** 1431 * dmaengine_terminate_async() - Terminate all active DMA transfers 1432 * @chan: The channel for which to terminate the transfers 1433 * 1434 * Calling this function will terminate all active and pending descriptors 1435 * that have previously been submitted to the channel. It is not guaranteed 1436 * though that the transfer for the active descriptor has stopped when the 1437 * function returns. Furthermore it is possible the complete callback of a 1438 * submitted transfer is still running when this function returns. 1439 * 1440 * dmaengine_synchronize() needs to be called before it is safe to free 1441 * any memory that is accessed by previously submitted descriptors or before 1442 * freeing any resources accessed from within the completion callback of any 1443 * previously submitted descriptors. 1444 * 1445 * This function can be called from atomic context as well as from within a 1446 * complete callback of a descriptor submitted on the same channel. 1447 * 1448 * If none of the two conditions above apply consider using 1449 * dmaengine_terminate_sync() instead. 1450 */ 1451 static inline int dmaengine_terminate_async(struct dma_chan *chan) 1452 { 1453 if (chan->device->device_terminate_all) 1454 return chan->device->device_terminate_all(chan); 1455 1456 return -EINVAL; 1457 } 1458 1459 /** 1460 * dmaengine_synchronize() - Synchronize DMA channel termination 1461 * @chan: The channel to synchronize 1462 * 1463 * Synchronizes to the DMA channel termination to the current context. When this 1464 * function returns it is guaranteed that all transfers for previously issued 1465 * descriptors have stopped and it is safe to free the memory associated 1466 * with them. Furthermore it is guaranteed that all complete callback functions 1467 * for a previously submitted descriptor have finished running and it is safe to 1468 * free resources accessed from within the complete callbacks. 1469 * 1470 * The behavior of this function is undefined if dma_async_issue_pending() has 1471 * been called between dmaengine_terminate_async() and this function. 1472 * 1473 * This function must only be called from non-atomic context and must not be 1474 * called from within a complete callback of a descriptor submitted on the same 1475 * channel. 1476 */ 1477 static inline void dmaengine_synchronize(struct dma_chan *chan) 1478 { 1479 might_sleep(); 1480 1481 if (chan->device->device_synchronize) 1482 chan->device->device_synchronize(chan); 1483 } 1484 1485 /** 1486 * dmaengine_terminate_sync() - Terminate all active DMA transfers 1487 * @chan: The channel for which to terminate the transfers 1488 * 1489 * Calling this function will terminate all active and pending transfers 1490 * that have previously been submitted to the channel. It is similar to 1491 * dmaengine_terminate_async() but guarantees that the DMA transfer has actually 1492 * stopped and that all complete callbacks have finished running when the 1493 * function returns. 1494 * 1495 * This function must only be called from non-atomic context and must not be 1496 * called from within a complete callback of a descriptor submitted on the same 1497 * channel. 1498 */ 1499 static inline int dmaengine_terminate_sync(struct dma_chan *chan) 1500 { 1501 int ret; 1502 1503 ret = dmaengine_terminate_async(chan); 1504 if (ret) 1505 return ret; 1506 1507 dmaengine_synchronize(chan); 1508 1509 return 0; 1510 } 1511 1512 static inline int dmaengine_pause(struct dma_chan *chan) 1513 { 1514 if (chan->device->device_pause) 1515 return chan->device->device_pause(chan); 1516 1517 return -ENOSYS; 1518 } 1519 1520 static inline int dmaengine_resume(struct dma_chan *chan) 1521 { 1522 if (chan->device->device_resume) 1523 return chan->device->device_resume(chan); 1524 1525 return -ENOSYS; 1526 } 1527 1528 static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan, 1529 dma_cookie_t cookie, struct dma_tx_state *state) 1530 { 1531 return chan->device->device_tx_status(chan, cookie, state); 1532 } 1533 1534 static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc) 1535 { 1536 return desc->tx_submit(desc); 1537 } 1538 1539 static inline bool dmaengine_check_align(enum dmaengine_alignment align, 1540 size_t off1, size_t off2, size_t len) 1541 { 1542 return !(((1 << align) - 1) & (off1 | off2 | len)); 1543 } 1544 1545 static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1, 1546 size_t off2, size_t len) 1547 { 1548 return dmaengine_check_align(dev->copy_align, off1, off2, len); 1549 } 1550 1551 static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1, 1552 size_t off2, size_t len) 1553 { 1554 return dmaengine_check_align(dev->xor_align, off1, off2, len); 1555 } 1556 1557 static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1, 1558 size_t off2, size_t len) 1559 { 1560 return dmaengine_check_align(dev->pq_align, off1, off2, len); 1561 } 1562 1563 static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1, 1564 size_t off2, size_t len) 1565 { 1566 return dmaengine_check_align(dev->fill_align, off1, off2, len); 1567 } 1568 1569 /** 1570 * dmaengine_get_copy_align - get copy alignment requirement of a DMA channel 1571 * @chan: DMA channel 1572 * 1573 * Return the copy alignment requirement of @chan as a power-of-2 exponent. 1574 */ 1575 static inline enum dmaengine_alignment 1576 dmaengine_get_copy_align(struct dma_chan *chan) 1577 { 1578 return chan->device->copy_align; 1579 } 1580 1581 /** 1582 * dmaengine_get_xor_align - get xor alignment requirement of a DMA channel 1583 * @chan: DMA channel 1584 * 1585 * Return the xor alignment requirement of @chan as a power-of-2 exponent. 1586 */ 1587 static inline enum dmaengine_alignment 1588 dmaengine_get_xor_align(struct dma_chan *chan) 1589 { 1590 return chan->device->xor_align; 1591 } 1592 1593 /** 1594 * dmaengine_get_pq_align - get pq alignment requirement of a DMA channel 1595 * @chan: DMA channel 1596 * 1597 * Return the pq alignment requirement of @chan as a power-of-2 exponent. 1598 */ 1599 static inline enum dmaengine_alignment 1600 dmaengine_get_pq_align(struct dma_chan *chan) 1601 { 1602 return chan->device->pq_align; 1603 } 1604 1605 /** 1606 * dmaengine_get_fill_align - get fill alignment requirement of a DMA channel 1607 * @chan: DMA channel 1608 * 1609 * Return the fill alignment requirement of @chan as a power-of-2 exponent. 1610 */ 1611 static inline enum dmaengine_alignment 1612 dmaengine_get_fill_align(struct dma_chan *chan) 1613 { 1614 return chan->device->fill_align; 1615 } 1616 1617 /** 1618 * dmaengine_get_cap_mask - get capability mask of a DMA channel 1619 * @chan: DMA channel 1620 * 1621 * Return a pointer to the capability mask of the DMA device backing @chan. 1622 */ 1623 static inline const dma_cap_mask_t * 1624 dmaengine_get_cap_mask(struct dma_chan *chan) 1625 { 1626 return &chan->device->cap_mask; 1627 } 1628 1629 /** 1630 * dmaengine_is_copy_aligned - test copy alignment 1631 * @chan: DMA channel 1632 * @off1: first buffer offset 1633 * @off2: second buffer offset 1634 * @len: transfer length 1635 * 1636 * Return true if off1, off2 and len satisfy the copy alignment requirement of 1637 * the DMA channel, false otherwise. 1638 */ 1639 static inline bool dmaengine_is_copy_aligned(struct dma_chan *chan, size_t off1, 1640 size_t off2, size_t len) 1641 { 1642 return dmaengine_check_align(chan->device->copy_align, off1, off2, len); 1643 } 1644 1645 /** 1646 * dmaengine_is_xor_aligned - test xor alignment 1647 * @chan: DMA channel 1648 * @off1: first buffer offset 1649 * @off2: second buffer offset 1650 * @len: transfer length 1651 * 1652 * Return true if off1, off2 and len satisfy the xor alignment requirement of 1653 * the DMA channel, false otherwise. 1654 */ 1655 static inline bool dmaengine_is_xor_aligned(struct dma_chan *chan, size_t off1, 1656 size_t off2, size_t len) 1657 { 1658 return dmaengine_check_align(chan->device->xor_align, off1, off2, len); 1659 } 1660 1661 /** 1662 * dmaengine_is_pq_aligned - test pq alignment 1663 * @chan: DMA channel 1664 * @off1: first buffer offset 1665 * @off2: second buffer offset 1666 * @len: transfer length 1667 * 1668 * Return true if off1, off2 and len satisfy the pq alignment requirement of 1669 * the DMA channel, false otherwise. 1670 */ 1671 static inline bool dmaengine_is_pq_aligned(struct dma_chan *chan, size_t off1, 1672 size_t off2, size_t len) 1673 { 1674 return dmaengine_check_align(chan->device->pq_align, off1, off2, len); 1675 } 1676 1677 /** 1678 * dmaengine_is_fill_aligned - test fill alignment 1679 * @chan: DMA channel 1680 * @off1: first buffer offset 1681 * @off2: second buffer offset 1682 * @len: transfer length 1683 * 1684 * Return true if off1, off2 and len satisfy the fill alignment requirement of 1685 * the DMA channel, false otherwise. 1686 */ 1687 static inline bool dmaengine_is_fill_aligned(struct dma_chan *chan, size_t off1, 1688 size_t off2, size_t len) 1689 { 1690 return dmaengine_check_align(chan->device->fill_align, off1, off2, len); 1691 } 1692 1693 static inline void 1694 dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue) 1695 { 1696 dma->max_pq = maxpq; 1697 if (has_pq_continue) 1698 dma->max_pq |= DMA_HAS_PQ_CONTINUE; 1699 } 1700 1701 static inline bool dmaf_continue(enum dma_ctrl_flags flags) 1702 { 1703 return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE; 1704 } 1705 1706 static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags) 1707 { 1708 enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P; 1709 1710 return (flags & mask) == mask; 1711 } 1712 1713 static inline bool dma_dev_has_pq_continue(struct dma_device *dma) 1714 { 1715 return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE; 1716 } 1717 1718 static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma) 1719 { 1720 return dma->max_pq & ~DMA_HAS_PQ_CONTINUE; 1721 } 1722 1723 /* dma_maxpq - reduce maxpq in the face of continued operations 1724 * @dma - dma device with PQ capability 1725 * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set 1726 * 1727 * When an engine does not support native continuation we need 3 extra 1728 * source slots to reuse P and Q with the following coefficients: 1729 * 1/ {00} * P : remove P from Q', but use it as a source for P' 1730 * 2/ {01} * Q : use Q to continue Q' calculation 1731 * 3/ {00} * Q : subtract Q from P' to cancel (2) 1732 * 1733 * In the case where P is disabled we only need 1 extra source: 1734 * 1/ {01} * Q : use Q to continue Q' calculation 1735 */ 1736 static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags) 1737 { 1738 if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags)) 1739 return dma_dev_to_maxpq(dma); 1740 if (dmaf_p_disabled_continue(flags)) 1741 return dma_dev_to_maxpq(dma) - 1; 1742 if (dmaf_continue(flags)) 1743 return dma_dev_to_maxpq(dma) - 3; 1744 BUG(); 1745 } 1746 1747 static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg, 1748 size_t dir_icg) 1749 { 1750 if (inc) { 1751 if (dir_icg) 1752 return dir_icg; 1753 if (sgl) 1754 return icg; 1755 } 1756 1757 return 0; 1758 } 1759 1760 static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt, 1761 struct data_chunk *chunk) 1762 { 1763 return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl, 1764 chunk->icg, chunk->dst_icg); 1765 } 1766 1767 static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt, 1768 struct data_chunk *chunk) 1769 { 1770 return dmaengine_get_icg(xt->src_inc, xt->src_sgl, 1771 chunk->icg, chunk->src_icg); 1772 } 1773 1774 /* --- public DMA engine API --- */ 1775 1776 #ifdef CONFIG_DMA_ENGINE 1777 void dmaengine_get(void); 1778 void dmaengine_put(void); 1779 #else 1780 static inline void dmaengine_get(void) 1781 { 1782 } 1783 static inline void dmaengine_put(void) 1784 { 1785 } 1786 #endif 1787 1788 #ifdef CONFIG_ASYNC_TX_DMA 1789 #define async_dmaengine_get() dmaengine_get() 1790 #define async_dmaengine_put() dmaengine_put() 1791 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH 1792 #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX) 1793 #else 1794 #define async_dma_find_channel(type) dma_find_channel(type) 1795 #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */ 1796 #else 1797 static inline void async_dmaengine_get(void) 1798 { 1799 } 1800 static inline void async_dmaengine_put(void) 1801 { 1802 } 1803 static inline struct dma_chan * 1804 async_dma_find_channel(enum dma_transaction_type type) 1805 { 1806 return NULL; 1807 } 1808 #endif /* CONFIG_ASYNC_TX_DMA */ 1809 void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx, 1810 struct dma_chan *chan); 1811 1812 static inline void async_tx_ack(struct dma_async_tx_descriptor *tx) 1813 { 1814 tx->flags |= DMA_CTRL_ACK; 1815 } 1816 1817 static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx) 1818 { 1819 tx->flags &= ~DMA_CTRL_ACK; 1820 } 1821 1822 static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx) 1823 { 1824 return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK; 1825 } 1826 1827 #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask)) 1828 static inline void 1829 __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp) 1830 { 1831 set_bit(tx_type, dstp->bits); 1832 } 1833 1834 #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask)) 1835 static inline void 1836 __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp) 1837 { 1838 clear_bit(tx_type, dstp->bits); 1839 } 1840 1841 #define dma_cap_zero(mask) __dma_cap_zero(&(mask)) 1842 static inline void __dma_cap_zero(dma_cap_mask_t *dstp) 1843 { 1844 bitmap_zero(dstp->bits, DMA_TX_TYPE_END); 1845 } 1846 1847 #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask)) 1848 static inline int 1849 __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp) 1850 { 1851 return test_bit(tx_type, srcp->bits); 1852 } 1853 1854 /** 1855 * dmaengine_has_cap - test whether a DMA channel supports a transaction type 1856 * @chan: DMA channel 1857 * @tx_type: transaction type to test 1858 * 1859 * Return true if @chan supports @tx_type, false otherwise. 1860 */ 1861 static inline bool 1862 dmaengine_has_cap(struct dma_chan *chan, enum dma_transaction_type tx_type) 1863 { 1864 return dma_has_cap(tx_type, chan->device->cap_mask); 1865 } 1866 1867 #define for_each_dma_cap_mask(cap, mask) \ 1868 for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END) 1869 1870 /** 1871 * dmaengine_prep_dma_interrupt() - Prepare a DMA interrupt descriptor. 1872 * @chan: The channel to be used for this descriptor 1873 * @flags: DMA engine flags 1874 * 1875 * Returns a descriptor for an interrupt transaction, or NULL if the 1876 * channel does not support DMA_INTERRUPT. 1877 */ 1878 static inline struct dma_async_tx_descriptor * 1879 dmaengine_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags) 1880 { 1881 if (!chan || !chan->device || !chan->device->device_prep_dma_interrupt || 1882 !dma_has_cap(DMA_INTERRUPT, chan->device->cap_mask)) 1883 return NULL; 1884 1885 return chan->device->device_prep_dma_interrupt(chan, flags); 1886 } 1887 1888 /** 1889 * dma_async_issue_pending - flush pending transactions to HW 1890 * @chan: target DMA channel 1891 * 1892 * This allows drivers to push copies to HW in batches, 1893 * reducing MMIO writes where possible. 1894 */ 1895 static inline void dma_async_issue_pending(struct dma_chan *chan) 1896 { 1897 chan->device->device_issue_pending(chan); 1898 } 1899 1900 /** 1901 * dma_async_is_tx_complete - poll for transaction completion 1902 * @chan: DMA channel 1903 * @cookie: transaction identifier to check status of 1904 * @last: returns last completed cookie, can be NULL 1905 * @used: returns last issued cookie, can be NULL 1906 * 1907 * If @last and @used are passed in, upon return they reflect the driver 1908 * internal state and can be used with dma_async_is_complete() to check 1909 * the status of multiple cookies without re-checking hardware state. 1910 */ 1911 static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan, 1912 dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used) 1913 { 1914 struct dma_tx_state state; 1915 enum dma_status status; 1916 1917 status = chan->device->device_tx_status(chan, cookie, &state); 1918 if (last) 1919 *last = state.last; 1920 if (used) 1921 *used = state.used; 1922 return status; 1923 } 1924 1925 /** 1926 * dma_async_is_complete - test a cookie against chan state 1927 * @cookie: transaction identifier to test status of 1928 * @last_complete: last know completed transaction 1929 * @last_used: last cookie value handed out 1930 * 1931 * dma_async_is_complete() is used in dma_async_is_tx_complete() 1932 * the test logic is separated for lightweight testing of multiple cookies 1933 */ 1934 static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie, 1935 dma_cookie_t last_complete, dma_cookie_t last_used) 1936 { 1937 if (last_complete <= last_used) { 1938 if ((cookie <= last_complete) || (cookie > last_used)) 1939 return DMA_COMPLETE; 1940 } else { 1941 if ((cookie <= last_complete) && (cookie > last_used)) 1942 return DMA_COMPLETE; 1943 } 1944 return DMA_IN_PROGRESS; 1945 } 1946 1947 static inline void 1948 dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue) 1949 { 1950 if (!st) 1951 return; 1952 1953 st->last = last; 1954 st->used = used; 1955 st->residue = residue; 1956 } 1957 1958 #ifdef CONFIG_DMA_ENGINE 1959 struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type); 1960 enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie); 1961 enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx); 1962 void dma_issue_pending_all(void); 1963 struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask, 1964 dma_filter_fn fn, void *fn_param, 1965 struct device_node *np); 1966 1967 struct dma_chan *dma_request_chan(struct device *dev, const char *name); 1968 struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask); 1969 struct dma_chan *devm_dma_request_chan(struct device *dev, const char *name); 1970 1971 void dma_release_channel(struct dma_chan *chan); 1972 int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps); 1973 #else 1974 static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type) 1975 { 1976 return NULL; 1977 } 1978 static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie) 1979 { 1980 return DMA_COMPLETE; 1981 } 1982 static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx) 1983 { 1984 return DMA_COMPLETE; 1985 } 1986 static inline void dma_issue_pending_all(void) 1987 { 1988 } 1989 static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask, 1990 dma_filter_fn fn, 1991 void *fn_param, 1992 struct device_node *np) 1993 { 1994 return NULL; 1995 } 1996 static inline struct dma_chan *dma_request_chan(struct device *dev, 1997 const char *name) 1998 { 1999 return ERR_PTR(-ENODEV); 2000 } 2001 static inline struct dma_chan *dma_request_chan_by_mask( 2002 const dma_cap_mask_t *mask) 2003 { 2004 return ERR_PTR(-ENODEV); 2005 } 2006 2007 static inline struct dma_chan *devm_dma_request_chan(struct device *dev, const char *name) 2008 { 2009 return ERR_PTR(-ENODEV); 2010 } 2011 2012 static inline void dma_release_channel(struct dma_chan *chan) 2013 { 2014 } 2015 static inline int dma_get_slave_caps(struct dma_chan *chan, 2016 struct dma_slave_caps *caps) 2017 { 2018 return -ENXIO; 2019 } 2020 #endif 2021 2022 static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx) 2023 { 2024 struct dma_slave_caps caps; 2025 int ret; 2026 2027 ret = dma_get_slave_caps(tx->chan, &caps); 2028 if (ret) 2029 return ret; 2030 2031 if (!caps.descriptor_reuse) 2032 return -EPERM; 2033 2034 tx->flags |= DMA_CTRL_REUSE; 2035 return 0; 2036 } 2037 2038 static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx) 2039 { 2040 tx->flags &= ~DMA_CTRL_REUSE; 2041 } 2042 2043 static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx) 2044 { 2045 return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE; 2046 } 2047 2048 static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc) 2049 { 2050 /* this is supported for reusable desc, so check that */ 2051 if (!dmaengine_desc_test_reuse(desc)) 2052 return -EPERM; 2053 2054 return desc->desc_free(desc); 2055 } 2056 2057 /** 2058 * dmaengine_desc_set_callback - set the callback, callback_result and 2059 * parameter on a descriptor 2060 * @tx: the descriptor to set the callback on 2061 * @cb: the plain completion callback, or NULL 2062 * @cb_result: the result-bearing completion callback, or NULL 2063 * @cb_param: the parameter to pass to the callback 2064 * 2065 * Sets the completion callbacks and their shared parameter on a DMA 2066 * transaction descriptor. Use this helper instead of assigning the fields 2067 * directly to ensure all three related fields are updated consistently. 2068 * Only one of @cb and @cb_result should be non-NULL; if both are provided 2069 * @cb_result takes precedence in drivers that support it. 2070 */ 2071 static inline void 2072 dmaengine_desc_set_callback(struct dma_async_tx_descriptor *tx, 2073 dma_async_tx_callback cb, 2074 dma_async_tx_callback_result cb_result, 2075 void *cb_param) 2076 { 2077 tx->callback = cb; 2078 tx->callback_result = cb_result; 2079 tx->callback_param = cb_param; 2080 } 2081 2082 /* --- DMA device --- */ 2083 2084 void dma_run_dependencies(struct dma_async_tx_descriptor *tx); 2085 #define dma_request_channel(mask, x, y) \ 2086 __dma_request_channel(&(mask), x, y, NULL) 2087 2088 static inline struct dma_chan 2089 *dma_request_slave_channel_compat(const dma_cap_mask_t mask, 2090 dma_filter_fn fn, void *fn_param, 2091 struct device *dev, const char *name) 2092 { 2093 struct dma_chan *chan; 2094 2095 chan = dma_request_chan(dev, name); 2096 if (!IS_ERR(chan)) 2097 return chan; 2098 2099 if (!fn || !fn_param) 2100 return NULL; 2101 2102 return dma_request_channel(mask, fn, fn_param); 2103 } 2104 2105 static inline struct device *dmaengine_chan_dev(struct dma_chan *chan) 2106 { 2107 return &chan->chan_dev->device; 2108 } 2109 2110 /* 2111 * dmaengine_get_provider_device() - Get DMA engine provider device. Typical 2112 * use is to find a DMA channel by filter. 2113 * @chan: DMA channel 2114 * 2115 * Return: Pointer to the DMA engine provider device. 2116 */ 2117 static inline struct device *dmaengine_get_provider_device(struct dma_chan *chan) 2118 { 2119 return chan->device->dev; 2120 } 2121 2122 /* 2123 * dmaengine_get_dma_device() - Get DMA device used for DMA mapping. Some 2124 * DMA engines provide per-channel iommu mapping. 2125 * @chan: DMA channel 2126 * 2127 * Return: Pointer to the device used for DMA mapping. 2128 */ 2129 static inline struct device *dmaengine_get_dma_device(struct dma_chan *chan) 2130 { 2131 if (chan->chan_dev->chan_dma_dev) 2132 return dmaengine_chan_dev(chan); 2133 2134 return dmaengine_get_provider_device(chan); 2135 } 2136 2137 #endif /* DMAENGINE_H */ 2138