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

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