From mboxrd@z Thu Jan 1 00:00:00 1970 From: Bin Meng Date: Tue, 28 Apr 2015 15:38:34 +0800 Subject: [U-Boot] [PATCH 10/20] x86: Add atomic operations In-Reply-To: <1430174911-27538-11-git-send-email-sjg@chromium.org> References: <1430174911-27538-1-git-send-email-sjg@chromium.org> <1430174911-27538-11-git-send-email-sjg@chromium.org> Message-ID: List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de On Tue, Apr 28, 2015 at 6:48 AM, Simon Glass wrote: > Add a subset of this header file from Linux 4.0 to support atomic operations > in U-Boot. > > Signed-off-by: Simon Glass > --- > > arch/x86/include/asm/atomic.h | 115 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 115 insertions(+) > create mode 100644 arch/x86/include/asm/atomic.h > > diff --git a/arch/x86/include/asm/atomic.h b/arch/x86/include/asm/atomic.h > new file mode 100644 > index 0000000..806f787 > --- /dev/null > +++ b/arch/x86/include/asm/atomic.h > @@ -0,0 +1,115 @@ > +#ifndef _ASM_X86_ATOMIC_H > +#define _ASM_X86_ATOMIC_H > + > +#include > +#include > +#include > + > +typedef struct { volatile int counter; } atomic_t; > + > +/* > + * Atomic operations that C can't guarantee us. Useful for > + * resource counting etc.. > + */ > + > +#define ATOMIC_INIT(i) { (i) } > + > +/** > + * atomic_read - read atomic variable > + * @v: pointer of type atomic_t > + * > + * Atomically reads the value of @v. > + */ > +static inline int atomic_read(const atomic_t *v) > +{ > + return ACCESS_ONCE((v)->counter); > +} > + > +/** > + * atomic_set - set atomic variable > + * @v: pointer of type atomic_t > + * @i: required value > + * > + * Atomically sets the value of @v to @i. > + */ > +static inline void atomic_set(atomic_t *v, int i) > +{ > + v->counter = i; > +} > + > +/** > + * atomic_add - add integer to atomic variable > + * @i: integer value to add > + * @v: pointer of type atomic_t > + * > + * Atomically adds @i to @v. > + */ > +static inline void atomic_add(int i, atomic_t *v) > +{ > + asm volatile(LOCK_PREFIX "addl %1,%0" > + : "+m" (v->counter) > + : "ir" (i)); > +} > + > +/** > + * atomic_sub - subtract integer from atomic variable > + * @i: integer value to subtract > + * @v: pointer of type atomic_t > + * > + * Atomically subtracts @i from @v. > + */ > +static inline void atomic_sub(int i, atomic_t *v) > +{ > + asm volatile(LOCK_PREFIX "subl %1,%0" > + : "+m" (v->counter) > + : "ir" (i)); > +} > + > +/** > + * atomic_inc - increment atomic variable > + * @v: pointer of type atomic_t > + * > + * Atomically increments @v by 1. > + */ > +static inline void atomic_inc(atomic_t *v) > +{ > + asm volatile(LOCK_PREFIX "incl %0" > + : "+m" (v->counter)); > +} > + > +/** > + * atomic_dec - decrement atomic variable > + * @v: pointer of type atomic_t > + * > + * Atomically decrements @v by 1. > + */ > +static inline void atomic_dec(atomic_t *v) > +{ > + asm volatile(LOCK_PREFIX "decl %0" > + : "+m" (v->counter)); > +} > + > +/** > + * atomic_inc_short - increment of a short integer > + * @v: pointer to type int > + * > + * Atomically adds 1 to @v > + * Returns the new value of @u > + */ > +static inline short int atomic_inc_short(short int *v) > +{ > + asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v)); > + return *v; > +} > + > +/* These are x86-specific, used by some header files */ > +#define atomic_clear_mask(mask, addr) \ > + asm volatile(LOCK_PREFIX "andl %0,%1" \ > + : : "r" (~(mask)), "m" (*(addr)) : "memory") > + > +#define atomic_set_mask(mask, addr) \ > + asm volatile(LOCK_PREFIX "orl %0,%1" \ > + : : "r" ((unsigned)(mask)), "m" (*(addr)) \ > + : "memory") > + > +#endif /* _ASM_X86_ATOMIC_H */ > -- Reviewed-by: Bin Meng