cmpxchg.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. long long _atomic64_xchg(long long *v, long long n);
  33. long long _atomic64_xchg_add(long long *v, long long i);
  34. long long _atomic64_xchg_add_unless(long long *v, long long a, long long u);
  35. long long _atomic64_cmpxchg(long long *v, long long o, long long 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, \
  49. (int)n); \
  50. })
  51. #define xchg64(ptr, n) \
  52. ({ \
  53. if (sizeof(*(ptr)) != 8) \
  54. __xchg_called_with_bad_pointer(); \
  55. smp_mb(); \
  56. (typeof(*(ptr)))_atomic64_xchg((long long *)(ptr), \
  57. (long long)(n)); \
  58. })
  59. #define cmpxchg64(ptr, o, n) \
  60. ({ \
  61. if (sizeof(*(ptr)) != 8) \
  62. __cmpxchg_called_with_bad_pointer(); \
  63. smp_mb(); \
  64. (typeof(*(ptr)))_atomic64_cmpxchg((long long *)ptr, \
  65. (long long)o, (long long)n); \
  66. })
  67. #else
  68. #define xchg(ptr, n) \
  69. ({ \
  70. typeof(*(ptr)) __x; \
  71. smp_mb(); \
  72. switch (sizeof(*(ptr))) { \
  73. case 4: \
  74. __x = (typeof(__x))(unsigned long) \
  75. __insn_exch4((ptr), \
  76. (u32)(unsigned long)(n)); \
  77. break; \
  78. case 8: \
  79. __x = (typeof(__x)) \
  80. __insn_exch((ptr), (unsigned long)(n)); \
  81. break; \
  82. default: \
  83. __xchg_called_with_bad_pointer(); \
  84. break; \
  85. } \
  86. smp_mb(); \
  87. __x; \
  88. })
  89. #define cmpxchg(ptr, o, n) \
  90. ({ \
  91. typeof(*(ptr)) __x; \
  92. __insn_mtspr(SPR_CMPEXCH_VALUE, (unsigned long)(o)); \
  93. smp_mb(); \
  94. switch (sizeof(*(ptr))) { \
  95. case 4: \
  96. __x = (typeof(__x))(unsigned long) \
  97. __insn_cmpexch4((ptr), \
  98. (u32)(unsigned long)(n)); \
  99. break; \
  100. case 8: \
  101. __x = (typeof(__x))__insn_cmpexch((ptr), \
  102. (long long)(n)); \
  103. break; \
  104. default: \
  105. __cmpxchg_called_with_bad_pointer(); \
  106. break; \
  107. } \
  108. smp_mb(); \
  109. __x; \
  110. })
  111. #define xchg64 xchg
  112. #define cmpxchg64 cmpxchg
  113. #endif
  114. #define tas(ptr) xchg((ptr), 1)
  115. #endif /* __ASSEMBLY__ */
  116. #endif /* _ASM_TILE_CMPXCHG_H */