cmpxchg.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * forked from parisc asm/atomic.h which was:
  3. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  4. * Copyright (C) 2006 Kyle McMartin <kyle@parisc-linux.org>
  5. */
  6. #ifndef _ASM_PARISC_CMPXCHG_H_
  7. #define _ASM_PARISC_CMPXCHG_H_
  8. /* This should get optimized out since it's never called.
  9. ** Or get a link error if xchg is used "wrong".
  10. */
  11. extern void __xchg_called_with_bad_pointer(void);
  12. /* __xchg32/64 defined in arch/parisc/lib/bitops.c */
  13. extern unsigned long __xchg8(char, char *);
  14. extern unsigned long __xchg32(int, int *);
  15. #ifdef CONFIG_64BIT
  16. extern unsigned long __xchg64(unsigned long, unsigned long *);
  17. #endif
  18. /* optimizer better get rid of switch since size is a constant */
  19. static inline unsigned long
  20. __xchg(unsigned long x, __volatile__ void *ptr, int size)
  21. {
  22. switch (size) {
  23. #ifdef CONFIG_64BIT
  24. case 8: return __xchg64(x, (unsigned long *) ptr);
  25. #endif
  26. case 4: return __xchg32((int) x, (int *) ptr);
  27. case 1: return __xchg8((char) x, (char *) ptr);
  28. }
  29. __xchg_called_with_bad_pointer();
  30. return x;
  31. }
  32. /*
  33. ** REVISIT - Abandoned use of LDCW in xchg() for now:
  34. ** o need to test sizeof(*ptr) to avoid clearing adjacent bytes
  35. ** o and while we are at it, could CONFIG_64BIT code use LDCD too?
  36. **
  37. ** if (__builtin_constant_p(x) && (x == NULL))
  38. ** if (((unsigned long)p & 0xf) == 0)
  39. ** return __ldcw(p);
  40. */
  41. #define xchg(ptr, x) \
  42. ((__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr), sizeof(*(ptr))))
  43. #define __HAVE_ARCH_CMPXCHG 1
  44. /* bug catcher for when unsupported size is used - won't link */
  45. extern void __cmpxchg_called_with_bad_pointer(void);
  46. /* __cmpxchg_u32/u64 defined in arch/parisc/lib/bitops.c */
  47. extern unsigned long __cmpxchg_u32(volatile unsigned int *m, unsigned int old,
  48. unsigned int new_);
  49. extern unsigned long __cmpxchg_u64(volatile unsigned long *ptr,
  50. unsigned long old, unsigned long new_);
  51. /* don't worry...optimizer will get rid of most of this */
  52. static inline unsigned long
  53. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
  54. {
  55. switch (size) {
  56. #ifdef CONFIG_64BIT
  57. case 8: return __cmpxchg_u64((unsigned long *)ptr, old, new_);
  58. #endif
  59. case 4: return __cmpxchg_u32((unsigned int *)ptr,
  60. (unsigned int)old, (unsigned int)new_);
  61. }
  62. __cmpxchg_called_with_bad_pointer();
  63. return old;
  64. }
  65. #define cmpxchg(ptr, o, n) \
  66. ({ \
  67. __typeof__(*(ptr)) _o_ = (o); \
  68. __typeof__(*(ptr)) _n_ = (n); \
  69. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  70. (unsigned long)_n_, sizeof(*(ptr))); \
  71. })
  72. #include <asm-generic/cmpxchg-local.h>
  73. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  74. unsigned long old,
  75. unsigned long new_, int size)
  76. {
  77. switch (size) {
  78. #ifdef CONFIG_64BIT
  79. case 8: return __cmpxchg_u64((unsigned long *)ptr, old, new_);
  80. #endif
  81. case 4: return __cmpxchg_u32(ptr, old, new_);
  82. default:
  83. return __cmpxchg_local_generic(ptr, old, new_, size);
  84. }
  85. }
  86. /*
  87. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  88. * them available.
  89. */
  90. #define cmpxchg_local(ptr, o, n) \
  91. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
  92. (unsigned long)(n), sizeof(*(ptr))))
  93. #ifdef CONFIG_64BIT
  94. #define cmpxchg64_local(ptr, o, n) \
  95. ({ \
  96. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  97. cmpxchg_local((ptr), (o), (n)); \
  98. })
  99. #else
  100. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  101. #endif
  102. #endif /* _ASM_PARISC_CMPXCHG_H_ */