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 * For details of nodemask_scnprintf(), nodelist_scnpintf() and
12 * nodemask_parse(), see bitmap_scnprintf() and bitmap_parse()
13 * in lib/bitmap.c.
14 *
15 * The available nodemask operations are:
16 *
17 * void node_set(node, mask) turn on bit 'node' in mask
18 * void node_clear(node, mask) turn off bit 'node' in mask
19 * void nodes_setall(mask) set all bits
20 * void nodes_clear(mask) clear all bits
21 * int node_isset(node, mask) true iff bit 'node' set in mask
22 * int node_test_and_set(node, mask) test and set bit 'node' in mask
23 *
24 * void nodes_and(dst, src1, src2) dst = src1 & src2 [intersection]
25 * void nodes_or(dst, src1, src2) dst = src1 | src2 [union]
26 * void nodes_xor(dst, src1, src2) dst = src1 ^ src2
27 * void nodes_andnot(dst, src1, src2) dst = src1 & ~src2
28 * void nodes_complement(dst, src) dst = ~src
29 *
30 * int nodes_equal(mask1, mask2) Does mask1 == mask2?
31 * int nodes_intersects(mask1, mask2) Do mask1 and mask2 intersect?
32 * int nodes_subset(mask1, mask2) Is mask1 a subset of mask2?
33 * int nodes_empty(mask) Is mask empty (no bits sets)?
34 * int nodes_full(mask) Is mask full (all bits sets)?
35 * int nodes_weight(mask) Hamming weight - number of set bits
36 *
37 * void nodes_shift_right(dst, src, n) Shift right
38 * void nodes_shift_left(dst, src, n) Shift left
39 *
40 * int first_node(mask) Number lowest set bit, or MAX_NUMNODES
41 * int next_node(node, mask) Next node past 'node', or MAX_NUMNODES
42 * int last_node(mask) Number highest set bit, or MAX_NUMNODES
43 * int first_unset_node(mask) First node not set in mask, or
44 * MAX_NUMNODES.
45 * int cycle_node(node, mask) Next node cycling from 'node', or
46 * MAX_NUMNODES
47 *
48 * nodemask_t nodemask_of_node(node) Return nodemask with bit 'node' set
49 * NODE_MASK_ALL Initializer - all bits set
50 * NODE_MASK_NONE Initializer - no bits set
51 * unsigned long *nodes_addr(mask) Array of unsigned long's in mask
52 *
53 * int nodemask_scnprintf(buf, len, mask) Format nodemask for printing
54 * int nodelist_scnprintf(buf, len, mask) Format nodemask as a list for printing
55 * int nodemask_parse(ubuf, ulen, mask) Parse ascii string as nodemask
56 *
57 * for_each_node_mask(node, mask) for-loop node over mask
58 *
59 * int num_online_nodes() Number of online Nodes
60 *
61 * int node_online(node) Is some node online?
62 *
63 * int any_online_node(mask) First online node in mask
64 *
65 * node_set_online(node) set bit 'node' in node_online_map
66 * node_set_offline(node) clear bit 'node' in node_online_map
67 *
68 * for_each_online_node(node) for-loop node over node_online_map
69 *
70 * Subtlety:
71 * 1) The 'type-checked' form of node_isset() causes gcc (3.3.2, anyway)
72 * to generate slightly worse code. So use a simple one-line #define
73 * for node_isset(), instead of wrapping an inline inside a macro, the
74 * way we do the other calls.
75 */
76
77 #include <xen/kernel.h>
78 #include <xen/bitmap.h>
79 #include <xen/numa.h>
80
81 typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;
82 extern nodemask_t _unused_nodemask_arg_;
83
84 #define node_set(node, dst) __node_set((node), &(dst))
__node_set(int node,volatile nodemask_t * dstp)85 static inline void __node_set(int node, volatile nodemask_t *dstp)
86 {
87 set_bit(node, dstp->bits);
88 }
89
90 #define node_clear(node, dst) __node_clear((node), &(dst))
__node_clear(int node,volatile nodemask_t * dstp)91 static inline void __node_clear(int node, volatile nodemask_t *dstp)
92 {
93 clear_bit(node, dstp->bits);
94 }
95
96 #define nodes_setall(dst) __nodes_setall(&(dst), MAX_NUMNODES)
__nodes_setall(nodemask_t * dstp,int nbits)97 static inline void __nodes_setall(nodemask_t *dstp, int nbits)
98 {
99 bitmap_fill(dstp->bits, nbits);
100 }
101
102 #define nodes_clear(dst) __nodes_clear(&(dst), MAX_NUMNODES)
__nodes_clear(nodemask_t * dstp,int nbits)103 static inline void __nodes_clear(nodemask_t *dstp, int nbits)
104 {
105 bitmap_zero(dstp->bits, nbits);
106 }
107
108 /* No static inline type checking - see Subtlety (1) above. */
109 #define node_isset(node, nodemask) test_bit((node), (nodemask).bits)
110
111 #define node_test_and_set(node, nodemask) \
112 __node_test_and_set((node), &(nodemask))
__node_test_and_set(int node,nodemask_t * addr)113 static inline int __node_test_and_set(int node, nodemask_t *addr)
114 {
115 return test_and_set_bit(node, addr->bits);
116 }
117
118 #define nodes_and(dst, src1, src2) \
119 __nodes_and(&(dst), &(src1), &(src2), MAX_NUMNODES)
__nodes_and(nodemask_t * dstp,const nodemask_t * src1p,const nodemask_t * src2p,int nbits)120 static inline void __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
121 const nodemask_t *src2p, int nbits)
122 {
123 bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
124 }
125
126 #define nodes_or(dst, src1, src2) \
127 __nodes_or(&(dst), &(src1), &(src2), MAX_NUMNODES)
__nodes_or(nodemask_t * dstp,const nodemask_t * src1p,const nodemask_t * src2p,int nbits)128 static inline void __nodes_or(nodemask_t *dstp, const nodemask_t *src1p,
129 const nodemask_t *src2p, int nbits)
130 {
131 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
132 }
133
134 #define nodes_xor(dst, src1, src2) \
135 __nodes_xor(&(dst), &(src1), &(src2), MAX_NUMNODES)
__nodes_xor(nodemask_t * dstp,const nodemask_t * src1p,const nodemask_t * src2p,int nbits)136 static inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1p,
137 const nodemask_t *src2p, int nbits)
138 {
139 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
140 }
141
142 #define nodes_andnot(dst, src1, src2) \
143 __nodes_andnot(&(dst), &(src1), &(src2), MAX_NUMNODES)
__nodes_andnot(nodemask_t * dstp,const nodemask_t * src1p,const nodemask_t * src2p,int nbits)144 static inline void __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
145 const nodemask_t *src2p, int nbits)
146 {
147 bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
148 }
149
150 #define nodes_complement(dst, src) \
151 __nodes_complement(&(dst), &(src), MAX_NUMNODES)
__nodes_complement(nodemask_t * dstp,const nodemask_t * srcp,int nbits)152 static inline void __nodes_complement(nodemask_t *dstp,
153 const nodemask_t *srcp, int nbits)
154 {
155 bitmap_complement(dstp->bits, srcp->bits, nbits);
156 }
157
158 #define nodes_equal(src1, src2) \
159 __nodes_equal(&(src1), &(src2), MAX_NUMNODES)
__nodes_equal(const nodemask_t * src1p,const nodemask_t * src2p,int nbits)160 static inline int __nodes_equal(const nodemask_t *src1p,
161 const nodemask_t *src2p, int nbits)
162 {
163 return bitmap_equal(src1p->bits, src2p->bits, nbits);
164 }
165
166 #define nodes_intersects(src1, src2) \
167 __nodes_intersects(&(src1), &(src2), MAX_NUMNODES)
__nodes_intersects(const nodemask_t * src1p,const nodemask_t * src2p,int nbits)168 static inline int __nodes_intersects(const nodemask_t *src1p,
169 const nodemask_t *src2p, int nbits)
170 {
171 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
172 }
173
174 #define nodes_subset(src1, src2) \
175 __nodes_subset(&(src1), &(src2), MAX_NUMNODES)
__nodes_subset(const nodemask_t * src1p,const nodemask_t * src2p,int nbits)176 static inline int __nodes_subset(const nodemask_t *src1p,
177 const nodemask_t *src2p, int nbits)
178 {
179 return bitmap_subset(src1p->bits, src2p->bits, nbits);
180 }
181
182 #define nodes_empty(src) __nodes_empty(&(src), MAX_NUMNODES)
__nodes_empty(const nodemask_t * srcp,int nbits)183 static inline int __nodes_empty(const nodemask_t *srcp, int nbits)
184 {
185 return bitmap_empty(srcp->bits, nbits);
186 }
187
188 #define nodes_full(nodemask) __nodes_full(&(nodemask), MAX_NUMNODES)
__nodes_full(const nodemask_t * srcp,int nbits)189 static inline int __nodes_full(const nodemask_t *srcp, int nbits)
190 {
191 return bitmap_full(srcp->bits, nbits);
192 }
193
194 #define nodes_weight(nodemask) __nodes_weight(&(nodemask), MAX_NUMNODES)
__nodes_weight(const nodemask_t * srcp,int nbits)195 static inline int __nodes_weight(const nodemask_t *srcp, int nbits)
196 {
197 return bitmap_weight(srcp->bits, nbits);
198 }
199
200 #define nodes_shift_right(dst, src, n) \
201 __nodes_shift_right(&(dst), &(src), (n), MAX_NUMNODES)
__nodes_shift_right(nodemask_t * dstp,const nodemask_t * srcp,int n,int nbits)202 static inline void __nodes_shift_right(nodemask_t *dstp,
203 const nodemask_t *srcp, int n, int nbits)
204 {
205 bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
206 }
207
208 #define nodes_shift_left(dst, src, n) \
209 __nodes_shift_left(&(dst), &(src), (n), MAX_NUMNODES)
__nodes_shift_left(nodemask_t * dstp,const nodemask_t * srcp,int n,int nbits)210 static inline void __nodes_shift_left(nodemask_t *dstp,
211 const nodemask_t *srcp, int n, int nbits)
212 {
213 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
214 }
215
216 /* FIXME: better would be to fix all architectures to never return
217 > MAX_NUMNODES, then the silly min_ts could be dropped. */
218
219 #define first_node(src) __first_node(&(src), MAX_NUMNODES)
__first_node(const nodemask_t * srcp,int nbits)220 static inline int __first_node(const nodemask_t *srcp, int nbits)
221 {
222 return min_t(int, nbits, find_first_bit(srcp->bits, nbits));
223 }
224
225 #define next_node(n, src) __next_node((n), &(src), MAX_NUMNODES)
__next_node(int n,const nodemask_t * srcp,int nbits)226 static inline int __next_node(int n, const nodemask_t *srcp, int nbits)
227 {
228 return min_t(int, nbits, find_next_bit(srcp->bits, nbits, n+1));
229 }
230
231 #define last_node(src) __last_node(&(src), MAX_NUMNODES)
__last_node(const nodemask_t * srcp,int nbits)232 static inline int __last_node(const nodemask_t *srcp, int nbits)
233 {
234 int node, pnode = nbits;
235 for (node = __first_node(srcp, nbits);
236 node < nbits;
237 node = __next_node(node, srcp, nbits))
238 pnode = node;
239 return pnode;
240 }
241
242 #define nodemask_of_node(node) \
243 ({ \
244 typeof(_unused_nodemask_arg_) m; \
245 if (sizeof(m) == sizeof(unsigned long)) { \
246 m.bits[0] = 1UL<<(node); \
247 } else { \
248 nodes_clear(m); \
249 node_set((node), m); \
250 } \
251 m; \
252 })
253
254 #define first_unset_node(mask) __first_unset_node(&(mask))
__first_unset_node(const nodemask_t * maskp)255 static inline int __first_unset_node(const nodemask_t *maskp)
256 {
257 return min_t(int,MAX_NUMNODES,
258 find_first_zero_bit(maskp->bits, MAX_NUMNODES));
259 }
260
261 #define cycle_node(n, src) __cycle_node((n), &(src), MAX_NUMNODES)
__cycle_node(int n,const nodemask_t * maskp,int nbits)262 static inline int __cycle_node(int n, const nodemask_t *maskp, int nbits)
263 {
264 int nxt = __next_node(n, maskp, nbits);
265
266 if (nxt == nbits)
267 nxt = __first_node(maskp, nbits);
268 return nxt;
269 }
270
271 #define NODE_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(MAX_NUMNODES)
272
273 #if MAX_NUMNODES <= BITS_PER_LONG
274
275 #define NODE_MASK_ALL \
276 ((nodemask_t) { { \
277 [BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD \
278 } })
279
280 #else
281
282 #define NODE_MASK_ALL \
283 ((nodemask_t) { { \
284 [0 ... BITS_TO_LONGS(MAX_NUMNODES)-2] = ~0UL, \
285 [BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD \
286 } })
287
288 #endif
289
290 #define NODE_MASK_NONE \
291 ((nodemask_t) { { \
292 [0 ... BITS_TO_LONGS(MAX_NUMNODES)-1] = 0UL \
293 } })
294
295 #define nodes_addr(src) ((src).bits)
296
297 #define nodelist_scnprintf(buf, len, src) \
298 __nodelist_scnprintf((buf), (len), (src), MAX_NUMNODES)
__nodelist_scnprintf(char * buf,int len,const nodemask_t * srcp,int nbits)299 static inline int __nodelist_scnprintf(char *buf, int len,
300 const nodemask_t *srcp, int nbits)
301 {
302 return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
303 }
304
305 #if 0
306 #define nodemask_scnprintf(buf, len, src) \
307 __nodemask_scnprintf((buf), (len), &(src), MAX_NUMNODES)
308 static inline int __nodemask_scnprintf(char *buf, int len,
309 const nodemask_t *srcp, int nbits)
310 {
311 return bitmap_scnprintf(buf, len, srcp->bits, nbits);
312 }
313
314 #define nodemask_parse(ubuf, ulen, dst) \
315 __nodemask_parse((ubuf), (ulen), &(dst), MAX_NUMNODES)
316 static inline int __nodemask_parse(const char __user *buf, int len,
317 nodemask_t *dstp, int nbits)
318 {
319 return bitmap_parse(buf, len, dstp->bits, nbits);
320 }
321 #endif
322
323 #if MAX_NUMNODES > 1
324 #define for_each_node_mask(node, mask) \
325 for ((node) = first_node(mask); \
326 (node) < MAX_NUMNODES; \
327 (node) = next_node((node), (mask)))
328 #else /* MAX_NUMNODES == 1 */
329 #define for_each_node_mask(node, mask) \
330 if (!nodes_empty(mask)) \
331 for ((node) = 0; (node) < 1; (node)++)
332 #endif /* MAX_NUMNODES */
333
334 /*
335 * The following particular system nodemasks and operations
336 * on them manage online nodes.
337 */
338
339 extern nodemask_t node_online_map;
340
341 #if MAX_NUMNODES > 1
342 #define num_online_nodes() nodes_weight(node_online_map)
343 #define node_online(node) node_isset((node), node_online_map)
344 #else
345 #define num_online_nodes() 1
346 #define node_online(node) ((node) == 0)
347 #endif
348
349 #define any_online_node(mask) \
350 ({ \
351 int node; \
352 for_each_node_mask(node, (mask)) \
353 if (node_online(node)) \
354 break; \
355 node; \
356 })
357
358 #define node_set_online(node) set_bit((node), node_online_map.bits)
359 #define node_set_offline(node) clear_bit((node), node_online_map.bits)
360
361 #define for_each_online_node(node) for_each_node_mask((node), node_online_map)
362
363 #endif /* __LINUX_NODEMASK_H */
364