1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreProcessorMask |
---|
7 | * |
---|
8 | * @brief This header file provides the interfaces of the |
---|
9 | * @ref RTEMSScoreProcessorMask. |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * Copyright (c) 2016, 2017 embedded brains GmbH. All rights reserved. |
---|
14 | * |
---|
15 | * embedded brains GmbH |
---|
16 | * Dornierstr. 4 |
---|
17 | * 82178 Puchheim |
---|
18 | * Germany |
---|
19 | * <rtems@embedded-brains.de> |
---|
20 | * |
---|
21 | * Redistribution and use in source and binary forms, with or without |
---|
22 | * modification, are permitted provided that the following conditions |
---|
23 | * are met: |
---|
24 | * 1. Redistributions of source code must retain the above copyright |
---|
25 | * notice, this list of conditions and the following disclaimer. |
---|
26 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
27 | * notice, this list of conditions and the following disclaimer in the |
---|
28 | * documentation and/or other materials provided with the distribution. |
---|
29 | * |
---|
30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
31 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
40 | * POSSIBILITY OF SUCH DAMAGE. |
---|
41 | */ |
---|
42 | |
---|
43 | #ifndef _RTEMS_SCORE_PROCESSORMASK_H |
---|
44 | #define _RTEMS_SCORE_PROCESSORMASK_H |
---|
45 | |
---|
46 | #include <rtems/score/cpu.h> |
---|
47 | |
---|
48 | #include <sys/cpuset.h> |
---|
49 | |
---|
50 | #include <strings.h> |
---|
51 | |
---|
52 | #ifdef __cplusplus |
---|
53 | extern "C" { |
---|
54 | #endif /* __cplusplus */ |
---|
55 | |
---|
56 | /** |
---|
57 | * @defgroup RTEMSScoreProcessorMask Processor Mask |
---|
58 | * |
---|
59 | * @ingroup RTEMSScore |
---|
60 | * |
---|
61 | * @brief This group contains the implementation to support processor masks. |
---|
62 | * |
---|
63 | * The processor mask provides a bit map large enough to provide one bit for |
---|
64 | * each processor in the system. It is a fixed size internal data type |
---|
65 | * provided for efficiency in addition to the API level cpu_set_t. |
---|
66 | * |
---|
67 | * @{ |
---|
68 | */ |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief A bit map which is large enough to provide one bit for each processor |
---|
72 | * in the system. |
---|
73 | */ |
---|
74 | typedef BITSET_DEFINE( Processor_mask, CPU_MAXIMUM_PROCESSORS ) Processor_mask; |
---|
75 | |
---|
76 | /** |
---|
77 | * @brief Sets the bits of the mask to zero, also considers CPU_MAXIMUM_PROCESSORS. |
---|
78 | * |
---|
79 | * @param[out] mask The mask to set to zero. |
---|
80 | */ |
---|
81 | RTEMS_INLINE_ROUTINE void _Processor_mask_Zero( Processor_mask *mask ) |
---|
82 | { |
---|
83 | BIT_ZERO( CPU_MAXIMUM_PROCESSORS, mask ); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * @brief Checks if the mask is zero, also considers CPU_MAXIMUM_PROCESSORS. |
---|
88 | * |
---|
89 | * @param mask The mask to check whether is is zero |
---|
90 | * |
---|
91 | * @retval true The mask is zero. |
---|
92 | * @retval false The mask is not zero. |
---|
93 | */ |
---|
94 | RTEMS_INLINE_ROUTINE bool _Processor_mask_Is_zero( const Processor_mask *mask ) |
---|
95 | { |
---|
96 | return BIT_EMPTY( CPU_MAXIMUM_PROCESSORS, mask ); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * @brief Fills the mask, also considers CPU_MAXIMUM_PROCESSORS. |
---|
101 | * |
---|
102 | * @param[out] mask The mask to fill |
---|
103 | */ |
---|
104 | RTEMS_INLINE_ROUTINE void _Processor_mask_Fill( Processor_mask *mask ) |
---|
105 | { |
---|
106 | BIT_FILL( CPU_MAXIMUM_PROCESSORS, mask ); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * @brief Copies the mask to another mask, also considers CPU_MAXIMUM_PROCESSORS. |
---|
111 | * |
---|
112 | * @param[out] dst The mask to copy @a src to. |
---|
113 | * @param src The mask to copy to @a dst. |
---|
114 | */ |
---|
115 | RTEMS_INLINE_ROUTINE void _Processor_mask_Assign( |
---|
116 | Processor_mask *dst, const Processor_mask *src |
---|
117 | ) |
---|
118 | { |
---|
119 | BIT_COPY( CPU_MAXIMUM_PROCESSORS, src, dst ); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * @brief Sets the specified index bit of the mask. |
---|
124 | * |
---|
125 | * @param[out] mask The mask to set the bit of. |
---|
126 | * @param index The index of the bit that shall be set. |
---|
127 | */ |
---|
128 | RTEMS_INLINE_ROUTINE void _Processor_mask_Set( |
---|
129 | Processor_mask *mask, |
---|
130 | uint32_t index |
---|
131 | ) |
---|
132 | { |
---|
133 | BIT_SET( CPU_MAXIMUM_PROCESSORS, index, mask ); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * @brief Clears the specified index bit of the mask. |
---|
138 | * |
---|
139 | * @param[out] mask The mask to clear the bit of. |
---|
140 | * @param index The index of the bit that shall be cleared. |
---|
141 | */ |
---|
142 | RTEMS_INLINE_ROUTINE void _Processor_mask_Clear( |
---|
143 | Processor_mask *mask, |
---|
144 | uint32_t index |
---|
145 | ) |
---|
146 | { |
---|
147 | BIT_CLR( CPU_MAXIMUM_PROCESSORS, index, mask ); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * @brief Checks if the specified index bit of the mask is set. |
---|
152 | * |
---|
153 | * @param mask The mask to check if the specified bit is set. |
---|
154 | * @param index The index of the bit that is checked. |
---|
155 | * |
---|
156 | * @retval true The specified index bit is set. |
---|
157 | * @retval false The specified index bit is not set. |
---|
158 | */ |
---|
159 | RTEMS_INLINE_ROUTINE bool _Processor_mask_Is_set( |
---|
160 | const Processor_mask *mask, |
---|
161 | uint32_t index |
---|
162 | ) |
---|
163 | { |
---|
164 | return BIT_ISSET( CPU_MAXIMUM_PROCESSORS, index, mask ); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * @brief Checks if the processor sets a and b are equal. |
---|
169 | * |
---|
170 | * @param a The first processor set. |
---|
171 | * @param b The seconde processor set. |
---|
172 | * |
---|
173 | * @retval true The processor sets a and b are equal. |
---|
174 | * @retval false The processor sets a and b are not equal. |
---|
175 | */ |
---|
176 | RTEMS_INLINE_ROUTINE bool _Processor_mask_Is_equal( |
---|
177 | const Processor_mask *a, |
---|
178 | const Processor_mask *b |
---|
179 | ) |
---|
180 | { |
---|
181 | return !BIT_CMP( CPU_MAXIMUM_PROCESSORS, a, b ); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * @brief Checks if the intersection of the processor sets a and b is |
---|
186 | * non-empty. |
---|
187 | * |
---|
188 | * @param a The first processor set. |
---|
189 | * @param b The second processor set. |
---|
190 | * |
---|
191 | * @retval true The intersection of the processor sets a and b is non-empty. |
---|
192 | * @retval false The intersection of the processor sets a and b is empty. |
---|
193 | */ |
---|
194 | RTEMS_INLINE_ROUTINE bool _Processor_mask_Has_overlap( |
---|
195 | const Processor_mask *a, |
---|
196 | const Processor_mask *b |
---|
197 | ) |
---|
198 | { |
---|
199 | return BIT_OVERLAP( CPU_MAXIMUM_PROCESSORS, a, b ); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * @brief Checks if the processor set small is a subset of processor set |
---|
204 | * big. |
---|
205 | * |
---|
206 | * @param big The bigger processor set. |
---|
207 | * @param small The smaller processor set. |
---|
208 | * |
---|
209 | * @retval true @a small is a subset of @a big. |
---|
210 | * @retval false @a small is not a subset of @a big. |
---|
211 | */ |
---|
212 | RTEMS_INLINE_ROUTINE bool _Processor_mask_Is_subset( |
---|
213 | const Processor_mask *big, |
---|
214 | const Processor_mask *small |
---|
215 | ) |
---|
216 | { |
---|
217 | return BIT_SUBSET( CPU_MAXIMUM_PROCESSORS, big, small ); |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * @brief Performs a bitwise a = b & c. |
---|
222 | * |
---|
223 | * @param[out] a The processor mask that is set by this operation. |
---|
224 | * @param b The first parameter of the AND-operation. |
---|
225 | * @param c The second parameter of the AND-operation. |
---|
226 | */ |
---|
227 | RTEMS_INLINE_ROUTINE void _Processor_mask_And( |
---|
228 | Processor_mask *a, |
---|
229 | const Processor_mask *b, |
---|
230 | const Processor_mask *c |
---|
231 | ) |
---|
232 | { |
---|
233 | BIT_AND2( CPU_MAXIMUM_PROCESSORS, a, b, c ); |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * @brief Performs a bitwise a = b & ~c. |
---|
238 | * |
---|
239 | * @param[out] a The processor mask that is set by this operation. |
---|
240 | * @param b The first parameter of the operation. |
---|
241 | * @param c The second parameter of the operation. |
---|
242 | */ |
---|
243 | RTEMS_INLINE_ROUTINE void _Processor_mask_Nand( |
---|
244 | Processor_mask *a, |
---|
245 | const Processor_mask *b, |
---|
246 | const Processor_mask *c |
---|
247 | ) |
---|
248 | { |
---|
249 | BIT_NAND2( CPU_MAXIMUM_PROCESSORS, a, b, c ); |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * @brief Performs a bitwise a = b | c. |
---|
254 | * |
---|
255 | * @param[out] a The processor mask that is set by this operation. |
---|
256 | * @param b The first parameter of the OR-operation. |
---|
257 | * @param c The second parameter of the OR-operation. |
---|
258 | */ |
---|
259 | RTEMS_INLINE_ROUTINE void _Processor_mask_Or( |
---|
260 | Processor_mask *a, |
---|
261 | const Processor_mask *b, |
---|
262 | const Processor_mask *c |
---|
263 | ) |
---|
264 | { |
---|
265 | BIT_OR2( CPU_MAXIMUM_PROCESSORS, a, b, c ); |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | * @brief Performs a bitwise a = b ^ c. |
---|
270 | * |
---|
271 | * @param[out] a The processor mask that is set by this operation. |
---|
272 | * @param b The first parameter of the XOR-operation. |
---|
273 | * @param c The second parameter of the XOR-operation. |
---|
274 | */ |
---|
275 | RTEMS_INLINE_ROUTINE void _Processor_mask_Xor( |
---|
276 | Processor_mask *a, |
---|
277 | const Processor_mask *b, |
---|
278 | const Processor_mask *c |
---|
279 | ) |
---|
280 | { |
---|
281 | BIT_XOR2( CPU_MAXIMUM_PROCESSORS, a, b, c ); |
---|
282 | } |
---|
283 | |
---|
284 | /** |
---|
285 | * @brief Gets the number of set bits in the processor mask. |
---|
286 | * |
---|
287 | * @param a The processor mask of which the set bits are counted. |
---|
288 | * |
---|
289 | * @return The number of set bits in @a a. |
---|
290 | */ |
---|
291 | RTEMS_INLINE_ROUTINE uint32_t _Processor_mask_Count( const Processor_mask *a ) |
---|
292 | { |
---|
293 | return (uint32_t) BIT_COUNT( CPU_MAXIMUM_PROCESSORS, a ); |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * @brief Finds the last set of the processor mask. |
---|
298 | * |
---|
299 | * @param a The processor mask wo find the last set of. |
---|
300 | * |
---|
301 | * @return The last set of @a a. |
---|
302 | */ |
---|
303 | RTEMS_INLINE_ROUTINE uint32_t _Processor_mask_Find_last_set( const Processor_mask *a ) |
---|
304 | { |
---|
305 | return (uint32_t) BIT_FLS( CPU_MAXIMUM_PROCESSORS, a ); |
---|
306 | } |
---|
307 | |
---|
308 | /** |
---|
309 | * @brief Returns the subset of 32 processors containing the specified index as |
---|
310 | * an unsigned 32-bit integer. |
---|
311 | * |
---|
312 | * @param mask The processor mask. |
---|
313 | * @param index The specified index. |
---|
314 | * |
---|
315 | * @return The subset containing the specified index as an unsigned 32-bit integer. |
---|
316 | */ |
---|
317 | RTEMS_INLINE_ROUTINE uint32_t _Processor_mask_To_uint32_t( |
---|
318 | const Processor_mask *mask, |
---|
319 | uint32_t index |
---|
320 | ) |
---|
321 | { |
---|
322 | long bits = mask->__bits[ __bitset_words( index ) ]; |
---|
323 | |
---|
324 | return (uint32_t) (bits >> (32 * (index % _BITSET_BITS) / 32)); |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * @brief Creates a processor set from an unsigned 32-bit integer relative to |
---|
329 | * the specified index. |
---|
330 | * |
---|
331 | * @param[out] mask The mask that is created. |
---|
332 | * @param bits The bits for creating the mask. |
---|
333 | * @param index The index to which the mask is relative. |
---|
334 | */ |
---|
335 | RTEMS_INLINE_ROUTINE void _Processor_mask_From_uint32_t( |
---|
336 | Processor_mask *mask, |
---|
337 | uint32_t bits, |
---|
338 | uint32_t index |
---|
339 | ) |
---|
340 | { |
---|
341 | _Processor_mask_Zero( mask ); |
---|
342 | mask->__bits[ __bitset_words( index ) ] = ((long) bits) << (32 * (index % _BITSET_BITS) / 32); |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * @brief Creates a processor set from the specified index. |
---|
347 | * |
---|
348 | * @param[out] The mask that is created. |
---|
349 | * @param index The specified index. |
---|
350 | */ |
---|
351 | RTEMS_INLINE_ROUTINE void _Processor_mask_From_index( |
---|
352 | Processor_mask *mask, |
---|
353 | uint32_t index |
---|
354 | ) |
---|
355 | { |
---|
356 | BIT_SETOF( CPU_MAXIMUM_PROCESSORS, (int) index, mask ); |
---|
357 | } |
---|
358 | |
---|
359 | typedef enum { |
---|
360 | PROCESSOR_MASK_COPY_LOSSLESS, |
---|
361 | PROCESSOR_MASK_COPY_PARTIAL_LOSS, |
---|
362 | PROCESSOR_MASK_COPY_COMPLETE_LOSS, |
---|
363 | PROCESSOR_MASK_COPY_INVALID_SIZE |
---|
364 | } Processor_mask_Copy_status; |
---|
365 | |
---|
366 | /** |
---|
367 | * @brief Checks if the copy status guarantees at most partial loss. |
---|
368 | * |
---|
369 | * @param status The copy status to check. |
---|
370 | * |
---|
371 | * @retval true At most partial loss can be guaranteed. |
---|
372 | * @retval false The status indicates more than partial loss. |
---|
373 | */ |
---|
374 | RTEMS_INLINE_ROUTINE bool _Processor_mask_Is_at_most_partial_loss( |
---|
375 | Processor_mask_Copy_status status |
---|
376 | ) |
---|
377 | { |
---|
378 | return (unsigned int) status <= PROCESSOR_MASK_COPY_PARTIAL_LOSS; |
---|
379 | } |
---|
380 | |
---|
381 | /** |
---|
382 | * @brief Copies one mask to another. |
---|
383 | * |
---|
384 | * @param[out] dst The destination of the copy operation. |
---|
385 | * @param dst_size The size of @a dst. |
---|
386 | * @param src The source of the copy operation. |
---|
387 | * @param src_size The size of @a src. |
---|
388 | * |
---|
389 | * @retval PROCESSOR_MASK_COPY_LOSSLESS It is guaranteed that the copy |
---|
390 | * operation is lossless. |
---|
391 | * @retval PROCESSOR_MASK_COPY_PARTIAL_LOSS Partial loss happened due |
---|
392 | * to the sizes of @a src and @a dst. |
---|
393 | * @retval PROCESSOR_MASK_COPY_COMPLETE_LOSS Complete loss happened due |
---|
394 | * to the sizes of @a src and @a dst. |
---|
395 | * @retval PROCESSOR_MASK_COPY_INVALID_SIZE One of the arguments sizes |
---|
396 | * is invalid (bigger than the size of a long). |
---|
397 | */ |
---|
398 | Processor_mask_Copy_status _Processor_mask_Copy( |
---|
399 | long *dst, |
---|
400 | size_t dst_size, |
---|
401 | const long *src, |
---|
402 | size_t src_size |
---|
403 | ); |
---|
404 | |
---|
405 | /** |
---|
406 | * @brief Copies one mask to another. |
---|
407 | * |
---|
408 | * @param src The source for the copy operation. |
---|
409 | * @param dst_size The size of @a dst. |
---|
410 | * @param[out] dst The destination for the copy operation. |
---|
411 | * |
---|
412 | * @retval PROCESSOR_MASK_COPY_LOSSLESS It is guaranteed that the copy |
---|
413 | * operation is lossless. |
---|
414 | * @retval PROCESSOR_MASK_COPY_PARTIAL_LOSS Partial loss happened due |
---|
415 | * to the sizes of @a src and @a dst. |
---|
416 | * @retval PROCESSOR_MASK_COPY_COMPLETE_LOSS Complete loss happened due |
---|
417 | * to the sizes of @a src and @a dst. |
---|
418 | * @retval PROCESSOR_MASK_COPY_INVALID_SIZE One of the arguments sizes |
---|
419 | * is invalid (bigger than the size of a long). |
---|
420 | */ |
---|
421 | RTEMS_INLINE_ROUTINE Processor_mask_Copy_status _Processor_mask_To_cpu_set_t( |
---|
422 | const Processor_mask *src, |
---|
423 | size_t dst_size, |
---|
424 | cpu_set_t *dst |
---|
425 | ) |
---|
426 | { |
---|
427 | return _Processor_mask_Copy( |
---|
428 | &dst->__bits[ 0 ], |
---|
429 | dst_size, |
---|
430 | &src->__bits[ 0 ], |
---|
431 | sizeof( *src ) |
---|
432 | ); |
---|
433 | } |
---|
434 | |
---|
435 | /** |
---|
436 | * @brief Copies one mask to another. |
---|
437 | * |
---|
438 | * @param src The source for the copy operation. |
---|
439 | * @param src_size The size of @a src. |
---|
440 | * @param[out] dst The destination for the copy operation. |
---|
441 | * |
---|
442 | * @retval PROCESSOR_MASK_COPY_LOSSLESS It is guaranteed that the copy |
---|
443 | * operation is lossless. |
---|
444 | * @retval PROCESSOR_MASK_COPY_PARTIAL_LOSS Partial loss happened due |
---|
445 | * to the sizes of @a src and @a dst. |
---|
446 | * @retval PROCESSOR_MASK_COPY_COMPLETE_LOSS Complete loss happened due |
---|
447 | * to the sizes of @a src and @a dst. |
---|
448 | * @retval PROCESSOR_MASK_COPY_INVALID_SIZE One of the arguments sizes |
---|
449 | * is invalid (bigger than the size of a long). |
---|
450 | */ |
---|
451 | RTEMS_INLINE_ROUTINE Processor_mask_Copy_status _Processor_mask_From_cpu_set_t( |
---|
452 | Processor_mask *dst, |
---|
453 | size_t src_size, |
---|
454 | const cpu_set_t *src |
---|
455 | ) |
---|
456 | { |
---|
457 | return _Processor_mask_Copy( |
---|
458 | &dst->__bits[ 0 ], |
---|
459 | sizeof( *dst ), |
---|
460 | &src->__bits[ 0 ], |
---|
461 | src_size |
---|
462 | ); |
---|
463 | } |
---|
464 | |
---|
465 | extern const Processor_mask _Processor_mask_The_one_and_only; |
---|
466 | |
---|
467 | /** @} */ |
---|
468 | |
---|
469 | #ifdef __cplusplus |
---|
470 | } |
---|
471 | #endif /* __cplusplus */ |
---|
472 | |
---|
473 | #endif /* _RTEMS_SCORE_PROCESSORMASK_H */ |
---|