ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-sparc / atomic.h
1 /* atomic.h: These still suck, but the I-cache hit rate is higher.
2  *
3  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
4  * Copyright (C) 2000 Anton Blanchard (anton@linuxcare.com.au)
5  *
6  * Additions by Keith M Wesolowski (wesolows@foobazco.org) based
7  * on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>.
8  */
9
10 #ifndef __ARCH_SPARC_ATOMIC__
11 #define __ARCH_SPARC_ATOMIC__
12
13 #include <linux/config.h>
14
15 typedef struct { volatile int counter; } atomic_t;
16
17 #ifdef __KERNEL__
18
19 #define ATOMIC_INIT(i)  { (i) }
20
21 extern int __atomic_add_return(int, atomic_t *);
22 extern void atomic_set(atomic_t *, int);
23
24 #define atomic_read(v)          ((v)->counter)
25
26 #define atomic_add(i, v)        ((void)__atomic_add_return( (int)(i), (v)))
27 #define atomic_sub(i, v)        ((void)__atomic_add_return(-(int)(i), (v)))
28 #define atomic_inc(v)           ((void)__atomic_add_return(        1, (v)))
29 #define atomic_dec(v)           ((void)__atomic_add_return(       -1, (v)))
30
31 #define atomic_add_return(i, v) (__atomic_add_return( (int)(i), (v)))
32 #define atomic_sub_return(i, v) (__atomic_add_return(-(int)(i), (v)))
33 #define atomic_inc_return(v)    (__atomic_add_return(        1, (v)))
34 #define atomic_dec_return(v)    (__atomic_add_return(       -1, (v)))
35
36 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
37
38 /* This is the old 24-bit implementation.  It's still used internally
39  * by some sparc-specific code, notably the semaphore implementation.
40  */
41 typedef struct { volatile int counter; } atomic24_t;
42
43 #ifndef CONFIG_SMP
44
45 #define ATOMIC24_INIT(i)  { (i) }
46 #define atomic24_read(v)          ((v)->counter)
47 #define atomic24_set(v, i)        (((v)->counter) = i)
48
49 #else
50 /* We do the bulk of the actual work out of line in two common
51  * routines in assembler, see arch/sparc/lib/atomic.S for the
52  * "fun" details.
53  *
54  * For SMP the trick is you embed the spin lock byte within
55  * the word, use the low byte so signedness is easily retained
56  * via a quick arithmetic shift.  It looks like this:
57  *
58  *      ----------------------------------------
59  *      | signed 24-bit counter value |  lock  |  atomic_t
60  *      ----------------------------------------
61  *       31                          8 7      0
62  */
63
64 #define ATOMIC24_INIT(i)        { ((i) << 8) }
65
66 static inline int atomic24_read(const atomic24_t *v)
67 {
68         int ret = v->counter;
69
70         while(ret & 0xff)
71                 ret = v->counter;
72
73         return ret >> 8;
74 }
75
76 #define atomic24_set(v, i)      (((v)->counter) = ((i) << 8))
77 #endif
78
79 static inline int __atomic24_add(int i, atomic24_t *v)
80 {
81         register volatile int *ptr asm("g1");
82         register int increment asm("g2");
83         register int tmp1 asm("g3");
84         register int tmp2 asm("g4");
85         register int tmp3 asm("g7");
86
87         ptr = &v->counter;
88         increment = i;
89
90         __asm__ __volatile__(
91         "mov    %%o7, %%g4\n\t"
92         "call   ___atomic24_add\n\t"
93         " add   %%o7, 8, %%o7\n"
94         : "=&r" (increment), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
95         : "0" (increment), "r" (ptr)
96         : "memory", "cc");
97
98         return increment;
99 }
100
101 static inline int __atomic24_sub(int i, atomic24_t *v)
102 {
103         register volatile int *ptr asm("g1");
104         register int increment asm("g2");
105         register int tmp1 asm("g3");
106         register int tmp2 asm("g4");
107         register int tmp3 asm("g7");
108
109         ptr = &v->counter;
110         increment = i;
111
112         __asm__ __volatile__(
113         "mov    %%o7, %%g4\n\t"
114         "call   ___atomic24_sub\n\t"
115         " add   %%o7, 8, %%o7\n"
116         : "=&r" (increment), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
117         : "0" (increment), "r" (ptr)
118         : "memory", "cc");
119
120         return increment;
121 }
122
123 #define atomic24_add(i, v) ((void)__atomic24_add((i), (v)))
124 #define atomic24_sub(i, v) ((void)__atomic24_sub((i), (v)))
125
126 #define atomic24_dec_return(v) __atomic24_sub(1, (v))
127 #define atomic24_inc_return(v) __atomic24_add(1, (v))
128
129 #define atomic24_sub_and_test(i, v) (__atomic24_sub((i), (v)) == 0)
130 #define atomic24_dec_and_test(v) (__atomic24_sub(1, (v)) == 0)
131
132 #define atomic24_inc(v) ((void)__atomic24_add(1, (v)))
133 #define atomic24_dec(v) ((void)__atomic24_sub(1, (v)))
134
135 #define atomic24_add_negative(i, v) (__atomic24_add((i), (v)) < 0)
136
137 /* Atomic operations are already serializing */
138 #define smp_mb__before_atomic_dec()     barrier()
139 #define smp_mb__after_atomic_dec()      barrier()
140 #define smp_mb__before_atomic_inc()     barrier()
141 #define smp_mb__after_atomic_inc()      barrier()
142
143 #endif /* !(__KERNEL__) */
144
145 #endif /* !(__ARCH_SPARC_ATOMIC__) */