cmpxchg.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef xchg
  14. /*
  15. * This function doesn't exist, so you'll get a linker error if
  16. * something tries to do an invalidly-sized xchg().
  17. */
  18. extern void __xchg_called_with_bad_pointer(void);
  19. static inline
  20. unsigned long __xchg(unsigned long x, volatile void *ptr, int size)
  21. {
  22. unsigned long ret, flags;
  23. switch (size) {
  24. case 1:
  25. #ifdef __xchg_u8
  26. return __xchg_u8(x, ptr);
  27. #else
  28. local_irq_save(flags);
  29. ret = *(volatile u8 *)ptr;
  30. *(volatile u8 *)ptr = x;
  31. local_irq_restore(flags);
  32. return ret;
  33. #endif /* __xchg_u8 */
  34. case 2:
  35. #ifdef __xchg_u16
  36. return __xchg_u16(x, ptr);
  37. #else
  38. local_irq_save(flags);
  39. ret = *(volatile u16 *)ptr;
  40. *(volatile u16 *)ptr = x;
  41. local_irq_restore(flags);
  42. return ret;
  43. #endif /* __xchg_u16 */
  44. case 4:
  45. #ifdef __xchg_u32
  46. return __xchg_u32(x, ptr);
  47. #else
  48. local_irq_save(flags);
  49. ret = *(volatile u32 *)ptr;
  50. *(volatile u32 *)ptr = x;
  51. local_irq_restore(flags);
  52. return ret;
  53. #endif /* __xchg_u32 */
  54. #ifdef CONFIG_64BIT
  55. case 8:
  56. #ifdef __xchg_u64
  57. return __xchg_u64(x, ptr);
  58. #else
  59. local_irq_save(flags);
  60. ret = *(volatile u64 *)ptr;
  61. *(volatile u64 *)ptr = x;
  62. local_irq_restore(flags);
  63. return ret;
  64. #endif /* __xchg_u64 */
  65. #endif /* CONFIG_64BIT */
  66. default:
  67. __xchg_called_with_bad_pointer();
  68. return x;
  69. }
  70. }
  71. #define xchg(ptr, x) ({ \
  72. ((__typeof__(*(ptr))) \
  73. __xchg((unsigned long)(x), (ptr), sizeof(*(ptr)))); \
  74. })
  75. #endif /* xchg */
  76. /*
  77. * Atomic compare and exchange.
  78. */
  79. #include <asm-generic/cmpxchg-local.h>
  80. #ifndef cmpxchg_local
  81. #define cmpxchg_local(ptr, o, n) ({ \
  82. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  83. (unsigned long)(n), sizeof(*(ptr)))); \
  84. })
  85. #endif
  86. #ifndef cmpxchg64_local
  87. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  88. #endif
  89. #define cmpxchg(ptr, o, n) cmpxchg_local((ptr), (o), (n))
  90. #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
  91. #endif /* __ASM_GENERIC_CMPXCHG_H */