sync_bitops.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_SYNC_BITOPS_H
  3. #define _ASM_X86_SYNC_BITOPS_H
  4. /*
  5. * Copyright 1992, Linus Torvalds.
  6. */
  7. /*
  8. * These have to be done with inline assembly: that way the bit-setting
  9. * is guaranteed to be atomic. All bit operations return 0 if the bit
  10. * was cleared before the operation and != 0 if it was not.
  11. *
  12. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  13. */
  14. #define ADDR (*(volatile long *)addr)
  15. /**
  16. * sync_set_bit - Atomically set a bit in memory
  17. * @nr: the bit to set
  18. * @addr: the address to start counting from
  19. *
  20. * This function is atomic and may not be reordered. See __set_bit()
  21. * if you do not require the atomic guarantees.
  22. *
  23. * Note that @nr may be almost arbitrarily large; this function is not
  24. * restricted to acting on a single-word quantity.
  25. */
  26. static inline void sync_set_bit(long nr, volatile unsigned long *addr)
  27. {
  28. asm volatile("lock; bts %1,%0"
  29. : "+m" (ADDR)
  30. : "Ir" (nr)
  31. : "memory");
  32. }
  33. /**
  34. * sync_clear_bit - Clears a bit in memory
  35. * @nr: Bit to clear
  36. * @addr: Address to start counting from
  37. *
  38. * sync_clear_bit() is atomic and may not be reordered. However, it does
  39. * not contain a memory barrier, so if it is used for locking purposes,
  40. * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
  41. * in order to ensure changes are visible on other processors.
  42. */
  43. static inline void sync_clear_bit(long nr, volatile unsigned long *addr)
  44. {
  45. asm volatile("lock; btr %1,%0"
  46. : "+m" (ADDR)
  47. : "Ir" (nr)
  48. : "memory");
  49. }
  50. /**
  51. * sync_change_bit - Toggle a bit in memory
  52. * @nr: Bit to change
  53. * @addr: Address to start counting from
  54. *
  55. * sync_change_bit() is atomic and may not be reordered.
  56. * Note that @nr may be almost arbitrarily large; this function is not
  57. * restricted to acting on a single-word quantity.
  58. */
  59. static inline void sync_change_bit(long nr, volatile unsigned long *addr)
  60. {
  61. asm volatile("lock; btc %1,%0"
  62. : "+m" (ADDR)
  63. : "Ir" (nr)
  64. : "memory");
  65. }
  66. /**
  67. * sync_test_and_set_bit - Set a bit and return its old value
  68. * @nr: Bit to set
  69. * @addr: Address to count from
  70. *
  71. * This operation is atomic and cannot be reordered.
  72. * It also implies a memory barrier.
  73. */
  74. static inline int sync_test_and_set_bit(long nr, volatile unsigned long *addr)
  75. {
  76. unsigned char oldbit;
  77. asm volatile("lock; bts %2,%1\n\tsetc %0"
  78. : "=qm" (oldbit), "+m" (ADDR)
  79. : "Ir" (nr) : "memory");
  80. return oldbit;
  81. }
  82. /**
  83. * sync_test_and_clear_bit - Clear a bit and return its old value
  84. * @nr: Bit to clear
  85. * @addr: Address to count from
  86. *
  87. * This operation is atomic and cannot be reordered.
  88. * It also implies a memory barrier.
  89. */
  90. static inline int sync_test_and_clear_bit(long nr, volatile unsigned long *addr)
  91. {
  92. unsigned char oldbit;
  93. asm volatile("lock; btr %2,%1\n\tsetc %0"
  94. : "=qm" (oldbit), "+m" (ADDR)
  95. : "Ir" (nr) : "memory");
  96. return oldbit;
  97. }
  98. /**
  99. * sync_test_and_change_bit - Change a bit and return its old value
  100. * @nr: Bit to change
  101. * @addr: Address to count from
  102. *
  103. * This operation is atomic and cannot be reordered.
  104. * It also implies a memory barrier.
  105. */
  106. static inline int sync_test_and_change_bit(long nr, volatile unsigned long *addr)
  107. {
  108. unsigned char oldbit;
  109. asm volatile("lock; btc %2,%1\n\tsetc %0"
  110. : "=qm" (oldbit), "+m" (ADDR)
  111. : "Ir" (nr) : "memory");
  112. return oldbit;
  113. }
  114. #define sync_test_bit(nr, addr) test_bit(nr, addr)
  115. #undef ADDR
  116. #endif /* _ASM_X86_SYNC_BITOPS_H */