1 #ifndef __LINUX_NODEMASK_H
2 #define __LINUX_NODEMASK_H
3 
4 /*
5  * Nodemasks provide a bitmap suitable for representing the
6  * set of Node's in a system, one bit position per Node number.
7  *
8  * See detailed comments in the file linux/bitmap.h describing the
9  * data type on which these nodemasks are based.
10  *
11  * The available nodemask operations are:
12  *
13  * void node_set(node, mask)		turn on bit 'node' in mask
14  * void node_clear(node, mask)		turn off bit 'node' in mask
15  * void nodes_setall(mask)		set all bits
16  * void nodes_clear(mask)		clear all bits
17  * bool nodemask_test(node, mask)	true iff bit 'node' set in mask
18  * int node_test_and_set(node, mask)	test and set bit 'node' in mask
19  *
20  * void nodes_and(dst, src1, src2)	dst = src1 & src2  [intersection]
21  * void nodes_or(dst, src1, src2)	dst = src1 | src2  [union]
22  * void nodes_xor(dst, src1, src2)	dst = src1 ^ src2
23  * void nodes_andnot(dst, src1, src2)	dst = src1 & ~src2
24  * void nodes_complement(dst, src)	dst = ~src
25  *
26  * int nodes_equal(mask1, mask2)	Does mask1 == mask2?
27  * int nodes_intersects(mask1, mask2)	Do mask1 and mask2 intersect?
28  * int nodes_subset(mask1, mask2)	Is mask1 a subset of mask2?
29  * int nodes_empty(mask)		Is mask empty (no bits sets)?
30  * int nodes_full(mask)			Is mask full (all bits sets)?
31  * int nodes_weight(mask)		Hamming weight - number of set bits
32  *
33  * int first_node(mask)			Number lowest set bit, or MAX_NUMNODES
34  * int next_node(node, mask)		Next node past 'node', or MAX_NUMNODES
35  * int last_node(mask)			Number highest set bit, or MAX_NUMNODES
36  * int cycle_node(node, mask)		Next node cycling from 'node', or
37  *					MAX_NUMNODES
38  *
39  * nodemask_t nodemask_of_node(node)	Return nodemask with bit 'node' set
40  * NODE_MASK_ALL			Initializer - all bits set
41  * NODE_MASK_NONE			Initializer - no bits set
42  * unsigned long *nodemask_bits(mask)	Array of unsigned long's in mask
43  *
44  * for_each_node_mask(node, mask)	for-loop node over mask
45  *
46  * int num_online_nodes()		Number of online Nodes
47  *
48  * bool node_online(node)		Is this node online?
49  *
50  * node_set_online(node)		set bit 'node' in node_online_map
51  * node_set_offline(node)		clear bit 'node' in node_online_map
52  *
53  * for_each_online_node(node)		for-loop node over node_online_map
54  */
55 
56 #include <xen/kernel.h>
57 #include <xen/bitmap.h>
58 #include <xen/numa.h>
59 
60 typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;
61 
62 /*
63  * printf arguments for a nodemask.  Shorthand for using '%*pb[l]' when
64  * printing a nodemask.
65  */
66 #define NODEMASK_PR(src) MAX_NUMNODES, nodemask_bits(src)
67 
68 #define nodemask_bits(src) ((src)->bits)
69 
70 extern nodemask_t _unused_nodemask_arg_;
71 
72 #define node_set(node, dst) __node_set((node), &(dst))
__node_set(int node,volatile nodemask_t * dstp)73 static inline void __node_set(int node, volatile nodemask_t *dstp)
74 {
75 	set_bit(node, dstp->bits);
76 }
77 
78 #define node_clear(node, dst) __node_clear((node), &(dst))
__node_clear(int node,volatile nodemask_t * dstp)79 static inline void __node_clear(int node, volatile nodemask_t *dstp)
80 {
81 	clear_bit(node, dstp->bits);
82 }
83 
84 #define nodes_setall(dst) __nodes_setall(&(dst), MAX_NUMNODES)
__nodes_setall(nodemask_t * dstp,int nbits)85 static inline void __nodes_setall(nodemask_t *dstp, int nbits)
86 {
87 	bitmap_fill(dstp->bits, nbits);
88 }
89 
90 #define nodes_clear(dst) __nodes_clear(&(dst), MAX_NUMNODES)
__nodes_clear(nodemask_t * dstp,int nbits)91 static inline void __nodes_clear(nodemask_t *dstp, int nbits)
92 {
93 	bitmap_zero(dstp->bits, nbits);
94 }
95 
nodemask_test(unsigned int node,const nodemask_t * dst)96 static inline bool nodemask_test(unsigned int node, const nodemask_t *dst)
97 {
98     return test_bit(node, dst->bits);
99 }
100 
101 #define node_test_and_set(node, nodemask) \
102 			__node_test_and_set((node), &(nodemask))
__node_test_and_set(int node,nodemask_t * addr)103 static inline int __node_test_and_set(int node, nodemask_t *addr)
104 {
105 	return test_and_set_bit(node, addr->bits);
106 }
107 
108 #define nodes_and(dst, src1, src2) \
109 			__nodes_and(&(dst), &(src1), &(src2), MAX_NUMNODES)
__nodes_and(nodemask_t * dstp,const nodemask_t * src1p,const nodemask_t * src2p,int nbits)110 static inline void __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
111 					const nodemask_t *src2p, int nbits)
112 {
113 	bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
114 }
115 
116 #define nodes_or(dst, src1, src2) \
117 			__nodes_or(&(dst), &(src1), &(src2), MAX_NUMNODES)
__nodes_or(nodemask_t * dstp,const nodemask_t * src1p,const nodemask_t * src2p,int nbits)118 static inline void __nodes_or(nodemask_t *dstp, const nodemask_t *src1p,
119 					const nodemask_t *src2p, int nbits)
120 {
121 	bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
122 }
123 
124 #define nodes_xor(dst, src1, src2) \
125 			__nodes_xor(&(dst), &(src1), &(src2), MAX_NUMNODES)
__nodes_xor(nodemask_t * dstp,const nodemask_t * src1p,const nodemask_t * src2p,int nbits)126 static inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1p,
127 					const nodemask_t *src2p, int nbits)
128 {
129 	bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
130 }
131 
132 #define nodes_andnot(dst, src1, src2) \
133 			__nodes_andnot(&(dst), &(src1), &(src2), MAX_NUMNODES)
__nodes_andnot(nodemask_t * dstp,const nodemask_t * src1p,const nodemask_t * src2p,int nbits)134 static inline void __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
135 					const nodemask_t *src2p, int nbits)
136 {
137 	bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
138 }
139 
140 #define nodes_complement(dst, src) \
141 			__nodes_complement(&(dst), &(src), MAX_NUMNODES)
__nodes_complement(nodemask_t * dstp,const nodemask_t * srcp,int nbits)142 static inline void __nodes_complement(nodemask_t *dstp,
143 					const nodemask_t *srcp, int nbits)
144 {
145 	bitmap_complement(dstp->bits, srcp->bits, nbits);
146 }
147 
148 #define nodes_equal(src1, src2) \
149 			__nodes_equal(&(src1), &(src2), MAX_NUMNODES)
__nodes_equal(const nodemask_t * src1p,const nodemask_t * src2p,int nbits)150 static inline int __nodes_equal(const nodemask_t *src1p,
151 					const nodemask_t *src2p, int nbits)
152 {
153 	return bitmap_equal(src1p->bits, src2p->bits, nbits);
154 }
155 
156 #define nodes_intersects(src1, src2) \
157 			__nodes_intersects(&(src1), &(src2), MAX_NUMNODES)
__nodes_intersects(const nodemask_t * src1p,const nodemask_t * src2p,int nbits)158 static inline int __nodes_intersects(const nodemask_t *src1p,
159 					const nodemask_t *src2p, int nbits)
160 {
161 	return bitmap_intersects(src1p->bits, src2p->bits, nbits);
162 }
163 
164 #define nodes_subset(src1, src2) \
165 			__nodes_subset(&(src1), &(src2), MAX_NUMNODES)
__nodes_subset(const nodemask_t * src1p,const nodemask_t * src2p,int nbits)166 static inline int __nodes_subset(const nodemask_t *src1p,
167 					const nodemask_t *src2p, int nbits)
168 {
169 	return bitmap_subset(src1p->bits, src2p->bits, nbits);
170 }
171 
172 #define nodes_empty(src) __nodes_empty(&(src), MAX_NUMNODES)
__nodes_empty(const nodemask_t * srcp,int nbits)173 static inline int __nodes_empty(const nodemask_t *srcp, int nbits)
174 {
175 	return bitmap_empty(srcp->bits, nbits);
176 }
177 
178 #define nodes_full(nodemask) __nodes_full(&(nodemask), MAX_NUMNODES)
__nodes_full(const nodemask_t * srcp,int nbits)179 static inline int __nodes_full(const nodemask_t *srcp, int nbits)
180 {
181 	return bitmap_full(srcp->bits, nbits);
182 }
183 
184 #define nodes_weight(nodemask) __nodes_weight(&(nodemask), MAX_NUMNODES)
__nodes_weight(const nodemask_t * srcp,int nbits)185 static inline int __nodes_weight(const nodemask_t *srcp, int nbits)
186 {
187 	return bitmap_weight(srcp->bits, nbits);
188 }
189 
190 /* FIXME: better would be to fix all architectures to never return
191           > MAX_NUMNODES, then the silly min_ts could be dropped. */
192 
193 #define first_node(src) __first_node(&(src), MAX_NUMNODES)
__first_node(const nodemask_t * srcp,int nbits)194 static inline int __first_node(const nodemask_t *srcp, int nbits)
195 {
196 	return min_t(int, nbits, find_first_bit(srcp->bits, nbits));
197 }
198 
199 #define next_node(n, src) __next_node((n), &(src), MAX_NUMNODES)
__next_node(int n,const nodemask_t * srcp,int nbits)200 static inline int __next_node(int n, const nodemask_t *srcp, int nbits)
201 {
202 	return min_t(int, nbits, find_next_bit(srcp->bits, nbits, n+1));
203 }
204 
205 #define last_node(src) __last_node(&(src), MAX_NUMNODES)
__last_node(const nodemask_t * srcp,int nbits)206 static inline int __last_node(const nodemask_t *srcp, int nbits)
207 {
208 	int node, pnode = nbits;
209 	for (node = __first_node(srcp, nbits);
210 	     node < nbits;
211 	     node = __next_node(node, srcp, nbits))
212 		pnode = node;
213 	return pnode;
214 }
215 
216 #define nodemask_of_node(node)						\
217 ({									\
218 	typeof(_unused_nodemask_arg_) m;				\
219 	if (sizeof(m) == sizeof(unsigned long)) {			\
220 		m.bits[0] = 1UL<<(node);				\
221 	} else {							\
222 		nodes_clear(m);						\
223 		node_set((node), m);					\
224 	}								\
225 	m;								\
226 })
227 
228 #define cycle_node(n, src) __cycle_node((n), &(src), MAX_NUMNODES)
__cycle_node(int n,const nodemask_t * maskp,int nbits)229 static inline int __cycle_node(int n, const nodemask_t *maskp, int nbits)
230 {
231     int nxt = __next_node(n, maskp, nbits);
232 
233     if (nxt == nbits)
234         nxt = __first_node(maskp, nbits);
235     return nxt;
236 }
237 
238 #define NODE_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(MAX_NUMNODES)
239 
240 #if MAX_NUMNODES <= BITS_PER_LONG
241 
242 #define NODE_MASK_ALL							\
243 ((nodemask_t) { {							\
244 	[BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD		\
245 } })
246 
247 #else
248 
249 #define NODE_MASK_ALL							\
250 ((nodemask_t) { {							\
251 	[0 ... BITS_TO_LONGS(MAX_NUMNODES)-2] = ~0UL,			\
252 	[BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD		\
253 } })
254 
255 #endif
256 
257 #define NODE_MASK_NONE							\
258 ((nodemask_t) { {							\
259 	[0 ... BITS_TO_LONGS(MAX_NUMNODES)-1] =  0UL			\
260 } })
261 
262 #if MAX_NUMNODES > 1
263 #define for_each_node_mask(node, mask)			\
264 	for ((node) = first_node(mask);			\
265 		(node) < MAX_NUMNODES;			\
266 		(node) = next_node((node), (mask)))
267 #else /* MAX_NUMNODES == 1 */
268 #define for_each_node_mask(node, mask)			\
269 	if (!nodes_empty(mask))				\
270 		for ((node) = 0; (node) < 1; (node)++)
271 #endif /* MAX_NUMNODES */
272 
273 /*
274  * The following particular system nodemasks and operations
275  * on them manage online nodes.
276  */
277 
278 extern nodemask_t node_online_map;
279 
280 #if MAX_NUMNODES > 1
281 #define num_online_nodes()	nodes_weight(node_online_map)
282 #define node_online(node)	nodemask_test(node, &node_online_map)
283 #else
284 #define num_online_nodes()	1
285 #define node_online(node)	((node) == 0)
286 #endif
287 
288 #define node_set_online(node)	   set_bit((node), node_online_map.bits)
289 #define node_set_offline(node)	   clear_bit((node), node_online_map.bits)
290 
291 #define for_each_online_node(node) for_each_node_mask((node), node_online_map)
292 
293 #endif /* __LINUX_NODEMASK_H */
294