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