Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Based on arch/arm/include/asm/atomic.h |
| 4 | * |
| 5 | * Copyright (C) 1996 Russell King. |
| 6 | * Copyright (C) 2002 Deep Blue Solutions Ltd. |
| 7 | * Copyright (C) 2012 ARM Ltd. |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef __ASM_ATOMIC_LSE_H |
| 11 | #define __ASM_ATOMIC_LSE_H |
| 12 | |
Will Deacon | 6822a84d | 2016-04-22 18:01:32 +0100 | [diff] [blame] | 13 | #define ATOMIC_OP(op, asm_op) \ |
Mark Rutland | 8e6082e | 2021-12-10 15:14:06 +0000 | [diff] [blame] | 14 | static inline void __lse_atomic_##op(int i, atomic_t *v) \ |
Will Deacon | 6822a84d | 2016-04-22 18:01:32 +0100 | [diff] [blame] | 15 | { \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 16 | asm volatile( \ |
Sami Tolvanen | e0d5896 | 2019-10-31 12:57:05 -0700 | [diff] [blame] | 17 | __LSE_PREAMBLE \ |
Mark Rutland | 8e6082e | 2021-12-10 15:14:06 +0000 | [diff] [blame] | 18 | " " #asm_op " %w[i], %[v]\n" \ |
Mark Rutland | 8a578a7 | 2021-12-10 15:14:09 +0000 | [diff] [blame] | 19 | : [v] "+Q" (v->counter) \ |
| 20 | : [i] "r" (i)); \ |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Will Deacon | 6822a84d | 2016-04-22 18:01:32 +0100 | [diff] [blame] | 23 | ATOMIC_OP(andnot, stclr) |
| 24 | ATOMIC_OP(or, stset) |
| 25 | ATOMIC_OP(xor, steor) |
| 26 | ATOMIC_OP(add, stadd) |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 27 | |
Mark Rutland | ef53245 | 2021-12-10 15:14:07 +0000 | [diff] [blame] | 28 | static inline void __lse_atomic_sub(int i, atomic_t *v) |
| 29 | { |
| 30 | __lse_atomic_add(-i, v); |
| 31 | } |
| 32 | |
Will Deacon | 6822a84d | 2016-04-22 18:01:32 +0100 | [diff] [blame] | 33 | #undef ATOMIC_OP |
Will Deacon | c09d6a0 | 2015-02-03 16:14:13 +0000 | [diff] [blame] | 34 | |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 35 | #define ATOMIC_FETCH_OP(name, mb, op, asm_op, cl...) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 36 | static inline int __lse_atomic_fetch_##op##name(int i, atomic_t *v) \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 37 | { \ |
Mark Rutland | 8a578a7 | 2021-12-10 15:14:09 +0000 | [diff] [blame] | 38 | int old; \ |
| 39 | \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 40 | asm volatile( \ |
Sami Tolvanen | e0d5896 | 2019-10-31 12:57:05 -0700 | [diff] [blame] | 41 | __LSE_PREAMBLE \ |
Mark Rutland | 8a578a7 | 2021-12-10 15:14:09 +0000 | [diff] [blame] | 42 | " " #asm_op #mb " %w[i], %w[old], %[v]" \ |
| 43 | : [v] "+Q" (v->counter), \ |
| 44 | [old] "=r" (old) \ |
| 45 | : [i] "r" (i) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 46 | : cl); \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 47 | \ |
Mark Rutland | 8a578a7 | 2021-12-10 15:14:09 +0000 | [diff] [blame] | 48 | return old; \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | #define ATOMIC_FETCH_OPS(op, asm_op) \ |
| 52 | ATOMIC_FETCH_OP(_relaxed, , op, asm_op) \ |
| 53 | ATOMIC_FETCH_OP(_acquire, a, op, asm_op, "memory") \ |
| 54 | ATOMIC_FETCH_OP(_release, l, op, asm_op, "memory") \ |
| 55 | ATOMIC_FETCH_OP( , al, op, asm_op, "memory") |
| 56 | |
| 57 | ATOMIC_FETCH_OPS(andnot, ldclr) |
| 58 | ATOMIC_FETCH_OPS(or, ldset) |
| 59 | ATOMIC_FETCH_OPS(xor, ldeor) |
| 60 | ATOMIC_FETCH_OPS(add, ldadd) |
| 61 | |
| 62 | #undef ATOMIC_FETCH_OP |
| 63 | #undef ATOMIC_FETCH_OPS |
| 64 | |
Mark Rutland | ef53245 | 2021-12-10 15:14:07 +0000 | [diff] [blame] | 65 | #define ATOMIC_FETCH_OP_SUB(name) \ |
| 66 | static inline int __lse_atomic_fetch_sub##name(int i, atomic_t *v) \ |
| 67 | { \ |
| 68 | return __lse_atomic_fetch_add##name(-i, v); \ |
| 69 | } |
| 70 | |
| 71 | ATOMIC_FETCH_OP_SUB(_relaxed) |
| 72 | ATOMIC_FETCH_OP_SUB(_acquire) |
| 73 | ATOMIC_FETCH_OP_SUB(_release) |
| 74 | ATOMIC_FETCH_OP_SUB( ) |
| 75 | |
| 76 | #undef ATOMIC_FETCH_OP_SUB |
| 77 | |
Mark Rutland | 053f58b | 2021-12-10 15:14:10 +0000 | [diff] [blame] | 78 | #define ATOMIC_OP_ADD_SUB_RETURN(name) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 79 | static inline int __lse_atomic_add_return##name(int i, atomic_t *v) \ |
Will Deacon | 305d454 | 2015-10-08 20:15:18 +0100 | [diff] [blame] | 80 | { \ |
Mark Rutland | 053f58b | 2021-12-10 15:14:10 +0000 | [diff] [blame] | 81 | return __lse_atomic_fetch_add##name(i, v) + i; \ |
Mark Rutland | ef53245 | 2021-12-10 15:14:07 +0000 | [diff] [blame] | 82 | } \ |
| 83 | \ |
| 84 | static inline int __lse_atomic_sub_return##name(int i, atomic_t *v) \ |
| 85 | { \ |
Mark Rutland | 053f58b | 2021-12-10 15:14:10 +0000 | [diff] [blame] | 86 | return __lse_atomic_fetch_sub(i, v) - i; \ |
Will Deacon | c09d6a0 | 2015-02-03 16:14:13 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Mark Rutland | 053f58b | 2021-12-10 15:14:10 +0000 | [diff] [blame] | 89 | ATOMIC_OP_ADD_SUB_RETURN(_relaxed) |
| 90 | ATOMIC_OP_ADD_SUB_RETURN(_acquire) |
| 91 | ATOMIC_OP_ADD_SUB_RETURN(_release) |
| 92 | ATOMIC_OP_ADD_SUB_RETURN( ) |
Will Deacon | 305d454 | 2015-10-08 20:15:18 +0100 | [diff] [blame] | 93 | |
Mark Rutland | ef53245 | 2021-12-10 15:14:07 +0000 | [diff] [blame] | 94 | #undef ATOMIC_OP_ADD_SUB_RETURN |
Will Deacon | 305d454 | 2015-10-08 20:15:18 +0100 | [diff] [blame] | 95 | |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 96 | static inline void __lse_atomic_and(int i, atomic_t *v) |
Will Deacon | c09d6a0 | 2015-02-03 16:14:13 +0000 | [diff] [blame] | 97 | { |
Mark Rutland | 5e9e43c | 2021-12-10 15:14:08 +0000 | [diff] [blame] | 98 | return __lse_atomic_andnot(~i, v); |
Will Deacon | c09d6a0 | 2015-02-03 16:14:13 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 101 | #define ATOMIC_FETCH_OP_AND(name, mb, cl...) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 102 | static inline int __lse_atomic_fetch_and##name(int i, atomic_t *v) \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 103 | { \ |
Mark Rutland | 5e9e43c | 2021-12-10 15:14:08 +0000 | [diff] [blame] | 104 | return __lse_atomic_fetch_andnot##name(~i, v); \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ATOMIC_FETCH_OP_AND(_relaxed, ) |
| 108 | ATOMIC_FETCH_OP_AND(_acquire, a, "memory") |
| 109 | ATOMIC_FETCH_OP_AND(_release, l, "memory") |
| 110 | ATOMIC_FETCH_OP_AND( , al, "memory") |
| 111 | |
| 112 | #undef ATOMIC_FETCH_OP_AND |
| 113 | |
Will Deacon | 6822a84d | 2016-04-22 18:01:32 +0100 | [diff] [blame] | 114 | #define ATOMIC64_OP(op, asm_op) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 115 | static inline void __lse_atomic64_##op(s64 i, atomic64_t *v) \ |
Will Deacon | 6822a84d | 2016-04-22 18:01:32 +0100 | [diff] [blame] | 116 | { \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 117 | asm volatile( \ |
Sami Tolvanen | e0d5896 | 2019-10-31 12:57:05 -0700 | [diff] [blame] | 118 | __LSE_PREAMBLE \ |
Mark Rutland | 8e6082e | 2021-12-10 15:14:06 +0000 | [diff] [blame] | 119 | " " #asm_op " %[i], %[v]\n" \ |
Mark Rutland | 8a578a7 | 2021-12-10 15:14:09 +0000 | [diff] [blame] | 120 | : [v] "+Q" (v->counter) \ |
| 121 | : [i] "r" (i)); \ |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Will Deacon | 6822a84d | 2016-04-22 18:01:32 +0100 | [diff] [blame] | 124 | ATOMIC64_OP(andnot, stclr) |
| 125 | ATOMIC64_OP(or, stset) |
| 126 | ATOMIC64_OP(xor, steor) |
| 127 | ATOMIC64_OP(add, stadd) |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 128 | |
Mark Rutland | ef53245 | 2021-12-10 15:14:07 +0000 | [diff] [blame] | 129 | static inline void __lse_atomic64_sub(s64 i, atomic64_t *v) |
| 130 | { |
| 131 | __lse_atomic64_add(-i, v); |
| 132 | } |
| 133 | |
Will Deacon | 6822a84d | 2016-04-22 18:01:32 +0100 | [diff] [blame] | 134 | #undef ATOMIC64_OP |
Will Deacon | c09d6a0 | 2015-02-03 16:14:13 +0000 | [diff] [blame] | 135 | |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 136 | #define ATOMIC64_FETCH_OP(name, mb, op, asm_op, cl...) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 137 | static inline long __lse_atomic64_fetch_##op##name(s64 i, atomic64_t *v)\ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 138 | { \ |
Mark Rutland | 8a578a7 | 2021-12-10 15:14:09 +0000 | [diff] [blame] | 139 | s64 old; \ |
| 140 | \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 141 | asm volatile( \ |
Sami Tolvanen | e0d5896 | 2019-10-31 12:57:05 -0700 | [diff] [blame] | 142 | __LSE_PREAMBLE \ |
Mark Rutland | 8a578a7 | 2021-12-10 15:14:09 +0000 | [diff] [blame] | 143 | " " #asm_op #mb " %[i], %[old], %[v]" \ |
| 144 | : [v] "+Q" (v->counter), \ |
| 145 | [old] "=r" (old) \ |
| 146 | : [i] "r" (i) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 147 | : cl); \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 148 | \ |
Mark Rutland | 8a578a7 | 2021-12-10 15:14:09 +0000 | [diff] [blame] | 149 | return old; \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | #define ATOMIC64_FETCH_OPS(op, asm_op) \ |
| 153 | ATOMIC64_FETCH_OP(_relaxed, , op, asm_op) \ |
| 154 | ATOMIC64_FETCH_OP(_acquire, a, op, asm_op, "memory") \ |
| 155 | ATOMIC64_FETCH_OP(_release, l, op, asm_op, "memory") \ |
| 156 | ATOMIC64_FETCH_OP( , al, op, asm_op, "memory") |
| 157 | |
| 158 | ATOMIC64_FETCH_OPS(andnot, ldclr) |
| 159 | ATOMIC64_FETCH_OPS(or, ldset) |
| 160 | ATOMIC64_FETCH_OPS(xor, ldeor) |
| 161 | ATOMIC64_FETCH_OPS(add, ldadd) |
| 162 | |
| 163 | #undef ATOMIC64_FETCH_OP |
| 164 | #undef ATOMIC64_FETCH_OPS |
| 165 | |
Mark Rutland | ef53245 | 2021-12-10 15:14:07 +0000 | [diff] [blame] | 166 | #define ATOMIC64_FETCH_OP_SUB(name) \ |
| 167 | static inline long __lse_atomic64_fetch_sub##name(s64 i, atomic64_t *v) \ |
| 168 | { \ |
| 169 | return __lse_atomic64_fetch_add##name(-i, v); \ |
| 170 | } |
| 171 | |
| 172 | ATOMIC64_FETCH_OP_SUB(_relaxed) |
| 173 | ATOMIC64_FETCH_OP_SUB(_acquire) |
| 174 | ATOMIC64_FETCH_OP_SUB(_release) |
| 175 | ATOMIC64_FETCH_OP_SUB( ) |
| 176 | |
| 177 | #undef ATOMIC64_FETCH_OP_SUB |
| 178 | |
Mark Rutland | 053f58b | 2021-12-10 15:14:10 +0000 | [diff] [blame] | 179 | #define ATOMIC64_OP_ADD_SUB_RETURN(name) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 180 | static inline long __lse_atomic64_add_return##name(s64 i, atomic64_t *v)\ |
Will Deacon | 305d454 | 2015-10-08 20:15:18 +0100 | [diff] [blame] | 181 | { \ |
Mark Rutland | 053f58b | 2021-12-10 15:14:10 +0000 | [diff] [blame] | 182 | return __lse_atomic64_fetch_add##name(i, v) + i; \ |
Mark Rutland | ef53245 | 2021-12-10 15:14:07 +0000 | [diff] [blame] | 183 | } \ |
| 184 | \ |
| 185 | static inline long __lse_atomic64_sub_return##name(s64 i, atomic64_t *v)\ |
| 186 | { \ |
Mark Rutland | 053f58b | 2021-12-10 15:14:10 +0000 | [diff] [blame] | 187 | return __lse_atomic64_fetch_sub##name(i, v) - i; \ |
Will Deacon | c09d6a0 | 2015-02-03 16:14:13 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Mark Rutland | 053f58b | 2021-12-10 15:14:10 +0000 | [diff] [blame] | 190 | ATOMIC64_OP_ADD_SUB_RETURN(_relaxed) |
| 191 | ATOMIC64_OP_ADD_SUB_RETURN(_acquire) |
| 192 | ATOMIC64_OP_ADD_SUB_RETURN(_release) |
| 193 | ATOMIC64_OP_ADD_SUB_RETURN( ) |
Will Deacon | 305d454 | 2015-10-08 20:15:18 +0100 | [diff] [blame] | 194 | |
Mark Rutland | ef53245 | 2021-12-10 15:14:07 +0000 | [diff] [blame] | 195 | #undef ATOMIC64_OP_ADD_SUB_RETURN |
Will Deacon | 305d454 | 2015-10-08 20:15:18 +0100 | [diff] [blame] | 196 | |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 197 | static inline void __lse_atomic64_and(s64 i, atomic64_t *v) |
Will Deacon | c09d6a0 | 2015-02-03 16:14:13 +0000 | [diff] [blame] | 198 | { |
Mark Rutland | 5e9e43c | 2021-12-10 15:14:08 +0000 | [diff] [blame] | 199 | return __lse_atomic64_andnot(~i, v); |
Will Deacon | c09d6a0 | 2015-02-03 16:14:13 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 202 | #define ATOMIC64_FETCH_OP_AND(name, mb, cl...) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 203 | static inline long __lse_atomic64_fetch_and##name(s64 i, atomic64_t *v) \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 204 | { \ |
Mark Rutland | 5e9e43c | 2021-12-10 15:14:08 +0000 | [diff] [blame] | 205 | return __lse_atomic64_fetch_andnot##name(~i, v); \ |
Will Deacon | 2efe95f | 2016-04-22 18:01:33 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | ATOMIC64_FETCH_OP_AND(_relaxed, ) |
| 209 | ATOMIC64_FETCH_OP_AND(_acquire, a, "memory") |
| 210 | ATOMIC64_FETCH_OP_AND(_release, l, "memory") |
| 211 | ATOMIC64_FETCH_OP_AND( , al, "memory") |
| 212 | |
| 213 | #undef ATOMIC64_FETCH_OP_AND |
| 214 | |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 215 | static inline s64 __lse_atomic64_dec_if_positive(atomic64_t *v) |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 216 | { |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 217 | unsigned long tmp; |
| 218 | |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 219 | asm volatile( |
Sami Tolvanen | e0d5896 | 2019-10-31 12:57:05 -0700 | [diff] [blame] | 220 | __LSE_PREAMBLE |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 221 | "1: ldr %x[tmp], %[v]\n" |
| 222 | " subs %[ret], %x[tmp], #1\n" |
Will Deacon | db26217 | 2015-05-29 14:44:06 +0100 | [diff] [blame] | 223 | " b.lt 2f\n" |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 224 | " casal %x[tmp], %[ret], %[v]\n" |
| 225 | " sub %x[tmp], %x[tmp], #1\n" |
| 226 | " sub %x[tmp], %x[tmp], %[ret]\n" |
| 227 | " cbnz %x[tmp], 1b\n" |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 228 | "2:" |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 229 | : [ret] "+&r" (v), [v] "+Q" (v->counter), [tmp] "=&r" (tmp) |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 230 | : |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 231 | : "cc", "memory"); |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 232 | |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 233 | return (long)v; |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Will Deacon | 5ef3fe4 | 2018-09-13 13:30:45 +0100 | [diff] [blame] | 236 | #define __CMPXCHG_CASE(w, sfx, name, sz, mb, cl...) \ |
Will Deacon | a48e61d | 2019-10-01 11:43:13 +0100 | [diff] [blame] | 237 | static __always_inline u##sz \ |
| 238 | __lse__cmpxchg_case_##name##sz(volatile void *ptr, \ |
Will Deacon | b4f9209 | 2018-09-13 14:28:33 +0100 | [diff] [blame] | 239 | u##sz old, \ |
Will Deacon | 5ef3fe4 | 2018-09-13 13:30:45 +0100 | [diff] [blame] | 240 | u##sz new) \ |
Will Deacon | c342f78 | 2015-04-23 20:08:49 +0100 | [diff] [blame] | 241 | { \ |
| 242 | register unsigned long x0 asm ("x0") = (unsigned long)ptr; \ |
Will Deacon | b4f9209 | 2018-09-13 14:28:33 +0100 | [diff] [blame] | 243 | register u##sz x1 asm ("x1") = old; \ |
Will Deacon | 5ef3fe4 | 2018-09-13 13:30:45 +0100 | [diff] [blame] | 244 | register u##sz x2 asm ("x2") = new; \ |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 245 | unsigned long tmp; \ |
Will Deacon | c342f78 | 2015-04-23 20:08:49 +0100 | [diff] [blame] | 246 | \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 247 | asm volatile( \ |
Sami Tolvanen | e0d5896 | 2019-10-31 12:57:05 -0700 | [diff] [blame] | 248 | __LSE_PREAMBLE \ |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 249 | " mov %" #w "[tmp], %" #w "[old]\n" \ |
| 250 | " cas" #mb #sfx "\t%" #w "[tmp], %" #w "[new], %[v]\n" \ |
| 251 | " mov %" #w "[ret], %" #w "[tmp]" \ |
Kees Cook | 3364c6c | 2022-01-12 12:22:59 -0800 | [diff] [blame] | 252 | : [ret] "+r" (x0), [v] "+Q" (*(u##sz *)ptr), \ |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 253 | [tmp] "=&r" (tmp) \ |
Will Deacon | c342f78 | 2015-04-23 20:08:49 +0100 | [diff] [blame] | 254 | : [old] "r" (x1), [new] "r" (x2) \ |
Andrew Murray | 3337cb5 | 2019-08-28 18:50:08 +0100 | [diff] [blame] | 255 | : cl); \ |
Will Deacon | c342f78 | 2015-04-23 20:08:49 +0100 | [diff] [blame] | 256 | \ |
| 257 | return x0; \ |
| 258 | } |
| 259 | |
Will Deacon | 5ef3fe4 | 2018-09-13 13:30:45 +0100 | [diff] [blame] | 260 | __CMPXCHG_CASE(w, b, , 8, ) |
| 261 | __CMPXCHG_CASE(w, h, , 16, ) |
| 262 | __CMPXCHG_CASE(w, , , 32, ) |
| 263 | __CMPXCHG_CASE(x, , , 64, ) |
| 264 | __CMPXCHG_CASE(w, b, acq_, 8, a, "memory") |
| 265 | __CMPXCHG_CASE(w, h, acq_, 16, a, "memory") |
| 266 | __CMPXCHG_CASE(w, , acq_, 32, a, "memory") |
| 267 | __CMPXCHG_CASE(x, , acq_, 64, a, "memory") |
| 268 | __CMPXCHG_CASE(w, b, rel_, 8, l, "memory") |
| 269 | __CMPXCHG_CASE(w, h, rel_, 16, l, "memory") |
| 270 | __CMPXCHG_CASE(w, , rel_, 32, l, "memory") |
| 271 | __CMPXCHG_CASE(x, , rel_, 64, l, "memory") |
| 272 | __CMPXCHG_CASE(w, b, mb_, 8, al, "memory") |
| 273 | __CMPXCHG_CASE(w, h, mb_, 16, al, "memory") |
| 274 | __CMPXCHG_CASE(w, , mb_, 32, al, "memory") |
| 275 | __CMPXCHG_CASE(x, , mb_, 64, al, "memory") |
Will Deacon | c342f78 | 2015-04-23 20:08:49 +0100 | [diff] [blame] | 276 | |
Will Deacon | c342f78 | 2015-04-23 20:08:49 +0100 | [diff] [blame] | 277 | #undef __CMPXCHG_CASE |
| 278 | |
Will Deacon | e9a4b79 | 2015-05-14 18:05:50 +0100 | [diff] [blame] | 279 | #define __CMPXCHG_DBL(name, mb, cl...) \ |
Will Deacon | a48e61d | 2019-10-01 11:43:13 +0100 | [diff] [blame] | 280 | static __always_inline long \ |
| 281 | __lse__cmpxchg_double##name(unsigned long old1, \ |
Will Deacon | e9a4b79 | 2015-05-14 18:05:50 +0100 | [diff] [blame] | 282 | unsigned long old2, \ |
| 283 | unsigned long new1, \ |
| 284 | unsigned long new2, \ |
| 285 | volatile void *ptr) \ |
| 286 | { \ |
| 287 | unsigned long oldval1 = old1; \ |
| 288 | unsigned long oldval2 = old2; \ |
| 289 | register unsigned long x0 asm ("x0") = old1; \ |
| 290 | register unsigned long x1 asm ("x1") = old2; \ |
| 291 | register unsigned long x2 asm ("x2") = new1; \ |
| 292 | register unsigned long x3 asm ("x3") = new2; \ |
| 293 | register unsigned long x4 asm ("x4") = (unsigned long)ptr; \ |
| 294 | \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 295 | asm volatile( \ |
Sami Tolvanen | e0d5896 | 2019-10-31 12:57:05 -0700 | [diff] [blame] | 296 | __LSE_PREAMBLE \ |
Will Deacon | e9a4b79 | 2015-05-14 18:05:50 +0100 | [diff] [blame] | 297 | " casp" #mb "\t%[old1], %[old2], %[new1], %[new2], %[v]\n"\ |
| 298 | " eor %[old1], %[old1], %[oldval1]\n" \ |
| 299 | " eor %[old2], %[old2], %[oldval2]\n" \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 300 | " orr %[old1], %[old1], %[old2]" \ |
Will Deacon | 32c3fa7 | 2018-05-21 17:44:57 +0100 | [diff] [blame] | 301 | : [old1] "+&r" (x0), [old2] "+&r" (x1), \ |
Will Deacon | e9a4b79 | 2015-05-14 18:05:50 +0100 | [diff] [blame] | 302 | [v] "+Q" (*(unsigned long *)ptr) \ |
| 303 | : [new1] "r" (x2), [new2] "r" (x3), [ptr] "r" (x4), \ |
| 304 | [oldval1] "r" (oldval1), [oldval2] "r" (oldval2) \ |
Andrew Murray | addfc38 | 2019-08-28 18:50:07 +0100 | [diff] [blame] | 305 | : cl); \ |
Will Deacon | e9a4b79 | 2015-05-14 18:05:50 +0100 | [diff] [blame] | 306 | \ |
| 307 | return x0; \ |
| 308 | } |
| 309 | |
| 310 | __CMPXCHG_DBL( , ) |
| 311 | __CMPXCHG_DBL(_mb, al, "memory") |
| 312 | |
Will Deacon | e9a4b79 | 2015-05-14 18:05:50 +0100 | [diff] [blame] | 313 | #undef __CMPXCHG_DBL |
| 314 | |
Will Deacon | c0385b2 | 2015-02-03 12:39:03 +0000 | [diff] [blame] | 315 | #endif /* __ASM_ATOMIC_LSE_H */ |