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