[PATCH v5 2/3] dmaengine: fsl-emda: add debugfs support

From: Frank Li
Date: Mon Oct 02 2023 - 14:38:27 EST


Add debugfs support to fsl-edma to enable dumping of register states.

Signed-off-by: Frank Li <Frank.Li@xxxxxxx>
---
drivers/dma/Makefile | 5 +-
drivers/dma/fsl-edma-common.h | 8 ++
drivers/dma/fsl-edma-debugfs.c | 200 +++++++++++++++++++++++++++++++++
drivers/dma/fsl-edma-main.c | 2 +
4 files changed, 213 insertions(+), 2 deletions(-)
create mode 100644 drivers/dma/fsl-edma-debugfs.c

diff --git a/drivers/dma/Makefile b/drivers/dma/Makefile
index 83553a97a010..a51c6397bcad 100644
--- a/drivers/dma/Makefile
+++ b/drivers/dma/Makefile
@@ -31,10 +31,11 @@ obj-$(CONFIG_DW_AXI_DMAC) += dw-axi-dmac/
obj-$(CONFIG_DW_DMAC_CORE) += dw/
obj-$(CONFIG_DW_EDMA) += dw-edma/
obj-$(CONFIG_EP93XX_DMA) += ep93xx_dma.o
+fsl-edma-debugfs-$(CONFIG_DEBUG_FS) := fsl-edma-debugfs.o
obj-$(CONFIG_FSL_DMA) += fsldma.o
-fsl-edma-objs := fsl-edma-main.o fsl-edma-common.o
+fsl-edma-objs := fsl-edma-main.o fsl-edma-common.o $(fsl-edma-debugfs-y)
obj-$(CONFIG_FSL_EDMA) += fsl-edma.o
-mcf-edma-objs := mcf-edma-main.o fsl-edma-common.o
+mcf-edma-objs := mcf-edma-main.o fsl-edma-common.o $(fsl-edma-debugfs-y)
obj-$(CONFIG_MCF_EDMA) += mcf-edma.o
obj-$(CONFIG_FSL_QDMA) += fsl-qdma.o
obj-$(CONFIG_FSL_RAID) += fsl_raid.o
diff --git a/drivers/dma/fsl-edma-common.h b/drivers/dma/fsl-edma-common.h
index 3cc0cc8fc2d0..029197440bc3 100644
--- a/drivers/dma/fsl-edma-common.h
+++ b/drivers/dma/fsl-edma-common.h
@@ -336,4 +336,12 @@ void fsl_edma_free_chan_resources(struct dma_chan *chan);
void fsl_edma_cleanup_vchan(struct dma_device *dmadev);
void fsl_edma_setup_regs(struct fsl_edma_engine *edma);

