cmpxchg.h 3.7 KB

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