Skip to contentSkip to conversation

Add Amlogic general DMA

PATCH Superseded Xianwei Zhao 3 participants 10 messages Jul 14 – Jul 15

Subsystems (3)

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v11 cover Add Amlogic general DMA

Add DMA driver and bindigns for the Amlogic SoCs.

---
Changes in v11:
- MAX_DMA_LEN 4-byte align, and return real status when get, and lockless error print.
- Link to v10: https://lore.kernel.org/r/[email protected]

Changes in v10:
- Free dma memory in workqueue.
- Deal with zero len sg (not consume sg_link),and reset status to DMA_COMPLETE when terminal channel.
- Add desc for #dma-cells in dt-bindings.
- Link to v9: https://lore.kernel.org/r/[email protected]

Changes in v9:
- Use each transmission request sg_link mem instead of the loop mem get.
- Fix some hidden issues which reviewed by ai robot.
- Link to v8: https://lore.kernel.org/r/[email protected]

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    |  68 ++
 MAINTAINERS                                        |   7 +
 drivers/dma/Kconfig                                |  10 +
 drivers/dma/Makefile                               |   1 +
 drivers/dma/amlogic-dma.c                          | 726 +++++++++++++++++++++
 include/dt-bindings/dma/amlogic,a9-dma.h           |   8 +
 6 files changed, 820 insertions(+)

---
base-commit: f53b2c30a192b35064be2584df7a800a8e6ac710
change-id: 20251215-amlogic-dma-79477d5cd264

Best regards,

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

PATCH v11 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    | 68 ++++++++++++++++++++++
 include/dt-bindings/dma/amlogic,a9-dma.h           |  8 +++
 2 files changed, 76 insertions(+)
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +68 -0
new file mode 100644
index 000000000000..6cedd5fdfefd
@@ -0,0 +1,68 @@
+# 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
+    description:
+      The first cell is the DMA channel type(DMA_TX or DMA_RX).
+      The second cell is the DMA channel index.
+
+  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 v11 1/3] dt-bindings: dma: Add Amlogic A9 SoC DMA

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The macros DMA_TX and DMA_RX in the global DT binding header lack an Amlogic/SoC specific prefix…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The macros DMA_TX and DMA_RX in the global DT binding header lack an Amlogic/SoC specific prefix, leading to namespace pollution and potential build conflicts. --

--- Patch [1]: [PATCH v11 1/3] dt-bindings: dma: Add Amlogic A9 SoC DMA --- commit 093f368eaa67418008421d43d724ce2e98651039
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 introduces the YAML schema and a header file defining the channel type macros.

Quoted 4 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +0 -0
new file mode 100644

[ ... ]

Quoted 5 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +5 -0
+  '#dma-cells':
+    const: 2
+    description:
+      The first cell is the DMA channel type(DMA_TX or DMA_RX).
+      The second cell is the DMA channel index.

[ ... ]

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

Quoted 12 lines
include/dt-bindings/dma/amlogic,a9-dma.h new file +7 -0
new file mode 100644
@@ -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]
Are these macro names too generic for a global dt-bindings header?

Since headers in include/dt-bindings/ are globally visible and frequently included by C drivers, unprefixed names like DMA_TX and DMA_RX could lead to namespace pollution and macro redefinition build failures if a driver includes this alongside other headers that use the same common names.

Would it be safer to add a vendor or SoC prefix, such as AMLOGIC_A9_DMA_TX?

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
maintainer
#3 lore raw

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

Quoted 40 lines · On 14-07-26, 08:19, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The macros DMA_TX and DMA_RX in the global DT binding header lack an Amlogic/SoC specific prefix, leading to namespace pollution and potential build conflicts. --

--- Patch [1]: [PATCH v11 1/3] dt-bindings: dma: Add Amlogic A9 SoC DMA --- commit 093f368eaa67418008421d43d724ce2e98651039
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 introduces the YAML schema and a header file defining the channel type macros.

