Skip to contentSkip to conversation

sparc32,leon: Allow and use CAS for atomic operations for LEON

PATCH In review Andreas Larsson 2 participants 4 messages Nov 14, 2017 – Nov 29, 2017

Subsystems (3)
lore
Share & tools
lore

Andreas Larsson

author maintainer
#0 lore raw

cover sparc32,leon: Allow and use CAS for atomic operations for LEON

The first patch makes sure that CAS can be assembled and the second patch adds the configurable possibility to use the CASA instruction rather than spinlock based emulation for atomic operations.

Andreas Larsson (2):
sparc32,leon: Build with -mcpu=leon3 for SPARC_LEON sparc32,leon: Add support for atomic operations with CAS

 arch/sparc/Kconfig                  |  17 ++++-
 arch/sparc/Makefile                 |  10 ++-
 arch/sparc/include/asm/atomic_32.h  |  55 ++++++++++++++-
 arch/sparc/include/asm/bitops_32.h  |  13 ++++
 arch/sparc/include/asm/cmpxchg_32.h |  37 ++++++++++
 arch/sparc/lib/Makefile             |   4 +-
 arch/sparc/lib/atomic_cas_32.S      |  69 +++++++++++++++++++
 arch/sparc/lib/bitops_cas_32.S      | 130 ++++++++++++++++++++++++++++++++++++
 8 files changed, 326 insertions(+), 9 deletions(-)
 create mode 100644 arch/sparc/lib/atomic_cas_32.S
 create mode 100644 arch/sparc/lib/bitops_cas_32.S
Signature · 1 line
2.8.0
author maintainer
#1 lore raw

PATCH 1/2 sparc32,leon: Build with -mcpu=leon3 for SPARC_LEON

Apart from using LEON3 instruction timing it allows for usage of the CASA instruction.

---
 arch/sparc/Makefile | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/sparc/Makefile +8 -2
index 8496a07..673ee73 100644
@@ -24,14 +24,20 @@ LDFLAGS        := -m elf32_sparc
 export BITS    := 32
 UTS_MACHINE    := sparc
 
+ifeq ($(CONFIG_SPARC_LEON),y)
+SPARC_MCPU=leon3
+else
+SPARC_MCPU=v8
+endif
+
 # We are adding -Wa,-Av8 to KBUILD_CFLAGS to deal with a specs bug in some
 # versions of gcc.  Some gcc versions won't pass -Av8 to binutils when you
 # give -mcpu=v8.  This silently worked with older bintutils versions but
 # does not any more.
-KBUILD_CFLAGS  += -m32 -mcpu=v8 -pipe -mno-fpu -fcall-used-g5 -fcall-used-g7
+KBUILD_CFLAGS  += -m32 -mcpu=$(SPARC_MCPU) -pipe -mno-fpu -fcall-used-g5 -fcall-used-g7
 KBUILD_CFLAGS  += -Wa,-Av8
 
-KBUILD_AFLAGS  += -m32 -Wa,-Av8
+KBUILD_AFLAGS  += -m32 -mcpu=$(SPARC_MCPU) -Wa,-Av8
 
 else
 #####
Signature · 1 line
2.8.0
maintainer
#2 lore raw

Re: [PATCH 1/2] sparc32,leon: Build with -mcpu=leon3 for SPARC_LEON

From: Andreas Larsson <[email protected]>
Date: Tue, 14 Nov 2017 15:03:42 +0100

Quoted 4 lines

Apart from using LEON3 instruction timing it allows for usage of the CASA instruction.

Up until this point the LEON kconfig option is largely passive.

All of the sparc_cpu_type logic drives what parts of the kernel are used based upon the run-time detected cpu type.

But with this change, it completely breaks the 32-bit sparc kernel on any non-Leon processor.

I wish you would reconsider this approach, and instead do hardcoded assembler instruction opcodes to use "casa" in assembler, and run time detect CAS support in the 32-bit cpu.

