Merge tag 'efi_updates_for_v5.11' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / Documentation / driver-api / dmaengine / client.rst
1 ====================
2 DMA Engine API Guide
3 ====================
4
5 Vinod 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
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 DMA usage only.
13
14 DMA usage
15 =========
16
17 The 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
29 The details of these operations are:
30
31 1. 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
51 2. 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
76 3. 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. Cyclic
90     interleaved DMA transfers are also possible if supported by the channel by
91     setting the DMA_PREP_REPEAT transfer flag.
92
93   A non-NULL return of this transfer API represents a "descriptor" for
94   the given transaction.
95
96   Interface:
97
98   .. code-block:: c
99
100      struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
101                 struct dma_chan *chan, struct scatterlist *sgl,
102                 unsigned int sg_len, enum dma_data_direction direction,
103                 unsigned long flags);
104
105      struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
106                 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
107                 size_t period_len, enum dma_data_direction direction);
108
109      struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
110                 struct dma_chan *chan, struct dma_interleaved_template *xt,
111                 unsigned long flags);
112
113   The peripheral driver is expected to have mapped the scatterlist for
114   the DMA operation prior to calling dmaengine_prep_slave_sg(), and must
115   keep the scatterlist mapped until the DMA operation has completed.
116   The scatterlist must be mapped using the DMA struct device.
117   If a mapping needs to be synchronized later, dma_sync_*_for_*() must be
118   called using the DMA struct device, too.
119   So, normal setup should look like this:
120
121   .. code-block:: c
122
123      struct device *dma_dev = dmaengine_get_dma_device(chan);
124
125      nr_sg = dma_map_sg(dma_dev, sgl, sg_len);
126         if (nr_sg == 0)
127                 /* error */
128
129         desc = dmaengine_prep_slave_sg(chan, sgl, nr_sg, direction, flags);
130
131   Once a descriptor has been obtained, the callback information can be
132   added and the descriptor must then be submitted. Some DMA engine
133   drivers may hold a spinlock between a successful preparation and
134   submission so it is important that these two operations are closely
135   paired.
136
137   .. note::
138
139      Although the async_tx API specifies that completion callback
140      routines cannot submit any new operations, this is not the
141      case for slave/cyclic DMA.
142
143      For slave DMA, the subsequent transaction may not be available
144      for submission prior to callback function being invoked, so
145      slave DMA callbacks are permitted to prepare and submit a new
146      transaction.
147
148      For cyclic DMA, a callback function may wish to terminate the
149      DMA via dmaengine_terminate_async().
150
151      Therefore, it is important that DMA engine drivers drop any
152      locks before calling the callback function which may cause a
153      deadlock.
154
155      Note that callbacks will always be invoked from the DMA
156      engines tasklet, never from interrupt context.
157
158   **Optional: per descriptor metadata**
159
160   DMAengine provides two ways for metadata support.
161
162   DESC_METADATA_CLIENT
163
164     The metadata buffer is allocated/provided by the client driver and it is
165     attached to the descriptor.
166
167   .. code-block:: c
168
169      int dmaengine_desc_attach_metadata(struct dma_async_tx_descriptor *desc,
170                                    void *data, size_t len);
171
172   DESC_METADATA_ENGINE
173
174     The metadata buffer is allocated/managed by the DMA driver. The client
175     driver can ask for the pointer, maximum size and the currently used size of
176     the metadata and can directly update or read it.
177
178     Becasue the DMA driver manages the memory area containing the metadata,
179     clients must make sure that they do not try to access or get the pointer
180     after their transfer completion callback has run for the descriptor.
181     If no completion callback has been defined for the transfer, then the
182     metadata must not be accessed after issue_pending.
183     In other words: if the aim is to read back metadata after the transfer is
184     completed, then the client must use completion callback.
185
186   .. code-block:: c
187
188      void *dmaengine_desc_get_metadata_ptr(struct dma_async_tx_descriptor *desc,
189                 size_t *payload_len, size_t *max_len);
190
191      int dmaengine_desc_set_metadata_len(struct dma_async_tx_descriptor *desc,
192                 size_t payload_len);
193
194   Client drivers can query if a given mode is supported with:
195
196   .. code-block:: c
197
198      bool dmaengine_is_metadata_mode_supported(struct dma_chan *chan,
199                 enum dma_desc_metadata_mode mode);
200
201   Depending on the used mode client drivers must follow different flow.
202
203   DESC_METADATA_CLIENT
204
205     - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
206
207       1. prepare the descriptor (dmaengine_prep_*)
208          construct the metadata in the client's buffer
209       2. use dmaengine_desc_attach_metadata() to attach the buffer to the
210          descriptor
211       3. submit the transfer
212
213     - DMA_DEV_TO_MEM:
214
215       1. prepare the descriptor (dmaengine_prep_*)
216       2. use dmaengine_desc_attach_metadata() to attach the buffer to the
217          descriptor
218       3. submit the transfer
219       4. when the transfer is completed, the metadata should be available in the
220          attached buffer
221
222   DESC_METADATA_ENGINE
223
224     - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM:
225
226       1. prepare the descriptor (dmaengine_prep_*)
227       2. use dmaengine_desc_get_metadata_ptr() to get the pointer to the
228          engine's metadata area
229       3. update the metadata at the pointer
230       4. use dmaengine_desc_set_metadata_len()  to tell the DMA engine the
231          amount of data the client has placed into the metadata buffer
232       5. submit the transfer
233
234     - DMA_DEV_TO_MEM:
235
236       1. prepare the descriptor (dmaengine_prep_*)
237       2. submit the transfer
238       3. on transfer completion, use dmaengine_desc_get_metadata_ptr() to get
239          the pointer to the engine's metadata area
240       4. read out the metadata from the pointer
241
242   .. note::
243
244      When DESC_METADATA_ENGINE mode is used the metadata area for the descriptor
245      is no longer valid after the transfer has been completed (valid up to the
246      point when the completion callback returns if used).
247
248      Mixed use of DESC_METADATA_CLIENT / DESC_METADATA_ENGINE is not allowed,
249      client drivers must use either of the modes per descriptor.
250
251 4. Submit the transaction
252
253    Once the descriptor has been prepared and the callback information
254    added, it must be placed on the DMA engine drivers pending queue.
255
256    Interface:
257
258    .. code-block:: c
259
260       dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
261
262    This returns a cookie can be used to check the progress of DMA engine
263    activity via other DMA engine calls not covered in this document.
264
265    dmaengine_submit() will not start the DMA operation, it merely adds
266    it to the pending queue. For this, see step 5, dma_async_issue_pending.
267
268    .. note::
269
270       After calling ``dmaengine_submit()`` the submitted transfer descriptor
271       (``struct dma_async_tx_descriptor``) belongs to the DMA engine.
272       Consequently, the client must consider invalid the pointer to that
273       descriptor.
274
275 5. Issue pending DMA requests and wait for callback notification
276
277    The transactions in the pending queue can be activated by calling the
278    issue_pending API. If channel is idle then the first transaction in
279    queue is started and subsequent ones queued up.
280
281    On completion of each DMA operation, the next in queue is started and
282    a tasklet triggered. The tasklet will then call the client driver
283    completion callback routine for notification, if set.
284
285    Interface:
286
287    .. code-block:: c
288
289       void dma_async_issue_pending(struct dma_chan *chan);
290
291 Further APIs
292 ------------
293
294 1. Terminate APIs
295
296    .. code-block:: c
297
298       int dmaengine_terminate_sync(struct dma_chan *chan)
299       int dmaengine_terminate_async(struct dma_chan *chan)
300       int dmaengine_terminate_all(struct dma_chan *chan) /* DEPRECATED */
301
302    This causes all activity for the DMA channel to be stopped, and may
303    discard data in the DMA FIFO which hasn't been fully transferred.
304    No callback functions will be called for any incomplete transfers.
305
306    Two variants of this function are available.
307
308    dmaengine_terminate_async() might not wait until the DMA has been fully
309    stopped or until any running complete callbacks have finished. But it is
310    possible to call dmaengine_terminate_async() from atomic context or from
311    within a complete callback. dmaengine_synchronize() must be called before it
312    is safe to free the memory accessed by the DMA transfer or free resources
313    accessed from within the complete callback.
314
315    dmaengine_terminate_sync() will wait for the transfer and any running
316    complete callbacks to finish before it returns. But the function must not be
317    called from atomic context or from within a complete callback.
318
319    dmaengine_terminate_all() is deprecated and should not be used in new code.
320
321 2. Pause API
322
323    .. code-block:: c
324
325       int dmaengine_pause(struct dma_chan *chan)
326
327    This pauses activity on the DMA channel without data loss.
328
329 3. Resume API
330
331    .. code-block:: c
332
333        int dmaengine_resume(struct dma_chan *chan)
334
335    Resume a previously paused DMA channel. It is invalid to resume a
336    channel which is not currently paused.
337
338 4. Check Txn complete
339
340    .. code-block:: c
341
342       enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
343                 dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
344
345    This can be used to check the status of the channel. Please see
346    the documentation in include/linux/dmaengine.h for a more complete
347    description of this API.
348
349    This can be used in conjunction with dma_async_is_complete() and
350    the cookie returned from dmaengine_submit() to check for
351    completion of a specific DMA transaction.
352
353    .. note::
354
355       Not all DMA engine drivers can return reliable information for
356       a running DMA channel. It is recommended that DMA engine users
357       pause or stop (via dmaengine_terminate_all()) the channel before
358       using this API.
359
360 5. Synchronize termination API
361
362    .. code-block:: c
363
364       void dmaengine_synchronize(struct dma_chan *chan)
365
366    Synchronize the termination of the DMA channel to the current context.
367
368    This function should be used after dmaengine_terminate_async() to synchronize
369    the termination of the DMA channel to the current context. The function will
370    wait for the transfer and any running complete callbacks to finish before it
371    returns.
372
373    If dmaengine_terminate_async() is used to stop the DMA channel this function
374    must be called before it is safe to free memory accessed by previously
375    submitted descriptors or to free any resources accessed within the complete
376    callback of previously submitted descriptors.
377
378    The behavior of this function is undefined if dma_async_issue_pending() has
379    been called between dmaengine_terminate_async() and this function.