fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / arm / kernel / time.c
index bfed0f4..3c8cdcf 100644 (file)
@@ -16,7 +16,6 @@
  *  1998-12-20  Updated NTP code according to technical memorandum Jan '96
  *              "A Kernel Model for Precision Timekeeping" by Dave Mills
  */
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/interrupt.h>
 #include <linux/errno.h>
 #include <linux/profile.h>
 #include <linux/sysdev.h>
+#include <linux/timer.h>
+#include <linux/irq.h>
 
-#include <asm/hardware.h>
-#include <asm/io.h>
-#include <asm/irq.h>
-#include <asm/leds.h>
-
-u64 jiffies_64 = INITIAL_JIFFIES;
+#include <linux/mc146818rtc.h>
 
-EXPORT_SYMBOL(jiffies_64);
+#include <asm/leds.h>
+#include <asm/thread_info.h>
+#include <asm/mach/time.h>
 
-extern unsigned long wall_jiffies;
+/*
+ * Our system timer.
+ */
+struct sys_timer *system_timer;
 
 /* this needs a better home */
-spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
+DEFINE_SPINLOCK(rtc_lock);
 
 #ifdef CONFIG_SA1100_RTC_MODULE
 EXPORT_SYMBOL(rtc_lock);
@@ -49,26 +50,32 @@ EXPORT_SYMBOL(rtc_lock);
 /* change this if you have some constant time drift */
 #define USECS_PER_JIFFY        (1000000/HZ)
 
-static int dummy_set_rtc(void)
+#ifdef CONFIG_SMP
+unsigned long profile_pc(struct pt_regs *regs)
 {
-       return 0;
+       unsigned long fp, pc = instruction_pointer(regs);
+
+       if (in_lock_functions(pc)) {
+               fp = regs->ARM_fp;
+               pc = pc_pointer(((unsigned long *)fp)[-1]);
+       }
+
+       return pc;
 }
+EXPORT_SYMBOL(profile_pc);
+#endif
 
 /*
  * hook for setting the RTC's idea of the current time.
  */
-int (*set_rtc)(void) = dummy_set_rtc;
+int (*set_rtc)(void);
 
+#ifndef CONFIG_GENERIC_TIME
 static unsigned long dummy_gettimeoffset(void)
 {
        return 0;
 }
-
-/*
- * hook for getting the time offset.  Note that it is
- * always called with interrupts disabled.
- */
-unsigned long (*gettimeoffset)(void) = dummy_gettimeoffset;
+#endif
 
 /*
  * Scheduler clock - returns current time in nanosec units.
@@ -81,28 +88,14 @@ unsigned long long __attribute__((weak)) sched_clock(void)
 }
 
 /*
- * Handle kernel profile stuff...
+ * An implementation of printk_clock() independent from
+ * sched_clock().  This avoids non-bootable kernels when
+ * printk_clock is enabled.
  */
-static inline void do_profile(struct pt_regs *regs)
+unsigned long long printk_clock(void)
 {
-
-       profile_hook(regs);
-
-       if (!user_mode(regs) &&
-           prof_buffer &&
-           current->pid) {
-               unsigned long pc = instruction_pointer(regs);
-               extern int _stext;
-
-               pc -= (unsigned long)&_stext;
-
-               pc >>= prof_shift;
-
-               if (pc >= prof_len)
-                       pc = prof_len - 1;
-
-               prof_buffer[pc] += 1;
-       }
+       return (unsigned long long)(jiffies - INITIAL_JIFFIES) *
+                       (1000000000 / HZ);
 }
 
 static unsigned long next_rtc_update;
