Skip to contentSkip to conversation

i3c: Remove the const qualifier from i2c_msg pointer in i2c_xfers API

PATCH Superseded Billy Tsai 5 participants 11 messages Jan 21, 2025 – Feb 4, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Billy Tsai

author
#0 lore raw

PATCH 1/2 i3c: Remove the const qualifier from i2c_msg pointer in i2c_xfers API

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

---
 drivers/i3c/master/dw-i3c-master.c     | 2 +-
 drivers/i3c/master/i3c-master-cdns.c   | 2 +-
 drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
 drivers/i3c/master/svc-i3c-master.c    | 2 +-
 include/linux/i3c/master.h             | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)
drivers/i3c/master/dw-i3c-master.c +1 -1
index d09ea5b6467c..54c7d86997d5 100644
@@ -1079,7 +1079,7 @@ static void dw_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
 }
 
 static int dw_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				   const struct i2c_msg *i2c_xfers,
+				   struct i2c_msg *i2c_xfers,
 				   int i2c_nxfers)
 {
 	struct dw_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev);
drivers/i3c/master/i3c-master-cdns.c +1 -1
index 8d69a34986d9..c9e4c129454c 100644
@@ -813,7 +813,7 @@ static int cdns_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int cdns_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				     const struct i2c_msg *xfers, int nxfers)
+				     struct i2c_msg *xfers, int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
 	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index e6e482a259b4..a408feac3e9e 100644
@@ -367,7 +367,7 @@ static int i3c_hci_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
-			     const struct i2c_msg *i2c_xfers, int nxfers)
+			     struct i2c_msg *i2c_xfers, int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
 	struct i3c_hci *hci = to_i3c_hci(m);
drivers/i3c/master/svc-i3c-master.c +1 -1
index c1ee3828e7ee..24bd701b5de0 100644
@@ -1584,7 +1584,7 @@ static int svc_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
 }
 
 static int svc_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				    const struct i2c_msg *xfers,