Quoted 4 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +0 -0
new file mode 100644

[ ... ]

Quoted 5 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +5 -0
+  '#dma-cells':
+    const: 2
+    description:
+      The first cell is the DMA channel type(DMA_TX or DMA_RX).
+      The second cell is the DMA channel index.

[ ... ]

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

Quoted 12 lines
include/dt-bindings/dma/amlogic,a9-dma.h new file +7 -0
new file mode 100644
@@ -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]
Are these macro names too generic for a global dt-bindings header?

Quoted 6 lines

Since headers in include/dt-bindings/ are globally visible and frequently included by C drivers, unprefixed names like DMA_TX and DMA_RX could lead to namespace pollution and macro redefinition build failures if a driver includes this alongside other headers that use the same common names.

Would it be safer to add a vendor or SoC prefix, such as AMLOGIC_A9_DMA_TX?

I think this makes sense to add here

Signature · 1 line
~Vinod
#4 lore raw

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

Hi Vinod Koul,
Thanks for your review.

Quoted 40 lines · Vinod Koul
Quoted 39 lines · On 14-07-26, 08:19,[email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The macros DMA_TX and DMA_RX in the global DT binding header lack an Amlogic/SoC specific prefix, leading to namespace pollution and potential build conflicts. --

--- Patch [1]: [PATCH v11 1/3] dt-bindings: dma: Add Amlogic A9 SoC DMA --- commit 093f368eaa67418008421d43d724ce2e98651039
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 introduces the YAML schema and a header file defining the channel type macros.

Quoted 4 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +0 -0
new file mode 100644

[ ... ]

Quoted 5 lines
Documentation/devicetree/bindings/dma/amlogic,a9-dma.yaml new file +5 -0
+  '#dma-cells':
+    const: 2
+    description:
+      The first cell is the DMA channel type(DMA_TX or DMA_RX).
+      The second cell is the DMA channel index.

[ ... ]

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

Quoted 12 lines
include/dt-bindings/dma/amlogic,a9-dma.h new file +7 -0
new file mode 100644
@@ -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]
Are these macro names too generic for a global dt-bindings header?

Quoted 7 lines
Quoted 6 lines

Since headers in include/dt-bindings/ are globally visible and frequently included by C drivers, unprefixed names like DMA_TX and DMA_RX could lead to namespace pollution and macro redefinition build failures if a driver includes this alongside other headers that use the same common names.

Would it be safer to add a vendor or SoC prefix, such as AMLOGIC_A9_DMA_TX?

I think this makes sense to add here

In the 4 version, I was advised to remove the prefix. Should I use DMA_TX or AMLOGIC_A9_DMA_TX here?

https://lore.kernel.org/linux-amlogic/[email protected]/

#5 lore raw

PATCH v11 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 | 726 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 737 insertions(+)
drivers/dma/Kconfig +10 -0
index ae6a682c9f76..01f96a8257e5 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 14aa086629d5..f62d12b08e15 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 +726 -0

726 lines not on this page · raw message

Signature · 1 line
2.52.0
#6 lore raw

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

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] aml_dma_tx_status falsely reports in-flight transfers as DMA_COMPLETE, causing data corruption.…

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] aml_dma_tx_status falsely reports in-flight transfers as DMA_COMPLETE, causing data corruption.
- [High] Data race and TOCTOU NULL pointer dereference in aml_dma_tx_status due to early lock release.
- [High] Use-After-Free in devres-managed remove path due to tasklet scheduled after tasklet_kill.
- [High] Pending free_work items on the global workqueue cause a kernel panic on module unload.
- [High] aml_dma_terminate_all() masks interrupts but does not clear pending interrupt status, potentially corrupting subsequent transfers.
- [Medium] Missing src_addr_widths configuration prevents DMA_DEV_TO_MEM clients from validating capabilities.
- [Medium] DMA descriptors use host-endian fields, breaking hardware functionality on big-endian platforms.
--

