Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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 Bunk | 0868ff7 | 2008-02-03 15:54:28 +0200 | [diff] [blame] | 4 | * Documentation/frv/atomic-ops.txt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/compiler.h> |
| 18 | #include <asm/byteorder.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #ifdef __KERNEL__ |
| 21 | |
Jiri Slaby | 0624517 | 2007-10-18 23:40:26 -0700 | [diff] [blame] | 22 | #ifndef _LINUX_BITOPS_H |
| 23 | #error only <linux/bitops.h> can be included directly |
| 24 | #endif |
| 25 | |
Akinobu Mita | 1f6d7a9 | 2006-03-26 01:39:22 -0800 | [diff] [blame] | 26 | #include <asm-generic/bitops/ffz.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Peter Zijlstra | b0d8003 | 2015-04-24 00:49:20 +0200 | [diff] [blame] | 28 | #include <asm/atomic.h> |
Mathieu Desnoyers | 6784fd5 | 2008-02-08 15:00:45 -0800 | [diff] [blame] | 29 | |
Justin Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 30 | static inline int test_and_clear_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Peter Zijlstra | b0d8003 | 2015-04-24 00:49:20 +0200 | [diff] [blame] | 32 | unsigned int *ptr = (void *)addr; |
| 33 | unsigned int mask = 1UL << (nr & 31); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | ptr += nr >> 5; |
Peter Zijlstra | b0d8003 | 2015-04-24 00:49:20 +0200 | [diff] [blame] | 35 | return (__atomic32_fetch_and(~mask, ptr) & mask) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Justin Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 38 | static inline int test_and_set_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Peter Zijlstra | b0d8003 | 2015-04-24 00:49:20 +0200 | [diff] [blame] | 40 | unsigned int *ptr = (void *)addr; |
| 41 | unsigned int mask = 1UL << (nr & 31); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | ptr += nr >> 5; |
Peter Zijlstra | b0d8003 | 2015-04-24 00:49:20 +0200 | [diff] [blame] | 43 | return (__atomic32_fetch_or(mask, ptr) & mask) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Justin Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 46 | static inline int test_and_change_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Peter Zijlstra | b0d8003 | 2015-04-24 00:49:20 +0200 | [diff] [blame] | 48 | unsigned int *ptr = (void *)addr; |
| 49 | unsigned int mask = 1UL << (nr & 31); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | ptr += nr >> 5; |
Peter Zijlstra | b0d8003 | 2015-04-24 00:49:20 +0200 | [diff] [blame] | 51 | return (__atomic32_fetch_xor(mask, ptr) & mask) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Justin Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 54 | static inline void clear_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
| 56 | test_and_clear_bit(nr, addr); |
| 57 | } |
| 58 | |
Justin Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 59 | static inline void set_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | test_and_set_bit(nr, addr); |
| 62 | } |
| 63 | |
Justin Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 64 | static inline void change_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
| 66 | test_and_change_bit(nr, addr); |
| 67 | } |
| 68 | |
Justin Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 69 | static inline void __clear_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
| 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 Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 79 | static inline void __set_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 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 Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 89 | static inline void __change_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 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 Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 99 | static inline int __test_and_clear_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 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 Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 111 | static inline int __test_and_set_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 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 Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 123 | static inline int __test_and_change_bit(unsigned long nr, volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 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 Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 138 | static inline int |
| 139 | __constant_test_bit(unsigned long nr, const volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0; |
| 142 | } |
| 143 | |
Justin Chen | d2f11bf | 2009-06-11 13:04:59 +0100 | [diff] [blame] | 144 | static inline int __test_bit(unsigned long nr, const volatile void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 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 Mita | 1f6d7a9 | 2006-03-26 01:39:22 -0800 | [diff] [blame] | 159 | #include <asm-generic/bitops/find.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
David Howells | 92fc707 | 2006-09-25 23:32:07 -0700 | [diff] [blame] | 161 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | */ |
| 169 | #define fls(x) \ |
| 170 | ({ \ |
| 171 | int bit; \ |
| 172 | \ |
David Howells | 92fc707 | 2006-09-25 23:32:07 -0700 | [diff] [blame] | 173 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | \ |
David Howells | 92fc707 | 2006-09-25 23:32:07 -0700 | [diff] [blame] | 183 | bit; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
David Howells | a8ad27d | 2006-09-25 23:32:08 -0700 | [diff] [blame] | 186 | /** |
| 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 | */ |
| 194 | static inline __attribute__((const)) |
| 195 | int 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 Howells | cf134483 | 2006-09-25 23:32:09 -0700 | [diff] [blame] | 227 | /** |
| 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 | */ |
| 234 | static inline __attribute__((const)) |
| 235 | int 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 | */ |
| 250 | static inline __attribute__((const)) |
| 251 | int __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 Russell | ee38e514 | 2009-01-03 16:14:05 +1030 | [diff] [blame] | 258 | /** |
| 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 | */ |
| 264 | static 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 Howells | f0d1b0b | 2006-12-08 02:37:49 -0800 | [diff] [blame] | 271 | /* |
| 272 | * special slimline version of fls() for calculating ilog2_u32() |
| 273 | * - note: no protection against n == 0 |
| 274 | */ |
| 275 | #define ARCH_HAS_ILOG2_U32 |
| 276 | static inline __attribute__((const)) |
| 277 | int __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 |
| 289 | static inline __attribute__((const)) |
| 290 | int __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 Mita | 1f6d7a9 | 2006-03-26 01:39:22 -0800 | [diff] [blame] | 315 | #include <asm-generic/bitops/sched.h> |
| 316 | #include <asm-generic/bitops/hweight.h> |
Nick Piggin | 2633357 | 2007-10-18 03:06:39 -0700 | [diff] [blame] | 317 | #include <asm-generic/bitops/lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Akinobu Mita | 861b5ae | 2011-03-23 16:42:02 -0700 | [diff] [blame] | 319 | #include <asm-generic/bitops/le.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Akinobu Mita | 148817b | 2011-07-26 16:09:04 -0700 | [diff] [blame] | 321 | #include <asm-generic/bitops/ext2-atomic-setbit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | #endif /* __KERNEL__ */ |
| 324 | |
| 325 | #endif /* _ASM_BITOPS_H */ |