Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * TSC frequency enumeration via MSR |
| 4 | * |
| 5 | * Copyright (C) 2013, 2018 Intel Corporation |
| 6 | * Author: Bin Gao <bin.gao@intel.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | |
| 11 | #include <asm/apic.h> |
| 12 | #include <asm/cpu_device_id.h> |
| 13 | #include <asm/intel-family.h> |
| 14 | #include <asm/msr.h> |
| 15 | #include <asm/param.h> |
| 16 | #include <asm/tsc.h> |
| 17 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 18 | #define MAX_NUM_FREQS 16 /* 4 bits to select the frequency */ |
| 19 | |
| 20 | /* |
| 21 | * The frequency numbers in the SDM are e.g. 83.3 MHz, which does not contain a |
| 22 | * lot of accuracy which leads to clock drift. As far as we know Bay Trail SoCs |
| 23 | * use a 25 MHz crystal and Cherry Trail uses a 19.2 MHz crystal, the crystal |
| 24 | * is the source clk for a root PLL which outputs 1600 and 100 MHz. It is |
| 25 | * unclear if the root PLL outputs are used directly by the CPU clock PLL or |
| 26 | * if there is another PLL in between. |
| 27 | * This does not matter though, we can model the chain of PLLs as a single PLL |
| 28 | * with a quotient equal to the quotients of all PLLs in the chain multiplied. |
| 29 | * So we can create a simplified model of the CPU clock setup using a reference |
| 30 | * clock of 100 MHz plus a quotient which gets us as close to the frequency |
| 31 | * from the SDM as possible. |
| 32 | * For the 83.3 MHz example from above this would give us 100 MHz * 5 / 6 = |
| 33 | * 83 and 1/3 MHz, which matches exactly what has been measured on actual hw. |
| 34 | */ |
| 35 | #define TSC_REFERENCE_KHZ 100000 |
| 36 | |
| 37 | struct muldiv { |
| 38 | u32 multiplier; |
| 39 | u32 divider; |
| 40 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * If MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be |
| 44 | * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40]. |
| 45 | * Unfortunately some Intel Atom SoCs aren't quite compliant to this, |
| 46 | * so we need manually differentiate SoC families. This is what the |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 47 | * field use_msr_plat does. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 48 | */ |
| 49 | struct freq_desc { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 50 | bool use_msr_plat; |
| 51 | struct muldiv muldiv[MAX_NUM_FREQS]; |
| 52 | /* |
| 53 | * Some CPU frequencies in the SDM do not map to known PLL freqs, in |
| 54 | * that case the muldiv array is empty and the freqs array is used. |
| 55 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 56 | u32 freqs[MAX_NUM_FREQS]; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 57 | u32 mask; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * Penwell and Clovertrail use spread spectrum clock, |
| 62 | * so the freq number is not exactly the same as reported |
| 63 | * by MSR based on SDM. |
| 64 | */ |
| 65 | static const struct freq_desc freq_desc_pnw = { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 66 | .use_msr_plat = false, |
| 67 | .freqs = { 0, 0, 0, 0, 0, 99840, 0, 83200 }, |
| 68 | .mask = 0x07, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static const struct freq_desc freq_desc_clv = { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 72 | .use_msr_plat = false, |
| 73 | .freqs = { 0, 133200, 0, 0, 0, 99840, 0, 83200 }, |
| 74 | .mask = 0x07, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 77 | /* |
| 78 | * Bay Trail SDM MSR_FSB_FREQ frequencies simplified PLL model: |
| 79 | * 000: 100 * 5 / 6 = 83.3333 MHz |
| 80 | * 001: 100 * 1 / 1 = 100.0000 MHz |
| 81 | * 010: 100 * 4 / 3 = 133.3333 MHz |
| 82 | * 011: 100 * 7 / 6 = 116.6667 MHz |
| 83 | * 100: 100 * 4 / 5 = 80.0000 MHz |
| 84 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 85 | static const struct freq_desc freq_desc_byt = { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 86 | .use_msr_plat = true, |
| 87 | .muldiv = { { 5, 6 }, { 1, 1 }, { 4, 3 }, { 7, 6 }, |
| 88 | { 4, 5 } }, |
| 89 | .mask = 0x07, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 92 | /* |
| 93 | * Cherry Trail SDM MSR_FSB_FREQ frequencies simplified PLL model: |
| 94 | * 0000: 100 * 5 / 6 = 83.3333 MHz |
| 95 | * 0001: 100 * 1 / 1 = 100.0000 MHz |
| 96 | * 0010: 100 * 4 / 3 = 133.3333 MHz |
| 97 | * 0011: 100 * 7 / 6 = 116.6667 MHz |
| 98 | * 0100: 100 * 4 / 5 = 80.0000 MHz |
| 99 | * 0101: 100 * 14 / 15 = 93.3333 MHz |
| 100 | * 0110: 100 * 9 / 10 = 90.0000 MHz |
| 101 | * 0111: 100 * 8 / 9 = 88.8889 MHz |
| 102 | * 1000: 100 * 7 / 8 = 87.5000 MHz |
| 103 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 104 | static const struct freq_desc freq_desc_cht = { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 105 | .use_msr_plat = true, |
| 106 | .muldiv = { { 5, 6 }, { 1, 1 }, { 4, 3 }, { 7, 6 }, |
| 107 | { 4, 5 }, { 14, 15 }, { 9, 10 }, { 8, 9 }, |
| 108 | { 7, 8 } }, |
| 109 | .mask = 0x0f, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 112 | /* |
| 113 | * Merriefield SDM MSR_FSB_FREQ frequencies simplified PLL model: |
| 114 | * 0001: 100 * 1 / 1 = 100.0000 MHz |
| 115 | * 0010: 100 * 4 / 3 = 133.3333 MHz |
| 116 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 117 | static const struct freq_desc freq_desc_tng = { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 118 | .use_msr_plat = true, |
| 119 | .muldiv = { { 0, 0 }, { 1, 1 }, { 4, 3 } }, |
| 120 | .mask = 0x07, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 123 | /* |
| 124 | * Moorefield SDM MSR_FSB_FREQ frequencies simplified PLL model: |
| 125 | * 0000: 100 * 5 / 6 = 83.3333 MHz |
| 126 | * 0001: 100 * 1 / 1 = 100.0000 MHz |
| 127 | * 0010: 100 * 4 / 3 = 133.3333 MHz |
| 128 | * 0011: 100 * 1 / 1 = 100.0000 MHz |
| 129 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 130 | static const struct freq_desc freq_desc_ann = { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 131 | .use_msr_plat = true, |
| 132 | .muldiv = { { 5, 6 }, { 1, 1 }, { 4, 3 }, { 1, 1 } }, |
| 133 | .mask = 0x0f, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 136 | /* |
| 137 | * 24 MHz crystal? : 24 * 13 / 4 = 78 MHz |
| 138 | * Frequency step for Lightning Mountain SoC is fixed to 78 MHz, |
| 139 | * so all the frequency entries are 78000. |
| 140 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 141 | static const struct freq_desc freq_desc_lgm = { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 142 | .use_msr_plat = true, |
| 143 | .freqs = { 78000, 78000, 78000, 78000, 78000, 78000, 78000, 78000, |
| 144 | 78000, 78000, 78000, 78000, 78000, 78000, 78000, 78000 }, |
| 145 | .mask = 0x0f, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 148 | static const struct x86_cpu_id tsc_msr_cpu_ids[] = { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 149 | INTEL_CPU_FAM6(ATOM_SALTWELL_MID, freq_desc_pnw), |
| 150 | INTEL_CPU_FAM6(ATOM_SALTWELL_TABLET, freq_desc_clv), |
| 151 | INTEL_CPU_FAM6(ATOM_SILVERMONT, freq_desc_byt), |
| 152 | INTEL_CPU_FAM6(ATOM_SILVERMONT_MID, freq_desc_tng), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 153 | INTEL_CPU_FAM6(ATOM_AIRMONT, freq_desc_cht), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 154 | INTEL_CPU_FAM6(ATOM_AIRMONT_MID, freq_desc_ann), |
| 155 | INTEL_CPU_FAM6(ATOM_AIRMONT_NP, freq_desc_lgm), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 156 | {} |
| 157 | }; |
| 158 | |
| 159 | /* |
| 160 | * MSR-based CPU/TSC frequency discovery for certain CPUs. |
| 161 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 162 | * Set global "lapic_timer_period" to bus_clock_cycles/jiffy |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 163 | * Return processor base frequency in KHz, or 0 on failure. |
| 164 | */ |
| 165 | unsigned long cpu_khz_from_msr(void) |
| 166 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 167 | u32 lo, hi, ratio, freq, tscref; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 168 | const struct freq_desc *freq_desc; |
| 169 | const struct x86_cpu_id *id; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 170 | const struct muldiv *md; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 171 | unsigned long res; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 172 | int index; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 173 | |
| 174 | id = x86_match_cpu(tsc_msr_cpu_ids); |
| 175 | if (!id) |
| 176 | return 0; |
| 177 | |
| 178 | freq_desc = (struct freq_desc *)id->driver_data; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 179 | if (freq_desc->use_msr_plat) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | rdmsr(MSR_PLATFORM_INFO, lo, hi); |
| 181 | ratio = (lo >> 8) & 0xff; |
| 182 | } else { |
| 183 | rdmsr(MSR_IA32_PERF_STATUS, lo, hi); |
| 184 | ratio = (hi >> 8) & 0x1f; |
| 185 | } |
| 186 | |
| 187 | /* Get FSB FREQ ID */ |
| 188 | rdmsr(MSR_FSB_FREQ, lo, hi); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 189 | index = lo & freq_desc->mask; |
| 190 | md = &freq_desc->muldiv[index]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 191 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 192 | /* |
| 193 | * Note this also catches cases where the index points to an unpopulated |
| 194 | * part of muldiv, in that case the else will set freq and res to 0. |
| 195 | */ |
| 196 | if (md->divider) { |
| 197 | tscref = TSC_REFERENCE_KHZ * md->multiplier; |
| 198 | freq = DIV_ROUND_CLOSEST(tscref, md->divider); |
| 199 | /* |
| 200 | * Multiplying by ratio before the division has better |
| 201 | * accuracy than just calculating freq * ratio. |
| 202 | */ |
| 203 | res = DIV_ROUND_CLOSEST(tscref * ratio, md->divider); |
| 204 | } else { |
| 205 | freq = freq_desc->freqs[index]; |
| 206 | res = freq * ratio; |
| 207 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 208 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 209 | if (freq == 0) |
| 210 | pr_err("Error MSR_FSB_FREQ index %d is unknown\n", index); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 211 | |
| 212 | #ifdef CONFIG_X86_LOCAL_APIC |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 213 | lapic_timer_period = (freq * 1000) / HZ; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 214 | #endif |
| 215 | |
| 216 | /* |
| 217 | * TSC frequency determined by MSR is always considered "known" |
| 218 | * because it is reported by HW. |
| 219 | * Another fact is that on MSR capable platforms, PIT/HPET is |
| 220 | * generally not available so calibration won't work at all. |
| 221 | */ |
| 222 | setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ); |
| 223 | |
| 224 | /* |
| 225 | * Unfortunately there is no way for hardware to tell whether the |
| 226 | * TSC is reliable. We were told by silicon design team that TSC |
| 227 | * on Atom SoCs are always "reliable". TSC is also the only |
| 228 | * reliable clocksource on these SoCs (HPET is either not present |
| 229 | * or not functional) so mark TSC reliable which removes the |
| 230 | * requirement for a watchdog clocksource. |
| 231 | */ |
| 232 | setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE); |
| 233 | |
| 234 | return res; |
| 235 | } |