--- Patch [2]: [PATCH v11 2/3] dmaengine: amlogic: Add general DMA driver for A9 --- commit 91d4c9c09e9b3baa0931dae1e37c022fae2b7447
Author: Xianwei Zhao <[email protected]>

dmaengine: amlogic: Add general DMA driver for A9

This patch introduces a new DMA engine driver for Amlogic A9 SoCs. The general-purpose DMA controller is used by multiple peripherals such as I2C, PIO, and I3C.

Quoted 5 lines
drivers/dma/amlogic-dma.c new file +0 -0
new file mode 100644
index 0000000000000..9de650a79abac

[ ... ]

drivers/dma/amlogic-dma.c line 105

Quoted 12 lines
drivers/dma/amlogic-dma.c new file +12 -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;
+	u32 addr_low;
+	u32 addr_high;
+	u32 revered;
+} __packed;

[Severity: Medium]
Since this structure describes a hardware memory descriptor, should these fields use __le32 instead of u32 to ensure the driver functions correctly on big-endian architectures?

[ ... ]

drivers/dma/amlogic-dma.c line 178

Quoted 6 lines
drivers/dma/amlogic-dma.c new file +6 -0
+static void aml_dma_free_desc(struct virt_dma_desc *vd)
+{
+	struct aml_dma_desc *aml_desc = to_aml_dma_desc(vd);
+
+	schedule_work(&aml_desc->free_work);
+}

[Severity: High]
Does scheduling memory cleanup on the system workqueue here introduce a panic on module unload?

If the driver module is unloaded while a work item is still pending, the module's text segment will be unmapped. When the workqueue executes aml_dma_free_desc_work(), it might jump to unmapped memory because aml_dma_remove() does not appear to flush this work.

[ ... ]

Quoted 7 lines
drivers/dma/amlogic-dma.c new file +7 -0
+static enum dma_status aml_dma_tx_status(struct dma_chan *chan,
+					 dma_cookie_t cookie,
+					 struct dma_tx_state *txstate)
+{
+	struct aml_dma_chan *aml_chan = to_aml_dma_chan(chan);
+	struct aml_dma_dev *aml_dma = aml_chan->aml_dma;
+	struct aml_dma_desc *aml_desc = NULL;

drivers/dma/amlogic-dma.c line 234

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+	struct virt_dma_desc *vd;
+	u32 residue = 0, done;
+	unsigned long flags;
+	enum dma_status ret;
+
+	ret = dma_cookie_status(chan, cookie, txstate);
+	if (ret == DMA_COMPLETE)
+		return ret;
+
+	ret = aml_chan->status;

[Severity: High]
Could this overwrite the correct DMA_IN_PROGRESS return value from dma_cookie_status() with aml_chan->status?

If aml_chan->status is usually set to DMA_COMPLETE during initialization, polling clients might mistakenly believe the transfer is finished before it actually is, leading to data corruption.

drivers/dma/amlogic-dma.c line 244

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+	if (!txstate)
+		return ret;
+
+	spin_lock_irqsave(&aml_chan->vchan.lock, flags);
+	vd = vchan_find_desc(&aml_chan->vchan, cookie);
+	spin_unlock_irqrestore(&aml_chan->vchan.lock, flags);
+	if (vd) {
+		aml_desc = to_aml_dma_desc(vd);
+		residue = aml_desc->data_len;
+	} else if (aml_chan->cur_desc && aml_chan->cur_desc->vd.tx.cookie == cookie) {

[Severity: High]
Can releasing the lock before accessing aml_chan->cur_desc lead to a NULL pointer dereference?

If a hardware interrupt completes the transfer concurrently, it sets cur_desc to NULL. A thread executing this code could check aml_chan->cur_desc (finding it non-NULL), but if the interrupt handler clears the pointer right after, the subsequent aml_chan->cur_desc->vd.tx.cookie access would dereference NULL.

Alternatively, aml_desc could be assigned NULL, causing a dereference when calculating the residue.

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+		aml_desc = aml_chan->cur_desc;
+		regmap_read(aml_dma->regmap, aml_chan->reg_offs + RCH_RD_LEN, &done);
+		residue = aml_desc->data_len - done;
+	} else {
+		dev_err(aml_dma->dma_device.dev, "cookie error\n");
+	}
+	dma_set_residue(txstate, residue);
+
+	return ret;
+}

