cmpxchg.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Generic UP xchg and cmpxchg using interrupt disablement. Does not
  4. * support SMP.
  5. */
  6. #ifndef __ASM_GENERIC_CMPXCHG_H
  7. #define __ASM_GENERIC_CMPXCHG_H
  8. #ifdef CONFIG_SMP
  9. #error "Cannot use generic cmpxchg on SMP"
  10. #endif
  11. #include <linux/types.h>
  12. #include <linux/irqflags.h>
  13. /*
  14. * This function doesn't exist, so you'll get a linker error if
  15. * something tries to do an invalidly-sized xchg().
  16. */
  17. extern void __generic_xchg_called_with_bad_pointer(void);
  18. static inline
  19. unsigned long __generic_xchg(unsigned long x, volatile void *ptr, int size)
  20. {
  21. unsigned long ret, flags;
  22. switch (size) {
  23. case 1:
  24. #ifdef __xchg_u8
  25. return __xchg_u8(x, ptr);
  26. #else
  27. local_irq_save(flags);
  28. ret = *(volatile u8 *)ptr;
  29. *(volatile u8 *)ptr = x;
  30. local_irq_restore(flags);
  31. return ret;
  32. #endif /* __xchg_u8 */
  33. case 2:
  34. #ifdef __xchg_u16
  35. return __xchg_u16(x, ptr);
  36. #else
  37. local_irq_save(flags);
  38. ret = *(volatile u16 *)ptr;
  39. *(volatile u16 *)ptr = x;
  40. local_irq_restore(flags);
  41. return ret;
  42. #endif /* __xchg_u16 */
  43. case 4:
  44. #ifdef __xchg_u32
  45. return __xchg_u32(x, ptr);
  46. #else
  47. local_irq_save(flags);
  48. ret = *(volatile u32 *)ptr;
  49. *(volatile u32 *)ptr = x;
  50. local_irq_restore(flags);
  51. return ret;
  52. #endif /* __xchg_u32 */
  53. #ifdef CONFIG_64BIT
  54. case 8:
  55. #ifdef __xchg_u64
  56. return __xchg_u64(x, ptr);
  57. #else
  58. local_irq_save(flags);
  59. ret = *(volatile u64 *)ptr;
  60. *(volatile u64 *)ptr = x;
  61. local_irq_restore(flags);
  62. return ret;
  63. #endif /* __xchg_u64 */
  64. #endif /* CONFIG_64BIT */
  65. default:
  66. __generic_xchg_called_with_bad_pointer();
  67. return x;
  68. }
  69. }
  70. #define generic_xchg(ptr, x) ({ \
  71. ((__typeof__(*(ptr))) \
  72. __generic_xchg((unsigned long)(x), (ptr), sizeof(*(ptr)))); \
  73. })
  74. /*
  75. * Atomic compare and exchange.
  76. */
  77. #include <asm-generic/cmpxchg-local.h>
  78. #define generic_cmpxchg_local(ptr, o, n) ({ \
  79. ((__typeof__(*(ptr)))__generic_cmpxchg_local((ptr), (unsigned long)(o), \
  80. (unsigned long)(n), sizeof(*(ptr)))); \
  81. })
  82. #define generic_cmpxchg64_local(ptr, o, n) \
  83. __generic_cmpxchg64_local((ptr), (o), (n))
  84. #ifndef arch_xchg
  85. #define arch_xchg generic_xchg
  86. #endif
  87. #ifndef arch_cmpxchg_local
  88. #define arch_cmpxchg_local generic_cmpxchg_local
  89. #endif
  90. #ifndef arch_cmpxchg64_local
  91. #define arch_cmpxchg64_local generic_cmpxchg64_local
  92. #endif
  93. #define arch_cmpxchg arch_cmpxchg_local
  94. #define arch_cmpxchg64 arch_cmpxchg64_local
  95. #endif /* __ASM_GENERIC_CMPXCHG_H */