xref: /freebsd/contrib/ofed/libmlx5/bitmap.h (revision d3d381b2b194b4d24853e92eecef55f262688d1a)
1 /*
2  * Copyright (c) 2000, 2011 Mellanox Technology Inc.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #ifndef BITMAP_H
34 #define BITMAP_H
35 
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <pthread.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/ipc.h>
42 #include <sys/shm.h>
43 #include <sys/mman.h>
44 #include <errno.h>
45 #include "mlx5.h"
46 
47 /* Only ia64 requires this */
48 #ifdef __ia64__
49 #define MLX5_SHM_ADDR ((void *)0x8000000000000000UL)
50 #define MLX5_SHMAT_FLAGS (SHM_RND)
51 #else
52 #define MLX5_SHM_ADDR NULL
53 #define MLX5_SHMAT_FLAGS 0
54 #endif
55 
56 #define BITS_PER_LONG		(8 * sizeof(long))
57 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_LONG)
58 
59 #ifndef HPAGE_SIZE
60 #define HPAGE_SIZE		(2UL * 1024 * 1024)
61 #endif
62 
63 #define MLX5_SHM_LENGTH		HPAGE_SIZE
64 #define MLX5_Q_CHUNK_SIZE	32768
65 #define MLX5_SHM_NUM_REGION	64
66 
67 static inline unsigned long mlx5_ffz(uint32_t word)
68 {
69 	return __builtin_ffs(~word) - 1;
70 }
71 
72 static inline uint32_t mlx5_find_first_zero_bit(const unsigned long *addr,
73 					 uint32_t size)
74 {
75 	const unsigned long *p = addr;
76 	uint32_t result = 0;
77 	unsigned long tmp;
78 
79 	while (size & ~(BITS_PER_LONG - 1)) {
80 		tmp = *(p++);
81 		if (~tmp)
82 			goto found;
83 		result += BITS_PER_LONG;
84 		size -= BITS_PER_LONG;
85 	}
86 	if (!size)
87 		return result;
88 
89 	tmp = (*p) | (~0UL << size);
90 	if (tmp == (uint32_t)~0UL)	/* Are any bits zero? */
91 		return result + size;	/* Nope. */
92 found:
93 	return result + mlx5_ffz(tmp);
94 }
95 
96 static inline void mlx5_set_bit(unsigned int nr, unsigned long *addr)
97 {
98 	addr[(nr / BITS_PER_LONG)] |= (1 << (nr % BITS_PER_LONG));
99 }
100 
101 static inline void mlx5_clear_bit(unsigned int nr,  unsigned long *addr)
102 {
103 	addr[(nr / BITS_PER_LONG)] &= ~(1 << (nr % BITS_PER_LONG));
104 }
105 
106 static inline int mlx5_test_bit(unsigned int nr, const unsigned long *addr)
107 {
108 	return !!(addr[(nr / BITS_PER_LONG)] & (1 <<  (nr % BITS_PER_LONG)));
109 }
110 
111 #endif
112