Add a regmap-backed Register Configuration Queue (RCQ) helper for
Allwinner A733 DE3.5. It batches dirty register blocks for DMA commit.

Signed-off-by: Alastair D'Silva <[email protected]>
---
 drivers/gpu/drm/sun4i/Makefile          |   2 +-
 drivers/gpu/drm/sun4i/sun60i_de35_rcq.c | 220 ++++++++++++++++++++++++
 drivers/gpu/drm/sun4i/sun60i_de35_rcq.h |  44 +++++
 drivers/gpu/drm/sun4i/sun8i_mixer.h     |  31 +++-
 4 files changed, 295 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/sun4i/sun60i_de35_rcq.c
 create mode 100644 drivers/gpu/drm/sun4i/sun60i_de35_rcq.h

diff --git a/drivers/gpu/drm/sun4i/Makefile b/drivers/gpu/drm/sun4i/Makefile
index 501e3d867918..184bf4f65b2a 100644
--- a/drivers/gpu/drm/sun4i/Makefile
+++ b/drivers/gpu/drm/sun4i/Makefile
@@ -17,7 +17,7 @@ sun8i-drm-hdmi-y              += sun8i_hdmi_phy_clk.o
 sun8i-mixer-y                  += sun8i_mixer.o sun8i_ui_layer.o \
                                   sun8i_vi_layer.o sun8i_ui_scaler.o \
                                   sun8i_vi_scaler.o sun8i_csc.o \
-                                  sun50i_planes.o
+                                  sun50i_planes.o sun60i_de35_rcq.o
 
 sun4i-tcon-y                   += sun4i_crtc.o
 sun4i-tcon-y                   += sun4i_tcon_dclk.o
