Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _ALPHA_ATOMIC_H |
| 2 | #define _ALPHA_ATOMIC_H |
| 3 | |
| 4 | /* |
| 5 | * Atomic operations that C can't guarantee us. Useful for |
| 6 | * resource counting etc... |
| 7 | * |
| 8 | * But use these as seldom as possible since they are much slower |
| 9 | * than regular operations. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | /* |
| 14 | * Counter is volatile to make sure gcc doesn't try to be clever |
| 15 | * and move things around on us. We need to use _exactly_ the address |
| 16 | * the user gave us, not some alias that contains the same information. |
| 17 | */ |
| 18 | typedef struct { volatile int counter; } atomic_t; |
| 19 | typedef struct { volatile long counter; } atomic64_t; |
| 20 | |
| 21 | #define ATOMIC_INIT(i) ( (atomic_t) { (i) } ) |
| 22 | #define ATOMIC64_INIT(i) ( (atomic64_t) { (i) } ) |
| 23 | |
| 24 | #define atomic_read(v) ((v)->counter + 0) |
| 25 | #define atomic64_read(v) ((v)->counter + 0) |
| 26 | |
| 27 | #define atomic_set(v,i) ((v)->counter = (i)) |
| 28 | #define atomic64_set(v,i) ((v)->counter = (i)) |
| 29 | |
| 30 | /* |
| 31 | * To get proper branch prediction for the main line, we must branch |
| 32 | * forward to code at the end of this object's .text section, then |
| 33 | * branch back to restart the operation. |
| 34 | */ |
| 35 | |
| 36 | static __inline__ void atomic_add(int i, atomic_t * v) |
| 37 | { |
| 38 | unsigned long temp; |
| 39 | __asm__ __volatile__( |
| 40 | "1: ldl_l %0,%1\n" |
| 41 | " addl %0,%2,%0\n" |
| 42 | " stl_c %0,%1\n" |
| 43 | " beq %0,2f\n" |
| 44 | ".subsection 2\n" |
| 45 | "2: br 1b\n" |
| 46 | ".previous" |
| 47 | :"=&r" (temp), "=m" (v->counter) |
| 48 | :"Ir" (i), "m" (v->counter)); |
| 49 | } |
| 50 | |
| 51 | static __inline__ void atomic64_add(long i, atomic64_t * v) |
| 52 | { |
| 53 | unsigned long temp; |
| 54 | __asm__ __volatile__( |
| 55 | "1: ldq_l %0,%1\n" |
| 56 | " addq %0,%2,%0\n" |
| 57 | " stq_c %0,%1\n" |
| 58 | " beq %0,2f\n" |
| 59 | ".subsection 2\n" |
| 60 | "2: br 1b\n" |
| 61 | ".previous" |
| 62 | :"=&r" (temp), "=m" (v->counter) |
| 63 | :"Ir" (i), "m" (v->counter)); |
| 64 | } |
| 65 | |
| 66 | static __inline__ void atomic_sub(int i, atomic_t * v) |
| 67 | { |
| 68 | unsigned long temp; |
| 69 | __asm__ __volatile__( |
| 70 | "1: ldl_l %0,%1\n" |
| 71 | " subl %0,%2,%0\n" |
| 72 | " stl_c %0,%1\n" |
| 73 | " beq %0,2f\n" |
| 74 | ".subsection 2\n" |
| 75 | "2: br 1b\n" |
| 76 | ".previous" |
| 77 | :"=&r" (temp), "=m" (v->counter) |
| 78 | :"Ir" (i), "m" (v->counter)); |
| 79 | } |
| 80 | |
| 81 | static __inline__ void atomic64_sub(long i, atomic64_t * v) |
| 82 | { |
| 83 | unsigned long temp; |
| 84 | __asm__ __volatile__( |
| 85 | "1: ldq_l %0,%1\n" |
| 86 | " subq %0,%2,%0\n" |
| 87 | " stq_c %0,%1\n" |
| 88 | " beq %0,2f\n" |
| 89 | ".subsection 2\n" |
| 90 | "2: br 1b\n" |
| 91 | ".previous" |
| 92 | :"=&r" (temp), "=m" (v->counter) |
| 93 | :"Ir" (i), "m" (v->counter)); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /* |
| 98 | * Same as above, but return the result value |
| 99 | */ |
| 100 | static __inline__ long atomic_add_return(int i, atomic_t * v) |
| 101 | { |
| 102 | long temp, result; |
Ivan Kokshaysky | d475f3f | 2005-10-21 22:06:15 +0400 | [diff] [blame^] | 103 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | __asm__ __volatile__( |
| 105 | "1: ldl_l %0,%1\n" |
| 106 | " addl %0,%3,%2\n" |
| 107 | " addl %0,%3,%0\n" |
| 108 | " stl_c %0,%1\n" |
| 109 | " beq %0,2f\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | ".subsection 2\n" |
| 111 | "2: br 1b\n" |
| 112 | ".previous" |
| 113 | :"=&r" (temp), "=m" (v->counter), "=&r" (result) |
| 114 | :"Ir" (i), "m" (v->counter) : "memory"); |
Ivan Kokshaysky | d475f3f | 2005-10-21 22:06:15 +0400 | [diff] [blame^] | 115 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return result; |
| 117 | } |
| 118 | |
| 119 | #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) |
| 120 | |
| 121 | static __inline__ long atomic64_add_return(long i, atomic64_t * v) |
| 122 | { |
| 123 | long temp, result; |
Ivan Kokshaysky | d475f3f | 2005-10-21 22:06:15 +0400 | [diff] [blame^] | 124 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | __asm__ __volatile__( |
| 126 | "1: ldq_l %0,%1\n" |
| 127 | " addq %0,%3,%2\n" |
| 128 | " addq %0,%3,%0\n" |
| 129 | " stq_c %0,%1\n" |
| 130 | " beq %0,2f\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | ".subsection 2\n" |
| 132 | "2: br 1b\n" |
| 133 | ".previous" |
| 134 | :"=&r" (temp), "=m" (v->counter), "=&r" (result) |
| 135 | :"Ir" (i), "m" (v->counter) : "memory"); |
Ivan Kokshaysky | d475f3f | 2005-10-21 22:06:15 +0400 | [diff] [blame^] | 136 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return result; |
| 138 | } |
| 139 | |
| 140 | static __inline__ long atomic_sub_return(int i, atomic_t * v) |
| 141 | { |
| 142 | long temp, result; |
Ivan Kokshaysky | d475f3f | 2005-10-21 22:06:15 +0400 | [diff] [blame^] | 143 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | __asm__ __volatile__( |
| 145 | "1: ldl_l %0,%1\n" |
| 146 | " subl %0,%3,%2\n" |
| 147 | " subl %0,%3,%0\n" |
| 148 | " stl_c %0,%1\n" |
| 149 | " beq %0,2f\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | ".subsection 2\n" |
| 151 | "2: br 1b\n" |
| 152 | ".previous" |
| 153 | :"=&r" (temp), "=m" (v->counter), "=&r" (result) |
| 154 | :"Ir" (i), "m" (v->counter) : "memory"); |
Ivan Kokshaysky | d475f3f | 2005-10-21 22:06:15 +0400 | [diff] [blame^] | 155 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return result; |
| 157 | } |
| 158 | |
| 159 | static __inline__ long atomic64_sub_return(long i, atomic64_t * v) |
| 160 | { |
| 161 | long temp, result; |
Ivan Kokshaysky | d475f3f | 2005-10-21 22:06:15 +0400 | [diff] [blame^] | 162 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | __asm__ __volatile__( |
| 164 | "1: ldq_l %0,%1\n" |
| 165 | " subq %0,%3,%2\n" |
| 166 | " subq %0,%3,%0\n" |
| 167 | " stq_c %0,%1\n" |
| 168 | " beq %0,2f\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | ".subsection 2\n" |
| 170 | "2: br 1b\n" |
| 171 | ".previous" |
| 172 | :"=&r" (temp), "=m" (v->counter), "=&r" (result) |
| 173 | :"Ir" (i), "m" (v->counter) : "memory"); |
Ivan Kokshaysky | d475f3f | 2005-10-21 22:06:15 +0400 | [diff] [blame^] | 174 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return result; |
| 176 | } |
| 177 | |
| 178 | #define atomic_dec_return(v) atomic_sub_return(1,(v)) |
| 179 | #define atomic64_dec_return(v) atomic64_sub_return(1,(v)) |
| 180 | |
| 181 | #define atomic_inc_return(v) atomic_add_return(1,(v)) |
| 182 | #define atomic64_inc_return(v) atomic64_add_return(1,(v)) |
| 183 | |
| 184 | #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0) |
| 185 | #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0) |
| 186 | |
| 187 | #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0) |
| 188 | #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) |
| 189 | #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0) |
| 190 | |
| 191 | #define atomic_inc(v) atomic_add(1,(v)) |
| 192 | #define atomic64_inc(v) atomic64_add(1,(v)) |
| 193 | |
| 194 | #define atomic_dec(v) atomic_sub(1,(v)) |
| 195 | #define atomic64_dec(v) atomic64_sub(1,(v)) |
| 196 | |
| 197 | #define smp_mb__before_atomic_dec() smp_mb() |
| 198 | #define smp_mb__after_atomic_dec() smp_mb() |
| 199 | #define smp_mb__before_atomic_inc() smp_mb() |
| 200 | #define smp_mb__after_atomic_inc() smp_mb() |
| 201 | |
| 202 | #endif /* _ALPHA_ATOMIC_H */ |