Skip to contentSkip to conversation

Add Amlogic general DMA

PATCH Superseded Xianwei Zhao 2 participants 6 messages May 21

Subsystems (3)

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v8 cover Add Amlogic general DMA

Add DMA driver and bindigns for the Amlogic SoCs.

---
Changes in v8:
- Use kzalloc instead of kmalloc.
- Initialize the temporary variable and fix a spelling mistake.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Take use vchan to support mltiple txns.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Some minor modifications according to Frank's suggestion.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Rename head file and rename macro definition.
- Rename the subject in [2/3] from "dma" to "dmaengine".
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Support split transfer when data len > MAX_LEN.
- When a module fails or exits, perform de-initialization.
- Some other minor modifications.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Adjust the format of binding according to Frank's suggestion.
- Some code format modified according to Frank's suggestion.
- Support one prep_sg and one submit, drop multi prep_sg and one submit.
- Keep pre state when resume from pause status.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Introduce what the DMA is used for in the A9 SoC.
- Some minor modifications were made according to Krzysztof's suggestions.
- Some modifications were made according to Neil's suggestions.
- Fix a build error.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Xianwei Zhao (3):
dt-bindings: dma: Add Amlogic A9 SoC DMA
dmaengine: amlogic: Add general DMA driver for A9
MAINTAINERS: Add an entry for Amlogic DMA driver

 .../devicetree/bindings/dma/amlogic,a9-dma.yaml    |  65 ++
 MAINTAINERS                                        |   7 +
 drivers/dma/Kconfig                                |  10 +
 drivers/dma/Makefile                               |   1 +
 drivers/dma/amlogic-dma.c                          | 682 +++++++++++++++++++++
 include/dt-bindings/dma/amlogic,a9-dma.h           |   8 +
 6 files changed, 773 insertions(+)

---
base-commit: 0b1f98df9cf024e9f1a43e0ef9c16d3466d17746
change-id: 20251215-amlogic-dma-79477d5cd264

Best regards,

Signature · 1 line
Xianwei Zhao <[email protected]>
#1 lore raw

PATCH v8 1/3 dt-bindings: dma: Add Amlogic A9 SoC DMA

Add documentation describing the Amlogic A9 SoC DMA. And add the properties specific values defines into a new include file.

---
 .../devicetree/bindings/dma/amlogic,a9-dma.yaml    | 65 ++++++++++++++++++++++
 include/dt-bindings/dma/amlogic,a9-dma.h           |  8 +++
 2 files changed, 73 insertions(+)
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +65 -0
new file mode 100644
index 000000000000..efd7b2602c33
@@ -0,0 +1,65 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/dma/amlogic,a9-dma.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Amlogic general DMA controller
+
+description:
+  This is a general-purpose peripheral DMA controller. It currently supports
+  major peripherals including I2C, I3C, PIO, and CAN-BUS. Transmit and receive
+  for the same peripheral use two separate channels, controlled by different
+  register sets. I2C and I3C transfer data in 1-byte units, while PIO and
+  CAN-BUS transfer data in 4-byte units. From the controller’s perspective,
+  there is no significant difference.
+
+maintainers:
+  - Xianwei Zhao <[email protected]>
+
+properties:
+  compatible:
+    const: amlogic,a9-dma
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  clock-names:
+    const: sys
+
+  '#dma-cells':
+    const: 2
+
+  dma-channels:
+    maximum: 64
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - '#dma-cells'
+  - dma-channels
+
+allOf:
+  - $ref: dma-controller.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    dma-controller@fe400000{
+        compatible = "amlogic,a9-dma";
+        reg = <0xfe400000 0x4000>;
+        interrupts = <GIC_SPI 35 IRQ_TYPE_EDGE_RISING>;
+        clocks = <&clkc 45>;
+        #dma-cells = <2>;
+        dma-channels = <28>;
+    };
include/dt-bindings/dma/amlogic,a9-dma.h new file +8 -0
new file mode 100644
index 000000000000..c59c2fd4b956
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR MIT) */
+
+#ifndef __DT_BINDINGS_DMA_AMLOGIC_DMA_H__
+#define __DT_BINDINGS_DMA_AMLOGIC_DMA_H__
+
+#define DMA_TX			0
+#define DMA_RX			1
+#endif /* __DT_BINDINGS_DMA_AMLOGIC_DMA_H__ */
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH v8 1/3] dt-bindings: dma: Add Amlogic A9 SoC DMA

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] The macros DMA_TX and DMA_RX are defined in a global header without any vendor or subsystem prefix.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] The macros DMA_TX and DMA_RX are defined in a global header without any vendor or subsystem prefix.
- [Low] The binding specifies #dma-cells: const: 2 but fails to document the semantics of the two cells. --

