2 * Intel & MS High Precision Event Timer Implementation.
4 * Copyright (C) 2003 Intel Corporation
6 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
7 * Bob Picco <robert.picco@hp.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/config.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/miscdevice.h>
20 #include <linux/major.h>
21 #include <linux/ioport.h>
22 #include <linux/fcntl.h>
23 #include <linux/init.h>
24 #include <linux/poll.h>
25 #include <linux/proc_fs.h>
26 #include <linux/spinlock.h>
27 #include <linux/sysctl.h>
28 #include <linux/wait.h>
29 #include <linux/bcd.h>
30 #include <linux/seq_file.h>
32 #include <asm/current.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
37 #include <asm/bitops.h>
38 #include <asm/div64.h>
40 #include <linux/acpi.h>
41 #include <acpi/acpi_bus.h>
42 #include <linux/hpet.h>
45 * The High Precision Event Timer driver.
46 * This driver is closely modelled after the rtc.c driver.
47 * http://www.intel.com/labs/platcomp/hpet/hpetspec.htm
49 #define HPET_USER_FREQ (64)
50 #define HPET_DRIFT (500)
52 static u32 hpet_ntimer, hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
54 /* A lock for concurrent access by app and isr hpet activity. */
55 static spinlock_t hpet_lock = SPIN_LOCK_UNLOCKED;
56 /* A lock for concurrent intermodule access to hpet and isr hpet activity. */
57 static spinlock_t hpet_task_lock = SPIN_LOCK_UNLOCKED;
59 #define HPET_DEV_NAME (7)
62 struct hpets *hd_hpets;
64 struct hpet_timer *hd_timer;
65 unsigned long hd_ireqfreq;
66 unsigned long hd_irqdata;
67 wait_queue_head_t hd_waitqueue;
68 struct fasync_struct *hd_async_queue;
69 struct hpet_task *hd_task;
70 unsigned int hd_flags;
72 unsigned int hd_hdwirq;
73 char hd_name[HPET_DEV_NAME];
77 struct hpets *hp_next;
79 unsigned long hp_period;
80 unsigned long hp_delta;
81 unsigned int hp_ntimer;
82 unsigned int hp_which;
83 struct hpet_dev hp_dev[1];
86 static struct hpets *hpets;
88 #define HPET_OPEN 0x0001
89 #define HPET_IE 0x0002 /* interrupt enabled */
90 #define HPET_PERIODIC 0x0004
92 #if BITS_PER_LONG == 64
93 #define write_counter(V, MC) writeq(V, MC)
94 #define read_counter(MC) readq(MC)
96 #define write_counter(V, MC) writel(V, MC)
97 #define read_counter(MC) readl(MC)
101 static unsigned long long __inline readq(void *addr)
103 return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
108 static void __inline writeq(unsigned long long v, void *addr)
110 writel(v & 0xffffffff, addr);
111 writel(v >> 32, addr + 4);
115 static irqreturn_t hpet_interrupt(int irq, void *data, struct pt_regs *regs)
117 struct hpet_dev *devp;
122 spin_lock(&hpet_lock);
126 * For non-periodic timers, increment the accumulator.
127 * This has the effect of treating non-periodic like periodic.
129 if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
132 t = devp->hd_ireqfreq;
133 m = read_counter(&devp->hd_hpet->hpet_mc);
134 write_counter(t + m + devp->hd_hpets->hp_delta,
135 &devp->hd_timer->hpet_compare);
138 isr = (1 << (devp - devp->hd_hpets->hp_dev));
139 writeq(isr, &devp->hd_hpet->hpet_isr);
140 spin_unlock(&hpet_lock);
142 spin_lock(&hpet_task_lock);
144 devp->hd_task->ht_func(devp->hd_task->ht_data);
145 spin_unlock(&hpet_task_lock);
147 wake_up_interruptible(&devp->hd_waitqueue);
149 kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
154 static int hpet_open(struct inode *inode, struct file *file)
156 struct hpet_dev *devp;
160 if (file->f_mode & FMODE_WRITE)
163 spin_lock_irq(&hpet_lock);
165 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
166 for (i = 0; i < hpetp->hp_ntimer; i++)
167 if (hpetp->hp_dev[i].hd_flags & HPET_OPEN
168 || hpetp->hp_dev[i].hd_task)
171 devp = &hpetp->hp_dev[i];
176 spin_unlock_irq(&hpet_lock);
180 file->private_data = devp;
181 devp->hd_irqdata = 0;
182 devp->hd_flags |= HPET_OPEN;
183 spin_unlock_irq(&hpet_lock);
189 hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
191 DECLARE_WAITQUEUE(wait, current);
194 struct hpet_dev *devp;
196 devp = file->private_data;
197 if (!devp->hd_ireqfreq)
200 if (count < sizeof(unsigned long))
203 add_wait_queue(&devp->hd_waitqueue, &wait);
206 set_current_state(TASK_INTERRUPTIBLE);
208 spin_lock_irq(&hpet_lock);
209 data = devp->hd_irqdata;
210 devp->hd_irqdata = 0;
211 spin_unlock_irq(&hpet_lock);
215 else if (file->f_flags & O_NONBLOCK) {
218 } else if (signal_pending(current)) {
219 retval = -ERESTARTSYS;
225 retval = put_user(data, (unsigned long __user *)buf);
227 retval = sizeof(unsigned long);
229 __set_current_state(TASK_RUNNING);
230 remove_wait_queue(&devp->hd_waitqueue, &wait);
235 static unsigned int hpet_poll(struct file *file, poll_table * wait)
238 struct hpet_dev *devp;
240 devp = file->private_data;
242 if (!devp->hd_ireqfreq)
245 poll_wait(file, &devp->hd_waitqueue, wait);
247 spin_lock_irq(&hpet_lock);
248 v = devp->hd_irqdata;
249 spin_unlock_irq(&hpet_lock);
252 return POLLIN | POLLRDNORM;
257 static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
259 #ifdef CONFIG_HPET_MMAP
260 struct hpet_dev *devp;
263 if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
266 devp = file->private_data;
267 addr = (unsigned long)devp->hd_hpet;
269 if (addr & (PAGE_SIZE - 1))
272 vma->vm_flags |= VM_IO;
273 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
277 (vma, vma->vm_start, addr, PAGE_SIZE, vma->vm_page_prot)) {
278 printk(KERN_ERR "remap_page_range failed in hpet.c\n");
288 static int hpet_fasync(int fd, struct file *file, int on)
290 struct hpet_dev *devp;
292 devp = file->private_data;
294 if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
300 static int hpet_release(struct inode *inode, struct file *file)
302 struct hpet_dev *devp;
303 struct hpet_timer *timer;
306 devp = file->private_data;
307 timer = devp->hd_timer;
309 spin_lock_irq(&hpet_lock);
311 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
312 &timer->hpet_config);
317 devp->hd_ireqfreq = 0;
319 if (devp->hd_flags & HPET_PERIODIC
320 && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
323 v = readq(&timer->hpet_config);
324 v ^= Tn_TYPE_CNF_MASK;
325 writeq(v, &timer->hpet_config);
328 devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
329 spin_unlock_irq(&hpet_lock);
334 if (file->f_flags & FASYNC)
335 hpet_fasync(-1, file, 0);
337 file->private_data = NULL;
341 static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
344 hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
347 struct hpet_dev *devp;
349 devp = file->private_data;
350 return hpet_ioctl_common(devp, cmd, arg, 0);
353 static int hpet_ioctl_ieon(struct hpet_dev *devp)
355 struct hpet_timer *timer;
359 unsigned long g, v, t, m;
360 unsigned long flags, isr;
362 timer = devp->hd_timer;
363 hpet = devp->hd_hpet;
364 hpetp = devp->hd_hpets;
366 v = readq(&timer->hpet_config);
367 spin_lock_irq(&hpet_lock);
369 if (devp->hd_flags & HPET_IE) {
370 spin_unlock_irq(&hpet_lock);
374 devp->hd_flags |= HPET_IE;
375 spin_unlock_irq(&hpet_lock);
377 t = readq(&timer->hpet_config);
378 irq = devp->hd_hdwirq;
381 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
384 (irq, hpet_interrupt, SA_INTERRUPT, devp->hd_name, (void *)devp)) {
385 printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
391 spin_lock_irq(&hpet_lock);
392 devp->hd_flags ^= HPET_IE;
393 spin_unlock_irq(&hpet_lock);
398 t = devp->hd_ireqfreq;
399 v = readq(&timer->hpet_config);
400 g = v | Tn_INT_ENB_CNF_MASK;
402 if (devp->hd_flags & HPET_PERIODIC) {
403 write_counter(t, &timer->hpet_compare);
404 g |= Tn_TYPE_CNF_MASK;
405 v |= Tn_TYPE_CNF_MASK;
406 writeq(v, &timer->hpet_config);
407 v |= Tn_VAL_SET_CNF_MASK;
408 writeq(v, &timer->hpet_config);
409 local_irq_save(flags);
410 m = read_counter(&hpet->hpet_mc);
411 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
413 local_irq_save(flags);
414 m = read_counter(&hpet->hpet_mc);
415 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
418 isr = (1 << (devp - hpets->hp_dev));
419 writeq(isr, &hpet->hpet_isr);
420 writeq(g, &timer->hpet_config);
421 local_irq_restore(flags);
426 static inline unsigned long hpet_time_div(unsigned long dis)
428 unsigned long long m = 1000000000000000ULL;
432 return (unsigned long)m;
436 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
438 struct hpet_timer *timer;
450 timer = devp->hd_timer;
451 hpet = devp->hd_hpet;
452 hpetp = devp->hd_hpets;
455 return hpet_ioctl_ieon(devp);
464 if ((devp->hd_flags & HPET_IE) == 0)
466 v = readq(&timer->hpet_config);
467 v &= ~Tn_INT_ENB_CNF_MASK;
468 writeq(v, &timer->hpet_config);
470 free_irq(devp->hd_irq, devp);
473 devp->hd_flags ^= HPET_IE;
477 struct hpet_info info;
479 info.hi_ireqfreq = hpet_time_div(hpetp->hp_period *
482 readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
483 info.hi_hpet = devp->hd_hpets->hp_which;
484 info.hi_timer = devp - devp->hd_hpets->hp_dev;
485 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
490 v = readq(&timer->hpet_config);
491 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
495 devp->hd_flags |= HPET_PERIODIC;
498 v = readq(&timer->hpet_config);
499 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
503 if (devp->hd_flags & HPET_PERIODIC &&
504 readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
505 v = readq(&timer->hpet_config);
506 v ^= Tn_TYPE_CNF_MASK;
507 writeq(v, &timer->hpet_config);
509 devp->hd_flags &= ~HPET_PERIODIC;
512 if (!kernel && (arg > hpet_max_freq) &&
513 !capable(CAP_SYS_RESOURCE)) {
518 if (arg & (arg - 1)) {
523 devp->hd_ireqfreq = hpet_time_div(hpetp->hp_period * arg);
529 static struct file_operations hpet_fops = {
530 .owner = THIS_MODULE,
536 .release = hpet_release,
537 .fasync = hpet_fasync,
541 EXPORT_SYMBOL(hpet_alloc);
542 EXPORT_SYMBOL(hpet_register);
543 EXPORT_SYMBOL(hpet_unregister);
544 EXPORT_SYMBOL(hpet_control);
546 int hpet_register(struct hpet_task *tp, int periodic)
550 struct hpet_timer *timer;
551 struct hpet_dev *devp;
556 mask = Tn_PER_INT_CAP_MASK;
565 spin_lock_irq(&hpet_task_lock);
566 spin_lock(&hpet_lock);
568 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
569 for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
570 i < hpetp->hp_ntimer; i++, timer++) {
571 if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
575 devp = &hpetp->hp_dev[i];
577 if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
582 tp->ht_opaque = devp;
587 spin_unlock(&hpet_lock);
588 spin_unlock_irq(&hpet_task_lock);
596 static inline int hpet_tpcheck(struct hpet_task *tp)
598 struct hpet_dev *devp;
601 devp = tp->ht_opaque;
606 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
607 if (devp >= hpetp->hp_dev
608 && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
609 && devp->hd_hpet == hpetp->hp_hpet)
615 int hpet_unregister(struct hpet_task *tp)
617 struct hpet_dev *devp;
618 struct hpet_timer *timer;
621 if ((err = hpet_tpcheck(tp)))
624 spin_lock_irq(&hpet_task_lock);
625 spin_lock(&hpet_lock);
627 devp = tp->ht_opaque;
628 if (devp->hd_task != tp) {
629 spin_unlock(&hpet_lock);
630 spin_unlock_irq(&hpet_task_lock);
634 timer = devp->hd_timer;
635 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
636 &timer->hpet_config);
637 devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
638 devp->hd_task = NULL;
639 spin_unlock(&hpet_lock);
640 spin_unlock_irq(&hpet_task_lock);
645 int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
647 struct hpet_dev *devp;
650 if ((err = hpet_tpcheck(tp)))
653 spin_lock_irq(&hpet_lock);
654 devp = tp->ht_opaque;
655 if (devp->hd_task != tp) {
656 spin_unlock_irq(&hpet_lock);
659 spin_unlock_irq(&hpet_lock);
660 return hpet_ioctl_common(devp, cmd, arg, 1);
663 #ifdef CONFIG_TIME_INTERPOLATION
665 static unsigned long hpet_offset, last_wall_hpet;
666 static long hpet_nsecs_per_cycle, hpet_cycles_per_sec;
668 static unsigned long hpet_getoffset(void)
670 return hpet_offset + (read_counter(&hpets->hp_hpet->hpet_mc) -
671 last_wall_hpet) * hpet_nsecs_per_cycle;
674 static void hpet_update(long delta)
677 unsigned long offset;
679 mc = read_counter(&hpets->hp_hpet->hpet_mc);
680 offset = hpet_offset + (mc - last_wall_hpet) * hpet_nsecs_per_cycle;
682 if (delta < 0 || (unsigned long)delta < offset)
683 hpet_offset = offset - delta;
689 static void hpet_reset(void)
692 last_wall_hpet = read_counter(&hpets->hp_hpet->hpet_mc);
695 static struct time_interpolator hpet_interpolator = {
696 .get_offset = hpet_getoffset,
697 .update = hpet_update,
703 static ctl_table hpet_table[] = {
706 .procname = "max-user-freq",
707 .data = &hpet_max_freq,
708 .maxlen = sizeof(int),
710 .proc_handler = &proc_dointvec,
715 static ctl_table hpet_root[] = {
726 static ctl_table dev_root[] = {
737 static struct ctl_table_header *sysctl_header;
740 * Adjustment for when arming the timer with
741 * initial conditions. That is, main counter
742 * ticks expired before interrupts are enabled.
744 #define TICK_CALIBRATE (1000UL)
746 static unsigned long __init hpet_calibrate(struct hpets *hpetp)
748 struct hpet_timer *timer = NULL;
749 unsigned long t, m, count, i, flags, start;
750 struct hpet_dev *devp;
754 for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
755 if ((devp->hd_flags & HPET_OPEN) == 0) {
756 timer = devp->hd_timer;
763 hpet = hpets->hp_hpet;
764 t = read_counter(&timer->hpet_compare);
767 count = hpet_time_div(hpetp->hp_period * TICK_CALIBRATE);
769 local_irq_save(flags);
771 start = read_counter(&hpet->hpet_mc);
774 m = read_counter(&hpet->hpet_mc);
775 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
776 } while (i++, (m - start) < count);
778 local_irq_restore(flags);
780 return (m - start) / i;
783 int __init hpet_alloc(struct hpet_data *hdp)
786 struct hpet_dev *devp;
791 static struct hpets *last __initdata = (struct hpets *)0;
794 * hpet_alloc can be called by platform dependent code.
795 * if platform dependent code has allocated the hpet
796 * ACPI also reports hpet, then we catch it here.
798 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
799 if (hpetp->hp_hpet == (struct hpet *)(hdp->hd_address))
802 siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
803 sizeof(struct hpet_dev));
805 hpetp = kmalloc(siz, GFP_KERNEL);
810 memset(hpetp, 0, siz);
812 hpetp->hp_which = hpet_nhpet++;
813 hpetp->hp_hpet = (struct hpet *)hdp->hd_address;
815 hpetp->hp_ntimer = hdp->hd_nirqs;
817 for (i = 0; i < hdp->hd_nirqs; i++)
818 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
820 hpet = hpetp->hp_hpet;
822 cap = readq(&hpet->hpet_cap);
824 ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
826 if (hpetp->hp_ntimer != ntimer) {
827 printk(KERN_WARNING "hpet: number irqs doesn't agree"
828 " with number of timers\n");
834 last->hp_next = hpetp;
840 hpetp->hp_period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
841 HPET_COUNTER_CLK_PERIOD_SHIFT;
843 mcfg = readq(&hpet->hpet_config);
844 if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
845 write_counter(0L, &hpet->hpet_mc);
846 mcfg |= HPET_ENABLE_CNF_MASK;
847 writeq(mcfg, &hpet->hpet_config);
850 for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer;
851 i++, hpet_ntimer++, devp++) {
853 struct hpet_timer *timer;
855 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
856 v = readq(&timer->hpet_config);
858 devp->hd_hpets = hpetp;
859 devp->hd_hpet = hpet;
860 devp->hd_timer = timer;
863 * If the timer was reserved by platform code,
864 * then make timer unavailable for opens.
866 if (hdp->hd_state & (1 << i)) {
867 devp->hd_flags = HPET_OPEN;
871 init_waitqueue_head(&devp->hd_waitqueue);
874 hpetp->hp_delta = hpet_calibrate(hpetp);
879 static acpi_status __init hpet_resources(struct acpi_resource *res, void *data)
881 struct hpet_data *hdp;
883 struct acpi_resource_address64 addr;
888 status = acpi_resource_to_address64(res, &addr);
890 if (ACPI_SUCCESS(status)) {
893 size = addr.max_address_range - addr.min_address_range + 1;
895 (unsigned long)ioremap(addr.min_address_range, size);
897 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
898 if (hpetp->hp_hpet == (struct hpet *)(hdp->hd_address))
900 } else if (res->id == ACPI_RSTYPE_EXT_IRQ) {
901 struct acpi_resource_ext_irq *irqp;
904 irqp = &res->data.extended_irq;
906 if (irqp->number_of_interrupts > 0) {
907 hdp->hd_nirqs = irqp->number_of_interrupts;
909 for (i = 0; i < hdp->hd_nirqs; i++)
911 acpi_register_gsi(irqp->interrupts[i],
913 irqp->active_high_low);
920 static int __init hpet_acpi_add(struct acpi_device *device)
923 struct hpet_data data;
925 memset(&data, 0, sizeof(data));
928 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
929 hpet_resources, &data);
931 if (ACPI_FAILURE(result))
934 if (!data.hd_address || !data.hd_nirqs) {
935 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
939 return hpet_alloc(&data);
942 static int __init hpet_acpi_remove(struct acpi_device *device, int type)
947 static struct acpi_driver hpet_acpi_driver __initdata = {
952 .add = hpet_acpi_add,
953 .remove = hpet_acpi_remove,
957 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
959 static int __init hpet_init(void)
961 (void)acpi_bus_register_driver(&hpet_acpi_driver);
964 if (misc_register(&hpet_misc))
967 sysctl_header = register_sysctl_table(dev_root, 0);
969 #ifdef CONFIG_TIME_INTERPOLATION
973 hpet = hpets->hp_hpet;
974 hpet_cycles_per_sec = hpet_time_div(hpets->hp_period);
975 hpet_interpolator.frequency = hpet_cycles_per_sec;
976 hpet_interpolator.drift = hpet_cycles_per_sec *
977 HPET_DRIFT / 1000000;
978 hpet_nsecs_per_cycle = 1000000000 / hpet_cycles_per_sec;
979 register_time_interpolator(&hpet_interpolator);
987 static void __exit hpet_exit(void)
989 acpi_bus_unregister_driver(&hpet_acpi_driver);
992 unregister_sysctl_table(sysctl_header);
997 module_init(hpet_init);
998 module_exit(hpet_exit);
999 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
1000 MODULE_LICENSE("GPL");