Skip to contentSkip to conversation

dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver

PATCH New Shan-Chun Hung 2 participants 4 messages Nov 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver

This series adds the PDMA controller driver and DT binding for the Nuvoton MA35D1 SoC.

The driver supports memory-to-memory, slave scatter-gather and cyclic transfers.

Tested on the MA35D1 SOM board with Linux v7.4-rc3:
- memory-to-memory with dmatest (2000 iterations of 1MB buffers, no failures)
- slave scatter-gather with a local (not yet submitted) QSPI patch
- cyclic slave with a local (not yet submitted) I2S patch

Changes in v3:
- Use dma_alloc_coherent() for the hardware SG table instead of kzalloc() + dma_map_single(); defer descriptor free in terminate_all().
- Rename ma35d1_desc_submit() to ma35d1_desc_prep_done(); prep() only prepares the descriptor.
- Rewrite the IRQ handler: read and ack TDSTS/ABTSTS once, then iterate the set bits.
- Address issue identified by Sashiko AI review:
- Terminate the descriptor with vchan_terminate_vdesc() when a cyclic transfer is aborted, avoiding a use-after-free in vchan_complete().
- Request the IRQ after the DMA device is registered.
- Use devm_of_dma_controller_register() and dmaenginem_async_device_register(), and drop remove(); a devm action stops the hardware and kills the tasklets.

Changes in v2:
- Address issues identified by Sashiko AI review:
- Program registers on descriptor start instead of prep.
- Fix interrupt completion handling and return IRQ_NONE when idle.
- Set PDMA_TBINTDIS on intermediate memcpy chunks.
- Validate scatter-gather lengths and bus width alignment.
- Fix channel request assignment in of_xlate().
- Safe controller teardown order in remove().
- Drop peripheral timeout support to simplify driver.
- binding: describe the hardware in the description, and drop dma-channels and the dt-bindings header.
- Use a fixed number of 10 channels and trim the includes.

Shan-Chun Hung (2):
dt-bindings: dma: nuvoton,ma35d1-pdma: Document MA35D1 PDMA controller
dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver

 .../bindings/dma/nuvoton,ma35d1-pdma.yaml     |  71 ++
 drivers/dma/Kconfig                           |  12 +
 drivers/dma/Makefile                          |   1 +
 drivers/dma/ma35d1-pdma.c                     | 662 ++++++++++++++++++
 4 files changed, 746 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dma/nuvoton,ma35d1-pdma.yaml
 create mode 100644 drivers/dma/ma35d1-pdma.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/2 dt-bindings: dma: nuvoton,ma35d1-pdma: Document MA35D1 PDMA controller

Add binding for Nuvoton MA35D1 PDMA controller.

---
 .../bindings/dma/nuvoton,ma35d1-pdma.yaml     | 71 +++++++++++++++++++
 1 file changed, 71 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dma/nuvoton,ma35d1-pdma.yaml
Documentation/devicetree/bindings/dma/nuvoton,ma35d1-pdma.yaml new file +71 -0
new file mode 100644
index 000000000000..d05971662a5d
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/dma/nuvoton,ma35d1-pdma.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton MA35D1 PDMA controller
+
+description:
+  The MA35D1 SoC has four Peripheral DMA (PDMA) controllers. Each controller
+  has 10 channels and performs memory-to-memory, memory-to-peripheral and
+  peripheral-to-memory transfers.
+
+maintainers:
+  - Shan-Chun Hung <[email protected]>
+
+allOf:
+  - $ref: dma-controller.yaml#
+
+properties:
+  compatible:
+    const: nuvoton,ma35d1-pdma
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+  "#dma-cells":
+    const: 2
+    description:
+      The first cell is the PDMA channel number. The second cell is the
+      peripheral request source number, as listed in the reference manual.
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - interrupts
+  - "#dma-cells"
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/nuvoton,ma35d1-clk.h>
+    #include <dt-bindings/reset/nuvoton,ma35d1-reset.h>
+
+    soc {
+        #address-cells = <2>;
+        #size-cells = <2>;
+
+        dma-controller@40080000 {
+            compatible = "nuvoton,ma35d1-pdma";
+            reg = <0x0 0x40080000 0x0 0x2000>;
+            interrupts = <GIC_SPI 18 IRQ_TYPE_LEVEL_HIGH>;
+            clocks = <&clk PDMA0_GATE>;
+            resets = <&sys MA35D1_RESET_PDMA0>;
+            #dma-cells = <2>;
+        };
+    };
+
+...
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 2/2 dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver

In reply to Shan-Chun Hung

Add a driver for the PDMA controller found in the Nuvoton MA35D1 SoC. It supports up to 10 channels with memory-to-memory, slave scatter-gather and cyclic transfers.

---
 drivers/dma/Kconfig       |  12 +
 drivers/dma/Makefile      |   1 +
 drivers/dma/ma35d1-pdma.c | 662 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 675 insertions(+)
 create mode 100644 drivers/dma/ma35d1-pdma.c
drivers/dma/Kconfig +12 -0
index 01f96a8257e5..3d4d09e459f1 100644
@@ -404,6 +404,18 @@ config LPC32XX_DMAMUX
 	  Support for PL080 multiplexed DMA request lines on
 	  LPC32XX platrofm.
 
