Path: blob/master/arch/x86/include/asm/atomic64_32.h
10821 views
#ifndef _ASM_X86_ATOMIC64_32_H1#define _ASM_X86_ATOMIC64_32_H23#include <linux/compiler.h>4#include <linux/types.h>5#include <asm/processor.h>6//#include <asm/cmpxchg.h>78/* An 64bit atomic type */910typedef struct {11u64 __aligned(8) counter;12} atomic64_t;1314#define ATOMIC64_INIT(val) { (val) }1516#ifdef CONFIG_X86_CMPXCHG6417#define ATOMIC64_ALTERNATIVE_(f, g) "call atomic64_" #g "_cx8"18#else19#define ATOMIC64_ALTERNATIVE_(f, g) ALTERNATIVE("call atomic64_" #f "_386", "call atomic64_" #g "_cx8", X86_FEATURE_CX8)20#endif2122#define ATOMIC64_ALTERNATIVE(f) ATOMIC64_ALTERNATIVE_(f, f)2324/**25* atomic64_cmpxchg - cmpxchg atomic64 variable26* @p: pointer to type atomic64_t27* @o: expected value28* @n: new value29*30* Atomically sets @v to @n if it was equal to @o and returns31* the old value.32*/3334static inline long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)35{36return cmpxchg64(&v->counter, o, n);37}3839/**40* atomic64_xchg - xchg atomic64 variable41* @v: pointer to type atomic64_t42* @n: value to assign43*44* Atomically xchgs the value of @v to @n and returns45* the old value.46*/47static inline long long atomic64_xchg(atomic64_t *v, long long n)48{49long long o;50unsigned high = (unsigned)(n >> 32);51unsigned low = (unsigned)n;52asm volatile(ATOMIC64_ALTERNATIVE(xchg)53: "=A" (o), "+b" (low), "+c" (high)54: "S" (v)55: "memory"56);57return o;58}5960/**61* atomic64_set - set atomic64 variable62* @v: pointer to type atomic64_t63* @n: value to assign64*65* Atomically sets the value of @v to @n.66*/67static inline void atomic64_set(atomic64_t *v, long long i)68{69unsigned high = (unsigned)(i >> 32);70unsigned low = (unsigned)i;71asm volatile(ATOMIC64_ALTERNATIVE(set)72: "+b" (low), "+c" (high)73: "S" (v)74: "eax", "edx", "memory"75);76}7778/**79* atomic64_read - read atomic64 variable80* @v: pointer to type atomic64_t81*82* Atomically reads the value of @v and returns it.83*/84static inline long long atomic64_read(atomic64_t *v)85{86long long r;87asm volatile(ATOMIC64_ALTERNATIVE(read)88: "=A" (r), "+c" (v)89: : "memory"90);91return r;92}9394/**95* atomic64_add_return - add and return96* @i: integer value to add97* @v: pointer to type atomic64_t98*99* Atomically adds @i to @v and returns @i + *@v100*/101static inline long long atomic64_add_return(long long i, atomic64_t *v)102{103asm volatile(ATOMIC64_ALTERNATIVE(add_return)104: "+A" (i), "+c" (v)105: : "memory"106);107return i;108}109110/*111* Other variants with different arithmetic operators:112*/113static inline long long atomic64_sub_return(long long i, atomic64_t *v)114{115asm volatile(ATOMIC64_ALTERNATIVE(sub_return)116: "+A" (i), "+c" (v)117: : "memory"118);119return i;120}121122static inline long long atomic64_inc_return(atomic64_t *v)123{124long long a;125asm volatile(ATOMIC64_ALTERNATIVE(inc_return)126: "=A" (a)127: "S" (v)128: "memory", "ecx"129);130return a;131}132133static inline long long atomic64_dec_return(atomic64_t *v)134{135long long a;136asm volatile(ATOMIC64_ALTERNATIVE(dec_return)137: "=A" (a)138: "S" (v)139: "memory", "ecx"140);141return a;142}143144/**145* atomic64_add - add integer to atomic64 variable146* @i: integer value to add147* @v: pointer to type atomic64_t148*149* Atomically adds @i to @v.150*/151static inline long long atomic64_add(long long i, atomic64_t *v)152{153asm volatile(ATOMIC64_ALTERNATIVE_(add, add_return)154: "+A" (i), "+c" (v)155: : "memory"156);157return i;158}159160/**161* atomic64_sub - subtract the atomic64 variable162* @i: integer value to subtract163* @v: pointer to type atomic64_t164*165* Atomically subtracts @i from @v.166*/167static inline long long atomic64_sub(long long i, atomic64_t *v)168{169asm volatile(ATOMIC64_ALTERNATIVE_(sub, sub_return)170: "+A" (i), "+c" (v)171: : "memory"172);173return i;174}175176/**177* atomic64_sub_and_test - subtract value from variable and test result178* @i: integer value to subtract179* @v: pointer to type atomic64_t180*181* Atomically subtracts @i from @v and returns182* true if the result is zero, or false for all183* other cases.184*/185static inline int atomic64_sub_and_test(long long i, atomic64_t *v)186{187return atomic64_sub_return(i, v) == 0;188}189190/**191* atomic64_inc - increment atomic64 variable192* @v: pointer to type atomic64_t193*194* Atomically increments @v by 1.195*/196static inline void atomic64_inc(atomic64_t *v)197{198asm volatile(ATOMIC64_ALTERNATIVE_(inc, inc_return)199: : "S" (v)200: "memory", "eax", "ecx", "edx"201);202}203204/**205* atomic64_dec - decrement atomic64 variable206* @ptr: pointer to type atomic64_t207*208* Atomically decrements @ptr by 1.209*/210static inline void atomic64_dec(atomic64_t *v)211{212asm volatile(ATOMIC64_ALTERNATIVE_(dec, dec_return)213: : "S" (v)214: "memory", "eax", "ecx", "edx"215);216}217218/**219* atomic64_dec_and_test - decrement and test220* @v: pointer to type atomic64_t221*222* Atomically decrements @v by 1 and223* returns true if the result is 0, or false for all other224* cases.225*/226static inline int atomic64_dec_and_test(atomic64_t *v)227{228return atomic64_dec_return(v) == 0;229}230231/**232* atomic64_inc_and_test - increment and test233* @v: pointer to type atomic64_t234*235* Atomically increments @v by 1236* and returns true if the result is zero, or false for all237* other cases.238*/239static inline int atomic64_inc_and_test(atomic64_t *v)240{241return atomic64_inc_return(v) == 0;242}243244/**245* atomic64_add_negative - add and test if negative246* @i: integer value to add247* @v: pointer to type atomic64_t248*249* Atomically adds @i to @v and returns true250* if the result is negative, or false when251* result is greater than or equal to zero.252*/253static inline int atomic64_add_negative(long long i, atomic64_t *v)254{255return atomic64_add_return(i, v) < 0;256}257258/**259* atomic64_add_unless - add unless the number is a given value260* @v: pointer of type atomic64_t261* @a: the amount to add to v...262* @u: ...unless v is equal to u.263*264* Atomically adds @a to @v, so long as it was not @u.265* Returns non-zero if @v was not @u, and zero otherwise.266*/267static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)268{269unsigned low = (unsigned)u;270unsigned high = (unsigned)(u >> 32);271asm volatile(ATOMIC64_ALTERNATIVE(add_unless) "\n\t"272: "+A" (a), "+c" (v), "+S" (low), "+D" (high)273: : "memory");274return (int)a;275}276277278static inline int atomic64_inc_not_zero(atomic64_t *v)279{280int r;281asm volatile(ATOMIC64_ALTERNATIVE(inc_not_zero)282: "=a" (r)283: "S" (v)284: "ecx", "edx", "memory"285);286return r;287}288289static inline long long atomic64_dec_if_positive(atomic64_t *v)290{291long long r;292asm volatile(ATOMIC64_ALTERNATIVE(dec_if_positive)293: "=A" (r)294: "S" (v)295: "ecx", "memory"296);297return r;298}299300#undef ATOMIC64_ALTERNATIVE301#undef ATOMIC64_ALTERNATIVE_302303#endif /* _ASM_X86_ATOMIC64_32_H */304305306