cmpxchg.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 (kernel_uses_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 (kernel_uses_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, 1b \n" \
  45. " .set pop \n" \
  46. "2: \n" \
  47. : "=&r" (__ret), "=R" (*m) \
  48. : "R" (*m), "Jr" (old), "Jr" (new) \
  49. : "memory"); \
  50. } else { \
  51. unsigned long __flags; \
  52. \
  53. raw_local_irq_save(__flags); \
  54. __ret = *m; \
  55. if (__ret == old) \
  56. *m = new; \
  57. raw_local_irq_restore(__flags); \
  58. } \
  59. \
  60. __ret; \
  61. })
  62. /*
  63. * This function doesn't exist, so you'll get a linker error
  64. * if something tries to do an invalid cmpxchg().
  65. */
  66. extern void __cmpxchg_called_with_bad_pointer(void);
  67. #define __cmpxchg(ptr, old, new, pre_barrier, post_barrier) \
  68. ({ \
  69. __typeof__(ptr) __ptr = (ptr); \
  70. __typeof__(*(ptr)) __old = (old); \
  71. __typeof__(*(ptr)) __new = (new); \
  72. __typeof__(*(ptr)) __res = 0; \
  73. \
  74. pre_barrier; \
  75. \
  76. switch (sizeof(*(__ptr))) { \
  77. case 4: \
  78. __res = __cmpxchg_asm("ll", "sc", __ptr, __old, __new); \
  79. break; \
  80. case 8: \
  81. if (sizeof(long) == 8) { \
  82. __res = __cmpxchg_asm("lld", "scd", __ptr, \
  83. __old, __new); \
  84. break; \
  85. } \
  86. default: \
  87. __cmpxchg_called_with_bad_pointer(); \
  88. break; \
  89. } \
  90. \
  91. post_barrier; \
  92. \
  93. __res; \
  94. })
  95. #define cmpxchg(ptr, old, new) __cmpxchg(ptr, old, new, smp_mb__before_llsc(), smp_llsc_mb())
  96. #define cmpxchg_local(ptr, old, new) __cmpxchg(ptr, old, new, , )
  97. #define cmpxchg64(ptr, o, n) \
  98. ({ \
  99. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  100. cmpxchg((ptr), (o), (n)); \
  101. })
  102. #ifdef CONFIG_64BIT
  103. #define cmpxchg64_local(ptr, o, n) \
  104. ({ \
  105. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  106. cmpxchg_local((ptr), (o), (n)); \
  107. })
  108. #else
  109. #include <asm-generic/cmpxchg-local.h>
  110. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  111. #endif
  112. #endif /* __ASM_CMPXCHG_H */