dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Fix STM32 I2C dma operations
@ 2025-06-16  8:53 Clément Le Goffic
  2025-06-16  8:53 ` [PATCH 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Clément Le Goffic @ 2025-06-16  8:53 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>
---
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   |  2 +-
 drivers/i2c/busses/i2c-stm32f7.c | 40 +++++++++++++++++++++++++++++-----------
 2 files changed, 30 insertions(+), 12 deletions(-)
---
base-commit: e04c78d86a9699d136910cfc0bdcf01087e3267e
change-id: 20250527-i2c-upstream-e5b69501359a

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


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

* [PATCH 1/3] i2c: stm32: fix the device used for the DMA map
  2025-06-16  8:53 [PATCH 0/3] Fix STM32 I2C dma operations Clément Le Goffic
@ 2025-06-16  8:53 ` Clément Le Goffic
  2025-06-26  8:37   ` Alain Volmat
  2025-06-16  8:53 ` [PATCH 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 11+ messages in thread
From: Clément Le Goffic @ 2025-06-16  8:53 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")
Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
---
 drivers/i2c/busses/i2c-stm32.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-stm32.c b/drivers/i2c/busses/i2c-stm32.c
index 157c64e27d0b..5e0b31aed774 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");

-- 
2.43.0


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

* [PATCH 2/3] i2c: stm32f7: unmap DMA mapped buffer
  2025-06-16  8:53 [PATCH 0/3] Fix STM32 I2C dma operations Clément Le Goffic
  2025-06-16  8:53 ` [PATCH 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
@ 2025-06-16  8:53 ` Clément Le Goffic
  2025-06-26  9:03   ` Alain Volmat
  2025-06-16  8:53 ` [PATCH 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
  2025-06-25 22:17 ` [PATCH 0/3] Fix STM32 I2C dma operations Andi Shyti
  3 siblings, 1 reply; 11+ messages in thread
From: Clément Le Goffic @ 2025-06-16  8:53 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")
Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
---
 drivers/i2c/busses/i2c-stm32f7.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
index 973a3a8c6d4a..a05cac5ee9db 100644
--- a/drivers/i2c/busses/i2c-stm32f7.c
+++ b/drivers/i2c/busses/i2c-stm32f7.c
@@ -1622,6 +1622,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 +1644,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] 11+ messages in thread

* [PATCH 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs
  2025-06-16  8:53 [PATCH 0/3] Fix STM32 I2C dma operations Clément Le Goffic
  2025-06-16  8:53 ` [PATCH 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
  2025-06-16  8:53 ` [PATCH 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
@ 2025-06-16  8:53 ` Clément Le Goffic
  2025-06-26 10:32   ` Alain Volmat
  2025-06-25 22:17 ` [PATCH 0/3] Fix STM32 I2C dma operations Andi Shyti
  3 siblings, 1 reply; 11+ messages in thread
From: Clément Le Goffic @ 2025-06-16  8:53 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.

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 a05cac5ee9db..5be14c8a2af4 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) {
@@ -1624,6 +1634,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;
 	}
@@ -1646,6 +1658,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] 11+ messages in thread

* Re: [PATCH 0/3] Fix STM32 I2C dma operations
  2025-06-16  8:53 [PATCH 0/3] Fix STM32 I2C dma operations Clément Le Goffic
                   ` (2 preceding siblings ...)
  2025-06-16  8:53 ` [PATCH 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
@ 2025-06-25 22:17 ` Andi Shyti
  3 siblings, 0 replies; 11+ messages in thread
From: Andi Shyti @ 2025-06-25 22:17 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

Pierre-Yves, Alain,

On Mon, Jun 16, 2025 at 10:53:53AM +0200, Clément Le Goffic wrote:
> 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>
> ---
> 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

any chance you can take a look at this series?

Thanks,
Andi

> 
>  drivers/i2c/busses/i2c-stm32.c   |  2 +-
>  drivers/i2c/busses/i2c-stm32f7.c | 40 +++++++++++++++++++++++++++++-----------
>  2 files changed, 30 insertions(+), 12 deletions(-)
> ---
> base-commit: e04c78d86a9699d136910cfc0bdcf01087e3267e
> change-id: 20250527-i2c-upstream-e5b69501359a
> 
> Best regards,
> -- 
> Clément Le Goffic <clement.legoffic@foss.st.com>
> 

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

* Re: [PATCH 1/3] i2c: stm32: fix the device used for the DMA map
  2025-06-16  8:53 ` [PATCH 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
@ 2025-06-26  8:37   ` Alain Volmat
  2025-06-26  8:43     ` Alain Volmat
  0 siblings, 1 reply; 11+ messages in thread
From: Alain Volmat @ 2025-06-26  8:37 UTC (permalink / raw)
  To: Clément Le Goffic
  Cc: Pierre-Yves MORDRET, Andi Shyti, 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 Clément,

On Mon, Jun 16, 2025 at 10:53:54AM +0200, 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.

Indeed, nice catch ! Thanks a lot !

> 
> Fixes: bb8822cbbc53 ("i2c: i2c-stm32: Add generic DMA API")
> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
> ---
>  drivers/i2c/busses/i2c-stm32.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/i2c/busses/i2c-stm32.c b/drivers/i2c/busses/i2c-stm32.c
> index 157c64e27d0b..5e0b31aed774 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");
> 
> -- 
> 2.43.0
>

Acked-by: Alain Volmat <alain.volmat@foss.st.com>

Regards,
Alain

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

* Re: [PATCH 1/3] i2c: stm32: fix the device used for the DMA map
  2025-06-26  8:37   ` Alain Volmat
@ 2025-06-26  8:43     ` Alain Volmat
  2025-06-27  7:30       ` Clement LE GOFFIC
  0 siblings, 1 reply; 11+ messages in thread
From: Alain Volmat @ 2025-06-26  8:43 UTC (permalink / raw)
  To: Clément Le Goffic
  Cc: Pierre-Yves MORDRET, Andi Shyti, 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 Clément,

Oups, I was too fast.

there might be another place to correct in the driver, dma_unmap_single
within the error handling of the function stm32_i2c_prep_dma_xfer.

   err:
            dma_unmap_single(chan_dev, dma->dma_buf, dma->dma_len,
                             dma->dma_data_dir);

Could you also correct this one as well ?

Alain


On Thu, Jun 26, 2025 at 10:37:51AM +0200, Alain Volmat wrote:
> Hi Clément,
> 
> On Mon, Jun 16, 2025 at 10:53:54AM +0200, 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.
> 
> Indeed, nice catch ! Thanks a lot !
> 
> > 
> > Fixes: bb8822cbbc53 ("i2c: i2c-stm32: Add generic DMA API")
> > Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
> > ---
> >  drivers/i2c/busses/i2c-stm32.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/i2c/busses/i2c-stm32.c b/drivers/i2c/busses/i2c-stm32.c
> > index 157c64e27d0b..5e0b31aed774 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");
> > 
> > -- 
> > 2.43.0
> >
> 
> Acked-by: Alain Volmat <alain.volmat@foss.st.com>
> 
> Regards,
> Alain

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

* Re: [PATCH 2/3] i2c: stm32f7: unmap DMA mapped buffer
  2025-06-16  8:53 ` [PATCH 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
@ 2025-06-26  9:03   ` Alain Volmat
  2025-06-27 10:16     ` Clement LE GOFFIC
  0 siblings, 1 reply; 11+ messages in thread
From: Alain Volmat @ 2025-06-26  9:03 UTC (permalink / raw)
  To: Clément Le Goffic
  Cc: Pierre-Yves MORDRET, Andi Shyti, 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 Clément,

thanks for the patch.

On Mon, Jun 16, 2025 at 10:53:55AM +0200, Clément Le Goffic wrote:
> Fix an issue where the mapped DMA buffer was not unmapped.
> 
> Fixes: 7ecc8cfde553 ("i2c: i2c-stm32f7: Add DMA support")
> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
> ---
>  drivers/i2c/busses/i2c-stm32f7.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
> index 973a3a8c6d4a..a05cac5ee9db 100644
> --- a/drivers/i2c/busses/i2c-stm32f7.c
> +++ b/drivers/i2c/busses/i2c-stm32f7.c
> @@ -1622,6 +1622,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 +1644,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;
>  			}
>  		}
> 

Sounds good to me, however there might be an additional place to fix
within the function stm32f7_i2c_handle_isr_errs:
Could you also take care of the unmap in the error ITs handling ?

Regards,
Alain

> -- 
> 2.43.0
> 

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

* Re: [PATCH 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs
  2025-06-16  8:53 ` [PATCH 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
@ 2025-06-26 10:32   ` Alain Volmat
  0 siblings, 0 replies; 11+ messages in thread
From: Alain Volmat @ 2025-06-26 10:32 UTC (permalink / raw)
  To: Clément Le Goffic
  Cc: Pierre-Yves MORDRET, Andi Shyti, 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 Clément,

thanks for the patch.

On Mon, Jun 16, 2025 at 10:53:56AM +0200, Clément Le Goffic wrote:
> Use the i2c-core-base APIs to allocate a DMA safe buffer when needed.
> 
> 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 a05cac5ee9db..5be14c8a2af4 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) {
> @@ -1624,6 +1634,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;
>  	}
> @@ -1646,6 +1658,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;
>  			}
>  		}
> 

