blob: 0df8e95e37151df000a8ca0610e28d04dfbb4cff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* bitops.h: bit operations for the Fujitsu FR-V CPUs
2 *
3 * For an explanation of how atomic ops work in this arch, see:
Adrian Bunk0868ff72008-02-03 15:54:28 +02004 * Documentation/frv/atomic-ops.txt
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14#ifndef _ASM_BITOPS_H
15#define _ASM_BITOPS_H
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/compiler.h>
18#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#ifdef __KERNEL__
21
Jiri Slaby06245172007-10-18 23:40:26 -070022#ifndef _LINUX_BITOPS_H
23#error only <linux/bitops.h> can be included directly
24#endif
25
Akinobu Mita1f6d7a92006-03-26 01:39:22 -080026#include <asm-generic/bitops/ffz.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Peter Zijlstrab0d80032015-04-24 00:49:20 +020028#include <asm/atomic.h>
Mathieu Desnoyers6784fd52008-02-08 15:00:45 -080029
Justin Chend2f11bf2009-06-11 13:04:59 +010030static inline int test_and_clear_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Peter Zijlstrab0d80032015-04-24 00:49:20 +020032 unsigned int *ptr = (void *)addr;
33 unsigned int mask = 1UL << (nr & 31);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 ptr += nr >> 5;
Peter Zijlstrab0d80032015-04-24 00:49:20 +020035 return (__atomic32_fetch_and(~mask, ptr) & mask) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Justin Chend2f11bf2009-06-11 13:04:59 +010038static inline int test_and_set_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Peter Zijlstrab0d80032015-04-24 00:49:20 +020040 unsigned int *ptr = (void *)addr;
41 unsigned int mask = 1UL << (nr & 31);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 ptr += nr >> 5;
Peter Zijlstrab0d80032015-04-24 00:49:20 +020043 return (__atomic32_fetch_or(mask, ptr) & mask) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Justin Chend2f11bf2009-06-11 13:04:59 +010046static inline int test_and_change_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Peter Zijlstrab0d80032015-04-24 00:49:20 +020048 unsigned int *ptr = (void *)addr;
49 unsigned int mask = 1UL << (nr & 31);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 ptr += nr >> 5;
Peter Zijlstrab0d80032015-04-24 00:49:20 +020051 return (__atomic32_fetch_xor(mask, ptr) & mask) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Justin Chend2f11bf2009-06-11 13:04:59 +010054static inline void clear_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 test_and_clear_bit(nr, addr);
57}
58
Justin Chend2f11bf2009-06-11 13:04:59 +010059static inline void set_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 test_and_set_bit(nr, addr);
62}
63
Justin Chend2f11bf2009-06-11 13:04:59 +010064static inline void change_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 test_and_change_bit(nr, addr);
67}
68
Justin Chend2f11bf2009-06-11 13:04:59 +010069static inline void __clear_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 volatile unsigned long *a = addr;
72 int mask;
73
74 a += nr >> 5;
75 mask = 1 << (nr & 31);
76 *a &= ~mask;
77}
78
Justin Chend2f11bf2009-06-11 13:04:59 +010079static inline void __set_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 volatile unsigned long *a = addr;
82 int mask;
83
84 a += nr >> 5;
85 mask = 1 << (nr & 31);
86 *a |= mask;
87}
88
Justin Chend2f11bf2009-06-11 13:04:59 +010089static inline void __change_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 volatile unsigned long *a = addr;
92 int mask;
93
94 a += nr >> 5;
95 mask = 1 << (nr & 31);
96 *a ^= mask;
97}
98
Justin Chend2f11bf2009-06-11 13:04:59 +010099static inline int __test_and_clear_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 volatile unsigned long *a = addr;
102 int mask, retval;
103
104 a += nr >> 5;
105 mask = 1 << (nr & 31);
106 retval = (mask & *a) != 0;
107 *a &= ~mask;
108 return retval;
109}
110
Justin Chend2f11bf2009-06-11 13:04:59 +0100111static inline int __test_and_set_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 volatile unsigned long *a = addr;
114 int mask, retval;
115
116 a += nr >> 5;
117 mask = 1 << (nr & 31);
118 retval = (mask & *a) != 0;
119 *a |= mask;
120 return retval;
121}
122
Justin Chend2f11bf2009-06-11 13:04:59 +0100123static inline int __test_and_change_bit(unsigned long nr, volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 volatile unsigned long *a = addr;
126 int mask, retval;
127
128 a += nr >> 5;
129 mask = 1 << (nr & 31);
130 retval = (mask & *a) != 0;
131 *a ^= mask;
132 return retval;
133}
134
135/*
136 * This routine doesn't need to be atomic.
137 */
Justin Chend2f11bf2009-06-11 13:04:59 +0100138static inline int
139__constant_test_bit(unsigned long nr, const volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
142}
143
Justin Chend2f11bf2009-06-11 13:04:59 +0100144static inline int __test_bit(unsigned long nr, const volatile void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int * a = (int *) addr;
147 int mask;
148
149 a += nr >> 5;
150 mask = 1 << (nr & 0x1f);
151 return ((mask & *a) != 0);
152}
153
154#define test_bit(nr,addr) \
155(__builtin_constant_p(nr) ? \
156 __constant_test_bit((nr),(addr)) : \
157 __test_bit((nr),(addr)))
158
Akinobu Mita1f6d7a92006-03-26 01:39:22 -0800159#include <asm-generic/bitops/find.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howells92fc7072006-09-25 23:32:07 -0700161/**
162 * fls - find last bit set
163 * @x: the word to search
164 *
165 * This is defined the same way as ffs:
166 * - return 32..1 to indicate bit 31..0 most significant bit set
167 * - return 0 to indicate no bits set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
169#define fls(x) \
170({ \
171 int bit; \
172 \
David Howells92fc7072006-09-25 23:32:07 -0700173 asm(" subcc %1,gr0,gr0,icc0 \n" \
174 " ckne icc0,cc4 \n" \
175 " cscan.p %1,gr0,%0 ,cc4,#1 \n" \
176 " csub %0,%0,%0 ,cc4,#0 \n" \
177 " csub %2,%0,%0 ,cc4,#1 \n" \
178 : "=&r"(bit) \
179 : "r"(x), "r"(32) \
180 : "icc0", "cc4" \
181 ); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 \
David Howells92fc7072006-09-25 23:32:07 -0700183 bit; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David Howellsa8ad27d2006-09-25 23:32:08 -0700186/**
187 * fls64 - find last bit set in a 64-bit value
188 * @n: the value to search
189 *
190 * This is defined the same way as ffs:
191 * - return 64..1 to indicate bit 63..0 most significant bit set
192 * - return 0 to indicate no bits set
193 */
194static inline __attribute__((const))
195int fls64(u64 n)
196{
197 union {
198 u64 ll;
199 struct { u32 h, l; };
200 } _;
201 int bit, x, y;
202
203 _.ll = n;
204
205 asm(" subcc.p %3,gr0,gr0,icc0 \n"
206 " subcc %4,gr0,gr0,icc1 \n"
207 " ckne icc0,cc4 \n"
208 " ckne icc1,cc5 \n"
209 " norcr cc4,cc5,cc6 \n"
210 " csub.p %0,%0,%0 ,cc6,1 \n"
211 " orcr cc5,cc4,cc4 \n"
212 " andcr cc4,cc5,cc4 \n"
213 " cscan.p %3,gr0,%0 ,cc4,0 \n"
214 " setlos #64,%1 \n"
215 " cscan.p %4,gr0,%0 ,cc4,1 \n"
216 " setlos #32,%2 \n"
217 " csub.p %1,%0,%0 ,cc4,0 \n"
218 " csub %2,%0,%0 ,cc4,1 \n"
219 : "=&r"(bit), "=r"(x), "=r"(y)
220 : "0r"(_.h), "r"(_.l)
221 : "icc0", "icc1", "cc4", "cc5", "cc6"
222 );
223 return bit;
224
225}
226
David Howellscf1344832006-09-25 23:32:09 -0700227/**
228 * ffs - find first bit set
229 * @x: the word to search
230 *
231 * - return 32..1 to indicate bit 31..0 most least significant bit set
232 * - return 0 to indicate no bits set
233 */
234static inline __attribute__((const))
235int ffs(int x)
236{
237 /* Note: (x & -x) gives us a mask that is the least significant
238 * (rightmost) 1-bit of the value in x.
239 */
240 return fls(x & -x);
241}
242
243/**
244 * __ffs - find first bit set
245 * @x: the word to search
246 *
247 * - return 31..0 to indicate bit 31..0 most least significant bit set
248 * - if no bits are set in x, the result is undefined
249 */
250static inline __attribute__((const))
251int __ffs(unsigned long x)
252{
253 int bit;
254 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x & -x));
255 return 31 - bit;
256}
257
Rusty Russellee38e5142009-01-03 16:14:05 +1030258/**
259 * __fls - find last (most-significant) set bit in a long word
260 * @word: the word to search
261 *
262 * Undefined if no set bit exists, so code should check against 0 first.
263 */
264static inline unsigned long __fls(unsigned long word)
265{
266 unsigned long bit;
267 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(word));
268 return bit;
269}
270
David Howellsf0d1b0b2006-12-08 02:37:49 -0800271/*
272 * special slimline version of fls() for calculating ilog2_u32()
273 * - note: no protection against n == 0
274 */
275#define ARCH_HAS_ILOG2_U32
276static inline __attribute__((const))
277int __ilog2_u32(u32 n)
278{
279 int bit;
280 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(n));
281 return 31 - bit;
282}
283
284/*
285 * special slimline version of fls64() for calculating ilog2_u64()
286 * - note: no protection against n == 0
287 */
288#define ARCH_HAS_ILOG2_U64
289static inline __attribute__((const))
290int __ilog2_u64(u64 n)
291{
292 union {
293 u64 ll;
294 struct { u32 h, l; };
295 } _;
296 int bit, x, y;
297
298 _.ll = n;
299
300 asm(" subcc %3,gr0,gr0,icc0 \n"
301 " ckeq icc0,cc4 \n"
302 " cscan.p %3,gr0,%0 ,cc4,0 \n"
303 " setlos #63,%1 \n"
304 " cscan.p %4,gr0,%0 ,cc4,1 \n"
305 " setlos #31,%2 \n"
306 " csub.p %1,%0,%0 ,cc4,0 \n"
307 " csub %2,%0,%0 ,cc4,1 \n"
308 : "=&r"(bit), "=r"(x), "=r"(y)
309 : "0r"(_.h), "r"(_.l)
310 : "icc0", "cc4"
311 );
312 return bit;
313}
314
Akinobu Mita1f6d7a92006-03-26 01:39:22 -0800315#include <asm-generic/bitops/sched.h>
316#include <asm-generic/bitops/hweight.h>
Nick Piggin26333572007-10-18 03:06:39 -0700317#include <asm-generic/bitops/lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Akinobu Mita861b5ae2011-03-23 16:42:02 -0700319#include <asm-generic/bitops/le.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Akinobu Mita148817b2011-07-26 16:09:04 -0700321#include <asm-generic/bitops/ext2-atomic-setbit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#endif /* __KERNEL__ */
324
325#endif /* _ASM_BITOPS_H */