+static struct timespec sgi_clock_offset;
+static int sgi_clock_period;
+
+/*
+ * Posix Timer Interface
+ */
+
+static struct timespec sgi_clock_offset;
+static int sgi_clock_period;
+
+static int sgi_clock_get(clockid_t clockid, struct timespec *tp)
+{
+ u64 nsec;
+
+ nsec = rtc_time() * sgi_clock_period
+ + sgi_clock_offset.tv_nsec;
+ tp->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &tp->tv_nsec)
+ + sgi_clock_offset.tv_sec;
+ return 0;
+};
+
+static int sgi_clock_set(clockid_t clockid, struct timespec *tp)
+{
+
+ u64 nsec;
+ u64 rem;
+
+ nsec = rtc_time() * sgi_clock_period;
+
+ sgi_clock_offset.tv_sec = tp->tv_sec - div_long_long_rem(nsec, NSEC_PER_SEC, &rem);
+
+ if (rem <= tp->tv_nsec)
+ sgi_clock_offset.tv_nsec = tp->tv_sec - rem;
+ else {
+ sgi_clock_offset.tv_nsec = tp->tv_sec + NSEC_PER_SEC - rem;
+ sgi_clock_offset.tv_sec--;
+ }
+ return 0;
+}
+
+/*
+ * Schedule the next periodic interrupt. This function will attempt
+ * to schedule a periodic interrupt later if necessary. If the scheduling
+ * of an interrupt fails then the time to skip is lengthened
+ * exponentially in order to ensure that the next interrupt
+ * can be properly scheduled..
+ */
+static int inline reschedule_periodic_timer(mmtimer_t *x)
+{
+ int n;
+ struct k_itimer *t = x->timer;
+
+ t->it.mmtimer.clock = x->i;
+ t->it_overrun--;
+
+ n = 0;
+ do {
+
+ t->it.mmtimer.expires += t->it.mmtimer.incr << n;
+ t->it_overrun += 1 << n;
+ n++;
+ if (n > 20)
+ return 1;
+
+ } while (!mmtimer_setup(x->i, t->it.mmtimer.expires));
+
+ return 0;
+}
+
+/**
+ * mmtimer_interrupt - timer interrupt handler
+ * @irq: irq received
+ * @dev_id: device the irq came from
+ * @regs: register state upon receipt of the interrupt
+ *
+ * Called when one of the comarators matches the counter, This
+ * routine will send signals to processes that have requested
+ * them.
+ *
+ * This interrupt is run in an interrupt context
+ * by the SHUB. It is therefore safe to locally access SHub
+ * registers.
+ */
+static irqreturn_t
+mmtimer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+{
+ int i;
+ unsigned long expires = 0;
+ int result = IRQ_NONE;
+ unsigned indx = cpu_to_node(smp_processor_id());
+
+ /*
+ * Do this once for each comparison register
+ */
+ for (i = 0; i < NUM_COMPARATORS; i++) {
+ mmtimer_t *base = timers[indx] + i;
+ /* Make sure this doesn't get reused before tasklet_sched */
+ spin_lock(&base->lock);
+ if (base->cpu == smp_processor_id()) {
+ if (base->timer)
+ expires = base->timer->it.mmtimer.expires;
+ /* expires test won't work with shared irqs */
+ if ((mmtimer_int_pending(i) > 0) ||
+ (expires && (expires < rtc_time()))) {
+ mmtimer_clr_int_pending(i);
+ tasklet_schedule(&base->tasklet);
+ result = IRQ_HANDLED;
+ }
+ }
+ spin_unlock(&base->lock);
+ expires = 0;
+ }
+ return result;
+}
+
+void mmtimer_tasklet(unsigned long data) {
+ mmtimer_t *x = (mmtimer_t *)data;
+ struct k_itimer *t = x->timer;
+ unsigned long flags;
+
+ if (t == NULL)
+ return;
+
+ /* Send signal and deal with periodic signals */
+ spin_lock_irqsave(&t->it_lock, flags);
+ spin_lock(&x->lock);
+ /* If timer was deleted between interrupt and here, leave */
+ if (t != x->timer)
+ goto out;
+ t->it_overrun = 0;
+
+ if (posix_timer_event(t, 0) != 0) {
+
+ // printk(KERN_WARNING "mmtimer: cannot deliver signal.\n");
+
+ t->it_overrun++;
+ }
+ if(t->it.mmtimer.incr) {
+ /* Periodic timer */
+ if (reschedule_periodic_timer(x)) {
+ printk(KERN_WARNING "mmtimer: unable to reschedule\n");
+ x->timer = NULL;
+ }
+ } else {
+ /* Ensure we don't false trigger in mmtimer_interrupt */
+ t->it.mmtimer.expires = 0;
+ }
+ t->it_overrun_last = t->it_overrun;
+out:
+ spin_unlock(&x->lock);
+ spin_unlock_irqrestore(&t->it_lock, flags);
+}
+
+static int sgi_timer_create(struct k_itimer *timer)
+{
+ /* Insure that a newly created timer is off */
+ timer->it.mmtimer.clock = TIMER_OFF;
+ return 0;
+}
+
+/* This does not really delete a timer. It just insures
+ * that the timer is not active
+ *
+ * Assumption: it_lock is already held with irq's disabled
+ */
+static int sgi_timer_del(struct k_itimer *timr)
+{
+ int i = timr->it.mmtimer.clock;
+ cnodeid_t nodeid = timr->it.mmtimer.node;
+ mmtimer_t *t = timers[nodeid] + i;
+ unsigned long irqflags;
+
+ if (i != TIMER_OFF) {
+ spin_lock_irqsave(&t->lock, irqflags);
+ mmtimer_disable_int(cnodeid_to_nasid(nodeid),i);
+ t->timer = NULL;
+ timr->it.mmtimer.clock = TIMER_OFF;
+ timr->it.mmtimer.expires = 0;
+ spin_unlock_irqrestore(&t->lock, irqflags);
+ }
+ return 0;
+}
+
+#define timespec_to_ns(x) ((x).tv_nsec + (x).tv_sec * NSEC_PER_SEC)
+#define ns_to_timespec(ts, nsec) (ts).tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &(ts).tv_nsec)
+
+/* Assumption: it_lock is already held with irq's disabled */
+static void sgi_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
+{
+
+ if (timr->it.mmtimer.clock == TIMER_OFF) {
+ cur_setting->it_interval.tv_nsec = 0;
+ cur_setting->it_interval.tv_sec = 0;
+ cur_setting->it_value.tv_nsec = 0;
+ cur_setting->it_value.tv_sec =0;
+ return;
+ }
+
+ ns_to_timespec(cur_setting->it_interval, timr->it.mmtimer.incr * sgi_clock_period);
+ ns_to_timespec(cur_setting->it_value, (timr->it.mmtimer.expires - rtc_time())* sgi_clock_period);
+ return;
+}
+
+
+static int sgi_timer_set(struct k_itimer *timr, int flags,
+ struct itimerspec * new_setting,
+ struct itimerspec * old_setting)
+{
+
+ int i;
+ unsigned long when, period, irqflags;
+ int err = 0;
+ cnodeid_t nodeid;
+ mmtimer_t *base;
+
+ if (old_setting)
+ sgi_timer_get(timr, old_setting);
+
+ sgi_timer_del(timr);
+ when = timespec_to_ns(new_setting->it_value);
+ period = timespec_to_ns(new_setting->it_interval);
+
+ if (when == 0)
+ /* Clear timer */
+ return 0;
+
+ if (flags & TIMER_ABSTIME) {
+ struct timespec n;
+ unsigned long now;
+
+ getnstimeofday(&n);
+ now = timespec_to_ns(n);
+ if (when > now)
+ when -= now;
+ else
+ /* Fire the timer immediately */
+ when = 0;
+ }
+
+ /*
+ * Convert to sgi clock period. Need to keep rtc_time() as near as possible
+ * to getnstimeofday() in order to be as faithful as possible to the time
+ * specified.
+ */
+ when = (when + sgi_clock_period - 1) / sgi_clock_period + rtc_time();
+ period = (period + sgi_clock_period - 1) / sgi_clock_period;
+
+ /*
+ * We are allocating a local SHub comparator. If we would be moved to another
+ * cpu then another SHub may be local to us. Prohibit that by switching off
+ * preemption.
+ */
+ preempt_disable();
+
+ nodeid = cpu_to_node(smp_processor_id());
+retry:
+ /* Don't use an allocated timer, or a deleted one that's pending */
+ for(i = 0; i< NUM_COMPARATORS; i++) {
+ base = timers[nodeid] + i;
+ if (!base->timer && !base->tasklet.state) {
+ break;
+ }
+ }
+
+ if (i == NUM_COMPARATORS) {
+ preempt_enable();
+ return -EBUSY;
+ }
+
+ spin_lock_irqsave(&base->lock, irqflags);
+
+ if (base->timer || base->tasklet.state != 0) {
+ spin_unlock_irqrestore(&base->lock, irqflags);
+ goto retry;
+ }
+ base->timer = timr;
+ base->cpu = smp_processor_id();
+
+ timr->it.mmtimer.clock = i;
+ timr->it.mmtimer.node = nodeid;
+ timr->it.mmtimer.incr = period;
+ timr->it.mmtimer.expires = when;
+
+ if (period == 0) {
+ if (!mmtimer_setup(i, when)) {
+ mmtimer_disable_int(-1, i);
+ posix_timer_event(timr, 0);
+ timr->it.mmtimer.expires = 0;
+ }
+ } else {
+ timr->it.mmtimer.expires -= period;
+ if (reschedule_periodic_timer(base))
+ err = -EINVAL;
+ }
+
+ spin_unlock_irqrestore(&base->lock, irqflags);
+
+ preempt_enable();
+
+ return err;
+}
+
+static struct k_clock sgi_clock = {
+ .res = 0,
+ .clock_set = sgi_clock_set,
+ .clock_get = sgi_clock_get,
+ .timer_create = sgi_timer_create,
+ .nsleep = do_posix_clock_nonanosleep,
+ .timer_set = sgi_timer_set,
+ .timer_del = sgi_timer_del,
+ .timer_get = sgi_timer_get
+};
+