Skip to contentSkip to conversation

Add DMA engine driver for Freescale MPC8xxx processors.

PATCH In review Zhang Wei 2 participants 4 messages Jul 10, 2007 – Jul 12, 2007

Subsystems (2)
lore
Share & tools
lore

Zhang Wei

author
#0 lore raw

PATCH 4/4 Add DMA engine driver for Freescale MPC8xxx processors.

This driver adopts DMA engine API, which could be used for MEM<-->MEM, IO_ADDR<-->MEM and IO_ADDR<-->IO_ADDR data transfer. This driver support both Basic and Extended chain mode of Freescale MPC8xxx DMA controller.

---
 drivers/dma/Kconfig  |    7 +
 drivers/dma/Makefile |    1 +
 drivers/dma/fsldma.c | 1074 ++++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/dma/fsldma.h |  161 ++++++++
 4 files changed, 1243 insertions(+), 0 deletions(-)
 create mode 100644 drivers/dma/fsldma.c
 create mode 100644 drivers/dma/fsldma.h
drivers/dma/Kconfig +7 -0
index 72be6c6..8102a0b 100644
@@ -32,4 +32,11 @@ config INTEL_IOATDMA
 	---help---
 	  Enable support for the Intel(R) I/OAT DMA engine.
 
+config FSL_DMA
+	bool "Freescale MPC8xxx DMA support"
+	depends on DMA_ENGINE && (PPC_86xx || PPC_85xx)
+	---help---
+	  Enable support for the Freescale DMA engine. Now, it support
+	  MPC8xxx processors.
+
 endmenu
drivers/dma/Makefile +1 -0
index bdcfdbd..7a28d5c 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_DMA_ENGINE) += dmaengine.o
 obj-$(CONFIG_NET_DMA) += iovlock.o
 obj-$(CONFIG_INTEL_IOATDMA) += ioatdma.o
+obj-$(CONFIG_FSL_DMA) += fsldma.o
drivers/dma/fsldma.c new file +1074 -0

1,074 lines not on this page · raw message

drivers/dma/fsldma.h new file +161 -0

161 lines not on this page · raw message

Signature · 1 line
1.5.1
#1 lore raw

drivers/dma/Kconfig config INTEL_IOATDMA · line 37

Quoted 3 lines · Zhang Wei
drivers/dma/Kconfig +3 -0
+config FSL_DMA
+	bool "Freescale MPC8xxx DMA support"
+	depends on DMA_ENGINE && (PPC_86xx || PPC_85xx)

Remove the dependency on specific PPC chips... let the device tree say whether the hardware is present.

drivers/dma/fsldma.c line 56

Quoted 5 lines
drivers/dma/fsldma.c new file +5 -0
+static inline int fsl_dma_idle(struct fsl_dma_chan *fsl_chan)
+{
+	return (((in_be32(&fsl_chan->reg_base->sr) & FSL_DMA_SR_CB) == 0) &&
+		((in_be32(&fsl_chan->reg_base->mr) & FSL_DMA_MR_CC) == 0));
+}

I'm still not convinced that there's any reason to check MR_CC, even if the driver *did* set it.

Quoted 6 lines
drivers/dma/fsldma.c new file +6 -0
+	/* We need the descriptor to be aligned to 32bytes
+	 * for meeting FSL DMA specification requirement.
+	 */
+	fsl_chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
+			fsl_chan->device->dev, sizeof(struct fsl_desc_sw),
+			32, 0);

drivers/dma/fsldma.c line 150

