Skip to contentSkip to conversation

mmc: add DMA SG synchronisation

PATCH New Guennadi Liakhovetski 3 participants 8 messages Jan 31, 2014 – Feb 6, 2014

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover mmc: add DMA SG synchronisation

Hi

This is something, that seems to be missing from all mmc host drivers. At least all except sdhci and, possibly, mmc-spi, but sdhci uses its own DMA engine, and mmc-spi does DMA synchronisation in a somewhat strange way - before and after SPI synchronisation for both data transfer directions. Otherwise all other drivers map and unmap SG lists, perform DMA transfers, but never call dma_sync_*_for_*(). Apparantly, this works, but IIUC, this violates the DMA API. I'm attaching a couple of patches for a couple of those drivers for consideration. If this is confirmed to be the right thing to do, I could do more of those.

Thanks
Guennadi
---
Guennadi Liakhovetski, Ph.D.
Freelance Open-Source Software Developer
http://www.open-technology.de/

#1 lore raw

PATCH 1/3 mmc: tmio-mmc: add DMA SG synchronisation

According to the DMA API data has to be synchronised before starting a DMA transfer to device and after completing a DMA transfer from device.

---
 drivers/mmc/host/tmio_mmc_dma.c |   21 ++++++++++++---------
 1 files changed, 12 insertions(+), 9 deletions(-)
drivers/mmc/host/tmio_mmc_dma.c +12 -9
index 65edb4a..a997b94 100644
@@ -168,9 +168,12 @@ static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
 	}
 
 	ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
-	if (ret > 0)
+	if (ret > 0) {
+		dma_sync_sg_for_device(chan->device->dev, sg, host->sg_len,
+				       DMA_TO_DEVICE);
 		desc = dmaengine_prep_slave_sg(chan, sg, ret,
 			DMA_MEM_TO_DEV, DMA_CTRL_ACK);
+	}
 
 	if (desc) {
 		cookie = dmaengine_submit(desc);
@@ -247,14 +250,14 @@ static void tmio_mmc_tasklet_fn(unsigned long arg)
 	if (!host->data)
 		goto out;
 
-	if (host->data->flags & MMC_DATA_READ)
-		dma_unmap_sg(host->chan_rx->device->dev,
-			     host->sg_ptr, host->sg_len,
-			     DMA_FROM_DEVICE);
-	else
-		dma_unmap_sg(host->chan_tx->device->dev,
-			     host->sg_ptr, host->sg_len,
-			     DMA_TO_DEVICE);
+	if (host->data->flags & MMC_DATA_READ) {
+		struct device *dev = host->chan_rx->device->dev;
+		dma_unmap_sg(dev, host->sg_ptr, host->sg_len, DMA_FROM_DEVICE);
+		dma_sync_sg_for_cpu(dev, host->sg_ptr, host->sg_len, DMA_FROM_DEVICE);
+	} else {
+		struct device *dev = host->chan_tx->device->dev;
+		dma_unmap_sg(dev, host->sg_ptr, host->sg_len, DMA_TO_DEVICE);
+	}
 
 	tmio_mmc_do_data_irq(host);
 out:
Signature · 1 line
1.7.2.5
#2 lore raw

PATCH 1/3 mmc: tmio-mmc: add DMA SG synchronisation

Quoted 4 lines · Guennadi Liakhovetski

According to the DMA API data has to be synchronised before starting a DMA transfer to device and after completing a DMA transfer from device.

Quoted 53 lines
---
 drivers/mmc/host/tmio_mmc_dma.c |   21 ++++++++++++---------
 1 files changed, 12 insertions(+), 9 deletions(-)
drivers/mmc/host/tmio_mmc_dma.c +12 -9
index 65edb4a..a997b94 100644
@@ -168,9 +168,12 @@ static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
 	}
 
 	ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
