You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rdtsc_timer.c 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * RDTSC timer
  27. *
  28. */
  29. #include <assert.h>
  30. #include <ipxe/timer.h>
  31. #include <ipxe/pit8254.h>
  32. /**
  33. * Number of TSC ticks per microsecond
  34. *
  35. * This is calibrated on the first use of the timer.
  36. */
  37. static unsigned long rdtsc_ticks_per_usec;
  38. /**
  39. * Delay for a fixed number of microseconds
  40. *
  41. * @v usecs Number of microseconds for which to delay
  42. */
  43. static void rdtsc_udelay ( unsigned long usecs ) {
  44. unsigned long start;
  45. unsigned long elapsed;
  46. /* Sanity guard, since we may divide by this */
  47. if ( ! usecs )
  48. usecs = 1;
  49. start = currticks();
  50. if ( rdtsc_ticks_per_usec ) {
  51. /* Already calibrated; busy-wait until done */
  52. do {
  53. elapsed = ( currticks() - start );
  54. } while ( elapsed < ( usecs * rdtsc_ticks_per_usec ) );
  55. } else {
  56. /* Not yet calibrated; use 8254 PIT and calibrate
  57. * based on result.
  58. */
  59. pit8254_udelay ( usecs );
  60. elapsed = ( currticks() - start );
  61. rdtsc_ticks_per_usec = ( elapsed / usecs );
  62. DBG ( "RDTSC timer calibrated: %ld ticks in %ld usecs "
  63. "(%ld MHz)\n", elapsed, usecs,
  64. ( rdtsc_ticks_per_usec << TSC_SHIFT ) );
  65. }
  66. }
  67. /**
  68. * Get number of ticks per second
  69. *
  70. * @ret ticks_per_sec Number of ticks per second
  71. */
  72. static unsigned long rdtsc_ticks_per_sec ( void ) {
  73. /* Calibrate timer, if not already done */
  74. if ( ! rdtsc_ticks_per_usec )
  75. udelay ( 1 );
  76. /* Sanity check */
  77. assert ( rdtsc_ticks_per_usec != 0 );
  78. return ( rdtsc_ticks_per_usec * 1000 * 1000 );
  79. }
  80. PROVIDE_TIMER ( rdtsc, udelay, rdtsc_udelay );
  81. PROVIDE_TIMER_INLINE ( rdtsc, currticks );
  82. PROVIDE_TIMER ( rdtsc, ticks_per_sec, rdtsc_ticks_per_sec );