aboutsummaryrefslogtreecommitdiffstats
path: root/include/asm-sparc/atomic.h
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /include/asm-sparc/atomic.h
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'include/asm-sparc/atomic.h')
-rw-r--r--include/asm-sparc/atomic.h158
1 files changed, 158 insertions, 0 deletions
diff --git a/include/asm-sparc/atomic.h b/include/asm-sparc/atomic.h
new file mode 100644
index 000000000000..37f6ab601c3d
--- /dev/null
+++ b/include/asm-sparc/atomic.h
@@ -0,0 +1,158 @@
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
15typedef struct { volatile int counter; } atomic_t;
16
17#ifdef __KERNEL__
18
19#define ATOMIC_INIT(i) { (i) }
20
21extern int __atomic_add_return(int, atomic_t *);
22extern 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_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
37
38/*
39 * atomic_inc_and_test - increment and test
40 * @v: pointer of type atomic_t
41 *
42 * Atomically increments @v by 1
43 * and returns true if the result is zero, or false for all
44 * other cases.
45 */
46#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
47
48#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
49#define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
50
51/* This is the old 24-bit implementation. It's still used internally
52 * by some sparc-specific code, notably the semaphore implementation.
53 */
54typedef struct { volatile int counter; } atomic24_t;
55
56#ifndef CONFIG_SMP
57
58#define ATOMIC24_INIT(i) { (i) }
59#define atomic24_read(v) ((v)->counter)
60#define atomic24_set(v, i) (((v)->counter) = i)
61
62#else
63/* We do the bulk of the actual work out of line in two common
64 * routines in assembler, see arch/sparc/lib/atomic.S for the
65 * "fun" details.
66 *
67 * For SMP the trick is you embed the spin lock byte within
68 * the word, use the low byte so signedness is easily retained
69 * via a quick arithmetic shift. It looks like this:
70 *
71 * ----------------------------------------
72 * | signed 24-bit counter value | lock | atomic_t
73 * ----------------------------------------
74 * 31 8 7 0
75 */
76
77#define ATOMIC24_INIT(i) { ((i) << 8) }
78
79static inline int atomic24_read(const atomic24_t *v)
80{
81 int ret = v->counter;
82
83 while(ret & 0xff)
84 ret = v->counter;
85
86 return ret >> 8;
87}
88
89#define atomic24_set(v, i) (((v)->counter) = ((i) << 8))
90#endif
91
92static inline int __atomic24_add(int i, atomic24_t *v)
93{
94 register volatile int *ptr asm("g1");
95 register int increment asm("g2");
96 register int tmp1 asm("g3");
97 register int tmp2 asm("g4");
98 register int tmp3 asm("g7");
99
100 ptr = &v->counter;
101 increment = i;
102
103 __asm__ __volatile__(
104 "mov %%o7, %%g4\n\t"
105 "call ___atomic24_add\n\t"
106 " add %%o7, 8, %%o7\n"
107 : "=&r" (increment), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
108 : "0" (increment), "r" (ptr)
109 : "memory", "cc");
110
111 return increment;
112}
113
114static inline int __atomic24_sub(int i, atomic24_t *v)
115{
116 register volatile int *ptr asm("g1");
117 register int increment asm("g2");
118 register int tmp1 asm("g3");
119 register int tmp2 asm("g4");
120 register int tmp3 asm("g7");
121
122 ptr = &v->counter;
123 increment = i;
124
125 __asm__ __volatile__(
126 "mov %%o7, %%g4\n\t"
127 "call ___atomic24_sub\n\t"
128 " add %%o7, 8, %%o7\n"
129 : "=&r" (increment), "=r" (tmp1), "=r" (tmp2), "=r" (tmp3)
130 : "0" (increment), "r" (ptr)
131 : "memory", "cc");
132
133 return increment;
134}
135
136#define atomic24_add(i, v) ((void)__atomic24_add((i), (v)))
137#define atomic24_sub(i, v) ((void)__atomic24_sub((i), (v)))
138
139#define atomic24_dec_return(v) __atomic24_sub(1, (v))
140#define atomic24_inc_return(v) __atomic24_add(1, (v))
141
142#define atomic24_sub_and_test(i, v) (__atomic24_sub((i), (v)) == 0)
143#define atomic24_dec_and_test(v) (__atomic24_sub(1, (v)) == 0)
144
145#define atomic24_inc(v) ((void)__atomic24_add(1, (v)))
146#define atomic24_dec(v) ((void)__atomic24_sub(1, (v)))
147
148#define atomic24_add_negative(i, v) (__atomic24_add((i), (v)) < 0)
149
150/* Atomic operations are already serializing */
151#define smp_mb__before_atomic_dec() barrier()
152#define smp_mb__after_atomic_dec() barrier()
153#define smp_mb__before_atomic_inc() barrier()
154#define smp_mb__after_atomic_inc() barrier()
155
156#endif /* !(__KERNEL__) */
157
158#endif /* !(__ARCH_SPARC_ATOMIC__) */