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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * RDTSC timer
  23. *
  24. */
  25. #include <assert.h>
  26. #include <ipxe/timer.h>
  27. #include <ipxe/timer2.h>
  28. /**
  29. * Number of TSC ticks per microsecond
  30. *
  31. * This is calibrated on the first use of the timer.
  32. */
  33. static unsigned long rdtsc_ticks_per_usec;
  34. /**
  35. * Delay for a fixed number of microseconds
  36. *
  37. * @v usecs Number of microseconds for which to delay
  38. */
  39. static void rdtsc_udelay ( unsigned long usecs ) {
  40. unsigned long start;
  41. unsigned long elapsed;
  42. /* Sanity guard, since we may divide by this */
  43. if ( ! usecs )
  44. usecs = 1;
  45. start = currticks();
  46. if ( rdtsc_ticks_per_usec ) {
  47. /* Already calibrated; busy-wait until done */
  48. do {
  49. elapsed = ( currticks() - start );
  50. } while ( elapsed < ( usecs * rdtsc_ticks_per_usec ) );
  51. } else {
  52. /* Not yet calibrated; use timer2 and calibrate
  53. * based on result.
  54. */
  55. timer2_udelay ( usecs );
  56. elapsed = ( currticks() - start );
  57. rdtsc_ticks_per_usec = ( elapsed / usecs );
  58. DBG ( "RDTSC timer calibrated: %ld ticks in %ld usecs "
  59. "(%ld MHz)\n", elapsed, usecs,
  60. ( rdtsc_ticks_per_usec << TSC_SHIFT ) );
  61. }
  62. }
  63. /**
  64. * Get number of ticks per second
  65. *
  66. * @ret ticks_per_sec Number of ticks per second
  67. */
  68. static unsigned long rdtsc_ticks_per_sec ( void ) {
  69. /* Calibrate timer, if not already done */
  70. if ( ! rdtsc_ticks_per_usec )
  71. udelay ( 1 );
  72. /* Sanity check */
  73. assert ( rdtsc_ticks_per_usec != 0 );
  74. return ( rdtsc_ticks_per_usec * 1000 * 1000 );
  75. }
  76. PROVIDE_TIMER ( rdtsc, udelay, rdtsc_udelay );
  77. PROVIDE_TIMER_INLINE ( rdtsc, currticks );
  78. PROVIDE_TIMER ( rdtsc, ticks_per_sec, rdtsc_ticks_per_sec );