cpumask: add CPU_MASK_ALL_PTR macro
[pandora-kernel.git] / include / linux / cpumask.h
1 #ifndef __LINUX_CPUMASK_H
2 #define __LINUX_CPUMASK_H
3
4 /*
5  * Cpumasks provide a bitmap suitable for representing the
6  * set of CPU's in a system, one bit position per CPU number.
7  *
8  * See detailed comments in the file linux/bitmap.h describing the
9  * data type on which these cpumasks are based.
10  *
11  * For details of cpumask_scnprintf() and cpumask_parse_user(),
12  * see bitmap_scnprintf() and bitmap_parse_user() in lib/bitmap.c.
13  * For details of cpulist_scnprintf() and cpulist_parse(), see
14  * bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c.
15  * For details of cpu_remap(), see bitmap_bitremap in lib/bitmap.c
16  * For details of cpus_remap(), see bitmap_remap in lib/bitmap.c.
17  *
18  * The available cpumask operations are:
19  *
20  * void cpu_set(cpu, mask)              turn on bit 'cpu' in mask
21  * void cpu_clear(cpu, mask)            turn off bit 'cpu' in mask
22  * void cpus_setall(mask)               set all bits
23  * void cpus_clear(mask)                clear all bits
24  * int cpu_isset(cpu, mask)             true iff bit 'cpu' set in mask
25  * int cpu_test_and_set(cpu, mask)      test and set bit 'cpu' in mask
26  *
27  * void cpus_and(dst, src1, src2)       dst = src1 & src2  [intersection]
28  * void cpus_or(dst, src1, src2)        dst = src1 | src2  [union]
29  * void cpus_xor(dst, src1, src2)       dst = src1 ^ src2
30  * void cpus_andnot(dst, src1, src2)    dst = src1 & ~src2
31  * void cpus_complement(dst, src)       dst = ~src
32  *
33  * int cpus_equal(mask1, mask2)         Does mask1 == mask2?
34  * int cpus_intersects(mask1, mask2)    Do mask1 and mask2 intersect?
35  * int cpus_subset(mask1, mask2)        Is mask1 a subset of mask2?
36  * int cpus_empty(mask)                 Is mask empty (no bits sets)?
37  * int cpus_full(mask)                  Is mask full (all bits sets)?
38  * int cpus_weight(mask)                Hamming weigh - number of set bits
39  *
40  * void cpus_shift_right(dst, src, n)   Shift right
41  * void cpus_shift_left(dst, src, n)    Shift left
42  *
43  * int first_cpu(mask)                  Number lowest set bit, or NR_CPUS
44  * int next_cpu(cpu, mask)              Next cpu past 'cpu', or NR_CPUS
45  *
46  * cpumask_t cpumask_of_cpu(cpu)        Return cpumask with bit 'cpu' set
47  * CPU_MASK_ALL                         Initializer - all bits set
48  * CPU_MASK_NONE                        Initializer - no bits set
49  * unsigned long *cpus_addr(mask)       Array of unsigned long's in mask
50  *
51  * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
52  * int cpumask_parse_user(ubuf, ulen, mask)     Parse ascii string as cpumask
53  * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
54  * int cpulist_parse(buf, map)          Parse ascii string as cpulist
55  * int cpu_remap(oldbit, old, new)      newbit = map(old, new)(oldbit)
56  * int cpus_remap(dst, src, old, new)   *dst = map(old, new)(src)
57  *
58  * for_each_cpu_mask(cpu, mask)         for-loop cpu over mask
59  *
60  * int num_online_cpus()                Number of online CPUs
61  * int num_possible_cpus()              Number of all possible CPUs
62  * int num_present_cpus()               Number of present CPUs
63  *
64  * int cpu_online(cpu)                  Is some cpu online?
65  * int cpu_possible(cpu)                Is some cpu possible?
66  * int cpu_present(cpu)                 Is some cpu present (can schedule)?
67  *
68  * int any_online_cpu(mask)             First online cpu in mask
69  *
70  * for_each_possible_cpu(cpu)           for-loop cpu over cpu_possible_map
71  * for_each_online_cpu(cpu)             for-loop cpu over cpu_online_map
72  * for_each_present_cpu(cpu)            for-loop cpu over cpu_present_map
73  *
74  * Subtlety:
75  * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
76  *    to generate slightly worse code.  Note for example the additional
77  *    40 lines of assembly code compiling the "for each possible cpu"
78  *    loops buried in the disk_stat_read() macros calls when compiling
79  *    drivers/block/genhd.c (arch i386, CONFIG_SMP=y).  So use a simple
80  *    one-line #define for cpu_isset(), instead of wrapping an inline
81  *    inside a macro, the way we do the other calls.
82  */
83
84 #include <linux/kernel.h>
85 #include <linux/threads.h>
86 #include <linux/bitmap.h>
87
88 typedef struct { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
89 extern cpumask_t _unused_cpumask_arg_;
90
91 #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
92 static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
93 {
94         set_bit(cpu, dstp->bits);
95 }
96
97 #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
98 static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
99 {
100         clear_bit(cpu, dstp->bits);
101 }
102
103 #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
104 static inline void __cpus_setall(cpumask_t *dstp, int nbits)
105 {
106         bitmap_fill(dstp->bits, nbits);
107 }
108
109 #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
110 static inline void __cpus_clear(cpumask_t *dstp, int nbits)
111 {
112         bitmap_zero(dstp->bits, nbits);
113 }
114
115 /* No static inline type checking - see Subtlety (1) above. */
116 #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
117
118 #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
119 static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
120 {
121         return test_and_set_bit(cpu, addr->bits);
122 }
123
124 #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
125 static inline void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
126                                         const cpumask_t *src2p, int nbits)
127 {
128         bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
129 }
130
131 #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
132 static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
133                                         const cpumask_t *src2p, int nbits)
134 {
135         bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
136 }
137
138 #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
139 static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
140                                         const cpumask_t *src2p, int nbits)
141 {
142         bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
143 }
144
145 #define cpus_andnot(dst, src1, src2) \
146                                 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
147 static inline void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
148                                         const cpumask_t *src2p, int nbits)
149 {
150         bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
151 }
152
153 #define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
154 static inline void __cpus_complement(cpumask_t *dstp,
155                                         const cpumask_t *srcp, int nbits)
156 {
157         bitmap_complement(dstp->bits, srcp->bits, nbits);
158 }
159
160 #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
161 static inline int __cpus_equal(const cpumask_t *src1p,
162                                         const cpumask_t *src2p, int nbits)
163 {
164         return bitmap_equal(src1p->bits, src2p->bits, nbits);
165 }
166
167 #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
168 static inline int __cpus_intersects(const cpumask_t *src1p,
169                                         const cpumask_t *src2p, int nbits)
170 {
171         return bitmap_intersects(src1p->bits, src2p->bits, nbits);
172 }
173
174 #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
175 static inline int __cpus_subset(const cpumask_t *src1p,
176                                         const cpumask_t *src2p, int nbits)
177 {
178         return bitmap_subset(src1p->bits, src2p->bits, nbits);
179 }
180
181 #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
182 static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
183 {
184         return bitmap_empty(srcp->bits, nbits);
185 }
186
187 #define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
188 static inline int __cpus_full(const cpumask_t *srcp, int nbits)
189 {
190         return bitmap_full(srcp->bits, nbits);
191 }
192
193 #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
194 static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
195 {
196         return bitmap_weight(srcp->bits, nbits);
197 }
198
199 #define cpus_shift_right(dst, src, n) \
200                         __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
201 static inline void __cpus_shift_right(cpumask_t *dstp,
202                                         const cpumask_t *srcp, int n, int nbits)
203 {
204         bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
205 }
206
207 #define cpus_shift_left(dst, src, n) \
208                         __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
209 static inline void __cpus_shift_left(cpumask_t *dstp,
210                                         const cpumask_t *srcp, int n, int nbits)
211 {
212         bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
213 }
214
215 #ifdef CONFIG_SMP
216 int __first_cpu(const cpumask_t *srcp);
217 #define first_cpu(src) __first_cpu(&(src))
218 int __next_cpu(int n, const cpumask_t *srcp);
219 #define next_cpu(n, src) __next_cpu((n), &(src))
220 #else
221 #define first_cpu(src)          ({ (void)(src); 0; })
222 #define next_cpu(n, src)        ({ (void)(src); 1; })
223 #endif
224
225 #define cpumask_of_cpu(cpu)                                             \
226 ({                                                                      \
227         typeof(_unused_cpumask_arg_) m;                                 \
228         if (sizeof(m) == sizeof(unsigned long)) {                       \
229                 m.bits[0] = 1UL<<(cpu);                                 \
230         } else {                                                        \
231                 cpus_clear(m);                                          \
232                 cpu_set((cpu), m);                                      \
233         }                                                               \
234         m;                                                              \
235 })
236
237 #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
238
239 #if NR_CPUS <= BITS_PER_LONG
240
241 #define CPU_MASK_ALL                                                    \
242 (cpumask_t) { {                                                         \
243         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
244 } }
245
246 #define CPU_MASK_ALL_PTR        (&CPU_MASK_ALL)
247
248 #else
249
250 #define CPU_MASK_ALL                                                    \
251 (cpumask_t) { {                                                         \
252         [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                        \
253         [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
254 } }
255
256 /* cpu_mask_all is in init/main.c */
257 extern cpumask_t cpu_mask_all;
258 #define CPU_MASK_ALL_PTR        (&cpu_mask_all)
259
260 #endif
261
262 #define CPU_MASK_NONE                                                   \
263 (cpumask_t) { {                                                         \
264         [0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL                         \
265 } }
266
267 #define CPU_MASK_CPU0                                                   \
268 (cpumask_t) { {                                                         \
269         [0] =  1UL                                                      \
270 } }
271
272 #define cpus_addr(src) ((src).bits)
273
274 #define cpumask_scnprintf(buf, len, src) \
275                         __cpumask_scnprintf((buf), (len), &(src), NR_CPUS)
276 static inline int __cpumask_scnprintf(char *buf, int len,
277                                         const cpumask_t *srcp, int nbits)
278 {
279         return bitmap_scnprintf(buf, len, srcp->bits, nbits);
280 }
281
282 #define cpumask_scnprintf_len(len) \
283                         __cpumask_scnprintf_len((len))
284 static inline int __cpumask_scnprintf_len(int len)
285 {
286         return bitmap_scnprintf_len(len);
287 }
288
289 #define cpumask_parse_user(ubuf, ulen, dst) \
290                         __cpumask_parse_user((ubuf), (ulen), &(dst), NR_CPUS)
291 static inline int __cpumask_parse_user(const char __user *buf, int len,
292                                         cpumask_t *dstp, int nbits)
293 {
294         return bitmap_parse_user(buf, len, dstp->bits, nbits);
295 }
296
297 #define cpulist_scnprintf(buf, len, src) \
298                         __cpulist_scnprintf((buf), (len), &(src), NR_CPUS)
299 static inline int __cpulist_scnprintf(char *buf, int len,
300                                         const cpumask_t *srcp, int nbits)
301 {
302         return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
303 }
304
305 #define cpulist_parse(buf, dst) __cpulist_parse((buf), &(dst), NR_CPUS)
306 static inline int __cpulist_parse(const char *buf, cpumask_t *dstp, int nbits)
307 {
308         return bitmap_parselist(buf, dstp->bits, nbits);
309 }
310
311 #define cpu_remap(oldbit, old, new) \
312                 __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
313 static inline int __cpu_remap(int oldbit,
314                 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
315 {
316         return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
317 }
318
319 #define cpus_remap(dst, src, old, new) \
320                 __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
321 static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
322                 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
323 {
324         bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
325 }
326
327 #if NR_CPUS > 1
328 #define for_each_cpu_mask(cpu, mask)            \
329         for ((cpu) = first_cpu(mask);           \
330                 (cpu) < NR_CPUS;                \
331                 (cpu) = next_cpu((cpu), (mask)))
332 #else /* NR_CPUS == 1 */
333 #define for_each_cpu_mask(cpu, mask)            \
334         for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
335 #endif /* NR_CPUS */
336
337 /*
338  * The following particular system cpumasks and operations manage
339  * possible, present and online cpus.  Each of them is a fixed size
340  * bitmap of size NR_CPUS.
341  *
342  *  #ifdef CONFIG_HOTPLUG_CPU
343  *     cpu_possible_map - has bit 'cpu' set iff cpu is populatable
344  *     cpu_present_map  - has bit 'cpu' set iff cpu is populated
345  *     cpu_online_map   - has bit 'cpu' set iff cpu available to scheduler
346  *  #else
347  *     cpu_possible_map - has bit 'cpu' set iff cpu is populated
348  *     cpu_present_map  - copy of cpu_possible_map
349  *     cpu_online_map   - has bit 'cpu' set iff cpu available to scheduler
350  *  #endif
351  *
352  *  In either case, NR_CPUS is fixed at compile time, as the static
353  *  size of these bitmaps.  The cpu_possible_map is fixed at boot
354  *  time, as the set of CPU id's that it is possible might ever
355  *  be plugged in at anytime during the life of that system boot.
356  *  The cpu_present_map is dynamic(*), representing which CPUs
357  *  are currently plugged in.  And cpu_online_map is the dynamic
358  *  subset of cpu_present_map, indicating those CPUs available
359  *  for scheduling.
360  *
361  *  If HOTPLUG is enabled, then cpu_possible_map is forced to have
362  *  all NR_CPUS bits set, otherwise it is just the set of CPUs that
363  *  ACPI reports present at boot.
364  *
365  *  If HOTPLUG is enabled, then cpu_present_map varies dynamically,
366  *  depending on what ACPI reports as currently plugged in, otherwise
367  *  cpu_present_map is just a copy of cpu_possible_map.
368  *
369  *  (*) Well, cpu_present_map is dynamic in the hotplug case.  If not
370  *      hotplug, it's a copy of cpu_possible_map, hence fixed at boot.
371  *
372  * Subtleties:
373  * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
374  *    assumption that their single CPU is online.  The UP
375  *    cpu_{online,possible,present}_maps are placebos.  Changing them
376  *    will have no useful affect on the following num_*_cpus()
377  *    and cpu_*() macros in the UP case.  This ugliness is a UP
378  *    optimization - don't waste any instructions or memory references
379  *    asking if you're online or how many CPUs there are if there is
380  *    only one CPU.
381  * 2) Most SMP arch's #define some of these maps to be some
382  *    other map specific to that arch.  Therefore, the following
383  *    must be #define macros, not inlines.  To see why, examine
384  *    the assembly code produced by the following.  Note that
385  *    set1() writes phys_x_map, but set2() writes x_map:
386  *        int x_map, phys_x_map;
387  *        #define set1(a) x_map = a
388  *        inline void set2(int a) { x_map = a; }
389  *        #define x_map phys_x_map
390  *        main(){ set1(3); set2(5); }
391  */
392
393 extern cpumask_t cpu_possible_map;
394 extern cpumask_t cpu_online_map;
395 extern cpumask_t cpu_present_map;
396
397 #if NR_CPUS > 1
398 #define num_online_cpus()       cpus_weight(cpu_online_map)
399 #define num_possible_cpus()     cpus_weight(cpu_possible_map)
400 #define num_present_cpus()      cpus_weight(cpu_present_map)
401 #define cpu_online(cpu)         cpu_isset((cpu), cpu_online_map)
402 #define cpu_possible(cpu)       cpu_isset((cpu), cpu_possible_map)
403 #define cpu_present(cpu)        cpu_isset((cpu), cpu_present_map)
404 #else
405 #define num_online_cpus()       1
406 #define num_possible_cpus()     1
407 #define num_present_cpus()      1
408 #define cpu_online(cpu)         ((cpu) == 0)
409 #define cpu_possible(cpu)       ((cpu) == 0)
410 #define cpu_present(cpu)        ((cpu) == 0)
411 #endif
412
413 #define cpu_is_offline(cpu)     unlikely(!cpu_online(cpu))
414
415 #ifdef CONFIG_SMP
416 extern int nr_cpu_ids;
417 #define any_online_cpu(mask) __any_online_cpu(&(mask))
418 int __any_online_cpu(const cpumask_t *mask);
419 #else
420 #define nr_cpu_ids                      1
421 #define any_online_cpu(mask)            0
422 #endif
423
424 #define for_each_possible_cpu(cpu)  for_each_cpu_mask((cpu), cpu_possible_map)
425 #define for_each_online_cpu(cpu)  for_each_cpu_mask((cpu), cpu_online_map)
426 #define for_each_present_cpu(cpu) for_each_cpu_mask((cpu), cpu_present_map)
427
428 #endif /* __LINUX_CPUMASK_H */