+config MA35D1_PDMA
+	tristate "Nuvoton MA35D1 PDMA Engine"
+	depends on ARCH_MA35 || COMPILE_TEST
+	select DMA_ENGINE
+	select DMA_VIRTUAL_CHANNELS
+	help
+	  Support the Nuvoton MA35D1 PDMA engine. This engine is integrated
+	  into Nuvoton MA35D1 chips. This DMA controller can transfer data
+	  from memory to memory, memory to peripheral and peripheral to memory.
+	  If you have a board based on such a SoC and wish to use DMA, say Y
+	  or M here.
+
 config MCF_EDMA
 	tristate "Freescale eDMA engine support, ColdFire mcf5441x SoCs"
 	depends on M5441x || (COMPILE_TEST && FSL_EDMA=n)
drivers/dma/Makefile +1 -0
index f62d12b08e15..dd99f7d12d4d 100644
@@ -52,6 +52,7 @@ obj-y += idxd/
 obj-$(CONFIG_K3_DMA) += k3dma.o
 obj-$(CONFIG_LPC18XX_DMAMUX) += lpc18xx-dmamux.o
 obj-$(CONFIG_LPC32XX_DMAMUX) += lpc32xx-dmamux.o
+obj-$(CONFIG_MA35D1_PDMA) += ma35d1-pdma.o
 obj-$(CONFIG_MILBEAUT_HDMAC) += milbeaut-hdmac.o
 obj-$(CONFIG_MILBEAUT_XDMAC) += milbeaut-xdmac.o
 obj-$(CONFIG_MMP_PDMA) += mmp_pdma.o
drivers/dma/ma35d1-pdma.c new file +662 -0

662 lines not on this page · raw message

Signature · 1 line
2.25.1
#3 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] dmaengine: ma35d1-pdma: broken init and teardown order in ma35d1_pdma_probe() - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] dmaengine: ma35d1-pdma: broken init and teardown order in ma35d1_pdma_probe()
- [Medium] dmaengine: ma35d1-pdma: missing tasklet sync in ma35d1_free_chan_resources()
- [Medium] dmaengine: ma35d1-pdma: status clear race in ma35d1_pdma_interrupt()
--

commit fe61217d728ce53c5336f64af791df9cdaf7d2d2
Author: Shan-Chun Hung <[email protected]>

dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver

This commit adds a DMA engine driver for the Nuvoton MA35D1 PDMA controller supporting up to 10 channels for memcpy, slave SG, and cyclic transfers.

Quoted 6 lines
drivers/dma/ma35d1-pdma.c new file +0 -0
new file mode 100644
index 0000000000000..0d6a744043214
@@ -0,0 +1,662 @@

[ ... ]

drivers/dma/ma35d1-pdma.c line 442

Quoted 9 lines
drivers/dma/ma35d1-pdma.c new file +9 -0
+static void ma35d1_free_chan_resources(struct dma_chan *chan)
+{
+	struct ma35d1_chan *ch = to_ma35d1_pdma_chan(chan);
+
+	ma35d1_terminate_all(chan);
+	vchan_free_chan_resources(&ch->vc);
+	ch->request = 0;
+	memset(&ch->cfg, 0, sizeof(ch->cfg));
+}

[Severity: Medium]
Can vchan_free_chan_resources() free an active descriptor while ch->vc.task is still running?

If a client releases a channel without first calling dmaengine_terminate_sync(), hardware DMA remains active until ma35d1_terminate_all() runs here. An interrupt firing just before ma35d1_terminate_all() disables the channel schedules ch->vc.task:

CPU0 CPU1
ma35d1_free_chan_resources()

                                  <hardirq>
                                  ma35d1_pdma_interrupt()
                                    vchan_cyclic_callback(&ch->desc->vd)
                                      tasklet_schedule(&vc->task)
                                  <softirq>
                                  vchan_complete()
                                    spin_lock_irqsave(&vc->lock)
                                    vd = vc->cyclic;
                                    spin_unlock_irqrestore(&vc->lock)
  ma35d1_terminate_all()
    vchan_terminate_vdesc(&ch->desc->vd)
  vchan_free_chan_resources(&ch->vc)
    ma35d1_pdma_desc_free(vd)
      kfree(d)
                                    dmaengine_desc_get_callback_invoke(
                                      &vd->tx, &vd->tx_result)
                                      <-- use-after-free

Calling vchan_synchronize(&ch->vc) between ma35d1_terminate_all() and vchan_free_chan_resources() waits for ch->vc.task to finish before freeing the descriptors.

[ ... ]

