bitops-op32.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_SH_BITOPS_OP32_H
  3. #define __ASM_SH_BITOPS_OP32_H
  4. #include <linux/bits.h>
  5. /*
  6. * The bit modifying instructions on SH-2A are only capable of working
  7. * with a 3-bit immediate, which signifies the shift position for the bit
  8. * being worked on.
  9. */
  10. #if defined(__BIG_ENDIAN)
  11. #define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)
  12. #define BYTE_NUMBER(nr) ((nr ^ BITOP_LE_SWIZZLE) / BITS_PER_BYTE)
  13. #define BYTE_OFFSET(nr) ((nr ^ BITOP_LE_SWIZZLE) % BITS_PER_BYTE)
  14. #else
  15. #define BYTE_NUMBER(nr) ((nr) / BITS_PER_BYTE)
  16. #define BYTE_OFFSET(nr) ((nr) % BITS_PER_BYTE)
  17. #endif
  18. static __always_inline void
  19. arch___set_bit(unsigned long nr, volatile unsigned long *addr)
  20. {
  21. if (__builtin_constant_p(nr)) {
  22. __asm__ __volatile__ (
  23. "bset.b %1, @(%O2,%0) ! __set_bit\n\t"
  24. : "+r" (addr)
  25. : "i" (BYTE_OFFSET(nr)), "i" (BYTE_NUMBER(nr))
  26. : "t", "memory"
  27. );
  28. } else {
  29. unsigned long mask = BIT_MASK(nr);
  30. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  31. *p |= mask;
  32. }
  33. }
  34. static __always_inline void
  35. arch___clear_bit(unsigned long nr, volatile unsigned long *addr)
  36. {
  37. if (__builtin_constant_p(nr)) {
  38. __asm__ __volatile__ (
  39. "bclr.b %1, @(%O2,%0) ! __clear_bit\n\t"
  40. : "+r" (addr)
  41. : "i" (BYTE_OFFSET(nr)),
  42. "i" (BYTE_NUMBER(nr))
  43. : "t", "memory"
  44. );
  45. } else {
  46. unsigned long mask = BIT_MASK(nr);
  47. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  48. *p &= ~mask;
  49. }
  50. }
  51. /**
  52. * arch___change_bit - Toggle a bit in memory
  53. * @nr: the bit to change
  54. * @addr: the address to start counting from
  55. *
  56. * Unlike change_bit(), this function is non-atomic and may be reordered.
  57. * If it's called on the same region of memory simultaneously, the effect
  58. * may be that only one operation succeeds.
  59. */
  60. static __always_inline void
  61. arch___change_bit(unsigned long nr, volatile unsigned long *addr)
  62. {
  63. if (__builtin_constant_p(nr)) {
  64. __asm__ __volatile__ (
  65. "bxor.b %1, @(%O2,%0) ! __change_bit\n\t"
  66. : "+r" (addr)
  67. : "i" (BYTE_OFFSET(nr)),
  68. "i" (BYTE_NUMBER(nr))
  69. : "t", "memory"
  70. );
  71. } else {
  72. unsigned long mask = BIT_MASK(nr);
  73. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  74. *p ^= mask;
  75. }
  76. }
  77. /**
  78. * arch___test_and_set_bit - Set a bit and return its old value
  79. * @nr: Bit to set
  80. * @addr: Address to count from
  81. *
  82. * This operation is non-atomic and can be reordered.
  83. * If two examples of this operation race, one can appear to succeed
  84. * but actually fail. You must protect multiple accesses with a lock.
  85. */
  86. static __always_inline bool
  87. arch___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
  88. {
  89. unsigned long mask = BIT_MASK(nr);
  90. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  91. unsigned long old = *p;
  92. *p = old | mask;
  93. return (old & mask) != 0;
  94. }
  95. /**
  96. * arch___test_and_clear_bit - Clear a bit and return its old value
  97. * @nr: Bit to clear
  98. * @addr: Address to count from
  99. *
  100. * This operation is non-atomic and can be reordered.
  101. * If two examples of this operation race, one can appear to succeed
  102. * but actually fail. You must protect multiple accesses with a lock.
  103. */
  104. static __always_inline bool
  105. arch___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
  106. {
  107. unsigned long mask = BIT_MASK(nr);
  108. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  109. unsigned long old = *p;
  110. *p = old & ~mask;
  111. return (old & mask) != 0;
  112. }
  113. /* WARNING: non atomic and it can be reordered! */
  114. static __always_inline bool
  115. arch___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
  116. {
  117. unsigned long mask = BIT_MASK(nr);
  118. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  119. unsigned long old = *p;
  120. *p = old ^ mask;
  121. return (old & mask) != 0;
  122. }
  123. #define arch_test_bit generic_test_bit
  124. #define arch_test_bit_acquire generic_test_bit_acquire
  125. #include <asm-generic/bitops/non-instrumented-non-atomic.h>
  126. #endif /* __ASM_SH_BITOPS_OP32_H */