tsc_timer.c 9.1 KB

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