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