cpuvar.h revision 1.1.1.1 1 1.1 darran /*
2 1.1 darran * CDDL HEADER START
3 1.1 darran *
4 1.1 darran * The contents of this file are subject to the terms of the
5 1.1 darran * Common Development and Distribution License (the "License").
6 1.1 darran * You may not use this file except in compliance with the License.
7 1.1 darran *
8 1.1 darran * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 1.1 darran * or http://www.opensolaris.org/os/licensing.
10 1.1 darran * See the License for the specific language governing permissions
11 1.1 darran * and limitations under the License.
12 1.1 darran *
13 1.1 darran * When distributing Covered Code, include this CDDL HEADER in each
14 1.1 darran * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 1.1 darran * If applicable, add the following below this CDDL HEADER, with the
16 1.1 darran * fields enclosed by brackets "[]" replaced with your own identifying
17 1.1 darran * information: Portions Copyright [yyyy] [name of copyright owner]
18 1.1 darran *
19 1.1 darran * CDDL HEADER END
20 1.1 darran */
21 1.1 darran
22 1.1 darran /*
23 1.1 darran * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 1.1 darran * Use is subject to license terms.
25 1.1 darran */
26 1.1 darran
27 1.1 darran #ifndef _SYS_CPUVAR_H
28 1.1 darran #define _SYS_CPUVAR_H
29 1.1 darran
30 1.1 darran #include <sys/thread.h>
31 1.1 darran #include <sys/sysinfo.h> /* has cpu_stat_t definition */
32 1.1 darran #include <sys/disp.h>
33 1.1 darran #include <sys/processor.h>
34 1.1 darran
35 1.1 darran #if (defined(_KERNEL) || defined(_KMEMUSER)) && defined(_MACHDEP)
36 1.1 darran #include <sys/machcpuvar.h>
37 1.1 darran #endif
38 1.1 darran
39 1.1 darran #include <sys/types.h>
40 1.1 darran #include <sys/file.h>
41 1.1 darran #include <sys/bitmap.h>
42 1.1 darran #include <sys/rwlock.h>
43 1.1 darran #include <sys/msacct.h>
44 1.1 darran #if defined(__GNUC__) && defined(_ASM_INLINES) && defined(_KERNEL) && \
45 1.1 darran (defined(__i386) || defined(__amd64))
46 1.1 darran #include <asm/cpuvar.h>
47 1.1 darran #endif
48 1.1 darran
49 1.1 darran #ifdef __cplusplus
50 1.1 darran extern "C" {
51 1.1 darran #endif
52 1.1 darran
53 1.1 darran struct squeue_set_s;
54 1.1 darran
55 1.1 darran #define CPU_CACHE_COHERENCE_SIZE 64
56 1.1 darran #define S_LOADAVG_SZ 11
57 1.1 darran #define S_MOVAVG_SZ 10
58 1.1 darran
59 1.1 darran struct loadavg_s {
60 1.1 darran int lg_cur; /* current loadavg entry */
61 1.1 darran unsigned int lg_len; /* number entries recorded */
62 1.1 darran hrtime_t lg_total; /* used to temporarily hold load totals */
63 1.1 darran hrtime_t lg_loads[S_LOADAVG_SZ]; /* table of recorded entries */
64 1.1 darran };
65 1.1 darran
66 1.1 darran /*
67 1.1 darran * For fast event tracing.
68 1.1 darran */
69 1.1 darran struct ftrace_record;
70 1.1 darran typedef struct ftrace_data {
71 1.1 darran int ftd_state; /* ftrace flags */
72 1.1 darran kmutex_t ftd_unused; /* ftrace buffer lock, unused */
73 1.1 darran struct ftrace_record *ftd_cur; /* current record */
74 1.1 darran struct ftrace_record *ftd_first; /* first record */
75 1.1 darran struct ftrace_record *ftd_last; /* last record */
76 1.1 darran } ftrace_data_t;
77 1.1 darran
78 1.1 darran struct cyc_cpu;
79 1.1 darran struct nvlist;
80 1.1 darran
81 1.1 darran /*
82 1.1 darran * Per-CPU data.
83 1.1 darran *
84 1.1 darran * Be careful adding new members: if they are not the same in all modules (e.g.
85 1.1 darran * change size depending on a #define), CTF uniquification can fail to work
86 1.1 darran * properly. Furthermore, this is transitive in that it applies recursively to
87 1.1 darran * all types pointed to by cpu_t.
88 1.1 darran */
89 1.1 darran typedef struct cpu {
90 1.1 darran processorid_t cpu_id; /* CPU number */
91 1.1 darran processorid_t cpu_seqid; /* sequential CPU id (0..ncpus-1) */
92 1.1 darran volatile cpu_flag_t cpu_flags; /* flags indicating CPU state */
93 1.1 darran struct cpu *cpu_self; /* pointer to itself */
94 1.1 darran kthread_t *cpu_thread; /* current thread */
95 1.1 darran kthread_t *cpu_idle_thread; /* idle thread for this CPU */
96 1.1 darran kthread_t *cpu_pause_thread; /* pause thread for this CPU */
97 1.1 darran klwp_id_t cpu_lwp; /* current lwp (if any) */
98 1.1 darran klwp_id_t cpu_fpowner; /* currently loaded fpu owner */
99 1.1 darran struct cpupart *cpu_part; /* partition with this CPU */
100 1.1 darran struct lgrp_ld *cpu_lpl; /* pointer to this cpu's load */
101 1.1 darran int cpu_cache_offset; /* see kmem.c for details */
102 1.1 darran
103 1.1 darran /*
104 1.1 darran * Links to other CPUs. It is safe to walk these lists if
105 1.1 darran * one of the following is true:
106 1.1 darran * - cpu_lock held
107 1.1 darran * - preemption disabled via kpreempt_disable
108 1.1 darran * - PIL >= DISP_LEVEL
109 1.1 darran * - acting thread is an interrupt thread
110 1.1 darran * - all other CPUs are paused
111 1.1 darran */
112 1.1 darran struct cpu *cpu_next; /* next existing CPU */
113 1.1 darran struct cpu *cpu_prev; /* prev existing CPU */
114 1.1 darran struct cpu *cpu_next_onln; /* next online (enabled) CPU */
115 1.1 darran struct cpu *cpu_prev_onln; /* prev online (enabled) CPU */
116 1.1 darran struct cpu *cpu_next_part; /* next CPU in partition */
117 1.1 darran struct cpu *cpu_prev_part; /* prev CPU in partition */
118 1.1 darran struct cpu *cpu_next_lgrp; /* next CPU in latency group */
119 1.1 darran struct cpu *cpu_prev_lgrp; /* prev CPU in latency group */
120 1.1 darran struct cpu *cpu_next_lpl; /* next CPU in lgrp partition */
121 1.1 darran struct cpu *cpu_prev_lpl;
122 1.1 darran
123 1.1 darran struct cpu_pg *cpu_pg; /* cpu's processor groups */
124 1.1 darran
125 1.1 darran void *cpu_reserved[4]; /* reserved for future use */
126 1.1 darran
127 1.1 darran /*
128 1.1 darran * Scheduling variables.
129 1.1 darran */
130 1.1 darran disp_t *cpu_disp; /* dispatch queue data */
131 1.1 darran /*
132 1.1 darran * Note that cpu_disp is set before the CPU is added to the system
133 1.1 darran * and is never modified. Hence, no additional locking is needed
134 1.1 darran * beyond what's necessary to access the cpu_t structure.
135 1.1 darran */
136 1.1 darran char cpu_runrun; /* scheduling flag - set to preempt */
137 1.1 darran char cpu_kprunrun; /* force kernel preemption */
138 1.1 darran pri_t cpu_chosen_level; /* priority at which cpu */
139 1.1 darran /* was chosen for scheduling */
140 1.1 darran kthread_t *cpu_dispthread; /* thread selected for dispatch */
141 1.1 darran disp_lock_t cpu_thread_lock; /* dispatcher lock on current thread */
142 1.1 darran uint8_t cpu_disp_flags; /* flags used by dispatcher */
143 1.1 darran /*
144 1.1 darran * The following field is updated when ever the cpu_dispthread
145 1.1 darran * changes. Also in places, where the current thread(cpu_dispthread)
146 1.1 darran * priority changes. This is used in disp_lowpri_cpu()
147 1.1 darran */
148 1.1 darran pri_t cpu_dispatch_pri; /* priority of cpu_dispthread */
149 1.1 darran clock_t cpu_last_swtch; /* last time switched to new thread */
150 1.1 darran
151 1.1 darran /*
152 1.1 darran * Interrupt data.
153 1.1 darran */
154 1.1 darran caddr_t cpu_intr_stack; /* interrupt stack */
155 1.1 darran kthread_t *cpu_intr_thread; /* interrupt thread list */
156 1.1 darran uint_t cpu_intr_actv; /* interrupt levels active (bitmask) */
157 1.1 darran int cpu_base_spl; /* priority for highest rupt active */
158 1.1 darran
159 1.1 darran /*
160 1.1 darran * Statistics.
161 1.1 darran */
162 1.1 darran cpu_stats_t cpu_stats; /* per-CPU statistics */
163 1.1 darran struct kstat *cpu_info_kstat; /* kstat for cpu info */
164 1.1 darran
165 1.1 darran uintptr_t cpu_profile_pc; /* kernel PC in profile interrupt */
166 1.1 darran uintptr_t cpu_profile_upc; /* user PC in profile interrupt */
167 1.1 darran uintptr_t cpu_profile_pil; /* PIL when profile interrupted */
168 1.1 darran
169 1.1 darran ftrace_data_t cpu_ftrace; /* per cpu ftrace data */
170 1.1 darran
171 1.1 darran clock_t cpu_deadman_lbolt; /* used by deadman() */
172 1.1 darran uint_t cpu_deadman_countdown; /* used by deadman() */
173 1.1 darran
174 1.1 darran kmutex_t cpu_cpc_ctxlock; /* protects context for idle thread */
175 1.1 darran kcpc_ctx_t *cpu_cpc_ctx; /* performance counter context */
176 1.1 darran
177 1.1 darran /*
178 1.1 darran * Configuration information for the processor_info system call.
179 1.1 darran */
180 1.1 darran processor_info_t cpu_type_info; /* config info */
181 1.1 darran time_t cpu_state_begin; /* when CPU entered current state */
182 1.1 darran char cpu_cpr_flags; /* CPR related info */
183 1.1 darran struct cyc_cpu *cpu_cyclic; /* per cpu cyclic subsystem data */
184 1.1 darran struct squeue_set_s *cpu_squeue_set; /* per cpu squeue set */
185 1.1 darran struct nvlist *cpu_props; /* pool-related properties */
186 1.1 darran
187 1.1 darran krwlock_t cpu_ft_lock; /* DTrace: fasttrap lock */
188 1.1 darran uintptr_t cpu_dtrace_caller; /* DTrace: caller, if any */
189 1.1 darran hrtime_t cpu_dtrace_chillmark; /* DTrace: chill mark time */
190 1.1 darran hrtime_t cpu_dtrace_chilled; /* DTrace: total chill time */
191 1.1 darran volatile uint16_t cpu_mstate; /* cpu microstate */
192 1.1 darran volatile uint16_t cpu_mstate_gen; /* generation counter */
193 1.1 darran volatile hrtime_t cpu_mstate_start; /* cpu microstate start time */
194 1.1 darran volatile hrtime_t cpu_acct[NCMSTATES]; /* cpu microstate data */
195 1.1 darran hrtime_t cpu_intracct[NCMSTATES]; /* interrupt mstate data */
196 1.1 darran hrtime_t cpu_waitrq; /* cpu run-queue wait time */
197 1.1 darran struct loadavg_s cpu_loadavg; /* loadavg info for this cpu */
198 1.1 darran
199 1.1 darran char *cpu_idstr; /* for printing and debugging */
200 1.1 darran char *cpu_brandstr; /* for printing */
201 1.1 darran
202 1.1 darran /*
203 1.1 darran * Sum of all device interrupt weights that are currently directed at
204 1.1 darran * this cpu. Cleared at start of interrupt redistribution.
205 1.1 darran */
206 1.1 darran int32_t cpu_intr_weight;
207 1.1 darran void *cpu_vm_data;
208 1.1 darran
209 1.1 darran struct cpu_physid *cpu_physid; /* physical associations */
210 1.1 darran
211 1.1 darran uint64_t cpu_curr_clock; /* current clock freq in Hz */
212 1.1 darran char *cpu_supp_freqs; /* supported freqs in Hz */
213 1.1 darran
214 1.1 darran /*
215 1.1 darran * Interrupt load factor used by dispatcher & softcall
216 1.1 darran */
217 1.1 darran hrtime_t cpu_intrlast; /* total interrupt time (nsec) */
218 1.1 darran int cpu_intrload; /* interrupt load factor (0-99%) */
219 1.1 darran
220 1.1 darran /*
221 1.1 darran * New members must be added /before/ this member, as the CTF tools
222 1.1 darran * rely on this being the last field before cpu_m, so they can
223 1.1 darran * correctly calculate the offset when synthetically adding the cpu_m
224 1.1 darran * member in objects that do not have it. This fixup is required for
225 1.1 darran * uniquification to work correctly.
226 1.1 darran */
227 1.1 darran uintptr_t cpu_m_pad;
228 1.1 darran
229 1.1 darran #if (defined(_KERNEL) || defined(_KMEMUSER)) && defined(_MACHDEP)
230 1.1 darran struct machcpu cpu_m; /* per architecture info */
231 1.1 darran #endif
232 1.1 darran } cpu_t;
233 1.1 darran
234 1.1 darran /*
235 1.1 darran * The cpu_core structure consists of per-CPU state available in any context.
236 1.1 darran * On some architectures, this may mean that the page(s) containing the
237 1.1 darran * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
238 1.1 darran * is up to the platform to assure that this is performed properly. Note that
239 1.1 darran * the structure is sized to avoid false sharing.
240 1.1 darran */
241 1.1 darran #define CPUC_SIZE (sizeof (uint16_t) + sizeof (uintptr_t) + \
242 1.1 darran sizeof (kmutex_t))
243 1.1 darran #define CPUC_PADSIZE CPU_CACHE_COHERENCE_SIZE - CPUC_SIZE
244 1.1 darran
245 1.1 darran typedef struct cpu_core {
246 1.1 darran uint16_t cpuc_dtrace_flags; /* DTrace flags */
247 1.1 darran uint8_t cpuc_pad[CPUC_PADSIZE]; /* padding */
248 1.1 darran uintptr_t cpuc_dtrace_illval; /* DTrace illegal value */
249 1.1 darran kmutex_t cpuc_pid_lock; /* DTrace pid provider lock */
250 1.1 darran } cpu_core_t;
251 1.1 darran
252 1.1 darran #ifdef _KERNEL
253 1.1 darran extern cpu_core_t cpu_core[];
254 1.1 darran #endif /* _KERNEL */
255 1.1 darran
256 1.1 darran /*
257 1.1 darran * CPU_ON_INTR() macro. Returns non-zero if currently on interrupt stack.
258 1.1 darran * Note that this isn't a test for a high PIL. For example, cpu_intr_actv
259 1.1 darran * does not get updated when we go through sys_trap from TL>0 at high PIL.
260 1.1 darran * getpil() should be used instead to check for PIL levels.
261 1.1 darran */
262 1.1 darran #define CPU_ON_INTR(cpup) ((cpup)->cpu_intr_actv >> (LOCK_LEVEL + 1))
263 1.1 darran
264 1.1 darran #if defined(_KERNEL) || defined(_KMEMUSER)
265 1.1 darran
266 1.1 darran #define INTR_STACK_SIZE MAX(DEFAULTSTKSZ, PAGESIZE)
267 1.1 darran
268 1.1 darran /* MEMBERS PROTECTED BY "atomicity": cpu_flags */
269 1.1 darran
270 1.1 darran /*
271 1.1 darran * Flags in the CPU structure.
272 1.1 darran *
273 1.1 darran * These are protected by cpu_lock (except during creation).
274 1.1 darran *
275 1.1 darran * Offlined-CPUs have three stages of being offline:
276 1.1 darran *
277 1.1 darran * CPU_ENABLE indicates that the CPU is participating in I/O interrupts
278 1.1 darran * that can be directed at a number of different CPUs. If CPU_ENABLE
279 1.1 darran * is off, the CPU will not be given interrupts that can be sent elsewhere,
280 1.1 darran * but will still get interrupts from devices associated with that CPU only,
281 1.1 darran * and from other CPUs.
282 1.1 darran *
283 1.1 darran * CPU_OFFLINE indicates that the dispatcher should not allow any threads
284 1.1 darran * other than interrupt threads to run on that CPU. A CPU will not have
285 1.1 darran * CPU_OFFLINE set if there are any bound threads (besides interrupts).
286 1.1 darran *
287 1.1 darran * CPU_QUIESCED is set if p_offline was able to completely turn idle the
288 1.1 darran * CPU and it will not have to run interrupt threads. In this case it'll
289 1.1 darran * stay in the idle loop until CPU_QUIESCED is turned off.
290 1.1 darran *
291 1.1 darran * CPU_FROZEN is used only by CPR to mark CPUs that have been successfully
292 1.1 darran * suspended (in the suspend path), or have yet to be resumed (in the resume
293 1.1 darran * case).
294 1.1 darran *
295 1.1 darran * On some platforms CPUs can be individually powered off.
296 1.1 darran * The following flags are set for powered off CPUs: CPU_QUIESCED,
297 1.1 darran * CPU_OFFLINE, and CPU_POWEROFF. The following flags are cleared:
298 1.1 darran * CPU_RUNNING, CPU_READY, CPU_EXISTS, and CPU_ENABLE.
299 1.1 darran */
300 1.1 darran #define CPU_RUNNING 0x001 /* CPU running */
301 1.1 darran #define CPU_READY 0x002 /* CPU ready for cross-calls */
302 1.1 darran #define CPU_QUIESCED 0x004 /* CPU will stay in idle */
303 1.1 darran #define CPU_EXISTS 0x008 /* CPU is configured */
304 1.1 darran #define CPU_ENABLE 0x010 /* CPU enabled for interrupts */
305 1.1 darran #define CPU_OFFLINE 0x020 /* CPU offline via p_online */
306 1.1 darran #define CPU_POWEROFF 0x040 /* CPU is powered off */
307 1.1 darran #define CPU_FROZEN 0x080 /* CPU is frozen via CPR suspend */
308 1.1 darran #define CPU_SPARE 0x100 /* CPU offline available for use */
309 1.1 darran #define CPU_FAULTED 0x200 /* CPU offline diagnosed faulty */
310 1.1 darran
311 1.1 darran #define FMT_CPU_FLAGS \
312 1.1 darran "\20\12fault\11spare\10frozen" \
313 1.1 darran "\7poweroff\6offline\5enable\4exist\3quiesced\2ready\1run"
314 1.1 darran
315 1.1 darran #define CPU_ACTIVE(cpu) (((cpu)->cpu_flags & CPU_OFFLINE) == 0)
316 1.1 darran
317 1.1 darran /*
318 1.1 darran * Flags for cpu_offline(), cpu_faulted(), and cpu_spare().
319 1.1 darran */
320 1.1 darran #define CPU_FORCED 0x0001 /* Force CPU offline */
321 1.1 darran
322 1.1 darran /*
323 1.1 darran * DTrace flags.
324 1.1 darran */
325 1.1 darran #define CPU_DTRACE_NOFAULT 0x0001 /* Don't fault */
326 1.1 darran #define CPU_DTRACE_DROP 0x0002 /* Drop this ECB */
327 1.1 darran #define CPU_DTRACE_BADADDR 0x0004 /* DTrace fault: bad address */
328 1.1 darran #define CPU_DTRACE_BADALIGN 0x0008 /* DTrace fault: bad alignment */
329 1.1 darran #define CPU_DTRACE_DIVZERO 0x0010 /* DTrace fault: divide by zero */
330 1.1 darran #define CPU_DTRACE_ILLOP 0x0020 /* DTrace fault: illegal operation */
331 1.1 darran #define CPU_DTRACE_NOSCRATCH 0x0040 /* DTrace fault: out of scratch */
332 1.1 darran #define CPU_DTRACE_KPRIV 0x0080 /* DTrace fault: bad kernel access */
333 1.1 darran #define CPU_DTRACE_UPRIV 0x0100 /* DTrace fault: bad user access */
334 1.1 darran #define CPU_DTRACE_TUPOFLOW 0x0200 /* DTrace fault: tuple stack overflow */
335 1.1 darran #if defined(__sparc)
336 1.1 darran #define CPU_DTRACE_FAKERESTORE 0x0400 /* pid provider hint to getreg */
337 1.1 darran #endif
338 1.1 darran #define CPU_DTRACE_ENTRY 0x0800 /* pid provider hint to ustack() */
339 1.1 darran #define CPU_DTRACE_BADSTACK 0x1000 /* DTrace fault: bad stack */
340 1.1 darran
341 1.1 darran #define CPU_DTRACE_FAULT (CPU_DTRACE_BADADDR | CPU_DTRACE_BADALIGN | \
342 1.1 darran CPU_DTRACE_DIVZERO | CPU_DTRACE_ILLOP | \
343 1.1 darran CPU_DTRACE_NOSCRATCH | CPU_DTRACE_KPRIV | \
344 1.1 darran CPU_DTRACE_UPRIV | CPU_DTRACE_TUPOFLOW | \
345 1.1 darran CPU_DTRACE_BADSTACK)
346 1.1 darran #define CPU_DTRACE_ERROR (CPU_DTRACE_FAULT | CPU_DTRACE_DROP)
347 1.1 darran
348 1.1 darran /*
349 1.1 darran * Dispatcher flags
350 1.1 darran * These flags must be changed only by the current CPU.
351 1.1 darran */
352 1.1 darran #define CPU_DISP_DONTSTEAL 0x01 /* CPU undergoing context swtch */
353 1.1 darran #define CPU_DISP_HALTED 0x02 /* CPU halted waiting for interrupt */
354 1.1 darran
355 1.1 darran
356 1.1 darran #endif /* _KERNEL || _KMEMUSER */
357 1.1 darran
358 1.1 darran #if (defined(_KERNEL) || defined(_KMEMUSER)) && defined(_MACHDEP)
359 1.1 darran
360 1.1 darran /*
361 1.1 darran * Macros for manipulating sets of CPUs as a bitmap. Note that this
362 1.1 darran * bitmap may vary in size depending on the maximum CPU id a specific
363 1.1 darran * platform supports. This may be different than the number of CPUs
364 1.1 darran * the platform supports, since CPU ids can be sparse. We define two
365 1.1 darran * sets of macros; one for platforms where the maximum CPU id is less
366 1.1 darran * than the number of bits in a single word (32 in a 32-bit kernel,
367 1.1 darran * 64 in a 64-bit kernel), and one for platforms that require bitmaps
368 1.1 darran * of more than one word.
369 1.1 darran */
370 1.1 darran
371 1.1 darran #define CPUSET_WORDS BT_BITOUL(NCPU)
372 1.1 darran #define CPUSET_NOTINSET ((uint_t)-1)
373 1.1 darran
374 1.1 darran #if CPUSET_WORDS > 1
375 1.1 darran
376 1.1 darran typedef struct cpuset {
377 1.1 darran ulong_t cpub[CPUSET_WORDS];
378 1.1 darran } cpuset_t;
379 1.1 darran
380 1.1 darran /*
381 1.1 darran * Private functions for manipulating cpusets that do not fit in a
382 1.1 darran * single word. These should not be used directly; instead the
383 1.1 darran * CPUSET_* macros should be used so the code will be portable
384 1.1 darran * across different definitions of NCPU.
385 1.1 darran */
386 1.1 darran extern void cpuset_all(cpuset_t *);
387 1.1 darran extern void cpuset_all_but(cpuset_t *, uint_t);
388 1.1 darran extern int cpuset_isnull(cpuset_t *);
389 1.1 darran extern int cpuset_cmp(cpuset_t *, cpuset_t *);
390 1.1 darran extern void cpuset_only(cpuset_t *, uint_t);
391 1.1 darran extern uint_t cpuset_find(cpuset_t *);
392 1.1 darran extern void cpuset_bounds(cpuset_t *, uint_t *, uint_t *);
393 1.1 darran
394 1.1 darran #define CPUSET_ALL(set) cpuset_all(&(set))
395 1.1 darran #define CPUSET_ALL_BUT(set, cpu) cpuset_all_but(&(set), cpu)
396 1.1 darran #define CPUSET_ONLY(set, cpu) cpuset_only(&(set), cpu)
397 1.1 darran #define CPU_IN_SET(set, cpu) BT_TEST((set).cpub, cpu)
398 1.1 darran #define CPUSET_ADD(set, cpu) BT_SET((set).cpub, cpu)
399 1.1 darran #define CPUSET_DEL(set, cpu) BT_CLEAR((set).cpub, cpu)
400 1.1 darran #define CPUSET_ISNULL(set) cpuset_isnull(&(set))
401 1.1 darran #define CPUSET_ISEQUAL(set1, set2) cpuset_cmp(&(set1), &(set2))
402 1.1 darran
403 1.1 darran /*
404 1.1 darran * Find one CPU in the cpuset.
405 1.1 darran * Sets "cpu" to the id of the found CPU, or CPUSET_NOTINSET if no cpu
406 1.1 darran * could be found. (i.e. empty set)
407 1.1 darran */
408 1.1 darran #define CPUSET_FIND(set, cpu) { \
409 1.1 darran cpu = cpuset_find(&(set)); \
410 1.1 darran }
411 1.1 darran
412 1.1 darran /*
413 1.1 darran * Determine the smallest and largest CPU id in the set. Returns
414 1.1 darran * CPUSET_NOTINSET in smallest and largest when set is empty.
415 1.1 darran */
416 1.1 darran #define CPUSET_BOUNDS(set, smallest, largest) { \
417 1.1 darran cpuset_bounds(&(set), &(smallest), &(largest)); \
418 1.1 darran }
419 1.1 darran
420 1.1 darran /*
421 1.1 darran * Atomic cpuset operations
422 1.1 darran * These are safe to use for concurrent cpuset manipulations.
423 1.1 darran * "xdel" and "xadd" are exclusive operations, that set "result" to "0"
424 1.1 darran * if the add or del was successful, or "-1" if not successful.
425 1.1 darran * (e.g. attempting to add a cpu to a cpuset that's already there, or
426 1.1 darran * deleting a cpu that's not in the cpuset)
427 1.1 darran */
428 1.1 darran
429 1.1 darran #define CPUSET_ATOMIC_DEL(set, cpu) BT_ATOMIC_CLEAR((set).cpub, (cpu))
430 1.1 darran #define CPUSET_ATOMIC_ADD(set, cpu) BT_ATOMIC_SET((set).cpub, (cpu))
431 1.1 darran
432 1.1 darran #define CPUSET_ATOMIC_XADD(set, cpu, result) \
433 1.1 darran BT_ATOMIC_SET_EXCL((set).cpub, cpu, result)
434 1.1 darran
435 1.1 darran #define CPUSET_ATOMIC_XDEL(set, cpu, result) \
436 1.1 darran BT_ATOMIC_CLEAR_EXCL((set).cpub, cpu, result)
437 1.1 darran
438 1.1 darran
439 1.1 darran #define CPUSET_OR(set1, set2) { \
440 1.1 darran int _i; \
441 1.1 darran for (_i = 0; _i < CPUSET_WORDS; _i++) \
442 1.1 darran (set1).cpub[_i] |= (set2).cpub[_i]; \
443 1.1 darran }
444 1.1 darran
445 1.1 darran #define CPUSET_XOR(set1, set2) { \
446 1.1 darran int _i; \
447 1.1 darran for (_i = 0; _i < CPUSET_WORDS; _i++) \
448 1.1 darran (set1).cpub[_i] ^= (set2).cpub[_i]; \
449 1.1 darran }
450 1.1 darran
451 1.1 darran #define CPUSET_AND(set1, set2) { \
452 1.1 darran int _i; \
453 1.1 darran for (_i = 0; _i < CPUSET_WORDS; _i++) \
454 1.1 darran (set1).cpub[_i] &= (set2).cpub[_i]; \
455 1.1 darran }
456 1.1 darran
457 1.1 darran #define CPUSET_ZERO(set) { \
458 1.1 darran int _i; \
459 1.1 darran for (_i = 0; _i < CPUSET_WORDS; _i++) \
460 1.1 darran (set).cpub[_i] = 0; \
461 1.1 darran }
462 1.1 darran
463 1.1 darran #elif CPUSET_WORDS == 1
464 1.1 darran
465 1.1 darran typedef ulong_t cpuset_t; /* a set of CPUs */
466 1.1 darran
467 1.1 darran #define CPUSET(cpu) (1UL << (cpu))
468 1.1 darran
469 1.1 darran #define CPUSET_ALL(set) ((void)((set) = ~0UL))
470 1.1 darran #define CPUSET_ALL_BUT(set, cpu) ((void)((set) = ~CPUSET(cpu)))
471 1.1 darran #define CPUSET_ONLY(set, cpu) ((void)((set) = CPUSET(cpu)))
472 1.1 darran #define CPU_IN_SET(set, cpu) ((set) & CPUSET(cpu))
473 1.1 darran #define CPUSET_ADD(set, cpu) ((void)((set) |= CPUSET(cpu)))
474 1.1 darran #define CPUSET_DEL(set, cpu) ((void)((set) &= ~CPUSET(cpu)))
475 1.1 darran #define CPUSET_ISNULL(set) ((set) == 0)
476 1.1 darran #define CPUSET_ISEQUAL(set1, set2) ((set1) == (set2))
477 1.1 darran #define CPUSET_OR(set1, set2) ((void)((set1) |= (set2)))
478 1.1 darran #define CPUSET_XOR(set1, set2) ((void)((set1) ^= (set2)))
479 1.1 darran #define CPUSET_AND(set1, set2) ((void)((set1) &= (set2)))
480 1.1 darran #define CPUSET_ZERO(set) ((void)((set) = 0))
481 1.1 darran
482 1.1 darran #define CPUSET_FIND(set, cpu) { \
483 1.1 darran cpu = (uint_t)(lowbit(set) - 1); \
484 1.1 darran }
485 1.1 darran
486 1.1 darran #define CPUSET_BOUNDS(set, smallest, largest) { \
487 1.1 darran smallest = (uint_t)(lowbit(set) - 1); \
488 1.1 darran largest = (uint_t)(highbit(set) - 1); \
489 1.1 darran }
490 1.1 darran
491 1.1 darran #define CPUSET_ATOMIC_DEL(set, cpu) atomic_and_long(&(set), ~CPUSET(cpu))
492 1.1 darran #define CPUSET_ATOMIC_ADD(set, cpu) atomic_or_long(&(set), CPUSET(cpu))
493 1.1 darran
494 1.1 darran #define CPUSET_ATOMIC_XADD(set, cpu, result) \
495 1.1 darran { result = atomic_set_long_excl(&(set), (cpu)); }
496 1.1 darran
497 1.1 darran #define CPUSET_ATOMIC_XDEL(set, cpu, result) \
498 1.1 darran { result = atomic_clear_long_excl(&(set), (cpu)); }
499 1.1 darran
500 1.1 darran #else /* CPUSET_WORDS <= 0 */
501 1.1 darran
502 1.1 darran #error NCPU is undefined or invalid
503 1.1 darran
504 1.1 darran #endif /* CPUSET_WORDS */
505 1.1 darran
506 1.1 darran extern cpuset_t cpu_seqid_inuse;
507 1.1 darran
508 1.1 darran #endif /* (_KERNEL || _KMEMUSER) && _MACHDEP */
509 1.1 darran
510 1.1 darran #define CPU_CPR_OFFLINE 0x0
511 1.1 darran #define CPU_CPR_ONLINE 0x1
512 1.1 darran #define CPU_CPR_IS_OFFLINE(cpu) (((cpu)->cpu_cpr_flags & CPU_CPR_ONLINE) == 0)
513 1.1 darran #define CPU_CPR_IS_ONLINE(cpu) ((cpu)->cpu_cpr_flags & CPU_CPR_ONLINE)
514 1.1 darran #define CPU_SET_CPR_FLAGS(cpu, flag) ((cpu)->cpu_cpr_flags |= flag)
515 1.1 darran
516 1.1 darran #if defined(_KERNEL) || defined(_KMEMUSER)
517 1.1 darran
518 1.1 darran extern struct cpu *cpu[]; /* indexed by CPU number */
519 1.1 darran extern cpu_t *cpu_list; /* list of CPUs */
520 1.1 darran extern cpu_t *cpu_active; /* list of active CPUs */
521 1.1 darran extern int ncpus; /* number of CPUs present */
522 1.1 darran extern int ncpus_online; /* number of CPUs not quiesced */
523 1.1 darran extern int max_ncpus; /* max present before ncpus is known */
524 1.1 darran extern int boot_max_ncpus; /* like max_ncpus but for real */
525 1.1 darran extern int boot_ncpus; /* # cpus present @ boot */
526 1.1 darran extern processorid_t max_cpuid; /* maximum CPU number */
527 1.1 darran extern struct cpu *cpu_inmotion; /* offline or partition move target */
528 1.1 darran extern cpu_t *clock_cpu_list;
529 1.1 darran
530 1.1 darran #if defined(__i386) || defined(__amd64)
531 1.1 darran extern struct cpu *curcpup(void);
532 1.1 darran #define CPU (curcpup()) /* Pointer to current CPU */
533 1.1 darran #else
534 1.1 darran #define CPU (curthread->t_cpu) /* Pointer to current CPU */
535 1.1 darran #endif
536 1.1 darran
537 1.1 darran /*
538 1.1 darran * CPU_CURRENT indicates to thread_affinity_set to use CPU->cpu_id
539 1.1 darran * as the target and to grab cpu_lock instead of requiring the caller
540 1.1 darran * to grab it.
541 1.1 darran */
542 1.1 darran #define CPU_CURRENT -3
543 1.1 darran
544 1.1 darran /*
545 1.1 darran * Per-CPU statistics
546 1.1 darran *
547 1.1 darran * cpu_stats_t contains numerous system and VM-related statistics, in the form
548 1.1 darran * of gauges or monotonically-increasing event occurrence counts.
549 1.1 darran */
550 1.1 darran
551 1.1 darran #define CPU_STATS_ENTER_K() kpreempt_disable()
552 1.1 darran #define CPU_STATS_EXIT_K() kpreempt_enable()
553 1.1 darran
554 1.1 darran #define CPU_STATS_ADD_K(class, stat, amount) \
555 1.1 darran { kpreempt_disable(); /* keep from switching CPUs */\
556 1.1 darran CPU_STATS_ADDQ(CPU, class, stat, amount); \
557 1.1 darran kpreempt_enable(); \
558 1.1 darran }
559 1.1 darran
560 1.1 darran #define CPU_STATS_ADDQ(cp, class, stat, amount) { \
561 1.1 darran extern void __dtrace_probe___cpu_##class##info_##stat(uint_t, \
562 1.1 darran uint64_t *, cpu_t *); \
563 1.1 darran uint64_t *stataddr = &((cp)->cpu_stats.class.stat); \
564 1.1 darran __dtrace_probe___cpu_##class##info_##stat((amount), \
565 1.1 darran stataddr, cp); \
566 1.1 darran *(stataddr) += (amount); \
567 1.1 darran }
568 1.1 darran
569 1.1 darran #define CPU_STATS(cp, stat) \
570 1.1 darran ((cp)->cpu_stats.stat)
571 1.1 darran
572 1.1 darran #endif /* _KERNEL || _KMEMUSER */
573 1.1 darran
574 1.1 darran /*
575 1.1 darran * CPU support routines.
576 1.1 darran */
577 1.1 darran #if defined(_KERNEL) && defined(__STDC__) /* not for genassym.c */
578 1.1 darran
579 1.1 darran struct zone;
580 1.1 darran
581 1.1 darran void cpu_list_init(cpu_t *);
582 1.1 darran void cpu_add_unit(cpu_t *);
583 1.1 darran void cpu_del_unit(int cpuid);
584 1.1 darran void cpu_add_active(cpu_t *);
585 1.1 darran void cpu_kstat_init(cpu_t *);
586 1.1 darran void cpu_visibility_add(cpu_t *, struct zone *);
587 1.1 darran void cpu_visibility_remove(cpu_t *, struct zone *);
588 1.1 darran void cpu_visibility_configure(cpu_t *, struct zone *);
589 1.1 darran void cpu_visibility_unconfigure(cpu_t *, struct zone *);
590 1.1 darran void cpu_visibility_online(cpu_t *, struct zone *);
591 1.1 darran void cpu_visibility_offline(cpu_t *, struct zone *);
592 1.1 darran void cpu_create_intrstat(cpu_t *);
593 1.1 darran void cpu_delete_intrstat(cpu_t *);
594 1.1 darran int cpu_kstat_intrstat_update(kstat_t *, int);
595 1.1 darran void cpu_intr_swtch_enter(kthread_t *);
596 1.1 darran void cpu_intr_swtch_exit(kthread_t *);
597 1.1 darran
598 1.1 darran void mbox_lock_init(void); /* initialize cross-call locks */
599 1.1 darran void mbox_init(int cpun); /* initialize cross-calls */
600 1.1 darran void poke_cpu(int cpun); /* interrupt another CPU (to preempt) */
601 1.1 darran
602 1.1 darran /*
603 1.1 darran * values for safe_list. Pause state that CPUs are in.
604 1.1 darran */
605 1.1 darran #define PAUSE_IDLE 0 /* normal state */
606 1.1 darran #define PAUSE_READY 1 /* paused thread ready to spl */
607 1.1 darran #define PAUSE_WAIT 2 /* paused thread is spl-ed high */
608 1.1 darran #define PAUSE_DIE 3 /* tell pause thread to leave */
609 1.1 darran #define PAUSE_DEAD 4 /* pause thread has left */
610 1.1 darran
611 1.1 darran void mach_cpu_pause(volatile char *);
612 1.1 darran
613 1.1 darran void pause_cpus(cpu_t *off_cp);
614 1.1 darran void start_cpus(void);
615 1.1 darran int cpus_paused(void);
616 1.1 darran
617 1.1 darran void cpu_pause_init(void);
618 1.1 darran cpu_t *cpu_get(processorid_t cpun); /* get the CPU struct associated */
619 1.1 darran
620 1.1 darran int cpu_online(cpu_t *cp); /* take cpu online */
621 1.1 darran int cpu_offline(cpu_t *cp, int flags); /* take cpu offline */
622 1.1 darran int cpu_spare(cpu_t *cp, int flags); /* take cpu to spare */
623 1.1 darran int cpu_faulted(cpu_t *cp, int flags); /* take cpu to faulted */
624 1.1 darran int cpu_poweron(cpu_t *cp); /* take powered-off cpu to offline */
625 1.1 darran int cpu_poweroff(cpu_t *cp); /* take offline cpu to powered-off */
626 1.1 darran
627 1.1 darran cpu_t *cpu_intr_next(cpu_t *cp); /* get next online CPU taking intrs */
628 1.1 darran int cpu_intr_count(cpu_t *cp); /* count # of CPUs handling intrs */
629 1.1 darran int cpu_intr_on(cpu_t *cp); /* CPU taking I/O interrupts? */
630 1.1 darran void cpu_intr_enable(cpu_t *cp); /* enable I/O interrupts */
631 1.1 darran int cpu_intr_disable(cpu_t *cp); /* disable I/O interrupts */
632 1.1 darran void cpu_intr_alloc(cpu_t *cp, int n); /* allocate interrupt threads */
633 1.1 darran
634 1.1 darran /*
635 1.1 darran * Routines for checking CPU states.
636 1.1 darran */
637 1.1 darran int cpu_is_online(cpu_t *); /* check if CPU is online */
638 1.1 darran int cpu_is_nointr(cpu_t *); /* check if CPU can service intrs */
639 1.1 darran int cpu_is_active(cpu_t *); /* check if CPU can run threads */
640 1.1 darran int cpu_is_offline(cpu_t *); /* check if CPU is offline */
641 1.1 darran int cpu_is_poweredoff(cpu_t *); /* check if CPU is powered off */
642 1.1 darran
643 1.1 darran int cpu_flagged_online(cpu_flag_t); /* flags show CPU is online */
644 1.1 darran int cpu_flagged_nointr(cpu_flag_t); /* flags show CPU not handling intrs */
645 1.1 darran int cpu_flagged_active(cpu_flag_t); /* flags show CPU scheduling threads */
646 1.1 darran int cpu_flagged_offline(cpu_flag_t); /* flags show CPU is offline */
647 1.1 darran int cpu_flagged_poweredoff(cpu_flag_t); /* flags show CPU is powered off */
648 1.1 darran
649 1.1 darran /*
650 1.1 darran * The processor_info(2) state of a CPU is a simplified representation suitable
651 1.1 darran * for use by an application program. Kernel subsystems should utilize the
652 1.1 darran * internal per-CPU state as given by the cpu_flags member of the cpu structure,
653 1.1 darran * as this information may include platform- or architecture-specific state
654 1.1 darran * critical to a subsystem's disposition of a particular CPU.
655 1.1 darran */
656 1.1 darran void cpu_set_state(cpu_t *); /* record/timestamp current state */
657 1.1 darran int cpu_get_state(cpu_t *); /* get current cpu state */
658 1.1 darran const char *cpu_get_state_str(cpu_t *); /* get current cpu state as string */
659 1.1 darran
660 1.1 darran
661 1.1 darran void cpu_set_supp_freqs(cpu_t *, const char *); /* set the CPU supported */
662 1.1 darran /* frequencies */
663 1.1 darran
664 1.1 darran int cpu_configure(int);
665 1.1 darran int cpu_unconfigure(int);
666 1.1 darran void cpu_destroy_bound_threads(cpu_t *cp);
667 1.1 darran
668 1.1 darran extern int cpu_bind_thread(kthread_t *tp, processorid_t bind,
669 1.1 darran processorid_t *obind, int *error);
670 1.1 darran extern int cpu_unbind(processorid_t cpu_id, boolean_t force);
671 1.1 darran extern void thread_affinity_set(kthread_t *t, int cpu_id);
672 1.1 darran extern void thread_affinity_clear(kthread_t *t);
673 1.1 darran extern void affinity_set(int cpu_id);
674 1.1 darran extern void affinity_clear(void);
675 1.1 darran extern void init_cpu_mstate(struct cpu *, int);
676 1.1 darran extern void term_cpu_mstate(struct cpu *);
677 1.1 darran extern void new_cpu_mstate(int, hrtime_t);
678 1.1 darran extern void get_cpu_mstate(struct cpu *, hrtime_t *);
679 1.1 darran extern void thread_nomigrate(void);
680 1.1 darran extern void thread_allowmigrate(void);
681 1.1 darran extern void weakbinding_stop(void);
682 1.1 darran extern void weakbinding_start(void);
683 1.1 darran
684 1.1 darran /*
685 1.1 darran * The following routines affect the CPUs participation in interrupt processing,
686 1.1 darran * if that is applicable on the architecture. This only affects interrupts
687 1.1 darran * which aren't directed at the processor (not cross calls).
688 1.1 darran *
689 1.1 darran * cpu_disable_intr returns non-zero if interrupts were previously enabled.
690 1.1 darran */
691 1.1 darran int cpu_disable_intr(struct cpu *cp); /* stop issuing interrupts to cpu */
692 1.1 darran void cpu_enable_intr(struct cpu *cp); /* start issuing interrupts to cpu */
693 1.1 darran
694 1.1 darran /*
695 1.1 darran * The mutex cpu_lock protects cpu_flags for all CPUs, as well as the ncpus
696 1.1 darran * and ncpus_online counts.
697 1.1 darran */
698 1.1 darran extern kmutex_t cpu_lock; /* lock protecting CPU data */
699 1.1 darran
700 1.1 darran typedef enum {
701 1.1 darran CPU_INIT,
702 1.1 darran CPU_CONFIG,
703 1.1 darran CPU_UNCONFIG,
704 1.1 darran CPU_ON,
705 1.1 darran CPU_OFF,
706 1.1 darran CPU_CPUPART_IN,
707 1.1 darran CPU_CPUPART_OUT
708 1.1 darran } cpu_setup_t;
709 1.1 darran
710 1.1 darran typedef int cpu_setup_func_t(cpu_setup_t, int, void *);
711 1.1 darran
712 1.1 darran /*
713 1.1 darran * Routines used to register interest in cpu's being added to or removed
714 1.1 darran * from the system.
715 1.1 darran */
716 1.1 darran extern void register_cpu_setup_func(cpu_setup_func_t *, void *);
717 1.1 darran extern void unregister_cpu_setup_func(cpu_setup_func_t *, void *);
718 1.1 darran extern void cpu_state_change_notify(int, cpu_setup_t);
719 1.1 darran
720 1.1 darran /*
721 1.1 darran * Create various strings that describe the given CPU for the
722 1.1 darran * processor_info system call and configuration-related kstats.
723 1.1 darran */
724 1.1 darran #define CPU_IDSTRLEN 100
725 1.1 darran
726 1.1 darran extern void init_cpu_info(struct cpu *);
727 1.1 darran extern void populate_idstr(struct cpu *);
728 1.1 darran extern void cpu_vm_data_init(struct cpu *);
729 1.1 darran extern void cpu_vm_data_destroy(struct cpu *);
730 1.1 darran
731 1.1 darran #endif /* _KERNEL */
732 1.1 darran
733 1.1 darran #ifdef __cplusplus
734 1.1 darran }
735 1.1 darran #endif
736 1.1 darran
737 1.1 darran #endif /* _SYS_CPUVAR_H */
738