cmpxchg.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef _ASM_IA64_CMPXCHG_H
  2. #define _ASM_IA64_CMPXCHG_H
  3. /*
  4. * Compare/Exchange, forked from asm/intrinsics.h
  5. * which was:
  6. *
  7. * Copyright (C) 2002-2003 Hewlett-Packard Co
  8. * David Mosberger-Tang <davidm@hpl.hp.com>
  9. */
  10. #ifndef __ASSEMBLY__
  11. #include <linux/types.h>
  12. /* include compiler specific intrinsics */
  13. #include <asm/ia64regs.h>
  14. #ifdef __INTEL_COMPILER
  15. # include <asm/intel_intrin.h>
  16. #else
  17. # include <asm/gcc_intrin.h>
  18. #endif
  19. /*
  20. * This function doesn't exist, so you'll get a linker error if
  21. * something tries to do an invalid xchg().
  22. */
  23. extern void ia64_xchg_called_with_bad_pointer(void);
  24. #define __xchg(x, ptr, size) \
  25. ({ \
  26. unsigned long __xchg_result; \
  27. \
  28. switch (size) { \
  29. case 1: \
  30. __xchg_result = ia64_xchg1((__u8 *)ptr, x); \
  31. break; \
  32. \
  33. case 2: \
  34. __xchg_result = ia64_xchg2((__u16 *)ptr, x); \
  35. break; \
  36. \
  37. case 4: \
  38. __xchg_result = ia64_xchg4((__u32 *)ptr, x); \
  39. break; \
  40. \
  41. case 8: \
  42. __xchg_result = ia64_xchg8((__u64 *)ptr, x); \
  43. break; \
  44. default: \
  45. ia64_xchg_called_with_bad_pointer(); \
  46. } \
  47. __xchg_result; \
  48. })
  49. #define xchg(ptr, x) \
  50. ((__typeof__(*(ptr))) __xchg((unsigned long) (x), (ptr), sizeof(*(ptr))))
  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. /*
  57. * This function doesn't exist, so you'll get a linker error
  58. * if something tries to do an invalid cmpxchg().
  59. */
  60. extern long ia64_cmpxchg_called_with_bad_pointer(void);
  61. #define ia64_cmpxchg(sem, ptr, old, new, size) \
  62. ({ \
  63. __u64 _o_, _r_; \
  64. \
  65. switch (size) { \
  66. case 1: \
  67. _o_ = (__u8) (long) (old); \
  68. break; \
  69. case 2: \
  70. _o_ = (__u16) (long) (old); \
  71. break; \
  72. case 4: \
  73. _o_ = (__u32) (long) (old); \
  74. break; \
  75. case 8: \
  76. _o_ = (__u64) (long) (old); \
  77. break; \
  78. default: \
  79. break; \
  80. } \
  81. switch (size) { \
  82. case 1: \
  83. _r_ = ia64_cmpxchg1_##sem((__u8 *) ptr, new, _o_); \
  84. break; \
  85. \
  86. case 2: \
  87. _r_ = ia64_cmpxchg2_##sem((__u16 *) ptr, new, _o_); \
  88. break; \
  89. \
  90. case 4: \
  91. _r_ = ia64_cmpxchg4_##sem((__u32 *) ptr, new, _o_); \
  92. break; \
  93. \
  94. case 8: \
  95. _r_ = ia64_cmpxchg8_##sem((__u64 *) ptr, new, _o_); \
  96. break; \
  97. \
  98. default: \
  99. _r_ = ia64_cmpxchg_called_with_bad_pointer(); \
  100. break; \
  101. } \
  102. (__typeof__(old)) _r_; \
  103. })
  104. #define cmpxchg_acq(ptr, o, n) \
  105. ia64_cmpxchg(acq, (ptr), (o), (n), sizeof(*(ptr)))
  106. #define cmpxchg_rel(ptr, o, n) \
  107. ia64_cmpxchg(rel, (ptr), (o), (n), sizeof(*(ptr)))
  108. /*
  109. * Worse still - early processor implementations actually just ignored
  110. * the acquire/release and did a full fence all the time. Unfortunately
  111. * this meant a lot of badly written code that used .acq when they really
  112. * wanted .rel became legacy out in the wild - so when we made a cpu
  113. * that strictly did the .acq or .rel ... all that code started breaking - so
  114. * we had to back-pedal and keep the "legacy" behavior of a full fence :-(
  115. */
  116. /* for compatibility with other platforms: */
  117. #define cmpxchg(ptr, o, n) cmpxchg_acq((ptr), (o), (n))
  118. #define cmpxchg64(ptr, o, n) cmpxchg_acq((ptr), (o), (n))
  119. #define cmpxchg_local cmpxchg
  120. #define cmpxchg64_local cmpxchg64
  121. #ifdef CONFIG_IA64_DEBUG_CMPXCHG
  122. # define CMPXCHG_BUGCHECK_DECL int _cmpxchg_bugcheck_count = 128;
  123. # define CMPXCHG_BUGCHECK(v) \
  124. do { \
  125. if (_cmpxchg_bugcheck_count-- <= 0) { \
  126. void *ip; \
  127. extern int printk(const char *fmt, ...); \
  128. ip = (void *) ia64_getreg(_IA64_REG_IP); \
  129. printk("CMPXCHG_BUGCHECK: stuck at %p on word %p\n", ip, (v));\
  130. break; \
  131. } \
  132. } while (0)
  133. #else /* !CONFIG_IA64_DEBUG_CMPXCHG */
  134. # define CMPXCHG_BUGCHECK_DECL
  135. # define CMPXCHG_BUGCHECK(v)
  136. #endif /* !CONFIG_IA64_DEBUG_CMPXCHG */
  137. #endif /* !__ASSEMBLY__ */
  138. #endif /* _ASM_IA64_CMPXCHG_H */