+#ifdef CONFIG_DEBUG_FS
+void fsl_edma_debugfs_on(struct fsl_edma_engine *edma);
+#else
+static inline void fsl_edma_debugfs_on(struct fsl_edma_engine *edma)
+{
+}
+#endif /* CONFIG_DEBUG_FS */
+
#endif /* _FSL_EDMA_COMMON_H_ */
diff --git a/drivers/dma/fsl-edma-debugfs.c b/drivers/dma/fsl-edma-debugfs.c
new file mode 100644
index 000000000000..99f34198dae0
--- /dev/null
+++ b/drivers/dma/fsl-edma-debugfs.c
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/debugfs.h>
+#include <linux/bitfield.h>
+
+#include "fsl-edma-common.h"
+
+#define fsl_edma_debugfs_reg(reg, b, _s, __name) \
+do { reg->name = __stringify(__name); \
+ reg->offset = offsetof(struct _s, __name); \
+ reg->size = sizeof(((struct _s *)0)->__name); \
+ reg->bigendian = b; \
+ reg++; \
+} while (0)
+
+#define fsl_edma_debugfs_regv1(reg, edma, __name) \
+do { reg->name = __stringify(__name); \
+ reg->offset = edma->regs.__name - edma->membase;\
+ reg->bigendian = edma->big_endian; \
+ reg++; \
+} while (0)
+
+static void fsl_edma_debufs_tcdreg(struct fsl_edma_chan *chan, struct dentry *dir)
+{
+ struct debugfs_regset *regset;
+ struct debugfs_reg *reg;
+ struct device *dev;
+ int be;
+
+ be = chan->edma->big_endian;
+
+ dev = &chan->pdev->dev;
+
+ regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
+ if (!regset)
+ return;
+
+ regset->dev = dev;
+ regset->base = chan->tcd;
+
+ /* sizeof(struct fsl_edma_hw_tcd)/sizeof(u16) is enough for hold all registers */
+ reg = devm_kcalloc(dev, sizeof(struct fsl_edma_hw_tcd)/sizeof(u16),
+ sizeof(*reg), GFP_KERNEL);
+
+ if (!reg)
+ return;
+
+ regset->regs = reg;
+
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, saddr);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, soff);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, attr);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, nbytes);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, slast);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, daddr);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, doff);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, citer);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, dlast_sga);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, csr);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma_hw_tcd, biter);
+
+ regset->nregs = reg - regset->regs;
+
+ debugfs_create_regset("tcd", 0444, dir, regset);
+}
+
+static void fsl_edma3_debufs_chan(struct fsl_edma_chan *chan, struct dentry *entry)
+{
+ struct debugfs_regset *regset;
+ struct debugfs_reg *reg;
+ struct device *dev;
+ int be;
+
+ be = chan->edma->big_endian;
+
+ dev = &chan->pdev->dev;
+
+ regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
+ if (!regset)
+ return;
+
+ regset->dev = dev;
+
+ reg = devm_kcalloc(dev, sizeof(struct fsl_edma3_ch_reg)/sizeof(u32),
+ sizeof(*reg), GFP_KERNEL);
+
+ if (!reg)
+ return;
+
+ regset->base = chan->tcd;
+ regset->base -= offsetof(struct fsl_edma3_ch_reg, tcd);
+
+ regset->regs = reg;
+
+ fsl_edma_debugfs_reg(reg, be, fsl_edma3_ch_reg, ch_csr);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma3_ch_reg, ch_es);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma3_ch_reg, ch_int);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma3_ch_reg, ch_sbr);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma3_ch_reg, ch_pri);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma3_ch_reg, ch_mux);
+ fsl_edma_debugfs_reg(reg, be, fsl_edma3_ch_reg, ch_mattr);
+
+ regset->nregs = reg - regset->regs;
+ debugfs_create_regset("regs", 0444, entry, regset);
+
+ fsl_edma_debufs_tcdreg(chan, entry);
+}
+
+static void fsl_edma3_debugfs_init(struct fsl_edma_engine *edma)
+{
+ struct fsl_edma_chan *chan;
+ struct dentry *dir;
+ int i;
+
+ for (i = 0; i < edma->n_chans; i++) {
+ if (edma->chan_masked & BIT(i))
+ continue;
+
+ chan = &edma->chans[i];
+ dir = debugfs_create_dir(chan->chan_name, edma->dma_dev.dbg_dev_root);
+
+ fsl_edma3_debufs_chan(chan, dir);
+ }
+
+}
+
+static void fsl_edma_debugfs_init(struct fsl_edma_engine *edma)
+{
+ struct debugfs_regset *regset;
+ struct fsl_edma_chan *chan;
+ struct debugfs_reg *reg;
+ struct dentry *dir;
+ struct device *dev;
+ int i;
+
+ dev = edma->dma_dev.dev;
+
+ regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
+ if (!regset)
+ return;
+
+ regset->dev = dev;
+
+ reg = devm_kcalloc(dev, sizeof(struct edma_regs)/sizeof(void *), sizeof(*reg), GFP_KERNEL);
+
+ if (!reg)
+ return;
+
+ regset->regs = reg;
+ regset->base = edma->membase;
+
+ fsl_edma_debugfs_regv1(reg, edma, cr);
+ fsl_edma_debugfs_regv1(reg, edma, es);
+ fsl_edma_debugfs_regv1(reg, edma, erqh);
+ fsl_edma_debugfs_regv1(reg, edma, erql);
+ fsl_edma_debugfs_regv1(reg, edma, eeih);
+ fsl_edma_debugfs_regv1(reg, edma, eeil);
+ fsl_edma_debugfs_regv1(reg, edma, seei);
+ fsl_edma_debugfs_regv1(reg, edma, ceei);
+ fsl_edma_debugfs_regv1(reg, edma, serq);
+ fsl_edma_debugfs_regv1(reg, edma, cerq);
+ fsl_edma_debugfs_regv1(reg, edma, cint);
+ fsl_edma_debugfs_regv1(reg, edma, cerr);
+ fsl_edma_debugfs_regv1(reg, edma, ssrt);
+ fsl_edma_debugfs_regv1(reg, edma, cdne);
+ fsl_edma_debugfs_regv1(reg, edma, inth);
+ fsl_edma_debugfs_regv1(reg, edma, errh);
+ fsl_edma_debugfs_regv1(reg, edma, errl);
+
+ regset->nregs = reg - regset->regs;
+
+ debugfs_create_regset("regs", 0444, edma->dma_dev.dbg_dev_root, regset);
+
+ for (i = 0; i < edma->n_chans; i++) {
+ if (edma->chan_masked & BIT(i))
+ continue;
+
+ chan = &edma->chans[i];
+ dir = debugfs_create_dir(chan->chan_name, edma->dma_dev.dbg_dev_root);
+
+ fsl_edma_debufs_tcdreg(chan, dir);
+ }
+}
+
+void fsl_edma_debugfs_on(struct fsl_edma_engine *edma)
+{
+ if (!debugfs_initialized())
+ return;
+
+ debugfs_create_bool("big_endian", 0444, edma->dma_dev.dbg_dev_root, &edma->big_endian);
+ debugfs_create_x64("chan_mask", 0444, edma->dma_dev.dbg_dev_root, &edma->chan_masked);
+ debugfs_create_x32("n_chans", 0444, edma->dma_dev.dbg_dev_root, &edma->n_chans);
+
+ if (edma->drvdata->flags & FSL_EDMA_DRV_SPLIT_REG)
+ fsl_edma3_debugfs_init(edma);
+ else
+ fsl_edma_debugfs_init(edma);
+}
+
+
diff --git a/drivers/dma/fsl-edma-main.c b/drivers/dma/fsl-edma-main.c
index 63d48d046f04..029a72872821 100644
--- a/drivers/dma/fsl-edma-main.c
+++ b/drivers/dma/fsl-edma-main.c
@@ -612,6 +612,8 @@ static int fsl_edma_probe(struct platform_device *pdev)
if (!(drvdata->flags & FSL_EDMA_DRV_SPLIT_REG))
edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr);

+ fsl_edma_debugfs_on(fsl_edma);
+
return 0;
}

--
2.34.1