Add Amlogic general DMA
Subsystems (3)
v10 cover Add Amlogic general DMA
Add DMA driver and bindigns for the Amlogic SoCs.
---
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 | 723 +++++++++++++++++++++ include/dt-bindings/dma/amlogic,a9-dma.h | 8 + 6 files changed, 817 insertions(+)
---
base-commit: f53b2c30a192b35064be2584df7a800a8e6ac710
change-id: 20251215-amlogic-dma-79477d5cd264
Best regards,
Signature · 1 line
Xianwei Zhao <[email protected]>
PATCH v10 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
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH v10 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 devicetree bindings header lack an SoC or vendor 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 devicetree bindings header lack an SoC or vendor prefix, causing namespace pollution. --
--- Patch [1]: [PATCH v10 1/3] dt-bindings: dma: Add Amlogic A9 SoC DMA --- commit dd68c09cf89a4142756eff7b44b06fb8f8ed1b20
Author: Xianwei Zhao <[email protected]>
dt-bindings: dma: Add Amlogic A9 SoC DMA
This commit adds documentation describing the Amlogic A9 SoC DMA controller. It also introduces a new include file containing specific property values for DMA channels.
[ ... ]
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@@ -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]
Do these macro names need a vendor or SoC prefix like AMLOGIC_A9_DMA_TX?
Since they are in a global devicetree bindings header, using generic names like DMA_TX and DMA_RX could pollute the global macro namespace. Are these likely to collide with existing definitions in other driver headers and cause macro redefinition build errors?
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
PATCH v10 2/3 dmaengine: amlogic: Add general DMA driver for A9
In reply to Xianwei ZhaoAmlogic 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 | 723 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 734 insertions(+)
drivers/dma/Kconfig +10 -0
@@ -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
@@ -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 +723 -0
723 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v10 2/3] dmaengine: amlogic: Add general DMA driver for A9
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] Scheduled descriptor free works are never flushed, leading to a use-after-free on module unload.…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] Scheduled descriptor free works are never flushed, leading to a use-after-free on module unload.
- [High] Missing IRQ synchronization before killing tasklets in remove() leads to use-after-free.
- [High] Failure to check the return value of platform_get_irq() breaks deferred probing.
- [High] SG_MAX_LEN is defined as an unaligned maximum value, causing unaligned physical address boundaries for subsequent DMA segments.
- [High] Missing 64-bit DMA Mask Configuration forces SWIOTLB bounce buffering on ARM64.
- [High] Resuming a paused DMA channel fails to restart pending transfers, leaving the channel permanently stalled.
- [High] Missing src_addr_widths configuration breaks DEV_TO_MEM clients.
- [Medium] Lockless status check leads to spurious errors and incorrect DMA_IN_PROGRESS returns.
- [Medium] aml_dma_tx_status() fails to report the DMA_PAUSED state.
--
--- Patch [2]: [PATCH v10 2/3] dmaengine: amlogic: Add general DMA driver for A9 --- commit 99b90ddfd63851fd9f1a5c3ca30d4210c8de8c83
Author: Xianwei Zhao <[email protected]>
Subject: dmaengine: amlogic: Add general DMA driver for A9
This patch introduces a general-purpose DMA controller driver for Amlogic A9 SoCs. It includes support for virtual channels, SG list scatter/gather capabilities, and basic transfer modes like Mem-to-Dev and Dev-to-Mem.
Quoted 5 lines
drivers/dma/amlogic-dma.c new file +0
-0
[ ... ]
drivers/dma/amlogic-dma.c line 92
Quoted 2 lines
drivers/dma/amlogic-dma.c new file +2
-0+#define MAX_CHAN_ID 32 +#define SG_MAX_LEN GENMASK(26, 0)
[Severity: High]
Does this define the segment boundary as an unaligned odd length?
Since GENMASK(26, 0) equates to 134,217,727 bytes, which is an odd number, using it to split segments will corrupt the alignment of all subsequent physical addresses in the chain.
Could this cause hardware alignment faults on the AXI bus for clients submitting large buffers?
[ ... ]
Quoted 5 lines
drivers/dma/amlogic-dma.c new file +5
-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);
drivers/dma/amlogic-dma.c line 232
Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10
-0+struct aml_dma_dev *aml_dma = aml_chan->aml_dma; +struct aml_dma_desc *aml_desc =NULL ; +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 || !txstate) +return ret;
[Severity: Medium]
Should this function intercept the status to report DMA_PAUSED?
The core dma_cookie_status() will return DMA_IN_PROGRESS for pending transfers. Because this unconditionally returns that value, it fails to substitute the driver's internal DMA_PAUSED state when the channel is paused.
This prevents clients from detecting that a channel has successfully paused.
drivers/dma/amlogic-dma.c line 245
Quoted 13 lines
drivers/dma/amlogic-dma.c new file +13
-0+ +spin_lock_irqsave (&aml_chan->vchan.lock, flags); + vd =vchan_find_desc (&aml_chan->vchan, cookie); +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) { + 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" ); + }
[Severity: Medium]
Can this lockless status check lead to spurious cookie errors?
Since dma_cookie_status() is called before acquiring the lock, the transfer could complete immediately after the check. By the time the lock is acquired, vchan_find_desc() will fail to find the descriptor, falling through to the else branch.
This would print a spurious "cookie error" and return a stale DMA_IN_PROGRESS status with a zero residue. Would re-evaluating the cookie status inside the lock prevent this race?
[ ... ]
Quoted 5 lines
drivers/dma/amlogic-dma.c new file +5
-0+ sg_link = aml_desc->sg_link; +for_each_sg (sgl, sg, sg_len, i) { + avail =sg_dma_len (sg); +if (avail ==0 ) +continue ;
drivers/dma/amlogic-dma.c line 304
Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10
-0+ paddr = sg->dma_address; +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 ); + paddr = paddr +SG_MAX_LEN ; + avail = avail -SG_MAX_LEN ; + sg_link++; + }
[Severity: High]
Will this split corrupt the physical address alignment?
Because SG_MAX_LEN is an odd number, adding it to paddr guarantees that the subsequent segment will start at an unaligned physical address. Could this cause hardware faults during transfers?
[ ... ]
drivers/dma/amlogic-dma.c line 348
Quoted 13 lines
drivers/dma/amlogic-dma.c new file +13
-0+static int aml_dma_chan_resume (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; +unsigned long flags; + +spin_lock_irqsave (&aml_chan->vchan.lock, flags); +regmap_clear_bits (aml_dma->regmap, aml_chan->reg_offs +RCH_CFG ,CFG_PAUSE ); + aml_chan->status = aml_chan->pre_status; +spin_unlock_irqrestore (&aml_chan->vchan.lock, flags); + +return 0 ; +}
[Severity: High]
Does this function need to inspect the desc_issued list or call aml_dma_start() after clearing the pause state?
If a client queues a new transfer and calls issue_pending() while the channel is paused, aml_dma_start() returns early. The transfer stays in desc_issued.
Without a call to aml_dma_start() during resume, the queued descriptors remain trapped indefinitely and the channel stalls.
[ ... ]
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 632
Quoted 8 lines
drivers/dma/amlogic-dma.c new file +8
-0+ 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;
[Severity: High]
Can this break deferred probing if platform_get_irq() fails?
If platform_get_irq() returns -EPROBE_DEFER, storing it directly without checking for negative errors means it will later be passed to devm_request_irq().
The request will treat it as a massive invalid IRQ number and reject it with -EINVAL, permanently failing the probe.
[Severity: High]
Does the DMA device initialization here miss declaring 64-bit addressing capabilities to the core framework?
The hardware naturally supports 64-bit physical addresses, but since dma_set_mask_and_coherent() is never called to set a 64-bit mask, the framework will fall back to a 32-bit mask.
Will this force any memory mapped above 4GB to use SWIOTLB bounce buffers on ARM64 systems?
[ ... ]
Quoted 4 lines
drivers/dma/amlogic-dma.c new file +4
-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;
drivers/dma/amlogic-dma.c line 660
Quoted 10 lines
drivers/dma/amlogic-dma.c new file +10
-0+ 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 */ + 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 ;
[Severity: High]
Does missing src_addr_widths configuration break DEV_TO_MEM clients?
The driver advertises DMA_DEV_TO_MEM support, but only populates dst_addr_widths.
When generic DMA clients (like ALSA/ASoC) attempt to configure the channel for capture (Rx), the capability check will fail because src_addr_widths is 0, causing the framework to reject the channel.
[ ... ]
drivers/dma/amlogic-dma.c line 692
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 );
[Severity: High]
Could unloading this module lead to a use-after-free from un-flushed work?
The aml_dma_free_desc() function schedules free_work on the global system workqueue. If a DMA channel is freed just before or during module unload, the scheduled work is not flushed or waited upon before devres drops the backing memory.
The asynchronous work will execute after aml_dma is freed, dereferencing a dangling pointer.
drivers/dma/amlogic-dma.c line 702
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]
Does killing tasklets here without IRQ synchronization expose a race window?
Because devm_request_irq() is used, the interrupt is freed by devres after remove() completes. If an interrupt handler is actively executing on another CPU, it can call tasklet_schedule() immediately after tasklet_kill() finishes.
Devres then frees the structures, and the newly scheduled tasklet will later execute on freed memory in the softirq context. Would calling synchronize_irq() before killing tasklets prevent this?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v10 3/3 MAINTAINERS: Add an entry for Amlogic DMA driver
In reply to Xianwei ZhaoAdd Amlogic DMA controller entry to MAINTAINERS to clarify the maintainers.
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -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