18c2ecf20Sopenharmony_ci================================== 28c2ecf20Sopenharmony_ciDMAengine controller documentation 38c2ecf20Sopenharmony_ci================================== 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ciHardware Introduction 68c2ecf20Sopenharmony_ci===================== 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ciMost of the Slave DMA controllers have the same general principles of 98c2ecf20Sopenharmony_cioperations. 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ciThey have a given number of channels to use for the DMA transfers, and 128c2ecf20Sopenharmony_cia given number of requests lines. 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ciRequests and channels are pretty much orthogonal. Channels can be used 158c2ecf20Sopenharmony_cito serve several to any requests. To simplify, channels are the 168c2ecf20Sopenharmony_cientities that will be doing the copy, and requests what endpoints are 178c2ecf20Sopenharmony_ciinvolved. 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciThe request lines actually correspond to physical lines going from the 208c2ecf20Sopenharmony_ciDMA-eligible devices to the controller itself. Whenever the device 218c2ecf20Sopenharmony_ciwill want to start a transfer, it will assert a DMA request (DRQ) by 228c2ecf20Sopenharmony_ciasserting that request line. 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ciA very simple DMA controller would only take into account a single 258c2ecf20Sopenharmony_ciparameter: the transfer size. At each clock cycle, it would transfer a 268c2ecf20Sopenharmony_cibyte of data from one buffer to another, until the transfer size has 278c2ecf20Sopenharmony_cibeen reached. 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ciThat wouldn't work well in the real world, since slave devices might 308c2ecf20Sopenharmony_cirequire a specific number of bits to be transferred in a single 318c2ecf20Sopenharmony_cicycle. For example, we may want to transfer as much data as the 328c2ecf20Sopenharmony_ciphysical bus allows to maximize performances when doing a simple 338c2ecf20Sopenharmony_cimemory copy operation, but our audio device could have a narrower FIFO 348c2ecf20Sopenharmony_cithat requires data to be written exactly 16 or 24 bits at a time. This 358c2ecf20Sopenharmony_ciis why most if not all of the DMA controllers can adjust this, using a 368c2ecf20Sopenharmony_ciparameter called the transfer width. 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ciMoreover, some DMA controllers, whenever the RAM is used as a source 398c2ecf20Sopenharmony_cior destination, can group the reads or writes in memory into a buffer, 408c2ecf20Sopenharmony_ciso instead of having a lot of small memory accesses, which is not 418c2ecf20Sopenharmony_cireally efficient, you'll get several bigger transfers. This is done 428c2ecf20Sopenharmony_ciusing a parameter called the burst size, that defines how many single 438c2ecf20Sopenharmony_cireads/writes it's allowed to do without the controller splitting the 448c2ecf20Sopenharmony_citransfer into smaller sub-transfers. 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ciOur theoretical DMA controller would then only be able to do transfers 478c2ecf20Sopenharmony_cithat involve a single contiguous block of data. However, some of the 488c2ecf20Sopenharmony_citransfers we usually have are not, and want to copy data from 498c2ecf20Sopenharmony_cinon-contiguous buffers to a contiguous buffer, which is called 508c2ecf20Sopenharmony_ciscatter-gather. 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ciDMAEngine, at least for mem2dev transfers, require support for 538c2ecf20Sopenharmony_ciscatter-gather. So we're left with two cases here: either we have a 548c2ecf20Sopenharmony_ciquite simple DMA controller that doesn't support it, and we'll have to 558c2ecf20Sopenharmony_ciimplement it in software, or we have a more advanced DMA controller, 568c2ecf20Sopenharmony_cithat implements in hardware scatter-gather. 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ciThe latter are usually programmed using a collection of chunks to 598c2ecf20Sopenharmony_citransfer, and whenever the transfer is started, the controller will go 608c2ecf20Sopenharmony_ciover that collection, doing whatever we programmed there. 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ciThis collection is usually either a table or a linked list. You will 638c2ecf20Sopenharmony_cithen push either the address of the table and its number of elements, 648c2ecf20Sopenharmony_cior the first item of the list to one channel of the DMA controller, 658c2ecf20Sopenharmony_ciand whenever a DRQ will be asserted, it will go through the collection 668c2ecf20Sopenharmony_cito know where to fetch the data from. 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciEither way, the format of this collection is completely dependent on 698c2ecf20Sopenharmony_ciyour hardware. Each DMA controller will require a different structure, 708c2ecf20Sopenharmony_cibut all of them will require, for every chunk, at least the source and 718c2ecf20Sopenharmony_cidestination addresses, whether it should increment these addresses or 728c2ecf20Sopenharmony_cinot and the three parameters we saw earlier: the burst size, the 738c2ecf20Sopenharmony_citransfer width and the transfer size. 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciThe one last thing is that usually, slave devices won't issue DRQ by 768c2ecf20Sopenharmony_cidefault, and you have to enable this in your slave device driver first 778c2ecf20Sopenharmony_ciwhenever you're willing to use DMA. 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ciThese were just the general memory-to-memory (also called mem2mem) or 808c2ecf20Sopenharmony_cimemory-to-device (mem2dev) kind of transfers. Most devices often 818c2ecf20Sopenharmony_cisupport other kind of transfers or memory operations that dmaengine 828c2ecf20Sopenharmony_cisupport and will be detailed later in this document. 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ciDMA Support in Linux 858c2ecf20Sopenharmony_ci==================== 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciHistorically, DMA controller drivers have been implemented using the 888c2ecf20Sopenharmony_ciasync TX API, to offload operations such as memory copy, XOR, 898c2ecf20Sopenharmony_cicryptography, etc., basically any memory to memory operation. 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ciOver time, the need for memory to device transfers arose, and 928c2ecf20Sopenharmony_cidmaengine was extended. Nowadays, the async TX API is written as a 938c2ecf20Sopenharmony_cilayer on top of dmaengine, and acts as a client. Still, dmaengine 948c2ecf20Sopenharmony_ciaccommodates that API in some cases, and made some design choices to 958c2ecf20Sopenharmony_ciensure that it stayed compatible. 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ciFor more information on the Async TX API, please look the relevant 988c2ecf20Sopenharmony_cidocumentation file in Documentation/crypto/async-tx-api.rst. 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ciDMAEngine APIs 1018c2ecf20Sopenharmony_ci============== 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci``struct dma_device`` Initialization 1048c2ecf20Sopenharmony_ci------------------------------------ 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ciJust like any other kernel framework, the whole DMAEngine registration 1078c2ecf20Sopenharmony_cirelies on the driver filling a structure and registering against the 1088c2ecf20Sopenharmony_ciframework. In our case, that structure is dma_device. 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ciThe first thing you need to do in your driver is to allocate this 1118c2ecf20Sopenharmony_cistructure. Any of the usual memory allocators will do, but you'll also 1128c2ecf20Sopenharmony_cineed to initialize a few fields in there: 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci- ``channels``: should be initialized as a list using the 1158c2ecf20Sopenharmony_ci INIT_LIST_HEAD macro for example 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci- ``src_addr_widths``: 1188c2ecf20Sopenharmony_ci should contain a bitmask of the supported source transfer width 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci- ``dst_addr_widths``: 1218c2ecf20Sopenharmony_ci should contain a bitmask of the supported destination transfer width 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci- ``directions``: 1248c2ecf20Sopenharmony_ci should contain a bitmask of the supported slave directions 1258c2ecf20Sopenharmony_ci (i.e. excluding mem2mem transfers) 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci- ``residue_granularity``: 1288c2ecf20Sopenharmony_ci granularity of the transfer residue reported to dma_set_residue. 1298c2ecf20Sopenharmony_ci This can be either: 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci - Descriptor: 1328c2ecf20Sopenharmony_ci your device doesn't support any kind of residue 1338c2ecf20Sopenharmony_ci reporting. The framework will only know that a particular 1348c2ecf20Sopenharmony_ci transaction descriptor is done. 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci - Segment: 1378c2ecf20Sopenharmony_ci your device is able to report which chunks have been transferred 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci - Burst: 1408c2ecf20Sopenharmony_ci your device is able to report which burst have been transferred 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci- ``dev``: should hold the pointer to the ``struct device`` associated 1438c2ecf20Sopenharmony_ci to your current driver instance. 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ciSupported transaction types 1468c2ecf20Sopenharmony_ci--------------------------- 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ciThe next thing you need is to set which transaction types your device 1498c2ecf20Sopenharmony_ci(and driver) supports. 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ciOur ``dma_device structure`` has a field called cap_mask that holds the 1528c2ecf20Sopenharmony_civarious types of transaction supported, and you need to modify this 1538c2ecf20Sopenharmony_cimask using the dma_cap_set function, with various flags depending on 1548c2ecf20Sopenharmony_citransaction types you support as an argument. 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ciAll those capabilities are defined in the ``dma_transaction_type enum``, 1578c2ecf20Sopenharmony_ciin ``include/linux/dmaengine.h`` 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ciCurrently, the types available are: 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci- DMA_MEMCPY 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci - The device is able to do memory to memory copies 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci- DMA_XOR 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci - The device is able to perform XOR operations on memory areas 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci - Used to accelerate XOR intensive tasks, such as RAID5 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci- DMA_XOR_VAL 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci - The device is able to perform parity check using the XOR 1748c2ecf20Sopenharmony_ci algorithm against a memory buffer. 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci- DMA_PQ 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci - The device is able to perform RAID6 P+Q computations, P being a 1798c2ecf20Sopenharmony_ci simple XOR, and Q being a Reed-Solomon algorithm. 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci- DMA_PQ_VAL 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci - The device is able to perform parity check using RAID6 P+Q 1848c2ecf20Sopenharmony_ci algorithm against a memory buffer. 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci- DMA_INTERRUPT 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci - The device is able to trigger a dummy transfer that will 1898c2ecf20Sopenharmony_ci generate periodic interrupts 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci - Used by the client drivers to register a callback that will be 1928c2ecf20Sopenharmony_ci called on a regular basis through the DMA controller interrupt 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci- DMA_PRIVATE 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci - The devices only supports slave transfers, and as such isn't 1978c2ecf20Sopenharmony_ci available for async transfers. 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci- DMA_ASYNC_TX 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci - Must not be set by the device, and will be set by the framework 2028c2ecf20Sopenharmony_ci if needed 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci - TODO: What is it about? 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci- DMA_SLAVE 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci - The device can handle device to memory transfers, including 2098c2ecf20Sopenharmony_ci scatter-gather transfers. 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci - While in the mem2mem case we were having two distinct types to 2128c2ecf20Sopenharmony_ci deal with a single chunk to copy or a collection of them, here, 2138c2ecf20Sopenharmony_ci we just have a single transaction type that is supposed to 2148c2ecf20Sopenharmony_ci handle both. 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci - If you want to transfer a single contiguous memory buffer, 2178c2ecf20Sopenharmony_ci simply build a scatter list with only one item. 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci- DMA_CYCLIC 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci - The device can handle cyclic transfers. 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci - A cyclic transfer is a transfer where the chunk collection will 2248c2ecf20Sopenharmony_ci loop over itself, with the last item pointing to the first. 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci - It's usually used for audio transfers, where you want to operate 2278c2ecf20Sopenharmony_ci on a single ring buffer that you will fill with your audio data. 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci- DMA_INTERLEAVE 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci - The device supports interleaved transfer. 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci - These transfers can transfer data from a non-contiguous buffer 2348c2ecf20Sopenharmony_ci to a non-contiguous buffer, opposed to DMA_SLAVE that can 2358c2ecf20Sopenharmony_ci transfer data from a non-contiguous data set to a continuous 2368c2ecf20Sopenharmony_ci destination buffer. 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci - It's usually used for 2d content transfers, in which case you 2398c2ecf20Sopenharmony_ci want to transfer a portion of uncompressed data directly to the 2408c2ecf20Sopenharmony_ci display to print it 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci- DMA_COMPLETION_NO_ORDER 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci - The device does not support in order completion. 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci - The driver should return DMA_OUT_OF_ORDER for device_tx_status if 2478c2ecf20Sopenharmony_ci the device is setting this capability. 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci - All cookie tracking and checking API should be treated as invalid if 2508c2ecf20Sopenharmony_ci the device exports this capability. 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci - At this point, this is incompatible with polling option for dmatest. 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci - If this cap is set, the user is recommended to provide an unique 2558c2ecf20Sopenharmony_ci identifier for each descriptor sent to the DMA device in order to 2568c2ecf20Sopenharmony_ci properly track the completion. 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci- DMA_REPEAT 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci - The device supports repeated transfers. A repeated transfer, indicated by 2618c2ecf20Sopenharmony_ci the DMA_PREP_REPEAT transfer flag, is similar to a cyclic transfer in that 2628c2ecf20Sopenharmony_ci it gets automatically repeated when it ends, but can additionally be 2638c2ecf20Sopenharmony_ci replaced by the client. 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci - This feature is limited to interleaved transfers, this flag should thus not 2668c2ecf20Sopenharmony_ci be set if the DMA_INTERLEAVE flag isn't set. This limitation is based on 2678c2ecf20Sopenharmony_ci the current needs of DMA clients, support for additional transfer types 2688c2ecf20Sopenharmony_ci should be added in the future if and when the need arises. 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci- DMA_LOAD_EOT 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci - The device supports replacing repeated transfers at end of transfer (EOT) 2738c2ecf20Sopenharmony_ci by queuing a new transfer with the DMA_PREP_LOAD_EOT flag set. 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci - Support for replacing a currently running transfer at another point (such 2768c2ecf20Sopenharmony_ci as end of burst instead of end of transfer) will be added in the future 2778c2ecf20Sopenharmony_ci based on DMA clients needs, if and when the need arises. 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ciThese various types will also affect how the source and destination 2808c2ecf20Sopenharmony_ciaddresses change over time. 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ciAddresses pointing to RAM are typically incremented (or decremented) 2838c2ecf20Sopenharmony_ciafter each transfer. In case of a ring buffer, they may loop 2848c2ecf20Sopenharmony_ci(DMA_CYCLIC). Addresses pointing to a device's register (e.g. a FIFO) 2858c2ecf20Sopenharmony_ciare typically fixed. 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ciPer descriptor metadata support 2888c2ecf20Sopenharmony_ci------------------------------- 2898c2ecf20Sopenharmony_ciSome data movement architecture (DMA controller and peripherals) uses metadata 2908c2ecf20Sopenharmony_ciassociated with a transaction. The DMA controller role is to transfer the 2918c2ecf20Sopenharmony_cipayload and the metadata alongside. 2928c2ecf20Sopenharmony_ciThe metadata itself is not used by the DMA engine itself, but it contains 2938c2ecf20Sopenharmony_ciparameters, keys, vectors, etc for peripheral or from the peripheral. 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ciThe DMAengine framework provides a generic ways to facilitate the metadata for 2968c2ecf20Sopenharmony_cidescriptors. Depending on the architecture the DMA driver can implement either 2978c2ecf20Sopenharmony_cior both of the methods and it is up to the client driver to choose which one 2988c2ecf20Sopenharmony_cito use. 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci- DESC_METADATA_CLIENT 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci The metadata buffer is allocated/provided by the client driver and it is 3038c2ecf20Sopenharmony_ci attached (via the dmaengine_desc_attach_metadata() helper to the descriptor. 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci From the DMA driver the following is expected for this mode: 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci The data from the provided metadata buffer should be prepared for the DMA 3108c2ecf20Sopenharmony_ci controller to be sent alongside of the payload data. Either by copying to a 3118c2ecf20Sopenharmony_ci hardware descriptor, or highly coupled packet. 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci - DMA_DEV_TO_MEM 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci On transfer completion the DMA driver must copy the metadata to the client 3168c2ecf20Sopenharmony_ci provided metadata buffer before notifying the client about the completion. 3178c2ecf20Sopenharmony_ci After the transfer completion, DMA drivers must not touch the metadata 3188c2ecf20Sopenharmony_ci buffer provided by the client. 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci- DESC_METADATA_ENGINE 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci The metadata buffer is allocated/managed by the DMA driver. The client driver 3238c2ecf20Sopenharmony_ci can ask for the pointer, maximum size and the currently used size of the 3248c2ecf20Sopenharmony_ci metadata and can directly update or read it. dmaengine_desc_get_metadata_ptr() 3258c2ecf20Sopenharmony_ci and dmaengine_desc_set_metadata_len() is provided as helper functions. 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci From the DMA driver the following is expected for this mode: 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci - get_metadata_ptr() 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci Should return a pointer for the metadata buffer, the maximum size of the 3328c2ecf20Sopenharmony_ci metadata buffer and the currently used / valid (if any) bytes in the buffer. 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci - set_metadata_len() 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci It is called by the clients after it have placed the metadata to the buffer 3378c2ecf20Sopenharmony_ci to let the DMA driver know the number of valid bytes provided. 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci Note: since the client will ask for the metadata pointer in the completion 3408c2ecf20Sopenharmony_ci callback (in DMA_DEV_TO_MEM case) the DMA driver must ensure that the 3418c2ecf20Sopenharmony_ci descriptor is not freed up prior the callback is called. 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ciDevice operations 3448c2ecf20Sopenharmony_ci----------------- 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ciOur dma_device structure also requires a few function pointers in 3478c2ecf20Sopenharmony_ciorder to implement the actual logic, now that we described what 3488c2ecf20Sopenharmony_cioperations we were able to perform. 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ciThe functions that we have to fill in there, and hence have to 3518c2ecf20Sopenharmony_ciimplement, obviously depend on the transaction types you reported as 3528c2ecf20Sopenharmony_cisupported. 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci- ``device_alloc_chan_resources`` 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci- ``device_free_chan_resources`` 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci - These functions will be called whenever a driver will call 3598c2ecf20Sopenharmony_ci ``dma_request_channel`` or ``dma_release_channel`` for the first/last 3608c2ecf20Sopenharmony_ci time on the channel associated to that driver. 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci - They are in charge of allocating/freeing all the needed 3638c2ecf20Sopenharmony_ci resources in order for that channel to be useful for your driver. 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci - These functions can sleep. 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci- ``device_prep_dma_*`` 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci - These functions are matching the capabilities you registered 3708c2ecf20Sopenharmony_ci previously. 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci - These functions all take the buffer or the scatterlist relevant 3738c2ecf20Sopenharmony_ci for the transfer being prepared, and should create a hardware 3748c2ecf20Sopenharmony_ci descriptor or a list of hardware descriptors from it 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci - These functions can be called from an interrupt context 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci - Any allocation you might do should be using the GFP_NOWAIT 3798c2ecf20Sopenharmony_ci flag, in order not to potentially sleep, but without depleting 3808c2ecf20Sopenharmony_ci the emergency pool either. 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci - Drivers should try to pre-allocate any memory they might need 3838c2ecf20Sopenharmony_ci during the transfer setup at probe time to avoid putting to 3848c2ecf20Sopenharmony_ci much pressure on the nowait allocator. 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci - It should return a unique instance of the 3878c2ecf20Sopenharmony_ci ``dma_async_tx_descriptor structure``, that further represents this 3888c2ecf20Sopenharmony_ci particular transfer. 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci - This structure can be initialized using the function 3918c2ecf20Sopenharmony_ci ``dma_async_tx_descriptor_init``. 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci - You'll also need to set two fields in this structure: 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci - flags: 3968c2ecf20Sopenharmony_ci TODO: Can it be modified by the driver itself, or 3978c2ecf20Sopenharmony_ci should it be always the flags passed in the arguments 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci - tx_submit: A pointer to a function you have to implement, 4008c2ecf20Sopenharmony_ci that is supposed to push the current transaction descriptor to a 4018c2ecf20Sopenharmony_ci pending queue, waiting for issue_pending to be called. 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci - In this structure the function pointer callback_result can be 4048c2ecf20Sopenharmony_ci initialized in order for the submitter to be notified that a 4058c2ecf20Sopenharmony_ci transaction has completed. In the earlier code the function pointer 4068c2ecf20Sopenharmony_ci callback has been used. However it does not provide any status to the 4078c2ecf20Sopenharmony_ci transaction and will be deprecated. The result structure defined as 4088c2ecf20Sopenharmony_ci ``dmaengine_result`` that is passed in to callback_result 4098c2ecf20Sopenharmony_ci has two fields: 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci - result: This provides the transfer result defined by 4128c2ecf20Sopenharmony_ci ``dmaengine_tx_result``. Either success or some error condition. 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci - residue: Provides the residue bytes of the transfer for those that 4158c2ecf20Sopenharmony_ci support residue. 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci- ``device_issue_pending`` 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci - Takes the first transaction descriptor in the pending queue, 4208c2ecf20Sopenharmony_ci and starts the transfer. Whenever that transfer is done, it 4218c2ecf20Sopenharmony_ci should move to the next transaction in the list. 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci - This function can be called in an interrupt context 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci- ``device_tx_status`` 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci - Should report the bytes left to go over on the given channel 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci - Should only care about the transaction descriptor passed as 4308c2ecf20Sopenharmony_ci argument, not the currently active one on a given channel 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci - The tx_state argument might be NULL 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci - Should use dma_set_residue to report it 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci - In the case of a cyclic transfer, it should only take into 4378c2ecf20Sopenharmony_ci account the current period. 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci - Should return DMA_OUT_OF_ORDER if the device does not support in order 4408c2ecf20Sopenharmony_ci completion and is completing the operation out of order. 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci - This function can be called in an interrupt context. 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci- device_config 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci - Reconfigures the channel with the configuration given as argument 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci - This command should NOT perform synchronously, or on any 4498c2ecf20Sopenharmony_ci currently queued transfers, but only on subsequent ones 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci - In this case, the function will receive a ``dma_slave_config`` 4528c2ecf20Sopenharmony_ci structure pointer as an argument, that will detail which 4538c2ecf20Sopenharmony_ci configuration to use. 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci - Even though that structure contains a direction field, this 4568c2ecf20Sopenharmony_ci field is deprecated in favor of the direction argument given to 4578c2ecf20Sopenharmony_ci the prep_* functions 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci - This call is mandatory for slave operations only. This should NOT be 4608c2ecf20Sopenharmony_ci set or expected to be set for memcpy operations. 4618c2ecf20Sopenharmony_ci If a driver support both, it should use this call for slave 4628c2ecf20Sopenharmony_ci operations only and not for memcpy ones. 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci- device_pause 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci - Pauses a transfer on the channel 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci - This command should operate synchronously on the channel, 4698c2ecf20Sopenharmony_ci pausing right away the work of the given channel 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci- device_resume 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci - Resumes a transfer on the channel 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci - This command should operate synchronously on the channel, 4768c2ecf20Sopenharmony_ci resuming right away the work of the given channel 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci- device_terminate_all 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci - Aborts all the pending and ongoing transfers on the channel 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci - For aborted transfers the complete callback should not be called 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci - Can be called from atomic context or from within a complete 4858c2ecf20Sopenharmony_ci callback of a descriptor. Must not sleep. Drivers must be able 4868c2ecf20Sopenharmony_ci to handle this correctly. 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci - Termination may be asynchronous. The driver does not have to 4898c2ecf20Sopenharmony_ci wait until the currently active transfer has completely stopped. 4908c2ecf20Sopenharmony_ci See device_synchronize. 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci- device_synchronize 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci - Must synchronize the termination of a channel to the current 4958c2ecf20Sopenharmony_ci context. 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci - Must make sure that memory for previously submitted 4988c2ecf20Sopenharmony_ci descriptors is no longer accessed by the DMA controller. 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci - Must make sure that all complete callbacks for previously 5018c2ecf20Sopenharmony_ci submitted descriptors have finished running and none are 5028c2ecf20Sopenharmony_ci scheduled to run. 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci - May sleep. 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ciMisc notes 5088c2ecf20Sopenharmony_ci========== 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci(stuff that should be documented, but don't really know 5118c2ecf20Sopenharmony_ciwhere to put them) 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci``dma_run_dependencies`` 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci- Should be called at the end of an async TX transfer, and can be 5168c2ecf20Sopenharmony_ci ignored in the slave transfers case. 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci- Makes sure that dependent operations are run before marking it 5198c2ecf20Sopenharmony_ci as complete. 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_cidma_cookie_t 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci- it's a DMA transaction ID that will increment over time. 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci- Not really relevant any more since the introduction of ``virt-dma`` 5268c2ecf20Sopenharmony_ci that abstracts it away. 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ciDMA_CTRL_ACK 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci- If clear, the descriptor cannot be reused by provider until the 5318c2ecf20Sopenharmony_ci client acknowledges receipt, i.e. has a chance to establish any 5328c2ecf20Sopenharmony_ci dependency chains 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci- This can be acked by invoking async_tx_ack() 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci- If set, does not mean descriptor can be reused 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ciDMA_CTRL_REUSE 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci- If set, the descriptor can be reused after being completed. It should 5418c2ecf20Sopenharmony_ci not be freed by provider if this flag is set. 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci- The descriptor should be prepared for reuse by invoking 5448c2ecf20Sopenharmony_ci ``dmaengine_desc_set_reuse()`` which will set DMA_CTRL_REUSE. 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci- ``dmaengine_desc_set_reuse()`` will succeed only when channel support 5478c2ecf20Sopenharmony_ci reusable descriptor as exhibited by capabilities 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci- As a consequence, if a device driver wants to skip the 5508c2ecf20Sopenharmony_ci ``dma_map_sg()`` and ``dma_unmap_sg()`` in between 2 transfers, 5518c2ecf20Sopenharmony_ci because the DMA'd data wasn't used, it can resubmit the transfer right after 5528c2ecf20Sopenharmony_ci its completion. 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ci- Descriptor can be freed in few ways 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci - Clearing DMA_CTRL_REUSE by invoking 5578c2ecf20Sopenharmony_ci ``dmaengine_desc_clear_reuse()`` and submitting for last txn 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci - Explicitly invoking ``dmaengine_desc_free()``, this can succeed only 5608c2ecf20Sopenharmony_ci when DMA_CTRL_REUSE is already set 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci - Terminating the channel 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci- DMA_PREP_CMD 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci - If set, the client driver tells DMA controller that passed data in DMA 5678c2ecf20Sopenharmony_ci API is command data. 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci - Interpretation of command data is DMA controller specific. It can be 5708c2ecf20Sopenharmony_ci used for issuing commands to other peripherals/register reads/register 5718c2ecf20Sopenharmony_ci writes for which the descriptor should be in different format from 5728c2ecf20Sopenharmony_ci normal data descriptors. 5738c2ecf20Sopenharmony_ci 5748c2ecf20Sopenharmony_ci- DMA_PREP_REPEAT 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ci - If set, the transfer will be automatically repeated when it ends until a 5778c2ecf20Sopenharmony_ci new transfer is queued on the same channel with the DMA_PREP_LOAD_EOT flag. 5788c2ecf20Sopenharmony_ci If the next transfer to be queued on the channel does not have the 5798c2ecf20Sopenharmony_ci DMA_PREP_LOAD_EOT flag set, the current transfer will be repeated until the 5808c2ecf20Sopenharmony_ci client terminates all transfers. 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci - This flag is only supported if the channel reports the DMA_REPEAT 5838c2ecf20Sopenharmony_ci capability. 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_ci- DMA_PREP_LOAD_EOT 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci - If set, the transfer will replace the transfer currently being executed at 5888c2ecf20Sopenharmony_ci the end of the transfer. 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci - This is the default behaviour for non-repeated transfers, specifying 5918c2ecf20Sopenharmony_ci DMA_PREP_LOAD_EOT for non-repeated transfers will thus make no difference. 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ci - When using repeated transfers, DMA clients will usually need to set the 5948c2ecf20Sopenharmony_ci DMA_PREP_LOAD_EOT flag on all transfers, otherwise the channel will keep 5958c2ecf20Sopenharmony_ci repeating the last repeated transfer and ignore the new transfers being 5968c2ecf20Sopenharmony_ci queued. Failure to set DMA_PREP_LOAD_EOT will appear as if the channel was 5978c2ecf20Sopenharmony_ci stuck on the previous transfer. 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci - This flag is only supported if the channel reports the DMA_LOAD_EOT 6008c2ecf20Sopenharmony_ci capability. 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ciGeneral Design Notes 6038c2ecf20Sopenharmony_ci==================== 6048c2ecf20Sopenharmony_ci 6058c2ecf20Sopenharmony_ciMost of the DMAEngine drivers you'll see are based on a similar design 6068c2ecf20Sopenharmony_cithat handles the end of transfer interrupts in the handler, but defer 6078c2ecf20Sopenharmony_cimost work to a tasklet, including the start of a new transfer whenever 6088c2ecf20Sopenharmony_cithe previous transfer ended. 6098c2ecf20Sopenharmony_ci 6108c2ecf20Sopenharmony_ciThis is a rather inefficient design though, because the inter-transfer 6118c2ecf20Sopenharmony_cilatency will be not only the interrupt latency, but also the 6128c2ecf20Sopenharmony_cischeduling latency of the tasklet, which will leave the channel idle 6138c2ecf20Sopenharmony_ciin between, which will slow down the global transfer rate. 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ciYou should avoid this kind of practice, and instead of electing a new 6168c2ecf20Sopenharmony_citransfer in your tasklet, move that part to the interrupt handler in 6178c2ecf20Sopenharmony_ciorder to have a shorter idle window (that we can't really avoid 6188c2ecf20Sopenharmony_cianyway). 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ciGlossary 6218c2ecf20Sopenharmony_ci======== 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci- Burst: A number of consecutive read or write operations that 6248c2ecf20Sopenharmony_ci can be queued to buffers before being flushed to memory. 6258c2ecf20Sopenharmony_ci 6268c2ecf20Sopenharmony_ci- Chunk: A contiguous collection of bursts 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci- Transfer: A collection of chunks (be it contiguous or not) 629