1/***********************license start***************
2 * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *   * Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *
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.
17
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
21 *     permission.
22
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
26 * countries.
27
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**************************************/
39
40
41
42
43
44
45
46/**
47 * @file
48 *
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
52 * executable image.
53 *
54 * <hr>$Revision: 70030 $<hr>
55 *
56 */
57
58
59#ifndef __CVMX_COREMASK_H__
60#define __CVMX_COREMASK_H__
61
62#include "cvmx-asm.h"
63
64#ifdef	__cplusplus
65extern "C" {
66#endif
67
68typedef uint64_t cvmx_coremask_holder_t;	/* basic type to hold the
69						   coremask bits */
70
71#define CVMX_COREMASK_HLDRSZ ((int)(sizeof(cvmx_coremask_holder_t) * 8))
72						/* bits per holder */
73
74#define CVMX_COREMASK_BMPSZ ((int)(CVMX_MAX_CORES / CVMX_COREMASK_HLDRSZ + 1))
75						/* bit map size */
76
77/*
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.
81 */
82#define CVMX_COREMASK_FOR_EACH_CORE_BEGIN(fec_pcm, fec_ppid)		\
83    do {								\
84    	int fec_i, fec_j;						\
85    				 					\
86	for (fec_i = 0; fec_i < CVMX_COREMASK_BMPSZ; fec_i++)		\
87	{								\
88	    for (fec_j = 0; fec_j < CVMX_COREMASK_HLDRSZ; fec_j++)	\
89	    { 								\
90		if (((cvmx_coremask_holder_t)1 << fec_j) & 		\
91		    (fec_pcm)->coremask_bitmap[fec_i])			\
92		{							\
93	            fec_ppid = fec_i * CVMX_COREMASK_HLDRSZ + fec_j;
94
95
96#define CVMX_COREMASK_FOR_EACH_CORE_END					\
97		}							\
98	    }								\
99	}								\
100   } while (0)
101
102struct cvmx_coremask {
103    /*
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.
107     */
108    cvmx_coremask_holder_t coremask_bitmap[CVMX_COREMASK_BMPSZ];
109};
110
111/*
112 * Is ``core'' set in the coremask?
113 *
114 * @param pcm is the pointer to the coremask.
115 * @param core
116 * @return 1 if core is set and 0 if not.
117 */
118static inline int cvmx_coremask_is_set_core(struct cvmx_coremask *pcm,
119    int core)
120{
121    int n, i;
122
123    n = core % CVMX_COREMASK_HLDRSZ;
124    i = core / CVMX_COREMASK_HLDRSZ;
125
126    return (int)((pcm->coremask_bitmap[i] & (1ull << n)) != 0);
127}
128
129/*
130 * Set ``core'' in the coremask.
131 *
132 * @param pcm is the pointer to the coremask.
133 * @param core
134 * @return 0.
135 */
136static inline int cvmx_coremask_set_core(struct cvmx_coremask *pcm,
137    int core)
138{
139    int n, i;
140
141    n = core % CVMX_COREMASK_HLDRSZ;
142    i = core / CVMX_COREMASK_HLDRSZ;
143    pcm->coremask_bitmap[i] |= (1ull << n);
144
145    return 0;
146}
147
148/*
149 * Clear ``core'' from the coremask.
150 *
151 * @param pcm is the pointer to the coremask.
152 * @param core
153 * @return 0.
154 */
155static inline int cvmx_coremask_clear_core(struct cvmx_coremask *pcm,
156    int core)
157{
158    int n, i;
159
160    n = core % CVMX_COREMASK_HLDRSZ;
161    i = core / CVMX_COREMASK_HLDRSZ;
162    pcm->coremask_bitmap[i] &= ~(1ull << n);
163
164    return 0;
165}
166
167/*
168 * Clear the coremask.
169 *
170 * @param pcm is the pointer to the coremask.
171 * @return 0.
172 */
173static inline int cvmx_coremask_clear_all(struct cvmx_coremask *pcm)
174{
175    int i;
176
177    for (i = 0; i < CVMX_COREMASK_BMPSZ; i++)
178        pcm->coremask_bitmap[i] = 0;
179
180    return 0;
181}
182
183/*
184 * Is the current core the first in the coremask?
185 *
186 * @param pcm is the pointer to the coremask.
187 * @return 1 for yes and 0 for no.
188 */
189static inline int cvmx_coremask_first_core_bmp(struct cvmx_coremask *pcm)
190{
191     int n, i;
192
193     n = (int) cvmx_get_core_num();
194     for (i = 0; i < CVMX_COREMASK_BMPSZ; i++)
195     {
196         if (pcm->coremask_bitmap[i])
197         {
198             if (n == 0 && pcm->coremask_bitmap[i] & 1)
199                 return 1;
200
201             if (n >= CVMX_COREMASK_HLDRSZ)
202                 return 0;
203
204             return ((((1ull << n) - 1) & pcm->coremask_bitmap[i]) == 0);
205         }
206         else
207             n -= CVMX_COREMASK_HLDRSZ;
208     }
209
210     return 0;
211}
212
213/*
214 * Is the current core a member of the coremask?
215 *
216 * @param pcm is the pointer to the coremask.
217 * @return 1 for yes and 0 for no.
218 */
219static inline int cvmx_coremask_is_member_bmp(struct cvmx_coremask *pcm)
220{
221    return cvmx_coremask_is_set_core(pcm, (int)cvmx_get_core_num());
222}
223
224/*
225 * coremask is simply unsigned int (32 bits).
226 *
227 * NOTE: supports up to 32 cores maximum.
228 *
229 * union of coremasks is simply bitwise-or.
230 * intersection of coremasks is simply bitwise-and.
231 *
232 */
233
234#define  CVMX_COREMASK_MAX  0xFFFFFFFFu    /* maximum supported mask */
235
236
237/**
238 * Compute coremask for a specific core.
239 *
240 * @param  core_id  The core ID
241 *
242 * @return  coremask for a specific core
243 *
244 */
245static inline unsigned int cvmx_coremask_core(unsigned int core_id)
246{
247    return (1u << core_id);
248}
249
250/**
251 * Compute coremask for num_cores cores starting with core 0.
252 *
253 * @param  num_cores  number of cores
254 *
255 * @return  coremask for num_cores cores
256 *
257 */
258static inline unsigned int cvmx_coremask_numcores(unsigned int num_cores)
259{
260    return (CVMX_COREMASK_MAX >> (CVMX_MAX_CORES - num_cores));
261}
262
263/**
264 * Compute coremask for a range of cores from core low to core high.
265 *
266 * @param  low   first core in the range
267 * @param  high  last core in the range
268 *
269 * @return  coremask for the range of cores
270 *
271 */
272static inline unsigned int cvmx_coremask_range(unsigned int low, unsigned int high)
273{
274    return ((CVMX_COREMASK_MAX >> (CVMX_MAX_CORES - 1 - high + low)) << low);
275}
276
277
278/**
279 * Test to see if current core is a member of coremask.
280 *
281 * @param  coremask  the coremask to test against
282 *
283 * @return  1 if current core is a member of coremask, 0 otherwise
284 *
285 */
286static inline int cvmx_coremask_is_member(unsigned int coremask)
287{
288    return ((cvmx_coremask_core(cvmx_get_core_num()) & coremask) != 0);
289}
290
291/**
292 * Test to see if current core is first core in coremask.
293 *
294 * @param  coremask  the coremask to test against
295 *
296 * @return  1 if current core is first core in the coremask, 0 otherwise
297 *
298 */
299static inline int cvmx_coremask_first_core(unsigned int coremask)
300{
301    return cvmx_coremask_is_member(coremask)
302        && ((cvmx_get_core_num() == 0) ||
303            ((cvmx_coremask_numcores(cvmx_get_core_num()) & coremask) == 0));
304}
305
306/**
307 * Wait (stall) until all cores in the given coremask has reached this point
308 * in the program execution before proceeding.
309 *
310 * @param  coremask  the group of cores performing the barrier sync
311 *
312 */
313extern void cvmx_coremask_barrier_sync(unsigned int coremask);
314
315#ifdef	__cplusplus
316}
317#endif
318
319#endif /* __CVMX_COREMASK_H__ */
320