Thank you.

author maintainer
#3 lore raw

PATCH 2/2 sparc32,leon: Add support for atomic operations with CAS

In reply to Andreas Larsson

Adds support for doing atomic operations using CAS on LEON instead of using the spinlock based emulated atomics. It is configurable for LEON as not all LEON hardware supports the CASA instruction.

This does not support cmpxchg64 as no 64-bit CAS instruction exists on LEON.

---
 arch/sparc/Kconfig                  |  17 ++++-
 arch/sparc/include/asm/atomic_32.h  |  55 ++++++++++++++-
 arch/sparc/include/asm/bitops_32.h  |  13 ++++
 arch/sparc/include/asm/cmpxchg_32.h |  37 ++++++++++
 arch/sparc/lib/Makefile             |   4 +-
 arch/sparc/lib/atomic_cas_32.S      |  69 +++++++++++++++++++
 arch/sparc/lib/bitops_cas_32.S      | 130 ++++++++++++++++++++++++++++++++++++
 7 files changed, 318 insertions(+), 7 deletions(-)
 create mode 100644 arch/sparc/lib/atomic_cas_32.S
 create mode 100644 arch/sparc/lib/bitops_cas_32.S
arch/sparc/Kconfig +14 -3
index 0be3828..b587579 100644
@@ -228,10 +228,10 @@ config ARCH_MAY_HAVE_PC_FDC
 
 config EMULATED_CMPXCHG
 	bool
-	default y if SPARC32
+	default y if SPARC32_NO_CAS
 	help
-	  Sparc32 does not have a CAS instruction like sparc64. cmpxchg()
-	  is emulated, and therefore it is not completely atomic.
+	  Sparc32 does not in general have a CAS instruction like sparc64.
+	  When cmpxchg() is emulated it is not completely atomic.
 
 # Makefile helpers
 config SPARC32_SMP
@@ -427,6 +427,10 @@ config SERIAL_CONSOLE
 
 	  If unsure, say N.
 
+config SPARC32_NO_CAS
+       bool
+       default y if SPARC32 && !SPARC_LEON_CAS
+
 config SPARC_LEON
 	bool "Sparc Leon processor family"
 	depends on SPARC32
@@ -441,6 +445,13 @@ config SPARC_LEON
 	  toolchain at www.gaisler.com.
 
 if SPARC_LEON
+config SPARC_LEON_CAS
+	bool "Use Compare and Swap"
+	default y
+	---help---
+	  If you say Y here the kernel will use the CASA instruction. Enable
+	  this only if the LEON processor has hardware support for it.
+
 menu "U-Boot options"
 
 config UBOOT_LOAD_ADDR
arch/sparc/include/asm/atomic_32.h +52 -3

67 lines not on this page · raw message

arch/sparc/include/asm/bitops_32.h +13 -0
index 600ed1d..a310e2f 100644
@@ -18,6 +18,17 @@
 #error only <linux/bitops.h> can be included directly
 #endif
 
+#ifdef CONFIG_SPARC_LEON_CAS
+
+int test_and_set_bit(unsigned long nr, volatile unsigned long *addr);
+int test_and_clear_bit(unsigned long nr, volatile unsigned long *addr);
+int test_and_change_bit(unsigned long nr, volatile unsigned long *addr);
+void set_bit(unsigned long nr, volatile unsigned long *addr);
+void clear_bit(unsigned long nr, volatile unsigned long *addr);
+void change_bit(unsigned long nr, volatile unsigned long *addr);
+
+#else /* CONFIG_SPARC_LEON_CAS */
+
 unsigned long ___set_bit(unsigned long *addr, unsigned long mask);
 unsigned long ___clear_bit(unsigned long *addr, unsigned long mask);
 unsigned long ___change_bit(unsigned long *addr, unsigned long mask);
@@ -88,6 +99,8 @@ static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
 	(void) ___change_bit(ADDR, mask);
 }
 
