xref: /linux/arch/s390/include/asm/bitops.h (revision 2fe05e1139a555ae91f00a812cb9520e7d3022ab)
1 /*
2  *    Copyright IBM Corp. 1999,2013
3  *
4  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
5  *
6  * The description below was taken in large parts from the powerpc
7  * bitops header file:
8  * Within a word, bits are numbered LSB first.  Lot's of places make
9  * this assumption by directly testing bits with (val & (1<<nr)).
10  * This can cause confusion for large (> 1 word) bitmaps on a
11  * big-endian system because, unlike little endian, the number of each
12  * bit depends on the word size.
13  *
14  * The bitop functions are defined to work on unsigned longs, so the bits
15  * end up numbered:
16  *   |63..............0|127............64|191...........128|255...........192|
17  *
18  * We also have special functions which work with an MSB0 encoding.
19  * The bits are numbered:
20  *   |0..............63|64............127|128...........191|192...........255|
21  *
22  * The main difference is that bit 0-63 in the bit number field needs to be
23  * reversed compared to the LSB0 encoded bit fields. This can be achieved by
24  * XOR with 0x3f.
25  *
26  */
27 
28 #ifndef _S390_BITOPS_H
29 #define _S390_BITOPS_H
30 
31 #ifndef _LINUX_BITOPS_H
32 #error only <linux/bitops.h> can be included directly
33 #endif
34 
35 #include <linux/typecheck.h>
36 #include <linux/compiler.h>
37 #include <asm/atomic_ops.h>
38 #include <asm/barrier.h>
39 
40 #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
41 
42 static inline unsigned long *
43 __bitops_word(unsigned long nr, volatile unsigned long *ptr)
44 {
45 	unsigned long addr;
46 
47 	addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3);
48 	return (unsigned long *)addr;
49 }
50 
51 static inline unsigned char *
52 __bitops_byte(unsigned long nr, volatile unsigned long *ptr)
53 {
54 	return ((unsigned char *)ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
55 }
56 
57 static inline void set_bit(unsigned long nr, volatile unsigned long *ptr)
58 {
59 	unsigned long *addr = __bitops_word(nr, ptr);
60 	unsigned long mask;
61 
62 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
63 	if (__builtin_constant_p(nr)) {
64 		unsigned char *caddr = __bitops_byte(nr, ptr);
65 
66 		asm volatile(
67 			"oi	%0,%b1\n"
68 			: "+Q" (*caddr)
69 			: "i" (1 << (nr & 7))
70 			: "cc", "memory");
71 		return;
72 	}
73 #endif
74 	mask = 1UL << (nr & (BITS_PER_LONG - 1));
75 	__atomic64_or(mask, addr);
76 }
77 
78 static inline void clear_bit(unsigned long nr, volatile unsigned long *ptr)
79 {
80 	unsigned long *addr = __bitops_word(nr, ptr);
81 	unsigned long mask;
82 
83 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
84 	if (__builtin_constant_p(nr)) {
85 		unsigned char *caddr = __bitops_byte(nr, ptr);
86 
87 		asm volatile(
88 			"ni	%0,%b1\n"
89 			: "+Q" (*caddr)
90 			: "i" (~(1 << (nr & 7)))
91 			: "cc", "memory");
92 		return;
93 	}
94 #endif
95 	mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
96 	__atomic64_and(mask, addr);
97 }
98 
99 static inline void change_bit(unsigned long nr, volatile unsigned long *ptr)
100 {
101 	unsigned long *addr = __bitops_word(nr, ptr);
102 	unsigned long mask;
103 
104 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
105 	if (__builtin_constant_p(nr)) {
106 		unsigned char *caddr = __bitops_byte(nr, ptr);
107 
108 		asm volatile(
109 			"xi	%0,%b1\n"
110 			: "+Q" (*caddr)
111 			: "i" (1 << (nr & 7))
112 			: "cc", "memory");
113 		return;
114 	}
115 #endif
116 	mask = 1UL << (nr & (BITS_PER_LONG - 1));
117 	__atomic64_xor(mask, addr);
118 }
119 
120 static inline int
121 test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
122 {
123 	unsigned long *addr = __bitops_word(nr, ptr);
124 	unsigned long old, mask;
125 
126 	mask = 1UL << (nr & (BITS_PER_LONG - 1));
127 	old = __atomic64_or_barrier(mask, addr);
128 	return (old & mask) != 0;
129 }
130 
131 static inline int
132 test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
133 {
134 	unsigned long *addr = __bitops_word(nr, ptr);
135 	unsigned long old, mask;
136 
137 	mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
138 	old = __atomic64_and_barrier(mask, addr);
139 	return (old & ~mask) != 0;
140 }
141 
142 static inline int
143 test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
144 {
145 	unsigned long *addr = __bitops_word(nr, ptr);
146 	unsigned long old, mask;
147 
148 	mask = 1UL << (nr & (BITS_PER_LONG - 1));
149 	old = __atomic64_xor_barrier(mask, addr);
150 	return (old & mask) != 0;
151 }
152 
153 static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
154 {
155 	unsigned char *addr = __bitops_byte(nr, ptr);
156 
157 	*addr |= 1 << (nr & 7);
158 }
159 
160 static inline void
161 __clear_bit(unsigned long nr, volatile unsigned long *ptr)
162 {
163 	unsigned char *addr = __bitops_byte(nr, ptr);
164 
165 	*addr &= ~(1 << (nr & 7));
166 }
167 
168 static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
169 {
170 	unsigned char *addr = __bitops_byte(nr, ptr);
171 
172 	*addr ^= 1 << (nr & 7);
173 }
174 
175 static inline int
176 __test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
177 {
178 	unsigned char *addr = __bitops_byte(nr, ptr);
179 	unsigned char ch;
180 
181 	ch = *addr;
182 	*addr |= 1 << (nr & 7);
183 	return (ch >> (nr & 7)) & 1;
184 }
185 
186 static inline int
187 __test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
188 {
189 	unsigned char *addr = __bitops_byte(nr, ptr);
190 	unsigned char ch;
191 
192 	ch = *addr;
193 	*addr &= ~(1 << (nr & 7));
194 	return (ch >> (nr & 7)) & 1;
195 }
196 
197 static inline int
198 __test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
199 {
200 	unsigned char *addr = __bitops_byte(nr, ptr);
201 	unsigned char ch;
202 
203 	ch = *addr;
204 	*addr ^= 1 << (nr & 7);
205 	return (ch >> (nr & 7)) & 1;
206 }
207 
208 static inline int test_bit(unsigned long nr, const volatile unsigned long *ptr)
209 {
210 	const volatile unsigned char *addr;
211 
212 	addr = ((const volatile unsigned char *)ptr);
213 	addr += (nr ^ (BITS_PER_LONG - 8)) >> 3;
214 	return (*addr >> (nr & 7)) & 1;
215 }
216 
217 static inline int test_and_set_bit_lock(unsigned long nr,
218 					volatile unsigned long *ptr)
219 {
220 	if (test_bit(nr, ptr))
221 		return 1;
222 	return test_and_set_bit(nr, ptr);
223 }
224 
225 static inline void clear_bit_unlock(unsigned long nr,
226 				    volatile unsigned long *ptr)
227 {
228 	smp_mb__before_atomic();
229 	clear_bit(nr, ptr);
230 }
231 
232 static inline void __clear_bit_unlock(unsigned long nr,
233 				      volatile unsigned long *ptr)
234 {
235 	smp_mb();
236 	__clear_bit(nr, ptr);
237 }
238 
239 /*
240  * Functions which use MSB0 bit numbering.
241  * The bits are numbered:
242  *   |0..............63|64............127|128...........191|192...........255|
243  */
244 unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size);
245 unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size,
246 				unsigned long offset);
247 
248 #define for_each_set_bit_inv(bit, addr, size)				\
249 	for ((bit) = find_first_bit_inv((addr), (size));		\
250 	     (bit) < (size);						\
251 	     (bit) = find_next_bit_inv((addr), (size), (bit) + 1))
252 
253 static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
254 {
255 	return set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
256 }
257 
258 static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
259 {
260 	return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
261 }
262 
263 static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
264 {
265 	return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
266 }
267 
268 static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
269 {
270 	return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
271 }
272 
273 static inline int test_bit_inv(unsigned long nr,
274 			       const volatile unsigned long *ptr)
275 {
276 	return test_bit(nr ^ (BITS_PER_LONG - 1), ptr);
277 }
278 
279 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
280 
281 /**
282  * __flogr - find leftmost one
283  * @word - The word to search
284  *
285  * Returns the bit number of the most significant bit set,
286  * where the most significant bit has bit number 0.
287  * If no bit is set this function returns 64.
288  */
289 static inline unsigned char __flogr(unsigned long word)
290 {
291 	if (__builtin_constant_p(word)) {
292 		unsigned long bit = 0;
293 
294 		if (!word)
295 			return 64;
296 		if (!(word & 0xffffffff00000000UL)) {
297 			word <<= 32;
298 			bit += 32;
299 		}
300 		if (!(word & 0xffff000000000000UL)) {
301 			word <<= 16;
302 			bit += 16;
303 		}
304 		if (!(word & 0xff00000000000000UL)) {
305 			word <<= 8;
306 			bit += 8;
307 		}
308 		if (!(word & 0xf000000000000000UL)) {
309 			word <<= 4;
310 			bit += 4;
311 		}
312 		if (!(word & 0xc000000000000000UL)) {
313 			word <<= 2;
314 			bit += 2;
315 		}
316 		if (!(word & 0x8000000000000000UL)) {
317 			word <<= 1;
318 			bit += 1;
319 		}
320 		return bit;
321 	} else {
322 		register unsigned long bit asm("4") = word;
323 		register unsigned long out asm("5");
324 
325 		asm volatile(
326 			"       flogr   %[bit],%[bit]\n"
327 			: [bit] "+d" (bit), [out] "=d" (out) : : "cc");
328 		return bit;
329 	}
330 }
331 
332 /**
333  * __ffs - find first bit in word.
334  * @word: The word to search
335  *
336  * Undefined if no bit exists, so code should check against 0 first.
337  */
338 static inline unsigned long __ffs(unsigned long word)
339 {
340 	return __flogr(-word & word) ^ (BITS_PER_LONG - 1);
341 }
342 
343 /**
344  * ffs - find first bit set
345  * @word: the word to search
346  *
347  * This is defined the same way as the libc and
348  * compiler builtin ffs routines (man ffs).
349  */
350 static inline int ffs(int word)
351 {
352 	unsigned long mask = 2 * BITS_PER_LONG - 1;
353 	unsigned int val = (unsigned int)word;
354 
355 	return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask;
356 }
357 
358 /**
359  * __fls - find last (most-significant) set bit in a long word
360  * @word: the word to search
361  *
362  * Undefined if no set bit exists, so code should check against 0 first.
363  */
364 static inline unsigned long __fls(unsigned long word)
365 {
366 	return __flogr(word) ^ (BITS_PER_LONG - 1);
367 }
368 
369 /**
370  * fls64 - find last set bit in a 64-bit word
371  * @word: the word to search
372  *
373  * This is defined in a similar way as the libc and compiler builtin
374  * ffsll, but returns the position of the most significant set bit.
375  *
376  * fls64(value) returns 0 if value is 0 or the position of the last
377  * set bit if value is nonzero. The last (most significant) bit is
378  * at position 64.
379  */
380 static inline int fls64(unsigned long word)
381 {
382 	unsigned long mask = 2 * BITS_PER_LONG - 1;
383 
384 	return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask;
385 }
386 
387 /**
388  * fls - find last (most-significant) bit set
389  * @word: the word to search
390  *
391  * This is defined the same way as ffs.
392  * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
393  */
394 static inline int fls(int word)
395 {
396 	return fls64((unsigned int)word);
397 }
398 
399 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
400 
401 #include <asm-generic/bitops/__ffs.h>
402 #include <asm-generic/bitops/ffs.h>
403 #include <asm-generic/bitops/__fls.h>
404 #include <asm-generic/bitops/fls.h>
405 #include <asm-generic/bitops/fls64.h>
406 
407 #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
408 
409 #include <asm-generic/bitops/ffz.h>
410 #include <asm-generic/bitops/find.h>
411 #include <asm-generic/bitops/hweight.h>
412 #include <asm-generic/bitops/sched.h>
413 #include <asm-generic/bitops/le.h>
414 #include <asm-generic/bitops/ext2-atomic-setbit.h>
415 
416 #endif /* _S390_BITOPS_H */
417