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