1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#ifndef _ASM_ATOMIC_H
23#define _ASM_ATOMIC_H
24
25#include <linux/types.h>
26#include <asm/cmpxchg.h>
27
28#define ATOMIC_INIT(i) { (i) }
29#define atomic_set(v, i) ((v)->counter = (i))
30
31
32
33
34
35
36
37#define atomic_read(v) ((v)->counter)
38
39
40
41
42
43
44#define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
65{
66 int __oldval;
67
68 asm volatile(
69 "1: %0 = memw_locked(%1);\n"
70 " { P0 = cmp.eq(%0,%2);\n"
71 " if (!P0.new) jump:nt 2f; }\n"
72 " memw_locked(%1,P0) = %3;\n"
73 " if (!P0) jump 1b;\n"
74 "2:\n"
75 : "=&r" (__oldval)
76 : "r" (&v->counter), "r" (old), "r" (new)
77 : "memory", "p0"
78 );
79
80 return __oldval;
81}
82
83static inline int atomic_add_return(int i, atomic_t *v)
84{
85 int output;
86
87 __asm__ __volatile__ (
88 "1: %0 = memw_locked(%1);\n"
89 " %0 = add(%0,%2);\n"
90 " memw_locked(%1,P3)=%0;\n"
91 " if !P3 jump 1b;\n"
92 : "=&r" (output)
93 : "r" (&v->counter), "r" (i)
94 : "memory", "p3"
95 );
96 return output;
97
98}
99
100#define atomic_add(i, v) atomic_add_return(i, (v))
101
102static inline int atomic_sub_return(int i, atomic_t *v)
103{
104 int output;
105 __asm__ __volatile__ (
106 "1: %0 = memw_locked(%1);\n"
107 " %0 = sub(%0,%2);\n"
108 " memw_locked(%1,P3)=%0\n"
109 " if !P3 jump 1b;\n"
110 : "=&r" (output)
111 : "r" (&v->counter), "r" (i)
112 : "memory", "p3"
113 );
114 return output;
115}
116
117#define atomic_sub(i, v) atomic_sub_return(i, (v))
118
119
120
121
122
123
124
125
126
127
128
129static inline int __atomic_add_unless(atomic_t *v, int a, int u)
130{
131 int __oldval;
132 register int tmp;
133
134 asm volatile(
135 "1: %0 = memw_locked(%2);"
136 " {"
137 " p3 = cmp.eq(%0, %4);"
138 " if (p3.new) jump:nt 2f;"
139 " %1 = add(%0, %3);"
140 " }"
141 " memw_locked(%2, p3) = %1;"
142 " {"
143 " if !p3 jump 1b;"
144 " }"
145 "2:"
146 : "=&r" (__oldval), "=&r" (tmp)
147 : "r" (v), "r" (a), "r" (u)
148 : "memory", "p3"
149 );
150 return __oldval;
151}
152
153#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
154
155#define atomic_inc(v) atomic_add(1, (v))
156#define atomic_dec(v) atomic_sub(1, (v))
157
158#define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
159#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
160#define atomic_sub_and_test(i, v) (atomic_sub_return(i, (v)) == 0)
161#define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
162
163
164#define atomic_inc_return(v) (atomic_add_return(1, v))
165#define atomic_dec_return(v) (atomic_sub_return(1, v))
166
167#endif
168