x86: Do TSC MSR calibration only for known/supported CPUs
[oweals/u-boot.git] / arch / x86 / lib / tsc_timer.c
1 /*
2  * Copyright (c) 2012 The Chromium OS Authors.
3  *
4  * TSC calibration codes are adapted from Linux kernel
5  * arch/x86/kernel/tsc_msr.c and arch/x86/kernel/tsc.c
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9
10 #include <common.h>
11 #include <malloc.h>
12 #include <asm/io.h>
13 #include <asm/i8254.h>
14 #include <asm/ibmpc.h>
15 #include <asm/msr.h>
16 #include <asm/u-boot-x86.h>
17
18 /* CPU reference clock frequency: in KHz */
19 #define FREQ_83         83200
20 #define FREQ_100        99840
21 #define FREQ_133        133200
22 #define FREQ_166        166400
23
24 #define MAX_NUM_FREQS   8
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 /*
29  * According to Intel 64 and IA-32 System Programming Guide,
30  * if MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be
31  * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40].
32  * Unfortunately some Intel Atom SoCs aren't quite compliant to this,
33  * so we need manually differentiate SoC families. This is what the
34  * field msr_plat does.
35  */
36 struct freq_desc {
37         u8 x86_family;  /* CPU family */
38         u8 x86_model;   /* model */
39         u8 msr_plat;    /* 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */
40         u32 freqs[MAX_NUM_FREQS];
41 };
42
43 static struct freq_desc freq_desc_tables[] = {
44         /* PNW */
45         { 6, 0x27, 0, { 0, 0, 0, 0, 0, FREQ_100, 0, FREQ_83 } },
46         /* CLV+ */
47         { 6, 0x35, 0, { 0, FREQ_133, 0, 0, 0, FREQ_100, 0, FREQ_83 } },
48         /* TNG */
49         { 6, 0x4a, 1, { 0, FREQ_100, FREQ_133, 0, 0, 0, 0, 0 } },
50         /* VLV2 */
51         { 6, 0x37, 1, { FREQ_83, FREQ_100, FREQ_133, FREQ_166, 0, 0, 0, 0 } },
52         /* ANN */
53         { 6, 0x5a, 1, { FREQ_83, FREQ_100, FREQ_133, FREQ_100, 0, 0, 0, 0 } },
54 };
55
56 static int match_cpu(u8 family, u8 model)
57 {
58         int i;
59
60         for (i = 0; i < ARRAY_SIZE(freq_desc_tables); i++) {
61                 if ((family == freq_desc_tables[i].x86_family) &&
62                     (model == freq_desc_tables[i].x86_model))
63                         return i;
64         }
65
66         return -1;
67 }
68
69 /* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */
70 #define id_to_freq(cpu_index, freq_id) \
71         (freq_desc_tables[cpu_index].freqs[freq_id])
72
73 /*
74  * Do MSR calibration only for known/supported CPUs.
75  *
76  * Returns the calibration value or 0 if MSR calibration failed.
77  */
78 static unsigned long try_msr_calibrate_tsc(void)
79 {
80         u32 lo, hi, ratio, freq_id, freq;
81         unsigned long res;
82         int cpu_index;
83
84         cpu_index = match_cpu(gd->arch.x86, gd->arch.x86_model);
85         if (cpu_index < 0)
86                 return 0;
87
88         if (freq_desc_tables[cpu_index].msr_plat) {
89                 rdmsr(MSR_PLATFORM_INFO, lo, hi);
90                 ratio = (lo >> 8) & 0x1f;
91         } else {
92                 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
93                 ratio = (hi >> 8) & 0x1f;
94         }
95         debug("Maximum core-clock to bus-clock ratio: 0x%x\n", ratio);
96
97         if (!ratio)
98                 goto fail;
99
100         /* Get FSB FREQ ID */
101         rdmsr(MSR_FSB_FREQ, lo, hi);
102         freq_id = lo & 0x7;
103         freq = id_to_freq(cpu_index, freq_id);
104         debug("Resolved frequency ID: %u, frequency: %u KHz\n", freq_id, freq);
105         if (!freq)
106                 goto fail;
107
108         /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */
109         res = freq * ratio / 1000;
110         debug("TSC runs at %lu MHz\n", res);
111
112         return res;
113
114 fail:
115         debug("Fast TSC calibration using MSR failed\n");
116         return 0;
117 }
118
119 void timer_set_base(u64 base)
120 {
121         gd->arch.tsc_base = base;
122 }
123
124 /*
125  * Get the number of CPU time counter ticks since it was read first time after
126  * restart. This yields a free running counter guaranteed to take almost 6
127  * years to wrap around even at 100GHz clock rate.
128  */
129 u64 __attribute__((no_instrument_function)) get_ticks(void)
130 {
131         u64 now_tick = rdtsc();
132
133         /* We assume that 0 means the base hasn't been set yet */
134         if (!gd->arch.tsc_base)
135                 panic("No tick base available");
136         return now_tick - gd->arch.tsc_base;
137 }
138
139 /* Get the speed of the TSC timer in MHz */
140 unsigned __attribute__((no_instrument_function)) long get_tbclk_mhz(void)
141 {
142         unsigned long fast_calibrate;
143
144         fast_calibrate = try_msr_calibrate_tsc();
145         if (!fast_calibrate)
146                 panic("TSC frequency is ZERO");
147
148         return fast_calibrate;
149 }
150
151 unsigned long get_tbclk(void)
152 {
153         return get_tbclk_mhz() * 1000 * 1000;
154 }
155
156 static ulong get_ms_timer(void)
157 {
158         return (get_ticks() * 1000) / get_tbclk();
159 }
160
161 ulong get_timer(ulong base)
162 {
163         return get_ms_timer() - base;
164 }
165
166 ulong __attribute__((no_instrument_function)) timer_get_us(void)
167 {
168         return get_ticks() / get_tbclk_mhz();
169 }
170
171 ulong timer_get_boot_us(void)
172 {
173         return timer_get_us();
174 }
175
176 void __udelay(unsigned long usec)
177 {
178         u64 now = get_ticks();
179         u64 stop;
180
181         stop = now + usec * get_tbclk_mhz();
182
183         while ((int64_t)(stop - get_ticks()) > 0)
184                 ;
185 }
186
187 int timer_init(void)
188 {
189 #ifdef CONFIG_SYS_PCAT_TIMER
190         /* Set up the PCAT timer if required */
191         pcat_timer_init();
192 #endif
193
194         return 0;
195 }