+				    struct i2c_msg *xfers,
 				    int nxfers)
 {
 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
include/linux/i3c/master.h +1 -1
index 12d532b012c5..c67922ece617 100644
@@ -475,7 +475,7 @@ struct i3c_master_controller_ops {
 	int (*attach_i2c_dev)(struct i2c_dev_desc *dev);
 	void (*detach_i2c_dev)(struct i2c_dev_desc *dev);
 	int (*i2c_xfers)(struct i2c_dev_desc *dev,
-			 const struct i2c_msg *xfers, int nxfers);
+			 struct i2c_msg *xfers, int nxfers);
 	int (*request_ibi)(struct i3c_dev_desc *dev,
 			   const struct i3c_ibi_setup *req);
 	void (*free_ibi)(struct i3c_dev_desc *dev);
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 i3c: mipi-i3c-hci: Use DMA-safe buffer for I2C transfers

The i2c_get_dma_safe_msg_buf function is used to ensure safe handling of I2C messages in HCI DMA mode.

---
 drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index a408feac3e9e..3567cd566339 100644
@@ -382,7 +382,7 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
 		return -ENOMEM;
 
 	for (i = 0; i < nxfers; i++) {
-		xfer[i].data = i2c_xfers[i].buf;
+		xfer[i].data = i2c_get_dma_safe_msg_buf(&i2c_xfers[i], 1);
 		xfer[i].data_len = i2c_xfers[i].len;
 		xfer[i].rnw = i2c_xfers[i].flags & I2C_M_RD;
 		hci->cmd->prep_i2c_xfer(hci, dev, &xfer[i]);
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v1 2/2] i3c: mipi-i3c-hci: Use DMA-safe buffer for I2C transfers

drivers/i3c/master/mipi-i3c-hci/core.c static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev, · line 385

Quoted 3 lines
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
-		xfer[i].data = i2c_xfers[i].buf;
+		xfer[i].data = i2c_get_dma_safe_msg_buf(&i2c_xfers[i], 1);

Where is a 'get' there also should be a 'put'.

author
#3 lore raw

Re: [PATCH v1 2/2] i3c: mipi-i3c-hci: Use DMA-safe buffer for I2C transfers

Quoted 6 lines
Quoted 4 lines

-             xfer[i].data = i2c_xfers[i].buf;

+             xfer[i].data = i2c_get_dma_safe_msg_buf(&i2c_xfers[i], 1);

Where is a 'get' there also should be a 'put'.

Thanks.
I will add the i2c_put_dma_safe_msg_buf in the next version.

Billy Tsai

#4 lore raw

Re: [PATCH v1 2/2] i3c: mipi-i3c-hci: Use DMA-safe buffer for I2C transfers

In reply to Billy Tsai
Quoted 13 lines · Billy Tsai

The i2c_get_dma_safe_msg_buf function is used to ensure safe handling of I2C messages in HCI DMA mode.

---
  drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/core.c +0 -0
index a408feac3e9e..3567cd566339 100644

drivers/i3c/master/mipi-i3c-hci/core.c static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev, · line 385

Quoted 5 lines
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
@@ -382,7 +382,7 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
  		return -ENOMEM;
 
  	for (i = 0; i < nxfers; i++) {
-		xfer[i].data = i2c_xfers[i].buf;
+		xfer[i].data = i2c_get_dma_safe_msg_buf(&i2c_xfers[i], 1);

Should be i2c_xfers[i].buf ? IIUC, it's iterating over many message buffers upto nxfers.

Quoted 3 lines
drivers/i3c/master/mipi-i3c-hci/core.c +0 -0
  		xfer[i].data_len = i2c_xfers[i].len;
  		xfer[i].rnw = i2c_xfers[i].flags & I2C_M_RD;
  		hci->cmd->prep_i2c_xfer(hci, dev, &xfer[i]);
Signature · 3 lines
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#5 lore raw

Re: [PATCH v1 2/2] i3c: mipi-i3c-hci: Use DMA-safe buffer for I2C transfers

In reply to Billy Tsai
Quoted 13 lines · Billy Tsai

The i2c_get_dma_safe_msg_buf function is used to ensure safe handling of I2C messages in HCI DMA mode.

---
  drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/core.c +0 -0
index a408feac3e9e..3567cd566339 100644

drivers/i3c/master/mipi-i3c-hci/core.c static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev, · line 388

Quoted 8 lines
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
@@ -382,7 +382,7 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
  		return -ENOMEM;
 
  	for (i = 0; i < nxfers; i++) {
-		xfer[i].data = i2c_xfers[i].buf;
+		xfer[i].data = i2c_get_dma_safe_msg_buf(&i2c_xfers[i], 1);
  		xfer[i].data_len = i2c_xfers[i].len;
  		xfer[i].rnw = i2c_xfers[i].flags & I2C_M_RD;
  		hci->cmd->prep_i2c_xfer(hci, dev, &xfer[i]);

This allow to remove calls to i3c_hci_alloc_safe_xfer_buf() and i3c_hci_free_safe_xfer_buf() in i3c_hci_i2c_xfers().

I was pondering the i2c_get_dma_safe_msg_buf() when making commit 4afd72876942 ("i3c: mipi-i3c-hci: Add DMA bounce buffer for private transfers") but decided to reuse the same (own) helper for both i3c_hci_priv_xfers() and i3c_hci_i2c_xfers().

In that sense I'd modify the commit log a bit that instead of ensuring safe handling patch switches from custom helper to i2c_get_dma_safe_msg_buf() and i2c_put_dma_safe_msg_buf() (please note the comment from Wolfram).

Reviewed-by on v1 1/2
#6 lore raw
In reply to Billy Tsai
Quoted 9 lines · Billy Tsai

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

Makes sense to me:

#7 lore raw
In reply to Billy Tsai

Good Change Billy. Thanks for bringing this to i3c.

Quoted 22 lines · Billy Tsai

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

---
  drivers/i3c/master/dw-i3c-master.c     | 2 +-
  drivers/i3c/master/i3c-master-cdns.c   | 2 +-
  drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
  drivers/i3c/master/svc-i3c-master.c    | 2 +-
  include/linux/i3c/master.h             | 2 +-
  5 files changed, 5 insertions(+), 5 deletions(-)
drivers/i3c/master/dw-i3c-master.c +0 -0
index d09ea5b6467c..54c7d86997d5 100644

drivers/i3c/master/dw-i3c-master.c static void dw_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev) · line 1082

Quoted 5 lines
drivers/i3c/master/dw-i3c-master.c +1 -1
@@ -1079,7 +1079,7 @@ static void dw_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
  }
 
  static int dw_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				   const struct i2c_msg *i2c_xfers,
+				   struct i2c_msg *i2c_xfers,

what we need is only i2c_xfers->buf to be DMA-safe, not the *i2c_xfers. Right ?

Quoted 16 lines
drivers/i3c/master/dw-i3c-master.c +0 -0
  				   int i2c_nxfers)
  {
  	struct dw_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev);
