2 * Atomic operations for the Hexagon architecture
4 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <linux/types.h>
26 #include <asm/cmpxchg.h>
28 #define ATOMIC_INIT(i) { (i) }
29 #define atomic_set(v, i) ((v)->counter = (i))
32 * atomic_read - reads a word, atomically
33 * @v: pointer to atomic value
35 * Assumes all word reads on our architecture are atomic.
37 #define atomic_read(v) ((v)->counter)
40 * atomic_xchg - atomic
41 * @v: pointer to memory to change
42 * @new: new value (technically passed in a register -- see xchg)
44 #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
48 * atomic_cmpxchg - atomic compare-and-exchange values
49 * @v: pointer to value to change
50 * @old: desired old value to match
51 * @new: new value to put in
53 * Parameters are then pointer, value-in-register, value-in-register,
54 * and the output is the old value.
56 * Apparently this is complicated for archs that don't support
57 * the memw_locked like we do (or it's broken or whatever).
59 * Kind of the lynchpin of the rest of the generically defined routines.
60 * Remember V2 had that bug with dotnew predicate set by memw_locked.
62 * "old" is "expected" old val, __oldval is actual old value
64 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
69 "1: %0 = memw_locked(%1);\n"
70 " { P0 = cmp.eq(%0,%2);\n"
71 " if (!P0.new) jump:nt 2f; }\n"
72 " memw_locked(%1,P0) = %3;\n"
73 " if (!P0) jump 1b;\n"
76 : "r" (&v->counter), "r" (old), "r" (new)
83 static inline int atomic_add_return(int i, atomic_t *v)
87 __asm__ __volatile__ (
88 "1: %0 = memw_locked(%1);\n"
90 " memw_locked(%1,P3)=%0;\n"
93 : "r" (&v->counter), "r" (i)
100 #define atomic_add(i, v) atomic_add_return(i, (v))
102 static inline int atomic_sub_return(int i, atomic_t *v)
105 __asm__ __volatile__ (
106 "1: %0 = memw_locked(%1);\n"
107 " %0 = sub(%0,%2);\n"
108 " memw_locked(%1,P3)=%0\n"
111 : "r" (&v->counter), "r" (i)
117 #define atomic_sub(i, v) atomic_sub_return(i, (v))
120 * atomic_add_unless - add unless the number is a given value
121 * @v: pointer to value
123 * @u: unless value is equal to u
125 * Returns 1 if the add happened, 0 if it didn't.
127 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
129 int output, __oldval;
131 "1: %0 = memw_locked(%2);"
133 " p3 = cmp.eq(%0, %4);"
134 " if (p3.new) jump:nt 2f;"
138 " memw_locked(%2, p3) = %0;"
144 : "=&r" (__oldval), "=&r" (output)
145 : "r" (v), "r" (a), "r" (u)
151 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
153 #define atomic_inc(v) atomic_add(1, (v))
154 #define atomic_dec(v) atomic_sub(1, (v))
156 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
157 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
158 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, (v)) == 0)
159 #define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
162 #define atomic_inc_return(v) (atomic_add_return(1, v))
163 #define atomic_dec_return(v) (atomic_sub_return(1, v))