cmpxchg.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Generic UP xchg and cmpxchg using interrupt disablement. Does not
  3. * support SMP.
  4. */
  5. #ifndef __ASM_GENERIC_CMPXCHG_H
  6. #define __ASM_GENERIC_CMPXCHG_H
  7. #ifdef CONFIG_SMP
  8. #error "Cannot use generic cmpxchg on SMP"
  9. #endif
  10. #include <linux/types.h>
  11. #include <linux/irqflags.h>
  12. #ifndef xchg
  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 __xchg_called_with_bad_pointer(void);
  18. static inline
  19. unsigned long __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. __xchg_called_with_bad_pointer();
  67. return x;
  68. }
  69. }
  70. #define xchg(ptr, x) ({ \
  71. ((__typeof__(*(ptr))) \
  72. __xchg((unsigned long)(x), (ptr), sizeof(*(ptr)))); \
  73. })
  74. #endif /* xchg */
  75. /*
  76. * Atomic compare and exchange.
  77. */
  78. #include <asm-generic/cmpxchg-local.h>
  79. #ifndef cmpxchg_local
  80. #define cmpxchg_local(ptr, o, n) ({ \
  81. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  82. (unsigned long)(n), sizeof(*(ptr)))); \
  83. })
  84. #endif
  85. #ifndef cmpxchg64_local
  86. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  87. #endif
  88. #define cmpxchg(ptr, o, n) cmpxchg_local((ptr), (o), (n))
  89. #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
  90. #endif /* __ASM_GENERIC_CMPXCHG_H */