blob: 8c9b631d2a783e8a9e7cf4041fb0d917da585d06 [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>
Grant Grundlera3660642005-10-21 22:45:22 -04009#include <asm/types.h> /* for BITS_PER_LONG/SHIFT_PER_LONG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070011#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13/*
14 * HP-PARISC specific bit operations
15 * for a detailed description of the functions please refer
16 * to include/asm-i386/bitops.h or kerneldoc
17 */
18
Grant Grundlera3660642005-10-21 22:45:22 -040019#define CHOP_SHIFTCOUNT(x) (((unsigned long) (x)) & (BITS_PER_LONG - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21
22#define smp_mb__before_clear_bit() smp_mb()
23#define smp_mb__after_clear_bit() smp_mb()
24
Grant Grundlera3660642005-10-21 22:45:22 -040025/* See http://marc.theaimsgroup.com/?t=108826637900003 for discussion
26 * on use of volatile and __*_bit() (set/clear/change):
27 * *_bit() want use of volatile.
28 * __*_bit() are "relaxed" and don't use spinlock or volatile.
29 */
30
31static __inline__ void set_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Grant Grundlera3660642005-10-21 22:45:22 -040033 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 unsigned long flags;
35
36 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 _atomic_spin_lock_irqsave(addr, flags);
38 *addr |= mask;
39 _atomic_spin_unlock_irqrestore(addr, flags);
40}
41
Grant Grundlera3660642005-10-21 22:45:22 -040042static __inline__ void clear_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Grant Grundlera3660642005-10-21 22:45:22 -040044 unsigned long mask = ~(1UL << CHOP_SHIFTCOUNT(nr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned long flags;
46
47 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 _atomic_spin_lock_irqsave(addr, flags);
Grant Grundlera3660642005-10-21 22:45:22 -040049 *addr &= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 _atomic_spin_unlock_irqrestore(addr, flags);
51}
52
Grant Grundlera3660642005-10-21 22:45:22 -040053static __inline__ void change_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Grant Grundlera3660642005-10-21 22:45:22 -040055 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned long flags;
57
58 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 _atomic_spin_lock_irqsave(addr, flags);
60 *addr ^= mask;
61 _atomic_spin_unlock_irqrestore(addr, flags);
62}
63
Grant Grundlera3660642005-10-21 22:45:22 -040064static __inline__ int test_and_set_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Grant Grundlera3660642005-10-21 22:45:22 -040066 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070067 unsigned long old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned long flags;
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070069 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 _atomic_spin_lock_irqsave(addr, flags);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070073 old = *addr;
74 set = (old & mask) ? 1 : 0;
75 if (!set)
76 *addr = old | mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 _atomic_spin_unlock_irqrestore(addr, flags);
78
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070079 return set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Grant Grundlera3660642005-10-21 22:45:22 -040082static __inline__ int test_and_clear_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Grant Grundlera3660642005-10-21 22:45:22 -040084 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070085 unsigned long old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned long flags;
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070087 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 _atomic_spin_lock_irqsave(addr, flags);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070091 old = *addr;
92 set = (old & mask) ? 1 : 0;
93 if (set)
94 *addr = old & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 _atomic_spin_unlock_irqrestore(addr, flags);
96
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070097 return set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Grant Grundlera3660642005-10-21 22:45:22 -0400100static __inline__ int test_and_change_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Grant Grundlera3660642005-10-21 22:45:22 -0400102 unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
103 unsigned long oldbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long flags;
105
106 addr += (nr >> SHIFT_PER_LONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 _atomic_spin_lock_irqsave(addr, flags);
Grant Grundlera3660642005-10-21 22:45:22 -0400108 oldbit = *addr;
109 *addr = oldbit ^ mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 _atomic_spin_unlock_irqrestore(addr, flags);
111
Grant Grundlera3660642005-10-21 22:45:22 -0400112 return (oldbit & mask) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Akinobu Mita59e18a22006-03-26 01:39:31 -0800115#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#ifdef __KERNEL__
118
119/**
120 * __ffs - find first bit in word. returns 0 to "BITS_PER_LONG-1".
121 * @word: The word to search
122 *
123 * __ffs() return is undefined if no bit is set.
124 *
125 * 32-bit fast __ffs by LaMont Jones "lamont At hp com".
126 * 64-bit enhancement by Grant Grundler "grundler At parisc-linux org".
127 * (with help from willy/jejb to get the semantics right)
128 *
129 * This algorithm avoids branches by making use of nullification.
130 * One side effect of "extr" instructions is it sets PSW[N] bit.
131 * How PSW[N] (nullify next insn) gets set is determined by the
132 * "condition" field (eg "<>" or "TR" below) in the extr* insn.
133 * Only the 1st and one of either the 2cd or 3rd insn will get executed.
134 * Each set of 3 insn will get executed in 2 cycles on PA8x00 vs 16 or so
135 * cycles for each mispredicted branch.
136 */
137
138static __inline__ unsigned long __ffs(unsigned long x)
139{
140 unsigned long ret;
141
142 __asm__(
Helge Deller513e7ec2007-01-28 15:09:20 +0100143#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 " ldi 63,%1\n"
145 " extrd,u,*<> %0,63,32,%%r0\n"
146 " extrd,u,*TR %0,31,32,%0\n" /* move top 32-bits down */
147 " addi -32,%1,%1\n"
148#else
149 " ldi 31,%1\n"
150#endif
151 " extru,<> %0,31,16,%%r0\n"
152 " extru,TR %0,15,16,%0\n" /* xxxx0000 -> 0000xxxx */
153 " addi -16,%1,%1\n"
154 " extru,<> %0,31,8,%%r0\n"
155 " extru,TR %0,23,8,%0\n" /* 0000xx00 -> 000000xx */
156 " addi -8,%1,%1\n"
157 " extru,<> %0,31,4,%%r0\n"
158 " extru,TR %0,27,4,%0\n" /* 000000x0 -> 0000000x */
159 " addi -4,%1,%1\n"
160 " extru,<> %0,31,2,%%r0\n"
161 " extru,TR %0,29,2,%0\n" /* 0000000y, 1100b -> 0011b */
162 " addi -2,%1,%1\n"
163 " extru,= %0,31,1,%%r0\n" /* check last bit */
164 " addi -1,%1,%1\n"
165 : "+r" (x), "=r" (ret) );
166 return ret;
167}
168
Akinobu Mita59e18a22006-03-26 01:39:31 -0800169#include <asm-generic/bitops/ffz.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*
172 * ffs: find first bit set. returns 1 to BITS_PER_LONG or 0 (if none set)
173 * This is defined the same way as the libc and compiler builtin
174 * ffs routines, therefore differs in spirit from the above ffz (man ffs).
175 */
176static __inline__ int ffs(int x)
177{
178 return x ? (__ffs((unsigned long)x) + 1) : 0;
179}
180
181/*
182 * fls: find last (most significant) bit set.
183 * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
184 */
185
186static __inline__ int fls(int x)
187{
188 int ret;
189 if (!x)
190 return 0;
191
192 __asm__(
193 " ldi 1,%1\n"
194 " extru,<> %0,15,16,%%r0\n"
195 " zdep,TR %0,15,16,%0\n" /* xxxx0000 */
196 " addi 16,%1,%1\n"
197 " extru,<> %0,7,8,%%r0\n"
198 " zdep,TR %0,23,24,%0\n" /* xx000000 */
199 " addi 8,%1,%1\n"
200 " extru,<> %0,3,4,%%r0\n"
201 " zdep,TR %0,27,28,%0\n" /* x0000000 */
202 " addi 4,%1,%1\n"
203 " extru,<> %0,1,2,%%r0\n"
204 " zdep,TR %0,29,30,%0\n" /* y0000000 (y&3 = 0) */
205 " addi 2,%1,%1\n"
206 " extru,= %0,0,1,%%r0\n"
207 " addi 1,%1,%1\n" /* if y & 8, add 1 */
208 : "+r" (x), "=r" (ret) );
209
210 return ret;
211}
212
Alexander van Heukelum56a6b1e2008-03-15 18:31:49 +0100213#include <asm-generic/bitops/__fls.h>
Akinobu Mita59e18a22006-03-26 01:39:31 -0800214#include <asm-generic/bitops/fls64.h>
215#include <asm-generic/bitops/hweight.h>
Nick Piggin26333572007-10-18 03:06:39 -0700216#include <asm-generic/bitops/lock.h>
Akinobu Mita59e18a22006-03-26 01:39:31 -0800217#include <asm-generic/bitops/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219#endif /* __KERNEL__ */
220
Akinobu Mita59e18a22006-03-26 01:39:31 -0800221#include <asm-generic/bitops/find.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#ifdef __KERNEL__
Akinobu Mita59e18a22006-03-26 01:39:31 -0800224
Akinobu Mita861b5ae2011-03-23 16:42:02 -0700225#include <asm-generic/bitops/le.h>
Akinobu Mita148817b2011-07-26 16:09:04 -0700226#include <asm-generic/bitops/ext2-atomic-setbit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228#endif /* __KERNEL__ */
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#endif /* _PARISC_BITOPS_H */