blob: 7c8816f7b7c4a8d86f571a9f034e8af122a0786d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Ralf Baechle102fa152007-02-16 17:18:50 +00006 * Copyright (c) 1994 - 1997, 99, 2000, 06, 07 Ralf Baechle (ralf@linux-mips.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (c) 1999, 2000 Silicon Graphics, Inc.
8 */
9#ifndef _ASM_BITOPS_H
10#define _ASM_BITOPS_H
11
Jiri Slaby06245172007-10-18 23:40:26 -070012#ifndef _LINUX_BITOPS_H
13#error only <linux/bitops.h> can be included directly
14#endif
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/compiler.h>
17#include <linux/types.h>
Ralf Baechle0004a9d2006-10-31 03:45:07 +000018#include <asm/barrier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/byteorder.h> /* sigh ... */
20#include <asm/cpu-features.h>
Ralf Baechle4ffd8b32006-11-30 01:14:50 +000021#include <asm/sgidefs.h>
22#include <asm/war.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ralf Baechle49a89ef2007-10-11 23:46:15 +010024#if _MIPS_SZLONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define SZLONG_LOG 5
26#define SZLONG_MASK 31UL
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000027#define __LL "ll "
28#define __SC "sc "
Ralf Baechle70342282013-01-22 12:59:30 +010029#define __INS "ins "
30#define __EXT "ext "
Ralf Baechle49a89ef2007-10-11 23:46:15 +010031#elif _MIPS_SZLONG == 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define SZLONG_LOG 6
33#define SZLONG_MASK 63UL
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000034#define __LL "lld "
35#define __SC "scd "
Ralf Baechle70342282013-01-22 12:59:30 +010036#define __INS "dins "
37#define __EXT "dext "
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#endif
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Jim Quinlan92d11592012-09-06 11:36:55 -040041 * These are the "slower" versions of the functions and are in bitops.c.
42 * These functions call raw_local_irq_{save,restore}().
43 */
44void __mips_set_bit(unsigned long nr, volatile unsigned long *addr);
45void __mips_clear_bit(unsigned long nr, volatile unsigned long *addr);
46void __mips_change_bit(unsigned long nr, volatile unsigned long *addr);
47int __mips_test_and_set_bit(unsigned long nr,
48 volatile unsigned long *addr);
49int __mips_test_and_set_bit_lock(unsigned long nr,
50 volatile unsigned long *addr);
51int __mips_test_and_clear_bit(unsigned long nr,
52 volatile unsigned long *addr);
53int __mips_test_and_change_bit(unsigned long nr,
54 volatile unsigned long *addr);
55
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * set_bit - Atomically set a bit in memory
59 * @nr: the bit to set
60 * @addr: the address to start counting from
61 *
62 * This function is atomic and may not be reordered. See __set_bit()
63 * if you do not require the atomic guarantees.
64 * Note that @nr may be almost arbitrarily large; this function is not
65 * restricted to acting on a single-word quantity.
66 */
67static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
68{
69 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Jim Quinlan9de79c52012-09-06 11:36:54 -040070 int bit = nr & SZLONG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 unsigned long temp;
72
David Daneyb791d112009-07-13 11:15:19 -070073 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +020075 " .set arch=r4000 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 "1: " __LL "%0, %1 # set_bit \n"
77 " or %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000078 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000080 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +000082 : "ir" (1UL << bit), "m" (*m));
Ralf Baechle102fa152007-02-16 17:18:50 +000083#ifdef CONFIG_CPU_MIPSR2
David Daneyb791d112009-07-13 11:15:19 -070084 } else if (kernel_uses_llsc && __builtin_constant_p(bit)) {
Ralf Baechle78373142010-10-29 19:08:24 +010085 do {
86 __asm__ __volatile__(
87 " " __LL "%0, %1 # set_bit \n"
88 " " __INS "%0, %3, %2, 1 \n"
89 " " __SC "%0, %1 \n"
90 : "=&r" (temp), "+m" (*m)
91 : "ir" (bit), "r" (~0));
92 } while (unlikely(!temp));
Ralf Baechle102fa152007-02-16 17:18:50 +000093#endif /* CONFIG_CPU_MIPSR2 */
David Daneyb791d112009-07-13 11:15:19 -070094 } else if (kernel_uses_llsc) {
Ralf Baechle78373142010-10-29 19:08:24 +010095 do {
96 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +020097 " .set arch=r4000 \n"
Ralf Baechle78373142010-10-29 19:08:24 +010098 " " __LL "%0, %1 # set_bit \n"
99 " or %0, %2 \n"
100 " " __SC "%0, %1 \n"
101 " .set mips0 \n"
102 : "=&r" (temp), "+m" (*m)
103 : "ir" (1UL << bit));
104 } while (unlikely(!temp));
Jim Quinlan92d11592012-09-06 11:36:55 -0400105 } else
106 __mips_set_bit(nr, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * clear_bit - Clears a bit in memory
111 * @nr: Bit to clear
112 * @addr: Address to start counting from
113 *
114 * clear_bit() is atomic and may not be reordered. However, it does
115 * not contain a memory barrier, so if it is used for locking purposes,
Peter Zijlstra91bbefe2014-03-13 19:00:36 +0100116 * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * in order to ensure changes are visible on other processors.
118 */
119static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
120{
121 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Jim Quinlan9de79c52012-09-06 11:36:54 -0400122 int bit = nr & SZLONG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned long temp;
124
David Daneyb791d112009-07-13 11:15:19 -0700125 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200127 " .set arch=r4000 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 "1: " __LL "%0, %1 # clear_bit \n"
129 " and %0, %2 \n"
130 " " __SC "%0, %1 \n"
131 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000132 " .set mips0 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100133 : "=&r" (temp), "+m" (*m)
134 : "ir" (~(1UL << bit)));
Ralf Baechle102fa152007-02-16 17:18:50 +0000135#ifdef CONFIG_CPU_MIPSR2
David Daneyb791d112009-07-13 11:15:19 -0700136 } else if (kernel_uses_llsc && __builtin_constant_p(bit)) {
Ralf Baechle78373142010-10-29 19:08:24 +0100137 do {
138 __asm__ __volatile__(
139 " " __LL "%0, %1 # clear_bit \n"
140 " " __INS "%0, $0, %2, 1 \n"
141 " " __SC "%0, %1 \n"
142 : "=&r" (temp), "+m" (*m)
143 : "ir" (bit));
144 } while (unlikely(!temp));
Ralf Baechle102fa152007-02-16 17:18:50 +0000145#endif /* CONFIG_CPU_MIPSR2 */
David Daneyb791d112009-07-13 11:15:19 -0700146 } else if (kernel_uses_llsc) {
Ralf Baechle78373142010-10-29 19:08:24 +0100147 do {
148 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200149 " .set arch=r4000 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100150 " " __LL "%0, %1 # clear_bit \n"
151 " and %0, %2 \n"
152 " " __SC "%0, %1 \n"
153 " .set mips0 \n"
154 : "=&r" (temp), "+m" (*m)
155 : "ir" (~(1UL << bit)));
156 } while (unlikely(!temp));
Jim Quinlan92d11592012-09-06 11:36:55 -0400157 } else
158 __mips_clear_bit(nr, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/*
Nick Piggin728697c2007-10-18 03:06:53 -0700162 * clear_bit_unlock - Clears a bit in memory
163 * @nr: Bit to clear
164 * @addr: Address to start counting from
165 *
166 * clear_bit() is atomic and implies release semantics before the memory
167 * operation. It can be used for an unlock.
168 */
169static inline void clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
170{
Peter Zijlstra91bbefe2014-03-13 19:00:36 +0100171 smp_mb__before_atomic();
Nick Piggin728697c2007-10-18 03:06:53 -0700172 clear_bit(nr, addr);
173}
174
175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * change_bit - Toggle a bit in memory
177 * @nr: Bit to change
178 * @addr: Address to start counting from
179 *
180 * change_bit() is atomic and may not be reordered.
181 * Note that @nr may be almost arbitrarily large; this function is not
182 * restricted to acting on a single-word quantity.
183 */
184static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
185{
Jim Quinlan9de79c52012-09-06 11:36:54 -0400186 int bit = nr & SZLONG_MASK;
Ralf Baechleb9611532007-03-05 00:56:15 +0000187
David Daneyb791d112009-07-13 11:15:19 -0700188 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
190 unsigned long temp;
191
192 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200193 " .set arch=r4000 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 "1: " __LL "%0, %1 # change_bit \n"
195 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000196 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000198 " .set mips0 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100199 : "=&r" (temp), "+m" (*m)
200 : "ir" (1UL << bit));
David Daneyb791d112009-07-13 11:15:19 -0700201 } else if (kernel_uses_llsc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
203 unsigned long temp;
204
Ralf Baechle78373142010-10-29 19:08:24 +0100205 do {
206 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200207 " .set arch=r4000 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100208 " " __LL "%0, %1 # change_bit \n"
209 " xor %0, %2 \n"
210 " " __SC "%0, %1 \n"
211 " .set mips0 \n"
212 : "=&r" (temp), "+m" (*m)
213 : "ir" (1UL << bit));
214 } while (unlikely(!temp));
Jim Quinlan92d11592012-09-06 11:36:55 -0400215 } else
216 __mips_change_bit(nr, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * test_and_set_bit - Set a bit and return its old value
221 * @nr: Bit to set
222 * @addr: Address to count from
223 *
224 * This operation is atomic and cannot be reordered.
225 * It also implies a memory barrier.
226 */
227static inline int test_and_set_bit(unsigned long nr,
228 volatile unsigned long *addr)
229{
Jim Quinlan9de79c52012-09-06 11:36:54 -0400230 int bit = nr & SZLONG_MASK;
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100231 unsigned long res;
Ralf Baechleb9611532007-03-05 00:56:15 +0000232
David Daneyf252ffd2010-01-08 17:17:43 -0800233 smp_mb__before_llsc();
Nick Pigginc8f30ae2007-10-18 03:06:52 -0700234
David Daneyb791d112009-07-13 11:15:19 -0700235 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100237 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200240 " .set arch=r4000 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 "1: " __LL "%0, %1 # test_and_set_bit \n"
242 " or %2, %0, %3 \n"
243 " " __SC "%2, %1 \n"
244 " beqzl %2, 1b \n"
245 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000246 " .set mips0 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100247 : "=&r" (temp), "+m" (*m), "=&r" (res)
248 : "r" (1UL << bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 : "memory");
David Daneyb791d112009-07-13 11:15:19 -0700250 } else if (kernel_uses_llsc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100252 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Ralf Baechle78373142010-10-29 19:08:24 +0100254 do {
255 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200256 " .set arch=r4000 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100257 " " __LL "%0, %1 # test_and_set_bit \n"
258 " or %2, %0, %3 \n"
259 " " __SC "%2, %1 \n"
260 " .set mips0 \n"
261 : "=&r" (temp), "+m" (*m), "=&r" (res)
262 : "r" (1UL << bit)
263 : "memory");
264 } while (unlikely(!res));
265
266 res = temp & (1UL << bit);
Jim Quinlan92d11592012-09-06 11:36:55 -0400267 } else
268 res = __mips_test_and_set_bit(nr, addr);
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000269
Ralf Baechle17099b12007-07-14 13:24:05 +0100270 smp_llsc_mb();
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100271
272 return res != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/*
Nick Piggin728697c2007-10-18 03:06:53 -0700276 * test_and_set_bit_lock - Set a bit and return its old value
277 * @nr: Bit to set
278 * @addr: Address to count from
279 *
280 * This operation is atomic and implies acquire ordering semantics
281 * after the memory operation.
282 */
283static inline int test_and_set_bit_lock(unsigned long nr,
284 volatile unsigned long *addr)
285{
Jim Quinlan9de79c52012-09-06 11:36:54 -0400286 int bit = nr & SZLONG_MASK;
Nick Piggin728697c2007-10-18 03:06:53 -0700287 unsigned long res;
288
David Daneyb791d112009-07-13 11:15:19 -0700289 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Nick Piggin728697c2007-10-18 03:06:53 -0700290 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
291 unsigned long temp;
292
293 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200294 " .set arch=r4000 \n"
Nick Piggin728697c2007-10-18 03:06:53 -0700295 "1: " __LL "%0, %1 # test_and_set_bit \n"
296 " or %2, %0, %3 \n"
297 " " __SC "%2, %1 \n"
298 " beqzl %2, 1b \n"
299 " and %2, %0, %3 \n"
300 " .set mips0 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100301 : "=&r" (temp), "+m" (*m), "=&r" (res)
302 : "r" (1UL << bit)
Nick Piggin728697c2007-10-18 03:06:53 -0700303 : "memory");
David Daneyb791d112009-07-13 11:15:19 -0700304 } else if (kernel_uses_llsc) {
Nick Piggin728697c2007-10-18 03:06:53 -0700305 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
306 unsigned long temp;
307
Ralf Baechle78373142010-10-29 19:08:24 +0100308 do {
309 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200310 " .set arch=r4000 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100311 " " __LL "%0, %1 # test_and_set_bit \n"
312 " or %2, %0, %3 \n"
313 " " __SC "%2, %1 \n"
314 " .set mips0 \n"
315 : "=&r" (temp), "+m" (*m), "=&r" (res)
316 : "r" (1UL << bit)
317 : "memory");
318 } while (unlikely(!res));
319
320 res = temp & (1UL << bit);
Jim Quinlan92d11592012-09-06 11:36:55 -0400321 } else
322 res = __mips_test_and_set_bit_lock(nr, addr);
Nick Piggin728697c2007-10-18 03:06:53 -0700323
324 smp_llsc_mb();
325
326 return res != 0;
327}
328/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * test_and_clear_bit - Clear a bit and return its old value
330 * @nr: Bit to clear
331 * @addr: Address to count from
332 *
333 * This operation is atomic and cannot be reordered.
334 * It also implies a memory barrier.
335 */
336static inline int test_and_clear_bit(unsigned long nr,
337 volatile unsigned long *addr)
338{
Jim Quinlan9de79c52012-09-06 11:36:54 -0400339 int bit = nr & SZLONG_MASK;
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100340 unsigned long res;
Ralf Baechleb9611532007-03-05 00:56:15 +0000341
David Daneyf252ffd2010-01-08 17:17:43 -0800342 smp_mb__before_llsc();
Nick Pigginc8f30ae2007-10-18 03:06:52 -0700343
David Daneyb791d112009-07-13 11:15:19 -0700344 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Atsushi Nemoto8e09ffb2007-06-14 00:56:31 +0900346 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200349 " .set arch=r4000 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 "1: " __LL "%0, %1 # test_and_clear_bit \n"
351 " or %2, %0, %3 \n"
352 " xor %2, %3 \n"
Ralf Baechle70342282013-01-22 12:59:30 +0100353 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 " beqzl %2, 1b \n"
355 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000356 " .set mips0 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100357 : "=&r" (temp), "+m" (*m), "=&r" (res)
358 : "r" (1UL << bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 : "memory");
Ralf Baechle102fa152007-02-16 17:18:50 +0000360#ifdef CONFIG_CPU_MIPSR2
David Daneyb791d112009-07-13 11:15:19 -0700361 } else if (kernel_uses_llsc && __builtin_constant_p(nr)) {
Ralf Baechle102fa152007-02-16 17:18:50 +0000362 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100363 unsigned long temp;
Ralf Baechle102fa152007-02-16 17:18:50 +0000364
Ralf Baechle78373142010-10-29 19:08:24 +0100365 do {
366 __asm__ __volatile__(
Ralf Baechle70342282013-01-22 12:59:30 +0100367 " " __LL "%0, %1 # test_and_clear_bit \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100368 " " __EXT "%2, %0, %3, 1 \n"
Ralf Baechle70342282013-01-22 12:59:30 +0100369 " " __INS "%0, $0, %3, 1 \n"
370 " " __SC "%0, %1 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100371 : "=&r" (temp), "+m" (*m), "=&r" (res)
372 : "ir" (bit)
373 : "memory");
374 } while (unlikely(!temp));
Ralf Baechle102fa152007-02-16 17:18:50 +0000375#endif
David Daneyb791d112009-07-13 11:15:19 -0700376 } else if (kernel_uses_llsc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100378 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Ralf Baechle78373142010-10-29 19:08:24 +0100380 do {
381 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200382 " .set arch=r4000 \n"
Ralf Baechle70342282013-01-22 12:59:30 +0100383 " " __LL "%0, %1 # test_and_clear_bit \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100384 " or %2, %0, %3 \n"
385 " xor %2, %3 \n"
Ralf Baechle70342282013-01-22 12:59:30 +0100386 " " __SC "%2, %1 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100387 " .set mips0 \n"
388 : "=&r" (temp), "+m" (*m), "=&r" (res)
389 : "r" (1UL << bit)
390 : "memory");
391 } while (unlikely(!res));
392
393 res = temp & (1UL << bit);
Jim Quinlan92d11592012-09-06 11:36:55 -0400394 } else
395 res = __mips_test_and_clear_bit(nr, addr);
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000396
Ralf Baechle17099b12007-07-14 13:24:05 +0100397 smp_llsc_mb();
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100398
399 return res != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * test_and_change_bit - Change a bit and return its old value
404 * @nr: Bit to change
405 * @addr: Address to count from
406 *
407 * This operation is atomic and cannot be reordered.
408 * It also implies a memory barrier.
409 */
410static inline int test_and_change_bit(unsigned long nr,
411 volatile unsigned long *addr)
412{
Jim Quinlan9de79c52012-09-06 11:36:54 -0400413 int bit = nr & SZLONG_MASK;
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100414 unsigned long res;
Ralf Baechleb9611532007-03-05 00:56:15 +0000415
David Daneyf252ffd2010-01-08 17:17:43 -0800416 smp_mb__before_llsc();
Nick Pigginc8f30ae2007-10-18 03:06:52 -0700417
David Daneyb791d112009-07-13 11:15:19 -0700418 if (kernel_uses_llsc && R10000_LLSC_WAR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100420 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200423 " .set arch=r4000 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000424 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000426 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 " beqzl %2, 1b \n"
428 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000429 " .set mips0 \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100430 : "=&r" (temp), "+m" (*m), "=&r" (res)
431 : "r" (1UL << bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 : "memory");
David Daneyb791d112009-07-13 11:15:19 -0700433 } else if (kernel_uses_llsc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100435 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Ralf Baechle78373142010-10-29 19:08:24 +0100437 do {
438 __asm__ __volatile__(
Ralf Baechlea809d462014-03-30 13:20:10 +0200439 " .set arch=r4000 \n"
Ralf Baechle70342282013-01-22 12:59:30 +0100440 " " __LL "%0, %1 # test_and_change_bit \n"
Ralf Baechle78373142010-10-29 19:08:24 +0100441 " xor %2, %0, %3 \n"
442 " " __SC "\t%2, %1 \n"
443 " .set mips0 \n"
444 : "=&r" (temp), "+m" (*m), "=&r" (res)
445 : "r" (1UL << bit)
446 : "memory");
447 } while (unlikely(!res));
448
449 res = temp & (1UL << bit);
Jim Quinlan92d11592012-09-06 11:36:55 -0400450 } else
451 res = __mips_test_and_change_bit(nr, addr);
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000452
Ralf Baechle17099b12007-07-14 13:24:05 +0100453 smp_llsc_mb();
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100454
455 return res != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800458#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Ralf Baechle65903262005-07-12 12:50:30 +0000460/*
Nick Piggin728697c2007-10-18 03:06:53 -0700461 * __clear_bit_unlock - Clears a bit in memory
462 * @nr: Bit to clear
463 * @addr: Address to start counting from
464 *
465 * __clear_bit() is non-atomic and implies release semantics before the memory
466 * operation. It can be used for an unlock if no other CPUs can concurrently
467 * modify other bits in the word.
468 */
469static inline void __clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
470{
471 smp_mb();
472 __clear_bit(nr, addr);
473}
474
475/*
Ralf Baechle65903262005-07-12 12:50:30 +0000476 * Return the bit position (0..63) of the most significant 1 bit in a word
477 * Returns -1 if no 1 bit exists
478 */
Ralf Baechle48162272008-10-28 09:40:35 +0000479static inline unsigned long __fls(unsigned long word)
Ralf Baechle65903262005-07-12 12:50:30 +0000480{
Ralf Baechle48162272008-10-28 09:40:35 +0000481 int num;
Ralf Baechle65903262005-07-12 12:50:30 +0000482
Ralf Baechle48162272008-10-28 09:40:35 +0000483 if (BITS_PER_LONG == 32 &&
Ralf Baechle47740eb2009-04-19 03:21:22 +0200484 __builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100485 __asm__(
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100486 " .set push \n"
487 " .set mips32 \n"
488 " clz %0, %1 \n"
489 " .set pop \n"
Ralf Baechle48162272008-10-28 09:40:35 +0000490 : "=r" (num)
491 : "r" (word));
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100492
Ralf Baechle48162272008-10-28 09:40:35 +0000493 return 31 - num;
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100494 }
495
Ralf Baechle48162272008-10-28 09:40:35 +0000496 if (BITS_PER_LONG == 64 &&
497 __builtin_constant_p(cpu_has_mips64) && cpu_has_mips64) {
498 __asm__(
499 " .set push \n"
500 " .set mips64 \n"
501 " dclz %0, %1 \n"
502 " .set pop \n"
503 : "=r" (num)
504 : "r" (word));
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100505
Ralf Baechle48162272008-10-28 09:40:35 +0000506 return 63 - num;
507 }
Ralf Baechle65903262005-07-12 12:50:30 +0000508
Ralf Baechle48162272008-10-28 09:40:35 +0000509 num = BITS_PER_LONG - 1;
510
511#if BITS_PER_LONG == 64
512 if (!(word & (~0ul << 32))) {
513 num -= 32;
514 word <<= 32;
515 }
516#endif
517 if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
518 num -= 16;
519 word <<= 16;
520 }
521 if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
522 num -= 8;
523 word <<= 8;
524 }
525 if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
526 num -= 4;
527 word <<= 4;
528 }
529 if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
530 num -= 2;
531 word <<= 2;
532 }
533 if (!(word & (~0ul << (BITS_PER_LONG-1))))
534 num -= 1;
535 return num;
Ralf Baechle65903262005-07-12 12:50:30 +0000536}
Ralf Baechle65903262005-07-12 12:50:30 +0000537
538/*
539 * __ffs - find first bit in word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * @word: The word to search
541 *
Ralf Baechle65903262005-07-12 12:50:30 +0000542 * Returns 0..SZLONG-1
543 * Undefined if no bit exists, so code should check against 0 first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Ralf Baechle65903262005-07-12 12:50:30 +0000545static inline unsigned long __ffs(unsigned long word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Ralf Baechleddc0d002008-05-04 14:53:53 +0100547 return __fls(word & -word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
Atsushi Nemotobc818242006-04-17 21:19:12 +0900551 * fls - find last bit set.
552 * @word: The word to search
553 *
554 * This is defined the same way as ffs.
555 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
556 */
Ralf Baechle48162272008-10-28 09:40:35 +0000557static inline int fls(int x)
Atsushi Nemotobc818242006-04-17 21:19:12 +0900558{
Ralf Baechle48162272008-10-28 09:40:35 +0000559 int r;
Atsushi Nemotobc818242006-04-17 21:19:12 +0900560
Ralf Baechle47740eb2009-04-19 03:21:22 +0200561 if (__builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) {
Ralf Baechle48162272008-10-28 09:40:35 +0000562 __asm__("clz %0, %1" : "=r" (x) : "r" (x));
563
564 return 32 - x;
565 }
566
567 r = 32;
568 if (!x)
569 return 0;
570 if (!(x & 0xffff0000u)) {
571 x <<= 16;
572 r -= 16;
573 }
574 if (!(x & 0xff000000u)) {
575 x <<= 8;
576 r -= 8;
577 }
578 if (!(x & 0xf0000000u)) {
579 x <<= 4;
580 r -= 4;
581 }
582 if (!(x & 0xc0000000u)) {
583 x <<= 2;
584 r -= 2;
585 }
586 if (!(x & 0x80000000u)) {
587 x <<= 1;
588 r -= 1;
589 }
590 return r;
Atsushi Nemotobc818242006-04-17 21:19:12 +0900591}
592
Atsushi Nemotobc818242006-04-17 21:19:12 +0900593#include <asm-generic/bitops/fls64.h>
Atsushi Nemotobc818242006-04-17 21:19:12 +0900594
595/*
Ralf Baechle65903262005-07-12 12:50:30 +0000596 * ffs - find first bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * @word: The word to search
598 *
Atsushi Nemotobc818242006-04-17 21:19:12 +0900599 * This is defined the same way as
600 * the libc and compiler builtin ffs routines, therefore
601 * differs in spirit from the above ffz (man ffs).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Atsushi Nemotobc818242006-04-17 21:19:12 +0900603static inline int ffs(int word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Ralf Baechle65903262005-07-12 12:50:30 +0000605 if (!word)
606 return 0;
607
Atsushi Nemotobc818242006-04-17 21:19:12 +0900608 return fls(word & -word);
Ralf Baechle65903262005-07-12 12:50:30 +0000609}
Ralf Baechle2caf1902006-01-30 17:14:41 +0000610
Atsushi Nemotobc818242006-04-17 21:19:12 +0900611#include <asm-generic/bitops/ffz.h>
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800612#include <asm-generic/bitops/find.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614#ifdef __KERNEL__
615
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800616#include <asm-generic/bitops/sched.h>
David Daney1a403d12010-06-25 16:46:07 -0700617
618#include <asm/arch_hweight.h>
619#include <asm-generic/bitops/const_hweight.h>
620
Akinobu Mita861b5ae2011-03-23 16:42:02 -0700621#include <asm-generic/bitops/le.h>
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800622#include <asm-generic/bitops/ext2-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624#endif /* __KERNEL__ */
625
626#endif /* _ASM_BITOPS_H */