cpumask.h
上传用户:szlgq88
上传日期:2009-04-28
资源大小:48287k
文件大小:14k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. #ifndef __LINUX_CPUMASK_H
  2. #define __LINUX_CPUMASK_H
  3. /*
  4.  * Cpumasks provide a bitmap suitable for representing the
  5.  * set of CPU's in a system, one bit position per CPU number.
  6.  *
  7.  * See detailed comments in the file linux/bitmap.h describing the
  8.  * data type on which these cpumasks are based.
  9.  *
  10.  * For details of cpumask_scnprintf() and cpumask_parse(),
  11.  * see bitmap_scnprintf() and bitmap_parse() in lib/bitmap.c.
  12.  * For details of cpulist_scnprintf() and cpulist_parse(), see
  13.  * bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c.
  14.  *
  15.  * The available cpumask operations are:
  16.  *
  17.  * void cpu_set(cpu, mask) turn on bit 'cpu' in mask
  18.  * void cpu_clear(cpu, mask) turn off bit 'cpu' in mask
  19.  * void cpus_setall(mask) set all bits
  20.  * void cpus_clear(mask) clear all bits
  21.  * int cpu_isset(cpu, mask) true iff bit 'cpu' set in mask
  22.  * int cpu_test_and_set(cpu, mask) test and set bit 'cpu' in mask
  23.  *
  24.  * void cpus_and(dst, src1, src2) dst = src1 & src2  [intersection]
  25.  * void cpus_or(dst, src1, src2) dst = src1 | src2  [union]
  26.  * void cpus_xor(dst, src1, src2) dst = src1 ^ src2
  27.  * void cpus_andnot(dst, src1, src2) dst = src1 & ~src2
  28.  * void cpus_complement(dst, src) dst = ~src
  29.  *
  30.  * int cpus_equal(mask1, mask2) Does mask1 == mask2?
  31.  * int cpus_intersects(mask1, mask2) Do mask1 and mask2 intersect?
  32.  * int cpus_subset(mask1, mask2) Is mask1 a subset of mask2?
  33.  * int cpus_empty(mask) Is mask empty (no bits sets)?
  34.  * int cpus_full(mask) Is mask full (all bits sets)?
  35.  * int cpus_weight(mask) Hamming weigh - number of set bits
  36.  *
  37.  * void cpus_shift_right(dst, src, n) Shift right
  38.  * void cpus_shift_left(dst, src, n) Shift left
  39.  *
  40.  * int first_cpu(mask) Number lowest set bit, or NR_CPUS
  41.  * int next_cpu(cpu, mask) Next cpu past 'cpu', or NR_CPUS
  42.  *
  43.  * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
  44.  * CPU_MASK_ALL Initializer - all bits set
  45.  * CPU_MASK_NONE Initializer - no bits set
  46.  * unsigned long *cpus_addr(mask) Array of unsigned long's in mask
  47.  *
  48.  * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
  49.  * int cpumask_parse(ubuf, ulen, mask) Parse ascii string as cpumask
  50.  * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
  51.  * int cpulist_parse(buf, map) Parse ascii string as cpulist
  52.  *
  53.  * for_each_cpu_mask(cpu, mask) for-loop cpu over mask
  54.  *
  55.  * int num_online_cpus() Number of online CPUs
  56.  * int num_possible_cpus() Number of all possible CPUs
  57.  * int num_present_cpus() Number of present CPUs
  58.  *
  59.  * int cpu_online(cpu) Is some cpu online?
  60.  * int cpu_possible(cpu) Is some cpu possible?
  61.  * int cpu_present(cpu) Is some cpu present (can schedule)?
  62.  *
  63.  * int any_online_cpu(mask) First online cpu in mask
  64.  *
  65.  * for_each_cpu(cpu) for-loop cpu over cpu_possible_map
  66.  * for_each_online_cpu(cpu) for-loop cpu over cpu_online_map
  67.  * for_each_present_cpu(cpu) for-loop cpu over cpu_present_map
  68.  *
  69.  * Subtlety:
  70.  * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
  71.  *    to generate slightly worse code.  Note for example the additional
  72.  *    40 lines of assembly code compiling the "for each possible cpu"
  73.  *    loops buried in the disk_stat_read() macros calls when compiling
  74.  *    drivers/block/genhd.c (arch i386, CONFIG_SMP=y).  So use a simple
  75.  *    one-line #define for cpu_isset(), instead of wrapping an inline
  76.  *    inside a macro, the way we do the other calls.
  77.  */
  78. #include <linux/kernel.h>
  79. #include <linux/threads.h>
  80. #include <linux/bitmap.h>
  81. #include <asm/bug.h>
  82. typedef struct { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
  83. extern cpumask_t _unused_cpumask_arg_;
  84. #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
  85. static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
  86. {
  87. set_bit(cpu, dstp->bits);
  88. }
  89. #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
  90. static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
  91. {
  92. clear_bit(cpu, dstp->bits);
  93. }
  94. #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
  95. static inline void __cpus_setall(cpumask_t *dstp, int nbits)
  96. {
  97. bitmap_fill(dstp->bits, nbits);
  98. }
  99. #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
  100. static inline void __cpus_clear(cpumask_t *dstp, int nbits)
  101. {
  102. bitmap_zero(dstp->bits, nbits);
  103. }
  104. /* No static inline type checking - see Subtlety (1) above. */
  105. #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
  106. #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
  107. static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
  108. {
  109. return test_and_set_bit(cpu, addr->bits);
  110. }
  111. #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
  112. static inline void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
  113. const cpumask_t *src2p, int nbits)
  114. {
  115. bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
  116. }
  117. #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
  118. static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
  119. const cpumask_t *src2p, int nbits)
  120. {
  121. bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
  122. }
  123. #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
  124. static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
  125. const cpumask_t *src2p, int nbits)
  126. {
  127. bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
  128. }
  129. #define cpus_andnot(dst, src1, src2) 
  130. __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
  131. static inline void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
  132. const cpumask_t *src2p, int nbits)
  133. {
  134. bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
  135. }
  136. #define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
  137. static inline void __cpus_complement(cpumask_t *dstp,
  138. const cpumask_t *srcp, int nbits)
  139. {
  140. bitmap_complement(dstp->bits, srcp->bits, nbits);
  141. }
  142. #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
  143. static inline int __cpus_equal(const cpumask_t *src1p,
  144. const cpumask_t *src2p, int nbits)
  145. {
  146. return bitmap_equal(src1p->bits, src2p->bits, nbits);
  147. }
  148. #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
  149. static inline int __cpus_intersects(const cpumask_t *src1p,
  150. const cpumask_t *src2p, int nbits)
  151. {
  152. return bitmap_intersects(src1p->bits, src2p->bits, nbits);
  153. }
  154. #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
  155. static inline int __cpus_subset(const cpumask_t *src1p,
  156. const cpumask_t *src2p, int nbits)
  157. {
  158. return bitmap_subset(src1p->bits, src2p->bits, nbits);
  159. }
  160. #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
  161. static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
  162. {
  163. return bitmap_empty(srcp->bits, nbits);
  164. }
  165. #define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
  166. static inline int __cpus_full(const cpumask_t *srcp, int nbits)
  167. {
  168. return bitmap_full(srcp->bits, nbits);
  169. }
  170. #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
  171. static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
  172. {
  173. return bitmap_weight(srcp->bits, nbits);
  174. }
  175. #define cpus_shift_right(dst, src, n) 
  176. __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
  177. static inline void __cpus_shift_right(cpumask_t *dstp,
  178. const cpumask_t *srcp, int n, int nbits)
  179. {
  180. bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
  181. }
  182. #define cpus_shift_left(dst, src, n) 
  183. __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
  184. static inline void __cpus_shift_left(cpumask_t *dstp,
  185. const cpumask_t *srcp, int n, int nbits)
  186. {
  187. bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
  188. }
  189. #define first_cpu(src) __first_cpu(&(src), NR_CPUS)
  190. static inline int __first_cpu(const cpumask_t *srcp, int nbits)
  191. {
  192. return min_t(int, nbits, find_first_bit(srcp->bits, nbits));
  193. }
  194. #define next_cpu(n, src) __next_cpu((n), &(src), NR_CPUS)
  195. static inline int __next_cpu(int n, const cpumask_t *srcp, int nbits)
  196. {
  197. return min_t(int, nbits, find_next_bit(srcp->bits, nbits, n+1));
  198. }
  199. #define cpumask_of_cpu(cpu)
  200. ({
  201. typeof(_unused_cpumask_arg_) m;
  202. if (sizeof(m) == sizeof(unsigned long)) {
  203. m.bits[0] = 1UL<<(cpu);
  204. } else {
  205. cpus_clear(m);
  206. cpu_set((cpu), m);
  207. }
  208. m;
  209. })
  210. #define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
  211. #if NR_CPUS <= BITS_PER_LONG
  212. #define CPU_MASK_ALL
  213. (cpumask_t) { {
  214. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD
  215. } }
  216. #else
  217. #define CPU_MASK_ALL
  218. (cpumask_t) { {
  219. [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,
  220. [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD
  221. } }
  222. #endif
  223. #define CPU_MASK_NONE
  224. (cpumask_t) { {
  225. [0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL
  226. } }
  227. #define CPU_MASK_CPU0
  228. (cpumask_t) { {
  229. [0] =  1UL
  230. } }
  231. #define cpus_addr(src) ((src).bits)
  232. #define cpumask_scnprintf(buf, len, src) 
  233. __cpumask_scnprintf((buf), (len), &(src), NR_CPUS)
  234. static inline int __cpumask_scnprintf(char *buf, int len,
  235. const cpumask_t *srcp, int nbits)
  236. {
  237. return bitmap_scnprintf(buf, len, srcp->bits, nbits);
  238. }
  239. #define cpumask_parse(ubuf, ulen, dst) 
  240. __cpumask_parse((ubuf), (ulen), &(dst), NR_CPUS)
  241. static inline int __cpumask_parse(const char __user *buf, int len,
  242. cpumask_t *dstp, int nbits)
  243. {
  244. return bitmap_parse(buf, len, dstp->bits, nbits);
  245. }
  246. #define cpulist_scnprintf(buf, len, src) 
  247. __cpulist_scnprintf((buf), (len), &(src), NR_CPUS)
  248. static inline int __cpulist_scnprintf(char *buf, int len,
  249. const cpumask_t *srcp, int nbits)
  250. {
  251. return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
  252. }
  253. #define cpulist_parse(buf, dst) __cpulist_parse((buf), &(dst), NR_CPUS)
  254. static inline int __cpulist_parse(const char *buf, cpumask_t *dstp, int nbits)
  255. {
  256. return bitmap_parselist(buf, dstp->bits, nbits);
  257. }
  258. #if NR_CPUS > 1
  259. #define for_each_cpu_mask(cpu, mask)
  260. for ((cpu) = first_cpu(mask);
  261. (cpu) < NR_CPUS;
  262. (cpu) = next_cpu((cpu), (mask)))
  263. #else /* NR_CPUS == 1 */
  264. #define for_each_cpu_mask(cpu, mask) for ((cpu) = 0; (cpu) < 1; (cpu)++)
  265. #endif /* NR_CPUS */
  266. /*
  267.  * The following particular system cpumasks and operations manage
  268.  * possible, present and online cpus.  Each of them is a fixed size
  269.  * bitmap of size NR_CPUS.
  270.  *
  271.  *  #ifdef CONFIG_HOTPLUG_CPU
  272.  *     cpu_possible_map - all NR_CPUS bits set
  273.  *     cpu_present_map  - has bit 'cpu' set iff cpu is populated
  274.  *     cpu_online_map   - has bit 'cpu' set iff cpu available to scheduler
  275.  *  #else
  276.  *     cpu_possible_map - has bit 'cpu' set iff cpu is populated
  277.  *     cpu_present_map  - copy of cpu_possible_map
  278.  *     cpu_online_map   - has bit 'cpu' set iff cpu available to scheduler
  279.  *  #endif
  280.  *
  281.  *  In either case, NR_CPUS is fixed at compile time, as the static
  282.  *  size of these bitmaps.  The cpu_possible_map is fixed at boot
  283.  *  time, as the set of CPU id's that it is possible might ever
  284.  *  be plugged in at anytime during the life of that system boot.
  285.  *  The cpu_present_map is dynamic(*), representing which CPUs
  286.  *  are currently plugged in.  And cpu_online_map is the dynamic
  287.  *  subset of cpu_present_map, indicating those CPUs available
  288.  *  for scheduling.
  289.  *
  290.  *  If HOTPLUG is enabled, then cpu_possible_map is forced to have
  291.  *  all NR_CPUS bits set, otherwise it is just the set of CPUs that
  292.  *  ACPI reports present at boot.
  293.  *
  294.  *  If HOTPLUG is enabled, then cpu_present_map varies dynamically,
  295.  *  depending on what ACPI reports as currently plugged in, otherwise
  296.  *  cpu_present_map is just a copy of cpu_possible_map.
  297.  *
  298.  *  (*) Well, cpu_present_map is dynamic in the hotplug case.  If not
  299.  *      hotplug, it's a copy of cpu_possible_map, hence fixed at boot.
  300.  *
  301.  * Subtleties:
  302.  * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
  303.  *    assumption that their single CPU is online.  The UP
  304.  *    cpu_{online,possible,present}_maps are placebos.  Changing them
  305.  *    will have no useful affect on the following num_*_cpus()
  306.  *    and cpu_*() macros in the UP case.  This ugliness is a UP
  307.  *    optimization - don't waste any instructions or memory references
  308.  *    asking if you're online or how many CPUs there are if there is
  309.  *    only one CPU.
  310.  * 2) Most SMP arch's #define some of these maps to be some
  311.  *    other map specific to that arch.  Therefore, the following
  312.  *    must be #define macros, not inlines.  To see why, examine
  313.  *    the assembly code produced by the following.  Note that
  314.  *    set1() writes phys_x_map, but set2() writes x_map:
  315.  *        int x_map, phys_x_map;
  316.  *        #define set1(a) x_map = a
  317.  *        inline void set2(int a) { x_map = a; }
  318.  *        #define x_map phys_x_map
  319.  *        main(){ set1(3); set2(5); }
  320.  */
  321. extern cpumask_t cpu_possible_map;
  322. extern cpumask_t cpu_online_map;
  323. extern cpumask_t cpu_present_map;
  324. #if NR_CPUS > 1
  325. #define num_online_cpus() cpus_weight(cpu_online_map)
  326. #define num_possible_cpus() cpus_weight(cpu_possible_map)
  327. #define num_present_cpus() cpus_weight(cpu_present_map)
  328. #define cpu_online(cpu) cpu_isset((cpu), cpu_online_map)
  329. #define cpu_possible(cpu) cpu_isset((cpu), cpu_possible_map)
  330. #define cpu_present(cpu) cpu_isset((cpu), cpu_present_map)
  331. #else
  332. #define num_online_cpus() 1
  333. #define num_possible_cpus() 1
  334. #define num_present_cpus() 1
  335. #define cpu_online(cpu) ((cpu) == 0)
  336. #define cpu_possible(cpu) ((cpu) == 0)
  337. #define cpu_present(cpu) ((cpu) == 0)
  338. #endif
  339. #define any_online_cpu(mask)
  340. ({
  341. int cpu;
  342. for_each_cpu_mask(cpu, (mask))
  343. if (cpu_online(cpu))
  344. break;
  345. cpu;
  346. })
  347. #define for_each_cpu(cpu)   for_each_cpu_mask((cpu), cpu_possible_map)
  348. #define for_each_online_cpu(cpu)  for_each_cpu_mask((cpu), cpu_online_map)
  349. #define for_each_present_cpu(cpu) for_each_cpu_mask((cpu), cpu_present_map)
  350. /* Find the highest possible smp_processor_id() */
  351. #define highest_possible_processor_id() 
  352. ({ 
  353. unsigned int cpu, highest = 0; 
  354. for_each_cpu_mask(cpu, cpu_possible_map) 
  355. highest = cpu; 
  356. highest; 
  357. })
  358. #endif /* __LINUX_CPUMASK_H */