jack1 codebase
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.

141 lines
3.6KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. Copyright (C) 2005 Jussi Laako
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include <config.h>
  18. #include <stdint.h>
  19. jack_time_t (*_jack_get_microseconds)(void) = 0;
  20. #if defined(__gnu_linux__) && (defined(__i386__) || defined(__x86_64__))
  21. #define HPET_SUPPORT
  22. #define HPET_MMAP_SIZE 1024
  23. #define HPET_CAPS 0x000
  24. #define HPET_PERIOD 0x004
  25. #define HPET_COUNTER 0x0f0
  26. #define HPET_CAPS_COUNTER_64BIT (1 << 13)
  27. #if defined(__x86_64__)
  28. typedef uint64_t hpet_counter_t;
  29. #else
  30. typedef uint32_t hpet_counter_t;
  31. #endif
  32. static int hpet_fd;
  33. static unsigned char *hpet_ptr;
  34. static uint32_t hpet_period; /* period length in femto secs */
  35. static uint64_t hpet_offset = 0;
  36. static uint64_t hpet_wrap;
  37. static hpet_counter_t hpet_previous = 0;
  38. #endif /* defined(__gnu_linux__) && (__i386__ || __x86_64__) */
  39. #ifdef HPET_SUPPORT
  40. int
  41. jack_hpet_init ()
  42. {
  43. uint32_t hpet_caps;
  44. hpet_fd = open("/dev/hpet", O_RDONLY);
  45. if (hpet_fd < 0) {
  46. jack_error ("This system has no accessible HPET device (%s)", strerror (errno));
  47. return -1;
  48. }
  49. hpet_ptr = (unsigned char *) mmap(NULL, HPET_MMAP_SIZE,
  50. PROT_READ, MAP_SHARED, hpet_fd, 0);
  51. if (hpet_ptr == MAP_FAILED) {
  52. jack_error ("This system has no mappable HPET device (%s)", strerror (errno));
  53. close (hpet_fd);
  54. return -1;
  55. }
  56. /* this assumes period to be constant. if needed,
  57. it can be moved to the clock access function
  58. */
  59. hpet_period = *((uint32_t *) (hpet_ptr + HPET_PERIOD));
  60. hpet_caps = *((uint32_t *) (hpet_ptr + HPET_CAPS));
  61. hpet_wrap = ((hpet_caps & HPET_CAPS_COUNTER_64BIT) &&
  62. (sizeof(hpet_counter_t) == sizeof(uint64_t))) ?
  63. 0 : ((uint64_t) 1 << 32);
  64. return 0;
  65. }
  66. static jack_time_t
  67. jack_get_microseconds_from_hpet (void)
  68. {
  69. hpet_counter_t hpet_counter;
  70. long double hpet_time;
  71. hpet_counter = *((hpet_counter_t *) (hpet_ptr + HPET_COUNTER));
  72. if (unlikely(hpet_counter < hpet_previous))
  73. hpet_offset += hpet_wrap;
  74. hpet_previous = hpet_counter;
  75. hpet_time = (long double) (hpet_offset + hpet_counter) *
  76. (long double) hpet_period * (long double) 1e-9;
  77. return ((jack_time_t) (hpet_time + 0.5));
  78. }
  79. #else
  80. static int
  81. jack_hpet_init ()
  82. {
  83. jack_error ("This version of JACK or this computer does not have HPET support.\n"
  84. "Please choose a different clock source.");
  85. return -1;
  86. }
  87. static jack_time_t
  88. jack_get_microseconds_from_hpet (void)
  89. {
  90. /* never called */
  91. return 0;
  92. }
  93. #endif /* HPET_SUPPORT */
  94. void
  95. jack_init_time ()
  96. {
  97. /* nothing to do on a generic system - we use the system clock */
  98. }
  99. void
  100. jack_set_clock_source (jack_timer_type_t clocksrc)
  101. {
  102. switch (clocksrc)
  103. {
  104. case JACK_TIMER_HPET:
  105. if (jack_hpet_init () == 0) {
  106. _jack_get_microseconds = jack_get_microseconds_from_hpet;
  107. } else {
  108. _jack_get_microseconds = jack_get_microseconds_from_system;
  109. }
  110. break;
  111. case JACK_TIMER_SYSTEM_CLOCK:
  112. default:
  113. _jack_get_microseconds = jack_get_microseconds_from_system;
  114. break;
  115. }
  116. }