cmpxchg_32.h 2.5 KB

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