mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Denis Orlov <denorl2009@gmail.com>,
	str@pengutronix.de, lst@pengutronix.de,
	Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/4] mci: stm32_sdmmc2: correct usage of DMA API
Date: Wed, 29 Nov 2023 07:17:58 +0100	[thread overview]
Message-ID: <20231129061758.1781732-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20231129061758.1781732-1-a.fatoum@pengutronix.de>

The new CONFIG_DMA_API_DEBUG option correctly detects that
dma_sync_single_for_device is called without dma_map_single.

In the particular case of the STM32 SDMMC2 driver, this shouldn't lead
to errors as dma_sync_single_for_cpu is only called after successful
DMA, not before. In other cases though, dirty cache lines may be evicted
and written back to cache, just before dma_sync_single_for_cpu,
resulting in memory corruption.

Switch to using dma_map_single to fix this.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/mci/stm32_sdmmc2.c | 41 ++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/drivers/mci/stm32_sdmmc2.c b/drivers/mci/stm32_sdmmc2.c
index 1bfef1ccf0eb..84969a29d0f4 100644
--- a/drivers/mci/stm32_sdmmc2.c
+++ b/drivers/mci/stm32_sdmmc2.c
@@ -257,11 +257,12 @@ static void stm32_sdmmc2_pwron(struct stm32_sdmmc2_priv *priv)
 	udelay(DIV_ROUND_UP(74 * USEC_PER_SEC, priv->mci.clock));
 }
 
-static void stm32_sdmmc2_start_data(struct stm32_sdmmc2_priv *priv,
+static int stm32_sdmmc2_start_data(struct stm32_sdmmc2_priv *priv,
 				    struct mci_data *data, u32 data_length)
 {
 	unsigned int num_bytes = data->blocks * data->blocksize;
-	u32 data_ctrl, idmabase0;
+	dma_addr_t idmabase0;
+	u32 data_ctrl;
 
 	/* Configure the SDMMC DPSM (Data Path State Machine) */
 	data_ctrl = (__ilog2_u32(data->blocksize) <<
@@ -270,27 +271,27 @@ static void stm32_sdmmc2_start_data(struct stm32_sdmmc2_priv *priv,
 
 	if (data->flags & MMC_DATA_READ) {
 		data_ctrl |= SDMMC_DCTRL_DTDIR;
-		idmabase0 = (u32)data->dest;
+		idmabase0 = dma_map_single(priv->dev, (void *)data->src, num_bytes,
+					   DMA_FROM_DEVICE);
 	} else {
-		idmabase0 = (u32)data->src;
+		idmabase0 = dma_map_single(priv->dev, (void *)data->src, num_bytes,
+					   DMA_TO_DEVICE);
 	}
 
+	if (dma_mapping_error(priv->dev, idmabase0))
+		return DMA_ERROR_CODE;
+
 	/* Set the SDMMC DataLength value */
 	writel(data_length, priv->base + SDMMC_DLEN);
 
 	/* Write to SDMMC DCTRL */
 	writel(data_ctrl, priv->base + SDMMC_DCTRL);
 
-	if (data->flags & MMC_DATA_WRITE)
-		dma_sync_single_for_device(priv->dev, (unsigned long)idmabase0,
-					   num_bytes, DMA_TO_DEVICE);
-	else
-		dma_sync_single_for_device(priv->dev, (unsigned long)idmabase0,
-					   num_bytes, DMA_FROM_DEVICE);
-
 	/* Enable internal DMA */
 	writel(idmabase0, priv->base + SDMMC_IDMABASE0);
 	writel(SDMMC_IDMACTRL_IDMAEN, priv->base + SDMMC_IDMACTRL);
+
+	return idmabase0;
 }
 
 static void stm32_sdmmc2_start_cmd(struct stm32_sdmmc2_priv *priv,
@@ -415,7 +416,8 @@ static int stm32_sdmmc2_end_cmd(struct stm32_sdmmc2_priv *priv,
 
 static int stm32_sdmmc2_end_data(struct stm32_sdmmc2_priv *priv,
 				 struct mci_cmd *cmd,
-				 struct mci_data *data)
+				 struct mci_data *data,
+				 dma_addr_t dma_addr)
 {
 	u32 mask = SDMMC_STA_DCRCFAIL | SDMMC_STA_DTIMEOUT |
 		   SDMMC_STA_IDMATE | SDMMC_STA_DATAEND;
@@ -436,12 +438,10 @@ static int stm32_sdmmc2_end_data(struct stm32_sdmmc2_priv *priv,
 		return ret;
 	}
 
-	if (data->flags & MMC_DATA_WRITE)
-		dma_sync_single_for_cpu(priv->dev, (unsigned long)data->src,
-					num_bytes, DMA_TO_DEVICE);
+	if (data->flags & MMC_DATA_READ)
+		dma_unmap_single(priv->dev, dma_addr, num_bytes, DMA_FROM_DEVICE);
 	else
-		dma_sync_single_for_cpu(priv->dev, (unsigned long)data->dest,
-					num_bytes, DMA_FROM_DEVICE);
+		dma_unmap_single(priv->dev, dma_addr, num_bytes, DMA_TO_DEVICE);
 
 	if (status & SDMMC_STA_DCRCFAIL) {
 		dev_err(priv->dev, "error SDMMC_STA_DCRCFAIL (0x%x) for cmd %d\n",
@@ -481,12 +481,15 @@ static int stm32_sdmmc2_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 {
 	struct stm32_sdmmc2_priv *priv = to_mci_host(mci);
 	u32 cmdat = data ? SDMMC_CMD_CMDTRANS : 0;
+	dma_addr_t dma_addr = DMA_ERROR_CODE;
 	u32 data_length = 0;
 	int ret;
 
 	if (data) {
 		data_length = data->blocks * data->blocksize;
-		stm32_sdmmc2_start_data(priv, data, data_length);
+		dma_addr = stm32_sdmmc2_start_data(priv, data, data_length);
+		if (dma_addr == DMA_ERROR_CODE)
+			return -EFAULT;
 	}
 
 	stm32_sdmmc2_start_cmd(priv, cmd, cmdat, data_length);
@@ -497,7 +500,7 @@ static int stm32_sdmmc2_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 	ret = stm32_sdmmc2_end_cmd(priv, cmd);
 
 	if (data && !ret)
-		ret = stm32_sdmmc2_end_data(priv, cmd, data);
+		ret = stm32_sdmmc2_end_data(priv, cmd, data, dma_addr);
 
 	/* Clear flags */
 	writel(SDMMC_ICR_STATIC_FLAGS, priv->base + SDMMC_ICR);
-- 
2.39.2




  parent reply	other threads:[~2023-11-29  6:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-29  6:17 [PATCH 0/4] dma: catch mistakes with CONFIG_DMA_API_DEBUG Ahmad Fatoum
2023-11-29  6:17 ` [PATCH 1/4] dma: factor out dma map generic implementations into file Ahmad Fatoum
2023-12-05  8:37   ` Sascha Hauer
2023-12-05  8:42     ` Ahmad Fatoum
2023-11-29  6:17 ` [PATCH 2/4] dma: add DMA API debugging support Ahmad Fatoum
2023-11-29  6:17 ` [PATCH 3/4] mci: core: remove broken, unneeded write bounce buffer Ahmad Fatoum
2023-11-29  6:17 ` Ahmad Fatoum [this message]
2023-12-05  7:52 ` [PATCH 0/4] dma: catch mistakes with CONFIG_DMA_API_DEBUG Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231129061758.1781732-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=denorl2009@gmail.com \
    --cc=lst@pengutronix.de \
    --cc=str@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox