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