cmpxchg.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright IBM Corp. 1999, 2011
  3. *
  4. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  5. */
  6. #ifndef __ASM_CMPXCHG_H
  7. #define __ASM_CMPXCHG_H
  8. #include <linux/mmdebug.h>
  9. #include <linux/types.h>
  10. #include <linux/bug.h>
  11. #define cmpxchg(ptr, o, n) \
  12. ({ \
  13. __typeof__(*(ptr)) __o = (o); \
  14. __typeof__(*(ptr)) __n = (n); \
  15. (__typeof__(*(ptr))) __sync_val_compare_and_swap((ptr),__o,__n);\
  16. })
  17. #define cmpxchg64 cmpxchg
  18. #define cmpxchg_local cmpxchg
  19. #define cmpxchg64_local cmpxchg
  20. #define xchg(ptr, x) \
  21. ({ \
  22. __typeof__(ptr) __ptr = (ptr); \
  23. __typeof__(*(ptr)) __old; \
  24. do { \
  25. __old = *__ptr; \
  26. } while (!__sync_bool_compare_and_swap(__ptr, __old, x)); \
  27. __old; \
  28. })
  29. #define __HAVE_ARCH_CMPXCHG
  30. #define __cmpxchg_double_op(p1, p2, o1, o2, n1, n2, insn) \
  31. ({ \
  32. register __typeof__(*(p1)) __old1 asm("2") = (o1); \
  33. register __typeof__(*(p2)) __old2 asm("3") = (o2); \
  34. register __typeof__(*(p1)) __new1 asm("4") = (n1); \
  35. register __typeof__(*(p2)) __new2 asm("5") = (n2); \
  36. int cc; \
  37. asm volatile( \
  38. insn " %[old],%[new],%[ptr]\n" \
  39. " ipm %[cc]\n" \
  40. " srl %[cc],28" \
  41. : [cc] "=d" (cc), [old] "+d" (__old1), "+d" (__old2) \
  42. : [new] "d" (__new1), "d" (__new2), \
  43. [ptr] "Q" (*(p1)), "Q" (*(p2)) \
  44. : "memory", "cc"); \
  45. !cc; \
  46. })
  47. #define __cmpxchg_double_4(p1, p2, o1, o2, n1, n2) \
  48. __cmpxchg_double_op(p1, p2, o1, o2, n1, n2, "cds")
  49. #define __cmpxchg_double_8(p1, p2, o1, o2, n1, n2) \
  50. __cmpxchg_double_op(p1, p2, o1, o2, n1, n2, "cdsg")
  51. extern void __cmpxchg_double_called_with_bad_pointer(void);
  52. #define __cmpxchg_double(p1, p2, o1, o2, n1, n2) \
  53. ({ \
  54. int __ret; \
  55. switch (sizeof(*(p1))) { \
  56. case 4: \
  57. __ret = __cmpxchg_double_4(p1, p2, o1, o2, n1, n2); \
  58. break; \
  59. case 8: \
  60. __ret = __cmpxchg_double_8(p1, p2, o1, o2, n1, n2); \
  61. break; \
  62. default: \
  63. __cmpxchg_double_called_with_bad_pointer(); \
  64. } \
  65. __ret; \
  66. })
  67. #define cmpxchg_double(p1, p2, o1, o2, n1, n2) \
  68. ({ \
  69. __typeof__(p1) __p1 = (p1); \
  70. __typeof__(p2) __p2 = (p2); \
  71. BUILD_BUG_ON(sizeof(*(p1)) != sizeof(long)); \
  72. BUILD_BUG_ON(sizeof(*(p2)) != sizeof(long)); \
  73. VM_BUG_ON((unsigned long)((__p1) + 1) != (unsigned long)(__p2));\
  74. __cmpxchg_double_8(__p1, __p2, o1, o2, n1, n2); \
  75. })
  76. #define system_has_cmpxchg_double() 1
  77. #endif /* __ASM_CMPXCHG_H */