This commit was generated by cvs2svn to compensate for changes in r517,
[linux-2.6.git] / drivers / char / mmtimer.c
1 /*
2  * Intel Multimedia Timer device implementation for SGI SN platforms.
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (c) 2001-2004 Silicon Graphics, Inc.  All rights reserved.
9  *
10  * This driver exports an API that should be supportable by any HPET or IA-PC
11  * multimedia timer.  The code below is currently specific to the SGI Altix
12  * SHub RTC, however.
13  *
14  * 11/01/01 - jbarnes - initial revision
15  * 9/10/04 - Christoph Lameter - remove interrupt support for kernel inclusion
16  * 10/1/04 - Christoph Lameter - provide posix clock CLOCK_SGI_CYCLE
17  * 10/13/04 - Christoph Lameter, Dimitri Sivanich - provide timer interrupt
18  *              support via the posix timer interface
19  */
20
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/ioctl.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/errno.h>
27 #include <linux/mm.h>
28 #include <linux/devfs_fs_kernel.h>
29 #include <linux/mmtimer.h>
30 #include <linux/miscdevice.h>
31 #include <linux/posix-timers.h>
32 #include <linux/interrupt.h>
33
34 #include <asm/uaccess.h>
35 #include <asm/sn/addrs.h>
36 #include <asm/sn/intr.h>
37 #include <asm/sn/shub_mmr.h>
38 #include <asm/sn/nodepda.h>
39
40 /* This is ugly and jbarnes has promised me to fix this later */
41 #include "../../arch/ia64/sn/include/shubio.h"
42
43 MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
44 MODULE_DESCRIPTION("SGI Altix RTC Timer");
45 MODULE_LICENSE("GPL");
46
47 /* name of the device, usually in /dev */
48 #define MMTIMER_NAME "mmtimer"
49 #define MMTIMER_DESC "SGI Altix RTC Timer"
50 #define MMTIMER_VERSION "2.0"
51
52 #define RTC_BITS 55 /* 55 bits for this implementation */
53
54 extern unsigned long sn_rtc_cycles_per_second;
55
56 #define RTC_COUNTER_ADDR        ((long *)LOCAL_MMR_ADDR(SH_RTC))
57
58 #define rtc_time()              (*RTC_COUNTER_ADDR)
59
60 static int mmtimer_ioctl(struct inode *inode, struct file *file,
61                          unsigned int cmd, unsigned long arg);
62 static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma);
63
64 /*
65  * Period in femtoseconds (10^-15 s)
66  */
67 static unsigned long mmtimer_femtoperiod = 0;
68
69 static struct file_operations mmtimer_fops = {
70         .owner =        THIS_MODULE,
71         .mmap =         mmtimer_mmap,
72         .ioctl =        mmtimer_ioctl,
73 };
74
75 /*
76  * Comparators and their associated info.  Shub has
77  * three comparison registers.
78  */
79
80 /*
81  * We only have comparison registers RTC1-4 currently available per
82  * node.  RTC0 is used by SAL.
83  */
84 #define NUM_COMPARATORS 3
85 /* Check for an RTC interrupt pending */
86 static int inline mmtimer_int_pending(int comparator)
87 {
88         if (HUB_L((unsigned long *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED)) &
89                         SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator)
90                 return 1;
91         else
92                 return 0;
93 }
94 /* Clear the RTC interrupt pending bit */
95 static void inline mmtimer_clr_int_pending(int comparator)
96 {
97         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED_ALIAS),
98                 SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator);
99 }
100
101 /* Setup timer on comparator RTC1 */
102 static void inline mmtimer_setup_int_0(u64 expires)
103 {
104         u64 val;
105
106         /* Disable interrupt */
107         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 0UL);
108
109         /* Initialize comparator value */
110         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), -1L);
111
112         /* Clear pending bit */
113         mmtimer_clr_int_pending(0);
114
115         val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC1_INT_CONFIG_IDX_SHFT) |
116                 ((u64)cpu_physical_id(smp_processor_id()) <<
117                         SH_RTC1_INT_CONFIG_PID_SHFT);
118
119         /* Set configuration */
120         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_CONFIG), val);
121
122         /* Enable RTC interrupts */
123         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 1UL);
124
125         /* Initialize comparator value */
126         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), expires);
127
128
129 }
130
131 /* Setup timer on comparator RTC2 */
132 static void inline mmtimer_setup_int_1(u64 expires)
133 {
134         u64 val;
135
136         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 0UL);
137
138         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), -1L);
139
140         mmtimer_clr_int_pending(1);
141
142         val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC2_INT_CONFIG_IDX_SHFT) |
143                 ((u64)cpu_physical_id(smp_processor_id()) <<
144                         SH_RTC2_INT_CONFIG_PID_SHFT);
145
146         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_CONFIG), val);
147
148         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 1UL);
149
150         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), expires);
151 }
152
153 /* Setup timer on comparator RTC3 */
154 static void inline mmtimer_setup_int_2(u64 expires)
155 {
156         u64 val;
157
158         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 0UL);
159
160         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), -1L);
161
162         mmtimer_clr_int_pending(2);
163
164         val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC3_INT_CONFIG_IDX_SHFT) |
165                 ((u64)cpu_physical_id(smp_processor_id()) <<
166                         SH_RTC3_INT_CONFIG_PID_SHFT);
167
168         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_CONFIG), val);
169
170         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 1UL);
171
172         HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), expires);
173 }
174
175 /*
176  * This function must be called with interrupts disabled and preemption off
177  * in order to insure that the setup succeeds in a deterministic time frame.
178  * It will check if the interrupt setup succeeded.
179  * mmtimer_setup will return the cycles that we were too late if the
180  * initialization failed.
181  */
182 static int inline mmtimer_setup(int comparator, unsigned long expires)
183 {
184
185         long diff;
186
187         switch (comparator) {
188         case 0:
189                 mmtimer_setup_int_0(expires);
190                 break;
191         case 1:
192                 mmtimer_setup_int_1(expires);
193                 break;
194         case 2:
195                 mmtimer_setup_int_2(expires);
196                 break;
197         }
198         /* We might've missed our expiration time */
199         diff = rtc_time() - expires;
200         if (diff > 0) {
201                 if (mmtimer_int_pending(comparator)) {
202                         /* We'll get an interrupt for this once we're done */
203                         return 0;
204                 }
205                 /* Looks like we missed it */
206                 return diff;
207         }
208
209         return 0;
210 }
211
212 static int inline mmtimer_disable_int(long nasid, int comparator)
213 {
214         switch (comparator) {
215         case 0:
216                 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE),
217                         0UL) : REMOTE_HUB_S(nasid, SH_RTC1_INT_ENABLE, 0UL);
218                 break;
219         case 1:
220                 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE),
221                         0UL) : REMOTE_HUB_S(nasid, SH_RTC2_INT_ENABLE, 0UL);
222                 break;
223         case 2:
224                 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE),
225                         0UL) : REMOTE_HUB_S(nasid, SH_RTC3_INT_ENABLE, 0UL);
226                 break;
227         default:
228                 return -EFAULT;
229         }
230         return 0;
231 }
232
233 #define TIMER_OFF 0xbadcabLL
234
235 /* There is one of these for each comparator */
236 typedef struct mmtimer {
237         spinlock_t lock ____cacheline_aligned;
238         struct k_itimer *timer;
239         int i;
240         int cpu;
241         struct tasklet_struct tasklet;
242 } mmtimer_t;
243
244 /*
245  * Total number of comparators is comparators/node * MAX nodes/running kernel
246  */
247 static mmtimer_t timers[NUM_COMPARATORS*MAX_COMPACT_NODES];
248
249 /**
250  * mmtimer_ioctl - ioctl interface for /dev/mmtimer
251  * @inode: inode of the device
252  * @file: file structure for the device
253  * @cmd: command to execute
254  * @arg: optional argument to command
255  *
256  * Executes the command specified by @cmd.  Returns 0 for success, < 0 for
257  * failure.
258  *
259  * Valid commands:
260  *
261  * %MMTIMER_GETOFFSET - Should return the offset (relative to the start
262  * of the page where the registers are mapped) for the counter in question.
263  *
264  * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15)
265  * seconds
266  *
267  * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address
268  * specified by @arg
269  *
270  * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter
271  *
272  * %MMTIMER_MMAPAVAIL - Returns 1 if the registers can be mmap'd into userspace
273  *
274  * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it
275  * in the address specified by @arg.
276  */
277 static int mmtimer_ioctl(struct inode *inode, struct file *file,
278                          unsigned int cmd, unsigned long arg)
279 {
280         int ret = 0;
281
282         switch (cmd) {
283         case MMTIMER_GETOFFSET: /* offset of the counter */
284                 /*
285                  * SN RTC registers are on their own 64k page
286                  */
287                 if(PAGE_SIZE <= (1 << 16))
288                         ret = (((long)RTC_COUNTER_ADDR) & (PAGE_SIZE-1)) / 8;
289                 else
290                         ret = -ENOSYS;
291                 break;
292
293         case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */
294                 if(copy_to_user((unsigned long __user *)arg,
295                                 &mmtimer_femtoperiod, sizeof(unsigned long)))
296                         return -EFAULT;
297                 break;
298
299         case MMTIMER_GETFREQ: /* frequency in Hz */
300                 if(copy_to_user((unsigned long __user *)arg,
301                                 &sn_rtc_cycles_per_second,
302                                 sizeof(unsigned long)))
303                         return -EFAULT;
304                 ret = 0;
305                 break;
306
307         case MMTIMER_GETBITS: /* number of bits in the clock */
308                 ret = RTC_BITS;
309                 break;
310
311         case MMTIMER_MMAPAVAIL: /* can we mmap the clock into userspace? */
312                 ret = (PAGE_SIZE <= (1 << 16)) ? 1 : 0;
313                 break;
314
315         case MMTIMER_GETCOUNTER:
316                 if(copy_to_user((unsigned long __user *)arg,
317                                 RTC_COUNTER_ADDR, sizeof(unsigned long)))
318                         return -EFAULT;
319                 break;
320         default:
321                 ret = -ENOSYS;
322                 break;
323         }
324
325         return ret;
326 }
327
328 /**
329  * mmtimer_mmap - maps the clock's registers into userspace
330  * @file: file structure for the device
331  * @vma: VMA to map the registers into
332  *
333  * Calls remap_pfn_range() to map the clock's registers into
334  * the calling process' address space.
335  */
336 static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma)
337 {
338         unsigned long mmtimer_addr;
339
340         if (vma->vm_end - vma->vm_start != PAGE_SIZE)
341                 return -EINVAL;
342
343         if (vma->vm_flags & VM_WRITE)
344                 return -EPERM;
345
346         if (PAGE_SIZE > (1 << 16))
347                 return -ENOSYS;
348
349         vma->vm_flags |= (VM_IO | VM_SHM | VM_LOCKED );
350         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
351
352         mmtimer_addr = __pa(RTC_COUNTER_ADDR);
353         mmtimer_addr &= ~(PAGE_SIZE - 1);
354         mmtimer_addr &= 0xfffffffffffffffUL;
355
356         if (remap_pfn_range(vma, vma->vm_start, mmtimer_addr >> PAGE_SHIFT,
357                                         PAGE_SIZE, vma->vm_page_prot)) {
358                 printk(KERN_ERR "remap_pfn_range failed in mmtimer.c\n");
359                 return -EAGAIN;
360         }
361
362         return 0;
363 }
364
365 static struct miscdevice mmtimer_miscdev = {
366         SGI_MMTIMER,
367         MMTIMER_NAME,
368         &mmtimer_fops
369 };
370
371 static struct timespec sgi_clock_offset;
372 static int sgi_clock_period;
373
374 /*
375  * Posix Timer Interface
376  */
377
378 static struct timespec sgi_clock_offset;
379 static int sgi_clock_period;
380
381 static int sgi_clock_get(struct timespec *tp)
382 {
383         u64 nsec;
384
385         nsec = rtc_time() * sgi_clock_period
386                         + sgi_clock_offset.tv_nsec;
387         tp->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &tp->tv_nsec)
388                         + sgi_clock_offset.tv_sec;
389         return 0;
390 };
391
392 static int sgi_clock_set(struct timespec *tp)
393 {
394
395         u64 nsec;
396         u64 rem;
397
398         nsec = rtc_time() * sgi_clock_period;
399
400         sgi_clock_offset.tv_sec = tp->tv_sec - div_long_long_rem(nsec, NSEC_PER_SEC, &rem);
401
402         if (rem <= tp->tv_nsec)
403                 sgi_clock_offset.tv_nsec = tp->tv_sec - rem;
404         else {
405                 sgi_clock_offset.tv_nsec = tp->tv_sec + NSEC_PER_SEC - rem;
406                 sgi_clock_offset.tv_sec--;
407         }
408         return 0;
409 }
410
411 /*
412  * Schedule the next periodic interrupt. This function will attempt
413  * to schedule a periodic interrupt later if necessary. If the scheduling
414  * of an interrupt fails then the time to skip is lengthened
415  * exponentially in order to ensure that the next interrupt
416  * can be properly scheduled..
417  */
418 static int inline reschedule_periodic_timer(mmtimer_t *x)
419 {
420         int n;
421         struct k_itimer *t = x->timer;
422
423         t->it_timer.magic = x->i;
424         t->it_overrun--;
425
426         n = 0;
427         do {
428
429                 t->it_timer.expires += t->it_incr << n;
430                 t->it_overrun += 1 << n;
431                 n++;
432                 if (n > 20)
433                         return 1;
434
435         } while (mmtimer_setup(x->i, t->it_timer.expires));
436
437         return 0;
438 }
439
440 /**
441  * mmtimer_interrupt - timer interrupt handler
442  * @irq: irq received
443  * @dev_id: device the irq came from
444  * @regs: register state upon receipt of the interrupt
445  *
446  * Called when one of the comarators matches the counter, This
447  * routine will send signals to processes that have requested
448  * them.
449  *
450  * This interrupt is run in an interrupt context
451  * by the SHUB. It is therefore safe to locally access SHub
452  * registers.
453  */
454 static irqreturn_t
455 mmtimer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
456 {
457         int i;
458         mmtimer_t *base = timers + cpuid_to_cnodeid(smp_processor_id()) *
459                                                 NUM_COMPARATORS;
460         unsigned long expires = 0;
461         int result = IRQ_NONE;
462
463         /*
464          * Do this once for each comparison register
465          */
466         for (i = 0; i < NUM_COMPARATORS; i++) {
467                 /* Make sure this doesn't get reused before tasklet_sched */
468                 spin_lock(&base[i].lock);
469                 if (base[i].cpu == smp_processor_id()) {
470                         if (base[i].timer)
471                                 expires = base[i].timer->it_timer.expires;
472                         /* expires test won't work with shared irqs */
473                         if ((mmtimer_int_pending(i) > 0) ||
474                                 (expires && (expires < rtc_time()))) {
475                                 mmtimer_clr_int_pending(i);
476                                 tasklet_schedule(&base[i].tasklet);
477                                 result = IRQ_HANDLED;
478                         }
479                 }
480                 spin_unlock(&base[i].lock);
481                 expires = 0;
482         }
483         return result;
484 }
485
486 void mmtimer_tasklet(unsigned long data) {
487         mmtimer_t *x = (mmtimer_t *)data;
488         struct k_itimer *t = x->timer;
489         unsigned long flags;
490
491         if (t == NULL)
492                 return;
493
494         /* Send signal and deal with periodic signals */
495         spin_lock_irqsave(&t->it_lock, flags);
496         spin_lock(&x->lock);
497         /* If timer was deleted between interrupt and here, leave */
498         if (t != x->timer)
499                 goto out;
500         t->it_overrun = 0;
501
502         if (tasklist_lock.write_lock || posix_timer_event(t, 0) != 0) {
503
504                 // printk(KERN_WARNING "mmtimer: cannot deliver signal.\n");
505
506                 t->it_overrun++;
507         }
508         if(t->it_incr) {
509                 /* Periodic timer */
510                 if (reschedule_periodic_timer(x)) {
511                         printk(KERN_WARNING "mmtimer: unable to reschedule\n");
512                         x->timer = NULL;
513                 }
514         } else {
515                 /* Ensure we don't false trigger in mmtimer_interrupt */
516                 t->it_timer.expires = 0;
517         }
518         t->it_overrun_last = t->it_overrun;
519 out:
520         spin_unlock(&x->lock);
521         spin_unlock_irqrestore(&t->it_lock, flags);
522 }
523
524 static int sgi_timer_create(struct k_itimer *timer)
525 {
526         /* Insure that a newly created timer is off */
527         timer->it_timer.magic = TIMER_OFF;
528         return 0;
529 }
530
531 /* This does not really delete a timer. It just insures
532  * that the timer is not active
533  *
534  * Assumption: it_lock is already held with irq's disabled
535  */
536 static int sgi_timer_del(struct k_itimer *timr)
537 {
538         int i = timr->it_timer.magic;
539         cnodeid_t nodeid = timr->it_timer.data;
540         mmtimer_t *t = timers + nodeid * NUM_COMPARATORS +i;
541         unsigned long irqflags;
542
543         if (i != TIMER_OFF) {
544                 spin_lock_irqsave(&t->lock, irqflags);
545                 mmtimer_disable_int(cnodeid_to_nasid(nodeid),i);
546                 t->timer = NULL;
547                 timr->it_timer.magic = TIMER_OFF;
548                 timr->it_timer.expires = 0;
549                 spin_unlock_irqrestore(&t->lock, irqflags);
550         }
551         return 0;
552 }
553
554 #define timespec_to_ns(x) ((x).tv_nsec + (x).tv_sec * NSEC_PER_SEC)
555 #define ns_to_timespec(ts, nsec) (ts).tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &(ts).tv_nsec)
556
557 /* Assumption: it_lock is already held with irq's disabled */
558 static void sgi_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
559 {
560
561         if (timr->it_timer.magic == TIMER_OFF) {
562                 cur_setting->it_interval.tv_nsec = 0;
563                 cur_setting->it_interval.tv_sec = 0;
564                 cur_setting->it_value.tv_nsec = 0;
565                 cur_setting->it_value.tv_sec =0;
566                 return;
567         }
568
569         ns_to_timespec(cur_setting->it_interval, timr->it_incr * sgi_clock_period);
570         ns_to_timespec(cur_setting->it_value, (timr->it_timer.expires - rtc_time())* sgi_clock_period);
571         return;
572 }
573
574
575 static int sgi_timer_set(struct k_itimer *timr, int flags,
576         struct itimerspec * new_setting,
577         struct itimerspec * old_setting)
578 {
579
580         int i;
581         unsigned long when, period, irqflags;
582         int err = 0;
583         cnodeid_t nodeid;
584         mmtimer_t *base;
585
586         if (old_setting)
587                 sgi_timer_get(timr, old_setting);
588
589         sgi_timer_del(timr);
590         when = timespec_to_ns(new_setting->it_value);
591         period = timespec_to_ns(new_setting->it_interval);
592
593         if (when == 0)
594                 /* Clear timer */
595                 return 0;
596
597         if (flags & TIMER_ABSTIME) {
598                 struct timespec n;
599
600                 getnstimeofday(&n);
601                 when -= timespec_to_ns(n);
602         }
603
604         /*
605          * Convert to sgi clock period. Need to keep rtc_time() as near as possible
606          * to getnstimeofday() in order to be as faithful as possible to the time
607          * specified.
608          */
609         when = (when + sgi_clock_period - 1) / sgi_clock_period + rtc_time();
610         period = (period + sgi_clock_period - 1)  / sgi_clock_period;
611
612         /*
613          * We are allocating a local SHub comparator. If we would be moved to another
614          * cpu then another SHub may be local to us. Prohibit that by switching off
615          * preemption.
616          */
617         preempt_disable();
618
619         nodeid =  cpuid_to_cnodeid(smp_processor_id());
620         base = timers + nodeid * NUM_COMPARATORS;
621 retry:
622         /* Don't use an allocated timer, or a deleted one that's pending */
623         for(i = 0; i< NUM_COMPARATORS; i++) {
624                 if (!base[i].timer && !base[i].tasklet.state) {
625                         break;
626                 }
627         }
628
629         if (i == NUM_COMPARATORS) {
630                 preempt_enable();
631                 return -EBUSY;
632         }
633
634         spin_lock_irqsave(&base[i].lock, irqflags);
635
636         if (base[i].timer || base[i].tasklet.state != 0) {
637                 spin_unlock_irqrestore(&base[i].lock, irqflags);
638                 goto retry;
639         }
640         base[i].timer = timr;
641         base[i].cpu = smp_processor_id();
642
643         timr->it_timer.magic = i;
644         timr->it_timer.data = nodeid;
645         timr->it_incr = period;
646         timr->it_timer.expires = when;
647
648         if (period == 0) {
649                 if (mmtimer_setup(i, when)) {
650                         mmtimer_disable_int(-1, i);
651                         posix_timer_event(timr, 0);
652                         timr->it_timer.expires = 0;
653                 }
654         } else {
655                 timr->it_timer.expires -= period;
656                 if (reschedule_periodic_timer(base+i))
657                         err = -EINVAL;
658         }
659
660         spin_unlock_irqrestore(&base[i].lock, irqflags);
661
662         preempt_enable();
663
664         return err;
665 }
666
667 static struct k_clock sgi_clock = {
668         .res = 0,
669         .clock_set = sgi_clock_set,
670         .clock_get = sgi_clock_get,
671         .timer_create = sgi_timer_create,
672         .nsleep = do_posix_clock_nonanosleep,
673         .timer_set = sgi_timer_set,
674         .timer_del = sgi_timer_del,
675         .timer_get = sgi_timer_get
676 };
677
678 /**
679  * mmtimer_init - device initialization routine
680  *
681  * Does initial setup for the mmtimer device.
682  */
683 static int __init mmtimer_init(void)
684 {
685         unsigned i;
686
687         if (!ia64_platform_is("sn2"))
688                 return -1;
689
690         /*
691          * Sanity check the cycles/sec variable
692          */
693         if (sn_rtc_cycles_per_second < 100000) {
694                 printk(KERN_ERR "%s: unable to determine clock frequency\n",
695                        MMTIMER_NAME);
696                 return -1;
697         }
698
699         mmtimer_femtoperiod = ((unsigned long)1E15 + sn_rtc_cycles_per_second /
700                                2) / sn_rtc_cycles_per_second;
701
702         for (i=0; i< NUM_COMPARATORS*MAX_COMPACT_NODES; i++) {
703                 spin_lock_init(&timers[i].lock);
704                 timers[i].timer = NULL;
705                 timers[i].cpu = 0;
706                 timers[i].i = i % NUM_COMPARATORS;
707                 tasklet_init(&timers[i].tasklet, mmtimer_tasklet, (unsigned long) (timers+i));
708         }
709
710         if (request_irq(SGI_MMTIMER_VECTOR, mmtimer_interrupt, SA_PERCPU_IRQ, MMTIMER_NAME, NULL)) {
711                 printk(KERN_WARNING "%s: unable to allocate interrupt.",
712                         MMTIMER_NAME);
713                 return -1;
714         }
715
716         strcpy(mmtimer_miscdev.devfs_name, MMTIMER_NAME);
717         if (misc_register(&mmtimer_miscdev)) {
718                 printk(KERN_ERR "%s: failed to register device\n",
719                        MMTIMER_NAME);
720                 return -1;
721         }
722
723         sgi_clock_period = sgi_clock.res = NSEC_PER_SEC / sn_rtc_cycles_per_second;
724         register_posix_clock(CLOCK_SGI_CYCLE, &sgi_clock);
725
726         printk(KERN_INFO "%s: v%s, %ld MHz\n", MMTIMER_DESC, MMTIMER_VERSION,
727                sn_rtc_cycles_per_second/(unsigned long)1E6);
728
729         return 0;
730 }
731
732 module_init(mmtimer_init);
733