1 /* $NetBSD: est.c,v 1.25 2006/06/18 16:39:56 nonaka Exp $ */
3 * Copyright (c) 2003 Michael Eriksson.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Copyright (c) 2004 The NetBSD Foundation, Inc.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the NetBSD
43 * Foundation, Inc. and its contributors.
44 * 4. Neither the name of The NetBSD Foundation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
62 * This is a driver for Intel's Enhanced SpeedStep Technology (EST),
63 * as implemented in Pentium M processors.
65 * Reference documentation:
67 * - IA-32 Intel Architecture Software Developer's Manual, Volume 3:
68 * System Programming Guide.
69 * Section 13.14, Enhanced Intel SpeedStep technology.
70 * Table B-2, MSRs in Pentium M Processors.
71 * http://www.intel.com/design/pentium4/manuals/253668.htm
73 * - Intel Pentium M Processor Datasheet.
74 * Table 5, Voltage and Current Specifications.
75 * http://www.intel.com/design/mobile/datashts/252612.htm
77 * - Intel Pentium M Processor on 90 nm Process with 2-MB L2 Cache Datasheet
78 * Table 3-4, 3-5, 3-6, Voltage and Current Specifications.
79 * http://www.intel.com/design/mobile/datashts/302189.htm
81 * - Linux cpufreq patches, speedstep-centrino.c.
82 * Encoding of MSR_PERF_CTL and MSR_PERF_STATUS.
83 * http://www.codemonkey.org.uk/projects/cpufreq/cpufreq-2.4.22-pre6-1.gz
85 * ACPI objects: _PCT is MSR location, _PSS is freq/voltage, _PPC is caps.
87 * $NetBSD: est.c,v 1.25 2006/06/18 16:39:56 nonaka Exp $
88 * $DragonFly: src/sys/platform/pc32/i386/est.c,v 1.9 2007/08/24 16:08:05 dillon Exp $
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/malloc.h>
94 #include <sys/kernel.h>
95 #include <sys/module.h>
96 #include <sys/sysctl.h>
98 #include <machine/cpu.h>
99 #include <machine/md_var.h>
100 #include <machine/specialreg.h>
108 /* Ultra Low Voltage Intel Pentium M processor 900 MHz */
109 static const struct fq_info pentium_m_900[] = {
115 /* Ultra Low Voltage Intel Pentium M processor 1.00 GHz */
116 static const struct fq_info pentium_m_1000[] = {
123 /* Low Voltage Intel Pentium M processor 1.10 GHz */
124 static const struct fq_info pentium_m_1100[] = {
132 /* Low Voltage Intel Pentium M processor 1.20 GHz */
133 static const struct fq_info pentium_m_1200[] = {
142 /* Low Voltage Intel Pentium M processor 1.30 GHz */
143 static const struct fq_info pentium_m_1300_lv[] = {
153 /* Intel Pentium M processor 1.30 GHz */
154 static const struct fq_info pentium_m_1300[] = {
162 /* Intel Pentium M processor 1.40 GHz */
163 static const struct fq_info pentium_m_1400[] = {
171 /* Intel Pentium M processor 1.50 GHz */
172 static const struct fq_info pentium_m_1500[] = {
181 /* Intel Pentium M processor 1.60 GHz */
182 static const struct fq_info pentium_m_1600[] = {
191 /* Intel Pentium M processor 1.70 GHz */
192 static const struct fq_info pentium_m_1700[] = {
201 /* Intel Pentium M processor 723 Ultra Low Voltage 1.0 GHz */
202 static const struct fq_info pentium_m_n723[] = {
209 /* Intel Pentium M processor 733 Ultra Low Voltage 1.1 GHz */
210 static const struct fq_info pentium_m_n733[] = {
218 /* Intel Pentium M processor 753 Ultra Low Voltage 1.2 GHz */
219 static const struct fq_info pentium_m_n753[] = {
228 /* Intel Pentium M processor 773 Ultra Low Voltage 1.3 GHz */
229 static const struct fq_info pentium_m_n773[] = {
239 /* Intel Pentium M processor 738 Low Voltage 1.4 GHz */
240 static const struct fq_info pentium_m_n738[] = {
251 /* Intel Pentium M processor 758 Low Voltage 1.5 GHz */
252 static const struct fq_info pentium_m_n758[] = {
264 /* Intel Pentium M processor 778 Low Voltage 1.6 GHz */
265 static const struct fq_info pentium_m_n778[] = {
278 /* Intel Pentium M processor 710 1.4 GHz */
279 static const struct fq_info pentium_m_n710[] = {
287 /* Intel Pentium M processor 715 1.5 GHz */
288 static const struct fq_info pentium_m_n715[] = {
296 /* Intel Pentium M processor 725 1.6 GHz */
297 static const struct fq_info pentium_m_n725[] = {
306 /* Intel Pentium M processor 730 1.6 GHz */
307 static const struct fq_info pentium_m_n730[] = {
315 /* Intel Pentium M processor 735 1.7 GHz */
316 static const struct fq_info pentium_m_n735[] = {
325 /* Intel Pentium M processor 740 1.73 GHz */
326 static const struct fq_info pentium_m_n740[] = {
333 /* Intel Pentium M processor 740 1.73 GHz (988-1308mV version?) */
334 static const struct fq_info pentium_m_n740_2[] = {
341 /* Intel Pentium M processor 745 1.8 GHz */
342 static const struct fq_info pentium_m_n745[] = {
352 /* Intel Pentium M processor 750 1.86 GHz */
353 /* values extracted from \_PR\NPSS (via _PSS) SDST ACPI table */
354 static const struct fq_info pentium_m_n750[] = {
362 static const struct fq_info pentium_m_n750_2[] = {
370 /* Intel Pentium M processor 755 2.0 GHz */
371 static const struct fq_info pentium_m_n755[] = {
382 /* Intel Pentium M processor 760 2.0 GHz */
383 static const struct fq_info pentium_m_n760[] = {
391 /* Intel Pentium M processor 760 2.0 GHz */
392 static const struct fq_info pentium_m_n760_2[] = {
400 /* Intel Pentium M processor 765 2.1 GHz */
401 static const struct fq_info pentium_m_n765[] = {
412 /* Intel Pentium M processor 770 2.13 GHz */
413 static const struct fq_info pentium_m_n770[] = {
424 /* Intel Pentium M processor 770 2.13 GHz */
425 static const struct fq_info pentium_m_n770_2[] = {
434 /* Intel Pentium Core Duo T2300 */
435 static const struct fq_info pentium_core_duo[] = {
447 const char *brand_tag;
450 const struct fq_info *table;
451 const int fsbmult; /* in multiples of 133 MHz */
454 #define ENTRY(s, i, v, f) { s, i, sizeof(v) / sizeof((v)[0]), v, f }
455 static const struct fqlist pentium_m[] = { /* Banias */
456 ENTRY(" 900", 0x0695, pentium_m_900, 3),
457 ENTRY("1000", 0x0695, pentium_m_1000, 3),
458 ENTRY("1100", 0x0695, pentium_m_1100, 3),
459 ENTRY("1200", 0x0695, pentium_m_1200, 3),
460 ENTRY("1300", 0x0695, pentium_m_1300, 3),
461 ENTRY("1300", 0x0695, pentium_m_1300_lv, 3),
462 ENTRY("1400", 0x0695, pentium_m_1400, 3),
463 ENTRY("1500", 0x0695, pentium_m_1500, 3),
464 ENTRY("1600", 0x0695, pentium_m_1600, 3),
465 ENTRY("1700", 0x0695, pentium_m_1700, 3),
468 static const struct fqlist pentium_m_dothan[] = {
470 /* low voltage CPUs */
471 ENTRY("1.00", 0x06d8, pentium_m_n723, 3),
472 ENTRY("1.10", 0x06d6, pentium_m_n733, 3),
473 ENTRY("1.20", 0x06d8, pentium_m_n753, 3),
474 ENTRY("1.30", 0, pentium_m_n773, 3), /* does this exist? */
476 /* ultra low voltage CPUs */
477 ENTRY("1.40", 0x06d6, pentium_m_n738, 3),
478 ENTRY("1.50", 0x06d8, pentium_m_n758, 3),
479 ENTRY("1.60", 0x06d8, pentium_m_n778, 3),
481 /* 'regular' 400 MHz FSB CPUs */
482 ENTRY("1.40", 0x06d6, pentium_m_n710, 3),
483 ENTRY("1.50", 0x06d6, pentium_m_n715, 3),
484 ENTRY("1.50", 0x06d8, pentium_m_n715, 3),
485 ENTRY("1.60", 0x06d6, pentium_m_n725, 3),
486 ENTRY("1.70", 0x06d6, pentium_m_n735, 3),
487 ENTRY("1.80", 0x06d6, pentium_m_n745, 3),
488 ENTRY("2.00", 0x06d6, pentium_m_n755, 3),
489 ENTRY("2.10", 0x06d6, pentium_m_n765, 3),
491 /* 533 MHz FSB CPUs */
492 ENTRY("1.60", 0x06d8, pentium_m_n730, 4),
493 ENTRY("1.73", 0x06d8, pentium_m_n740, 4),
494 ENTRY("1.73", 0x06d8, pentium_m_n740_2, 4),
495 ENTRY("1.86", 0x06d8, pentium_m_n750, 4),
496 ENTRY("1.86", 0x06d8, pentium_m_n750_2, 4),
497 ENTRY("2.00", 0x06d8, pentium_m_n760, 4),
498 ENTRY("2.00", 0x06d8, pentium_m_n760_2, 4),
499 ENTRY("2.13", 0x06d8, pentium_m_n770, 4),
500 ENTRY("2.13", 0x06d8, pentium_m_n770_2, 4),
505 static const struct fqlist pentium_yonah[] = {
507 /* 666 MHz FSB CPUs */
508 ENTRY("1.66", 0x06e8, pentium_core_duo, 5 ),
514 const char *brand_prefix;
515 const char *brand_suffix;
517 const struct fqlist *list;
520 static const struct est_cpu est_cpus[] = {
522 "Intel(R) Pentium(R) M processor ", "MHz",
523 (sizeof(pentium_m) / sizeof(pentium_m[0])),
527 "Intel(R) Pentium(R) M processor ", "GHz",
528 (sizeof(pentium_m_dothan) / sizeof(pentium_m_dothan[0])),
532 "Genuine Intel(R) CPU T2300 @ ", "GHz",
533 (sizeof(pentium_yonah) / sizeof(pentium_yonah[0])),
538 #define NESTCPUS (sizeof(est_cpus) / sizeof(est_cpus[0]))
540 #define MSR2MV(msr) (((int) (msr) & 0xff) * 16 + 700)
541 #define MSR2MHZ(msr) (((((int) (msr) >> 8) & 0xff) * 100 * fsbmult + 1)/ 3)
542 #define MV2MSR(mv) ((((int) (mv) - 700) >> 4) & 0xff)
543 #define MHZ2MSR(mhz) (((3 * (mhz + 30) / (100 * fsbmult)) & 0xff) << 8)
544 /* XXX 30 is slop to deal with the 33.333 MHz roundoff values */
547 * Names and numbers from IA-32 System Programming Guide
548 * (not found in <machine/specialregs.h>
550 #define MSR_PERF_STATUS 0x198
551 #define MSR_PERF_CTL 0x199
553 static const struct fqlist *est_fqlist; /* not NULL if functional */
556 static const char est_desc[] = "Enhanced SpeedStep";
558 static char freqs_available[80];
561 est_sysctl_helper(SYSCTL_HANDLER_ARGS)
564 int fq, oldfq, err = 0;
567 if (est_fqlist == NULL)
570 oldfq = MSR2MHZ(rdmsr(MSR_PERF_CTL));
572 if (req->newptr != NULL) {
573 err = SYSCTL_IN(req, &fq, sizeof(fq));
578 for (i = est_fqlist->tablec - 1; i > 0; i--) {
579 if (est_fqlist->table[i].mhz >= fq)
582 fq = est_fqlist->table[i].mhz;
583 msr = (rdmsr(MSR_PERF_CTL) & ~0xffffULL) |
584 MV2MSR(est_fqlist->table[i].mv) |
585 MHZ2MSR(est_fqlist->table[i].mhz);
586 wrmsr(MSR_PERF_CTL, msr);
589 err = SYSCTL_OUT(req, &oldfq, sizeof(oldfq));
596 * Look for a CPU matching hw.model
598 static const struct fqlist *
599 findcpu(const char *hwmodel, int mv)
601 const struct est_cpu *ccpu;
602 const struct fqlist *fql;
608 for (ccpu = est_cpus; ccpu < est_cpus + NESTCPUS; ++ccpu) {
609 len = strlen(ccpu->brand_prefix);
610 if (strncmp(ccpu->brand_prefix, hwmodel, len) != 0)
613 for (i = 0; i < ccpu->listc; i++) {
614 fql = &ccpu->list[i];
615 len = strlen(fql->brand_tag);
616 if (strncmp(fql->brand_tag, tag, len) != 0 ||
617 strcmp(ccpu->brand_suffix, tag + len))
620 if (fql->cpu_id == 0 || fql->cpu_id == cpu_id) {
621 /* verify operating point is in table, because
622 CPUID + brand_tag still isn't unique. */
623 for (k = fql->tablec - 1; k >= 0; k--) {
624 if (fql->table[k].mv == mv)
634 static struct sysctl_ctx_list machdep_est_ctx;
640 int mib[] = { CTL_HW, HW_MODEL };
641 size_t modellen = sizeof(hwmodel);
642 struct sysctl_oid *oid, *leaf;
645 size_t len, freq_len;
649 if ((cpu_feature2 & CPUID2_EST) == 0) {
650 kprintf("Enhanced SpeedStep unsupported on this hardware.\n");
654 modellen = sizeof(hwmodel);
655 err = kernel_sysctl(mib, 2, hwmodel, &modellen, NULL, 0, NULL);
657 kprintf("kernel_sysctl hw.model failed\n");
661 msr = rdmsr(MSR_PERF_STATUS);
663 kprintf("%s (%d mV) ", est_desc, mv);
665 est_fqlist = findcpu(hwmodel, mv);
666 if (est_fqlist == NULL) {
667 kprintf(" - unknown CPU or operating point"
668 "(cpu_id:%#x, msr:%#llx).\n", cpu_id, msr);
673 * OK, tell the user the available frequencies.
675 fsbmult = est_fqlist->fsbmult;
676 kprintf("%d MHz\n", MSR2MHZ(msr));
678 freq_len = est_fqlist->tablec * (sizeof("9999 ")-1) + 1;
679 if (freq_len >= sizeof(freqs_available)) {
680 kprintf("increase the size of freqs_available[]\n");
683 freqs_available[0] = '\0';
685 for (i = 0; i < est_fqlist->tablec; i++) {
686 len += ksnprintf(freqs_available + len, freq_len - len, "%d%s",
687 est_fqlist->table[i].mhz,
688 i < est_fqlist->tablec - 1 ? " " : "");
690 kprintf("%s frequencies available (MHz): %s\n", est_desc,
694 * Setup the sysctl sub-tree machdep.est.*
696 oid = SYSCTL_ADD_NODE(&machdep_est_ctx,
697 SYSCTL_STATIC_CHILDREN(_machdep), OID_AUTO, "est",
698 CTLFLAG_RD, NULL, "");
701 oid = SYSCTL_ADD_NODE(&machdep_est_ctx, SYSCTL_CHILDREN(oid),
702 OID_AUTO, "frequency", CTLFLAG_RD, NULL, "");
705 leaf = SYSCTL_ADD_PROC(&machdep_est_ctx, SYSCTL_CHILDREN(oid),
706 OID_AUTO, "target", CTLTYPE_INT | CTLFLAG_RW, NULL, NULL,
707 est_sysctl_helper, "I",
708 "Target CPU frequency for Enhanced SpeedStep");
711 leaf = SYSCTL_ADD_PROC(&machdep_est_ctx, SYSCTL_CHILDREN(oid),
712 OID_AUTO, "current", CTLTYPE_INT | CTLFLAG_RD, NULL, NULL,
713 est_sysctl_helper, "I",
714 "Current CPU frequency for Enhanced SpeedStep");
717 leaf = SYSCTL_ADD_STRING(&machdep_est_ctx, SYSCTL_CHILDREN(oid),
718 OID_AUTO, "available", CTLFLAG_RD, freqs_available,
719 sizeof(freqs_available),
720 "CPU frequencies supported by Enhanced SpeedStep");
728 est_modevh(struct module *m __unused, int what, void *arg __unused)
734 error = sysctl_ctx_init(&machdep_est_ctx);
740 error = sysctl_ctx_free(&machdep_est_ctx);
749 static moduledata_t est_mod = {
755 DECLARE_MODULE(est, est_mod, SI_BOOT2_KLD, SI_ORDER_ANY);