1 | /*
|
---|
2 | * Copyright (c) 2012 Adam Hraska
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup kernel_generic
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief CPU mask manipulation functions.
|
---|
36 | */
|
---|
37 | #include <assert.h>
|
---|
38 | #include <cpu/cpu_mask.h>
|
---|
39 | #include <cpu.h>
|
---|
40 | #include <config.h>
|
---|
41 |
|
---|
42 | static const size_t word_size = sizeof(unsigned int);
|
---|
43 | static const size_t word_bit_cnt = 8 * sizeof(unsigned int);
|
---|
44 |
|
---|
45 | /** Returns the size of cpu_mask_t for the detected number of cpus in bytes. */
|
---|
46 | size_t cpu_mask_size(void)
|
---|
47 | {
|
---|
48 | size_t word_cnt = (config.cpu_count + word_bit_cnt - 1) / word_bit_cnt;
|
---|
49 | return word_cnt * word_size;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /** Add first cpu_cnt cpus to the mask, ie sets the first cpu_cnt bits. */
|
---|
53 | static void cpu_mask_count(cpu_mask_t *cpus, size_t cpu_cnt)
|
---|
54 | {
|
---|
55 | assert(NULL != cpus);
|
---|
56 | assert(cpu_cnt <= config.cpu_count);
|
---|
57 |
|
---|
58 | for (size_t active_word = 0;
|
---|
59 | (active_word + 1) * word_bit_cnt <= cpu_cnt;
|
---|
60 | ++active_word) {
|
---|
61 | /* Set all bits in the cell/word. */
|
---|
62 | cpus->mask[active_word] = -1;
|
---|
63 | }
|
---|
64 |
|
---|
65 | size_t remaining_bits = (cpu_cnt % word_bit_cnt);
|
---|
66 | if (0 < remaining_bits) {
|
---|
67 | /* Set lower remaining_bits of the last word. */
|
---|
68 | cpus->mask[cpu_cnt / word_bit_cnt] = (1 << remaining_bits) - 1;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Sets bits corresponding to the active cpus, ie the first
|
---|
73 | * config.cpu_active cpus.
|
---|
74 | */
|
---|
75 | void cpu_mask_active(cpu_mask_t *cpus)
|
---|
76 | {
|
---|
77 | cpu_mask_none(cpus);
|
---|
78 | cpu_mask_count(cpus, config.cpu_active);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Sets bits for all cpus of the mask. */
|
---|
82 | void cpu_mask_all(cpu_mask_t *cpus)
|
---|
83 | {
|
---|
84 | cpu_mask_count(cpus, config.cpu_count);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Resets/removes all bits. */
|
---|
88 | void cpu_mask_none(cpu_mask_t *cpus)
|
---|
89 | {
|
---|
90 | assert(cpus);
|
---|
91 |
|
---|
92 | size_t word_cnt = cpu_mask_size() / word_size;
|
---|
93 |
|
---|
94 | for (size_t word = 0; word < word_cnt; ++word) {
|
---|
95 | cpus->mask[word] = 0;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Sets the bit corresponding to cpu_id to true. */
|
---|
100 | void cpu_mask_set(cpu_mask_t *cpus, unsigned int cpu_id)
|
---|
101 | {
|
---|
102 | size_t word = cpu_id / word_bit_cnt;
|
---|
103 | size_t word_pos = cpu_id % word_bit_cnt;
|
---|
104 |
|
---|
105 | cpus->mask[word] |= (1U << word_pos);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Resets the bit corresponding to cpu_id to false. */
|
---|
109 | void cpu_mask_reset(cpu_mask_t *cpus, unsigned int cpu_id)
|
---|
110 | {
|
---|
111 | size_t word = cpu_id / word_bit_cnt;
|
---|
112 | size_t word_pos = cpu_id % word_bit_cnt;
|
---|
113 |
|
---|
114 | cpus->mask[word] &= ~(1U << word_pos);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Returns true if the bit corresponding to cpu_id is set. */
|
---|
118 | bool cpu_mask_is_set(cpu_mask_t *cpus, unsigned int cpu_id)
|
---|
119 | {
|
---|
120 | size_t word = cpu_id / word_bit_cnt;
|
---|
121 | size_t word_pos = cpu_id % word_bit_cnt;
|
---|
122 |
|
---|
123 | return 0 != (cpus->mask[word] & (1U << word_pos));
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Returns true if no bits are set. */
|
---|
127 | bool cpu_mask_is_none(cpu_mask_t *cpus)
|
---|
128 | {
|
---|
129 | size_t word_cnt = cpu_mask_size() / word_size;
|
---|
130 |
|
---|
131 | for (size_t word = 0; word < word_cnt; ++word) {
|
---|
132 | if (cpus->mask[word])
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** @}
|
---|
140 | */
|
---|