commit 7a02c7e6145050221d77d7a4745ee4d4e651163b
Author: Xianwei Zhao <[email protected]>

dt-bindings: dma: Add Amlogic A9 SoC DMA

This commit adds device tree binding documentation for the Amlogic A9 SoC general-purpose DMA controller. It also introduces a new dt-bindings header file with property values for the DMA transfer directions.

Quoted 5 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +0 -0
new file mode 100644
index 0000000000000..efd7b2602c336

[ ... ]

Quoted 3 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +3 -0
+properties:
+  compatible:
+    const: amlogic,a9-dma

[ ... ]

Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml line 37

Quoted 5 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +5 -0
+  clock-names:
+    const: sys
+
+  '#dma-cells':
+    const: 2

[Severity: Low]
Should this include a description explaining what the two cells represent?

Usually bindings with multiple cells document the semantics of each cell (e.g., first cell for the request line, second for transfer direction) so clients know how to construct their dmas specifier.

[ ... ]

include/dt-bindings/dma/amlogic,a9-dma.h line 7

Quoted 13 lines
include/dt-bindings/dma/amlogic,a9-dma.h new file +7 -0
new file mode 100644
index 0000000000000..c59c2fd4b9561
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR MIT) */
+
+#ifndef __DT_BINDINGS_DMA_AMLOGIC_DMA_H__
+#define __DT_BINDINGS_DMA_AMLOGIC_DMA_H__
+
+#define DMA_TX			0
+#define DMA_RX			1

[Severity: Low]
Would it be safer to add a vendor or device prefix to these macros?

Since this header is globally accessible and can be included in both device tree sources and driver code, generic names like DMA_TX and DMA_RX might cause macro redefinition conflicts with other kernel components. Adding a prefix like AMLOGIC_A9_ would avoid this.

Quoted 1 line
include/dt-bindings/dma/amlogic,a9-dma.h new file +1 -0
+#endif /* __DT_BINDINGS_DMA_AMLOGIC_DMA_H__ */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v8 2/3 dmaengine: amlogic: Add general DMA driver for A9

In reply to Xianwei Zhao

Amlogic A9 SoCs include a general-purpose DMA controller that can be used by multiple peripherals, such as I2C PIO and I3C. Each peripheral group is associated with a dedicated DMA channel in hardware.

---
 drivers/dma/Kconfig       |  10 +
 drivers/dma/Makefile      |   1 +
 drivers/dma/amlogic-dma.c | 682 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 693 insertions(+)
drivers/dma/Kconfig +10 -0
index e98e3e8c5036..400ae0f11f89 100644
@@ -85,6 +85,16 @@ config AMCC_PPC440SPE_ADMA
 	help
 	  Enable support for the AMCC PPC440SPe RAID engines.
 
