Simon Glass | d37de65 | 2015-04-28 20:25:12 -0600 | [diff] [blame] | 1 | #ifndef _ASM_X86_ATOMIC_H |
| 2 | #define _ASM_X86_ATOMIC_H |
| 3 | |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 4 | #include <linux/bitops.h> |
Simon Glass | d37de65 | 2015-04-28 20:25:12 -0600 | [diff] [blame] | 5 | #include <linux/compiler.h> |
| 6 | #include <linux/types.h> |
| 7 | #include <asm/processor.h> |
| 8 | |
| 9 | typedef struct { volatile int counter; } atomic_t; |
| 10 | |
| 11 | /* |
| 12 | * Atomic operations that C can't guarantee us. Useful for |
| 13 | * resource counting etc.. |
| 14 | */ |
| 15 | |
| 16 | #define ATOMIC_INIT(i) { (i) } |
| 17 | |
| 18 | /** |
| 19 | * atomic_read - read atomic variable |
| 20 | * @v: pointer of type atomic_t |
| 21 | * |
| 22 | * Atomically reads the value of @v. |
| 23 | */ |
| 24 | static inline int atomic_read(const atomic_t *v) |
| 25 | { |
Tom Rini | b828d10 | 2020-05-14 08:30:04 -0400 | [diff] [blame] | 26 | return READ_ONCE((v)->counter); |
Simon Glass | d37de65 | 2015-04-28 20:25:12 -0600 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /** |
| 30 | * atomic_set - set atomic variable |
| 31 | * @v: pointer of type atomic_t |
| 32 | * @i: required value |
| 33 | * |
| 34 | * Atomically sets the value of @v to @i. |
| 35 | */ |
| 36 | static inline void atomic_set(atomic_t *v, int i) |
| 37 | { |
| 38 | v->counter = i; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * atomic_add - add integer to atomic variable |
| 43 | * @i: integer value to add |
| 44 | * @v: pointer of type atomic_t |
| 45 | * |
| 46 | * Atomically adds @i to @v. |
| 47 | */ |
| 48 | static inline void atomic_add(int i, atomic_t *v) |
| 49 | { |
| 50 | asm volatile(LOCK_PREFIX "addl %1,%0" |
| 51 | : "+m" (v->counter) |
| 52 | : "ir" (i)); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * atomic_sub - subtract integer from atomic variable |
| 57 | * @i: integer value to subtract |
| 58 | * @v: pointer of type atomic_t |
| 59 | * |
| 60 | * Atomically subtracts @i from @v. |
| 61 | */ |
| 62 | static inline void atomic_sub(int i, atomic_t *v) |
| 63 | { |
| 64 | asm volatile(LOCK_PREFIX "subl %1,%0" |
| 65 | : "+m" (v->counter) |
| 66 | : "ir" (i)); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * atomic_inc - increment atomic variable |
| 71 | * @v: pointer of type atomic_t |
| 72 | * |
| 73 | * Atomically increments @v by 1. |
| 74 | */ |
| 75 | static inline void atomic_inc(atomic_t *v) |
| 76 | { |
| 77 | asm volatile(LOCK_PREFIX "incl %0" |
| 78 | : "+m" (v->counter)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * atomic_dec - decrement atomic variable |
| 83 | * @v: pointer of type atomic_t |
| 84 | * |
| 85 | * Atomically decrements @v by 1. |
| 86 | */ |
| 87 | static inline void atomic_dec(atomic_t *v) |
| 88 | { |
| 89 | asm volatile(LOCK_PREFIX "decl %0" |
| 90 | : "+m" (v->counter)); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * atomic_inc_short - increment of a short integer |
| 95 | * @v: pointer to type int |
| 96 | * |
| 97 | * Atomically adds 1 to @v |
| 98 | * Returns the new value of @u |
| 99 | */ |
| 100 | static inline short int atomic_inc_short(short int *v) |
| 101 | { |
| 102 | asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v)); |
| 103 | return *v; |
| 104 | } |
| 105 | |
| 106 | /* These are x86-specific, used by some header files */ |
| 107 | #define atomic_clear_mask(mask, addr) \ |
| 108 | asm volatile(LOCK_PREFIX "andl %0,%1" \ |
| 109 | : : "r" (~(mask)), "m" (*(addr)) : "memory") |
| 110 | |
| 111 | #define atomic_set_mask(mask, addr) \ |
| 112 | asm volatile(LOCK_PREFIX "orl %0,%1" \ |
| 113 | : : "r" ((unsigned)(mask)), "m" (*(addr)) \ |
| 114 | : "memory") |
| 115 | |
| 116 | #endif /* _ASM_X86_ATOMIC_H */ |