2 * Intel & MS High Precision Event Timer Implementation.
8 #include <linux/config.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/miscdevice.h>
14 #include <linux/major.h>
15 #include <linux/ioport.h>
16 #include <linux/fcntl.h>
17 #include <linux/init.h>
18 #include <linux/poll.h>
19 #include <linux/proc_fs.h>
20 #include <linux/spinlock.h>
21 #include <linux/sysctl.h>
22 #include <linux/wait.h>
23 #include <linux/bcd.h>
24 #include <linux/seq_file.h>
26 #include <asm/current.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
31 #include <asm/bitops.h>
32 #include <asm/div64.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <linux/hpet.h>
39 * The High Precision Event Timer driver.
40 * This driver is closely modelled after the rtc.c driver.
41 * http://www.intel.com/labs/platcomp/hpet/hpetspec.htm
43 #define HPET_USER_FREQ (64)
44 #define HPET_DRIFT (500)
46 static u32 hpet_ntimer, hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
48 /* A lock for concurrent access by app and isr hpet activity. */
49 static spinlock_t hpet_lock = SPIN_LOCK_UNLOCKED;
50 /* A lock for concurrent intermodule access to hpet and isr hpet activity. */
51 static spinlock_t hpet_task_lock = SPIN_LOCK_UNLOCKED;
53 #define HPET_DEV_NAME (7)
56 struct hpets *hd_hpets;
58 struct hpet_timer *hd_timer;
59 unsigned long hd_ireqfreq;
60 unsigned long hd_irqdata;
61 wait_queue_head_t hd_waitqueue;
62 struct fasync_struct *hd_async_queue;
63 struct hpet_task *hd_task;
64 unsigned int hd_flags;
66 unsigned int hd_hdwirq;
67 char hd_name[HPET_DEV_NAME];
71 struct hpets *hp_next;
73 unsigned long hp_period;
74 unsigned long hp_delta;
75 unsigned int hp_ntimer;
76 unsigned int hp_which;
77 struct hpet_dev hp_dev[1];
80 static struct hpets *hpets;
82 #define HPET_OPEN 0x0001
83 #define HPET_IE 0x0002 /* interrupt enabled */
84 #define HPET_PERIODIC 0x0004
86 #if BITS_PER_LONG == 64
87 #define write_counter(V, MC) writeq(V, MC)
88 #define read_counter(MC) readq(MC)
90 #define write_counter(V, MC) writel(V, MC)
91 #define read_counter(MC) readl(MC)
95 static unsigned long long __inline readq(void *addr)
97 return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
102 static void __inline writeq(unsigned long long v, void *addr)
104 writel(v & 0xffffffff, addr);
105 writel(v >> 32, addr + 4);
109 static irqreturn_t hpet_interrupt(int irq, void *data, struct pt_regs *regs)
111 struct hpet_dev *devp;
116 spin_lock(&hpet_lock);
120 * For non-periodic timers, increment the accumulator.
121 * This has the effect of treating non-periodic like periodic.
123 if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
126 t = devp->hd_ireqfreq;
127 m = read_counter(&devp->hd_hpet->hpet_mc);
128 write_counter(t + m + devp->hd_hpets->hp_delta,
129 &devp->hd_timer->hpet_compare);
132 isr = (1 << (devp - devp->hd_hpets->hp_dev));
133 writeq(isr, &devp->hd_hpet->hpet_isr);
134 spin_unlock(&hpet_lock);
136 spin_lock(&hpet_task_lock);
138 devp->hd_task->ht_func(devp->hd_task->ht_data);
139 spin_unlock(&hpet_task_lock);
141 wake_up_interruptible(&devp->hd_waitqueue);
143 kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
148 static int hpet_open(struct inode *inode, struct file *file)
150 struct hpet_dev *devp;
154 if (file->f_mode & FMODE_WRITE)
157 spin_lock_irq(&hpet_lock);
159 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
160 for (i = 0; i < hpetp->hp_ntimer; i++)
161 if (hpetp->hp_dev[i].hd_flags & HPET_OPEN
162 || hpetp->hp_dev[i].hd_task)
165 devp = &hpetp->hp_dev[i];
170 spin_unlock_irq(&hpet_lock);
174 file->private_data = devp;
175 devp->hd_irqdata = 0;
176 devp->hd_flags |= HPET_OPEN;
177 spin_unlock_irq(&hpet_lock);
183 hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
185 DECLARE_WAITQUEUE(wait, current);
188 struct hpet_dev *devp;
190 devp = file->private_data;
191 if (!devp->hd_ireqfreq)
194 if (count < sizeof(unsigned long))
197 add_wait_queue(&devp->hd_waitqueue, &wait);
200 set_current_state(TASK_INTERRUPTIBLE);
202 spin_lock_irq(&hpet_lock);
203 data = devp->hd_irqdata;
204 devp->hd_irqdata = 0;
205 spin_unlock_irq(&hpet_lock);
209 else if (file->f_flags & O_NONBLOCK) {
212 } else if (signal_pending(current)) {
213 retval = -ERESTARTSYS;
219 retval = put_user(data, (unsigned long __user *)buf);
221 retval = sizeof(unsigned long);
223 __set_current_state(TASK_RUNNING);
224 remove_wait_queue(&devp->hd_waitqueue, &wait);
229 static unsigned int hpet_poll(struct file *file, poll_table * wait)
232 struct hpet_dev *devp;
234 devp = file->private_data;
236 if (!devp->hd_ireqfreq)
239 poll_wait(file, &devp->hd_waitqueue, wait);
241 spin_lock_irq(&hpet_lock);
242 v = devp->hd_irqdata;
243 spin_unlock_irq(&hpet_lock);
246 return POLLIN | POLLRDNORM;
251 static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
253 #ifdef CONFIG_HPET_NOMMAP
256 struct hpet_dev *devp;
259 if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
262 devp = file->private_data;
263 addr = (unsigned long)devp->hd_hpet;
265 if (addr & (PAGE_SIZE - 1))
268 vma->vm_flags |= VM_IO;
269 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
273 (vma, vma->vm_start, addr, PAGE_SIZE, vma->vm_page_prot)) {
274 printk(KERN_ERR "remap_page_range failed in hpet.c\n");
282 static int hpet_fasync(int fd, struct file *file, int on)
284 struct hpet_dev *devp;
286 devp = file->private_data;
288 if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
294 static int hpet_release(struct inode *inode, struct file *file)
296 struct hpet_dev *devp;
297 struct hpet_timer *timer;
300 devp = file->private_data;
301 timer = devp->hd_timer;
303 spin_lock_irq(&hpet_lock);
305 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
306 &timer->hpet_config);
311 devp->hd_ireqfreq = 0;
313 if (devp->hd_flags & HPET_PERIODIC
314 && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
317 v = readq(&timer->hpet_config);
318 v ^= Tn_TYPE_CNF_MASK;
319 writeq(v, &timer->hpet_config);
322 devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
323 spin_unlock_irq(&hpet_lock);
328 if (file->f_flags & FASYNC)
329 hpet_fasync(-1, file, 0);
331 file->private_data = NULL;
335 static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
338 hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
341 struct hpet_dev *devp;
343 devp = file->private_data;
344 return hpet_ioctl_common(devp, cmd, arg, 0);
347 static int hpet_ioctl_ieon(struct hpet_dev *devp)
349 struct hpet_timer *timer;
353 unsigned long g, v, t, m;
354 unsigned long flags, isr;
356 timer = devp->hd_timer;
357 hpet = devp->hd_hpet;
358 hpetp = devp->hd_hpets;
360 v = readq(&timer->hpet_config);
361 spin_lock_irq(&hpet_lock);
363 if (devp->hd_flags & HPET_IE) {
364 spin_unlock_irq(&hpet_lock);
368 devp->hd_flags |= HPET_IE;
369 spin_unlock_irq(&hpet_lock);
371 t = readq(&timer->hpet_config);
372 irq = devp->hd_hdwirq;
375 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
378 (irq, hpet_interrupt, SA_INTERRUPT, devp->hd_name, (void *)devp)) {
379 printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
385 spin_lock_irq(&hpet_lock);
386 devp->hd_flags ^= HPET_IE;
387 spin_unlock_irq(&hpet_lock);
392 t = devp->hd_ireqfreq;
393 v = readq(&timer->hpet_config);
394 g = v | Tn_INT_ENB_CNF_MASK;
396 if (devp->hd_flags & HPET_PERIODIC) {
397 write_counter(t, &timer->hpet_compare);
398 g |= Tn_TYPE_CNF_MASK;
399 v |= Tn_TYPE_CNF_MASK;
400 writeq(v, &timer->hpet_config);
401 v |= Tn_VAL_SET_CNF_MASK;
402 writeq(v, &timer->hpet_config);
403 local_irq_save(flags);
404 m = read_counter(&hpet->hpet_mc);
405 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
407 local_irq_save(flags);
408 m = read_counter(&hpet->hpet_mc);
409 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
412 isr = (1 << (devp - hpets->hp_dev));
413 writeq(isr, &hpet->hpet_isr);
414 writeq(g, &timer->hpet_config);
415 local_irq_restore(flags);
420 static inline unsigned long hpet_time_div(unsigned long dis)
422 unsigned long long m = 1000000000000000ULL;
426 return (unsigned long)m;
430 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
432 struct hpet_timer *timer;
444 timer = devp->hd_timer;
445 hpet = devp->hd_hpet;
446 hpetp = devp->hd_hpets;
449 return hpet_ioctl_ieon(devp);
458 if ((devp->hd_flags & HPET_IE) == 0)
460 v = readq(&timer->hpet_config);
461 v &= ~Tn_INT_ENB_CNF_MASK;
462 writeq(v, &timer->hpet_config);
464 free_irq(devp->hd_irq, devp);
467 devp->hd_flags ^= HPET_IE;
471 struct hpet_info info;
473 info.hi_ireqfreq = hpet_time_div(hpetp->hp_period *
476 readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
477 info.hi_hpet = devp->hd_hpets->hp_which;
478 info.hi_timer = devp - devp->hd_hpets->hp_dev;
479 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
484 v = readq(&timer->hpet_config);
485 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
489 devp->hd_flags |= HPET_PERIODIC;
492 v = readq(&timer->hpet_config);
493 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
497 if (devp->hd_flags & HPET_PERIODIC &&
498 readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
499 v = readq(&timer->hpet_config);
500 v ^= Tn_TYPE_CNF_MASK;
501 writeq(v, &timer->hpet_config);
503 devp->hd_flags &= ~HPET_PERIODIC;
506 if (!kernel && (arg > hpet_max_freq) &&
507 !capable(CAP_SYS_RESOURCE)) {
512 if (arg & (arg - 1)) {
517 devp->hd_ireqfreq = hpet_time_div(hpetp->hp_period * arg);
523 static struct file_operations hpet_fops = {
524 .owner = THIS_MODULE,
530 .release = hpet_release,
531 .fasync = hpet_fasync,
535 EXPORT_SYMBOL(hpet_alloc);
536 EXPORT_SYMBOL(hpet_register);
537 EXPORT_SYMBOL(hpet_unregister);
538 EXPORT_SYMBOL(hpet_control);
540 int hpet_register(struct hpet_task *tp, int periodic)
544 struct hpet_timer *timer;
545 struct hpet_dev *devp;
550 mask = Tn_PER_INT_CAP_MASK;
559 spin_lock_irq(&hpet_task_lock);
560 spin_lock(&hpet_lock);
562 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
563 for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
564 i < hpetp->hp_ntimer; i++, timer++) {
565 if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
569 devp = &hpetp->hp_dev[i];
571 if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
576 tp->ht_opaque = devp;
581 spin_unlock(&hpet_lock);
582 spin_unlock_irq(&hpet_task_lock);
590 static inline int hpet_tpcheck(struct hpet_task *tp)
592 struct hpet_dev *devp;
595 devp = tp->ht_opaque;
600 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
601 if (devp >= hpetp->hp_dev
602 && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
603 && devp->hd_hpet == hpetp->hp_hpet)
609 int hpet_unregister(struct hpet_task *tp)
611 struct hpet_dev *devp;
612 struct hpet_timer *timer;
615 if ((err = hpet_tpcheck(tp)))
618 spin_lock_irq(&hpet_task_lock);
619 spin_lock(&hpet_lock);
621 devp = tp->ht_opaque;
622 if (devp->hd_task != tp) {
623 spin_unlock(&hpet_lock);
624 spin_unlock_irq(&hpet_task_lock);
628 timer = devp->hd_timer;
629 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
630 &timer->hpet_config);
631 devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
632 devp->hd_task = NULL;
633 spin_unlock(&hpet_lock);
634 spin_unlock_irq(&hpet_task_lock);
639 int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
641 struct hpet_dev *devp;
644 if ((err = hpet_tpcheck(tp)))
647 spin_lock_irq(&hpet_lock);
648 devp = tp->ht_opaque;
649 if (devp->hd_task != tp) {
650 spin_unlock_irq(&hpet_lock);
653 spin_unlock_irq(&hpet_lock);
654 return hpet_ioctl_common(devp, cmd, arg, 1);
657 #ifdef CONFIG_TIME_INTERPOLATION
659 static unsigned long hpet_offset, last_wall_hpet;
660 static long hpet_nsecs_per_cycle, hpet_cycles_per_sec;
662 static unsigned long hpet_getoffset(void)
664 return hpet_offset + (read_counter(&hpets->hp_hpet->hpet_mc) -
665 last_wall_hpet) * hpet_nsecs_per_cycle;
668 static void hpet_update(long delta)
671 unsigned long offset;
673 mc = read_counter(&hpets->hp_hpet->hpet_mc);
674 offset = hpet_offset + (mc - last_wall_hpet) * hpet_nsecs_per_cycle;
676 if (delta < 0 || (unsigned long)delta < offset)
677 hpet_offset = offset - delta;
683 static void hpet_reset(void)
686 last_wall_hpet = read_counter(&hpets->hp_hpet->hpet_mc);
689 static struct time_interpolator hpet_interpolator = {
690 .get_offset = hpet_getoffset,
691 .update = hpet_update,
697 static ctl_table hpet_table[] = {
700 .procname = "max-user-freq",
701 .data = &hpet_max_freq,
702 .maxlen = sizeof(int),
704 .proc_handler = &proc_dointvec,
709 static ctl_table hpet_root[] = {
720 static ctl_table dev_root[] = {
731 static struct ctl_table_header *sysctl_header;
734 * Adjustment for when arming the timer with
735 * initial conditions. That is, main counter
736 * ticks expired before interrupts are enabled.
738 #define TICK_CALIBRATE (1000UL)
740 static unsigned long __init hpet_calibrate(struct hpets *hpetp)
742 struct hpet_timer *timer = NULL;
743 unsigned long t, m, count, i, flags, start;
744 struct hpet_dev *devp;
748 for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
749 if ((devp->hd_flags & HPET_OPEN) == 0) {
750 timer = devp->hd_timer;
757 hpet = hpets->hp_hpet;
758 t = read_counter(&timer->hpet_compare);
761 count = hpet_time_div(hpetp->hp_period * TICK_CALIBRATE);
763 local_irq_save(flags);
765 start = read_counter(&hpet->hpet_mc);
768 m = read_counter(&hpet->hpet_mc);
769 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
770 } while (i++, (m - start) < count);
772 local_irq_restore(flags);
774 return (m - start) / i;
777 int __init hpet_alloc(struct hpet_data *hdp)
780 struct hpet_dev *devp;
785 static struct hpets *last __initdata = (struct hpets *)0;
788 * hpet_alloc can be called by platform dependent code.
789 * if platform dependent code has allocated the hpet
790 * ACPI also reports hpet, then we catch it here.
792 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
793 if (hpetp->hp_hpet == (struct hpet *)(hdp->hd_address))
796 siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
797 sizeof(struct hpet_dev));
799 hpetp = kmalloc(siz, GFP_KERNEL);
804 memset(hpetp, 0, siz);
806 hpetp->hp_which = hpet_nhpet++;
807 hpetp->hp_hpet = (struct hpet *)hdp->hd_address;
809 hpetp->hp_ntimer = hdp->hd_nirqs;
811 for (i = 0; i < hdp->hd_nirqs; i++)
812 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
814 hpet = hpetp->hp_hpet;
816 cap = readq(&hpet->hpet_cap);
818 ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
820 if (hpetp->hp_ntimer != ntimer) {
821 printk(KERN_WARNING "hpet: number irqs doesn't agree"
822 " with number of timers\n");
828 last->hp_next = hpetp;
834 hpetp->hp_period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
835 HPET_COUNTER_CLK_PERIOD_SHIFT;
837 mcfg = readq(&hpet->hpet_config);
838 if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
839 write_counter(0L, &hpet->hpet_mc);
840 mcfg |= HPET_ENABLE_CNF_MASK;
841 writeq(mcfg, &hpet->hpet_config);
844 for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer;
845 i++, hpet_ntimer++, devp++) {
847 struct hpet_timer *timer;
849 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
850 v = readq(&timer->hpet_config);
852 devp->hd_hpets = hpetp;
853 devp->hd_hpet = hpet;
854 devp->hd_timer = timer;
857 * If the timer was reserved by platform code,
858 * then make timer unavailable for opens.
860 if (hdp->hd_state & (1 << i)) {
861 devp->hd_flags = HPET_OPEN;
865 init_waitqueue_head(&devp->hd_waitqueue);
868 hpetp->hp_delta = hpet_calibrate(hpetp);
873 static acpi_status __init hpet_resources(struct acpi_resource *res, void *data)
875 struct hpet_data *hdp;
877 struct acpi_resource_address64 addr;
882 status = acpi_resource_to_address64(res, &addr);
884 if (ACPI_SUCCESS(status)) {
887 size = addr.max_address_range - addr.min_address_range + 1;
889 (unsigned long)ioremap(addr.min_address_range, size);
891 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
892 if (hpetp->hp_hpet == (struct hpet *)(hdp->hd_address))
894 } else if (res->id == ACPI_RSTYPE_EXT_IRQ) {
895 struct acpi_resource_ext_irq *irqp;
898 irqp = &res->data.extended_irq;
900 if (irqp->number_of_interrupts > 0) {
901 hdp->hd_nirqs = irqp->number_of_interrupts;
903 for (i = 0; i < hdp->hd_nirqs; i++)
906 acpi_register_gsi(irqp->interrupts[i],
908 irqp->active_high_low);
910 hdp->hd_irq[i] = irqp->interrupts[i];
918 static int __init hpet_acpi_add(struct acpi_device *device)
921 struct hpet_data data;
923 memset(&data, 0, sizeof(data));
926 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
927 hpet_resources, &data);
929 if (ACPI_FAILURE(result))
932 if (!data.hd_address || !data.hd_nirqs) {
933 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
937 return hpet_alloc(&data);
940 static int __init hpet_acpi_remove(struct acpi_device *device, int type)
945 static struct acpi_driver hpet_acpi_driver __initdata = {
950 .add = hpet_acpi_add,
951 .remove = hpet_acpi_remove,
955 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
957 static int __init hpet_init(void)
959 (void)acpi_bus_register_driver(&hpet_acpi_driver);
962 if (misc_register(&hpet_misc))
965 sysctl_header = register_sysctl_table(dev_root, 0);
967 #ifdef CONFIG_TIME_INTERPOLATION
971 hpet = hpets->hp_hpet;
972 hpet_cycles_per_sec = hpet_time_div(hpets->hp_period);
973 hpet_interpolator.frequency = hpet_cycles_per_sec;
974 hpet_interpolator.drift = hpet_cycles_per_sec *
975 HPET_DRIFT / 1000000;
976 hpet_nsecs_per_cycle = 1000000000 / hpet_cycles_per_sec;
977 register_time_interpolator(&hpet_interpolator);
985 static void __exit hpet_exit(void)
987 acpi_bus_unregister_driver(&hpet_acpi_driver);
990 unregister_sysctl_table(sysctl_header);
995 module_init(hpet_init);
996 module_exit(hpet_exit);
997 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
998 MODULE_LICENSE("GPL");