]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/mn10300/include/asm/atomic.h
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[karo-tx-linux.git] / arch / mn10300 / include / asm / atomic.h
1 /* MN10300 Atomic counter operations
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 #ifndef _ASM_ATOMIC_H
12 #define _ASM_ATOMIC_H
13
14 #include <asm/irqflags.h>
15 #include <asm/cmpxchg.h>
16 #include <asm/barrier.h>
17
18 #ifndef CONFIG_SMP
19 #include <asm-generic/atomic.h>
20 #else
21
22 /*
23  * Atomic operations that C can't guarantee us.  Useful for
24  * resource counting etc..
25  */
26
27 #define ATOMIC_INIT(i)  { (i) }
28
29 #ifdef __KERNEL__
30
31 /**
32  * atomic_read - read atomic variable
33  * @v: pointer of type atomic_t
34  *
35  * Atomically reads the value of @v.  Note that the guaranteed
36  * useful range of an atomic_t is only 24 bits.
37  */
38 #define atomic_read(v)  (ACCESS_ONCE((v)->counter))
39
40 /**
41  * atomic_set - set atomic variable
42  * @v: pointer of type atomic_t
43  * @i: required value
44  *
45  * Atomically sets the value of @v to @i.  Note that the guaranteed
46  * useful range of an atomic_t is only 24 bits.
47  */
48 #define atomic_set(v, i) (((v)->counter) = (i))
49
50 /**
51  * atomic_add_return - add integer to atomic variable
52  * @i: integer value to add
53  * @v: pointer of type atomic_t
54  *
55  * Atomically adds @i to @v and returns the result
56  * Note that the guaranteed useful range of an atomic_t is only 24 bits.
57  */
58 static inline int atomic_add_return(int i, atomic_t *v)
59 {
60         int retval;
61 #ifdef CONFIG_SMP
62         int status;
63
64         asm volatile(
65                 "1:     mov     %4,(_AAR,%3)    \n"
66                 "       mov     (_ADR,%3),%1    \n"
67                 "       add     %5,%1           \n"
68                 "       mov     %1,(_ADR,%3)    \n"
69                 "       mov     (_ADR,%3),%0    \n"     /* flush */
70                 "       mov     (_ASR,%3),%0    \n"
71                 "       or      %0,%0           \n"
72                 "       bne     1b              \n"
73                 : "=&r"(status), "=&r"(retval), "=m"(v->counter)
74                 : "a"(ATOMIC_OPS_BASE_ADDR), "r"(&v->counter), "r"(i)
75                 : "memory", "cc");
76
77 #else
78         unsigned long flags;
79
80         flags = arch_local_cli_save();
81         retval = v->counter;
82         retval += i;
83         v->counter = retval;
84         arch_local_irq_restore(flags);
85 #endif
86         return retval;
87 }
88
89 /**
90  * atomic_sub_return - subtract integer from atomic variable
91  * @i: integer value to subtract
92  * @v: pointer of type atomic_t
93  *
94  * Atomically subtracts @i from @v and returns the result
95  * Note that the guaranteed useful range of an atomic_t is only 24 bits.
96  */
97 static inline int atomic_sub_return(int i, atomic_t *v)
98 {
99         int retval;
100 #ifdef CONFIG_SMP
101         int status;
102
103         asm volatile(
104                 "1:     mov     %4,(_AAR,%3)    \n"
105                 "       mov     (_ADR,%3),%1    \n"
106                 "       sub     %5,%1           \n"
107                 "       mov     %1,(_ADR,%3)    \n"
108                 "       mov     (_ADR,%3),%0    \n"     /* flush */
109                 "       mov     (_ASR,%3),%0    \n"
110                 "       or      %0,%0           \n"
111                 "       bne     1b              \n"
112                 : "=&r"(status), "=&r"(retval), "=m"(v->counter)
113                 : "a"(ATOMIC_OPS_BASE_ADDR), "r"(&v->counter), "r"(i)
114                 : "memory", "cc");
115
116 #else
117         unsigned long flags;
118         flags = arch_local_cli_save();
119         retval = v->counter;
120         retval -= i;
121         v->counter = retval;
122         arch_local_irq_restore(flags);
123 #endif
124         return retval;
125 }
126
127 static inline int atomic_add_negative(int i, atomic_t *v)
128 {
129         return atomic_add_return(i, v) < 0;
130 }
131
132 static inline void atomic_add(int i, atomic_t *v)
133 {
134         atomic_add_return(i, v);
135 }
136
137 static inline void atomic_sub(int i, atomic_t *v)
138 {
139         atomic_sub_return(i, v);
140 }
141
142 static inline void atomic_inc(atomic_t *v)
143 {
144         atomic_add_return(1, v);
145 }
146
147 static inline void atomic_dec(atomic_t *v)
148 {
149         atomic_sub_return(1, v);
150 }
151
152 #define atomic_dec_return(v)            atomic_sub_return(1, (v))
153 #define atomic_inc_return(v)            atomic_add_return(1, (v))
154
155 #define atomic_sub_and_test(i, v)       (atomic_sub_return((i), (v)) == 0)
156 #define atomic_dec_and_test(v)          (atomic_sub_return(1, (v)) == 0)
157 #define atomic_inc_and_test(v)          (atomic_add_return(1, (v)) == 0)
158
159 #define __atomic_add_unless(v, a, u)                            \
160 ({                                                              \
161         int c, old;                                             \
162         c = atomic_read(v);                                     \
163         while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
164                 c = old;                                        \
165         c;                                                      \
166 })
167
168 #define atomic_xchg(ptr, v)             (xchg(&(ptr)->counter, (v)))
169 #define atomic_cmpxchg(v, old, new)     (cmpxchg(&((v)->counter), (old), (new)))
170
171 /**
172  * atomic_clear_mask - Atomically clear bits in memory
173  * @mask: Mask of the bits to be cleared
174  * @v: pointer to word in memory
175  *
176  * Atomically clears the bits set in mask from the memory word specified.
177  */
178 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
179 {
180 #ifdef CONFIG_SMP
181         int status;
182
183         asm volatile(
184                 "1:     mov     %3,(_AAR,%2)    \n"
185                 "       mov     (_ADR,%2),%0    \n"
186                 "       and     %4,%0           \n"
187                 "       mov     %0,(_ADR,%2)    \n"
188                 "       mov     (_ADR,%2),%0    \n"     /* flush */
189                 "       mov     (_ASR,%2),%0    \n"
190                 "       or      %0,%0           \n"
191                 "       bne     1b              \n"
192                 : "=&r"(status), "=m"(*addr)
193                 : "a"(ATOMIC_OPS_BASE_ADDR), "r"(addr), "r"(~mask)
194                 : "memory", "cc");
195 #else
196         unsigned long flags;
197
198         mask = ~mask;
199         flags = arch_local_cli_save();
200         *addr &= mask;
201         arch_local_irq_restore(flags);
202 #endif
203 }
204
205 /**
206  * atomic_set_mask - Atomically set bits in memory
207  * @mask: Mask of the bits to be set
208  * @v: pointer to word in memory
209  *
210  * Atomically sets the bits set in mask from the memory word specified.
211  */
212 static inline void atomic_set_mask(unsigned long mask, unsigned long *addr)
213 {
214 #ifdef CONFIG_SMP
215         int status;
216
217         asm volatile(
218                 "1:     mov     %3,(_AAR,%2)    \n"
219                 "       mov     (_ADR,%2),%0    \n"
220                 "       or      %4,%0           \n"
221                 "       mov     %0,(_ADR,%2)    \n"
222                 "       mov     (_ADR,%2),%0    \n"     /* flush */
223                 "       mov     (_ASR,%2),%0    \n"
224                 "       or      %0,%0           \n"
225                 "       bne     1b              \n"
226                 : "=&r"(status), "=m"(*addr)
227                 : "a"(ATOMIC_OPS_BASE_ADDR), "r"(addr), "r"(mask)
228                 : "memory", "cc");
229 #else
230         unsigned long flags;
231
232         flags = arch_local_cli_save();
233         *addr |= mask;
234         arch_local_irq_restore(flags);
235 #endif
236 }
237
238 #endif /* __KERNEL__ */
239 #endif /* CONFIG_SMP */
240 #endif /* _ASM_ATOMIC_H */