brintos

brintos / linux-shallow public Read only

0
0
Text · 13.4 KiB · d491e38 Raw
389 lines · plain
1====================2DMA Engine API Guide3====================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.rst``9 10 11Below is a guide to device driver writers on how to use the Slave-DMA API of the12DMA Engine. This is applicable only for slave DMA usage only.13 14DMA usage15=========16 17The slave DMA usage consists of following steps:18 19- Allocate a DMA slave channel20 21- Set slave and controller specific parameters22 23- Get a descriptor for transaction24 25- Submit the transaction26 27- Issue pending requests and wait for callback notification28 29The details of these operations are:30 311. Allocate a DMA slave channel32 33   Channel allocation is slightly different in the slave DMA context,34   client drivers typically need a channel from a particular DMA35   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:: c41 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 based46   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 parameters52 53   Next step is always to pass some specific information to the DMA54   driver. Most of the generic information which a slave DMA can use55   is in struct dma_slave_config. This allows the clients to specify56   DMA direction, DMA addresses, bus widths, DMA burst lengths etc57   for the peripheral.58 59   If some DMA controllers have more parameters to be sent then they60   should try to embed struct dma_slave_config in their controller61   specific structure. That gives flexibility to client to pass more62   parameters, if required.63 64   Interface:65 66   .. code-block:: c67 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.h72   for a detailed explanation of the struct members. Please note73   that the 'direction' member will be going away as it duplicates the74   direction given in the prepare call.75 763. Get a descriptor for transaction77 78  For slave usage the various modes of slave transfers supported by the79  DMA-engine are:80 81  - slave_sg: DMA a list of scatter gather buffers from/to a peripheral82 83  - peripheral_dma_vec: DMA an array of scatter gather buffers from/to a84    peripheral. Similar to slave_sg, but uses an array of dma_vec85    structures instead of a scatterlist.86 87  - dma_cyclic: Perform a cyclic DMA operation from/to a peripheral till the88    operation is explicitly stopped.89 90  - interleaved_dma: This is common to Slave as well as M2M clients. For slave91    address of devices' fifo could be already known to the driver.92    Various types of operations could be expressed by setting93    appropriate values to the 'dma_interleaved_template' members. Cyclic94    interleaved DMA transfers are also possible if supported by the channel by95    setting the DMA_PREP_REPEAT transfer flag.96 97  A non-NULL return of this transfer API represents a "descriptor" for98  the given transaction.99 100  Interface:101 102  .. code-block:: c103 104     struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(105		struct dma_chan *chan, struct scatterlist *sgl,106		unsigned int sg_len, enum dma_data_direction direction,107		unsigned long flags);108 109     struct dma_async_tx_descriptor *dmaengine_prep_peripheral_dma_vec(110		struct dma_chan *chan, const struct dma_vec *vecs,111		size_t nents, enum dma_data_direction direction,112		unsigned long flags);113 114     struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(115		struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,116		size_t period_len, enum dma_data_direction direction);117 118     struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(119		struct dma_chan *chan, struct dma_interleaved_template *xt,120		unsigned long flags);121 122  The peripheral driver is expected to have mapped the scatterlist for123  the DMA operation prior to calling dmaengine_prep_slave_sg(), and must124  keep the scatterlist mapped until the DMA operation has completed.125  The scatterlist must be mapped using the DMA struct device.126  If a mapping needs to be synchronized later, dma_sync_*_for_*() must be127  called using the DMA struct device, too.128  So, normal setup should look like this:129 130  .. code-block:: c131 132     struct device *dma_dev = dmaengine_get_dma_device(chan);133 134     nr_sg = dma_map_sg(dma_dev, sgl, sg_len);135	if (nr_sg == 0)136		/* error */137 138	desc = dmaengine_prep_slave_sg(chan, sgl, nr_sg, direction, flags);139 140  Once a descriptor has been obtained, the callback information can be141  added and the descriptor must then be submitted. Some DMA engine142  drivers may hold a spinlock between a successful preparation and143  submission so it is important that these two operations are closely144  paired.145 146  .. note::147 148     Although the async_tx API specifies that completion callback149     routines cannot submit any new operations, this is not the150     case for slave/cyclic DMA.151 152     For slave DMA, the subsequent transaction may not be available153     for submission prior to callback function being invoked, so154     slave DMA callbacks are permitted to prepare and submit a new155     transaction.156 157     For cyclic DMA, a callback function may wish to terminate the158     DMA via dmaengine_terminate_async().159 160     Therefore, it is important that DMA engine drivers drop any161     locks before calling the callback function which may cause a162     deadlock.163 164     Note that callbacks will always be invoked from the DMA165     engines tasklet, never from interrupt context.166 167  **Optional: per descriptor metadata**168 169  DMAengine provides two ways for metadata support.170 171  DESC_METADATA_CLIENT172 173    The metadata buffer is allocated/provided by the client driver and it is174    attached to the descriptor.175 176  .. code-block:: c177 178     int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,179				   void *data, size_t len);180 181  DESC_METADATA_ENGINE182 183    The metadata buffer is allocated/managed by the DMA driver. The client184    driver can ask for the pointer, maximum size and the currently used size of185    the metadata and can directly update or read it.186 187    Because the DMA driver manages the memory area containing the metadata,188    clients must make sure that they do not try to access or get the pointer189    after their transfer completion callback has run for the descriptor.190    If no completion callback has been defined for the transfer, then the191    metadata must not be accessed after issue_pending.192    In other words: if the aim is to read back metadata after the transfer is193    completed, then the client must use completion callback.194 195  .. code-block:: c196 197     void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,198		size_t *payload_len, size_t *max_len);199 200     int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,201		size_t payload_len);202 203  Client drivers can query if a given mode is supported with:204 205  .. code-block:: c206 207     bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan,208		enum dma_desc_metadata_mode mode);209 210  Depending on the used mode client drivers must follow different flow.211 212  DESC_METADATA_CLIENT213 214    - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:215 216      1. prepare the descriptor (dmaengine_prep_*)217         construct the metadata in the client's buffer218      2. use dmaengine_desc_attach_metadata() to attach the buffer to the219         descriptor220      3. submit the transfer221 222    - DMA_DEV_TO_MEM:223 224      1. prepare the descriptor (dmaengine_prep_*)225      2. use dmaengine_desc_attach_metadata() to attach the buffer to the226         descriptor227      3. submit the transfer228      4. when the transfer is completed, the metadata should be available in the229         attached buffer230 231  DESC_METADATA_ENGINE232 233    - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:234 235      1. prepare the descriptor (dmaengine_prep_*)236      2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the237         engine's metadata area238      3. update the metadata at the pointer239      4. use dmaengine_desc_set_metadata_len()  to tell the DMA engine the240         amount of data the client has placed into the metadata buffer241      5. submit the transfer242 243    - DMA_DEV_TO_MEM:244 245      1. prepare the descriptor (dmaengine_prep_*)246      2. submit the transfer247      3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get248         the pointer to the engine's metadata area249      4. read out the metadata from the pointer250 251  .. note::252 253     When DESC_METADATA_ENGINE mode is used the metadata area for the descriptor254     is no longer valid after the transfer has been completed (valid up to the255     point when the completion callback returns if used).256 257     Mixed use of DESC_METADATA_CLIENT / DESC_METADATA_ENGINE is not allowed,258     client drivers must use either of the modes per descriptor.259 2604. Submit the transaction261 262   Once the descriptor has been prepared and the callback information263   added, it must be placed on the DMA engine drivers pending queue.264 265   Interface:266 267   .. code-block:: c268 269      dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)270 271   This returns a cookie can be used to check the progress of DMA engine272   activity via other DMA engine calls not covered in this document.273 274   dmaengine_submit() will not start the DMA operation, it merely adds275   it to the pending queue. For this, see step 5, dma_async_issue_pending.276 277   .. note::278 279      After calling ``dmaengine_submit()`` the submitted transfer descriptor280      (``struct dma_async_tx_descriptor``) belongs to the DMA engine.281      Consequently, the client must consider invalid the pointer to that282      descriptor.283 2845. Issue pending DMA requests and wait for callback notification285 286   The transactions in the pending queue can be activated by calling the287   issue_pending API. If channel is idle then the first transaction in288   queue is started and subsequent ones queued up.289 290   On completion of each DMA operation, the next in queue is started and291   a tasklet triggered. The tasklet will then call the client driver292   completion callback routine for notification, if set.293 294   Interface:295 296   .. code-block:: c297 298      void dma_async_issue_pending(struct dma_chan *chan);299 300Further APIs301------------302 3031. Terminate APIs304 305   .. code-block:: c306 307      int dmaengine_terminate_sync(struct dma_chan *chan)308      int dmaengine_terminate_async(struct dma_chan *chan)309      int dmaengine_terminate_all(struct dma_chan *chan) /* DEPRECATED */310 311   This causes all activity for the DMA channel to be stopped, and may312   discard data in the DMA FIFO which hasn't been fully transferred.313   No callback functions will be called for any incomplete transfers.314 315   Two variants of this function are available.316 317   dmaengine_terminate_async() might not wait until the DMA has been fully318   stopped or until any running complete callbacks have finished. But it is319   possible to call dmaengine_terminate_async() from atomic context or from320   within a complete callback. dmaengine_synchronize() must be called before it321   is safe to free the memory accessed by the DMA transfer or free resources322   accessed from within the complete callback.323 324   dmaengine_terminate_sync() will wait for the transfer and any running325   complete callbacks to finish before it returns. But the function must not be326   called from atomic context or from within a complete callback.327 328   dmaengine_terminate_all() is deprecated and should not be used in new code.329 3302. Pause API331 332   .. code-block:: c333 334      int dmaengine_pause(struct dma_chan *chan)335 336   This pauses activity on the DMA channel without data loss.337 3383. Resume API339 340   .. code-block:: c341 342       int dmaengine_resume(struct dma_chan *chan)343 344   Resume a previously paused DMA channel. It is invalid to resume a345   channel which is not currently paused.346 3474. Check Txn complete348 349   .. code-block:: c350 351      enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,352		dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)353 354   This can be used to check the status of the channel. Please see355   the documentation in include/linux/dmaengine.h for a more complete356   description of this API.357 358   This can be used in conjunction with dma_async_is_complete() and359   the cookie returned from dmaengine_submit() to check for360   completion of a specific DMA transaction.361 362   .. note::363 364      Not all DMA engine drivers can return reliable information for365      a running DMA channel. It is recommended that DMA engine users366      pause or stop (via dmaengine_terminate_all()) the channel before367      using this API.368 3695. Synchronize termination API370 371   .. code-block:: c372 373      void dmaengine_synchronize(struct dma_chan *chan)374 375   Synchronize the termination of the DMA channel to the current context.376 377   This function should be used after dmaengine_terminate_async() to synchronize378   the termination of the DMA channel to the current context. The function will379   wait for the transfer and any running complete callbacks to finish before it380   returns.381 382   If dmaengine_terminate_async() is used to stop the DMA channel this function383   must be called before it is safe to free memory accessed by previously384   submitted descriptors or to free any resources accessed within the complete385   callback of previously submitted descriptors.386 387   The behavior of this function is undefined if dma_async_issue_pending() has388   been called between dmaengine_terminate_async() and this function.389