diff --git a/drivers/gpu/drm/sun4i/sun60i_de35_rcq.c 
b/drivers/gpu/drm/sun4i/sun60i_de35_rcq.c
new file mode 100644
index 000000000000..037b6c8259dd
--- /dev/null
+++ b/drivers/gpu/drm/sun4i/sun60i_de35_rcq.c
@@ -0,0 +1,220 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Alastair D'Silva <[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/dma-mapping.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+
+#include "sun60i_de35_rcq.h"
+#include "sun8i_mixer.h"
+
+#define SUN60I_DE35_TOP_RCQ_CTL_UPDATE         BIT(0)
+#define SUN60I_DE35_RCQ_DW0_LEN_MASK           GENMASK(23, 0)
+#define SUN60I_DE35_RCQ_DW0_HADDR_MASK         GENMASK(31, 24)
+#define SUN60I_DE35_RCQ_HEAD_DIRTY             BIT(0)
+
+struct sun60i_de35_rcq_block {
+       u32     offset;
+       u32     size;
+       u32     slab_off;
+       bool    dirty;
+};
+
+struct sun60i_de35_rcq {
+       struct device                   *dev;
+       void __iomem                    *mmio;
+       u32                             de_base_offset;
+       void                            *slab;
+       dma_addr_t                      slab_dma;
+       size_t                          slab_size;
+       unsigned int                    num_blocks;
+       struct sun60i_de35_rcq_block    blocks[];
+};
+
+static struct sun60i_de35_rcq_block *
+sun60i_de35_rcq_find_block(struct sun60i_de35_rcq *rcq, unsigned int reg)
+{
+       unsigned int i;
+
+       for (i = 0; i < rcq->num_blocks; i++) {
+               struct sun60i_de35_rcq_block *blk = &rcq->blocks[i];
+
+               if (reg >= blk->offset && reg < blk->offset + blk->size)
+                       return blk;
+       }
+
+       return NULL;
+}
+
+static int sun60i_de35_rcq_reg_write(void *context, unsigned int reg,
+                                    unsigned int val)
+{
+       struct sun60i_de35_rcq *rcq = context;
+       struct sun60i_de35_rcq_block *blk;
+
+       blk = sun60i_de35_rcq_find_block(rcq, reg);
+       if (blk) {
+               __le32 *slot = rcq->slab + blk->slab_off + (reg - blk->offset);
+
+               *slot = cpu_to_le32(val);
+               blk->dirty = true;
+               return 0;
+       }
+
+       writel(val, rcq->mmio + reg);
+       return 0;
+}
+
+static int sun60i_de35_rcq_reg_read(void *context, unsigned int reg,
+                                   unsigned int *val)
+{
+       struct sun60i_de35_rcq *rcq = context;
+       struct sun60i_de35_rcq_block *blk;
+
+       blk = sun60i_de35_rcq_find_block(rcq, reg);
+       if (blk) {
+               __le32 *slot = rcq->slab + blk->slab_off + (reg - blk->offset);
+
+               *val = le32_to_cpu(*slot);
+               return 0;
+       }
+
+       *val = readl(rcq->mmio + reg);
+       return 0;
+}
+
+static const struct regmap_bus sun60i_de35_rcq_regmap_bus = {
+       .reg_write = sun60i_de35_rcq_reg_write,
+       .reg_read = sun60i_de35_rcq_reg_read,
+};
+
+struct regmap *
+sun60i_de35_rcq_regmap_init(struct device *dev, void __iomem *mmio,
+                           u32 de_base_offset,
+                           const struct sun60i_de35_rcq_block_desc *descs,
+                           unsigned int num_blocks,
+                           const struct regmap_config *config,
+                           struct sun60i_de35_rcq **rcq_out)
+{
+       struct sun60i_de35_rcq *rcq;
+       size_t slab_off = 0;
+       unsigned int i;
+
+       rcq = devm_kzalloc(dev, struct_size(rcq, blocks, num_blocks),
+                          GFP_KERNEL);
+       if (!rcq)
+               return ERR_PTR(-ENOMEM);
+
+       rcq->dev = dev;
+       rcq->mmio = mmio;
+       rcq->de_base_offset = de_base_offset;
+       rcq->num_blocks = num_blocks;
+
+       for (i = 0; i < num_blocks; i++) {
+               rcq->blocks[i].offset = descs[i].offset;
+               rcq->blocks[i].size = descs[i].size;
+               rcq->blocks[i].slab_off = slab_off;
+               slab_off += ALIGN(descs[i].size, 64);
+       }
+
+       rcq->slab_size = slab_off;
+       /*
+        * Coherent allocations are zeroed; do not read back hardware SRAM
+        * because DE3.5 channel registers contain uninitialized reset values.
+        */
+       rcq->slab = dmam_alloc_coherent(dev, rcq->slab_size, &rcq->slab_dma,
+                                       GFP_KERNEL);
+       if (!rcq->slab)
+               return ERR_PTR(-ENOMEM);
+
+       for (i = 0; i < num_blocks; i++)
+               rcq->blocks[i].dirty = true;
+
+       *rcq_out = rcq;
+
+       return devm_regmap_init(dev, &sun60i_de35_rcq_regmap_bus, rcq,
+                               config);
+}
+
+static unsigned int
+sun60i_de35_rcq_collect(struct sun60i_de35_rcq *rcq,
+                       struct sun60i_de35_rcq_head *heads,
+                       unsigned int count, unsigned int max_heads)
+{
+       unsigned int i;
+
+       if (!rcq)
+               return count;
+
+       for (i = 0; i < rcq->num_blocks; i++) {
+               struct sun60i_de35_rcq_block *blk = &rcq->blocks[i];
+               dma_addr_t addr;
+
+               if (!blk->dirty)
+                       continue;
+
+               if (count >= max_heads) {
+                       WARN_ON_ONCE(1);
+                       break;
+               }
+
+               addr = rcq->slab_dma + blk->slab_off;
+               heads[count].low_addr = cpu_to_le32(lower_32_bits(addr));
+               heads[count].dw0 = 
cpu_to_le32(FIELD_PREP(SUN60I_DE35_RCQ_DW0_LEN_MASK,
+                                                         blk->size) |
+                                              
FIELD_PREP(SUN60I_DE35_RCQ_DW0_HADDR_MASK,
+                                                         upper_32_bits(addr)));
+               heads[count].dirty = cpu_to_le32(SUN60I_DE35_RCQ_HEAD_DIRTY);
+               heads[count].reg_offset = cpu_to_le32(rcq->de_base_offset +
+                                                     blk->offset);
+               blk->dirty = false;
+               count++;
+       }
+
+       return count;
+}
+
+/*
+ * Serialized by DRM atomic commit locking (single CRTC/mixer on A733).
+ */
+void sun60i_de35_rcq_commit(struct regmap *top_regs,
+                           struct sun60i_de35_rcq *rcq_a,
+                           struct sun60i_de35_rcq *rcq_b,
+                           struct sun60i_de35_rcq_head *heads,
+                           dma_addr_t heads_dma, unsigned int max_heads)
+{
+       unsigned int count = 0;
+       u32 val;
+       int ret;
+
+       ret = regmap_read_poll_timeout(top_regs, SUN60I_DE35_TOP_RCQ_CTL, val,
+                                      !(val & SUN60I_DE35_TOP_RCQ_CTL_UPDATE),
+                                      100, 50000);
+       if (ret) {
+               dev_warn_ratelimited(rcq_a ? rcq_a->dev : rcq_b->dev,
+                                    "DE3.5 RCQ busy timeout\n");
+               return;
+       }
+
+       count = sun60i_de35_rcq_collect(rcq_a, heads, count, max_heads);
+       count = sun60i_de35_rcq_collect(rcq_b, heads, count, max_heads);
+       if (!count)
+               return;
+
+       /* Ensure descriptor and shadow register writes reach memory before DMA 
*/
+       dma_wmb();
+
+       regmap_write(top_regs, SUN60I_DE35_TOP_RCQ_HADDR_L,
+                    lower_32_bits(heads_dma));
+       regmap_write(top_regs, SUN60I_DE35_TOP_RCQ_HADDR_H,
+                    upper_32_bits(heads_dma));
+       regmap_write(top_regs, SUN60I_DE35_TOP_RCQ_LEN,
+                    count * sizeof(*heads));
+       regmap_write(top_regs, SUN60I_DE35_TOP_RCQ_CTL,
+                    SUN60I_DE35_TOP_RCQ_CTL_UPDATE);
+}
diff --git a/drivers/gpu/drm/sun4i/sun60i_de35_rcq.h 
b/drivers/gpu/drm/sun4i/sun60i_de35_rcq.h
new file mode 100644
index 000000000000..0b189b414caf
--- /dev/null
+++ b/drivers/gpu/drm/sun4i/sun60i_de35_rcq.h
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2026 Alastair D'Silva <[email protected]>
+ */
+
+#ifndef _SUN60I_DE35_RCQ_H_
+#define _SUN60I_DE35_RCQ_H_
+
+#include <linux/types.h>
+
+struct device;
+struct regmap;
+
+struct sun60i_de35_rcq_block_desc {
+       u32     offset;
+       u32     size;
+};
+
+struct sun60i_de35_rcq_head {
+       __le32  low_addr;
+       __le32  dw0;
+       __le32  dirty;
+       __le32  reg_offset;
+};
+
+struct sun60i_de35_rcq;
+
+struct regmap_config;
+
+struct regmap *
+sun60i_de35_rcq_regmap_init(struct device *dev, void __iomem *mmio,
+                           u32 de_base_offset,
+                           const struct sun60i_de35_rcq_block_desc *descs,
+                           unsigned int num_blocks,
+                           const struct regmap_config *config,
+                           struct sun60i_de35_rcq **rcq_out);
+
+void sun60i_de35_rcq_commit(struct regmap *top_regs,
+                           struct sun60i_de35_rcq *rcq_a,
+                           struct sun60i_de35_rcq *rcq_b,
+                           struct sun60i_de35_rcq_head *heads,
+                           dma_addr_t heads_dma, unsigned int max_heads);
+
+#endif /* _SUN60I_DE35_RCQ_H_ */
diff --git a/drivers/gpu/drm/sun4i/sun8i_mixer.h 
b/drivers/gpu/drm/sun4i/sun8i_mixer.h
index 9552c0bd9b99..9e3a42344748 100644
--- a/drivers/gpu/drm/sun4i/sun8i_mixer.h
+++ b/drivers/gpu/drm/sun4i/sun8i_mixer.h
@@ -148,6 +148,23 @@
 #define SUN50I_MIXER_CDC0_EN                   0xd0000
 #define SUN50I_MIXER_CDC1_EN                   0xd8000
 
