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.

195 lines
4.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. $Id$
  17. */
  18. #include <config.h>
  19. static jack_time_t __jack_cpu_mhz = 0;
  20. jack_time_t (*_jack_get_microseconds)(void) = 0;
  21. #if defined(__gnu_linux__) && (defined(__i386__) || defined(__x86_64__))
  22. #define HPET_SUPPORT
  23. #define HPET_MMAP_SIZE 1024
  24. #define HPET_PERIOD 0x004
  25. #define HPET_COUNTER 0x0f0
  26. #if defined(__x86_64__)
  27. #define HPET_64BIT
  28. #else
  29. #define HPET_32BIT
  30. #endif
  31. static int hpet_fd;
  32. static unsigned char *hpet_ptr;
  33. static unsigned int hpet_period; /* period length in femto secs */
  34. #endif /* defined(__gnu_linux__) && (__i386__ || __x86_64__) */
  35. #ifdef HPET_SUPPORT
  36. int
  37. jack_hpet_init ()
  38. {
  39. hpet_fd = open("/dev/hpet", O_RDONLY);
  40. if (hpet_fd < 0) {
  41. jack_error ("This system has no accessible HPET device (%s)", strerror (errno));
  42. return -1;
  43. }
  44. hpet_ptr = (unsigned char *) mmap(NULL, HPET_MMAP_SIZE,
  45. PROT_READ, MAP_SHARED, hpet_fd, 0);
  46. if (hpet_ptr == NULL) {
  47. jack_error ("This system has no mappable HPET device (%s)", strerror (errno));
  48. close (hpet_fd);
  49. return -1;
  50. }
  51. /* this assumes period to be constant. if needed,
  52. it can be moved to the clock access function
  53. */
  54. hpet_period = *((unsigned int *) (hpet_ptr + HPET_PERIOD));
  55. return 0;
  56. }
  57. static jack_time_t
  58. jack_get_microseconds_from_hpet (void)
  59. {
  60. #ifdef HPET_64BIT
  61. unsigned long long hpet_counter;
  62. #else
  63. unsigned int hpet_counter;
  64. #endif
  65. long double hpet_time;
  66. #ifdef HPET_64BIT
  67. hpet_counter = *((unsigned long long *) (hpet_ptr + HPET_COUNTER));
  68. #else
  69. hpet_counter = *((unsigned int *) (hpet_ptr + HPET_COUNTER));
  70. #endif
  71. hpet_time = (long double) hpet_counter * (long double) hpet_period *
  72. (long double) 1e-9;
  73. return ((jack_time_t) (hpet_time + 0.5));
  74. }
  75. #else
  76. static int
  77. jack_hpet_init ()
  78. {
  79. jack_error ("This version of JACK or this computer does not have HPET support.\n"
  80. "Please choose a different clock source.");
  81. return -1;
  82. }
  83. static jack_time_t
  84. jack_get_microseconds_from_hpet (void)
  85. {
  86. /* never called */
  87. return 0;
  88. }
  89. #endif /* HPET_SUPPORT */
  90. jack_time_t
  91. jack_get_microseconds_from_cycles (void) {
  92. return get_cycles() / __jack_cpu_mhz;
  93. }
  94. /*
  95. * This is another kludge. It looks CPU-dependent, but actually it
  96. * reflects the lack of standards for the Linux kernel formatting of
  97. * /proc/cpuinfo.
  98. */
  99. jack_time_t
  100. jack_get_mhz (void)
  101. {
  102. FILE *f = fopen("/proc/cpuinfo", "r");
  103. if (f == 0)
  104. {
  105. perror("can't open /proc/cpuinfo\n");
  106. exit(1);
  107. }
  108. for ( ; ; )
  109. {
  110. jack_time_t mhz;
  111. int ret;
  112. char buf[1000];
  113. if (fgets(buf, sizeof(buf), f) == NULL) {
  114. jack_error ("FATAL: cannot locate cpu MHz in "
  115. "/proc/cpuinfo\n");
  116. exit(1);
  117. }
  118. #if defined(__powerpc__)
  119. ret = sscanf(buf, "clock\t: %" SCNu64 "MHz", &mhz);
  120. #elif defined( __i386__ ) || defined (__hppa__) || defined (__ia64__) || \
  121. defined(__x86_64__)
  122. ret = sscanf(buf, "cpu MHz : %" SCNu64, &mhz);
  123. #elif defined( __sparc__ )
  124. ret = sscanf(buf, "Cpu0Bogo : %" SCNu64, &mhz);
  125. #elif defined( __mc68000__ )
  126. ret = sscanf(buf, "Clocking: %" SCNu64, &mhz);
  127. #elif defined( __s390__ )
  128. ret = sscanf(buf, "bogomips per cpu: %" SCNu64, &mhz);
  129. #else /* MIPS, ARM, alpha */
  130. ret = sscanf(buf, "BogoMIPS : %" SCNu64, &mhz);
  131. #endif
  132. if (ret == 1)
  133. {
  134. fclose(f);
  135. return (jack_time_t)mhz;
  136. }
  137. }
  138. }
  139. void
  140. jack_init_time ()
  141. {
  142. __jack_cpu_mhz = jack_get_mhz ();
  143. }
  144. void
  145. jack_set_clock_source (jack_timer_type_t clocksrc)
  146. {
  147. switch (clocksrc)
  148. {
  149. case JACK_TIMER_CYCLE_COUNTER:
  150. _jack_get_microseconds = jack_get_microseconds_from_cycles;
  151. break;
  152. case JACK_TIMER_HPET:
  153. if (jack_hpet_init () == 0) {
  154. _jack_get_microseconds = jack_get_microseconds_from_hpet;
  155. } else {
  156. _jack_get_microseconds = jack_get_microseconds_from_system;
  157. }
  158. break;
  159. case JACK_TIMER_SYSTEM_CLOCK:
  160. default:
  161. _jack_get_microseconds = jack_get_microseconds_from_system;
  162. break;
  163. }
  164. }