linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes
@ 2023-08-16 23:23 Prabhakar
  2023-08-16 23:23 ` [PATCH v3 1/3] riscv: dma-mapping: only invalidate after DMA, not flush Prabhakar
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Prabhakar @ 2023-08-16 23:23 UTC (permalink / raw)
  To: Arnd Bergmann, Christoph Hellwig, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Anup Patel, Andrew Jones, Jisheng Zhang,
	linux-kernel
  Cc: Geert Uytterhoeven, Samuel Holland, linux-riscv,
	linux-renesas-soc, Prabhakar, Lad Prabhakar

From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>

This patch series is a subset from Arnd's original series [0]. Ive just
picked up the bits required for RISC-V unification of cache flushing.
Remaining patches from the series [0] will be taken care by Arnd soon.

Cheers,
Prabhakar

v2->v3
* Got rid of dma-sync.h and included the changes directly into dma-noncoherent.c

v1->v2
* Dropped others archs
* Included RB and ACKs
* Fixed checkpatch issues

V1:
[0] https://patchwork.kernel.org/project/linux-riscv/cover/20230327121317.4081816-1-arnd@kernel.org/

Arnd Bergmann (2):
  riscv: dma-mapping: only invalidate after DMA, not flush
  riscv: dma-mapping: skip invalidation before bidirectional DMA

Lad Prabhakar (1):
  riscv: dma-mapping: switch over to generic implementation

 arch/riscv/mm/dma-noncoherent.c | 60 ++++++++++++++++++++++++++++-----
 1 file changed, 51 insertions(+), 9 deletions(-)

-- 
2.34.1


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

* [PATCH v3 1/3] riscv: dma-mapping: only invalidate after DMA, not flush
  2023-08-16 23:23 [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes Prabhakar
@ 2023-08-16 23:23 ` Prabhakar
  2023-08-16 23:23 ` [PATCH v3 2/3] riscv: dma-mapping: skip invalidation before bidirectional DMA Prabhakar
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Prabhakar @ 2023-08-16 23:23 UTC (permalink / raw)
  To: Arnd Bergmann, Christoph Hellwig, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Anup Patel, Andrew Jones, Jisheng Zhang,
	linux-kernel
  Cc: Geert Uytterhoeven, Samuel Holland, linux-riscv,
	linux-renesas-soc, Prabhakar, Lad Prabhakar, Palmer Dabbelt

From: Arnd Bergmann <arnd@arndb.de>

No other architecture intentionally writes back dirty cache lines into
a buffer that a device has just finished writing into. If the cache is
clean, this has no effect at all, but if a cacheline in the buffer has
actually been written by the CPU,  there is a driver bug that is likely
made worse by overwriting that buffer.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Acked-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
---
v2->v3
* No change

v1->v2
* Fixed typo drive->driver in commit message
* Included RB and ACKs
---
 arch/riscv/mm/dma-noncoherent.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/mm/dma-noncoherent.c b/arch/riscv/mm/dma-noncoherent.c
index d51a75864e53..94614cf61cdd 100644
--- a/arch/riscv/mm/dma-noncoherent.c
+++ b/arch/riscv/mm/dma-noncoherent.c
@@ -42,7 +42,7 @@ void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
 		break;
 	case DMA_FROM_DEVICE:
 	case DMA_BIDIRECTIONAL:
-		ALT_CMO_OP(flush, vaddr, size, riscv_cbom_block_size);
+		ALT_CMO_OP(inval, vaddr, size, riscv_cbom_block_size);
 		break;
 	default:
 		break;
-- 
2.34.1


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

* [PATCH v3 2/3] riscv: dma-mapping: skip invalidation before bidirectional DMA
  2023-08-16 23:23 [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes Prabhakar
  2023-08-16 23:23 ` [PATCH v3 1/3] riscv: dma-mapping: only invalidate after DMA, not flush Prabhakar