+config AMLOGIC_DMA
+	tristate "Amlogic general DMA support"
+	depends on ARCH_MESON || COMPILE_TEST
+	select DMA_ENGINE
+	select DMA_VIRTUAL_CHANNELS
+	select REGMAP_MMIO
+	help
+	  Enable support for the Amlogic general DMA engines. THis DMA
+	  controller is used some Amlogic SoCs, such as A9.
+
 config APPLE_ADMAC
 	tristate "Apple ADMAC support"
 	depends on ARCH_APPLE || COMPILE_TEST
drivers/dma/Makefile +1 -0
index df566c4958b6..8457383281a8 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_DMATEST) += dmatest.o
 obj-$(CONFIG_ALTERA_MSGDMA) += altera-msgdma.o
 obj-$(CONFIG_AMBA_PL08X) += amba-pl08x.o
 obj-$(CONFIG_AMCC_PPC440SPE_ADMA) += ppc4xx/
+obj-$(CONFIG_AMLOGIC_DMA) += amlogic-dma.o
 obj-$(CONFIG_APPLE_ADMAC) += apple-admac.o
 obj-$(CONFIG_ARM_DMA350) += arm-dma350.o
 obj-$(CONFIG_AT_HDMAC) += at_hdmac.o
drivers/dma/amlogic-dma.c new file +682 -0

682 lines not on this page · raw message

Signature · 1 line
2.52.0
#4 lore raw

Re: [PATCH v8 2/3] dmaengine: amlogic: Add general DMA driver for A9

Thank you for your contribution! Sashiko AI review found 17 potential issue(s) to consider: - [Critical] The naive ring buffer allocator overflows on large scatterlists and silently overwrites…

Thank you for your contribution! Sashiko AI review found 17 potential issue(s) to consider:
- [Critical] The naive ring buffer allocator overflows on large scatterlists and silently overwrites active in-flight DMA descriptors.
- [High] The DMA engine device is unregistered twice on driver removal and probe error paths.
- [High] The DMA controller is exposed to clients before hardware and interrupts are fully initialized.
- [High] Integer truncation in the calculation of the allocation length for aml_dma_dev leads to a heap buffer overflow.
- [High] The driver allocates memory with GFP_KERNEL inside a DMA preparation callback, which can sleep in atomic context.
- [High] The driver unconditionally dereferences cur_desc in the interrupt handler and terminate_all, leading to a kernel panic if the channel is idle.
- [High] A single DMA transfer error permanently deadlocks the channel.
- [High] Calling aml_dma_prep_slave_sg with an empty scatterlist results in a buffer underflow write.
- [High] Mismatch between allocated links and written links for zero-length scatterlist elements.
- [High] Missing tasklet_kill() during driver removal leads to Use-After-Free.
- [High] For WCH (DEV_TO_MEM) channels, the hardware is never unpaused after terminate_all() or pause().
- [High] The 64-bit physical address of the DMA descriptor list is truncated to 32 bits, causing DMA faults.
- [Medium] Incorrect and non-compliant descriptor residue calculation in aml_dma_tx_status.
- [Medium] Signed integer overflow due to 32-bit shift in the interrupt handler.
- [Medium] Concurrent modification of aml_chan->status without locking causes state corruption.
- [Medium] The shared interrupt handler unconditionally returns IRQ_HANDLED even when it processes no events.
- [Medium] Hardware descriptor fields use native endianness and unaligned 64-bit access.
--

commit 6328a62df7a031573dae5a113f1f4a5b08fd7d50
Author: Xianwei Zhao <[email protected]>
Subject: dmaengine: amlogic: Add general DMA driver for A9

This patch adds a new DMA engine driver for the Amlogic A9 general-purpose DMA controller. It handles transfer operations using the virt-dma framework and registers the controller using the dmaengine API.

Quoted 6 lines
drivers/dma/amlogic-dma.c new file +0 -0
new file mode 100644
index 0000000000000..7dae5ba15c7e7
@@ -0,0 +1,682 @@

[ ... ]

drivers/dma/amlogic-dma.c line 104

