Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 1999,2013 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 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 | * |
Heiko Carstens | 48002bd | 2015-10-08 13:51:44 +0200 | [diff] [blame] | 14 | * The bitop functions are defined to work on unsigned longs, so the bits |
| 15 | * end up numbered: |
Heiko Carstens | db85eae | 2014-02-12 12:43:40 +0100 | [diff] [blame] | 16 | * |63..............0|127............64|191...........128|255...........192| |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 17 | * |
| 18 | * There are a few little-endian macros used mostly for filesystem |
Heiko Carstens | 48002bd | 2015-10-08 13:51:44 +0200 | [diff] [blame] | 19 | * bitmaps, these work on similar bit array layouts, but byte-oriented: |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 20 | * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56| |
| 21 | * |
Heiko Carstens | 48002bd | 2015-10-08 13:51:44 +0200 | [diff] [blame] | 22 | * The main difference is that bit 3-5 in the bit number field needs to be |
| 23 | * reversed compared to the big-endian bit fields. This can be achieved by |
| 24 | * XOR with 0x38. |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 25 | * |
Heiko Carstens | 48002bd | 2015-10-08 13:51:44 +0200 | [diff] [blame] | 26 | * We also have special functions which work with an MSB0 encoding. |
| 27 | * The bits are numbered: |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 28 | * |0..............63|64............127|128...........191|192...........255| |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 29 | * |
Heiko Carstens | 48002bd | 2015-10-08 13:51:44 +0200 | [diff] [blame] | 30 | * The main difference is that bit 0-63 in the bit number field needs to be |
| 31 | * reversed compared to the LSB0 encoded bit fields. This can be achieved by |
| 32 | * XOR with 0x3f. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * |
| 34 | */ |
Heiko Carstens | c406abd | 2006-06-29 14:56:13 +0200 | [diff] [blame] | 35 | |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 36 | #ifndef _S390_BITOPS_H |
| 37 | #define _S390_BITOPS_H |
| 38 | |
Jiri Slaby | 0624517 | 2007-10-18 23:40:26 -0700 | [diff] [blame] | 39 | #ifndef _LINUX_BITOPS_H |
| 40 | #error only <linux/bitops.h> can be included directly |
| 41 | #endif |
| 42 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 43 | #include <linux/typecheck.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/compiler.h> |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 45 | #include <asm/barrier.h> |
| 46 | |
| 47 | #define __BITOPS_NO_BARRIER "\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 49 | #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES |
| 50 | |
| 51 | #define __BITOPS_OR "laog" |
| 52 | #define __BITOPS_AND "lang" |
| 53 | #define __BITOPS_XOR "laxg" |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 54 | #define __BITOPS_BARRIER "bcr 14,0\n" |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 55 | |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 56 | #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 57 | ({ \ |
| 58 | unsigned long __old; \ |
| 59 | \ |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 60 | typecheck(unsigned long *, (__addr)); \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 61 | asm volatile( \ |
| 62 | __op_string " %0,%2,%1\n" \ |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 63 | __barrier \ |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 64 | : "=d" (__old), "+Q" (*(__addr)) \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 65 | : "d" (__val) \ |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 66 | : "cc", "memory"); \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 67 | __old; \ |
| 68 | }) |
| 69 | |
| 70 | #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */ |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #define __BITOPS_OR "ogr" |
| 73 | #define __BITOPS_AND "ngr" |
| 74 | #define __BITOPS_XOR "xgr" |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 75 | #define __BITOPS_BARRIER "\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 77 | #define __BITOPS_LOOP(__addr, __val, __op_string, __barrier) \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 78 | ({ \ |
| 79 | unsigned long __old, __new; \ |
| 80 | \ |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 81 | typecheck(unsigned long *, (__addr)); \ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 82 | asm volatile( \ |
| 83 | " lg %0,%2\n" \ |
| 84 | "0: lgr %1,%0\n" \ |
| 85 | __op_string " %1,%3\n" \ |
| 86 | " csg %0,%1,%2\n" \ |
| 87 | " jl 0b" \ |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 88 | : "=&d" (__old), "=&d" (__new), "+Q" (*(__addr))\ |
| 89 | : "d" (__val) \ |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 90 | : "cc", "memory"); \ |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 91 | __old; \ |
| 92 | }) |
| 93 | |
| 94 | #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 95 | |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 96 | #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 98 | static inline unsigned long * |
| 99 | __bitops_word(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 101 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 103 | addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3); |
| 104 | return (unsigned long *)addr; |
| 105 | } |
| 106 | |
| 107 | static inline unsigned char * |
| 108 | __bitops_byte(unsigned long nr, volatile unsigned long *ptr) |
| 109 | { |
| 110 | return ((unsigned char *)ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3); |
| 111 | } |
| 112 | |
| 113 | static inline void set_bit(unsigned long nr, volatile unsigned long *ptr) |
| 114 | { |
| 115 | unsigned long *addr = __bitops_word(nr, ptr); |
| 116 | unsigned long mask; |
| 117 | |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 118 | #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES |
| 119 | if (__builtin_constant_p(nr)) { |
| 120 | unsigned char *caddr = __bitops_byte(nr, ptr); |
| 121 | |
| 122 | asm volatile( |
| 123 | "oi %0,%b1\n" |
| 124 | : "+Q" (*caddr) |
| 125 | : "i" (1 << (nr & 7)) |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 126 | : "cc", "memory"); |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | #endif |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 130 | mask = 1UL << (nr & (BITS_PER_LONG - 1)); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 131 | __BITOPS_LOOP(addr, mask, __BITOPS_OR, __BITOPS_NO_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 134 | static inline void clear_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 136 | unsigned long *addr = __bitops_word(nr, ptr); |
| 137 | unsigned long mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 139 | #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES |
| 140 | if (__builtin_constant_p(nr)) { |
| 141 | unsigned char *caddr = __bitops_byte(nr, ptr); |
| 142 | |
| 143 | asm volatile( |
| 144 | "ni %0,%b1\n" |
| 145 | : "+Q" (*caddr) |
| 146 | : "i" (~(1 << (nr & 7))) |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 147 | : "cc", "memory"); |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | #endif |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 151 | mask = ~(1UL << (nr & (BITS_PER_LONG - 1))); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 152 | __BITOPS_LOOP(addr, mask, __BITOPS_AND, __BITOPS_NO_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 155 | static inline void change_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 157 | unsigned long *addr = __bitops_word(nr, ptr); |
| 158 | unsigned long mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 160 | #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES |
| 161 | if (__builtin_constant_p(nr)) { |
| 162 | unsigned char *caddr = __bitops_byte(nr, ptr); |
| 163 | |
| 164 | asm volatile( |
| 165 | "xi %0,%b1\n" |
| 166 | : "+Q" (*caddr) |
| 167 | : "i" (1 << (nr & 7)) |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 168 | : "cc", "memory"); |
Heiko Carstens | 4ae8032 | 2013-09-17 09:48:44 +0200 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | #endif |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 172 | mask = 1UL << (nr & (BITS_PER_LONG - 1)); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 173 | __BITOPS_LOOP(addr, mask, __BITOPS_XOR, __BITOPS_NO_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 177 | test_and_set_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 179 | unsigned long *addr = __bitops_word(nr, ptr); |
| 180 | unsigned long old, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 182 | mask = 1UL << (nr & (BITS_PER_LONG - 1)); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 183 | old = __BITOPS_LOOP(addr, mask, __BITOPS_OR, __BITOPS_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return (old & mask) != 0; |
| 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 188 | test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 190 | unsigned long *addr = __bitops_word(nr, ptr); |
| 191 | unsigned long old, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 193 | mask = ~(1UL << (nr & (BITS_PER_LONG - 1))); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 194 | old = __BITOPS_LOOP(addr, mask, __BITOPS_AND, __BITOPS_BARRIER); |
Heiko Carstens | e344e52 | 2013-09-10 15:35:39 +0200 | [diff] [blame] | 195 | return (old & ~mask) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 199 | test_and_change_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 201 | unsigned long *addr = __bitops_word(nr, ptr); |
| 202 | unsigned long old, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Akinobu Mita | 01c2475 | 2013-03-23 23:05:29 +0900 | [diff] [blame] | 204 | mask = 1UL << (nr & (BITS_PER_LONG - 1)); |
Heiko Carstens | 0ccc8b7 | 2014-03-20 08:55:00 +0100 | [diff] [blame] | 205 | old = __BITOPS_LOOP(addr, mask, __BITOPS_XOR, __BITOPS_BARRIER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | return (old & mask) != 0; |
| 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr) |
| 210 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 211 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 213 | *addr |= 1 << (nr & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | __clear_bit(unsigned long nr, volatile unsigned long *ptr) |
| 218 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 219 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 221 | *addr &= ~(1 << (nr & 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr) |
| 225 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 226 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 228 | *addr ^= 1 << (nr & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 232 | __test_and_set_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 234 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | unsigned char ch; |
| 236 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 237 | ch = *addr; |
| 238 | *addr |= 1 << (nr & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return (ch >> (nr & 7)) & 1; |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 243 | __test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 245 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | unsigned char ch; |
| 247 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 248 | ch = *addr; |
| 249 | *addr &= ~(1 << (nr & 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return (ch >> (nr & 7)) & 1; |
| 251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | static inline int |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 254 | __test_and_change_bit(unsigned long nr, volatile unsigned long *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 256 | unsigned char *addr = __bitops_byte(nr, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | unsigned char ch; |
| 258 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 259 | ch = *addr; |
| 260 | *addr ^= 1 << (nr & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | return (ch >> (nr & 7)) & 1; |
| 262 | } |
| 263 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 264 | static inline int test_bit(unsigned long nr, const volatile unsigned long *ptr) |
| 265 | { |
| 266 | const volatile unsigned char *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Heiko Carstens | 370b0b5 | 2013-09-16 16:22:05 +0200 | [diff] [blame] | 268 | addr = ((const volatile unsigned char *)ptr); |
| 269 | addr += (nr ^ (BITS_PER_LONG - 8)) >> 3; |
| 270 | return (*addr >> (nr & 7)) & 1; |
| 271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Martin Schwidefsky | acdc9fc | 2015-08-20 12:57:33 +0200 | [diff] [blame] | 273 | static inline int test_and_set_bit_lock(unsigned long nr, |
| 274 | volatile unsigned long *ptr) |
| 275 | { |
| 276 | if (test_bit(nr, ptr)) |
| 277 | return 1; |
| 278 | return test_and_set_bit(nr, ptr); |
| 279 | } |
| 280 | |
| 281 | static inline void clear_bit_unlock(unsigned long nr, |
| 282 | volatile unsigned long *ptr) |
| 283 | { |
| 284 | smp_mb__before_atomic(); |
| 285 | clear_bit(nr, ptr); |
| 286 | } |
| 287 | |
| 288 | static inline void __clear_bit_unlock(unsigned long nr, |
| 289 | volatile unsigned long *ptr) |
| 290 | { |
| 291 | smp_mb(); |
| 292 | __clear_bit(nr, ptr); |
| 293 | } |
| 294 | |
Martin Schwidefsky | afff7e2 | 2005-07-27 11:44:58 -0700 | [diff] [blame] | 295 | /* |
Heiko Carstens | 7d7c7b2 | 2013-09-23 12:01:44 +0200 | [diff] [blame] | 296 | * Functions which use MSB0 bit numbering. |
Heiko Carstens | 48002bd | 2015-10-08 13:51:44 +0200 | [diff] [blame] | 297 | * The bits are numbered: |
Heiko Carstens | 7d7c7b2 | 2013-09-23 12:01:44 +0200 | [diff] [blame] | 298 | * |0..............63|64............127|128...........191|192...........255| |
Martin Schwidefsky | afff7e2 | 2005-07-27 11:44:58 -0700 | [diff] [blame] | 299 | */ |
Heiko Carstens | 7d7c7b2 | 2013-09-23 12:01:44 +0200 | [diff] [blame] | 300 | unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size); |
| 301 | unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size, |
| 302 | unsigned long offset); |
| 303 | |
| 304 | static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr) |
| 305 | { |
| 306 | return set_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 307 | } |
| 308 | |
| 309 | static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr) |
| 310 | { |
| 311 | return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 312 | } |
| 313 | |
| 314 | static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr) |
| 315 | { |
| 316 | return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 317 | } |
| 318 | |
| 319 | static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr) |
| 320 | { |
| 321 | return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 322 | } |
| 323 | |
| 324 | static inline int test_bit_inv(unsigned long nr, |
| 325 | const volatile unsigned long *ptr) |
| 326 | { |
| 327 | return test_bit(nr ^ (BITS_PER_LONG - 1), ptr); |
| 328 | } |
Martin Schwidefsky | afff7e2 | 2005-07-27 11:44:58 -0700 | [diff] [blame] | 329 | |
Heiko Carstens | b1cb7e2 | 2013-09-19 11:05:32 +0200 | [diff] [blame] | 330 | #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES |
| 331 | |
| 332 | /** |
| 333 | * __flogr - find leftmost one |
| 334 | * @word - The word to search |
| 335 | * |
| 336 | * Returns the bit number of the most significant bit set, |
| 337 | * where the most significant bit has bit number 0. |
| 338 | * If no bit is set this function returns 64. |
| 339 | */ |
| 340 | static inline unsigned char __flogr(unsigned long word) |
| 341 | { |
| 342 | if (__builtin_constant_p(word)) { |
| 343 | unsigned long bit = 0; |
| 344 | |
| 345 | if (!word) |
| 346 | return 64; |
| 347 | if (!(word & 0xffffffff00000000UL)) { |
| 348 | word <<= 32; |
| 349 | bit += 32; |
| 350 | } |
| 351 | if (!(word & 0xffff000000000000UL)) { |
| 352 | word <<= 16; |
| 353 | bit += 16; |
| 354 | } |
| 355 | if (!(word & 0xff00000000000000UL)) { |
| 356 | word <<= 8; |
| 357 | bit += 8; |
| 358 | } |
| 359 | if (!(word & 0xf000000000000000UL)) { |
| 360 | word <<= 4; |
| 361 | bit += 4; |
| 362 | } |
| 363 | if (!(word & 0xc000000000000000UL)) { |
| 364 | word <<= 2; |
| 365 | bit += 2; |
| 366 | } |
| 367 | if (!(word & 0x8000000000000000UL)) { |
| 368 | word <<= 1; |
| 369 | bit += 1; |
| 370 | } |
| 371 | return bit; |
| 372 | } else { |
| 373 | register unsigned long bit asm("4") = word; |
| 374 | register unsigned long out asm("5"); |
| 375 | |
| 376 | asm volatile( |
| 377 | " flogr %[bit],%[bit]\n" |
| 378 | : [bit] "+d" (bit), [out] "=d" (out) : : "cc"); |
| 379 | return bit; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * __ffs - find first bit in word. |
| 385 | * @word: The word to search |
| 386 | * |
| 387 | * Undefined if no bit exists, so code should check against 0 first. |
| 388 | */ |
| 389 | static inline unsigned long __ffs(unsigned long word) |
| 390 | { |
| 391 | return __flogr(-word & word) ^ (BITS_PER_LONG - 1); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * ffs - find first bit set |
| 396 | * @word: the word to search |
| 397 | * |
| 398 | * This is defined the same way as the libc and |
| 399 | * compiler builtin ffs routines (man ffs). |
| 400 | */ |
| 401 | static inline int ffs(int word) |
| 402 | { |
| 403 | unsigned long mask = 2 * BITS_PER_LONG - 1; |
| 404 | unsigned int val = (unsigned int)word; |
| 405 | |
| 406 | return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * __fls - find last (most-significant) set bit in a long word |
| 411 | * @word: the word to search |
| 412 | * |
| 413 | * Undefined if no set bit exists, so code should check against 0 first. |
| 414 | */ |
| 415 | static inline unsigned long __fls(unsigned long word) |
| 416 | { |
| 417 | return __flogr(word) ^ (BITS_PER_LONG - 1); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * fls64 - find last set bit in a 64-bit word |
| 422 | * @word: the word to search |
| 423 | * |
| 424 | * This is defined in a similar way as the libc and compiler builtin |
| 425 | * ffsll, but returns the position of the most significant set bit. |
| 426 | * |
| 427 | * fls64(value) returns 0 if value is 0 or the position of the last |
| 428 | * set bit if value is nonzero. The last (most significant) bit is |
| 429 | * at position 64. |
| 430 | */ |
| 431 | static inline int fls64(unsigned long word) |
| 432 | { |
| 433 | unsigned long mask = 2 * BITS_PER_LONG - 1; |
| 434 | |
| 435 | return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * fls - find last (most-significant) bit set |
| 440 | * @word: the word to search |
| 441 | * |
| 442 | * This is defined the same way as ffs. |
| 443 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. |
| 444 | */ |
| 445 | static inline int fls(int word) |
| 446 | { |
| 447 | return fls64((unsigned int)word); |
| 448 | } |
| 449 | |
| 450 | #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */ |
| 451 | |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 452 | #include <asm-generic/bitops/__ffs.h> |
| 453 | #include <asm-generic/bitops/ffs.h> |
Alexander van Heukelum | 56a6b1e | 2008-03-15 18:31:49 +0100 | [diff] [blame] | 454 | #include <asm-generic/bitops/__fls.h> |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 455 | #include <asm-generic/bitops/fls.h> |
Akinobu Mita | 7e33db4 | 2006-03-26 01:39:34 -0800 | [diff] [blame] | 456 | #include <asm-generic/bitops/fls64.h> |
Heiko Carstens | b1cb7e2 | 2013-09-19 11:05:32 +0200 | [diff] [blame] | 457 | |
| 458 | #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */ |
| 459 | |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 460 | #include <asm-generic/bitops/ffz.h> |
| 461 | #include <asm-generic/bitops/find.h> |
Akinobu Mita | 7e33db4 | 2006-03-26 01:39:34 -0800 | [diff] [blame] | 462 | #include <asm-generic/bitops/hweight.h> |
Heiko Carstens | 746479c | 2013-09-18 11:45:36 +0200 | [diff] [blame] | 463 | #include <asm-generic/bitops/sched.h> |
Akinobu Mita | 802caab | 2011-05-26 16:26:12 -0700 | [diff] [blame] | 464 | #include <asm-generic/bitops/le.h> |
Akinobu Mita | 148817b | 2011-07-26 16:09:04 -0700 | [diff] [blame] | 465 | #include <asm-generic/bitops/ext2-atomic-setbit.h> |
Akinobu Mita | 50b9b47 | 2011-03-23 16:41:57 -0700 | [diff] [blame] | 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | #endif /* _S390_BITOPS_H */ |