linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] dmaengine: stm32-dma: avoid 64-bit division in stm32_dma_get_max_width
@ 2021-10-19 15:35 Arnd Bergmann
  2021-10-20 10:45 ` Amelie DELAUNAY
  0 siblings, 1 reply; 2+ messages in thread
From: Arnd Bergmann @ 2021-10-19 15:35 UTC (permalink / raw)
  To: Vinod Koul, Maxime Coquelin, Alexandre Torgue, Amelie Delaunay
  Cc: Arnd Bergmann, Zhang Qilong, dmaengine, linux-stm32,
	linux-arm-kernel, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

Using the % operator on a 64-bit variable is expensive and can
cause a link failure:

arm-linux-gnueabi-ld: drivers/dma/stm32-dma.o: in function `stm32_dma_get_max_width':
stm32-dma.c:(.text+0x170): undefined reference to `__aeabi_uldivmod'
arm-linux-gnueabi-ld: drivers/dma/stm32-dma.o: in function `stm32_dma_set_xfer_param':
stm32-dma.c:(.text+0x1cd4): undefined reference to `__aeabi_uldivmod'

As we know that we just want to check the alignment in
stm32_dma_get_max_width(), there is no need for a full division, and
using a simple mask is a faster replacement.

In stm32_dma_set_xfer_param(), it is possible to pass a non-power-of-two
length, so this does not work. I assume this would in fact be a mistake,
and the hardware does not work correctly with a burst of e.g. 5 bytes
on a five-byte aligned address. Change this to only allow burst
transfers if the address is a multiple of the length, and that length
is a power-of-two number.

Fixes: b20fd5fa310c ("dmaengine: stm32-dma: fix stm32_dma_get_max_width")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 drivers/dma/stm32-dma.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/dma/stm32-dma.c b/drivers/dma/stm32-dma.c
index 2283c500f4ce..102278f7d13e 100644
--- a/drivers/dma/stm32-dma.c
+++ b/drivers/dma/stm32-dma.c
@@ -280,7 +280,7 @@ static enum dma_slave_buswidth stm32_dma_get_max_width(u32 buf_len,
 	       max_width > DMA_SLAVE_BUSWIDTH_1_BYTE)
 		max_width = max_width >> 1;
 
-	if (buf_addr % max_width)
+	if (buf_addr & (max_width - 1))
 		max_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
 
 	return max_width;
@@ -757,7 +757,7 @@ static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
 		 * Set memory burst size - burst not possible if address is not aligned on
 		 * the address boundary equal to the size of the transfer
 		 */
-		if (buf_addr % buf_len)
+		if (!is_power_of_2(buf_len) || (buf_addr & (buf_len -1)))
 			src_maxburst = 1;
 		else
 			src_maxburst = STM32_DMA_MAX_BURST;
@@ -813,7 +813,7 @@ static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
 		 * Set memory burst size - burst not possible if address is not aligned on
 		 * the address boundary equal to the size of the transfer
 		 */
-		if (buf_addr % buf_len)
+		if (!is_power_of_2(buf_len) || (buf_addr & (buf_len -1)))
 			dst_maxburst = 1;
 		else
 			dst_maxburst = STM32_DMA_MAX_BURST;
-- 
2.29.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH] dmaengine: stm32-dma: avoid 64-bit division in stm32_dma_get_max_width
  2021-10-19 15:35 [PATCH] dmaengine: stm32-dma: avoid 64-bit division in stm32_dma_get_max_width Arnd Bergmann
@ 2021-10-20 10:45 ` Amelie DELAUNAY
  0 siblings, 0 replies; 2+ messages in thread
From: Amelie DELAUNAY @ 2021-10-20 10:45 UTC (permalink / raw)
  To: Arnd Bergmann, Vinod Koul, Maxime Coquelin, Alexandre Torgue
  Cc: Arnd Bergmann, Zhang Qilong, dmaengine, linux-stm32,
	linux-arm-kernel, linux-kernel

