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