1 #ifndef __LINUX_CPUMASK_H
2 #define __LINUX_CPUMASK_H
5 * Cpumasks provide a bitmap suitable for representing the
6 * set of CPU's in a system, one bit position per CPU number.
8 * See detailed comments in the file linux/bitmap.h describing the
9 * data type on which these cpumasks are based.
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 * For details of cpus_onto(), see bitmap_onto in lib/bitmap.c.
18 * For details of cpus_fold(), see bitmap_fold in lib/bitmap.c.
20 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21 * Note: The alternate operations with the suffix "_nr" are used
22 * to limit the range of the loop to nr_cpu_ids instead of
23 * NR_CPUS when NR_CPUS > 64 for performance reasons.
24 * If NR_CPUS is <= 64 then most assembler bitmask
25 * operators execute faster with a constant range, so
26 * the operator will continue to use NR_CPUS.
28 * Another consideration is that nr_cpu_ids is initialized
29 * to NR_CPUS and isn't lowered until the possible cpus are
30 * discovered (including any disabled cpus). So early uses
31 * will span the entire range of NR_CPUS.
32 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
34 * The available cpumask operations are:
36 * void cpu_set(cpu, mask) turn on bit 'cpu' in mask
37 * void cpu_clear(cpu, mask) turn off bit 'cpu' in mask
38 * void cpus_setall(mask) set all bits
39 * void cpus_clear(mask) clear all bits
40 * int cpu_isset(cpu, mask) true iff bit 'cpu' set in mask
41 * int cpu_test_and_set(cpu, mask) test and set bit 'cpu' in mask
43 * void cpus_and(dst, src1, src2) dst = src1 & src2 [intersection]
44 * void cpus_or(dst, src1, src2) dst = src1 | src2 [union]
45 * void cpus_xor(dst, src1, src2) dst = src1 ^ src2
46 * void cpus_andnot(dst, src1, src2) dst = src1 & ~src2
47 * void cpus_complement(dst, src) dst = ~src
49 * int cpus_equal(mask1, mask2) Does mask1 == mask2?
50 * int cpus_intersects(mask1, mask2) Do mask1 and mask2 intersect?
51 * int cpus_subset(mask1, mask2) Is mask1 a subset of mask2?
52 * int cpus_empty(mask) Is mask empty (no bits sets)?
53 * int cpus_full(mask) Is mask full (all bits sets)?
54 * int cpus_weight(mask) Hamming weigh - number of set bits
55 * int cpus_weight_nr(mask) Same using nr_cpu_ids instead of NR_CPUS
57 * void cpus_shift_right(dst, src, n) Shift right
58 * void cpus_shift_left(dst, src, n) Shift left
60 * int first_cpu(mask) Number lowest set bit, or NR_CPUS
61 * int next_cpu(cpu, mask) Next cpu past 'cpu', or NR_CPUS
62 * int next_cpu_nr(cpu, mask) Next cpu past 'cpu', or nr_cpu_ids
64 * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
65 *ifdef CONFIG_HAS_CPUMASK_OF_CPU
66 * cpumask_of_cpu_ptr_declare(v) Declares cpumask_t *v
67 * cpumask_of_cpu_ptr_next(v, cpu) Sets v = &cpumask_of_cpu_map[cpu]
68 * cpumask_of_cpu_ptr(v, cpu) Combines above two operations
70 * cpumask_of_cpu_ptr_declare(v) Declares cpumask_t _v and *v = &_v
71 * cpumask_of_cpu_ptr_next(v, cpu) Sets _v = cpumask_of_cpu(cpu)
72 * cpumask_of_cpu_ptr(v, cpu) Combines above two operations
74 * CPU_MASK_ALL Initializer - all bits set
75 * CPU_MASK_NONE Initializer - no bits set
76 * unsigned long *cpus_addr(mask) Array of unsigned long's in mask
78 *if NR_CPUS > BITS_PER_LONG
79 * CPUMASK_ALLOC(m) Declares and allocates struct m *m =
80 * (struct m *)kmalloc(sizeof(*m), ...)
81 * CPUMASK_FREE(m) Macro for kfree(v)
83 * CPUMASK_ALLOC(m) Declares struct m _m, *m = &_m
86 * CPUMASK_VAR(v, m) Declares cpumask_t *v =
87 * m + offset(struct m, v)
89 * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
90 * int cpumask_parse_user(ubuf, ulen, mask) Parse ascii string as cpumask
91 * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
92 * int cpulist_parse(buf, map) Parse ascii string as cpulist
93 * int cpu_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
94 * void cpus_remap(dst, src, old, new) *dst = map(old, new)(src)
95 * void cpus_onto(dst, orig, relmap) *dst = orig relative to relmap
96 * void cpus_fold(dst, orig, sz) dst bits = orig bits mod sz
98 * for_each_cpu_mask(cpu, mask) for-loop cpu over mask using NR_CPUS
99 * for_each_cpu_mask_nr(cpu, mask) for-loop cpu over mask using nr_cpu_ids
101 * int num_online_cpus() Number of online CPUs
102 * int num_possible_cpus() Number of all possible CPUs
103 * int num_present_cpus() Number of present CPUs
105 * int cpu_online(cpu) Is some cpu online?
106 * int cpu_possible(cpu) Is some cpu possible?
107 * int cpu_present(cpu) Is some cpu present (can schedule)?
109 * int any_online_cpu(mask) First online cpu in mask
111 * for_each_possible_cpu(cpu) for-loop cpu over cpu_possible_map
112 * for_each_online_cpu(cpu) for-loop cpu over cpu_online_map
113 * for_each_present_cpu(cpu) for-loop cpu over cpu_present_map
116 * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
117 * to generate slightly worse code. Note for example the additional
118 * 40 lines of assembly code compiling the "for each possible cpu"
119 * loops buried in the disk_stat_read() macros calls when compiling
120 * drivers/block/genhd.c (arch i386, CONFIG_SMP=y). So use a simple
121 * one-line #define for cpu_isset(), instead of wrapping an inline
122 * inside a macro, the way we do the other calls.
125 #include <linux/kernel.h>
126 #include <linux/threads.h>
127 #include <linux/bitmap.h>
129 typedef struct { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
130 extern cpumask_t _unused_cpumask_arg_;
132 #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
133 static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
135 set_bit(cpu, dstp->bits);
138 #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
139 static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
141 clear_bit(cpu, dstp->bits);
144 #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
145 static inline void __cpus_setall(cpumask_t *dstp, int nbits)
147 bitmap_fill(dstp->bits, nbits);
150 #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
151 static inline void __cpus_clear(cpumask_t *dstp, int nbits)
153 bitmap_zero(dstp->bits, nbits);
156 /* No static inline type checking - see Subtlety (1) above. */
157 #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
159 #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
160 static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
162 return test_and_set_bit(cpu, addr->bits);
165 #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
166 static inline void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
167 const cpumask_t *src2p, int nbits)
169 bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
172 #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
173 static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
174 const cpumask_t *src2p, int nbits)
176 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
179 #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
180 static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
181 const cpumask_t *src2p, int nbits)
183 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
186 #define cpus_andnot(dst, src1, src2) \
187 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
188 static inline void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
189 const cpumask_t *src2p, int nbits)
191 bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
194 #define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
195 static inline void __cpus_complement(cpumask_t *dstp,
196 const cpumask_t *srcp, int nbits)
198 bitmap_complement(dstp->bits, srcp->bits, nbits);
201 #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
202 static inline int __cpus_equal(const cpumask_t *src1p,
203 const cpumask_t *src2p, int nbits)
205 return bitmap_equal(src1p->bits, src2p->bits, nbits);
208 #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
209 static inline int __cpus_intersects(const cpumask_t *src1p,
210 const cpumask_t *src2p, int nbits)
212 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
215 #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
216 static inline int __cpus_subset(const cpumask_t *src1p,
217 const cpumask_t *src2p, int nbits)
219 return bitmap_subset(src1p->bits, src2p->bits, nbits);
222 #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
223 static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
225 return bitmap_empty(srcp->bits, nbits);
228 #define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
229 static inline int __cpus_full(const cpumask_t *srcp, int nbits)
231 return bitmap_full(srcp->bits, nbits);
234 #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
235 static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
237 return bitmap_weight(srcp->bits, nbits);
240 #define cpus_shift_right(dst, src, n) \
241 __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
242 static inline void __cpus_shift_right(cpumask_t *dstp,
243 const cpumask_t *srcp, int n, int nbits)
245 bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
248 #define cpus_shift_left(dst, src, n) \
249 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
250 static inline void __cpus_shift_left(cpumask_t *dstp,
251 const cpumask_t *srcp, int n, int nbits)
253 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
257 #ifdef CONFIG_HAVE_CPUMASK_OF_CPU_MAP
258 extern cpumask_t *cpumask_of_cpu_map;
259 #define cpumask_of_cpu(cpu) (cpumask_of_cpu_map[cpu])
260 #define cpumask_of_cpu_ptr(v, cpu) \
261 const cpumask_t *v = &cpumask_of_cpu(cpu)
262 #define cpumask_of_cpu_ptr_declare(v) \
264 #define cpumask_of_cpu_ptr_next(v, cpu) \
265 v = &cpumask_of_cpu(cpu)
267 #define cpumask_of_cpu(cpu) \
269 typeof(_unused_cpumask_arg_) m; \
270 if (sizeof(m) == sizeof(unsigned long)) { \
271 m.bits[0] = 1UL<<(cpu); \
278 #define cpumask_of_cpu_ptr(v, cpu) \
279 cpumask_t _##v = cpumask_of_cpu(cpu); \
280 const cpumask_t *v = &_##v
281 #define cpumask_of_cpu_ptr_declare(v) \
283 const cpumask_t *v = &_##v
284 #define cpumask_of_cpu_ptr_next(v, cpu) \
285 _##v = cpumask_of_cpu(cpu)
288 #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
290 #if NR_CPUS <= BITS_PER_LONG
292 #define CPU_MASK_ALL \
294 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
297 #define CPU_MASK_ALL_PTR (&CPU_MASK_ALL)
301 #define CPU_MASK_ALL \
303 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
304 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
307 /* cpu_mask_all is in init/main.c */
308 extern cpumask_t cpu_mask_all;
309 #define CPU_MASK_ALL_PTR (&cpu_mask_all)
313 #define CPU_MASK_NONE \
315 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
318 #define CPU_MASK_CPU0 \
323 #define cpus_addr(src) ((src).bits)
325 #if NR_CPUS > BITS_PER_LONG
326 #define CPUMASK_ALLOC(m) struct m *m = kmalloc(sizeof(*m), GFP_KERNEL)
327 #define CPUMASK_FREE(m) kfree(m)
329 #define CPUMASK_ALLOC(m) struct allmasks _m, *m = &_m
330 #define CPUMASK_FREE(m)
332 #define CPUMASK_VAR(v, m) cpumask_t *v = (cpumask_t *) \
333 ((unsigned long)(m) + offsetof(struct m, v))
335 #define cpumask_scnprintf(buf, len, src) \
336 __cpumask_scnprintf((buf), (len), &(src), NR_CPUS)
337 static inline int __cpumask_scnprintf(char *buf, int len,
338 const cpumask_t *srcp, int nbits)
340 return bitmap_scnprintf(buf, len, srcp->bits, nbits);
343 #define cpumask_parse_user(ubuf, ulen, dst) \
344 __cpumask_parse_user((ubuf), (ulen), &(dst), NR_CPUS)
345 static inline int __cpumask_parse_user(const char __user *buf, int len,
346 cpumask_t *dstp, int nbits)
348 return bitmap_parse_user(buf, len, dstp->bits, nbits);
351 #define cpulist_scnprintf(buf, len, src) \
352 __cpulist_scnprintf((buf), (len), &(src), NR_CPUS)
353 static inline int __cpulist_scnprintf(char *buf, int len,
354 const cpumask_t *srcp, int nbits)
356 return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
359 #define cpulist_parse(buf, dst) __cpulist_parse((buf), &(dst), NR_CPUS)
360 static inline int __cpulist_parse(const char *buf, cpumask_t *dstp, int nbits)
362 return bitmap_parselist(buf, dstp->bits, nbits);
365 #define cpu_remap(oldbit, old, new) \
366 __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
367 static inline int __cpu_remap(int oldbit,
368 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
370 return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
373 #define cpus_remap(dst, src, old, new) \
374 __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
375 static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
376 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
378 bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
381 #define cpus_onto(dst, orig, relmap) \
382 __cpus_onto(&(dst), &(orig), &(relmap), NR_CPUS)
383 static inline void __cpus_onto(cpumask_t *dstp, const cpumask_t *origp,
384 const cpumask_t *relmapp, int nbits)
386 bitmap_onto(dstp->bits, origp->bits, relmapp->bits, nbits);
389 #define cpus_fold(dst, orig, sz) \
390 __cpus_fold(&(dst), &(orig), sz, NR_CPUS)
391 static inline void __cpus_fold(cpumask_t *dstp, const cpumask_t *origp,
394 bitmap_fold(dstp->bits, origp->bits, sz, nbits);
400 #define first_cpu(src) ({ (void)(src); 0; })
401 #define next_cpu(n, src) ({ (void)(src); 1; })
402 #define any_online_cpu(mask) 0
403 #define for_each_cpu_mask(cpu, mask) \
404 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
406 #else /* NR_CPUS > 1 */
408 extern int nr_cpu_ids;
409 int __first_cpu(const cpumask_t *srcp);
410 int __next_cpu(int n, const cpumask_t *srcp);
411 int __any_online_cpu(const cpumask_t *mask);
413 #define first_cpu(src) __first_cpu(&(src))
414 #define next_cpu(n, src) __next_cpu((n), &(src))
415 #define any_online_cpu(mask) __any_online_cpu(&(mask))
416 #define for_each_cpu_mask(cpu, mask) \
418 (cpu) = next_cpu((cpu), (mask)), \
424 #define next_cpu_nr(n, src) next_cpu(n, src)
425 #define cpus_weight_nr(cpumask) cpus_weight(cpumask)
426 #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
428 #else /* NR_CPUS > 64 */
430 int __next_cpu_nr(int n, const cpumask_t *srcp);
431 #define next_cpu_nr(n, src) __next_cpu_nr((n), &(src))
432 #define cpus_weight_nr(cpumask) __cpus_weight(&(cpumask), nr_cpu_ids)
433 #define for_each_cpu_mask_nr(cpu, mask) \
435 (cpu) = next_cpu_nr((cpu), (mask)), \
436 (cpu) < nr_cpu_ids; )
438 #endif /* NR_CPUS > 64 */
441 * The following particular system cpumasks and operations manage
442 * possible, present and online cpus. Each of them is a fixed size
443 * bitmap of size NR_CPUS.
445 * #ifdef CONFIG_HOTPLUG_CPU
446 * cpu_possible_map - has bit 'cpu' set iff cpu is populatable
447 * cpu_present_map - has bit 'cpu' set iff cpu is populated
448 * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
450 * cpu_possible_map - has bit 'cpu' set iff cpu is populated
451 * cpu_present_map - copy of cpu_possible_map
452 * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
455 * In either case, NR_CPUS is fixed at compile time, as the static
456 * size of these bitmaps. The cpu_possible_map is fixed at boot
457 * time, as the set of CPU id's that it is possible might ever
458 * be plugged in at anytime during the life of that system boot.
459 * The cpu_present_map is dynamic(*), representing which CPUs
460 * are currently plugged in. And cpu_online_map is the dynamic
461 * subset of cpu_present_map, indicating those CPUs available
464 * If HOTPLUG is enabled, then cpu_possible_map is forced to have
465 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
466 * ACPI reports present at boot.
468 * If HOTPLUG is enabled, then cpu_present_map varies dynamically,
469 * depending on what ACPI reports as currently plugged in, otherwise
470 * cpu_present_map is just a copy of cpu_possible_map.
472 * (*) Well, cpu_present_map is dynamic in the hotplug case. If not
473 * hotplug, it's a copy of cpu_possible_map, hence fixed at boot.
476 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
477 * assumption that their single CPU is online. The UP
478 * cpu_{online,possible,present}_maps are placebos. Changing them
479 * will have no useful affect on the following num_*_cpus()
480 * and cpu_*() macros in the UP case. This ugliness is a UP
481 * optimization - don't waste any instructions or memory references
482 * asking if you're online or how many CPUs there are if there is
484 * 2) Most SMP arch's #define some of these maps to be some
485 * other map specific to that arch. Therefore, the following
486 * must be #define macros, not inlines. To see why, examine
487 * the assembly code produced by the following. Note that
488 * set1() writes phys_x_map, but set2() writes x_map:
489 * int x_map, phys_x_map;
490 * #define set1(a) x_map = a
491 * inline void set2(int a) { x_map = a; }
492 * #define x_map phys_x_map
493 * main(){ set1(3); set2(5); }
496 extern cpumask_t cpu_possible_map;
497 extern cpumask_t cpu_online_map;
498 extern cpumask_t cpu_present_map;
501 #define num_online_cpus() cpus_weight_nr(cpu_online_map)
502 #define num_possible_cpus() cpus_weight_nr(cpu_possible_map)
503 #define num_present_cpus() cpus_weight_nr(cpu_present_map)
504 #define cpu_online(cpu) cpu_isset((cpu), cpu_online_map)
505 #define cpu_possible(cpu) cpu_isset((cpu), cpu_possible_map)
506 #define cpu_present(cpu) cpu_isset((cpu), cpu_present_map)
508 #define num_online_cpus() 1
509 #define num_possible_cpus() 1
510 #define num_present_cpus() 1
511 #define cpu_online(cpu) ((cpu) == 0)
512 #define cpu_possible(cpu) ((cpu) == 0)
513 #define cpu_present(cpu) ((cpu) == 0)
516 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
518 #define for_each_possible_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_possible_map)
519 #define for_each_online_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_online_map)
520 #define for_each_present_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_present_map)
522 #endif /* __LINUX_CPUMASK_H */