Skip to contentSkip to conversation

dmaengine: Loongson1: Add Loongson1 dmaengine driver

PATCH RESEND Superseded Keguang Zhang 2 participants 4 messages May 20, 2021 – Jul 5, 2021

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Keguang Zhang

author maintainer
#0 lore raw

PATCH RESEND v4 1/1 dmaengine: Loongson1: Add Loongson1 dmaengine driver

From: Kelvin Cheung <[email protected]>

This patch adds DMA Engine driver for Loongson1B.

---
V3 -> V4:
Use dma_slave_map to find the proper channel. Explicitly call devm_request_irq() and tasklet_kill(). Fix namespace issue.
Some minor fixes and cleanups.
V2 -> V3:
Rename ls1x_dma_filter_fn to ls1x_dma_filter.
V1 -> V2:
Change the config from 'DMA_LOONGSON1' to 'LOONGSON1_DMA', and rearrange it in alphabetical order in Kconfig and Makefile. Fix comment style.

---
 drivers/dma/Kconfig         |   9 +
 drivers/dma/Makefile        |   1 +
 drivers/dma/loongson1-dma.c | 514 ++++++++++++++++++++++++++++++++++++
 3 files changed, 524 insertions(+)
 create mode 100644 drivers/dma/loongson1-dma.c
drivers/dma/Kconfig +9 -0
index 6ab9d9a488a6..b3ceeae61483 100644
@@ -343,6 +343,15 @@ config K3_DMA
 	  Support the DMA engine for Hisilicon K3 platform
 	  devices.
 
+config LOONGSON1_DMA
+	tristate "Loongson1 DMA support"
+	depends on MACH_LOONGSON32
+	select DMA_ENGINE
+	select DMA_VIRTUAL_CHANNELS
+	help
+	  This selects support for the DMA controller in Loongson1 SoCs,
+	  and is required by Loongson1 NAND controller and AC97 support.
+
 config LPC18XX_DMAMUX
 	bool "NXP LPC18xx/43xx DMA MUX for PL080"
 	depends on ARCH_LPC18XX || COMPILE_TEST
drivers/dma/Makefile +1 -0
index aa69094e3547..6d54e485036e 100644
@@ -44,6 +44,7 @@ obj-$(CONFIG_INTEL_IOATDMA) += ioat/
 obj-$(CONFIG_INTEL_IDXD) += idxd/
 obj-$(CONFIG_INTEL_IOP_ADMA) += iop-adma.o
 obj-$(CONFIG_K3_DMA) += k3dma.o
+obj-$(CONFIG_LOONGSON1_DMA) += loongson1-dma.o
 obj-$(CONFIG_LPC18XX_DMAMUX) += lpc18xx-dmamux.o
 obj-$(CONFIG_MILBEAUT_HDMAC) += milbeaut-hdmac.o
 obj-$(CONFIG_MILBEAUT_XDMAC) += milbeaut-xdmac.o
drivers/dma/loongson1-dma.c new file +514 -0

514 lines not on this page · raw message

base-commit: 6f64aa5746d254f2b9b94b837b7862549f2565a0

Signature · 1 line
2.30.2
maintainer
#1 lore raw

drivers/dma/Kconfig config K3_DMA · line 348

Quoted 3 lines · Keguang Zhang
drivers/dma/Kconfig +3 -0
+config LOONGSON1_DMA
+	tristate "Loongson1 DMA support"
+	depends on MACH_LOONGSON32

Why does it have to do that? The dma driver is generic..

drivers/dma/loongson1-dma.c line 126

