cmpxchg_32.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef __ARCH_SPARC_CMPXCHG__
  12. #define __ARCH_SPARC_CMPXCHG__
  13. unsigned long __xchg_u32(volatile u32 *m, u32 new);
  14. void __xchg_called_with_bad_pointer(void);
  15. static __always_inline unsigned long __arch_xchg(unsigned long x, __volatile__ void * ptr, int size)
  16. {
  17. switch (size) {
  18. case 4:
  19. return __xchg_u32(ptr, x);
  20. }
  21. __xchg_called_with_bad_pointer();
  22. return x;
  23. }
  24. #define arch_xchg(ptr,x) ({(__typeof__(*(ptr)))__arch_xchg((unsigned long)(x),(ptr),sizeof(*(ptr)));})
  25. /* Emulate cmpxchg() the same way we emulate atomics,
  26. * by hashing the object address and indexing into an array
  27. * of spinlocks to get a bit of performance...
  28. *
  29. * See arch/sparc/lib/atomic32.c for implementation.
  30. *
  31. * Cribbed from <asm-parisc/atomic.h>
  32. */
  33. /* bug catcher for when unsupported size is used - won't link */
  34. void __cmpxchg_called_with_bad_pointer(void);
  35. u8 __cmpxchg_u8(volatile u8 *m, u8 old, u8 new_);
  36. u16 __cmpxchg_u16(volatile u16 *m, u16 old, u16 new_);
  37. u32 __cmpxchg_u32(volatile u32 *m, u32 old, u32 new_);
  38. /* don't worry...optimizer will get rid of most of this */
  39. static inline unsigned long
  40. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
  41. {
  42. return
  43. size == 1 ? __cmpxchg_u8(ptr, old, new_) :
  44. size == 2 ? __cmpxchg_u16(ptr, old, new_) :
  45. size == 4 ? __cmpxchg_u32(ptr, old, new_) :
  46. (__cmpxchg_called_with_bad_pointer(), old);
  47. }
  48. #define arch_cmpxchg(ptr, o, n) \
  49. ({ \
  50. __typeof__(*(ptr)) _o_ = (o); \
  51. __typeof__(*(ptr)) _n_ = (n); \
  52. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  53. (unsigned long)_n_, sizeof(*(ptr))); \
  54. })
  55. u64 __cmpxchg_u64(volatile u64 *ptr, u64 old, u64 new);
  56. #define arch_cmpxchg64(ptr, old, new) __cmpxchg_u64(ptr, old, new)
  57. #include <asm-generic/cmpxchg-local.h>
  58. /*
  59. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  60. * them available.
  61. */
  62. #define arch_cmpxchg_local(ptr, o, n) \
  63. ((__typeof__(*(ptr)))__generic_cmpxchg_local((ptr), (unsigned long)(o),\
  64. (unsigned long)(n), sizeof(*(ptr))))
  65. #define arch_cmpxchg64_local(ptr, o, n) __generic_cmpxchg64_local((ptr), (o), (n))
  66. #endif /* __ARCH_SPARC_CMPXCHG__ */