162306a36Sopenharmony_ci==================================
262306a36Sopenharmony_ciDMAengine controller documentation
362306a36Sopenharmony_ci==================================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciHardware Introduction
662306a36Sopenharmony_ci=====================
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciMost of the Slave DMA controllers have the same general principles of
962306a36Sopenharmony_cioperations.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciThey have a given number of channels to use for the DMA transfers, and
1262306a36Sopenharmony_cia given number of requests lines.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciRequests and channels are pretty much orthogonal. Channels can be used
1562306a36Sopenharmony_cito serve several to any requests. To simplify, channels are the
1662306a36Sopenharmony_cientities that will be doing the copy, and requests what endpoints are
1762306a36Sopenharmony_ciinvolved.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciThe request lines actually correspond to physical lines going from the
2062306a36Sopenharmony_ciDMA-eligible devices to the controller itself. Whenever the device
2162306a36Sopenharmony_ciwill want to start a transfer, it will assert a DMA request (DRQ) by
2262306a36Sopenharmony_ciasserting that request line.
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciA very simple DMA controller would only take into account a single
2562306a36Sopenharmony_ciparameter: the transfer size. At each clock cycle, it would transfer a
2662306a36Sopenharmony_cibyte of data from one buffer to another, until the transfer size has
2762306a36Sopenharmony_cibeen reached.
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciThat wouldn't work well in the real world, since slave devices might
3062306a36Sopenharmony_cirequire a specific number of bits to be transferred in a single
3162306a36Sopenharmony_cicycle. For example, we may want to transfer as much data as the
3262306a36Sopenharmony_ciphysical bus allows to maximize performances when doing a simple
3362306a36Sopenharmony_cimemory copy operation, but our audio device could have a narrower FIFO
3462306a36Sopenharmony_cithat requires data to be written exactly 16 or 24 bits at a time. This
3562306a36Sopenharmony_ciis why most if not all of the DMA controllers can adjust this, using a
3662306a36Sopenharmony_ciparameter called the transfer width.
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciMoreover, some DMA controllers, whenever the RAM is used as a source
3962306a36Sopenharmony_cior destination, can group the reads or writes in memory into a buffer,
4062306a36Sopenharmony_ciso instead of having a lot of small memory accesses, which is not
4162306a36Sopenharmony_cireally efficient, you'll get several bigger transfers. This is done
4262306a36Sopenharmony_ciusing a parameter called the burst size, that defines how many single
4362306a36Sopenharmony_cireads/writes it's allowed to do without the controller splitting the
4462306a36Sopenharmony_citransfer into smaller sub-transfers.
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ciOur theoretical DMA controller would then only be able to do transfers
4762306a36Sopenharmony_cithat involve a single contiguous block of data. However, some of the
4862306a36Sopenharmony_citransfers we usually have are not, and want to copy data from
4962306a36Sopenharmony_cinon-contiguous buffers to a contiguous buffer, which is called
5062306a36Sopenharmony_ciscatter-gather.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciDMAEngine, at least for mem2dev transfers, require support for
5362306a36Sopenharmony_ciscatter-gather. So we're left with two cases here: either we have a
5462306a36Sopenharmony_ciquite simple DMA controller that doesn't support it, and we'll have to
5562306a36Sopenharmony_ciimplement it in software, or we have a more advanced DMA controller,
5662306a36Sopenharmony_cithat implements in hardware scatter-gather.
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciThe latter are usually programmed using a collection of chunks to
5962306a36Sopenharmony_citransfer, and whenever the transfer is started, the controller will go
6062306a36Sopenharmony_ciover that collection, doing whatever we programmed there.
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciThis collection is usually either a table or a linked list. You will
6362306a36Sopenharmony_cithen push either the address of the table and its number of elements,
6462306a36Sopenharmony_cior the first item of the list to one channel of the DMA controller,
6562306a36Sopenharmony_ciand whenever a DRQ will be asserted, it will go through the collection
6662306a36Sopenharmony_cito know where to fetch the data from.
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciEither way, the format of this collection is completely dependent on
6962306a36Sopenharmony_ciyour hardware. Each DMA controller will require a different structure,
7062306a36Sopenharmony_cibut all of them will require, for every chunk, at least the source and
7162306a36Sopenharmony_cidestination addresses, whether it should increment these addresses or
7262306a36Sopenharmony_cinot and the three parameters we saw earlier: the burst size, the
7362306a36Sopenharmony_citransfer width and the transfer size.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciThe one last thing is that usually, slave devices won't issue DRQ by
7662306a36Sopenharmony_cidefault, and you have to enable this in your slave device driver first
7762306a36Sopenharmony_ciwhenever you're willing to use DMA.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciThese were just the general memory-to-memory (also called mem2mem) or
8062306a36Sopenharmony_cimemory-to-device (mem2dev) kind of transfers. Most devices often
8162306a36Sopenharmony_cisupport other kind of transfers or memory operations that dmaengine
8262306a36Sopenharmony_cisupport and will be detailed later in this document.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciDMA Support in Linux
8562306a36Sopenharmony_ci====================
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciHistorically, DMA controller drivers have been implemented using the
8862306a36Sopenharmony_ciasync TX API, to offload operations such as memory copy, XOR,
8962306a36Sopenharmony_cicryptography, etc., basically any memory to memory operation.
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ciOver time, the need for memory to device transfers arose, and
9262306a36Sopenharmony_cidmaengine was extended. Nowadays, the async TX API is written as a
9362306a36Sopenharmony_cilayer on top of dmaengine, and acts as a client. Still, dmaengine
9462306a36Sopenharmony_ciaccommodates that API in some cases, and made some design choices to
9562306a36Sopenharmony_ciensure that it stayed compatible.
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciFor more information on the Async TX API, please look the relevant
9862306a36Sopenharmony_cidocumentation file in Documentation/crypto/async-tx-api.rst.
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciDMAEngine APIs
10162306a36Sopenharmony_ci==============
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci``struct dma_device`` Initialization
10462306a36Sopenharmony_ci------------------------------------
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciJust like any other kernel framework, the whole DMAEngine registration
10762306a36Sopenharmony_cirelies on the driver filling a structure and registering against the
10862306a36Sopenharmony_ciframework. In our case, that structure is dma_device.
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciThe first thing you need to do in your driver is to allocate this
11162306a36Sopenharmony_cistructure. Any of the usual memory allocators will do, but you'll also
11262306a36Sopenharmony_cineed to initialize a few fields in there:
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci- ``channels``: should be initialized as a list using the
11562306a36Sopenharmony_ci  INIT_LIST_HEAD macro for example
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci- ``src_addr_widths``:
11862306a36Sopenharmony_ci  should contain a bitmask of the supported source transfer width
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci- ``dst_addr_widths``:
12162306a36Sopenharmony_ci  should contain a bitmask of the supported destination transfer width
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci- ``directions``:
12462306a36Sopenharmony_ci  should contain a bitmask of the supported slave directions
12562306a36Sopenharmony_ci  (i.e. excluding mem2mem transfers)
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci- ``residue_granularity``:
12862306a36Sopenharmony_ci  granularity of the transfer residue reported to dma_set_residue.
12962306a36Sopenharmony_ci  This can be either:
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci  - Descriptor:
13262306a36Sopenharmony_ci    your device doesn't support any kind of residue
13362306a36Sopenharmony_ci    reporting. The framework will only know that a particular
13462306a36Sopenharmony_ci    transaction descriptor is done.
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci  - Segment:
13762306a36Sopenharmony_ci    your device is able to report which chunks have been transferred
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci  - Burst:
14062306a36Sopenharmony_ci    your device is able to report which burst have been transferred
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci- ``dev``: should hold the pointer to the ``struct device`` associated
14362306a36Sopenharmony_ci  to your current driver instance.
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ciSupported transaction types
14662306a36Sopenharmony_ci---------------------------
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ciThe next thing you need is to set which transaction types your device
14962306a36Sopenharmony_ci(and driver) supports.
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciOur ``dma_device structure`` has a field called cap_mask that holds the
15262306a36Sopenharmony_civarious types of transaction supported, and you need to modify this
15362306a36Sopenharmony_cimask using the dma_cap_set function, with various flags depending on
15462306a36Sopenharmony_citransaction types you support as an argument.
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciAll those capabilities are defined in the ``dma_transaction_type enum``,
15762306a36Sopenharmony_ciin ``include/linux/dmaengine.h``
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciCurrently, the types available are:
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci- DMA_MEMCPY
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci  - The device is able to do memory to memory copies
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci  - No matter what the overall size of the combined chunks for source and
16662306a36Sopenharmony_ci    destination is, only as many bytes as the smallest of the two will be
16762306a36Sopenharmony_ci    transmitted. That means the number and size of the scatter-gather buffers in
16862306a36Sopenharmony_ci    both lists need not be the same, and that the operation functionally is
16962306a36Sopenharmony_ci    equivalent to a ``strncpy`` where the ``count`` argument equals the smallest
17062306a36Sopenharmony_ci    total size of the two scatter-gather list buffers.
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci  - It's usually used for copying pixel data between host memory and
17362306a36Sopenharmony_ci    memory-mapped GPU device memory, such as found on modern PCI video graphics
17462306a36Sopenharmony_ci    cards. The most immediate example is the OpenGL API function
17562306a36Sopenharmony_ci    ``glReadPielx()``, which might require a verbatim copy of a huge framebuffer
17662306a36Sopenharmony_ci    from local device memory onto host memory.
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci- DMA_XOR
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci  - The device is able to perform XOR operations on memory areas
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci  - Used to accelerate XOR intensive tasks, such as RAID5
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci- DMA_XOR_VAL
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci  - The device is able to perform parity check using the XOR
18762306a36Sopenharmony_ci    algorithm against a memory buffer.
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci- DMA_PQ
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci  - The device is able to perform RAID6 P+Q computations, P being a
19262306a36Sopenharmony_ci    simple XOR, and Q being a Reed-Solomon algorithm.
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci- DMA_PQ_VAL
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci  - The device is able to perform parity check using RAID6 P+Q
19762306a36Sopenharmony_ci    algorithm against a memory buffer.
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci- DMA_MEMSET
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci  - The device is able to fill memory with the provided pattern
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci  - The pattern is treated as a single byte signed value.
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci- DMA_INTERRUPT
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci  - The device is able to trigger a dummy transfer that will
20862306a36Sopenharmony_ci    generate periodic interrupts
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci  - Used by the client drivers to register a callback that will be
21162306a36Sopenharmony_ci    called on a regular basis through the DMA controller interrupt
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci- DMA_PRIVATE
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci  - The devices only supports slave transfers, and as such isn't
21662306a36Sopenharmony_ci    available for async transfers.
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci- DMA_ASYNC_TX
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci  - Must not be set by the device, and will be set by the framework
22162306a36Sopenharmony_ci    if needed
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci  - TODO: What is it about?
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci- DMA_SLAVE
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci  - The device can handle device to memory transfers, including
22862306a36Sopenharmony_ci    scatter-gather transfers.
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci  - While in the mem2mem case we were having two distinct types to
23162306a36Sopenharmony_ci    deal with a single chunk to copy or a collection of them, here,
23262306a36Sopenharmony_ci    we just have a single transaction type that is supposed to
23362306a36Sopenharmony_ci    handle both.
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci  - If you want to transfer a single contiguous memory buffer,
23662306a36Sopenharmony_ci    simply build a scatter list with only one item.
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci- DMA_CYCLIC
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci  - The device can handle cyclic transfers.
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci  - A cyclic transfer is a transfer where the chunk collection will
24362306a36Sopenharmony_ci    loop over itself, with the last item pointing to the first.
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci  - It's usually used for audio transfers, where you want to operate
24662306a36Sopenharmony_ci    on a single ring buffer that you will fill with your audio data.
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci- DMA_INTERLEAVE
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci  - The device supports interleaved transfer.
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci  - These transfers can transfer data from a non-contiguous buffer
25362306a36Sopenharmony_ci    to a non-contiguous buffer, opposed to DMA_SLAVE that can
25462306a36Sopenharmony_ci    transfer data from a non-contiguous data set to a continuous
25562306a36Sopenharmony_ci    destination buffer.
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci  - It's usually used for 2d content transfers, in which case you
25862306a36Sopenharmony_ci    want to transfer a portion of uncompressed data directly to the
25962306a36Sopenharmony_ci    display to print it
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci- DMA_COMPLETION_NO_ORDER
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci  - The device does not support in order completion.
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci  - The driver should return DMA_OUT_OF_ORDER for device_tx_status if
26662306a36Sopenharmony_ci    the device is setting this capability.
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci  - All cookie tracking and checking API should be treated as invalid if
26962306a36Sopenharmony_ci    the device exports this capability.
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci  - At this point, this is incompatible with polling option for dmatest.
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci  - If this cap is set, the user is recommended to provide an unique
27462306a36Sopenharmony_ci    identifier for each descriptor sent to the DMA device in order to
27562306a36Sopenharmony_ci    properly track the completion.
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci- DMA_REPEAT
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci  - The device supports repeated transfers. A repeated transfer, indicated by
28062306a36Sopenharmony_ci    the DMA_PREP_REPEAT transfer flag, is similar to a cyclic transfer in that
28162306a36Sopenharmony_ci    it gets automatically repeated when it ends, but can additionally be
28262306a36Sopenharmony_ci    replaced by the client.
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci  - This feature is limited to interleaved transfers, this flag should thus not
28562306a36Sopenharmony_ci    be set if the DMA_INTERLEAVE flag isn't set. This limitation is based on
28662306a36Sopenharmony_ci    the current needs of DMA clients, support for additional transfer types
28762306a36Sopenharmony_ci    should be added in the future if and when the need arises.
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci- DMA_LOAD_EOT
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci  - The device supports replacing repeated transfers at end of transfer (EOT)
29262306a36Sopenharmony_ci    by queuing a new transfer with the DMA_PREP_LOAD_EOT flag set.
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci  - Support for replacing a currently running transfer at another point (such
29562306a36Sopenharmony_ci    as end of burst instead of end of transfer) will be added in the future
29662306a36Sopenharmony_ci    based on DMA clients needs, if and when the need arises.
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ciThese various types will also affect how the source and destination
29962306a36Sopenharmony_ciaddresses change over time.
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ciAddresses pointing to RAM are typically incremented (or decremented)
30262306a36Sopenharmony_ciafter each transfer. In case of a ring buffer, they may loop
30362306a36Sopenharmony_ci(DMA_CYCLIC). Addresses pointing to a device's register (e.g. a FIFO)
30462306a36Sopenharmony_ciare typically fixed.
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ciPer descriptor metadata support
30762306a36Sopenharmony_ci-------------------------------
30862306a36Sopenharmony_ciSome data movement architecture (DMA controller and peripherals) uses metadata
30962306a36Sopenharmony_ciassociated with a transaction. The DMA controller role is to transfer the
31062306a36Sopenharmony_cipayload and the metadata alongside.
31162306a36Sopenharmony_ciThe metadata itself is not used by the DMA engine itself, but it contains
31262306a36Sopenharmony_ciparameters, keys, vectors, etc for peripheral or from the peripheral.
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ciThe DMAengine framework provides a generic ways to facilitate the metadata for
31562306a36Sopenharmony_cidescriptors. Depending on the architecture the DMA driver can implement either
31662306a36Sopenharmony_cior both of the methods and it is up to the client driver to choose which one
31762306a36Sopenharmony_cito use.
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci- DESC_METADATA_CLIENT
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci  The metadata buffer is allocated/provided by the client driver and it is
32262306a36Sopenharmony_ci  attached (via the dmaengine_desc_attach_metadata() helper to the descriptor.
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci  From the DMA driver the following is expected for this mode:
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci  - DMA_MEM_TO_DEV / DEV_MEM_TO_MEM
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci    The data from the provided metadata buffer should be prepared for the DMA
32962306a36Sopenharmony_ci    controller to be sent alongside of the payload data. Either by copying to a
33062306a36Sopenharmony_ci    hardware descriptor, or highly coupled packet.
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci  - DMA_DEV_TO_MEM
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci    On transfer completion the DMA driver must copy the metadata to the client
33562306a36Sopenharmony_ci    provided metadata buffer before notifying the client about the completion.
33662306a36Sopenharmony_ci    After the transfer completion, DMA drivers must not touch the metadata
33762306a36Sopenharmony_ci    buffer provided by the client.
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci- DESC_METADATA_ENGINE
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci  The metadata buffer is allocated/managed by the DMA driver. The client driver
34262306a36Sopenharmony_ci  can ask for the pointer, maximum size and the currently used size of the
34362306a36Sopenharmony_ci  metadata and can directly update or read it. dmaengine_desc_get_metadata_ptr()
34462306a36Sopenharmony_ci  and dmaengine_desc_set_metadata_len() is provided as helper functions.
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci  From the DMA driver the following is expected for this mode:
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci  - get_metadata_ptr()
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci    Should return a pointer for the metadata buffer, the maximum size of the
35162306a36Sopenharmony_ci    metadata buffer and the currently used / valid (if any) bytes in the buffer.
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci  - set_metadata_len()
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci    It is called by the clients after it have placed the metadata to the buffer
35662306a36Sopenharmony_ci    to let the DMA driver know the number of valid bytes provided.
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci  Note: since the client will ask for the metadata pointer in the completion
35962306a36Sopenharmony_ci  callback (in DMA_DEV_TO_MEM case) the DMA driver must ensure that the
36062306a36Sopenharmony_ci  descriptor is not freed up prior the callback is called.
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ciDevice operations
36362306a36Sopenharmony_ci-----------------
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ciOur dma_device structure also requires a few function pointers in
36662306a36Sopenharmony_ciorder to implement the actual logic, now that we described what
36762306a36Sopenharmony_cioperations we were able to perform.
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ciThe functions that we have to fill in there, and hence have to
37062306a36Sopenharmony_ciimplement, obviously depend on the transaction types you reported as
37162306a36Sopenharmony_cisupported.
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci- ``device_alloc_chan_resources``
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci- ``device_free_chan_resources``
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci  - These functions will be called whenever a driver will call
37862306a36Sopenharmony_ci    ``dma_request_channel`` or ``dma_release_channel`` for the first/last
37962306a36Sopenharmony_ci    time on the channel associated to that driver.
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci  - They are in charge of allocating/freeing all the needed
38262306a36Sopenharmony_ci    resources in order for that channel to be useful for your driver.
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci  - These functions can sleep.
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci- ``device_prep_dma_*``
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci  - These functions are matching the capabilities you registered
38962306a36Sopenharmony_ci    previously.
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci  - These functions all take the buffer or the scatterlist relevant
39262306a36Sopenharmony_ci    for the transfer being prepared, and should create a hardware
39362306a36Sopenharmony_ci    descriptor or a list of hardware descriptors from it
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci  - These functions can be called from an interrupt context
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci  - Any allocation you might do should be using the GFP_NOWAIT
39862306a36Sopenharmony_ci    flag, in order not to potentially sleep, but without depleting
39962306a36Sopenharmony_ci    the emergency pool either.
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci  - Drivers should try to pre-allocate any memory they might need
40262306a36Sopenharmony_ci    during the transfer setup at probe time to avoid putting to
40362306a36Sopenharmony_ci    much pressure on the nowait allocator.
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci  - It should return a unique instance of the
40662306a36Sopenharmony_ci    ``dma_async_tx_descriptor structure``, that further represents this
40762306a36Sopenharmony_ci    particular transfer.
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci  - This structure can be initialized using the function
41062306a36Sopenharmony_ci    ``dma_async_tx_descriptor_init``.
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci  - You'll also need to set two fields in this structure:
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci    - flags:
41562306a36Sopenharmony_ci      TODO: Can it be modified by the driver itself, or
41662306a36Sopenharmony_ci      should it be always the flags passed in the arguments
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci    - tx_submit: A pointer to a function you have to implement,
41962306a36Sopenharmony_ci      that is supposed to push the current transaction descriptor to a
42062306a36Sopenharmony_ci      pending queue, waiting for issue_pending to be called.
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci  - In this structure the function pointer callback_result can be
42362306a36Sopenharmony_ci    initialized in order for the submitter to be notified that a
42462306a36Sopenharmony_ci    transaction has completed. In the earlier code the function pointer
42562306a36Sopenharmony_ci    callback has been used. However it does not provide any status to the
42662306a36Sopenharmony_ci    transaction and will be deprecated. The result structure defined as
42762306a36Sopenharmony_ci    ``dmaengine_result`` that is passed in to callback_result
42862306a36Sopenharmony_ci    has two fields:
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci    - result: This provides the transfer result defined by
43162306a36Sopenharmony_ci      ``dmaengine_tx_result``. Either success or some error condition.
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci    - residue: Provides the residue bytes of the transfer for those that
43462306a36Sopenharmony_ci      support residue.
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci- ``device_issue_pending``
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci  - Takes the first transaction descriptor in the pending queue,
43962306a36Sopenharmony_ci    and starts the transfer. Whenever that transfer is done, it
44062306a36Sopenharmony_ci    should move to the next transaction in the list.
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci  - This function can be called in an interrupt context
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci- ``device_tx_status``
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci  - Should report the bytes left to go over on the given channel
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci  - Should only care about the transaction descriptor passed as
44962306a36Sopenharmony_ci    argument, not the currently active one on a given channel
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci  - The tx_state argument might be NULL
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci  - Should use dma_set_residue to report it
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci  - In the case of a cyclic transfer, it should only take into
45662306a36Sopenharmony_ci    account the total size of the cyclic buffer.
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci  - Should return DMA_OUT_OF_ORDER if the device does not support in order
45962306a36Sopenharmony_ci    completion and is completing the operation out of order.
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci  - This function can be called in an interrupt context.
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci- device_config
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci  - Reconfigures the channel with the configuration given as argument
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci  - This command should NOT perform synchronously, or on any
46862306a36Sopenharmony_ci    currently queued transfers, but only on subsequent ones
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci  - In this case, the function will receive a ``dma_slave_config``
47162306a36Sopenharmony_ci    structure pointer as an argument, that will detail which
47262306a36Sopenharmony_ci    configuration to use.
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci  - Even though that structure contains a direction field, this
47562306a36Sopenharmony_ci    field is deprecated in favor of the direction argument given to
47662306a36Sopenharmony_ci    the prep_* functions
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci  - This call is mandatory for slave operations only. This should NOT be
47962306a36Sopenharmony_ci    set or expected to be set for memcpy operations.
48062306a36Sopenharmony_ci    If a driver support both, it should use this call for slave
48162306a36Sopenharmony_ci    operations only and not for memcpy ones.
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci- device_pause
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci  - Pauses a transfer on the channel
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci  - This command should operate synchronously on the channel,
48862306a36Sopenharmony_ci    pausing right away the work of the given channel
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci- device_resume
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci  - Resumes a transfer on the channel
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci  - This command should operate synchronously on the channel,
49562306a36Sopenharmony_ci    resuming right away the work of the given channel
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci- device_terminate_all
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci  - Aborts all the pending and ongoing transfers on the channel
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci  - For aborted transfers the complete callback should not be called
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci  - Can be called from atomic context or from within a complete
50462306a36Sopenharmony_ci    callback of a descriptor. Must not sleep. Drivers must be able
50562306a36Sopenharmony_ci    to handle this correctly.
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci  - Termination may be asynchronous. The driver does not have to
50862306a36Sopenharmony_ci    wait until the currently active transfer has completely stopped.
50962306a36Sopenharmony_ci    See device_synchronize.
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci- device_synchronize
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci  - Must synchronize the termination of a channel to the current
51462306a36Sopenharmony_ci    context.
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci  - Must make sure that memory for previously submitted
51762306a36Sopenharmony_ci    descriptors is no longer accessed by the DMA controller.
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci  - Must make sure that all complete callbacks for previously
52062306a36Sopenharmony_ci    submitted descriptors have finished running and none are
52162306a36Sopenharmony_ci    scheduled to run.
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci  - May sleep.
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ciMisc notes
52762306a36Sopenharmony_ci==========
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci(stuff that should be documented, but don't really know
53062306a36Sopenharmony_ciwhere to put them)
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci``dma_run_dependencies``
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci- Should be called at the end of an async TX transfer, and can be
53562306a36Sopenharmony_ci  ignored in the slave transfers case.
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci- Makes sure that dependent operations are run before marking it
53862306a36Sopenharmony_ci  as complete.
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_cidma_cookie_t
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci- it's a DMA transaction ID that will increment over time.
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci- Not really relevant any more since the introduction of ``virt-dma``
54562306a36Sopenharmony_ci  that abstracts it away.
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ciDMA_CTRL_ACK
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci- If clear, the descriptor cannot be reused by provider until the
55062306a36Sopenharmony_ci  client acknowledges receipt, i.e. has a chance to establish any
55162306a36Sopenharmony_ci  dependency chains
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci- This can be acked by invoking async_tx_ack()
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ci- If set, does not mean descriptor can be reused
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ciDMA_CTRL_REUSE
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci- If set, the descriptor can be reused after being completed. It should
56062306a36Sopenharmony_ci  not be freed by provider if this flag is set.
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci- The descriptor should be prepared for reuse by invoking
56362306a36Sopenharmony_ci  ``dmaengine_desc_set_reuse()`` which will set DMA_CTRL_REUSE.
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci- ``dmaengine_desc_set_reuse()`` will succeed only when channel support
56662306a36Sopenharmony_ci  reusable descriptor as exhibited by capabilities
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci- As a consequence, if a device driver wants to skip the
56962306a36Sopenharmony_ci  ``dma_map_sg()`` and ``dma_unmap_sg()`` in between 2 transfers,
57062306a36Sopenharmony_ci  because the DMA'd data wasn't used, it can resubmit the transfer right after
57162306a36Sopenharmony_ci  its completion.
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci- Descriptor can be freed in few ways
57462306a36Sopenharmony_ci
57562306a36Sopenharmony_ci  - Clearing DMA_CTRL_REUSE by invoking
57662306a36Sopenharmony_ci    ``dmaengine_desc_clear_reuse()`` and submitting for last txn
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci  - Explicitly invoking ``dmaengine_desc_free()``, this can succeed only
57962306a36Sopenharmony_ci    when DMA_CTRL_REUSE is already set
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci  - Terminating the channel
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci- DMA_PREP_CMD
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ci  - If set, the client driver tells DMA controller that passed data in DMA
58662306a36Sopenharmony_ci    API is command data.
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci  - Interpretation of command data is DMA controller specific. It can be
58962306a36Sopenharmony_ci    used for issuing commands to other peripherals/register reads/register
59062306a36Sopenharmony_ci    writes for which the descriptor should be in different format from
59162306a36Sopenharmony_ci    normal data descriptors.
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci- DMA_PREP_REPEAT
59462306a36Sopenharmony_ci
59562306a36Sopenharmony_ci  - If set, the transfer will be automatically repeated when it ends until a
59662306a36Sopenharmony_ci    new transfer is queued on the same channel with the DMA_PREP_LOAD_EOT flag.
59762306a36Sopenharmony_ci    If the next transfer to be queued on the channel does not have the
59862306a36Sopenharmony_ci    DMA_PREP_LOAD_EOT flag set, the current transfer will be repeated until the
59962306a36Sopenharmony_ci    client terminates all transfers.
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci  - This flag is only supported if the channel reports the DMA_REPEAT
60262306a36Sopenharmony_ci    capability.
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ci- DMA_PREP_LOAD_EOT
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci  - If set, the transfer will replace the transfer currently being executed at
60762306a36Sopenharmony_ci    the end of the transfer.
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci  - This is the default behaviour for non-repeated transfers, specifying
61062306a36Sopenharmony_ci    DMA_PREP_LOAD_EOT for non-repeated transfers will thus make no difference.
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci  - When using repeated transfers, DMA clients will usually need to set the
61362306a36Sopenharmony_ci    DMA_PREP_LOAD_EOT flag on all transfers, otherwise the channel will keep
61462306a36Sopenharmony_ci    repeating the last repeated transfer and ignore the new transfers being
61562306a36Sopenharmony_ci    queued. Failure to set DMA_PREP_LOAD_EOT will appear as if the channel was
61662306a36Sopenharmony_ci    stuck on the previous transfer.
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci  - This flag is only supported if the channel reports the DMA_LOAD_EOT
61962306a36Sopenharmony_ci    capability.
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ciGeneral Design Notes
62262306a36Sopenharmony_ci====================
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ciMost of the DMAEngine drivers you'll see are based on a similar design
62562306a36Sopenharmony_cithat handles the end of transfer interrupts in the handler, but defer
62662306a36Sopenharmony_cimost work to a tasklet, including the start of a new transfer whenever
62762306a36Sopenharmony_cithe previous transfer ended.
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ciThis is a rather inefficient design though, because the inter-transfer
63062306a36Sopenharmony_cilatency will be not only the interrupt latency, but also the
63162306a36Sopenharmony_cischeduling latency of the tasklet, which will leave the channel idle
63262306a36Sopenharmony_ciin between, which will slow down the global transfer rate.
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ciYou should avoid this kind of practice, and instead of electing a new
63562306a36Sopenharmony_citransfer in your tasklet, move that part to the interrupt handler in
63662306a36Sopenharmony_ciorder to have a shorter idle window (that we can't really avoid
63762306a36Sopenharmony_cianyway).
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ciGlossary
64062306a36Sopenharmony_ci========
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci- Burst: A number of consecutive read or write operations that
64362306a36Sopenharmony_ci  can be queued to buffers before being flushed to memory.
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci- Chunk: A contiguous collection of bursts
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci- Transfer: A collection of chunks (be it contiguous or not)
648