cmpxchg_64.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* 64-bit atomic xchg() and cmpxchg() definitions.
  2. *
  3. * Copyright (C) 1996, 1997, 2000 David S. Miller ([email protected])
  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. 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. static inline unsigned long
  58. __cmpxchg_u32(volatile int *m, int old, int new)
  59. {
  60. __asm__ __volatile__("cas [%2], %3, %0"
  61. : "=&r" (new)
  62. : "0" (new), "r" (m), "r" (old)
  63. : "memory");
  64. return new;
  65. }
  66. static inline unsigned long
  67. __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
  68. {
  69. __asm__ __volatile__("casx [%2], %3, %0"
  70. : "=&r" (new)
  71. : "0" (new), "r" (m), "r" (old)
  72. : "memory");
  73. return new;
  74. }
  75. /* This function doesn't exist, so you'll get a linker error
  76. if something tries to do an invalid cmpxchg(). */
  77. void __cmpxchg_called_with_bad_pointer(void);
  78. static inline unsigned long
  79. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  80. {
  81. switch (size) {
  82. case 4:
  83. return __cmpxchg_u32(ptr, old, new);
  84. case 8:
  85. return __cmpxchg_u64(ptr, old, new);
  86. }
  87. __cmpxchg_called_with_bad_pointer();
  88. return old;
  89. }
  90. #define cmpxchg(ptr,o,n) \
  91. ({ \
  92. __typeof__(*(ptr)) _o_ = (o); \
  93. __typeof__(*(ptr)) _n_ = (n); \
  94. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  95. (unsigned long)_n_, sizeof(*(ptr))); \
  96. })
  97. /*
  98. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  99. * them available.
  100. */
  101. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  102. unsigned long old,
  103. unsigned long new, int size)
  104. {
  105. switch (size) {
  106. case 4:
  107. case 8: return __cmpxchg(ptr, old, new, size);
  108. default:
  109. return __cmpxchg_local_generic(ptr, old, new, size);
  110. }
  111. return old;
  112. }
  113. #define cmpxchg_local(ptr, o, n) \
  114. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
  115. (unsigned long)(n), sizeof(*(ptr))))
  116. #define cmpxchg64_local(ptr, o, n) \
  117. ({ \
  118. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  119. cmpxchg_local((ptr), (o), (n)); \
  120. })
  121. #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
  122. #endif /* __ARCH_SPARC64_CMPXCHG__ */