@@ -115,7 +108,7 @@ static unsigned long next_rtc_update;
  */
 static inline void do_set_rtc(void)
 {
-       if (time_status & STA_UNSYNC || set_rtc == NULL)
+       if (!ntp_synced() || set_rtc == NULL)
                return;
 
        if (next_rtc_update &&
@@ -191,7 +184,7 @@ static ssize_t leds_store(struct sys_device *dev, const char *buf, size_t size)
 
 static SYSDEV_ATTR(event, 0200, NULL, leds_store);
 
-static int leds_suspend(struct sys_device *dev, u32 state)
+static int leds_suspend(struct sys_device *dev, pm_message_t state)
 {
        leds_event(led_stop);
        return 0;
@@ -238,33 +231,29 @@ EXPORT_SYMBOL(leds_event);
 #endif
 
 #ifdef CONFIG_LEDS_TIMER
-static void do_leds(void)
+static inline void do_leds(void)
 {
-       static unsigned int count = 50;
+       static unsigned int count = HZ/2;
 
        if (--count == 0) {
-               count = 50;
+               count = HZ/2;
                leds_event(led_timer);
        }
 }
 #else
-#define do_leds()
+#define        do_leds()
 #endif
 
+#ifndef CONFIG_GENERIC_TIME
 void do_gettimeofday(struct timeval *tv)
 {
        unsigned long flags;
        unsigned long seq;
-       unsigned long usec, sec, lost;
+       unsigned long usec, sec;
 
        do {
                seq = read_seqbegin_irqsave(&xtime_lock, flags);
-               usec = gettimeoffset();
-
-               lost = jiffies - wall_jiffies;
-               if (lost)
-                       usec += lost * USECS_PER_JIFFY;
-
+               usec = system_timer->offset();
                sec = xtime.tv_sec;
                usec += xtime.tv_nsec / 1000;
        } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
@@ -296,8 +285,7 @@ int do_settimeofday(struct timespec *tv)
         * wall time.  Discover what correction gettimeofday() would have
         * done, and then undo it!
         */
-       nsec -= gettimeoffset() * NSEC_PER_USEC;
-       nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
+       nsec -= system_timer->offset() * NSEC_PER_USEC;
 
        wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
        wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
@@ -305,23 +293,232 @@ int do_settimeofday(struct timespec *tv)
        set_normalized_timespec(&xtime, sec, nsec);
        set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
 
-       time_adjust = 0;                /* stop active adjtime() */
-       time_status |= STA_UNSYNC;
-       time_maxerror = NTP_PHASE_LIMIT;
-       time_esterror = NTP_PHASE_LIMIT;
+       ntp_clear();
        write_sequnlock_irq(&xtime_lock);
        clock_was_set();
        return 0;
 }
 
 EXPORT_SYMBOL(do_settimeofday);
+#endif /* !CONFIG_GENERIC_TIME */
 
-static struct irqaction timer_irq = {
-       .name   = "timer",
-       .flags  = SA_INTERRUPT,
+/**
+ * save_time_delta - Save the offset between system time and RTC time
+ * @delta: pointer to timespec to store delta
+ * @rtc: pointer to timespec for current RTC time
+ *
+ * Return a delta between the system time and the RTC time, such
+ * that system time can be restored later with restore_time_delta()
+ */
+void save_time_delta(struct timespec *delta, struct timespec *rtc)
+{
+       set_normalized_timespec(delta,
+                               xtime.tv_sec - rtc->tv_sec,
+                               xtime.tv_nsec - rtc->tv_nsec);
+}
+EXPORT_SYMBOL(save_time_delta);
+
+/**
+ * restore_time_delta - Restore the current system time
+ * @delta: delta returned by save_time_delta()
+ * @rtc: pointer to timespec for current RTC time
+ */
+void restore_time_delta(struct timespec *delta, struct timespec *rtc)
+{
+       struct timespec ts;
+
+       set_normalized_timespec(&ts,
+                               delta->tv_sec + rtc->tv_sec,
+                               delta->tv_nsec + rtc->tv_nsec);
+
+       do_settimeofday(&ts);
+}
+EXPORT_SYMBOL(restore_time_delta);
+
+/*
+ * Kernel system timer support.
+ */
+void timer_tick(void)
+{
+       profile_tick(CPU_PROFILING);
+       do_leds();
+       do_set_rtc();
+       do_timer(1);
+#ifndef CONFIG_SMP
+       update_process_times(user_mode(get_irq_regs()));
+#endif
+}
+
+#ifdef CONFIG_PM
+static int timer_suspend(struct sys_device *dev, pm_message_t state)
+{
+       struct sys_timer *timer = container_of(dev, struct sys_timer, dev);
+
+       if (timer->suspend != NULL)
+               timer->suspend();
+
+       return 0;
+}
+
+static int timer_resume(struct sys_device *dev)
+{
+       struct sys_timer *timer = container_of(dev, struct sys_timer, dev);
+
+       if (timer->resume != NULL)
+               timer->resume();
+
+       return 0;
+}
+#else
+#define timer_suspend NULL
+#define timer_resume NULL
+#endif
+
+static struct sysdev_class timer_sysclass = {
+       set_kset_name("timer"),
+       .suspend        = timer_suspend,
+       .resume         = timer_resume,
 };
 
+#ifdef CONFIG_NO_IDLE_HZ
+static int timer_dyn_tick_enable(void)
+{
+       struct dyn_tick_timer *dyn_tick = system_timer->dyn_tick;
+       unsigned long flags;
+       int ret = -ENODEV;
+
+       if (dyn_tick) {
+               spin_lock_irqsave(&dyn_tick->lock, flags);
+               ret = 0;
+               if (!(dyn_tick->state & DYN_TICK_ENABLED)) {
+                       ret = dyn_tick->enable();
+
+                       if (ret == 0)
+                               dyn_tick->state |= DYN_TICK_ENABLED;
+               }
+               spin_unlock_irqrestore(&dyn_tick->lock, flags);
+       }
+
+       return ret;
+}
+
+static int timer_dyn_tick_disable(void)
+{
+       struct dyn_tick_timer *dyn_tick = system_timer->dyn_tick;
+       unsigned long flags;
+       int ret = -ENODEV;
+
+       if (dyn_tick) {
+               spin_lock_irqsave(&dyn_tick->lock, flags);
+               ret = 0;
+               if (dyn_tick->state & DYN_TICK_ENABLED) {
+                       ret = dyn_tick->disable();
+
+                       if (ret == 0)
+                               dyn_tick->state &= ~DYN_TICK_ENABLED;
+               }
+               spin_unlock_irqrestore(&dyn_tick->lock, flags);
+       }
+
+       return ret;
+}
+
+/*
+ * Reprogram the system timer for at least the calculated time interval.
+ * This function should be called from the idle thread with IRQs disabled,
+ * immediately before sleeping.
+ */
+void timer_dyn_reprogram(void)
+{
+       struct dyn_tick_timer *dyn_tick = system_timer->dyn_tick;
+       unsigned long next, seq, flags;
+
+       if (!dyn_tick)
+               return;
+
+       spin_lock_irqsave(&dyn_tick->lock, flags);
+       if (dyn_tick->state & DYN_TICK_ENABLED) {
+               next = next_timer_interrupt();
+               do {
+                       seq = read_seqbegin(&xtime_lock);
+                       dyn_tick->reprogram(next - jiffies);
+               } while (read_seqretry(&xtime_lock, seq));
+       }
+       spin_unlock_irqrestore(&dyn_tick->lock, flags);
+}
+
+static ssize_t timer_show_dyn_tick(struct sys_device *dev, char *buf)
+{
+       return sprintf(buf, "%i\n",
+                      (system_timer->dyn_tick->state & DYN_TICK_ENABLED) >> 1);
+}
+
+static ssize_t timer_set_dyn_tick(struct sys_device *dev, const char *buf,
+                                 size_t count)
+{
+       unsigned int enable = simple_strtoul(buf, NULL, 2);
+
+       if (enable)
+               timer_dyn_tick_enable();
+       else
+               timer_dyn_tick_disable();
+
+       return count;
+}
+static SYSDEV_ATTR(dyn_tick, 0644, timer_show_dyn_tick, timer_set_dyn_tick);
+
 /*
- * Include architecture specific code
+ * dyntick=enable|disable
  */
-#include <asm/arch/time.h>
+static char dyntick_str[4] __initdata = "";
+
+static int __init dyntick_setup(char *str)
+{
+       if (str)
+               strlcpy(dyntick_str, str, sizeof(dyntick_str));
+       return 1;
+}
+
+__setup("dyntick=", dyntick_setup);
+#endif
+
+static int __init timer_init_sysfs(void)
+{
+       int ret = sysdev_class_register(&timer_sysclass);
+       if (ret == 0) {
+               system_timer->dev.cls = &timer_sysclass;
+               ret = sysdev_register(&system_timer->dev);
+       }
+
+#ifdef CONFIG_NO_IDLE_HZ
+       if (ret == 0 && system_timer->dyn_tick) {
+               ret = sysdev_create_file(&system_timer->dev, &attr_dyn_tick);
+
+               /*
+                * Turn on dynamic tick after calibrate delay
+                * for correct bogomips
+                */
+               if (ret == 0 && dyntick_str[0] == 'e')
+                       ret = timer_dyn_tick_enable();
+       }
+#endif
+
+       return ret;
+}
+
+device_initcall(timer_init_sysfs);
+
+void __init time_init(void)
+{
+#ifndef CONFIG_GENERIC_TIME
+       if (system_timer->offset == NULL)
+               system_timer->offset = dummy_gettimeoffset;
+#endif
+       system_timer->init();
+
+#ifdef CONFIG_NO_IDLE_HZ
+       if (system_timer->dyn_tick)
+               system_timer->dyn_tick->lock = SPIN_LOCK_UNLOCKED;
+#endif
+}
+