XenevaOS
Loading...
Searching...
No Matches
bitops.h
Go to the documentation of this file.
1#ifndef __LINUX_BITOPS_H__
2#define __LINUX_BITOPS_H__
3
4/*
5 * DCL <linux/bitops.h> -- bit arithmetic.
6 *
7 * tty_buffer.c includes this for one thing: __ALIGN_MASK(), which rounds a
8 * requested buffer length up to the next 256-byte boundary
9 * (`size = __ALIGN_MASK(size, TTYB_ALIGN_MASK)` at tty_buffer.c:172). That
10 * rounding is what lets the free list work at all -- a recycled buffer is only
11 * reused when the new request is <= MIN_TTYB_SIZE, and both sides have to
12 * agree on the granularity or a 257-byte request silently gets a 256-byte
13 * buffer.
14 *
15 * <linux/kernel.h> already has the same operation spelled __ALIGN_KERNEL_MASK
16 * with an extra typeof() cast; __ALIGN_MASK is mainline's name for it, kept
17 * here rather than in kernel.h because nothing outside a ported file has ever
18 * asked for that spelling.
19 *
20 * The bit-search helpers used to be deliberately absent, on the grounds that
21 * no file in the staged tty/serial set called them. drm_drv.c:574 now does --
22 * for_each_set_bit() over the wedge-recovery bitmask in drm_dev_wedged_event()
23 * -- so they are here, and the reasoning is restated rather than deleted: a
24 * bitmap search still has no caller that would notice how it is implemented,
25 * only one that needs the right bit number back. The loops below are a linear
26 * walk; mainline's find_next_bit() reads a word at a time and skips whole
27 * zero words. That difference is invisible at the one call site, whose
28 * bitmap is a single unsigned long, and would stop being invisible the day a
29 * driver scans a large mask in a hot path -- at which point the word-wise
30 * version belongs here, not a faster loop over the same array.
31 */
32
33/*
34 * BITS_PER_LONG and DECLARE_BITMAP sit above the kernel.h include, which is
35 * not where either would naturally go. kernel.h pulls in a wide subtree
36 * (virtio.h, completion.h, wait.h, sched.h) and nodemask_types.h sits at the
37 * far end of it, already using DECLARE_BITMAP(bits, MAX_NUMNODES). By the time
38 * that chain reaches it this header's own include guard is set, so
39 * nodemask_types.h's #include <linux/bitops.h> is a no-op and a definition
40 * placed below would not have been seen. Defining them first is what makes
41 * that chain parse.
42 *
43 * For DECLARE_BITMAP that is also the only order that works, not merely one
44 * that does: <linux/bitmap.h> includes this header at its top, so a file
45 * starting there arrives with DECLARE_BITMAP undefined and bitmap.h's own copy
46 * -- mainline's home for it -- not yet reached. It has to live here.
47 *
48 * BITS_PER_LONG is a fallback only -- <linux/kernel.h> also defines it. Kept
49 * in step with kernel.h (sizeof(long)*8, not 64): this target is LLP64, see
50 * the note there for what a hardcoded 64 does to the word index and the shift
51 * below.
52 */
53#ifndef BITS_PER_LONG
54/* 32 -- the value above, restated as a #if-readable number so the two
55 * headers agree token for token when a file includes both (they are
56 * otherwise identical and would trip -Wmacro-redefined). */
57#define BITS_PER_LONG 32
58#endif
59
60/* BITS_TO_LONGS is spelled out rather than called: its definition is below the
61 * include above, and this one has to precede it. The formulas have to agree. */
62#ifndef DECLARE_BITMAP
63#define DECLARE_BITMAP(name, bits) \
64 unsigned long name[(bits + BITS_PER_LONG - 1) / BITS_PER_LONG]
65#endif
66
67/*
68 * fls and fls_long are defined below with the rest of the word-scan family,
69 * but declared here, immediately above the kernel.h include, because
70 * <linux/log2.h> -- reached through kernel.h:89 -- calls both as it is
71 * parsed: fls() in __ilog2_u32() at log2.h:24, fls_long() in
72 * __roundup_pow_of_two()/__rounddown_pow_of_two() at log2.h:57/:67. A file
73 * that includes this header before kernel.h (drm_drv.c:29 is the one in the
74 * DRM set) re-enters this header from log2.h:12 while it is itself parked on
75 * that include; the include guard makes the re-entry a no-op, so without
76 * these lines log2.h's calls are parsed with no declaration in sight. The
77 * calls stay quiet under -Wno-implicit-function-declaration, but the
78 * definition below is then a conflicting type for fls_long -- one hard
79 * error, in exactly that one object. The declarations cost nothing: same
80 * types as the definitions, no code generated.
81 */
82static inline int fls(unsigned int x);
83static inline unsigned int fls_long(unsigned long l);
84
85#include <linux/kernel.h>
86/* CHAR_BIT, for BITS_PER_TYPE(). DCL's is <limits.h> at the BaseHdr root:
87 * linux/limits.h is the kernel's own limits and carries no CHAR_BIT. */
88#include <limits.h>
89
90#ifndef __ALIGN_MASK
91#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
92#endif
93
94/*
95 * IS_ALIGNED(x, a) -- "x is a multiple of a". mainline keeps it in
96 * <linux/kernel.h> next to ALIGN(); DCL's kernel.h has ALIGN() but not this,
97 * and both callers in the DRM set reach bitops.h: drm_dumb_buffers.c:81
98 * rejects a driver hw_size_align that is not a page multiple (the size
99 * rounding at :86 would otherwise quietly round to the driver's own
100 * granularity), drm_file.c:855 stops the KiB unit walk once the byte count
101 * stops being a multiple of SZ_1K, so the number printed by drm_fdinfo is
102 * never fractional. The cast on `a` is mainline's; both callers pass an int
103 * constant, so it changes nothing here, but it is kept so the macro answers
104 * like the kernel.h spelling it stands in for when a future caller passes a
105 * mask wider than x.
106 */
107#ifndef IS_ALIGNED
108#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
109#endif
110
111#ifndef BIT
112#define BIT(nr) (1UL << (nr))
113#endif
114
115#ifndef BIT_MASK
116#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
117#endif
118
119#ifndef BITS_TO_LONGS
120#define BITS_TO_LONGS(nr) (((nr) + BITS_PER_LONG - 1) / BITS_PER_LONG)
121#endif
122
123#ifndef test_bit
124#define test_bit(nr, addr) \
125 (((unsigned long*)(addr))[(nr) / BITS_PER_LONG] & \
126 (1UL << ((nr) % BITS_PER_LONG)))
127#endif
128
129#ifndef set_bit
130#define set_bit(nr, addr) \
131 (((unsigned long*)(addr))[(nr) / BITS_PER_LONG] |= \
132 (1UL << ((nr) % BITS_PER_LONG)))
133#endif
134
135#ifndef clear_bit
136#define clear_bit(nr, addr) \
137 (((unsigned long*)(addr))[(nr) / BITS_PER_LONG] &= \
138 ~(1UL << ((nr) % BITS_PER_LONG)))
139#endif
140
141/*
142 * BIT_WORD, __ffs, __set_bit, __test_and_set_bit, __test_and_clear_bit --
143 * the __-prefixed base operations, which are a family beside the macros above
144 * rather than another spelling of them.
145 *
146 * mainline's split between the two is atomicity: set_bit() may be atomic on
147 * some architectures and __set_bit() explicitly is not, and a caller picks the
148 * __ form when it already holds the lock or does not need one. DCL's
149 * set_bit() above is already a plain |= with no barrier, so the two are the
150 * same operation here -- but they stay two names, because the callers are
151 * mainline's (xarray.c:95, :102, :135 and drm_mm.c:478) and a later port that
152 * does need the distinction should not find half the family missing.
153 *
154 * __ffs() is index-of-first-set-bit, and mainline documents the answer as
155 * undefined for a word of 0: drm_mm.c:1887 calls it on `last + 1`, which the
156 * surrounding loop has already established is non-zero. __builtin_ctzl is
157 * what mainline's generic version reaches for as well; `long` is 32 bits on
158 * this target and the builtin follows the type, so the width is right.
159 *
160 * __test_and_*_bit() return the bit's previous state, which is what makes the
161 * xarray mark code read as it does -- node_set_mark() answers "was it already
162 * set" from the return, not from a second read.
163 */
164#ifndef BIT_WORD
165#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
166#endif
167
168static inline unsigned long __ffs(unsigned long word)
169{
170 return (unsigned long)__builtin_ctzl(word);
171}
172
173static inline void __set_bit(int nr, volatile unsigned long* addr)
174{
175 addr[BIT_WORD(nr)] |= BIT_MASK(nr);
176}
177
178static inline int __test_and_set_bit(int nr, volatile unsigned long* addr)
179{
180 unsigned long* p = (unsigned long*)addr + BIT_WORD(nr);
181 unsigned long old = *p;
182 unsigned long mask = BIT_MASK(nr);
183
184 *p = old | mask;
185 return (old & mask) != 0;
186}
187
188static inline int __test_and_clear_bit(int nr, volatile unsigned long* addr)
189{
190 unsigned long* p = (unsigned long*)addr + BIT_WORD(nr);
191 unsigned long old = *p;
192 unsigned long mask = BIT_MASK(nr);
193
194 *p = old & ~mask;
195 return (old & mask) != 0;
196}
197
198/*
199 * __clear_bit and clear_bit_unlock -- the two clears the DRM set calls by
200 * name, beside the set/test family above.
201 *
202 * __clear_bit is the non-atomic member: drm_mm.c:844 drops
203 * DRM_MM_NODE_SCANNED_BIT after a scan block is removed, with the scan list
204 * already private to the caller. Its body is __set_bit's mirror; written as
205 * a typed inline rather than reusing the clear_bit macro so the address
206 * keeps the same information __set_bit already gets.
207 *
208 * clear_bit_unlock is mainline's name for "clear it and let it be seen"
209 * when releasing a lock -- drm_mm.c:648 clears DRM_MM_NODE_ALLOCATED_BIT as
210 * the node is freed, and any concurrent scanner must not miss that. On this
211 * uniprocessor target every bit operation is already a plain
212 * read-modify-write with no barrier to order it against, so the two are the
213 * same instruction sequence; keeping the name means the vendored call site
214 * does not have to be rewritten to say so, and the day this port gains an
215 * SMP build the barrier belongs in exactly this one place.
216 */
217static inline void __clear_bit(int nr, volatile unsigned long* addr)
218{
219 addr[BIT_WORD(nr)] &= ~BIT_MASK(nr);
220}
221
222static inline void clear_bit_unlock(int nr, volatile unsigned long* addr)
223{
224 __clear_bit(nr, addr);
225}
226
227/*
228 * assign_bit -- set or clear according to a value, in one expression.
229 * tty_port.h's flow-control setters are written against it (set_cts_flow,
230 * set_check_carrier, set_initialized, ...), all of which take a bool and are
231 * called with a literal. Written as a statement-macro rather than a function
232 * so `addr` keeps whatever type it arrived with -- tty_port_active() reads
233 * through a const pointer and tty_port_set_active() through a mutable one,
234 * and a function taking unsigned long* would force a cast on the first.
235 */
236#ifndef assign_bit
237#define assign_bit(nr, addr, val) \
238 do { \
239 if (val) \
240 set_bit((nr), (addr)); \
241 else \
242 clear_bit((nr), (addr)); \
243 } while (0)
244#endif
245
246/*
247 * The bit-search helpers mainline keeps in lib/find_bit.c. Here they are
248 * static inline in the header, because this tree has no lib/find_bit.c and a
249 * non-inline definition would need exactly one object file to hold it -- and
250 * the file that would need it (drm_drv.c) cannot be edited.
251 *
252 * Both find_next_* take the bitmap as `const unsigned long *`, which is the
253 * type drm_dev_wedged_event() passes (`&method`, an unsigned long). test_bit
254 * is the macro above and casts away the const itself, so the loops below need
255 * no cast of their own.
256 */
257#ifndef BITS_PER_TYPE
258#define BITS_PER_TYPE(type) (sizeof(type) * CHAR_BIT)
259#endif
260
261static inline unsigned long find_next_bit(const unsigned long *addr,
262 unsigned long size,
263 unsigned long offset)
264{
265 unsigned long i;
266
267 for (i = offset; i < size; i++)
268 if (test_bit(i, addr))
269 return i;
270
271 return size;
272}
273
274static inline unsigned long find_next_zero_bit(const unsigned long *addr,
275 unsigned long size,
276 unsigned long offset)
277{
278 unsigned long i;
279
280 for (i = offset; i < size; i++)
281 if (!test_bit(i, addr))
282 return i;
283
284 return size;
285}
286
287static inline unsigned long find_first_bit(const unsigned long *addr,
288 unsigned long size)
289{
290 return find_next_bit(addr, size, 0);
291}
292
293static inline unsigned long find_first_zero_bit(const unsigned long *addr,
294 unsigned long size)
295{
296 return find_next_zero_bit(addr, size, 0);
297}
298
299/*
300 * ffs, fls, fls_long, hweight32/hweight64 -- word scans and population
301 * counts, the second family of bit arithmetic the DRM set reached for.
302 *
303 * mainline gets ffs and fls from <asm-generic/bitops/>; both reduce to the
304 * two builtins used here. __builtin_ffs() answers 0 for 0, which is ffs()'s
305 * contract; fls() must answer 0 too and __builtin_clz() is undefined for 0,
306 * hence the ternary in front of it. drm_blend.c:352 turns the lowest set
307 * rotation bit into an array index (ffs of a mask), and
308 * drm_client_modeset.c:191 asks for the highest set rotation bit (fls).
309 *
310 * fls_long is fls() for this target's four-byte long, and the 64-bit arm is
311 * removed by the preprocessor rather than left behind a runtime
312 * `if (BITS_PER_LONG == 64)`: both arms of such a condition would have to
313 * compile, and DCL carries no fls64(). dma-resv.c:335 (the fence-array
314 * growth estimate) is the only caller.
315 *
316 * hweight32/hweight64 are the popcounts. drm_crtc.c:991 and
317 * drm_crtc_helper.c:507 ask whether a crtc can be driven by more than one
318 * encoder, drm_connector.c:2444/:3351 count encoders and scaling modes, and
319 * drm_property.c:218 sizes a bitmask property by how many bits its
320 * validation mask has. The builtins follow the argument type, so the 32/64
321 * split is exactly what each name promises.
322 */
323#ifndef ffs
324#define ffs(x) __builtin_ffs(x)
325#endif
326
327static inline int fls(unsigned int x)
328{
329 return x ? 32 - __builtin_clz(x) : 0;
330}
331
332static inline unsigned int fls_long(unsigned long l)
333{
334#if BITS_PER_LONG == 64
335 return fls64(l); /* DCL has no fls64(); unreachable at 32 */
336#else
337 return (unsigned int)fls(l);
338#endif
339}
340
341#ifndef hweight32
342#define hweight32(w) __builtin_popcount(w)
343#endif
344
345#ifndef hweight64
346#define hweight64(w) __builtin_popcountll(w)
347#endif
348
349#ifndef change_bit
350#define change_bit(nr, addr) __change_bit((nr), (addr))
351#endif
352#ifndef __change_bit
353static inline void __change_bit(int nr, volatile unsigned long *addr)
354{
355 unsigned long mask = 1UL << (nr & (BITS_PER_LONG - 1));
356 unsigned long *p = (unsigned long *)addr + (nr / BITS_PER_LONG);
357 *p ^= mask;
358}
359#endif
360
361#ifndef bitmap_weight
362static inline unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
363{
364 unsigned int w = 0;
365 for (unsigned int i = 0; i < nbits; i++)
366 w += test_bit(i, src) ? 1 : 0;
367 return w;
368}
369#endif
370#ifndef bitmap_subset
371static inline bool bitmap_subset(const unsigned long *src1,
372 const unsigned long *src2, unsigned int nbits)
373{
374 for (unsigned int i = 0; i < nbits; i++)
375 if (test_bit(i, src1) && !test_bit(i, src2))
376 return false;
377 return true;
378}
379#endif
380
381#ifndef for_each_set_bit_from
382#define for_each_set_bit_from(bit, addr, size) \
383 for (; (bit) = find_next_bit((addr), (size), (bit)), \
384 (bit) < (size); (bit)++)
385#endif
386
387#ifndef for_each_set_bit
388#define for_each_set_bit(bit, addr, size) \
389 for ((bit) = find_first_bit((addr), (size)); \
390 (bit) < (size); \
391 (bit) = find_next_bit((addr), (size), (bit) + 1))
392#endif
393
394#ifndef for_each_clear_bit
395#define for_each_clear_bit(bit, addr, size) \
396 for ((bit) = find_first_zero_bit((addr), (size)); \
397 (bit) < (size); \
398 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
399#endif
400
401/*
402 * Two additions at the very bottom -- reachability, not anything bitops.h
403 * itself uses, which is why they sit after every definition above rather
404 * than at the top with the other includes.
405 *
406 * <linux/bug.h> is how every WARN_ONCE() user reaches the macro. bug.h
407 * already arrives directly in most of them, but drm_cache.c,
408 * drm_plane_helper.c and drm_probe_helper.c never include it, and WARN_ONCE
409 * has to be a header macro rather than a DCL shim: the whole point is
410 * splicing fmt and __VA_ARGS__ into the report at the call site. bitops.h is
411 * a header all three already include, and every other WARN_ONCE user reaches
412 * it too, so one include here covers all fifteen objects. bug.h's own
413 * <asm/bug.h> walks on to <linux/kernel.h>, whose guard the include at the
414 * top of this file already spent; by the time this line runs that is a
415 * no-op.
416 *
417 * le32_to_cpup is drm_format_helper.c's pointer load (drm_format_helper.c:
418 * 265-268 and the xfrm line helpers below them). mainline gets it from
419 * <linux/kernel.h>:38 including <asm/byteorder.h>; DCL's kernel.h does not
420 * carry that include, and drm_format_helper.c reaches the byteorder layer
421 * through nothing else. Pulling <asm/byteorder.h> in here would also work,
422 * but it would put generic.h's object-like le16_to_cpu()/cpu_to_le16()
423 * definitions in front of the function-like ones <linux/types.h>:129
424 * already publishes, turning six -Wmacro-redefined warnings into a line in
425 * every object this tree builds. Only the one name is needed, so only the
426 * one name is defined -- spelled exactly as generic.h:101 spells it, so a
427 * file that reaches generic.h afterwards sees an identical redefinition and
428 * no warning. The uapi header supplies the static inline __le32_to_cpup()
429 * the alias points at, and is already part of every include chain that saw
430 * generic.h; elsewhere it is one more include guard.
431 */
432#include <linux/bug.h>
433
434#ifndef le32_to_cpup
435#include <uapi/linux/byteorder/little_endian.h> /* __le32_to_cpup */
436#define le32_to_cpup __le32_to_cpup
437#endif
438
439#endif /* __LINUX_BITOPS_H__ */
#define BITS_PER_LONG
Definition bitops.h:57
#define BIT_MASK(nr)
Definition bitops.h:116
#define BIT_WORD(nr)
Definition bitops.h:165
#define test_bit(nr, addr)
Definition bitops.h:124
size_t size
Definition drm_bridge.h:1299
size_t size_t offset
Definition drm_bridge.h:1299
int x
Definition layout.c:118
int w
Definition layout.c:118