[PATCH v2 06/10] can: grcan: optimize DMA by 32-bit accesses

Arun Muthusamy posted 10 patches 1 month, 2 weeks ago
[PATCH v2 06/10] can: grcan: optimize DMA by 32-bit accesses
Posted by Arun Muthusamy 1 month, 2 weeks ago
From: Daniel Hellstrom <daniel@gaisler.com>

Optimizes DMA transfers in the GRCAN driver by reorganizing
data handling to use 32-bit accesses instead of individual
byte accesses.

Signed-off-by: Daniel Hellstrom <daniel@gaisler.com>
Signed-off-by: Arun Muthusamy <arun.muthusamy@gaisler.com>
---
 drivers/net/can/grcan.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
index cac85fbe6acf..8a6c59473cf4 100644
--- a/drivers/net/can/grcan.c
+++ b/drivers/net/can/grcan.c
@@ -1218,7 +1218,7 @@ static int grcan_receive(struct net_device *dev, int budget)
 	struct sk_buff *skb;
 	u32 wr, rd, startrd;
 	u32 *slot;
-	u32 i, rtr, eff, j, shift;
+	u32 rtr, eff;
 	int work_done = 0;
 
 	rd = grcan_read_reg(&regs->rxrd);
@@ -1254,10 +1254,10 @@ static int grcan_receive(struct net_device *dev, int budget)
 		if (rtr) {
 			cf->can_id |= CAN_RTR_FLAG;
 		} else {
-			for (i = 0; i < cf->len; i++) {
-				j = GRCAN_MSG_DATA_SLOT_INDEX(i);
-				shift = GRCAN_MSG_DATA_SHIFT(i);
-				cf->data[i] = (u8)(slot[j] >> shift);
+			if (cf->can_dlc > 0) {
+				memcpy(cf->data, &slot[2], sizeof(u32));
+				if (cf->can_dlc > 4)
+					memcpy(cf->data + 4, &slot[3], sizeof(u32));
 			}
 
 			stats->rx_bytes += cf->len;
@@ -1397,8 +1397,7 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
 	u32 id, txwr, txrd, space, txctrl;
 	int slotindex;
 	u32 *slot;
-	u32 i, rtr, eff, dlc, tmp, err;
-	int j, shift;
+	u32 rtr, eff, dlc, tmp, err;
 	unsigned long flags;
 	u32 oneshotmode = priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT;
 
@@ -1451,10 +1450,11 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
 	slot[1] = ((dlc << GRCAN_MSG_DLC_BIT) & GRCAN_MSG_DLC);
 	slot[2] = 0;
 	slot[3] = 0;
-	for (i = 0; i < dlc; i++) {
-		j = GRCAN_MSG_DATA_SLOT_INDEX(i);
-		shift = GRCAN_MSG_DATA_SHIFT(i);
-		slot[j] |= cf->data[i] << shift;
+	if (dlc > 0) {
+		memcpy(&slot[2], cf->data, sizeof(u32));
+		slot[2] = *(u32 *)(cf->data);
+		if (dlc > 4)
+			memcpy(&slot[3], cf->data + 4, sizeof(u32));
 	}
 
 	/* Checking that channel has not been disabled. These cases
-- 
2.51.0
Re: [PATCH v2 06/10] can: grcan: optimize DMA by 32-bit accesses
Posted by Vincent Mailhol 1 month, 2 weeks ago
On 23/12/2025 at 11:56, Arun Muthusamy wrote:
> From: Daniel Hellstrom <daniel@gaisler.com>
> 
> Optimizes DMA transfers in the GRCAN driver by reorganizing
> data handling to use 32-bit accesses instead of individual
> byte accesses.
> 
> Signed-off-by: Daniel Hellstrom <daniel@gaisler.com>
> Signed-off-by: Arun Muthusamy <arun.muthusamy@gaisler.com>
> ---
>  drivers/net/can/grcan.c | 22 +++++++++++-----------
>  1 file changed, 11 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
> index cac85fbe6acf..8a6c59473cf4 100644
> --- a/drivers/net/can/grcan.c
> +++ b/drivers/net/can/grcan.c
> @@ -1218,7 +1218,7 @@ static int grcan_receive(struct net_device *dev, int budget)
>  	struct sk_buff *skb;
>  	u32 wr, rd, startrd;
>  	u32 *slot;
> -	u32 i, rtr, eff, j, shift;
> +	u32 rtr, eff;
>  	int work_done = 0;
>  
>  	rd = grcan_read_reg(&regs->rxrd);
> @@ -1254,10 +1254,10 @@ static int grcan_receive(struct net_device *dev, int budget)
>  		if (rtr) {
>  			cf->can_id |= CAN_RTR_FLAG;
>  		} else {
> -			for (i = 0; i < cf->len; i++) {
> -				j = GRCAN_MSG_DATA_SLOT_INDEX(i);
> -				shift = GRCAN_MSG_DATA_SHIFT(i);
> -				cf->data[i] = (u8)(slot[j] >> shift);
> +			if (cf->can_dlc > 0) {
> +				memcpy(cf->data, &slot[2], sizeof(u32));
> +				if (cf->can_dlc > 4)
> +					memcpy(cf->data + 4, &slot[3], sizeof(u32));
>  			}

Nitpick: you may instead do:

	if (cf->can_dlc > 0)
		memcpy(cf->data, &slot[2], sizeof(u32));
	if (cf->can_dlc > 4)
		memcpy(cf->data + 4, &slot[3], sizeof(u32));

and let the compiler take care of the optimization for you ;)

>  
>  			stats->rx_bytes += cf->len;
> @@ -1397,8 +1397,7 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
>  	u32 id, txwr, txrd, space, txctrl;
>  	int slotindex;
>  	u32 *slot;
> -	u32 i, rtr, eff, dlc, tmp, err;
> -	int j, shift;
> +	u32 rtr, eff, dlc, tmp, err;
>  	unsigned long flags;
>  	u32 oneshotmode = priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT;
>  
> @@ -1451,10 +1450,11 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
>  	slot[1] = ((dlc << GRCAN_MSG_DLC_BIT) & GRCAN_MSG_DLC);
>  	slot[2] = 0;
>  	slot[3] = 0;
> -	for (i = 0; i < dlc; i++) {
> -		j = GRCAN_MSG_DATA_SLOT_INDEX(i);
> -		shift = GRCAN_MSG_DATA_SHIFT(i);
> -		slot[j] |= cf->data[i] << shift;
> +	if (dlc > 0) {
> +		memcpy(&slot[2], cf->data, sizeof(u32));
> +		slot[2] = *(u32 *)(cf->data);

Why do you have both the memcpy() and the "slot[2] =" assignment?

> +		if (dlc > 4)
> +			memcpy(&slot[3], cf->data + 4, sizeof(u32));
>  	}
>  
>  	/* Checking that channel has not been disabled. These cases

Don't forget also to remove the unused macros.


Yours sincerely,
Vincent Mailhol