cmpxchg.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * forked from parisc asm/atomic.h which was:
  4. * Copyright (C) 2000 Philipp Rumpf <[email protected]>
  5. * Copyright (C) 2006 Kyle McMartin <[email protected]>
  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. __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. __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_u32/u64 defined in arch/parisc/lib/bitops.c */
  53. extern unsigned long __cmpxchg_u32(volatile unsigned int *m, unsigned int old,
  54. unsigned int new_);
  55. extern u64 __cmpxchg_u64(volatile u64 *ptr, u64 old, u64 new_);
  56. extern u8 __cmpxchg_u8(volatile u8 *ptr, u8 old, u8 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. switch (size) {
  62. #ifdef CONFIG_64BIT
  63. case 8: return __cmpxchg_u64((u64 *)ptr, old, new_);
  64. #endif
  65. case 4: return __cmpxchg_u32((unsigned int *)ptr,
  66. (unsigned int)old, (unsigned int)new_);
  67. case 1: return __cmpxchg_u8((u8 *)ptr, old & 0xff, new_ & 0xff);
  68. }
  69. __cmpxchg_called_with_bad_pointer();
  70. return old;
  71. }
  72. #define arch_cmpxchg(ptr, o, n) \
  73. ({ \
  74. __typeof__(*(ptr)) _o_ = (o); \
  75. __typeof__(*(ptr)) _n_ = (n); \
  76. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  77. (unsigned long)_n_, sizeof(*(ptr))); \
  78. })
  79. #include <asm-generic/cmpxchg-local.h>
  80. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  81. unsigned long old,
  82. unsigned long new_, int size)
  83. {
  84. switch (size) {
  85. #ifdef CONFIG_64BIT
  86. case 8: return __cmpxchg_u64((u64 *)ptr, old, new_);
  87. #endif
  88. case 4: return __cmpxchg_u32(ptr, old, new_);
  89. default:
  90. return __generic_cmpxchg_local(ptr, old, new_, size);
  91. }
  92. }
  93. /*
  94. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  95. * them available.
  96. */
  97. #define arch_cmpxchg_local(ptr, o, n) \
  98. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
  99. (unsigned long)(n), sizeof(*(ptr))))
  100. #ifdef CONFIG_64BIT
  101. #define arch_cmpxchg64_local(ptr, o, n) \
  102. ({ \
  103. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  104. cmpxchg_local((ptr), (o), (n)); \
  105. })
  106. #else
  107. #define arch_cmpxchg64_local(ptr, o, n) __generic_cmpxchg64_local((ptr), (o), (n))
  108. #endif
  109. #define arch_cmpxchg64(ptr, o, n) __cmpxchg_u64(ptr, o, n)
  110. #endif /* _ASM_PARISC_CMPXCHG_H_ */