15cff7825Smh27603 /* 25cff7825Smh27603 * CDDL HEADER START 35cff7825Smh27603 * 45cff7825Smh27603 * The contents of this file are subject to the terms of the 55cff7825Smh27603 * Common Development and Distribution License (the "License"). 65cff7825Smh27603 * You may not use this file except in compliance with the License. 75cff7825Smh27603 * 85cff7825Smh27603 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95cff7825Smh27603 * or http://www.opensolaris.org/os/licensing. 105cff7825Smh27603 * See the License for the specific language governing permissions 115cff7825Smh27603 * and limitations under the License. 125cff7825Smh27603 * 135cff7825Smh27603 * When distributing Covered Code, include this CDDL HEADER in each 145cff7825Smh27603 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155cff7825Smh27603 * If applicable, add the following below this CDDL HEADER, with the 165cff7825Smh27603 * fields enclosed by brackets "[]" replaced with your own identifying 175cff7825Smh27603 * information: Portions Copyright [yyyy] [name of copyright owner] 185cff7825Smh27603 * 195cff7825Smh27603 * CDDL HEADER END 205cff7825Smh27603 */ 215cff7825Smh27603 /* 22fc68e77cSmh27603 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 235cff7825Smh27603 * Use is subject to license terms. 245cff7825Smh27603 */ 255cff7825Smh27603 265cff7825Smh27603 #ifndef _SYS_CPUDRV_H 275cff7825Smh27603 #define _SYS_CPUDRV_H 285cff7825Smh27603 295cff7825Smh27603 #include <sys/promif.h> 305cff7825Smh27603 #include <sys/cpuvar.h> 315cff7825Smh27603 #include <sys/taskq.h> 325cff7825Smh27603 335cff7825Smh27603 #ifdef __cplusplus 345cff7825Smh27603 extern "C" { 355cff7825Smh27603 #endif 365cff7825Smh27603 375cff7825Smh27603 #ifdef _KERNEL 385cff7825Smh27603 395cff7825Smh27603 /* 405cff7825Smh27603 * CPU power management data 415cff7825Smh27603 */ 425cff7825Smh27603 /* 435cff7825Smh27603 * Data related to a particular speed. 445cff7825Smh27603 * 455cff7825Smh27603 * All per speed data nodes for a CPU are linked together using down_spd. 465cff7825Smh27603 * The link list is ordered with first node containing data for 475cff7825Smh27603 * normal (maximum) speed. up_spd points to the next speed up. Currently 485cff7825Smh27603 * all up_spd's point to the normal speed but this can be changed in future. 495cff7825Smh27603 * quant_cnt is the number of ticks when monitoring system will be called 505cff7825Smh27603 * next. There are different quant_cnt for different speeds. 515cff7825Smh27603 * 525cff7825Smh27603 * Note that 'speed' has different meaning depending upon the platform. 535cff7825Smh27603 * On SPARC, the speed is really a divisor of the maximum speed (e.g., a speed 545cff7825Smh27603 * of 2 means that it's 1/2 the maximum speed). On x86, speed is a processor 555cff7825Smh27603 * frequency. 565cff7825Smh27603 */ 575cff7825Smh27603 typedef struct cpudrv_pm_spd { 585cff7825Smh27603 uint_t speed; /* platform dependent notion */ 595cff7825Smh27603 uint_t quant_cnt; /* quantum count in ticks */ 605cff7825Smh27603 struct cpudrv_pm_spd *down_spd; /* ptr to next speed down */ 615cff7825Smh27603 struct cpudrv_pm_spd *up_spd; /* ptr to next speed up */ 625cff7825Smh27603 uint_t idle_hwm; /* down if idle thread >= hwm */ 635cff7825Smh27603 uint_t idle_lwm; /* up if idle thread < lwm */ 645cff7825Smh27603 uint_t idle_bhwm_cnt; /* # of iters idle is < hwm */ 655cff7825Smh27603 uint_t idle_blwm_cnt; /* # of iters idle is < lwm */ 665cff7825Smh27603 uint_t user_hwm; /* up if user thread > hwm */ 675cff7825Smh27603 int user_lwm; /* down if user thread <= lwm */ 685cff7825Smh27603 int pm_level; /* power level for framework */ 695cff7825Smh27603 } cpudrv_pm_spd_t; 705cff7825Smh27603 715cff7825Smh27603 /* 725cff7825Smh27603 * Power management data 735cff7825Smh27603 */ 745cff7825Smh27603 typedef struct cpudrv_pm { 755cff7825Smh27603 cpudrv_pm_spd_t *head_spd; /* ptr to head of speed */ 765cff7825Smh27603 cpudrv_pm_spd_t *cur_spd; /* ptr to current speed */ 775cff7825Smh27603 cpudrv_pm_spd_t *targ_spd; /* target speed when cur_spd */ 785cff7825Smh27603 /* is unknown (i.e. NULL) */ 795cff7825Smh27603 uint_t num_spd; /* number of speeds */ 805cff7825Smh27603 hrtime_t lastquan_mstate[NCMSTATES]; /* last quantum's mstate */ 815cff7825Smh27603 clock_t lastquan_lbolt; /* last quantum's lbolt */ 825cff7825Smh27603 int pm_busycnt; /* pm_busy_component() count */ 835cff7825Smh27603 taskq_t *tq; /* taskq handler for CPU monitor */ 845cff7825Smh27603 timeout_id_t timeout_id; /* cpudrv_pm_monitor()'s timeout_id */ 855cff7825Smh27603 int timeout_count; /* count dispatched timeouts */ 865cff7825Smh27603 kmutex_t timeout_lock; /* protect timeout_count */ 875cff7825Smh27603 kcondvar_t timeout_cv; /* wait on timeout_count change */ 885cff7825Smh27603 #if defined(__x86) 89*7f606aceSMark Haywood kthread_t *pm_governor_thread; /* governor thread */ 905cff7825Smh27603 #endif 9168afbec1Smh27603 boolean_t pm_started; /* PM really started */ 925cff7825Smh27603 } cpudrv_pm_t; 935cff7825Smh27603 945cff7825Smh27603 /* 955cff7825Smh27603 * Idle & user threads water marks in percentage 965cff7825Smh27603 */ 975cff7825Smh27603 #if defined(__x86) 984b3651bdSmh27603 #define CPUDRV_PM_IDLE_HWM 85 /* idle high water mark */ 994b3651bdSmh27603 #define CPUDRV_PM_IDLE_LWM 70 /* idle low water mark */ 100fc68e77cSmh27603 #define CPUDRV_PM_IDLE_BLWM_CNT_MAX 1 /* # of iters idle can be < lwm */ 101fc68e77cSmh27603 #define CPUDRV_PM_IDLE_BHWM_CNT_MAX 1 /* # of iters idle can be < hwm */ 1025cff7825Smh27603 #else 1034b3651bdSmh27603 #define CPUDRV_PM_IDLE_HWM 98 /* idle high water mark */ 1045cff7825Smh27603 #define CPUDRV_PM_IDLE_LWM 8 /* idle low water mark */ 105fc68e77cSmh27603 #define CPUDRV_PM_IDLE_BLWM_CNT_MAX 2 /* # of iters idle can be < lwm */ 106fc68e77cSmh27603 #define CPUDRV_PM_IDLE_BHWM_CNT_MAX 2 /* # of iters idle can be < hwm */ 1075cff7825Smh27603 #endif 1085cff7825Smh27603 #define CPUDRV_PM_USER_HWM 20 /* user high water mark */ 1095cff7825Smh27603 #define CPUDRV_PM_IDLE_BUF_ZONE 4 /* buffer zone when going down */ 1105cff7825Smh27603 1115cff7825Smh27603 1125cff7825Smh27603 /* 1135cff7825Smh27603 * Maximums for creating 'pm-components' property 1145cff7825Smh27603 */ 1155cff7825Smh27603 #define CPUDRV_PM_COMP_MAX_DIG 4 /* max digits in power level */ 1165cff7825Smh27603 /* or divisor */ 1175cff7825Smh27603 #define CPUDRV_PM_COMP_MAX_VAL 9999 /* max value in above digits */ 1185cff7825Smh27603 1195cff7825Smh27603 /* 1205cff7825Smh27603 * Component number for calls to PM framework 1215cff7825Smh27603 */ 1225cff7825Smh27603 #define CPUDRV_PM_COMP_NUM 0 /* first component is 0 */ 1235cff7825Smh27603 1245cff7825Smh27603 /* 1255cff7825Smh27603 * Quantum counts for normal and other clock speeds in terms of ticks. 1265cff7825Smh27603 * 1275cff7825Smh27603 * In determining the quantum count, we need to balance two opposing factors: 1285cff7825Smh27603 * 1295cff7825Smh27603 * 1) Minimal delay when user start using the CPU that is in low 1305cff7825Smh27603 * power mode -- requires that we monitor more frequently, 1315cff7825Smh27603 * 1325cff7825Smh27603 * 2) Extra code executed because of frequent monitoring -- requires 1335cff7825Smh27603 * that we monitor less frequently. 1345cff7825Smh27603 * 1355cff7825Smh27603 * We reach a tradeoff between these two requirements by monitoring 1365cff7825Smh27603 * more frequently when we are in low speed mode (CPUDRV_PM_QUANT_CNT_OTHR) 1375cff7825Smh27603 * so we can bring the CPU up without user noticing it. Moreover, at low 1385cff7825Smh27603 * speed we are not using CPU much so extra code execution should be fine. 1395cff7825Smh27603 * Since we are in no hurry to bring CPU down and at normal speed and we 1405cff7825Smh27603 * might really be using the CPU fully, we monitor less frequently 1415cff7825Smh27603 * (CPUDRV_PM_QUANT_CNT_NORMAL). 1425cff7825Smh27603 */ 143fc68e77cSmh27603 #if defined(__x86) 144fc68e77cSmh27603 #define CPUDRV_PM_QUANT_CNT_NORMAL (hz * 1) /* 1 sec */ 145fc68e77cSmh27603 #else 1465cff7825Smh27603 #define CPUDRV_PM_QUANT_CNT_NORMAL (hz * 5) /* 5 sec */ 147fc68e77cSmh27603 #endif 1485cff7825Smh27603 #define CPUDRV_PM_QUANT_CNT_OTHR (hz * 1) /* 1 sec */ 1495cff7825Smh27603 1505cff7825Smh27603 /* 1515cff7825Smh27603 * Taskq parameters 1525cff7825Smh27603 */ 1535cff7825Smh27603 #define CPUDRV_PM_TASKQ_THREADS 1 /* # threads to run CPU monitor */ 1545cff7825Smh27603 #define CPUDRV_PM_TASKQ_MIN 2 /* min # of taskq entries */ 1555cff7825Smh27603 #define CPUDRV_PM_TASKQ_MAX 2 /* max # of taskq entries */ 1565cff7825Smh27603 1575cff7825Smh27603 1585cff7825Smh27603 /* 1595cff7825Smh27603 * Device driver state structure 1605cff7825Smh27603 */ 1615cff7825Smh27603 typedef struct cpudrv_devstate { 1625cff7825Smh27603 dev_info_t *dip; /* devinfo handle */ 1635cff7825Smh27603 processorid_t cpu_id; /* CPU number for this node */ 1645cff7825Smh27603 cpudrv_pm_t cpudrv_pm; /* power management data */ 1655cff7825Smh27603 kmutex_t lock; /* protects state struct */ 166*7f606aceSMark Haywood void *mach_state; /* machine specific state */ 1675cff7825Smh27603 } cpudrv_devstate_t; 1685cff7825Smh27603 1695cff7825Smh27603 extern void *cpudrv_state; 1705cff7825Smh27603 1715cff7825Smh27603 /* 1725cff7825Smh27603 * Debugging definitions 1735cff7825Smh27603 */ 1745cff7825Smh27603 #ifdef DEBUG 1755cff7825Smh27603 #define D_INIT 0x00000001 1765cff7825Smh27603 #define D_FINI 0x00000002 1775cff7825Smh27603 #define D_ATTACH 0x00000004 1785cff7825Smh27603 #define D_DETACH 0x00000008 1795cff7825Smh27603 #define D_POWER 0x00000010 1805cff7825Smh27603 #define D_PM_INIT 0x00000020 1815cff7825Smh27603 #define D_PM_FREE 0x00000040 1825cff7825Smh27603 #define D_PM_COMP_CREATE 0x00000080 1835cff7825Smh27603 #define D_PM_MONITOR 0x00000100 1845cff7825Smh27603 #define D_PM_MONITOR_VERBOSE 0x00000200 1855cff7825Smh27603 #define D_PM_MONITOR_DELAY 0x00000400 1865cff7825Smh27603 1875cff7825Smh27603 extern uint_t cpudrv_debug; 1885cff7825Smh27603 1895cff7825Smh27603 #define _PRINTF prom_printf 1905cff7825Smh27603 #define DPRINTF(flag, args) if (cpudrv_debug & flag) _PRINTF args; 1915cff7825Smh27603 #else 1925cff7825Smh27603 #define DPRINTF(flag, args) 1935cff7825Smh27603 #endif /* DEBUG */ 1945cff7825Smh27603 1955cff7825Smh27603 extern int cpudrv_pm_change_speed(cpudrv_devstate_t *, cpudrv_pm_spd_t *); 1965cff7825Smh27603 extern boolean_t cpudrv_pm_get_cpu_id(dev_info_t *, processorid_t *); 197*7f606aceSMark Haywood extern boolean_t cpudrv_pm_power_ready(void); 198*7f606aceSMark Haywood extern boolean_t cpudrv_pm_is_governor_thread(cpudrv_pm_t *); 199*7f606aceSMark Haywood extern boolean_t cpudrv_mach_pm_init(cpudrv_devstate_t *); 200*7f606aceSMark Haywood extern void cpudrv_mach_pm_free(cpudrv_devstate_t *); 2015cff7825Smh27603 2025cff7825Smh27603 #endif /* _KERNEL */ 2035cff7825Smh27603 2045cff7825Smh27603 #ifdef __cplusplus 2055cff7825Smh27603 } 2065cff7825Smh27603 #endif 2075cff7825Smh27603 2085cff7825Smh27603 #endif /* _SYS_CPUDRV_H */ 209