Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Maciej W. Rozycki | edf7b93 | 2013-11-01 23:47:05 +0000 | [diff] [blame] | 2 | * Atomic operations that C can't guarantee us. Useful for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * resource counting etc.. |
| 4 | * |
| 5 | * But use these as seldom as possible since they are much more slower |
| 6 | * than regular operations. |
| 7 | * |
| 8 | * This file is subject to the terms and conditions of the GNU General Public |
| 9 | * License. See the file "COPYING" in the main directory of this archive |
| 10 | * for more details. |
| 11 | * |
Ralf Baechle | e303e08 | 2006-11-30 01:14:50 +0000 | [diff] [blame] | 12 | * Copyright (C) 1996, 97, 99, 2000, 03, 04, 06 by Ralf Baechle |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #ifndef _ASM_ATOMIC_H |
| 15 | #define _ASM_ATOMIC_H |
| 16 | |
Ralf Baechle | 192ef36 | 2006-07-07 14:07:18 +0100 | [diff] [blame] | 17 | #include <linux/irqflags.h> |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 18 | #include <linux/types.h> |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 19 | #include <asm/barrier.h> |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 20 | #include <asm/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/cpu-features.h> |
David Howells | b81947c | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 22 | #include <asm/cmpxchg.h> |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 23 | #include <asm/llsc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/war.h> |
| 25 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 26 | #define ATOMIC_INIT(i) { (i) } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * atomic_read - read atomic variable |
| 30 | * @v: pointer of type atomic_t |
| 31 | * |
| 32 | * Atomically reads the value of @v. |
| 33 | */ |
Peter Zijlstra | 62e8a32 | 2015-09-18 11:13:10 +0200 | [diff] [blame] | 34 | #define atomic_read(v) READ_ONCE((v)->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * atomic_set - set atomic variable |
| 38 | * @v: pointer of type atomic_t |
| 39 | * @i: required value |
| 40 | * |
| 41 | * Atomically sets the value of @v to @i. |
| 42 | */ |
Peter Zijlstra | 62e8a32 | 2015-09-18 11:13:10 +0200 | [diff] [blame] | 43 | #define atomic_set(v, i) WRITE_ONCE((v)->counter, (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 45 | #define ATOMIC_OP(op, c_op, asm_op) \ |
| 46 | static __inline__ void atomic_##op(int i, atomic_t * v) \ |
| 47 | { \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 48 | if (kernel_uses_llsc) { \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 49 | int temp; \ |
| 50 | \ |
Huacai Chen | e02e07e | 2019-01-15 16:04:54 +0800 | [diff] [blame] | 51 | loongson_llsc_mb(); \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 52 | __asm__ __volatile__( \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 53 | " .set push \n" \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 54 | " .set "MIPS_ISA_LEVEL" \n" \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 55 | "1: ll %0, %1 # atomic_" #op " \n" \ |
| 56 | " " #asm_op " %0, %2 \n" \ |
| 57 | " sc %0, %1 \n" \ |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 58 | "\t" __SC_BEQZ "%0, 1b \n" \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 59 | " .set pop \n" \ |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 60 | : "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (v->counter) \ |
Peter Zijlstra | 4234411 | 2019-06-13 15:43:20 +0200 | [diff] [blame] | 61 | : "Ir" (i) : __LLSC_CLOBBER); \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 62 | } else { \ |
| 63 | unsigned long flags; \ |
| 64 | \ |
| 65 | raw_local_irq_save(flags); \ |
| 66 | v->counter c_op i; \ |
| 67 | raw_local_irq_restore(flags); \ |
| 68 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 71 | #define ATOMIC_OP_RETURN(op, c_op, asm_op) \ |
Peter Zijlstra | 4ec4585 | 2016-04-18 01:15:25 +0200 | [diff] [blame] | 72 | static __inline__ int atomic_##op##_return_relaxed(int i, atomic_t * v) \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 73 | { \ |
| 74 | int result; \ |
| 75 | \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 76 | if (kernel_uses_llsc) { \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 77 | int temp; \ |
| 78 | \ |
Huacai Chen | e02e07e | 2019-01-15 16:04:54 +0800 | [diff] [blame] | 79 | loongson_llsc_mb(); \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 80 | __asm__ __volatile__( \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 81 | " .set push \n" \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 82 | " .set "MIPS_ISA_LEVEL" \n" \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 83 | "1: ll %1, %2 # atomic_" #op "_return \n" \ |
| 84 | " " #asm_op " %0, %1, %3 \n" \ |
| 85 | " sc %0, %2 \n" \ |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 86 | "\t" __SC_BEQZ "%0, 1b \n" \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 87 | " " #asm_op " %0, %1, %3 \n" \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 88 | " .set pop \n" \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 89 | : "=&r" (result), "=&r" (temp), \ |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 90 | "+" GCC_OFF_SMALL_ASM() (v->counter) \ |
Peter Zijlstra | 4234411 | 2019-06-13 15:43:20 +0200 | [diff] [blame] | 91 | : "Ir" (i) : __LLSC_CLOBBER); \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 92 | } else { \ |
| 93 | unsigned long flags; \ |
| 94 | \ |
| 95 | raw_local_irq_save(flags); \ |
| 96 | result = v->counter; \ |
| 97 | result c_op i; \ |
| 98 | v->counter = result; \ |
| 99 | raw_local_irq_restore(flags); \ |
| 100 | } \ |
| 101 | \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 102 | return result; \ |
| 103 | } |
| 104 | |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 105 | #define ATOMIC_FETCH_OP(op, c_op, asm_op) \ |
Peter Zijlstra | 4ec4585 | 2016-04-18 01:15:25 +0200 | [diff] [blame] | 106 | static __inline__ int atomic_fetch_##op##_relaxed(int i, atomic_t * v) \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 107 | { \ |
| 108 | int result; \ |
| 109 | \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 110 | if (kernel_uses_llsc) { \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 111 | int temp; \ |
| 112 | \ |
Huacai Chen | e02e07e | 2019-01-15 16:04:54 +0800 | [diff] [blame] | 113 | loongson_llsc_mb(); \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 114 | __asm__ __volatile__( \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 115 | " .set push \n" \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 116 | " .set "MIPS_ISA_LEVEL" \n" \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 117 | "1: ll %1, %2 # atomic_fetch_" #op " \n" \ |
| 118 | " " #asm_op " %0, %1, %3 \n" \ |
| 119 | " sc %0, %2 \n" \ |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 120 | "\t" __SC_BEQZ "%0, 1b \n" \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 121 | " .set pop \n" \ |
Paul Burton | cfd54de | 2018-08-17 15:36:24 -0700 | [diff] [blame] | 122 | " move %0, %1 \n" \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 123 | : "=&r" (result), "=&r" (temp), \ |
| 124 | "+" GCC_OFF_SMALL_ASM() (v->counter) \ |
Peter Zijlstra | 4234411 | 2019-06-13 15:43:20 +0200 | [diff] [blame] | 125 | : "Ir" (i) : __LLSC_CLOBBER); \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 126 | } else { \ |
| 127 | unsigned long flags; \ |
| 128 | \ |
| 129 | raw_local_irq_save(flags); \ |
| 130 | result = v->counter; \ |
| 131 | v->counter c_op i; \ |
| 132 | raw_local_irq_restore(flags); \ |
| 133 | } \ |
| 134 | \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 135 | return result; \ |
| 136 | } |
| 137 | |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 138 | #define ATOMIC_OPS(op, c_op, asm_op) \ |
| 139 | ATOMIC_OP(op, c_op, asm_op) \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 140 | ATOMIC_OP_RETURN(op, c_op, asm_op) \ |
| 141 | ATOMIC_FETCH_OP(op, c_op, asm_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Peter Zijlstra | ef31563 | 2014-03-26 17:56:43 +0100 | [diff] [blame] | 143 | ATOMIC_OPS(add, +=, addu) |
| 144 | ATOMIC_OPS(sub, -=, subu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Peter Zijlstra | 4ec4585 | 2016-04-18 01:15:25 +0200 | [diff] [blame] | 146 | #define atomic_add_return_relaxed atomic_add_return_relaxed |
| 147 | #define atomic_sub_return_relaxed atomic_sub_return_relaxed |
| 148 | #define atomic_fetch_add_relaxed atomic_fetch_add_relaxed |
| 149 | #define atomic_fetch_sub_relaxed atomic_fetch_sub_relaxed |
| 150 | |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 151 | #undef ATOMIC_OPS |
| 152 | #define ATOMIC_OPS(op, c_op, asm_op) \ |
| 153 | ATOMIC_OP(op, c_op, asm_op) \ |
| 154 | ATOMIC_FETCH_OP(op, c_op, asm_op) |
| 155 | |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 156 | ATOMIC_OPS(and, &=, and) |
| 157 | ATOMIC_OPS(or, |=, or) |
| 158 | ATOMIC_OPS(xor, ^=, xor) |
Peter Zijlstra | 27782f2 | 2014-04-23 19:51:36 +0200 | [diff] [blame] | 159 | |
Peter Zijlstra | 4ec4585 | 2016-04-18 01:15:25 +0200 | [diff] [blame] | 160 | #define atomic_fetch_and_relaxed atomic_fetch_and_relaxed |
| 161 | #define atomic_fetch_or_relaxed atomic_fetch_or_relaxed |
| 162 | #define atomic_fetch_xor_relaxed atomic_fetch_xor_relaxed |
| 163 | |
Peter Zijlstra | ef31563 | 2014-03-26 17:56:43 +0100 | [diff] [blame] | 164 | #undef ATOMIC_OPS |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 165 | #undef ATOMIC_FETCH_OP |
Peter Zijlstra | ef31563 | 2014-03-26 17:56:43 +0100 | [diff] [blame] | 166 | #undef ATOMIC_OP_RETURN |
| 167 | #undef ATOMIC_OP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | /* |
Arnaud Giersch | f10d14d | 2005-11-13 00:38:18 +0100 | [diff] [blame] | 170 | * atomic_sub_if_positive - conditionally subtract integer from atomic variable |
| 171 | * @i: integer value to subtract |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | * @v: pointer of type atomic_t |
| 173 | * |
Arnaud Giersch | f10d14d | 2005-11-13 00:38:18 +0100 | [diff] [blame] | 174 | * Atomically test @v and subtract @i if @v is greater or equal than @i. |
| 175 | * The function returns the old value of @v minus @i. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | */ |
| 177 | static __inline__ int atomic_sub_if_positive(int i, atomic_t * v) |
| 178 | { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 179 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 181 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 182 | |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 183 | if (kernel_uses_llsc) { |
Ralf Baechle | 915ec1e | 2009-01-12 00:52:18 +0000 | [diff] [blame] | 184 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Peter Zijlstra | 1c6c1ca | 2019-06-13 15:43:19 +0200 | [diff] [blame] | 186 | loongson_llsc_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | __asm__ __volatile__( |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 188 | " .set push \n" |
Markos Chandras | 0038df2 | 2015-01-06 11:09:24 +0000 | [diff] [blame] | 189 | " .set "MIPS_ISA_LEVEL" \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | "1: ll %1, %2 # atomic_sub_if_positive\n" |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 191 | " .set pop \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | " subu %0, %1, %3 \n" |
Joshua Kinard | a0a5ac3 | 2017-11-18 22:29:56 -0500 | [diff] [blame] | 193 | " move %1, %0 \n" |
Peter Zijlstra | 1c6c1ca | 2019-06-13 15:43:19 +0200 | [diff] [blame] | 194 | " bltz %0, 2f \n" |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 195 | " .set push \n" |
Paul Burton | cfd54de | 2018-08-17 15:36:24 -0700 | [diff] [blame] | 196 | " .set "MIPS_ISA_LEVEL" \n" |
Joshua Kinard | a0a5ac3 | 2017-11-18 22:29:56 -0500 | [diff] [blame] | 197 | " sc %1, %2 \n" |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 198 | "\t" __SC_BEQZ "%1, 1b \n" |
Peter Zijlstra | 1c6c1ca | 2019-06-13 15:43:19 +0200 | [diff] [blame] | 199 | "2: \n" |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 200 | " .set pop \n" |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 201 | : "=&r" (result), "=&r" (temp), |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 202 | "+" GCC_OFF_SMALL_ASM() (v->counter) |
Peter Zijlstra | 4234411 | 2019-06-13 15:43:20 +0200 | [diff] [blame] | 203 | : "Ir" (i) : __LLSC_CLOBBER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } else { |
| 205 | unsigned long flags; |
| 206 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 207 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | result = v->counter; |
| 209 | result -= i; |
| 210 | if (result >= 0) |
| 211 | v->counter = result; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 212 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 215 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return result; |
| 218 | } |
| 219 | |
Mathieu Desnoyers | e12f644 | 2007-05-08 00:34:24 -0700 | [diff] [blame] | 220 | #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) |
| 221 | #define atomic_xchg(v, new) (xchg(&((v)->counter), (new))) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | * atomic_dec_if_positive - decrement by 1 if old value positive |
| 225 | * @v: pointer of type atomic_t |
| 226 | */ |
| 227 | #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v) |
| 228 | |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 229 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | #define ATOMIC64_INIT(i) { (i) } |
| 232 | |
| 233 | /* |
| 234 | * atomic64_read - read atomic variable |
| 235 | * @v: pointer of type atomic64_t |
| 236 | * |
| 237 | */ |
Peter Zijlstra | 62e8a32 | 2015-09-18 11:13:10 +0200 | [diff] [blame] | 238 | #define atomic64_read(v) READ_ONCE((v)->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * atomic64_set - set atomic variable |
| 242 | * @v: pointer of type atomic64_t |
| 243 | * @i: required value |
| 244 | */ |
Peter Zijlstra | 62e8a32 | 2015-09-18 11:13:10 +0200 | [diff] [blame] | 245 | #define atomic64_set(v, i) WRITE_ONCE((v)->counter, (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 247 | #define ATOMIC64_OP(op, c_op, asm_op) \ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 248 | static __inline__ void atomic64_##op(s64 i, atomic64_t * v) \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 249 | { \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 250 | if (kernel_uses_llsc) { \ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 251 | s64 temp; \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 252 | \ |
Huacai Chen | e02e07e | 2019-01-15 16:04:54 +0800 | [diff] [blame] | 253 | loongson_llsc_mb(); \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 254 | __asm__ __volatile__( \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 255 | " .set push \n" \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 256 | " .set "MIPS_ISA_LEVEL" \n" \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 257 | "1: lld %0, %1 # atomic64_" #op " \n" \ |
| 258 | " " #asm_op " %0, %2 \n" \ |
| 259 | " scd %0, %1 \n" \ |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 260 | "\t" __SC_BEQZ "%0, 1b \n" \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 261 | " .set pop \n" \ |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 262 | : "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (v->counter) \ |
Peter Zijlstra | 4234411 | 2019-06-13 15:43:20 +0200 | [diff] [blame] | 263 | : "Ir" (i) : __LLSC_CLOBBER); \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 264 | } else { \ |
| 265 | unsigned long flags; \ |
| 266 | \ |
| 267 | raw_local_irq_save(flags); \ |
| 268 | v->counter c_op i; \ |
| 269 | raw_local_irq_restore(flags); \ |
| 270 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 273 | #define ATOMIC64_OP_RETURN(op, c_op, asm_op) \ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 274 | static __inline__ s64 atomic64_##op##_return_relaxed(s64 i, atomic64_t * v) \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 275 | { \ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 276 | s64 result; \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 277 | \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 278 | if (kernel_uses_llsc) { \ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 279 | s64 temp; \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 280 | \ |
Huacai Chen | e02e07e | 2019-01-15 16:04:54 +0800 | [diff] [blame] | 281 | loongson_llsc_mb(); \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 282 | __asm__ __volatile__( \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 283 | " .set push \n" \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 284 | " .set "MIPS_ISA_LEVEL" \n" \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 285 | "1: lld %1, %2 # atomic64_" #op "_return\n" \ |
| 286 | " " #asm_op " %0, %1, %3 \n" \ |
| 287 | " scd %0, %2 \n" \ |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 288 | "\t" __SC_BEQZ "%0, 1b \n" \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 289 | " " #asm_op " %0, %1, %3 \n" \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 290 | " .set pop \n" \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 291 | : "=&r" (result), "=&r" (temp), \ |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 292 | "+" GCC_OFF_SMALL_ASM() (v->counter) \ |
Peter Zijlstra | 4234411 | 2019-06-13 15:43:20 +0200 | [diff] [blame] | 293 | : "Ir" (i) : __LLSC_CLOBBER); \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 294 | } else { \ |
| 295 | unsigned long flags; \ |
| 296 | \ |
| 297 | raw_local_irq_save(flags); \ |
| 298 | result = v->counter; \ |
| 299 | result c_op i; \ |
| 300 | v->counter = result; \ |
| 301 | raw_local_irq_restore(flags); \ |
| 302 | } \ |
| 303 | \ |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 304 | return result; \ |
| 305 | } |
| 306 | |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 307 | #define ATOMIC64_FETCH_OP(op, c_op, asm_op) \ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 308 | static __inline__ s64 atomic64_fetch_##op##_relaxed(s64 i, atomic64_t * v) \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 309 | { \ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 310 | s64 result; \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 311 | \ |
Huacai Chen | db1ce3f | 2018-12-25 08:51:01 +0800 | [diff] [blame] | 312 | if (kernel_uses_llsc) { \ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 313 | s64 temp; \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 314 | \ |
Huacai Chen | e02e07e | 2019-01-15 16:04:54 +0800 | [diff] [blame] | 315 | loongson_llsc_mb(); \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 316 | __asm__ __volatile__( \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 317 | " .set push \n" \ |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 318 | " .set "MIPS_ISA_LEVEL" \n" \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 319 | "1: lld %1, %2 # atomic64_fetch_" #op "\n" \ |
| 320 | " " #asm_op " %0, %1, %3 \n" \ |
| 321 | " scd %0, %2 \n" \ |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 322 | "\t" __SC_BEQZ "%0, 1b \n" \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 323 | " move %0, %1 \n" \ |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 324 | " .set pop \n" \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 325 | : "=&r" (result), "=&r" (temp), \ |
| 326 | "+" GCC_OFF_SMALL_ASM() (v->counter) \ |
Peter Zijlstra | 4234411 | 2019-06-13 15:43:20 +0200 | [diff] [blame] | 327 | : "Ir" (i) : __LLSC_CLOBBER); \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 328 | } else { \ |
| 329 | unsigned long flags; \ |
| 330 | \ |
| 331 | raw_local_irq_save(flags); \ |
| 332 | result = v->counter; \ |
| 333 | v->counter c_op i; \ |
| 334 | raw_local_irq_restore(flags); \ |
| 335 | } \ |
| 336 | \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 337 | return result; \ |
| 338 | } |
| 339 | |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 340 | #define ATOMIC64_OPS(op, c_op, asm_op) \ |
| 341 | ATOMIC64_OP(op, c_op, asm_op) \ |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 342 | ATOMIC64_OP_RETURN(op, c_op, asm_op) \ |
| 343 | ATOMIC64_FETCH_OP(op, c_op, asm_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Peter Zijlstra | ef31563 | 2014-03-26 17:56:43 +0100 | [diff] [blame] | 345 | ATOMIC64_OPS(add, +=, daddu) |
| 346 | ATOMIC64_OPS(sub, -=, dsubu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Peter Zijlstra | 4ec4585 | 2016-04-18 01:15:25 +0200 | [diff] [blame] | 348 | #define atomic64_add_return_relaxed atomic64_add_return_relaxed |
| 349 | #define atomic64_sub_return_relaxed atomic64_sub_return_relaxed |
| 350 | #define atomic64_fetch_add_relaxed atomic64_fetch_add_relaxed |
| 351 | #define atomic64_fetch_sub_relaxed atomic64_fetch_sub_relaxed |
| 352 | |
Peter Zijlstra | ef31563 | 2014-03-26 17:56:43 +0100 | [diff] [blame] | 353 | #undef ATOMIC64_OPS |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 354 | #define ATOMIC64_OPS(op, c_op, asm_op) \ |
| 355 | ATOMIC64_OP(op, c_op, asm_op) \ |
| 356 | ATOMIC64_FETCH_OP(op, c_op, asm_op) |
| 357 | |
| 358 | ATOMIC64_OPS(and, &=, and) |
| 359 | ATOMIC64_OPS(or, |=, or) |
| 360 | ATOMIC64_OPS(xor, ^=, xor) |
| 361 | |
Peter Zijlstra | 4ec4585 | 2016-04-18 01:15:25 +0200 | [diff] [blame] | 362 | #define atomic64_fetch_and_relaxed atomic64_fetch_and_relaxed |
| 363 | #define atomic64_fetch_or_relaxed atomic64_fetch_or_relaxed |
| 364 | #define atomic64_fetch_xor_relaxed atomic64_fetch_xor_relaxed |
| 365 | |
Peter Zijlstra | 4edac52 | 2016-04-18 01:16:06 +0200 | [diff] [blame] | 366 | #undef ATOMIC64_OPS |
| 367 | #undef ATOMIC64_FETCH_OP |
Peter Zijlstra | ef31563 | 2014-03-26 17:56:43 +0100 | [diff] [blame] | 368 | #undef ATOMIC64_OP_RETURN |
| 369 | #undef ATOMIC64_OP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | /* |
Maciej W. Rozycki | ddb3108 | 2014-11-15 22:09:54 +0000 | [diff] [blame] | 372 | * atomic64_sub_if_positive - conditionally subtract integer from atomic |
| 373 | * variable |
Arnaud Giersch | f10d14d | 2005-11-13 00:38:18 +0100 | [diff] [blame] | 374 | * @i: integer value to subtract |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | * @v: pointer of type atomic64_t |
| 376 | * |
Arnaud Giersch | f10d14d | 2005-11-13 00:38:18 +0100 | [diff] [blame] | 377 | * Atomically test @v and subtract @i if @v is greater or equal than @i. |
| 378 | * The function returns the old value of @v minus @i. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | */ |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 380 | static __inline__ s64 atomic64_sub_if_positive(s64 i, atomic64_t * v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 382 | s64 result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 384 | smp_mb__before_llsc(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 385 | |
Joshua Kinard | 4936084 | 2017-11-18 23:00:51 -0500 | [diff] [blame] | 386 | if (kernel_uses_llsc) { |
Mark Rutland | d184cf1 | 2019-05-22 14:22:41 +0100 | [diff] [blame] | 387 | s64 temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | __asm__ __volatile__( |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 390 | " .set push \n" |
Markos Chandras | 0038df2 | 2015-01-06 11:09:24 +0000 | [diff] [blame] | 391 | " .set "MIPS_ISA_LEVEL" \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | "1: lld %1, %2 # atomic64_sub_if_positive\n" |
| 393 | " dsubu %0, %1, %3 \n" |
Joshua Kinard | a0a5ac3 | 2017-11-18 22:29:56 -0500 | [diff] [blame] | 394 | " move %1, %0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | " bltz %0, 1f \n" |
Joshua Kinard | a0a5ac3 | 2017-11-18 22:29:56 -0500 | [diff] [blame] | 396 | " scd %1, %2 \n" |
Paul Burton | 878f75c | 2019-10-01 21:53:05 +0000 | [diff] [blame^] | 397 | "\t" __SC_BEQZ "%1, 1b \n" |
Ralf Baechle | 5095202 | 2008-07-03 23:28:35 +0100 | [diff] [blame] | 398 | "1: \n" |
Paul Burton | 378ed6f | 2018-11-08 20:14:38 +0000 | [diff] [blame] | 399 | " .set pop \n" |
Maciej W. Rozycki | b0984c4 | 2014-11-15 22:08:48 +0000 | [diff] [blame] | 400 | : "=&r" (result), "=&r" (temp), |
Markos Chandras | 94bfb75 | 2015-01-26 12:44:11 +0000 | [diff] [blame] | 401 | "+" GCC_OFF_SMALL_ASM() (v->counter) |
Joshua Kinard | b4f2a17 | 2012-06-24 21:01:34 -0400 | [diff] [blame] | 402 | : "Ir" (i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } else { |
| 404 | unsigned long flags; |
| 405 | |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 406 | raw_local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | result = v->counter; |
| 408 | result -= i; |
| 409 | if (result >= 0) |
| 410 | v->counter = result; |
Ralf Baechle | 49edd09 | 2007-03-16 16:10:36 +0000 | [diff] [blame] | 411 | raw_local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 414 | smp_llsc_mb(); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | return result; |
| 417 | } |
| 418 | |
Mathieu Desnoyers | e12f644 | 2007-05-08 00:34:24 -0700 | [diff] [blame] | 419 | #define atomic64_cmpxchg(v, o, n) \ |
Atsushi Nemoto | 7b239bb | 2007-05-10 23:47:45 +0900 | [diff] [blame] | 420 | ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n))) |
Mathieu Desnoyers | e12f644 | 2007-05-08 00:34:24 -0700 | [diff] [blame] | 421 | #define atomic64_xchg(v, new) (xchg(&((v)->counter), (new))) |
| 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | * atomic64_dec_if_positive - decrement by 1 if old value positive |
| 425 | * @v: pointer of type atomic64_t |
| 426 | */ |
| 427 | #define atomic64_dec_if_positive(v) atomic64_sub_if_positive(1, v) |
| 428 | |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 429 | #endif /* CONFIG_64BIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | #endif /* _ASM_ATOMIC_H */ |