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.

182 lines
4.4KB

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