xref: /illumos-gate/usr/src/uts/common/sys/cpudrv.h (revision 6af9d452)
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
55cff7825Smh  * Common Development and Distribution License (the "License").
65cff7825Smh  * 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  */
217c478bd9Sstevel@tonic-gate /*
22fcddbe1fSMark Haywood  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25444f66e7SMark Haywood /*
26444f66e7SMark Haywood  * Copyright (c) 2009,  Intel Corporation.
27444f66e7SMark Haywood  * All Rights Reserved.
28444f66e7SMark Haywood  */
297c478bd9Sstevel@tonic-gate 
305cff7825Smh #ifndef _SYS_CPUDRV_H
315cff7825Smh #define	_SYS_CPUDRV_H
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/promif.h>
347c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
357c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
387c478bd9Sstevel@tonic-gate extern "C" {
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef _KERNEL
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
445cff7825Smh  * CPU power management data
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Data related to a particular speed.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * All per speed data nodes for a CPU are linked together using down_spd.
507c478bd9Sstevel@tonic-gate  * The link list is ordered with first node containing data for
517c478bd9Sstevel@tonic-gate  * normal (maximum) speed. up_spd points to the next speed up. Currently
527c478bd9Sstevel@tonic-gate  * all up_spd's point to the normal speed but this can be changed in future.
537c478bd9Sstevel@tonic-gate  * quant_cnt is the number of ticks when monitoring system will be called
547c478bd9Sstevel@tonic-gate  * next. There are different quant_cnt for different speeds.
555cff7825Smh  *
565cff7825Smh  * Note that 'speed' has different meaning depending upon the platform.
575cff7825Smh  * On SPARC, the speed is really a divisor of the maximum speed (e.g., a speed
585cff7825Smh  * of 2 means that it's 1/2 the maximum speed). On x86, speed is a processor
595cff7825Smh  * frequency.
607c478bd9Sstevel@tonic-gate  */
615cff7825Smh typedef struct cpudrv_pm_spd {
625cff7825Smh 	uint_t			speed;		/* platform dependent notion */
637c478bd9Sstevel@tonic-gate 	uint_t			quant_cnt;	/* quantum count in ticks */
645cff7825Smh 	struct cpudrv_pm_spd	*down_spd;	/* ptr to next speed down */
655cff7825Smh 	struct cpudrv_pm_spd	*up_spd;	/* ptr to next speed up */
667c478bd9Sstevel@tonic-gate 	uint_t			idle_hwm;	/* down if idle thread >= hwm */
677c478bd9Sstevel@tonic-gate 	uint_t			idle_lwm;	/* up if idle thread < lwm */
687c478bd9Sstevel@tonic-gate 	uint_t			idle_bhwm_cnt;	/* # of iters idle is < hwm */
697c478bd9Sstevel@tonic-gate 	uint_t			idle_blwm_cnt;	/* # of iters idle is < lwm */
707c478bd9Sstevel@tonic-gate 	uint_t			user_hwm;	/* up if user thread > hwm */
717c478bd9Sstevel@tonic-gate 	int			user_lwm;	/* down if user thread <= lwm */
727c478bd9Sstevel@tonic-gate 	int			pm_level;	/* power level for framework */
735cff7825Smh } cpudrv_pm_spd_t;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Power management data
777c478bd9Sstevel@tonic-gate  */
785cff7825Smh typedef struct cpudrv_pm {
795cff7825Smh 	cpudrv_pm_spd_t	*head_spd;	/* ptr to head of speed */
805cff7825Smh 	cpudrv_pm_spd_t	*cur_spd;	/* ptr to current speed */
817c478bd9Sstevel@tonic-gate 	uint_t		num_spd;	/* number of speeds */
825cff7825Smh 	hrtime_t	lastquan_mstate[NCMSTATES]; /* last quantum's mstate */
830e751525SEric Saxe 	clock_t		lastquan_ticks;	/* last quantum's clock tick */
847c478bd9Sstevel@tonic-gate 	int		pm_busycnt;	/* pm_busy_component() count  */
85444f66e7SMark Haywood 	ddi_taskq_t	*tq;		/* taskq handler for CPU monitor */
860e751525SEric Saxe 	timeout_id_t	timeout_id;	/* cpudrv_monitor()'s timeout_id */
877c478bd9Sstevel@tonic-gate 	int		timeout_count;	/* count dispatched timeouts */
887c478bd9Sstevel@tonic-gate 	kmutex_t	timeout_lock;	/* protect timeout_count */
897c478bd9Sstevel@tonic-gate 	kcondvar_t	timeout_cv;	/* wait on timeout_count change */
905cff7825Smh #if defined(__x86)
917f606aceSMark Haywood 	kthread_t	*pm_governor_thread; /* governor thread */
9217353130SMark Haywood 	cpudrv_pm_spd_t	*top_spd;	/* ptr to effective head speed */
935cff7825Smh #endif
9468afbec1Smh 	boolean_t	pm_started;	/* PM really started */
955cff7825Smh } cpudrv_pm_t;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Idle & user threads water marks in percentage
997c478bd9Sstevel@tonic-gate  */
1005cff7825Smh #if defined(__x86)
1010e751525SEric Saxe #define	CPUDRV_IDLE_HWM		85	/* idle high water mark */
1020e751525SEric Saxe #define	CPUDRV_IDLE_LWM		70	/* idle low water mark */
1030e751525SEric Saxe #define	CPUDRV_IDLE_BLWM_CNT_MAX	1    /* # of iters idle can be < lwm */
1040e751525SEric Saxe #define	CPUDRV_IDLE_BHWM_CNT_MAX	1    /* # of iters idle can be < hwm */
1055cff7825Smh #else
1060e751525SEric Saxe #define	CPUDRV_IDLE_HWM		98	/* idle high water mark */
1070e751525SEric Saxe #define	CPUDRV_IDLE_LWM		8	/* idle low water mark */
1080e751525SEric Saxe #define	CPUDRV_IDLE_BLWM_CNT_MAX	2    /* # of iters idle can be < lwm */
1090e751525SEric Saxe #define	CPUDRV_IDLE_BHWM_CNT_MAX	2    /* # of iters idle can be < hwm */
1105cff7825Smh #endif
1110e751525SEric Saxe #define	CPUDRV_USER_HWM		20	/* user high water mark */
1120e751525SEric Saxe #define	CPUDRV_IDLE_BUF_ZONE		4    /* buffer zone when going down */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Maximums for creating 'pm-components' property
1177c478bd9Sstevel@tonic-gate  */
1180e751525SEric Saxe #define	CPUDRV_COMP_MAX_DIG	4	/* max digits in power level */
1197c478bd9Sstevel@tonic-gate 					/* or divisor */
1200e751525SEric Saxe #define	CPUDRV_COMP_MAX_VAL	9999	/* max value in above digits */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Component number for calls to PM framework
1247c478bd9Sstevel@tonic-gate  */
1250e751525SEric Saxe #define	CPUDRV_COMP_NUM	0	/* first component is 0 */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * Quantum counts for normal and other clock speeds in terms of ticks.
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * In determining the quantum count, we need to balance two opposing factors:
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  *	1) Minimal delay when user start using the CPU that is in low
1337c478bd9Sstevel@tonic-gate  *	power mode -- requires that we monitor more frequently,
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  *	2) Extra code executed because of frequent monitoring -- requires
1367c478bd9Sstevel@tonic-gate  *	that we monitor less frequently.
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  * We reach a tradeoff between these two requirements by monitoring
1390e751525SEric Saxe  * more frequently when we are in low speed mode (CPUDRV_QUANT_CNT_OTHR)
1407c478bd9Sstevel@tonic-gate  * so we can bring the CPU up without user noticing it. Moreover, at low
1417c478bd9Sstevel@tonic-gate  * speed we are not using CPU much so extra code execution should be fine.
1427c478bd9Sstevel@tonic-gate  * Since we are in no hurry to bring CPU down and at normal speed and we
1437c478bd9Sstevel@tonic-gate  * might really be using the CPU fully, we monitor less frequently
1440e751525SEric Saxe  * (CPUDRV_QUANT_CNT_NORMAL).
1457c478bd9Sstevel@tonic-gate  */
146fc68e77cSmh #if defined(__x86)
1470e751525SEric Saxe #define	CPUDRV_QUANT_CNT_NORMAL	(hz * 1)	/* 1 sec */
148fc68e77cSmh #else
1490e751525SEric Saxe #define	CPUDRV_QUANT_CNT_NORMAL	(hz * 5)	/* 5 sec */
150fc68e77cSmh #endif
1510e751525SEric Saxe #define	CPUDRV_QUANT_CNT_OTHR	(hz * 1)	/* 1 sec */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Taskq parameters
1557c478bd9Sstevel@tonic-gate  */
1560e751525SEric Saxe #define	CPUDRV_TASKQ_THREADS		1    /* # threads to run CPU monitor */
1570e751525SEric Saxe #define	CPUDRV_TASKQ_MIN		2	/* min # of taskq entries */
1580e751525SEric Saxe #define	CPUDRV_TASKQ_MAX		2	/* max # of taskq entries */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Device driver state structure
1637c478bd9Sstevel@tonic-gate  */
1645cff7825Smh typedef struct cpudrv_devstate {
1657c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;		/* devinfo handle */
1660e751525SEric Saxe 	cpu_t		*cp;		/* CPU data for this node */
1677c478bd9Sstevel@tonic-gate 	processorid_t	cpu_id;		/* CPU number for this node */
1685cff7825Smh 	cpudrv_pm_t	cpudrv_pm;	/* power management data */
1697c478bd9Sstevel@tonic-gate 	kmutex_t	lock;		/* protects state struct */
1705cff7825Smh } cpudrv_devstate_t;
1717c478bd9Sstevel@tonic-gate 
1725cff7825Smh extern void	*cpudrv_state;
1730e751525SEric Saxe extern boolean_t cpudrv_enabled;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Debugging definitions
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1797c478bd9Sstevel@tonic-gate #define	D_INIT			0x00000001
1807c478bd9Sstevel@tonic-gate #define	D_FINI			0x00000002
1817c478bd9Sstevel@tonic-gate #define	D_ATTACH		0x00000004
1827c478bd9Sstevel@tonic-gate #define	D_DETACH		0x00000008
1837c478bd9Sstevel@tonic-gate #define	D_POWER			0x00000010
1847c478bd9Sstevel@tonic-gate #define	D_PM_INIT		0x00000020
1857c478bd9Sstevel@tonic-gate #define	D_PM_FREE		0x00000040
1867c478bd9Sstevel@tonic-gate #define	D_PM_COMP_CREATE	0x00000080
1877c478bd9Sstevel@tonic-gate #define	D_PM_MONITOR		0x00000100
1887c478bd9Sstevel@tonic-gate #define	D_PM_MONITOR_VERBOSE	0x00000200
1897c478bd9Sstevel@tonic-gate #define	D_PM_MONITOR_DELAY	0x00000400
1907c478bd9Sstevel@tonic-gate 
1915cff7825Smh extern uint_t	cpudrv_debug;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate #define	_PRINTF prom_printf
1945cff7825Smh #define	DPRINTF(flag, args)	if (cpudrv_debug & flag) _PRINTF args;
1957c478bd9Sstevel@tonic-gate #else
1967c478bd9Sstevel@tonic-gate #define	DPRINTF(flag, args)
1977c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1987c478bd9Sstevel@tonic-gate 
1990e751525SEric Saxe extern int cpudrv_change_speed(cpudrv_devstate_t *, cpudrv_pm_spd_t *);
2000e751525SEric Saxe extern boolean_t cpudrv_get_cpu_id(dev_info_t *, processorid_t *);
2010e751525SEric Saxe extern boolean_t cpudrv_is_governor_thread(cpudrv_pm_t *);
2020e751525SEric Saxe extern boolean_t cpudrv_mach_init(cpudrv_devstate_t *);
203444f66e7SMark Haywood extern boolean_t cpudrv_mach_fini(cpudrv_devstate_t *);
204444f66e7SMark Haywood extern boolean_t cpudrv_power_ready(cpu_t *);
2050e751525SEric Saxe extern boolean_t cpudrv_is_enabled(cpudrv_devstate_t *);
2060e751525SEric Saxe extern void cpudrv_set_supp_freqs(cpudrv_devstate_t *);
207*6af9d452Saubrey.li@intel.com extern int cpudrv_get_cpu(cpudrv_devstate_t *);
2085cff7825Smh 
2097c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate #endif
2147c478bd9Sstevel@tonic-gate 
2155cff7825Smh #endif /* _SYS_CPUDRV_H */
216