@ 2023-08-16 23:23 ` Prabhakar
  2023-08-27 10:22   ` Jisheng Zhang
  2023-08-16 23:23 ` [PATCH v3 3/3] riscv: dma-mapping: switch over to generic implementation Prabhakar
  2023-08-30 20:30 ` [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes patchwork-bot+linux-riscv
  3 siblings, 1 reply; 7+ messages in thread
From: Prabhakar @ 2023-08-16 23:23 UTC (permalink / raw)
  To: Arnd Bergmann, Christoph Hellwig, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Anup Patel, Andrew Jones, Jisheng Zhang,
	linux-kernel
  Cc: Geert Uytterhoeven, Samuel Holland, linux-riscv,
	linux-renesas-soc, Prabhakar, Lad Prabhakar, Palmer Dabbelt,
	Guo Ren

From: Arnd Bergmann <arnd@arndb.de>

For a DMA_BIDIRECTIONAL transfer, the caches have to be cleaned
first to let the device see data written by the CPU, and invalidated
after the transfer to let the CPU see data written by the device.

riscv also invalidates the caches before the transfer, which does
not appear to serve any purpose.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Acked-by: Palmer Dabbelt <palmer@rivosinc.com>
Acked-by: Guo Ren <guoren@kernel.org>
Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
---
v2->v3
* No change

v1->v2
 * Included RB and ACKs
---
 arch/riscv/mm/dma-noncoherent.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/mm/dma-noncoherent.c b/arch/riscv/mm/dma-noncoherent.c
index 94614cf61cdd..fc6377a64c8d 100644
--- a/arch/riscv/mm/dma-noncoherent.c
+++ b/arch/riscv/mm/dma-noncoherent.c
@@ -25,7 +25,7 @@ void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
 		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
 		break;
 	case DMA_BIDIRECTIONAL:
-		ALT_CMO_OP(flush, vaddr, size, riscv_cbom_block_size);
+		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
 		break;
 	default:
 		break;
-- 
2.34.1


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

* [PATCH v3 3/3] riscv: dma-mapping: switch over to generic implementation
  2023-08-16 23:23 [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes Prabhakar
  2023-08-16 23:23 ` [PATCH v3 1/3] riscv: dma-mapping: only invalidate after DMA, not flush Prabhakar
  2023-08-16 23:23 ` [PATCH v3 2/3] riscv: dma-mapping: skip invalidation before bidirectional DMA Prabhakar
@ 2023-08-16 23:23 ` Prabhakar
  2023-08-30 20:30 ` [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes patchwork-bot+linux-riscv
  3 siblings, 0 replies; 7+ messages in thread
From: Prabhakar @ 2023-08-16 23:23 UTC (permalink / raw)
  To: Arnd Bergmann, Christoph Hellwig, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Anup Patel, Andrew Jones, Jisheng Zhang,
	linux-kernel
  Cc: Geert Uytterhoeven, Samuel Holland, linux-riscv,
	linux-renesas-soc, Prabhakar, Lad Prabhakar

From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>

Add helper functions for cache wback/inval/clean and use them
arch_sync_dma_for_device()/arch_sync_dma_for_cpu() functions. The proposed
changes are in preparation for switching over to generic implementation.

Reorganization of the code is based on the patch (Link[0]) from Arnd.
For now I have dropped CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU check as this
will be enabled by default upon selection of RISCV_DMA_NONCOHERENT
and also dropped arch_dma_mark_dcache_clean().

Link[0]: https://lore.kernel.org/all/20230327121317.4081816-22-arnd@kernel.org/
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
---
Hi Arnd,
I have kept the changes minimal here as compared to your original patch and
dropped arch_dma_mark_dcache_clean() function and config checks for now as we
are currently implementing for riscv only.

Cheers, Prabhakar

v3:
* New patch
---
 arch/riscv/mm/dma-noncoherent.c | 60 ++++++++++++++++++++++++++++-----
 1 file changed, 51 insertions(+), 9 deletions(-)

diff --git a/arch/riscv/mm/dma-noncoherent.c b/arch/riscv/mm/dma-noncoherent.c
index fc6377a64c8d..06b8fea58e20 100644
--- a/arch/riscv/mm/dma-noncoherent.c
+++ b/arch/riscv/mm/dma-noncoherent.c
@@ -12,21 +12,61 @@
 
 static bool noncoherent_supported __ro_after_init;
 
-void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
-			      enum dma_data_direction dir)
+static inline void arch_dma_cache_wback(phys_addr_t paddr, size_t size)
+{
+	void *vaddr = phys_to_virt(paddr);
+
+	ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
+}
+
+static inline void arch_dma_cache_inv(phys_addr_t paddr, size_t size)
+{
+	void *vaddr = phys_to_virt(paddr);
+
+	ALT_CMO_OP(inval, vaddr, size, riscv_cbom_block_size);
+}
+
+static inline void arch_dma_cache_wback_inv(phys_addr_t paddr, size_t size)
 {
 	void *vaddr = phys_to_virt(paddr);
 
+	ALT_CMO_OP(flush, vaddr, size, riscv_cbom_block_size);
+}
+
+static inline bool arch_sync_dma_clean_before_fromdevice(void)
+{
+	return true;
+}
+
+static inline bool arch_sync_dma_cpu_needs_post_dma_flush(void)
+{
+	return true;
+}
+
+void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
+			      enum dma_data_direction dir)
+{
 	switch (dir) {
 	case DMA_TO_DEVICE:
-		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
+		arch_dma_cache_wback(paddr, size);
 		break;
+
 	case DMA_FROM_DEVICE:
-		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
-		break;
+		if (!arch_sync_dma_clean_before_fromdevice()) {
+			arch_dma_cache_inv(paddr, size);
+			break;
+		}
+		fallthrough;
+
 	case DMA_BIDIRECTIONAL:
-		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
+		/* Skip the invalidate here if it's done later */
+		if (IS_ENABLED(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) &&
+		    arch_sync_dma_cpu_needs_post_dma_flush())
+			arch_dma_cache_wback(paddr, size);
+		else
+			arch_dma_cache_wback_inv(paddr, size);
 		break;
+
 	default:
 		break;
 	}
@@ -35,15 +75,17 @@ void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
 			   enum dma_data_direction dir)
 {
-	void *vaddr = phys_to_virt(paddr);
-
 	switch (dir) {
 	case DMA_TO_DEVICE:
 		break;
+
 	case DMA_FROM_DEVICE:
 	case DMA_BIDIRECTIONAL:
-		ALT_CMO_OP(inval, vaddr, size, riscv_cbom_block_size);
+		/* FROM_DEVICE invalidate needed if speculative CPU prefetch only */
+		if (arch_sync_dma_cpu_needs_post_dma_flush())
+			arch_dma_cache_inv(paddr, size);
 		break;
+
 	default:
 		break;
 	}
-- 
2.34.1


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

* Re: [PATCH v3 2/3] riscv: dma-mapping: skip invalidation before bidirectional DMA
  2023-08-16 23:23 ` [PATCH v3 2/3] riscv: dma-mapping: skip invalidation before bidirectional DMA Prabhakar
@ 2023-08-27 10:22   ` Jisheng Zhang
  2023-08-27 19:41     ` Arnd Bergmann
  0 siblings, 1 reply; 7+ messages in thread
From: Jisheng Zhang @ 2023-08-27 10:22 UTC (permalink / raw)
  To: Prabhakar
  Cc: Arnd Bergmann, Christoph Hellwig, Paul Walmsley, Palmer Dabbelt,
	Albert Ou, Conor Dooley, Anup Patel, Andrew Jones, linux-kernel,
	Geert Uytterhoeven, Samuel Holland, linux-riscv,
	linux-renesas-soc, Lad Prabhakar, Palmer Dabbelt, Guo Ren

On Thu, Aug 17, 2023 at 12:23:35AM +0100, Prabhakar wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> For a DMA_BIDIRECTIONAL transfer, the caches have to be cleaned
> first to let the device see data written by the CPU, and invalidated
> after the transfer to let the CPU see data written by the device.
> 
> riscv also invalidates the caches before the transfer, which does
> not appear to serve any purpose.
> 
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
> Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
> Acked-by: Palmer Dabbelt <palmer@rivosinc.com>
> Acked-by: Guo Ren <guoren@kernel.org>
> Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
> ---
> v2->v3
> * No change
> 
> v1->v2
>  * Included RB and ACKs
> ---
>  arch/riscv/mm/dma-noncoherent.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/riscv/mm/dma-noncoherent.c b/arch/riscv/mm/dma-noncoherent.c
> index 94614cf61cdd..fc6377a64c8d 100644
> --- a/arch/riscv/mm/dma-noncoherent.c
> +++ b/arch/riscv/mm/dma-noncoherent.c
> @@ -25,7 +25,7 @@ void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
>  		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
>  		break;
>  	case DMA_BIDIRECTIONAL:
> -		ALT_CMO_OP(flush, vaddr, size, riscv_cbom_block_size);
> +		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);

The code could be simplified a lot since after this patch, the action
is always "clean".

Thanks
>  		break;
>  	default:
>  		break;
> -- 
> 2.34.1
> 

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

* Re: [PATCH v3 2/3] riscv: dma-mapping: skip invalidation before bidirectional DMA
  2023-08-27 10:22   ` Jisheng Zhang
@ 2023-08-27 19:41     ` Arnd Bergmann
  0 siblings, 0 replies; 7+ messages in thread
From: Arnd Bergmann @ 2023-08-27 19:41 UTC (permalink / raw)
  To: Jisheng Zhang, Prabhakar
  Cc: Christoph Hellwig, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Conor.Dooley, Anup Patel, Andrew Jones, linux-kernel,
	Geert Uytterhoeven, Samuel Holland, linux-riscv, Linux-Renesas,
	Lad, Prabhakar, Palmer Dabbelt, guoren

On Sun, Aug 27, 2023, at 06:22, Jisheng Zhang wrote:
> On Thu, Aug 17, 2023 at 12:23:35AM +0100, Prabhakar wrote:
>
> The code could be simplified a lot since after this patch, the action
> is always "clean".
>

I think it's better to leave it at the elaborate version, as I still
want to merge my patch series to unify this with the other architectures,
and this would introduce a few compile-time conditionals in here.

      Arnd

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

* Re: [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes
  2023-08-16 23:23 [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes Prabhakar
                   ` (2 preceding siblings ...)
  2023-08-16 23:23 ` [PATCH v3 3/3] riscv: dma-mapping: switch over to generic implementation Prabhakar
@ 2023-08-30 20:30 ` patchwork-bot+linux-riscv
  3 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+linux-riscv @ 2023-08-30 20:30 UTC (permalink / raw)
  To: Prabhakar
  Cc: linux-riscv, arnd, hch, paul.walmsley, palmer, aou, conor.dooley,
	apatel, ajones, jszhang, linux-kernel, geert+renesas, samuel,
	linux-renesas-soc, prabhakar.mahadev-lad.rj

Hello:

This series was applied to riscv/linux.git (for-next)
by Palmer Dabbelt <palmer@rivosinc.com>:

On Thu, 17 Aug 2023 00:23:33 +0100 you wrote:
> From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
> 
> This patch series is a subset from Arnd's original series [0]. Ive just
> picked up the bits required for RISC-V unification of cache flushing.
> Remaining patches from the series [0] will be taken care by Arnd soon.
> 
> Cheers,
> Prabhakar
> 
> [...]

Here is the summary with links:
  - [v3,1/3] riscv: dma-mapping: only invalidate after DMA, not flush
    https://git.kernel.org/riscv/c/fbfc740ced8f
  - [v3,2/3] riscv: dma-mapping: skip invalidation before bidirectional DMA
    https://git.kernel.org/riscv/c/14be7c16d420
  - [v3,3/3] riscv: dma-mapping: switch over to generic implementation
    https://git.kernel.org/riscv/c/eb76e5111881

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2023-08-30 22:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-16 23:23 [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes Prabhakar
2023-08-16 23:23 ` [PATCH v3 1/3] riscv: dma-mapping: only invalidate after DMA, not flush Prabhakar
2023-08-16 23:23 ` [PATCH v3 2/3] riscv: dma-mapping: skip invalidation before bidirectional DMA Prabhakar
2023-08-27 10:22   ` Jisheng Zhang
2023-08-27 19:41     ` Arnd Bergmann
2023-08-16 23:23 ` [PATCH v3 3/3] riscv: dma-mapping: switch over to generic implementation Prabhakar
2023-08-30 20:30 ` [PATCH v3 0/3] riscv: dma-mapping: unify support for cache flushes patchwork-bot+linux-riscv

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