xref: /titanic_44/usr/src/uts/common/sys/cpudrv.h (revision 20e6d5c536ad5b300e7fafb6a92e13040f492977)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_CPUDRV_H
27 #define	_SYS_CPUDRV_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/promif.h>
32 #include <sys/cpuvar.h>
33 #include <sys/taskq.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #ifdef _KERNEL
40 
41 /*
42  * CPU power management data
43  */
44 /*
45  * Data related to a particular speed.
46  *
47  * All per speed data nodes for a CPU are linked together using down_spd.
48  * The link list is ordered with first node containing data for
49  * normal (maximum) speed. up_spd points to the next speed up. Currently
50  * all up_spd's point to the normal speed but this can be changed in future.
51  * quant_cnt is the number of ticks when monitoring system will be called
52  * next. There are different quant_cnt for different speeds.
53  *
54  * Note that 'speed' has different meaning depending upon the platform.
55  * On SPARC, the speed is really a divisor of the maximum speed (e.g., a speed
56  * of 2 means that it's 1/2 the maximum speed). On x86, speed is a processor
57  * frequency.
58  */
59 typedef struct cpudrv_pm_spd {
60 	uint_t			speed;		/* platform dependent notion */
61 	uint_t			quant_cnt;	/* quantum count in ticks */
62 	struct cpudrv_pm_spd	*down_spd;	/* ptr to next speed down */
63 	struct cpudrv_pm_spd	*up_spd;	/* ptr to next speed up */
64 	uint_t			idle_hwm;	/* down if idle thread >= hwm */
65 	uint_t			idle_lwm;	/* up if idle thread < lwm */
66 	uint_t			idle_bhwm_cnt;	/* # of iters idle is < hwm */
67 	uint_t			idle_blwm_cnt;	/* # of iters idle is < lwm */
68 	uint_t			user_hwm;	/* up if user thread > hwm */
69 	int			user_lwm;	/* down if user thread <= lwm */
70 	int			pm_level;	/* power level for framework */
71 } cpudrv_pm_spd_t;
72 
73 /*
74  * Power management data
75  */
76 typedef struct cpudrv_pm {
77 	cpudrv_pm_spd_t	*head_spd;	/* ptr to head of speed */
78 	cpudrv_pm_spd_t	*cur_spd;	/* ptr to current speed */
79 	cpudrv_pm_spd_t	*targ_spd;	/* target speed when cur_spd */
80 					/* is unknown (i.e. NULL) */
81 	uint_t		num_spd;	/* number of speeds */
82 	hrtime_t	lastquan_mstate[NCMSTATES]; /* last quantum's mstate */
83 	clock_t		lastquan_lbolt;	/* last quantum's lbolt */
84 	int		pm_busycnt;	/* pm_busy_component() count  */
85 	taskq_t		*tq;		/* taskq handler for CPU monitor */
86 	timeout_id_t	timeout_id;	/* cpudrv_pm_monitor()'s timeout_id */
87 	int		timeout_count;	/* count dispatched timeouts */
88 	kmutex_t	timeout_lock;	/* protect timeout_count */
89 	kcondvar_t	timeout_cv;	/* wait on timeout_count change */
90 #if defined(__x86)
91 	kthread_t	*pm_throttle_thread; /* throttling thread */
92 #endif
93 	boolean_t	pm_started;	/* PM really started */
94 } cpudrv_pm_t;
95 
96 /*
97  * Idle & user threads water marks in percentage
98  */
99 #if defined(__x86)
100 #define	CPUDRV_PM_IDLE_HWM		85	/* idle high water mark */
101 #define	CPUDRV_PM_IDLE_LWM		70	/* idle low water mark */
102 #define	CPUDRV_PM_IDLE_BLWM_CNT_MAX	1    /* # of iters idle can be < lwm */
103 #define	CPUDRV_PM_IDLE_BHWM_CNT_MAX	1    /* # of iters idle can be < hwm */
104 #else
105 #define	CPUDRV_PM_IDLE_HWM		98	/* idle high water mark */
106 #define	CPUDRV_PM_IDLE_LWM		8	/* idle low water mark */
107 #define	CPUDRV_PM_IDLE_BLWM_CNT_MAX	2    /* # of iters idle can be < lwm */
108 #define	CPUDRV_PM_IDLE_BHWM_CNT_MAX	2    /* # of iters idle can be < hwm */
109 #endif
110 #define	CPUDRV_PM_USER_HWM		20	/* user high water mark */
111 #define	CPUDRV_PM_IDLE_BUF_ZONE		4    /* buffer zone when going down */
112 
113 
114 /*
115  * Maximums for creating 'pm-components' property
116  */
117 #define	CPUDRV_PM_COMP_MAX_DIG	4	/* max digits in power level */
118 					/* or divisor */
119 #define	CPUDRV_PM_COMP_MAX_VAL	9999	/* max value in above digits */
120 
121 /*
122  * Component number for calls to PM framework
123  */
124 #define	CPUDRV_PM_COMP_NUM	0	/* first component is 0 */
125 
126 /*
127  * Quantum counts for normal and other clock speeds in terms of ticks.
128  *
129  * In determining the quantum count, we need to balance two opposing factors:
130  *
131  *	1) Minimal delay when user start using the CPU that is in low
132  *	power mode -- requires that we monitor more frequently,
133  *
134  *	2) Extra code executed because of frequent monitoring -- requires
135  *	that we monitor less frequently.
136  *
137  * We reach a tradeoff between these two requirements by monitoring
138  * more frequently when we are in low speed mode (CPUDRV_PM_QUANT_CNT_OTHR)
139  * so we can bring the CPU up without user noticing it. Moreover, at low
140  * speed we are not using CPU much so extra code execution should be fine.
141  * Since we are in no hurry to bring CPU down and at normal speed and we
142  * might really be using the CPU fully, we monitor less frequently
143  * (CPUDRV_PM_QUANT_CNT_NORMAL).
144  */
145 #if defined(__x86)
146 #define	CPUDRV_PM_QUANT_CNT_NORMAL	(hz * 1)	/* 1 sec */
147 #else
148 #define	CPUDRV_PM_QUANT_CNT_NORMAL	(hz * 5)	/* 5 sec */
149 #endif
150 #define	CPUDRV_PM_QUANT_CNT_OTHR	(hz * 1)	/* 1 sec */
151 
152 /*
153  * Taskq parameters
154  */
155 #define	CPUDRV_PM_TASKQ_THREADS		1    /* # threads to run CPU monitor */
156 #define	CPUDRV_PM_TASKQ_MIN		2	/* min # of taskq entries */
157 #define	CPUDRV_PM_TASKQ_MAX		2	/* max # of taskq entries */
158 
159 
160 /*
161  * Device driver state structure
162  */
163 typedef struct cpudrv_devstate {
164 	dev_info_t	*dip;		/* devinfo handle */
165 	processorid_t	cpu_id;		/* CPU number for this node */
166 	cpudrv_pm_t	cpudrv_pm;	/* power management data */
167 	kmutex_t	lock;		/* protects state struct */
168 #if defined(__x86)
169 	void		*acpi_handle;	/* ACPI cache */
170 	void		*module_state;  /* CPU module state */
171 #endif
172 } cpudrv_devstate_t;
173 
174 extern void	*cpudrv_state;
175 
176 /*
177  * Debugging definitions
178  */
179 #ifdef	DEBUG
180 #define	D_INIT			0x00000001
181 #define	D_FINI			0x00000002
182 #define	D_ATTACH		0x00000004
183 #define	D_DETACH		0x00000008
184 #define	D_POWER			0x00000010
185 #define	D_PM_INIT		0x00000020
186 #define	D_PM_FREE		0x00000040
187 #define	D_PM_COMP_CREATE	0x00000080
188 #define	D_PM_MONITOR		0x00000100
189 #define	D_PM_MONITOR_VERBOSE	0x00000200
190 #define	D_PM_MONITOR_DELAY	0x00000400
191 
192 extern uint_t	cpudrv_debug;
193 
194 #define	_PRINTF prom_printf
195 #define	DPRINTF(flag, args)	if (cpudrv_debug & flag) _PRINTF args;
196 #else
197 #define	DPRINTF(flag, args)
198 #endif /* DEBUG */
199 
200 extern int cpudrv_pm_change_speed(cpudrv_devstate_t *, cpudrv_pm_spd_t *);
201 extern boolean_t cpudrv_pm_get_cpu_id(dev_info_t *, processorid_t *);
202 extern boolean_t cpudrv_pm_all_instances_ready(void);
203 extern boolean_t cpudrv_pm_is_throttle_thread(cpudrv_pm_t *);
204 extern boolean_t cpudrv_pm_init_module(cpudrv_devstate_t *);
205 extern void cpudrv_pm_free_module(cpudrv_devstate_t *);
206 
207 #endif /* _KERNEL */
208 
209 #ifdef	__cplusplus
210 }
211 #endif
212 
213 #endif /* _SYS_CPUDRV_H */
214