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

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