cmpxchg_64.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* 64-bit atomic xchg() and cmpxchg() definitions.
  2. *
  3. * Copyright (C) 1996, 1997, 2000 David S. Miller (davem@redhat.com)
  4. */
  5. #ifndef __ARCH_SPARC64_CMPXCHG__
  6. #define __ARCH_SPARC64_CMPXCHG__
  7. static inline unsigned long xchg32(__volatile__ unsigned int *m, unsigned int val)
  8. {
  9. unsigned long tmp1, tmp2;
  10. __asm__ __volatile__(
  11. " mov %0, %1\n"
  12. "1: lduw [%4], %2\n"
  13. " cas [%4], %2, %0\n"
  14. " cmp %2, %0\n"
  15. " bne,a,pn %%icc, 1b\n"
  16. " mov %1, %0\n"
  17. : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
  18. : "0" (val), "r" (m)
  19. : "cc", "memory");
  20. return val;
  21. }
  22. static inline unsigned long xchg64(__volatile__ unsigned long *m, unsigned long val)
  23. {
  24. unsigned long tmp1, tmp2;
  25. __asm__ __volatile__(
  26. " mov %0, %1\n"
  27. "1: ldx [%4], %2\n"
  28. " casx [%4], %2, %0\n"
  29. " cmp %2, %0\n"
  30. " bne,a,pn %%xcc, 1b\n"
  31. " mov %1, %0\n"
  32. : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
  33. : "0" (val), "r" (m)
  34. : "cc", "memory");
  35. return val;
  36. }
  37. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  38. extern void __xchg_called_with_bad_pointer(void);
  39. static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr,
  40. int size)
  41. {
  42. switch (size) {
  43. case 4:
  44. return xchg32(ptr, x);
  45. case 8:
  46. return xchg64(ptr, x);
  47. }
  48. __xchg_called_with_bad_pointer();
  49. return x;
  50. }
  51. /*
  52. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  53. * store NEW in MEM. Return the initial value in MEM. Success is
  54. * indicated by comparing RETURN with OLD.
  55. */
  56. #include <asm-generic/cmpxchg-local.h>
  57. #define __HAVE_ARCH_CMPXCHG 1
  58. static inline unsigned long
  59. __cmpxchg_u32(volatile int *m, int old, int new)
  60. {
  61. __asm__ __volatile__("cas [%2], %3, %0"
  62. : "=&r" (new)
  63. : "0" (new), "r" (m), "r" (old)
  64. : "memory");
  65. return new;
  66. }
  67. static inline unsigned long
  68. __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
  69. {
  70. __asm__ __volatile__("casx [%2], %3, %0"
  71. : "=&r" (new)
  72. : "0" (new), "r" (m), "r" (old)
  73. : "memory");
  74. return new;
  75. }
  76. /* This function doesn't exist, so you'll get a linker error
  77. if something tries to do an invalid cmpxchg(). */
  78. extern void __cmpxchg_called_with_bad_pointer(void);
  79. static inline unsigned long
  80. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  81. {
  82. switch (size) {
  83. case 4:
  84. return __cmpxchg_u32(ptr, old, new);
  85. case 8:
  86. return __cmpxchg_u64(ptr, old, new);
  87. }
  88. __cmpxchg_called_with_bad_pointer();
  89. return old;
  90. }
  91. #define cmpxchg(ptr,o,n) \
  92. ({ \
  93. __typeof__(*(ptr)) _o_ = (o); \
  94. __typeof__(*(ptr)) _n_ = (n); \
  95. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  96. (unsigned long)_n_, sizeof(*(ptr))); \
  97. })
  98. /*
  99. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  100. * them available.
  101. */
  102. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  103. unsigned long old,
  104. unsigned long new, int size)
  105. {
  106. switch (size) {
  107. case 4:
  108. case 8: return __cmpxchg(ptr, old, new, size);
  109. default:
  110. return __cmpxchg_local_generic(ptr, old, new, size);
  111. }
  112. return old;
  113. }
  114. #define cmpxchg_local(ptr, o, n) \
  115. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
  116. (unsigned long)(n), sizeof(*(ptr))))
  117. #define cmpxchg64_local(ptr, o, n) \
  118. ({ \
  119. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  120. cmpxchg_local((ptr), (o), (n)); \
  121. })
  122. #endif /* __ARCH_SPARC64_CMPXCHG__ */