1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Atomic operations for the Hexagon architecture
5 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
11 #include <linux/types.h>
12 #include <asm/cmpxchg.h>
13 #include <asm/barrier.h>
15 #define ATOMIC_INIT(i) { (i) }
17 /* Normal writes in our arch don't clear lock reservations */
19 static inline void atomic_set(atomic_t *v, int new)
22 "1: r6 = memw_locked(%0);\n"
23 " memw_locked(%0,p0) = %1;\n"
24 " if (!P0) jump 1b;\n"
26 : "r" (&v->counter), "r" (new)
27 : "memory", "p0", "r6"
31 #define atomic_set_release(v, i) atomic_set((v), (i))
34 * atomic_read - reads a word, atomically
35 * @v: pointer to atomic value
37 * Assumes all word reads on our architecture are atomic.
39 #define atomic_read(v) READ_ONCE((v)->counter)
42 * atomic_xchg - atomic
43 * @v: pointer to memory to change
44 * @new: new value (technically passed in a register -- see xchg)
46 #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
50 * atomic_cmpxchg - atomic compare-and-exchange values
51 * @v: pointer to value to change
52 * @old: desired old value to match
53 * @new: new value to put in
55 * Parameters are then pointer, value-in-register, value-in-register,
56 * and the output is the old value.
58 * Apparently this is complicated for archs that don't support
59 * the memw_locked like we do (or it's broken or whatever).
61 * Kind of the lynchpin of the rest of the generically defined routines.
62 * Remember V2 had that bug with dotnew predicate set by memw_locked.
64 * "old" is "expected" old val, __oldval is actual old value
66 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
71 "1: %0 = memw_locked(%1);\n"
72 " { P0 = cmp.eq(%0,%2);\n"
73 " if (!P0.new) jump:nt 2f; }\n"
74 " memw_locked(%1,P0) = %3;\n"
75 " if (!P0) jump 1b;\n"
78 : "r" (&v->counter), "r" (old), "r" (new)
85 #define ATOMIC_OP(op) \
86 static inline void atomic_##op(int i, atomic_t *v) \
90 __asm__ __volatile__ ( \
91 "1: %0 = memw_locked(%1);\n" \
92 " %0 = "#op "(%0,%2);\n" \
93 " memw_locked(%1,P3)=%0;\n" \
94 " if !P3 jump 1b;\n" \
96 : "r" (&v->counter), "r" (i) \
101 #define ATOMIC_OP_RETURN(op) \
102 static inline int atomic_##op##_return(int i, atomic_t *v) \
106 __asm__ __volatile__ ( \
107 "1: %0 = memw_locked(%1);\n" \
108 " %0 = "#op "(%0,%2);\n" \
109 " memw_locked(%1,P3)=%0;\n" \
110 " if !P3 jump 1b;\n" \
112 : "r" (&v->counter), "r" (i) \
118 #define ATOMIC_FETCH_OP(op) \
119 static inline int atomic_fetch_##op(int i, atomic_t *v) \
123 __asm__ __volatile__ ( \
124 "1: %0 = memw_locked(%2);\n" \
125 " %1 = "#op "(%0,%3);\n" \
126 " memw_locked(%2,P3)=%1;\n" \
127 " if !P3 jump 1b;\n" \
128 : "=&r" (output), "=&r" (val) \
129 : "r" (&v->counter), "r" (i) \
135 #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) ATOMIC_FETCH_OP(op)
141 #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_FETCH_OP(op)
148 #undef ATOMIC_FETCH_OP
149 #undef ATOMIC_OP_RETURN
153 * atomic_fetch_add_unless - add unless the number is a given value
154 * @v: pointer to value
156 * @u: unless value is equal to u
162 static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
168 "1: %0 = memw_locked(%2);"
170 " p3 = cmp.eq(%0, %4);"
171 " if (p3.new) jump:nt 2f;"
174 " memw_locked(%2, p3) = %1;"
179 : "=&r" (__oldval), "=&r" (tmp)
180 : "r" (v), "r" (a), "r" (u)
185 #define atomic_fetch_add_unless atomic_fetch_add_unless