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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <stddef.h>
  20. #include <ipxe/timer.h>
  21. #include <linux_api.h>
  22. /** @file
  23. *
  24. * iPXE timer API for linux
  25. *
  26. */
  27. /**
  28. * Delay for a fixed number of microseconds
  29. *
  30. * @v usecs Number of microseconds for which to delay
  31. */
  32. static void linux_udelay(unsigned long usecs)
  33. {
  34. linux_usleep(usecs);
  35. }
  36. /**
  37. * Get number of ticks per second
  38. *
  39. * @ret ticks_per_sec Number of ticks per second
  40. */
  41. static unsigned long linux_ticks_per_sec(void)
  42. {
  43. return 1000;
  44. }
  45. /**
  46. * Get current system time in ticks
  47. *
  48. * linux doesn't provide an easy access to jiffies so implement it by measuring
  49. * the time since the first call to this function.
  50. *
  51. * Since this function is used to seed the (non-cryptographic) random
  52. * number generator, we round the start time down to the nearest whole
  53. * second. This minimises the chances of generating identical RNG
  54. * sequences (and hence identical TCP port numbers, etc) on
  55. * consecutive invocations of iPXE.
  56. *
  57. * @ret ticks Current time, in ticks
  58. */
  59. static unsigned long linux_currticks(void)
  60. {
  61. static struct timeval start;
  62. static int initialized = 0;
  63. if (! initialized) {
  64. linux_gettimeofday(&start, NULL);
  65. initialized = 1;
  66. }
  67. struct timeval now;
  68. linux_gettimeofday(&now, NULL);
  69. unsigned long ticks = (now.tv_sec - start.tv_sec) * linux_ticks_per_sec();
  70. ticks += now.tv_usec / (long)(1000000 / linux_ticks_per_sec());
  71. return ticks;
  72. }
  73. PROVIDE_TIMER(linux, udelay, linux_udelay);
  74. PROVIDE_TIMER(linux, currticks, linux_currticks);
  75. PROVIDE_TIMER(linux, ticks_per_sec, linux_ticks_per_sec);