cmpxchg.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * cmpxchg.h -- forked from asm/atomic.h with this copyright:
  3. *
  4. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation, version 2.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13. * NON INFRINGEMENT. See the GNU General Public License for
  14. * more details.
  15. *
  16. */
  17. #ifndef _ASM_TILE_CMPXCHG_H
  18. #define _ASM_TILE_CMPXCHG_H
  19. #ifndef __ASSEMBLY__
  20. #include <asm/barrier.h>
  21. /* Nonexistent functions intended to cause compile errors. */
  22. extern void __xchg_called_with_bad_pointer(void)
  23. __compiletime_error("Bad argument size for xchg");
  24. extern void __cmpxchg_called_with_bad_pointer(void)
  25. __compiletime_error("Bad argument size for cmpxchg");
  26. #ifndef __tilegx__
  27. /* Note the _atomic_xxx() routines include a final mb(). */
  28. int _atomic_xchg(int *ptr, int n);
  29. int _atomic_xchg_add(int *v, int i);
  30. int _atomic_xchg_add_unless(int *v, int a, int u);
  31. int _atomic_cmpxchg(int *ptr, int o, int n);
  32. u64 _atomic64_xchg(u64 *v, u64 n);
  33. u64 _atomic64_xchg_add(u64 *v, u64 i);
  34. u64 _atomic64_xchg_add_unless(u64 *v, u64 a, u64 u);
  35. u64 _atomic64_cmpxchg(u64 *v, u64 o, u64 n);
  36. #define xchg(ptr, n) \
  37. ({ \
  38. if (sizeof(*(ptr)) != 4) \
  39. __xchg_called_with_bad_pointer(); \
  40. smp_mb(); \
  41. (typeof(*(ptr)))_atomic_xchg((int *)(ptr), (int)(n)); \
  42. })
  43. #define cmpxchg(ptr, o, n) \
  44. ({ \
  45. if (sizeof(*(ptr)) != 4) \
  46. __cmpxchg_called_with_bad_pointer(); \
  47. smp_mb(); \
  48. (typeof(*(ptr)))_atomic_cmpxchg((int *)ptr, (int)o, (int)n); \
  49. })
  50. #define xchg64(ptr, n) \
  51. ({ \
  52. if (sizeof(*(ptr)) != 8) \
  53. __xchg_called_with_bad_pointer(); \
  54. smp_mb(); \
  55. (typeof(*(ptr)))_atomic64_xchg((u64 *)(ptr), (u64)(n)); \
  56. })
  57. #define cmpxchg64(ptr, o, n) \
  58. ({ \
  59. if (sizeof(*(ptr)) != 8) \
  60. __cmpxchg_called_with_bad_pointer(); \
  61. smp_mb(); \
  62. (typeof(*(ptr)))_atomic64_cmpxchg((u64 *)ptr, (u64)o, (u64)n); \
  63. })
  64. #else
  65. #define xchg(ptr, n) \
  66. ({ \
  67. typeof(*(ptr)) __x; \
  68. smp_mb(); \
  69. switch (sizeof(*(ptr))) { \
  70. case 4: \
  71. __x = (typeof(__x))(unsigned long) \
  72. __insn_exch4((ptr), (u32)(unsigned long)(n)); \
  73. break; \
  74. case 8: \
  75. __x = (typeof(__x)) \
  76. __insn_exch((ptr), (unsigned long)(n)); \
  77. break; \
  78. default: \
  79. __xchg_called_with_bad_pointer(); \
  80. break; \
  81. } \
  82. smp_mb(); \
  83. __x; \
  84. })
  85. #define cmpxchg(ptr, o, n) \
  86. ({ \
  87. typeof(*(ptr)) __x; \
  88. __insn_mtspr(SPR_CMPEXCH_VALUE, (unsigned long)(o)); \
  89. smp_mb(); \
  90. switch (sizeof(*(ptr))) { \
  91. case 4: \
  92. __x = (typeof(__x))(unsigned long) \
  93. __insn_cmpexch4((ptr), (u32)(unsigned long)(n)); \
  94. break; \
  95. case 8: \
  96. __x = (typeof(__x))__insn_cmpexch((ptr), (u64)(n)); \
  97. break; \
  98. default: \
  99. __cmpxchg_called_with_bad_pointer(); \
  100. break; \
  101. } \
  102. smp_mb(); \
  103. __x; \
  104. })
  105. #define xchg64 xchg
  106. #define cmpxchg64 cmpxchg
  107. #endif
  108. #define tas(ptr) xchg((ptr), 1)
  109. #endif /* __ASSEMBLY__ */
  110. #endif /* _ASM_TILE_CMPXCHG_H */