[ ... ]

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 303

Quoted 5 lines
drivers/dma/amlogic-dma.c new file +5 -0
+		while (avail > SG_MAX_LEN) {
+			/* set dma address and len to sglink*/
+			sg_link->addr_low = lower_32_bits(paddr);
+			sg_link->addr_high = upper_32_bits(paddr);
+			sg_link->ctl = FIELD_PREP(LINK_LEN, SG_MAX_LEN);

[Severity: Medium]
Should these assignments use cpu_to_le32() to ensure proper conversion of host-endian values before writing to coherent DMA memory? Without it, this could break hardware functionality on big-endian platforms.

[ ... ]

Quoted 2 lines
drivers/dma/amlogic-dma.c new file +2 -0
+static int aml_dma_terminate_all(struct dma_chan *chan)
+{

[ ... ]

drivers/dma/amlogic-dma.c line 369

Quoted 8 lines
drivers/dma/amlogic-dma.c new file +8 -0
+	spin_lock_irqsave(&aml_chan->vchan.lock, flags);
+	regmap_set_bits(aml_dma->regmap, aml_chan->reg_offs + RCH_CFG, CFG_PAUSE);
+	regmap_set_bits(aml_dma->regmap, aml_chan->reg_offs + RCH_CFG, CFG_CLEAR);
+
+	if (aml_chan->direction == DMA_MEM_TO_DEV)
+		regmap_set_bits(aml_dma->regmap, RCH_INT_MASK, BIT(chan_id));
+	else if (aml_chan->direction == DMA_DEV_TO_MEM)
+		regmap_set_bits(aml_dma->regmap, WCH_INT_MASK, BIT(chan_id));

[Severity: High]
Does masking the interrupts here without also clearing the pending interrupt status (via CLEAR_RCH or CLEAR_WCH) risk corrupting subsequent transfers?

If an interrupt is already pending, its status bit remains set. When aml_dma_start() is called later and unmasks the interrupt, it will fire immediately and falsely complete the new transfer.

[ ... ]

Quoted 2 lines
drivers/dma/amlogic-dma.c new file +2 -0
+static int aml_dma_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/dma/amlogic-dma.c line 662

Quoted 4 lines
drivers/dma/amlogic-dma.c new file +4 -0
+	dma_dev->device_issue_pending = aml_dma_issue_pending;
+	/* PIO 4 bytes and I2C 1 byte */
+	dma_dev->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);
+	dma_dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);

[Severity: Medium]
Since this driver advertises DMA_DEV_TO_MEM support, is there a missing configuration for src_addr_widths?

If omitted, client drivers validating source widths using dma_get_slave_caps() will see a width of 0 and might fail initialization.

[ ... ]

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+static void aml_dma_remove(struct platform_device *pdev)
+{
+	struct aml_dma_dev *aml_dma = platform_get_drvdata(pdev);
+	struct aml_dma_chan *aml_chan = NULL;
+	int i;
+
+	of_dma_controller_free((&pdev->dev)->of_node);
+
+	regmap_write(aml_dma->regmap, RCH_INT_MASK, 0xffffffff);
+	regmap_write(aml_dma->regmap, WCH_INT_MASK, 0xffffffff);

drivers/dma/amlogic-dma.c line 705

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+
+	for (i = 0; i < MAX_CHAN_ID; i++) {
+		aml_chan = aml_dma->aml_rch[i];
+		if (aml_chan)
+			tasklet_kill(&aml_chan->vchan.task);
+		aml_chan = aml_dma->aml_wch[i];
+		if (aml_chan)
+			tasklet_kill(&aml_chan->vchan.task);
+	}
+}

