sync_bitops.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <asm/rmwcc.h>
  15. #define ADDR (*(volatile long *)addr)
  16. /**
  17. * sync_set_bit - Atomically set a bit in memory
  18. * @nr: the bit to set
  19. * @addr: the address to start counting from
  20. *
  21. * This function is atomic and may not be reordered. See __set_bit()
  22. * if you do not require the atomic guarantees.
  23. *
  24. * Note that @nr may be almost arbitrarily large; this function is not
  25. * restricted to acting on a single-word quantity.
  26. */
  27. static inline void sync_set_bit(long nr, volatile unsigned long *addr)
  28. {
  29. asm volatile("lock; " __ASM_SIZE(bts) " %1,%0"
  30. : "+m" (ADDR)
  31. : "Ir" (nr)
  32. : "memory");
  33. }
  34. /**
  35. * sync_clear_bit - Clears a bit in memory
  36. * @nr: Bit to clear
  37. * @addr: Address to start counting from
  38. *
  39. * sync_clear_bit() is atomic and may not be reordered. However, it does
  40. * not contain a memory barrier, so if it is used for locking purposes,
  41. * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
  42. * in order to ensure changes are visible on other processors.
  43. */
  44. static inline void sync_clear_bit(long nr, volatile unsigned long *addr)
  45. {
  46. asm volatile("lock; " __ASM_SIZE(btr) " %1,%0"
  47. : "+m" (ADDR)
  48. : "Ir" (nr)
  49. : "memory");
  50. }
  51. /**
  52. * sync_change_bit - Toggle a bit in memory
  53. * @nr: Bit to change
  54. * @addr: Address to start counting from
  55. *
  56. * sync_change_bit() is atomic and may not be reordered.
  57. * Note that @nr may be almost arbitrarily large; this function is not
  58. * restricted to acting on a single-word quantity.
  59. */
  60. static inline void sync_change_bit(long nr, volatile unsigned long *addr)
  61. {
  62. asm volatile("lock; " __ASM_SIZE(btc) " %1,%0"
  63. : "+m" (ADDR)
  64. : "Ir" (nr)
  65. : "memory");
  66. }
  67. /**
  68. * sync_test_and_set_bit - Set a bit and return its old value
  69. * @nr: Bit to set
  70. * @addr: Address to count from
  71. *
  72. * This operation is atomic and cannot be reordered.
  73. * It also implies a memory barrier.
  74. */
  75. static inline bool sync_test_and_set_bit(long nr, volatile unsigned long *addr)
  76. {
  77. return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(bts), *addr, c, "Ir", nr);
  78. }
  79. /**
  80. * sync_test_and_clear_bit - Clear a bit and return its old value
  81. * @nr: Bit to clear
  82. * @addr: Address to count from
  83. *
  84. * This operation is atomic and cannot be reordered.
  85. * It also implies a memory barrier.
  86. */
  87. static inline int sync_test_and_clear_bit(long nr, volatile unsigned long *addr)
  88. {
  89. return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btr), *addr, c, "Ir", nr);
  90. }
  91. /**
  92. * sync_test_and_change_bit - Change a bit and return its old value
  93. * @nr: Bit to change
  94. * @addr: Address to count from
  95. *
  96. * This operation is atomic and cannot be reordered.
  97. * It also implies a memory barrier.
  98. */
  99. static inline int sync_test_and_change_bit(long nr, volatile unsigned long *addr)
  100. {
  101. return GEN_BINARY_RMWcc("lock; " __ASM_SIZE(btc), *addr, c, "Ir", nr);
  102. }
  103. #define sync_test_bit(nr, addr) test_bit(nr, addr)
  104. #undef ADDR
  105. #endif /* _ASM_X86_SYNC_BITOPS_H */