+/* DE3.5 TOP registers (relative to 0x05008000) */
+#define SUN60I_DE35_TOP_RESET                  0x000
+#define SUN60I_DE35_TOP_CLK                    0x004
+#define SUN60I_DE35_TOP_MBUS_CLK               0x008
+#define SUN60I_DE35_TOP_DE2TCON_MUX            0x010
+#define SUN60I_DE35_TOP_VCH2CORE_MUX           0x028
+#define SUN60I_DE35_TOP_UCH2CORE_MUX           0x02c
+#define SUN60I_DE35_TOP_PORT2CHN_MUX           0x030
+#define SUN60I_DE35_TOP_BUF_DEPTH              0x050
+#define SUN60I_DE35_TOP_GLB_CTL                        0x100
+#define SUN60I_DE35_TOP_OUT_SIZE               0x108
+#define SUN60I_DE35_TOP_AUTO_CLK               0x10c
+#define SUN60I_DE35_TOP_RCQ_CTL                        0x110
+#define SUN60I_DE35_TOP_RCQ_HADDR_L            0x114
+#define SUN60I_DE35_TOP_RCQ_HADDR_H            0x118
+#define SUN60I_DE35_TOP_RCQ_LEN                        0x11c
+
 enum {
        /* First mixer or second mixer with VEP support. */
        CCSC_MIXER0_LAYOUT,
@@ -161,6 +178,7 @@ enum sun8i_mixer_type {
        SUN8I_MIXER_DE2,
        SUN8I_MIXER_DE3,
        SUN8I_MIXER_DE33,
+       SUN8I_MIXER_DE35,
 };
 
 /**
@@ -203,6 +221,9 @@ struct sun8i_mixer_cfg {
        unsigned long           mod_rate;
 };
 
+struct sun60i_de35_rcq;
+struct sun60i_de35_rcq_head;
+
 struct sun8i_mixer {
        struct sunxi_engine             engine;
 
@@ -212,9 +233,16 @@ struct sun8i_mixer {
 
        struct clk                      *bus_clk;
        struct clk                      *mod_clk;
+       struct clk                      *ahb_clk;
+       struct clk                      *mbus_clk;
 
        struct regmap                   *top_regs;
        struct device                   *planes_dev;
+
+       struct sun60i_de35_rcq          *rcq;
+       struct sun60i_de35_rcq_head     *rcq_heads;
+       dma_addr_t                      rcq_heads_dma;
+       unsigned int                    rcq_max_heads;
 };
 
 enum {
@@ -253,7 +281,8 @@ sun8i_blender_base(struct sun8i_mixer *mixer)
 static inline u32
 sun8i_channel_base(struct sun8i_layer *layer)
 {
-       if (layer->cfg->de_type == SUN8I_MIXER_DE33)
+       if (layer->cfg->de_type == SUN8I_MIXER_DE33 ||
+           layer->cfg->de_type == SUN8I_MIXER_DE35)
                return DE33_CH_BASE + layer->channel * DE33_CH_SIZE;
        else if (layer->cfg->de_type == SUN8I_MIXER_DE3)
                return DE3_CH_BASE + layer->channel * DE3_CH_SIZE;
-- 
2.53.0

Reply via email to