xref: /titanic_53/usr/src/uts/common/sys/disp.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #ifndef _SYS_DISP_H
32*7c478bd9Sstevel@tonic-gate #define	_SYS_DISP_H
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.11	*/
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/class.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
41*7c478bd9Sstevel@tonic-gate extern "C" {
42*7c478bd9Sstevel@tonic-gate #endif
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * The following is the format of a dispatcher queue entry.
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate typedef struct dispq {
48*7c478bd9Sstevel@tonic-gate 	kthread_t	*dq_first;	/* first thread on queue or NULL */
49*7c478bd9Sstevel@tonic-gate 	kthread_t	*dq_last;	/* last thread on queue or NULL */
50*7c478bd9Sstevel@tonic-gate 	int		dq_sruncnt;	/* number of loaded, runnable */
51*7c478bd9Sstevel@tonic-gate 					/*    threads on queue */
52*7c478bd9Sstevel@tonic-gate } dispq_t;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  * Dispatch queue structure.
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate typedef struct _disp {
58*7c478bd9Sstevel@tonic-gate 	disp_lock_t	disp_lock;	/* protects dispatching fields */
59*7c478bd9Sstevel@tonic-gate 	pri_t		disp_npri;	/* # of priority levels in queue */
60*7c478bd9Sstevel@tonic-gate 	dispq_t		*disp_q;		/* the dispatch queue */
61*7c478bd9Sstevel@tonic-gate 	dispq_t		*disp_q_limit;	/* ptr past end of dispatch queue */
62*7c478bd9Sstevel@tonic-gate 	ulong_t		*disp_qactmap;	/* bitmap of active dispatch queues */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	/*
65*7c478bd9Sstevel@tonic-gate 	 * Priorities:
66*7c478bd9Sstevel@tonic-gate 	 *	disp_maxrunpri is the maximum run priority of runnable threads
67*7c478bd9Sstevel@tonic-gate 	 * 	on this queue.  It is -1 if nothing is runnable.
68*7c478bd9Sstevel@tonic-gate 	 *
69*7c478bd9Sstevel@tonic-gate 	 *	disp_max_unbound_pri is the maximum run priority of threads on
70*7c478bd9Sstevel@tonic-gate 	 *	this dispatch queue but runnable by any CPU.  This may be left
71*7c478bd9Sstevel@tonic-gate 	 * 	artificially high, then corrected when some CPU tries to take
72*7c478bd9Sstevel@tonic-gate 	 *	an unbound thread.  It is -1 if nothing is runnable.
73*7c478bd9Sstevel@tonic-gate 	 */
74*7c478bd9Sstevel@tonic-gate 	pri_t		disp_maxrunpri;	/* maximum run priority */
75*7c478bd9Sstevel@tonic-gate 	pri_t		disp_max_unbound_pri;	/* max pri of unbound threads */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	volatile int	disp_nrunnable;	/* runnable threads in cpu dispq */
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	struct cpu	*disp_cpu;	/* cpu owning this queue or NULL */
80*7c478bd9Sstevel@tonic-gate } disp_t;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate #define	MAXCLSYSPRI	99
85*7c478bd9Sstevel@tonic-gate #define	MINCLSYSPRI	60
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * Global scheduling variables.
90*7c478bd9Sstevel@tonic-gate  *	- See sys/cpuvar.h for CPU-local variables.
91*7c478bd9Sstevel@tonic-gate  */
92*7c478bd9Sstevel@tonic-gate extern int	nswapped;	/* number of swapped threads */
93*7c478bd9Sstevel@tonic-gate 				/* nswapped protected by swap_lock */
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate extern	pri_t	minclsyspri;	/* minimum level of any system class */
96*7c478bd9Sstevel@tonic-gate extern	pri_t	maxclsyspri;	/* maximum level of any system class */
97*7c478bd9Sstevel@tonic-gate extern	pri_t	intr_pri;	/* interrupt thread priority base level */
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * Amount of time that may elapse before a thread is considered to have
101*7c478bd9Sstevel@tonic-gate  * lost it's cache investment.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate #define	RECHOOSE_INTERVAL	3
104*7c478bd9Sstevel@tonic-gate extern int	rechoose_interval;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate  * Kernel preemption occurs if a higher-priority thread is runnable with
108*7c478bd9Sstevel@tonic-gate  * a priority at or above kpreemptpri.
109*7c478bd9Sstevel@tonic-gate  *
110*7c478bd9Sstevel@tonic-gate  * So that other processors can watch for such threads, a separate
111*7c478bd9Sstevel@tonic-gate  * dispatch queue with unbound work above kpreemptpri is maintained.
112*7c478bd9Sstevel@tonic-gate  * This is part of the CPU partition structure (cpupart_t).
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate extern	pri_t	kpreemptpri;	/* level above which preemption takes place */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate extern void		disp_kp_alloc(disp_t *, pri_t);	/* allocate kp queue */
117*7c478bd9Sstevel@tonic-gate extern void		disp_kp_free(disp_t *);		/* free kp queue */
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate /*
120*7c478bd9Sstevel@tonic-gate  * Macro for use by scheduling classes to decide whether the thread is about
121*7c478bd9Sstevel@tonic-gate  * to be scheduled or not.  This returns the maximum run priority.
122*7c478bd9Sstevel@tonic-gate  */
123*7c478bd9Sstevel@tonic-gate #define	DISP_MAXRUNPRI(t)	((t)->t_disp_queue->disp_maxrunpri)
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate  * Platform callbacks for various dispatcher operations
127*7c478bd9Sstevel@tonic-gate  *
128*7c478bd9Sstevel@tonic-gate  * idle_cpu() is invoked when a cpu goes idle, and has nothing to do.
129*7c478bd9Sstevel@tonic-gate  * disp_enq_thread() is invoked when a thread is placed on a run queue.
130*7c478bd9Sstevel@tonic-gate  */
131*7c478bd9Sstevel@tonic-gate extern void	(*idle_cpu)();
132*7c478bd9Sstevel@tonic-gate extern void	(*disp_enq_thread)(struct cpu *, int);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate extern int		dispdeq(kthread_t *);
136*7c478bd9Sstevel@tonic-gate extern void		dispinit(void);
137*7c478bd9Sstevel@tonic-gate extern void		disp_add(sclass_t *);
138*7c478bd9Sstevel@tonic-gate extern int		intr_active(struct cpu *, int);
139*7c478bd9Sstevel@tonic-gate extern int		servicing_interrupt(void);
140*7c478bd9Sstevel@tonic-gate extern void		preempt(void);
141*7c478bd9Sstevel@tonic-gate extern void		setbackdq(kthread_t *);
142*7c478bd9Sstevel@tonic-gate extern void		setfrontdq(kthread_t *);
143*7c478bd9Sstevel@tonic-gate extern void		swtch(void);
144*7c478bd9Sstevel@tonic-gate extern void		swtch_to(kthread_t *);
145*7c478bd9Sstevel@tonic-gate extern void		swtch_from_zombie(void)
146*7c478bd9Sstevel@tonic-gate 				__NORETURN;
147*7c478bd9Sstevel@tonic-gate extern void		dq_sruninc(kthread_t *);
148*7c478bd9Sstevel@tonic-gate extern void		dq_srundec(kthread_t *);
149*7c478bd9Sstevel@tonic-gate extern void		cpu_rechoose(kthread_t *);
150*7c478bd9Sstevel@tonic-gate extern void		cpu_surrender(kthread_t *);
151*7c478bd9Sstevel@tonic-gate extern void		kpreempt(int);
152*7c478bd9Sstevel@tonic-gate extern struct cpu	*disp_lowpri_cpu(struct cpu *, struct lgrp_ld *, pri_t,
153*7c478bd9Sstevel@tonic-gate 			    struct cpu *);
154*7c478bd9Sstevel@tonic-gate extern int		disp_bound_threads(struct cpu *, int);
155*7c478bd9Sstevel@tonic-gate extern int		disp_bound_anythreads(struct cpu *, int);
156*7c478bd9Sstevel@tonic-gate extern int		disp_bound_partition(struct cpu *, int);
157*7c478bd9Sstevel@tonic-gate extern void		disp_cpu_init(struct cpu *);
158*7c478bd9Sstevel@tonic-gate extern void		disp_cpu_fini(struct cpu *);
159*7c478bd9Sstevel@tonic-gate extern void		disp_cpu_inactive(struct cpu *);
160*7c478bd9Sstevel@tonic-gate extern void		disp_adjust_unbound_pri(kthread_t *);
161*7c478bd9Sstevel@tonic-gate extern void		resume(kthread_t *);
162*7c478bd9Sstevel@tonic-gate extern void		resume_from_intr(kthread_t *);
163*7c478bd9Sstevel@tonic-gate extern void		resume_from_zombie(kthread_t *)
164*7c478bd9Sstevel@tonic-gate 				__NORETURN;
165*7c478bd9Sstevel@tonic-gate extern void		disp_swapped_enq(kthread_t *);
166*7c478bd9Sstevel@tonic-gate extern int		disp_anywork(void);
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate #define	KPREEMPT_SYNC		(-1)
169*7c478bd9Sstevel@tonic-gate #define	kpreempt_disable()				\
170*7c478bd9Sstevel@tonic-gate 	{						\
171*7c478bd9Sstevel@tonic-gate 		curthread->t_preempt++;			\
172*7c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_preempt >= 1);	\
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate #define	kpreempt_enable()				\
175*7c478bd9Sstevel@tonic-gate 	{						\
176*7c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_preempt >= 1);	\
177*7c478bd9Sstevel@tonic-gate 		if (--curthread->t_preempt == 0 &&	\
178*7c478bd9Sstevel@tonic-gate 		    CPU->cpu_kprunrun)			\
179*7c478bd9Sstevel@tonic-gate 			kpreempt(KPREEMPT_SYNC);	\
180*7c478bd9Sstevel@tonic-gate 	}
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate #endif
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate #endif	/* _SYS_DISP_H */
189