tsc_timer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012 The Chromium OS Authors.
  4. *
  5. * TSC calibration codes are adapted from Linux kernel
  6. * arch/x86/kernel/tsc_msr.c and arch/x86/kernel/tsc.c
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <malloc.h>
  11. #include <timer.h>
  12. #include <asm/cpu.h>
  13. #include <asm/io.h>
  14. #include <asm/i8254.h>
  15. #include <asm/ibmpc.h>
  16. #include <asm/msr.h>
  17. #include <asm/u-boot-x86.h>
  18. #define MAX_NUM_FREQS 9
  19. DECLARE_GLOBAL_DATA_PTR;
  20. static unsigned long cpu_mhz_from_cpuid(void)
  21. {
  22. if (gd->arch.x86_vendor != X86_VENDOR_INTEL)
  23. return 0;
  24. if (cpuid_eax(0) < 0x16)
  25. return 0;
  26. return cpuid_eax(0x16);
  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. /* 2: use 100MHz, 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */
  40. u8 msr_plat;
  41. u32 freqs[MAX_NUM_FREQS];
  42. };
  43. static struct freq_desc freq_desc_tables[] = {
  44. /* PNW */
  45. { 6, 0x27, 0, { 0, 0, 0, 0, 0, 99840, 0, 83200, 0 } },
  46. /* CLV+ */
  47. { 6, 0x35, 0, { 0, 133200, 0, 0, 0, 99840, 0, 83200, 0 } },
  48. /* TNG - Intel Atom processor Z3400 series */
  49. { 6, 0x4a, 1, { 0, 100000, 133300, 0, 0, 0, 0, 0, 0 } },
  50. /* VLV2 - Intel Atom processor E3000, Z3600, Z3700 series */
  51. { 6, 0x37, 1, { 83300, 100000, 133300, 116700, 80000, 0, 0, 0, 0 } },
  52. /* ANN - Intel Atom processor Z3500 series */
  53. { 6, 0x5a, 1, { 83300, 100000, 133300, 100000, 0, 0, 0, 0, 0 } },
  54. /* AMT - Intel Atom processor X7-Z8000 and X5-Z8000 series */
  55. { 6, 0x4c, 1, { 83300, 100000, 133300, 116700,
  56. 80000, 93300, 90000, 88900, 87500 } },
  57. /* Ivybridge */
  58. { 6, 0x3a, 2, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
  59. };
  60. static int match_cpu(u8 family, u8 model)
  61. {
  62. int i;
  63. for (i = 0; i < ARRAY_SIZE(freq_desc_tables); i++) {
  64. if ((family == freq_desc_tables[i].x86_family) &&
  65. (model == freq_desc_tables[i].x86_model))
  66. return i;
  67. }
  68. return -1;
  69. }
  70. /* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */
  71. #define id_to_freq(cpu_index, freq_id) \
  72. (freq_desc_tables[cpu_index].freqs[freq_id])
  73. /*
  74. * TSC on Intel Atom SoCs capable of determining TSC frequency by MSR is
  75. * reliable and the frequency is known (provided by HW).
  76. *
  77. * On these platforms PIT/HPET is generally not available so calibration won't
  78. * work at all and there is no other clocksource to act as a watchdog for the
  79. * TSC, so we have no other choice than to trust it.
  80. *
  81. * Returns the TSC frequency in MHz or 0 if HW does not provide it.
  82. */
  83. static unsigned long __maybe_unused cpu_mhz_from_msr(void)
  84. {
  85. u32 lo, hi, ratio, freq_id, freq;
  86. unsigned long res;
  87. int cpu_index;
  88. if (gd->arch.x86_vendor != X86_VENDOR_INTEL)
  89. return 0;
  90. cpu_index = match_cpu(gd->arch.x86, gd->arch.x86_model);
  91. if (cpu_index < 0)
  92. return 0;
  93. if (freq_desc_tables[cpu_index].msr_plat) {
  94. rdmsr(MSR_PLATFORM_INFO, lo, hi);
  95. ratio = (lo >> 8) & 0xff;
  96. } else {
  97. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  98. ratio = (hi >> 8) & 0x1f;
  99. }
  100. debug("Maximum core-clock to bus-clock ratio: 0x%x\n", ratio);
  101. if (freq_desc_tables[cpu_index].msr_plat == 2) {
  102. /* TODO: Figure out how best to deal with this */
  103. freq = 100000;
  104. debug("Using frequency: %u KHz\n", freq);
  105. } else {
  106. /* Get FSB FREQ ID */
  107. rdmsr(MSR_FSB_FREQ, lo, hi);
  108. freq_id = lo & 0x7;
  109. freq = id_to_freq(cpu_index, freq_id);
  110. debug("Resolved frequency ID: %u, frequency: %u KHz\n",
  111. freq_id, freq);
  112. }
  113. /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */
  114. res = freq * ratio / 1000;
  115. debug("TSC runs at %lu MHz\n", res);
  116. return res;
  117. }
  118. /*
  119. * This reads the current MSB of the PIT counter, and
  120. * checks if we are running on sufficiently fast and
  121. * non-virtualized hardware.
  122. *
  123. * Our expectations are:
  124. *
  125. * - the PIT is running at roughly 1.19MHz
  126. *
  127. * - each IO is going to take about 1us on real hardware,
  128. * but we allow it to be much faster (by a factor of 10) or
  129. * _slightly_ slower (ie we allow up to a 2us read+counter
  130. * update - anything else implies a unacceptably slow CPU
  131. * or PIT for the fast calibration to work.
  132. *
  133. * - with 256 PIT ticks to read the value, we have 214us to
  134. * see the same MSB (and overhead like doing a single TSC
  135. * read per MSB value etc).
  136. *
  137. * - We're doing 2 reads per loop (LSB, MSB), and we expect
  138. * them each to take about a microsecond on real hardware.
  139. * So we expect a count value of around 100. But we'll be
  140. * generous, and accept anything over 50.
  141. *
  142. * - if the PIT is stuck, and we see *many* more reads, we
  143. * return early (and the next caller of pit_expect_msb()
  144. * then consider it a failure when they don't see the
  145. * next expected value).
  146. *
  147. * These expectations mean that we know that we have seen the
  148. * transition from one expected value to another with a fairly
  149. * high accuracy, and we didn't miss any events. We can thus
  150. * use the TSC value at the transitions to calculate a pretty
  151. * good value for the TSC frequencty.
  152. */
  153. static inline int pit_verify_msb(unsigned char val)
  154. {
  155. /* Ignore LSB */
  156. inb(0x42);
  157. return inb(0x42) == val;
  158. }
  159. static inline int pit_expect_msb(unsigned char val, u64 *tscp,
  160. unsigned long *deltap)
  161. {
  162. int count;
  163. u64 tsc = 0, prev_tsc = 0;
  164. for (count = 0; count < 50000; count++) {
  165. if (!pit_verify_msb(val))
  166. break;
  167. prev_tsc = tsc;
  168. tsc = rdtsc();
  169. }
  170. *deltap = rdtsc() - prev_tsc;
  171. *tscp = tsc;
  172. /*
  173. * We require _some_ success, but the quality control
  174. * will be based on the error terms on the TSC values.
  175. */
  176. return count > 5;
  177. }
  178. /*
  179. * How many MSB values do we want to see? We aim for
  180. * a maximum error rate of 500ppm (in practice the
  181. * real error is much smaller), but refuse to spend
  182. * more than 50ms on it.
  183. */
  184. #define MAX_QUICK_PIT_MS 50
  185. #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256)
  186. static unsigned long __maybe_unused quick_pit_calibrate(void)
  187. {
  188. int i;
  189. u64 tsc, delta;
  190. unsigned long d1, d2;
  191. /* Set the Gate high, disable speaker */
  192. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  193. /*
  194. * Counter 2, mode 0 (one-shot), binary count
  195. *
  196. * NOTE! Mode 2 decrements by two (and then the
  197. * output is flipped each time, giving the same
  198. * final output frequency as a decrement-by-one),
  199. * so mode 0 is much better when looking at the
  200. * individual counts.
  201. */
  202. outb(0xb0, 0x43);
  203. /* Start at 0xffff */
  204. outb(0xff, 0x42);
  205. outb(0xff, 0x42);
  206. /*
  207. * The PIT starts counting at the next edge, so we
  208. * need to delay for a microsecond. The easiest way
  209. * to do that is to just read back the 16-bit counter
  210. * once from the PIT.
  211. */
  212. pit_verify_msb(0);
  213. if (pit_expect_msb(0xff, &tsc, &d1)) {
  214. for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) {
  215. if (!pit_expect_msb(0xff-i, &delta, &d2))
  216. break;
  217. /*
  218. * Iterate until the error is less than 500 ppm
  219. */
  220. delta -= tsc;
  221. if (d1+d2 >= delta >> 11)
  222. continue;
  223. /*
  224. * Check the PIT one more time to verify that
  225. * all TSC reads were stable wrt the PIT.
  226. *
  227. * This also guarantees serialization of the
  228. * last cycle read ('d2') in pit_expect_msb.
  229. */
  230. if (!pit_verify_msb(0xfe - i))
  231. break;
  232. goto success;
  233. }
  234. }
  235. debug("Fast TSC calibration failed\n");
  236. return 0;
  237. success:
  238. /*
  239. * Ok, if we get here, then we've seen the
  240. * MSB of the PIT decrement 'i' times, and the
  241. * error has shrunk to less than 500 ppm.
  242. *
  243. * As a result, we can depend on there not being
  244. * any odd delays anywhere, and the TSC reads are
  245. * reliable (within the error).
  246. *
  247. * kHz = ticks / time-in-seconds / 1000;
  248. * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000
  249. * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000)
  250. */
  251. delta *= PIT_TICK_RATE;
  252. delta /= (i*256*1000);
  253. debug("Fast TSC calibration using PIT\n");
  254. return delta / 1000;
  255. }
  256. /* Get the speed of the TSC timer in MHz */
  257. unsigned notrace long get_tbclk_mhz(void)
  258. {
  259. return get_tbclk() / 1000000;
  260. }
  261. static ulong get_ms_timer(void)
  262. {
  263. return (get_ticks() * 1000) / get_tbclk();
  264. }
  265. ulong get_timer(ulong base)
  266. {
  267. return get_ms_timer() - base;
  268. }
  269. ulong notrace timer_get_us(void)
  270. {
  271. return get_ticks() / get_tbclk_mhz();
  272. }
  273. ulong timer_get_boot_us(void)
  274. {
  275. return timer_get_us();
  276. }
  277. void __udelay(unsigned long usec)
  278. {
  279. u64 now = get_ticks();
  280. u64 stop;
  281. stop = now + usec * get_tbclk_mhz();
  282. while ((int64_t)(stop - get_ticks()) > 0)
  283. #if defined(CONFIG_QEMU) && defined(CONFIG_SMP)
  284. /*
  285. * Add a 'pause' instruction on qemu target,
  286. * to give other VCPUs a chance to run.
  287. */
  288. asm volatile("pause");
  289. #else
  290. ;
  291. #endif
  292. }
  293. static int tsc_timer_get_count(struct udevice *dev, u64 *count)
  294. {
  295. u64 now_tick = rdtsc();
  296. *count = now_tick - gd->arch.tsc_base;
  297. return 0;
  298. }
  299. static void tsc_timer_ensure_setup(bool early)
  300. {
  301. if (gd->arch.tsc_base)
  302. return;
  303. gd->arch.tsc_base = rdtsc();
  304. if (!gd->arch.clock_rate) {
  305. unsigned long fast_calibrate;
  306. fast_calibrate = cpu_mhz_from_cpuid();
  307. if (fast_calibrate)
  308. goto done;
  309. fast_calibrate = cpu_mhz_from_msr();
  310. if (fast_calibrate)
  311. goto done;
  312. fast_calibrate = quick_pit_calibrate();
  313. if (fast_calibrate)
  314. goto done;
  315. if (early)
  316. fast_calibrate = CONFIG_X86_TSC_TIMER_EARLY_FREQ;
  317. else
  318. return;
  319. done:
  320. gd->arch.clock_rate = fast_calibrate * 1000000;
  321. }
  322. }
  323. static int tsc_timer_probe(struct udevice *dev)
  324. {
  325. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  326. /* Try hardware calibration first */
  327. tsc_timer_ensure_setup(false);
  328. if (!gd->arch.clock_rate) {
  329. /*
  330. * Use the clock frequency specified in the
  331. * device tree as last resort
  332. */
  333. if (!uc_priv->clock_rate)
  334. panic("TSC frequency is ZERO");
  335. } else {
  336. uc_priv->clock_rate = gd->arch.clock_rate;
  337. }
  338. return 0;
  339. }
  340. unsigned long notrace timer_early_get_rate(void)
  341. {
  342. /*
  343. * When TSC timer is used as the early timer, be warned that the timer
  344. * clock rate can only be calibrated via some hardware ways. Specifying
  345. * it in the device tree won't work for the early timer.
  346. */
  347. tsc_timer_ensure_setup(true);
  348. return gd->arch.clock_rate;
  349. }
  350. u64 notrace timer_early_get_count(void)
  351. {
  352. return rdtsc() - gd->arch.tsc_base;
  353. }
  354. static const struct timer_ops tsc_timer_ops = {
  355. .get_count = tsc_timer_get_count,
  356. };
  357. static const struct udevice_id tsc_timer_ids[] = {
  358. { .compatible = "x86,tsc-timer", },
  359. { }
  360. };
  361. U_BOOT_DRIVER(tsc_timer) = {
  362. .name = "tsc_timer",
  363. .id = UCLASS_TIMER,
  364. .of_match = tsc_timer_ids,
  365. .probe = tsc_timer_probe,
  366. .ops = &tsc_timer_ops,
  367. };