patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-s390 / spinlock.h
1 /*
2  *  include/asm-s390/spinlock.h
3  *
4  *  S390 version
5  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7  *
8  *  Derived from "include/asm-i386/spinlock.h"
9  */
10
11 #ifndef __ASM_SPINLOCK_H
12 #define __ASM_SPINLOCK_H
13
14 #ifdef __s390x__
15 /*
16  * Grmph, take care of %&#! user space programs that include
17  * asm/spinlock.h. The diagnose is only available in kernel
18  * context.
19  */
20 #ifdef __KERNEL__
21 #include <asm/lowcore.h>
22 #define __DIAG44_INSN "ex"
23 #define __DIAG44_OPERAND __LC_DIAG44_OPCODE
24 #else
25 #define __DIAG44_INSN "#"
26 #define __DIAG44_OPERAND 0
27 #endif
28 #endif /* __s390x__ */
29
30 /*
31  * Simple spin lock operations.  There are two variants, one clears IRQ's
32  * on the local processor, one does not.
33  *
34  * We make no fairness assumptions. They have a cost.
35  */
36
37 typedef struct {
38         volatile unsigned int lock;
39 } __attribute__ ((aligned (4))) spinlock_t;
40
41 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
42 #define spin_lock_init(lp) do { (lp)->lock = 0; } while(0)
43 #define spin_unlock_wait(lp)    do { barrier(); } while(((volatile spinlock_t *)(lp))->lock)
44 #define spin_is_locked(x) ((x)->lock != 0)
45 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
46
47 extern inline void _raw_spin_lock(spinlock_t *lp)
48 {
49 #ifndef __s390x__
50         unsigned int reg1, reg2;
51         __asm__ __volatile__("    bras  %0,1f\n"
52                            "0:  diag  0,0,68\n"
53                            "1:  slr   %1,%1\n"
54                            "    cs    %1,%0,0(%3)\n"
55                            "    jl    0b\n"
56                            : "=&d" (reg1), "=&d" (reg2), "=m" (lp->lock)
57                            : "a" (&lp->lock), "m" (lp->lock)
58                            : "cc", "memory" );
59 #else /* __s390x__ */
60         unsigned long reg1, reg2;
61         __asm__ __volatile__("    bras  %1,1f\n"
62                            "0:  " __DIAG44_INSN " 0,%4\n"
63                            "1:  slr   %0,%0\n"
64                            "    cs    %0,%1,0(%3)\n"
65                            "    jl    0b\n"
66                            : "=&d" (reg1), "=&d" (reg2), "=m" (lp->lock)
67                            : "a" (&lp->lock), "i" (__DIAG44_OPERAND),
68                              "m" (lp->lock) : "cc", "memory" );
69 #endif /* __s390x__ */
70 }
71
72 extern inline int _raw_spin_trylock(spinlock_t *lp)
73 {
74         unsigned long reg;
75         unsigned int result;
76
77         __asm__ __volatile__("    basr  %1,0\n"
78                            "0:  cs    %0,%1,0(%3)"
79                            : "=d" (result), "=&d" (reg), "=m" (lp->lock)
80                            : "a" (&lp->lock), "m" (lp->lock), "0" (0)
81                            : "cc", "memory" );
82         return !result;
83 }
84
85 extern inline void _raw_spin_unlock(spinlock_t *lp)
86 {
87         unsigned int old;
88
89         __asm__ __volatile__("cs %0,%3,0(%4)"
90                            : "=d" (old), "=m" (lp->lock)
91                            : "0" (lp->lock), "d" (0), "a" (lp)
92                            : "cc", "memory" );
93 }
94                 
95 /*
96  * Read-write spinlocks, allowing multiple readers
97  * but only one writer.
98  *
99  * NOTE! it is quite common to have readers in interrupts
100  * but no interrupt writers. For those circumstances we
101  * can "mix" irq-safe locks - any writer needs to get a
102  * irq-safe write-lock, but readers can get non-irqsafe
103  * read-locks.
104  */
105 typedef struct {
106         volatile unsigned long lock;
107         volatile unsigned long owner_pc;
108 } rwlock_t;
109
110 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 }
111
112 #define rwlock_init(x)  do { *(x) = RW_LOCK_UNLOCKED; } while(0)
113
114 #define rwlock_is_locked(x) ((x)->lock != 0)
115
116 #ifndef __s390x__
117 #define _raw_read_lock(rw)   \
118         asm volatile("   l     2,0(%1)\n"   \
119                      "   j     1f\n"     \
120                      "0: diag  0,0,68\n" \
121                      "1: la    2,0(2)\n"     /* clear high (=write) bit */ \
122                      "   la    3,1(2)\n"     /* one more reader */ \
123                      "   cs    2,3,0(%1)\n"  /* try to write new value */ \
124                      "   jl    0b"       \
125                      : "=m" ((rw)->lock) : "a" (&(rw)->lock), \
126                        "m" ((rw)->lock) : "2", "3", "cc", "memory" )
127 #else /* __s390x__ */
128 #define _raw_read_lock(rw)   \
129         asm volatile("   lg    2,0(%1)\n"   \
130                      "   j     1f\n"     \
131                      "0: " __DIAG44_INSN " 0,%2\n" \
132                      "1: nihh  2,0x7fff\n" /* clear high (=write) bit */ \
133                      "   la    3,1(2)\n"   /* one more reader */  \
134                      "   csg   2,3,0(%1)\n" /* try to write new value */ \
135                      "   jl    0b"       \
136                      : "=m" ((rw)->lock) \
137                      : "a" (&(rw)->lock), "i" (__DIAG44_OPERAND), \
138                        "m" ((rw)->lock) : "2", "3", "cc", "memory" )
139 #endif /* __s390x__ */
140
141 #ifndef __s390x__
142 #define _raw_read_unlock(rw) \
143         asm volatile("   l     2,0(%1)\n"   \
144                      "   j     1f\n"     \
145                      "0: diag  0,0,68\n" \
146                      "1: lr    3,2\n"    \
147                      "   ahi   3,-1\n"    /* one less reader */ \
148                      "   cs    2,3,0(%1)\n" \
149                      "   jl    0b"       \
150                      : "=m" ((rw)->lock) : "a" (&(rw)->lock), \
151                        "m" ((rw)->lock) : "2", "3", "cc", "memory" )
152 #else /* __s390x__ */
153 #define _raw_read_unlock(rw) \
154         asm volatile("   lg    2,0(%1)\n"   \
155                      "   j     1f\n"     \
156                      "0: " __DIAG44_INSN " 0,%2\n" \
157                      "1: lgr   3,2\n"    \
158                      "   bctgr 3,0\n"    /* one less reader */ \
159                      "   csg   2,3,0(%1)\n" \
160                      "   jl    0b"       \
161                      : "=m" ((rw)->lock) \
162                      : "a" (&(rw)->lock), "i" (__DIAG44_OPERAND), \
163                        "m" ((rw)->lock) : "2", "3", "cc", "memory" )
164 #endif /* __s390x__ */
165
166 #ifndef __s390x__
167 #define _raw_write_lock(rw) \
168         asm volatile("   lhi   3,1\n"    \
169                      "   sll   3,31\n"    /* new lock value = 0x80000000 */ \
170                      "   j     1f\n"     \
171                      "0: diag  0,0,68\n" \
172                      "1: slr   2,2\n"     /* old lock value must be 0 */ \
173                      "   cs    2,3,0(%1)\n" \
174                      "   jl    0b"       \
175                      : "=m" ((rw)->lock) : "a" (&(rw)->lock), \
176                        "m" ((rw)->lock) : "2", "3", "cc", "memory" )
177 #else /* __s390x__ */
178 #define _raw_write_lock(rw) \
179         asm volatile("   llihh 3,0x8000\n" /* new lock value = 0x80...0 */ \
180                      "   j     1f\n"       \
181                      "0: " __DIAG44_INSN " 0,%2\n"   \
182                      "1: slgr  2,2\n"      /* old lock value must be 0 */ \
183                      "   csg   2,3,0(%1)\n" \
184                      "   jl    0b"         \
185                      : "=m" ((rw)->lock) \
186                      : "a" (&(rw)->lock), "i" (__DIAG44_OPERAND), \
187                        "m" ((rw)->lock) : "2", "3", "cc", "memory" )
188 #endif /* __s390x__ */
189
190 #ifndef __s390x__
191 #define _raw_write_unlock(rw) \
192         asm volatile("   slr   3,3\n"     /* new lock value = 0 */ \
193                      "   j     1f\n"     \
194                      "0: diag  0,0,68\n" \
195                      "1: lhi   2,1\n"    \
196                      "   sll   2,31\n"    /* old lock value must be 0x80000000 */ \
197                      "   cs    2,3,0(%1)\n" \
198                      "   jl    0b"       \
199                      : "=m" ((rw)->lock) : "a" (&(rw)->lock), \
200                        "m" ((rw)->lock) : "2", "3", "cc", "memory" )
201 #else /* __s390x__ */
202 #define _raw_write_unlock(rw) \
203         asm volatile("   slgr  3,3\n"      /* new lock value = 0 */ \
204                      "   j     1f\n"       \
205                      "0: " __DIAG44_INSN " 0,%2\n"   \
206                      "1: llihh 2,0x8000\n" /* old lock value must be 0x8..0 */\
207                      "   csg   2,3,0(%1)\n"   \
208                      "   jl    0b"         \
209                      : "=m" ((rw)->lock) \
210                      : "a" (&(rw)->lock), "i" (__DIAG44_OPERAND), \
211                        "m" ((rw)->lock) : "2", "3", "cc", "memory" )
212 #endif /* __s390x__ */
213
214 extern inline int _raw_write_trylock(rwlock_t *rw)
215 {
216         unsigned long result, reg;
217         
218         __asm__ __volatile__(
219 #ifndef __s390x__
220                              "   lhi  %1,1\n"
221                              "   sll  %1,31\n"
222                              "   cs   %0,%1,0(%3)"
223 #else /* __s390x__ */
224                              "   llihh %1,0x8000\n"
225                              "0: csg %0,%1,0(%3)\n"
226 #endif /* __s390x__ */
227                              : "=d" (result), "=&d" (reg), "=m" (rw->lock)
228                              : "a" (&rw->lock), "m" (rw->lock), "0" (0UL)
229                              : "cc", "memory" );
230         return result == 0;
231 }
232
233 #endif /* __ASM_SPINLOCK_H */