Looks good to me.

Acked-by: Alain Volmat <alain.volmat@foss.st.com>

Regards,
Alain

> -- 
> 2.43.0
> 

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

* Re: [PATCH 1/3] i2c: stm32: fix the device used for the DMA map
  2025-06-26  8:43     ` Alain Volmat
@ 2025-06-27  7:30       ` Clement LE GOFFIC
  0 siblings, 0 replies; 11+ messages in thread
From: Clement LE GOFFIC @ 2025-06-27  7:30 UTC (permalink / raw)
  To: Alain Volmat
  Cc: Pierre-Yves MORDRET, Andi Shyti, 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

On 6/26/25 10:43, Alain Volmat wrote:
> Hi Clément,
> 
> Oups, I was too fast.
> 
> there might be another place to correct in the driver, dma_unmap_single
> within the error handling of the function stm32_i2c_prep_dma_xfer.
> 
>     err:
>              dma_unmap_single(chan_dev, dma->dma_buf, dma->dma_len,
>                               dma->dma_data_dir);
> 
> Could you also correct this one as well ?
> 
> Alain

Hi Alain,

Oh yes you're right, I'll send a v2

Best regard,
Clément
> 
> 
> On Thu, Jun 26, 2025 at 10:37:51AM +0200, Alain Volmat wrote:
>> Hi Clément,
>>
>> On Mon, Jun 16, 2025 at 10:53:54AM +0200, 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.
>>
>> Indeed, nice catch ! Thanks a lot !
>>
>>>
>>> Fixes: bb8822cbbc53 ("i2c: i2c-stm32: Add generic DMA API")
>>> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
>>> ---
>>>   drivers/i2c/busses/i2c-stm32.c | 2 +-
>>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/i2c/busses/i2c-stm32.c b/drivers/i2c/busses/i2c-stm32.c
>>> index 157c64e27d0b..5e0b31aed774 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");
>>>
>>> -- 
>>> 2.43.0
>>>
>>
>> Acked-by: Alain Volmat <alain.volmat@foss.st.com>
>>
>> Regards,
>> Alain


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

