tsc_timer.c 9.8 KB

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