Quoted 11 lines
drivers/dma/amlogic-dma.c new file +11 -0
+struct aml_dma_sg_link {
+#define LINK_LEN		GENMASK(26, 0)
+#define LINK_IRQ		BIT(27)
+#define LINK_EOC		BIT(28)
+#define LINK_LOOP		BIT(29)
+#define LINK_ERR		BIT(30)
+#define LINK_OWNER		BIT(31)
+	u32 ctl;
+	u64 address;
+	u32 revered;
+} __packed;

[Severity: Medium]
Will this __packed structure cause alignment faults on architectures sensitive to unaligned memory accesses? The 64-bit address field is forced to an unaligned 4-byte offset. Are the native types (u32, u64) safe here, or could they break DMA execution on big-endian kernels?

[ ... ]

Quoted 4 lines
drivers/dma/amlogic-dma.c new file +4 -0
+static enum dma_status aml_dma_tx_status(struct dma_chan *chan,
+					 dma_cookie_t cookie,
+					 struct dma_tx_state *txstate)
+{

[ ... ]

drivers/dma/amlogic-dma.c line 238

Quoted 9 lines
drivers/dma/amlogic-dma.c new file +9 -0
+	spin_lock_irqsave(&aml_chan->vchan.lock, flags);
+	vd = vchan_find_desc(&aml_chan->vchan, cookie);
+	if (vd) {
+		list_for_each_entry(vd, &aml_chan->vchan.desc_issued, node) {
+			aml_desc = to_aml_dma_desc(vd);
+			residue += aml_desc->data_len;
+			if (vd->tx.cookie == cookie)
+				break;
+		}

[Severity: Medium]
Does this incorrectly accumulate the data_len of independent descriptors? Furthermore, since list_for_each_entry() overwrites the loop cursor 'vd', could this traverse the entire desc_issued list without breaking if the target cookie resides in desc_submitted, thereby returning an incorrect total?

[ ... ]

Quoted 5 lines
drivers/dma/amlogic-dma.c new file +5 -0
+static int find_dma_chan_link(struct aml_dma_chan *aml_chan, u32 num)
+{
+	int idx;
+	unsigned long flags;
+

drivers/dma/amlogic-dma.c line 272

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+	spin_lock_irqsave(&aml_chan->vchan.lock, flags);
+	if ((aml_chan->idx_next + num) >= DMA_MAX_LINK)
+		idx = 0;
+	else
+		idx = aml_chan->idx_next;
+	aml_chan->idx_next = idx + num;
+	spin_unlock_irqrestore(&aml_chan->vchan.lock, flags);
+
+	return idx;
+}

[Severity: Critical]
Is it possible for 'num' to be larger than DMA_MAX_LINK (256)? If a scatterlist needs more than 256 links, could idx_next become out of bounds and cause the descriptor preparation loop to write past the end of the sg_link array? Could this also silently overwrite active in-flight descriptors without checking if they are currently being processed?

[ ... ]

Quoted 5 lines
drivers/dma/amlogic-dma.c new file +5 -0
+static struct dma_async_tx_descriptor *aml_dma_prep_slave_sg
+		(struct dma_chan *chan, struct scatterlist *sgl,
+		unsigned int sg_len, enum dma_transfer_direction direction,
+		unsigned long flags, void *context)
+{

[ ... ]

drivers/dma/amlogic-dma.c line 293

Quoted 1 line
drivers/dma/amlogic-dma.c new file +1 -0
+	aml_desc = kzalloc(sizeof(*aml_desc), GFP_KERNEL);

[Severity: High]
Can this allocation sleep in atomic context? Dmaengine prep callbacks are expected to be callable from atomic contexts (e.g. inside interrupt handlers or while holding spinlocks). Should GFP_NOWAIT or GFP_ATOMIC be used instead?

Quoted 13 lines
drivers/dma/amlogic-dma.c new file +13 -0
+	if (!aml_desc)
+		return NULL;
+	link_count = sg_nents_for_dma(sgl, sg_len, SG_MAX_LEN);
+	aml_desc->idx = find_dma_chan_link(aml_chan, link_count);
+	sg_link = aml_chan->sg_link + aml_desc->idx;
+	for_each_sg(sgl, sg, sg_len, i) {
+		avail = sg_dma_len(sg);
+		paddr = sg->dma_address;
+		while (avail > SG_MAX_LEN) {
+			/* set dma address and len to sglink*/
+			sg_link->address = paddr;
+			sg_link->ctl = FIELD_PREP(LINK_LEN, SG_MAX_LEN);
+			paddr = paddr + SG_MAX_LEN;

drivers/dma/amlogic-dma.c line 316

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+			avail = avail - SG_MAX_LEN;
+			sg_link++;
+		}
+		/* set dma address and len to sglink*/
+		sg_link->address = paddr;
+		sg_link->ctl = FIELD_PREP(LINK_LEN, avail);
+
+		aml_desc->data_len += sg_dma_len(sg);
+		sg_link++;
+	}

[Severity: High]
If a scatterlist element has a length of 0, sg_nents_for_dma() will not allocate a link slot for it. Will this loop unconditionally increment sg_link++ for that element anyway, causing the driver to consume more link slots than allocated and corrupt the ring buffer?

drivers/dma/amlogic-dma.c line 320

Quoted 4 lines
drivers/dma/amlogic-dma.c new file +4 -0
+
+	/* the last sg set eoc flag */
+	sg_link--;
+	sg_link->ctl |= LINK_EOC;

[Severity: High]
If aml_dma_prep_slave_sg() is called with sg_len == 0, the for_each_sg loop will not execute. Could this cause sg_link-- to point before the allocated descriptor index, leading to an out-of-bounds underflow write?

[ ... ]

drivers/dma/amlogic-dma.c line 332

Quoted 8 lines
drivers/dma/amlogic-dma.c new file +8 -0
+static int aml_dma_chan_pause(struct dma_chan *chan)
+{
+	struct aml_dma_chan *aml_chan = to_aml_dma_chan(chan);
+	struct aml_dma_dev *aml_dma = aml_chan->aml_dma;
+
+	regmap_set_bits(aml_dma->regmap, aml_chan->reg_offs + RCH_CFG, CFG_PAUSE);
+	aml_chan->pre_status = aml_chan->status;
+	aml_chan->status = DMA_PAUSED;

[Severity: Medium]
Are these status updates subject to race conditions? aml_chan->status is modified here without acquiring aml_chan->vchan.lock, while it is also modified concurrently by the interrupt handler.

[ ... ]

Quoted 2 lines
drivers/dma/amlogic-dma.c new file +2 -0
+static void aml_dma_start(struct aml_dma_chan *aml_chan)
+{

[ ... ]

drivers/dma/amlogic-dma.c line 396

Quoted 3 lines
drivers/dma/amlogic-dma.c new file +3 -0
+	if (aml_chan->direction == DMA_MEM_TO_DEV) {
+		regmap_write(aml_dma->regmap, aml_chan->reg_offs + RCH_ADDR,
+			     (aml_chan->sg_link_phys + aml_desc->idx * DMA_LINK_SIZE));

[Severity: High]
Does passing a 64-bit dma_addr_t (sg_link_phys) to regmap_write() silently truncate the upper 32 bits of the address? If memory is allocated above 4GB, will this lead to incorrect memory accesses by the DMA controller?

drivers/dma/amlogic-dma.c line 406

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+		regmap_write(aml_dma->regmap, aml_chan->reg_offs + RCH_LEN, aml_desc->data_len);
+		regmap_clear_bits(aml_dma->regmap, RCH_INT_MASK, BIT(chan_id));
+		/* for rch (tx) need set cfg 0 to trigger start */
+		regmap_write(aml_dma->regmap, aml_chan->reg_offs + RCH_CFG, 0);
+	} else if (aml_chan->direction == DMA_DEV_TO_MEM) {
+		regmap_write(aml_dma->regmap, aml_chan->reg_offs + WCH_ADDR,
+			     (aml_chan->sg_link_phys + aml_desc->idx * DMA_LINK_SIZE));
+		regmap_write(aml_dma->regmap, aml_chan->reg_offs + WCH_LEN, aml_desc->data_len);
+		regmap_clear_bits(aml_dma->regmap, WCH_INT_MASK, BIT(chan_id));
+	}

[Severity: High]
For WCH (DEV_TO_MEM) channels, aml_dma_start() neglects to write to WCH_CFG. If aml_dma_terminate_all() or aml_dma_chan_pause() previously set the CFG_PAUSE bit, will WCH channels remain indefinitely paused?

Quoted 1 line

+}

[ ... ]

Quoted 2 lines
drivers/dma/amlogic-dma.c new file +2 -0
+static irqreturn_t aml_dma_interrupt_handler(int irq, void *dev_id)
+{

[ ... ]

drivers/dma/amlogic-dma.c line 446

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+	while (done) {
+		i = ffs(done) - 1;
+		regmap_write(aml_dma->regmap, CLEAR_RCH, BIT(i));
+		done &= ~BIT(i);
+		aml_chan = aml_dma->aml_rch[i];
+		if (!aml_chan) {
+			dev_err(aml_dma->dma_device.dev, "idx %d rch not initialized\n", i);
+			continue;
+		}
+		aml_chan->status = (err & (1 << i)) ? DMA_ERROR : DMA_COMPLETE;

[Severity: Medium]
Could this shift result in a signed integer overflow? If 'i' reaches 31 (since MAX_CHAN_ID is 32), shifting the signed 32-bit literal '1' causes undefined behavior. Should this use 1U << i or BIT(i) instead?

[Severity: High]
If a hardware error occurs and aml_chan->status is set to DMA_ERROR, is there any mechanism to reset the status back to DMA_COMPLETE? Without a reset path, will aml_dma_start() simply return immediately for all future transfers, permanently deadlocking the channel?

drivers/dma/amlogic-dma.c line 453

Quoted 7 lines
drivers/dma/amlogic-dma.c new file +7 -0
+		spin_lock(&aml_chan->vchan.lock);
+		aml_desc = aml_chan->cur_desc;
+		if (aml_chan->status == DMA_ERROR) {
+			aml_desc->vd.tx_result.result = DMA_TRANS_READ_FAILED;
+			regmap_read(aml_dma->regmap, aml_chan->reg_offs + RCH_RD_LEN, &cpl_data);
+			aml_desc->vd.tx_result.residue = aml_desc->data_len - cpl_data;
+		}

[Severity: High]
If a spurious interrupt fires, or if it fires concurrently with aml_dma_terminate_all() (which sets cur_desc to NULL), will aml_desc be NULL here? This appears to unconditionally dereference aml_desc, potentially causing a NULL pointer dereference.

[ ... ]

drivers/dma/amlogic-dma.c line 499

Quoted 4 lines
drivers/dma/amlogic-dma.c new file +4 -0
+	}
+
+	return IRQ_HANDLED;
+}

[Severity: Medium]
Because this driver requests a shared interrupt line (IRQF_SHARED), shouldn't the handler return IRQ_NONE if it processed no events (e.g., done and end were both 0)? Returning IRQ_HANDLED unconditionally could break shared interrupt semantics and cause issues with the kernel's spurious interrupt detection.

[ ... ]

Quoted 7 lines
drivers/dma/amlogic-dma.c new file +7 -0
+static int aml_dma_probe(struct platform_device *pdev)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct dma_device *dma_dev;
+	struct aml_dma_dev *aml_dma;
+	int ret, i, len;
+	u32 chan_nr;

[ ... ]

drivers/dma/amlogic-dma.c line 573

Quoted 6 lines
drivers/dma/amlogic-dma.c new file +6 -0
+	ret = of_property_read_u32(np, "dma-channels", &chan_nr);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "failed to read dma-channels\n");
+
+	len = sizeof(struct aml_dma_dev) + sizeof(struct aml_dma_chan) * chan_nr;
+	aml_dma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);

[Severity: High]
Is the calculation of 'len' susceptible to an integer overflow? 'chan_nr' is read directly from the device tree and isn't bounds-checked before being used in the calculation of the signed integer 'len'. Could a large 'chan_nr' cause a heap buffer overflow later during initialization?

[ ... ]

drivers/dma/amlogic-dma.c line 637

Quoted 13 lines
drivers/dma/amlogic-dma.c new file +13 -0
+	ret = dmaenginem_async_device_register(dma_dev);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "failed to register dmaenginem\n");
+
+	ret = of_dma_controller_register(np, aml_of_dma_xlate, aml_dma);
+	if (ret)
+		goto err_dmaenginem_register;
+
+	regmap_write(aml_dma->regmap, RCH_INT_MASK, 0xffffffff);
+	regmap_write(aml_dma->regmap, WCH_INT_MASK, 0xffffffff);
+
+	ret = devm_request_irq(&pdev->dev, aml_dma->irq, aml_dma_interrupt_handler,
+			       IRQF_SHARED, dev_name(&pdev->dev), aml_dma);

[Severity: High]
Does exposing the DMA controller to clients before the interrupt is requested leave a window where a client could queue a transfer and the hardware could generate an unhandled interrupt? Should the interrupt be initialized before dmaenginem_async_device_register() and of_dma_controller_register() are called?

drivers/dma/amlogic-dma.c line 650

Quoted 13 lines
drivers/dma/amlogic-dma.c new file +13 -0
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request_irq\n");
+		goto err_of_dma_controller;
+	}
+
+	platform_set_drvdata(pdev, aml_dma);
+
+	return 0;
+
+err_of_dma_controller:
+	of_dma_controller_free(np);
+err_dmaenginem_register:
+	dma_async_device_unregister(dma_dev);