-	if (ret > 0)
+	if (ret > 0) {
+		dma_sync_sg_for_device(chan->device->dev, sg, host->sg_len,
+				       DMA_TO_DEVICE);
 		desc = dmaengine_prep_slave_sg(chan, sg, ret,
 			DMA_MEM_TO_DEV, DMA_CTRL_ACK);
+	}
 
 	if (desc) {
 		cookie = dmaengine_submit(desc);
@@ -247,14 +250,14 @@ static void tmio_mmc_tasklet_fn(unsigned long arg)
 	if (!host->data)
 		goto out;
 
-	if (host->data->flags & MMC_DATA_READ)
-		dma_unmap_sg(host->chan_rx->device->dev,
-			     host->sg_ptr, host->sg_len,
-			     DMA_FROM_DEVICE);
-	else
-		dma_unmap_sg(host->chan_tx->device->dev,
-			     host->sg_ptr, host->sg_len,
-			     DMA_TO_DEVICE);
+	if (host->data->flags & MMC_DATA_READ) {
+		struct device *dev = host->chan_rx->device->dev;
+		dma_unmap_sg(dev, host->sg_ptr, host->sg_len, DMA_FROM_DEVICE);
+		dma_sync_sg_for_cpu(dev, host->sg_ptr, host->sg_len, DMA_FROM_DEVICE);
+	} else {
+		struct device *dev = host->chan_tx->device->dev;
+		dma_unmap_sg(dev, host->sg_ptr, host->sg_len, DMA_TO_DEVICE);
+	}
 
 	tmio_mmc_do_data_irq(host);
 out:
Signature · 6 lines
1.7.2.5

--
To unsubscribe from this list: send the line "unsubscribe linux-sh" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

PATCH 1/3 mmc: tmio-mmc: add DMA SG synchronisation

In reply to Guennadi Liakhovetski
Quoted 4 lines · Guennadi Liakhovetski

According to the DMA API data has to be synchronised before starting a DMA transfer to device and after completing a DMA transfer from device.

I've got 1/3 and 2/3, but not 3/3 ?

Signature · 2 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius
#4 lore raw

Re: [PATCH 1/3] mmc: tmio-mmc: add DMA SG synchronisation

Hi Ben

Quoted 7 lines · Ben Dooks
Quoted 4 lines · Guennadi Liakhovetski

According to the DMA API data has to be synchronised before starting a DMA transfer to device and after completing a DMA transfer from device.

I've got 1/3 and 2/3, but not 3/3 ?

Unfortunately that thread didn't make it to the ALKML - all patches bounced, so, they only arrived to sh and mmc. (A day before and a couple of hours later posting to ALKML worked just fine, just that one time...). You can view the whole thread here
http://thread.gmane.org/gmane.linux.kernel.mmc/24969/focus=24972

Thanks
Guennadi
---
Guennadi Liakhovetski, Ph.D.
Freelance Open-Source Software Developer
http://www.open-technology.de/

#5 lore raw

PATCH 2/3 mmc: sh-mmcif: add DMA SG synchronisation

In reply to Guennadi Liakhovetski

According to the DMA API data has to be synchronised before starting a DMA transfer to device and after completing a DMA transfer from device.

---
 drivers/mmc/host/sh_mmcif.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)
