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.

linux_timer.c 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@gmail.com>
  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 St, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. FILE_LICENCE(GPL2_OR_LATER);
  19. #include <ipxe/timer.h>
  20. #include <linux_api.h>
  21. /** @file
  22. *
  23. * iPXE timer API for linux
  24. *
  25. */
  26. /**
  27. * Delay for a fixed number of microseconds
  28. *
  29. * @v usecs Number of microseconds for which to delay
  30. */
  31. static void linux_udelay(unsigned long usecs)
  32. {
  33. linux_usleep(usecs);
  34. }
  35. /**
  36. * Get number of ticks per second
  37. *
  38. * @ret ticks_per_sec Number of ticks per second
  39. */
  40. static unsigned long linux_ticks_per_sec(void)
  41. {
  42. return 1000;
  43. }
  44. /**
  45. * Get current system time in ticks
  46. *
  47. * linux doesn't provide an easy access to jiffies so implement it by measuring
  48. * the time since the first call to this function.
  49. *
  50. * @ret ticks Current time, in ticks
  51. */
  52. static unsigned long linux_currticks(void)
  53. {
  54. static struct timeval start;
  55. static int initialized = 0;
  56. if (! initialized) {
  57. linux_gettimeofday(&start, NULL);
  58. initialized = 1;
  59. }
  60. struct timeval now;
  61. linux_gettimeofday(&now, NULL);
  62. unsigned long ticks = (now.tv_sec - start.tv_sec) * linux_ticks_per_sec();
  63. ticks += (now.tv_usec - start.tv_usec) / (long)(1000000 / linux_ticks_per_sec());
  64. return ticks;
  65. }
  66. PROVIDE_TIMER(linux, udelay, linux_udelay);
  67. PROVIDE_TIMER(linux, currticks, linux_currticks);
  68. PROVIDE_TIMER(linux, ticks_per_sec, linux_ticks_per_sec);