1 /* $OpenBSD: p4tcc.c,v 1.1 2003/12/20 18:23:18 tedu Exp $ */
3 * Copyright (c) 2003 Ted Unangst
4 * Copyright (c) 2004 Maxim Sobolev <sobomax@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Restrict power consumption by using thermal control circuit.
30 * This operates independently of speedstep.
31 * Found on Pentium 4 and later models (feature TM).
34 * Intel Developer's manual v.3 #245472-012
36 * On some models, the cpu can hang if it's running at a slow speed.
37 * Workarounds included below.
39 * $FreeBSD: /repoman/r/ncvs/src/sys/i386/i386/p4tcc.c,v 1.3.2.1 2004/03/03 15:24:15 sobomax Exp $
40 * $DragonFly: src/sys/platform/pc32/i386/p4tcc.c,v 1.3 2007/04/30 07:18:55 dillon Exp $
43 #include <sys/cdefs.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/power.h>
51 #include <sys/sysctl.h>
52 #include <sys/types.h>
54 #include <machine/md_var.h>
55 #include <machine/specialreg.h>
57 static u_int p4tcc_percentage;
58 static u_int p4tcc_economy;
59 static u_int p4tcc_performance;
60 static struct sysctl_ctx_list p4tcc_sysctl_ctx;
61 static struct sysctl_oid *p4tcc_sysctl_tree;
78 #define TCC_LEVELS sizeof(tcc) / sizeof(tcc[0])
86 msreg = rdmsr(MSR_THERM_CONTROL);
87 msreg = (msreg >> 1) & 0x07;
88 for (i = 0; i < TCC_LEVELS; i++) {
89 if (msreg == tcc[i].reg)
93 return (tcc[i].rlevel);
97 p4tcc_setperf(u_int percentage)
102 if (percentage > tcc[0].rlevel)
103 percentage = tcc[0].rlevel;
104 for (i = 0; i < TCC_LEVELS - 1; i++) {
105 if (percentage > tcc[i].level)
109 msreg = rdmsr(MSR_THERM_CONTROL);
110 msreg &= ~0x1e; /* bit 0 reserved */
112 msreg |= tcc[i].reg << 1 | 1 << 4;
113 wrmsr(MSR_THERM_CONTROL, msreg);
117 p4tcc_perf_sysctl(SYSCTL_HANDLER_ARGS)
122 p4tcc_percentage = p4tcc_getperf();
123 percentage = p4tcc_percentage;
124 error = sysctl_handle_int(oidp, &percentage, 0, req);
125 if (error || !req->newptr) {
128 if (p4tcc_percentage != percentage) {
129 p4tcc_setperf(percentage);
136 p4tcc_power_profile(void *arg)
141 state = power_profile_get_state();
142 if (state != POWER_PROFILE_PERFORMANCE &&
143 state != POWER_PROFILE_ECONOMY) {
148 case POWER_PROFILE_PERFORMANCE:
149 new = p4tcc_performance;
151 case POWER_PROFILE_ECONOMY:
155 new = p4tcc_getperf();
159 if (p4tcc_getperf() != new) {
165 p4tcc_profile_sysctl(SYSCTL_HANDLER_ARGS)
171 argp = (u_int32_t *)oidp->oid_arg1;
173 error = sysctl_handle_int(oidp, &arg, 0, req);
175 /* error or no new value */
176 if ((error != 0) || (req->newptr == NULL))
180 if (arg > tcc[0].rlevel)
183 /* set new value and possibly switch */
186 p4tcc_power_profile(NULL);
188 *argp = p4tcc_getperf();
194 setup_p4tcc(void *dummy __unused)
197 if ((cpu_feature & (CPUID_ACPI | CPUID_TM)) !=
198 (CPUID_ACPI | CPUID_TM))
201 switch (cpu_id & 0xf) {
202 case 0x22: /* errata O50 P44 and Z21 */
208 tcc[TCC_LEVELS - 1] = tcc[TCC_LEVELS - 2];
210 case 0x07: /* errata N44 and P18 */
214 /* hang at 12.5 and 25 */
215 tcc[TCC_LEVELS - 1] = tcc[TCC_LEVELS - 2] = tcc[TCC_LEVELS - 3];
221 p4tcc_economy = tcc[TCC_LEVELS - 1].rlevel;
222 p4tcc_performance = tcc[0].rlevel;
224 p4tcc_percentage = p4tcc_getperf();
225 kprintf("Pentium 4 TCC support enabled, current performance %u%%\n",
228 sysctl_ctx_init(&p4tcc_sysctl_ctx);
229 p4tcc_sysctl_tree = SYSCTL_ADD_NODE(&p4tcc_sysctl_ctx,
230 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, "p4tcc", CTLFLAG_RD, 0,
231 "Pentium 4 Thermal Control Circuitry support");
232 SYSCTL_ADD_PROC(&p4tcc_sysctl_ctx,
233 SYSCTL_CHILDREN(p4tcc_sysctl_tree), OID_AUTO,
234 "cpuperf", CTLTYPE_INT | CTLFLAG_RW,
235 &p4tcc_percentage, 0, p4tcc_perf_sysctl, "I",
236 "CPU performance in % of maximum");
237 SYSCTL_ADD_PROC(&p4tcc_sysctl_ctx,
238 SYSCTL_CHILDREN(p4tcc_sysctl_tree), OID_AUTO,
239 "cpuperf_performance", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_RW,
240 &p4tcc_performance, 0, p4tcc_profile_sysctl, "I",
241 "CPU performance in % of maximum in Performance mode");
242 SYSCTL_ADD_PROC(&p4tcc_sysctl_ctx,
243 SYSCTL_CHILDREN(p4tcc_sysctl_tree), OID_AUTO,
244 "cpuperf_economy", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_RW,
245 &p4tcc_economy, 0, p4tcc_profile_sysctl, "I",
246 "CPU performance in % of maximum in Economy mode");
248 /* register performance profile change handler */
249 EVENTHANDLER_REGISTER(power_profile_change, p4tcc_power_profile, NULL, 0);
253 * Set this is pre-smp to give us a chance to play nice in case
254 * SMP startup locks the system up.
256 SYSINIT(setup_p4tcc, SI_BOOT2_PRESMP, SI_ORDER_ANY, setup_p4tcc, NULL);