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.

timer.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <ipxe/process.h>
  26. #include <ipxe/console.h>
  27. #include <ipxe/keys.h>
  28. #include <ipxe/nap.h>
  29. #include <ipxe/init.h>
  30. #include <ipxe/timer.h>
  31. /** Current timer */
  32. static struct timer *timer;
  33. /**
  34. * Get current system time in ticks
  35. *
  36. * @ret ticks Current time, in ticks
  37. */
  38. unsigned long currticks ( void ) {
  39. /* Guard against use during early initialisation */
  40. if ( ! timer ) {
  41. DBGC ( &timer, "TIMER currticks() called before initialisation "
  42. "from %p\n", __builtin_return_address ( 0 ) );
  43. return 0;
  44. }
  45. /* Use selected timer */
  46. return timer->currticks();
  47. }
  48. /**
  49. * Delay for a fixed number of microseconds
  50. *
  51. * @v usecs Number of microseconds for which to delay
  52. */
  53. void udelay ( unsigned long usecs ) {
  54. /* Guard against use during early initialisation */
  55. if ( ! timer ) {
  56. DBGC ( &timer, "TIMER udelay() called before initialisation "
  57. "from %p\n", __builtin_return_address ( 0 ) );
  58. return;
  59. }
  60. /* Use selected timer */
  61. timer->udelay ( usecs );
  62. }
  63. /**
  64. * Delay for a fixed number of milliseconds
  65. *
  66. * @v msecs Number of milliseconds for which to delay
  67. */
  68. void mdelay ( unsigned long msecs ) {
  69. /* Guard against use during early initialisation */
  70. if ( ! timer ) {
  71. DBGC ( &timer, "TIMER mdelay() called before initialisation "
  72. "from %p\n", __builtin_return_address ( 0 ) );
  73. return;
  74. }
  75. /* Delay for specified number of milliseconds */
  76. while ( msecs-- )
  77. udelay ( 1000 );
  78. }
  79. /**
  80. * Sleep (possibly interruptibly) for a fixed number of seconds
  81. *
  82. * @v secs Number of seconds for which to delay
  83. * @v interrupted Interrupt checking method, or NULL
  84. * @ret secs Number of seconds remaining, if interrupted
  85. */
  86. static unsigned int sleep_interruptible ( unsigned int secs,
  87. int ( * interrupted ) ( void ) ) {
  88. unsigned long start = currticks();
  89. unsigned long now;
  90. for ( ; secs ; secs-- ) {
  91. while ( ( ( now = currticks() ) - start ) < TICKS_PER_SEC ) {
  92. step();
  93. if ( interrupted && interrupted() )
  94. return secs;
  95. cpu_nap();
  96. }
  97. start = now;
  98. }
  99. return 0;
  100. }
  101. /**
  102. * Check if sleep has been interrupted by keypress
  103. *
  104. * @ret interrupted Sleep has been interrupted
  105. */
  106. static int keypress_interrupted ( void ) {
  107. return ( iskey() && ( getchar() == CTRL_C ) );
  108. }
  109. /**
  110. * Sleep (interruptibly) for a fixed number of seconds
  111. *
  112. * @v secs Number of seconds for which to delay
  113. * @ret secs Number of seconds remaining, if interrupted
  114. */
  115. unsigned int sleep ( unsigned int secs ) {
  116. return sleep_interruptible ( secs, keypress_interrupted );
  117. }
  118. /**
  119. * Sleep (uninterruptibly) for a fixed number of seconds
  120. *
  121. * @v secs Number of seconds for which to delay
  122. */
  123. void sleep_fixed ( unsigned int secs ) {
  124. sleep_interruptible ( secs, NULL );
  125. }
  126. /**
  127. * Find a working timer
  128. *
  129. */
  130. static void timer_probe ( void ) {
  131. int rc;
  132. /* Use first working timer */
  133. for_each_table_entry ( timer, TIMERS ) {
  134. if ( ( timer->probe == NULL ) ||
  135. ( ( rc = timer->probe() ) == 0 ) ) {
  136. DBGC ( &timer, "TIMER using %s\n", timer->name );
  137. return;
  138. }
  139. DBGC ( &timer, "TIMER could not initialise %s: %s\n",
  140. timer->name, strerror ( rc ) );
  141. }
  142. /* This is a fatal error */
  143. DBGC ( &timer, "TIMER found no working timers!\n" );
  144. while ( 1 ) {}
  145. }
  146. /** Timer initialisation function */
  147. struct init_fn timer_init_fn __init_fn ( INIT_EARLY ) = {
  148. .initialise = timer_probe,
  149. };
  150. /* Drag in timer configuration */
  151. REQUIRING_SYMBOL ( timer_init_fn );
  152. REQUIRE_OBJECT ( config_timer );