tsc_timer.c 9.0 KB

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