Fix STM32 I2C dma operations
v2 cover Fix STM32 I2C dma operations
This patch series aims to fix some issues inside the driver's DMA handling.
It also uses newer I2C DMA API.
---
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/[email protected]
---
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: 86731a2a651e58953fc949573895f2fa6d456841
change-id: 20250527-i2c-upstream-e5b69501359a
Best regards,
Signature · 1 line
Clément Le Goffic <[email protected]>
PATCH v2 1/3 i2c: stm32: fix the device used for the DMA map
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.
--- drivers/i2c/busses/i2c-stm32.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-stm32.c +2 -2
@@ -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; }
Signature · 1 line
2.43.0
Re: [PATCH v2 1/3] i2c: stm32: fix the device used for the DMA map
Hello Clément,
Quoted 26 lines · 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 <[email protected]>--- drivers/i2c/busses/i2c-stm32.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/i2c/busses/i2c-stm32.c +1
-1@@ -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" );
drivers/i2c/busses/i2c-stm32.c int stm32_i2c_prep_dma_xfer(struct device *dev, struct stm32_i2c_dma *dma, · line 156
Quoted 8 lines
drivers/i2c/busses/i2c-stm32.c +1
-1@@ -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; }
Thanks for the v2.
Regards,
Alain
Quoted 3 lines
Signature · 1 line
2.43.0
PATCH v2 2/3 i2c: stm32f7: unmap DMA mapped buffer
In reply to Clément Le GofficFix an issue where the mapped DMA buffer was not unmapped.
--- drivers/i2c/busses/i2c-stm32f7.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/i2c/busses/i2c-stm32f7.c +6 -0
@@ -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 ; } }
Signature · 1 line
2.43.0
Re: [PATCH v2 2/3] i2c: stm32f7: unmap DMA mapped buffer
Hello Clément,
Quoted 32 lines · 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 <[email protected]>--- drivers/i2c/busses/i2c-stm32f7.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/i2c/busses/i2c-stm32f7.c +4
-0@@ -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 ; }
drivers/i2c/busses/i2c-stm32f7.c static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data) · line 1653
Quoted 8 lines
drivers/i2c/busses/i2c-stm32f7.c +2
-0@@ -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 ; } }
Thanks for the update. Looks good to me.
Regards,
Alain
Quoted 3 lines
Signature · 1 line
2.43.0
PATCH v2 3/3 i2c: stm32f7: support i2c_*_dma_safe_msg_buf APIs
In reply to Clément Le GofficUse the i2c-core-base APIs to allocate a DMA safe buffer when needed.
--- drivers/i2c/busses/i2c-stm32f7.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-)
drivers/i2c/busses/i2c-stm32f7.c +25 -11
@@ -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 ; } }
Signature · 1 line
2.43.0