xref: /OK3568_Linux_fs/kernel/include/asm-generic/bitops/lock.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _ASM_GENERIC_BITOPS_LOCK_H_
3*4882a593Smuzhiyun #define _ASM_GENERIC_BITOPS_LOCK_H_
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/atomic.h>
6*4882a593Smuzhiyun #include <linux/compiler.h>
7*4882a593Smuzhiyun #include <asm/barrier.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /**
10*4882a593Smuzhiyun  * test_and_set_bit_lock - Set a bit and return its old value, for lock
11*4882a593Smuzhiyun  * @nr: Bit to set
12*4882a593Smuzhiyun  * @addr: Address to count from
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * This operation is atomic and provides acquire barrier semantics if
15*4882a593Smuzhiyun  * the returned value is 0.
16*4882a593Smuzhiyun  * It can be used to implement bit locks.
17*4882a593Smuzhiyun  */
test_and_set_bit_lock(unsigned int nr,volatile unsigned long * p)18*4882a593Smuzhiyun static inline int test_and_set_bit_lock(unsigned int nr,
19*4882a593Smuzhiyun 					volatile unsigned long *p)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	long old;
22*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	p += BIT_WORD(nr);
25*4882a593Smuzhiyun 	if (READ_ONCE(*p) & mask)
26*4882a593Smuzhiyun 		return 1;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	old = atomic_long_fetch_or_acquire(mask, (atomic_long_t *)p);
29*4882a593Smuzhiyun 	return !!(old & mask);
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun  * clear_bit_unlock - Clear a bit in memory, for unlock
35*4882a593Smuzhiyun  * @nr: the bit to set
36*4882a593Smuzhiyun  * @addr: the address to start counting from
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * This operation is atomic and provides release barrier semantics.
39*4882a593Smuzhiyun  */
clear_bit_unlock(unsigned int nr,volatile unsigned long * p)40*4882a593Smuzhiyun static inline void clear_bit_unlock(unsigned int nr, volatile unsigned long *p)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	p += BIT_WORD(nr);
43*4882a593Smuzhiyun 	atomic_long_fetch_andnot_release(BIT_MASK(nr), (atomic_long_t *)p);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /**
47*4882a593Smuzhiyun  * __clear_bit_unlock - Clear a bit in memory, for unlock
48*4882a593Smuzhiyun  * @nr: the bit to set
49*4882a593Smuzhiyun  * @addr: the address to start counting from
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * A weaker form of clear_bit_unlock() as used by __bit_lock_unlock(). If all
52*4882a593Smuzhiyun  * the bits in the word are protected by this lock some archs can use weaker
53*4882a593Smuzhiyun  * ops to safely unlock.
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * See for example x86's implementation.
56*4882a593Smuzhiyun  */
__clear_bit_unlock(unsigned int nr,volatile unsigned long * p)57*4882a593Smuzhiyun static inline void __clear_bit_unlock(unsigned int nr,
58*4882a593Smuzhiyun 				      volatile unsigned long *p)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	unsigned long old;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	p += BIT_WORD(nr);
63*4882a593Smuzhiyun 	old = READ_ONCE(*p);
64*4882a593Smuzhiyun 	old &= ~BIT_MASK(nr);
65*4882a593Smuzhiyun 	atomic_long_set_release((atomic_long_t *)p, old);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /**
69*4882a593Smuzhiyun  * clear_bit_unlock_is_negative_byte - Clear a bit in memory and test if bottom
70*4882a593Smuzhiyun  *                                     byte is negative, for unlock.
71*4882a593Smuzhiyun  * @nr: the bit to clear
72*4882a593Smuzhiyun  * @addr: the address to start counting from
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * This is a bit of a one-trick-pony for the filemap code, which clears
75*4882a593Smuzhiyun  * PG_locked and tests PG_waiters,
76*4882a593Smuzhiyun  */
77*4882a593Smuzhiyun #ifndef clear_bit_unlock_is_negative_byte
clear_bit_unlock_is_negative_byte(unsigned int nr,volatile unsigned long * p)78*4882a593Smuzhiyun static inline bool clear_bit_unlock_is_negative_byte(unsigned int nr,
79*4882a593Smuzhiyun 						     volatile unsigned long *p)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	long old;
82*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	p += BIT_WORD(nr);
85*4882a593Smuzhiyun 	old = atomic_long_fetch_andnot_release(mask, (atomic_long_t *)p);
86*4882a593Smuzhiyun 	return !!(old & BIT(7));
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun #define clear_bit_unlock_is_negative_byte clear_bit_unlock_is_negative_byte
89*4882a593Smuzhiyun #endif
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun #endif /* _ASM_GENERIC_BITOPS_LOCK_H_ */
92