# STM32 DMA-MDMA chaining

_Peios Kernel / Hardware and drivers / Processor architectures / arm: stm32_

> This document describes the STM32 DMA-MDMA chaining feature. But before going further, let's introduce the peripherals involved.

<a id="rst-stm32-dma-mdma-chaining"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-introduction"></a>

## Introduction

> This document describes the STM32 DMA-MDMA chaining feature. But before going further, let's introduce the peripherals involved.
>
> To offload data transfers from the CPU, STM32 microprocessors (MPUs) embed direct memory access controllers (DMA).
>
> STM32MP1 SoCs embed both STM32 DMA and STM32 MDMA controllers. STM32 DMA request routing capabilities are enhanced by a DMA request multiplexer (STM32 DMAMUX).
>
> <strong>STM32 DMAMUX</strong>
>
> STM32 DMAMUX routes any DMA request from a given peripheral to any STM32 DMA controller (STM32MP1 counts two STM32 DMA controllers) channels.
>
> <strong>STM32 DMA</strong>
>
> STM32 DMA is mainly used to implement central data buffer storage (usually in the system SRAM) for different peripheral. It can access external RAMs but without the ability to generate convenient burst transfer ensuring the best load of the AXI.
>
> <strong>STM32 MDMA</strong>
>
> STM32 MDMA (Master DMA) is mainly used to manage direct data transfers between RAM data buffers without CPU intervention. It can also be used in a hierarchical structure that uses STM32 DMA as first level data buffer interfaces for AHB peripherals, while the STM32 MDMA acts as a second level DMA with better performance. As a AXI/AHB master, STM32 MDMA can take control of the AXI/AHB bus.

<a id="rst-principles"></a>

## Principles

