xref: /illumos-gate/usr/src/uts/common/sys/cpuvar.h (revision 2850d85b7b93f31e578520dc3b3feb24db609c62)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52b616c6cSwesolows  * Common Development and Distribution License (the "License").
62b616c6cSwesolows  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212b616c6cSwesolows 
227c478bd9Sstevel@tonic-gate /*
23*2850d85bSmv  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _SYS_CPUVAR_H
287c478bd9Sstevel@tonic-gate #define	_SYS_CPUVAR_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/thread.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>	/* has cpu_stat_t definition */
347c478bd9Sstevel@tonic-gate #include <sys/disp.h>
357c478bd9Sstevel@tonic-gate #include <sys/processor.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #if (defined(_KERNEL) || defined(_KMEMUSER)) && defined(_MACHDEP)
387c478bd9Sstevel@tonic-gate #include <sys/machcpuvar.h>
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
447c478bd9Sstevel@tonic-gate #include <sys/rwlock.h>
457c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
462b616c6cSwesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && defined(_KERNEL) && \
472b616c6cSwesolows 	(defined(__i386) || defined(__amd64))
487c478bd9Sstevel@tonic-gate #include <asm/cpuvar.h>
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
527c478bd9Sstevel@tonic-gate extern "C" {
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate struct squeue_set_s;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	CPU_CACHE_COHERENCE_SIZE	64
587c478bd9Sstevel@tonic-gate #define	S_LOADAVG_SZ	11
597c478bd9Sstevel@tonic-gate #define	S_MOVAVG_SZ	10
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate struct loadavg_s {
627c478bd9Sstevel@tonic-gate 	int lg_cur;		/* current loadavg entry */
637c478bd9Sstevel@tonic-gate 	unsigned int lg_len;	/* number entries recorded */
647c478bd9Sstevel@tonic-gate 	hrtime_t lg_total;	/* used to temporarily hold load totals */
657c478bd9Sstevel@tonic-gate 	hrtime_t lg_loads[S_LOADAVG_SZ];	/* table of recorded entries */
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * For fast event tracing.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate struct ftrace_record;
727c478bd9Sstevel@tonic-gate typedef struct ftrace_data {
737c478bd9Sstevel@tonic-gate 	int			ftd_state;	/* ftrace flags */
744df4bd60Sbs 	kmutex_t		ftd_unused;	/* ftrace buffer lock, unused */
757c478bd9Sstevel@tonic-gate 	struct ftrace_record	*ftd_cur;	/* current record */
767c478bd9Sstevel@tonic-gate 	struct ftrace_record	*ftd_first;	/* first record */
777c478bd9Sstevel@tonic-gate 	struct ftrace_record	*ftd_last;	/* last record */
787c478bd9Sstevel@tonic-gate } ftrace_data_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate struct cyc_cpu;
817c478bd9Sstevel@tonic-gate struct nvlist;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Per-CPU data.
85e824d57fSjohnlev  *
86e824d57fSjohnlev  * Be careful adding new members: if they are not the same in all modules (e.g.
87e824d57fSjohnlev  * change size depending on a #define), CTF uniquification can fail to work
88e824d57fSjohnlev  * properly.  Furthermore, this is transitive in that it applies recursively to
89e824d57fSjohnlev  * all types pointed to by cpu_t.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate typedef struct cpu {
927c478bd9Sstevel@tonic-gate 	processorid_t	cpu_id;			/* CPU number */
937c478bd9Sstevel@tonic-gate 	processorid_t	cpu_seqid;	/* sequential CPU id (0..ncpus-1) */
947c478bd9Sstevel@tonic-gate 	volatile cpu_flag_t cpu_flags;		/* flags indicating CPU state */
957c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_self;		/* pointer to itself */
967c478bd9Sstevel@tonic-gate 	kthread_t	*cpu_thread;		/* current thread */
977c478bd9Sstevel@tonic-gate 	kthread_t	*cpu_idle_thread;	/* idle thread for this CPU */
987c478bd9Sstevel@tonic-gate 	kthread_t	*cpu_pause_thread;	/* pause thread for this CPU */
997c478bd9Sstevel@tonic-gate 	klwp_id_t	cpu_lwp;		/* current lwp (if any) */
1007c478bd9Sstevel@tonic-gate 	klwp_id_t	cpu_fpowner;		/* currently loaded fpu owner */
1017c478bd9Sstevel@tonic-gate 	struct cpupart	*cpu_part;		/* partition with this CPU */
1027c478bd9Sstevel@tonic-gate 	struct lgrp_ld	*cpu_lpl;		/* pointer to this cpu's load */
1037c478bd9Sstevel@tonic-gate 	int		cpu_cache_offset;	/* see kmem.c for details */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * Links to other CPUs.  It is safe to walk these lists if
1077c478bd9Sstevel@tonic-gate 	 * one of the following is true:
1087c478bd9Sstevel@tonic-gate 	 * 	- cpu_lock held
1097c478bd9Sstevel@tonic-gate 	 * 	- preemption disabled via kpreempt_disable
1107c478bd9Sstevel@tonic-gate 	 * 	- PIL >= DISP_LEVEL
1117c478bd9Sstevel@tonic-gate 	 * 	- acting thread is an interrupt thread
1127c478bd9Sstevel@tonic-gate 	 * 	- all other CPUs are paused
1137c478bd9Sstevel@tonic-gate 	 */
1147c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_next;		/* next existing CPU */
1157c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_prev;		/* prev existing CPU */
1167c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_next_onln;		/* next online (enabled) CPU */
1177c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_prev_onln;		/* prev online (enabled) CPU */
1187c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_next_part;		/* next CPU in partition */
1197c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_prev_part;		/* prev CPU in partition */
1207c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_next_lgrp;		/* next CPU in latency group */
1217c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_prev_lgrp;		/* prev CPU in latency group */
1227c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_next_lpl;		/* next CPU in lgrp partition */
1237c478bd9Sstevel@tonic-gate 	struct cpu	*cpu_prev_lpl;
124fb2f18f8Sesaxe 
125fb2f18f8Sesaxe 	struct cpu_pg	*cpu_pg;		/* cpu's processor groups */
126fb2f18f8Sesaxe 
1277c478bd9Sstevel@tonic-gate 	void		*cpu_reserved[4];	/* reserved for future use */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Scheduling variables.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	disp_t		*cpu_disp;		/* dispatch queue data */
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * Note that cpu_disp is set before the CPU is added to the system
1357c478bd9Sstevel@tonic-gate 	 * and is never modified.  Hence, no additional locking is needed
1367c478bd9Sstevel@tonic-gate 	 * beyond what's necessary to access the cpu_t structure.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	char		cpu_runrun;	/* scheduling flag - set to preempt */
1397c478bd9Sstevel@tonic-gate 	char		cpu_kprunrun;		/* force kernel preemption */
1407c478bd9Sstevel@tonic-gate 	pri_t		cpu_chosen_level; 	/* priority at which cpu */
1417c478bd9Sstevel@tonic-gate 						/* was chosen for scheduling */
1427c478bd9Sstevel@tonic-gate 	kthread_t	*cpu_dispthread; /* thread selected for dispatch */
1437c478bd9Sstevel@tonic-gate 	disp_lock_t	cpu_thread_lock; /* dispatcher lock on current thread */
1447c478bd9Sstevel@tonic-gate 	uint8_t		cpu_disp_flags;	/* flags used by dispatcher */
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 * The following field is updated when ever the cpu_dispthread
1477c478bd9Sstevel@tonic-gate 	 * changes. Also in places, where the current thread(cpu_dispthread)
1487c478bd9Sstevel@tonic-gate 	 * priority changes. This is used in disp_lowpri_cpu()
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	pri_t		cpu_dispatch_pri; /* priority of cpu_dispthread */
1517c478bd9Sstevel@tonic-gate 	clock_t		cpu_last_swtch;	/* last time switched to new thread */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * Interrupt data.
1557c478bd9Sstevel@tonic-gate 	 */
1567c478bd9Sstevel@tonic-gate 	caddr_t		cpu_intr_stack;	/* interrupt stack */
1577c478bd9Sstevel@tonic-gate 	kthread_t	*cpu_intr_thread; /* interrupt thread list */
1587c478bd9Sstevel@tonic-gate 	uint_t		cpu_intr_actv;	/* interrupt levels active (bitmask) */
1597c478bd9Sstevel@tonic-gate 	int		cpu_base_spl;	/* priority for highest rupt active */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * Statistics.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	cpu_stats_t	cpu_stats;		/* per-CPU statistics */
1657c478bd9Sstevel@tonic-gate 	struct kstat	*cpu_info_kstat;	/* kstat for cpu info */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	uintptr_t	cpu_profile_pc;	/* kernel PC in profile interrupt */
1687c478bd9Sstevel@tonic-gate 	uintptr_t	cpu_profile_upc; /* user PC in profile interrupt */
1697c478bd9Sstevel@tonic-gate 	uintptr_t	cpu_profile_pil; /* PIL when profile interrupted */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	ftrace_data_t	cpu_ftrace;		/* per cpu ftrace data */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	clock_t		cpu_deadman_lbolt;	/* used by deadman() */
1747c478bd9Sstevel@tonic-gate 	uint_t		cpu_deadman_countdown;	/* used by deadman() */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	kmutex_t	cpu_cpc_ctxlock; /* protects context for idle thread */
1777c478bd9Sstevel@tonic-gate 	kcpc_ctx_t	*cpu_cpc_ctx;	/* performance counter context */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * Configuration information for the processor_info system call.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	processor_info_t cpu_type_info;	/* config info */
1837c478bd9Sstevel@tonic-gate 	time_t		cpu_state_begin; /* when CPU entered current state */
1847c478bd9Sstevel@tonic-gate 	char		cpu_cpr_flags;	/* CPR related info */
1857c478bd9Sstevel@tonic-gate 	struct cyc_cpu	*cpu_cyclic;	/* per cpu cyclic subsystem data */
1867c478bd9Sstevel@tonic-gate 	struct squeue_set_s *cpu_squeue_set;	/* per cpu squeue set */
1877c478bd9Sstevel@tonic-gate 	struct nvlist	*cpu_props;	/* pool-related properties */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	krwlock_t	cpu_ft_lock;		/* DTrace: fasttrap lock */
1907c478bd9Sstevel@tonic-gate 	uintptr_t	cpu_dtrace_caller;	/* DTrace: caller, if any */
1917c478bd9Sstevel@tonic-gate 	hrtime_t	cpu_dtrace_chillmark;	/* DTrace: chill mark time */
1927c478bd9Sstevel@tonic-gate 	hrtime_t	cpu_dtrace_chilled;	/* DTrace: total chill time */
1939102d475Sesolom 	volatile uint16_t cpu_mstate;		/* cpu microstate */
1949102d475Sesolom 	volatile uint16_t cpu_mstate_gen;	/* generation counter */
1959102d475Sesolom 	volatile hrtime_t cpu_mstate_start;	/* cpu microstate start time */
1969102d475Sesolom 	volatile hrtime_t cpu_acct[NCMSTATES];	/* cpu microstate data */
197eda89462Sesolom 	hrtime_t	cpu_intracct[NCMSTATES]; /* interrupt mstate data */
1987c478bd9Sstevel@tonic-gate 	hrtime_t	cpu_waitrq;		/* cpu run-queue wait time */
1997c478bd9Sstevel@tonic-gate 	struct loadavg_s cpu_loadavg;		/* loadavg info for this cpu */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	char		*cpu_idstr;	/* for printing and debugging */
2027c478bd9Sstevel@tonic-gate 	char		*cpu_brandstr;	/* for printing */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * Sum of all device interrupt weights that are currently directed at
2067c478bd9Sstevel@tonic-gate 	 * this cpu. Cleared at start of interrupt redistribution.
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	int32_t		cpu_intr_weight;
209affbd3ccSkchow 	void		*cpu_vm_data;
2107c478bd9Sstevel@tonic-gate 
211ab761399Sesaxe 	struct cpu_physid *cpu_physid;	/* physical associations */
212ab761399Sesaxe 
213cf74e62bSmh 	uint64_t	cpu_curr_clock;		/* current clock freq in Hz */
214cf74e62bSmh 	char		*cpu_supp_freqs;	/* supported freqs in Hz */
215cf74e62bSmh 
2163aedfe0bSmishra 	/*
2173aedfe0bSmishra 	 * Interrupt load factor used by dispatcher & softcall
2183aedfe0bSmishra 	 */
2193aedfe0bSmishra 	hrtime_t	cpu_intrlast;   /* total interrupt time (nsec) */
2203aedfe0bSmishra 	int		cpu_intrload;   /* interrupt load factor (0-99%) */
2213aedfe0bSmishra 
2227c478bd9Sstevel@tonic-gate 	/*
223e824d57fSjohnlev 	 * New members must be added /before/ this member, as the CTF tools
224e824d57fSjohnlev 	 * rely on this being the last field before cpu_m, so they can
225e824d57fSjohnlev 	 * correctly calculate the offset when synthetically adding the cpu_m
226e824d57fSjohnlev 	 * member in objects that do not have it.  This fixup is required for
227e824d57fSjohnlev 	 * uniquification to work correctly.
2287c478bd9Sstevel@tonic-gate 	 */
229e824d57fSjohnlev 	uintptr_t	cpu_m_pad;
230e824d57fSjohnlev 
231e824d57fSjohnlev #if (defined(_KERNEL) || defined(_KMEMUSER)) && defined(_MACHDEP)
2327c478bd9Sstevel@tonic-gate 	struct machcpu	cpu_m;		/* per architecture info */
2337c478bd9Sstevel@tonic-gate #endif
2347c478bd9Sstevel@tonic-gate } cpu_t;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * The cpu_core structure consists of per-CPU state available in any context.
2387c478bd9Sstevel@tonic-gate  * On some architectures, this may mean that the page(s) containing the
2397c478bd9Sstevel@tonic-gate  * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
2407c478bd9Sstevel@tonic-gate  * is up to the platform to assure that this is performed properly.  Note that
2417c478bd9Sstevel@tonic-gate  * the structure is sized to avoid false sharing.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate #define	CPUC_SIZE		(sizeof (uint16_t) + sizeof (uintptr_t) + \
2447c478bd9Sstevel@tonic-gate 				sizeof (kmutex_t))
2457c478bd9Sstevel@tonic-gate #define	CPUC_PADSIZE		CPU_CACHE_COHERENCE_SIZE - CPUC_SIZE
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate typedef struct cpu_core {
2487c478bd9Sstevel@tonic-gate 	uint16_t	cpuc_dtrace_flags;	/* DTrace flags */
2497c478bd9Sstevel@tonic-gate 	uint8_t		cpuc_pad[CPUC_PADSIZE];	/* padding */
2507c478bd9Sstevel@tonic-gate 	uintptr_t	cpuc_dtrace_illval;	/* DTrace illegal value */
2517c478bd9Sstevel@tonic-gate 	kmutex_t	cpuc_pid_lock;		/* DTrace pid provider lock */
2527c478bd9Sstevel@tonic-gate } cpu_core_t;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #ifdef _KERNEL
2557c478bd9Sstevel@tonic-gate extern cpu_core_t cpu_core[];
2567c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * CPU_ON_INTR() macro. Returns non-zero if currently on interrupt stack.
2607c478bd9Sstevel@tonic-gate  * Note that this isn't a test for a high PIL.  For example, cpu_intr_actv
2617c478bd9Sstevel@tonic-gate  * does not get updated when we go through sys_trap from TL>0 at high PIL.
2627c478bd9Sstevel@tonic-gate  * getpil() should be used instead to check for PIL levels.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate #define	CPU_ON_INTR(cpup) ((cpup)->cpu_intr_actv >> (LOCK_LEVEL + 1))
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate #if defined(_KERNEL) || defined(_KMEMUSER)
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate #define	INTR_STACK_SIZE	MAX(DEFAULTSTKSZ, PAGESIZE)
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /* MEMBERS PROTECTED BY "atomicity": cpu_flags */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * Flags in the CPU structure.
2747c478bd9Sstevel@tonic-gate  *
2757c478bd9Sstevel@tonic-gate  * These are protected by cpu_lock (except during creation).
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  * Offlined-CPUs have three stages of being offline:
2787c478bd9Sstevel@tonic-gate  *
2797c478bd9Sstevel@tonic-gate  * CPU_ENABLE indicates that the CPU is participating in I/O interrupts
2807c478bd9Sstevel@tonic-gate  * that can be directed at a number of different CPUs.  If CPU_ENABLE
2817c478bd9Sstevel@tonic-gate  * is off, the CPU will not be given interrupts that can be sent elsewhere,
2827c478bd9Sstevel@tonic-gate  * but will still get interrupts from devices associated with that CPU only,
2837c478bd9Sstevel@tonic-gate  * and from other CPUs.
2847c478bd9Sstevel@tonic-gate  *
2857c478bd9Sstevel@tonic-gate  * CPU_OFFLINE indicates that the dispatcher should not allow any threads
2867c478bd9Sstevel@tonic-gate  * other than interrupt threads to run on that CPU.  A CPU will not have
2877c478bd9Sstevel@tonic-gate  * CPU_OFFLINE set if there are any bound threads (besides interrupts).
2887c478bd9Sstevel@tonic-gate  *
2897c478bd9Sstevel@tonic-gate  * CPU_QUIESCED is set if p_offline was able to completely turn idle the
2907c478bd9Sstevel@tonic-gate  * CPU and it will not have to run interrupt threads.  In this case it'll
2917c478bd9Sstevel@tonic-gate  * stay in the idle loop until CPU_QUIESCED is turned off.
2927c478bd9Sstevel@tonic-gate  *
2937c478bd9Sstevel@tonic-gate  * CPU_FROZEN is used only by CPR to mark CPUs that have been successfully
2947c478bd9Sstevel@tonic-gate  * suspended (in the suspend path), or have yet to be resumed (in the resume
2957c478bd9Sstevel@tonic-gate  * case).
2967c478bd9Sstevel@tonic-gate  *
2977c478bd9Sstevel@tonic-gate  * On some platforms CPUs can be individually powered off.
2987c478bd9Sstevel@tonic-gate  * The following flags are set for powered off CPUs: CPU_QUIESCED,
2997c478bd9Sstevel@tonic-gate  * CPU_OFFLINE, and CPU_POWEROFF.  The following flags are cleared:
3007c478bd9Sstevel@tonic-gate  * CPU_RUNNING, CPU_READY, CPU_EXISTS, and CPU_ENABLE.
3017c478bd9Sstevel@tonic-gate  */
3027c478bd9Sstevel@tonic-gate #define	CPU_RUNNING	0x001		/* CPU running */
3037c478bd9Sstevel@tonic-gate #define	CPU_READY	0x002		/* CPU ready for cross-calls */
3047c478bd9Sstevel@tonic-gate #define	CPU_QUIESCED	0x004		/* CPU will stay in idle */
3057c478bd9Sstevel@tonic-gate #define	CPU_EXISTS	0x008		/* CPU is configured */
3067c478bd9Sstevel@tonic-gate #define	CPU_ENABLE	0x010		/* CPU enabled for interrupts */
3077c478bd9Sstevel@tonic-gate #define	CPU_OFFLINE	0x020		/* CPU offline via p_online */
3087c478bd9Sstevel@tonic-gate #define	CPU_POWEROFF	0x040		/* CPU is powered off */
3097c478bd9Sstevel@tonic-gate #define	CPU_FROZEN	0x080		/* CPU is frozen via CPR suspend */
3107c478bd9Sstevel@tonic-gate #define	CPU_SPARE	0x100		/* CPU offline available for use */
3117c478bd9Sstevel@tonic-gate #define	CPU_FAULTED	0x200		/* CPU offline diagnosed faulty */
3127c478bd9Sstevel@tonic-gate 
313ae115bc7Smrj #define	FMT_CPU_FLAGS							\
314ae115bc7Smrj 	"\20\12fault\11spare\10frozen"					\
315ae115bc7Smrj 	"\7poweroff\6offline\5enable\4exist\3quiesced\2ready\1run"
316ae115bc7Smrj 
3177c478bd9Sstevel@tonic-gate #define	CPU_ACTIVE(cpu)	(((cpu)->cpu_flags & CPU_OFFLINE) == 0)
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * Flags for cpu_offline(), cpu_faulted(), and cpu_spare().
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate #define	CPU_FORCED	0x0001		/* Force CPU offline */
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * DTrace flags.
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_NOFAULT	0x0001	/* Don't fault */
3287c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_DROP		0x0002	/* Drop this ECB */
3297c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_BADADDR	0x0004	/* DTrace fault: bad address */
3307c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_BADALIGN	0x0008	/* DTrace fault: bad alignment */
3317c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_DIVZERO	0x0010	/* DTrace fault: divide by zero */
3327c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_ILLOP	0x0020	/* DTrace fault: illegal operation */
3337c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_NOSCRATCH	0x0040	/* DTrace fault: out of scratch */
3347c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_KPRIV	0x0080	/* DTrace fault: bad kernel access */
3357c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_UPRIV	0x0100	/* DTrace fault: bad user access */
3367c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_TUPOFLOW	0x0200	/* DTrace fault: tuple stack overflow */
3377c478bd9Sstevel@tonic-gate #if defined(__sparc)
3387c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_FAKERESTORE	0x0400	/* pid provider hint to getreg */
3397c478bd9Sstevel@tonic-gate #endif
3407c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_ENTRY	0x0800	/* pid provider hint to ustack() */
341b8fac8e1Sjhaslam #define	CPU_DTRACE_BADSTACK	0x1000	/* DTrace fault: bad stack */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_FAULT	(CPU_DTRACE_BADADDR | CPU_DTRACE_BADALIGN | \
3447c478bd9Sstevel@tonic-gate 				CPU_DTRACE_DIVZERO | CPU_DTRACE_ILLOP | \
3457c478bd9Sstevel@tonic-gate 				CPU_DTRACE_NOSCRATCH | CPU_DTRACE_KPRIV | \
346b8fac8e1Sjhaslam 				CPU_DTRACE_UPRIV | CPU_DTRACE_TUPOFLOW | \
347b8fac8e1Sjhaslam 				CPU_DTRACE_BADSTACK)
3487c478bd9Sstevel@tonic-gate #define	CPU_DTRACE_ERROR	(CPU_DTRACE_FAULT | CPU_DTRACE_DROP)
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * Dispatcher flags
3527c478bd9Sstevel@tonic-gate  * These flags must be changed only by the current CPU.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate #define	CPU_DISP_DONTSTEAL	0x01	/* CPU undergoing context swtch */
3557c478bd9Sstevel@tonic-gate #define	CPU_DISP_HALTED		0x02	/* CPU halted waiting for interrupt */
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate #endif /* _KERNEL || _KMEMUSER */
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate #if (defined(_KERNEL) || defined(_KMEMUSER)) && defined(_MACHDEP)
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * Macros for manipulating sets of CPUs as a bitmap.  Note that this
3647c478bd9Sstevel@tonic-gate  * bitmap may vary in size depending on the maximum CPU id a specific
3657c478bd9Sstevel@tonic-gate  * platform supports.  This may be different than the number of CPUs
3667c478bd9Sstevel@tonic-gate  * the platform supports, since CPU ids can be sparse.  We define two
3677c478bd9Sstevel@tonic-gate  * sets of macros; one for platforms where the maximum CPU id is less
3687c478bd9Sstevel@tonic-gate  * than the number of bits in a single word (32 in a 32-bit kernel,
3697c478bd9Sstevel@tonic-gate  * 64 in a 64-bit kernel), and one for platforms that require bitmaps
3707c478bd9Sstevel@tonic-gate  * of more than one word.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate #define	CPUSET_WORDS	BT_BITOUL(NCPU)
3747c478bd9Sstevel@tonic-gate #define	CPUSET_NOTINSET	((uint_t)-1)
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate #if	CPUSET_WORDS > 1
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate typedef struct cpuset {
3797c478bd9Sstevel@tonic-gate 	ulong_t	cpub[CPUSET_WORDS];
3807c478bd9Sstevel@tonic-gate } cpuset_t;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * Private functions for manipulating cpusets that do not fit in a
3847c478bd9Sstevel@tonic-gate  * single word.  These should not be used directly; instead the
3857c478bd9Sstevel@tonic-gate  * CPUSET_* macros should be used so the code will be portable
3867c478bd9Sstevel@tonic-gate  * across different definitions of NCPU.
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate extern	void	cpuset_all(cpuset_t *);
3897c478bd9Sstevel@tonic-gate extern	void	cpuset_all_but(cpuset_t *, uint_t);
3907c478bd9Sstevel@tonic-gate extern	int	cpuset_isnull(cpuset_t *);
3917c478bd9Sstevel@tonic-gate extern	int	cpuset_cmp(cpuset_t *, cpuset_t *);
3927c478bd9Sstevel@tonic-gate extern	void	cpuset_only(cpuset_t *, uint_t);
3937c478bd9Sstevel@tonic-gate extern	uint_t	cpuset_find(cpuset_t *);
39400423197Sha extern	void	cpuset_bounds(cpuset_t *, uint_t *, uint_t *);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate #define	CPUSET_ALL(set)			cpuset_all(&(set))
3977c478bd9Sstevel@tonic-gate #define	CPUSET_ALL_BUT(set, cpu)	cpuset_all_but(&(set), cpu)
3987c478bd9Sstevel@tonic-gate #define	CPUSET_ONLY(set, cpu)		cpuset_only(&(set), cpu)
3997c478bd9Sstevel@tonic-gate #define	CPU_IN_SET(set, cpu)		BT_TEST((set).cpub, cpu)
4007c478bd9Sstevel@tonic-gate #define	CPUSET_ADD(set, cpu)		BT_SET((set).cpub, cpu)
4017c478bd9Sstevel@tonic-gate #define	CPUSET_DEL(set, cpu)		BT_CLEAR((set).cpub, cpu)
4027c478bd9Sstevel@tonic-gate #define	CPUSET_ISNULL(set)		cpuset_isnull(&(set))
4037c478bd9Sstevel@tonic-gate #define	CPUSET_ISEQUAL(set1, set2)	cpuset_cmp(&(set1), &(set2))
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * Find one CPU in the cpuset.
4077c478bd9Sstevel@tonic-gate  * Sets "cpu" to the id of the found CPU, or CPUSET_NOTINSET if no cpu
4087c478bd9Sstevel@tonic-gate  * could be found. (i.e. empty set)
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate #define	CPUSET_FIND(set, cpu)		{		\
4117c478bd9Sstevel@tonic-gate 	cpu = cpuset_find(&(set));			\
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
41400423197Sha /*
41500423197Sha  * Determine the smallest and largest CPU id in the set. Returns
41600423197Sha  * CPUSET_NOTINSET in smallest and largest when set is empty.
41700423197Sha  */
41800423197Sha #define	CPUSET_BOUNDS(set, smallest, largest)	{		\
41900423197Sha 	cpuset_bounds(&(set), &(smallest), &(largest));		\
42000423197Sha }
42100423197Sha 
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate  * Atomic cpuset operations
4247c478bd9Sstevel@tonic-gate  * These are safe to use for concurrent cpuset manipulations.
4257c478bd9Sstevel@tonic-gate  * "xdel" and "xadd" are exclusive operations, that set "result" to "0"
4267c478bd9Sstevel@tonic-gate  * if the add or del was successful, or "-1" if not successful.
4277c478bd9Sstevel@tonic-gate  * (e.g. attempting to add a cpu to a cpuset that's already there, or
4287c478bd9Sstevel@tonic-gate  * deleting a cpu that's not in the cpuset)
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate #define	CPUSET_ATOMIC_DEL(set, cpu)	BT_ATOMIC_CLEAR((set).cpub, (cpu))
4327c478bd9Sstevel@tonic-gate #define	CPUSET_ATOMIC_ADD(set, cpu)	BT_ATOMIC_SET((set).cpub, (cpu))
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate #define	CPUSET_ATOMIC_XADD(set, cpu, result) \
4357c478bd9Sstevel@tonic-gate 	BT_ATOMIC_SET_EXCL((set).cpub, cpu, result)
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate #define	CPUSET_ATOMIC_XDEL(set, cpu, result) \
4387c478bd9Sstevel@tonic-gate 	BT_ATOMIC_CLEAR_EXCL((set).cpub, cpu, result)
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate #define	CPUSET_OR(set1, set2)		{		\
4427c478bd9Sstevel@tonic-gate 	int _i;						\
4437c478bd9Sstevel@tonic-gate 	for (_i = 0; _i < CPUSET_WORDS; _i++)		\
4447c478bd9Sstevel@tonic-gate 		(set1).cpub[_i] |= (set2).cpub[_i];	\
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4473aedfe0bSmishra #define	CPUSET_XOR(set1, set2)		{		\
4483aedfe0bSmishra 	int _i;						\
4493aedfe0bSmishra 	for (_i = 0; _i < CPUSET_WORDS; _i++)		\
4503aedfe0bSmishra 		(set1).cpub[_i] ^= (set2).cpub[_i];	\
4513aedfe0bSmishra }
4523aedfe0bSmishra 
4537c478bd9Sstevel@tonic-gate #define	CPUSET_AND(set1, set2)		{		\
4547c478bd9Sstevel@tonic-gate 	int _i;						\
4557c478bd9Sstevel@tonic-gate 	for (_i = 0; _i < CPUSET_WORDS; _i++)		\
4567c478bd9Sstevel@tonic-gate 		(set1).cpub[_i] &= (set2).cpub[_i];	\
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate #define	CPUSET_ZERO(set)		{		\
4607c478bd9Sstevel@tonic-gate 	int _i;						\
4617c478bd9Sstevel@tonic-gate 	for (_i = 0; _i < CPUSET_WORDS; _i++)		\
4627c478bd9Sstevel@tonic-gate 		(set).cpub[_i] = 0;			\
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate #elif	CPUSET_WORDS == 1
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate typedef	ulong_t	cpuset_t;	/* a set of CPUs */
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate #define	CPUSET(cpu)			(1UL << (cpu))
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate #define	CPUSET_ALL(set)			((void)((set) = ~0UL))
4727c478bd9Sstevel@tonic-gate #define	CPUSET_ALL_BUT(set, cpu)	((void)((set) = ~CPUSET(cpu)))
4737c478bd9Sstevel@tonic-gate #define	CPUSET_ONLY(set, cpu)		((void)((set) = CPUSET(cpu)))
4747c478bd9Sstevel@tonic-gate #define	CPU_IN_SET(set, cpu)		((set) & CPUSET(cpu))
4757c478bd9Sstevel@tonic-gate #define	CPUSET_ADD(set, cpu)		((void)((set) |= CPUSET(cpu)))
4767c478bd9Sstevel@tonic-gate #define	CPUSET_DEL(set, cpu)		((void)((set) &= ~CPUSET(cpu)))
4777c478bd9Sstevel@tonic-gate #define	CPUSET_ISNULL(set)		((set) == 0)
4787c478bd9Sstevel@tonic-gate #define	CPUSET_ISEQUAL(set1, set2)	((set1) == (set2))
4797c478bd9Sstevel@tonic-gate #define	CPUSET_OR(set1, set2)		((void)((set1) |= (set2)))
4803aedfe0bSmishra #define	CPUSET_XOR(set1, set2)		((void)((set1) ^= (set2)))
4817c478bd9Sstevel@tonic-gate #define	CPUSET_AND(set1, set2)		((void)((set1) &= (set2)))
4827c478bd9Sstevel@tonic-gate #define	CPUSET_ZERO(set)		((void)((set) = 0))
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate #define	CPUSET_FIND(set, cpu)		{		\
4857c478bd9Sstevel@tonic-gate 	cpu = (uint_t)(lowbit(set) - 1);				\
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
48800423197Sha #define	CPUSET_BOUNDS(set, smallest, largest)	{	\
48900423197Sha 	smallest = (uint_t)(lowbit(set) - 1);		\
49000423197Sha 	largest = (uint_t)(highbit(set) - 1);		\
49100423197Sha }
49200423197Sha 
4937c478bd9Sstevel@tonic-gate #define	CPUSET_ATOMIC_DEL(set, cpu)	atomic_and_long(&(set), ~CPUSET(cpu))
4947c478bd9Sstevel@tonic-gate #define	CPUSET_ATOMIC_ADD(set, cpu)	atomic_or_long(&(set), CPUSET(cpu))
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate #define	CPUSET_ATOMIC_XADD(set, cpu, result) \
4977c478bd9Sstevel@tonic-gate 	{ result = atomic_set_long_excl(&(set), (cpu)); }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate #define	CPUSET_ATOMIC_XDEL(set, cpu, result) \
5007c478bd9Sstevel@tonic-gate 	{ result = atomic_clear_long_excl(&(set), (cpu)); }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate #else	/* CPUSET_WORDS <= 0 */
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate #error NCPU is undefined or invalid
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate #endif	/* CPUSET_WORDS	*/
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate extern cpuset_t cpu_seqid_inuse;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate #endif	/* (_KERNEL || _KMEMUSER) && _MACHDEP */
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate #define	CPU_CPR_OFFLINE		0x0
5137c478bd9Sstevel@tonic-gate #define	CPU_CPR_ONLINE		0x1
5147c478bd9Sstevel@tonic-gate #define	CPU_CPR_IS_OFFLINE(cpu)	(((cpu)->cpu_cpr_flags & CPU_CPR_ONLINE) == 0)
5152df1fe9cSrandyf #define	CPU_CPR_IS_ONLINE(cpu)	((cpu)->cpu_cpr_flags & CPU_CPR_ONLINE)
5167c478bd9Sstevel@tonic-gate #define	CPU_SET_CPR_FLAGS(cpu, flag)	((cpu)->cpu_cpr_flags |= flag)
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate #if defined(_KERNEL) || defined(_KMEMUSER)
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate extern struct cpu	*cpu[];		/* indexed by CPU number */
5217c478bd9Sstevel@tonic-gate extern cpu_t		*cpu_list;	/* list of CPUs */
522*2850d85bSmv extern cpu_t		*cpu_active;	/* list of active CPUs */
5237c478bd9Sstevel@tonic-gate extern int		ncpus;		/* number of CPUs present */
5247c478bd9Sstevel@tonic-gate extern int		ncpus_online;	/* number of CPUs not quiesced */
5257c478bd9Sstevel@tonic-gate extern int		max_ncpus;	/* max present before ncpus is known */
5267c478bd9Sstevel@tonic-gate extern int		boot_max_ncpus;	/* like max_ncpus but for real */
5277c478bd9Sstevel@tonic-gate extern processorid_t	max_cpuid;	/* maximum CPU number */
5287c478bd9Sstevel@tonic-gate extern struct cpu	*cpu_inmotion;	/* offline or partition move target */
529c97ad5cdSakolb extern cpu_t		*clock_cpu_list;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
5327c478bd9Sstevel@tonic-gate extern struct cpu *curcpup(void);
5337c478bd9Sstevel@tonic-gate #define	CPU		(curcpup())	/* Pointer to current CPU */
5347c478bd9Sstevel@tonic-gate #else
5357c478bd9Sstevel@tonic-gate #define	CPU		(curthread->t_cpu)	/* Pointer to current CPU */
5367c478bd9Sstevel@tonic-gate #endif
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * CPU_CURRENT indicates to thread_affinity_set to use CPU->cpu_id
5407c478bd9Sstevel@tonic-gate  * as the target and to grab cpu_lock instead of requiring the caller
5417c478bd9Sstevel@tonic-gate  * to grab it.
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate #define	CPU_CURRENT	-3
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate  * Per-CPU statistics
5477c478bd9Sstevel@tonic-gate  *
5487c478bd9Sstevel@tonic-gate  * cpu_stats_t contains numerous system and VM-related statistics, in the form
5497c478bd9Sstevel@tonic-gate  * of gauges or monotonically-increasing event occurrence counts.
5507c478bd9Sstevel@tonic-gate  */
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate #define	CPU_STATS_ENTER_K()	kpreempt_disable()
5537c478bd9Sstevel@tonic-gate #define	CPU_STATS_EXIT_K()	kpreempt_enable()
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate #define	CPU_STATS_ADD_K(class, stat, amount) \
5567c478bd9Sstevel@tonic-gate 	{	kpreempt_disable(); /* keep from switching CPUs */\
5577c478bd9Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, class, stat, amount); \
5587c478bd9Sstevel@tonic-gate 		kpreempt_enable(); \
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate #define	CPU_STATS_ADDQ(cp, class, stat, amount)	{			\
5627c478bd9Sstevel@tonic-gate 	extern void __dtrace_probe___cpu_##class##info_##stat(uint_t,	\
5637c478bd9Sstevel@tonic-gate 	    uint64_t *, cpu_t *);					\
5647c478bd9Sstevel@tonic-gate 	uint64_t *stataddr = &((cp)->cpu_stats.class.stat);		\
5657c478bd9Sstevel@tonic-gate 	__dtrace_probe___cpu_##class##info_##stat((amount),		\
5667c478bd9Sstevel@tonic-gate 	    stataddr, cp);						\
5677c478bd9Sstevel@tonic-gate 	*(stataddr) += (amount);					\
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate #define	CPU_STATS(cp, stat)                                       \
5717c478bd9Sstevel@tonic-gate 	((cp)->cpu_stats.stat)
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate #endif /* _KERNEL || _KMEMUSER */
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * CPU support routines.
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate #if	defined(_KERNEL) && defined(__STDC__)	/* not for genassym.c */
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate struct zone;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate void	cpu_list_init(cpu_t *);
5837c478bd9Sstevel@tonic-gate void	cpu_add_unit(cpu_t *);
5847c478bd9Sstevel@tonic-gate void	cpu_del_unit(int cpuid);
5857c478bd9Sstevel@tonic-gate void	cpu_add_active(cpu_t *);
5867c478bd9Sstevel@tonic-gate void	cpu_kstat_init(cpu_t *);
5877c478bd9Sstevel@tonic-gate void	cpu_visibility_add(cpu_t *, struct zone *);
5887c478bd9Sstevel@tonic-gate void	cpu_visibility_remove(cpu_t *, struct zone *);
5897c478bd9Sstevel@tonic-gate void	cpu_visibility_configure(cpu_t *, struct zone *);
5907c478bd9Sstevel@tonic-gate void	cpu_visibility_unconfigure(cpu_t *, struct zone *);
5917c478bd9Sstevel@tonic-gate void	cpu_visibility_online(cpu_t *, struct zone *);
5927c478bd9Sstevel@tonic-gate void	cpu_visibility_offline(cpu_t *, struct zone *);
5937c478bd9Sstevel@tonic-gate void	cpu_create_intrstat(cpu_t *);
5947c478bd9Sstevel@tonic-gate void	cpu_delete_intrstat(cpu_t *);
5957c478bd9Sstevel@tonic-gate int	cpu_kstat_intrstat_update(kstat_t *, int);
5967c478bd9Sstevel@tonic-gate void	cpu_intr_swtch_enter(kthread_t *);
5977c478bd9Sstevel@tonic-gate void	cpu_intr_swtch_exit(kthread_t *);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate void	mbox_lock_init(void);	 /* initialize cross-call locks */
6007c478bd9Sstevel@tonic-gate void	mbox_init(int cpun);	 /* initialize cross-calls */
6017c478bd9Sstevel@tonic-gate void	poke_cpu(int cpun);	 /* interrupt another CPU (to preempt) */
6027c478bd9Sstevel@tonic-gate 
603ae115bc7Smrj /*
604ae115bc7Smrj  * values for safe_list.  Pause state that CPUs are in.
605ae115bc7Smrj  */
606ae115bc7Smrj #define	PAUSE_IDLE	0		/* normal state */
607ae115bc7Smrj #define	PAUSE_READY	1		/* paused thread ready to spl */
608ae115bc7Smrj #define	PAUSE_WAIT	2		/* paused thread is spl-ed high */
609ae115bc7Smrj #define	PAUSE_DIE	3		/* tell pause thread to leave */
610ae115bc7Smrj #define	PAUSE_DEAD	4		/* pause thread has left */
611ae115bc7Smrj 
612ae115bc7Smrj void	mach_cpu_pause(volatile char *);
613ae115bc7Smrj 
6147c478bd9Sstevel@tonic-gate void	pause_cpus(cpu_t *off_cp);
6157c478bd9Sstevel@tonic-gate void	start_cpus(void);
6167c478bd9Sstevel@tonic-gate int	cpus_paused(void);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate void	cpu_pause_init(void);
6197c478bd9Sstevel@tonic-gate cpu_t	*cpu_get(processorid_t cpun);	/* get the CPU struct associated */
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate int	cpu_online(cpu_t *cp);			/* take cpu online */
6227c478bd9Sstevel@tonic-gate int	cpu_offline(cpu_t *cp, int flags);	/* take cpu offline */
6237c478bd9Sstevel@tonic-gate int	cpu_spare(cpu_t *cp, int flags);	/* take cpu to spare */
6247c478bd9Sstevel@tonic-gate int	cpu_faulted(cpu_t *cp, int flags);	/* take cpu to faulted */
6257c478bd9Sstevel@tonic-gate int	cpu_poweron(cpu_t *cp);		/* take powered-off cpu to offline */
6267c478bd9Sstevel@tonic-gate int	cpu_poweroff(cpu_t *cp);	/* take offline cpu to powered-off */
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate cpu_t	*cpu_intr_next(cpu_t *cp);	/* get next online CPU taking intrs */
6297c478bd9Sstevel@tonic-gate int	cpu_intr_count(cpu_t *cp);	/* count # of CPUs handling intrs */
6307c478bd9Sstevel@tonic-gate int	cpu_intr_on(cpu_t *cp);		/* CPU taking I/O interrupts? */
6317c478bd9Sstevel@tonic-gate void	cpu_intr_enable(cpu_t *cp);	/* enable I/O interrupts */
6327c478bd9Sstevel@tonic-gate int	cpu_intr_disable(cpu_t *cp);	/* disable I/O interrupts */
633100b72f4Sandrei void	cpu_intr_alloc(cpu_t *cp, int n); /* allocate interrupt threads */
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate /*
6367c478bd9Sstevel@tonic-gate  * Routines for checking CPU states.
6377c478bd9Sstevel@tonic-gate  */
6387c478bd9Sstevel@tonic-gate int	cpu_is_online(cpu_t *);		/* check if CPU is online */
6397c478bd9Sstevel@tonic-gate int	cpu_is_nointr(cpu_t *);		/* check if CPU can service intrs */
6407c478bd9Sstevel@tonic-gate int	cpu_is_active(cpu_t *);		/* check if CPU can run threads */
6417c478bd9Sstevel@tonic-gate int	cpu_is_offline(cpu_t *);	/* check if CPU is offline */
6427c478bd9Sstevel@tonic-gate int	cpu_is_poweredoff(cpu_t *);	/* check if CPU is powered off */
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate int	cpu_flagged_online(cpu_flag_t);	/* flags show CPU is online */
6457c478bd9Sstevel@tonic-gate int	cpu_flagged_nointr(cpu_flag_t);	/* flags show CPU not handling intrs */
6467c478bd9Sstevel@tonic-gate int	cpu_flagged_active(cpu_flag_t); /* flags show CPU scheduling threads */
6477c478bd9Sstevel@tonic-gate int	cpu_flagged_offline(cpu_flag_t); /* flags show CPU is offline */
6487c478bd9Sstevel@tonic-gate int	cpu_flagged_poweredoff(cpu_flag_t); /* flags show CPU is powered off */
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * The processor_info(2) state of a CPU is a simplified representation suitable
6527c478bd9Sstevel@tonic-gate  * for use by an application program.  Kernel subsystems should utilize the
6537c478bd9Sstevel@tonic-gate  * internal per-CPU state as given by the cpu_flags member of the cpu structure,
6547c478bd9Sstevel@tonic-gate  * as this information may include platform- or architecture-specific state
6557c478bd9Sstevel@tonic-gate  * critical to a subsystem's disposition of a particular CPU.
6567c478bd9Sstevel@tonic-gate  */
6577c478bd9Sstevel@tonic-gate void	cpu_set_state(cpu_t *);		/* record/timestamp current state */
6587c478bd9Sstevel@tonic-gate int	cpu_get_state(cpu_t *);		/* get current cpu state */
6597c478bd9Sstevel@tonic-gate const char *cpu_get_state_str(cpu_t *);	/* get current cpu state as string */
6607c478bd9Sstevel@tonic-gate 
66168afbec1Smh 
66268afbec1Smh void	cpu_set_supp_freqs(cpu_t *, const char *); /* set the CPU supported */
66368afbec1Smh 						/* frequencies */
66468afbec1Smh 
6657c478bd9Sstevel@tonic-gate int	cpu_configure(int);
6667c478bd9Sstevel@tonic-gate int	cpu_unconfigure(int);
6677c478bd9Sstevel@tonic-gate void	cpu_destroy_bound_threads(cpu_t *cp);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate extern int cpu_bind_thread(kthread_t *tp, processorid_t bind,
6707c478bd9Sstevel@tonic-gate     processorid_t *obind, int *error);
6717c478bd9Sstevel@tonic-gate extern int cpu_unbind(processorid_t cpu_id);
6727c478bd9Sstevel@tonic-gate extern void thread_affinity_set(kthread_t *t, int cpu_id);
6737c478bd9Sstevel@tonic-gate extern void thread_affinity_clear(kthread_t *t);
6747c478bd9Sstevel@tonic-gate extern void affinity_set(int cpu_id);
6757c478bd9Sstevel@tonic-gate extern void affinity_clear(void);
6767c478bd9Sstevel@tonic-gate extern void init_cpu_mstate(struct cpu *, int);
6777c478bd9Sstevel@tonic-gate extern void term_cpu_mstate(struct cpu *);
678eda89462Sesolom extern void new_cpu_mstate(int, hrtime_t);
679eda89462Sesolom extern void get_cpu_mstate(struct cpu *, hrtime_t *);
6807c478bd9Sstevel@tonic-gate extern void thread_nomigrate(void);
6817c478bd9Sstevel@tonic-gate extern void thread_allowmigrate(void);
6827c478bd9Sstevel@tonic-gate extern void weakbinding_stop(void);
6837c478bd9Sstevel@tonic-gate extern void weakbinding_start(void);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate /*
6867c478bd9Sstevel@tonic-gate  * The following routines affect the CPUs participation in interrupt processing,
6877c478bd9Sstevel@tonic-gate  * if that is applicable on the architecture.  This only affects interrupts
6887c478bd9Sstevel@tonic-gate  * which aren't directed at the processor (not cross calls).
6897c478bd9Sstevel@tonic-gate  *
6907c478bd9Sstevel@tonic-gate  * cpu_disable_intr returns non-zero if interrupts were previously enabled.
6917c478bd9Sstevel@tonic-gate  */
6927c478bd9Sstevel@tonic-gate int	cpu_disable_intr(struct cpu *cp); /* stop issuing interrupts to cpu */
6937c478bd9Sstevel@tonic-gate void	cpu_enable_intr(struct cpu *cp); /* start issuing interrupts to cpu */
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate /*
6967c478bd9Sstevel@tonic-gate  * The mutex cpu_lock protects cpu_flags for all CPUs, as well as the ncpus
6977c478bd9Sstevel@tonic-gate  * and ncpus_online counts.
6987c478bd9Sstevel@tonic-gate  */
6997c478bd9Sstevel@tonic-gate extern kmutex_t	cpu_lock;	/* lock protecting CPU data */
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate typedef enum {
7027c478bd9Sstevel@tonic-gate 	CPU_INIT,
7037c478bd9Sstevel@tonic-gate 	CPU_CONFIG,
7047c478bd9Sstevel@tonic-gate 	CPU_UNCONFIG,
7057c478bd9Sstevel@tonic-gate 	CPU_ON,
7067c478bd9Sstevel@tonic-gate 	CPU_OFF,
7077c478bd9Sstevel@tonic-gate 	CPU_CPUPART_IN,
7087c478bd9Sstevel@tonic-gate 	CPU_CPUPART_OUT
7097c478bd9Sstevel@tonic-gate } cpu_setup_t;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate typedef int cpu_setup_func_t(cpu_setup_t, int, void *);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate  * Routines used to register interest in cpu's being added to or removed
7157c478bd9Sstevel@tonic-gate  * from the system.
7167c478bd9Sstevel@tonic-gate  */
7177c478bd9Sstevel@tonic-gate extern void register_cpu_setup_func(cpu_setup_func_t *, void *);
7187c478bd9Sstevel@tonic-gate extern void unregister_cpu_setup_func(cpu_setup_func_t *, void *);
7197c478bd9Sstevel@tonic-gate extern void cpu_state_change_notify(int, cpu_setup_t);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate /*
7227c478bd9Sstevel@tonic-gate  * Create various strings that describe the given CPU for the
7237c478bd9Sstevel@tonic-gate  * processor_info system call and configuration-related kstats.
7247c478bd9Sstevel@tonic-gate  */
7257c478bd9Sstevel@tonic-gate #define	CPU_IDSTRLEN	100
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate extern void init_cpu_info(struct cpu *);
728affbd3ccSkchow extern void cpu_vm_data_init(struct cpu *);
729affbd3ccSkchow extern void cpu_vm_data_destroy(struct cpu *);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate #endif
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate #endif /* _SYS_CPUVAR_H */
738