1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_SH_BITOPS_OP32_H
3 #define __ASM_SH_BITOPS_OP32_H
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
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)
15 #define BYTE_NUMBER(nr) ((nr) / BITS_PER_BYTE)
16 #define BYTE_OFFSET(nr) ((nr) % BITS_PER_BYTE)
19 #define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))
21 static inline void __set_bit(int nr
, volatile unsigned long *addr
)
23 if (IS_IMMEDIATE(nr
)) {
24 __asm__
__volatile__ (
25 "bset.b %1, @(%O2,%0) ! __set_bit\n\t"
27 : "i" (BYTE_OFFSET(nr
)), "i" (BYTE_NUMBER(nr
))
31 unsigned long mask
= BIT_MASK(nr
);
32 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
38 static inline void __clear_bit(int nr
, volatile unsigned long *addr
)
40 if (IS_IMMEDIATE(nr
)) {
41 __asm__
__volatile__ (
42 "bclr.b %1, @(%O2,%0) ! __clear_bit\n\t"
44 : "i" (BYTE_OFFSET(nr
)),
49 unsigned long mask
= BIT_MASK(nr
);
50 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
57 * __change_bit - Toggle a bit in memory
58 * @nr: the bit to change
59 * @addr: the address to start counting from
61 * Unlike change_bit(), this function is non-atomic and may be reordered.
62 * If it's called on the same region of memory simultaneously, the effect
63 * may be that only one operation succeeds.
65 static inline void __change_bit(int nr
, volatile unsigned long *addr
)
67 if (IS_IMMEDIATE(nr
)) {
68 __asm__
__volatile__ (
69 "bxor.b %1, @(%O2,%0) ! __change_bit\n\t"
71 : "i" (BYTE_OFFSET(nr
)),
76 unsigned long mask
= BIT_MASK(nr
);
77 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
84 * __test_and_set_bit - Set a bit and return its old value
86 * @addr: Address to count from
88 * This operation is non-atomic and can be reordered.
89 * If two examples of this operation race, one can appear to succeed
90 * but actually fail. You must protect multiple accesses with a lock.
92 static inline int __test_and_set_bit(int nr
, volatile unsigned long *addr
)
94 unsigned long mask
= BIT_MASK(nr
);
95 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
96 unsigned long old
= *p
;
99 return (old
& mask
) != 0;
103 * __test_and_clear_bit - Clear a bit and return its old value
105 * @addr: Address to count from
107 * This operation is non-atomic and can be reordered.
108 * If two examples of this operation race, one can appear to succeed
109 * but actually fail. You must protect multiple accesses with a lock.
111 static inline int __test_and_clear_bit(int nr
, volatile unsigned long *addr
)
113 unsigned long mask
= BIT_MASK(nr
);
114 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
115 unsigned long old
= *p
;
118 return (old
& mask
) != 0;
121 /* WARNING: non atomic and it can be reordered! */
122 static inline int __test_and_change_bit(int nr
,
123 volatile unsigned long *addr
)
125 unsigned long mask
= BIT_MASK(nr
);
126 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
127 unsigned long old
= *p
;
130 return (old
& mask
) != 0;
134 * test_bit - Determine whether a bit is set
135 * @nr: bit number to test
136 * @addr: Address to start counting from
138 static inline int test_bit(int nr
, const volatile unsigned long *addr
)
140 return 1UL & (addr
[BIT_WORD(nr
)] >> (nr
& (BITS_PER_LONG
-1)));
143 #endif /* __ASM_SH_BITOPS_OP32_H */