Hi Arnd,

Thanks for your patch.

On 10/19/21 5:35 PM, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> Using the % operator on a 64-bit variable is expensive and can
> cause a link failure:
> 
> arm-linux-gnueabi-ld: drivers/dma/stm32-dma.o: in function `stm32_dma_get_max_width':
> stm32-dma.c:(.text+0x170): undefined reference to `__aeabi_uldivmod'
> arm-linux-gnueabi-ld: drivers/dma/stm32-dma.o: in function `stm32_dma_set_xfer_param':
> stm32-dma.c:(.text+0x1cd4): undefined reference to `__aeabi_uldivmod'
> 
> As we know that we just want to check the alignment in
> stm32_dma_get_max_width(), there is no need for a full division, and
> using a simple mask is a faster replacement.
> 
> In stm32_dma_set_xfer_param(), it is possible to pass a non-power-of-two
> length, so this does not work. I assume this would in fact be a mistake,
> and the hardware does not work correctly with a burst of e.g. 5 bytes
> on a five-byte aligned address. Change this to only allow burst
> transfers if the address is a multiple of the length, and that length
> is a power-of-two number.
> 
> Fixes: b20fd5fa310c ("dmaengine: stm32-dma: fix stm32_dma_get_max_width")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>   drivers/dma/stm32-dma.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/dma/stm32-dma.c b/drivers/dma/stm32-dma.c
> index 2283c500f4ce..102278f7d13e 100644
> --- a/drivers/dma/stm32-dma.c
> +++ b/drivers/dma/stm32-dma.c
> @@ -280,7 +280,7 @@ static enum dma_slave_buswidth stm32_dma_get_max_width(u32 buf_len,
>   	       max_width > DMA_SLAVE_BUSWIDTH_1_BYTE)
>   		max_width = max_width >> 1;
>   
> -	if (buf_addr % max_width)
> +	if (buf_addr & (max_width - 1))
>   		max_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
>   
>   	return max_width;
> @@ -757,7 +757,7 @@ static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
>   		 * Set memory burst size - burst not possible if address is not aligned on
>   		 * the address boundary equal to the size of the transfer
>   		 */
> -		if (buf_addr % buf_len)
> +		if (!is_power_of_2(buf_len) || (buf_addr & (buf_len -1)))

No need to check !is_power_of_2(buf_len) here.
Just after computing src_maxburst,
	src_best_burst = stm32_dma_get_best_burst(buf_len,
						  src_maxburst,
						  fifoth,
						  src_addr_width);
The configured burst (src_best_best) already take buf_len into account.

So I would remove !is_power_of_2(buf_len) from the if here and fix the 
missing space:

CHECK: spaces preferred around that '-' (ctx:WxV)
#68: FILE: drivers/dma/stm32-dma.c:760:
+		if (!is_power_of_2(buf_len) || (buf_addr & (buf_len -1)))
  		                                                    ^

>   			src_maxburst = 1;
>   		else
>   			src_maxburst = STM32_DMA_MAX_BURST;
> @@ -813,7 +813,7 @@ static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
>   		 * Set memory burst size - burst not possible if address is not aligned on
>   		 * the address boundary equal to the size of the transfer
>   		 */
> -		if (buf_addr % buf_len)
> +		if (!is_power_of_2(buf_len) || (buf_addr & (buf_len -1)))

Ditto.

>   			dst_maxburst = 1;
>   		else
>   			dst_maxburst = STM32_DMA_MAX_BURST;
> 

With these fixes, you can add my
Reviewed-by: Amelie Delaunay <amelie.delaunay@foss.st.com>
Tested-by: Amelie Delaunay <amelie.delaunay@foss.st.com>

Regards,
Amelie

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

end of thread, other threads:[~2021-10-20 10:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-19 15:35 [PATCH] dmaengine: stm32-dma: avoid 64-bit division in stm32_dma_get_max_width Arnd Bergmann
2021-10-20 10:45 ` Amelie DELAUNAY

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