[Severity: High]
Because dmaenginem_async_device_register() already registers a devres action to unregister the device upon failure, does calling dma_async_device_unregister() here manually result in a double-free or list corruption?

drivers/dma/amlogic-dma.c line 660

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+
+	return ret;
+}
+
+static void aml_dma_remove(struct platform_device *pdev)
+{
+	struct aml_dma_dev *aml_dma = platform_get_drvdata(pdev);
+
+	of_dma_controller_free((&pdev->dev)->of_node);
+	dma_async_device_unregister(&aml_dma->dma_device);

[Severity: High]
Similarly, does calling dma_async_device_unregister() here cause a double-free since dmaenginem_async_device_register() was used?

[Severity: High]
Is a call to tasklet_kill() needed here during removal? Since the virt-dma framework schedules tasklets for DMA completions, failing to kill them could lead to a use-after-free if a tasklet executes after the driver has been unloaded.

Quoted 1 line

+}

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

PATCH v8 3/3 MAINTAINERS: Add an entry for Amlogic DMA driver

In reply to Xianwei Zhao

Add Amlogic DMA controller entry to MAINTAINERS to clarify the maintainers.

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
index b38452804a2d..e7530b1e7152 100644
@@ -1316,6 +1316,13 @@ F:	Documentation/devicetree/bindings/perf/amlogic,g12-ddr-pmu.yaml
 F:	drivers/perf/amlogic/
 F:	include/soc/amlogic/
 
+AMLOGIC DMA DRIVER
+M:	Xianwei Zhao <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml
+F:	drivers/dma/amlogic-dma.c
+
 AMLOGIC ISP DRIVER
 M:	Keke Li <[email protected]>
 L:	[email protected]
Signature · 1 line
2.52.0