Pull pnpacpi into acpica branch
[powerpc.git] / include / asm-s390 / atomic.h
1 #ifndef __ARCH_S390_ATOMIC__
2 #define __ARCH_S390_ATOMIC__
3
4 /*
5  *  include/asm-s390/atomic.h
6  *
7  *  S390 version
8  *    Copyright (C) 1999-2005 IBM Deutschland Entwicklung GmbH, IBM Corporation
9  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
10  *               Denis Joseph Barrow,
11  *               Arnd Bergmann (arndb@de.ibm.com)
12  *
13  *  Derived from "include/asm-i386/bitops.h"
14  *    Copyright (C) 1992, Linus Torvalds
15  *
16  */
17
18 /*
19  * Atomic operations that C can't guarantee us.  Useful for
20  * resource counting etc..
21  * S390 uses 'Compare And Swap' for atomicity in SMP enviroment
22  */
23
24 typedef struct {
25         volatile int counter;
26 } __attribute__ ((aligned (4))) atomic_t;
27 #define ATOMIC_INIT(i)  { (i) }
28
29 #ifdef __KERNEL__
30
31 #define __CS_LOOP(ptr, op_val, op_string) ({                            \
32         typeof(ptr->counter) old_val, new_val;                          \
33         __asm__ __volatile__("   l     %0,0(%3)\n"                      \
34                              "0: lr    %1,%0\n"                         \
35                              op_string "  %1,%4\n"                      \
36                              "   cs    %0,%1,0(%3)\n"                   \
37                              "   jl    0b"                              \
38                              : "=&d" (old_val), "=&d" (new_val),        \
39                                "=m" (((atomic_t *)(ptr))->counter)      \
40                              : "a" (ptr), "d" (op_val),                 \
41                                "m" (((atomic_t *)(ptr))->counter)       \
42                              : "cc", "memory" );                        \
43         new_val;                                                        \
44 })
45 #define atomic_read(v)          ((v)->counter)
46 #define atomic_set(v,i)         (((v)->counter) = (i))
47
48 static __inline__ int atomic_add_return(int i, atomic_t * v)
49 {
50         return __CS_LOOP(v, i, "ar");
51 }
52 #define atomic_add(_i, _v)              atomic_add_return(_i, _v)
53 #define atomic_add_negative(_i, _v)     (atomic_add_return(_i, _v) < 0)
54 #define atomic_inc(_v)                  atomic_add_return(1, _v)
55 #define atomic_inc_return(_v)           atomic_add_return(1, _v)
56 #define atomic_inc_and_test(_v)         (atomic_add_return(1, _v) == 0)
57
58 static __inline__ int atomic_sub_return(int i, atomic_t * v)
59 {
60         return __CS_LOOP(v, i, "sr");
61 }
62 #define atomic_sub(_i, _v)              atomic_sub_return(_i, _v)
63 #define atomic_sub_and_test(_i, _v)     (atomic_sub_return(_i, _v) == 0)
64 #define atomic_dec(_v)                  atomic_sub_return(1, _v)
65 #define atomic_dec_return(_v)           atomic_sub_return(1, _v)
66 #define atomic_dec_and_test(_v)         (atomic_sub_return(1, _v) == 0)
67
68 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
69 {
70                __CS_LOOP(v, ~mask, "nr");
71 }
72
73 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
74 {
75                __CS_LOOP(v, mask, "or");
76 }
77
78 static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
79 {
80         __asm__ __volatile__("  cs   %0,%3,0(%2)\n"
81                              : "+d" (old), "=m" (v->counter)
82                              : "a" (v), "d" (new), "m" (v->counter)
83                              : "cc", "memory" );
84         return old;
85 }
86
87 static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
88 {
89         int c, old;
90
91         c = atomic_read(v);
92         while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
93                 c = old;
94         return c != u;
95 }
96
97 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
98
99 #undef __CS_LOOP
100
101 #ifdef __s390x__
102 typedef struct {
103         volatile long long counter;
104 } __attribute__ ((aligned (8))) atomic64_t;
105 #define ATOMIC64_INIT(i)  { (i) }
106
107 #define __CSG_LOOP(ptr, op_val, op_string) ({                           \
108         typeof(ptr->counter) old_val, new_val;                          \
109         __asm__ __volatile__("   lg    %0,0(%3)\n"                      \
110                              "0: lgr   %1,%0\n"                         \
111                              op_string "  %1,%4\n"                      \
112                              "   csg   %0,%1,0(%3)\n"                   \
113                              "   jl    0b"                              \
114                              : "=&d" (old_val), "=&d" (new_val),        \
115                                "=m" (((atomic_t *)(ptr))->counter)      \
116                              : "a" (ptr), "d" (op_val),                 \
117                                "m" (((atomic_t *)(ptr))->counter)       \
118                              : "cc", "memory" );                        \
119         new_val;                                                        \
120 })
121 #define atomic64_read(v)          ((v)->counter)
122 #define atomic64_set(v,i)         (((v)->counter) = (i))
123
124 static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
125 {
126         return __CSG_LOOP(v, i, "agr");
127 }
128 #define atomic64_add(_i, _v)            atomic64_add_return(_i, _v)
129 #define atomic64_add_negative(_i, _v)   (atomic64_add_return(_i, _v) < 0)
130 #define atomic64_inc(_v)                atomic64_add_return(1, _v)
131 #define atomic64_inc_return(_v)         atomic64_add_return(1, _v)
132 #define atomic64_inc_and_test(_v)       (atomic64_add_return(1, _v) == 0)
133
134 static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
135 {
136         return __CSG_LOOP(v, i, "sgr");
137 }
138 #define atomic64_sub(_i, _v)            atomic64_sub_return(_i, _v)
139 #define atomic64_sub_and_test(_i, _v)   (atomic64_sub_return(_i, _v) == 0)
140 #define atomic64_dec(_v)                atomic64_sub_return(1, _v)
141 #define atomic64_dec_return(_v)         atomic64_sub_return(1, _v)
142 #define atomic64_dec_and_test(_v)       (atomic64_sub_return(1, _v) == 0)
143
144 static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
145 {
146                __CSG_LOOP(v, ~mask, "ngr");
147 }
148
149 static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
150 {
151                __CSG_LOOP(v, mask, "ogr");
152 }
153
154 static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
155                                              long long old, long long new)
156 {
157         __asm__ __volatile__("  csg  %0,%3,0(%2)\n"
158                              : "+d" (old), "=m" (v->counter)
159                              : "a" (v), "d" (new), "m" (v->counter)
160                              : "cc", "memory" );
161         return old;
162 }
163
164 static __inline__ int atomic64_add_unless(atomic64_t *v,
165                                           long long a, long long u)
166 {
167         long long c, old;
168
169         c = atomic64_read(v);
170         while (c != u && (old = atomic64_cmpxchg(v, c, c + a)) != c)
171                 c = old;
172         return c != u;
173 }
174
175 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
176
177 #undef __CSG_LOOP
178 #endif
179
180 #define smp_mb__before_atomic_dec()     smp_mb()
181 #define smp_mb__after_atomic_dec()      smp_mb()
182 #define smp_mb__before_atomic_inc()     smp_mb()
183 #define smp_mb__after_atomic_inc()      smp_mb()
184
185 #include <asm-generic/atomic.h>
186 #endif /* __KERNEL__ */
187 #endif /* __ARCH_S390_ATOMIC__  */