blob: da87943328a510d468d089fdf648edfa5933445b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _PARISC_BITOPS_H
2#define _PARISC_BITOPS_H
3
Jiri Slaby06245172007-10-18 23:40:26 -07004#ifndef _LINUX_BITOPS_H
5#error only <linux/bitops.h> can be included directly
6#endif
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/compiler.h>
Helge Deller2ad5d522017-01-28 11:52:02 +01009#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/byteorder.h>
Peter Zijlstrae4a65e92014-03-13 19:00:36 +010011#include <asm/barrier.h>
Arun Sharma600634972011-07-26 16:09:06 -070012#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14/*
15 * HP-PARISC specific bit operations
16 * for a detailed description of the functions please refer
17 * to include/asm-i386/bitops.h or kerneldoc
18 */
19
Helge Deller2ad5d522017-01-28 11:52:02 +010020#if __BITS_PER_LONG == 64
21#define SHIFT_PER_LONG 6
22#else
23#define SHIFT_PER_LONG 5
24#endif
25
Grant Grundlera366064c2005-10-21 22:45:22 -040026#define CHOP_SHIFTCOUNT(x) (((unsigned long) (x)) & (BITS_PER_LONG - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28
Grant Grundlera366064c2005-10-21 22:45:22 -040029/* See http://marc.theaimsgroup.com/?t=108826637900003 for discussion
30 * on use of volatile and __*_bit() (set/clear/change):
31 * *_bit() want use of volatile.
32 * __*_bit() are "relaxed" and don't use spinlock or volatile.
33 */
34
35static __inline__ void set_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Grant Grundlera366064c2005-10-21 22:45:22 -040037 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 unsigned long flags;
39
40 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 _atomic_spin_lock_irqsave(addr, flags);
42 *addr |= mask;
43 _atomic_spin_unlock_irqrestore(addr, flags);
44}
45
Grant Grundlera366064c2005-10-21 22:45:22 -040046static __inline__ void clear_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Grant Grundlera366064c2005-10-21 22:45:22 -040048 unsigned long mask = ~(1UL << CHOP_SHIFTCOUNT(nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned long flags;
50
51 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 _atomic_spin_lock_irqsave(addr, flags);
Grant Grundlera366064c2005-10-21 22:45:22 -040053 *addr &= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 _atomic_spin_unlock_irqrestore(addr, flags);
55}
56
Grant Grundlera366064c2005-10-21 22:45:22 -040057static __inline__ void change_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Grant Grundlera366064c2005-10-21 22:45:22 -040059 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned long flags;
61
62 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 _atomic_spin_lock_irqsave(addr, flags);
64 *addr ^= mask;
65 _atomic_spin_unlock_irqrestore(addr, flags);
66}
67
Grant Grundlera366064c2005-10-21 22:45:22 -040068static __inline__ int test_and_set_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Grant Grundlera366064c2005-10-21 22:45:22 -040070 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070071 unsigned long old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long flags;
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070073 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 _atomic_spin_lock_irqsave(addr, flags);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070077 old = *addr;
78 set = (old & mask) ? 1 : 0;
79 if (!set)
80 *addr = old | mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 _atomic_spin_unlock_irqrestore(addr, flags);
82
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070083 return set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Grant Grundlera366064c2005-10-21 22:45:22 -040086static __inline__ int test_and_clear_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Grant Grundlera366064c2005-10-21 22:45:22 -040088 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070089 unsigned long old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned long flags;
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070091 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 _atomic_spin_lock_irqsave(addr, flags);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070095 old = *addr;
96 set = (old & mask) ? 1 : 0;
97 if (set)
98 *addr = old & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 _atomic_spin_unlock_irqrestore(addr, flags);
100
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -0700101 return set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Grant Grundlera366064c2005-10-21 22:45:22 -0400104static __inline__ int test_and_change_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Grant Grundlera366064c2005-10-21 22:45:22 -0400106 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
107 unsigned long oldbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned long flags;
109
110 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 _atomic_spin_lock_irqsave(addr, flags);
Grant Grundlera366064c2005-10-21 22:45:22 -0400112 oldbit = *addr;
113 *addr = oldbit ^ mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 _atomic_spin_unlock_irqrestore(addr, flags);
115
Grant Grundlera366064c2005-10-21 22:45:22 -0400116 return (oldbit & mask) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Akinobu Mita59e18a22006-03-26 01:39:31 -0800119#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121#ifdef __KERNEL__
122
123/**
124 * __ffs - find first bit in word. returns 0 to "BITS_PER_LONG-1".
125 * @word: The word to search
126 *
127 * __ffs() return is undefined if no bit is set.
128 *
129 * 32-bit fast __ffs by LaMont Jones "lamont At hp com".
130 * 64-bit enhancement by Grant Grundler "grundler At parisc-linux org".
131 * (with help from willy/jejb to get the semantics right)
132 *
133 * This algorithm avoids branches by making use of nullification.
134 * One side effect of "extr" instructions is it sets PSW[N] bit.
135 * How PSW[N] (nullify next insn) gets set is determined by the
136 * "condition" field (eg "<>" or "TR" below) in the extr* insn.
137 * Only the 1st and one of either the 2cd or 3rd insn will get executed.
138 * Each set of 3 insn will get executed in 2 cycles on PA8x00 vs 16 or so
139 * cycles for each mispredicted branch.
140 */
141
142static __inline__ unsigned long __ffs(unsigned long x)
143{
144 unsigned long ret;
145
146 __asm__(
Helge Deller513e7ec2007-01-28 15:09:20 +0100147#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 " ldi 63,%1\n"
149 " extrd,u,*<> %0,63,32,%%r0\n"
150 " extrd,u,*TR %0,31,32,%0\n" /* move top 32-bits down */
151 " addi -32,%1,%1\n"
152#else
153 " ldi 31,%1\n"
154#endif
155 " extru,<> %0,31,16,%%r0\n"
156 " extru,TR %0,15,16,%0\n" /* xxxx0000 -> 0000xxxx */
157 " addi -16,%1,%1\n"
158 " extru,<> %0,31,8,%%r0\n"
159 " extru,TR %0,23,8,%0\n" /* 0000xx00 -> 000000xx */
160 " addi -8,%1,%1\n"
161 " extru,<> %0,31,4,%%r0\n"
162 " extru,TR %0,27,4,%0\n" /* 000000x0 -> 0000000x */
163 " addi -4,%1,%1\n"
164 " extru,<> %0,31,2,%%r0\n"
165 " extru,TR %0,29,2,%0\n" /* 0000000y, 1100b -> 0011b */
166 " addi -2,%1,%1\n"
167 " extru,= %0,31,1,%%r0\n" /* check last bit */
168 " addi -1,%1,%1\n"
169 : "+r" (x), "=r" (ret) );
170 return ret;
171}
172
Akinobu Mita59e18a22006-03-26 01:39:31 -0800173#include <asm-generic/bitops/ffz.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/*
176 * ffs: find first bit set. returns 1 to BITS_PER_LONG or 0 (if none set)
177 * This is defined the same way as the libc and compiler builtin
178 * ffs routines, therefore differs in spirit from the above ffz (man ffs).
179 */
180static __inline__ int ffs(int x)
181{
182 return x ? (__ffs((unsigned long)x) + 1) : 0;
183}
184
185/*
186 * fls: find last (most significant) bit set.
187 * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
188 */
189
190static __inline__ int fls(int x)
191{
192 int ret;
193 if (!x)
194 return 0;
195
196 __asm__(
197 " ldi 1,%1\n"
198 " extru,<> %0,15,16,%%r0\n"
199 " zdep,TR %0,15,16,%0\n" /* xxxx0000 */
200 " addi 16,%1,%1\n"
201 " extru,<> %0,7,8,%%r0\n"
202 " zdep,TR %0,23,24,%0\n" /* xx000000 */
203 " addi 8,%1,%1\n"
204 " extru,<> %0,3,4,%%r0\n"
205 " zdep,TR %0,27,28,%0\n" /* x0000000 */
206 " addi 4,%1,%1\n"
207 " extru,<> %0,1,2,%%r0\n"
208 " zdep,TR %0,29,30,%0\n" /* y0000000 (y&3 = 0) */
209 " addi 2,%1,%1\n"
210 " extru,= %0,0,1,%%r0\n"
211 " addi 1,%1,%1\n" /* if y & 8, add 1 */
212 : "+r" (x), "=r" (ret) );
213
214 return ret;
215}
216
Alexander van Heukelum56a6b1e2008-03-15 18:31:49 +0100217#include <asm-generic/bitops/__fls.h>
Akinobu Mita59e18a22006-03-26 01:39:31 -0800218#include <asm-generic/bitops/fls64.h>
219#include <asm-generic/bitops/hweight.h>
Nick Piggin26333572007-10-18 03:06:39 -0700220#include <asm-generic/bitops/lock.h>
Akinobu Mita59e18a22006-03-26 01:39:31 -0800221#include <asm-generic/bitops/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#endif /* __KERNEL__ */
224
Akinobu Mita59e18a22006-03-26 01:39:31 -0800225#include <asm-generic/bitops/find.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227#ifdef __KERNEL__
Akinobu Mita59e18a22006-03-26 01:39:31 -0800228
Akinobu Mita861b5ae2011-03-23 16:42:02 -0700229#include <asm-generic/bitops/le.h>
Akinobu Mita148817b2011-07-26 16:09:04 -0700230#include <asm-generic/bitops/ext2-atomic-setbit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232#endif /* __KERNEL__ */
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#endif /* _PARISC_BITOPS_H */