> STM32 DMA-MDMA chaining feature relies on the strengths of STM32 DMA and STM32 MDMA controllers.
>
> STM32 DMA has a circular Double Buffer Mode (DBM). At each end of transaction (when DMA data counter - DMA\_SxNDTR - reaches 0), the memory pointers (configured with DMA\_SxSM0AR and DMA\_SxM1AR) are swapped and the DMA data counter is automatically reloaded. This allows the SW or the STM32 MDMA to process one memory area while the second memory area is being filled/used by the STM32 DMA transfer.
>
> With STM32 MDMA linked-list mode, a single request initiates the data array (collection of nodes) to be transferred until the linked-list pointer for the channel is null. The channel transfer complete of the last node is the end of transfer, unless first and last nodes are linked to each other, in such a case, the linked-list loops on to create a circular MDMA transfer.
>
> STM32 MDMA has direct connections with STM32 DMA. This enables autonomous communication and synchronization between peripherals, thus saving CPU resources and bus congestion. Transfer Complete signal of STM32 DMA channel can triggers STM32 MDMA transfer. STM32 MDMA can clear the request generated by the STM32 DMA by writing to its Interrupt Clear register (whose address is stored in MDMA\_CxMAR, and bit mask in MDMA\_CxMDR).
>
>
> <table>
> <caption>STM32 MDMA interconnect table with STM32 DMA</caption>
> <thead>
> <tr>
> <th>
>
>
> STM32 DMAMUX channels
>
> </th>
> <th>
>
>
> STM32 DMA channels
>
> </th>
> <th>
>
>
> STM32 DMA Transfer complete signal
>
> </th>
> <th>
>
>
> STM32 MDMA request
>
> </th>
> </tr>
> </thead>
> <tbody>
> <tr>
> <td>
>
>
> Channel <em>0</em>
>
> </td>
> <td>
>
>
> DMA1 channel 0
>
> </td>
> <td>
>
>
> dma1\_tcf0
>
> </td>
> <td>
>
>
> <em>0x00</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>1</em>
>
> </td>
> <td>
>
>
> DMA1 channel 1
>
> </td>
> <td>
>
>
> dma1\_tcf1
>
> </td>
> <td>
>
>
> <em>0x01</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>2</em>
>
> </td>
> <td>
>
>
> DMA1 channel 2
>
> </td>
> <td>
>
>
> dma1\_tcf2
>
> </td>
> <td>
>
>
> <em>0x02</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>3</em>
>
> </td>
> <td>
>
>
> DMA1 channel 3
>
> </td>
> <td>
>
>
> dma1\_tcf3
>
> </td>
> <td>
>
>
> <em>0x03</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>4</em>
>
> </td>
> <td>
>
>
> DMA1 channel 4
>
> </td>
> <td>
>
>
> dma1\_tcf4
>
> </td>
> <td>
>
>
> <em>0x04</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>5</em>
>
> </td>
> <td>
>
>
> DMA1 channel 5
>
> </td>
> <td>
>
>
> dma1\_tcf5
>
> </td>
> <td>
>
>
> <em>0x05</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>6</em>
>
> </td>
> <td>
>
>
> DMA1 channel 6
>
> </td>
> <td>
>
>
> dma1\_tcf6
>
> </td>
> <td>
>
>
> <em>0x06</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>7</em>
>
> </td>
> <td>
>
>
> DMA1 channel 7
>
> </td>
> <td>
>
>
> dma1\_tcf7
>
> </td>
> <td>
>
>
> <em>0x07</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>8</em>
>
> </td>
> <td>
>
>
> DMA2 channel 0
>
> </td>
> <td>
>
>
> dma2\_tcf0
>
> </td>
> <td>
>
>
> <em>0x08</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>9</em>
>
> </td>
> <td>
>
>
> DMA2 channel 1
>
> </td>
> <td>
>
>
> dma2\_tcf1
>
> </td>
> <td>
>
>
> <em>0x09</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>10</em>
>
> </td>
> <td>
>
>
> DMA2 channel 2
>
> </td>
> <td>
>
>
> dma2\_tcf2
>
> </td>
> <td>
>
>
> <em>0x0A</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>11</em>
>
> </td>
> <td>
>
>
> DMA2 channel 3
>
> </td>
> <td>
>
>
> dma2\_tcf3
>
> </td>
> <td>
>
>
> <em>0x0B</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>12</em>
>
> </td>
> <td>
>
>
> DMA2 channel 4
>
> </td>
> <td>
>
>
> dma2\_tcf4
>
> </td>
> <td>
>
>
> <em>0x0C</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>13</em>
>
> </td>
> <td>
>
>
> DMA2 channel 5
>
> </td>
> <td>
>
>
> dma2\_tcf5
>
> </td>
> <td>
>
>
> <em>0x0D</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>14</em>
>
> </td>
> <td>
>
>
> DMA2 channel 6
>
> </td>
> <td>
>
>
> dma2\_tcf6
>
> </td>
> <td>
>
>
> <em>0x0E</em>
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> Channel <em>15</em>
>
> </td>
> <td>
>
>
> DMA2 channel 7
>
> </td>
> <td>
>
>
> dma2\_tcf7
>
> </td>
> <td>
>
>
> <em>0x0F</em>
>
> </td>
> </tr>
> </tbody>
> </table>
>
> STM32 DMA-MDMA chaining feature then uses a SRAM buffer. STM32MP1 SoCs embed three fast access static internal RAMs of various size, used for data storage. Due to STM32 DMA legacy (within microcontrollers), STM32 DMA performances are bad with DDR, while they are optimal with SRAM. Hence the SRAM buffer used between STM32 DMA and STM32 MDMA. This buffer is split in two equal periods and STM32 DMA uses one period while STM32 MDMA uses the other period simultaneously.
>
>
>
> ```text
>                 dma[1:2]-tcf[0:7]
>                .----------------.
>  ____________ '    _________     V____________
> | STM32 DMA  |    /  __|>_  \    | STM32 MDMA |
> |------------|   |  /     \  |   |------------|
> | DMA_SxM0AR |<=>| | SRAM  | |<=>| []-[]...[] |
> | DMA_SxM1AR |   |  \_____/  |   |            |
> |____________|    \___<|____/    |____________|
> ```
>
> STM32 DMA-MDMA chaining uses (struct dma\_slave\_config).peripheral\_config to exchange the parameters needed to configure MDMA. These parameters are gathered into a u32 array with three values:
>
> - the STM32 MDMA request (which is actually the DMAMUX channel ID),
>
> - the address of the STM32 DMA register to clear the Transfer Complete interrupt flag,
>
> - the mask of the Transfer Complete interrupt flag of the STM32 DMA channel.

<a id="rst-device-tree-updates-for-stm32-dma-mdma-chaining-support"></a>

## Device Tree updates for STM32 DMA-MDMA chaining support

