source: rtems-libbsd/rtemsbsd/powerpc/include/linux/bitops.h @ 7f1f428

55-freebsd-126-freebsd-12
Last change on this file since 7f1f428 was cd089b9, checked in by Sebastian Huber <sebastian.huber@…>, on 05/05/17 at 06:47:39

Linux update to 4.11-rc5

Linux baseline a71c9a1c779f2499fb2afc0553e543f18aff6edf (4.11-rc5).

  • Property mode set to 100644
File size: 10.8 KB
Line 
1/*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice unmodified, this list of conditions, and the following
13 *    disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31#ifndef _LINUX_BITOPS_H_
32#define _LINUX_BITOPS_H_
33
34#include <sys/param.h>
35#include <sys/systm.h>
36
37#define BIT(nr)                 (1UL << (nr))
38#ifdef __LP64__
39#define BITS_PER_LONG           64
40#else
41#define BITS_PER_LONG           32
42#endif
43#define BITMAP_FIRST_WORD_MASK(start)   (~0UL << ((start) % BITS_PER_LONG))
44#define BITMAP_LAST_WORD_MASK(n)        (~0UL >> (BITS_PER_LONG - (n)))
45#define BITS_TO_LONGS(n)        howmany((n), BITS_PER_LONG)
46#define BIT_MASK(nr)            (1UL << ((nr) & (BITS_PER_LONG - 1)))
47#define BIT_WORD(nr)            ((nr) / BITS_PER_LONG)
48#define GENMASK(hi, lo)         (((2UL << ((hi) - (lo))) - 1UL) << (lo))
49#define BITS_PER_BYTE           8
50
51static inline int
52__ffs(int mask)
53{
54        return (ffs(mask) - 1);
55}
56
57static inline int
58__fls(int mask)
59{
60        return (fls(mask) - 1);
61}
62
63static inline int
64__ffsl(long mask)
65{
66        return (ffsl(mask) - 1);
67}
68
69static inline int
70__flsl(long mask)
71{
72        return (flsl(mask) - 1);
73}
74
75
76#define ffz(mask)       __ffs(~(mask))
77
78static inline int get_count_order(unsigned int count)
79{
80        int order;
81
82        order = fls(count) - 1;
83        if (count & (count - 1))
84                order++;
85        return order;
86}
87
88static inline unsigned long
89find_first_bit(unsigned long *addr, unsigned long size)
90{
91        long mask;
92        int bit;
93
94        for (bit = 0; size >= BITS_PER_LONG;
95            size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
96                if (*addr == 0)
97                        continue;
98                return (bit + __ffsl(*addr));
99        }
100        if (size) {
101                mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
102                if (mask)
103                        bit += __ffsl(mask);
104                else
105                        bit += size;
106        }
107        return (bit);
108}
109
110static inline unsigned long
111find_first_zero_bit(unsigned long *addr, unsigned long size)
112{
113        long mask;
114        int bit;
115
116        for (bit = 0; size >= BITS_PER_LONG;
117            size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
118                if (~(*addr) == 0)
119                        continue;
120                return (bit + __ffsl(~(*addr)));
121        }
122        if (size) {
123                mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
124                if (mask)
125                        bit += __ffsl(mask);
126                else
127                        bit += size;
128        }
129        return (bit);
130}
131
132static inline unsigned long
133find_last_bit(unsigned long *addr, unsigned long size)
134{
135        long mask;
136        int offs;
137        int bit;
138        int pos;
139
140        pos = size / BITS_PER_LONG;
141        offs = size % BITS_PER_LONG;
142        bit = BITS_PER_LONG * pos;
143        addr += pos;
144        if (offs) {
145                mask = (*addr) & BITMAP_LAST_WORD_MASK(offs);
146                if (mask)
147                        return (bit + __flsl(mask));
148        }
149        while (--pos) {
150                addr--;
151                bit -= BITS_PER_LONG;
152                if (*addr)
153                        return (bit + __flsl(mask));
154        }
155        return (size);
156}
157
158static inline unsigned long
159find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset)
160{
161        long mask;
162        int offs;
163        int bit;
164        int pos;
165
166        if (offset >= size)
167                return (size);
168        pos = offset / BITS_PER_LONG;
169        offs = offset % BITS_PER_LONG;
170        bit = BITS_PER_LONG * pos;
171        addr += pos;
172        if (offs) {
173                mask = (*addr) & ~BITMAP_LAST_WORD_MASK(offs);
174                if (mask)
175                        return (bit + __ffsl(mask));
176                if (size - bit <= BITS_PER_LONG)
177                        return (size);
178                bit += BITS_PER_LONG;
179                addr++;
180        }
181        for (size -= bit; size >= BITS_PER_LONG;
182            size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
183                if (*addr == 0)
184                        continue;
185                return (bit + __ffsl(*addr));
186        }
187        if (size) {
188                mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
189                if (mask)
190                        bit += __ffsl(mask);
191                else
192                        bit += size;
193        }
194        return (bit);
195}
196
197static inline unsigned long
198find_next_zero_bit(unsigned long *addr, unsigned long size,
199    unsigned long offset)
200{
201        long mask;
202        int offs;
203        int bit;
204        int pos;
205
206        if (offset >= size)
207                return (size);
208        pos = offset / BITS_PER_LONG;
209        offs = offset % BITS_PER_LONG;
210        bit = BITS_PER_LONG * pos;
211        addr += pos;
212        if (offs) {
213                mask = ~(*addr) & ~BITMAP_LAST_WORD_MASK(offs);
214                if (mask)
215                        return (bit + __ffsl(mask));
216                if (size - bit <= BITS_PER_LONG)
217                        return (size);
218                bit += BITS_PER_LONG;
219                addr++;
220        }
221        for (size -= bit; size >= BITS_PER_LONG;
222            size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
223                if (~(*addr) == 0)
224                        continue;
225                return (bit + __ffsl(~(*addr)));
226        }
227        if (size) {
228                mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
229                if (mask)
230                        bit += __ffsl(mask);
231                else
232                        bit += size;
233        }
234        return (bit);
235}
236
237static inline void
238bitmap_zero(unsigned long *addr, int size)
239{
240        int len;
241
242        len = BITS_TO_LONGS(size) * sizeof(long);
243        memset(addr, 0, len);
244}
245
246static inline void
247bitmap_fill(unsigned long *addr, int size)
248{
249        int tail;
250        int len;
251
252        len = (size / BITS_PER_LONG) * sizeof(long);
253        memset(addr, 0xff, len);
254        tail = size & (BITS_PER_LONG - 1);
255        if (tail)
256                addr[size / BITS_PER_LONG] = BITMAP_LAST_WORD_MASK(tail);
257}
258
259static inline int
260bitmap_full(unsigned long *addr, int size)
261{
262        unsigned long mask;
263        int tail;
264        int len;
265        int i;
266
267        len = size / BITS_PER_LONG;
268        for (i = 0; i < len; i++)
269                if (addr[i] != ~0UL)
270                        return (0);
271        tail = size & (BITS_PER_LONG - 1);
272        if (tail) {
273                mask = BITMAP_LAST_WORD_MASK(tail);
274                if ((addr[i] & mask) != mask)
275                        return (0);
276        }
277        return (1);
278}
279
280static inline int
281bitmap_empty(unsigned long *addr, int size)
282{
283        unsigned long mask;
284        int tail;
285        int len;
286        int i;
287
288        len = size / BITS_PER_LONG;
289        for (i = 0; i < len; i++)
290                if (addr[i] != 0)
291                        return (0);
292        tail = size & (BITS_PER_LONG - 1);
293        if (tail) {
294                mask = BITMAP_LAST_WORD_MASK(tail);
295                if ((addr[i] & mask) != 0)
296                        return (0);
297        }
298        return (1);
299}
300
301#define set_bits(m, a)                                                  \
302    atomic_set_long((volatile long *)(a), (long)m)
303
304#define __set_bit(i, a)                                                 \
305    set_bits(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
306
307#define set_bit(i, a)                                                   \
308    set_bits(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
309
310#define clear_bits(m, a)                                                \
311    atomic_clear_long((volatile long *)(a), m)
312
313#define __clear_bit(i, a)                                               \
314    clear_bits(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
315
316#define clear_bit(i, a)                                                 \
317    clear_bits(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
318
319#define test_bit(i, a)                                                  \
320    !!(atomic_load_acq_long(&((volatile long *)(a))[BIT_WORD(i)]) &     \
321    BIT_MASK(i))
322
323static inline long
324test_and_clear_bit(long bit, long *var)
325{
326        long val;
327
328        var += BIT_WORD(bit);
329        bit %= BITS_PER_LONG;
330        bit = (1UL << bit);
331        do {
332                val = *(volatile long *)var;
333        } while (atomic_cmpset_long(var, val, val & ~bit) == 0);
334
335        return !!(val & bit);
336}
337
338static inline long
339test_and_set_bit(long bit, long *var)
340{
341        long val;
342
343        var += BIT_WORD(bit);
344        bit %= BITS_PER_LONG;
345        bit = (1UL << bit);
346        do {
347                val = *(volatile long *)var;
348        } while (atomic_cmpset_long(var, val, val | bit) == 0);
349
350        return !!(val & bit);
351}
352
353static inline void
354bitmap_set(unsigned long *map, int start, int nr)
355{
356        unsigned long *p = map + BIT_WORD(start);
357        const int size = start + nr;
358        int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
359        unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
360
361        while (nr - bits_to_set >= 0) {
362                *p |= mask_to_set;
363                nr -= bits_to_set;
364                bits_to_set = BITS_PER_LONG;
365                mask_to_set = ~0UL;
366                p++;
367        }
368        if (nr) {
369                mask_to_set &= BITMAP_LAST_WORD_MASK(size);
370                *p |= mask_to_set;
371        }
372}
373
374static inline void
375bitmap_clear(unsigned long *map, int start, int nr)
376{
377        unsigned long *p = map + BIT_WORD(start);
378        const int size = start + nr;
379        int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
380        unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
381
382        while (nr - bits_to_clear >= 0) {
383                *p &= ~mask_to_clear;
384                nr -= bits_to_clear;
385                bits_to_clear = BITS_PER_LONG;
386                mask_to_clear = ~0UL;
387                p++;
388        }
389        if (nr) {
390                mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
391                *p &= ~mask_to_clear;
392        }
393}
394
395enum {
396        REG_OP_ISFREE,
397        REG_OP_ALLOC,
398        REG_OP_RELEASE,
399};
400
401static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
402{
403        int nbits_reg;
404        int index;
405        int offset;
406        int nlongs_reg;
407        int nbitsinlong;
408        unsigned long mask;
409        int i;
410        int ret = 0;
411
412        nbits_reg = 1 << order;
413        index = pos / BITS_PER_LONG;
414        offset = pos - (index * BITS_PER_LONG);
415        nlongs_reg = BITS_TO_LONGS(nbits_reg);
416        nbitsinlong = min(nbits_reg,  BITS_PER_LONG);
417
418        mask = (1UL << (nbitsinlong - 1));
419        mask += mask - 1;
420        mask <<= offset;
421
422        switch (reg_op) {
423        case REG_OP_ISFREE:
424                for (i = 0; i < nlongs_reg; i++) {
425                        if (bitmap[index + i] & mask)
426                                goto done;
427                }
428                ret = 1;
429                break;
430
431        case REG_OP_ALLOC:
432                for (i = 0; i < nlongs_reg; i++)
433                        bitmap[index + i] |= mask;
434                break;
435
436        case REG_OP_RELEASE:
437                for (i = 0; i < nlongs_reg; i++)
438                        bitmap[index + i] &= ~mask;
439                break;
440        }
441done:
442        return ret;
443}
444
445static inline int
446bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
447{
448        int pos;
449        int end;
450
451        for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
452                if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
453                        continue;
454                __reg_op(bitmap, pos, order, REG_OP_ALLOC);
455                return pos;
456        }
457        return -ENOMEM;
458}
459
460static inline int
461bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
462{
463        if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
464                return -EBUSY;
465        __reg_op(bitmap, pos, order, REG_OP_ALLOC);
466        return 0;
467}
468
469static inline void
470bitmap_release_region(unsigned long *bitmap, int pos, int order)
471{
472        __reg_op(bitmap, pos, order, REG_OP_RELEASE);
473}
474
475
476#define for_each_set_bit(bit, addr, size) \
477        for ((bit) = find_first_bit((addr), (size));            \
478             (bit) < (size);                                    \
479             (bit) = find_next_bit((addr), (size), (bit) + 1))
480
481#endif  /* _LINUX_BITOPS_H_ */
Note: See TracBrowser for help on using the repository browser.