[Severity: High]
Can a use-after-free occur during driver unbind if an interrupt fires concurrently?

Since the devres-managed IRQ is not freed or synchronized before calling tasklet_kill(), an active interrupt handler on another CPU could schedule the tasklet again right after tasklet_kill() returns. Once the remove function finishes, devres frees the channel memory containing the tasklet, leading to execution on freed memory.

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

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

In reply to Xianwei Zhao
Quoted 53 lines · On 14-07-26, 08:08, Xianwei Zhao via B4 Relay wrote:

From: Xianwei Zhao <[email protected]>

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 | 726 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 737 insertions(+)
drivers/dma/Kconfig +10 -0
index ae6a682c9f76..01f96a8257e5 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 14aa086629d5..f62d12b08e15 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 +0 -0
new file mode 100644
index 000000000000..9de650a79aba

drivers/dma/amlogic-dma.c line 3

Quoted 3 lines
drivers/dma/amlogic-dma.c new file +3 -0
@@ -0,0 +1,726 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR MIT)
+/*
+ * Copyright (C) 2025 Amlogic, Inc. All rights reserved

2026 please

Quoted 16 lines
drivers/dma/amlogic-dma.c new file +16 -0
+/* DMA controller reg */
+#define RCH_INT_MASK		0x1000
+#define WCH_INT_MASK		0x1004
+#define CLEAR_W_BATCH		0x1014
+#define CLEAR_RCH		0x1024
+#define CLEAR_WCH		0x1028
+#define RCH_ACTIVE		0x1038
+#define WCH_ACTIVE		0x103c
+#define RCH_DONE		0x104c
+#define WCH_DONE		0x1050
+#define RCH_ERR			0x1060
+#define RCH_LEN_ERR		0x1064
+#define WCH_ERR			0x1068
+#define DMA_BATCH_END		0x1078
+#define WCH_EOC_DONE		0x1088
+#define WDMA_RESP_ERR		0x1098

drivers/dma/amlogic-dma.c line 92

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+#define UPT_PKT_SYNC		0x10a8
+#define RCHN_CFG		0x10ac
+#define WCHN_CFG		0x10b0
+#define MEM_PD_CFG		0x10b4
+#define MEM_BUS_CFG		0x10b8
+#define DMA_GMV_CFG		0x10bc
+#define DMA_GMR_CFG		0x10c0
+
+#define MAX_CHAN_ID		32
+#define SG_MAX_LEN		(GENMASK(26, 0) & ~0x3)

So you define a mask for 0-26 and then clear everything expect last two bits, why not define last two bits..? Something does not look right here

Quoted 66 lines
drivers/dma/amlogic-dma.c new file +66 -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;
+
+	const struct regmap_config aml_regmap_config = {
+		.reg_bits = 32,
+		.val_bits = 32,
+		.reg_stride = 4,
+		.max_register = 0x3000,
+	};
+
+	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");
+	if (chan_nr > (MAX_CHAN_ID * 2))
+		return dev_err_probe(&pdev->dev, -EINVAL, "dma-channels unusual\n");
+
+	len = sizeof(struct aml_dma_dev) + sizeof(struct aml_dma_chan) * chan_nr;
+	aml_dma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
+	if (!aml_dma)
+		return -ENOMEM;
+
+	aml_dma->chan_nr = chan_nr;
+
+	aml_dma->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(aml_dma->base))
+		return PTR_ERR(aml_dma->base);
+
+	aml_dma->regmap = devm_regmap_init_mmio(&pdev->dev, aml_dma->base,
+						&aml_regmap_config);
+	if (IS_ERR_OR_NULL(aml_dma->regmap))
+		return PTR_ERR(aml_dma->regmap);
+
+	aml_dma->clk = devm_clk_get_enabled(&pdev->dev, NULL);
+	if (IS_ERR(aml_dma->clk))
+		return PTR_ERR(aml_dma->clk);
+
+	aml_dma->irq = platform_get_irq(pdev, 0);
+
+	aml_dma->pdev = pdev;
+	aml_dma->dma_device.dev = &pdev->dev;
+
+	dma_dev = &aml_dma->dma_device;
+	INIT_LIST_HEAD(&dma_dev->channels);
+
+	/* Initialize channel parameters */
+	for (i = 0; i < chan_nr; i++) {
+		struct aml_dma_chan *aml_chan = &aml_dma->aml_chans[i];
+
+		aml_chan->aml_dma = aml_dma;
+		aml_chan->vchan.desc_free = aml_dma_free_desc;
+		vchan_init(&aml_chan->vchan, &aml_dma->dma_device);
+	}
+	aml_dma->chan_used = 0;
+
+	dma_set_max_seg_size(dma_dev->dev, SG_MAX_LEN);
+	dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
+	dma_dev->device_alloc_chan_resources = aml_dma_alloc_chan_resources;
+	dma_dev->device_free_chan_resources = aml_dma_free_chan_resources;
+	dma_dev->device_tx_status = aml_dma_tx_status;
+	dma_dev->device_prep_slave_sg = aml_dma_prep_slave_sg;
+	dma_dev->device_pause = aml_dma_chan_pause;

