History

x86/tsc_msr: Make MSR derived TSC frequency more accurate

The "Intel 64 and IA-32 Architectures Software Developer’s Manual Volume 4: Model-Specific Registers" has the following table for the values from freq_desc_byt: 000B: 083.3 MHz 001B: 100.0 MHz 010B: 133.3 MHz 011B: 116.7 MHz 100B: 080.0 MHz Notice how for e.g the 83.3 MHz value there are 3 significant digits, which translates to an accuracy of a 1000 ppm, where as a typical crystal oscillator is 20 - 100 ppm, so the accuracy of the frequency format used in the Software Developer’s Manual is not really helpful. As far as we know Bay Trail SoCs use a 25 MHz crystal and Cherry Trail uses a 19.2 MHz crystal, the crystal is the source clock for a root PLL which outputs 1600 and 100 MHz. It is unclear if the root PLL outputs are used directly by the CPU clock PLL or if there is another PLL in between. This does not matter though, we can model the chain of PLLs as a single PLL with a quotient equal to the quotients of all PLLs in the chain multiplied. So we can create a simplified model of the CPU clock setup using a reference clock of 100 MHz plus a quotient which gets us as close to the frequency from the SDM as possible. For the 83.3 MHz example from above this would give 100 MHz * 5 / 6 = 83 and 1/3 MHz, which matches exactly what has been measured on actual hardware. Use a simplified PLL model with a reference clock of 100 MHz for all Bay and Cherry Trail models. This has been tested on the following models: CPU freq before: CPU freq after: Intel N2840 2165.800 MHz 2166.667 MHz Intel Z3736 1332.800 MHz 1333.333 MHz Intel Z3775 1466.300 MHz 1466.667 MHz Intel Z8350 1440.000 MHz 1440.000 MHz Intel Z8750 1600.000 MHz 1600.000 MHz This fixes the time drifting by about 1 second per hour (20 - 30 seconds per day) on (some) devices which rely on the tsc_msr.c code to determine the TSC frequency.

Hans de Goede committed 6 years ago

Commit fac01d11 parent c8810e21 file · +86 −11Browse files

Unified · Split

