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