drivers/i3c/master/i3c-master-cdns.c +1 -1
index 8d69a34986d9..c9e4c129454c 100644
@@ -813,7 +813,7 @@ static int cdns_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
  }
 
  static int cdns_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				     const struct i2c_msg *xfers, int nxfers)
+				     struct i2c_msg *xfers, int nxfers)
 
  {
  	struct i3c_master_controller *m = i2c_dev_get_master(dev);
Quoted 1 line
  	struct cdns_i3c_master *master = to_cdns_i3c_master(m);

we only need to use i2c_get_dma_safe_msg_buf() for the xfers[i].buf being used inside the driver as a protection ? Also I guess same change is required for all the vendor drivers. But Not sure if it's feasible with this patch ?

Quoted 39 lines
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index e6e482a259b4..a408feac3e9e 100644
@@ -367,7 +367,7 @@ static int i3c_hci_priv_xfers(struct i3c_dev_desc *dev,
  }
 
  static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
-			     const struct i2c_msg *i2c_xfers, int nxfers)
+			     struct i2c_msg *i2c_xfers, int nxfers)
  {
  	struct i3c_master_controller *m = i2c_dev_get_master(dev);
  	struct i3c_hci *hci = to_i3c_hci(m);
drivers/i3c/master/svc-i3c-master.c +1 -1
index c1ee3828e7ee..24bd701b5de0 100644
@@ -1584,7 +1584,7 @@ static int svc_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
  }
 
  static int svc_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				    const struct i2c_msg *xfers,
+				    struct i2c_msg *xfers,
  				    int nxfers)
  {
  	struct i3c_master_controller *m = i2c_dev_get_master(dev);
include/linux/i3c/master.h +1 -1
index 12d532b012c5..c67922ece617 100644
@@ -475,7 +475,7 @@ struct i3c_master_controller_ops {
  	int (*attach_i2c_dev)(struct i2c_dev_desc *dev);
  	void (*detach_i2c_dev)(struct i2c_dev_desc *dev);
  	int (*i2c_xfers)(struct i2c_dev_desc *dev,
-			 const struct i2c_msg *xfers, int nxfers);
+			 struct i2c_msg *xfers, int nxfers);
  	int (*request_ibi)(struct i3c_dev_desc *dev,
  			   const struct i3c_ibi_setup *req);
  	void (*free_ibi)(struct i3c_dev_desc *dev);
Signature · 3 lines
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#8 lore raw
Quoted 3 lines

what we need is only i2c_xfers->buf to be DMA-safe, not the *i2c_xfers. Right ?

Yes.

Quoted 2 lines

Also I guess same change is required for all the vendor drivers. But Not sure if it's feasible with this patch ?

I also wondered if the I3C core should maybe provide this, but then I saw that currently only the mipi-hci driver uses DMA and concluded that per-driver is better. At least for now.

#9 lore raw
In reply to Billy Tsai
Quoted 7 lines · Billy Tsai

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

Why not modify i2c_get_dma_safe_msg_buf() to accept 'const struct i2c_msg' instead?

Quoted 8 lines
---
 drivers/i3c/master/dw-i3c-master.c     | 2 +-
 drivers/i3c/master/i3c-master-cdns.c   | 2 +-
 drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
 drivers/i3c/master/svc-i3c-master.c    | 2 +-
 include/linux/i3c/master.h             | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)