Quoted 10 lines
drivers/dma/fsldma.c new file +10 -0
+	if (unlikely(!fsl_chan->desc_pool)) {
+		dev_err(fsl_chan->device->dev, "No memory for channel %d "
+			"descriptor dma pool.\n", fsl_chan->id);
+		return 0;
+	}
+
+	/* Allocate list ring, and form the static list ring */
+	for (i = 0; i < FSLDMA_LD_INIT_RING_SIZE; i++) {
+		desc = fsl_dma_alloc_descriptor(fsl_chan->desc_pool,
+				GFP_KERNEL);

It'd be much simpler to allocate the entire ring at once. No need for linked lists, DMA pools, etc. Just a single dma_alloc_coherent.

In general, this driver seems far more complex than it needs to be.

Review

Quoted 2 lines
(quoted diff) +2 -0
+		switch (fsl_chan->mode) {
+		case FSL_DMA_EXTENDED:

What benefit do we get out of using extended mode? If the driver can do everything it needs to with basic, with no performance penalty, why not always use basic?

Quoted 5 lines
drivers/dma/fsldma.c new file +5 -0
+static dma_cookie_t do_fsl_dma_memcpy(struct fsl_dma_chan *fsl_chan,
+				      dma_addr_t dest,
+				      dma_addr_t src, size_t len,
+				      dma_xfer_callback cb, void *data)
+{

drivers/dma/fsldma.c line 243

Quoted 10 lines
drivers/dma/fsldma.c new file +10 -0
+	struct fsl_desc_sw *first = NULL, *prev = NULL, *list, *new;
+	size_t copy;
+	dma_cookie_t cookie;
+	unsigned long flags;
+	struct fsl_dma_device *fdev = fsl_chan->device;
+	int err = 0;
+	LIST_HEAD(link_chain);
+
+	if (unlikely(!fsl_chan || !dest || !src))
+		return -EFAULT;

-EINVAL for fsl_chan, if you bother checking at all (I wouldn't; it doesn't come from untrusted code. Better to show a nasty-looking oops to bring attention to the problem).

Don't check dest and src against NULL; zero is a potentially valid DMA address.

drivers/dma/fsldma.c line 288

Quoted 13 lines
drivers/dma/fsldma.c new file +13 -0
+		/* Stop the DMA */
+		fsl_dma_halt(fsl_chan);
+		/* Insert the ld descriptor to the LD ring */
+		list_add(&ld->node, fsl_chan->enque);
+		switch (fsl_chan->mode) {
+		case FSL_DMA_EXTENDED:
+			INSERT_LD_RING(fsl_chan, ld, list, next_ls_addr);
+			break;
+		case FSL_DMA_BASIC:
+			INSERT_LD_RING(fsl_chan, ld, link, next_ln_addr);
+			break;
+		}
+		spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);

It'd be nice if we didn't have to stop the DMA in order to insert new descriptors.

drivers/dma/fsldma.c line 295

Quoted 5 lines
drivers/dma/fsldma.c new file +5 -0
+	/* cookie incr and addition to used_list must be atomic */
+	cookie = fsl_chan->common.cookie;
+	cookie++;
+	if (cookie < 0)
+		cookie = 1;

Why not just use the index into the ring as the cookie?

drivers/dma/fsldma.c line 684

Quoted 7 lines
drivers/dma/fsldma.c new file +7 -0
+	stat = in_be32(&fsl_chan->reg_base->sr);
+	dev_dbg(fsl_chan->device->dev, "event: channel %d, stat = 0x%x\n",
+						fsl_chan->id, stat);
+	if (!stat)
+		return IRQ_NONE;
+	busy = stat & (FSL_DMA_SR_CB);
+	stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);

This masking must happen *before* the IRQ_NONE check.

-Scott

#2 lore raw

=20

Quoted 11 lines

-----Original Message-----
From: Wood Scott-B07421=20
=20

drivers/dma/Kconfig config INTEL_IOATDMA · line 37

Quoted 3 lines · Zhang Wei
drivers/dma/Kconfig +3 -0
+config FSL_DMA
+	bool "Freescale MPC8xxx DMA support"
+	depends on DMA_ENGINE && (PPC_86xx || PPC_85xx)

=20
Remove the dependency on specific PPC chips... let the=20 device tree say
whether the hardware is present.

.... Remove it.

Quoted 5 lines

=20

drivers/dma/fsldma.c

Quoted 3 lines
drivers/dma/fsldma.c new file +3 -0
+static inline int fsl_dma_idle(struct fsl_dma_chan *fsl_chan)
+{
+	return (((in_be32(&fsl_chan->reg_base->sr) &=20

FSL_DMA_SR_CB) =3D=3D 0) &&

Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		((in_be32(&fsl_chan->reg_base->mr) &=20

FSL_DMA_MR_CC) =3D=3D 0));

Quoted 1 line

+}

=20
I'm still not convinced that there's any reason to check=20 MR_CC, even if
the driver *did* set it.
=20

Remove it.

Quoted 11 lines

drivers/dma/fsldma.c

Quoted 4 lines
drivers/dma/fsldma.c new file +4 -0
+	/* We need the descriptor to be aligned to 32bytes
+	 * for meeting FSL DMA specification requirement.
+	 */
+	fsl_chan->desc_pool =3D=20

dma_pool_create("fsl_dma_engine_desc_pool",

Review

Quoted 1 line
(quoted diff) +1 -0
+			fsl_chan->device->dev, sizeof(struct=20

fsl_desc_sw),

drivers/dma/fsldma.c

Quoted 3 lines
drivers/dma/fsldma.c new file +3 -0
+			32, 0);
+	if (unlikely(!fsl_chan->desc_pool)) {
+		dev_err(fsl_chan->device->dev, "No memory for=20

channel %d "

Quoted 12 lines

drivers/dma/fsldma.c line 150

Quoted 8 lines
drivers/dma/fsldma.c new file +8 -0
+			"descriptor dma pool.\n", fsl_chan->id);
+		return 0;
+	}
+
+	/* Allocate list ring, and form the static list ring */
+	for (i =3D 0; i < FSLDMA_LD_INIT_RING_SIZE; i++) {
+		desc =3D fsl_dma_alloc_descriptor(fsl_chan->desc_pool,
+				GFP_KERNEL);

=20
It'd be much simpler to allocate the entire ring at once. No need for linked lists, DMA pools, etc. Just a single dma_alloc_coherent. =20

I use a flexible ld ring size here. If there is no more free ring, the driver can add new ld to the ring.

Quoted 11 lines

In general, this driver seems far more complex than it needs to be. =20

Review

Quoted 2 lines
(quoted diff) +2 -0
+		switch (fsl_chan->mode) {
+		case FSL_DMA_EXTENDED:

=20
What benefit do we get out of using extended mode? If the=20 driver can do
everything it needs to with basic, with no performance=20 penalty, why not
always use basic?
=20

Why there are extended mode in silicon? :) Since there are here, we use it.

Quoted 16 lines
Quoted 1 line

+static dma_cookie_t do_fsl_dma_memcpy(struct fsl_dma_chan=20

*fsl_chan,

Quoted 4 lines
drivers/dma/fsldma.c new file +4 -0
+				      dma_addr_t dest,
+				      dma_addr_t src, size_t len,
+				      dma_xfer_callback cb, void *data)
+{

drivers/dma/fsldma.c line 243

Quoted 10 lines
drivers/dma/fsldma.c new file +10 -0
+	struct fsl_desc_sw *first =3D NULL, *prev =3D NULL, *list, *new;
+	size_t copy;
+	dma_cookie_t cookie;
+	unsigned long flags;
+	struct fsl_dma_device *fdev =3D fsl_chan->device;
+	int err =3D 0;
+	LIST_HEAD(link_chain);
+
+	if (unlikely(!fsl_chan || !dest || !src))
+		return -EFAULT;
Quoted 8 lines

=20
-EINVAL for fsl_chan, if you bother checking at all (I wouldn't; it doesn't come from untrusted code. Better to show a=20 nasty-looking oops to
bring attention to the problem).
=20
Don't check dest and src against NULL; zero is a potentially valid DMA address.

Ok. Do not check dest and src with NULL value. Hope the caller know what he do.:)

Quoted 13 lines

=20

drivers/dma/fsldma.c

Quoted 7 lines
drivers/dma/fsldma.c new file +7 -0
+		/* Stop the DMA */
+		fsl_dma_halt(fsl_chan);
+		/* Insert the ld descriptor to the LD ring */
+		list_add(&ld->node, fsl_chan->enque);
+		switch (fsl_chan->mode) {
+		case FSL_DMA_EXTENDED:
+			INSERT_LD_RING(fsl_chan, ld, list,=20

next_ls_addr);

drivers/dma/fsldma.c

Quoted 3 lines
drivers/dma/fsldma.c new file +3 -0
+			break;
+		case FSL_DMA_BASIC:
+			INSERT_LD_RING(fsl_chan, ld, link,=20

next_ln_addr);

Quoted 6 lines

drivers/dma/fsldma.c line 219

Quoted 3 lines
drivers/dma/fsldma.c new file +3 -0
+			break;
+		}
+		spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);

=20
It'd be nice if we didn't have to stop the DMA in order to insert new descriptors.

Since there is only happen at no more free ld in the ring. We need to break the ld-ring and add new ld to it. So the most safe operation is to stop DMA controller.

Quoted 8 lines

=20

drivers/dma/fsldma.c

Quoted 5 lines
drivers/dma/fsldma.c new file +5 -0
+	/* cookie incr and addition to used_list must be atomic */
+	cookie =3D fsl_chan->common.cookie;
+	cookie++;
+	if (cookie < 0)
+		cookie =3D 1;

=20
Why not just use the index into the ring as the cookie?

The cookie will record all transfer by increased number. The ring index is less for all transfer.

Quoted 4 lines

=20

Review

Quoted 2 lines
(quoted diff) +2 -0
+	stat =3D in_be32(&fsl_chan->reg_base->sr);
+	dev_dbg(fsl_chan->device->dev, "event: channel %d, stat=20

=3D 0x%x\n",

Quoted 8 lines

drivers/dma/fsldma.c

Quoted 5 lines
drivers/dma/fsldma.c new file +5 -0
+						fsl_chan->id, stat);
+	if (!stat)
+		return IRQ_NONE;
+	busy =3D stat & (FSL_DMA_SR_CB);
+	stat &=3D ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);

