cmpxchg_32.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* 32-bit atomic xchg() and cmpxchg() definitions.
  3. *
  4. * Copyright (C) 1996 David S. Miller ([email protected])
  5. * Copyright (C) 2000 Anton Blanchard ([email protected])
  6. * Copyright (C) 2007 Kyle McMartin ([email protected])
  7. *
  8. * Additions by Keith M Wesolowski ([email protected]) based
  9. * on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf <[email protected]>.
  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 inline unsigned long __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)))__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. /* we only need to support cmpxchg of a u32 on sparc */
  36. unsigned long __cmpxchg_u32(volatile u32 *m, u32 old, u32 new_);
  37. /* don't worry...optimizer will get rid of most of this */
  38. static inline unsigned long
  39. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
  40. {
  41. switch (size) {
  42. case 4:
  43. return __cmpxchg_u32((u32 *)ptr, (u32)old, (u32)new_);
  44. default:
  45. __cmpxchg_called_with_bad_pointer();
  46. break;
  47. }
  48. return old;
  49. }
  50. #define arch_cmpxchg(ptr, o, n) \
  51. ({ \
  52. __typeof__(*(ptr)) _o_ = (o); \
  53. __typeof__(*(ptr)) _n_ = (n); \
  54. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  55. (unsigned long)_n_, sizeof(*(ptr))); \
  56. })
  57. u64 __cmpxchg_u64(u64 *ptr, u64 old, u64 new);
  58. #define arch_cmpxchg64(ptr, old, new) __cmpxchg_u64(ptr, old, new)
  59. #include <asm-generic/cmpxchg-local.h>
  60. /*
  61. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  62. * them available.
  63. */
  64. #define arch_cmpxchg_local(ptr, o, n) \
  65. ((__typeof__(*(ptr)))__generic_cmpxchg_local((ptr), (unsigned long)(o),\
  66. (unsigned long)(n), sizeof(*(ptr))))
  67. #define arch_cmpxchg64_local(ptr, o, n) __generic_cmpxchg64_local((ptr), (o), (n))
  68. #endif /* __ARCH_SPARC_CMPXCHG__ */