atomic64_32.h 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef _ASM_X86_ATOMIC64_32_H
#define _ASM_X86_ATOMIC64_32_H

#include <linux/compiler.h>
#include <linux/types.h>
#include <asm/processor.h>
//#include <asm/cmpxchg.h>

/* An 64bit atomic type */

typedef struct {
	u64 __aligned(8) counter;
} atomic64_t;

#define ATOMIC64_INIT(val)	{ (val) }

17 18 19 20 21 22 23 24
#define __ATOMIC64_DECL(sym) void atomic64_##sym(atomic64_t *, ...)
#ifndef ATOMIC64_EXPORT
#define ATOMIC64_DECL_ONE __ATOMIC64_DECL
#else
#define ATOMIC64_DECL_ONE(sym) __ATOMIC64_DECL(sym); \
	ATOMIC64_EXPORT(atomic64_##sym)
#endif

25
#ifdef CONFIG_X86_CMPXCHG64
26 27 28 29 30
#define __alternative_atomic64(f, g, out, in...) \
	asm volatile("call %P[func]" \
		     : out : [func] "i" (atomic64_##g##_cx8), ## in)

#define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8)
31
#else
32 33 34 35 36 37 38 39 40 41 42
#define __alternative_atomic64(f, g, out, in...) \
	alternative_call(atomic64_##f##_386, atomic64_##g##_cx8, \
			 X86_FEATURE_CX8, ASM_OUTPUT2(out), ## in)

#define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8); \
	ATOMIC64_DECL_ONE(sym##_386)

ATOMIC64_DECL_ONE(add_386);
ATOMIC64_DECL_ONE(sub_386);
ATOMIC64_DECL_ONE(inc_386);
ATOMIC64_DECL_ONE(dec_386);
43 44
#endif

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#define alternative_atomic64(f, out, in...) \
	__alternative_atomic64(f, f, ASM_OUTPUT2(out), ## in)

ATOMIC64_DECL(read);
ATOMIC64_DECL(set);
ATOMIC64_DECL(xchg);
ATOMIC64_DECL(add_return);
ATOMIC64_DECL(sub_return);
ATOMIC64_DECL(inc_return);
ATOMIC64_DECL(dec_return);
ATOMIC64_DECL(dec_if_positive);
ATOMIC64_DECL(inc_not_zero);
ATOMIC64_DECL(add_unless);

#undef ATOMIC64_DECL
#undef ATOMIC64_DECL_ONE
#undef __ATOMIC64_DECL
#undef ATOMIC64_EXPORT
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/**
 * atomic64_cmpxchg - cmpxchg atomic64 variable
 * @p: pointer to type atomic64_t
 * @o: expected value
 * @n: new value
 *
 * Atomically sets @v to @n if it was equal to @o and returns
 * the old value.
 */

static inline long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
{
	return cmpxchg64(&v->counter, o, n);
}
78 79 80

/**
 * atomic64_xchg - xchg atomic64 variable
81 82
 * @v: pointer to type atomic64_t
 * @n: value to assign
83
 *
84
 * Atomically xchgs the value of @v to @n and returns
85 86
 * the old value.
 */
87 88 89 90 91
static inline long long atomic64_xchg(atomic64_t *v, long long n)
{
	long long o;
	unsigned high = (unsigned)(n >> 32);
	unsigned low = (unsigned)n;
92 93 94
	alternative_atomic64(xchg, "=&A" (o),
			     "S" (v), "b" (low), "c" (high)
			     : "memory");
95 96
	return o;
}
97 98 99

/**
 * atomic64_set - set atomic64 variable
100 101
 * @v: pointer to type atomic64_t
 * @n: value to assign
102
 *
103
 * Atomically sets the value of @v to @n.
104
 */
105 106 107 108
static inline void atomic64_set(atomic64_t *v, long long i)
{
	unsigned high = (unsigned)(i >> 32);
	unsigned low = (unsigned)i;
109 110 111
	alternative_atomic64(set, /* no output */,
			     "S" (v), "b" (low), "c" (high)
			     : "eax", "edx", "memory");
112
}
113 114 115

/**
 * atomic64_read - read atomic64 variable
116
 * @v: pointer to type atomic64_t
117
 *
118
 * Atomically reads the value of @v and returns it.
119
 */
120
static inline long long atomic64_read(const atomic64_t *v)
121
{
122
	long long r;
123
	alternative_atomic64(read, "=&A" (r), "c" (v) : "memory");
124 125
	return r;
 }
126 127 128

/**
 * atomic64_add_return - add and return
129 130
 * @i: integer value to add
 * @v: pointer to type atomic64_t
131
 *
132
 * Atomically adds @i to @v and returns @i + *@v
133
 */
134 135
static inline long long atomic64_add_return(long long i, atomic64_t *v)
{
136 137 138
	alternative_atomic64(add_return,
			     ASM_OUTPUT2("+A" (i), "+c" (v)),
			     ASM_NO_INPUT_CLOBBER("memory"));
139 140
	return i;
}
141 142 143 144

/*
 * Other variants with different arithmetic operators:
 */
145 146
static inline long long atomic64_sub_return(long long i, atomic64_t *v)
{
147 148 149
	alternative_atomic64(sub_return,
			     ASM_OUTPUT2("+A" (i), "+c" (v)),
			     ASM_NO_INPUT_CLOBBER("memory"));
150 151 152 153 154 155
	return i;
}

static inline long long atomic64_inc_return(atomic64_t *v)
{
	long long a;
156 157
	alternative_atomic64(inc_return, "=&A" (a),
			     "S" (v) : "memory", "ecx");
158 159 160 161 162 163
	return a;
}

static inline long long atomic64_dec_return(atomic64_t *v)
{
	long long a;
164 165
	alternative_atomic64(dec_return, "=&A" (a),
			     "S" (v) : "memory", "ecx");
166 167
	return a;
}
168 169 170

/**
 * atomic64_add - add integer to atomic64 variable
171 172
 * @i: integer value to add
 * @v: pointer to type atomic64_t
173
 *
174
 * Atomically adds @i to @v.
175
 */
176 177
static inline long long atomic64_add(long long i, atomic64_t *v)
{
178 179 180
	__alternative_atomic64(add, add_return,
			       ASM_OUTPUT2("+A" (i), "+c" (v)),
			       ASM_NO_INPUT_CLOBBER("memory"));
181 182
	return i;
}
183 184 185

/**
 * atomic64_sub - subtract the atomic64 variable
186 187
 * @i: integer value to subtract
 * @v: pointer to type atomic64_t
188
 *
189
 * Atomically subtracts @i from @v.
190
 */
191 192
static inline long long atomic64_sub(long long i, atomic64_t *v)
{
193 194 195
	__alternative_atomic64(sub, sub_return,
			       ASM_OUTPUT2("+A" (i), "+c" (v)),
			       ASM_NO_INPUT_CLOBBER("memory"));
196 197
	return i;
}
198 199 200

/**
 * atomic64_sub_and_test - subtract value from variable and test result
201 202 203 204
 * @i: integer value to subtract
 * @v: pointer to type atomic64_t
  *
 * Atomically subtracts @i from @v and returns
205 206 207
 * true if the result is zero, or false for all
 * other cases.
 */
208 209 210 211
static inline int atomic64_sub_and_test(long long i, atomic64_t *v)
{
	return atomic64_sub_return(i, v) == 0;
}
212 213 214

/**
 * atomic64_inc - increment atomic64 variable
215
 * @v: pointer to type atomic64_t
216
 *
217
 * Atomically increments @v by 1.
218
 */
219 220
static inline void atomic64_inc(atomic64_t *v)
{
221 222
	__alternative_atomic64(inc, inc_return, /* no output */,
			       "S" (v) : "memory", "eax", "ecx", "edx");
223
}
224 225 226 227 228 229 230

/**
 * atomic64_dec - decrement atomic64 variable
 * @ptr: pointer to type atomic64_t
 *
 * Atomically decrements @ptr by 1.
 */
231 232
static inline void atomic64_dec(atomic64_t *v)
{
233 234
	__alternative_atomic64(dec, dec_return, /* no output */,
			       "S" (v) : "memory", "eax", "ecx", "edx");
235
}
236 237 238

/**
 * atomic64_dec_and_test - decrement and test
239
 * @v: pointer to type atomic64_t
240
 *
241
 * Atomically decrements @v by 1 and
242 243 244
 * returns true if the result is 0, or false for all other
 * cases.
 */
245 246 247 248
static inline int atomic64_dec_and_test(atomic64_t *v)
{
	return atomic64_dec_return(v) == 0;
}
249 250 251

/**
 * atomic64_inc_and_test - increment and test
252
 * @v: pointer to type atomic64_t
253
 *
254
 * Atomically increments @v by 1
255 256 257
 * and returns true if the result is zero, or false for all
 * other cases.
 */
258 259 260 261
static inline int atomic64_inc_and_test(atomic64_t *v)
{
	return atomic64_inc_return(v) == 0;
}
262 263 264

/**
 * atomic64_add_negative - add and test if negative
265 266
 * @i: integer value to add
 * @v: pointer to type atomic64_t
267
 *
268
 * Atomically adds @i to @v and returns true
269 270 271
 * if the result is negative, or false when
 * result is greater than or equal to zero.
 */
272 273 274 275 276 277 278 279 280 281 282 283
static inline int atomic64_add_negative(long long i, atomic64_t *v)
{
	return atomic64_add_return(i, v) < 0;
}

/**
 * atomic64_add_unless - add unless the number is a given value
 * @v: pointer of type atomic64_t
 * @a: the amount to add to v...
 * @u: ...unless v is equal to u.
 *
 * Atomically adds @a to @v, so long as it was not @u.
284
 * Returns non-zero if the add was done, zero otherwise.
285 286 287 288 289
 */
static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
{
	unsigned low = (unsigned)u;
	unsigned high = (unsigned)(u >> 32);
290
	alternative_atomic64(add_unless,
291 292
			     ASM_OUTPUT2("+A" (a), "+c" (low), "+D" (high)),
			     "S" (v) : "memory");
293 294 295 296 297 298 299
	return (int)a;
}


static inline int atomic64_inc_not_zero(atomic64_t *v)
{
	int r;
300 301
	alternative_atomic64(inc_not_zero, "=&a" (r),
			     "S" (v) : "ecx", "edx", "memory");
302 303 304 305 306 307
	return r;
}

static inline long long atomic64_dec_if_positive(atomic64_t *v)
{
	long long r;
308 309
	alternative_atomic64(dec_if_positive, "=&A" (r),
			     "S" (v) : "ecx", "memory");
310 311 312
	return r;
}

313 314
#undef alternative_atomic64
#undef __alternative_atomic64
315 316

#endif /* _ASM_X86_ATOMIC64_32_H */