> <strong>1. Allocate a SRAM buffer</strong>
>
> > SRAM device tree node is defined in SoC device tree. You can refer to it in your board device tree to define your SRAM pool.
> >
> >
> >
> > ```text
> > &sram {
> >         my_foo_device_dma_pool: dma-sram@0 {
> >                 reg = <0x0 0x1000>;
> >         };
> > };
> > ```
> >
> > Be careful of the start index, in case there are other SRAM consumers. Define your pool size strategically: to optimise chaining, the idea is that STM32 DMA and STM32 MDMA can work simultaneously, on each buffer of the SRAM. If the SRAM period is greater than the expected DMA transfer, then STM32 DMA and STM32 MDMA will work sequentially instead of simultaneously. It is not a functional issue but it is not optimal.
> >
> > Don't forget to refer to your SRAM pool in your device node. You need to define a new property.
> >
> >
> >
> > ```text
> > &my_foo_device {
> >         ...
> >         my_dma_pool = &my_foo_device_dma_pool;
> > };
> > ```
> >
> > Then get this SRAM pool in your foo driver and allocate your SRAM buffer.
>
> <strong>2. Allocate a STM32 DMA channel and a STM32 MDMA channel</strong>
>
> > You need to define an extra channel in your device tree node, in addition to the one you should already have for "classic" DMA operation.
> >
> > This new channel must be taken from STM32 MDMA channels, so, the phandle of the DMA controller to use is the MDMA controller's one.
> >
> >
> >
> > ```text
> > &my_foo_device {
> >         [...]
> >         my_dma_pool = &my_foo_device_dma_pool;
> >         dmas = <&dmamux1 ...>,                // STM32 DMA channel
> >                <&mdma1 0 0x3 0x1200000a 0 0>; // + STM32 MDMA channel
> > };
> > ```
> >
> > Concerning STM32 MDMA bindings:
> >
> > 1\. The request line number : whatever the value here, it will be overwritten by MDMA driver with the STM32 DMAMUX channel ID passed through (struct dma\_slave\_config).peripheral\_config
> >
> > 2\. The priority level : choose Very High (0x3) so that your channel will take priority other the other during request arbitration
> >
> > 3\. A 32bit mask specifying the DMA channel configuration : source and destination address increment, block transfer with 128 bytes per single transfer
> >
> > 4\. The 32bit value specifying the register to be used to acknowledge the request: it will be overwritten by MDMA driver, with the DMA channel interrupt flag clear register address passed through (struct dma\_slave\_config).peripheral\_config
> >
> > 5\. The 32bit mask specifying the value to be written to acknowledge the request: it will be overwritten by MDMA driver, with the DMA channel Transfer Complete flag passed through (struct dma\_slave\_config).peripheral\_config

<a id="rst-driver-updates-for-stm32-dma-mdma-chaining-support-in-foo-driver"></a>

## Driver updates for STM32 DMA-MDMA chaining support in foo driver

