hpet.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. High Precision Event Timer Driver for Linux
  2. The High Precision Event Timer (HPET) hardware is the future replacement
  3. for the 8254 and Real Time Clock (RTC) periodic timer functionality.
  4. Each HPET can have up to 32 timers. It is possible to configure the
  5. first two timers as legacy replacements for 8254 and RTC periodic timers.
  6. A specification done by Intel and Microsoft can be found at
  7. <http://www.intel.com/hardwaredesign/hpetspec.htm>.
  8. The driver supports detection of HPET driver allocation and initialization
  9. of the HPET before the driver module_init routine is called. This enables
  10. platform code which uses timer 0 or 1 as the main timer to intercept HPET
  11. initialization. An example of this initialization can be found in
  12. arch/i386/kernel/time_hpet.c.
  13. The driver provides two APIs which are very similar to the API found in
  14. the rtc.c driver. There is a user space API and a kernel space API.
  15. An example user space program is provided below.
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <string.h>
  21. #include <memory.h>
  22. #include <malloc.h>
  23. #include <time.h>
  24. #include <ctype.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <signal.h>
  28. #include <fcntl.h>
  29. #include <errno.h>
  30. #include <sys/time.h>
  31. #include <linux/hpet.h>
  32. extern void hpet_open_close(int, const char **);
  33. extern void hpet_info(int, const char **);
  34. extern void hpet_poll(int, const char **);
  35. extern void hpet_fasync(int, const char **);
  36. extern void hpet_read(int, const char **);
  37. #include <sys/poll.h>
  38. #include <sys/ioctl.h>
  39. #include <signal.h>
  40. struct hpet_command {
  41. char *command;
  42. void (*func)(int argc, const char ** argv);
  43. } hpet_command[] = {
  44. {
  45. "open-close",
  46. hpet_open_close
  47. },
  48. {
  49. "info",
  50. hpet_info
  51. },
  52. {
  53. "poll",
  54. hpet_poll
  55. },
  56. {
  57. "fasync",
  58. hpet_fasync
  59. },
  60. };
  61. int
  62. main(int argc, const char ** argv)
  63. {
  64. int i;
  65. argc--;
  66. argv++;
  67. if (!argc) {
  68. fprintf(stderr, "-hpet: requires command\n");
  69. return -1;
  70. }
  71. for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
  72. if (!strcmp(argv[0], hpet_command[i].command)) {
  73. argc--;
  74. argv++;
  75. fprintf(stderr, "-hpet: executing %s\n",
  76. hpet_command[i].command);
  77. hpet_command[i].func(argc, argv);
  78. return 0;
  79. }
  80. fprintf(stderr, "do_hpet: command %s not implemented\n", argv[0]);
  81. return -1;
  82. }
  83. void
  84. hpet_open_close(int argc, const char **argv)
  85. {
  86. int fd;
  87. if (argc != 1) {
  88. fprintf(stderr, "hpet_open_close: device-name\n");
  89. return;
  90. }
  91. fd = open(argv[0], O_RDONLY);
  92. if (fd < 0)
  93. fprintf(stderr, "hpet_open_close: open failed\n");
  94. else
  95. close(fd);
  96. return;
  97. }
  98. void
  99. hpet_info(int argc, const char **argv)
  100. {
  101. }
  102. void
  103. hpet_poll(int argc, const char **argv)
  104. {
  105. unsigned long freq;
  106. int iterations, i, fd;
  107. struct pollfd pfd;
  108. struct hpet_info info;
  109. struct timeval stv, etv;
  110. struct timezone tz;
  111. long usec;
  112. if (argc != 3) {
  113. fprintf(stderr, "hpet_poll: device-name freq iterations\n");
  114. return;
  115. }
  116. freq = atoi(argv[1]);
  117. iterations = atoi(argv[2]);
  118. fd = open(argv[0], O_RDONLY);
  119. if (fd < 0) {
  120. fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
  121. return;
  122. }
  123. if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
  124. fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
  125. goto out;
  126. }
  127. if (ioctl(fd, HPET_INFO, &info) < 0) {
  128. fprintf(stderr, "hpet_poll: failed to get info\n");
  129. goto out;
  130. }
  131. fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
  132. if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
  133. fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
  134. goto out;
  135. }
  136. if (ioctl(fd, HPET_IE_ON, 0) < 0) {
  137. fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
  138. goto out;
  139. }
  140. pfd.fd = fd;
  141. pfd.events = POLLIN;
  142. for (i = 0; i < iterations; i++) {
  143. pfd.revents = 0;
  144. gettimeofday(&stv, &tz);
  145. if (poll(&pfd, 1, -1) < 0)
  146. fprintf(stderr, "hpet_poll: poll failed\n");
  147. else {
  148. long data;
  149. gettimeofday(&etv, &tz);
  150. usec = stv.tv_sec * 1000000 + stv.tv_usec;
  151. usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
  152. fprintf(stderr,
  153. "hpet_poll: expired time = 0x%lx\n", usec);
  154. fprintf(stderr, "hpet_poll: revents = 0x%x\n",
  155. pfd.revents);
  156. if (read(fd, &data, sizeof(data)) != sizeof(data)) {
  157. fprintf(stderr, "hpet_poll: read failed\n");
  158. }
  159. else
  160. fprintf(stderr, "hpet_poll: data 0x%lx\n",
  161. data);
  162. }
  163. }
  164. out:
  165. close(fd);
  166. return;
  167. }
  168. static int hpet_sigio_count;
  169. static void
  170. hpet_sigio(int val)
  171. {
  172. fprintf(stderr, "hpet_sigio: called\n");
  173. hpet_sigio_count++;
  174. }
  175. void
  176. hpet_fasync(int argc, const char **argv)
  177. {
  178. unsigned long freq;
  179. int iterations, i, fd, value;
  180. sig_t oldsig;
  181. struct hpet_info info;
  182. hpet_sigio_count = 0;
  183. fd = -1;
  184. if ((oldsig = signal(SIGIO, hpet_sigio)) == SIG_ERR) {
  185. fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
  186. return;
  187. }
  188. if (argc != 3) {
  189. fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
  190. goto out;
  191. }
  192. fd = open(argv[0], O_RDONLY);
  193. if (fd < 0) {
  194. fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
  195. return;
  196. }
  197. if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
  198. ((value = fcntl(fd, F_GETFL)) == 1) ||
  199. (fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
  200. fprintf(stderr, "hpet_fasync: fcntl failed\n");
  201. goto out;
  202. }
  203. freq = atoi(argv[1]);
  204. iterations = atoi(argv[2]);
  205. if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
  206. fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
  207. goto out;
  208. }
  209. if (ioctl(fd, HPET_INFO, &info) < 0) {
  210. fprintf(stderr, "hpet_fasync: failed to get info\n");
  211. goto out;
  212. }
  213. fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
  214. if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
  215. fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
  216. goto out;
  217. }
  218. if (ioctl(fd, HPET_IE_ON, 0) < 0) {
  219. fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
  220. goto out;
  221. }
  222. for (i = 0; i < iterations; i++) {
  223. (void) pause();
  224. fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
  225. }
  226. out:
  227. signal(SIGIO, oldsig);
  228. if (fd >= 0)
  229. close(fd);
  230. return;
  231. }
  232. The kernel API has three interfaces exported from the driver:
  233. hpet_register(struct hpet_task *tp, int periodic)
  234. hpet_unregister(struct hpet_task *tp)
  235. hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
  236. The kernel module using this interface fills in the ht_func and ht_data
  237. members of the hpet_task structure before calling hpet_register.
  238. hpet_control simply vectors to the hpet_ioctl routine and has the same
  239. commands and respective arguments as the user API. hpet_unregister
  240. is used to terminate usage of the HPET timer reserved by hpet_register.