]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools: Add riscv barrier implementation
authorCharlie Jenkins <charlie@rivosinc.com>
Tue, 6 Aug 2024 22:01:23 +0000 (15:01 -0700)
committerPalmer Dabbelt <palmer@rivosinc.com>
Fri, 20 Sep 2024 08:46:46 +0000 (01:46 -0700)
Many of the other architectures use their custom barrier implementations.
Use the barrier code from the kernel sources to optimize barriers in
tools.

Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
Reviewed-by: Andrea Parri <parri.andrea@gmail.com>
Link: https://lore.kernel.org/r/20240806-optimize_ring_buffer_read_riscv-v2-1-ca7e193ae198@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
tools/arch/riscv/include/asm/barrier.h [new file with mode: 0644]
tools/arch/riscv/include/asm/fence.h [new file with mode: 0644]
tools/include/asm/barrier.h

diff --git a/tools/arch/riscv/include/asm/barrier.h b/tools/arch/riscv/include/asm/barrier.h
new file mode 100644 (file)
index 0000000..6997f19
--- /dev/null
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copied from the kernel sources to tools/arch/riscv:
+ *
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2013 Regents of the University of California
+ * Copyright (C) 2017 SiFive
+ */
+
+#ifndef _TOOLS_LINUX_ASM_RISCV_BARRIER_H
+#define _TOOLS_LINUX_ASM_RISCV_BARRIER_H
+
+#include <asm/fence.h>
+#include <linux/compiler.h>
+
+/* These barriers need to enforce ordering on both devices and memory. */
+#define mb()           RISCV_FENCE(iorw, iorw)
+#define rmb()          RISCV_FENCE(ir, ir)
+#define wmb()          RISCV_FENCE(ow, ow)
+
+/* These barriers do not need to enforce ordering on devices, just memory. */
+#define smp_mb()       RISCV_FENCE(rw, rw)
+#define smp_rmb()      RISCV_FENCE(r, r)
+#define smp_wmb()      RISCV_FENCE(w, w)
+
+#define smp_store_release(p, v)                                                \
+do {                                                                   \
+       RISCV_FENCE(rw, w);                                             \
+       WRITE_ONCE(*p, v);                                              \
+} while (0)
+
+#define smp_load_acquire(p)                                            \
+({                                                                     \
+       typeof(*p) ___p1 = READ_ONCE(*p);                               \
+       RISCV_FENCE(r, rw);                                             \
+       ___p1;                                                          \
+})
+
+#endif /* _TOOLS_LINUX_ASM_RISCV_BARRIER_H */
diff --git a/tools/arch/riscv/include/asm/fence.h b/tools/arch/riscv/include/asm/fence.h
new file mode 100644 (file)
index 0000000..37860e8
--- /dev/null
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copied from the kernel sources to tools/arch/riscv:
+ */
+
+#ifndef _ASM_RISCV_FENCE_H
+#define _ASM_RISCV_FENCE_H
+
+#define RISCV_FENCE_ASM(p, s)          "\tfence " #p "," #s "\n"
+#define RISCV_FENCE(p, s) \
+       ({ __asm__ __volatile__ (RISCV_FENCE_ASM(p, s) : : : "memory"); })
+
+#endif /* _ASM_RISCV_FENCE_H */
index 8d378c57cb011414bd16fded0d977f9b64f933cf..0c21678ac5e65fcc2fc77a5d2aaa36a2ddd40524 100644 (file)
@@ -8,6 +8,8 @@
 #include "../../arch/arm64/include/asm/barrier.h"
 #elif defined(__powerpc__)
 #include "../../arch/powerpc/include/asm/barrier.h"
+#elif defined(__riscv)
+#include "../../arch/riscv/include/asm/barrier.h"
 #elif defined(__s390__)
 #include "../../arch/s390/include/asm/barrier.h"
 #elif defined(__sh__)