xref: /openbmc/u-boot/arch/powerpc/include/asm/atomic.h (revision 4e3349b6)
1 /*
2  * PowerPC atomic operations
3  */
4 
5 #ifndef _ASM_PPC_ATOMIC_H_
6 #define _ASM_PPC_ATOMIC_H_
7 
8 #include <linux/config.h>
9 
10 #ifdef CONFIG_SMP
11 typedef struct { volatile int counter; } atomic_t;
12 #else
13 typedef struct { int counter; } atomic_t;
14 #endif
15 
16 #define ATOMIC_INIT(i)	{ (i) }
17 
18 #define atomic_read(v)		((v)->counter)
19 #define atomic_set(v,i)		(((v)->counter) = (i))
20 
21 extern void atomic_clear_mask(unsigned long mask, unsigned long *addr);
22 extern void atomic_set_mask(unsigned long mask, unsigned long *addr);
23 
24 extern __inline__ int atomic_add_return(int a, atomic_t *v)
25 {
26 	int t;
27 
28 	__asm__ __volatile__("\n\
29 1:	lwarx	%0,0,%3\n\
30 	add	%0,%2,%0\n\
31 	stwcx.	%0,0,%3\n\
32 	bne-	1b"
33 	: "=&r" (t), "=m" (*v)
34 	: "r" (a), "r" (v), "m" (*v)
35 	: "cc");
36 
37 	return t;
38 }
39 
40 extern __inline__ int atomic_sub_return(int a, atomic_t *v)
41 {
42 	int t;
43 
44 	__asm__ __volatile__("\n\
45 1:	lwarx	%0,0,%3\n\
46 	subf	%0,%2,%0\n\
47 	stwcx.	%0,0,%3\n\
48 	bne-	1b"
49 	: "=&r" (t), "=m" (*v)
50 	: "r" (a), "r" (v), "m" (*v)
51 	: "cc");
52 
53 	return t;
54 }
55 
56 extern __inline__ int atomic_inc_return(atomic_t *v)
57 {
58 	int t;
59 
60 	__asm__ __volatile__("\n\
61 1:	lwarx	%0,0,%2\n\
62 	addic	%0,%0,1\n\
63 	stwcx.	%0,0,%2\n\
64 	bne-	1b"
65 	: "=&r" (t), "=m" (*v)
66 	: "r" (v), "m" (*v)
67 	: "cc");
68 
69 	return t;
70 }
71 
72 extern __inline__ int atomic_dec_return(atomic_t *v)
73 {
74 	int t;
75 
76 	__asm__ __volatile__("\n\
77 1:	lwarx	%0,0,%2\n\
78 	addic	%0,%0,-1\n\
79 	stwcx.	%0,0,%2\n\
80 	bne	1b"
81 	: "=&r" (t), "=m" (*v)
82 	: "r" (v), "m" (*v)
83 	: "cc");
84 
85 	return t;
86 }
87 
88 #define atomic_add(a, v)		((void) atomic_add_return((a), (v)))
89 #define atomic_sub(a, v)		((void) atomic_sub_return((a), (v)))
90 #define atomic_sub_and_test(a, v)	(atomic_sub_return((a), (v)) == 0)
91 #define atomic_inc(v)			((void) atomic_inc_return((v)))
92 #define atomic_dec(v)			((void) atomic_dec_return((v)))
93 #define atomic_dec_and_test(v)		(atomic_dec_return((v)) == 0)
94 
95 #endif /* _ASM_PPC_ATOMIC_H_ */
96