xref: /openbmc/linux/arch/sh/include/asm/bitops-cas.h (revision 799a545b)
1 #ifndef __ASM_SH_BITOPS_CAS_H
2 #define __ASM_SH_BITOPS_CAS_H
3 
4 static inline unsigned __bo_cas(volatile unsigned *p, unsigned old, unsigned new)
5 {
6 	__asm__ __volatile__("cas.l %1,%0,@r0"
7 		: "+r"(new)
8 		: "r"(old), "z"(p)
9 		: "t", "memory" );
10 	return new;
11 }
12 
13 static inline void set_bit(int nr, volatile void *addr)
14 {
15 	unsigned mask, old;
16 	volatile unsigned *a = addr;
17 
18 	a += nr >> 5;
19 	mask = 1U << (nr & 0x1f);
20 
21 	do old = *a;
22 	while (__bo_cas(a, old, old|mask) != old);
23 }
24 
25 static inline void clear_bit(int nr, volatile void *addr)
26 {
27 	unsigned mask, old;
28 	volatile unsigned *a = addr;
29 
30 	a += nr >> 5;
31 	mask = 1U << (nr & 0x1f);
32 
33 	do old = *a;
34 	while (__bo_cas(a, old, old&~mask) != old);
35 }
36 
37 static inline void change_bit(int nr, volatile void *addr)
38 {
39 	unsigned mask, old;
40 	volatile unsigned *a = addr;
41 
42 	a += nr >> 5;
43 	mask = 1U << (nr & 0x1f);
44 
45 	do old = *a;
46 	while (__bo_cas(a, old, old^mask) != old);
47 }
48 
49 static inline int test_and_set_bit(int nr, volatile void *addr)
50 {
51 	unsigned mask, old;
52 	volatile unsigned *a = addr;
53 
54 	a += nr >> 5;
55 	mask = 1U << (nr & 0x1f);
56 
57 	do old = *a;
58 	while (__bo_cas(a, old, old|mask) != old);
59 
60 	return !!(old & mask);
61 }
62 
63 static inline int test_and_clear_bit(int nr, volatile void *addr)
64 {
65 	unsigned mask, old;
66 	volatile unsigned *a = addr;
67 
68 	a += nr >> 5;
69 	mask = 1U << (nr & 0x1f);
70 
71 	do old = *a;
72 	while (__bo_cas(a, old, old&~mask) != old);
73 
74 	return !!(old & mask);
75 }
76 
77 static inline int test_and_change_bit(int nr, volatile void *addr)
78 {
79 	unsigned mask, old;
80 	volatile unsigned *a = addr;
81 
82 	a += nr >> 5;
83 	mask = 1U << (nr & 0x1f);
84 
85 	do old = *a;
86 	while (__bo_cas(a, old, old^mask) != old);
87 
88 	return !!(old & mask);
89 }
90 
91 #include <asm-generic/bitops/non-atomic.h>
92 
93 #endif /* __ASM_SH_BITOPS_CAS_H */
94