2 * 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-2006 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>
29 #include <linux/mmtimer.h>
30 #include <linux/miscdevice.h>
31 #include <linux/posix-timers.h>
32 #include <linux/interrupt.h>
33 #include <linux/time.h>
34 #include <linux/math64.h>
36 #include <asm/uaccess.h>
37 #include <asm/sn/addrs.h>
38 #include <asm/sn/intr.h>
39 #include <asm/sn/shub_mmr.h>
40 #include <asm/sn/nodepda.h>
41 #include <asm/sn/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.1"
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 const struct file_operations mmtimer_fops = {
72 .ioctl = mmtimer_ioctl,
76 * We only have comparison registers RTC1-4 currently available per
77 * node. RTC0 is used by SAL.
79 /* Check for an RTC interrupt pending */
80 static int mmtimer_int_pending(int comparator)
82 if (HUB_L((unsigned long *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED)) &
83 SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator)
89 /* Clear the RTC interrupt pending bit */
90 static void mmtimer_clr_int_pending(int comparator)
92 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED_ALIAS),
93 SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator);
96 /* Setup timer on comparator RTC1 */
97 static void mmtimer_setup_int_0(int cpu, u64 expires)
101 /* Disable interrupt */
102 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 0UL);
104 /* Initialize comparator value */
105 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), -1L);
107 /* Clear pending bit */
108 mmtimer_clr_int_pending(0);
110 val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC1_INT_CONFIG_IDX_SHFT) |
111 ((u64)cpu_physical_id(cpu) <<
112 SH_RTC1_INT_CONFIG_PID_SHFT);
114 /* Set configuration */
115 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_CONFIG), val);
117 /* Enable RTC interrupts */
118 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 1UL);
120 /* Initialize comparator value */
121 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), expires);
126 /* Setup timer on comparator RTC2 */
127 static void mmtimer_setup_int_1(int cpu, u64 expires)
131 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 0UL);
133 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), -1L);
135 mmtimer_clr_int_pending(1);
137 val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC2_INT_CONFIG_IDX_SHFT) |
138 ((u64)cpu_physical_id(cpu) <<
139 SH_RTC2_INT_CONFIG_PID_SHFT);
141 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_CONFIG), val);
143 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 1UL);
145 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), expires);
148 /* Setup timer on comparator RTC3 */
149 static void mmtimer_setup_int_2(int cpu, u64 expires)
153 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 0UL);
155 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), -1L);
157 mmtimer_clr_int_pending(2);
159 val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC3_INT_CONFIG_IDX_SHFT) |
160 ((u64)cpu_physical_id(cpu) <<
161 SH_RTC3_INT_CONFIG_PID_SHFT);
163 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_CONFIG), val);
165 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 1UL);
167 HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), expires);
171 * This function must be called with interrupts disabled and preemption off
172 * in order to insure that the setup succeeds in a deterministic time frame.
173 * It will check if the interrupt setup succeeded.
175 static int mmtimer_setup(int cpu, int comparator, unsigned long expires)
178 switch (comparator) {
180 mmtimer_setup_int_0(cpu, expires);
183 mmtimer_setup_int_1(cpu, expires);
186 mmtimer_setup_int_2(cpu, expires);
189 /* We might've missed our expiration time */
190 if (rtc_time() <= expires)
194 * If an interrupt is already pending then its okay
195 * if not then we failed
197 return mmtimer_int_pending(comparator);
200 static int mmtimer_disable_int(long nasid, int comparator)
202 switch (comparator) {
204 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE),
205 0UL) : REMOTE_HUB_S(nasid, SH_RTC1_INT_ENABLE, 0UL);
208 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE),
209 0UL) : REMOTE_HUB_S(nasid, SH_RTC2_INT_ENABLE, 0UL);
212 nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE),
213 0UL) : REMOTE_HUB_S(nasid, SH_RTC3_INT_ENABLE, 0UL);
221 #define COMPARATOR 1 /* The comparator to use */
223 #define TIMER_OFF 0xbadcabLL /* Timer is not setup */
224 #define TIMER_SET 0 /* Comparator is set for this timer */
226 /* There is one of these for each timer */
229 struct k_itimer *timer;
233 struct mmtimer_node {
234 spinlock_t lock ____cacheline_aligned;
235 struct rb_root timer_head;
236 struct rb_node *next;
237 struct tasklet_struct tasklet;
239 static struct mmtimer_node *timers;
243 * Add a new mmtimer struct to the node's mmtimer list.
244 * This function assumes the struct mmtimer_node is locked.
246 static void mmtimer_add_list(struct mmtimer *n)
248 int nodeid = n->timer->it.mmtimer.node;
249 unsigned long expires = n->timer->it.mmtimer.expires;
250 struct rb_node **link = &timers[nodeid].timer_head.rb_node;
251 struct rb_node *parent = NULL;
255 * Find the right place in the rbtree:
259 x = rb_entry(parent, struct mmtimer, list);
261 if (expires < x->timer->it.mmtimer.expires)
262 link = &(*link)->rb_left;
264 link = &(*link)->rb_right;
268 * Insert the timer to the rbtree and check whether it
269 * replaces the first pending timer
271 rb_link_node(&n->list, parent, link);
272 rb_insert_color(&n->list, &timers[nodeid].timer_head);
274 if (!timers[nodeid].next || expires < rb_entry(timers[nodeid].next,
275 struct mmtimer, list)->timer->it.mmtimer.expires)
276 timers[nodeid].next = &n->list;
280 * Set the comparator for the next timer.
281 * This function assumes the struct mmtimer_node is locked.
283 static void mmtimer_set_next_timer(int nodeid)
285 struct mmtimer_node *n = &timers[nodeid];
294 x = rb_entry(n->next, struct mmtimer, list);
296 if (!t->it.mmtimer.incr) {
297 /* Not an interval timer */
298 if (!mmtimer_setup(x->cpu, COMPARATOR,
299 t->it.mmtimer.expires)) {
300 /* Late setup, fire now */
301 tasklet_schedule(&n->tasklet);
308 while (!mmtimer_setup(x->cpu, COMPARATOR, t->it.mmtimer.expires)) {
310 struct rb_node *next;
311 t->it.mmtimer.expires += t->it.mmtimer.incr << o;
312 t->it_overrun += 1 << o;
315 printk(KERN_ALERT "mmtimer: cannot reschedule timer\n");
316 t->it.mmtimer.clock = TIMER_OFF;
317 n->next = rb_next(&x->list);
318 rb_erase(&x->list, &n->timer_head);
323 e = t->it.mmtimer.expires;
324 next = rb_next(&x->list);
329 e1 = rb_entry(next, struct mmtimer, list)->
330 timer->it.mmtimer.expires;
333 rb_erase(&x->list, &n->timer_head);
341 * mmtimer_ioctl - ioctl interface for /dev/mmtimer
342 * @inode: inode of the device
343 * @file: file structure for the device
344 * @cmd: command to execute
345 * @arg: optional argument to command
347 * Executes the command specified by @cmd. Returns 0 for success, < 0 for
352 * %MMTIMER_GETOFFSET - Should return the offset (relative to the start
353 * of the page where the registers are mapped) for the counter in question.
355 * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15)
358 * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address
361 * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter
363 * %MMTIMER_MMAPAVAIL - Returns 1 if the registers can be mmap'd into userspace
365 * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it
366 * in the address specified by @arg.
368 static int mmtimer_ioctl(struct inode *inode, struct file *file,
369 unsigned int cmd, unsigned long arg)
374 case MMTIMER_GETOFFSET: /* offset of the counter */
376 * SN RTC registers are on their own 64k page
378 if(PAGE_SIZE <= (1 << 16))
379 ret = (((long)RTC_COUNTER_ADDR) & (PAGE_SIZE-1)) / 8;
384 case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */
385 if(copy_to_user((unsigned long __user *)arg,
386 &mmtimer_femtoperiod, sizeof(unsigned long)))
390 case MMTIMER_GETFREQ: /* frequency in Hz */
391 if(copy_to_user((unsigned long __user *)arg,
392 &sn_rtc_cycles_per_second,
393 sizeof(unsigned long)))
398 case MMTIMER_GETBITS: /* number of bits in the clock */
402 case MMTIMER_MMAPAVAIL: /* can we mmap the clock into userspace? */
403 ret = (PAGE_SIZE <= (1 << 16)) ? 1 : 0;
406 case MMTIMER_GETCOUNTER:
407 if(copy_to_user((unsigned long __user *)arg,
408 RTC_COUNTER_ADDR, sizeof(unsigned long)))
420 * mmtimer_mmap - maps the clock's registers into userspace
421 * @file: file structure for the device
422 * @vma: VMA to map the registers into
424 * Calls remap_pfn_range() to map the clock's registers into
425 * the calling process' address space.
427 static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma)
429 unsigned long mmtimer_addr;
431 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
434 if (vma->vm_flags & VM_WRITE)
437 if (PAGE_SIZE > (1 << 16))
440 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
442 mmtimer_addr = __pa(RTC_COUNTER_ADDR);
443 mmtimer_addr &= ~(PAGE_SIZE - 1);
444 mmtimer_addr &= 0xfffffffffffffffUL;
446 if (remap_pfn_range(vma, vma->vm_start, mmtimer_addr >> PAGE_SHIFT,
447 PAGE_SIZE, vma->vm_page_prot)) {
448 printk(KERN_ERR "remap_pfn_range failed in mmtimer.c\n");
455 static struct miscdevice mmtimer_miscdev = {
461 static struct timespec sgi_clock_offset;
462 static int sgi_clock_period;
465 * Posix Timer Interface
468 static struct timespec sgi_clock_offset;
469 static int sgi_clock_period;
471 static int sgi_clock_get(clockid_t clockid, struct timespec *tp)
475 nsec = rtc_time() * sgi_clock_period
476 + sgi_clock_offset.tv_nsec;
477 *tp = ns_to_timespec(nsec);
478 tp->tv_sec += sgi_clock_offset.tv_sec;
482 static int sgi_clock_set(clockid_t clockid, struct timespec *tp)
488 nsec = rtc_time() * sgi_clock_period;
490 sgi_clock_offset.tv_sec = tp->tv_sec - div_u64_rem(nsec, NSEC_PER_SEC, &rem);
492 if (rem <= tp->tv_nsec)
493 sgi_clock_offset.tv_nsec = tp->tv_sec - rem;
495 sgi_clock_offset.tv_nsec = tp->tv_sec + NSEC_PER_SEC - rem;
496 sgi_clock_offset.tv_sec--;
502 * mmtimer_interrupt - timer interrupt handler
504 * @dev_id: device the irq came from
506 * Called when one of the comarators matches the counter, This
507 * routine will send signals to processes that have requested
510 * This interrupt is run in an interrupt context
511 * by the SHUB. It is therefore safe to locally access SHub
515 mmtimer_interrupt(int irq, void *dev_id)
517 unsigned long expires = 0;
518 int result = IRQ_NONE;
519 unsigned indx = cpu_to_node(smp_processor_id());
520 struct mmtimer *base;
522 spin_lock(&timers[indx].lock);
523 base = rb_entry(timers[indx].next, struct mmtimer, list);
525 spin_unlock(&timers[indx].lock);
529 if (base->cpu == smp_processor_id()) {
531 expires = base->timer->it.mmtimer.expires;
532 /* expires test won't work with shared irqs */
533 if ((mmtimer_int_pending(COMPARATOR) > 0) ||
534 (expires && (expires <= rtc_time()))) {
535 mmtimer_clr_int_pending(COMPARATOR);
536 tasklet_schedule(&timers[indx].tasklet);
537 result = IRQ_HANDLED;
540 spin_unlock(&timers[indx].lock);
544 static void mmtimer_tasklet(unsigned long data)
547 struct mmtimer_node *mn = &timers[nodeid];
548 struct mmtimer *x = rb_entry(mn->next, struct mmtimer, list);
552 /* Send signal and deal with periodic signals */
553 spin_lock_irqsave(&mn->lock, flags);
557 x = rb_entry(mn->next, struct mmtimer, list);
560 if (t->it.mmtimer.clock == TIMER_OFF)
565 mn->next = rb_next(&x->list);
566 rb_erase(&x->list, &mn->timer_head);
568 if (posix_timer_event(t, 0) != 0)
571 if(t->it.mmtimer.incr) {
572 t->it.mmtimer.expires += t->it.mmtimer.incr;
575 /* Ensure we don't false trigger in mmtimer_interrupt */
576 t->it.mmtimer.clock = TIMER_OFF;
577 t->it.mmtimer.expires = 0;
580 /* Set comparator for next timer, if there is one */
581 mmtimer_set_next_timer(nodeid);
583 t->it_overrun_last = t->it_overrun;
585 spin_unlock_irqrestore(&mn->lock, flags);
588 static int sgi_timer_create(struct k_itimer *timer)
590 /* Insure that a newly created timer is off */
591 timer->it.mmtimer.clock = TIMER_OFF;
595 /* This does not really delete a timer. It just insures
596 * that the timer is not active
598 * Assumption: it_lock is already held with irq's disabled
600 static int sgi_timer_del(struct k_itimer *timr)
602 cnodeid_t nodeid = timr->it.mmtimer.node;
603 unsigned long irqflags;
605 spin_lock_irqsave(&timers[nodeid].lock, irqflags);
606 if (timr->it.mmtimer.clock != TIMER_OFF) {
607 unsigned long expires = timr->it.mmtimer.expires;
608 struct rb_node *n = timers[nodeid].timer_head.rb_node;
609 struct mmtimer *uninitialized_var(t);
612 timr->it.mmtimer.clock = TIMER_OFF;
613 timr->it.mmtimer.expires = 0;
616 t = rb_entry(n, struct mmtimer, list);
617 if (t->timer == timr)
620 if (expires < t->timer->it.mmtimer.expires)
627 spin_unlock_irqrestore(&timers[nodeid].lock, irqflags);
631 if (timers[nodeid].next == n) {
632 timers[nodeid].next = rb_next(n);
636 rb_erase(n, &timers[nodeid].timer_head);
640 mmtimer_disable_int(cnodeid_to_nasid(nodeid),
642 mmtimer_set_next_timer(nodeid);
645 spin_unlock_irqrestore(&timers[nodeid].lock, irqflags);
649 /* Assumption: it_lock is already held with irq's disabled */
650 static void sgi_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
653 if (timr->it.mmtimer.clock == TIMER_OFF) {
654 cur_setting->it_interval.tv_nsec = 0;
655 cur_setting->it_interval.tv_sec = 0;
656 cur_setting->it_value.tv_nsec = 0;
657 cur_setting->it_value.tv_sec =0;
661 cur_setting->it_interval = ns_to_timespec(timr->it.mmtimer.incr * sgi_clock_period);
662 cur_setting->it_value = ns_to_timespec((timr->it.mmtimer.expires - rtc_time()) * sgi_clock_period);
666 static int sgi_timer_set(struct k_itimer *timr, int flags,
667 struct itimerspec * new_setting,
668 struct itimerspec * old_setting)
670 unsigned long when, period, irqflags;
673 struct mmtimer *base;
677 sgi_timer_get(timr, old_setting);
680 when = timespec_to_ns(&new_setting->it_value);
681 period = timespec_to_ns(&new_setting->it_interval);
687 base = kmalloc(sizeof(struct mmtimer), GFP_KERNEL);
691 if (flags & TIMER_ABSTIME) {
696 now = timespec_to_ns(&n);
700 /* Fire the timer immediately */
705 * Convert to sgi clock period. Need to keep rtc_time() as near as possible
706 * to getnstimeofday() in order to be as faithful as possible to the time
709 when = (when + sgi_clock_period - 1) / sgi_clock_period + rtc_time();
710 period = (period + sgi_clock_period - 1) / sgi_clock_period;
713 * We are allocating a local SHub comparator. If we would be moved to another
714 * cpu then another SHub may be local to us. Prohibit that by switching off
719 nodeid = cpu_to_node(smp_processor_id());
721 /* Lock the node timer structure */
722 spin_lock_irqsave(&timers[nodeid].lock, irqflags);
725 base->cpu = smp_processor_id();
727 timr->it.mmtimer.clock = TIMER_SET;
728 timr->it.mmtimer.node = nodeid;
729 timr->it.mmtimer.incr = period;
730 timr->it.mmtimer.expires = when;
732 n = timers[nodeid].next;
734 /* Add the new struct mmtimer to node's timer list */
735 mmtimer_add_list(base);
737 if (timers[nodeid].next == n) {
738 /* No need to reprogram comparator for now */
739 spin_unlock_irqrestore(&timers[nodeid].lock, irqflags);
744 /* We need to reprogram the comparator */
746 mmtimer_disable_int(cnodeid_to_nasid(nodeid), COMPARATOR);
748 mmtimer_set_next_timer(nodeid);
750 /* Unlock the node timer structure */
751 spin_unlock_irqrestore(&timers[nodeid].lock, irqflags);
758 static struct k_clock sgi_clock = {
760 .clock_set = sgi_clock_set,
761 .clock_get = sgi_clock_get,
762 .timer_create = sgi_timer_create,
763 .nsleep = do_posix_clock_nonanosleep,
764 .timer_set = sgi_timer_set,
765 .timer_del = sgi_timer_del,
766 .timer_get = sgi_timer_get
770 * mmtimer_init - device initialization routine
772 * Does initial setup for the mmtimer device.
774 static int __init mmtimer_init(void)
776 cnodeid_t node, maxn = -1;
778 if (!ia64_platform_is("sn2"))
782 * Sanity check the cycles/sec variable
784 if (sn_rtc_cycles_per_second < 100000) {
785 printk(KERN_ERR "%s: unable to determine clock frequency\n",
790 mmtimer_femtoperiod = ((unsigned long)1E15 + sn_rtc_cycles_per_second /
791 2) / sn_rtc_cycles_per_second;
793 if (request_irq(SGI_MMTIMER_VECTOR, mmtimer_interrupt, IRQF_PERCPU, MMTIMER_NAME, NULL)) {
794 printk(KERN_WARNING "%s: unable to allocate interrupt.",
799 if (misc_register(&mmtimer_miscdev)) {
800 printk(KERN_ERR "%s: failed to register device\n",
805 /* Get max numbered node, calculate slots needed */
806 for_each_online_node(node) {
811 /* Allocate list of node ptrs to mmtimer_t's */
812 timers = kzalloc(sizeof(struct mmtimer_node)*maxn, GFP_KERNEL);
813 if (timers == NULL) {
814 printk(KERN_ERR "%s: failed to allocate memory for device\n",
819 /* Initialize struct mmtimer's for each online node */
820 for_each_online_node(node) {
821 spin_lock_init(&timers[node].lock);
822 tasklet_init(&timers[node].tasklet, mmtimer_tasklet,
823 (unsigned long) node);
826 sgi_clock_period = sgi_clock.res = NSEC_PER_SEC / sn_rtc_cycles_per_second;
827 register_posix_clock(CLOCK_SGI_CYCLE, &sgi_clock);
829 printk(KERN_INFO "%s: v%s, %ld MHz\n", MMTIMER_DESC, MMTIMER_VERSION,
830 sn_rtc_cycles_per_second/(unsigned long)1E6);
836 misc_deregister(&mmtimer_miscdev);
838 free_irq(SGI_MMTIMER_VECTOR, NULL);
843 module_init(mmtimer_init);