+#endif /* CONFIG_SPARC_LEON_CAS */
+
 #include <asm-generic/bitops/non-atomic.h>
 
 #include <asm-generic/bitops/ffz.h>
arch/sparc/include/asm/cmpxchg_32.h +37 -0
index a0101a5..826510d 100644
@@ -11,14 +11,34 @@
 #ifndef __ARCH_SPARC_CMPXCHG__
 #define __ARCH_SPARC_CMPXCHG__
 
+#ifdef CONFIG_SPARC_LEON_CAS
+
+static inline unsigned long xchg_u32(__volatile__ unsigned long *m, unsigned long val)
+{
+	__asm__ __volatile__("swap [%2], %0"
+			     : "=&r" (val)
+			     : "0" (val), "r" (m)
+			     : "memory");
+	return val;
+}
+
+#else /* CONFIG_SPARC_LEON_CAS */
+
 unsigned long __xchg_u32(volatile u32 *m, u32 new);
+
+#endif /* CONFIG_SPARC_LEON_CAS */
+
 void __xchg_called_with_bad_pointer(void);
 
 static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr, int size)
 {
 	switch (size) {
 	case 4:
+#ifdef CONFIG_SPARC_LEON_CAS
+		return xchg_u32(ptr, x);
+#else
 		return __xchg_u32(ptr, x);
+#endif
 	}
 	__xchg_called_with_bad_pointer();
 	return x;
@@ -37,8 +57,23 @@ static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr, int
 
 /* bug catcher for when unsupported size is used - won't link */
 void __cmpxchg_called_with_bad_pointer(void);
+
 /* we only need to support cmpxchg of a u32 on sparc */
+#ifdef CONFIG_SPARC_LEON_CAS
+static inline unsigned long
+__cmpxchg_u32(volatile int *m, int old, int new)
+{
+	__asm__ __volatile__("casa [%2] 0xb, %3, %0"
+			     : "=&r" (new)
+			     : "0" (new), "r" (m), "r" (old)
+			     : "memory");
+
+	return new;
+}
+
+#else /* CONFIG_SPARC_LEON_CAS */
 unsigned long __cmpxchg_u32(volatile u32 *m, u32 old, u32 new_);
+#endif /* CONFIG_SPARC_LEON_CAS */
 
 /* don't worry...optimizer will get rid of most of this */
 static inline unsigned long
@@ -62,8 +97,10 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
 			(unsigned long)_n_, sizeof(*(ptr)));		\
 })
 
+#ifndef CONFIG_SPARC_LEON_CAS
 u64 __cmpxchg_u64(u64 *ptr, u64 old, u64 new);
 #define cmpxchg64(ptr, old, new)	__cmpxchg_u64(ptr, old, new)
+#endif
 
 #include <asm-generic/cmpxchg-local.h>
 
arch/sparc/lib/Makefile +3 -1
index a1a2d39..1fd575a 100644
@@ -48,5 +48,7 @@ lib-$(CONFIG_SPARC64) += copy_in_user.o memmove.o
 lib-$(CONFIG_SPARC64) += mcount.o ipcsum.o xor.o hweight.o ffs.o
 
 obj-$(CONFIG_SPARC64) += iomap.o
-obj-$(CONFIG_SPARC32) += atomic32.o ucmpdi2.o
+obj-$(CONFIG_SPARC32) += ucmpdi2.o
+obj-$(CONFIG_SPARC32_NO_CAS) += atomic32.o
+obj-$(CONFIG_SPARC_LEON_CAS) += atomic_cas_32.o bitops_cas_32.o
 obj-$(CONFIG_SPARC64) += PeeCeeI.o
arch/sparc/lib/atomic_cas_32.S new file +69 -0

69 lines not on this page · raw message

arch/sparc/lib/bitops_cas_32.S new file +130 -0

130 lines not on this page · raw message

Signature · 1 line
2.8.0