arch/x86/kernel/tsc_msr.c+86−11
@@ -17,6 +17,28 @@ · 16 unmodified lines
17 17
18#define MAX_NUM_FREQS 16 /* 4 bits to select the frequency */18#define MAX_NUM_FREQS 16 /* 4 bits to select the frequency */
19 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
37struct muldiv {
38 u32 multiplier;
39 u32 divider;
40};
41
20/*42/*
21 * If MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be43 * If MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be
22 * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40].44 * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40].
@@ -26,6 +48,11 @@ · 3 unmodified lines
26 */48 */
27struct freq_desc {49struct freq_desc {
28 bool use_msr_plat;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 */
29 u32 freqs[MAX_NUM_FREQS];56 u32 freqs[MAX_NUM_FREQS];
30 u32 mask;57 u32 mask;
31};58};
@@ -47,31 +74,66 @@ static const struct freq_desc freq_desc_clv = { · 15 unmodified lines
47 .mask = 0x07,74 .mask = 0x07,
48};75};
49 76
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 */
50static const struct freq_desc freq_desc_byt = {85static const struct freq_desc freq_desc_byt = {
51 .use_msr_plat = true,86 .use_msr_plat = true,
52 .freqs = { 83300, 100000, 133300, 116700, 80000, 0, 0, 0 },87 .muldiv = { { 5, 6 }, { 1, 1 }, { 4, 3 }, { 7, 6 },
88 { 4, 5 } },
53 .mask = 0x07,89 .mask = 0x07,
54};90};
55 91
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 */
56static const struct freq_desc freq_desc_cht = {104static const struct freq_desc freq_desc_cht = {
57 .use_msr_plat = true,105 .use_msr_plat = true,
58 .freqs = { 83300, 100000, 133300, 116700, 80000, 93300, 90000,106 .muldiv = { { 5, 6 }, { 1, 1 }, { 4, 3 }, { 7, 6 },
59 88900, 87500 },107 { 4, 5 }, { 14, 15 }, { 9, 10 }, { 8, 9 },
108 { 7, 8 } },
60 .mask = 0x0f,109 .mask = 0x0f,
61};110};
62 111
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 */
63static const struct freq_desc freq_desc_tng = {117static const struct freq_desc freq_desc_tng = {
64 .use_msr_plat = true,118 .use_msr_plat = true,
65 .freqs = { 0, 100000, 133300, 0, 0, 0, 0, 0 },119 .muldiv = { { 0, 0 }, { 1, 1 }, { 4, 3 } },
66 .mask = 0x07,120 .mask = 0x07,
67};121};
68 122
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 */
69static const struct freq_desc freq_desc_ann = {130static const struct freq_desc freq_desc_ann = {
70 .use_msr_plat = true,131 .use_msr_plat = true,
71 .freqs = { 83300, 100000, 133300, 100000, 0, 0, 0, 0 },132 .muldiv = { { 5, 6 }, { 1, 1 }, { 4, 3 }, { 1, 1 } },
72 .mask = 0x0f,133 .mask = 0x0f,
73};134};
74 135
136/* 24 MHz crystal? : 24 * 13 / 4 = 78 MHz */
75static const struct freq_desc freq_desc_lgm = {137static const struct freq_desc freq_desc_lgm = {
76 .use_msr_plat = true,138 .use_msr_plat = true,
77 .freqs = { 78000, 78000, 78000, 78000, 78000, 78000, 78000, 78000 },139 .freqs = { 78000, 78000, 78000, 78000, 78000, 78000, 78000, 78000 },
@@ -97,9 +159,10 @@ static const struct x86_cpu_id tsc_msr_cpu_ids[] = { · 19 unmodified lines
97 */159 */
98unsigned long cpu_khz_from_msr(void)160unsigned long cpu_khz_from_msr(void)
99{161{
100 u32 lo, hi, ratio, freq;162 u32 lo, hi, ratio, freq, tscref;
101 const struct freq_desc *freq_desc;163 const struct freq_desc *freq_desc;
102 const struct x86_cpu_id *id;164 const struct x86_cpu_id *id;
165 const struct muldiv *md;
103 unsigned long res;166 unsigned long res;
104 int index;167 int index;
105 168
@@ -119,12 +182,24 @@ unsigned long cpu_khz_from_msr(void) · 13 unmodified lines
119 /* Get FSB FREQ ID */182 /* Get FSB FREQ ID */
120 rdmsr(MSR_FSB_FREQ, lo, hi);183 rdmsr(MSR_FSB_FREQ, lo, hi);
121 index = lo & freq_desc->mask;184 index = lo & freq_desc->mask;
185 md = &freq_desc->muldiv[index];
122 186
123 /* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */187 /*
124 freq = freq_desc->freqs[index];188 * Note this also catches cases where the index points to an unpopulated
125 189 * part of muldiv, in that case the else will set freq and res to 0.
126 /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */190 */
127 res = freq * ratio;191 if (md->divider) {
192 tscref = TSC_REFERENCE_KHZ * md->multiplier;
193 freq = DIV_ROUND_CLOSEST(tscref, md->divider);
194 /*
195 * Multiplying by ratio before the division has better
196 * accuracy than just calculating freq * ratio.
197 */
198 res = DIV_ROUND_CLOSEST(tscref * ratio, md->divider);
199 } else {
200 freq = freq_desc->freqs[index];
201 res = freq * ratio;
202 }
128 203
129 if (freq == 0)204 if (freq == 0)
130 pr_err("Error MSR_FSB_FREQ index %d is unknown\n", index);205 pr_err("Error MSR_FSB_FREQ index %d is unknown\n", index);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t