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