Acked-by on v1 1/2
#10 lore raw
In reply to Billy Tsai

Looks good to me.

Quoted 9 lines · Billy Tsai

The change is necessary to enable the use of the i2c_get_dma_safe_msg_buf() API, which requires a non-const struct i2c_msg * to operate. The i2c_get_dma_safe_msg_buf() function ensures safe handling of I2C messages when using DMA, making it essential for scenarios where DMA transfers are involved. By removing the const qualifier, this patch allows drivers to prepare and manage DMA-safe buffers directly.

Quoted 73 lines
---
  drivers/i3c/master/dw-i3c-master.c     | 2 +-
  drivers/i3c/master/i3c-master-cdns.c   | 2 +-
  drivers/i3c/master/mipi-i3c-hci/core.c | 2 +-
  drivers/i3c/master/svc-i3c-master.c    | 2 +-
  include/linux/i3c/master.h             | 2 +-
  5 files changed, 5 insertions(+), 5 deletions(-)
drivers/i3c/master/dw-i3c-master.c +1 -1
index d09ea5b6467c..54c7d86997d5 100644
@@ -1079,7 +1079,7 @@ static void dw_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
  }
 
  static int dw_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				   const struct i2c_msg *i2c_xfers,
+				   struct i2c_msg *i2c_xfers,
  				   int i2c_nxfers)
  {
  	struct dw_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev);
drivers/i3c/master/i3c-master-cdns.c +1 -1
index 8d69a34986d9..c9e4c129454c 100644
@@ -813,7 +813,7 @@ static int cdns_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
  }
 
  static int cdns_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				     const struct i2c_msg *xfers, int nxfers)
+				     struct i2c_msg *xfers, int nxfers)
  {
  	struct i3c_master_controller *m = i2c_dev_get_master(dev);
  	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index e6e482a259b4..a408feac3e9e 100644
@@ -367,7 +367,7 @@ static int i3c_hci_priv_xfers(struct i3c_dev_desc *dev,
  }
 
  static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
-			     const struct i2c_msg *i2c_xfers, int nxfers)
+			     struct i2c_msg *i2c_xfers, int nxfers)
  {
  	struct i3c_master_controller *m = i2c_dev_get_master(dev);
  	struct i3c_hci *hci = to_i3c_hci(m);
drivers/i3c/master/svc-i3c-master.c +1 -1
index c1ee3828e7ee..24bd701b5de0 100644
@@ -1584,7 +1584,7 @@ static int svc_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
  }
 
  static int svc_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
-				    const struct i2c_msg *xfers,
+				    struct i2c_msg *xfers,
  				    int nxfers)
  {
  	struct i3c_master_controller *m = i2c_dev_get_master(dev);
include/linux/i3c/master.h +1 -1
index 12d532b012c5..c67922ece617 100644
@@ -475,7 +475,7 @@ struct i3c_master_controller_ops {
  	int (*attach_i2c_dev)(struct i2c_dev_desc *dev);
  	void (*detach_i2c_dev)(struct i2c_dev_desc *dev);
  	int (*i2c_xfers)(struct i2c_dev_desc *dev,
-			 const struct i2c_msg *xfers, int nxfers);
+			 struct i2c_msg *xfers, int nxfers);
  	int (*request_ibi)(struct i3c_dev_desc *dev,
  			   const struct i3c_ibi_setup *req);
  	void (*free_ibi)(struct i3c_dev_desc *dev);