tile: Provide atomic_{or,xor,and}
[cascardo/linux.git] / arch / tile / include / asm / atomic_64.h
1 /*
2  * Copyright 2011 Tilera Corporation. All Rights Reserved.
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful, but
9  *   WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11  *   NON INFRINGEMENT.  See the GNU General Public License for
12  *   more details.
13  *
14  * Do not include directly; use <linux/atomic.h>.
15  */
16
17 #ifndef _ASM_TILE_ATOMIC_64_H
18 #define _ASM_TILE_ATOMIC_64_H
19
20 #ifndef __ASSEMBLY__
21
22 #include <asm/barrier.h>
23 #include <arch/spr_def.h>
24
25 /* First, the 32-bit atomic ops that are "real" on our 64-bit platform. */
26
27 #define atomic_set(v, i) ((v)->counter = (i))
28
29 /*
30  * The smp_mb() operations throughout are to support the fact that
31  * Linux requires memory barriers before and after the operation,
32  * on any routine which updates memory and returns a value.
33  */
34
35 static inline void atomic_add(int i, atomic_t *v)
36 {
37         __insn_fetchadd4((void *)&v->counter, i);
38 }
39
40 static inline int atomic_add_return(int i, atomic_t *v)
41 {
42         int val;
43         smp_mb();  /* barrier for proper semantics */
44         val = __insn_fetchadd4((void *)&v->counter, i) + i;
45         barrier();  /* the "+ i" above will wait on memory */
46         return val;
47 }
48
49 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
50 {
51         int guess, oldval = v->counter;
52         do {
53                 if (oldval == u)
54                         break;
55                 guess = oldval;
56                 oldval = cmpxchg(&v->counter, guess, guess + a);
57         } while (guess != oldval);
58         return oldval;
59 }
60
61 #define CONFIG_ARCH_HAS_ATOMIC_OR
62
63 static inline void atomic_and(int i, atomic_t *v)
64 {
65         __insn_fetchand4((void *)&v->counter, i);
66 }
67
68 static inline void atomic_or(int i, atomic_t *v)
69 {
70         __insn_fetchor4((void *)&v->counter, i);
71 }
72
73 static inline void atomic_xor(int i, atomic_t *v)
74 {
75         int guess, oldval = v->counter;
76         do {
77                 guess = oldval;
78                 __insn_mtspr(SPR_CMPEXCH_VALUE, guess);
79                 oldval = __insn_cmpexch4(&v->counter, guess ^ i);
80         } while (guess != oldval);
81 }
82
83 /* Now the true 64-bit operations. */
84
85 #define ATOMIC64_INIT(i)        { (i) }
86
87 #define atomic64_read(v)                ((v)->counter)
88 #define atomic64_set(v, i) ((v)->counter = (i))
89
90 static inline void atomic64_add(long i, atomic64_t *v)
91 {
92         __insn_fetchadd((void *)&v->counter, i);
93 }
94
95 static inline long atomic64_add_return(long i, atomic64_t *v)
96 {
97         int val;
98         smp_mb();  /* barrier for proper semantics */
99         val = __insn_fetchadd((void *)&v->counter, i) + i;
100         barrier();  /* the "+ i" above will wait on memory */
101         return val;
102 }
103
104 static inline long atomic64_add_unless(atomic64_t *v, long a, long u)
105 {
106         long guess, oldval = v->counter;
107         do {
108                 if (oldval == u)
109                         break;
110                 guess = oldval;
111                 oldval = cmpxchg(&v->counter, guess, guess + a);
112         } while (guess != oldval);
113         return oldval != u;
114 }
115
116 static inline void atomic64_and(long i, atomic64_t *v)
117 {
118         __insn_fetchand((void *)&v->counter, i);
119 }
120
121 static inline void atomic64_or(long i, atomic64_t *v)
122 {
123         __insn_fetchor((void *)&v->counter, i);
124 }
125
126 static inline void atomic64_xor(long i, atomic64_t *v)
127 {
128         long guess, oldval = v->counter;
129         do {
130                 guess = oldval;
131                 __insn_mtspr(SPR_CMPEXCH_VALUE, guess);
132                 oldval = __insn_cmpexch(&v->counter, guess ^ i);
133         } while (guess != oldval);
134 }
135
136 #define atomic64_sub_return(i, v)       atomic64_add_return(-(i), (v))
137 #define atomic64_sub(i, v)              atomic64_add(-(i), (v))
138 #define atomic64_inc_return(v)          atomic64_add_return(1, (v))
139 #define atomic64_dec_return(v)          atomic64_sub_return(1, (v))
140 #define atomic64_inc(v)                 atomic64_add(1, (v))
141 #define atomic64_dec(v)                 atomic64_sub(1, (v))
142
143 #define atomic64_inc_and_test(v)        (atomic64_inc_return(v) == 0)
144 #define atomic64_dec_and_test(v)        (atomic64_dec_return(v) == 0)
145 #define atomic64_sub_and_test(i, v)     (atomic64_sub_return((i), (v)) == 0)
146 #define atomic64_add_negative(i, v)     (atomic64_add_return((i), (v)) < 0)
147
148 #define atomic64_inc_not_zero(v)        atomic64_add_unless((v), 1, 0)
149
150 #endif /* !__ASSEMBLY__ */
151
152 #endif /* _ASM_TILE_ATOMIC_64_H */