> <strong>0. (optional) Refactor the original sg\_table if dmaengine\_prep\_slave\_sg()</strong>
>
> > In case of dmaengine\_prep\_slave\_sg(), the original sg\_table can't be used as is. Two new sg\_tables must be created from the original one. One for STM32 DMA transfer (where memory address targets now the SRAM buffer instead of DDR buffer) and one for STM32 MDMA transfer (where memory address targets the DDR buffer).
> >
> > The new sg\_list items must fit SRAM period length. Here is an example for DMA\_DEV\_TO\_MEM:
> >
> >
> >
> > ```text
> > /*
> >   * Assuming sgl and nents, respectively the initial scatterlist and its
> >   * length.
> >   * Assuming sram_dma_buf and sram_period, respectively the memory
> >   * allocated from the pool for DMA usage, and the length of the period,
> >   * which is half of the sram_buf size.
> >   */
> > struct sg_table new_dma_sgt, new_mdma_sgt;
> > struct scatterlist *s, *_sgl;
> > dma_addr_t ddr_dma_buf;
> > u32 new_nents = 0, len;
> > int i;
> >
> > /* Count the number of entries needed */
> > for_each_sg(sgl, s, nents, i)
> >         if (sg_dma_len(s) > sram_period)
> >                 new_nents += DIV_ROUND_UP(sg_dma_len(s), sram_period);
> >         else
> >                 new_nents++;
> >
> > /* Create sg table for STM32 DMA channel */
> > ret = sg_alloc_table(&new_dma_sgt, new_nents, GFP_ATOMIC);
> > if (ret)
> >         dev_err(dev, "DMA sg table alloc failed\n");
> >
> > for_each_sg(new_dma_sgt.sgl, s, new_dma_sgt.nents, i) {
> >         _sgl = sgl;
> >         sg_dma_len(s) = min(sg_dma_len(_sgl), sram_period);
> >         /* Targets the beginning = first half of the sram_buf */
> >         s->dma_address = sram_buf;
> >         /*
> >           * Targets the second half of the sram_buf
> >           * for odd indexes of the item of the sg_list
> >           */
> >         if (i & 1)
> >                 s->dma_address += sram_period;
> > }
> >
> > /* Create sg table for STM32 MDMA channel */
> > ret = sg_alloc_table(&new_mdma_sgt, new_nents, GFP_ATOMIC);
> > if (ret)
> >         dev_err(dev, "MDMA sg_table alloc failed\n");
> >
> > _sgl = sgl;
> > len = sg_dma_len(sgl);
> > ddr_dma_buf = sg_dma_address(sgl);
> > for_each_sg(mdma_sgt.sgl, s, mdma_sgt.nents, i) {
> >         size_t bytes = min_t(size_t, len, sram_period);
> >
> >         sg_dma_len(s) = bytes;
> >         sg_dma_address(s) = ddr_dma_buf;
> >         len -= bytes;
> >
> >         if (!len && sg_next(_sgl)) {
> >                 _sgl = sg_next(_sgl);
> >                 len = sg_dma_len(_sgl);
> >                 ddr_dma_buf = sg_dma_address(_sgl);
> >         } else {
> >                 ddr_dma_buf += bytes;
> >         }
> > }
> > ```
> >
> > Don't forget to release these new sg\_tables after getting the descriptors with dmaengine\_prep\_slave\_sg().
>
> <strong>1. Set controller specific parameters</strong>
>
> > First, use dmaengine\_slave\_config() with a struct dma\_slave\_config to configure STM32 DMA channel. You just have to take care of DMA addresses, the memory address (depending on the transfer direction) must point on your SRAM buffer, and set (struct dma\_slave\_config).peripheral\_size \!= 0.
> >
> > STM32 DMA driver will check (struct dma\_slave\_config).peripheral\_size to determine if chaining is being used or not. If it is used, then STM32 DMA driver fills (struct dma\_slave\_config).peripheral\_config with an array of three u32 : the first one containing STM32 DMAMUX channel ID, the second one the channel interrupt flag clear register address, and the third one the channel Transfer Complete flag mask.
> >
> > Then, use dmaengine\_slave\_config with another struct dma\_slave\_config to configure STM32 MDMA channel. Take care of DMA addresses, the device address (depending on the transfer direction) must point on your SRAM buffer, and the memory address must point to the buffer originally used for "classic" DMA operation. Use the previous (struct dma\_slave\_config).peripheral\_size and .peripheral\_config that have been updated by STM32 DMA driver, to set (struct dma\_slave\_config).peripheral\_size and .peripheral\_config of the struct dma\_slave\_config to configure STM32 MDMA channel.
> >
> >
> >
> > ```text
> > struct dma_slave_config dma_conf;
> > struct dma_slave_config mdma_conf;
> >
> > memset(&dma_conf, 0, sizeof(dma_conf));
> > [...]
> > config.direction = DMA_DEV_TO_MEM;
> > config.dst_addr = sram_dma_buf;        // SRAM buffer
> > config.peripheral_size = 1;            // peripheral_size != 0 => chaining
> >
> > dmaengine_slave_config(dma_chan, &dma_config);
> >
> > memset(&mdma_conf, 0, sizeof(mdma_conf));
> > config.direction = DMA_DEV_TO_MEM;
> > mdma_conf.src_addr = sram_dma_buf;     // SRAM buffer
> > mdma_conf.dst_addr = rx_dma_buf;       // original memory buffer
> > mdma_conf.peripheral_size = dma_conf.peripheral_size;       // <- dma_conf
> > mdma_conf.peripheral_config = dma_config.peripheral_config; // <- dma_conf
> >
> > dmaengine_slave_config(mdma_chan, &mdma_conf);
> > ```
>
> <strong>2. Get a descriptor for STM32 DMA channel transaction</strong>
>
> > In the same way you get your descriptor for your "classic" DMA operation, you just have to replace the original sg\_list (in case of dmaengine\_prep\_slave\_sg()) with the new sg\_list using SRAM buffer, or to replace the original buffer address, length and period (in case of dmaengine\_prep\_dma\_cyclic()) with the new SRAM buffer.
>
> <strong>3. Get a descriptor for STM32 MDMA channel transaction</strong>
>
> > If you previously get descriptor (for STM32 DMA) with
> >
> > - dmaengine\_prep\_slave\_sg(), then use dmaengine\_prep\_slave\_sg() for STM32 MDMA;
> >
> > - dmaengine\_prep\_dma\_cyclic(), then use dmaengine\_prep\_dma\_cyclic() for STM32 MDMA.
> >
> > Use the new sg\_list using SRAM buffer (in case of dmaengine\_prep\_slave\_sg()) or, depending on the transfer direction, either the original DDR buffer (in case of DMA\_DEV\_TO\_MEM) or the SRAM buffer (in case of DMA\_MEM\_TO\_DEV), the source address being previously set with dmaengine\_slave\_config().
>
> <strong>4. Submit both transactions</strong>
>
> > Before submitting your transactions, you may need to define on which descriptor you want a callback to be called at the end of the transfer (dmaengine\_prep\_slave\_sg()) or the period (dmaengine\_prep\_dma\_cyclic()). Depending on the direction, set the callback on the descriptor that finishes the overall transfer:
> >
> > - DMA\_DEV\_TO\_MEM: set the callback on the "MDMA" descriptor
> >
> > - DMA\_MEM\_TO\_DEV: set the callback on the "DMA" descriptor
> >
> > Then, submit the descriptors whatever the order, with dmaengine\_tx\_submit().
>
> <strong>5. Issue pending requests (and wait for callback notification)</strong>
>
> As STM32 MDMA channel transfer is triggered by STM32 DMA, you must issue STM32 MDMA channel before STM32 DMA channel.
>
> If any, your callback will be called to warn you about the end of the overall transfer or the period completion.
>
> Don't forget to terminate both channels. STM32 DMA channel is configured in cyclic Double-Buffer mode so it won't be disabled by HW, you need to terminate it. STM32 MDMA channel will be stopped by HW in case of sg transfer, but not in case of cyclic transfer. You can terminate it whatever the kind of transfer.
>
> <strong>STM32 DMA-MDMA chaining DMA\_MEM\_TO\_DEV special case</strong>
>
> STM32 DMA-MDMA chaining in DMA\_MEM\_TO\_DEV is a special case. Indeed, the STM32 MDMA feeds the SRAM buffer with the DDR data, and the STM32 DMA reads data from SRAM buffer. So some data (the first period) have to be copied in SRAM buffer when the STM32 DMA starts to read.
>
> A trick could be pausing the STM32 DMA channel (that will raise a Transfer Complete signal, triggering the STM32 MDMA channel), but the first data read by the STM32 DMA could be "wrong". The proper way is to prepare the first SRAM period with dmaengine\_prep\_dma\_memcpy(). Then this first period should be "removed" from the sg or the cyclic transfer.
>
> Due to this complexity, rather use the STM32 DMA-MDMA chaining for DMA\_DEV\_TO\_MEM and keep the "classic" DMA usage for DMA\_MEM\_TO\_DEV, unless you're not afraid.

<a id="rst-resources"></a>

## Resources

> Application note, datasheet and reference manual are available on ST website ([STM32MP1](<https://www.st.com/en/microcontrollers-microprocessors/stm32mp1-series.html>)).
>
> Dedicated focus on three application notes ([AN5224](<https://www.st.com/resource/en/application_note/an5224-stm32-dmamux-the-dma-request-router-stmicroelectronics.pdf>), [AN4031](<https://www.st.com/resource/en/application_note/dm00046011-using-the-stm32f2-stm32f4-and-stm32f7-series-dma-controller-stmicroelectronics.pdf>) &amp; [AN5001](<https://www.st.com/resource/en/application_note/an5001-stm32cube-expansion-package-for-stm32h7-series-mdma-stmicroelectronics.pdf>)) dealing with STM32 DMAMUX, STM32 DMA and STM32 MDMA.

<a id="rst-stm32mp1"></a>



<a id="rst-an5224"></a>



<a id="rst-an4031"></a>



<a id="rst-an5001"></a>



**Authors:** 

- Amelie Delaunay &lt;[amelie.delaunay@foss.st.com](<mailto:amelie.delaunay@foss.st.com>)&gt;
