1 /* MN10300 Atomic counter operations
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
14 #include <asm/irqflags.h>
15 #include <asm/cmpxchg.h>
16 #include <asm/barrier.h>
19 #include <asm-generic/atomic.h>
23 * Atomic operations that C can't guarantee us. Useful for
24 * resource counting etc..
27 #define ATOMIC_INIT(i) { (i) }
32 * atomic_read - read atomic variable
33 * @v: pointer of type atomic_t
35 * Atomically reads the value of @v. Note that the guaranteed
36 * useful range of an atomic_t is only 24 bits.
38 #define atomic_read(v) (ACCESS_ONCE((v)->counter))
41 * atomic_set - set atomic variable
42 * @v: pointer of type atomic_t
45 * Atomically sets the value of @v to @i. Note that the guaranteed
46 * useful range of an atomic_t is only 24 bits.
48 #define atomic_set(v, i) (((v)->counter) = (i))
51 * atomic_add_return - add integer to atomic variable
52 * @i: integer value to add
53 * @v: pointer of type atomic_t
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.
58 static inline int atomic_add_return(int i, atomic_t *v)
65 "1: mov %4,(_AAR,%3) \n"
66 " mov (_ADR,%3),%1 \n"
68 " mov %1,(_ADR,%3) \n"
69 " mov (_ADR,%3),%0 \n" /* flush */
70 " mov (_ASR,%3),%0 \n"
73 : "=&r"(status), "=&r"(retval), "=m"(v->counter)
74 : "a"(ATOMIC_OPS_BASE_ADDR), "r"(&v->counter), "r"(i)
80 flags = arch_local_cli_save();
84 arch_local_irq_restore(flags);
90 * atomic_sub_return - subtract integer from atomic variable
91 * @i: integer value to subtract
92 * @v: pointer of type atomic_t
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.
97 static inline int atomic_sub_return(int i, atomic_t *v)
104 "1: mov %4,(_AAR,%3) \n"
105 " mov (_ADR,%3),%1 \n"
107 " mov %1,(_ADR,%3) \n"
108 " mov (_ADR,%3),%0 \n" /* flush */
109 " mov (_ASR,%3),%0 \n"
112 : "=&r"(status), "=&r"(retval), "=m"(v->counter)
113 : "a"(ATOMIC_OPS_BASE_ADDR), "r"(&v->counter), "r"(i)
118 flags = arch_local_cli_save();
122 arch_local_irq_restore(flags);
127 static inline int atomic_add_negative(int i, atomic_t *v)
129 return atomic_add_return(i, v) < 0;
132 static inline void atomic_add(int i, atomic_t *v)
134 atomic_add_return(i, v);
137 static inline void atomic_sub(int i, atomic_t *v)
139 atomic_sub_return(i, v);
142 static inline void atomic_inc(atomic_t *v)
144 atomic_add_return(1, v);
147 static inline void atomic_dec(atomic_t *v)
149 atomic_sub_return(1, v);
152 #define atomic_dec_return(v) atomic_sub_return(1, (v))
153 #define atomic_inc_return(v) atomic_add_return(1, (v))
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)
159 #define __atomic_add_unless(v, a, u) \
162 c = atomic_read(v); \
163 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
168 #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
169 #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
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
176 * Atomically clears the bits set in mask from the memory word specified.
178 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
184 "1: mov %3,(_AAR,%2) \n"
185 " mov (_ADR,%2),%0 \n"
187 " mov %0,(_ADR,%2) \n"
188 " mov (_ADR,%2),%0 \n" /* flush */
189 " mov (_ASR,%2),%0 \n"
192 : "=&r"(status), "=m"(*addr)
193 : "a"(ATOMIC_OPS_BASE_ADDR), "r"(addr), "r"(~mask)
199 flags = arch_local_cli_save();
201 arch_local_irq_restore(flags);
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
210 * Atomically sets the bits set in mask from the memory word specified.
212 static inline void atomic_set_mask(unsigned long mask, unsigned long *addr)
218 "1: mov %3,(_AAR,%2) \n"
219 " mov (_ADR,%2),%0 \n"
221 " mov %0,(_ADR,%2) \n"
222 " mov (_ADR,%2),%0 \n" /* flush */
223 " mov (_ASR,%2),%0 \n"
226 : "=&r"(status), "=m"(*addr)
227 : "a"(ATOMIC_OPS_BASE_ADDR), "r"(addr), "r"(mask)
232 flags = arch_local_cli_save();
234 arch_local_irq_restore(flags);
238 #endif /* __KERNEL__ */
239 #endif /* CONFIG_SMP */
240 #endif /* _ASM_ATOMIC_H */