blob: 8fa3b6d4a320e6a31ba190cffb2749fcf9cad20e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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 *
Reinette Chatre01a3ee22006-10-11 01:21:55 -070011 * For details of cpumask_scnprintf() and cpumask_parse_user(),
12 * see bitmap_scnprintf() and bitmap_parse_user() in lib/bitmap.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * For details of cpulist_scnprintf() and cpulist_parse(), see
14 * bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c.
Paul Jacksonfb5eeee2005-10-30 15:02:33 -080015 * 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.
Paul Jackson7ea931c2008-04-28 02:12:29 -070017 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
Mike Travis41df0d612008-05-12 21:21:13 +020020 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
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.
27 *
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 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * The available cpumask operations are:
35 *
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
42 *
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
48 *
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
Mike Travis41df0d612008-05-12 21:21:13 +020055 * int cpus_weight_nr(mask) Same using nr_cpu_ids instead of NR_CPUS
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *
57 * void cpus_shift_right(dst, src, n) Shift right
58 * void cpus_shift_left(dst, src, n) Shift left
59 *
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
Mike Travis41df0d612008-05-12 21:21:13 +020062 * int next_cpu_nr(cpu, mask) Next cpu past 'cpu', or nr_cpu_ids
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
Mike Travisb8d317d2008-07-24 18:21:29 -070065 * (can be used as an lvalue)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * CPU_MASK_ALL Initializer - all bits set
67 * CPU_MASK_NONE Initializer - no bits set
68 * unsigned long *cpus_addr(mask) Array of unsigned long's in mask
69 *
Mike Travis80422d32008-07-18 18:11:33 -070070 * CPUMASK_ALLOC kmalloc's a structure that is a composite of many cpumask_t
71 * variables, and CPUMASK_PTR provides pointers to each field.
72 *
73 * The structure should be defined something like this:
74 * struct my_cpumasks {
75 * cpumask_t mask1;
76 * cpumask_t mask2;
77 * };
78 *
79 * Usage is then:
80 * CPUMASK_ALLOC(my_cpumasks);
81 * CPUMASK_PTR(mask1, my_cpumasks);
82 * CPUMASK_PTR(mask2, my_cpumasks);
83 *
84 * --- DO NOT reference cpumask_t pointers until this check ---
85 * if (my_cpumasks == NULL)
86 * "kmalloc failed"...
87 *
88 * References are now pointers to the cpumask_t variables (*mask1, ...)
89 *
Mike Travis77586c22008-07-15 14:14:36 -070090 *if NR_CPUS > BITS_PER_LONG
91 * CPUMASK_ALLOC(m) Declares and allocates struct m *m =
Mike Travis80422d32008-07-18 18:11:33 -070092 * kmalloc(sizeof(*m), GFP_KERNEL)
93 * CPUMASK_FREE(m) Macro for kfree(m)
Mike Travis77586c22008-07-15 14:14:36 -070094 *else
95 * CPUMASK_ALLOC(m) Declares struct m _m, *m = &_m
96 * CPUMASK_FREE(m) Nop
97 *endif
Mike Travis80422d32008-07-18 18:11:33 -070098 * CPUMASK_PTR(v, m) Declares cpumask_t *v = &(m->v)
99 * ------------------------------------------------------------------------
Mike Travis77586c22008-07-15 14:14:36 -0700100 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700102 * int cpumask_parse_user(ubuf, ulen, mask) Parse ascii string as cpumask
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
104 * int cpulist_parse(buf, map) Parse ascii string as cpulist
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800105 * int cpu_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
Paul Jackson7ea931c2008-04-28 02:12:29 -0700106 * void cpus_remap(dst, src, old, new) *dst = map(old, new)(src)
107 * void cpus_onto(dst, orig, relmap) *dst = orig relative to relmap
108 * void cpus_fold(dst, orig, sz) dst bits = orig bits mod sz
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Mike Travis41df0d612008-05-12 21:21:13 +0200110 * for_each_cpu_mask(cpu, mask) for-loop cpu over mask using NR_CPUS
111 * for_each_cpu_mask_nr(cpu, mask) for-loop cpu over mask using nr_cpu_ids
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
113 * int num_online_cpus() Number of online CPUs
114 * int num_possible_cpus() Number of all possible CPUs
115 * int num_present_cpus() Number of present CPUs
116 *
117 * int cpu_online(cpu) Is some cpu online?
118 * int cpu_possible(cpu) Is some cpu possible?
119 * int cpu_present(cpu) Is some cpu present (can schedule)?
120 *
121 * int any_online_cpu(mask) First online cpu in mask
122 *
KAMEZAWA Hiroyuki631d6742006-03-28 01:56:36 -0800123 * for_each_possible_cpu(cpu) for-loop cpu over cpu_possible_map
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * for_each_online_cpu(cpu) for-loop cpu over cpu_online_map
125 * for_each_present_cpu(cpu) for-loop cpu over cpu_present_map
126 *
127 * Subtlety:
128 * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
129 * to generate slightly worse code. Note for example the additional
130 * 40 lines of assembly code compiling the "for each possible cpu"
131 * loops buried in the disk_stat_read() macros calls when compiling
132 * drivers/block/genhd.c (arch i386, CONFIG_SMP=y). So use a simple
133 * one-line #define for cpu_isset(), instead of wrapping an inline
134 * inside a macro, the way we do the other calls.
135 */
136
137#include <linux/kernel.h>
138#include <linux/threads.h>
139#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141typedef struct { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
142extern cpumask_t _unused_cpumask_arg_;
143
144#define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
145static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
146{
147 set_bit(cpu, dstp->bits);
148}
149
150#define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
151static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
152{
153 clear_bit(cpu, dstp->bits);
154}
155
156#define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
157static inline void __cpus_setall(cpumask_t *dstp, int nbits)
158{
159 bitmap_fill(dstp->bits, nbits);
160}
161
162#define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
163static inline void __cpus_clear(cpumask_t *dstp, int nbits)
164{
165 bitmap_zero(dstp->bits, nbits);
166}
167
168/* No static inline type checking - see Subtlety (1) above. */
169#define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
170
171#define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
172static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
173{
174 return test_and_set_bit(cpu, addr->bits);
175}
176
177#define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
178static inline void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
179 const cpumask_t *src2p, int nbits)
180{
181 bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
182}
183
184#define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
185static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
186 const cpumask_t *src2p, int nbits)
187{
188 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
189}
190
191#define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
192static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
193 const cpumask_t *src2p, int nbits)
194{
195 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
196}
197
198#define cpus_andnot(dst, src1, src2) \
199 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
200static inline void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
201 const cpumask_t *src2p, int nbits)
202{
203 bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
204}
205
206#define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
207static inline void __cpus_complement(cpumask_t *dstp,
208 const cpumask_t *srcp, int nbits)
209{
210 bitmap_complement(dstp->bits, srcp->bits, nbits);
211}
212
213#define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
214static inline int __cpus_equal(const cpumask_t *src1p,
215 const cpumask_t *src2p, int nbits)
216{
217 return bitmap_equal(src1p->bits, src2p->bits, nbits);
218}
219
220#define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
221static inline int __cpus_intersects(const cpumask_t *src1p,
222 const cpumask_t *src2p, int nbits)
223{
224 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
225}
226
227#define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
228static inline int __cpus_subset(const cpumask_t *src1p,
229 const cpumask_t *src2p, int nbits)
230{
231 return bitmap_subset(src1p->bits, src2p->bits, nbits);
232}
233
234#define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
235static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
236{
237 return bitmap_empty(srcp->bits, nbits);
238}
239
240#define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
241static inline int __cpus_full(const cpumask_t *srcp, int nbits)
242{
243 return bitmap_full(srcp->bits, nbits);
244}
245
246#define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
247static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
248{
249 return bitmap_weight(srcp->bits, nbits);
250}
251
252#define cpus_shift_right(dst, src, n) \
253 __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
254static inline void __cpus_shift_right(cpumask_t *dstp,
255 const cpumask_t *srcp, int n, int nbits)
256{
257 bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
258}
259
260#define cpus_shift_left(dst, src, n) \
261 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
262static inline void __cpus_shift_left(cpumask_t *dstp,
263 const cpumask_t *srcp, int n, int nbits)
264{
265 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Mike Travisb8d317d2008-07-24 18:21:29 -0700269/* cpumask_of_cpu_map[] is in kernel/cpu.c */
270extern const cpumask_t *cpumask_of_cpu_map;
Mike Travis65c01182008-07-15 14:14:30 -0700271#define cpumask_of_cpu(cpu) (cpumask_of_cpu_map[cpu])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273#define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
274
275#if NR_CPUS <= BITS_PER_LONG
276
277#define CPU_MASK_ALL \
278(cpumask_t) { { \
279 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
280} }
281
Mike Travis321a8e92008-04-04 18:11:02 -0700282#define CPU_MASK_ALL_PTR (&CPU_MASK_ALL)
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#else
285
286#define CPU_MASK_ALL \
287(cpumask_t) { { \
288 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
289 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
290} }
291
Mike Travis321a8e92008-04-04 18:11:02 -0700292/* cpu_mask_all is in init/main.c */
293extern cpumask_t cpu_mask_all;
294#define CPU_MASK_ALL_PTR (&cpu_mask_all)
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296#endif
297
298#define CPU_MASK_NONE \
299(cpumask_t) { { \
300 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
301} }
302
303#define CPU_MASK_CPU0 \
304(cpumask_t) { { \
305 [0] = 1UL \
306} }
307
308#define cpus_addr(src) ((src).bits)
309
Mike Travis77586c22008-07-15 14:14:36 -0700310#if NR_CPUS > BITS_PER_LONG
311#define CPUMASK_ALLOC(m) struct m *m = kmalloc(sizeof(*m), GFP_KERNEL)
312#define CPUMASK_FREE(m) kfree(m)
313#else
Mike Travis80422d32008-07-18 18:11:33 -0700314#define CPUMASK_ALLOC(m) struct m _m, *m = &_m
Mike Travis77586c22008-07-15 14:14:36 -0700315#define CPUMASK_FREE(m)
316#endif
Mike Travis80422d32008-07-18 18:11:33 -0700317#define CPUMASK_PTR(v, m) cpumask_t *v = &(m->v)
Mike Travis77586c22008-07-15 14:14:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#define cpumask_scnprintf(buf, len, src) \
320 __cpumask_scnprintf((buf), (len), &(src), NR_CPUS)
321static inline int __cpumask_scnprintf(char *buf, int len,
322 const cpumask_t *srcp, int nbits)
323{
324 return bitmap_scnprintf(buf, len, srcp->bits, nbits);
325}
326
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700327#define cpumask_parse_user(ubuf, ulen, dst) \
328 __cpumask_parse_user((ubuf), (ulen), &(dst), NR_CPUS)
329static inline int __cpumask_parse_user(const char __user *buf, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 cpumask_t *dstp, int nbits)
331{
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700332 return bitmap_parse_user(buf, len, dstp->bits, nbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335#define cpulist_scnprintf(buf, len, src) \
336 __cpulist_scnprintf((buf), (len), &(src), NR_CPUS)
337static inline int __cpulist_scnprintf(char *buf, int len,
338 const cpumask_t *srcp, int nbits)
339{
340 return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
341}
342
343#define cpulist_parse(buf, dst) __cpulist_parse((buf), &(dst), NR_CPUS)
344static inline int __cpulist_parse(const char *buf, cpumask_t *dstp, int nbits)
345{
346 return bitmap_parselist(buf, dstp->bits, nbits);
347}
348
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800349#define cpu_remap(oldbit, old, new) \
350 __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
351static inline int __cpu_remap(int oldbit,
352 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
353{
354 return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
355}
356
357#define cpus_remap(dst, src, old, new) \
358 __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
359static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
360 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
361{
362 bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
363}
364
Paul Jackson7ea931c2008-04-28 02:12:29 -0700365#define cpus_onto(dst, orig, relmap) \
366 __cpus_onto(&(dst), &(orig), &(relmap), NR_CPUS)
367static inline void __cpus_onto(cpumask_t *dstp, const cpumask_t *origp,
368 const cpumask_t *relmapp, int nbits)
369{
370 bitmap_onto(dstp->bits, origp->bits, relmapp->bits, nbits);
371}
372
373#define cpus_fold(dst, orig, sz) \
374 __cpus_fold(&(dst), &(orig), sz, NR_CPUS)
375static inline void __cpus_fold(cpumask_t *dstp, const cpumask_t *origp,
376 int sz, int nbits)
377{
378 bitmap_fold(dstp->bits, origp->bits, sz, nbits);
379}
380
Mike Travis41df0d612008-05-12 21:21:13 +0200381#if NR_CPUS == 1
382
383#define nr_cpu_ids 1
384#define first_cpu(src) ({ (void)(src); 0; })
385#define next_cpu(n, src) ({ (void)(src); 1; })
386#define any_online_cpu(mask) 0
387#define for_each_cpu_mask(cpu, mask) \
388 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
389
390#else /* NR_CPUS > 1 */
391
392extern int nr_cpu_ids;
393int __first_cpu(const cpumask_t *srcp);
394int __next_cpu(int n, const cpumask_t *srcp);
395int __any_online_cpu(const cpumask_t *mask);
396
397#define first_cpu(src) __first_cpu(&(src))
398#define next_cpu(n, src) __next_cpu((n), &(src))
399#define any_online_cpu(mask) __any_online_cpu(&(mask))
Alexander van Heukelum7baac8b2008-05-13 11:28:21 +0200400#define for_each_cpu_mask(cpu, mask) \
401 for ((cpu) = -1; \
402 (cpu) = next_cpu((cpu), (mask)), \
403 (cpu) < NR_CPUS; )
Mike Travis41df0d612008-05-12 21:21:13 +0200404#endif
405
406#if NR_CPUS <= 64
407
408#define next_cpu_nr(n, src) next_cpu(n, src)
409#define cpus_weight_nr(cpumask) cpus_weight(cpumask)
410#define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
411
412#else /* NR_CPUS > 64 */
413
414int __next_cpu_nr(int n, const cpumask_t *srcp);
415#define next_cpu_nr(n, src) __next_cpu_nr((n), &(src))
416#define cpus_weight_nr(cpumask) __cpus_weight(&(cpumask), nr_cpu_ids)
Alexander van Heukelum7baac8b2008-05-13 11:28:21 +0200417#define for_each_cpu_mask_nr(cpu, mask) \
418 for ((cpu) = -1; \
419 (cpu) = next_cpu_nr((cpu), (mask)), \
420 (cpu) < nr_cpu_ids; )
Mike Travis41df0d612008-05-12 21:21:13 +0200421
422#endif /* NR_CPUS > 64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424/*
425 * The following particular system cpumasks and operations manage
Max Krasnyanskye761b772008-07-15 04:43:49 -0700426 * possible, present, active and online cpus. Each of them is a fixed size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * bitmap of size NR_CPUS.
428 *
429 * #ifdef CONFIG_HOTPLUG_CPU
Andrew Morton7a8ef1c2006-02-10 01:51:08 -0800430 * cpu_possible_map - has bit 'cpu' set iff cpu is populatable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * cpu_present_map - has bit 'cpu' set iff cpu is populated
432 * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
Max Krasnyanskye761b772008-07-15 04:43:49 -0700433 * cpu_active_map - has bit 'cpu' set iff cpu available to migration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * #else
435 * cpu_possible_map - has bit 'cpu' set iff cpu is populated
436 * cpu_present_map - copy of cpu_possible_map
437 * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
438 * #endif
439 *
440 * In either case, NR_CPUS is fixed at compile time, as the static
441 * size of these bitmaps. The cpu_possible_map is fixed at boot
442 * time, as the set of CPU id's that it is possible might ever
443 * be plugged in at anytime during the life of that system boot.
444 * The cpu_present_map is dynamic(*), representing which CPUs
445 * are currently plugged in. And cpu_online_map is the dynamic
446 * subset of cpu_present_map, indicating those CPUs available
447 * for scheduling.
448 *
449 * If HOTPLUG is enabled, then cpu_possible_map is forced to have
450 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
451 * ACPI reports present at boot.
452 *
453 * If HOTPLUG is enabled, then cpu_present_map varies dynamically,
454 * depending on what ACPI reports as currently plugged in, otherwise
455 * cpu_present_map is just a copy of cpu_possible_map.
456 *
457 * (*) Well, cpu_present_map is dynamic in the hotplug case. If not
458 * hotplug, it's a copy of cpu_possible_map, hence fixed at boot.
459 *
460 * Subtleties:
461 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
462 * assumption that their single CPU is online. The UP
463 * cpu_{online,possible,present}_maps are placebos. Changing them
464 * will have no useful affect on the following num_*_cpus()
465 * and cpu_*() macros in the UP case. This ugliness is a UP
466 * optimization - don't waste any instructions or memory references
467 * asking if you're online or how many CPUs there are if there is
468 * only one CPU.
469 * 2) Most SMP arch's #define some of these maps to be some
470 * other map specific to that arch. Therefore, the following
471 * must be #define macros, not inlines. To see why, examine
472 * the assembly code produced by the following. Note that
473 * set1() writes phys_x_map, but set2() writes x_map:
474 * int x_map, phys_x_map;
475 * #define set1(a) x_map = a
476 * inline void set2(int a) { x_map = a; }
477 * #define x_map phys_x_map
478 * main(){ set1(3); set2(5); }
479 */
480
481extern cpumask_t cpu_possible_map;
482extern cpumask_t cpu_online_map;
483extern cpumask_t cpu_present_map;
Max Krasnyanskye761b772008-07-15 04:43:49 -0700484extern cpumask_t cpu_active_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486#if NR_CPUS > 1
Mike Travis41df0d612008-05-12 21:21:13 +0200487#define num_online_cpus() cpus_weight_nr(cpu_online_map)
488#define num_possible_cpus() cpus_weight_nr(cpu_possible_map)
489#define num_present_cpus() cpus_weight_nr(cpu_present_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#define cpu_online(cpu) cpu_isset((cpu), cpu_online_map)
491#define cpu_possible(cpu) cpu_isset((cpu), cpu_possible_map)
492#define cpu_present(cpu) cpu_isset((cpu), cpu_present_map)
Max Krasnyanskye761b772008-07-15 04:43:49 -0700493#define cpu_active(cpu) cpu_isset((cpu), cpu_active_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494#else
495#define num_online_cpus() 1
496#define num_possible_cpus() 1
497#define num_present_cpus() 1
498#define cpu_online(cpu) ((cpu) == 0)
499#define cpu_possible(cpu) ((cpu) == 0)
500#define cpu_present(cpu) ((cpu) == 0)
Max Krasnyanskye761b772008-07-15 04:43:49 -0700501#define cpu_active(cpu) ((cpu) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502#endif
503
Ingo Molnara2638982007-12-30 11:58:17 +0100504#define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
505
Mike Travis41df0d612008-05-12 21:21:13 +0200506#define for_each_possible_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_possible_map)
507#define for_each_online_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_online_map)
508#define for_each_present_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_present_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#endif /* __LINUX_CPUMASK_H */