* Re: [PATCH 2/3] i2c: stm32f7: unmap DMA mapped buffer
  2025-06-26  9:03   ` Alain Volmat
@ 2025-06-27 10:16     ` Clement LE GOFFIC
  0 siblings, 0 replies; 11+ messages in thread
From: Clement LE GOFFIC @ 2025-06-27 10:16 UTC (permalink / raw)
  To: Alain Volmat
  Cc: Pierre-Yves MORDRET, Andi Shyti, 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

On 6/26/25 11:03, Alain Volmat wrote:
> Hi Clément,
> 
> thanks for the patch.
> 
> On Mon, Jun 16, 2025 at 10:53:55AM +0200, Clément Le Goffic wrote:
>> Fix an issue where the mapped DMA buffer was not unmapped.
>>
>> Fixes: 7ecc8cfde553 ("i2c: i2c-stm32f7: Add DMA support")
>> Signed-off-by: Clément Le Goffic <clement.legoffic@foss.st.com>
>> ---
>>   drivers/i2c/busses/i2c-stm32f7.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/drivers/i2c/busses/i2c-stm32f7.c b/drivers/i2c/busses/i2c-stm32f7.c
>> index 973a3a8c6d4a..a05cac5ee9db 100644
>> --- a/drivers/i2c/busses/i2c-stm32f7.c
>> +++ b/drivers/i2c/busses/i2c-stm32f7.c
>> @@ -1622,6 +1622,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 +1644,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;
>>   			}
>>   		}
>>
> 
> Sounds good to me, however there might be an additional place to fix
> within the function stm32f7_i2c_handle_isr_errs:
> Could you also take care of the unmap in the error ITs handling ?

Ack
> 
> Regards,
> Alain
> 
>> -- 
>> 2.43.0
>>


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

end of thread, other threads:[~2025-06-27 10:18 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-16  8:53 [PATCH 0/3] Fix STM32 I2C dma operations Clément Le Goffic
2025-06-16  8:53 ` [PATCH 1/3] i2c: stm32: fix the device used for the DMA map Clément Le Goffic
2025-06-26  8:37   ` Alain Volmat
2025-06-26  8:43     ` Alain Volmat
2025-06-27  7:30       ` Clement LE GOFFIC
2025-06-16  8:53 ` [PATCH 2/3] i2c: stm32f7: unmap DMA mapped buffer Clément Le Goffic
2025-06-26  9:03   ` Alain Volmat
2025-06-27 10:16     ` Clement LE GOFFIC
2025-06-16  8:53 ` [PATCH 3/3] i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs Clément Le Goffic
2025-06-26 10:32   ` Alain Volmat
2025-06-25 22:17 ` [PATCH 0/3] Fix STM32 I2C dma operations Andi Shyti

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).