tsc_pmtimer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* kern/i386/tsc.c - x86 TSC time source implementation
  2. * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
  3. * This module uses the PIT to calibrate the TSC to
  4. * real time.
  5. *
  6. * GRUB -- GRand Unified Bootloader
  7. * Copyright (C) 2008 Free Software Foundation, Inc.
  8. *
  9. * GRUB is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * GRUB is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <grub/types.h>
  23. #include <grub/time.h>
  24. #include <grub/misc.h>
  25. #include <grub/i386/tsc.h>
  26. #include <grub/i386/pmtimer.h>
  27. #include <grub/acpi.h>
  28. #include <grub/cpu/io.h>
  29. grub_uint64_t
  30. grub_pmtimer_wait_count_tsc (grub_port_t pmtimer,
  31. grub_uint16_t num_pm_ticks)
  32. {
  33. grub_uint32_t start;
  34. grub_uint64_t cur, end;
  35. grub_uint64_t start_tsc;
  36. grub_uint64_t end_tsc;
  37. grub_uint32_t num_iter = 0;
  38. int bad_reads = 0;
  39. /*
  40. * Some timers are 24-bit and some are 32-bit, but it doesn't make much
  41. * difference to us. Caring which one we have isn't really worth it since
  42. * the low-order digits will give us enough data to calibrate TSC. So just
  43. * mask the top-order byte off.
  44. */
  45. cur = start = grub_inl (pmtimer) & 0x00ffffffUL;
  46. end = start + num_pm_ticks;
  47. start_tsc = grub_get_tsc ();
  48. while (1)
  49. {
  50. cur &= 0xffffffffff000000ULL;
  51. /* Only take the low-order 24-bit for the reason explained above. */
  52. cur |= grub_inl (pmtimer) & 0x00ffffffUL;
  53. end_tsc = grub_get_tsc();
  54. /*
  55. * If we get 10 reads in a row that are obviously dead pins, there's no
  56. * reason to do this thousands of times.
  57. */
  58. if (cur == 0xffffffUL || cur == 0)
  59. {
  60. bad_reads++;
  61. grub_dprintf ("pmtimer",
  62. "pmtimer: 0x%"PRIxGRUB_UINT64_T" bad_reads: %d\n",
  63. cur, bad_reads);
  64. if (bad_reads == 10)
  65. {
  66. grub_dprintf ("pmtimer", "timer is broken; giving up.\n");
  67. return 0;
  68. }
  69. }
  70. if (cur < start)
  71. cur += 0x1000000;
  72. if (cur >= end)
  73. {
  74. grub_dprintf ("pmtimer", "pmtimer delta is 0x%"PRIxGRUB_UINT64_T"\n",
  75. cur - start);
  76. grub_dprintf ("pmtimer", "tsc delta is 0x%"PRIxGRUB_UINT64_T"\n",
  77. end_tsc - start_tsc);
  78. return end_tsc - start_tsc;
  79. }
  80. /*
  81. * Check for broken PM timer. 1ms at 10GHz should be 1E+7 TSCs; at
  82. * 250MHz it should be 2.5E5. So if after 4E+7 TSCs on a 10GHz machine,
  83. * we should have seen pmtimer show 4ms of change (i.e. cur =~ start + 14320);
  84. * on a 250MHz machine that should be 160ms (start + 572800). If after
  85. * this a time we still don't have 1ms on pmtimer, then pmtimer is broken.
  86. *
  87. * Likewise, if our code is perfectly efficient and introduces no delays
  88. * whatsoever, on a 10GHz system we should see a TSC delta of 3580 in
  89. * ~3580 iterations. On a 250MHz machine that should be ~900 iterations.
  90. *
  91. * With those factors in mind, there are two limits here. There's a hard
  92. * limit here at 8x our desired pm timer delta. This limit was picked as
  93. * an arbitrarily large value that's still not a lot of time to humans,
  94. * because if we get that far this is either an implausibly fast machine
  95. * or the pmtimer is not running. And there is another limit on a 4 ms TSC
  96. * delta on a 10 GHz clock, without seeing cur converge on our target value.
  97. */
  98. if ((++num_iter > (grub_uint32_t) num_pm_ticks << 3UL) || end_tsc - start_tsc > 40000000)
  99. {
  100. grub_dprintf ("pmtimer",
  101. "pmtimer delta is 0x%"PRIxGRUB_UINT64_T" (%"PRIxGRUB_UINT32_T" iterations)\n",
  102. cur - start, num_iter);
  103. grub_dprintf ("pmtimer",
  104. "tsc delta is implausible: 0x%"PRIxGRUB_UINT64_T"\n",
  105. end_tsc - start_tsc);
  106. return 0;
  107. }
  108. }
  109. }
  110. int
  111. grub_tsc_calibrate_from_pmtimer (void)
  112. {
  113. struct grub_acpi_fadt *fadt;
  114. grub_port_t pmtimer;
  115. grub_uint64_t tsc_diff;
  116. fadt = grub_acpi_find_fadt ();
  117. if (!fadt)
  118. {
  119. grub_dprintf ("pmtimer", "No FADT found; not using pmtimer.\n");
  120. return 0;
  121. }
  122. pmtimer = fadt->pmtimer;
  123. if (!pmtimer)
  124. {
  125. grub_dprintf ("pmtimer", "FADT does not specify pmtimer; skipping.\n");
  126. return 0;
  127. }
  128. /* It's 3.579545 MHz clock. Wait 1 ms. */
  129. tsc_diff = grub_pmtimer_wait_count_tsc (pmtimer, 3580);
  130. if (tsc_diff == 0)
  131. return 0;
  132. grub_tsc_rate = grub_divmod64 ((1ULL << 32), tsc_diff, 0);
  133. return 1;
  134. }