Quoted 11 lines
drivers/dma/loongson1-dma.c new file +11 -0
+static int ls1x_dma_alloc_chan_resources(struct dma_chan *dchan)
+{
+	struct ls1x_dma_chan *chan = to_ls1x_dma_chan(dchan);
+
+	chan->desc_pool = dma_pool_create(dma_chan_name(dchan),
+					  dchan->device->dev,
+					  sizeof(struct ls1x_dma_lli),
+					  __alignof__(struct ls1x_dma_lli), 0);
+	if (!chan->desc_pool) {
+		dev_err(chan2dev(dchan),
+			"failed to alloc DMA descriptor pool!\n");

This can be dropped, allocators will warn you for the allocation failures

Quoted 16 lines
drivers/dma/loongson1-dma.c new file +16 -0
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
+static void ls1x_dma_free_desc(struct virt_dma_desc *vdesc)
+{
+	struct ls1x_dma_desc *desc = to_ls1x_dma_desc(vdesc);
+
+	if (desc->nr_descs) {
+		unsigned int i = desc->nr_descs;
+		struct ls1x_dma_hwdesc *hwdesc;
+
+		do {
+			hwdesc = &desc->hwdesc[--i];

drivers/dma/loongson1-dma.c line 152

Quoted 10 lines
drivers/dma/loongson1-dma.c new file +10 -0
+			dma_pool_free(desc->chan->desc_pool, hwdesc->lli,
+				      hwdesc->phys);
+		} while (i);
+	}
+
+	kfree(desc);
+}
+
+static struct ls1x_dma_desc *ls1x_dma_alloc_desc(struct ls1x_dma_chan *chan,
+						 int sg_len)

single line now :)

drivers/dma/loongson1-dma.c line 159

Quoted 7 lines
drivers/dma/loongson1-dma.c new file +7 -0
+{
+	struct ls1x_dma_desc *desc;
+	struct dma_chan *dchan = &chan->vchan.chan;
+
+	desc = kzalloc(struct_size(desc, hwdesc, sg_len), GFP_NOWAIT);
+	if (!desc)
+		dev_err(chan2dev(dchan), "failed to alloc DMA descriptor!\n");

this can be dropped too..

Quoted 26 lines
drivers/dma/loongson1-dma.c new file +26 -0
+static struct dma_async_tx_descriptor *
+ls1x_dma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
+		       unsigned int sg_len,
+		       enum dma_transfer_direction direction,
+		       unsigned long flags, void *context)
+{
+	struct ls1x_dma_chan *chan = to_ls1x_dma_chan(dchan);
+	struct dma_slave_config *cfg = &chan->cfg;
+	struct ls1x_dma_desc *desc;
+	struct scatterlist *sg;
+	unsigned int dev_addr, bus_width, cmd, i;
+
+	if (!is_slave_direction(direction)) {
+		dev_err(chan2dev(dchan), "invalid DMA direction!\n");
+		return NULL;
+	}
+
+	dev_dbg(chan2dev(dchan), "sg_len=%d, dir=%s, flags=0x%lx\n", sg_len,
+		direction == DMA_MEM_TO_DEV ? "to device" : "from device",
+		flags);
+
+	switch (direction) {
+	case DMA_MEM_TO_DEV:
+		dev_addr = cfg->dst_addr;
+		bus_width = cfg->dst_addr_width;
+		cmd = LS1X_DMA_RAM2DEV | LS1X_DMA_INT;

drivers/dma/loongson1-dma.c line 199

Quoted 10 lines
drivers/dma/loongson1-dma.c new file +10 -0
+		break;
+	case DMA_DEV_TO_MEM:
+		dev_addr = cfg->src_addr;
+		bus_width = cfg->src_addr_width;
+		cmd = LS1X_DMA_INT;
+		break;
+	default:
+		dev_err(chan2dev(dchan),
+			"unsupported DMA transfer mode! %d\n", direction);
+		return NULL;

will this be ever executed?

drivers/dma/loongson1-dma.c line 267

Quoted 7 lines
drivers/dma/loongson1-dma.c new file +7 -0
+static int ls1x_dma_slave_config(struct dma_chan *dchan,
+				 struct dma_slave_config *config)
+{
+	struct ls1x_dma_chan *chan = to_ls1x_dma_chan(dchan);
+
+	if (!dchan)
+		return -EINVAL;

should this not be checked before you dereference this to get chan

drivers/dma/loongson1-dma.c line 303

Quoted 10 lines
drivers/dma/loongson1-dma.c new file +10 -0
+static void ls1x_dma_trigger(struct ls1x_dma_chan *chan)
+{
+	struct dma_chan *dchan = &chan->vchan.chan;
+	struct ls1x_dma_desc *desc;
+	struct virt_dma_desc *vdesc;
+	unsigned int val;
+
+	vdesc = vchan_next_desc(&chan->vchan);
+	if (!vdesc) {
+		dev_warn(chan2dev(dchan), "No pending descriptor\n");

Hmm, I would not log that... this is called from ls1x_dma_issue_pending() and which can be called from client driver but previous completion would push and this can find empty queue so it can happen quite frequently

drivers/dma/loongson1-dma.c line 339

Quoted 10 lines
drivers/dma/loongson1-dma.c new file +10 -0
+static irqreturn_t ls1x_dma_irq_handler(int irq, void *data)
+{
+	struct ls1x_dma_chan *chan = data;
+	struct dma_chan *dchan = &chan->vchan.chan;
+
+	dev_dbg(chan2dev(dchan), "DMA IRQ %d on channel %d\n", irq, chan->id);
+	if (!chan->desc) {
+		dev_warn(chan2dev(dchan),
+			 "DMA IRQ with no active descriptor on channel %d\n",
+			 chan->id);

single line pls

drivers/dma/loongson1-dma.c line 351

Quoted 12 lines
drivers/dma/loongson1-dma.c new file +12 -0
+		return IRQ_NONE;
+	}
+
+	spin_lock(&chan->vchan.lock);
+
+	if (chan->desc->type == DMA_CYCLIC) {
+		vchan_cyclic_callback(&chan->desc->vdesc);
+	} else {
+		list_del(&chan->desc->vdesc.node);
+		vchan_cookie_complete(&chan->desc->vdesc);
+		chan->desc = NULL;
+	}

not submitting next txn, defeats the purpose of dma if we dont push txns as fast as possible..

drivers/dma/loongson1-dma.c line 496

Quoted 6 lines
drivers/dma/loongson1-dma.c new file +6 -0
+static struct platform_driver ls1x_dma_driver = {
+	.probe	= ls1x_dma_probe,
+	.remove	= ls1x_dma_remove,
+	.driver	= {
+		.name	= "ls1x-dma",
+	},

No device tree?

Signature · 1 line
~Vinod
author maintainer
#2 lore raw
Quoted 8 lines · Vinod Koul <[email protected]> 于2021年6月7日周一 下午7:07写道:

drivers/dma/Kconfig config K3_DMA · line 348

Quoted 3 lines · Keguang Zhang
drivers/dma/Kconfig +3 -0
+config LOONGSON1_DMA
+     tristate "Loongson1 DMA support"
+     depends on MACH_LOONGSON32

Why does it have to do that? The dma driver is generic..

This driver is only available for LOONGSON32 CPUs.

Quoted 15 lines

drivers/dma/loongson1-dma.c line 126

Quoted 12 lines
drivers/dma/loongson1-dma.c new file +11 -0
+static int ls1x_dma_alloc_chan_resources(struct dma_chan *dchan)
+{
+     struct ls1x_dma_chan *chan = to_ls1x_dma_chan(dchan);
+
+     chan->desc_pool = dma_pool_create(dma_chan_name(dchan),
+                                       dchan->device->dev,
+                                       sizeof(struct ls1x_dma_lli),
+                                       __alignof__(struct ls1x_dma_lli), 0);
+     if (!chan->desc_pool) {
+             dev_err(chan2dev(dchan),
+                     "failed to alloc DMA descriptor pool!\n");

This can be dropped, allocators will warn you for the allocation failures

will do.

Quoted 21 lines
Quoted 21 lines
drivers/dma/loongson1-dma.c new file +20 -0
+             return -ENOMEM;
+     }
+
+     return 0;
+}
+
+static void ls1x_dma_free_desc(struct virt_dma_desc *vdesc)
+{
+     struct ls1x_dma_desc *desc = to_ls1x_dma_desc(vdesc);
+
+     if (desc->nr_descs) {
+             unsigned int i = desc->nr_descs;
+             struct ls1x_dma_hwdesc *hwdesc;
+
+             do {
+                     hwdesc = &desc->hwdesc[--i];
+                     dma_pool_free(desc->chan->desc_pool, hwdesc->lli,
+                                   hwdesc->phys);
+             } while (i);
+     }
Quoted 8 lines

drivers/dma/loongson1-dma.c line 152

Quoted 6 lines
drivers/dma/loongson1-dma.c new file +6 -0
+
+     kfree(desc);
+}
+
+static struct ls1x_dma_desc *ls1x_dma_alloc_desc(struct ls1x_dma_chan *chan,
+                                              int sg_len)

single line now :)

will do.

Quoted 10 lines

drivers/dma/loongson1-dma.c line 159

Quoted 8 lines
drivers/dma/loongson1-dma.c new file +7 -0
+{
+     struct ls1x_dma_desc *desc;
+     struct dma_chan *dchan = &chan->vchan.chan;
+
+     desc = kzalloc(struct_size(desc, hwdesc, sg_len), GFP_NOWAIT);
+     if (!desc)
+             dev_err(chan2dev(dchan), "failed to alloc DMA descriptor!\n");

this can be dropped too..

will do.

Quoted 31 lines
Quoted 31 lines
drivers/dma/loongson1-dma.c new file +30 -0
+static struct dma_async_tx_descriptor *
+ls1x_dma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
+                    unsigned int sg_len,
+                    enum dma_transfer_direction direction,
+                    unsigned long flags, void *context)
+{
+     struct ls1x_dma_chan *chan = to_ls1x_dma_chan(dchan);
+     struct dma_slave_config *cfg = &chan->cfg;
+     struct ls1x_dma_desc *desc;
+     struct scatterlist *sg;
+     unsigned int dev_addr, bus_width, cmd, i;
+
+     if (!is_slave_direction(direction)) {
+             dev_err(chan2dev(dchan), "invalid DMA direction!\n");
+             return NULL;
+     }
+
+     dev_dbg(chan2dev(dchan), "sg_len=%d, dir=%s, flags=0x%lx\n", sg_len,
+             direction == DMA_MEM_TO_DEV ? "to device" : "from device",
+             flags);
+
+     switch (direction) {
+     case DMA_MEM_TO_DEV:
+             dev_addr = cfg->dst_addr;
+             bus_width = cfg->dst_addr_width;
+             cmd = LS1X_DMA_RAM2DEV | LS1X_DMA_INT;
+             break;
+     case DMA_DEV_TO_MEM:
+             dev_addr = cfg->src_addr;
+             bus_width = cfg->src_addr_width;
Quoted 8 lines

drivers/dma/loongson1-dma.c line 199

Quoted 6 lines
drivers/dma/loongson1-dma.c new file +6 -0
+             cmd = LS1X_DMA_INT;
+             break;
+     default:
+             dev_err(chan2dev(dchan),
+                     "unsupported DMA transfer mode! %d\n", direction);
+             return NULL;

will this be ever executed?

just in case.

Quoted 10 lines

drivers/dma/loongson1-dma.c line 267

Quoted 8 lines
drivers/dma/loongson1-dma.c new file +7 -0
+static int ls1x_dma_slave_config(struct dma_chan *dchan,
+                              struct dma_slave_config *config)
+{
+     struct ls1x_dma_chan *chan = to_ls1x_dma_chan(dchan);
+
+     if (!dchan)
+             return -EINVAL;

should this not be checked before you dereference this to get chan

will drop this check.

Quoted 16 lines

drivers/dma/loongson1-dma.c line 303

Quoted 11 lines
drivers/dma/loongson1-dma.c new file +10 -0
+static void ls1x_dma_trigger(struct ls1x_dma_chan *chan)
+{
+     struct dma_chan *dchan = &chan->vchan.chan;
+     struct ls1x_dma_desc *desc;
+     struct virt_dma_desc *vdesc;
+     unsigned int val;
+
+     vdesc = vchan_next_desc(&chan->vchan);
+     if (!vdesc) {
+             dev_warn(chan2dev(dchan), "No pending descriptor\n");

Hmm, I would not log that... this is called from ls1x_dma_issue_pending() and which can be called from client driver but previous completion would push and this can find empty queue so it can happen quite frequently

will drop this warning.

Quoted 13 lines

drivers/dma/loongson1-dma.c line 339

Quoted 11 lines
drivers/dma/loongson1-dma.c new file +10 -0
+static irqreturn_t ls1x_dma_irq_handler(int irq, void *data)
+{
+     struct ls1x_dma_chan *chan = data;
+     struct dma_chan *dchan = &chan->vchan.chan;
+
+     dev_dbg(chan2dev(dchan), "DMA IRQ %d on channel %d\n", irq, chan->id);
+     if (!chan->desc) {
+             dev_warn(chan2dev(dchan),
+                      "DMA IRQ with no active descriptor on channel %d\n",
+                      chan->id);

single line pls

will do.

Quoted 16 lines

drivers/dma/loongson1-dma.c line 351

Quoted 13 lines
drivers/dma/loongson1-dma.c new file +12 -0
+             return IRQ_NONE;
+     }
+
+     spin_lock(&chan->vchan.lock);
+
+     if (chan->desc->type == DMA_CYCLIC) {
+             vchan_cyclic_callback(&chan->desc->vdesc);
+     } else {
+             list_del(&chan->desc->vdesc.node);
+             vchan_cookie_complete(&chan->desc->vdesc);
+             chan->desc = NULL;
+     }

not submitting next txn, defeats the purpose of dma if we dont push txns as fast as possible..

will fix it.

Quoted 9 lines

drivers/dma/loongson1-dma.c line 496

Quoted 7 lines
drivers/dma/loongson1-dma.c new file +6 -0
+static struct platform_driver ls1x_dma_driver = {
+     .probe  = ls1x_dma_probe,
+     .remove = ls1x_dma_remove,
+     .driver = {
+             .name   = "ls1x-dma",
+     },

No device tree?

Because the LOONGSON32 platform doesn't support DT yet.

Quoted 3 lines

--
~Vinod

Signature · 3 lines
Best regards,

Kelvin Cheung
maintainer
#3 lore raw
Quoted 11 lines · Kelvin Cheung
Quoted 8 lines · Vinod Koul <[email protected]> 于2021年6月7日周一 下午7:07写道:

drivers/dma/Kconfig config K3_DMA · line 348

Quoted 3 lines · Keguang Zhang
drivers/dma/Kconfig +3 -0
+config LOONGSON1_DMA
+     tristate "Loongson1 DMA support"
+     depends on MACH_LOONGSON32

Why does it have to do that? The dma driver is generic..

This driver is only available for LOONGSON32 CPUs.

the underlaying firmware would ensure this driver is probed if you have such a device, so why have this restriction?

Quoted 10 lines
Quoted 8 lines

drivers/dma/loongson1-dma.c line 496

Quoted 6 lines
drivers/dma/loongson1-dma.c new file +6 -0
+static struct platform_driver ls1x_dma_driver = {
+     .probe  = ls1x_dma_probe,
+     .remove = ls1x_dma_remove,
+     .driver = {
+             .name   = "ls1x-dma",
+     },

No device tree?

Because the LOONGSON32 platform doesn't support DT yet.

Okay so how is the platform device created?

Signature · 1 line
~Vinod