linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] Fix STM32 I2C dma operations
@ 2025-06-30 12:55 Clément Le Goffic
  2025-06-30 12:55 ` [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Clément Le Goffic @ 2025-06-30 12:55 UTC (permalink / raw)
  To: Pierre-Yves MORDRET, Alain Volmat, Andi Shyti, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang
  Cc: Pierre-Yves MORDRET, linux-i2c, linux-stm32, linux-arm-kernel,
	linux-kernel, linux-media, dri-devel, linaro-mm-sig,
	Clément Le Goffic

This patch series aims to fix some issues inside the driver's DMA
handling.
It also uses newer I2C DMA API.

Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
---
Changes in v3:
- Add Alain Volmat's "Acked-by" on patch 1 and 2
- Link to v2: https://lore.kernel.org/r/20250627-i2c-upstream-v2-0-8c14523481dc@foss.st.com

Changes in v2:
- Fix the dev used in dma_unmap also in the error path of
  `stm32_i2c_prep_dma_xfer`
- Add a dma_unmap_single also in the ITs error handler
- Add Alain Volmat's "Acked-by" on patch 3
- Link to v1: https://lore.kernel.org/r/20250616-i2c-upstream-v1-0-42d3d5374e65@foss.st.com

---
Clément Le Goffic (3):
      i2c: stm32: fix the device used for the DMA map
      i2c: stm32f7: unmap DMA mapped buffer
      i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs

 drivers/i2c/busses/i2c-stm32.c   |  4 ++--
 drivers/i2c/busses/i2c-stm32f7.c | 42 +++++++++++++++++++++++++++++-----------
 2 files changed, 33 insertions(+), 13 deletions(-)
---
base-commit: d0b3b7b22dfa1f4b515fd3a295b3fd958f9e81af
change-id: 20250527-i2c-upstream-e5b69501359a

Best regards,
-- 
Clément Le Goffic <clement.legoffic@foss.st.com>


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map
  2025-06-30 12:55 [PATCH v3 0/3] Fix STM32 I2C dma operations Clément Le Goffic
@ 2025-06-30 12:55 ` Clément Le Goffic
  2025-06-30 13:05   ` Pierre Yves MORDRET
  2025-07-02 16:57   ` Andi Shyti
  2025-06-30 12:55 ` [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
  2025-06-30 12:55 ` [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
  2 siblings, 2 replies; 13+ messages in thread
From: Clément Le Goffic @ 2025-06-30 12:55 UTC (permalink / raw)
  To: Pierre-Yves MORDRET, Alain Volmat, Andi Shyti, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang
  Cc: Pierre-Yves MORDRET, linux-i2c, linux-stm32, linux-arm-kernel,
	linux-kernel, linux-media, dri-devel, linaro-mm-sig,
	Clément Le Goffic

If the DMA mapping failed, it produced an error log with the wrong
device name:
"stm32-dma3 40400000.dma-controller: rejecting DMA map of vmalloc memory"
Fix this issue by replacing the dev with the I2C dev.

Fixes: bb8822cbbc53 ("i2c: i2c-stm32: Add generic DMA API")
Acked-by: Alain Volmat <alain.volmat@foss.st.com>
Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
---
 drivers/i2c/busses/i2c-stm32.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/i2c/busses/i2c-stm32.c b/drivers/i2c/busses/i2c-stm32.c
index 157c64e27d0b..272a5dffb08f 100644
--- a/drivers/i2c/busses/i2c-stm32.c
+++ b/drivers/i2c/busses/i2c-stm32.c
@@ -118,7 +118,7 @@ int stm32_i2c_prep_dma_xfer(struct device *dev, struct stm32_i2c_dma *dma,
 	dma->dma_len = len;
 	chan_dev = dma->chan_using->device->dev;
 
-	dma->dma_buf = dma_map_single(chan_dev, buf, dma->dma_len,
+	dma->dma_buf = dma_map_single(dev, buf, dma->dma_len,
 				      dma->dma_data_dir);
 	if (dma_mapping_error(chan_dev, dma->dma_buf)) {
 		dev_err(dev, "DMA mapping failed\n");
@@ -150,7 +150,7 @@ int stm32_i2c_prep_dma_xfer(struct device *dev, struct stm32_i2c_dma *dma,
 	return 0;
 
 err:
-	dma_unmap_single(chan_dev, dma->dma_buf, dma->dma_len,
+	dma_unmap_single(dev, dma->dma_buf, dma->dma_len,
 			 dma->dma_data_dir);
 	return ret;
 }

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer
  2025-06-30 12:55 [PATCH v3 0/3] Fix STM32 I2C dma operations Clément Le Goffic
  2025-06-30 12:55 ` [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
@ 2025-06-30 12:55 ` Clément Le Goffic
  2025-06-30 13:06   ` Pierre Yves MORDRET
  2025-07-02 17:08   ` Andi Shyti
  2025-06-30 12:55 ` [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
  2 siblings, 2 replies; 13+ messages in thread
From: Clément Le Goffic @ 2025-06-30 12:55 UTC (permalink / raw)
  To: Pierre-Yves MORDRET, Alain Volmat, Andi Shyti, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang
  Cc: Pierre-Yves MORDRET, linux-i2c, linux-stm32, linux-arm-kernel,
	linux-kernel, linux-media, dri-devel, linaro-mm-sig,
	Clément Le Goffic

Fix an issue where the mapped DMA buffer was not unmapped.

Fixes: 7ecc8cfde553 ("i2c: i2c-stm32f7: Add DMA support")
Acked-by: Alain Volmat <alain.volmat@foss.st.com>
Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
---
 drivers/i2c/busses/i2c-stm32f7.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
index e4aaeb2262d0..042386b4cabe 100644
--- a/drivers/i2c/busses/i2c-stm32f7.c
+++ b/drivers/i2c/busses/i2c-stm32f7.c
@@ -1554,6 +1554,8 @@ static irqreturn_t stm32f7_i2c_handle_isr_errs(struct stm32f7_i2c_dev *i2c_dev,
 	if (i2c_dev->use_dma) {
 		stm32f7_i2c_disable_dma_req(i2c_dev);
 		dmaengine_terminate_async(dma->chan_using);
+		dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
+				 dma->dma_data_dir);
 	}
 
 	i2c_dev->master_mode = false;
@@ -1622,6 +1624,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
 		if (i2c_dev->use_dma) {
 			stm32f7_i2c_disable_dma_req(i2c_dev);
 			dmaengine_terminate_async(dma->chan_using);
+			dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
+					 dma->dma_data_dir);
 		}
 		f7_msg->result = -ENXIO;
 	}
@@ -1642,6 +1646,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
 				dev_dbg(i2c_dev->dev, "<%s>: Timed out\n", __func__);
 				stm32f7_i2c_disable_dma_req(i2c_dev);
 				dmaengine_terminate_async(dma->chan_using);
+				dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
+						 dma->dma_data_dir);
 				f7_msg->result = -ETIMEDOUT;
 			}
 		}

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs
  2025-06-30 12:55 [PATCH v3 0/3] Fix STM32 I2C dma operations Clément Le Goffic
  2025-06-30 12:55 ` [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
  2025-06-30 12:55 ` [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
@ 2025-06-30 12:55 ` Clément Le Goffic
  2025-06-30 13:07   ` Pierre Yves MORDRET
  2025-07-02 17:15   ` Andi Shyti
  2 siblings, 2 replies; 13+ messages in thread
From: Clément Le Goffic @ 2025-06-30 12:55 UTC (permalink / raw)
  To: Pierre-Yves MORDRET, Alain Volmat, Andi Shyti, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang
  Cc: Pierre-Yves MORDRET, linux-i2c, linux-stm32, linux-arm-kernel,
	linux-kernel, linux-media, dri-devel, linaro-mm-sig,
	Clément Le Goffic

Use the i2c-core-base APIs to allocate a DMA safe buffer when needed.

Acked-by: Alain Volmat <alain.volmat@foss.st.com>
Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
---
 drivers/i2c/busses/i2c-stm32f7.c | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
index 042386b4cabe..d06f0efdece3 100644
--- a/drivers/i2c/busses/i2c-stm32f7.c
+++ b/drivers/i2c/busses/i2c-stm32f7.c
@@ -742,9 +742,12 @@ static void stm32f7_i2c_dma_callback(void *arg)
 	struct stm32f7_i2c_dev *i2c_dev = (struct stm32f7_i2c_dev *)arg;
 	struct stm32_i2c_dma *dma = i2c_dev->dma;
 	struct device *dev = dma->chan_using->device->dev;
+	struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
 
 	stm32f7_i2c_disable_dma_req(i2c_dev);
 	dma_unmap_single(dev, dma->dma_buf, dma->dma_len, dma->dma_data_dir);
+	if (!f7_msg->smbus)
+		i2c_put_dma_safe_msg_buf(f7_msg->buf, i2c_dev->msg, true);
 	complete(&dma->dma_complete);
 }
 
@@ -880,6 +883,7 @@ static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev,
 {
 	struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
 	void __iomem *base = i2c_dev->base;
+	u8 *dma_buf;
 	u32 cr1, cr2;
 	int ret;
 
@@ -929,17 +933,23 @@ static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev,
 
 	/* Configure DMA or enable RX/TX interrupt */
 	i2c_dev->use_dma = false;
-	if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN
-	    && !i2c_dev->atomic) {
-		ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma,
-					      msg->flags & I2C_M_RD,
-					      f7_msg->count, f7_msg->buf,
-					      stm32f7_i2c_dma_callback,
-					      i2c_dev);
-		if (!ret)
-			i2c_dev->use_dma = true;
-		else
-			dev_warn(i2c_dev->dev, "can't use DMA\n");
+	if (i2c_dev->dma && !i2c_dev->atomic) {
+		dma_buf = i2c_get_dma_safe_msg_buf(msg, STM32F7_I2C_DMA_LEN_MIN);
+		if (dma_buf) {
+			f7_msg->buf = dma_buf;
+			ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma,
+						      msg->flags & I2C_M_RD,
+						      f7_msg->count, f7_msg->buf,
+						      stm32f7_i2c_dma_callback,
+						      i2c_dev);
+			if (ret) {
+				dev_warn(i2c_dev->dev, "can't use DMA\n");
+				i2c_put_dma_safe_msg_buf(f7_msg->buf, msg, false);
+				f7_msg->buf = msg->buf;
+			} else {
+				i2c_dev->use_dma = true;
+			}
+		}
 	}
 
 	if (!i2c_dev->use_dma) {
@@ -1626,6 +1636,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
 			dmaengine_terminate_async(dma->chan_using);
 			dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
 					 dma->dma_data_dir);
+			if (!f7_msg->smbus)
+				i2c_put_dma_safe_msg_buf(f7_msg->buf, i2c_dev->msg, false);
 		}
 		f7_msg->result = -ENXIO;
 	}
@@ -1648,6 +1660,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
 				dmaengine_terminate_async(dma->chan_using);
 				dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
 						 dma->dma_data_dir);
+				if (!f7_msg->smbus)
+					i2c_put_dma_safe_msg_buf(f7_msg->buf, i2c_dev->msg, false);
 				f7_msg->result = -ETIMEDOUT;
 			}
 		}

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map
  2025-06-30 12:55 ` [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
@ 2025-06-30 13:05   ` Pierre Yves MORDRET
  2025-07-02 16:57   ` Andi Shyti
  1 sibling, 0 replies; 13+ messages in thread
From: Pierre Yves MORDRET @ 2025-06-30 13:05 UTC (permalink / raw)
  To: Clément Le Goffic, Alain Volmat, Andi Shyti, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang
  Cc: Pierre-Yves MORDRET, linux-i2c, linux-stm32, linux-arm-kernel,
	linux-kernel, linux-media, dri-devel, linaro-mm-sig

Hi Clement,

On 6/30/25 14:55, Clément Le Goffic wrote:
> If the DMA mapping failed, it produced an error log with the wrong
> device name:
> "stm32-dma3 40400000.dma-controller: rejecting DMA map of vmalloc memory"
> Fix this issue by replacing the dev with the I2C dev.
> 
> Fixes: bb8822cbbc53 ("i2c: i2c-stm32: Add generic DMA API")
> Acked-by: Alain Volmat <alain.volmat@foss.st.com>
> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
> ---
>  drivers/i2c/busses/i2c-stm32.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-stm32.c b/drivers/i2c/busses/i2c-stm32.c
> index 157c64e27d0b..272a5dffb08f 100644
> --- a/drivers/i2c/busses/i2c-stm32.c
> +++ b/drivers/i2c/busses/i2c-stm32.c
> @@ -118,7 +118,7 @@ int stm32_i2c_prep_dma_xfer(struct device *dev, struct stm32_i2c_dma *dma,
>  	dma->dma_len = len;
>  	chan_dev = dma->chan_using->device->dev;
>  
> -	dma->dma_buf = dma_map_single(chan_dev, buf, dma->dma_len,
> +	dma->dma_buf = dma_map_single(dev, buf, dma->dma_len,
>  				      dma->dma_data_dir);
>  	if (dma_mapping_error(chan_dev, dma->dma_buf)) {
>  		dev_err(dev, "DMA mapping failed\n");
> @@ -150,7 +150,7 @@ int stm32_i2c_prep_dma_xfer(struct device *dev, struct stm32_i2c_dma *dma,
>  	return 0;
>  
>  err:
> -	dma_unmap_single(chan_dev, dma->dma_buf, dma->dma_len,
> +	dma_unmap_single(dev, dma->dma_buf, dma->dma_len,
>  			 dma->dma_data_dir);
>  	return ret;
>  }
> 

Thx for this V3 submission

Reviewed-by : Pierre-Yves MORDRET <pierre-yves.mordret@foss.st.com>

Regards

-- 
--
~ Py MORDRET
--

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer
  2025-06-30 12:55 ` [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
@ 2025-06-30 13:06   ` Pierre Yves MORDRET
  2025-07-02 17:08   ` Andi Shyti
  1 sibling, 0 replies; 13+ messages in thread
From: Pierre Yves MORDRET @ 2025-06-30 13:06 UTC (permalink / raw)
  To: Clément Le Goffic, Alain Volmat, Andi Shyti, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang
  Cc: Pierre-Yves MORDRET, linux-i2c, linux-stm32, linux-arm-kernel,
	linux-kernel, linux-media, dri-devel, linaro-mm-sig

Hi Clement,


On 6/30/25 14:55, Clément Le Goffic wrote:
> Fix an issue where the mapped DMA buffer was not unmapped.
> 
> Fixes: 7ecc8cfde553 ("i2c: i2c-stm32f7: Add DMA support")
> Acked-by: Alain Volmat <alain.volmat@foss.st.com>
> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
> ---
>  drivers/i2c/busses/i2c-stm32f7.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
> index e4aaeb2262d0..042386b4cabe 100644
> --- a/drivers/i2c/busses/i2c-stm32f7.c
> +++ b/drivers/i2c/busses/i2c-stm32f7.c
> @@ -1554,6 +1554,8 @@ static irqreturn_t stm32f7_i2c_handle_isr_errs(struct stm32f7_i2c_dev *i2c_dev,
>  	if (i2c_dev->use_dma) {
>  		stm32f7_i2c_disable_dma_req(i2c_dev);
>  		dmaengine_terminate_async(dma->chan_using);
> +		dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
> +				 dma->dma_data_dir);
>  	}
>  
>  	i2c_dev->master_mode = false;
> @@ -1622,6 +1624,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
>  		if (i2c_dev->use_dma) {
>  			stm32f7_i2c_disable_dma_req(i2c_dev);
>  			dmaengine_terminate_async(dma->chan_using);
> +			dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
> +					 dma->dma_data_dir);
>  		}
>  		f7_msg->result = -ENXIO;
>  	}
> @@ -1642,6 +1646,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
>  				dev_dbg(i2c_dev->dev, "<%s>: Timed out\n", __func__);
>  				stm32f7_i2c_disable_dma_req(i2c_dev);
>  				dmaengine_terminate_async(dma->chan_using);
> +				dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
> +						 dma->dma_data_dir);
>  				f7_msg->result = -ETIMEDOUT;
>  			}
>  		}
> 

