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