cmpxchg.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2003, 06, 07 by Ralf Baechle (ralf@linux-mips.org)
  7. */
  8. #ifndef __ASM_CMPXCHG_H
  9. #define __ASM_CMPXCHG_H
  10. #include <linux/irqflags.h>
  11. #define __HAVE_ARCH_CMPXCHG 1
  12. #define __cmpxchg_asm(ld, st, m, old, new) \
  13. ({ \
  14. __typeof(*(m)) __ret; \
  15. \
  16. if (cpu_has_llsc && R10000_LLSC_WAR) { \
  17. __asm__ __volatile__( \
  18. " .set push \n" \
  19. " .set noat \n" \
  20. " .set mips3 \n" \
  21. "1: " ld " %0, %2 # __cmpxchg_asm \n" \
  22. " bne %0, %z3, 2f \n" \
  23. " .set mips0 \n" \
  24. " move $1, %z4 \n" \
  25. " .set mips3 \n" \
  26. " " st " $1, %1 \n" \
  27. " beqzl $1, 1b \n" \
  28. "2: \n" \
  29. " .set pop \n" \
  30. : "=&r" (__ret), "=R" (*m) \
  31. : "R" (*m), "Jr" (old), "Jr" (new) \
  32. : "memory"); \
  33. } else if (cpu_has_llsc) { \
  34. __asm__ __volatile__( \
  35. " .set push \n" \
  36. " .set noat \n" \
  37. " .set mips3 \n" \
  38. "1: " ld " %0, %2 # __cmpxchg_asm \n" \
  39. " bne %0, %z3, 2f \n" \
  40. " .set mips0 \n" \
  41. " move $1, %z4 \n" \
  42. " .set mips3 \n" \
  43. " " st " $1, %1 \n" \
  44. " beqz $1, 3f \n" \
  45. "2: \n" \
  46. " .subsection 2 \n" \
  47. "3: b 1b \n" \
  48. " .previous \n" \
  49. " .set pop \n" \
  50. : "=&r" (__ret), "=R" (*m) \
  51. : "R" (*m), "Jr" (old), "Jr" (new) \
  52. : "memory"); \
  53. } else { \
  54. unsigned long __flags; \
  55. \
  56. raw_local_irq_save(__flags); \
  57. __ret = *m; \
  58. if (__ret == old) \
  59. *m = new; \
  60. raw_local_irq_restore(__flags); \
  61. } \
  62. \
  63. __ret; \
  64. })
  65. /*
  66. * This function doesn't exist, so you'll get a linker error
  67. * if something tries to do an invalid cmpxchg().
  68. */
  69. extern void __cmpxchg_called_with_bad_pointer(void);
  70. #define __cmpxchg(ptr, old, new, barrier) \
  71. ({ \
  72. __typeof__(ptr) __ptr = (ptr); \
  73. __typeof__(*(ptr)) __old = (old); \
  74. __typeof__(*(ptr)) __new = (new); \
  75. __typeof__(*(ptr)) __res = 0; \
  76. \
  77. barrier; \
  78. \
  79. switch (sizeof(*(__ptr))) { \
  80. case 4: \
  81. __res = __cmpxchg_asm("ll", "sc", __ptr, __old, __new); \
  82. break; \
  83. case 8: \
  84. if (sizeof(long) == 8) { \
  85. __res = __cmpxchg_asm("lld", "scd", __ptr, \
  86. __old, __new); \
  87. break; \
  88. } \
  89. default: \
  90. __cmpxchg_called_with_bad_pointer(); \
  91. break; \
  92. } \
  93. \
  94. barrier; \
  95. \
  96. __res; \
  97. })
  98. #define cmpxchg(ptr, old, new) __cmpxchg(ptr, old, new, smp_llsc_mb())
  99. #define cmpxchg_local(ptr, old, new) __cmpxchg(ptr, old, new, )
  100. #define cmpxchg64(ptr, o, n) \
  101. ({ \
  102. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  103. cmpxchg((ptr), (o), (n)); \
  104. })
  105. #ifdef CONFIG_64BIT
  106. #define cmpxchg64_local(ptr, o, n) \
  107. ({ \
  108. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  109. cmpxchg_local((ptr), (o), (n)); \
  110. })
  111. #else
  112. #include <asm-generic/cmpxchg-local.h>
  113. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  114. #endif
  115. #endif /* __ASM_CMPXCHG_H */