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