vserver 1.9.3
[linux-2.6.git] / drivers / char / hpet.c
1 /*
2  * Intel & MS High Precision Event Timer Implementation.
3  *
4  * Copyright (C) 2003 Intel Corporation
5  *      Venki Pallipadi
6  * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
7  *      Bob Picco <robert.picco@hp.com>
8  *
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.
12  */
13
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>
31
32 #include <asm/current.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <asm/io.h>
36 #include <asm/irq.h>
37 #include <asm/bitops.h>
38 #include <asm/div64.h>
39
40 #include <linux/acpi.h>
41 #include <acpi/acpi_bus.h>
42 #include <linux/hpet.h>
43
44 /*
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
48  */
49 #define HPET_USER_FREQ  (64)
50 #define HPET_DRIFT      (500)
51
52 static u32 hpet_ntimer, hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
53
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;
58
59 #define HPET_DEV_NAME   (7)
60
61 struct hpet_dev {
62         struct hpets *hd_hpets;
63         struct hpet __iomem *hd_hpet;
64         struct hpet_timer __iomem *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;
71         unsigned int hd_irq;
72         unsigned int hd_hdwirq;
73         char hd_name[HPET_DEV_NAME];
74 };
75
76 struct hpets {
77         struct hpets *hp_next;
78         struct hpet __iomem *hp_hpet;
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];
84 };
85
86 static struct hpets *hpets;
87
88 #define HPET_OPEN               0x0001
89 #define HPET_IE                 0x0002  /* interrupt enabled */
90 #define HPET_PERIODIC           0x0004
91
92 #if BITS_PER_LONG == 64
93 #define write_counter(V, MC)    writeq(V, MC)
94 #define read_counter(MC)        readq(MC)
95 #else
96 #define write_counter(V, MC)    writel(V, MC)
97 #define read_counter(MC)        readl(MC)
98 #endif
99
100 #ifndef readq
101 static unsigned long long __inline readq(void __iomem *addr)
102 {
103         return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
104 }
105 #endif
106
107 #ifndef writeq
108 static void __inline writeq(unsigned long long v, void __iomem *addr)
109 {
110         writel(v & 0xffffffff, addr);
111         writel(v >> 32, addr + 4);
112 }
113 #endif
114
115 static irqreturn_t hpet_interrupt(int irq, void *data, struct pt_regs *regs)
116 {
117         struct hpet_dev *devp;
118         unsigned long isr;
119
120         devp = data;
121
122         spin_lock(&hpet_lock);
123         devp->hd_irqdata++;
124
125         /*
126          * For non-periodic timers, increment the accumulator.
127          * This has the effect of treating non-periodic like periodic.
128          */
129         if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
130                 unsigned long m, t;
131
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);
136         }
137
138         isr = (1 << (devp - devp->hd_hpets->hp_dev));
139         writeq(isr, &devp->hd_hpet->hpet_isr);
140         spin_unlock(&hpet_lock);
141
142         spin_lock(&hpet_task_lock);
143         if (devp->hd_task)
144                 devp->hd_task->ht_func(devp->hd_task->ht_data);
145         spin_unlock(&hpet_task_lock);
146
147         wake_up_interruptible(&devp->hd_waitqueue);
148
149         kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
150
151         return IRQ_HANDLED;
152 }
153
154 static int hpet_open(struct inode *inode, struct file *file)
155 {
156         struct hpet_dev *devp;
157         struct hpets *hpetp;
158         int i;
159
160         if (file->f_mode & FMODE_WRITE)
161                 return -EINVAL;
162
163         spin_lock_irq(&hpet_lock);
164
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)
169                                 continue;
170                         else {
171                                 devp = &hpetp->hp_dev[i];
172                                 break;
173                         }
174
175         if (!devp) {
176                 spin_unlock_irq(&hpet_lock);
177                 return -EBUSY;
178         }
179
180         file->private_data = devp;
181         devp->hd_irqdata = 0;
182         devp->hd_flags |= HPET_OPEN;
183         spin_unlock_irq(&hpet_lock);
184
185         return 0;
186 }
187
188 static ssize_t
189 hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
190 {
191         DECLARE_WAITQUEUE(wait, current);
192         unsigned long data;
193         ssize_t retval;
194         struct hpet_dev *devp;
195
196         devp = file->private_data;
197         if (!devp->hd_ireqfreq)
198                 return -EIO;
199
200         if (count < sizeof(unsigned long))
201                 return -EINVAL;
202
203         add_wait_queue(&devp->hd_waitqueue, &wait);
204
205         for ( ; ; ) {
206                 set_current_state(TASK_INTERRUPTIBLE);
207
208                 spin_lock_irq(&hpet_lock);
209                 data = devp->hd_irqdata;
210                 devp->hd_irqdata = 0;
211                 spin_unlock_irq(&hpet_lock);
212
213                 if (data)
214                         break;
215                 else if (file->f_flags & O_NONBLOCK) {
216                         retval = -EAGAIN;
217                         goto out;
218                 } else if (signal_pending(current)) {
219                         retval = -ERESTARTSYS;
220                         goto out;
221                 }
222                 schedule();
223         }
224
225         retval = put_user(data, (unsigned long __user *)buf);
226         if (!retval)
227                 retval = sizeof(unsigned long);
228 out:
229         __set_current_state(TASK_RUNNING);
230         remove_wait_queue(&devp->hd_waitqueue, &wait);
231
232         return retval;
233 }
234
235 static unsigned int hpet_poll(struct file *file, poll_table * wait)
236 {
237         unsigned long v;
238         struct hpet_dev *devp;
239
240         devp = file->private_data;
241
242         if (!devp->hd_ireqfreq)
243                 return 0;
244
245         poll_wait(file, &devp->hd_waitqueue, wait);
246
247         spin_lock_irq(&hpet_lock);
248         v = devp->hd_irqdata;
249         spin_unlock_irq(&hpet_lock);
250
251         if (v != 0)
252                 return POLLIN | POLLRDNORM;
253
254         return 0;
255 }
256
257 static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
258 {
259 #ifdef  CONFIG_HPET_MMAP
260         struct hpet_dev *devp;
261         unsigned long addr;
262
263         if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
264                 return -EINVAL;
265
266         devp = file->private_data;
267         addr = (unsigned long)devp->hd_hpet;
268
269         if (addr & (PAGE_SIZE - 1))
270                 return -ENOSYS;
271
272         vma->vm_flags |= VM_IO;
273         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
274         addr = __pa(addr);
275
276         if (remap_page_range
277             (vma, vma->vm_start, addr, PAGE_SIZE, vma->vm_page_prot)) {
278                 printk(KERN_ERR "remap_page_range failed in hpet.c\n");
279                 return -EAGAIN;
280         }
281
282         return 0;
283 #else
284         return -ENOSYS;
285 #endif
286 }
287
288 static int hpet_fasync(int fd, struct file *file, int on)
289 {
290         struct hpet_dev *devp;
291
292         devp = file->private_data;
293
294         if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
295                 return 0;
296         else
297                 return -EIO;
298 }
299
300 static int hpet_release(struct inode *inode, struct file *file)
301 {
302         struct hpet_dev *devp;
303         struct hpet_timer __iomem *timer;
304         int irq = 0;
305
306         devp = file->private_data;
307         timer = devp->hd_timer;
308
309         spin_lock_irq(&hpet_lock);
310
311         writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
312                &timer->hpet_config);
313
314         irq = devp->hd_irq;
315         devp->hd_irq = 0;
316
317         devp->hd_ireqfreq = 0;
318
319         if (devp->hd_flags & HPET_PERIODIC
320             && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
321                 unsigned long v;
322
323                 v = readq(&timer->hpet_config);
324                 v ^= Tn_TYPE_CNF_MASK;
325                 writeq(v, &timer->hpet_config);
326         }
327
328         devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
329         spin_unlock_irq(&hpet_lock);
330
331         if (irq)
332                 free_irq(irq, devp);
333
334         if (file->f_flags & FASYNC)
335                 hpet_fasync(-1, file, 0);
336
337         file->private_data = NULL;
338         return 0;
339 }
340
341 static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
342
343 static int
344 hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
345            unsigned long arg)
346 {
347         struct hpet_dev *devp;
348
349         devp = file->private_data;
350         return hpet_ioctl_common(devp, cmd, arg, 0);
351 }
352
353 static int hpet_ioctl_ieon(struct hpet_dev *devp)
354 {
355         struct hpet_timer __iomem *timer;
356         struct hpet __iomem *hpet;
357         struct hpets *hpetp;
358         int irq;
359         unsigned long g, v, t, m;
360         unsigned long flags, isr;
361
362         timer = devp->hd_timer;
363         hpet = devp->hd_hpet;
364         hpetp = devp->hd_hpets;
365
366         v = readq(&timer->hpet_config);
367         spin_lock_irq(&hpet_lock);
368
369         if (devp->hd_flags & HPET_IE) {
370                 spin_unlock_irq(&hpet_lock);
371                 return -EBUSY;
372         }
373
374         devp->hd_flags |= HPET_IE;
375         spin_unlock_irq(&hpet_lock);
376
377         t = readq(&timer->hpet_config);
378         irq = devp->hd_hdwirq;
379
380         if (irq) {
381                 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
382
383                 if (request_irq
384                     (irq, hpet_interrupt, SA_INTERRUPT, devp->hd_name, (void *)devp)) {
385                         printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
386                         irq = 0;
387                 }
388         }
389
390         if (irq == 0) {
391                 spin_lock_irq(&hpet_lock);
392                 devp->hd_flags ^= HPET_IE;
393                 spin_unlock_irq(&hpet_lock);
394                 return -EIO;
395         }
396
397         devp->hd_irq = irq;
398         t = devp->hd_ireqfreq;
399         v = readq(&timer->hpet_config);
400         g = v | Tn_INT_ENB_CNF_MASK;
401
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);
412         } else {
413                 local_irq_save(flags);
414                 m = read_counter(&hpet->hpet_mc);
415                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
416         }
417
418         isr = (1 << (devp - hpets->hp_dev));
419         writeq(isr, &hpet->hpet_isr);
420         writeq(g, &timer->hpet_config);
421         local_irq_restore(flags);
422
423         return 0;
424 }
425
426 static inline unsigned long hpet_time_div(unsigned long dis)
427 {
428         unsigned long long m = 1000000000000000ULL;
429
430         do_div(m, dis);
431
432         return (unsigned long)m;
433 }
434
435 static int
436 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
437 {
438         struct hpet_timer __iomem *timer;
439         struct hpet __iomem *hpet;
440         struct hpets *hpetp;
441         int err;
442         unsigned long v;
443
444         switch (cmd) {
445         case HPET_IE_OFF:
446         case HPET_INFO:
447         case HPET_EPI:
448         case HPET_DPI:
449         case HPET_IRQFREQ:
450                 timer = devp->hd_timer;
451                 hpet = devp->hd_hpet;
452                 hpetp = devp->hd_hpets;
453                 break;
454         case HPET_IE_ON:
455                 return hpet_ioctl_ieon(devp);
456         default:
457                 return -EINVAL;
458         }
459
460         err = 0;
461
462         switch (cmd) {
463         case HPET_IE_OFF:
464                 if ((devp->hd_flags & HPET_IE) == 0)
465                         break;
466                 v = readq(&timer->hpet_config);
467                 v &= ~Tn_INT_ENB_CNF_MASK;
468                 writeq(v, &timer->hpet_config);
469                 if (devp->hd_irq) {
470                         free_irq(devp->hd_irq, devp);
471                         devp->hd_irq = 0;
472                 }
473                 devp->hd_flags ^= HPET_IE;
474                 break;
475         case HPET_INFO:
476                 {
477                         struct hpet_info info;
478
479                         info.hi_ireqfreq = hpet_time_div(hpetp->hp_period *
480                                                          devp->hd_ireqfreq);
481                         info.hi_flags =
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)))
486                                 err = -EFAULT;
487                         break;
488                 }
489         case HPET_EPI:
490                 v = readq(&timer->hpet_config);
491                 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
492                         err = -ENXIO;
493                         break;
494                 }
495                 devp->hd_flags |= HPET_PERIODIC;
496                 break;
497         case HPET_DPI:
498                 v = readq(&timer->hpet_config);
499                 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
500                         err = -ENXIO;
501                         break;
502                 }
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);
508                 }
509                 devp->hd_flags &= ~HPET_PERIODIC;
510                 break;
511         case HPET_IRQFREQ:
512                 if (!kernel && (arg > hpet_max_freq) &&
513                     !capable(CAP_SYS_RESOURCE)) {
514                         err = -EACCES;
515                         break;
516                 }
517
518                 if (arg & (arg - 1)) {
519                         err = -EINVAL;
520                         break;
521                 }
522
523                 devp->hd_ireqfreq = hpet_time_div(hpetp->hp_period * arg);
524         }
525
526         return err;
527 }
528
529 static struct file_operations hpet_fops = {
530         .owner = THIS_MODULE,
531         .llseek = no_llseek,
532         .read = hpet_read,
533         .poll = hpet_poll,
534         .ioctl = hpet_ioctl,
535         .open = hpet_open,
536         .release = hpet_release,
537         .fasync = hpet_fasync,
538         .mmap = hpet_mmap,
539 };
540
541 EXPORT_SYMBOL(hpet_alloc);
542 EXPORT_SYMBOL(hpet_register);
543 EXPORT_SYMBOL(hpet_unregister);
544 EXPORT_SYMBOL(hpet_control);
545
546 int hpet_register(struct hpet_task *tp, int periodic)
547 {
548         unsigned int i;
549         u64 mask;
550         struct hpet_timer __iomem *timer;
551         struct hpet_dev *devp;
552         struct hpets *hpetp;
553
554         switch (periodic) {
555         case 1:
556                 mask = Tn_PER_INT_CAP_MASK;
557                 break;
558         case 0:
559                 mask = 0;
560                 break;
561         default:
562                 return -EINVAL;
563         }
564
565         spin_lock_irq(&hpet_task_lock);
566         spin_lock(&hpet_lock);
567
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)
572                             != mask)
573                                 continue;
574
575                         devp = &hpetp->hp_dev[i];
576
577                         if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
578                                 devp = NULL;
579                                 continue;
580                         }
581
582                         tp->ht_opaque = devp;
583                         devp->hd_task = tp;
584                         break;
585                 }
586
587         spin_unlock(&hpet_lock);
588         spin_unlock_irq(&hpet_task_lock);
589
590         if (tp->ht_opaque)
591                 return 0;
592         else
593                 return -EBUSY;
594 }
595
596 static inline int hpet_tpcheck(struct hpet_task *tp)
597 {
598         struct hpet_dev *devp;
599         struct hpets *hpetp;
600
601         devp = tp->ht_opaque;
602
603         if (!devp)
604                 return -ENXIO;
605
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)
610                         return 0;
611
612         return -ENXIO;
613 }
614
615 int hpet_unregister(struct hpet_task *tp)
616 {
617         struct hpet_dev *devp;
618         struct hpet_timer __iomem *timer;
619         int err;
620
621         if ((err = hpet_tpcheck(tp)))
622                 return err;
623
624         spin_lock_irq(&hpet_task_lock);
625         spin_lock(&hpet_lock);
626
627         devp = tp->ht_opaque;
628         if (devp->hd_task != tp) {
629                 spin_unlock(&hpet_lock);
630                 spin_unlock_irq(&hpet_task_lock);
631                 return -ENXIO;
632         }
633
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);
641
642         return 0;
643 }
644
645 int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
646 {
647         struct hpet_dev *devp;
648         int err;
649
650         if ((err = hpet_tpcheck(tp)))
651                 return err;
652
653         spin_lock_irq(&hpet_lock);
654         devp = tp->ht_opaque;
655         if (devp->hd_task != tp) {
656                 spin_unlock_irq(&hpet_lock);
657                 return -ENXIO;
658         }
659         spin_unlock_irq(&hpet_lock);
660         return hpet_ioctl_common(devp, cmd, arg, 1);
661 }
662
663 #ifdef  CONFIG_TIME_INTERPOLATION
664
665 static struct time_interpolator hpet_interpolator = {
666         .source = TIME_SOURCE_MMIO64,
667         .shift = 10
668 };
669
670 #endif
671
672 static ctl_table hpet_table[] = {
673         {
674          .ctl_name = 1,
675          .procname = "max-user-freq",
676          .data = &hpet_max_freq,
677          .maxlen = sizeof(int),
678          .mode = 0644,
679          .proc_handler = &proc_dointvec,
680          },
681         {.ctl_name = 0}
682 };
683
684 static ctl_table hpet_root[] = {
685         {
686          .ctl_name = 1,
687          .procname = "hpet",
688          .maxlen = 0,
689          .mode = 0555,
690          .child = hpet_table,
691          },
692         {.ctl_name = 0}
693 };
694
695 static ctl_table dev_root[] = {
696         {
697          .ctl_name = CTL_DEV,
698          .procname = "dev",
699          .maxlen = 0,
700          .mode = 0555,
701          .child = hpet_root,
702          },
703         {.ctl_name = 0}
704 };
705
706 static struct ctl_table_header *sysctl_header;
707
708 /*
709  * Adjustment for when arming the timer with
710  * initial conditions.  That is, main counter
711  * ticks expired before interrupts are enabled.
712  */
713 #define TICK_CALIBRATE  (1000UL)
714
715 static unsigned long __init hpet_calibrate(struct hpets *hpetp)
716 {
717         struct hpet_timer __iomem *timer = NULL;
718         unsigned long t, m, count, i, flags, start;
719         struct hpet_dev *devp;
720         int j;
721         struct hpet __iomem *hpet;
722
723         for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
724                 if ((devp->hd_flags & HPET_OPEN) == 0) {
725                         timer = devp->hd_timer;
726                         break;
727                 }
728
729         if (!timer)
730                 return 0;
731
732         hpet = hpets->hp_hpet;
733         t = read_counter(&timer->hpet_compare);
734
735         i = 0;
736         count = hpet_time_div(hpetp->hp_period * TICK_CALIBRATE);
737
738         local_irq_save(flags);
739
740         start = read_counter(&hpet->hpet_mc);
741
742         do {
743                 m = read_counter(&hpet->hpet_mc);
744                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
745         } while (i++, (m - start) < count);
746
747         local_irq_restore(flags);
748
749         return (m - start) / i;
750 }
751
752 int __init hpet_alloc(struct hpet_data *hdp)
753 {
754         u64 cap, mcfg;
755         struct hpet_dev *devp;
756         u32 i, ntimer;
757         struct hpets *hpetp;
758         size_t siz;
759         struct hpet __iomem *hpet;
760         static struct hpets *last __initdata = (struct hpets *)0;
761         unsigned long ns;
762
763         /*
764          * hpet_alloc can be called by platform dependent code.
765          * if platform dependent code has allocated the hpet
766          * ACPI also reports hpet, then we catch it here.
767          */
768         for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
769                 if (hpetp->hp_hpet == hdp->hd_address)
770                         return 0;
771
772         siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
773                                       sizeof(struct hpet_dev));
774
775         hpetp = kmalloc(siz, GFP_KERNEL);
776
777         if (!hpetp)
778                 return -ENOMEM;
779
780         memset(hpetp, 0, siz);
781
782         hpetp->hp_which = hpet_nhpet++;
783         hpetp->hp_hpet = hdp->hd_address;
784
785         hpetp->hp_ntimer = hdp->hd_nirqs;
786
787         for (i = 0; i < hdp->hd_nirqs; i++)
788                 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
789
790         hpet = hpetp->hp_hpet;
791
792         cap = readq(&hpet->hpet_cap);
793
794         ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
795
796         if (hpetp->hp_ntimer != ntimer) {
797                 printk(KERN_WARNING "hpet: number irqs doesn't agree"
798                        " with number of timers\n");
799                 kfree(hpetp);
800                 return -ENODEV;
801         }
802
803         if (last)
804                 last->hp_next = hpetp;
805         else
806                 hpets = hpetp;
807
808         last = hpetp;
809
810         hpetp->hp_period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
811             HPET_COUNTER_CLK_PERIOD_SHIFT;
812
813         printk(KERN_INFO "hpet%d: at MMIO 0x%p, IRQ%s",
814                 hpetp->hp_which, hpet, hpetp->hp_ntimer > 1 ? "s" : "");
815         for (i = 0; i < hpetp->hp_ntimer; i++)
816                 printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
817         printk("\n");
818
819         ns = hpetp->hp_period;  /* femptoseconds, 10^-15 */
820         do_div(ns, 1000000);    /* convert to nanoseconds, 10^-9 */
821         printk(KERN_INFO "hpet%d: %ldns tick, %d %d-bit timers\n",
822                 hpetp->hp_which, ns, hpetp->hp_ntimer,
823                 cap & HPET_COUNTER_SIZE_MASK ? 64 : 32);
824
825         mcfg = readq(&hpet->hpet_config);
826         if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
827                 write_counter(0L, &hpet->hpet_mc);
828                 mcfg |= HPET_ENABLE_CNF_MASK;
829                 writeq(mcfg, &hpet->hpet_config);
830         }
831
832         for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer;
833              i++, hpet_ntimer++, devp++) {
834                 unsigned long v;
835                 struct hpet_timer __iomem *timer;
836
837                 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
838                 v = readq(&timer->hpet_config);
839
840                 devp->hd_hpets = hpetp;
841                 devp->hd_hpet = hpet;
842                 devp->hd_timer = timer;
843
844                 /*
845                  * If the timer was reserved by platform code,
846                  * then make timer unavailable for opens.
847                  */
848                 if (hdp->hd_state & (1 << i)) {
849                         devp->hd_flags = HPET_OPEN;
850                         continue;
851                 }
852
853                 init_waitqueue_head(&devp->hd_waitqueue);
854         }
855
856         hpetp->hp_delta = hpet_calibrate(hpetp);
857
858         return 0;
859 }
860
861 static acpi_status __init hpet_resources(struct acpi_resource *res, void *data)
862 {
863         struct hpet_data *hdp;
864         acpi_status status;
865         struct acpi_resource_address64 addr;
866         struct hpets *hpetp;
867
868         hdp = data;
869
870         status = acpi_resource_to_address64(res, &addr);
871
872         if (ACPI_SUCCESS(status)) {
873                 unsigned long size;
874
875                 size = addr.max_address_range - addr.min_address_range + 1;
876                 hdp->hd_address = ioremap(addr.min_address_range, size);
877
878                 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
879                         if (hpetp->hp_hpet == hdp->hd_address)
880                                 return -EBUSY;
881         } else if (res->id == ACPI_RSTYPE_EXT_IRQ) {
882                 struct acpi_resource_ext_irq *irqp;
883                 int i;
884
885                 irqp = &res->data.extended_irq;
886
887                 if (irqp->number_of_interrupts > 0) {
888                         hdp->hd_nirqs = irqp->number_of_interrupts;
889
890                         for (i = 0; i < hdp->hd_nirqs; i++)
891                                 hdp->hd_irq[i] =
892                                     acpi_register_gsi(irqp->interrupts[i],
893                                                       irqp->edge_level,
894                                                       irqp->active_high_low);
895                 }
896         }
897
898         return AE_OK;
899 }
900
901 static int __init hpet_acpi_add(struct acpi_device *device)
902 {
903         acpi_status result;
904         struct hpet_data data;
905
906         memset(&data, 0, sizeof(data));
907
908         result =
909             acpi_walk_resources(device->handle, METHOD_NAME__CRS,
910                                 hpet_resources, &data);
911
912         if (ACPI_FAILURE(result))
913                 return -ENODEV;
914
915         if (!data.hd_address || !data.hd_nirqs) {
916                 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
917                 return -ENODEV;
918         }
919
920         return hpet_alloc(&data);
921 }
922
923 static int __init hpet_acpi_remove(struct acpi_device *device, int type)
924 {
925         return 0;
926 }
927
928 static struct acpi_driver hpet_acpi_driver = {
929         .name = "hpet",
930         .ids = "PNP0103",
931         .ops = {
932                 .add = hpet_acpi_add,
933                 .remove = hpet_acpi_remove,
934                 },
935 };
936
937 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
938
939 static int __init hpet_init(void)
940 {
941         (void)acpi_bus_register_driver(&hpet_acpi_driver);
942
943         if (hpets) {
944                 if (misc_register(&hpet_misc))
945                         return -ENODEV;
946
947                 sysctl_header = register_sysctl_table(dev_root, 0);
948
949 #ifdef  CONFIG_TIME_INTERPOLATION
950                 {
951                         struct hpet *hpet;
952
953                         hpet = hpets->hp_hpet;
954                         hpet_interpolator.addr = &hpets->hp_hpet->hpet_mc;
955                         hpet_interpolator.frequency = hpet_time_div(hpets->hp_period);
956                         hpet_interpolator.drift = hpet_interpolator.frequency *
957                             HPET_DRIFT / 1000000;
958                         register_time_interpolator(&hpet_interpolator);
959                 }
960 #endif
961                 return 0;
962         } else
963                 return -ENODEV;
964 }
965
966 static void __exit hpet_exit(void)
967 {
968         acpi_bus_unregister_driver(&hpet_acpi_driver);
969
970         if (hpets)
971                 unregister_sysctl_table(sysctl_header);
972
973         return;
974 }
975
976 module_init(hpet_init);
977 module_exit(hpet_exit);
978 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
979 MODULE_LICENSE("GPL");