Thx for this V3 submission

Reviewed-by: Pierre-Yves MORDRET <pierre-yves.mordret@foss.st.com>

Regards

-- 
--
~ Py MORDRET
--

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs
  2025-06-30 12:55 ` [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
@ 2025-06-30 13:07   ` Pierre Yves MORDRET
  2025-07-02 17:15   ` Andi Shyti
  1 sibling, 0 replies; 13+ messages in thread
From: Pierre Yves MORDRET @ 2025-06-30 13:07 UTC (permalink / raw)
  To: Clément Le Goffic, Alain Volmat, Andi Shyti, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang
  Cc: Pierre-Yves MORDRET, linux-i2c, linux-stm32, linux-arm-kernel,
	linux-kernel, linux-media, dri-devel, linaro-mm-sig

Hi Clement,

On 6/30/25 14:55, Clément Le Goffic wrote:
> Use the i2c-core-base APIs to allocate a DMA safe buffer when needed.
> 
> Acked-by: Alain Volmat <alain.volmat@foss.st.com>
> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
> ---
>  drivers/i2c/busses/i2c-stm32f7.c | 36 +++++++++++++++++++++++++-----------
>  1 file changed, 25 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
> index 042386b4cabe..d06f0efdece3 100644
> --- a/drivers/i2c/busses/i2c-stm32f7.c
> +++ b/drivers/i2c/busses/i2c-stm32f7.c
> @@ -742,9 +742,12 @@ static void stm32f7_i2c_dma_callback(void *arg)
>  	struct stm32f7_i2c_dev *i2c_dev = (struct stm32f7_i2c_dev *)arg;
>  	struct stm32_i2c_dma *dma = i2c_dev->dma;
>  	struct device *dev = dma->chan_using->device->dev;
> +	struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
>  
>  	stm32f7_i2c_disable_dma_req(i2c_dev);
>  	dma_unmap_single(dev, dma->dma_buf, dma->dma_len, dma->dma_data_dir);
> +	if (!f7_msg->smbus)
> +		i2c_put_dma_safe_msg_buf(f7_msg->buf, i2c_dev->msg, true);
>  	complete(&dma->dma_complete);
>  }
>  
> @@ -880,6 +883,7 @@ static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev,
>  {
>  	struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg;
>  	void __iomem *base = i2c_dev->base;
> +	u8 *dma_buf;
>  	u32 cr1, cr2;
>  	int ret;
>  
> @@ -929,17 +933,23 @@ static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev,
>  
>  	/* Configure DMA or enable RX/TX interrupt */
>  	i2c_dev->use_dma = false;
> -	if (i2c_dev->dma && f7_msg->count >= STM32F7_I2C_DMA_LEN_MIN
> -	    && !i2c_dev->atomic) {
> -		ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma,
> -					      msg->flags & I2C_M_RD,
> -					      f7_msg->count, f7_msg->buf,
> -					      stm32f7_i2c_dma_callback,
> -					      i2c_dev);
> -		if (!ret)
> -			i2c_dev->use_dma = true;
> -		else
> -			dev_warn(i2c_dev->dev, "can't use DMA\n");
> +	if (i2c_dev->dma && !i2c_dev->atomic) {
> +		dma_buf = i2c_get_dma_safe_msg_buf(msg, STM32F7_I2C_DMA_LEN_MIN);
> +		if (dma_buf) {
> +			f7_msg->buf = dma_buf;
> +			ret = stm32_i2c_prep_dma_xfer(i2c_dev->dev, i2c_dev->dma,
> +						      msg->flags & I2C_M_RD,
> +						      f7_msg->count, f7_msg->buf,
> +						      stm32f7_i2c_dma_callback,
> +						      i2c_dev);
> +			if (ret) {
> +				dev_warn(i2c_dev->dev, "can't use DMA\n");
> +				i2c_put_dma_safe_msg_buf(f7_msg->buf, msg, false);
> +				f7_msg->buf = msg->buf;
> +			} else {
> +				i2c_dev->use_dma = true;
> +			}
> +		}
>  	}
>  
>  	if (!i2c_dev->use_dma) {
> @@ -1626,6 +1636,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
>  			dmaengine_terminate_async(dma->chan_using);
>  			dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
>  					 dma->dma_data_dir);
> +			if (!f7_msg->smbus)
> +				i2c_put_dma_safe_msg_buf(f7_msg->buf, i2c_dev->msg, false);
>  		}
>  		f7_msg->result = -ENXIO;
>  	}
> @@ -1648,6 +1660,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
>  				dmaengine_terminate_async(dma->chan_using);
>  				dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
>  						 dma->dma_data_dir);
> +				if (!f7_msg->smbus)
> +					i2c_put_dma_safe_msg_buf(f7_msg->buf, i2c_dev->msg, false);
>  				f7_msg->result = -ETIMEDOUT;
>  			}
>  		}
> 

Thx for this V3 submission

Reviewed-by: Pierre-Yves MORDRET <pierre-yves.mordret@foss.st.com>

Regards

-- 
--
~ Py MORDRET
--

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map
  2025-06-30 12:55 ` [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
  2025-06-30 13:05   ` Pierre Yves MORDRET
@ 2025-07-02 16:57   ` Andi Shyti
  2025-07-03  7:20     ` Clement LE GOFFIC
  1 sibling, 1 reply; 13+ messages in thread
From: Andi Shyti @ 2025-07-02 16:57 UTC (permalink / raw)
  To: Clément Le Goffic
  Cc: Pierre-Yves MORDRET, Alain Volmat, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang, Pierre-Yves MORDRET,
	linux-i2c, linux-stm32, linux-arm-kernel, linux-kernel,
	linux-media, dri-devel, linaro-mm-sig

Hi Clement,

...

> @@ -118,7 +118,7 @@ int stm32_i2c_prep_dma_xfer(struct device *dev, struct stm32_i2c_dma *dma,
>  	dma->dma_len = len;
>  	chan_dev = dma->chan_using->device->dev;
>  
> -	dma->dma_buf = dma_map_single(chan_dev, buf, dma->dma_len,
> +	dma->dma_buf = dma_map_single(dev, buf, dma->dma_len,
>  				      dma->dma_data_dir);
>  	if (dma_mapping_error(chan_dev, dma->dma_buf)) {
			      ^^^^^^^^

this one should be "dev" too, which renders the chan_dev variable
unused.

Thanks,
Andi

>  		dev_err(dev, "DMA mapping failed\n");

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer
  2025-06-30 12:55 ` [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
  2025-06-30 13:06   ` Pierre Yves MORDRET
@ 2025-07-02 17:08   ` Andi Shyti
  2025-07-03  9:19     ` Clement LE GOFFIC
  1 sibling, 1 reply; 13+ messages in thread
From: Andi Shyti @ 2025-07-02 17:08 UTC (permalink / raw)
  To: Clément Le Goffic
  Cc: Pierre-Yves MORDRET, Alain Volmat, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang, Pierre-Yves MORDRET,
	linux-i2c, linux-stm32, linux-arm-kernel, linux-kernel,
	linux-media, dri-devel, linaro-mm-sig

Hi Clement,

On Mon, Jun 30, 2025 at 02:55:14PM +0200, Clément Le Goffic wrote:
> Fix an issue where the mapped DMA buffer was not unmapped.

"Fix an issue..." is too generic. Can you be more specific? Where
was it mapped? Where was it left unmapped?

Please, do consider that the user needs to understand what
happens in the patch without needing to look into the patch.

> Fixes: 7ecc8cfde553 ("i2c: i2c-stm32f7: Add DMA support")
> Acked-by: Alain Volmat <alain.volmat@foss.st.com>
> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
> ---
>  drivers/i2c/busses/i2c-stm32f7.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
> index e4aaeb2262d0..042386b4cabe 100644
> --- a/drivers/i2c/busses/i2c-stm32f7.c
> +++ b/drivers/i2c/busses/i2c-stm32f7.c
> @@ -1554,6 +1554,8 @@ static irqreturn_t stm32f7_i2c_handle_isr_errs(struct stm32f7_i2c_dev *i2c_dev,
>  	if (i2c_dev->use_dma) {
>  		stm32f7_i2c_disable_dma_req(i2c_dev);
>  		dmaengine_terminate_async(dma->chan_using);
> +		dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
> +				 dma->dma_data_dir);
>  	}
>  
>  	i2c_dev->master_mode = false;
> @@ -1622,6 +1624,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
>  		if (i2c_dev->use_dma) {
>  			stm32f7_i2c_disable_dma_req(i2c_dev);
>  			dmaengine_terminate_async(dma->chan_using);
> +			dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
> +					 dma->dma_data_dir);
>  		}
>  		f7_msg->result = -ENXIO;
>  	}
> @@ -1642,6 +1646,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
>  				dev_dbg(i2c_dev->dev, "<%s>: Timed out\n", __func__);
>  				stm32f7_i2c_disable_dma_req(i2c_dev);
>  				dmaengine_terminate_async(dma->chan_using);
> +				dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
> +						 dma->dma_data_dir);

Can't we use the dma_callback here, or similar? I see some
similar patterns and I think the code can be improved.

Andi

>  				f7_msg->result = -ETIMEDOUT;
>  			}
>  		}
> 
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs
  2025-06-30 12:55 ` [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
  2025-06-30 13:07   ` Pierre Yves MORDRET
@ 2025-07-02 17:15   ` Andi Shyti
  2025-07-03  9:21     ` Clement LE GOFFIC
  1 sibling, 1 reply; 13+ messages in thread
From: Andi Shyti @ 2025-07-02 17:15 UTC (permalink / raw)
  To: Clément Le Goffic
  Cc: Pierre-Yves MORDRET, Alain Volmat, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang, Pierre-Yves MORDRET,
	linux-i2c, linux-stm32, linux-arm-kernel, linux-kernel,
	linux-media, dri-devel, linaro-mm-sig

Hi Clement,

On Mon, Jun 30, 2025 at 02:55:15PM +0200, Clément Le Goffic wrote:
> Use the i2c-core-base APIs to allocate a DMA safe buffer when needed.

same here, I don't understand anything... you could have written
"do some coding" and it would have been the same :-)

Thanks,
Andi

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map
  2025-07-02 16:57   ` Andi Shyti
@ 2025-07-03  7:20     ` Clement LE GOFFIC
  0 siblings, 0 replies; 13+ messages in thread
From: Clement LE GOFFIC @ 2025-07-03  7:20 UTC (permalink / raw)
  To: Andi Shyti
  Cc: Pierre-Yves MORDRET, Alain Volmat, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang, Pierre-Yves MORDRET,
	linux-i2c, linux-stm32, linux-arm-kernel, linux-kernel,
	linux-media, dri-devel, linaro-mm-sig

Hi Andy,

On 7/2/25 18:57, Andi Shyti wrote:
> Hi Clement,
> 
> ...
> 
>> @@ -118,7 +118,7 @@ int stm32_i2c_prep_dma_xfer(struct device *dev, struct stm32_i2c_dma *dma,
>>   	dma->dma_len = len;
>>   	chan_dev = dma->chan_using->device->dev;
>>   
>> -	dma->dma_buf = dma_map_single(chan_dev, buf, dma->dma_len,
>> +	dma->dma_buf = dma_map_single(dev, buf, dma->dma_len,
>>   				      dma->dma_data_dir);
>>   	if (dma_mapping_error(chan_dev, dma->dma_buf)) {
> 			      ^^^^^^^^
> 
> this one should be "dev" too, which renders the chan_dev variable
> unused.

Oh yes will send a v4

Best regards,
Clément


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer
  2025-07-02 17:08   ` Andi Shyti
@ 2025-07-03  9:19     ` Clement LE GOFFIC
  0 siblings, 0 replies; 13+ messages in thread
From: Clement LE GOFFIC @ 2025-07-03  9:19 UTC (permalink / raw)
  To: Andi Shyti
  Cc: Pierre-Yves MORDRET, Alain Volmat, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang, Pierre-Yves MORDRET,
	linux-i2c, linux-stm32, linux-arm-kernel, linux-kernel,
	linux-media, dri-devel, linaro-mm-sig

Hi Andi,

On 7/2/25 19:08, Andi Shyti wrote:
> Hi Clement,
> 
> On Mon, Jun 30, 2025 at 02:55:14PM +0200, Clément Le Goffic wrote:
>> Fix an issue where the mapped DMA buffer was not unmapped.
> 
> "Fix an issue..." is too generic. Can you be more specific? Where
> was it mapped? Where was it left unmapped?
> 
> Please, do consider that the user needs to understand what
> happens in the patch without needing to look into the patch.

Ok sure I'll refine the commit message.

> 
>> Fixes: 7ecc8cfde553 ("i2c: i2c-stm32f7: Add DMA support")
>> Acked-by: Alain Volmat <alain.volmat@foss.st.com>
>> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
>> ---
>>   drivers/i2c/busses/i2c-stm32f7.c | 6 ++++++
>>   1 file changed, 6 insertions(+)
>>
>> diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
>> index e4aaeb2262d0..042386b4cabe 100644
>> --- a/drivers/i2c/busses/i2c-stm32f7.c
>> +++ b/drivers/i2c/busses/i2c-stm32f7.c
>> @@ -1554,6 +1554,8 @@ static irqreturn_t stm32f7_i2c_handle_isr_errs(struct stm32f7_i2c_dev *i2c_dev,
>>   	if (i2c_dev->use_dma) {
>>   		stm32f7_i2c_disable_dma_req(i2c_dev);
>>   		dmaengine_terminate_async(dma->chan_using);
>> +		dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
>> +				 dma->dma_data_dir);
>>   	}
>>   
>>   	i2c_dev->master_mode = false;
>> @@ -1622,6 +1624,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
>>   		if (i2c_dev->use_dma) {
>>   			stm32f7_i2c_disable_dma_req(i2c_dev);
>>   			dmaengine_terminate_async(dma->chan_using);
>> +			dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
>> +					 dma->dma_data_dir);
>>   		}
>>   		f7_msg->result = -ENXIO;
>>   	}
>> @@ -1642,6 +1646,8 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data)
>>   				dev_dbg(i2c_dev->dev, "<%s>: Timed out\n", __func__);
>>   				stm32f7_i2c_disable_dma_req(i2c_dev);
>>   				dmaengine_terminate_async(dma->chan_using);
>> +				dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
>> +						 dma->dma_data_dir);
> 
> Can't we use the dma_callback here, or similar? I see some
> similar patterns and I think the code can be improved.

Yes, it seems the code can be factorized.
I'll submit a new version with the factorization.

Best regards,
Clément


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs
  2025-07-02 17:15   ` Andi Shyti
@ 2025-07-03  9:21     ` Clement LE GOFFIC
  0 siblings, 0 replies; 13+ messages in thread
From: Clement LE GOFFIC @ 2025-07-03  9:21 UTC (permalink / raw)
  To: Andi Shyti
  Cc: Pierre-Yves MORDRET, Alain Volmat, Maxime Coquelin,
	Alexandre Torgue, Sumit Semwal, Christian König,
	M'boumba Cedric Madianga, Wolfram Sang, Pierre-Yves MORDRET,
	linux-i2c, linux-stm32, linux-arm-kernel, linux-kernel,
	linux-media, dri-devel, linaro-mm-sig

Hi Andy,

On 7/2/25 19:15, Andi Shyti wrote:
> Hi Clement,
> 
> On Mon, Jun 30, 2025 at 02:55:15PM +0200, Clément Le Goffic wrote:
>> Use the i2c-core-base APIs to allocate a DMA safe buffer when needed.
> 
> same here, I don't understand anything... you could have written
> "do some coding" and it would have been the same :-)

Ok I'll try to be more precise here also.

Best regards,
Clément

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-07-03  9:23 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-30 12:55 [PATCH v3 0/3] Fix STM32 I2C dma operations Clément Le Goffic
2025-06-30 12:55 ` [PATCH v3 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
2025-06-30 13:05   ` Pierre Yves MORDRET
2025-07-02 16:57   ` Andi Shyti
2025-07-03  7:20     ` Clement LE GOFFIC
2025-06-30 12:55 ` [PATCH v3 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
2025-06-30 13:06   ` Pierre Yves MORDRET
2025-07-02 17:08   ` Andi Shyti
2025-07-03  9:19     ` Clement LE GOFFIC
2025-06-30 12:55 ` [PATCH v3 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
2025-06-30 13:07   ` Pierre Yves MORDRET
2025-07-02 17:15   ` Andi Shyti
2025-07-03  9:21     ` Clement LE GOFFIC

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).