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