blob: 9807030557c4df86541cb47607807d28a2837493 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright 2010 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 * Atomic primitives.
15 */
16
17#ifndef _ASM_TILE_ATOMIC_H
18#define _ASM_TILE_ATOMIC_H
19
Paul Gortmaker34f2c0a2012-04-01 16:38:46 -040020#include <asm/cmpxchg.h>
21
Chris Metcalf867e3592010-05-28 23:09:12 -040022#ifndef __ASSEMBLY__
23
24#include <linux/compiler.h>
David Howellsbd119c62012-03-28 18:30:03 +010025#include <linux/types.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040026
27#define ATOMIC_INIT(i) { (i) }
28
29/**
30 * atomic_read - read atomic variable
31 * @v: pointer of type atomic_t
32 *
33 * Atomically reads the value of @v.
34 */
35static inline int atomic_read(const atomic_t *v)
36{
Peter Zijlstra62e8a322015-09-18 11:13:10 +020037 return READ_ONCE(v->counter);
Chris Metcalf867e3592010-05-28 23:09:12 -040038}
39
40/**
41 * atomic_sub_return - subtract integer and return
42 * @v: pointer of type atomic_t
43 * @i: integer value to subtract
44 *
45 * Atomically subtracts @i from @v and returns @v - @i
46 */
47#define atomic_sub_return(i, v) atomic_add_return((int)(-(i)), (v))
48
Peter Zijlstra1af5de92016-04-18 01:16:03 +020049#define atomic_fetch_sub(i, v) atomic_fetch_add(-(int)(i), (v))
50
51#define atomic_fetch_or atomic_fetch_or
52
Chris Metcalf867e3592010-05-28 23:09:12 -040053/**
54 * atomic_sub - subtract integer from atomic variable
55 * @i: integer value to subtract
56 * @v: pointer of type atomic_t
57 *
58 * Atomically subtracts @i from @v.
59 */
60#define atomic_sub(i, v) atomic_add((int)(-(i)), (v))
61
62/**
63 * atomic_sub_and_test - subtract value from variable and test result
64 * @i: integer value to subtract
65 * @v: pointer of type atomic_t
66 *
67 * Atomically subtracts @i from @v and returns true if the result is
68 * zero, or false for all other cases.
69 */
70#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
71
72/**
73 * atomic_inc_return - increment memory and return
74 * @v: pointer of type atomic_t
75 *
76 * Atomically increments @v by 1 and returns the new value.
77 */
78#define atomic_inc_return(v) atomic_add_return(1, (v))
79
80/**
81 * atomic_dec_return - decrement memory and return
82 * @v: pointer of type atomic_t
83 *
84 * Atomically decrements @v by 1 and returns the new value.
85 */
86#define atomic_dec_return(v) atomic_sub_return(1, (v))
87
88/**
89 * atomic_inc - increment atomic variable
90 * @v: pointer of type atomic_t
91 *
92 * Atomically increments @v by 1.
93 */
94#define atomic_inc(v) atomic_add(1, (v))
95
96/**
97 * atomic_dec - decrement atomic variable
98 * @v: pointer of type atomic_t
99 *
100 * Atomically decrements @v by 1.
101 */
102#define atomic_dec(v) atomic_sub(1, (v))
103
104/**
105 * atomic_dec_and_test - decrement and test
106 * @v: pointer of type atomic_t
107 *
108 * Atomically decrements @v by 1 and returns true if the result is 0.
109 */
110#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
111
112/**
113 * atomic_inc_and_test - increment and test
114 * @v: pointer of type atomic_t
115 *
116 * Atomically increments @v by 1 and returns true if the result is 0.
117 */
118#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
119
120/**
Chris Metcalf6dc96582013-09-06 08:56:45 -0400121 * atomic_xchg - atomically exchange contents of memory with a new value
122 * @v: pointer of type atomic_t
123 * @i: integer value to store in memory
124 *
125 * Atomically sets @v to @i and returns old @v
126 */
127static inline int atomic_xchg(atomic_t *v, int n)
128{
129 return xchg(&v->counter, n);
130}
131
132/**
133 * atomic_cmpxchg - atomically exchange contents of memory if it matches
134 * @v: pointer of type atomic_t
135 * @o: old value that memory should have
136 * @n: new value to write to memory if it matches
137 *
138 * Atomically checks if @v holds @o and replaces it with @n if so.
139 * Returns the old value at @v.
140 */
141static inline int atomic_cmpxchg(atomic_t *v, int o, int n)
142{
143 return cmpxchg(&v->counter, o, n);
144}
145
146/**
Chris Metcalf867e3592010-05-28 23:09:12 -0400147 * atomic_add_negative - add and test if negative
148 * @v: pointer of type atomic_t
149 * @i: integer value to add
150 *
151 * Atomically adds @i to @v and returns true if the result is
152 * negative, or false when result is greater than or equal to zero.
153 */
154#define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
155
Chris Metcalf867e3592010-05-28 23:09:12 -0400156#endif /* __ASSEMBLY__ */
157
158#ifndef __tilegx__
159#include <asm/atomic_32.h>
160#else
161#include <asm/atomic_64.h>
162#endif
163
Chris Metcalfadf6d9b2013-02-01 12:37:48 -0500164#ifndef __ASSEMBLY__
165
Chris Metcalf6dc96582013-09-06 08:56:45 -0400166/**
167 * atomic64_xchg - atomically exchange contents of memory with a new value
168 * @v: pointer of type atomic64_t
169 * @i: integer value to store in memory
170 *
171 * Atomically sets @v to @i and returns old @v
172 */
Chen Gangb924a692013-09-25 12:14:08 +0800173static inline long long atomic64_xchg(atomic64_t *v, long long n)
Chris Metcalf6dc96582013-09-06 08:56:45 -0400174{
175 return xchg64(&v->counter, n);
176}
177
178/**
179 * atomic64_cmpxchg - atomically exchange contents of memory if it matches
180 * @v: pointer of type atomic64_t
181 * @o: old value that memory should have
182 * @n: new value to write to memory if it matches
183 *
184 * Atomically checks if @v holds @o and replaces it with @n if so.
185 * Returns the old value at @v.
186 */
Chen Gangb924a692013-09-25 12:14:08 +0800187static inline long long atomic64_cmpxchg(atomic64_t *v, long long o,
188 long long n)
Chris Metcalf6dc96582013-09-06 08:56:45 -0400189{
190 return cmpxchg64(&v->counter, o, n);
191}
192
Chris Metcalfadf6d9b2013-02-01 12:37:48 -0500193static inline long long atomic64_dec_if_positive(atomic64_t *v)
194{
195 long long c, old, dec;
196
197 c = atomic64_read(v);
198 for (;;) {
199 dec = c - 1;
200 if (unlikely(dec < 0))
201 break;
202 old = atomic64_cmpxchg((v), c, dec);
203 if (likely(old == c))
204 break;
205 c = old;
206 }
207 return dec;
208}
209
210#endif /* __ASSEMBLY__ */
211
Chris Metcalf867e3592010-05-28 23:09:12 -0400212#endif /* _ASM_TILE_ATOMIC_H */