Quoted 31 lines
drivers/dma/ma35d1-pdma.c new file +31 -0
+static irqreturn_t ma35d1_pdma_interrupt(int irq, void *devid)
+{
+	struct ma35d1_pdmadev *pdmadev = devid;
+	unsigned long pending, done, abort;
+	struct ma35d1_chan *ch;
+	unsigned long flags;
+	int i;
+
+	done = readl(pdmadev->channels[0].base + PDMA_TDSTS);
+	abort = readl(pdmadev->channels[0].base + PDMA_ABTSTS);
+	pending = done | abort;
+	if (!pending)
+		return IRQ_NONE;
+
+	writel(done, pdmadev->channels[0].base + PDMA_TDSTS);
+	writel(abort, pdmadev->channels[0].base + PDMA_ABTSTS);
+
+	for_each_set_bit(i, &pending, PDMA_MAX_CHANS) {
+		ch = &pdmadev->channels[i];
+
+		spin_lock_irqsave(&ch->vc.lock, flags);
+
+		if (ch->desc) {
+			if (test_bit(i, &abort) && ch->desc->cyclic) {
+				vchan_terminate_vdesc(&ch->desc->vd);
+				ch->desc = NULL;
+			} else if (test_bit(i, &abort)) {
+				ch->desc->vd.tx_result.result = DMA_TRANS_ABORTED;
+				ma35d1_pdma_complete(ch);
+			} else if (ch->desc->cyclic) {
+				vchan_cyclic_callback(&ch->desc->vd);

drivers/dma/ma35d1-pdma.c line 542

Quoted 10 lines
drivers/dma/ma35d1-pdma.c new file +10 -0
+			} else {
+				ma35d1_pdma_complete(ch);
+			}
+		}
+
+		spin_unlock_irqrestore(&ch->vc.lock, flags);
+	}
+
+	return IRQ_HANDLED;
+}

[Severity: Medium]
Can reading and clearing PDMA_TDSTS and PDMA_ABTSTS before acquiring ch->vc.lock apply a stale completion or abort status to a newly started transfer?

CPU0 CPU1
<hardirq>
ma35d1_pdma_interrupt()
done = readl(... + PDMA_TDSTS)
writel(done, ... + PDMA_TDSTS)

                                  ma35d1_terminate_all()
                                    spin_lock_irqsave(&ch->vc.lock)
                                    writel(BIT(ch->id), ... + PDMA_TDSTS)
                                    ch->desc = NULL;
                                    spin_unlock_irqrestore(&ch->vc.lock)
                                  ma35d1_issue_pending()
                                    spin_lock_irqsave(&ch->vc.lock)
                                    ma35d1_pdma_start_desc()
                                      ch->desc = new_desc;
                                      ma35d1_pdma_start(ch)
                                    spin_unlock_irqrestore(&ch->vc.lock)

spin_lock_irqsave(&ch->vc.lock)
if (ch->desc)
ma35d1_pdma_complete(ch) <-- completes new_desc while DMA is active
spin_unlock_irqrestore(&ch->vc.lock)

Prematurely completing new_desc schedules its descriptor free callback and starts the next descriptor on the channel while hardware DMA is still using new_desc->sg. Checking and clearing channel i's bits in PDMA_TDSTS and PDMA_ABTSTS under ch->vc.lock would prevent this race.

Quoted 13 lines
drivers/dma/ma35d1-pdma.c new file +13 -0
+
+static void ma35d1_pdma_teardown(void *data)
+{
+	struct ma35d1_pdmadev *pdmadev = data;
+	void __iomem *base = pdmadev->channels[0].base;
+	int i;
+
+	writel(0, base + PDMA_CHCTL);
+	writel(0, base + PDMA_INTEN);
+
+	for (i = 0; i < PDMA_MAX_CHANS; i++)
+		tasklet_kill(&pdmadev->channels[i].vc.task);
+}

[ ... ]

Quoted 5 lines
drivers/dma/ma35d1-pdma.c new file +5 -0
+	ret = devm_add_action_or_reset(dev, ma35d1_pdma_teardown, pdmadev);
+	if (ret)
+		return ret;
+
+	ret = dmaenginem_async_device_register(&pdmadev->ddev);

drivers/dma/ma35d1-pdma.c line 640

Quoted 10 lines
drivers/dma/ma35d1-pdma.c new file +10 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to register DMA device\n");
+
+	ret = devm_request_irq(dev, pdmadev->irq, ma35d1_pdma_interrupt, 0, pdev->name, pdmadev);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to request IRQ\n");
+
+	ret = devm_of_dma_controller_register(dev, node, ma35d1_of_xlate, pdmadev);
+	if (ret)
+		return dev_err_probe(dev, ret, "of_dma_controller_register failed\n");

[Severity: Medium]
Does registering ma35d1_pdma_teardown() and pdmadev->ddev before devm_request_irq() invert the probe and devres teardown order?

During probe, dmaenginem_async_device_register() exposes the DMA channels before the IRQ handler is installed. Because devres runs cleanup actions in reverse order, driver unbind or a failure in devm_of_dma_controller_register() frees the IRQ and unregisters pdmadev->ddev while hardware channels (PDMA_CHCTL) and interrupts (PDMA_INTEN) are still enabled and before ma35d1_pdma_teardown() calls tasklet_kill() on channels[i].vc.task.

Requesting the IRQ before registering ma35d1_pdma_teardown(), and registering ma35d1_pdma_teardown() after dmaenginem_async_device_register(), ensures hardware interrupts and tasklets are stopped before the DMA device is unregistered and the IRQ is freed.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2