cpumask.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #ifndef __LINUX_CPUMASK_H
  2. #define __LINUX_CPUMASK_H
  3. #define NR_CPUS 4096
  4. /*
  5. * Cpumasks provide a bitmap suitable for representing the
  6. * set of CPU's in a system, one bit position per CPU number.
  7. *
  8. * See detailed comments in the file linux/bitmap.h describing the
  9. * data type on which these cpumasks are based.
  10. *
  11. * For details of cpumask_scnprintf() and cpumask_parse_user(),
  12. * see bitmap_scnprintf() and bitmap_parse_user() in lib/bitmap.c.
  13. * For details of cpulist_scnprintf() and cpulist_parse(), see
  14. * bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c.
  15. * For details of cpu_remap(), see bitmap_bitremap in lib/bitmap.c
  16. * For details of cpus_remap(), see bitmap_remap in lib/bitmap.c.
  17. *
  18. * The available cpumask operations are:
  19. *
  20. * void cpu_set(cpu, mask) turn on bit 'cpu' in mask
  21. * void cpu_clear(cpu, mask) turn off bit 'cpu' in mask
  22. * void cpus_setall(mask) set all bits
  23. * void cpus_clear(mask) clear all bits
  24. * int cpu_isset(cpu, mask) true iff bit 'cpu' set in mask
  25. * int cpu_test_and_set(cpu, mask) test and set bit 'cpu' in mask
  26. *
  27. * void cpus_and(dst, src1, src2) dst = src1 & src2 [intersection]
  28. * void cpus_or(dst, src1, src2) dst = src1 | src2 [union]
  29. * void cpus_xor(dst, src1, src2) dst = src1 ^ src2
  30. * void cpus_andnot(dst, src1, src2) dst = src1 & ~src2
  31. * void cpus_complement(dst, src) dst = ~src
  32. *
  33. * int cpus_equal(mask1, mask2) Does mask1 == mask2?
  34. * int cpus_intersects(mask1, mask2) Do mask1 and mask2 intersect?
  35. * int cpus_subset(mask1, mask2) Is mask1 a subset of mask2?
  36. * int cpus_empty(mask) Is mask empty (no bits sets)?
  37. * int cpus_full(mask) Is mask full (all bits sets)?
  38. * int cpus_weight(mask) Hamming weigh - number of set bits
  39. *
  40. * void cpus_shift_right(dst, src, n) Shift right
  41. * void cpus_shift_left(dst, src, n) Shift left
  42. *
  43. * int first_cpu(mask) Number lowest set bit, or NR_CPUS
  44. * int next_cpu(cpu, mask) Next cpu past 'cpu', or NR_CPUS
  45. *
  46. * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
  47. * CPU_MASK_ALL Initializer - all bits set
  48. * CPU_MASK_NONE Initializer - no bits set
  49. * unsigned long *cpus_addr(mask) Array of unsigned long's in mask
  50. *
  51. * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
  52. * int cpumask_parse_user(ubuf, ulen, mask) Parse ascii string as cpumask
  53. * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
  54. * int cpulist_parse(buf, map) Parse ascii string as cpulist
  55. * int cpu_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
  56. * int cpus_remap(dst, src, old, new) *dst = map(old, new)(src)
  57. *
  58. * for_each_cpu_mask(cpu, mask) for-loop cpu over mask
  59. *
  60. * int num_online_cpus() Number of online CPUs
  61. * int num_possible_cpus() Number of all possible CPUs
  62. * int num_present_cpus() Number of present CPUs
  63. *
  64. * int cpu_online(cpu) Is some cpu online?
  65. * int cpu_possible(cpu) Is some cpu possible?
  66. * int cpu_present(cpu) Is some cpu present (can schedule)?
  67. *
  68. * int any_online_cpu(mask) First online cpu in mask
  69. *
  70. * for_each_possible_cpu(cpu) for-loop cpu over cpu_possible_map
  71. * for_each_online_cpu(cpu) for-loop cpu over cpu_online_map
  72. * for_each_present_cpu(cpu) for-loop cpu over cpu_present_map
  73. *
  74. * Subtlety:
  75. * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
  76. * to generate slightly worse code. Note for example the additional
  77. * 40 lines of assembly code compiling the "for each possible cpu"
  78. * loops buried in the disk_stat_read() macros calls when compiling
  79. * drivers/block/genhd.c (arch i386, CONFIG_SMP=y). So use a simple
  80. * one-line #define for cpu_isset(), instead of wrapping an inline
  81. * inside a macro, the way we do the other calls.
  82. */
  83. #include <stdlib.h>
  84. #include "bitmap.h"
  85. #include "bit_array.h"
  86. #include "hexio.h"
  87. typedef struct {
  88. BIT_ARRAY* bits;
  89. } cpumask_t;
  90. extern cpumask_t _unused_cpumask_arg_;
  91. #define cpus_init(dst) ((dst).bits = bit_array_create(NR_CPUS))
  92. #define cpus_free(dst) (bit_array_free((dst).bits))
  93. #define cpus_copy(dst, src) bit_array_copy_all((dst).bits, (src).bits)
  94. #define cpu_set(cpu, dst) bit_array_set_bit((dst).bits, cpu)
  95. #define cpu_clear(cpu, dst) bit_array_clear_bit((dst).bits, cpu)
  96. #define cpus_setall(dst) bit_array_set_all((dst).bits)
  97. #define cpus_clear(dst) bit_array_clear_all((dst).bits)
  98. #define cpu_isset(cpu, cpumask) bit_array_get_bit((cpumask).bits, cpu)
  99. #define cpus_and(dst, src1, src2) bit_array_and((dst).bits, (src1).bits, (src2).bits)
  100. #define cpus_or(dst, src1, src2) bit_array_or((dst).bits, (src1).bits, (src2).bits)
  101. #define cpus_xor(dst, src1, src2) bit_array_xor((dst).bits, (src1).bits, (src2).bits)
  102. #define cpus_complement(dst, src) bit_array_not((dst).bits, (src).bits)
  103. #define cpus_equal(src1, src2) (bit_array_cmp((src1).bits, (src2).bits) == 0)
  104. #define cpus_empty(src) (bit_array_num_bits_set((src).bits) == 0)
  105. #define cpus_full(src) (bit_array_num_bits_cleared((src).bits) == 0)
  106. #define cpus_weight(cpumask) bit_array_num_bits_set((cpumask).bits)\
  107. #define cpus_shift_right(dst, n) bit_array_shift_right((dst).bits, n, 0)
  108. #define cpus_shift_left(dst, n) bit_array_shift_left((dst).bits, n, 0)
  109. static inline int __first_cpu(const cpumask_t srcp)
  110. {
  111. bit_index_t res = bit_array_length(srcp.bits);
  112. bit_array_find_first_set_bit(srcp.bits, &res);
  113. return res;
  114. }
  115. #define first_cpu(src) __first_cpu((src))
  116. int __next_cpu(int n, const cpumask_t *srcp);
  117. #define next_cpu(n, src) __next_cpu((n), (src))
  118. /*#define cpumask_of_cpu(cpu) \
  119. ({ \
  120. typeof(_unused_cpumask_arg_) m; \
  121. if (sizeof(m) == sizeof(unsigned long)) { \
  122. m.bits[0] = 1UL<<(cpu); \
  123. } else { \
  124. cpus_clear(m); \
  125. cpu_set((cpu), m); \
  126. } \
  127. m; \
  128. })
  129. #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
  130. #if 0
  131. #define CPU_MASK_ALL \
  132. (cpumask_t) { { \
  133. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  134. } }
  135. #else
  136. #define CPU_MASK_ALL \
  137. (cpumask_t) { { \
  138. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
  139. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
  140. } }
  141. #endif
  142. #define CPU_MASK_NONE \
  143. (cpumask_t) { { \
  144. [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
  145. } }
  146. #define CPU_MASK_CPU0 \
  147. (cpumask_t) { { \
  148. [0] = 1UL \
  149. } }
  150. #define cpus_addr(src) ((src).bits)
  151. */
  152. #define cpumask_scnprintf(buf, len, src) bitmask_displayhex((buf), (len), (src).bits)
  153. #define cpumask_parse_user(ubuf, ulen, dst) bitmask_parsehex((ubuf), (dst).bits)
  154. /*
  155. #define cpulist_scnprintf(buf, len, src) \
  156. __cpulist_scnprintf((buf), (len), &(src), NR_CPUS)
  157. static inline int __cpulist_scnprintf(char *buf, int len,
  158. const cpumask_t *srcp, int nbits)
  159. {
  160. return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
  161. }
  162. #define cpulist_parse(buf, dst) __cpulist_parse((buf), &(dst), NR_CPUS)
  163. static inline int __cpulist_parse(const char *buf, cpumask_t *dstp, int nbits)
  164. {
  165. return bitmap_parselist(buf, dstp->bits, nbits);
  166. }
  167. #define cpu_remap(oldbit, old, new) \
  168. __cpu_remap((oldbit), &(old), &(new), NR_CPUS)
  169. static inline int __cpu_remap(int oldbit,
  170. const cpumask_t *oldp, const cpumask_t *newp, int nbits)
  171. {
  172. return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
  173. }
  174. #define cpus_remap(dst, src, old, new) \
  175. __cpus_remap(&(dst), &(src), &(old), &(new), NR_CPUS)
  176. static inline void __cpus_remap(cpumask_t *dstp, const cpumask_t *srcp,
  177. const cpumask_t *oldp, const cpumask_t *newp, int nbits)
  178. {
  179. bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
  180. }
  181. */
  182. /*#if NR_CPUS > 1
  183. #define for_each_cpu_mask(cpu, mask) \
  184. for ((cpu) = first_cpu(mask); \
  185. (cpu) < NR_CPUS; \
  186. (cpu) = next_cpu((cpu), (mask)))
  187. #else *//* NR_CPUS == 1 */
  188. /*#define for_each_cpu_mask(cpu, mask) \
  189. for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
  190. #endif *//* NR_CPUS */
  191. /*
  192. * The following particular system cpumasks and operations manage
  193. * possible, present and online cpus. Each of them is a fixed size
  194. * bitmap of size NR_CPUS.
  195. *
  196. * #ifdef CONFIG_HOTPLUG_CPU
  197. * cpu_possible_map - has bit 'cpu' set iff cpu is populatable
  198. * cpu_present_map - has bit 'cpu' set iff cpu is populated
  199. * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
  200. * #else
  201. * cpu_possible_map - has bit 'cpu' set iff cpu is populated
  202. * cpu_present_map - copy of cpu_possible_map
  203. * cpu_online_map - has bit 'cpu' set iff cpu available to scheduler
  204. * #endif
  205. *
  206. * In either case, NR_CPUS is fixed at compile time, as the static
  207. * size of these bitmaps. The cpu_possible_map is fixed at boot
  208. * time, as the set of CPU id's that it is possible might ever
  209. * be plugged in at anytime during the life of that system boot.
  210. * The cpu_present_map is dynamic(*), representing which CPUs
  211. * are currently plugged in. And cpu_online_map is the dynamic
  212. * subset of cpu_present_map, indicating those CPUs available
  213. * for scheduling.
  214. *
  215. * If HOTPLUG is enabled, then cpu_possible_map is forced to have
  216. * all NR_CPUS bits set, otherwise it is just the set of CPUs that
  217. * ACPI reports present at boot.
  218. *
  219. * If HOTPLUG is enabled, then cpu_present_map varies dynamically,
  220. * depending on what ACPI reports as currently plugged in, otherwise
  221. * cpu_present_map is just a copy of cpu_possible_map.
  222. *
  223. * (*) Well, cpu_present_map is dynamic in the hotplug case. If not
  224. * hotplug, it's a copy of cpu_possible_map, hence fixed at boot.
  225. *
  226. * Subtleties:
  227. * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
  228. * assumption that their single CPU is online. The UP
  229. * cpu_{online,possible,present}_maps are placebos. Changing them
  230. * will have no useful affect on the following num_*_cpus()
  231. * and cpu_*() macros in the UP case. This ugliness is a UP
  232. * optimization - don't waste any instructions or memory references
  233. * asking if you're online or how many CPUs there are if there is
  234. * only one CPU.
  235. * 2) Most SMP arch's #define some of these maps to be some
  236. * other map specific to that arch. Therefore, the following
  237. * must be #define macros, not inlines. To see why, examine
  238. * the assembly code produced by the following. Note that
  239. * set1() writes phys_x_map, but set2() writes x_map:
  240. * int x_map, phys_x_map;
  241. * #define set1(a) x_map = a
  242. * inline void set2(int a) { x_map = a; }
  243. * #define x_map phys_x_map
  244. * main(){ set1(3); set2(5); }
  245. */
  246. /*
  247. extern cpumask_t cpu_possible_map;
  248. extern cpumask_t cpu_online_map;
  249. extern cpumask_t cpu_present_map;
  250. #if NR_CPUS > 1
  251. #define num_online_cpus() cpus_weight(cpu_online_map)
  252. #define num_possible_cpus() cpus_weight(cpu_possible_map)
  253. #define num_present_cpus() cpus_weight(cpu_present_map)
  254. #define cpu_online(cpu) cpu_isset((cpu), cpu_online_map)
  255. #define cpu_possible(cpu) cpu_isset((cpu), cpu_possible_map)
  256. #define cpu_present(cpu) cpu_isset((cpu), cpu_present_map)
  257. #else
  258. #define num_online_cpus() 1
  259. #define num_possible_cpus() 1
  260. #define num_present_cpus() 1
  261. #define cpu_online(cpu) ((cpu) == 0)
  262. #define cpu_possible(cpu) ((cpu) == 0)
  263. #define cpu_present(cpu) ((cpu) == 0)
  264. #endif
  265. int highest_possible_processor_id(void);
  266. #define any_online_cpu(mask) __any_online_cpu(&(mask))
  267. int __any_online_cpu(const cpumask_t *mask);
  268. #define for_each_possible_cpu(cpu) for_each_cpu_mask((cpu), cpu_possible_map)
  269. #define for_each_online_cpu(cpu) for_each_cpu_mask((cpu), cpu_online_map)
  270. #define for_each_present_cpu(cpu) for_each_cpu_mask((cpu), cpu_present_map)
  271. */
  272. #endif /* __LINUX_CPUMASK_H */