177fe6612SVinod Koul================================== 277fe6612SVinod KoulDMAengine controller documentation 377fe6612SVinod Koul================================== 477fe6612SVinod Koul 577fe6612SVinod KoulHardware Introduction 677fe6612SVinod Koul===================== 777fe6612SVinod Koul 877fe6612SVinod KoulMost of the Slave DMA controllers have the same general principles of 977fe6612SVinod Kouloperations. 1077fe6612SVinod Koul 1177fe6612SVinod KoulThey have a given number of channels to use for the DMA transfers, and 1277fe6612SVinod Koula given number of requests lines. 1377fe6612SVinod Koul 1477fe6612SVinod KoulRequests and channels are pretty much orthogonal. Channels can be used 1577fe6612SVinod Koulto serve several to any requests. To simplify, channels are the 1677fe6612SVinod Koulentities that will be doing the copy, and requests what endpoints are 1777fe6612SVinod Koulinvolved. 1877fe6612SVinod Koul 1977fe6612SVinod KoulThe request lines actually correspond to physical lines going from the 2077fe6612SVinod KoulDMA-eligible devices to the controller itself. Whenever the device 2177fe6612SVinod Koulwill want to start a transfer, it will assert a DMA request (DRQ) by 2277fe6612SVinod Koulasserting that request line. 2377fe6612SVinod Koul 2477fe6612SVinod KoulA very simple DMA controller would only take into account a single 2577fe6612SVinod Koulparameter: the transfer size. At each clock cycle, it would transfer a 2677fe6612SVinod Koulbyte of data from one buffer to another, until the transfer size has 2777fe6612SVinod Koulbeen reached. 2877fe6612SVinod Koul 2977fe6612SVinod KoulThat wouldn't work well in the real world, since slave devices might 3077fe6612SVinod Koulrequire a specific number of bits to be transferred in a single 3177fe6612SVinod Koulcycle. For example, we may want to transfer as much data as the 3277fe6612SVinod Koulphysical bus allows to maximize performances when doing a simple 3377fe6612SVinod Koulmemory copy operation, but our audio device could have a narrower FIFO 3477fe6612SVinod Koulthat requires data to be written exactly 16 or 24 bits at a time. This 3577fe6612SVinod Koulis why most if not all of the DMA controllers can adjust this, using a 3677fe6612SVinod Koulparameter called the transfer width. 3777fe6612SVinod Koul 3877fe6612SVinod KoulMoreover, some DMA controllers, whenever the RAM is used as a source 3977fe6612SVinod Koulor destination, can group the reads or writes in memory into a buffer, 4077fe6612SVinod Koulso instead of having a lot of small memory accesses, which is not 4177fe6612SVinod Koulreally efficient, you'll get several bigger transfers. This is done 4277fe6612SVinod Koulusing a parameter called the burst size, that defines how many single 4377fe6612SVinod Koulreads/writes it's allowed to do without the controller splitting the 4477fe6612SVinod Koultransfer into smaller sub-transfers. 4577fe6612SVinod Koul 4677fe6612SVinod KoulOur theoretical DMA controller would then only be able to do transfers 4777fe6612SVinod Koulthat involve a single contiguous block of data. However, some of the 4877fe6612SVinod Koultransfers we usually have are not, and want to copy data from 4977fe6612SVinod Koulnon-contiguous buffers to a contiguous buffer, which is called 5077fe6612SVinod Koulscatter-gather. 5177fe6612SVinod Koul 5277fe6612SVinod KoulDMAEngine, at least for mem2dev transfers, require support for 5377fe6612SVinod Koulscatter-gather. So we're left with two cases here: either we have a 5477fe6612SVinod Koulquite simple DMA controller that doesn't support it, and we'll have to 5577fe6612SVinod Koulimplement it in software, or we have a more advanced DMA controller, 5677fe6612SVinod Koulthat implements in hardware scatter-gather. 5777fe6612SVinod Koul 5877fe6612SVinod KoulThe latter are usually programmed using a collection of chunks to 5977fe6612SVinod Koultransfer, and whenever the transfer is started, the controller will go 6077fe6612SVinod Koulover that collection, doing whatever we programmed there. 6177fe6612SVinod Koul 6277fe6612SVinod KoulThis collection is usually either a table or a linked list. You will 6377fe6612SVinod Koulthen push either the address of the table and its number of elements, 6477fe6612SVinod Koulor the first item of the list to one channel of the DMA controller, 6577fe6612SVinod Kouland whenever a DRQ will be asserted, it will go through the collection 6677fe6612SVinod Koulto know where to fetch the data from. 6777fe6612SVinod Koul 6877fe6612SVinod KoulEither way, the format of this collection is completely dependent on 6977fe6612SVinod Koulyour hardware. Each DMA controller will require a different structure, 7077fe6612SVinod Koulbut all of them will require, for every chunk, at least the source and 7177fe6612SVinod Kouldestination addresses, whether it should increment these addresses or 7277fe6612SVinod Koulnot and the three parameters we saw earlier: the burst size, the 7377fe6612SVinod Koultransfer width and the transfer size. 7477fe6612SVinod Koul 7577fe6612SVinod KoulThe one last thing is that usually, slave devices won't issue DRQ by 7677fe6612SVinod Kouldefault, and you have to enable this in your slave device driver first 7777fe6612SVinod Koulwhenever you're willing to use DMA. 7877fe6612SVinod Koul 7977fe6612SVinod KoulThese were just the general memory-to-memory (also called mem2mem) or 8077fe6612SVinod Koulmemory-to-device (mem2dev) kind of transfers. Most devices often 8177fe6612SVinod Koulsupport other kind of transfers or memory operations that dmaengine 8277fe6612SVinod Koulsupport and will be detailed later in this document. 8377fe6612SVinod Koul 8477fe6612SVinod KoulDMA Support in Linux 8577fe6612SVinod Koul==================== 8677fe6612SVinod Koul 8777fe6612SVinod KoulHistorically, DMA controller drivers have been implemented using the 8877fe6612SVinod Koulasync TX API, to offload operations such as memory copy, XOR, 8977fe6612SVinod Koulcryptography, etc., basically any memory to memory operation. 9077fe6612SVinod Koul 9177fe6612SVinod KoulOver time, the need for memory to device transfers arose, and 9277fe6612SVinod Kouldmaengine was extended. Nowadays, the async TX API is written as a 9377fe6612SVinod Koullayer on top of dmaengine, and acts as a client. Still, dmaengine 9477fe6612SVinod Koulaccommodates that API in some cases, and made some design choices to 9577fe6612SVinod Koulensure that it stayed compatible. 9677fe6612SVinod Koul 9777fe6612SVinod KoulFor more information on the Async TX API, please look the relevant 98ddc92399SMauro Carvalho Chehabdocumentation file in Documentation/crypto/async-tx-api.rst. 9977fe6612SVinod Koul 10077fe6612SVinod KoulDMAEngine APIs 10177fe6612SVinod Koul============== 10277fe6612SVinod Koul 10377fe6612SVinod Koul``struct dma_device`` Initialization 10477fe6612SVinod Koul------------------------------------ 10577fe6612SVinod Koul 10677fe6612SVinod KoulJust like any other kernel framework, the whole DMAEngine registration 10777fe6612SVinod Koulrelies on the driver filling a structure and registering against the 10877fe6612SVinod Koulframework. In our case, that structure is dma_device. 10977fe6612SVinod Koul 11077fe6612SVinod KoulThe first thing you need to do in your driver is to allocate this 11177fe6612SVinod Koulstructure. Any of the usual memory allocators will do, but you'll also 11277fe6612SVinod Koulneed to initialize a few fields in there: 11377fe6612SVinod Koul 114881053f7SLuca Ceresoli- ``channels``: should be initialized as a list using the 11577fe6612SVinod Koul INIT_LIST_HEAD macro for example 11677fe6612SVinod Koul 117881053f7SLuca Ceresoli- ``src_addr_widths``: 11877fe6612SVinod Koul should contain a bitmask of the supported source transfer width 11977fe6612SVinod Koul 120881053f7SLuca Ceresoli- ``dst_addr_widths``: 12177fe6612SVinod Koul should contain a bitmask of the supported destination transfer width 12277fe6612SVinod Koul 123881053f7SLuca Ceresoli- ``directions``: 12477fe6612SVinod Koul should contain a bitmask of the supported slave directions 12577fe6612SVinod Koul (i.e. excluding mem2mem transfers) 12677fe6612SVinod Koul 127881053f7SLuca Ceresoli- ``residue_granularity``: 128a5d33206SLuca Ceresoli granularity of the transfer residue reported to dma_set_residue. 12977fe6612SVinod Koul This can be either: 13077fe6612SVinod Koul 131a5d33206SLuca Ceresoli - Descriptor: 132a5d33206SLuca Ceresoli your device doesn't support any kind of residue 13377fe6612SVinod Koul reporting. The framework will only know that a particular 13477fe6612SVinod Koul transaction descriptor is done. 13577fe6612SVinod Koul 136a5d33206SLuca Ceresoli - Segment: 137a5d33206SLuca Ceresoli your device is able to report which chunks have been transferred 13877fe6612SVinod Koul 139a5d33206SLuca Ceresoli - Burst: 140a5d33206SLuca Ceresoli your device is able to report which burst have been transferred 14177fe6612SVinod Koul 142881053f7SLuca Ceresoli- ``dev``: should hold the pointer to the ``struct device`` associated 14377fe6612SVinod Koul to your current driver instance. 14477fe6612SVinod Koul 14577fe6612SVinod KoulSupported transaction types 14677fe6612SVinod Koul--------------------------- 14777fe6612SVinod Koul 14877fe6612SVinod KoulThe next thing you need is to set which transaction types your device 14977fe6612SVinod Koul(and driver) supports. 15077fe6612SVinod Koul 15177fe6612SVinod KoulOur ``dma_device structure`` has a field called cap_mask that holds the 15277fe6612SVinod Koulvarious types of transaction supported, and you need to modify this 15377fe6612SVinod Koulmask using the dma_cap_set function, with various flags depending on 15477fe6612SVinod Koultransaction types you support as an argument. 15577fe6612SVinod Koul 15677fe6612SVinod KoulAll those capabilities are defined in the ``dma_transaction_type enum``, 15777fe6612SVinod Koulin ``include/linux/dmaengine.h`` 15877fe6612SVinod Koul 15977fe6612SVinod KoulCurrently, the types available are: 16077fe6612SVinod Koul 16177fe6612SVinod Koul- DMA_MEMCPY 16277fe6612SVinod Koul 16377fe6612SVinod Koul - The device is able to do memory to memory copies 16477fe6612SVinod Koul 16558fe1076SAdrian Larumbe - No matter what the overall size of the combined chunks for source and 16658fe1076SAdrian Larumbe destination is, only as many bytes as the smallest of the two will be 16758fe1076SAdrian Larumbe transmitted. That means the number and size of the scatter-gather buffers in 16858fe1076SAdrian Larumbe both lists need not be the same, and that the operation functionally is 16958fe1076SAdrian Larumbe equivalent to a ``strncpy`` where the ``count`` argument equals the smallest 17058fe1076SAdrian Larumbe total size of the two scatter-gather list buffers. 17158fe1076SAdrian Larumbe 17258fe1076SAdrian Larumbe - It's usually used for copying pixel data between host memory and 17358fe1076SAdrian Larumbe memory-mapped GPU device memory, such as found on modern PCI video graphics 17458fe1076SAdrian Larumbe cards. The most immediate example is the OpenGL API function 17558fe1076SAdrian Larumbe ``glReadPielx()``, which might require a verbatim copy of a huge framebuffer 17658fe1076SAdrian Larumbe from local device memory onto host memory. 17758fe1076SAdrian Larumbe 17877fe6612SVinod Koul- DMA_XOR 17977fe6612SVinod Koul 18077fe6612SVinod Koul - The device is able to perform XOR operations on memory areas 18177fe6612SVinod Koul 18277fe6612SVinod Koul - Used to accelerate XOR intensive tasks, such as RAID5 18377fe6612SVinod Koul 18477fe6612SVinod Koul- DMA_XOR_VAL 18577fe6612SVinod Koul 18677fe6612SVinod Koul - The device is able to perform parity check using the XOR 18777fe6612SVinod Koul algorithm against a memory buffer. 18877fe6612SVinod Koul 18977fe6612SVinod Koul- DMA_PQ 19077fe6612SVinod Koul 19177fe6612SVinod Koul - The device is able to perform RAID6 P+Q computations, P being a 19277fe6612SVinod Koul simple XOR, and Q being a Reed-Solomon algorithm. 19377fe6612SVinod Koul 19477fe6612SVinod Koul- DMA_PQ_VAL 19577fe6612SVinod Koul 19677fe6612SVinod Koul - The device is able to perform parity check using RAID6 P+Q 19777fe6612SVinod Koul algorithm against a memory buffer. 19877fe6612SVinod Koul 199*fc44ff0aSBen Walker- DMA_MEMSET 200*fc44ff0aSBen Walker 201*fc44ff0aSBen Walker - The device is able to fill memory with the provided pattern 202*fc44ff0aSBen Walker 203*fc44ff0aSBen Walker - The pattern is treated as a single byte signed value. 204*fc44ff0aSBen Walker 20577fe6612SVinod Koul- DMA_INTERRUPT 20677fe6612SVinod Koul 20777fe6612SVinod Koul - The device is able to trigger a dummy transfer that will 20877fe6612SVinod Koul generate periodic interrupts 20977fe6612SVinod Koul 21077fe6612SVinod Koul - Used by the client drivers to register a callback that will be 21177fe6612SVinod Koul called on a regular basis through the DMA controller interrupt 21277fe6612SVinod Koul 21377fe6612SVinod Koul- DMA_PRIVATE 21477fe6612SVinod Koul 21577fe6612SVinod Koul - The devices only supports slave transfers, and as such isn't 21677fe6612SVinod Koul available for async transfers. 21777fe6612SVinod Koul 21877fe6612SVinod Koul- DMA_ASYNC_TX 21977fe6612SVinod Koul 22077fe6612SVinod Koul - Must not be set by the device, and will be set by the framework 22177fe6612SVinod Koul if needed 22277fe6612SVinod Koul 22377fe6612SVinod Koul - TODO: What is it about? 22477fe6612SVinod Koul 22577fe6612SVinod Koul- DMA_SLAVE 22677fe6612SVinod Koul 22777fe6612SVinod Koul - The device can handle device to memory transfers, including 22877fe6612SVinod Koul scatter-gather transfers. 22977fe6612SVinod Koul 23077fe6612SVinod Koul - While in the mem2mem case we were having two distinct types to 23177fe6612SVinod Koul deal with a single chunk to copy or a collection of them, here, 23277fe6612SVinod Koul we just have a single transaction type that is supposed to 23377fe6612SVinod Koul handle both. 23477fe6612SVinod Koul 23577fe6612SVinod Koul - If you want to transfer a single contiguous memory buffer, 23677fe6612SVinod Koul simply build a scatter list with only one item. 23777fe6612SVinod Koul 23877fe6612SVinod Koul- DMA_CYCLIC 23977fe6612SVinod Koul 24077fe6612SVinod Koul - The device can handle cyclic transfers. 24177fe6612SVinod Koul 24277fe6612SVinod Koul - A cyclic transfer is a transfer where the chunk collection will 24377fe6612SVinod Koul loop over itself, with the last item pointing to the first. 24477fe6612SVinod Koul 24577fe6612SVinod Koul - It's usually used for audio transfers, where you want to operate 24677fe6612SVinod Koul on a single ring buffer that you will fill with your audio data. 24777fe6612SVinod Koul 24877fe6612SVinod Koul- DMA_INTERLEAVE 24977fe6612SVinod Koul 25077fe6612SVinod Koul - The device supports interleaved transfer. 25177fe6612SVinod Koul 25277fe6612SVinod Koul - These transfers can transfer data from a non-contiguous buffer 25377fe6612SVinod Koul to a non-contiguous buffer, opposed to DMA_SLAVE that can 25477fe6612SVinod Koul transfer data from a non-contiguous data set to a continuous 25577fe6612SVinod Koul destination buffer. 25677fe6612SVinod Koul 25777fe6612SVinod Koul - It's usually used for 2d content transfers, in which case you 25877fe6612SVinod Koul want to transfer a portion of uncompressed data directly to the 25977fe6612SVinod Koul display to print it 26077fe6612SVinod Koul 26147ec7f09SDave Jiang- DMA_COMPLETION_NO_ORDER 26247ec7f09SDave Jiang 26347ec7f09SDave Jiang - The device does not support in order completion. 26447ec7f09SDave Jiang 26547ec7f09SDave Jiang - The driver should return DMA_OUT_OF_ORDER for device_tx_status if 26647ec7f09SDave Jiang the device is setting this capability. 26747ec7f09SDave Jiang 26847ec7f09SDave Jiang - All cookie tracking and checking API should be treated as invalid if 26947ec7f09SDave Jiang the device exports this capability. 27047ec7f09SDave Jiang 27147ec7f09SDave Jiang - At this point, this is incompatible with polling option for dmatest. 27247ec7f09SDave Jiang 27347ec7f09SDave Jiang - If this cap is set, the user is recommended to provide an unique 27447ec7f09SDave Jiang identifier for each descriptor sent to the DMA device in order to 27547ec7f09SDave Jiang properly track the completion. 27647ec7f09SDave Jiang 2779c8ebd8bSLaurent Pinchart- DMA_REPEAT 2789c8ebd8bSLaurent Pinchart 2799c8ebd8bSLaurent Pinchart - The device supports repeated transfers. A repeated transfer, indicated by 2809c8ebd8bSLaurent Pinchart the DMA_PREP_REPEAT transfer flag, is similar to a cyclic transfer in that 2819c8ebd8bSLaurent Pinchart it gets automatically repeated when it ends, but can additionally be 2829c8ebd8bSLaurent Pinchart replaced by the client. 2839c8ebd8bSLaurent Pinchart 2849c8ebd8bSLaurent Pinchart - This feature is limited to interleaved transfers, this flag should thus not 2859c8ebd8bSLaurent Pinchart be set if the DMA_INTERLEAVE flag isn't set. This limitation is based on 2869c8ebd8bSLaurent Pinchart the current needs of DMA clients, support for additional transfer types 2879c8ebd8bSLaurent Pinchart should be added in the future if and when the need arises. 2889c8ebd8bSLaurent Pinchart 2899c8ebd8bSLaurent Pinchart- DMA_LOAD_EOT 2909c8ebd8bSLaurent Pinchart 2919c8ebd8bSLaurent Pinchart - The device supports replacing repeated transfers at end of transfer (EOT) 2929c8ebd8bSLaurent Pinchart by queuing a new transfer with the DMA_PREP_LOAD_EOT flag set. 2939c8ebd8bSLaurent Pinchart 2949c8ebd8bSLaurent Pinchart - Support for replacing a currently running transfer at another point (such 2959c8ebd8bSLaurent Pinchart as end of burst instead of end of transfer) will be added in the future 2969c8ebd8bSLaurent Pinchart based on DMA clients needs, if and when the need arises. 2979c8ebd8bSLaurent Pinchart 29877fe6612SVinod KoulThese various types will also affect how the source and destination 29977fe6612SVinod Kouladdresses change over time. 30077fe6612SVinod Koul 30177fe6612SVinod KoulAddresses pointing to RAM are typically incremented (or decremented) 30277fe6612SVinod Koulafter each transfer. In case of a ring buffer, they may loop 30377fe6612SVinod Koul(DMA_CYCLIC). Addresses pointing to a device's register (e.g. a FIFO) 30477fe6612SVinod Koulare typically fixed. 30577fe6612SVinod Koul 3067d083ae9SPeter UjfalusiPer descriptor metadata support 3077d083ae9SPeter Ujfalusi------------------------------- 3087d083ae9SPeter UjfalusiSome data movement architecture (DMA controller and peripherals) uses metadata 3097d083ae9SPeter Ujfalusiassociated with a transaction. The DMA controller role is to transfer the 3107d083ae9SPeter Ujfalusipayload and the metadata alongside. 3117d083ae9SPeter UjfalusiThe metadata itself is not used by the DMA engine itself, but it contains 3127d083ae9SPeter Ujfalusiparameters, keys, vectors, etc for peripheral or from the peripheral. 3137d083ae9SPeter Ujfalusi 3147d083ae9SPeter UjfalusiThe DMAengine framework provides a generic ways to facilitate the metadata for 3157d083ae9SPeter Ujfalusidescriptors. Depending on the architecture the DMA driver can implement either 3167d083ae9SPeter Ujfalusior both of the methods and it is up to the client driver to choose which one 3177d083ae9SPeter Ujfalusito use. 3187d083ae9SPeter Ujfalusi 3197d083ae9SPeter Ujfalusi- DESC_METADATA_CLIENT 3207d083ae9SPeter Ujfalusi 3217d083ae9SPeter Ujfalusi The metadata buffer is allocated/provided by the client driver and it is 3227d083ae9SPeter Ujfalusi attached (via the dmaengine_desc_attach_metadata() helper to the descriptor. 3237d083ae9SPeter Ujfalusi 3247d083ae9SPeter Ujfalusi From the DMA driver the following is expected for this mode: 325cf7da891SMauro Carvalho Chehab 3267d083ae9SPeter Ujfalusi - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM 327cf7da891SMauro Carvalho Chehab 3287d083ae9SPeter Ujfalusi The data from the provided metadata buffer should be prepared for the DMA 3297d083ae9SPeter Ujfalusi controller to be sent alongside of the payload data. Either by copying to a 3307d083ae9SPeter Ujfalusi hardware descriptor, or highly coupled packet. 331cf7da891SMauro Carvalho Chehab 3327d083ae9SPeter Ujfalusi - DMA_DEV_TO_MEM 333cf7da891SMauro Carvalho Chehab 3347d083ae9SPeter Ujfalusi On transfer completion the DMA driver must copy the metadata to the client 3357d083ae9SPeter Ujfalusi provided metadata buffer before notifying the client about the completion. 3367d083ae9SPeter Ujfalusi After the transfer completion, DMA drivers must not touch the metadata 3377d083ae9SPeter Ujfalusi buffer provided by the client. 3387d083ae9SPeter Ujfalusi 3397d083ae9SPeter Ujfalusi- DESC_METADATA_ENGINE 3407d083ae9SPeter Ujfalusi 3417d083ae9SPeter Ujfalusi The metadata buffer is allocated/managed by the DMA driver. The client driver 3427d083ae9SPeter Ujfalusi can ask for the pointer, maximum size and the currently used size of the 3437d083ae9SPeter Ujfalusi metadata and can directly update or read it. dmaengine_desc_get_metadata_ptr() 3447d083ae9SPeter Ujfalusi and dmaengine_desc_set_metadata_len() is provided as helper functions. 3457d083ae9SPeter Ujfalusi 3467d083ae9SPeter Ujfalusi From the DMA driver the following is expected for this mode: 347cf7da891SMauro Carvalho Chehab 348cf7da891SMauro Carvalho Chehab - get_metadata_ptr() 349cf7da891SMauro Carvalho Chehab 3507d083ae9SPeter Ujfalusi Should return a pointer for the metadata buffer, the maximum size of the 3517d083ae9SPeter Ujfalusi metadata buffer and the currently used / valid (if any) bytes in the buffer. 352cf7da891SMauro Carvalho Chehab 353cf7da891SMauro Carvalho Chehab - set_metadata_len() 354cf7da891SMauro Carvalho Chehab 3557d083ae9SPeter Ujfalusi It is called by the clients after it have placed the metadata to the buffer 3567d083ae9SPeter Ujfalusi to let the DMA driver know the number of valid bytes provided. 3577d083ae9SPeter Ujfalusi 3587d083ae9SPeter Ujfalusi Note: since the client will ask for the metadata pointer in the completion 3597d083ae9SPeter Ujfalusi callback (in DMA_DEV_TO_MEM case) the DMA driver must ensure that the 3607d083ae9SPeter Ujfalusi descriptor is not freed up prior the callback is called. 3617d083ae9SPeter Ujfalusi 36277fe6612SVinod KoulDevice operations 36377fe6612SVinod Koul----------------- 36477fe6612SVinod Koul 36577fe6612SVinod KoulOur dma_device structure also requires a few function pointers in 36677fe6612SVinod Koulorder to implement the actual logic, now that we described what 36777fe6612SVinod Kouloperations we were able to perform. 36877fe6612SVinod Koul 36977fe6612SVinod KoulThe functions that we have to fill in there, and hence have to 37077fe6612SVinod Koulimplement, obviously depend on the transaction types you reported as 37177fe6612SVinod Koulsupported. 37277fe6612SVinod Koul 37377fe6612SVinod Koul- ``device_alloc_chan_resources`` 37477fe6612SVinod Koul 37577fe6612SVinod Koul- ``device_free_chan_resources`` 37677fe6612SVinod Koul 37777fe6612SVinod Koul - These functions will be called whenever a driver will call 37877fe6612SVinod Koul ``dma_request_channel`` or ``dma_release_channel`` for the first/last 37977fe6612SVinod Koul time on the channel associated to that driver. 38077fe6612SVinod Koul 38177fe6612SVinod Koul - They are in charge of allocating/freeing all the needed 38277fe6612SVinod Koul resources in order for that channel to be useful for your driver. 38377fe6612SVinod Koul 38477fe6612SVinod Koul - These functions can sleep. 38577fe6612SVinod Koul 38677fe6612SVinod Koul- ``device_prep_dma_*`` 38777fe6612SVinod Koul 38877fe6612SVinod Koul - These functions are matching the capabilities you registered 38977fe6612SVinod Koul previously. 39077fe6612SVinod Koul 39177fe6612SVinod Koul - These functions all take the buffer or the scatterlist relevant 39277fe6612SVinod Koul for the transfer being prepared, and should create a hardware 39377fe6612SVinod Koul descriptor or a list of hardware descriptors from it 39477fe6612SVinod Koul 39577fe6612SVinod Koul - These functions can be called from an interrupt context 39677fe6612SVinod Koul 39777fe6612SVinod Koul - Any allocation you might do should be using the GFP_NOWAIT 39877fe6612SVinod Koul flag, in order not to potentially sleep, but without depleting 39977fe6612SVinod Koul the emergency pool either. 40077fe6612SVinod Koul 40177fe6612SVinod Koul - Drivers should try to pre-allocate any memory they might need 40277fe6612SVinod Koul during the transfer setup at probe time to avoid putting to 40377fe6612SVinod Koul much pressure on the nowait allocator. 40477fe6612SVinod Koul 40577fe6612SVinod Koul - It should return a unique instance of the 40677fe6612SVinod Koul ``dma_async_tx_descriptor structure``, that further represents this 40777fe6612SVinod Koul particular transfer. 40877fe6612SVinod Koul 40977fe6612SVinod Koul - This structure can be initialized using the function 41077fe6612SVinod Koul ``dma_async_tx_descriptor_init``. 41177fe6612SVinod Koul 41277fe6612SVinod Koul - You'll also need to set two fields in this structure: 41377fe6612SVinod Koul 41477fe6612SVinod Koul - flags: 41577fe6612SVinod Koul TODO: Can it be modified by the driver itself, or 41677fe6612SVinod Koul should it be always the flags passed in the arguments 41777fe6612SVinod Koul 41877fe6612SVinod Koul - tx_submit: A pointer to a function you have to implement, 41977fe6612SVinod Koul that is supposed to push the current transaction descriptor to a 42077fe6612SVinod Koul pending queue, waiting for issue_pending to be called. 42177fe6612SVinod Koul 42277fe6612SVinod Koul - In this structure the function pointer callback_result can be 42377fe6612SVinod Koul initialized in order for the submitter to be notified that a 42477fe6612SVinod Koul transaction has completed. In the earlier code the function pointer 42577fe6612SVinod Koul callback has been used. However it does not provide any status to the 42677fe6612SVinod Koul transaction and will be deprecated. The result structure defined as 42777fe6612SVinod Koul ``dmaengine_result`` that is passed in to callback_result 42877fe6612SVinod Koul has two fields: 42977fe6612SVinod Koul 43077fe6612SVinod Koul - result: This provides the transfer result defined by 43177fe6612SVinod Koul ``dmaengine_tx_result``. Either success or some error condition. 43277fe6612SVinod Koul 43377fe6612SVinod Koul - residue: Provides the residue bytes of the transfer for those that 43477fe6612SVinod Koul support residue. 43577fe6612SVinod Koul 43677fe6612SVinod Koul- ``device_issue_pending`` 43777fe6612SVinod Koul 43877fe6612SVinod Koul - Takes the first transaction descriptor in the pending queue, 43977fe6612SVinod Koul and starts the transfer. Whenever that transfer is done, it 44077fe6612SVinod Koul should move to the next transaction in the list. 44177fe6612SVinod Koul 44277fe6612SVinod Koul - This function can be called in an interrupt context 44377fe6612SVinod Koul 44477fe6612SVinod Koul- ``device_tx_status`` 44577fe6612SVinod Koul 44677fe6612SVinod Koul - Should report the bytes left to go over on the given channel 44777fe6612SVinod Koul 44877fe6612SVinod Koul - Should only care about the transaction descriptor passed as 44977fe6612SVinod Koul argument, not the currently active one on a given channel 45077fe6612SVinod Koul 45177fe6612SVinod Koul - The tx_state argument might be NULL 45277fe6612SVinod Koul 45377fe6612SVinod Koul - Should use dma_set_residue to report it 45477fe6612SVinod Koul 45577fe6612SVinod Koul - In the case of a cyclic transfer, it should only take into 4561f854536SPaul Kocialkowski account the total size of the cyclic buffer. 45777fe6612SVinod Koul 45847ec7f09SDave Jiang - Should return DMA_OUT_OF_ORDER if the device does not support in order 45947ec7f09SDave Jiang completion and is completing the operation out of order. 46047ec7f09SDave Jiang 46177fe6612SVinod Koul - This function can be called in an interrupt context. 46277fe6612SVinod Koul 46377fe6612SVinod Koul- device_config 46477fe6612SVinod Koul 46577fe6612SVinod Koul - Reconfigures the channel with the configuration given as argument 46677fe6612SVinod Koul 46777fe6612SVinod Koul - This command should NOT perform synchronously, or on any 46877fe6612SVinod Koul currently queued transfers, but only on subsequent ones 46977fe6612SVinod Koul 47077fe6612SVinod Koul - In this case, the function will receive a ``dma_slave_config`` 47177fe6612SVinod Koul structure pointer as an argument, that will detail which 47277fe6612SVinod Koul configuration to use. 47377fe6612SVinod Koul 47477fe6612SVinod Koul - Even though that structure contains a direction field, this 47577fe6612SVinod Koul field is deprecated in favor of the direction argument given to 47677fe6612SVinod Koul the prep_* functions 47777fe6612SVinod Koul 47877fe6612SVinod Koul - This call is mandatory for slave operations only. This should NOT be 47977fe6612SVinod Koul set or expected to be set for memcpy operations. 48077fe6612SVinod Koul If a driver support both, it should use this call for slave 48177fe6612SVinod Koul operations only and not for memcpy ones. 48277fe6612SVinod Koul 48377fe6612SVinod Koul- device_pause 48477fe6612SVinod Koul 48577fe6612SVinod Koul - Pauses a transfer on the channel 48677fe6612SVinod Koul 48777fe6612SVinod Koul - This command should operate synchronously on the channel, 48877fe6612SVinod Koul pausing right away the work of the given channel 48977fe6612SVinod Koul 49077fe6612SVinod Koul- device_resume 49177fe6612SVinod Koul 49277fe6612SVinod Koul - Resumes a transfer on the channel 49377fe6612SVinod Koul 49477fe6612SVinod Koul - This command should operate synchronously on the channel, 49577fe6612SVinod Koul resuming right away the work of the given channel 49677fe6612SVinod Koul 49777fe6612SVinod Koul- device_terminate_all 49877fe6612SVinod Koul 49977fe6612SVinod Koul - Aborts all the pending and ongoing transfers on the channel 50077fe6612SVinod Koul 50177fe6612SVinod Koul - For aborted transfers the complete callback should not be called 50277fe6612SVinod Koul 50377fe6612SVinod Koul - Can be called from atomic context or from within a complete 50477fe6612SVinod Koul callback of a descriptor. Must not sleep. Drivers must be able 50577fe6612SVinod Koul to handle this correctly. 50677fe6612SVinod Koul 50777fe6612SVinod Koul - Termination may be asynchronous. The driver does not have to 50877fe6612SVinod Koul wait until the currently active transfer has completely stopped. 50977fe6612SVinod Koul See device_synchronize. 51077fe6612SVinod Koul 51177fe6612SVinod Koul- device_synchronize 51277fe6612SVinod Koul 51377fe6612SVinod Koul - Must synchronize the termination of a channel to the current 51477fe6612SVinod Koul context. 51577fe6612SVinod Koul 51677fe6612SVinod Koul - Must make sure that memory for previously submitted 51777fe6612SVinod Koul descriptors is no longer accessed by the DMA controller. 51877fe6612SVinod Koul 51977fe6612SVinod Koul - Must make sure that all complete callbacks for previously 52077fe6612SVinod Koul submitted descriptors have finished running and none are 52177fe6612SVinod Koul scheduled to run. 52277fe6612SVinod Koul 52377fe6612SVinod Koul - May sleep. 52477fe6612SVinod Koul 52577fe6612SVinod Koul 52677fe6612SVinod KoulMisc notes 52777fe6612SVinod Koul========== 52877fe6612SVinod Koul 52977fe6612SVinod Koul(stuff that should be documented, but don't really know 53077fe6612SVinod Koulwhere to put them) 53177fe6612SVinod Koul 53277fe6612SVinod Koul``dma_run_dependencies`` 53377fe6612SVinod Koul 53477fe6612SVinod Koul- Should be called at the end of an async TX transfer, and can be 53577fe6612SVinod Koul ignored in the slave transfers case. 53677fe6612SVinod Koul 53777fe6612SVinod Koul- Makes sure that dependent operations are run before marking it 53877fe6612SVinod Koul as complete. 53977fe6612SVinod Koul 54077fe6612SVinod Kouldma_cookie_t 54177fe6612SVinod Koul 54277fe6612SVinod Koul- it's a DMA transaction ID that will increment over time. 54377fe6612SVinod Koul 54477fe6612SVinod Koul- Not really relevant any more since the introduction of ``virt-dma`` 54577fe6612SVinod Koul that abstracts it away. 54677fe6612SVinod Koul 54777fe6612SVinod KoulDMA_CTRL_ACK 54877fe6612SVinod Koul 54977fe6612SVinod Koul- If clear, the descriptor cannot be reused by provider until the 5503621d3e5SRandy Dunlap client acknowledges receipt, i.e. has a chance to establish any 55177fe6612SVinod Koul dependency chains 55277fe6612SVinod Koul 55377fe6612SVinod Koul- This can be acked by invoking async_tx_ack() 55477fe6612SVinod Koul 55577fe6612SVinod Koul- If set, does not mean descriptor can be reused 55677fe6612SVinod Koul 55777fe6612SVinod KoulDMA_CTRL_REUSE 55877fe6612SVinod Koul 55977fe6612SVinod Koul- If set, the descriptor can be reused after being completed. It should 56077fe6612SVinod Koul not be freed by provider if this flag is set. 56177fe6612SVinod Koul 56277fe6612SVinod Koul- The descriptor should be prepared for reuse by invoking 56377fe6612SVinod Koul ``dmaengine_desc_set_reuse()`` which will set DMA_CTRL_REUSE. 56477fe6612SVinod Koul 56577fe6612SVinod Koul- ``dmaengine_desc_set_reuse()`` will succeed only when channel support 56677fe6612SVinod Koul reusable descriptor as exhibited by capabilities 56777fe6612SVinod Koul 56877fe6612SVinod Koul- As a consequence, if a device driver wants to skip the 56977fe6612SVinod Koul ``dma_map_sg()`` and ``dma_unmap_sg()`` in between 2 transfers, 57077fe6612SVinod Koul because the DMA'd data wasn't used, it can resubmit the transfer right after 57177fe6612SVinod Koul its completion. 57277fe6612SVinod Koul 57377fe6612SVinod Koul- Descriptor can be freed in few ways 57477fe6612SVinod Koul 57577fe6612SVinod Koul - Clearing DMA_CTRL_REUSE by invoking 57677fe6612SVinod Koul ``dmaengine_desc_clear_reuse()`` and submitting for last txn 57777fe6612SVinod Koul 57877fe6612SVinod Koul - Explicitly invoking ``dmaengine_desc_free()``, this can succeed only 57977fe6612SVinod Koul when DMA_CTRL_REUSE is already set 58077fe6612SVinod Koul 58177fe6612SVinod Koul - Terminating the channel 58277fe6612SVinod Koul 58377fe6612SVinod Koul- DMA_PREP_CMD 58477fe6612SVinod Koul 58577fe6612SVinod Koul - If set, the client driver tells DMA controller that passed data in DMA 58677fe6612SVinod Koul API is command data. 58777fe6612SVinod Koul 58877fe6612SVinod Koul - Interpretation of command data is DMA controller specific. It can be 58977fe6612SVinod Koul used for issuing commands to other peripherals/register reads/register 59077fe6612SVinod Koul writes for which the descriptor should be in different format from 59177fe6612SVinod Koul normal data descriptors. 59277fe6612SVinod Koul 5939c8ebd8bSLaurent Pinchart- DMA_PREP_REPEAT 5949c8ebd8bSLaurent Pinchart 5959c8ebd8bSLaurent Pinchart - If set, the transfer will be automatically repeated when it ends until a 5969c8ebd8bSLaurent Pinchart new transfer is queued on the same channel with the DMA_PREP_LOAD_EOT flag. 5979c8ebd8bSLaurent Pinchart If the next transfer to be queued on the channel does not have the 5989c8ebd8bSLaurent Pinchart DMA_PREP_LOAD_EOT flag set, the current transfer will be repeated until the 5999c8ebd8bSLaurent Pinchart client terminates all transfers. 6009c8ebd8bSLaurent Pinchart 6019c8ebd8bSLaurent Pinchart - This flag is only supported if the channel reports the DMA_REPEAT 6029c8ebd8bSLaurent Pinchart capability. 6039c8ebd8bSLaurent Pinchart 6049c8ebd8bSLaurent Pinchart- DMA_PREP_LOAD_EOT 6059c8ebd8bSLaurent Pinchart 6069c8ebd8bSLaurent Pinchart - If set, the transfer will replace the transfer currently being executed at 6079c8ebd8bSLaurent Pinchart the end of the transfer. 6089c8ebd8bSLaurent Pinchart 6099c8ebd8bSLaurent Pinchart - This is the default behaviour for non-repeated transfers, specifying 6109c8ebd8bSLaurent Pinchart DMA_PREP_LOAD_EOT for non-repeated transfers will thus make no difference. 6119c8ebd8bSLaurent Pinchart 6129c8ebd8bSLaurent Pinchart - When using repeated transfers, DMA clients will usually need to set the 6139c8ebd8bSLaurent Pinchart DMA_PREP_LOAD_EOT flag on all transfers, otherwise the channel will keep 6149c8ebd8bSLaurent Pinchart repeating the last repeated transfer and ignore the new transfers being 6159c8ebd8bSLaurent Pinchart queued. Failure to set DMA_PREP_LOAD_EOT will appear as if the channel was 6169c8ebd8bSLaurent Pinchart stuck on the previous transfer. 6179c8ebd8bSLaurent Pinchart 6189c8ebd8bSLaurent Pinchart - This flag is only supported if the channel reports the DMA_LOAD_EOT 6199c8ebd8bSLaurent Pinchart capability. 6209c8ebd8bSLaurent Pinchart 62177fe6612SVinod KoulGeneral Design Notes 62277fe6612SVinod Koul==================== 62377fe6612SVinod Koul 62477fe6612SVinod KoulMost of the DMAEngine drivers you'll see are based on a similar design 62577fe6612SVinod Koulthat handles the end of transfer interrupts in the handler, but defer 62677fe6612SVinod Koulmost work to a tasklet, including the start of a new transfer whenever 62777fe6612SVinod Koulthe previous transfer ended. 62877fe6612SVinod Koul 62977fe6612SVinod KoulThis is a rather inefficient design though, because the inter-transfer 63077fe6612SVinod Koullatency will be not only the interrupt latency, but also the 63177fe6612SVinod Koulscheduling latency of the tasklet, which will leave the channel idle 63277fe6612SVinod Koulin between, which will slow down the global transfer rate. 63377fe6612SVinod Koul 63477fe6612SVinod KoulYou should avoid this kind of practice, and instead of electing a new 63577fe6612SVinod Koultransfer in your tasklet, move that part to the interrupt handler in 63677fe6612SVinod Koulorder to have a shorter idle window (that we can't really avoid 63777fe6612SVinod Koulanyway). 63877fe6612SVinod Koul 63977fe6612SVinod KoulGlossary 64077fe6612SVinod Koul======== 64177fe6612SVinod Koul 64277fe6612SVinod Koul- Burst: A number of consecutive read or write operations that 64377fe6612SVinod Koul can be queued to buffers before being flushed to memory. 64477fe6612SVinod Koul 64577fe6612SVinod Koul- Chunk: A contiguous collection of bursts 64677fe6612SVinod Koul 64777fe6612SVinod Koul- Transfer: A collection of chunks (be it contiguous or not) 648