]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/contrib/octeon-sdk/cvmx-coremask.c
MFC r362623:
[FreeBSD/stable/8.git] / sys / contrib / octeon-sdk / cvmx-coremask.c
1 /***********************license start***************
2  *  Copyright (c) 2003-2008 Cavium Networks (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 Networks 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  *  TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24  *  AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25  *  OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26  *  RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27  *  REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28  *  DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29  *  OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30  *  PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31  *  POSSESSION OR CORRESPONDENCE TO DESCRIPTION.  THE ENTIRE RISK ARISING OUT
32  *  OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
33  *
34  *
35  *  For any questions regarding licensing please contact marketing@caviumnetworks.com
36  *
37  ***********************license end**************************************/
38
39
40
41
42
43
44 /**
45  * @file
46  *
47  * Module to support operations on bitmap of cores. Coremask can be used to
48  * select a specific core, a group of cores, or all available cores, for
49  * initialization and differentiation of roles within a single shared binary
50  * executable image.
51  *
52  * <hr>$Revision: 41586 $<hr>
53  *
54  */
55
56 #include "cvmx-config.h"
57 #include "cvmx.h"
58 #include "cvmx-spinlock.h"
59 #include "cvmx-coremask.h"
60
61
62 #define  CVMX_COREMASK_MAX_SYNCS  20  /* maximum number of coremasks for barrier sync */
63
64 /**
65  * This structure defines the private state maintained by coremask module.
66  *
67  */
68 CVMX_SHARED static struct {
69
70     cvmx_spinlock_t            lock;       /**< mutex spinlock */
71
72     struct {
73
74         unsigned int           coremask;   /**< coremask specified for barrier */
75         unsigned int           checkin;    /**< bitmask of cores checking in */
76         volatile unsigned int  exit;       /**< variable to poll for exit condition */
77
78     } s[CVMX_COREMASK_MAX_SYNCS];
79
80 } state = {
81
82     { CVMX_SPINLOCK_UNLOCKED_VAL },
83
84     { { 0, 0, 0 } },
85 };
86
87
88 /**
89  * Wait (stall) until all cores in the given coremask has reached this point
90  * in the program execution before proceeding.
91  *
92  * @param  coremask  the group of cores performing the barrier sync
93  *
94  */
95 void cvmx_coremask_barrier_sync(unsigned int coremask)
96 {
97     int i;
98     unsigned int target;
99
100     assert(coremask != 0);
101
102     cvmx_spinlock_lock(&state.lock);
103
104     for (i = 0; i < CVMX_COREMASK_MAX_SYNCS; i++) {
105
106         if (state.s[i].coremask == 0) {
107             /* end of existing coremask list, create new entry, fall-thru */
108             state.s[i].coremask = coremask;
109         }
110
111         if (state.s[i].coremask == coremask) {
112
113             target = state.s[i].exit + 1;  /* wrap-around at 32b */
114
115             state.s[i].checkin |= cvmx_coremask_core(cvmx_get_core_num());
116             if (state.s[i].checkin == coremask) {
117                 state.s[i].checkin = 0;
118                 state.s[i].exit = target;  /* signal exit condition */
119             }
120             cvmx_spinlock_unlock(&state.lock);
121
122             while (state.s[i].exit != target)
123                 ;
124
125             return;
126         }
127     }
128
129     /* error condition - coremask array overflowed */
130     cvmx_spinlock_unlock(&state.lock);
131     assert(0);
132 }