drivers/mmc/host/sh_mmcif.c +10 -8
index d032b08..902780c 100644
@@ -345,6 +345,8 @@ static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
 			 DMA_TO_DEVICE);
 	if (ret > 0) {
 		host->dma_active = true;
+		dma_sync_sg_for_device(chan->device->dev, sg, data->sg_len,
+				       DMA_TO_DEVICE);
 		desc = dmaengine_prep_slave_sg(chan, sg, ret,
 			DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 	}
@@ -1118,14 +1120,14 @@ static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
 	time = wait_for_completion_interruptible_timeout(&host->dma_complete,
 							 host->timeout);
 
-	if (data->flags & MMC_DATA_READ)
-		dma_unmap_sg(host->chan_rx->device->dev,
-			     data->sg, data->sg_len,
-			     DMA_FROM_DEVICE);
-	else
-		dma_unmap_sg(host->chan_tx->device->dev,
-			     data->sg, data->sg_len,
-			     DMA_TO_DEVICE);
+	if (data->flags & MMC_DATA_READ) {
+		struct device *dev = host->chan_rx->device->dev;
+		dma_unmap_sg(dev, data->sg, data->sg_len, DMA_FROM_DEVICE);
+		dma_sync_sg_for_cpu(dev, data->sg, data->sg_len, DMA_FROM_DEVICE);
+	} else {
+		struct device *dev = host->chan_tx->device->dev;
+		dma_unmap_sg(dev, data->sg, data->sg_len, DMA_TO_DEVICE);
+	}
 
 	if (host->sd_error) {
 		dev_err(host->mmc->parent,
Signature · 1 line
1.7.2.5
#6 lore raw

PATCH 2/3 mmc: sh-mmcif: add DMA SG synchronisation

Quoted 4 lines · Guennadi Liakhovetski

According to the DMA API data has to be synchronised before starting a DMA transfer to device and after completing a DMA transfer from device.

Quoted 48 lines
---
 drivers/mmc/host/sh_mmcif.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)
drivers/mmc/host/sh_mmcif.c +10 -8
index d032b08..902780c 100644
@@ -345,6 +345,8 @@ static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
 			 DMA_TO_DEVICE);
 	if (ret > 0) {
 		host->dma_active = true;
+		dma_sync_sg_for_device(chan->device->dev, sg, data->sg_len,
+				       DMA_TO_DEVICE);
 		desc = dmaengine_prep_slave_sg(chan, sg, ret,
 			DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 	}
@@ -1118,14 +1120,14 @@ static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
 	time = wait_for_completion_interruptible_timeout(&host->dma_complete,
 							 host->timeout);
 
-	if (data->flags & MMC_DATA_READ)
-		dma_unmap_sg(host->chan_rx->device->dev,
-			     data->sg, data->sg_len,
-			     DMA_FROM_DEVICE);
-	else
-		dma_unmap_sg(host->chan_tx->device->dev,
-			     data->sg, data->sg_len,
-			     DMA_TO_DEVICE);
+	if (data->flags & MMC_DATA_READ) {
+		struct device *dev = host->chan_rx->device->dev;
+		dma_unmap_sg(dev, data->sg, data->sg_len, DMA_FROM_DEVICE);
+		dma_sync_sg_for_cpu(dev, data->sg, data->sg_len, DMA_FROM_DEVICE);
+	} else {
+		struct device *dev = host->chan_tx->device->dev;
+		dma_unmap_sg(dev, data->sg, data->sg_len, DMA_TO_DEVICE);
+	}
 
 	if (host->sd_error) {
 		dev_err(host->mmc->parent,
Signature · 6 lines
1.7.2.5

--
To unsubscribe from this list: send the line "unsubscribe linux-sh" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#7 lore raw

PATCH 3/3 mmc: mxcmmc: add DMA SG synchronisation

In reply to Guennadi Liakhovetski

According to the DMA API data has to be synchronised before starting a DMA transfer to device and after completing a DMA transfer from device.

---
 drivers/mmc/host/mxcmmc.c |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)
drivers/mmc/host/mxcmmc.c +12 -6
index f7199c8..31dfc7b 100644
@@ -326,6 +326,7 @@ static inline void mxcmci_swap_buffers(struct mmc_data *data) {}
 
 static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
 {
+	struct device *dev = host->dma->device->dev;
 	unsigned int nob = data->blocks;
 	unsigned int blksz = data->blksz;
 	unsigned int datasize = nob * blksz;
@@ -363,18 +364,20 @@ static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
 		mxcmci_swap_buffers(data);
 	}
 
-	nents = dma_map_sg(host->dma->device->dev, data->sg,
-				     data->sg_len,  host->dma_dir);
+	nents = dma_map_sg(dev, data->sg, data->sg_len,  host->dma_dir);
 	if (nents != data->sg_len)
 		return -EINVAL;
 
+	if (data->flags & MMC_DATA_WRITE)
+		dma_sync_sg_for_device(dev, data->sg, data->sg_len,
+				       host->dma_dir);
+
 	host->desc = dmaengine_prep_slave_sg(host->dma,
 		data->sg, data->sg_len, slave_dirn,
 		DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 
 	if (!host->desc) {
-		dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
-				host->dma_dir);
+		dma_unmap_sg(dev, data->sg, data->sg_len, host->dma_dir);
 		host->do_dma = 0;
 		return 0; /* Fall back to PIO */
 	}
@@ -487,8 +490,11 @@ static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
 	int data_error;
 
 	if (mxcmci_use_dma(host)) {
-		dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
-				host->dma_dir);
+		struct device *dev = host->dma->device->dev;
+		dma_unmap_sg(dev, data->sg, data->sg_len, host->dma_dir);
+		if (host->dma_dir == DMA_FROM_DEVICE)
+			dma_sync_sg_for_cpu(dev, data->sg, data->sg_len,
+					    host->dma_dir);
 		mxcmci_swap_buffers(data);
 	}
 
Signature · 1 line
1.7.2.5