]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/smp.h
Merge gdtoa-20110304.
[FreeBSD/FreeBSD.git] / sys / sys / smp.h
1 /*-
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD$
10  */
11
12 #ifndef _SYS_SMP_H_
13 #define _SYS_SMP_H_
14
15 #ifdef _KERNEL
16
17 #ifndef LOCORE
18
19 /*
20  * Topology of a NUMA or HTT system.
21  *
22  * The top level topology is an array of pointers to groups.  Each group
23  * contains a bitmask of cpus in its group or subgroups.  It may also
24  * contain a pointer to an array of child groups.
25  *
26  * The bitmasks at non leaf groups may be used by consumers who support
27  * a smaller depth than the hardware provides.
28  *
29  * The topology may be omitted by systems where all CPUs are equal.
30  */
31
32 struct cpu_group {
33         struct cpu_group *cg_parent;    /* Our parent group. */
34         struct cpu_group *cg_child;     /* Optional children groups. */
35         cpumask_t       cg_mask;        /* Mask of cpus in this group. */
36         int8_t          cg_count;       /* Count of cpus in this group. */
37         int8_t          cg_children;    /* Number of children groups. */
38         int8_t          cg_level;       /* Shared cache level. */
39         int8_t          cg_flags;       /* Traversal modifiers. */
40 };
41
42 typedef struct cpu_group *cpu_group_t;
43
44 /*
45  * Defines common resources for CPUs in the group.  The highest level
46  * resource should be used when multiple are shared.
47  */
48 #define CG_SHARE_NONE   0
49 #define CG_SHARE_L1     1
50 #define CG_SHARE_L2     2
51 #define CG_SHARE_L3     3
52
53 /*
54  * Behavior modifiers for load balancing and affinity.
55  */
56 #define CG_FLAG_HTT     0x01            /* Schedule the alternate core last. */
57 #define CG_FLAG_SMT     0x02            /* New age htt, less crippled. */
58 #define CG_FLAG_THREAD  (CG_FLAG_HTT | CG_FLAG_SMT)     /* Any threading. */
59
60 /*
61  * Convenience routines for building topologies.
62  */
63 #ifdef SMP
64 struct cpu_group *smp_topo(void);
65 struct cpu_group *smp_topo_none(void);
66 struct cpu_group *smp_topo_1level(int l1share, int l1count, int l1flags);
67 struct cpu_group *smp_topo_2level(int l2share, int l2count, int l1share,
68     int l1count, int l1flags);
69 struct cpu_group *smp_topo_find(struct cpu_group *top, int cpu);
70
71 extern void (*cpustop_restartfunc)(void);
72 extern int smp_active;
73 extern int smp_cpus;
74 extern volatile cpumask_t started_cpus;
75 extern volatile cpumask_t stopped_cpus;
76 extern cpumask_t idle_cpus_mask;
77 extern cpumask_t hlt_cpus_mask;
78 extern cpumask_t logical_cpus_mask;
79 #endif /* SMP */
80
81 extern u_int mp_maxid;
82 extern int mp_maxcpus;
83 extern int mp_ncpus;
84 extern volatile int smp_started;
85
86 extern cpumask_t all_cpus;
87
88 /*
89  * Macro allowing us to determine whether a CPU is absent at any given
90  * time, thus permitting us to configure sparse maps of cpuid-dependent
91  * (per-CPU) structures.
92  */
93 #define CPU_ABSENT(x_cpu)       ((all_cpus & (1 << (x_cpu))) == 0)
94
95 /*
96  * Macros to iterate over non-absent CPUs.  CPU_FOREACH() takes an
97  * integer iterator and iterates over the available set of CPUs.
98  * CPU_FIRST() returns the id of the first non-absent CPU.  CPU_NEXT()
99  * returns the id of the next non-absent CPU.  It will wrap back to
100  * CPU_FIRST() once the end of the list is reached.  The iterators are
101  * currently implemented via inline functions.
102  */
103 #define CPU_FOREACH(i)                                                  \
104         for ((i) = 0; (i) <= mp_maxid; (i)++)                           \
105                 if (!CPU_ABSENT((i)))
106
107 static __inline int
108 cpu_first(void)
109 {
110         int i;
111
112         for (i = 0;; i++)
113                 if (!CPU_ABSENT(i))
114                         return (i);
115 }
116
117 static __inline int
118 cpu_next(int i)
119 {
120
121         for (;;) {
122                 i++;
123                 if (i > mp_maxid)
124                         i = 0;
125                 if (!CPU_ABSENT(i))
126                         return (i);
127         }
128 }
129
130 #define CPU_FIRST()     cpu_first()
131 #define CPU_NEXT(i)     cpu_next((i))
132
133 #ifdef SMP
134 /*
135  * Machine dependent functions used to initialize MP support.
136  *
137  * The cpu_mp_probe() should check to see if MP support is present and return
138  * zero if it is not or non-zero if it is.  If MP support is present, then
139  * cpu_mp_start() will be called so that MP can be enabled.  This function
140  * should do things such as startup secondary processors.  It should also
141  * setup mp_ncpus, all_cpus, and smp_cpus.  It should also ensure that
142  * smp_active and smp_started are initialized at the appropriate time.
143  * Once cpu_mp_start() returns, machine independent MP startup code will be
144  * executed and a simple message will be output to the console.  Finally,
145  * cpu_mp_announce() will be called so that machine dependent messages about
146  * the MP support may be output to the console if desired.
147  *
148  * The cpu_setmaxid() function is called very early during the boot process
149  * so that the MD code may set mp_maxid to provide an upper bound on CPU IDs
150  * that other subsystems may use.  If a platform is not able to determine
151  * the exact maximum ID that early, then it may set mp_maxid to MAXCPU - 1.
152  */
153 struct thread;
154
155 struct cpu_group *cpu_topo(void);
156 void    cpu_mp_announce(void);
157 int     cpu_mp_probe(void);
158 void    cpu_mp_setmaxid(void);
159 void    cpu_mp_start(void);
160
161 void    forward_signal(struct thread *);
162 int     restart_cpus(cpumask_t);
163 int     stop_cpus(cpumask_t);
164 int     stop_cpus_hard(cpumask_t);
165 #if defined(__amd64__)
166 int     suspend_cpus(cpumask_t);
167 #endif
168 void    smp_rendezvous_action(void);
169 extern  struct mtx smp_ipi_mtx;
170
171 #endif /* SMP */
172 void    smp_no_rendevous_barrier(void *);
173 void    smp_rendezvous(void (*)(void *), 
174                        void (*)(void *),
175                        void (*)(void *),
176                        void *arg);
177 void    smp_rendezvous_cpus(cpumask_t,
178                        void (*)(void *), 
179                        void (*)(void *),
180                        void (*)(void *),
181                        void *arg);
182 #endif /* !LOCORE */
183 #endif /* _KERNEL */
184 #endif /* _SYS_SMP_H_ */