- patches.fixes/patch-2.6.11-rc1: 2.6.11-rc1.
[linux-flexiantxendom0-3.2.10.git] / include / asm-ia64 / spinlock.h
1 #ifndef _ASM_IA64_SPINLOCK_H
2 #define _ASM_IA64_SPINLOCK_H
3
4 /*
5  * Copyright (C) 1998-2003 Hewlett-Packard Co
6  *      David Mosberger-Tang <davidm@hpl.hp.com>
7  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8  *
9  * This file is used for SMP configurations only.
10  */
11
12 #include <linux/compiler.h>
13 #include <linux/kernel.h>
14
15 #include <asm/atomic.h>
16 #include <asm/bitops.h>
17 #include <asm/intrinsics.h>
18 #include <asm/system.h>
19
20 typedef struct {
21         volatile unsigned int lock;
22 #ifdef CONFIG_PREEMPT
23         unsigned int break_lock;
24 #endif
25 } spinlock_t;
26
27 #define SPIN_LOCK_UNLOCKED                      (spinlock_t) { 0 }
28 #define spin_lock_init(x)                       ((x)->lock = 0)
29
30 #ifdef ASM_SUPPORTED
31 /*
32  * Try to get the lock.  If we fail to get the lock, make a non-standard call to
33  * ia64_spinlock_contention().  We do not use a normal call because that would force all
34  * callers of spin_lock() to be non-leaf routines.  Instead, ia64_spinlock_contention() is
35  * carefully coded to touch only those registers that spin_lock() marks "clobbered".
36  */
37
38 #define IA64_SPINLOCK_CLOBBERS "ar.ccv", "ar.pfs", "p14", "p15", "r27", "r28", "r29", "r30", "b6", "memory"
39
40 static inline void
41 _raw_spin_lock_flags (spinlock_t *lock, unsigned long flags)
42 {
43         register volatile unsigned int *ptr asm ("r31") = &lock->lock;
44
45 #if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 3)
46 # ifdef CONFIG_ITANIUM
47         /* don't use brl on Itanium... */
48         asm volatile ("{\n\t"
49                       "  mov ar.ccv = r0\n\t"
50                       "  mov r28 = ip\n\t"
51                       "  mov r30 = 1;;\n\t"
52                       "}\n\t"
53                       "cmpxchg4.acq r30 = [%1], r30, ar.ccv\n\t"
54                       "movl r29 = ia64_spinlock_contention_pre3_4;;\n\t"
55                       "cmp4.ne p14, p0 = r30, r0\n\t"
56                       "mov b6 = r29;;\n\t"
57                       "mov r27=%2\n\t"
58                       "(p14) br.cond.spnt.many b6"
59                       : "=r"(ptr) : "r"(ptr), "r" (flags) : IA64_SPINLOCK_CLOBBERS);
60 # else
61         asm volatile ("{\n\t"
62                       "  mov ar.ccv = r0\n\t"
63                       "  mov r28 = ip\n\t"
64                       "  mov r30 = 1;;\n\t"
65                       "}\n\t"
66                       "cmpxchg4.acq r30 = [%1], r30, ar.ccv;;\n\t"
67                       "cmp4.ne p14, p0 = r30, r0\n\t"
68                       "mov r27=%2\n\t"
69                       "(p14) brl.cond.spnt.many ia64_spinlock_contention_pre3_4;;"
70                       : "=r"(ptr) : "r"(ptr), "r" (flags) : IA64_SPINLOCK_CLOBBERS);
71 # endif /* CONFIG_MCKINLEY */
72 #else
73 # ifdef CONFIG_ITANIUM
74         /* don't use brl on Itanium... */
75         /* mis-declare, so we get the entry-point, not it's function descriptor: */
76         asm volatile ("mov r30 = 1\n\t"
77                       "mov r27=%2\n\t"
78                       "mov ar.ccv = r0;;\n\t"
79                       "cmpxchg4.acq r30 = [%0], r30, ar.ccv\n\t"
80                       "movl r29 = ia64_spinlock_contention;;\n\t"
81                       "cmp4.ne p14, p0 = r30, r0\n\t"
82                       "mov b6 = r29;;\n\t"
83                       "(p14) br.call.spnt.many b6 = b6"
84                       : "=r"(ptr) : "r"(ptr), "r" (flags) : IA64_SPINLOCK_CLOBBERS);
85 # else
86         asm volatile ("mov r30 = 1\n\t"
87                       "mov r27=%2\n\t"
88                       "mov ar.ccv = r0;;\n\t"
89                       "cmpxchg4.acq r30 = [%0], r30, ar.ccv;;\n\t"
90                       "cmp4.ne p14, p0 = r30, r0\n\t"
91                       "(p14) brl.call.spnt.many b6=ia64_spinlock_contention;;"
92                       : "=r"(ptr) : "r"(ptr), "r" (flags) : IA64_SPINLOCK_CLOBBERS);
93 # endif /* CONFIG_MCKINLEY */
94 #endif
95 }
96 #define _raw_spin_lock(lock) _raw_spin_lock_flags(lock, 0)
97 #else /* !ASM_SUPPORTED */
98 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
99 # define _raw_spin_lock(x)                                                              \
100 do {                                                                                    \
101         __u32 *ia64_spinlock_ptr = (__u32 *) (x);                                       \
102         __u64 ia64_spinlock_val;                                                        \
103         ia64_spinlock_val = ia64_cmpxchg4_acq(ia64_spinlock_ptr, 1, 0);                 \
104         if (unlikely(ia64_spinlock_val)) {                                              \
105                 do {                                                                    \
106                         while (*ia64_spinlock_ptr)                                      \
107                                 ia64_barrier();                                         \
108                         ia64_spinlock_val = ia64_cmpxchg4_acq(ia64_spinlock_ptr, 1, 0); \
109                 } while (ia64_spinlock_val);                                            \
110         }                                                                               \
111 } while (0)
112 #endif /* !ASM_SUPPORTED */
113
114 #define spin_is_locked(x)       ((x)->lock != 0)
115 #define _raw_spin_unlock(x)     do { barrier(); ((spinlock_t *) x)->lock = 0; } while (0)
116 #define _raw_spin_trylock(x)    (cmpxchg_acq(&(x)->lock, 0, 1) == 0)
117 #define spin_unlock_wait(x)     do { barrier(); } while ((x)->lock)
118
119 typedef struct {
120         volatile unsigned int read_counter      : 31;
121         volatile unsigned int write_lock        :  1;
122 #ifdef CONFIG_PREEMPT
123         unsigned int break_lock;
124 #endif
125 } rwlock_t;
126 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 }
127
128 #define rwlock_init(x)          do { *(x) = RW_LOCK_UNLOCKED; } while(0)
129 #define rwlock_is_locked(x)     (*(volatile int *) (x) != 0)
130
131 #define _raw_read_lock(rw)                                                              \
132 do {                                                                                    \
133         rwlock_t *__read_lock_ptr = (rw);                                               \
134                                                                                         \
135         while (unlikely(ia64_fetchadd(1, (int *) __read_lock_ptr, acq) < 0)) {          \
136                 ia64_fetchadd(-1, (int *) __read_lock_ptr, rel);                        \
137                 while (*(volatile int *)__read_lock_ptr < 0)                            \
138                         cpu_relax();                                                    \
139         }                                                                               \
140 } while (0)
141
142 #define _raw_read_unlock(rw)                                    \
143 do {                                                            \
144         rwlock_t *__read_lock_ptr = (rw);                       \
145         ia64_fetchadd(-1, (int *) __read_lock_ptr, rel);        \
146 } while (0)
147
148 #ifdef ASM_SUPPORTED
149 #define _raw_write_lock(rw)                                                     \
150 do {                                                                            \
151         __asm__ __volatile__ (                                                  \
152                 "mov ar.ccv = r0\n"                                             \
153                 "dep r29 = -1, r0, 31, 1;;\n"                                   \
154                 "1:\n"                                                          \
155                 "ld4 r2 = [%0];;\n"                                             \
156                 "cmp4.eq p0,p7 = r0,r2\n"                                       \
157                 "(p7) br.cond.spnt.few 1b \n"                                   \
158                 "cmpxchg4.acq r2 = [%0], r29, ar.ccv;;\n"                       \
159                 "cmp4.eq p0,p7 = r0, r2\n"                                      \
160                 "(p7) br.cond.spnt.few 1b;;\n"                                  \
161                 :: "r"(rw) : "ar.ccv", "p7", "r2", "r29", "memory");            \
162 } while(0)
163
164 #define _raw_write_trylock(rw)                                                  \
165 ({                                                                              \
166         register long result;                                                   \
167                                                                                 \
168         __asm__ __volatile__ (                                                  \
169                 "mov ar.ccv = r0\n"                                             \
170                 "dep r29 = -1, r0, 31, 1;;\n"                                   \
171                 "cmpxchg4.acq %0 = [%1], r29, ar.ccv\n"                         \
172                 : "=r"(result) : "r"(rw) : "ar.ccv", "r29", "memory");          \
173         (result == 0);                                                          \
174 })
175
176 #else /* !ASM_SUPPORTED */
177
178 #define _raw_write_lock(l)                                                              \
179 ({                                                                                      \
180         __u64 ia64_val, ia64_set_val = ia64_dep_mi(-1, 0, 31, 1);                       \
181         __u32 *ia64_write_lock_ptr = (__u32 *) (l);                                     \
182         do {                                                                            \
183                 while (*ia64_write_lock_ptr)                                            \
184                         ia64_barrier();                                                 \
185                 ia64_val = ia64_cmpxchg4_acq(ia64_write_lock_ptr, ia64_set_val, 0);     \
186         } while (ia64_val);                                                             \
187 })
188
189 #define _raw_write_trylock(rw)                                          \
190 ({                                                                      \
191         __u64 ia64_val;                                                 \
192         __u64 ia64_set_val = ia64_dep_mi(-1, 0, 31,1);                  \
193         ia64_val = ia64_cmpxchg4_acq((__u32 *)(rw), ia64_set_val, 0);   \
194         (ia64_val == 0);                                                \
195 })
196
197 #endif /* !ASM_SUPPORTED */
198
199 #define _raw_read_trylock(lock) generic_raw_read_trylock(lock)
200
201 #define _raw_write_unlock(x)                                                            \
202 ({                                                                                      \
203         smp_mb__before_clear_bit();     /* need barrier before releasing lock... */     \
204         clear_bit(31, (x));                                                             \
205 })
206
207 #endif /*  _ASM_IA64_SPINLOCK_H */