1 High Precision Event Timer Driver for Linux
3 The High Precision Event Timer (HPET) hardware is the future replacement for the 8254 and Real
4 Time Clock (RTC) periodic timer functionality. Each HPET can have up two 32 timers. It is possible
5 to configure the first two timers as legacy replacements for 8254 and RTC periodic. A specification
6 done by INTEL and Microsoft can be found at http://www.intel.com/labs/platcomp/hpet/hpetspec.htm.
8 The driver supports detection of HPET driver allocation and initialization of the HPET before the
9 driver module_init routine is called. This enables platform code which uses timer 0 or 1 as the
10 main timer to intercept HPET initialization. An example of this initialization can be found in
11 arch/i386/kernel/time_hpet.c.
13 The driver provides two APIs which are very similar to the API found in the rtc.c driver.
14 There is a user space API and a kernel space API. An example user space program is provided
26 #include <sys/types.h>
32 #include <linux/hpet.h>
35 extern void hpet_open_close(int, const char **);
36 extern void hpet_info(int, const char **);
37 extern void hpet_poll(int, const char **);
38 extern void hpet_fasync(int, const char **);
39 extern void hpet_read(int, const char **);
42 #include <sys/ioctl.h>
47 void (*func)(int argc, const char ** argv);
68 main(int argc, const char ** argv)
76 fprintf(stderr, "-hpet: requires command\n");
81 for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
82 if (!strcmp(argv[0], hpet_command[i].command)) {
85 fprintf(stderr, "-hpet: executing %s\n",
86 hpet_command[i].command);
87 hpet_command[i].func(argc, argv);
91 fprintf(stderr, "do_hpet: command %s not implemented\n", argv[0]);
97 hpet_open_close(int argc, const char **argv)
102 fprintf(stderr, "hpet_open_close: device-name\n");
106 fd = open(argv[0], O_RDONLY);
108 fprintf(stderr, "hpet_open_close: open failed\n");
116 hpet_info(int argc, const char **argv)
121 hpet_poll(int argc, const char **argv)
124 int iterations, i, fd;
126 struct hpet_info info;
127 struct timeval stv, etv;
132 fprintf(stderr, "hpet_poll: device-name freq iterations\n");
136 freq = atoi(argv[1]);
137 iterations = atoi(argv[2]);
139 fd = open(argv[0], O_RDONLY);
142 fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
146 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
147 fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
151 if (ioctl(fd, HPET_INFO, &info) < 0) {
152 fprintf(stderr, "hpet_poll: failed to get info\n");
156 fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
158 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
159 fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
163 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
164 fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
171 for (i = 0; i < iterations; i++) {
173 gettimeofday(&stv, &tz);
174 if (poll(&pfd, 1, -1) < 0)
175 fprintf(stderr, "hpet_poll: poll failed\n");
179 gettimeofday(&etv, &tz);
180 usec = stv.tv_sec * 1000000 + stv.tv_usec;
181 usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
184 "hpet_poll: expired time = 0x%lx\n", usec);
186 fprintf(stderr, "hpet_poll: revents = 0x%x\n",
189 if (read(fd, &data, sizeof(data)) != sizeof(data)) {
190 fprintf(stderr, "hpet_poll: read failed\n");
193 fprintf(stderr, "hpet_poll: data 0x%lx\n",
203 static int hpet_sigio_count;
208 fprintf(stderr, "hpet_sigio: called\n");
213 hpet_fasync(int argc, const char **argv)
216 int iterations, i, fd, value;
218 struct hpet_info info;
220 hpet_sigio_count = 0;
223 if ((oldsig = signal(SIGIO, hpet_sigio)) == SIG_ERR) {
224 fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
229 fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
233 fd = open(argv[0], O_RDONLY);
236 fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
241 if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
242 ((value = fcntl(fd, F_GETFL)) == 1) ||
243 (fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
244 fprintf(stderr, "hpet_fasync: fcntl failed\n");
248 freq = atoi(argv[1]);
249 iterations = atoi(argv[2]);
251 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
252 fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
256 if (ioctl(fd, HPET_INFO, &info) < 0) {
257 fprintf(stderr, "hpet_fasync: failed to get info\n");
261 fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
263 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
264 fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
268 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
269 fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
273 for (i = 0; i < iterations; i++) {
275 fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
279 signal(SIGIO, oldsig);
287 The kernel API has three interfaces exported from the driver:
289 hpet_register(struct hpet_task *tp, int periodic)
290 hpet_unregister(struct hpet_task *tp)
291 hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
293 The kernel module using this interface fills in the ht_func and ht_data members of the
294 hpet_task structure before calling hpet_register. hpet_control simply vectors to the hpet_ioctl
295 routine and has the same commands and respective arguments as the user API. hpet_unregister
296 is used to terminate usage of the HPET timer reserved by hpet_register.