Thomas Gleixner | 4f19048 | 2019-05-27 08:55:14 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Library for common functions for Intel SpeedStep v.1 and v.2 support |
| 6 | * |
| 7 | * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous* |
| 8 | */ |
| 9 | |
Joe Perches | 1c5864e | 2016-04-05 13:28:25 -0700 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/cpufreq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include <asm/msr.h> |
Matthias-Christian Ott | 199785e | 2009-02-20 20:52:17 -0500 | [diff] [blame] | 19 | #include <asm/tsc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "speedstep-lib.h" |
| 21 | |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 22 | #define PFX "speedstep-lib: " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 25 | static int relaxed_check; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #else |
| 27 | #define relaxed_check 0 |
| 28 | #endif |
| 29 | |
| 30 | /********************************************************************* |
| 31 | * GET PROCESSOR CORE SPEED IN KHZ * |
| 32 | *********************************************************************/ |
| 33 | |
Rusty Russell | 1cce76c | 2009-11-17 14:39:53 -0800 | [diff] [blame] | 34 | static unsigned int pentium3_get_frequency(enum speedstep_processor processor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 36 | /* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */ |
Colin Ian King | 843791b | 2017-08-25 18:00:16 +0100 | [diff] [blame] | 37 | static const struct { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | unsigned int ratio; /* Frequency Multiplier (x10) */ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 39 | u8 bitmap; /* power on configuration bits |
| 40 | [27, 25:22] (in MSR 0x2a) */ |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 41 | } msr_decode_mult[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { 30, 0x01 }, |
| 43 | { 35, 0x05 }, |
| 44 | { 40, 0x02 }, |
| 45 | { 45, 0x06 }, |
| 46 | { 50, 0x00 }, |
| 47 | { 55, 0x04 }, |
| 48 | { 60, 0x0b }, |
| 49 | { 65, 0x0f }, |
| 50 | { 70, 0x09 }, |
| 51 | { 75, 0x0d }, |
| 52 | { 80, 0x0a }, |
| 53 | { 85, 0x26 }, |
| 54 | { 90, 0x20 }, |
| 55 | { 100, 0x2b }, |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 56 | { 0, 0xff } /* error or unknown value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */ |
Colin Ian King | 843791b | 2017-08-25 18:00:16 +0100 | [diff] [blame] | 60 | static const struct { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 61 | unsigned int value; /* Front Side Bus speed in MHz */ |
| 62 | u8 bitmap; /* power on configuration bits [18: 19] |
| 63 | (in MSR 0x2a) */ |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 64 | } msr_decode_fsb[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { 66, 0x0 }, |
| 66 | { 100, 0x2 }, |
| 67 | { 133, 0x1 }, |
| 68 | { 0, 0xff} |
| 69 | }; |
| 70 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 71 | u32 msr_lo, msr_tmp; |
| 72 | int i = 0, j = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* read MSR 0x2a - we only need the low 32 bits */ |
| 75 | rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 76 | pr_debug("P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | msr_tmp = msr_lo; |
| 78 | |
| 79 | /* decode the FSB */ |
| 80 | msr_tmp &= 0x00c0000; |
| 81 | msr_tmp >>= 18; |
| 82 | while (msr_tmp != msr_decode_fsb[i].bitmap) { |
| 83 | if (msr_decode_fsb[i].bitmap == 0xff) |
| 84 | return 0; |
| 85 | i++; |
| 86 | } |
| 87 | |
| 88 | /* decode the multiplier */ |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 89 | if (processor == SPEEDSTEP_CPU_PIII_C_EARLY) { |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 90 | pr_debug("workaround for early PIIIs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | msr_lo &= 0x03c00000; |
| 92 | } else |
| 93 | msr_lo &= 0x0bc00000; |
| 94 | msr_lo >>= 22; |
| 95 | while (msr_lo != msr_decode_mult[j].bitmap) { |
| 96 | if (msr_decode_mult[j].bitmap == 0xff) |
| 97 | return 0; |
| 98 | j++; |
| 99 | } |
| 100 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 101 | pr_debug("speed is %u\n", |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 102 | (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 104 | return msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
| 108 | static unsigned int pentiumM_get_frequency(void) |
| 109 | { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 110 | u32 msr_lo, msr_tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 113 | pr_debug("PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | /* see table B-2 of 24547212.pdf */ |
| 116 | if (msr_lo & 0x00040000) { |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 117 | printk(KERN_DEBUG PFX "PM - invalid FSB: 0x%x 0x%x\n", |
| 118 | msr_lo, msr_tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | msr_tmp = (msr_lo >> 22) & 0x1f; |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 123 | pr_debug("bits 22-26 are 0x%x, speed is %u\n", |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 124 | msr_tmp, (msr_tmp * 100 * 1000)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 126 | return msr_tmp * 100 * 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 129 | static unsigned int pentium_core_get_frequency(void) |
| 130 | { |
| 131 | u32 fsb = 0; |
| 132 | u32 msr_lo, msr_tmp; |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 133 | int ret; |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 134 | |
| 135 | rdmsr(MSR_FSB_FREQ, msr_lo, msr_tmp); |
Dominik Brodowski | e11952b | 2006-12-04 20:39:16 -0500 | [diff] [blame] | 136 | /* see table B-2 of 25366920.pdf */ |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 137 | switch (msr_lo & 0x07) { |
| 138 | case 5: |
Dominik Brodowski | e11952b | 2006-12-04 20:39:16 -0500 | [diff] [blame] | 139 | fsb = 100000; |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 140 | break; |
| 141 | case 1: |
Dominik Brodowski | e11952b | 2006-12-04 20:39:16 -0500 | [diff] [blame] | 142 | fsb = 133333; |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 143 | break; |
| 144 | case 3: |
Dominik Brodowski | e11952b | 2006-12-04 20:39:16 -0500 | [diff] [blame] | 145 | fsb = 166667; |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 146 | break; |
Herton Ronaldo Krzesinski | c60e19e | 2008-11-15 17:02:46 -0200 | [diff] [blame] | 147 | case 2: |
| 148 | fsb = 200000; |
| 149 | break; |
| 150 | case 0: |
| 151 | fsb = 266667; |
| 152 | break; |
| 153 | case 4: |
| 154 | fsb = 333333; |
| 155 | break; |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 156 | default: |
Joe Perches | b49c22a | 2016-04-05 13:28:24 -0700 | [diff] [blame] | 157 | pr_err("PCORE - MSR_FSB_FREQ undefined value\n"); |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 161 | pr_debug("PCORE - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 162 | msr_lo, msr_tmp); |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 163 | |
| 164 | msr_tmp = (msr_lo >> 22) & 0x1f; |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 165 | pr_debug("bits 22-26 are 0x%x, speed is %u\n", |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 166 | msr_tmp, (msr_tmp * fsb)); |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 167 | |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 168 | ret = (msr_tmp * fsb); |
| 169 | return ret; |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 170 | } |
Dominik Brodowski | e11952b | 2006-12-04 20:39:16 -0500 | [diff] [blame] | 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | static unsigned int pentium4_get_frequency(void) |
| 174 | { |
| 175 | struct cpuinfo_x86 *c = &boot_cpu_data; |
| 176 | u32 msr_lo, msr_hi, mult; |
| 177 | unsigned int fsb = 0; |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 178 | unsigned int ret; |
Matthias-Christian Ott | 199785e | 2009-02-20 20:52:17 -0500 | [diff] [blame] | 179 | u8 fsb_code; |
| 180 | |
| 181 | /* Pentium 4 Model 0 and 1 do not have the Core Clock Frequency |
| 182 | * to System Bus Frequency Ratio Field in the Processor Frequency |
| 183 | * Configuration Register of the MSR. Therefore the current |
| 184 | * frequency cannot be calculated and has to be measured. |
| 185 | */ |
| 186 | if (c->x86_model < 2) |
| 187 | return cpu_khz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | rdmsr(0x2c, msr_lo, msr_hi); |
| 190 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 191 | pr_debug("P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 193 | /* decode the FSB: see IA-32 Intel (C) Architecture Software |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * Developer's Manual, Volume 3: System Prgramming Guide, |
| 195 | * revision #12 in Table B-1: MSRs in the Pentium 4 and |
| 196 | * Intel Xeon Processors, on page B-4 and B-5. |
| 197 | */ |
Matthias-Christian Ott | 199785e | 2009-02-20 20:52:17 -0500 | [diff] [blame] | 198 | fsb_code = (msr_lo >> 16) & 0x7; |
| 199 | switch (fsb_code) { |
| 200 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | fsb = 100 * 1000; |
Matthias-Christian Ott | 199785e | 2009-02-20 20:52:17 -0500 | [diff] [blame] | 202 | break; |
| 203 | case 1: |
| 204 | fsb = 13333 * 10; |
| 205 | break; |
| 206 | case 2: |
| 207 | fsb = 200 * 1000; |
| 208 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (!fsb) |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 212 | printk(KERN_DEBUG PFX "couldn't detect FSB speed. " |
| 213 | "Please send an e-mail to <linux@brodo.de>\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | /* Multiplier. */ |
Zhao Yakui | ed9cbcd | 2007-11-20 14:20:21 -0500 | [diff] [blame] | 216 | mult = msr_lo >> 24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 218 | pr_debug("P4 - FSB %u kHz; Multiplier %u; Speed %u kHz\n", |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 219 | fsb, mult, (fsb * mult)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 221 | ret = (fsb * mult); |
| 222 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 225 | |
Rusty Russell | 394122a | 2009-06-11 22:59:58 +0930 | [diff] [blame] | 226 | /* Warning: may get called from smp_call_function_single. */ |
Rusty Russell | 1cce76c | 2009-11-17 14:39:53 -0800 | [diff] [blame] | 227 | unsigned int speedstep_get_frequency(enum speedstep_processor processor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 229 | switch (processor) { |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 230 | case SPEEDSTEP_CPU_PCORE: |
Dominik Brodowski | 4e74663 | 2006-10-31 12:44:08 -0500 | [diff] [blame] | 231 | return pentium_core_get_frequency(); |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 232 | case SPEEDSTEP_CPU_PM: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return pentiumM_get_frequency(); |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 234 | case SPEEDSTEP_CPU_P4D: |
| 235 | case SPEEDSTEP_CPU_P4M: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return pentium4_get_frequency(); |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 237 | case SPEEDSTEP_CPU_PIII_T: |
| 238 | case SPEEDSTEP_CPU_PIII_C: |
| 239 | case SPEEDSTEP_CPU_PIII_C_EARLY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return pentium3_get_frequency(processor); |
| 241 | default: |
| 242 | return 0; |
Tom Rix | 00d4394 | 2020-10-27 11:59:34 -0700 | [diff] [blame] | 243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return 0; |
| 245 | } |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 246 | EXPORT_SYMBOL_GPL(speedstep_get_frequency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | |
| 249 | /********************************************************************* |
| 250 | * DETECT SPEEDSTEP-CAPABLE PROCESSOR * |
| 251 | *********************************************************************/ |
| 252 | |
Andi Kleen | fa8031a | 2012-01-26 00:09:12 +0100 | [diff] [blame] | 253 | /* Keep in sync with the x86_cpu_id tables in the different modules */ |
Luc Van Oostenryck | df21443 | 2018-04-24 15:14:13 +0200 | [diff] [blame] | 254 | enum speedstep_processor speedstep_detect_processor(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 256 | struct cpuinfo_x86 *c = &cpu_data(0); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 257 | u32 ebx, msr_lo, msr_hi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 259 | pr_debug("x86: %x, model: %x\n", c->x86, c->x86_model); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 261 | if ((c->x86_vendor != X86_VENDOR_INTEL) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | ((c->x86 != 6) && (c->x86 != 0xF))) |
| 263 | return 0; |
| 264 | |
| 265 | if (c->x86 == 0xF) { |
| 266 | /* Intel Mobile Pentium 4-M |
| 267 | * or Intel Mobile Pentium 4 with 533 MHz FSB */ |
| 268 | if (c->x86_model != 2) |
| 269 | return 0; |
| 270 | |
| 271 | ebx = cpuid_ebx(0x00000001); |
| 272 | ebx &= 0x000000FF; |
| 273 | |
Jia Zhang | b399151 | 2018-01-01 09:52:10 +0800 | [diff] [blame] | 274 | pr_debug("ebx value is %x, x86_stepping is %x\n", ebx, c->x86_stepping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Jia Zhang | b399151 | 2018-01-01 09:52:10 +0800 | [diff] [blame] | 276 | switch (c->x86_stepping) { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 277 | case 4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | /* |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 279 | * B-stepping [M-P4-M] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | * sample has ebx = 0x0f, production has 0x0e. |
| 281 | */ |
| 282 | if ((ebx == 0x0e) || (ebx == 0x0f)) |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 283 | return SPEEDSTEP_CPU_P4M; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | break; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 285 | case 7: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* |
| 287 | * C-stepping [M-P4-M] |
| 288 | * needs to have ebx=0x0e, else it's a celeron: |
| 289 | * cf. 25130917.pdf / page 7, footnote 5 even |
| 290 | * though 25072120.pdf / page 7 doesn't say |
| 291 | * samples are only of B-stepping... |
| 292 | */ |
| 293 | if (ebx == 0x0e) |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 294 | return SPEEDSTEP_CPU_P4M; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | break; |
| 296 | case 9: |
| 297 | /* |
| 298 | * D-stepping [M-P4-M or M-P4/533] |
| 299 | * |
| 300 | * this is totally strange: CPUID 0x0F29 is |
| 301 | * used by M-P4-M, M-P4/533 and(!) Celeron CPUs. |
| 302 | * The latter need to be sorted out as they don't |
| 303 | * support speedstep. |
| 304 | * Celerons with CPUID 0x0F29 may have either |
| 305 | * ebx=0x8 or 0xf -- 25130917.pdf doesn't say anything |
| 306 | * specific. |
| 307 | * M-P4-Ms may have either ebx=0xe or 0xf [see above] |
| 308 | * M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf] |
| 309 | * also, M-P4M HTs have ebx=0x8, too |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 310 | * For now, they are distinguished by the model_id |
| 311 | * string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | */ |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 313 | if ((ebx == 0x0e) || |
| 314 | (strstr(c->x86_model_id, |
| 315 | "Mobile Intel(R) Pentium(R) 4") != NULL)) |
| 316 | return SPEEDSTEP_CPU_P4M; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | break; |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | switch (c->x86_model) { |
| 325 | case 0x0B: /* Intel PIII [Tualatin] */ |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 326 | /* cpuid_ebx(1) is 0x04 for desktop PIII, |
| 327 | * 0x06 for mobile PIII-M */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | ebx = cpuid_ebx(0x00000001); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 329 | pr_debug("ebx is %x\n", ebx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | ebx &= 0x000000FF; |
| 332 | |
| 333 | if (ebx != 0x06) |
| 334 | return 0; |
| 335 | |
| 336 | /* So far all PIII-M processors support SpeedStep. See |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 337 | * Intel's 24540640.pdf of June 2003 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | */ |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 339 | return SPEEDSTEP_CPU_PIII_T; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 341 | case 0x08: /* Intel PIII [Coppermine] */ |
| 342 | |
| 343 | /* all mobile PIII Coppermines have FSB 100 MHz |
| 344 | * ==> sort out a few desktop PIIIs. */ |
| 345 | rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 346 | pr_debug("Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n", |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 347 | msr_lo, msr_hi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | msr_lo &= 0x00c0000; |
| 349 | if (msr_lo != 0x0080000) |
| 350 | return 0; |
| 351 | |
| 352 | /* |
| 353 | * If the processor is a mobile version, |
| 354 | * platform ID has bit 50 set |
| 355 | * it has SpeedStep technology if either |
| 356 | * bit 56 or 57 is set |
| 357 | */ |
| 358 | rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 359 | pr_debug("Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n", |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 360 | msr_lo, msr_hi); |
| 361 | if ((msr_hi & (1<<18)) && |
| 362 | (relaxed_check ? 1 : (msr_hi & (3<<24)))) { |
Jia Zhang | b399151 | 2018-01-01 09:52:10 +0800 | [diff] [blame] | 363 | if (c->x86_stepping == 0x01) { |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 364 | pr_debug("early PIII version\n"); |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 365 | return SPEEDSTEP_CPU_PIII_C_EARLY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } else |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 367 | return SPEEDSTEP_CPU_PIII_C; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 369 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | default: |
| 371 | return 0; |
| 372 | } |
| 373 | } |
| 374 | EXPORT_SYMBOL_GPL(speedstep_detect_processor); |
| 375 | |
| 376 | |
| 377 | /********************************************************************* |
| 378 | * DETECT SPEEDSTEP SPEEDS * |
| 379 | *********************************************************************/ |
| 380 | |
Rusty Russell | 1cce76c | 2009-11-17 14:39:53 -0800 | [diff] [blame] | 381 | unsigned int speedstep_get_freqs(enum speedstep_processor processor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | unsigned int *low_speed, |
| 383 | unsigned int *high_speed, |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 384 | unsigned int *transition_latency, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | void (*set_state) (unsigned int state)) |
| 386 | { |
| 387 | unsigned int prev_speed; |
| 388 | unsigned int ret = 0; |
| 389 | unsigned long flags; |
Abhilash Jindal | 72e624d | 2015-08-11 12:01:22 -0400 | [diff] [blame] | 390 | ktime_t tv1, tv2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | if ((!processor) || (!low_speed) || (!high_speed) || (!set_state)) |
| 393 | return -EINVAL; |
| 394 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 395 | pr_debug("trying to determine both speeds\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | /* get current speed */ |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 398 | prev_speed = speedstep_get_frequency(processor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (!prev_speed) |
| 400 | return -EIO; |
| 401 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 402 | pr_debug("previous speed is %u\n", prev_speed); |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 403 | |
Mikulas Patocka | d4d4eda | 2015-02-09 13:38:17 -0500 | [diff] [blame] | 404 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | local_irq_save(flags); |
| 406 | |
| 407 | /* switch to low state */ |
| 408 | set_state(SPEEDSTEP_LOW); |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 409 | *low_speed = speedstep_get_frequency(processor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (!*low_speed) { |
| 411 | ret = -EIO; |
| 412 | goto out; |
| 413 | } |
| 414 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 415 | pr_debug("low speed is %u\n", *low_speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 417 | /* start latency measurement */ |
| 418 | if (transition_latency) |
Abhilash Jindal | 72e624d | 2015-08-11 12:01:22 -0400 | [diff] [blame] | 419 | tv1 = ktime_get(); |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | /* switch to high state */ |
| 422 | set_state(SPEEDSTEP_HIGH); |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 423 | |
| 424 | /* end latency measurement */ |
| 425 | if (transition_latency) |
Abhilash Jindal | 72e624d | 2015-08-11 12:01:22 -0400 | [diff] [blame] | 426 | tv2 = ktime_get(); |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 427 | |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 428 | *high_speed = speedstep_get_frequency(processor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | if (!*high_speed) { |
| 430 | ret = -EIO; |
| 431 | goto out; |
| 432 | } |
| 433 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 434 | pr_debug("high speed is %u\n", *high_speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | if (*low_speed == *high_speed) { |
| 437 | ret = -ENODEV; |
| 438 | goto out; |
| 439 | } |
| 440 | |
| 441 | /* switch to previous state, if necessary */ |
| 442 | if (*high_speed != prev_speed) |
| 443 | set_state(SPEEDSTEP_LOW); |
| 444 | |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 445 | if (transition_latency) { |
Abhilash Jindal | 72e624d | 2015-08-11 12:01:22 -0400 | [diff] [blame] | 446 | *transition_latency = ktime_to_us(ktime_sub(tv2, tv1)); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 447 | pr_debug("transition latency is %u uSec\n", *transition_latency); |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 448 | |
| 449 | /* convert uSec to nSec and add 20% for safety reasons */ |
| 450 | *transition_latency *= 1200; |
| 451 | |
| 452 | /* check if the latency measurement is too high or too low |
| 453 | * and set it to a safe value (500uSec) in that case |
| 454 | */ |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 455 | if (*transition_latency > 10000000 || |
| 456 | *transition_latency < 50000) { |
Joe Perches | 1c5864e | 2016-04-05 13:28:25 -0700 | [diff] [blame] | 457 | pr_warn("frequency transition measured seems out of range (%u nSec), falling back to a safe one of %u nSec\n", |
Joe Perches | b49c22a | 2016-04-05 13:28:24 -0700 | [diff] [blame] | 458 | *transition_latency, 500000); |
Mattia Dongili | 1a10760 | 2005-12-02 21:59:41 +0100 | [diff] [blame] | 459 | *transition_latency = 500000; |
| 460 | } |
| 461 | } |
| 462 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 463 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | local_irq_restore(flags); |
Mikulas Patocka | d4d4eda | 2015-02-09 13:38:17 -0500 | [diff] [blame] | 465 | preempt_enable(); |
| 466 | |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 467 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | EXPORT_SYMBOL_GPL(speedstep_get_freqs); |
| 470 | |
| 471 | #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK |
| 472 | module_param(relaxed_check, int, 0444); |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 473 | MODULE_PARM_DESC(relaxed_check, |
| 474 | "Don't do all checks for speedstep capability."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | #endif |
| 476 | |
Dave Jones | bbfebd6 | 2009-01-17 23:55:22 -0500 | [diff] [blame] | 477 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>"); |
| 478 | MODULE_DESCRIPTION("Library for Intel SpeedStep 1 or 2 cpufreq drivers."); |
| 479 | MODULE_LICENSE("GPL"); |