tsc_timer.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <malloc.h>
  11. #include <asm/io.h>
  12. #include <asm/i8254.h>
  13. #include <asm/ibmpc.h>
  14. #include <asm/msr.h>
  15. #include <asm/u-boot-x86.h>
  16. /* CPU reference clock frequency: in KHz */
  17. #define FREQ_83 83200
  18. #define FREQ_100 99840
  19. #define FREQ_133 133200
  20. #define FREQ_166 166400
  21. #define MAX_NUM_FREQS 8
  22. DECLARE_GLOBAL_DATA_PTR;
  23. /*
  24. * According to Intel 64 and IA-32 System Programming Guide,
  25. * if MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be
  26. * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40].
  27. * Unfortunately some Intel Atom SoCs aren't quite compliant to this,
  28. * so we need manually differentiate SoC families. This is what the
  29. * field msr_plat does.
  30. */
  31. struct freq_desc {
  32. u8 x86_family; /* CPU family */
  33. u8 x86_model; /* model */
  34. /* 2: use 100MHz, 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */
  35. u8 msr_plat;
  36. u32 freqs[MAX_NUM_FREQS];
  37. };
  38. static struct freq_desc freq_desc_tables[] = {
  39. /* PNW */
  40. { 6, 0x27, 0, { 0, 0, 0, 0, 0, FREQ_100, 0, FREQ_83 } },
  41. /* CLV+ */
  42. { 6, 0x35, 0, { 0, FREQ_133, 0, 0, 0, FREQ_100, 0, FREQ_83 } },
  43. /* TNG */
  44. { 6, 0x4a, 1, { 0, FREQ_100, FREQ_133, 0, 0, 0, 0, 0 } },
  45. /* VLV2 */
  46. { 6, 0x37, 1, { FREQ_83, FREQ_100, FREQ_133, FREQ_166, 0, 0, 0, 0 } },
  47. /* Ivybridge */
  48. { 6, 0x3a, 2, { 0, 0, 0, 0, 0, 0, 0, 0 } },
  49. /* ANN */
  50. { 6, 0x5a, 1, { FREQ_83, FREQ_100, FREQ_133, FREQ_100, 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. * Do MSR calibration only for known/supported CPUs.
  67. *
  68. * Returns the calibration value or 0 if MSR calibration failed.
  69. */
  70. static unsigned long __maybe_unused try_msr_calibrate_tsc(void)
  71. {
  72. u32 lo, hi, ratio, freq_id, freq;
  73. unsigned long res;
  74. int cpu_index;
  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) & 0x1f;
  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 (!ratio)
  87. goto fail;
  88. if (freq_desc_tables[cpu_index].msr_plat == 2) {
  89. /* TODO: Figure out how best to deal with this */
  90. freq = FREQ_100;
  91. debug("Using frequency: %u KHz\n", freq);
  92. } else {
  93. /* Get FSB FREQ ID */
  94. rdmsr(MSR_FSB_FREQ, lo, hi);
  95. freq_id = lo & 0x7;
  96. freq = id_to_freq(cpu_index, freq_id);
  97. debug("Resolved frequency ID: %u, frequency: %u KHz\n",
  98. freq_id, freq);
  99. }
  100. if (!freq)
  101. goto fail;
  102. /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */
  103. res = freq * ratio / 1000;
  104. debug("TSC runs at %lu MHz\n", res);
  105. return res;
  106. fail:
  107. debug("Fast TSC calibration using MSR failed\n");
  108. return 0;
  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. void timer_set_base(u64 base)
  249. {
  250. gd->arch.tsc_base = base;
  251. }
  252. /*
  253. * Get the number of CPU time counter ticks since it was read first time after
  254. * restart. This yields a free running counter guaranteed to take almost 6
  255. * years to wrap around even at 100GHz clock rate.
  256. */
  257. u64 notrace get_ticks(void)
  258. {
  259. u64 now_tick = rdtsc();
  260. /* We assume that 0 means the base hasn't been set yet */
  261. if (!gd->arch.tsc_base)
  262. panic("No tick base available");
  263. return now_tick - gd->arch.tsc_base;
  264. }
  265. /* Get the speed of the TSC timer in MHz */
  266. unsigned notrace long get_tbclk_mhz(void)
  267. {
  268. unsigned long fast_calibrate;
  269. if (gd->arch.tsc_mhz)
  270. return gd->arch.tsc_mhz;
  271. #ifdef CONFIG_TSC_CALIBRATION_BYPASS
  272. fast_calibrate = CONFIG_TSC_FREQ_IN_MHZ;
  273. #else
  274. fast_calibrate = try_msr_calibrate_tsc();
  275. if (!fast_calibrate) {
  276. fast_calibrate = quick_pit_calibrate();
  277. if (!fast_calibrate)
  278. panic("TSC frequency is ZERO");
  279. }
  280. #endif
  281. gd->arch.tsc_mhz = fast_calibrate;
  282. return fast_calibrate;
  283. }
  284. unsigned long get_tbclk(void)
  285. {
  286. return get_tbclk_mhz() * 1000 * 1000;
  287. }
  288. static ulong get_ms_timer(void)
  289. {
  290. return (get_ticks() * 1000) / get_tbclk();
  291. }
  292. ulong get_timer(ulong base)
  293. {
  294. return get_ms_timer() - base;
  295. }
  296. ulong notrace timer_get_us(void)
  297. {
  298. return get_ticks() / get_tbclk_mhz();
  299. }
  300. ulong timer_get_boot_us(void)
  301. {
  302. return timer_get_us();
  303. }
  304. void __udelay(unsigned long usec)
  305. {
  306. u64 now = get_ticks();
  307. u64 stop;
  308. stop = now + usec * get_tbclk_mhz();
  309. while ((int64_t)(stop - get_ticks()) > 0)
  310. #if defined(CONFIG_QEMU) && defined(CONFIG_SMP)
  311. /*
  312. * Add a 'pause' instruction on qemu target,
  313. * to give other VCPUs a chance to run.
  314. */
  315. asm volatile("pause");
  316. #else
  317. ;
  318. #endif
  319. }
  320. int timer_init(void)
  321. {
  322. #ifdef CONFIG_I8254_TIMER
  323. /* Set up the i8254 timer if required */
  324. i8254_init();
  325. #endif
  326. return 0;
  327. }