drivers/dma/amlogic-dma.c line 666

Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10 -0
+	dma_dev->device_resume = aml_dma_chan_resume;
+	dma_dev->device_terminate_all = aml_dma_terminate_all;
+	dma_dev->device_issue_pending = aml_dma_issue_pending;
+	/* PIO 4 bytes and I2C 1 byte */
+	dma_dev->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);
+	dma_dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
+	dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
+
+	regmap_write(aml_dma->regmap, RCH_INT_MASK, 0xffffffff);
+	regmap_write(aml_dma->regmap, WCH_INT_MASK, 0xffffffff);

I think we have macros for 32bit masks, please use that here and other places

Signature · 1 line
~Vinod
#8 lore raw

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

Hi Vinod Koul,
Thanks for your review.

Quoted 79 lines · Vinod Koul
Quoted 53 lines · On 14-07-26, 08:08, Xianwei Zhao via B4 Relay wrote:

From: Xianwei Zhao<[email protected]>

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 | 726 ++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 737 insertions(+)
drivers/dma/Kconfig +10 -0
index ae6a682c9f76..01f96a8257e5 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 14aa086629d5..f62d12b08e15 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 +0 -0
new file mode 100644
index 000000000000..9de650a79aba

drivers/dma/amlogic-dma.c line 3

Quoted 3 lines
drivers/dma/amlogic-dma.c new file +3 -0
@@ -0,0 +1,726 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR MIT)
+/*
+ * Copyright (C) 2025 Amlogic, Inc. All rights reserved

2026 please

Quoted 20 lines
drivers/dma/amlogic-dma.c new file +20 -0
+/* DMA controller reg */
+#define RCH_INT_MASK         0x1000
+#define WCH_INT_MASK         0x1004
+#define CLEAR_W_BATCH                0x1014
+#define CLEAR_RCH            0x1024
+#define CLEAR_WCH            0x1028
+#define RCH_ACTIVE           0x1038
+#define WCH_ACTIVE           0x103c
+#define RCH_DONE             0x104c
+#define WCH_DONE             0x1050
+#define RCH_ERR                      0x1060
+#define RCH_LEN_ERR          0x1064
+#define WCH_ERR                      0x1068
+#define DMA_BATCH_END                0x1078
+#define WCH_EOC_DONE         0x1088
+#define WDMA_RESP_ERR                0x1098
+#define UPT_PKT_SYNC         0x10a8
+#define RCHN_CFG             0x10ac
+#define WCHN_CFG             0x10b0
+#define MEM_PD_CFG           0x10b4
Quoted 9 lines

drivers/dma/amlogic-dma.c line 92

