1==================== 2DMA Engine API Guide 3==================== 4 5Vinod Koul <vinod dot koul at intel.com> 6 7.. note:: For DMA Engine usage in async_tx please see: 8 ``Documentation/crypto/async-tx-api.txt`` 9 10 11Below is a guide to device driver writers on how to use the Slave-DMA API of the 12DMA Engine. This is applicable only for slave DMA usage only. 13 14DMA usage 15========= 16 17The slave DMA usage consists of following steps: 18 19- Allocate a DMA slave channel 20 21- Set slave and controller specific parameters 22 23- Get a descriptor for transaction 24 25- Submit the transaction 26 27- Issue pending requests and wait for callback notification 28 29The details of these operations are: 30 311. Allocate a DMA slave channel 32 33 Channel allocation is slightly different in the slave DMA context, 34 client drivers typically need a channel from a particular DMA 35 controller only and even in some cases a specific channel is desired. 36 To request a channel dma_request_chan() API is used. 37 38 Interface: 39 40 .. code-block:: c 41 42 struct dma_chan *dma_request_chan(struct device *dev, const char *name); 43 44 Which will find and return the ``name`` DMA channel associated with the 'dev' 45 device. The association is done via DT, ACPI or board file based 46 dma_slave_map matching table. 47 48 A channel allocated via this interface is exclusive to the caller, 49 until dma_release_channel() is called. 50 512. Set slave and controller specific parameters 52 53 Next step is always to pass some specific information to the DMA 54 driver. Most of the generic information which a slave DMA can use 55 is in struct dma_slave_config. This allows the clients to specify 56 DMA direction, DMA addresses, bus widths, DMA burst lengths etc 57 for the peripheral. 58 59 If some DMA controllers have more parameters to be sent then they 60 should try to embed struct dma_slave_config in their controller 61 specific structure. That gives flexibility to client to pass more 62 parameters, if required. 63 64 Interface: 65 66 .. code-block:: c 67 68 int dmaengine_slave_config(struct dma_chan *chan, 69 struct dma_slave_config *config) 70 71 Please see the dma_slave_config structure definition in dmaengine.h 72 for a detailed explanation of the struct members. Please note 73 that the 'direction' member will be going away as it duplicates the 74 direction given in the prepare call. 75 763. Get a descriptor for transaction 77 78 For slave usage the various modes of slave transfers supported by the 79 DMA-engine are: 80 81 - slave_sg: DMA a list of scatter gather buffers from/to a peripheral 82 83 - dma_cyclic: Perform a cyclic DMA operation from/to a peripheral till the 84 operation is explicitly stopped. 85 86 - interleaved_dma: This is common to Slave as well as M2M clients. For slave 87 address of devices' fifo could be already known to the driver. 88 Various types of operations could be expressed by setting 89 appropriate values to the 'dma_interleaved_template' members. 90 91 A non-NULL return of this transfer API represents a "descriptor" for 92 the given transaction. 93 94 Interface: 95 96 .. code-block:: c 97 98 struct dma_async_tx_descriptor *dmaengine_prep_slave_sg( 99 struct dma_chan *chan, struct scatterlist *sgl, 100 unsigned int sg_len, enum dma_data_direction direction, 101 unsigned long flags); 102 103 struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic( 104 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, 105 size_t period_len, enum dma_data_direction direction); 106 107 struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma( 108 struct dma_chan *chan, struct dma_interleaved_template *xt, 109 unsigned long flags); 110 111 The peripheral driver is expected to have mapped the scatterlist for 112 the DMA operation prior to calling dmaengine_prep_slave_sg(), and must 113 keep the scatterlist mapped until the DMA operation has completed. 114 The scatterlist must be mapped using the DMA struct device. 115 If a mapping needs to be synchronized later, dma_sync_*_for_*() must be 116 called using the DMA struct device, too. 117 So, normal setup should look like this: 118 119 .. code-block:: c 120 121 nr_sg = dma_map_sg(chan->device->dev, sgl, sg_len); 122 if (nr_sg == 0) 123 /* error */ 124 125 desc = dmaengine_prep_slave_sg(chan, sgl, nr_sg, direction, flags); 126 127 Once a descriptor has been obtained, the callback information can be 128 added and the descriptor must then be submitted. Some DMA engine 129 drivers may hold a spinlock between a successful preparation and 130 submission so it is important that these two operations are closely 131 paired. 132 133 .. note:: 134 135 Although the async_tx API specifies that completion callback 136 routines cannot submit any new operations, this is not the 137 case for slave/cyclic DMA. 138 139 For slave DMA, the subsequent transaction may not be available 140 for submission prior to callback function being invoked, so 141 slave DMA callbacks are permitted to prepare and submit a new 142 transaction. 143 144 For cyclic DMA, a callback function may wish to terminate the 145 DMA via dmaengine_terminate_async(). 146 147 Therefore, it is important that DMA engine drivers drop any 148 locks before calling the callback function which may cause a 149 deadlock. 150 151 Note that callbacks will always be invoked from the DMA 152 engines tasklet, never from interrupt context. 153 154 **Optional: per descriptor metadata** 155 156 DMAengine provides two ways for metadata support. 157 158 DESC_METADATA_CLIENT 159 160 The metadata buffer is allocated/provided by the client driver and it is 161 attached to the descriptor. 162 163 .. code-block:: c 164 165 int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc, 166 void *data, size_t len); 167 168 DESC_METADATA_ENGINE 169 170 The metadata buffer is allocated/managed by the DMA driver. The client 171 driver can ask for the pointer, maximum size and the currently used size of 172 the metadata and can directly update or read it. 173 174 Becasue the DMA driver manages the memory area containing the metadata, 175 clients must make sure that they do not try to access or get the pointer 176 after their transfer completion callback has run for the descriptor. 177 If no completion callback has been defined for the transfer, then the 178 metadata must not be accessed after issue_pending. 179 In other words: if the aim is to read back metadata after the transfer is 180 completed, then the client must use completion callback. 181 182 .. code-block:: c 183 184 void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc, 185 size_t *payload_len, size_t *max_len); 186 187 int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc, 188 size_t payload_len); 189 190 Client drivers can query if a given mode is supported with: 191 192 .. code-block:: c 193 194 bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan, 195 enum dma_desc_metadata_mode mode); 196 197 Depending on the used mode client drivers must follow different flow. 198 199 DESC_METADATA_CLIENT 200 201 - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM: 202 203 1. prepare the descriptor (dmaengine_prep_*) 204 construct the metadata in the client's buffer 205 2. use dmaengine_desc_attach_metadata() to attach the buffer to the 206 descriptor 207 3. submit the transfer 208 209 - DMA_DEV_TO_MEM: 210 211 1. prepare the descriptor (dmaengine_prep_*) 212 2. use dmaengine_desc_attach_metadata() to attach the buffer to the 213 descriptor 214 3. submit the transfer 215 4. when the transfer is completed, the metadata should be available in the 216 attached buffer 217 218 DESC_METADATA_ENGINE 219 220 - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM: 221 222 1. prepare the descriptor (dmaengine_prep_*) 223 2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the 224 engine's metadata area 225 3. update the metadata at the pointer 226 4. use dmaengine_desc_set_metadata_len() to tell the DMA engine the 227 amount of data the client has placed into the metadata buffer 228 5. submit the transfer 229 230 - DMA_DEV_TO_MEM: 231 232 1. prepare the descriptor (dmaengine_prep_*) 233 2. submit the transfer 234 3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get 235 the pointer to the engine's metadata area 236 4. read out the metadata from the pointer 237 238 .. note:: 239 240 When DESC_METADATA_ENGINE mode is used the metadata area for the descriptor 241 is no longer valid after the transfer has been completed (valid up to the 242 point when the completion callback returns if used). 243 244 Mixed use of DESC_METADATA_CLIENT / DESC_METADATA_ENGINE is not allowed, 245 client drivers must use either of the modes per descriptor. 246 2474. Submit the transaction 248 249 Once the descriptor has been prepared and the callback information 250 added, it must be placed on the DMA engine drivers pending queue. 251 252 Interface: 253 254 .. code-block:: c 255 256 dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc) 257 258 This returns a cookie can be used to check the progress of DMA engine 259 activity via other DMA engine calls not covered in this document. 260 261 dmaengine_submit() will not start the DMA operation, it merely adds 262 it to the pending queue. For this, see step 5, dma_async_issue_pending. 263 264 .. note:: 265 266 After calling ``dmaengine_submit()`` the submitted transfer descriptor 267 (``struct dma_async_tx_descriptor``) belongs to the DMA engine. 268 Consequently, the client must consider invalid the pointer to that 269 descriptor. 270 2715. Issue pending DMA requests and wait for callback notification 272 273 The transactions in the pending queue can be activated by calling the 274 issue_pending API. If channel is idle then the first transaction in 275 queue is started and subsequent ones queued up. 276 277 On completion of each DMA operation, the next in queue is started and 278 a tasklet triggered. The tasklet will then call the client driver 279 completion callback routine for notification, if set. 280 281 Interface: 282 283 .. code-block:: c 284 285 void dma_async_issue_pending(struct dma_chan *chan); 286 287Further APIs 288------------ 289 2901. Terminate APIs 291 292 .. code-block:: c 293 294 int dmaengine_terminate_sync(struct dma_chan *chan) 295 int dmaengine_terminate_async(struct dma_chan *chan) 296 int dmaengine_terminate_all(struct dma_chan *chan) /* DEPRECATED */ 297 298 This causes all activity for the DMA channel to be stopped, and may 299 discard data in the DMA FIFO which hasn't been fully transferred. 300 No callback functions will be called for any incomplete transfers. 301 302 Two variants of this function are available. 303 304 dmaengine_terminate_async() might not wait until the DMA has been fully 305 stopped or until any running complete callbacks have finished. But it is 306 possible to call dmaengine_terminate_async() from atomic context or from 307 within a complete callback. dmaengine_synchronize() must be called before it 308 is safe to free the memory accessed by the DMA transfer or free resources 309 accessed from within the complete callback. 310 311 dmaengine_terminate_sync() will wait for the transfer and any running 312 complete callbacks to finish before it returns. But the function must not be 313 called from atomic context or from within a complete callback. 314 315 dmaengine_terminate_all() is deprecated and should not be used in new code. 316 3172. Pause API 318 319 .. code-block:: c 320 321 int dmaengine_pause(struct dma_chan *chan) 322 323 This pauses activity on the DMA channel without data loss. 324 3253. Resume API 326 327 .. code-block:: c 328 329 int dmaengine_resume(struct dma_chan *chan) 330 331 Resume a previously paused DMA channel. It is invalid to resume a 332 channel which is not currently paused. 333 3344. Check Txn complete 335 336 .. code-block:: c 337 338 enum dma_status dma_async_is_tx_complete(struct dma_chan *chan, 339 dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used) 340 341 This can be used to check the status of the channel. Please see 342 the documentation in include/linux/dmaengine.h for a more complete 343 description of this API. 344 345 This can be used in conjunction with dma_async_is_complete() and 346 the cookie returned from dmaengine_submit() to check for 347 completion of a specific DMA transaction. 348 349 .. note:: 350 351 Not all DMA engine drivers can return reliable information for 352 a running DMA channel. It is recommended that DMA engine users 353 pause or stop (via dmaengine_terminate_all()) the channel before 354 using this API. 355 3565. Synchronize termination API 357 358 .. code-block:: c 359 360 void dmaengine_synchronize(struct dma_chan *chan) 361 362 Synchronize the termination of the DMA channel to the current context. 363 364 This function should be used after dmaengine_terminate_async() to synchronize 365 the termination of the DMA channel to the current context. The function will 366 wait for the transfer and any running complete callbacks to finish before it 367 returns. 368 369 If dmaengine_terminate_async() is used to stop the DMA channel this function 370 must be called before it is safe to free memory accessed by previously 371 submitted descriptors or to free any resources accessed within the complete 372 callback of previously submitted descriptors. 373 374 The behavior of this function is undefined if dma_async_issue_pending() has 375 been called between dmaengine_terminate_async() and this function. 376