drbd: merge_bvec_fn: properly remap bvm->bi_bdev
[pandora-kernel.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/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/miscdevice.h>
19 #include <linux/major.h>
20 #include <linux/ioport.h>
21 #include <linux/fcntl.h>
22 #include <linux/init.h>
23 #include <linux/poll.h>
24 #include <linux/mm.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 #include <linux/bitops.h>
32 #include <linux/compat.h>
33 #include <linux/clocksource.h>
34 #include <linux/uaccess.h>
35 #include <linux/slab.h>
36 #include <linux/io.h>
37
38 #include <asm/current.h>
39 #include <asm/system.h>
40 #include <asm/irq.h>
41 #include <asm/div64.h>
42
43 #include <linux/acpi.h>
44 #include <acpi/acpi_bus.h>
45 #include <linux/hpet.h>
46
47 /*
48  * The High Precision Event Timer driver.
49  * This driver is closely modelled after the rtc.c driver.
50  * http://www.intel.com/hardwaredesign/hpetspec_1.pdf
51  */
52 #define HPET_USER_FREQ  (64)
53 #define HPET_DRIFT      (500)
54
55 #define HPET_RANGE_SIZE         1024    /* from HPET spec */
56
57
58 /* WARNING -- don't get confused.  These macros are never used
59  * to write the (single) counter, and rarely to read it.
60  * They're badly named; to fix, someday.
61  */
62 #if BITS_PER_LONG == 64
63 #define write_counter(V, MC)    writeq(V, MC)
64 #define read_counter(MC)        readq(MC)
65 #else
66 #define write_counter(V, MC)    writel(V, MC)
67 #define read_counter(MC)        readl(MC)
68 #endif
69
70 static DEFINE_MUTEX(hpet_mutex); /* replaces BKL */
71 static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
72
73 /* This clocksource driver currently only works on ia64 */
74 #ifdef CONFIG_IA64
75 static void __iomem *hpet_mctr;
76
77 static cycle_t read_hpet(struct clocksource *cs)
78 {
79         return (cycle_t)read_counter((void __iomem *)hpet_mctr);
80 }
81
82 static struct clocksource clocksource_hpet = {
83         .name           = "hpet",
84         .rating         = 250,
85         .read           = read_hpet,
86         .mask           = CLOCKSOURCE_MASK(64),
87         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
88 };
89 static struct clocksource *hpet_clocksource;
90 #endif
91
92 /* A lock for concurrent access by app and isr hpet activity. */
93 static DEFINE_SPINLOCK(hpet_lock);
94
95 #define HPET_DEV_NAME   (7)
96
97 struct hpet_dev {
98         struct hpets *hd_hpets;
99         struct hpet __iomem *hd_hpet;
100         struct hpet_timer __iomem *hd_timer;
101         unsigned long hd_ireqfreq;
102         unsigned long hd_irqdata;
103         wait_queue_head_t hd_waitqueue;
104         struct fasync_struct *hd_async_queue;
105         unsigned int hd_flags;
106         unsigned int hd_irq;
107         unsigned int hd_hdwirq;
108         char hd_name[HPET_DEV_NAME];
109 };
110
111 struct hpets {
112         struct hpets *hp_next;
113         struct hpet __iomem *hp_hpet;
114         unsigned long hp_hpet_phys;
115         struct clocksource *hp_clocksource;
116         unsigned long long hp_tick_freq;
117         unsigned long hp_delta;
118         unsigned int hp_ntimer;
119         unsigned int hp_which;
120         struct hpet_dev hp_dev[1];
121 };
122
123 static struct hpets *hpets;
124
125 #define HPET_OPEN               0x0001
126 #define HPET_IE                 0x0002  /* interrupt enabled */
127 #define HPET_PERIODIC           0x0004
128 #define HPET_SHARED_IRQ         0x0008
129
130
131 #ifndef readq
132 static inline unsigned long long readq(void __iomem *addr)
133 {
134         return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
135 }
136 #endif
137
138 #ifndef writeq
139 static inline void writeq(unsigned long long v, void __iomem *addr)
140 {
141         writel(v & 0xffffffff, addr);
142         writel(v >> 32, addr + 4);
143 }
144 #endif
145
146 static irqreturn_t hpet_interrupt(int irq, void *data)
147 {
148         struct hpet_dev *devp;
149         unsigned long isr;
150
151         devp = data;
152         isr = 1 << (devp - devp->hd_hpets->hp_dev);
153
154         if ((devp->hd_flags & HPET_SHARED_IRQ) &&
155             !(isr & readl(&devp->hd_hpet->hpet_isr)))
156                 return IRQ_NONE;
157
158         spin_lock(&hpet_lock);
159         devp->hd_irqdata++;
160
161         /*
162          * For non-periodic timers, increment the accumulator.
163          * This has the effect of treating non-periodic like periodic.
164          */
165         if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
166                 unsigned long m, t, mc, base, k;
167                 struct hpet __iomem *hpet = devp->hd_hpet;
168                 struct hpets *hpetp = devp->hd_hpets;
169
170                 t = devp->hd_ireqfreq;
171                 m = read_counter(&devp->hd_timer->hpet_compare);
172                 mc = read_counter(&hpet->hpet_mc);
173                 /* The time for the next interrupt would logically be t + m,
174                  * however, if we are very unlucky and the interrupt is delayed
175                  * for longer than t then we will completely miss the next
176                  * interrupt if we set t + m and an application will hang.
177                  * Therefore we need to make a more complex computation assuming
178                  * that there exists a k for which the following is true:
179                  * k * t + base < mc + delta
180                  * (k + 1) * t + base > mc + delta
181                  * where t is the interval in hpet ticks for the given freq,
182                  * base is the theoretical start value 0 < base < t,
183                  * mc is the main counter value at the time of the interrupt,
184                  * delta is the time it takes to write the a value to the
185                  * comparator.
186                  * k may then be computed as (mc - base + delta) / t .
187                  */
188                 base = mc % t;
189                 k = (mc - base + hpetp->hp_delta) / t;
190                 write_counter(t * (k + 1) + base,
191                               &devp->hd_timer->hpet_compare);
192         }
193
194         if (devp->hd_flags & HPET_SHARED_IRQ)
195                 writel(isr, &devp->hd_hpet->hpet_isr);
196         spin_unlock(&hpet_lock);
197
198         wake_up_interruptible(&devp->hd_waitqueue);
199
200         kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
201
202         return IRQ_HANDLED;
203 }
204
205 static void hpet_timer_set_irq(struct hpet_dev *devp)
206 {
207         unsigned long v;
208         int irq, gsi;
209         struct hpet_timer __iomem *timer;
210
211         spin_lock_irq(&hpet_lock);
212         if (devp->hd_hdwirq) {
213                 spin_unlock_irq(&hpet_lock);
214                 return;
215         }
216
217         timer = devp->hd_timer;
218
219         /* we prefer level triggered mode */
220         v = readl(&timer->hpet_config);
221         if (!(v & Tn_INT_TYPE_CNF_MASK)) {
222                 v |= Tn_INT_TYPE_CNF_MASK;
223                 writel(v, &timer->hpet_config);
224         }
225         spin_unlock_irq(&hpet_lock);
226
227         v = (readq(&timer->hpet_config) & Tn_INT_ROUTE_CAP_MASK) >>
228                                  Tn_INT_ROUTE_CAP_SHIFT;
229
230         /*
231          * In PIC mode, skip IRQ0-4, IRQ6-9, IRQ12-15 which is always used by
232          * legacy device. In IO APIC mode, we skip all the legacy IRQS.
233          */
234         if (acpi_irq_model == ACPI_IRQ_MODEL_PIC)
235                 v &= ~0xf3df;
236         else
237                 v &= ~0xffff;
238
239         for_each_set_bit(irq, &v, HPET_MAX_IRQ) {
240                 if (irq >= nr_irqs) {
241                         irq = HPET_MAX_IRQ;
242                         break;
243                 }
244
245                 gsi = acpi_register_gsi(NULL, irq, ACPI_LEVEL_SENSITIVE,
246                                         ACPI_ACTIVE_LOW);
247                 if (gsi > 0)
248                         break;
249
250                 /* FIXME: Setup interrupt source table */
251         }
252
253         if (irq < HPET_MAX_IRQ) {
254                 spin_lock_irq(&hpet_lock);
255                 v = readl(&timer->hpet_config);
256                 v |= irq << Tn_INT_ROUTE_CNF_SHIFT;
257                 writel(v, &timer->hpet_config);
258                 devp->hd_hdwirq = gsi;
259                 spin_unlock_irq(&hpet_lock);
260         }
261         return;
262 }
263
264 static int hpet_open(struct inode *inode, struct file *file)
265 {
266         struct hpet_dev *devp;
267         struct hpets *hpetp;
268         int i;
269
270         if (file->f_mode & FMODE_WRITE)
271                 return -EINVAL;
272
273         mutex_lock(&hpet_mutex);
274         spin_lock_irq(&hpet_lock);
275
276         for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
277                 for (i = 0; i < hpetp->hp_ntimer; i++)
278                         if (hpetp->hp_dev[i].hd_flags & HPET_OPEN)
279                                 continue;
280                         else {
281                                 devp = &hpetp->hp_dev[i];
282                                 break;
283                         }
284
285         if (!devp) {
286                 spin_unlock_irq(&hpet_lock);
287                 mutex_unlock(&hpet_mutex);
288                 return -EBUSY;
289         }
290
291         file->private_data = devp;
292         devp->hd_irqdata = 0;
293         devp->hd_flags |= HPET_OPEN;
294         spin_unlock_irq(&hpet_lock);
295         mutex_unlock(&hpet_mutex);
296
297         hpet_timer_set_irq(devp);
298
299         return 0;
300 }
301
302 static ssize_t
303 hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
304 {
305         DECLARE_WAITQUEUE(wait, current);
306         unsigned long data;
307         ssize_t retval;
308         struct hpet_dev *devp;
309
310         devp = file->private_data;
311         if (!devp->hd_ireqfreq)
312                 return -EIO;
313
314         if (count < sizeof(unsigned long))
315                 return -EINVAL;
316
317         add_wait_queue(&devp->hd_waitqueue, &wait);
318
319         for ( ; ; ) {
320                 set_current_state(TASK_INTERRUPTIBLE);
321
322                 spin_lock_irq(&hpet_lock);
323                 data = devp->hd_irqdata;
324                 devp->hd_irqdata = 0;
325                 spin_unlock_irq(&hpet_lock);
326
327                 if (data)
328                         break;
329                 else if (file->f_flags & O_NONBLOCK) {
330                         retval = -EAGAIN;
331                         goto out;
332                 } else if (signal_pending(current)) {
333                         retval = -ERESTARTSYS;
334                         goto out;
335                 }
336                 schedule();
337         }
338
339         retval = put_user(data, (unsigned long __user *)buf);
340         if (!retval)
341                 retval = sizeof(unsigned long);
342 out:
343         __set_current_state(TASK_RUNNING);
344         remove_wait_queue(&devp->hd_waitqueue, &wait);
345
346         return retval;
347 }
348
349 static unsigned int hpet_poll(struct file *file, poll_table * wait)
350 {
351         unsigned long v;
352         struct hpet_dev *devp;
353
354         devp = file->private_data;
355
356         if (!devp->hd_ireqfreq)
357                 return 0;
358
359         poll_wait(file, &devp->hd_waitqueue, wait);
360
361         spin_lock_irq(&hpet_lock);
362         v = devp->hd_irqdata;
363         spin_unlock_irq(&hpet_lock);
364
365         if (v != 0)
366                 return POLLIN | POLLRDNORM;
367
368         return 0;
369 }
370
371 static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
372 {
373 #ifdef  CONFIG_HPET_MMAP
374         struct hpet_dev *devp;
375         unsigned long addr;
376
377         devp = file->private_data;
378         addr = devp->hd_hpets->hp_hpet_phys;
379
380         if (addr & (PAGE_SIZE - 1))
381                 return -ENOSYS;
382
383         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
384         return vm_iomap_memory(vma, addr, PAGE_SIZE);
385 #else
386         return -ENOSYS;
387 #endif
388 }
389
390 static int hpet_fasync(int fd, struct file *file, int on)
391 {
392         struct hpet_dev *devp;
393
394         devp = file->private_data;
395
396         if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
397                 return 0;
398         else
399                 return -EIO;
400 }
401
402 static int hpet_release(struct inode *inode, struct file *file)
403 {
404         struct hpet_dev *devp;
405         struct hpet_timer __iomem *timer;
406         int irq = 0;
407
408         devp = file->private_data;
409         timer = devp->hd_timer;
410
411         spin_lock_irq(&hpet_lock);
412
413         writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
414                &timer->hpet_config);
415
416         irq = devp->hd_irq;
417         devp->hd_irq = 0;
418
419         devp->hd_ireqfreq = 0;
420
421         if (devp->hd_flags & HPET_PERIODIC
422             && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
423                 unsigned long v;
424
425                 v = readq(&timer->hpet_config);
426                 v ^= Tn_TYPE_CNF_MASK;
427                 writeq(v, &timer->hpet_config);
428         }
429
430         devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
431         spin_unlock_irq(&hpet_lock);
432
433         if (irq)
434                 free_irq(irq, devp);
435
436         file->private_data = NULL;
437         return 0;
438 }
439
440 static int hpet_ioctl_ieon(struct hpet_dev *devp)
441 {
442         struct hpet_timer __iomem *timer;
443         struct hpet __iomem *hpet;
444         struct hpets *hpetp;
445         int irq;
446         unsigned long g, v, t, m;
447         unsigned long flags, isr;
448
449         timer = devp->hd_timer;
450         hpet = devp->hd_hpet;
451         hpetp = devp->hd_hpets;
452
453         if (!devp->hd_ireqfreq)
454                 return -EIO;
455
456         spin_lock_irq(&hpet_lock);
457
458         if (devp->hd_flags & HPET_IE) {
459                 spin_unlock_irq(&hpet_lock);
460                 return -EBUSY;
461         }
462
463         devp->hd_flags |= HPET_IE;
464
465         if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK)
466                 devp->hd_flags |= HPET_SHARED_IRQ;
467         spin_unlock_irq(&hpet_lock);
468
469         irq = devp->hd_hdwirq;
470
471         if (irq) {
472                 unsigned long irq_flags;
473
474                 if (devp->hd_flags & HPET_SHARED_IRQ) {
475                         /*
476                          * To prevent the interrupt handler from seeing an
477                          * unwanted interrupt status bit, program the timer
478                          * so that it will not fire in the near future ...
479                          */
480                         writel(readl(&timer->hpet_config) & ~Tn_TYPE_CNF_MASK,
481                                &timer->hpet_config);
482                         write_counter(read_counter(&hpet->hpet_mc),
483                                       &timer->hpet_compare);
484                         /* ... and clear any left-over status. */
485                         isr = 1 << (devp - devp->hd_hpets->hp_dev);
486                         writel(isr, &hpet->hpet_isr);
487                 }
488
489                 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
490                 irq_flags = devp->hd_flags & HPET_SHARED_IRQ
491                                                 ? IRQF_SHARED : IRQF_DISABLED;
492                 if (request_irq(irq, hpet_interrupt, irq_flags,
493                                 devp->hd_name, (void *)devp)) {
494                         printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
495                         irq = 0;
496                 }
497         }
498
499         if (irq == 0) {
500                 spin_lock_irq(&hpet_lock);
501                 devp->hd_flags ^= HPET_IE;
502                 spin_unlock_irq(&hpet_lock);
503                 return -EIO;
504         }
505
506         devp->hd_irq = irq;
507         t = devp->hd_ireqfreq;
508         v = readq(&timer->hpet_config);
509
510         /* 64-bit comparators are not yet supported through the ioctls,
511          * so force this into 32-bit mode if it supports both modes
512          */
513         g = v | Tn_32MODE_CNF_MASK | Tn_INT_ENB_CNF_MASK;
514
515         if (devp->hd_flags & HPET_PERIODIC) {
516                 g |= Tn_TYPE_CNF_MASK;
517                 v |= Tn_TYPE_CNF_MASK | Tn_VAL_SET_CNF_MASK;
518                 writeq(v, &timer->hpet_config);
519                 local_irq_save(flags);
520
521                 /*
522                  * NOTE: First we modify the hidden accumulator
523                  * register supported by periodic-capable comparators.
524                  * We never want to modify the (single) counter; that
525                  * would affect all the comparators. The value written
526                  * is the counter value when the first interrupt is due.
527                  */
528                 m = read_counter(&hpet->hpet_mc);
529                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
530                 /*
531                  * Then we modify the comparator, indicating the period
532                  * for subsequent interrupt.
533                  */
534                 write_counter(t, &timer->hpet_compare);
535         } else {
536                 local_irq_save(flags);
537                 m = read_counter(&hpet->hpet_mc);
538                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
539         }
540
541         if (devp->hd_flags & HPET_SHARED_IRQ) {
542                 isr = 1 << (devp - devp->hd_hpets->hp_dev);
543                 writel(isr, &hpet->hpet_isr);
544         }
545         writeq(g, &timer->hpet_config);
546         local_irq_restore(flags);
547
548         return 0;
549 }
550
551 /* converts Hz to number of timer ticks */
552 static inline unsigned long hpet_time_div(struct hpets *hpets,
553                                           unsigned long dis)
554 {
555         unsigned long long m;
556
557         m = hpets->hp_tick_freq + (dis >> 1);
558         do_div(m, dis);
559         return (unsigned long)m;
560 }
561
562 static int
563 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg,
564                   struct hpet_info *info)
565 {
566         struct hpet_timer __iomem *timer;
567         struct hpet __iomem *hpet;
568         struct hpets *hpetp;
569         int err;
570         unsigned long v;
571
572         switch (cmd) {
573         case HPET_IE_OFF:
574         case HPET_INFO:
575         case HPET_EPI:
576         case HPET_DPI:
577         case HPET_IRQFREQ:
578                 timer = devp->hd_timer;
579                 hpet = devp->hd_hpet;
580                 hpetp = devp->hd_hpets;
581                 break;
582         case HPET_IE_ON:
583                 return hpet_ioctl_ieon(devp);
584         default:
585                 return -EINVAL;
586         }
587
588         err = 0;
589
590         switch (cmd) {
591         case HPET_IE_OFF:
592                 if ((devp->hd_flags & HPET_IE) == 0)
593                         break;
594                 v = readq(&timer->hpet_config);
595                 v &= ~Tn_INT_ENB_CNF_MASK;
596                 writeq(v, &timer->hpet_config);
597                 if (devp->hd_irq) {
598                         free_irq(devp->hd_irq, devp);
599                         devp->hd_irq = 0;
600                 }
601                 devp->hd_flags ^= HPET_IE;
602                 break;
603         case HPET_INFO:
604                 {
605                         memset(info, 0, sizeof(*info));
606                         if (devp->hd_ireqfreq)
607                                 info->hi_ireqfreq =
608                                         hpet_time_div(hpetp, devp->hd_ireqfreq);
609                         info->hi_flags =
610                             readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
611                         info->hi_hpet = hpetp->hp_which;
612                         info->hi_timer = devp - hpetp->hp_dev;
613                         break;
614                 }
615         case HPET_EPI:
616                 v = readq(&timer->hpet_config);
617                 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
618                         err = -ENXIO;
619                         break;
620                 }
621                 devp->hd_flags |= HPET_PERIODIC;
622                 break;
623         case HPET_DPI:
624                 v = readq(&timer->hpet_config);
625                 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
626                         err = -ENXIO;
627                         break;
628                 }
629                 if (devp->hd_flags & HPET_PERIODIC &&
630                     readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
631                         v = readq(&timer->hpet_config);
632                         v ^= Tn_TYPE_CNF_MASK;
633                         writeq(v, &timer->hpet_config);
634                 }
635                 devp->hd_flags &= ~HPET_PERIODIC;
636                 break;
637         case HPET_IRQFREQ:
638                 if ((arg > hpet_max_freq) &&
639                     !capable(CAP_SYS_RESOURCE)) {
640                         err = -EACCES;
641                         break;
642                 }
643
644                 if (!arg) {
645                         err = -EINVAL;
646                         break;
647                 }
648
649                 devp->hd_ireqfreq = hpet_time_div(hpetp, arg);
650         }
651
652         return err;
653 }
654
655 static long
656 hpet_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
657 {
658         struct hpet_info info;
659         int err;
660
661         mutex_lock(&hpet_mutex);
662         err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
663         mutex_unlock(&hpet_mutex);
664
665         if ((cmd == HPET_INFO) && !err &&
666             (copy_to_user((void __user *)arg, &info, sizeof(info))))
667                 err = -EFAULT;
668
669         return err;
670 }
671
672 #ifdef CONFIG_COMPAT
673 struct compat_hpet_info {
674         compat_ulong_t hi_ireqfreq;     /* Hz */
675         compat_ulong_t hi_flags;        /* information */
676         unsigned short hi_hpet;
677         unsigned short hi_timer;
678 };
679
680 static long
681 hpet_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
682 {
683         struct hpet_info info;
684         int err;
685
686         mutex_lock(&hpet_mutex);
687         err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
688         mutex_unlock(&hpet_mutex);
689
690         if ((cmd == HPET_INFO) && !err) {
691                 struct compat_hpet_info __user *u = compat_ptr(arg);
692                 if (put_user(info.hi_ireqfreq, &u->hi_ireqfreq) ||
693                     put_user(info.hi_flags, &u->hi_flags) ||
694                     put_user(info.hi_hpet, &u->hi_hpet) ||
695                     put_user(info.hi_timer, &u->hi_timer))
696                         err = -EFAULT;
697         }
698
699         return err;
700 }
701 #endif
702
703 static const struct file_operations hpet_fops = {
704         .owner = THIS_MODULE,
705         .llseek = no_llseek,
706         .read = hpet_read,
707         .poll = hpet_poll,
708         .unlocked_ioctl = hpet_ioctl,
709 #ifdef CONFIG_COMPAT
710         .compat_ioctl = hpet_compat_ioctl,
711 #endif
712         .open = hpet_open,
713         .release = hpet_release,
714         .fasync = hpet_fasync,
715         .mmap = hpet_mmap,
716 };
717
718 static int hpet_is_known(struct hpet_data *hdp)
719 {
720         struct hpets *hpetp;
721
722         for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
723                 if (hpetp->hp_hpet_phys == hdp->hd_phys_address)
724                         return 1;
725
726         return 0;
727 }
728
729 static ctl_table hpet_table[] = {
730         {
731          .procname = "max-user-freq",
732          .data = &hpet_max_freq,
733          .maxlen = sizeof(int),
734          .mode = 0644,
735          .proc_handler = proc_dointvec,
736          },
737         {}
738 };
739
740 static ctl_table hpet_root[] = {
741         {
742          .procname = "hpet",
743          .maxlen = 0,
744          .mode = 0555,
745          .child = hpet_table,
746          },
747         {}
748 };
749
750 static ctl_table dev_root[] = {
751         {
752          .procname = "dev",
753          .maxlen = 0,
754          .mode = 0555,
755          .child = hpet_root,
756          },
757         {}
758 };
759
760 static struct ctl_table_header *sysctl_header;
761
762 /*
763  * Adjustment for when arming the timer with
764  * initial conditions.  That is, main counter
765  * ticks expired before interrupts are enabled.
766  */
767 #define TICK_CALIBRATE  (1000UL)
768
769 static unsigned long __hpet_calibrate(struct hpets *hpetp)
770 {
771         struct hpet_timer __iomem *timer = NULL;
772         unsigned long t, m, count, i, flags, start;
773         struct hpet_dev *devp;
774         int j;
775         struct hpet __iomem *hpet;
776
777         for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
778                 if ((devp->hd_flags & HPET_OPEN) == 0) {
779                         timer = devp->hd_timer;
780                         break;
781                 }
782
783         if (!timer)
784                 return 0;
785
786         hpet = hpetp->hp_hpet;
787         t = read_counter(&timer->hpet_compare);
788
789         i = 0;
790         count = hpet_time_div(hpetp, TICK_CALIBRATE);
791
792         local_irq_save(flags);
793
794         start = read_counter(&hpet->hpet_mc);
795
796         do {
797                 m = read_counter(&hpet->hpet_mc);
798                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
799         } while (i++, (m - start) < count);
800
801         local_irq_restore(flags);
802
803         return (m - start) / i;
804 }
805
806 static unsigned long hpet_calibrate(struct hpets *hpetp)
807 {
808         unsigned long ret = -1;
809         unsigned long tmp;
810
811         /*
812          * Try to calibrate until return value becomes stable small value.
813          * If SMI interruption occurs in calibration loop, the return value
814          * will be big. This avoids its impact.
815          */
816         for ( ; ; ) {
817                 tmp = __hpet_calibrate(hpetp);
818                 if (ret <= tmp)
819                         break;
820                 ret = tmp;
821         }
822
823         return ret;
824 }
825
826 int hpet_alloc(struct hpet_data *hdp)
827 {
828         u64 cap, mcfg;
829         struct hpet_dev *devp;
830         u32 i, ntimer;
831         struct hpets *hpetp;
832         size_t siz;
833         struct hpet __iomem *hpet;
834         static struct hpets *last;
835         unsigned long period;
836         unsigned long long temp;
837         u32 remainder;
838
839         /*
840          * hpet_alloc can be called by platform dependent code.
841          * If platform dependent code has allocated the hpet that
842          * ACPI has also reported, then we catch it here.
843          */
844         if (hpet_is_known(hdp)) {
845                 printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
846                         __func__);
847                 return 0;
848         }
849
850         siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
851                                       sizeof(struct hpet_dev));
852
853         hpetp = kzalloc(siz, GFP_KERNEL);
854
855         if (!hpetp)
856                 return -ENOMEM;
857
858         hpetp->hp_which = hpet_nhpet++;
859         hpetp->hp_hpet = hdp->hd_address;
860         hpetp->hp_hpet_phys = hdp->hd_phys_address;
861
862         hpetp->hp_ntimer = hdp->hd_nirqs;
863
864         for (i = 0; i < hdp->hd_nirqs; i++)
865                 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
866
867         hpet = hpetp->hp_hpet;
868
869         cap = readq(&hpet->hpet_cap);
870
871         ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
872
873         if (hpetp->hp_ntimer != ntimer) {
874                 printk(KERN_WARNING "hpet: number irqs doesn't agree"
875                        " with number of timers\n");
876                 kfree(hpetp);
877                 return -ENODEV;
878         }
879
880         if (last)
881                 last->hp_next = hpetp;
882         else
883                 hpets = hpetp;
884
885         last = hpetp;
886
887         period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
888                 HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
889         temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
890         temp += period >> 1; /* round */
891         do_div(temp, period);
892         hpetp->hp_tick_freq = temp; /* ticks per second */
893
894         printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
895                 hpetp->hp_which, hdp->hd_phys_address,
896                 hpetp->hp_ntimer > 1 ? "s" : "");
897         for (i = 0; i < hpetp->hp_ntimer; i++)
898                 printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
899         printk("\n");
900
901         temp = hpetp->hp_tick_freq;
902         remainder = do_div(temp, 1000000);
903         printk(KERN_INFO
904                 "hpet%u: %u comparators, %d-bit %u.%06u MHz counter\n",
905                 hpetp->hp_which, hpetp->hp_ntimer,
906                 cap & HPET_COUNTER_SIZE_MASK ? 64 : 32,
907                 (unsigned) temp, remainder);
908
909         mcfg = readq(&hpet->hpet_config);
910         if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
911                 write_counter(0L, &hpet->hpet_mc);
912                 mcfg |= HPET_ENABLE_CNF_MASK;
913                 writeq(mcfg, &hpet->hpet_config);
914         }
915
916         for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
917                 struct hpet_timer __iomem *timer;
918
919                 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
920
921                 devp->hd_hpets = hpetp;
922                 devp->hd_hpet = hpet;
923                 devp->hd_timer = timer;
924
925                 /*
926                  * If the timer was reserved by platform code,
927                  * then make timer unavailable for opens.
928                  */
929                 if (hdp->hd_state & (1 << i)) {
930                         devp->hd_flags = HPET_OPEN;
931                         continue;
932                 }
933
934                 init_waitqueue_head(&devp->hd_waitqueue);
935         }
936
937         hpetp->hp_delta = hpet_calibrate(hpetp);
938
939 /* This clocksource driver currently only works on ia64 */
940 #ifdef CONFIG_IA64
941         if (!hpet_clocksource) {
942                 hpet_mctr = (void __iomem *)&hpetp->hp_hpet->hpet_mc;
943                 clocksource_hpet.archdata.fsys_mmio = hpet_mctr;
944                 clocksource_register_hz(&clocksource_hpet, hpetp->hp_tick_freq);
945                 hpetp->hp_clocksource = &clocksource_hpet;
946                 hpet_clocksource = &clocksource_hpet;
947         }
948 #endif
949
950         return 0;
951 }
952
953 static acpi_status hpet_resources(struct acpi_resource *res, void *data)
954 {
955         struct hpet_data *hdp;
956         acpi_status status;
957         struct acpi_resource_address64 addr;
958
959         hdp = data;
960
961         status = acpi_resource_to_address64(res, &addr);
962
963         if (ACPI_SUCCESS(status)) {
964                 hdp->hd_phys_address = addr.minimum;
965                 hdp->hd_address = ioremap(addr.minimum, addr.address_length);
966
967                 if (hpet_is_known(hdp)) {
968                         iounmap(hdp->hd_address);
969                         return AE_ALREADY_EXISTS;
970                 }
971         } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
972                 struct acpi_resource_fixed_memory32 *fixmem32;
973
974                 fixmem32 = &res->data.fixed_memory32;
975                 if (!fixmem32)
976                         return AE_NO_MEMORY;
977
978                 hdp->hd_phys_address = fixmem32->address;
979                 hdp->hd_address = ioremap(fixmem32->address,
980                                                 HPET_RANGE_SIZE);
981
982                 if (hpet_is_known(hdp)) {
983                         iounmap(hdp->hd_address);
984                         return AE_ALREADY_EXISTS;
985                 }
986         } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
987                 struct acpi_resource_extended_irq *irqp;
988                 int i, irq;
989
990                 irqp = &res->data.extended_irq;
991
992                 for (i = 0; i < irqp->interrupt_count; i++) {
993                         irq = acpi_register_gsi(NULL, irqp->interrupts[i],
994                                       irqp->triggering, irqp->polarity);
995                         if (irq < 0)
996                                 return AE_ERROR;
997
998                         hdp->hd_irq[hdp->hd_nirqs] = irq;
999                         hdp->hd_nirqs++;
1000                 }
1001         }
1002
1003         return AE_OK;
1004 }
1005
1006 static int hpet_acpi_add(struct acpi_device *device)
1007 {
1008         acpi_status result;
1009         struct hpet_data data;
1010
1011         memset(&data, 0, sizeof(data));
1012
1013         result =
1014             acpi_walk_resources(device->handle, METHOD_NAME__CRS,
1015                                 hpet_resources, &data);
1016
1017         if (ACPI_FAILURE(result))
1018                 return -ENODEV;
1019
1020         if (!data.hd_address || !data.hd_nirqs) {
1021                 if (data.hd_address)
1022                         iounmap(data.hd_address);
1023                 printk("%s: no address or irqs in _CRS\n", __func__);
1024                 return -ENODEV;
1025         }
1026
1027         return hpet_alloc(&data);
1028 }
1029
1030 static int hpet_acpi_remove(struct acpi_device *device, int type)
1031 {
1032         /* XXX need to unregister clocksource, dealloc mem, etc */
1033         return -EINVAL;
1034 }
1035
1036 static const struct acpi_device_id hpet_device_ids[] = {
1037         {"PNP0103", 0},
1038         {"", 0},
1039 };
1040 MODULE_DEVICE_TABLE(acpi, hpet_device_ids);
1041
1042 static struct acpi_driver hpet_acpi_driver = {
1043         .name = "hpet",
1044         .ids = hpet_device_ids,
1045         .ops = {
1046                 .add = hpet_acpi_add,
1047                 .remove = hpet_acpi_remove,
1048                 },
1049 };
1050
1051 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
1052
1053 static int __init hpet_init(void)
1054 {
1055         int result;
1056
1057         result = misc_register(&hpet_misc);
1058         if (result < 0)
1059                 return -ENODEV;
1060
1061         sysctl_header = register_sysctl_table(dev_root);
1062
1063         result = acpi_bus_register_driver(&hpet_acpi_driver);
1064         if (result < 0) {
1065                 if (sysctl_header)
1066                         unregister_sysctl_table(sysctl_header);
1067                 misc_deregister(&hpet_misc);
1068                 return result;
1069         }
1070
1071         return 0;
1072 }
1073
1074 static void __exit hpet_exit(void)
1075 {
1076         acpi_bus_unregister_driver(&hpet_acpi_driver);
1077
1078         if (sysctl_header)
1079                 unregister_sysctl_table(sysctl_header);
1080         misc_deregister(&hpet_misc);
1081
1082         return;
1083 }
1084
1085 module_init(hpet_init);
1086 module_exit(hpet_exit);
1087 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
1088 MODULE_LICENSE("GPL");