Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _PARISC_BITOPS_H |
| 3 | #define _PARISC_BITOPS_H |
| 4 | |
Jiri Slaby | 0624517 | 2007-10-18 23:40:26 -0700 | [diff] [blame] | 5 | #ifndef _LINUX_BITOPS_H |
| 6 | #error only <linux/bitops.h> can be included directly |
| 7 | #endif |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/compiler.h> |
Helge Deller | 2ad5d52 | 2017-01-28 11:52:02 +0100 | [diff] [blame] | 10 | #include <asm/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/byteorder.h> |
Peter Zijlstra | e4a65e9 | 2014-03-13 19:00:36 +0100 | [diff] [blame] | 12 | #include <asm/barrier.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 13 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * HP-PARISC specific bit operations |
| 17 | * for a detailed description of the functions please refer |
| 18 | * to include/asm-i386/bitops.h or kerneldoc |
| 19 | */ |
| 20 | |
Helge Deller | 2ad5d52 | 2017-01-28 11:52:02 +0100 | [diff] [blame] | 21 | #if __BITS_PER_LONG == 64 |
| 22 | #define SHIFT_PER_LONG 6 |
| 23 | #else |
| 24 | #define SHIFT_PER_LONG 5 |
| 25 | #endif |
| 26 | |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 27 | #define CHOP_SHIFTCOUNT(x) (((unsigned long) (x)) & (BITS_PER_LONG - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 30 | /* See http://marc.theaimsgroup.com/?t=108826637900003 for discussion |
| 31 | * on use of volatile and __*_bit() (set/clear/change): |
| 32 | * *_bit() want use of volatile. |
| 33 | * __*_bit() are "relaxed" and don't use spinlock or volatile. |
| 34 | */ |
| 35 | |
| 36 | static __inline__ void set_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 38 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | unsigned long flags; |
| 40 | |
| 41 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | _atomic_spin_lock_irqsave(addr, flags); |
| 43 | *addr |= mask; |
| 44 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 45 | } |
| 46 | |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 47 | static __inline__ void clear_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 49 | unsigned long mask = ~(1UL << CHOP_SHIFTCOUNT(nr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | unsigned long flags; |
| 51 | |
| 52 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | _atomic_spin_lock_irqsave(addr, flags); |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 54 | *addr &= mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 56 | } |
| 57 | |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 58 | static __inline__ void change_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 60 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | unsigned long flags; |
| 62 | |
| 63 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | _atomic_spin_lock_irqsave(addr, flags); |
| 65 | *addr ^= mask; |
| 66 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 67 | } |
| 68 | |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 69 | static __inline__ int test_and_set_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 71 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
Matthew Wilcox | af5917f | 2006-12-14 09:00:25 -0700 | [diff] [blame] | 72 | unsigned long old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | unsigned long flags; |
Matthew Wilcox | af5917f | 2006-12-14 09:00:25 -0700 | [diff] [blame] | 74 | int set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | _atomic_spin_lock_irqsave(addr, flags); |
Matthew Wilcox | af5917f | 2006-12-14 09:00:25 -0700 | [diff] [blame] | 78 | old = *addr; |
| 79 | set = (old & mask) ? 1 : 0; |
| 80 | if (!set) |
| 81 | *addr = old | mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 83 | |
Matthew Wilcox | af5917f | 2006-12-14 09:00:25 -0700 | [diff] [blame] | 84 | return set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 87 | static __inline__ int test_and_clear_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 89 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
Matthew Wilcox | af5917f | 2006-12-14 09:00:25 -0700 | [diff] [blame] | 90 | unsigned long old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | unsigned long flags; |
Matthew Wilcox | af5917f | 2006-12-14 09:00:25 -0700 | [diff] [blame] | 92 | int set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | _atomic_spin_lock_irqsave(addr, flags); |
Matthew Wilcox | af5917f | 2006-12-14 09:00:25 -0700 | [diff] [blame] | 96 | old = *addr; |
| 97 | set = (old & mask) ? 1 : 0; |
| 98 | if (set) |
| 99 | *addr = old & ~mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 101 | |
Matthew Wilcox | af5917f | 2006-12-14 09:00:25 -0700 | [diff] [blame] | 102 | return set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 105 | static __inline__ int test_and_change_bit(int nr, volatile unsigned long * addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 107 | unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr); |
| 108 | unsigned long oldbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | unsigned long flags; |
| 110 | |
| 111 | addr += (nr >> SHIFT_PER_LONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | _atomic_spin_lock_irqsave(addr, flags); |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 113 | oldbit = *addr; |
| 114 | *addr = oldbit ^ mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | _atomic_spin_unlock_irqrestore(addr, flags); |
| 116 | |
Grant Grundler | a366064c | 2005-10-21 22:45:22 -0400 | [diff] [blame] | 117 | return (oldbit & mask) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Akinobu Mita | 59e18a2 | 2006-03-26 01:39:31 -0800 | [diff] [blame] | 120 | #include <asm-generic/bitops/non-atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | #ifdef __KERNEL__ |
| 123 | |
| 124 | /** |
| 125 | * __ffs - find first bit in word. returns 0 to "BITS_PER_LONG-1". |
| 126 | * @word: The word to search |
| 127 | * |
| 128 | * __ffs() return is undefined if no bit is set. |
| 129 | * |
| 130 | * 32-bit fast __ffs by LaMont Jones "lamont At hp com". |
| 131 | * 64-bit enhancement by Grant Grundler "grundler At parisc-linux org". |
| 132 | * (with help from willy/jejb to get the semantics right) |
| 133 | * |
| 134 | * This algorithm avoids branches by making use of nullification. |
| 135 | * One side effect of "extr" instructions is it sets PSW[N] bit. |
| 136 | * How PSW[N] (nullify next insn) gets set is determined by the |
| 137 | * "condition" field (eg "<>" or "TR" below) in the extr* insn. |
| 138 | * Only the 1st and one of either the 2cd or 3rd insn will get executed. |
| 139 | * Each set of 3 insn will get executed in 2 cycles on PA8x00 vs 16 or so |
| 140 | * cycles for each mispredicted branch. |
| 141 | */ |
| 142 | |
| 143 | static __inline__ unsigned long __ffs(unsigned long x) |
| 144 | { |
| 145 | unsigned long ret; |
| 146 | |
| 147 | __asm__( |
Helge Deller | 513e7ec | 2007-01-28 15:09:20 +0100 | [diff] [blame] | 148 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | " ldi 63,%1\n" |
| 150 | " extrd,u,*<> %0,63,32,%%r0\n" |
| 151 | " extrd,u,*TR %0,31,32,%0\n" /* move top 32-bits down */ |
| 152 | " addi -32,%1,%1\n" |
| 153 | #else |
| 154 | " ldi 31,%1\n" |
| 155 | #endif |
| 156 | " extru,<> %0,31,16,%%r0\n" |
| 157 | " extru,TR %0,15,16,%0\n" /* xxxx0000 -> 0000xxxx */ |
| 158 | " addi -16,%1,%1\n" |
| 159 | " extru,<> %0,31,8,%%r0\n" |
| 160 | " extru,TR %0,23,8,%0\n" /* 0000xx00 -> 000000xx */ |
| 161 | " addi -8,%1,%1\n" |
| 162 | " extru,<> %0,31,4,%%r0\n" |
| 163 | " extru,TR %0,27,4,%0\n" /* 000000x0 -> 0000000x */ |
| 164 | " addi -4,%1,%1\n" |
| 165 | " extru,<> %0,31,2,%%r0\n" |
| 166 | " extru,TR %0,29,2,%0\n" /* 0000000y, 1100b -> 0011b */ |
| 167 | " addi -2,%1,%1\n" |
| 168 | " extru,= %0,31,1,%%r0\n" /* check last bit */ |
| 169 | " addi -1,%1,%1\n" |
| 170 | : "+r" (x), "=r" (ret) ); |
| 171 | return ret; |
| 172 | } |
| 173 | |
Akinobu Mita | 59e18a2 | 2006-03-26 01:39:31 -0800 | [diff] [blame] | 174 | #include <asm-generic/bitops/ffz.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * ffs: find first bit set. returns 1 to BITS_PER_LONG or 0 (if none set) |
| 178 | * This is defined the same way as the libc and compiler builtin |
| 179 | * ffs routines, therefore differs in spirit from the above ffz (man ffs). |
| 180 | */ |
| 181 | static __inline__ int ffs(int x) |
| 182 | { |
| 183 | return x ? (__ffs((unsigned long)x) + 1) : 0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * fls: find last (most significant) bit set. |
| 188 | * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. |
| 189 | */ |
| 190 | |
Matthew Wilcox | 3fc2579 | 2019-01-03 15:26:41 -0800 | [diff] [blame^] | 191 | static __inline__ int fls(unsigned int x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | int ret; |
| 194 | if (!x) |
| 195 | return 0; |
| 196 | |
| 197 | __asm__( |
| 198 | " ldi 1,%1\n" |
| 199 | " extru,<> %0,15,16,%%r0\n" |
| 200 | " zdep,TR %0,15,16,%0\n" /* xxxx0000 */ |
| 201 | " addi 16,%1,%1\n" |
| 202 | " extru,<> %0,7,8,%%r0\n" |
| 203 | " zdep,TR %0,23,24,%0\n" /* xx000000 */ |
| 204 | " addi 8,%1,%1\n" |
| 205 | " extru,<> %0,3,4,%%r0\n" |
| 206 | " zdep,TR %0,27,28,%0\n" /* x0000000 */ |
| 207 | " addi 4,%1,%1\n" |
| 208 | " extru,<> %0,1,2,%%r0\n" |
| 209 | " zdep,TR %0,29,30,%0\n" /* y0000000 (y&3 = 0) */ |
| 210 | " addi 2,%1,%1\n" |
| 211 | " extru,= %0,0,1,%%r0\n" |
| 212 | " addi 1,%1,%1\n" /* if y & 8, add 1 */ |
| 213 | : "+r" (x), "=r" (ret) ); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
Alexander van Heukelum | 56a6b1e | 2008-03-15 18:31:49 +0100 | [diff] [blame] | 218 | #include <asm-generic/bitops/__fls.h> |
Akinobu Mita | 59e18a2 | 2006-03-26 01:39:31 -0800 | [diff] [blame] | 219 | #include <asm-generic/bitops/fls64.h> |
| 220 | #include <asm-generic/bitops/hweight.h> |
Nick Piggin | 2633357 | 2007-10-18 03:06:39 -0700 | [diff] [blame] | 221 | #include <asm-generic/bitops/lock.h> |
Akinobu Mita | 59e18a2 | 2006-03-26 01:39:31 -0800 | [diff] [blame] | 222 | #include <asm-generic/bitops/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | #endif /* __KERNEL__ */ |
| 225 | |
Akinobu Mita | 59e18a2 | 2006-03-26 01:39:31 -0800 | [diff] [blame] | 226 | #include <asm-generic/bitops/find.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | #ifdef __KERNEL__ |
Akinobu Mita | 59e18a2 | 2006-03-26 01:39:31 -0800 | [diff] [blame] | 229 | |
Akinobu Mita | 861b5ae | 2011-03-23 16:42:02 -0700 | [diff] [blame] | 230 | #include <asm-generic/bitops/le.h> |
Akinobu Mita | 148817b | 2011-07-26 16:09:04 -0700 | [diff] [blame] | 231 | #include <asm-generic/bitops/ext2-atomic-setbit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | #endif /* __KERNEL__ */ |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | #endif /* _PARISC_BITOPS_H */ |