cpumask: change cpumask_scnprintf, cpumask_parse_user, cpulist_parse, and cpulist_scn...
[linux-2.6-block.git] / include / linux / cpumask.h
CommitLineData
1da177e4
LT
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 *
2d3854a3
RR
8 * The new cpumask_ ops take a "struct cpumask *"; the old ones
9 * use cpumask_t.
10 *
1da177e4
LT
11 * See detailed comments in the file linux/bitmap.h describing the
12 * data type on which these cpumasks are based.
13 *
01a3ee2b
RC
14 * For details of cpumask_scnprintf() and cpumask_parse_user(),
15 * see bitmap_scnprintf() and bitmap_parse_user() in lib/bitmap.c.
1da177e4
LT
16 * For details of cpulist_scnprintf() and cpulist_parse(), see
17 * bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c.
fb5eeeee
PJ
18 * For details of cpu_remap(), see bitmap_bitremap in lib/bitmap.c
19 * For details of cpus_remap(), see bitmap_remap in lib/bitmap.c.
7ea931c9
PJ
20 * For details of cpus_onto(), see bitmap_onto in lib/bitmap.c.
21 * For details of cpus_fold(), see bitmap_fold in lib/bitmap.c.
1da177e4 22 *
41df0d61
MT
23 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24 * Note: The alternate operations with the suffix "_nr" are used
25 * to limit the range of the loop to nr_cpu_ids instead of
26 * NR_CPUS when NR_CPUS > 64 for performance reasons.
27 * If NR_CPUS is <= 64 then most assembler bitmask
28 * operators execute faster with a constant range, so
29 * the operator will continue to use NR_CPUS.
30 *
31 * Another consideration is that nr_cpu_ids is initialized
32 * to NR_CPUS and isn't lowered until the possible cpus are
33 * discovered (including any disabled cpus). So early uses
34 * will span the entire range of NR_CPUS.
35 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36 *
2d3854a3 37 * The obsolescent cpumask operations are:
1da177e4
LT
38 *
39 * void cpu_set(cpu, mask) turn on bit 'cpu' in mask
40 * void cpu_clear(cpu, mask) turn off bit 'cpu' in mask
41 * void cpus_setall(mask) set all bits
42 * void cpus_clear(mask) clear all bits
43 * int cpu_isset(cpu, mask) true iff bit 'cpu' set in mask
44 * int cpu_test_and_set(cpu, mask) test and set bit 'cpu' in mask
45 *
46 * void cpus_and(dst, src1, src2) dst = src1 & src2 [intersection]
47 * void cpus_or(dst, src1, src2) dst = src1 | src2 [union]
48 * void cpus_xor(dst, src1, src2) dst = src1 ^ src2
49 * void cpus_andnot(dst, src1, src2) dst = src1 & ~src2
50 * void cpus_complement(dst, src) dst = ~src
51 *
52 * int cpus_equal(mask1, mask2) Does mask1 == mask2?
53 * int cpus_intersects(mask1, mask2) Do mask1 and mask2 intersect?
54 * int cpus_subset(mask1, mask2) Is mask1 a subset of mask2?
55 * int cpus_empty(mask) Is mask empty (no bits sets)?
56 * int cpus_full(mask) Is mask full (all bits sets)?
57 * int cpus_weight(mask) Hamming weigh - number of set bits
41df0d61 58 * int cpus_weight_nr(mask) Same using nr_cpu_ids instead of NR_CPUS
1da177e4
LT
59 *
60 * void cpus_shift_right(dst, src, n) Shift right
61 * void cpus_shift_left(dst, src, n) Shift left
62 *
63 * int first_cpu(mask) Number lowest set bit, or NR_CPUS
64 * int next_cpu(cpu, mask) Next cpu past 'cpu', or NR_CPUS
41df0d61 65 * int next_cpu_nr(cpu, mask) Next cpu past 'cpu', or nr_cpu_ids
1da177e4
LT
66 *
67 * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
b8d317d1 68 * (can be used as an lvalue)
1da177e4
LT
69 * CPU_MASK_ALL Initializer - all bits set
70 * CPU_MASK_NONE Initializer - no bits set
71 * unsigned long *cpus_addr(mask) Array of unsigned long's in mask
72 *
80422d34
MT
73 * CPUMASK_ALLOC kmalloc's a structure that is a composite of many cpumask_t
74 * variables, and CPUMASK_PTR provides pointers to each field.
75 *
76 * The structure should be defined something like this:
77 * struct my_cpumasks {
78 * cpumask_t mask1;
79 * cpumask_t mask2;
80 * };
81 *
82 * Usage is then:
83 * CPUMASK_ALLOC(my_cpumasks);
84 * CPUMASK_PTR(mask1, my_cpumasks);
85 * CPUMASK_PTR(mask2, my_cpumasks);
86 *
87 * --- DO NOT reference cpumask_t pointers until this check ---
88 * if (my_cpumasks == NULL)
89 * "kmalloc failed"...
90 *
91 * References are now pointers to the cpumask_t variables (*mask1, ...)
92 *
77586c2b
MT
93 *if NR_CPUS > BITS_PER_LONG
94 * CPUMASK_ALLOC(m) Declares and allocates struct m *m =
80422d34
MT
95 * kmalloc(sizeof(*m), GFP_KERNEL)
96 * CPUMASK_FREE(m) Macro for kfree(m)
77586c2b
MT
97 *else
98 * CPUMASK_ALLOC(m) Declares struct m _m, *m = &_m
99 * CPUMASK_FREE(m) Nop
100 *endif
80422d34
MT
101 * CPUMASK_PTR(v, m) Declares cpumask_t *v = &(m->v)
102 * ------------------------------------------------------------------------
77586c2b 103 *
1da177e4 104 * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
01a3ee2b 105 * int cpumask_parse_user(ubuf, ulen, mask) Parse ascii string as cpumask
1da177e4
LT
106 * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
107 * int cpulist_parse(buf, map) Parse ascii string as cpulist
fb5eeeee 108 * int cpu_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
7ea931c9
PJ
109 * void cpus_remap(dst, src, old, new) *dst = map(old, new)(src)
110 * void cpus_onto(dst, orig, relmap) *dst = orig relative to relmap
111 * void cpus_fold(dst, orig, sz) dst bits = orig bits mod sz
1da177e4 112 *
41df0d61
MT
113 * for_each_cpu_mask(cpu, mask) for-loop cpu over mask using NR_CPUS
114 * for_each_cpu_mask_nr(cpu, mask) for-loop cpu over mask using nr_cpu_ids
1da177e4
LT
115 *
116 * int num_online_cpus() Number of online CPUs
117 * int num_possible_cpus() Number of all possible CPUs
118 * int num_present_cpus() Number of present CPUs
119 *
120 * int cpu_online(cpu) Is some cpu online?
121 * int cpu_possible(cpu) Is some cpu possible?
122 * int cpu_present(cpu) Is some cpu present (can schedule)?
123 *
124 * int any_online_cpu(mask) First online cpu in mask
125 *
631d6747 126 * for_each_possible_cpu(cpu) for-loop cpu over cpu_possible_map
1da177e4
LT
127 * for_each_online_cpu(cpu) for-loop cpu over cpu_online_map
128 * for_each_present_cpu(cpu) for-loop cpu over cpu_present_map
129 *
130 * Subtlety:
131 * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
132 * to generate slightly worse code. Note for example the additional
133 * 40 lines of assembly code compiling the "for each possible cpu"
134 * loops buried in the disk_stat_read() macros calls when compiling
135 * drivers/block/genhd.c (arch i386, CONFIG_SMP=y). So use a simple
136 * one-line #define for cpu_isset(), instead of wrapping an inline
137 * inside a macro, the way we do the other calls.
138 */
139
140#include <linux/kernel.h>
141#include <linux/threads.h>
142#include <linux/bitmap.h>
1da177e4 143
2d3854a3 144typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
1da177e4
LT
145extern cpumask_t _unused_cpumask_arg_;
146
147#define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
148static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
149{
150 set_bit(cpu, dstp->bits);
151}
152
153#define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
154static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
155{
156 clear_bit(cpu, dstp->bits);
157}
158
159#define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
160static inline void __cpus_setall(cpumask_t *dstp, int nbits)
161{
162 bitmap_fill(dstp->bits, nbits);
163}
164
165#define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
166static inline void __cpus_clear(cpumask_t *dstp, int nbits)
167{
168 bitmap_zero(dstp->bits, nbits);
169}
170
171/* No static inline type checking - see Subtlety (1) above. */
172#define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
173
174#define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
175static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
176{
177 return test_and_set_bit(cpu, addr->bits);
178}
179
180#define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
181static inline void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
182 const cpumask_t *src2p, int nbits)
183{
184 bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
185}
186
187#define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
188static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
189 const cpumask_t *src2p, int nbits)
190{
191 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
192}
193
194#define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
195static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
196 const cpumask_t *src2p, int nbits)
197{
198 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
199}
200
201#define cpus_andnot(dst, src1, src2) \
202 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
203static inline void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
204 const cpumask_t *src2p, int nbits)
205{
206 bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
207}
208
209#define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
210static inline void __cpus_complement(cpumask_t *dstp,
211 const cpumask_t *srcp, int nbits)
212{
213 bitmap_complement(dstp->bits, srcp->bits, nbits);
214}
215
216#define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
217static inline int __cpus_equal(const cpumask_t *src1p,
218 const cpumask_t *src2p, int nbits)
219{
220 return bitmap_equal(src1p->bits, src2p->bits, nbits);
221}
222
223#define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
224static inline int __cpus_intersects(const cpumask_t *src1p,
225 const cpumask_t *src2p, int nbits)
226{
227 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
228}
229
230#define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
231static inline int __cpus_subset(const cpumask_t *src1p,
232 const cpumask_t *src2p, int nbits)
233{
234 return bitmap_subset(src1p->bits, src2p->bits, nbits);
235}
236
237#define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
238static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
239{
240 return bitmap_empty(srcp->bits, nbits);
241}
242
243#define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
244static inline int __cpus_full(const cpumask_t *srcp, int nbits)
245{
246 return bitmap_full(srcp->bits, nbits);
247}
248
249#define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
250static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
251{
252 return bitmap_weight(srcp->bits, nbits);
253}
254
255#define cpus_shift_right(dst, src, n) \
256 __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
257static inline void __cpus_shift_right(cpumask_t *dstp,
258 const cpumask_t *srcp, int n, int nbits)
259{
260 bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
261}
262
263#define cpus_shift_left(dst, src, n) \
264 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
265static inline void __cpus_shift_left(cpumask_t *dstp,
266 const cpumask_t *srcp, int n, int nbits)
267{
268 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
269}
270
e56b3bc7
LT
271/*
272 * Special-case data structure for "single bit set only" constant CPU masks.
273 *
274 * We pre-generate all the 64 (or 32) possible bit positions, with enough
275 * padding to the left and the right, and return the constant pointer
276 * appropriately offset.
277 */
278extern const unsigned long
279 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
280
281static inline const cpumask_t *get_cpu_mask(unsigned int cpu)
282{
283 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
284 p -= cpu / BITS_PER_LONG;
285 return (const cpumask_t *)p;
286}
287
288/*
289 * In cases where we take the address of the cpumask immediately,
290 * gcc optimizes it out (it's a constant) and there's no huge stack
291 * variable created:
292 */
3dd730f2 293#define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
1da177e4 294
1da177e4
LT
295
296#define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
297
298#if NR_CPUS <= BITS_PER_LONG
299
300#define CPU_MASK_ALL \
301(cpumask_t) { { \
302 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
303} }
304
321a8e9d
MT
305#define CPU_MASK_ALL_PTR (&CPU_MASK_ALL)
306
1da177e4
LT
307#else
308
309#define CPU_MASK_ALL \
310(cpumask_t) { { \
311 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
312 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
313} }
314
321a8e9d
MT
315/* cpu_mask_all is in init/main.c */
316extern cpumask_t cpu_mask_all;
317#define CPU_MASK_ALL_PTR (&cpu_mask_all)
318
1da177e4
LT
319#endif
320
321#define CPU_MASK_NONE \
322(cpumask_t) { { \
323 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
324} }
325
326#define CPU_MASK_CPU0 \
327(cpumask_t) { { \
328 [0] = 1UL \
329} }
330
331#define cpus_addr(src) ((src).bits)
332
77586c2b
MT
333#if NR_CPUS > BITS_PER_LONG
334#define CPUMASK_ALLOC(m) struct m *m = kmalloc(sizeof(*m), GFP_KERNEL)
335#define CPUMASK_FREE(m) kfree(m)
336#else
80422d34 337#define CPUMASK_ALLOC(m) struct m _m, *m = &_m
77586c2b
MT
338#define CPUMASK_FREE(m)
339#endif
80422d34 340#define CPUMASK_PTR(v, m) cpumask_t *v = &(m->v)
77586c2b 341
fb5eeeee
PJ
342#define cpu_remap(oldbit, old, new) \
343 __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
344static inline int __cpu_remap(int oldbit,
345 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
346{
347 return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
348}
349
350#define cpus_remap(dst, src, old, new) \
351 __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
352static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
353 const cpumask_t *oldp, const cpumask_t *newp, int nbits)
354{
355 bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
356}
357
7ea931c9
PJ
358#define cpus_onto(dst, orig, relmap) \
359 __cpus_onto(&(dst), &(orig), &(relmap), NR_CPUS)
360static inline void __cpus_onto(cpumask_t *dstp, const cpumask_t *origp,
361 const cpumask_t *relmapp, int nbits)
362{
363 bitmap_onto(dstp->bits, origp->bits, relmapp->bits, nbits);
364}
365
366#define cpus_fold(dst, orig, sz) \
367 __cpus_fold(&(dst), &(orig), sz, NR_CPUS)
368static inline void __cpus_fold(cpumask_t *dstp, const cpumask_t *origp,
369 int sz, int nbits)
370{
371 bitmap_fold(dstp->bits, origp->bits, sz, nbits);
372}
373
41df0d61
MT
374#if NR_CPUS == 1
375
376#define nr_cpu_ids 1
377#define first_cpu(src) ({ (void)(src); 0; })
378#define next_cpu(n, src) ({ (void)(src); 1; })
379#define any_online_cpu(mask) 0
380#define for_each_cpu_mask(cpu, mask) \
381 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
382
383#else /* NR_CPUS > 1 */
384
385extern int nr_cpu_ids;
386int __first_cpu(const cpumask_t *srcp);
387int __next_cpu(int n, const cpumask_t *srcp);
388int __any_online_cpu(const cpumask_t *mask);
389
390#define first_cpu(src) __first_cpu(&(src))
391#define next_cpu(n, src) __next_cpu((n), &(src))
392#define any_online_cpu(mask) __any_online_cpu(&(mask))
7baac8b9
AH
393#define for_each_cpu_mask(cpu, mask) \
394 for ((cpu) = -1; \
395 (cpu) = next_cpu((cpu), (mask)), \
396 (cpu) < NR_CPUS; )
41df0d61
MT
397#endif
398
399#if NR_CPUS <= 64
400
401#define next_cpu_nr(n, src) next_cpu(n, src)
402#define cpus_weight_nr(cpumask) cpus_weight(cpumask)
403#define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
404
405#else /* NR_CPUS > 64 */
406
407int __next_cpu_nr(int n, const cpumask_t *srcp);
408#define next_cpu_nr(n, src) __next_cpu_nr((n), &(src))
409#define cpus_weight_nr(cpumask) __cpus_weight(&(cpumask), nr_cpu_ids)
7baac8b9
AH
410#define for_each_cpu_mask_nr(cpu, mask) \
411 for ((cpu) = -1; \
412 (cpu) = next_cpu_nr((cpu), (mask)), \
413 (cpu) < nr_cpu_ids; )
41df0d61
MT
414
415#endif /* NR_CPUS > 64 */
1da177e4
LT
416
417/*
418 * The following particular system cpumasks and operations manage
e761b772 419 * possible, present, active and online cpus. Each of them is a fixed size
1da177e4
LT
420 * bitmap of size NR_CPUS.
421 *
422 * #ifdef CONFIG_HOTPLUG_CPU
7a8ef1cb 423 * cpu_possible_map - has bit 'cpu' set iff cpu is populatable
1da177e4
LT
424 * cpu_present_map - has bit 'cpu' set iff cpu is populated
425 * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
e761b772 426 * cpu_active_map - has bit 'cpu' set iff cpu available to migration
1da177e4
LT
427 * #else
428 * cpu_possible_map - has bit 'cpu' set iff cpu is populated
429 * cpu_present_map - copy of cpu_possible_map
430 * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
431 * #endif
432 *
433 * In either case, NR_CPUS is fixed at compile time, as the static
434 * size of these bitmaps. The cpu_possible_map is fixed at boot
435 * time, as the set of CPU id's that it is possible might ever
436 * be plugged in at anytime during the life of that system boot.
437 * The cpu_present_map is dynamic(*), representing which CPUs
438 * are currently plugged in. And cpu_online_map is the dynamic
439 * subset of cpu_present_map, indicating those CPUs available
440 * for scheduling.
441 *
442 * If HOTPLUG is enabled, then cpu_possible_map is forced to have
443 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
444 * ACPI reports present at boot.
445 *
446 * If HOTPLUG is enabled, then cpu_present_map varies dynamically,
447 * depending on what ACPI reports as currently plugged in, otherwise
448 * cpu_present_map is just a copy of cpu_possible_map.
449 *
450 * (*) Well, cpu_present_map is dynamic in the hotplug case. If not
451 * hotplug, it's a copy of cpu_possible_map, hence fixed at boot.
452 *
453 * Subtleties:
454 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
455 * assumption that their single CPU is online. The UP
456 * cpu_{online,possible,present}_maps are placebos. Changing them
457 * will have no useful affect on the following num_*_cpus()
458 * and cpu_*() macros in the UP case. This ugliness is a UP
459 * optimization - don't waste any instructions or memory references
460 * asking if you're online or how many CPUs there are if there is
461 * only one CPU.
462 * 2) Most SMP arch's #define some of these maps to be some
463 * other map specific to that arch. Therefore, the following
464 * must be #define macros, not inlines. To see why, examine
465 * the assembly code produced by the following. Note that
466 * set1() writes phys_x_map, but set2() writes x_map:
467 * int x_map, phys_x_map;
468 * #define set1(a) x_map = a
469 * inline void set2(int a) { x_map = a; }
470 * #define x_map phys_x_map
471 * main(){ set1(3); set2(5); }
472 */
473
474extern cpumask_t cpu_possible_map;
475extern cpumask_t cpu_online_map;
476extern cpumask_t cpu_present_map;
e761b772 477extern cpumask_t cpu_active_map;
1da177e4
LT
478
479#if NR_CPUS > 1
41df0d61
MT
480#define num_online_cpus() cpus_weight_nr(cpu_online_map)
481#define num_possible_cpus() cpus_weight_nr(cpu_possible_map)
482#define num_present_cpus() cpus_weight_nr(cpu_present_map)
1da177e4
LT
483#define cpu_online(cpu) cpu_isset((cpu), cpu_online_map)
484#define cpu_possible(cpu) cpu_isset((cpu), cpu_possible_map)
485#define cpu_present(cpu) cpu_isset((cpu), cpu_present_map)
e761b772 486#define cpu_active(cpu) cpu_isset((cpu), cpu_active_map)
1da177e4
LT
487#else
488#define num_online_cpus() 1
489#define num_possible_cpus() 1
490#define num_present_cpus() 1
491#define cpu_online(cpu) ((cpu) == 0)
492#define cpu_possible(cpu) ((cpu) == 0)
493#define cpu_present(cpu) ((cpu) == 0)
e761b772 494#define cpu_active(cpu) ((cpu) == 0)
1da177e4
LT
495#endif
496
a263898f
IM
497#define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
498
41df0d61
MT
499#define for_each_possible_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_possible_map)
500#define for_each_online_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_online_map)
501#define for_each_present_cpu(cpu) for_each_cpu_mask_nr((cpu), cpu_present_map)
1da177e4 502
2d3854a3
RR
503/* These are the new versions of the cpumask operators: passed by pointer.
504 * The older versions will be implemented in terms of these, then deleted. */
505#define cpumask_bits(maskp) ((maskp)->bits)
506
507#if NR_CPUS <= BITS_PER_LONG
508#define CPU_BITS_ALL \
509{ \
510 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
511}
512
513/* This produces more efficient code. */
514#define nr_cpumask_bits NR_CPUS
515
516#else /* NR_CPUS > BITS_PER_LONG */
517
518#define CPU_BITS_ALL \
519{ \
520 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
521 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
522}
523
524#define nr_cpumask_bits nr_cpu_ids
525#endif /* NR_CPUS > BITS_PER_LONG */
526
527/* verify cpu argument to cpumask_* operators */
528static inline unsigned int cpumask_check(unsigned int cpu)
529{
530#ifdef CONFIG_DEBUG_PER_CPU_MAPS
531 WARN_ON_ONCE(cpu >= nr_cpumask_bits);
532#endif /* CONFIG_DEBUG_PER_CPU_MAPS */
533 return cpu;
534}
535
536#if NR_CPUS == 1
984f2f37
RR
537/* Uniprocessor. Assume all masks are "1". */
538static inline unsigned int cpumask_first(const struct cpumask *srcp)
539{
540 return 0;
541}
542
543/* Valid inputs for n are -1 and 0. */
544static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
545{
546 return n+1;
547}
548
549static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
550{
551 return n+1;
552}
553
554static inline unsigned int cpumask_next_and(int n,
555 const struct cpumask *srcp,
556 const struct cpumask *andp)
557{
558 return n+1;
559}
560
561/* cpu must be a valid cpu, ie 0, so there's no other choice. */
562static inline unsigned int cpumask_any_but(const struct cpumask *mask,
563 unsigned int cpu)
564{
565 return 1;
566}
2d3854a3
RR
567
568#define for_each_cpu(cpu, mask) \
569 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
570#define for_each_cpu_and(cpu, mask, and) \
571 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
572#else
573/**
574 * cpumask_first - get the first cpu in a cpumask
575 * @srcp: the cpumask pointer
576 *
577 * Returns >= nr_cpu_ids if no cpus set.
578 */
579static inline unsigned int cpumask_first(const struct cpumask *srcp)
580{
581 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
582}
583
584/**
585 * cpumask_next - get the next cpu in a cpumask
586 * @n: the cpu prior to the place to search (ie. return will be > @n)
587 * @srcp: the cpumask pointer
588 *
589 * Returns >= nr_cpu_ids if no further cpus set.
590 */
591static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
592{
593 /* -1 is a legal arg here. */
594 if (n != -1)
595 cpumask_check(n);
596 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
597}
598
599/**
600 * cpumask_next_zero - get the next unset cpu in a cpumask
601 * @n: the cpu prior to the place to search (ie. return will be > @n)
602 * @srcp: the cpumask pointer
603 *
604 * Returns >= nr_cpu_ids if no further cpus unset.
605 */
606static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
607{
608 /* -1 is a legal arg here. */
609 if (n != -1)
610 cpumask_check(n);
611 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
612}
613
614int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
615int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
616
984f2f37
RR
617/**
618 * for_each_cpu - iterate over every cpu in a mask
619 * @cpu: the (optionally unsigned) integer iterator
620 * @mask: the cpumask pointer
621 *
622 * After the loop, cpu is >= nr_cpu_ids.
623 */
2d3854a3
RR
624#define for_each_cpu(cpu, mask) \
625 for ((cpu) = -1; \
626 (cpu) = cpumask_next((cpu), (mask)), \
627 (cpu) < nr_cpu_ids;)
984f2f37
RR
628
629/**
630 * for_each_cpu_and - iterate over every cpu in both masks
631 * @cpu: the (optionally unsigned) integer iterator
632 * @mask: the first cpumask pointer
633 * @and: the second cpumask pointer
634 *
635 * This saves a temporary CPU mask in many places. It is equivalent to:
636 * struct cpumask tmp;
637 * cpumask_and(&tmp, &mask, &and);
638 * for_each_cpu(cpu, &tmp)
639 * ...
640 *
641 * After the loop, cpu is >= nr_cpu_ids.
642 */
2d3854a3
RR
643#define for_each_cpu_and(cpu, mask, and) \
644 for ((cpu) = -1; \
645 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
646 (cpu) < nr_cpu_ids;)
647#endif /* SMP */
648
649#define CPU_BITS_NONE \
650{ \
651 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
652}
653
654#define CPU_BITS_CPU0 \
655{ \
656 [0] = 1UL \
657}
658
659/**
660 * cpumask_set_cpu - set a cpu in a cpumask
661 * @cpu: cpu number (< nr_cpu_ids)
662 * @dstp: the cpumask pointer
663 */
664static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
665{
666 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
667}
668
669/**
670 * cpumask_clear_cpu - clear a cpu in a cpumask
671 * @cpu: cpu number (< nr_cpu_ids)
672 * @dstp: the cpumask pointer
673 */
674static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
675{
676 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
677}
678
679/**
680 * cpumask_test_cpu - test for a cpu in a cpumask
681 * @cpu: cpu number (< nr_cpu_ids)
682 * @cpumask: the cpumask pointer
683 *
684 * No static inline type checking - see Subtlety (1) above.
685 */
686#define cpumask_test_cpu(cpu, cpumask) \
687 test_bit(cpumask_check(cpu), (cpumask)->bits)
688
689/**
690 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
691 * @cpu: cpu number (< nr_cpu_ids)
692 * @cpumask: the cpumask pointer
693 *
694 * test_and_set_bit wrapper for cpumasks.
695 */
696static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
697{
698 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
699}
700
701/**
702 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
703 * @dstp: the cpumask pointer
704 */
705static inline void cpumask_setall(struct cpumask *dstp)
706{
707 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
708}
709
710/**
711 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
712 * @dstp: the cpumask pointer
713 */
714static inline void cpumask_clear(struct cpumask *dstp)
715{
716 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
717}
718
719/**
720 * cpumask_and - *dstp = *src1p & *src2p
721 * @dstp: the cpumask result
722 * @src1p: the first input
723 * @src2p: the second input
724 */
725static inline void cpumask_and(struct cpumask *dstp,
726 const struct cpumask *src1p,
727 const struct cpumask *src2p)
728{
729 bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
730 cpumask_bits(src2p), nr_cpumask_bits);
731}
732
733/**
734 * cpumask_or - *dstp = *src1p | *src2p
735 * @dstp: the cpumask result
736 * @src1p: the first input
737 * @src2p: the second input
738 */
739static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
740 const struct cpumask *src2p)
741{
742 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
743 cpumask_bits(src2p), nr_cpumask_bits);
744}
745
746/**
747 * cpumask_xor - *dstp = *src1p ^ *src2p
748 * @dstp: the cpumask result
749 * @src1p: the first input
750 * @src2p: the second input
751 */
752static inline void cpumask_xor(struct cpumask *dstp,
753 const struct cpumask *src1p,
754 const struct cpumask *src2p)
755{
756 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
757 cpumask_bits(src2p), nr_cpumask_bits);
758}
759
760/**
761 * cpumask_andnot - *dstp = *src1p & ~*src2p
762 * @dstp: the cpumask result
763 * @src1p: the first input
764 * @src2p: the second input
765 */
766static inline void cpumask_andnot(struct cpumask *dstp,
767 const struct cpumask *src1p,
768 const struct cpumask *src2p)
769{
770 bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
771 cpumask_bits(src2p), nr_cpumask_bits);
772}
773
774/**
775 * cpumask_complement - *dstp = ~*srcp
776 * @dstp: the cpumask result
777 * @srcp: the input to invert
778 */
779static inline void cpumask_complement(struct cpumask *dstp,
780 const struct cpumask *srcp)
781{
782 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
783 nr_cpumask_bits);
784}
785
786/**
787 * cpumask_equal - *src1p == *src2p
788 * @src1p: the first input
789 * @src2p: the second input
790 */
791static inline bool cpumask_equal(const struct cpumask *src1p,
792 const struct cpumask *src2p)
793{
794 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
795 nr_cpumask_bits);
796}
797
798/**
799 * cpumask_intersects - (*src1p & *src2p) != 0
800 * @src1p: the first input
801 * @src2p: the second input
802 */
803static inline bool cpumask_intersects(const struct cpumask *src1p,
804 const struct cpumask *src2p)
805{
806 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
807 nr_cpumask_bits);
808}
809
810/**
811 * cpumask_subset - (*src1p & ~*src2p) == 0
812 * @src1p: the first input
813 * @src2p: the second input
814 */
815static inline int cpumask_subset(const struct cpumask *src1p,
816 const struct cpumask *src2p)
817{
818 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
819 nr_cpumask_bits);
820}
821
822/**
823 * cpumask_empty - *srcp == 0
824 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
825 */
826static inline bool cpumask_empty(const struct cpumask *srcp)
827{
828 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
829}
830
831/**
832 * cpumask_full - *srcp == 0xFFFFFFFF...
833 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
834 */
835static inline bool cpumask_full(const struct cpumask *srcp)
836{
837 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
838}
839
840/**
841 * cpumask_weight - Count of bits in *srcp
842 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
843 */
844static inline unsigned int cpumask_weight(const struct cpumask *srcp)
845{
846 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
847}
848
849/**
850 * cpumask_shift_right - *dstp = *srcp >> n
851 * @dstp: the cpumask result
852 * @srcp: the input to shift
853 * @n: the number of bits to shift by
854 */
855static inline void cpumask_shift_right(struct cpumask *dstp,
856 const struct cpumask *srcp, int n)
857{
858 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
859 nr_cpumask_bits);
860}
861
862/**
863 * cpumask_shift_left - *dstp = *srcp << n
864 * @dstp: the cpumask result
865 * @srcp: the input to shift
866 * @n: the number of bits to shift by
867 */
868static inline void cpumask_shift_left(struct cpumask *dstp,
869 const struct cpumask *srcp, int n)
870{
871 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
872 nr_cpumask_bits);
873}
874
875/**
876 * cpumask_copy - *dstp = *srcp
877 * @dstp: the result
878 * @srcp: the input cpumask
879 */
880static inline void cpumask_copy(struct cpumask *dstp,
881 const struct cpumask *srcp)
882{
883 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
884}
885
886/**
887 * cpumask_any - pick a "random" cpu from *srcp
888 * @srcp: the input cpumask
889 *
890 * Returns >= nr_cpu_ids if no cpus set.
891 */
892#define cpumask_any(srcp) cpumask_first(srcp)
893
894/**
895 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
896 * @src1p: the first input
897 * @src2p: the second input
898 *
899 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
900 */
901#define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
902
903/**
904 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
905 * @mask1: the first input cpumask
906 * @mask2: the second input cpumask
907 *
908 * Returns >= nr_cpu_ids if no cpus set.
909 */
910#define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
911
cd83e42c
RR
912/**
913 * cpumask_of - the cpumask containing just a given cpu
914 * @cpu: the cpu (<= nr_cpu_ids)
915 */
916#define cpumask_of(cpu) (get_cpu_mask(cpu))
917
29c0177e
RR
918/**
919 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
920 * @buf: the buffer to sprintf into
921 * @len: the length of the buffer
922 * @srcp: the cpumask to print
923 *
924 * If len is zero, returns zero. Otherwise returns the length of the
925 * (nul-terminated) @buf string.
926 */
927static inline int cpumask_scnprintf(char *buf, int len,
928 const struct cpumask *srcp)
929{
930 return bitmap_scnprintf(buf, len, srcp->bits, nr_cpumask_bits);
931}
932
933/**
934 * cpumask_parse_user - extract a cpumask from a user string
935 * @buf: the buffer to extract from
936 * @len: the length of the buffer
937 * @dstp: the cpumask to set.
938 *
939 * Returns -errno, or 0 for success.
940 */
941static inline int cpumask_parse_user(const char __user *buf, int len,
942 struct cpumask *dstp)
943{
944 return bitmap_parse_user(buf, len, dstp->bits, nr_cpumask_bits);
945}
946
947/**
948 * cpulist_scnprintf - print a cpumask into a string as comma-separated list
949 * @buf: the buffer to sprintf into
950 * @len: the length of the buffer
951 * @srcp: the cpumask to print
952 *
953 * If len is zero, returns zero. Otherwise returns the length of the
954 * (nul-terminated) @buf string.
955 */
956static inline int cpulist_scnprintf(char *buf, int len,
957 const struct cpumask *srcp)
958{
959 return bitmap_scnlistprintf(buf, len, srcp->bits, nr_cpumask_bits);
960}
961
962/**
963 * cpulist_parse_user - extract a cpumask from a user string of ranges
964 * @buf: the buffer to extract from
965 * @len: the length of the buffer
966 * @dstp: the cpumask to set.
967 *
968 * Returns -errno, or 0 for success.
969 */
970static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
971{
972 return bitmap_parselist(buf, dstp->bits, nr_cpumask_bits);
973}
974
2d3854a3
RR
975/**
976 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
977 * @bitmap: the bitmap
978 *
979 * There are a few places where cpumask_var_t isn't appropriate and
980 * static cpumasks must be used (eg. very early boot), yet we don't
981 * expose the definition of 'struct cpumask'.
982 *
983 * This does the conversion, and can be used as a constant initializer.
984 */
985#define to_cpumask(bitmap) \
986 ((struct cpumask *)(1 ? (bitmap) \
987 : (void *)sizeof(__check_is_bitmap(bitmap))))
988
989static inline int __check_is_bitmap(const unsigned long *bitmap)
990{
991 return 1;
992}
993
994/**
995 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
996 *
997 * This will eventually be a runtime variable, depending on nr_cpu_ids.
998 */
999static inline size_t cpumask_size(void)
1000{
1001 /* FIXME: Once all cpumask assignments are eliminated, this
1002 * can be nr_cpumask_bits */
1003 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
1004}
1005
1006/*
1007 * cpumask_var_t: struct cpumask for stack usage.
1008 *
1009 * Oh, the wicked games we play! In order to make kernel coding a
1010 * little more difficult, we typedef cpumask_var_t to an array or a
1011 * pointer: doing &mask on an array is a noop, so it still works.
1012 *
1013 * ie.
1014 * cpumask_var_t tmpmask;
1015 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
1016 * return -ENOMEM;
1017 *
1018 * ... use 'tmpmask' like a normal struct cpumask * ...
1019 *
1020 * free_cpumask_var(tmpmask);
1021 */
1022#ifdef CONFIG_CPUMASK_OFFSTACK
1023typedef struct cpumask *cpumask_var_t;
1024
1025bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
1026void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
1027void free_cpumask_var(cpumask_var_t mask);
cd83e42c 1028void free_bootmem_cpumask_var(cpumask_var_t mask);
2d3854a3
RR
1029
1030#else
1031typedef struct cpumask cpumask_var_t[1];
1032
1033static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1034{
1035 return true;
1036}
1037
1038static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1039{
1040}
1041
1042static inline void free_cpumask_var(cpumask_var_t mask)
1043{
1044}
cd83e42c
RR
1045
1046static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
1047{
1048}
2d3854a3
RR
1049#endif /* CONFIG_CPUMASK_OFFSTACK */
1050
1051/* The pointer versions of the maps, these will become the primary versions. */
1052#define cpu_possible_mask ((const struct cpumask *)&cpu_possible_map)
1053#define cpu_online_mask ((const struct cpumask *)&cpu_online_map)
1054#define cpu_present_mask ((const struct cpumask *)&cpu_present_map)
1055#define cpu_active_mask ((const struct cpumask *)&cpu_active_map)
1056
1057/* It's common to want to use cpu_all_mask in struct member initializers,
1058 * so it has to refer to an address rather than a pointer. */
1059extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
1060#define cpu_all_mask to_cpumask(cpu_all_bits)
1061
1062/* First bits of cpu_bit_bitmap are in fact unset. */
1063#define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
1064
1065/* Wrappers for arch boot code to manipulate normally-constant masks */
1066static inline void set_cpu_possible(unsigned int cpu, bool possible)
1067{
1068 if (possible)
1069 cpumask_set_cpu(cpu, &cpu_possible_map);
1070 else
1071 cpumask_clear_cpu(cpu, &cpu_possible_map);
1072}
1073
1074static inline void set_cpu_present(unsigned int cpu, bool present)
1075{
1076 if (present)
1077 cpumask_set_cpu(cpu, &cpu_present_map);
1078 else
1079 cpumask_clear_cpu(cpu, &cpu_present_map);
1080}
1081
1082static inline void set_cpu_online(unsigned int cpu, bool online)
1083{
1084 if (online)
1085 cpumask_set_cpu(cpu, &cpu_online_map);
1086 else
1087 cpumask_clear_cpu(cpu, &cpu_online_map);
1088}
1089
1090static inline void set_cpu_active(unsigned int cpu, bool active)
1091{
1092 if (active)
1093 cpumask_set_cpu(cpu, &cpu_active_map);
1094 else
1095 cpumask_clear_cpu(cpu, &cpu_active_map);
1096}
1097
1098static inline void init_cpu_present(const struct cpumask *src)
1099{
1100 cpumask_copy(&cpu_present_map, src);
1101}
1102
1103static inline void init_cpu_possible(const struct cpumask *src)
1104{
1105 cpumask_copy(&cpu_possible_map, src);
1106}
1107
1108static inline void init_cpu_online(const struct cpumask *src)
1109{
1110 cpumask_copy(&cpu_online_map, src);
1111}
1da177e4 1112#endif /* __LINUX_CPUMASK_H */