Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
| 4 | * |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 5 | * TSC calibration codes are adapted from Linux kernel |
| 6 | * arch/x86/kernel/tsc_msr.c and arch/x86/kernel/tsc.c |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 12 | #include <time.h> |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 13 | #include <timer.h> |
Bin Meng | 0b992e4 | 2017-07-25 20:12:01 -0700 | [diff] [blame] | 14 | #include <asm/cpu.h> |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <asm/i8254.h> |
| 17 | #include <asm/ibmpc.h> |
| 18 | #include <asm/msr.h> |
| 19 | #include <asm/u-boot-x86.h> |
| 20 | |
Bin Meng | 3df39ef | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 21 | #define MAX_NUM_FREQS 9 |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 22 | |
Bernhard Messerklinger | ca7db86 | 2019-01-07 12:14:40 +0100 | [diff] [blame] | 23 | #define INTEL_FAM6_SKYLAKE_MOBILE 0x4E |
| 24 | #define INTEL_FAM6_ATOM_GOLDMONT 0x5C /* Apollo Lake */ |
| 25 | #define INTEL_FAM6_SKYLAKE_DESKTOP 0x5E |
| 26 | #define INTEL_FAM6_ATOM_GOLDMONT_X 0x5F /* Denverton */ |
| 27 | #define INTEL_FAM6_KABYLAKE_MOBILE 0x8E |
| 28 | #define INTEL_FAM6_KABYLAKE_DESKTOP 0x9E |
| 29 | |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Bernhard Messerklinger | ca7db86 | 2019-01-07 12:14:40 +0100 | [diff] [blame] | 32 | /* |
| 33 | * native_calibrate_tsc |
| 34 | * Determine TSC frequency via CPUID, else return 0. |
| 35 | */ |
| 36 | static unsigned long native_calibrate_tsc(void) |
| 37 | { |
| 38 | struct cpuid_result tsc_info; |
| 39 | unsigned int crystal_freq; |
| 40 | |
| 41 | if (gd->arch.x86_vendor != X86_VENDOR_INTEL) |
| 42 | return 0; |
| 43 | |
| 44 | if (cpuid_eax(0) < 0x15) |
| 45 | return 0; |
| 46 | |
| 47 | tsc_info = cpuid(0x15); |
| 48 | |
| 49 | if (tsc_info.ebx == 0 || tsc_info.eax == 0) |
| 50 | return 0; |
| 51 | |
| 52 | crystal_freq = tsc_info.ecx / 1000; |
| 53 | |
| 54 | if (!crystal_freq) { |
| 55 | switch (gd->arch.x86_model) { |
| 56 | case INTEL_FAM6_SKYLAKE_MOBILE: |
| 57 | case INTEL_FAM6_SKYLAKE_DESKTOP: |
| 58 | case INTEL_FAM6_KABYLAKE_MOBILE: |
| 59 | case INTEL_FAM6_KABYLAKE_DESKTOP: |
| 60 | crystal_freq = 24000; /* 24.0 MHz */ |
| 61 | break; |
| 62 | case INTEL_FAM6_ATOM_GOLDMONT_X: |
| 63 | crystal_freq = 25000; /* 25.0 MHz */ |
| 64 | break; |
| 65 | case INTEL_FAM6_ATOM_GOLDMONT: |
| 66 | crystal_freq = 19200; /* 19.2 MHz */ |
| 67 | break; |
| 68 | default: |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return (crystal_freq * tsc_info.ebx / tsc_info.eax) / 1000; |
| 74 | } |
| 75 | |
Christian Gmeiner | acc2482 | 2018-05-14 11:32:17 +0200 | [diff] [blame] | 76 | static unsigned long cpu_mhz_from_cpuid(void) |
| 77 | { |
| 78 | if (gd->arch.x86_vendor != X86_VENDOR_INTEL) |
| 79 | return 0; |
| 80 | |
| 81 | if (cpuid_eax(0) < 0x16) |
| 82 | return 0; |
| 83 | |
| 84 | return cpuid_eax(0x16); |
| 85 | } |
| 86 | |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 87 | /* |
| 88 | * According to Intel 64 and IA-32 System Programming Guide, |
| 89 | * if MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be |
| 90 | * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40]. |
| 91 | * Unfortunately some Intel Atom SoCs aren't quite compliant to this, |
| 92 | * so we need manually differentiate SoC families. This is what the |
| 93 | * field msr_plat does. |
| 94 | */ |
| 95 | struct freq_desc { |
| 96 | u8 x86_family; /* CPU family */ |
| 97 | u8 x86_model; /* model */ |
Simon Glass | 5c1b685 | 2014-11-12 22:42:04 -0700 | [diff] [blame] | 98 | /* 2: use 100MHz, 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */ |
| 99 | u8 msr_plat; |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 100 | u32 freqs[MAX_NUM_FREQS]; |
| 101 | }; |
| 102 | |
| 103 | static struct freq_desc freq_desc_tables[] = { |
| 104 | /* PNW */ |
Bin Meng | 3df39ef | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 105 | { 6, 0x27, 0, { 0, 0, 0, 0, 0, 99840, 0, 83200, 0 } }, |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 106 | /* CLV+ */ |
Bin Meng | 3df39ef | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 107 | { 6, 0x35, 0, { 0, 133200, 0, 0, 0, 99840, 0, 83200, 0 } }, |
Bin Meng | c636774 | 2017-07-25 20:12:03 -0700 | [diff] [blame] | 108 | /* TNG - Intel Atom processor Z3400 series */ |
Bin Meng | 3df39ef | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 109 | { 6, 0x4a, 1, { 0, 100000, 133300, 0, 0, 0, 0, 0, 0 } }, |
Bin Meng | c636774 | 2017-07-25 20:12:03 -0700 | [diff] [blame] | 110 | /* VLV2 - Intel Atom processor E3000, Z3600, Z3700 series */ |
Bin Meng | 3df39ef | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 111 | { 6, 0x37, 1, { 83300, 100000, 133300, 116700, 80000, 0, 0, 0, 0 } }, |
Bin Meng | c636774 | 2017-07-25 20:12:03 -0700 | [diff] [blame] | 112 | /* ANN - Intel Atom processor Z3500 series */ |
Bin Meng | 3df39ef | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 113 | { 6, 0x5a, 1, { 83300, 100000, 133300, 100000, 0, 0, 0, 0, 0 } }, |
| 114 | /* AMT - Intel Atom processor X7-Z8000 and X5-Z8000 series */ |
| 115 | { 6, 0x4c, 1, { 83300, 100000, 133300, 116700, |
| 116 | 80000, 93300, 90000, 88900, 87500 } }, |
Simon Glass | 5c1b685 | 2014-11-12 22:42:04 -0700 | [diff] [blame] | 117 | /* Ivybridge */ |
Bin Meng | 3df39ef | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 118 | { 6, 0x3a, 2, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static int match_cpu(u8 family, u8 model) |
| 122 | { |
| 123 | int i; |
| 124 | |
| 125 | for (i = 0; i < ARRAY_SIZE(freq_desc_tables); i++) { |
| 126 | if ((family == freq_desc_tables[i].x86_family) && |
| 127 | (model == freq_desc_tables[i].x86_model)) |
| 128 | return i; |
| 129 | } |
| 130 | |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | /* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */ |
| 135 | #define id_to_freq(cpu_index, freq_id) \ |
| 136 | (freq_desc_tables[cpu_index].freqs[freq_id]) |
| 137 | |
| 138 | /* |
Bin Meng | 167a401 | 2017-07-25 20:12:05 -0700 | [diff] [blame] | 139 | * TSC on Intel Atom SoCs capable of determining TSC frequency by MSR is |
| 140 | * reliable and the frequency is known (provided by HW). |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 141 | * |
Bin Meng | 167a401 | 2017-07-25 20:12:05 -0700 | [diff] [blame] | 142 | * On these platforms PIT/HPET is generally not available so calibration won't |
| 143 | * work at all and there is no other clocksource to act as a watchdog for the |
| 144 | * TSC, so we have no other choice than to trust it. |
| 145 | * |
| 146 | * Returns the TSC frequency in MHz or 0 if HW does not provide it. |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 147 | */ |
Bin Meng | 167a401 | 2017-07-25 20:12:05 -0700 | [diff] [blame] | 148 | static unsigned long __maybe_unused cpu_mhz_from_msr(void) |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 149 | { |
| 150 | u32 lo, hi, ratio, freq_id, freq; |
| 151 | unsigned long res; |
| 152 | int cpu_index; |
| 153 | |
Bin Meng | 0b992e4 | 2017-07-25 20:12:01 -0700 | [diff] [blame] | 154 | if (gd->arch.x86_vendor != X86_VENDOR_INTEL) |
| 155 | return 0; |
| 156 | |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 157 | cpu_index = match_cpu(gd->arch.x86, gd->arch.x86_model); |
| 158 | if (cpu_index < 0) |
| 159 | return 0; |
| 160 | |
| 161 | if (freq_desc_tables[cpu_index].msr_plat) { |
| 162 | rdmsr(MSR_PLATFORM_INFO, lo, hi); |
Bin Meng | d92e9c8 | 2017-07-25 20:12:00 -0700 | [diff] [blame] | 163 | ratio = (lo >> 8) & 0xff; |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 164 | } else { |
| 165 | rdmsr(MSR_IA32_PERF_STATUS, lo, hi); |
| 166 | ratio = (hi >> 8) & 0x1f; |
| 167 | } |
| 168 | debug("Maximum core-clock to bus-clock ratio: 0x%x\n", ratio); |
| 169 | |
Simon Glass | 5c1b685 | 2014-11-12 22:42:04 -0700 | [diff] [blame] | 170 | if (freq_desc_tables[cpu_index].msr_plat == 2) { |
| 171 | /* TODO: Figure out how best to deal with this */ |
Bin Meng | f575715 | 2017-07-25 20:12:04 -0700 | [diff] [blame] | 172 | freq = 100000; |
Simon Glass | 5c1b685 | 2014-11-12 22:42:04 -0700 | [diff] [blame] | 173 | debug("Using frequency: %u KHz\n", freq); |
| 174 | } else { |
| 175 | /* Get FSB FREQ ID */ |
| 176 | rdmsr(MSR_FSB_FREQ, lo, hi); |
| 177 | freq_id = lo & 0x7; |
| 178 | freq = id_to_freq(cpu_index, freq_id); |
| 179 | debug("Resolved frequency ID: %u, frequency: %u KHz\n", |
| 180 | freq_id, freq); |
| 181 | } |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 182 | |
| 183 | /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */ |
| 184 | res = freq * ratio / 1000; |
| 185 | debug("TSC runs at %lu MHz\n", res); |
| 186 | |
| 187 | return res; |
Bin Meng | 076bb44 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Bin Meng | 80de049 | 2014-11-09 22:19:25 +0800 | [diff] [blame] | 190 | /* |
| 191 | * This reads the current MSB of the PIT counter, and |
| 192 | * checks if we are running on sufficiently fast and |
| 193 | * non-virtualized hardware. |
| 194 | * |
| 195 | * Our expectations are: |
| 196 | * |
| 197 | * - the PIT is running at roughly 1.19MHz |
| 198 | * |
| 199 | * - each IO is going to take about 1us on real hardware, |
| 200 | * but we allow it to be much faster (by a factor of 10) or |
| 201 | * _slightly_ slower (ie we allow up to a 2us read+counter |
| 202 | * update - anything else implies a unacceptably slow CPU |
| 203 | * or PIT for the fast calibration to work. |
| 204 | * |
| 205 | * - with 256 PIT ticks to read the value, we have 214us to |
| 206 | * see the same MSB (and overhead like doing a single TSC |
| 207 | * read per MSB value etc). |
| 208 | * |
| 209 | * - We're doing 2 reads per loop (LSB, MSB), and we expect |
| 210 | * them each to take about a microsecond on real hardware. |
| 211 | * So we expect a count value of around 100. But we'll be |
| 212 | * generous, and accept anything over 50. |
| 213 | * |
| 214 | * - if the PIT is stuck, and we see *many* more reads, we |
| 215 | * return early (and the next caller of pit_expect_msb() |
| 216 | * then consider it a failure when they don't see the |
| 217 | * next expected value). |
| 218 | * |
| 219 | * These expectations mean that we know that we have seen the |
| 220 | * transition from one expected value to another with a fairly |
| 221 | * high accuracy, and we didn't miss any events. We can thus |
| 222 | * use the TSC value at the transitions to calculate a pretty |
| 223 | * good value for the TSC frequencty. |
| 224 | */ |
| 225 | static inline int pit_verify_msb(unsigned char val) |
| 226 | { |
| 227 | /* Ignore LSB */ |
| 228 | inb(0x42); |
| 229 | return inb(0x42) == val; |
| 230 | } |
| 231 | |
| 232 | static inline int pit_expect_msb(unsigned char val, u64 *tscp, |
| 233 | unsigned long *deltap) |
| 234 | { |
| 235 | int count; |
| 236 | u64 tsc = 0, prev_tsc = 0; |
| 237 | |
| 238 | for (count = 0; count < 50000; count++) { |
| 239 | if (!pit_verify_msb(val)) |
| 240 | break; |
| 241 | prev_tsc = tsc; |
| 242 | tsc = rdtsc(); |
| 243 | } |
| 244 | *deltap = rdtsc() - prev_tsc; |
| 245 | *tscp = tsc; |
| 246 | |
| 247 | /* |
| 248 | * We require _some_ success, but the quality control |
| 249 | * will be based on the error terms on the TSC values. |
| 250 | */ |
| 251 | return count > 5; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * How many MSB values do we want to see? We aim for |
| 256 | * a maximum error rate of 500ppm (in practice the |
| 257 | * real error is much smaller), but refuse to spend |
| 258 | * more than 50ms on it. |
| 259 | */ |
| 260 | #define MAX_QUICK_PIT_MS 50 |
| 261 | #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256) |
| 262 | |
Bin Meng | 3ba6a0f | 2015-01-06 22:14:14 +0800 | [diff] [blame] | 263 | static unsigned long __maybe_unused quick_pit_calibrate(void) |
Bin Meng | 80de049 | 2014-11-09 22:19:25 +0800 | [diff] [blame] | 264 | { |
| 265 | int i; |
| 266 | u64 tsc, delta; |
| 267 | unsigned long d1, d2; |
| 268 | |
| 269 | /* Set the Gate high, disable speaker */ |
| 270 | outb((inb(0x61) & ~0x02) | 0x01, 0x61); |
| 271 | |
| 272 | /* |
| 273 | * Counter 2, mode 0 (one-shot), binary count |
| 274 | * |
| 275 | * NOTE! Mode 2 decrements by two (and then the |
| 276 | * output is flipped each time, giving the same |
| 277 | * final output frequency as a decrement-by-one), |
| 278 | * so mode 0 is much better when looking at the |
| 279 | * individual counts. |
| 280 | */ |
| 281 | outb(0xb0, 0x43); |
| 282 | |
| 283 | /* Start at 0xffff */ |
| 284 | outb(0xff, 0x42); |
| 285 | outb(0xff, 0x42); |
| 286 | |
| 287 | /* |
| 288 | * The PIT starts counting at the next edge, so we |
| 289 | * need to delay for a microsecond. The easiest way |
| 290 | * to do that is to just read back the 16-bit counter |
| 291 | * once from the PIT. |
| 292 | */ |
| 293 | pit_verify_msb(0); |
| 294 | |
| 295 | if (pit_expect_msb(0xff, &tsc, &d1)) { |
| 296 | for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) { |
| 297 | if (!pit_expect_msb(0xff-i, &delta, &d2)) |
| 298 | break; |
| 299 | |
| 300 | /* |
| 301 | * Iterate until the error is less than 500 ppm |
| 302 | */ |
| 303 | delta -= tsc; |
| 304 | if (d1+d2 >= delta >> 11) |
| 305 | continue; |
| 306 | |
| 307 | /* |
| 308 | * Check the PIT one more time to verify that |
| 309 | * all TSC reads were stable wrt the PIT. |
| 310 | * |
| 311 | * This also guarantees serialization of the |
| 312 | * last cycle read ('d2') in pit_expect_msb. |
| 313 | */ |
| 314 | if (!pit_verify_msb(0xfe - i)) |
| 315 | break; |
| 316 | goto success; |
| 317 | } |
| 318 | } |
| 319 | debug("Fast TSC calibration failed\n"); |
| 320 | return 0; |
| 321 | |
| 322 | success: |
| 323 | /* |
| 324 | * Ok, if we get here, then we've seen the |
| 325 | * MSB of the PIT decrement 'i' times, and the |
| 326 | * error has shrunk to less than 500 ppm. |
| 327 | * |
| 328 | * As a result, we can depend on there not being |
| 329 | * any odd delays anywhere, and the TSC reads are |
| 330 | * reliable (within the error). |
| 331 | * |
| 332 | * kHz = ticks / time-in-seconds / 1000; |
| 333 | * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000 |
| 334 | * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000) |
| 335 | */ |
| 336 | delta *= PIT_TICK_RATE; |
| 337 | delta /= (i*256*1000); |
| 338 | debug("Fast TSC calibration using PIT\n"); |
| 339 | return delta / 1000; |
| 340 | } |
| 341 | |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 342 | /* Get the speed of the TSC timer in MHz */ |
Bin Meng | 2f80fc5 | 2015-11-13 00:11:20 -0800 | [diff] [blame] | 343 | unsigned notrace long get_tbclk_mhz(void) |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 344 | { |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 345 | return get_tbclk() / 1000000; |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 348 | static ulong get_ms_timer(void) |
| 349 | { |
| 350 | return (get_ticks() * 1000) / get_tbclk(); |
| 351 | } |
| 352 | |
| 353 | ulong get_timer(ulong base) |
| 354 | { |
| 355 | return get_ms_timer() - base; |
| 356 | } |
| 357 | |
Bin Meng | 2f80fc5 | 2015-11-13 00:11:20 -0800 | [diff] [blame] | 358 | ulong notrace timer_get_us(void) |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 359 | { |
| 360 | return get_ticks() / get_tbclk_mhz(); |
| 361 | } |
| 362 | |
| 363 | ulong timer_get_boot_us(void) |
| 364 | { |
| 365 | return timer_get_us(); |
| 366 | } |
| 367 | |
| 368 | void __udelay(unsigned long usec) |
| 369 | { |
| 370 | u64 now = get_ticks(); |
| 371 | u64 stop; |
| 372 | |
| 373 | stop = now + usec * get_tbclk_mhz(); |
| 374 | |
| 375 | while ((int64_t)(stop - get_ticks()) > 0) |
Miao Yan | 417576c | 2015-07-27 19:16:07 +0800 | [diff] [blame] | 376 | #if defined(CONFIG_QEMU) && defined(CONFIG_SMP) |
| 377 | /* |
| 378 | * Add a 'pause' instruction on qemu target, |
| 379 | * to give other VCPUs a chance to run. |
| 380 | */ |
| 381 | asm volatile("pause"); |
| 382 | #else |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 383 | ; |
Miao Yan | 417576c | 2015-07-27 19:16:07 +0800 | [diff] [blame] | 384 | #endif |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 387 | static int tsc_timer_get_count(struct udevice *dev, u64 *count) |
| 388 | { |
| 389 | u64 now_tick = rdtsc(); |
| 390 | |
| 391 | *count = now_tick - gd->arch.tsc_base; |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Bin Meng | 6ce3836 | 2018-10-13 20:52:10 -0700 | [diff] [blame] | 396 | static void tsc_timer_ensure_setup(bool early) |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 397 | { |
Simon Glass | a478a26 | 2019-10-20 21:37:47 -0600 | [diff] [blame] | 398 | if (gd->arch.tsc_inited) |
Simon Glass | 2ff50f5 | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 399 | return; |
Simon Glass | 77dd7c6 | 2019-12-06 21:41:49 -0700 | [diff] [blame^] | 400 | if (IS_ENABLED(CONFIG_X86_TSC_READ_BASE)) |
| 401 | gd->arch.tsc_base = rdtsc(); |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 402 | |
Simon Glass | 2ff50f5 | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 403 | if (!gd->arch.clock_rate) { |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 404 | unsigned long fast_calibrate; |
| 405 | |
Bernhard Messerklinger | ca7db86 | 2019-01-07 12:14:40 +0100 | [diff] [blame] | 406 | fast_calibrate = native_calibrate_tsc(); |
| 407 | if (fast_calibrate) |
| 408 | goto done; |
| 409 | |
Christian Gmeiner | acc2482 | 2018-05-14 11:32:17 +0200 | [diff] [blame] | 410 | fast_calibrate = cpu_mhz_from_cpuid(); |
| 411 | if (fast_calibrate) |
| 412 | goto done; |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 413 | |
Christian Gmeiner | acc2482 | 2018-05-14 11:32:17 +0200 | [diff] [blame] | 414 | fast_calibrate = cpu_mhz_from_msr(); |
| 415 | if (fast_calibrate) |
| 416 | goto done; |
| 417 | |
| 418 | fast_calibrate = quick_pit_calibrate(); |
| 419 | if (fast_calibrate) |
| 420 | goto done; |
| 421 | |
Bin Meng | 6ce3836 | 2018-10-13 20:52:10 -0700 | [diff] [blame] | 422 | if (early) |
| 423 | fast_calibrate = CONFIG_X86_TSC_TIMER_EARLY_FREQ; |
Bin Meng | 165db7c | 2018-08-10 02:39:36 -0700 | [diff] [blame] | 424 | else |
| 425 | return; |
Christian Gmeiner | acc2482 | 2018-05-14 11:32:17 +0200 | [diff] [blame] | 426 | |
| 427 | done: |
Simon Glass | 2ff50f5 | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 428 | gd->arch.clock_rate = fast_calibrate * 1000000; |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 429 | } |
Simon Glass | a478a26 | 2019-10-20 21:37:47 -0600 | [diff] [blame] | 430 | gd->arch.tsc_inited = true; |
Simon Glass | 2ff50f5 | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static int tsc_timer_probe(struct udevice *dev) |
| 434 | { |
| 435 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 436 | |
Bin Meng | 165db7c | 2018-08-10 02:39:36 -0700 | [diff] [blame] | 437 | /* Try hardware calibration first */ |
| 438 | tsc_timer_ensure_setup(false); |
| 439 | if (!gd->arch.clock_rate) { |
| 440 | /* |
| 441 | * Use the clock frequency specified in the |
| 442 | * device tree as last resort |
| 443 | */ |
| 444 | if (!uc_priv->clock_rate) |
| 445 | panic("TSC frequency is ZERO"); |
Bin Meng | 94e72a6 | 2018-06-23 03:03:47 -0700 | [diff] [blame] | 446 | } else { |
Bin Meng | 165db7c | 2018-08-10 02:39:36 -0700 | [diff] [blame] | 447 | uc_priv->clock_rate = gd->arch.clock_rate; |
Bin Meng | 94e72a6 | 2018-06-23 03:03:47 -0700 | [diff] [blame] | 448 | } |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
Simon Glass | 2ff50f5 | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 453 | unsigned long notrace timer_early_get_rate(void) |
| 454 | { |
Bin Meng | 94e72a6 | 2018-06-23 03:03:47 -0700 | [diff] [blame] | 455 | /* |
| 456 | * When TSC timer is used as the early timer, be warned that the timer |
| 457 | * clock rate can only be calibrated via some hardware ways. Specifying |
| 458 | * it in the device tree won't work for the early timer. |
| 459 | */ |
Bin Meng | 165db7c | 2018-08-10 02:39:36 -0700 | [diff] [blame] | 460 | tsc_timer_ensure_setup(true); |
Simon Glass | 2ff50f5 | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 461 | |
| 462 | return gd->arch.clock_rate; |
| 463 | } |
| 464 | |
| 465 | u64 notrace timer_early_get_count(void) |
| 466 | { |
Simon Glass | 096c71e | 2019-10-20 21:31:54 -0600 | [diff] [blame] | 467 | tsc_timer_ensure_setup(true); |
| 468 | |
Simon Glass | 2ff50f5 | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 469 | return rdtsc() - gd->arch.tsc_base; |
| 470 | } |
| 471 | |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 472 | static const struct timer_ops tsc_timer_ops = { |
| 473 | .get_count = tsc_timer_get_count, |
| 474 | }; |
| 475 | |
| 476 | static const struct udevice_id tsc_timer_ids[] = { |
| 477 | { .compatible = "x86,tsc-timer", }, |
| 478 | { } |
| 479 | }; |
| 480 | |
| 481 | U_BOOT_DRIVER(tsc_timer) = { |
| 482 | .name = "tsc_timer", |
| 483 | .id = UCLASS_TIMER, |
| 484 | .of_match = tsc_timer_ids, |
| 485 | .probe = tsc_timer_probe, |
| 486 | .ops = &tsc_timer_ops, |
Bin Meng | 4e51fc2 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 487 | }; |