blob: 56ffd260c669b7d9ce50838edb07acb1de004600 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _PARISC_BITOPS_H
3#define _PARISC_BITOPS_H
4
Jiri Slaby06245172007-10-18 23:40:26 -07005#ifndef _LINUX_BITOPS_H
6#error only <linux/bitops.h> can be included directly
7#endif
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/compiler.h>
Helge Deller2ad5d522017-01-28 11:52:02 +010010#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/byteorder.h>
Peter Zijlstrae4a65e92014-03-13 19:00:36 +010012#include <asm/barrier.h>
Arun Sharma600634972011-07-26 16:09:06 -070013#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Helge Dellerb1606282022-02-13 22:29:25 +010015/* compiler build environment sanity checks: */
16#if !defined(CONFIG_64BIT) && defined(__LP64__)
17#error "Please use 'ARCH=parisc' to build the 32-bit kernel."
18#endif
19#if defined(CONFIG_64BIT) && !defined(__LP64__)
20#error "Please use 'ARCH=parisc64' to build the 64-bit kernel."
21#endif
22
Grant Grundlera366064c2005-10-21 22:45:22 -040023/* See http://marc.theaimsgroup.com/?t=108826637900003 for discussion
24 * on use of volatile and __*_bit() (set/clear/change):
25 * *_bit() want use of volatile.
26 * __*_bit() are "relaxed" and don't use spinlock or volatile.
27 */
28
29static __inline__ void set_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Helge Deller208151b2020-06-14 10:54:10 +020031 unsigned long mask = BIT_MASK(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 unsigned long flags;
33
Helge Deller208151b2020-06-14 10:54:10 +020034 addr += BIT_WORD(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 _atomic_spin_lock_irqsave(addr, flags);
36 *addr |= mask;
37 _atomic_spin_unlock_irqrestore(addr, flags);
38}
39
Grant Grundlera366064c2005-10-21 22:45:22 -040040static __inline__ void clear_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Helge Deller208151b2020-06-14 10:54:10 +020042 unsigned long mask = BIT_MASK(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned long flags;
44
Helge Deller208151b2020-06-14 10:54:10 +020045 addr += BIT_WORD(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 _atomic_spin_lock_irqsave(addr, flags);
Helge Deller208151b2020-06-14 10:54:10 +020047 *addr &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 _atomic_spin_unlock_irqrestore(addr, flags);
49}
50
Grant Grundlera366064c2005-10-21 22:45:22 -040051static __inline__ void change_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Helge Deller208151b2020-06-14 10:54:10 +020053 unsigned long mask = BIT_MASK(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 unsigned long flags;
55
Helge Deller208151b2020-06-14 10:54:10 +020056 addr += BIT_WORD(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 _atomic_spin_lock_irqsave(addr, flags);
58 *addr ^= mask;
59 _atomic_spin_unlock_irqrestore(addr, flags);
60}
61
Grant Grundlera366064c2005-10-21 22:45:22 -040062static __inline__ int test_and_set_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Helge Deller208151b2020-06-14 10:54:10 +020064 unsigned long mask = BIT_MASK(nr);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070065 unsigned long old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 unsigned long flags;
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070067 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Helge Deller208151b2020-06-14 10:54:10 +020069 addr += BIT_WORD(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 _atomic_spin_lock_irqsave(addr, flags);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070071 old = *addr;
72 set = (old & mask) ? 1 : 0;
73 if (!set)
74 *addr = old | mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 _atomic_spin_unlock_irqrestore(addr, flags);
76
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070077 return set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Grant Grundlera366064c2005-10-21 22:45:22 -040080static __inline__ int test_and_clear_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Helge Deller208151b2020-06-14 10:54:10 +020082 unsigned long mask = BIT_MASK(nr);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070083 unsigned long old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned long flags;
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070085 int set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Helge Deller208151b2020-06-14 10:54:10 +020087 addr += BIT_WORD(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 _atomic_spin_lock_irqsave(addr, flags);
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070089 old = *addr;
90 set = (old & mask) ? 1 : 0;
91 if (set)
92 *addr = old & ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 _atomic_spin_unlock_irqrestore(addr, flags);
94
Matthew Wilcoxaf5917f2006-12-14 09:00:25 -070095 return set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Grant Grundlera366064c2005-10-21 22:45:22 -040098static __inline__ int test_and_change_bit(int nr, volatile unsigned long * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Helge Deller208151b2020-06-14 10:54:10 +0200100 unsigned long mask = BIT_MASK(nr);
Grant Grundlera366064c2005-10-21 22:45:22 -0400101 unsigned long oldbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned long flags;
103
Helge Deller208151b2020-06-14 10:54:10 +0200104 addr += BIT_WORD(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 _atomic_spin_lock_irqsave(addr, flags);
Grant Grundlera366064c2005-10-21 22:45:22 -0400106 oldbit = *addr;
107 *addr = oldbit ^ mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 _atomic_spin_unlock_irqrestore(addr, flags);
109
Grant Grundlera366064c2005-10-21 22:45:22 -0400110 return (oldbit & mask) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Akinobu Mita59e18a22006-03-26 01:39:31 -0800113#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/**
116 * __ffs - find first bit in word. returns 0 to "BITS_PER_LONG-1".
117 * @word: The word to search
118 *
119 * __ffs() return is undefined if no bit is set.
120 *
121 * 32-bit fast __ffs by LaMont Jones "lamont At hp com".
122 * 64-bit enhancement by Grant Grundler "grundler At parisc-linux org".
123 * (with help from willy/jejb to get the semantics right)
124 *
125 * This algorithm avoids branches by making use of nullification.
126 * One side effect of "extr" instructions is it sets PSW[N] bit.
127 * How PSW[N] (nullify next insn) gets set is determined by the
128 * "condition" field (eg "<>" or "TR" below) in the extr* insn.
129 * Only the 1st and one of either the 2cd or 3rd insn will get executed.
130 * Each set of 3 insn will get executed in 2 cycles on PA8x00 vs 16 or so
131 * cycles for each mispredicted branch.
132 */
133
134static __inline__ unsigned long __ffs(unsigned long x)
135{
136 unsigned long ret;
137
138 __asm__(
Helge Deller513e7ec2007-01-28 15:09:20 +0100139#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 " ldi 63,%1\n"
141 " extrd,u,*<> %0,63,32,%%r0\n"
142 " extrd,u,*TR %0,31,32,%0\n" /* move top 32-bits down */
143 " addi -32,%1,%1\n"
144#else
145 " ldi 31,%1\n"
146#endif
147 " extru,<> %0,31,16,%%r0\n"
148 " extru,TR %0,15,16,%0\n" /* xxxx0000 -> 0000xxxx */
149 " addi -16,%1,%1\n"
150 " extru,<> %0,31,8,%%r0\n"
151 " extru,TR %0,23,8,%0\n" /* 0000xx00 -> 000000xx */
152 " addi -8,%1,%1\n"
153 " extru,<> %0,31,4,%%r0\n"
154 " extru,TR %0,27,4,%0\n" /* 000000x0 -> 0000000x */
155 " addi -4,%1,%1\n"
156 " extru,<> %0,31,2,%%r0\n"
157 " extru,TR %0,29,2,%0\n" /* 0000000y, 1100b -> 0011b */
158 " addi -2,%1,%1\n"
159 " extru,= %0,31,1,%%r0\n" /* check last bit */
160 " addi -1,%1,%1\n"
161 : "+r" (x), "=r" (ret) );
162 return ret;
163}
164
Akinobu Mita59e18a22006-03-26 01:39:31 -0800165#include <asm-generic/bitops/ffz.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/*
168 * ffs: find first bit set. returns 1 to BITS_PER_LONG or 0 (if none set)
169 * This is defined the same way as the libc and compiler builtin
170 * ffs routines, therefore differs in spirit from the above ffz (man ffs).
171 */
172static __inline__ int ffs(int x)
173{
174 return x ? (__ffs((unsigned long)x) + 1) : 0;
175}
176
177/*
178 * fls: find last (most significant) bit set.
179 * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
180 */
181
Matthew Wilcox3fc25792019-01-03 15:26:41 -0800182static __inline__ int fls(unsigned int x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 int ret;
185 if (!x)
186 return 0;
187
188 __asm__(
189 " ldi 1,%1\n"
190 " extru,<> %0,15,16,%%r0\n"
191 " zdep,TR %0,15,16,%0\n" /* xxxx0000 */
192 " addi 16,%1,%1\n"
193 " extru,<> %0,7,8,%%r0\n"
194 " zdep,TR %0,23,24,%0\n" /* xx000000 */
195 " addi 8,%1,%1\n"
196 " extru,<> %0,3,4,%%r0\n"
197 " zdep,TR %0,27,28,%0\n" /* x0000000 */
198 " addi 4,%1,%1\n"
199 " extru,<> %0,1,2,%%r0\n"
200 " zdep,TR %0,29,30,%0\n" /* y0000000 (y&3 = 0) */
201 " addi 2,%1,%1\n"
202 " extru,= %0,0,1,%%r0\n"
203 " addi 1,%1,%1\n" /* if y & 8, add 1 */
204 : "+r" (x), "=r" (ret) );
205
206 return ret;
207}
208
Alexander van Heukelum56a6b1e2008-03-15 18:31:49 +0100209#include <asm-generic/bitops/__fls.h>
Akinobu Mita59e18a22006-03-26 01:39:31 -0800210#include <asm-generic/bitops/fls64.h>
211#include <asm-generic/bitops/hweight.h>
Nick Piggin26333572007-10-18 03:06:39 -0700212#include <asm-generic/bitops/lock.h>
Akinobu Mita59e18a22006-03-26 01:39:31 -0800213#include <asm-generic/bitops/sched.h>
Akinobu Mita861b5ae2011-03-23 16:42:02 -0700214#include <asm-generic/bitops/le.h>
Akinobu Mita148817b2011-07-26 16:09:04 -0700215#include <asm-generic/bitops/ext2-atomic-setbit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#endif /* _PARISC_BITOPS_H */