=20
This masking must happen *before* the IRQ_NONE check. =20

Really? FSL_DMA_SR_CH is also a stat of event. And I need the FSL_DMA_SR_CB status.

Thanks!
Wei.

#3 lore raw
Quoted 7 lines · Zhang Wei-r63237 wrote:
Quoted 3 lines

It'd be much simpler to allocate the entire ring at once. No need for linked lists, DMA pools, etc. Just a single dma_alloc_coherent.

I use a flexible ld ring size here. If there is no more free ring, the driver can add new ld to the ring.

I don't think that's worth all the complexity that it adds. Most drivers make do just fine with a fixed-size ring.

Quoted 6 lines
Quoted 4 lines

What benefit do we get out of using extended mode? If the driver can do everything it needs to with basic, with no performance penalty, why not always use basic?

Why there are extended mode in silicon? :)

I've been wondering that myself. :-)

Quoted 1 line

Since there are here, we use it.

"Because it's there" really isn't a good reason for adding complexity to the code.

Quoted 6 lines
Quoted 2 lines

It'd be nice if we didn't have to stop the DMA in order to insert new descriptors.

Since there is only happen at no more free ld in the ring. We need to break the ld-ring and add new ld to it.

Or you could just block until a previous descriptor completes.

Quoted 5 lines
Quoted 1 line

Why not just use the index into the ring as the cookie?

The cookie will record all transfer by increased number. The ring index is less for all transfer.

I'm not sure I follow. All you need is a way to identify a currently used descriptor, right?

Quoted 9 lines
Quoted 7 lines

drivers/dma/fsldma.c line 684

Quoted 5 lines
drivers/dma/fsldma.c new file +5 -0
+						fsl_chan->id, stat);
+	if (!stat)
+		return IRQ_NONE;
+	busy = stat & (FSL_DMA_SR_CB);
+	stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);

This masking must happen *before* the IRQ_NONE check.

Really? FSL_DMA_SR_CH is also a stat of event.

Does the IRQ handler always clear the bit? If not, then it doesn't count.

Quoted 1 line

And I need the FSL_DMA_SR_CB status.

The busy assignment can still happen before the mask... just move the IRQ_NONE check down.

-Scott