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