1 High Precision Event Timer Driver for Linux
3 The High Precision Event Timer (HPET) hardware is the future replacement
4 for the 8254 and Real Time Clock (RTC) periodic timer functionality.
5 Each HPET can have up to 32 timers. It is possible to configure the
6 first two timers as legacy replacements for 8254 and RTC periodic timers.
7 A specification done by Intel and Microsoft can be found at
8 <http://www.intel.com/hardwaredesign/hpetspec.htm>.
10 The driver supports detection of HPET driver allocation and initialization
11 of the HPET before the driver module_init routine is called. This enables
12 platform code which uses timer 0 or 1 as the main timer to intercept HPET
13 initialization. An example of this initialization can be found in
14 arch/i386/kernel/time_hpet.c.
16 The driver provides two APIs which are very similar to the API found in
17 the rtc.c driver. There is a user space API and a kernel space API.
18 An example user space program is provided below.
29 #include <sys/types.h>
35 #include <linux/hpet.h>
38 extern void hpet_open_close(int, const char **);
39 extern void hpet_info(int, const char **);
40 extern void hpet_poll(int, const char **);
41 extern void hpet_fasync(int, const char **);
42 extern void hpet_read(int, const char **);
45 #include <sys/ioctl.h>
50 void (*func)(int argc, const char ** argv);
71 main(int argc, const char ** argv)
79 fprintf(stderr, "-hpet: requires command\n");
84 for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
85 if (!strcmp(argv[0], hpet_command[i].command)) {
88 fprintf(stderr, "-hpet: executing %s\n",
89 hpet_command[i].command);
90 hpet_command[i].func(argc, argv);
94 fprintf(stderr, "do_hpet: command %s not implemented\n", argv[0]);
100 hpet_open_close(int argc, const char **argv)
105 fprintf(stderr, "hpet_open_close: device-name\n");
109 fd = open(argv[0], O_RDONLY);
111 fprintf(stderr, "hpet_open_close: open failed\n");
119 hpet_info(int argc, const char **argv)
124 hpet_poll(int argc, const char **argv)
127 int iterations, i, fd;
129 struct hpet_info info;
130 struct timeval stv, etv;
135 fprintf(stderr, "hpet_poll: device-name freq iterations\n");
139 freq = atoi(argv[1]);
140 iterations = atoi(argv[2]);
142 fd = open(argv[0], O_RDONLY);
145 fprintf(stderr, "hpet_poll: open of %s failed\n", argv[0]);
149 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
150 fprintf(stderr, "hpet_poll: HPET_IRQFREQ failed\n");
154 if (ioctl(fd, HPET_INFO, &info) < 0) {
155 fprintf(stderr, "hpet_poll: failed to get info\n");
159 fprintf(stderr, "hpet_poll: info.hi_flags 0x%lx\n", info.hi_flags);
161 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
162 fprintf(stderr, "hpet_poll: HPET_EPI failed\n");
166 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
167 fprintf(stderr, "hpet_poll, HPET_IE_ON failed\n");
174 for (i = 0; i < iterations; i++) {
176 gettimeofday(&stv, &tz);
177 if (poll(&pfd, 1, -1) < 0)
178 fprintf(stderr, "hpet_poll: poll failed\n");
182 gettimeofday(&etv, &tz);
183 usec = stv.tv_sec * 1000000 + stv.tv_usec;
184 usec = (etv.tv_sec * 1000000 + etv.tv_usec) - usec;
187 "hpet_poll: expired time = 0x%lx\n", usec);
189 fprintf(stderr, "hpet_poll: revents = 0x%x\n",
192 if (read(fd, &data, sizeof(data)) != sizeof(data)) {
193 fprintf(stderr, "hpet_poll: read failed\n");
196 fprintf(stderr, "hpet_poll: data 0x%lx\n",
206 static int hpet_sigio_count;
211 fprintf(stderr, "hpet_sigio: called\n");
216 hpet_fasync(int argc, const char **argv)
219 int iterations, i, fd, value;
221 struct hpet_info info;
223 hpet_sigio_count = 0;
226 if ((oldsig = signal(SIGIO, hpet_sigio)) == SIG_ERR) {
227 fprintf(stderr, "hpet_fasync: failed to set signal handler\n");
232 fprintf(stderr, "hpet_fasync: device-name freq iterations\n");
236 fd = open(argv[0], O_RDONLY);
239 fprintf(stderr, "hpet_fasync: failed to open %s\n", argv[0]);
244 if ((fcntl(fd, F_SETOWN, getpid()) == 1) ||
245 ((value = fcntl(fd, F_GETFL)) == 1) ||
246 (fcntl(fd, F_SETFL, value | O_ASYNC) == 1)) {
247 fprintf(stderr, "hpet_fasync: fcntl failed\n");
251 freq = atoi(argv[1]);
252 iterations = atoi(argv[2]);
254 if (ioctl(fd, HPET_IRQFREQ, freq) < 0) {
255 fprintf(stderr, "hpet_fasync: HPET_IRQFREQ failed\n");
259 if (ioctl(fd, HPET_INFO, &info) < 0) {
260 fprintf(stderr, "hpet_fasync: failed to get info\n");
264 fprintf(stderr, "hpet_fasync: info.hi_flags 0x%lx\n", info.hi_flags);
266 if (info.hi_flags && (ioctl(fd, HPET_EPI, 0) < 0)) {
267 fprintf(stderr, "hpet_fasync: HPET_EPI failed\n");
271 if (ioctl(fd, HPET_IE_ON, 0) < 0) {
272 fprintf(stderr, "hpet_fasync, HPET_IE_ON failed\n");
276 for (i = 0; i < iterations; i++) {
278 fprintf(stderr, "hpet_fasync: count = %d\n", hpet_sigio_count);
282 signal(SIGIO, oldsig);
290 The kernel API has three interfaces exported from the driver:
292 hpet_register(struct hpet_task *tp, int periodic)
293 hpet_unregister(struct hpet_task *tp)
294 hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
296 The kernel module using this interface fills in the ht_func and ht_data
297 members of the hpet_task structure before calling hpet_register.
298 hpet_control simply vectors to the hpet_ioctl routine and has the same
299 commands and respective arguments as the user API. hpet_unregister
300 is used to terminate usage of the HPET timer reserved by hpet_register.