tsc_timer.c 9.8 KB

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