2 * Intel Multimedia Timer device implementation for SGI SN platforms.
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
8 * Copyright (c) 2001-2004 Silicon Graphics, Inc. All rights reserved.
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
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
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>
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>
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>
40 /* This is ugly and jbarnes has promised me to fix this later */
41 #include "../../arch/ia64/sn/include/shubio.h"
43 MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
44 MODULE_DESCRIPTION("SGI Altix RTC Timer");
45 MODULE_LICENSE("GPL");
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"
52 #define RTC_BITS 55 /* 55 bits for this implementation */
54 extern unsigned long sn_rtc_cycles_per_second;
56 #define RTC_COUNTER_ADDR ((long *)LOCAL_MMR_ADDR(SH_RTC))
58 #define rtc_time() (*RTC_COUNTER_ADDR)
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);
65 * Period in femtoseconds (10^-15 s)
67 static unsigned long mmtimer_femtoperiod = 0;
69 static struct file_operations mmtimer_fops = {
72 .ioctl = mmtimer_ioctl,
76 * Comparators and their associated info. Shub has
77 * three comparison registers.
81 * We only have comparison registers RTC1-4 currently available per
82 * node. RTC0 is used by SAL.
84 #define NUM_COMPARATORS 3
85 /* Check for an RTC interrupt pending */
86 static int inline mmtimer_int_pending(int comparator)
88 if (HUB_L((unsigned long *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED)) &
89 SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator)
94 /* Clear the RTC interrupt pending bit */
95 static void inline mmtimer_clr_int_pending(int comparator)
97 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED_ALIAS),
98 SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator);
101 /* Setup timer on comparator RTC1 */
102 static void inline mmtimer_setup_int_0(u64 expires)
106 /* Disable interrupt */
107 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 0UL);
109 /* Initialize comparator value */
110 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), -1L);
112 /* Clear pending bit */
113 mmtimer_clr_int_pending(0);
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);
119 /* Set configuration */
120 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_CONFIG), val);
122 /* Enable RTC interrupts */
123 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 1UL);
125 /* Initialize comparator value */
126 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), expires);
131 /* Setup timer on comparator RTC2 */
132 static void inline mmtimer_setup_int_1(u64 expires)
136 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 0UL);
138 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), -1L);
140 mmtimer_clr_int_pending(1);
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);
146 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_CONFIG), val);
148 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 1UL);
150 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), expires);
153 /* Setup timer on comparator RTC3 */
154 static void inline mmtimer_setup_int_2(u64 expires)
158 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 0UL);
160 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), -1L);
162 mmtimer_clr_int_pending(2);
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);
168 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_CONFIG), val);
170 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 1UL);
172 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), expires);
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.
182 static int inline mmtimer_setup(int comparator, unsigned long expires)
187 switch (comparator) {
189 mmtimer_setup_int_0(expires);
192 mmtimer_setup_int_1(expires);
195 mmtimer_setup_int_2(expires);
198 /* We might've missed our expiration time */
199 diff = rtc_time() - expires;
201 if (mmtimer_int_pending(comparator)) {
202 /* We'll get an interrupt for this once we're done */
205 /* Looks like we missed it */
212 static int inline mmtimer_disable_int(long nasid, int comparator)
214 switch (comparator) {
216 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE),
217 0UL) : REMOTE_HUB_S(nasid, SH_RTC1_INT_ENABLE, 0UL);
220 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE),
221 0UL) : REMOTE_HUB_S(nasid, SH_RTC2_INT_ENABLE, 0UL);
224 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE),
225 0UL) : REMOTE_HUB_S(nasid, SH_RTC3_INT_ENABLE, 0UL);
233 #define TIMER_OFF 0xbadcabLL
235 /* There is one of these for each comparator */
236 typedef struct mmtimer {
237 spinlock_t lock ____cacheline_aligned;
238 struct k_itimer *timer;
241 struct tasklet_struct tasklet;
245 * Total number of comparators is comparators/node * MAX nodes/running kernel
247 static mmtimer_t timers[NUM_COMPARATORS*MAX_COMPACT_NODES];
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
256 * Executes the command specified by @cmd. Returns 0 for success, < 0 for
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.
264 * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15)
267 * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address
270 * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter
272 * %MMTIMER_MMAPAVAIL - Returns 1 if the registers can be mmap'd into userspace
274 * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it
275 * in the address specified by @arg.
277 static int mmtimer_ioctl(struct inode *inode, struct file *file,
278 unsigned int cmd, unsigned long arg)
283 case MMTIMER_GETOFFSET: /* offset of the counter */
285 * SN RTC registers are on their own 64k page
287 if(PAGE_SIZE <= (1 << 16))
288 ret = (((long)RTC_COUNTER_ADDR) & (PAGE_SIZE-1)) / 8;
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)))
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)))
307 case MMTIMER_GETBITS: /* number of bits in the clock */
311 case MMTIMER_MMAPAVAIL: /* can we mmap the clock into userspace? */
312 ret = (PAGE_SIZE <= (1 << 16)) ? 1 : 0;
315 case MMTIMER_GETCOUNTER:
316 if(copy_to_user((unsigned long __user *)arg,
317 RTC_COUNTER_ADDR, sizeof(unsigned long)))
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
333 * Calls remap_pfn_range() to map the clock's registers into
334 * the calling process' address space.
336 static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma)
338 unsigned long mmtimer_addr;
340 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
343 if (vma->vm_flags & VM_WRITE)
346 if (PAGE_SIZE > (1 << 16))
349 vma->vm_flags |= (VM_IO | VM_SHM | VM_LOCKED );
350 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
352 mmtimer_addr = __pa(RTC_COUNTER_ADDR);
353 mmtimer_addr &= ~(PAGE_SIZE - 1);
354 mmtimer_addr &= 0xfffffffffffffffUL;
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");
365 static struct miscdevice mmtimer_miscdev = {
371 static struct timespec sgi_clock_offset;
372 static int sgi_clock_period;
375 * Posix Timer Interface
378 static struct timespec sgi_clock_offset;
379 static int sgi_clock_period;
381 static int sgi_clock_get(struct timespec *tp)
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;
392 static int sgi_clock_set(struct timespec *tp)
398 nsec = rtc_time() * sgi_clock_period;
400 sgi_clock_offset.tv_sec = tp->tv_sec - div_long_long_rem(nsec, NSEC_PER_SEC, &rem);
402 if (rem <= tp->tv_nsec)
403 sgi_clock_offset.tv_nsec = tp->tv_sec - rem;
405 sgi_clock_offset.tv_nsec = tp->tv_sec + NSEC_PER_SEC - rem;
406 sgi_clock_offset.tv_sec--;
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..
418 static int inline reschedule_periodic_timer(mmtimer_t *x)
421 struct k_itimer *t = x->timer;
423 t->it_timer.magic = x->i;
429 t->it_timer.expires += t->it_incr << n;
430 t->it_overrun += 1 << n;
435 } while (mmtimer_setup(x->i, t->it_timer.expires));
441 * mmtimer_interrupt - timer interrupt handler
443 * @dev_id: device the irq came from
444 * @regs: register state upon receipt of the interrupt
446 * Called when one of the comarators matches the counter, This
447 * routine will send signals to processes that have requested
450 * This interrupt is run in an interrupt context
451 * by the SHUB. It is therefore safe to locally access SHub
455 mmtimer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
458 mmtimer_t *base = timers + cpuid_to_cnodeid(smp_processor_id()) *
460 unsigned long expires = 0;
461 int result = IRQ_NONE;
464 * Do this once for each comparison register
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()) {
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;
480 spin_unlock(&base[i].lock);
486 void mmtimer_tasklet(unsigned long data) {
487 mmtimer_t *x = (mmtimer_t *)data;
488 struct k_itimer *t = x->timer;
494 /* Send signal and deal with periodic signals */
495 spin_lock_irqsave(&t->it_lock, flags);
497 /* If timer was deleted between interrupt and here, leave */
502 if (tasklist_lock.write_lock || posix_timer_event(t, 0) != 0) {
504 // printk(KERN_WARNING "mmtimer: cannot deliver signal.\n");
510 if (reschedule_periodic_timer(x)) {
511 printk(KERN_WARNING "mmtimer: unable to reschedule\n");
515 /* Ensure we don't false trigger in mmtimer_interrupt */
516 t->it_timer.expires = 0;
518 t->it_overrun_last = t->it_overrun;
520 spin_unlock(&x->lock);
521 spin_unlock_irqrestore(&t->it_lock, flags);
524 static int sgi_timer_create(struct k_itimer *timer)
526 /* Insure that a newly created timer is off */
527 timer->it_timer.magic = TIMER_OFF;
531 /* This does not really delete a timer. It just insures
532 * that the timer is not active
534 * Assumption: it_lock is already held with irq's disabled
536 static int sgi_timer_del(struct k_itimer *timr)
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;
543 if (i != TIMER_OFF) {
544 spin_lock_irqsave(&t->lock, irqflags);
545 mmtimer_disable_int(cnodeid_to_nasid(nodeid),i);
547 timr->it_timer.magic = TIMER_OFF;
548 timr->it_timer.expires = 0;
549 spin_unlock_irqrestore(&t->lock, irqflags);
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)
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)
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;
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);
575 static int sgi_timer_set(struct k_itimer *timr, int flags,
576 struct itimerspec * new_setting,
577 struct itimerspec * old_setting)
581 unsigned long when, period, irqflags;
587 sgi_timer_get(timr, old_setting);
590 when = timespec_to_ns(new_setting->it_value);
591 period = timespec_to_ns(new_setting->it_interval);
597 if (flags & TIMER_ABSTIME) {
601 when -= timespec_to_ns(n);
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
609 when = (when + sgi_clock_period - 1) / sgi_clock_period + rtc_time();
610 period = (period + sgi_clock_period - 1) / sgi_clock_period;
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
619 nodeid = cpuid_to_cnodeid(smp_processor_id());
620 base = timers + nodeid * NUM_COMPARATORS;
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) {
629 if (i == NUM_COMPARATORS) {
634 spin_lock_irqsave(&base[i].lock, irqflags);
636 if (base[i].timer || base[i].tasklet.state != 0) {
637 spin_unlock_irqrestore(&base[i].lock, irqflags);
640 base[i].timer = timr;
641 base[i].cpu = smp_processor_id();
643 timr->it_timer.magic = i;
644 timr->it_timer.data = nodeid;
645 timr->it_incr = period;
646 timr->it_timer.expires = when;
649 if (mmtimer_setup(i, when)) {
650 mmtimer_disable_int(-1, i);
651 posix_timer_event(timr, 0);
652 timr->it_timer.expires = 0;
655 timr->it_timer.expires -= period;
656 if (reschedule_periodic_timer(base+i))
660 spin_unlock_irqrestore(&base[i].lock, irqflags);
667 static struct k_clock sgi_clock = {
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
679 * mmtimer_init - device initialization routine
681 * Does initial setup for the mmtimer device.
683 static int __init mmtimer_init(void)
687 if (!ia64_platform_is("sn2"))
691 * Sanity check the cycles/sec variable
693 if (sn_rtc_cycles_per_second < 100000) {
694 printk(KERN_ERR "%s: unable to determine clock frequency\n",
699 mmtimer_femtoperiod = ((unsigned long)1E15 + sn_rtc_cycles_per_second /
700 2) / sn_rtc_cycles_per_second;
702 for (i=0; i< NUM_COMPARATORS*MAX_COMPACT_NODES; i++) {
703 spin_lock_init(&timers[i].lock);
704 timers[i].timer = NULL;
706 timers[i].i = i % NUM_COMPARATORS;
707 tasklet_init(&timers[i].tasklet, mmtimer_tasklet, (unsigned long) (timers+i));
710 if (request_irq(SGI_MMTIMER_VECTOR, mmtimer_interrupt, SA_PERCPU_IRQ, MMTIMER_NAME, NULL)) {
711 printk(KERN_WARNING "%s: unable to allocate interrupt.",
716 strcpy(mmtimer_miscdev.devfs_name, MMTIMER_NAME);
717 if (misc_register(&mmtimer_miscdev)) {
718 printk(KERN_ERR "%s: failed to register device\n",
723 sgi_clock_period = sgi_clock.res = NSEC_PER_SEC / sn_rtc_cycles_per_second;
724 register_posix_clock(CLOCK_SGI_CYCLE, &sgi_clock);
726 printk(KERN_INFO "%s: v%s, %ld MHz\n", MMTIMER_DESC, MMTIMER_VERSION,
727 sn_rtc_cycles_per_second/(unsigned long)1E6);
732 module_init(mmtimer_init);