summaryrefslogtreecommitdiffstats
path: root/arch/x86/lib/atomic64_32.c
blob: cd11803f9448fc034fc8bb284518e64e94e58dd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <linux/compiler.h>
#include <linux/types.h>
#include <asm/processor.h>
#include <asm/cmpxchg.h>
#include <asm/atomic.h>

static noinline u64 cmpxchg8b(u64 *ptr, u64 old, u64 new)
{
	u32 low = new;
	u32 high = new >> 32;

	asm volatile(
		LOCK_PREFIX "cmpxchg8b %1\n"
		     : "+A" (old), "+m" (*ptr)
		     :  "b" (low),  "c" (high)
		     );
	return old;
}

u64 atomic64_cmpxchg(atomic64_t *ptr, u64 old_val, u64 new_val)
{
	return cmpxchg8b(&ptr->counter, old_val, new_val);
}

/**
 * atomic64_xchg - xchg atomic64 variable
 * @ptr:      pointer to type atomic64_t
 * @new_val:  value to assign
 *
 * Atomically xchgs the value of @ptr to @new_val and returns
 * the old value.
 */

u64 atomic64_xchg(atomic64_t *ptr, u64 new_val)
{
	u64 old_val;

	do {
		old_val = __atomic64_read(ptr);
	} while (atomic64_cmpxchg(ptr, old_val, new_val) != old_val);

	return old_val;
}

/**
 * atomic64_set - set atomic64 variable
 * @ptr:      pointer to type atomic64_t
 * @new_val:  value to assign
 *
 * Atomically sets the value of @ptr to @new_val.
 */
void atomic64_set(atomic64_t *ptr, u64 new_val)
{
	atomic64_xchg(ptr, new_val);
}

/**
 * atomic64_read - read atomic64 variable
 * @ptr:      pointer to type atomic64_t
 *
 * Atomically reads the value of @ptr and returns it.
 */
u64 atomic64_read(atomic64_t *ptr)
{
	u64 res;

	asm volatile(
		"mov %%ebx, %%eax\n\t"
		"mov %%ecx, %%edx\n\t"
		LOCK_PREFIX "cmpxchg8b %1\n"
			: "+A" (res)
			: "m" (*ptr)
		);

	return res;
}

/**
 * atomic64_add_return - add and return
 * @delta: integer value to add
 * @ptr:   pointer to type atomic64_t
 *
 * Atomically adds @delta to @ptr and returns @delta + *@ptr
 */
noinline u64 atomic64_add_return(u64 delta, atomic64_t *ptr)
{
	/*
	 * Try first with a (probably incorrect) assumption about
	 * what we have there. We'll do two loops most likely,
	 * but we'll get an ownership MESI transaction straight away
	 * instead of a read transaction followed by a
	 * flush-for-ownership transaction:
	 */
	u64 old_val, new_val, real_val = 1ULL << 32;

	do {
		old_val = real_val;
		new_val = old_val + delta;

		real_val = atomic64_cmpxchg(ptr, old_val, new_val);

	} while (real_val != old_val);

	return new_val;
}

u64 atomic64_sub_return(u64 delta, atomic64_t *ptr)
{
	return atomic64_add_return(-delta, ptr);
}

u64 atomic64_inc_return(atomic64_t *ptr)
{
	return atomic64_add_return(1, ptr);
}

u64 atomic64_dec_return(atomic64_t *ptr)
{
	return atomic64_sub_return(1, ptr);
}

/**
 * atomic64_add - add integer to atomic64 variable
 * @delta: integer value to add
 * @ptr:   pointer to type atomic64_t
 *
 * Atomically adds @delta to @ptr.
 */
void atomic64_add(u64 delta, atomic64_t *ptr)
{
	atomic64_add_return(delta, ptr);
}

/**
 * atomic64_sub - subtract the atomic64 variable
 * @delta: integer value to subtract
 * @ptr:   pointer to type atomic64_t
 *
 * Atomically subtracts @delta from @ptr.
 */
void atomic64_sub(u64 delta, atomic64_t *ptr)
{
	atomic64_add(-delta, ptr);
}

/**
 * atomic64_sub_and_test - subtract value from variable and test result
 * @delta: integer value to subtract
 * @ptr:   pointer to type atomic64_t
 *
 * Atomically subtracts @delta from @ptr and returns
 * true if the result is zero, or false for all
 * other cases.
 */
int atomic64_sub_and_test(u64 delta, atomic64_t *ptr)
{
	u64 old_val = atomic64_sub_return(delta, ptr);

	return old_val == 0;
}

/**
 * atomic64_inc - increment atomic64 variable
 * @ptr: pointer to type atomic64_t
 *
 * Atomically increments @ptr by 1.
 */
void atomic64_inc(atomic64_t *ptr)
{
	atomic64_add(1, ptr);
}

/**
 * atomic64_dec - decrement atomic64 variable
 * @ptr: pointer to type atomic64_t
 *
 * Atomically decrements @ptr by 1.
 */
void atomic64_dec(atomic64_t *ptr)
{
	atomic64_sub(1, ptr);
}

/**
 * atomic64_dec_and_test - decrement and test
 * @ptr: pointer to type atomic64_t
 *
 * Atomically decrements @ptr by 1 and
 * returns true if the result is 0, or false for all other
 * cases.
 */
int atomic64_dec_and_test(atomic64_t *ptr)
{
	return atomic64_sub_and_test(1, ptr);
}

/**
 * atomic64_inc_and_test - increment and test
 * @ptr: pointer to type atomic64_t
 *
 * Atomically increments @ptr by 1
 * and returns true if the result is zero, or false for all
 * other cases.
 */
int atomic64_inc_and_test(atomic64_t *ptr)
{
	return atomic64_sub_and_test(-1, ptr);
}

/**
 * atomic64_add_negative - add and test if negative
 * @delta: integer value to add
 * @ptr:   pointer to type atomic64_t
 *
 * Atomically adds @delta to @ptr and returns true
 * if the result is negative, or false when
 * result is greater than or equal to zero.
 */
int atomic64_add_negative(u64 delta, atomic64_t *ptr)
{
	long long old_val = atomic64_add_return(delta, ptr);

	return old_val < 0;
}
OpenPOWER on IntegriCloud