tsc_timer.c 9.1 KB

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