1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
49 * Module to support operations on bitmap of cores. Coremask can be used to
50 * select a specific core, a group of cores, or all available cores, for
51 * initialization and differentiation of roles within a single shared binary
54 * <hr>$Revision: 70030 $<hr>
59 #ifndef __CVMX_COREMASK_H__
60 #define __CVMX_COREMASK_H__
68 typedef uint64_t cvmx_coremask_holder_t; /* basic type to hold the
71 #define CVMX_COREMASK_HLDRSZ ((int)(sizeof(cvmx_coremask_holder_t) * 8))
74 #define CVMX_COREMASK_BMPSZ ((int)(CVMX_MAX_CORES / CVMX_COREMASK_HLDRSZ + 1))
78 * The macro pair implement a way to iterate active cores in the mask.
79 * @param fec_pcm points to the coremask.
80 * @param fec_ppid is the active core's id.
82 #define CVMX_COREMASK_FOR_EACH_CORE_BEGIN(fec_pcm, fec_ppid) \
86 for (fec_i = 0; fec_i < CVMX_COREMASK_BMPSZ; fec_i++) \
88 for (fec_j = 0; fec_j < CVMX_COREMASK_HLDRSZ; fec_j++) \
90 if (((cvmx_coremask_holder_t)1 << fec_j) & \
91 (fec_pcm)->coremask_bitmap[fec_i]) \
93 fec_ppid = fec_i * CVMX_COREMASK_HLDRSZ + fec_j;
96 #define CVMX_COREMASK_FOR_EACH_CORE_END \
102 struct cvmx_coremask {
104 * Big-endian. Array elems of larger indices represent cores of
105 * bigger ids. So do MSBs within a cvmx_coremask_holder_t. Ditto
106 * MSbs within a byte.
108 cvmx_coremask_holder_t coremask_bitmap[CVMX_COREMASK_BMPSZ];
112 * Is ``core'' set in the coremask?
114 * @param pcm is the pointer to the coremask.
116 * @return 1 if core is set and 0 if not.
118 static inline int cvmx_coremask_is_set_core(struct cvmx_coremask *pcm,
123 n = core % CVMX_COREMASK_HLDRSZ;
124 i = core / CVMX_COREMASK_HLDRSZ;
126 return (int)((pcm->coremask_bitmap[i] & (1ull << n)) != 0);
130 * Set ``core'' in the coremask.
132 * @param pcm is the pointer to the coremask.
136 static inline int cvmx_coremask_set_core(struct cvmx_coremask *pcm,
141 n = core % CVMX_COREMASK_HLDRSZ;
142 i = core / CVMX_COREMASK_HLDRSZ;
143 pcm->coremask_bitmap[i] |= (1ull << n);
149 * Clear ``core'' from the coremask.
151 * @param pcm is the pointer to the coremask.
155 static inline int cvmx_coremask_clear_core(struct cvmx_coremask *pcm,
160 n = core % CVMX_COREMASK_HLDRSZ;
161 i = core / CVMX_COREMASK_HLDRSZ;
162 pcm->coremask_bitmap[i] &= ~(1ull << n);
168 * Clear the coremask.
170 * @param pcm is the pointer to the coremask.
173 static inline int cvmx_coremask_clear_all(struct cvmx_coremask *pcm)
177 for (i = 0; i < CVMX_COREMASK_BMPSZ; i++)
178 pcm->coremask_bitmap[i] = 0;
184 * Is the current core the first in the coremask?
186 * @param pcm is the pointer to the coremask.
187 * @return 1 for yes and 0 for no.
189 static inline int cvmx_coremask_first_core_bmp(struct cvmx_coremask *pcm)
193 n = (int) cvmx_get_core_num();
194 for (i = 0; i < CVMX_COREMASK_BMPSZ; i++)
196 if (pcm->coremask_bitmap[i])
198 if (n == 0 && pcm->coremask_bitmap[i] & 1)
201 if (n >= CVMX_COREMASK_HLDRSZ)
204 return ((((1ull << n) - 1) & pcm->coremask_bitmap[i]) == 0);
207 n -= CVMX_COREMASK_HLDRSZ;
214 * Is the current core a member of the coremask?
216 * @param pcm is the pointer to the coremask.
217 * @return 1 for yes and 0 for no.
219 static inline int cvmx_coremask_is_member_bmp(struct cvmx_coremask *pcm)
221 return cvmx_coremask_is_set_core(pcm, (int)cvmx_get_core_num());
225 * coremask is simply unsigned int (32 bits).
227 * NOTE: supports up to 32 cores maximum.
229 * union of coremasks is simply bitwise-or.
230 * intersection of coremasks is simply bitwise-and.
234 #define CVMX_COREMASK_MAX 0xFFFFFFFFu /* maximum supported mask */
238 * Compute coremask for a specific core.
240 * @param core_id The core ID
242 * @return coremask for a specific core
245 static inline unsigned int cvmx_coremask_core(unsigned int core_id)
247 return (1u << core_id);
251 * Compute coremask for num_cores cores starting with core 0.
253 * @param num_cores number of cores
255 * @return coremask for num_cores cores
258 static inline unsigned int cvmx_coremask_numcores(unsigned int num_cores)
260 return (CVMX_COREMASK_MAX >> (CVMX_MAX_CORES - num_cores));
264 * Compute coremask for a range of cores from core low to core high.
266 * @param low first core in the range
267 * @param high last core in the range
269 * @return coremask for the range of cores
272 static inline unsigned int cvmx_coremask_range(unsigned int low, unsigned int high)
274 return ((CVMX_COREMASK_MAX >> (CVMX_MAX_CORES - 1 - high + low)) << low);
279 * Test to see if current core is a member of coremask.
281 * @param coremask the coremask to test against
283 * @return 1 if current core is a member of coremask, 0 otherwise
286 static inline int cvmx_coremask_is_member(unsigned int coremask)
288 return ((cvmx_coremask_core(cvmx_get_core_num()) & coremask) != 0);
292 * Test to see if current core is first core in coremask.
294 * @param coremask the coremask to test against
296 * @return 1 if current core is first core in the coremask, 0 otherwise
299 static inline int cvmx_coremask_first_core(unsigned int coremask)
301 return cvmx_coremask_is_member(coremask)
302 && ((cvmx_get_core_num() == 0) ||
303 ((cvmx_coremask_numcores(cvmx_get_core_num()) & coremask) == 0));
307 * Wait (stall) until all cores in the given coremask has reached this point
308 * in the program execution before proceeding.
310 * @param coremask the group of cores performing the barrier sync
313 extern void cvmx_coremask_barrier_sync(unsigned int coremask);
319 #endif /* __CVMX_COREMASK_H__ */