0001 ====================
0002 DMA Engine API Guide
0003 ====================
0004
0005 Vinod Koul <vinod dot koul at intel.com>
0006
0007 .. note:: For DMA Engine usage in async_tx please see:
0008 ``Documentation/crypto/async-tx-api.rst``
0009
0010
0011 Below is a guide to device driver writers on how to use the Slave-DMA API of the
0012 DMA Engine. This is applicable only for slave DMA usage only.
0013
0014 DMA usage
0015 =========
0016
0017 The slave DMA usage consists of following steps:
0018
0019 - Allocate a DMA slave channel
0020
0021 - Set slave and controller specific parameters
0022
0023 - Get a descriptor for transaction
0024
0025 - Submit the transaction
0026
0027 - Issue pending requests and wait for callback notification
0028
0029 The details of these operations are:
0030
0031 1. Allocate a DMA slave channel
0032
0033 Channel allocation is slightly different in the slave DMA context,
0034 client drivers typically need a channel from a particular DMA
0035 controller only and even in some cases a specific channel is desired.
0036 To request a channel dma_request_chan() API is used.
0037
0038 Interface:
0039
0040 .. code-block:: c
0041
0042 struct dma_chan *dma_request_chan(struct device *dev, const char *name);
0043
0044 Which will find and return the ``name`` DMA channel associated with the 'dev'
0045 device. The association is done via DT, ACPI or board file based
0046 dma_slave_map matching table.
0047
0048 A channel allocated via this interface is exclusive to the caller,
0049 until dma_release_channel() is called.
0050
0051 2. Set slave and controller specific parameters
0052
0053 Next step is always to pass some specific information to the DMA
0054 driver. Most of the generic information which a slave DMA can use
0055 is in struct dma_slave_config. This allows the clients to specify
0056 DMA direction, DMA addresses, bus widths, DMA burst lengths etc
0057 for the peripheral.
0058
0059 If some DMA controllers have more parameters to be sent then they
0060 should try to embed struct dma_slave_config in their controller
0061 specific structure. That gives flexibility to client to pass more
0062 parameters, if required.
0063
0064 Interface:
0065
0066 .. code-block:: c
0067
0068 int dmaengine_slave_config(struct dma_chan *chan,
0069 struct dma_slave_config *config)
0070
0071 Please see the dma_slave_config structure definition in dmaengine.h
0072 for a detailed explanation of the struct members. Please note
0073 that the 'direction' member will be going away as it duplicates the
0074 direction given in the prepare call.
0075
0076 3. Get a descriptor for transaction
0077
0078 For slave usage the various modes of slave transfers supported by the
0079 DMA-engine are:
0080
0081 - slave_sg: DMA a list of scatter gather buffers from/to a peripheral
0082
0083 - dma_cyclic: Perform a cyclic DMA operation from/to a peripheral till the
0084 operation is explicitly stopped.
0085
0086 - interleaved_dma: This is common to Slave as well as M2M clients. For slave
0087 address of devices' fifo could be already known to the driver.
0088 Various types of operations could be expressed by setting
0089 appropriate values to the 'dma_interleaved_template' members. Cyclic
0090 interleaved DMA transfers are also possible if supported by the channel by
0091 setting the DMA_PREP_REPEAT transfer flag.
0092
0093 A non-NULL return of this transfer API represents a "descriptor" for
0094 the given transaction.
0095
0096 Interface:
0097
0098 .. code-block:: c
0099
0100 struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
0101 struct dma_chan *chan, struct scatterlist *sgl,
0102 unsigned int sg_len, enum dma_data_direction direction,
0103 unsigned long flags);
0104
0105 struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
0106 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
0107 size_t period_len, enum dma_data_direction direction);
0108
0109 struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
0110 struct dma_chan *chan, struct dma_interleaved_template *xt,
0111 unsigned long flags);
0112
0113 The peripheral driver is expected to have mapped the scatterlist for
0114 the DMA operation prior to calling dmaengine_prep_slave_sg(), and must
0115 keep the scatterlist mapped until the DMA operation has completed.
0116 The scatterlist must be mapped using the DMA struct device.
0117 If a mapping needs to be synchronized later, dma_sync_*_for_*() must be
0118 called using the DMA struct device, too.
0119 So, normal setup should look like this:
0120
0121 .. code-block:: c
0122
0123 struct device *dma_dev = dmaengine_get_dma_device(chan);
0124
0125 nr_sg = dma_map_sg(dma_dev, sgl, sg_len);
0126 if (nr_sg == 0)
0127 /* error */
0128
0129 desc = dmaengine_prep_slave_sg(chan, sgl, nr_sg, direction, flags);
0130
0131 Once a descriptor has been obtained, the callback information can be
0132 added and the descriptor must then be submitted. Some DMA engine
0133 drivers may hold a spinlock between a successful preparation and
0134 submission so it is important that these two operations are closely
0135 paired.
0136
0137 .. note::
0138
0139 Although the async_tx API specifies that completion callback
0140 routines cannot submit any new operations, this is not the
0141 case for slave/cyclic DMA.
0142
0143 For slave DMA, the subsequent transaction may not be available
0144 for submission prior to callback function being invoked, so
0145 slave DMA callbacks are permitted to prepare and submit a new
0146 transaction.
0147
0148 For cyclic DMA, a callback function may wish to terminate the
0149 DMA via dmaengine_terminate_async().
0150
0151 Therefore, it is important that DMA engine drivers drop any
0152 locks before calling the callback function which may cause a
0153 deadlock.
0154
0155 Note that callbacks will always be invoked from the DMA
0156 engines tasklet, never from interrupt context.
0157
0158 **Optional: per descriptor metadata**
0159
0160 DMAengine provides two ways for metadata support.
0161
0162 DESC_METADATA_CLIENT
0163
0164 The metadata buffer is allocated/provided by the client driver and it is
0165 attached to the descriptor.
0166
0167 .. code-block:: c
0168
0169 int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
0170 void *data, size_t len);
0171
0172 DESC_METADATA_ENGINE
0173
0174 The metadata buffer is allocated/managed by the DMA driver. The client
0175 driver can ask for the pointer, maximum size and the currently used size of
0176 the metadata and can directly update or read it.
0177
0178 Becasue the DMA driver manages the memory area containing the metadata,
0179 clients must make sure that they do not try to access or get the pointer
0180 after their transfer completion callback has run for the descriptor.
0181 If no completion callback has been defined for the transfer, then the
0182 metadata must not be accessed after issue_pending.
0183 In other words: if the aim is to read back metadata after the transfer is
0184 completed, then the client must use completion callback.
0185
0186 .. code-block:: c
0187
0188 void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
0189 size_t *payload_len, size_t *max_len);
0190
0191 int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
0192 size_t payload_len);
0193
0194 Client drivers can query if a given mode is supported with:
0195
0196 .. code-block:: c
0197
0198 bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan,
0199 enum dma_desc_metadata_mode mode);
0200
0201 Depending on the used mode client drivers must follow different flow.
0202
0203 DESC_METADATA_CLIENT
0204
0205 - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
0206
0207 1. prepare the descriptor (dmaengine_prep_*)
0208 construct the metadata in the client's buffer
0209 2. use dmaengine_desc_attach_metadata() to attach the buffer to the
0210 descriptor
0211 3. submit the transfer
0212
0213 - DMA_DEV_TO_MEM:
0214
0215 1. prepare the descriptor (dmaengine_prep_*)
0216 2. use dmaengine_desc_attach_metadata() to attach the buffer to the
0217 descriptor
0218 3. submit the transfer
0219 4. when the transfer is completed, the metadata should be available in the
0220 attached buffer
0221
0222 DESC_METADATA_ENGINE
0223
0224 - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
0225
0226 1. prepare the descriptor (dmaengine_prep_*)
0227 2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the
0228 engine's metadata area
0229 3. update the metadata at the pointer
0230 4. use dmaengine_desc_set_metadata_len() to tell the DMA engine the
0231 amount of data the client has placed into the metadata buffer
0232 5. submit the transfer
0233
0234 - DMA_DEV_TO_MEM:
0235
0236 1. prepare the descriptor (dmaengine_prep_*)
0237 2. submit the transfer
0238 3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get
0239 the pointer to the engine's metadata area
0240 4. read out the metadata from the pointer
0241
0242 .. note::
0243
0244 When DESC_METADATA_ENGINE mode is used the metadata area for the descriptor
0245 is no longer valid after the transfer has been completed (valid up to the
0246 point when the completion callback returns if used).
0247
0248 Mixed use of DESC_METADATA_CLIENT / DESC_METADATA_ENGINE is not allowed,
0249 client drivers must use either of the modes per descriptor.
0250
0251 4. Submit the transaction
0252
0253 Once the descriptor has been prepared and the callback information
0254 added, it must be placed on the DMA engine drivers pending queue.
0255
0256 Interface:
0257
0258 .. code-block:: c
0259
0260 dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
0261
0262 This returns a cookie can be used to check the progress of DMA engine
0263 activity via other DMA engine calls not covered in this document.
0264
0265 dmaengine_submit() will not start the DMA operation, it merely adds
0266 it to the pending queue. For this, see step 5, dma_async_issue_pending.
0267
0268 .. note::
0269
0270 After calling ``dmaengine_submit()`` the submitted transfer descriptor
0271 (``struct dma_async_tx_descriptor``) belongs to the DMA engine.
0272 Consequently, the client must consider invalid the pointer to that
0273 descriptor.
0274
0275 5. Issue pending DMA requests and wait for callback notification
0276
0277 The transactions in the pending queue can be activated by calling the
0278 issue_pending API. If channel is idle then the first transaction in
0279 queue is started and subsequent ones queued up.
0280
0281 On completion of each DMA operation, the next in queue is started and
0282 a tasklet triggered. The tasklet will then call the client driver
0283 completion callback routine for notification, if set.
0284
0285 Interface:
0286
0287 .. code-block:: c
0288
0289 void dma_async_issue_pending(struct dma_chan *chan);
0290
0291 Further APIs
0292 ------------
0293
0294 1. Terminate APIs
0295
0296 .. code-block:: c
0297
0298 int dmaengine_terminate_sync(struct dma_chan *chan)
0299 int dmaengine_terminate_async(struct dma_chan *chan)
0300 int dmaengine_terminate_all(struct dma_chan *chan) /* DEPRECATED */
0301
0302 This causes all activity for the DMA channel to be stopped, and may
0303 discard data in the DMA FIFO which hasn't been fully transferred.
0304 No callback functions will be called for any incomplete transfers.
0305
0306 Two variants of this function are available.
0307
0308 dmaengine_terminate_async() might not wait until the DMA has been fully
0309 stopped or until any running complete callbacks have finished. But it is
0310 possible to call dmaengine_terminate_async() from atomic context or from
0311 within a complete callback. dmaengine_synchronize() must be called before it
0312 is safe to free the memory accessed by the DMA transfer or free resources
0313 accessed from within the complete callback.
0314
0315 dmaengine_terminate_sync() will wait for the transfer and any running
0316 complete callbacks to finish before it returns. But the function must not be
0317 called from atomic context or from within a complete callback.
0318
0319 dmaengine_terminate_all() is deprecated and should not be used in new code.
0320
0321 2. Pause API
0322
0323 .. code-block:: c
0324
0325 int dmaengine_pause(struct dma_chan *chan)
0326
0327 This pauses activity on the DMA channel without data loss.
0328
0329 3. Resume API
0330
0331 .. code-block:: c
0332
0333 int dmaengine_resume(struct dma_chan *chan)
0334
0335 Resume a previously paused DMA channel. It is invalid to resume a
0336 channel which is not currently paused.
0337
0338 4. Check Txn complete
0339
0340 .. code-block:: c
0341
0342 enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
0343 dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
0344
0345 This can be used to check the status of the channel. Please see
0346 the documentation in include/linux/dmaengine.h for a more complete
0347 description of this API.
0348
0349 This can be used in conjunction with dma_async_is_complete() and
0350 the cookie returned from dmaengine_submit() to check for
0351 completion of a specific DMA transaction.
0352
0353 .. note::
0354
0355 Not all DMA engine drivers can return reliable information for
0356 a running DMA channel. It is recommended that DMA engine users
0357 pause or stop (via dmaengine_terminate_all()) the channel before
0358 using this API.
0359
0360 5. Synchronize termination API
0361
0362 .. code-block:: c
0363
0364 void dmaengine_synchronize(struct dma_chan *chan)
0365
0366 Synchronize the termination of the DMA channel to the current context.
0367
0368 This function should be used after dmaengine_terminate_async() to synchronize
0369 the termination of the DMA channel to the current context. The function will
0370 wait for the transfer and any running complete callbacks to finish before it
0371 returns.
0372
0373 If dmaengine_terminate_async() is used to stop the DMA channel this function
0374 must be called before it is safe to free memory accessed by previously
0375 submitted descriptors or to free any resources accessed within the complete
0376 callback of previously submitted descriptors.
0377
0378 The behavior of this function is undefined if dma_async_issue_pending() has
0379 been called between dmaengine_terminate_async() and this function.