Quoted 6 lines
drivers/dma/amlogic-dma.c new file +6 -0
+#define MEM_BUS_CFG          0x10b8
+#define DMA_GMV_CFG          0x10bc
+#define DMA_GMR_CFG          0x10c0
+
+#define MAX_CHAN_ID          32
+#define SG_MAX_LEN           (GENMASK(26, 0) & ~0x3)

So you define a mask for 0-26 and then clear everything expect last two bits, why not define last two bits..? Something does not look right here

Will do, define GENMASK(26, 2)

Quoted 70 lines
Quoted 70 lines
drivers/dma/amlogic-dma.c new file +70 -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;
+
+     const struct regmap_config aml_regmap_config = {
+             .reg_bits = 32,
+             .val_bits = 32,
+             .reg_stride = 4,
+             .max_register = 0x3000,
+     };
+
+     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");
+     if (chan_nr > (MAX_CHAN_ID * 2))
+             return dev_err_probe(&pdev->dev, -EINVAL, "dma-channels unusual\n");
+
+     len = sizeof(struct aml_dma_dev) + sizeof(struct aml_dma_chan) * chan_nr;
+     aml_dma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
+     if (!aml_dma)
+             return -ENOMEM;
+
+     aml_dma->chan_nr = chan_nr;
+
+     aml_dma->base = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(aml_dma->base))
+             return PTR_ERR(aml_dma->base);
+
+     aml_dma->regmap = devm_regmap_init_mmio(&pdev->dev, aml_dma->base,
+                                             &aml_regmap_config);
+     if (IS_ERR_OR_NULL(aml_dma->regmap))
+             return PTR_ERR(aml_dma->regmap);
+
+     aml_dma->clk = devm_clk_get_enabled(&pdev->dev, NULL);
+     if (IS_ERR(aml_dma->clk))
+             return PTR_ERR(aml_dma->clk);
+
+     aml_dma->irq = platform_get_irq(pdev, 0);
+
+     aml_dma->pdev = pdev;
+     aml_dma->dma_device.dev = &pdev->dev;
+
+     dma_dev = &aml_dma->dma_device;
+     INIT_LIST_HEAD(&dma_dev->channels);
+
+     /* Initialize channel parameters */
+     for (i = 0; i < chan_nr; i++) {
+             struct aml_dma_chan *aml_chan = &aml_dma->aml_chans[i];
+
+             aml_chan->aml_dma = aml_dma;
+             aml_chan->vchan.desc_free = aml_dma_free_desc;
+             vchan_init(&aml_chan->vchan, &aml_dma->dma_device);
+     }
+     aml_dma->chan_used = 0;
+
+     dma_set_max_seg_size(dma_dev->dev, SG_MAX_LEN);
+     dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
+     dma_dev->device_alloc_chan_resources = aml_dma_alloc_chan_resources;
+     dma_dev->device_free_chan_resources = aml_dma_free_chan_resources;
+     dma_dev->device_tx_status = aml_dma_tx_status;
+     dma_dev->device_prep_slave_sg = aml_dma_prep_slave_sg;
+     dma_dev->device_pause = aml_dma_chan_pause;
+     dma_dev->device_resume = aml_dma_chan_resume;
+     dma_dev->device_terminate_all = aml_dma_terminate_all;
+     dma_dev->device_issue_pending = aml_dma_issue_pending;
+     /* PIO 4 bytes and I2C 1 byte */
Quoted 8 lines

drivers/dma/amlogic-dma.c line 666

Quoted 6 lines
drivers/dma/amlogic-dma.c new file +6 -0
+     dma_dev->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);
+     dma_dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
+     dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
+
+     regmap_write(aml_dma->regmap, RCH_INT_MASK, 0xffffffff);
+     regmap_write(aml_dma->regmap, WCH_INT_MASK, 0xffffffff);

I think we have macros for 32bit masks, please use that here and other places

Will do.

#9 lore raw

PATCH v11 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 3e7b2d9e9c24..b4ef8d3f52cb 100644
@@ -1307,6 +1307,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