1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 32-bit atomic xchg() and cmpxchg() definitions. 3 * 4 * Copyright (C) 1996 David S. Miller (davem@davemloft.net) 5 * Copyright (C) 2000 Anton Blanchard (anton@linuxcare.com.au) 6 * Copyright (C) 2007 Kyle McMartin (kyle@parisc-linux.org) 7 * 8 * Additions by Keith M Wesolowski (wesolows@foobazco.org) based 9 * on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>. 10 */ 11 12 #ifndef __ARCH_SPARC_CMPXCHG__ 13 #define __ARCH_SPARC_CMPXCHG__ 14 15 unsigned long __xchg_u32(volatile u32 *m, u32 new); 16 void __xchg_called_with_bad_pointer(void); 17 18 static __always_inline unsigned long __arch_xchg(unsigned long x, __volatile__ void * ptr, int size) 19 { 20 switch (size) { 21 case 4: 22 return __xchg_u32(ptr, x); 23 } 24 __xchg_called_with_bad_pointer(); 25 return x; 26 } 27 28 #define arch_xchg(ptr,x) ({(__typeof__(*(ptr)))__arch_xchg((unsigned long)(x),(ptr),sizeof(*(ptr)));}) 29 30 /* Emulate cmpxchg() the same way we emulate atomics, 31 * by hashing the object address and indexing into an array 32 * of spinlocks to get a bit of performance... 33 * 34 * See arch/sparc/lib/atomic32.c for implementation. 35 * 36 * Cribbed from <asm-parisc/atomic.h> 37 */ 38 39 /* bug catcher for when unsupported size is used - won't link */ 40 void __cmpxchg_called_with_bad_pointer(void); 41 u8 __cmpxchg_u8(volatile u8 *m, u8 old, u8 new_); 42 u16 __cmpxchg_u16(volatile u16 *m, u16 old, u16 new_); 43 u32 __cmpxchg_u32(volatile u32 *m, u32 old, u32 new_); 44 45 /* don't worry...optimizer will get rid of most of this */ 46 static inline unsigned long 47 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size) 48 { 49 return 50 size == 1 ? __cmpxchg_u8(ptr, old, new_) : 51 size == 2 ? __cmpxchg_u16(ptr, old, new_) : 52 size == 4 ? __cmpxchg_u32(ptr, old, new_) : 53 (__cmpxchg_called_with_bad_pointer(), old); 54 } 55 56 #define arch_cmpxchg(ptr, o, n) \ 57 ({ \ 58 __typeof__(*(ptr)) _o_ = (o); \ 59 __typeof__(*(ptr)) _n_ = (n); \ 60 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ 61 (unsigned long)_n_, sizeof(*(ptr))); \ 62 }) 63 64 u64 __cmpxchg_u64(volatile u64 *ptr, u64 old, u64 new); 65 #define arch_cmpxchg64(ptr, old, new) __cmpxchg_u64(ptr, old, new) 66 67 #include <asm-generic/cmpxchg-local.h> 68 69 /* 70 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make 71 * them available. 72 */ 73 #define arch_cmpxchg_local(ptr, o, n) \ 74 ((__typeof__(*(ptr)))__generic_cmpxchg_local((ptr), (unsigned long)(o),\ 75 (unsigned long)(n), sizeof(*(ptr)))) 76 #define arch_cmpxchg64_local(ptr, o, n) __generic_cmpxchg64_local((ptr), (o), (n)) 77 78 #endif /* __ARCH_SPARC_CMPXCHG__ */ 79