linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / arch / powerpc / kernel / time.c
index a124499..86f7e3d 100644 (file)
@@ -32,6 +32,7 @@
  *      2 of the License, or (at your option) any later version.
  */
 
+#include <linux/config.h>
 #include <linux/errno.h>
 #include <linux/module.h>
 #include <linux/sched.h>
@@ -50,7 +51,6 @@
 #include <linux/percpu.h>
 #include <linux/rtc.h>
 #include <linux/jiffies.h>
-#include <linux/posix-timers.h>
 
 #include <asm/io.h>
 #include <asm/processor.h>
@@ -75,6 +75,7 @@
 
 /* keep track of when we need to update the rtc */
 time_t last_rtc_update;
+extern int piranha_simulator;
 #ifdef CONFIG_PPC_ISERIES
 unsigned long iSeries_recal_titan = 0;
 unsigned long iSeries_recal_tb = 0; 
@@ -97,11 +98,10 @@ unsigned long tb_ticks_per_jiffy;
 unsigned long tb_ticks_per_usec = 100; /* sane default */
 EXPORT_SYMBOL(tb_ticks_per_usec);
 unsigned long tb_ticks_per_sec;
-EXPORT_SYMBOL(tb_ticks_per_sec);       /* for cputime_t conversions */
 u64 tb_to_xs;
 unsigned tb_to_us;
 
-#define TICKLEN_SCALE  TICK_LENGTH_SHIFT
+#define TICKLEN_SCALE  (SHIFT_SCALE - 10)
 u64 last_tick_len;     /* units are ns / 2^TICKLEN_SCALE */
 u64 ticklen_to_xs;     /* 0.64 fraction */
 
@@ -125,226 +125,15 @@ static long timezone_offset;
 unsigned long ppc_proc_freq;
 unsigned long ppc_tb_freq;
 
-static u64 tb_last_jiffy __cacheline_aligned_in_smp;
-static DEFINE_PER_CPU(u64, last_jiffy);
+u64 tb_last_jiffy __cacheline_aligned_in_smp;
+unsigned long tb_last_stamp;
 
-#ifdef CONFIG_VIRT_CPU_ACCOUNTING
 /*
- * Factors for converting from cputime_t (timebase ticks) to
- * jiffies, milliseconds, seconds, and clock_t (1/USER_HZ seconds).
- * These are all stored as 0.64 fixed-point binary fractions.
+ * Note that on ppc32 this only stores the bottom 32 bits of
+ * the timebase value, but that's enough to tell when a jiffy
+ * has passed.
  */
-u64 __cputime_jiffies_factor;
-EXPORT_SYMBOL(__cputime_jiffies_factor);
-u64 __cputime_msec_factor;
-EXPORT_SYMBOL(__cputime_msec_factor);
-u64 __cputime_sec_factor;
-EXPORT_SYMBOL(__cputime_sec_factor);
-u64 __cputime_clockt_factor;
-EXPORT_SYMBOL(__cputime_clockt_factor);
-
-static void calc_cputime_factors(void)
-{
-       struct div_result res;
-
-       div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
-       __cputime_jiffies_factor = res.result_low;
-       div128_by_32(1000, 0, tb_ticks_per_sec, &res);
-       __cputime_msec_factor = res.result_low;
-       div128_by_32(1, 0, tb_ticks_per_sec, &res);
-       __cputime_sec_factor = res.result_low;
-       div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
-       __cputime_clockt_factor = res.result_low;
-}
-
-/*
- * Read the PURR on systems that have it, otherwise the timebase.
- */
-static u64 read_purr(void)
-{
-       if (cpu_has_feature(CPU_FTR_PURR))
-               return mfspr(SPRN_PURR);
-       return mftb();
-}
-
-/*
- * Account time for a transition between system, hard irq
- * or soft irq state.
- */
-void account_system_vtime(struct task_struct *tsk)
-{
-       u64 now, delta;
-       unsigned long flags;
-
-       local_irq_save(flags);
-       now = read_purr();
-       delta = now - get_paca()->startpurr;
-       get_paca()->startpurr = now;
-       if (!in_interrupt()) {
-               delta += get_paca()->system_time;
-               get_paca()->system_time = 0;
-       }
-       account_system_time(tsk, 0, delta);
-       local_irq_restore(flags);
-}
-
-/*
- * Transfer the user and system times accumulated in the paca
- * by the exception entry and exit code to the generic process
- * user and system time records.
- * Must be called with interrupts disabled.
- */
-void account_process_vtime(struct task_struct *tsk)
-{
-       cputime_t utime;
-
-       utime = get_paca()->user_time;
-       get_paca()->user_time = 0;
-       account_user_time(tsk, utime);
-}
-
-static void account_process_time(struct pt_regs *regs)
-{
-       int cpu = smp_processor_id();
-
-       account_process_vtime(current);
-       run_local_timers();
-       if (rcu_pending(cpu))
-               rcu_check_callbacks(cpu, user_mode(regs));
-       scheduler_tick();
-       run_posix_cpu_timers(current);
-}
-
-#ifdef CONFIG_PPC_SPLPAR
-/*
- * Stuff for accounting stolen time.
- */
-struct cpu_purr_data {
-       int     initialized;                    /* thread is running */
-       u64     tb0;                    /* timebase at origin time */
-       u64     purr0;                  /* PURR at origin time */
-       u64     tb;                     /* last TB value read */
-       u64     purr;                   /* last PURR value read */
-       u64     stolen;                 /* stolen time so far */
-       spinlock_t lock;
-};
-
-static DEFINE_PER_CPU(struct cpu_purr_data, cpu_purr_data);
-
-static void snapshot_tb_and_purr(void *data)
-{
-       struct cpu_purr_data *p = &__get_cpu_var(cpu_purr_data);
-
-       p->tb0 = mftb();
-       p->purr0 = mfspr(SPRN_PURR);
-       p->tb = p->tb0;
-       p->purr = 0;
-       wmb();
-       p->initialized = 1;
-}
-
-/*
- * Called during boot when all cpus have come up.
- */
-void snapshot_timebases(void)
-{
-       int cpu;
-
-       if (!cpu_has_feature(CPU_FTR_PURR))
-               return;
-       for_each_possible_cpu(cpu)
-               spin_lock_init(&per_cpu(cpu_purr_data, cpu).lock);
-       on_each_cpu(snapshot_tb_and_purr, NULL, 0, 1);
-}
-
-void calculate_steal_time(void)
-{
-       u64 tb, purr, t0;
-       s64 stolen;
-       struct cpu_purr_data *p0, *pme, *phim;
-       int cpu;
-
-       if (!cpu_has_feature(CPU_FTR_PURR))
-               return;
-       cpu = smp_processor_id();
-       pme = &per_cpu(cpu_purr_data, cpu);
-       if (!pme->initialized)
-               return;         /* this can happen in early boot */
-       p0 = &per_cpu(cpu_purr_data, cpu & ~1);
-       phim = &per_cpu(cpu_purr_data, cpu ^ 1);
-       spin_lock(&p0->lock);
-       tb = mftb();
-       purr = mfspr(SPRN_PURR) - pme->purr0;
-       if (!phim->initialized || !cpu_online(cpu ^ 1)) {
-               stolen = (tb - pme->tb) - (purr - pme->purr);
-       } else {
-               t0 = pme->tb0;
-               if (phim->tb0 < t0)
-                       t0 = phim->tb0;
-               stolen = phim->tb - t0 - phim->purr - purr - p0->stolen;
-       }
-       if (stolen > 0) {
-               account_steal_time(current, stolen);
-               p0->stolen += stolen;
-       }
-       pme->tb = tb;
-       pme->purr = purr;
-       spin_unlock(&p0->lock);
-}
-
-/*
- * Must be called before the cpu is added to the online map when
- * a cpu is being brought up at runtime.
- */
-static void snapshot_purr(void)
-{
-       int cpu;
-       u64 purr;
-       struct cpu_purr_data *p0, *pme, *phim;
-       unsigned long flags;
-
-       if (!cpu_has_feature(CPU_FTR_PURR))
-               return;
-       cpu = smp_processor_id();
-       pme = &per_cpu(cpu_purr_data, cpu);
-       p0 = &per_cpu(cpu_purr_data, cpu & ~1);
-       phim = &per_cpu(cpu_purr_data, cpu ^ 1);
-       spin_lock_irqsave(&p0->lock, flags);
-       pme->tb = pme->tb0 = mftb();
-       purr = mfspr(SPRN_PURR);
-       if (!phim->initialized) {
-               pme->purr = 0;
-               pme->purr0 = purr;
-       } else {
-               /* set p->purr and p->purr0 for no change in p0->stolen */
-               pme->purr = phim->tb - phim->tb0 - phim->purr - p0->stolen;
-               pme->purr0 = purr - pme->purr;
-       }
-       pme->initialized = 1;
-       spin_unlock_irqrestore(&p0->lock, flags);
-}
-
-#endif /* CONFIG_PPC_SPLPAR */
-
-#else /* ! CONFIG_VIRT_CPU_ACCOUNTING */
-#define calc_cputime_factors()
-#define account_process_time(regs)     update_process_times(user_mode(regs))
-#define calculate_steal_time()         do { } while (0)
-#endif
-
-#if !(defined(CONFIG_VIRT_CPU_ACCOUNTING) && defined(CONFIG_PPC_SPLPAR))
-#define snapshot_purr()                        do { } while (0)
-#endif
-
-/*
- * Called when a cpu comes up after the system has finished booting,
- * i.e. as a result of a hotplug cpu action.
- */
-void snapshot_timebase(void)
-{
-       __get_cpu_var(last_jiffy) = get_tb();
-       snapshot_purr();
-}
+DEFINE_PER_CPU(unsigned long, last_jiffy);
 
 void __delay(unsigned long loops)
 {
@@ -410,7 +199,7 @@ static __inline__ void timer_check_rtc(void)
 /*
  * This version of gettimeofday has microsecond resolution.
  */
-static inline void __do_gettimeofday(struct timeval *tv)
+static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
 {
        unsigned long sec, usec;
        u64 tb_ticks, xsec;
@@ -424,12 +213,7 @@ static inline void __do_gettimeofday(struct timeval *tv)
         * without a divide (and in fact, without a multiply)
         */
        temp_varp = do_gtod.varp;
-
-       /* Sampling the time base must be done after loading
-        * do_gtod.varp in order to avoid racing with update_gtod.
-        */
-       data_barrier(temp_varp);
-       tb_ticks = get_tb() - temp_varp->tb_orig_stamp;
+       tb_ticks = tb_val - temp_varp->tb_orig_stamp;
        temp_tb_to_xs = temp_varp->tb_to_xs;
        temp_stamp_xsec = temp_varp->stamp_xsec;
        xsec = temp_stamp_xsec + mulhdu(tb_ticks, temp_tb_to_xs);
@@ -451,7 +235,7 @@ void do_gettimeofday(struct timeval *tv)
                do {
                        seq = read_seqbegin_irqsave(&xtime_lock, flags);
                        sec = xtime.tv_sec;
-                       nsec = xtime.tv_nsec + tb_ticks_since(tb_last_jiffy);
+                       nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp);
                } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
                usec = nsec / 1000;
                while (usec >= 1000000) {
@@ -462,7 +246,7 @@ void do_gettimeofday(struct timeval *tv)
                tv->tv_usec = usec;
                return;
        }
-       __do_gettimeofday(tv);
+       __do_gettimeofday(tv, get_tb());
 }
 
 EXPORT_SYMBOL(do_gettimeofday);
@@ -608,7 +392,6 @@ static void iSeries_tb_recal(void)
                                                new_tb_ticks_per_jiffy, sign, tick_diff );
                                tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
                                tb_ticks_per_sec   = new_tb_ticks_per_sec;
-                               calc_cputime_factors();
                                div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
                                do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
                                tb_to_xs = divres.result_low;
@@ -648,7 +431,6 @@ void timer_interrupt(struct pt_regs * regs)
        int next_dec;
        int cpu = smp_processor_id();
        unsigned long ticks;
-       u64 tb_next_jiffy;
 
 #ifdef CONFIG_PPC32
        if (atomic_read(&ppc_n_lost_interrupts) != 0)
@@ -658,7 +440,6 @@ void timer_interrupt(struct pt_regs * regs)
        irq_enter();
 
        profile_tick(CPU_PROFILING, regs);
-       calculate_steal_time();
 
 #ifdef CONFIG_PPC_ISERIES
        get_lppaca()->int_dword.fields.decr_int = 0;
@@ -680,7 +461,7 @@ void timer_interrupt(struct pt_regs * regs)
                 * is the case.
                 */
                if (!cpu_is_offline(cpu))
-                       account_process_time(regs);
+                       update_process_times(user_mode(regs));
 
                /*
                 * No need to check whether cpu is offline here; boot_cpuid
@@ -690,13 +471,11 @@ void timer_interrupt(struct pt_regs * regs)
                        continue;
 
                write_seqlock(&xtime_lock);
-               tb_next_jiffy = tb_last_jiffy + tb_ticks_per_jiffy;
-               if (per_cpu(last_jiffy, cpu) >= tb_next_jiffy) {
-                       tb_last_jiffy = tb_next_jiffy;
-                       do_timer(regs);
-                       timer_recalc_offset(tb_last_jiffy);
-                       timer_check_rtc();
-               }
+               tb_last_jiffy += tb_ticks_per_jiffy;
+               tb_last_stamp = per_cpu(last_jiffy, cpu);
+               do_timer(regs);
+               timer_recalc_offset(tb_last_jiffy);
+               timer_check_rtc();
                write_sequnlock(&xtime_lock);
        }
        
@@ -739,27 +518,13 @@ void wakeup_decrementer(void)
 void __init smp_space_timers(unsigned int max_cpus)
 {
        int i;
-       unsigned long half = tb_ticks_per_jiffy / 2;
        unsigned long offset = tb_ticks_per_jiffy / max_cpus;
-       u64 previous_tb = per_cpu(last_jiffy, boot_cpuid);
+       unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid);
 
        /* make sure tb > per_cpu(last_jiffy, cpu) for all cpus always */
        previous_tb -= tb_ticks_per_jiffy;
-       /*
-        * The stolen time calculation for POWER5 shared-processor LPAR
-        * systems works better if the two threads' timebase interrupts
-        * are staggered by half a jiffy with respect to each other.
-        */
-       for_each_possible_cpu(i) {
-               if (i == boot_cpuid)
-                       continue;
-               if (i == (boot_cpuid ^ 1))
-                       per_cpu(last_jiffy, i) =
-                               per_cpu(last_jiffy, boot_cpuid) - half;
-               else if (i & 1)
-                       per_cpu(last_jiffy, i) =
-                               per_cpu(last_jiffy, i ^ 1) + half;
-               else {
+       for_each_cpu(i) {
+               if (i != boot_cpuid) {
                        previous_tb += offset;
                        per_cpu(last_jiffy, i) = previous_tb;
                }
@@ -822,7 +587,7 @@ int do_settimeofday(struct timespec *tv)
         * and therefore the (jiffies - wall_jiffies) computation
         * has been removed.
         */
-       tb_delta = tb_ticks_since(tb_last_jiffy);
+       tb_delta = tb_ticks_since(tb_last_stamp);
        tb_delta = mulhdu(tb_delta, do_gtod.varp->tb_to_xs); /* in xsec */
        new_nsec -= SCALE_XSEC(tb_delta, 1000000000);
 
@@ -857,50 +622,42 @@ int do_settimeofday(struct timespec *tv)
 
 EXPORT_SYMBOL(do_settimeofday);
 
-static int __init get_freq(char *name, int cells, unsigned long *val)
+void __init generic_calibrate_decr(void)
 {
        struct device_node *cpu;
        unsigned int *fp;
-       int found = 0;
+       int node_found;
 
-       /* The cpu node should have timebase and clock frequency properties */
+       /*
+        * The cpu node should have a timebase-frequency property
+        * to tell us the rate at which the decrementer counts.
+        */
        cpu = of_find_node_by_type(NULL, "cpu");
 
+       ppc_tb_freq = DEFAULT_TB_FREQ;          /* hardcoded default */
+       node_found = 0;
        if (cpu) {
-               fp = (unsigned int *)get_property(cpu, name, NULL);
+               fp = (unsigned int *)get_property(cpu, "timebase-frequency",
+                                                 NULL);
                if (fp) {
-                       found = 1;
-                       *val = 0;
-                       while (cells--)
-                               *val = (*val << 32) | *fp++;
+                       node_found = 1;
+                       ppc_tb_freq = *fp;
                }
-
-               of_node_put(cpu);
        }
-
-       return found;
-}
-
-void __init generic_calibrate_decr(void)
-{
-       ppc_tb_freq = DEFAULT_TB_FREQ;          /* hardcoded default */
-
-       if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
-           !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
-
+       if (!node_found)
                printk(KERN_ERR "WARNING: Estimating decrementer frequency "
                                "(not found)\n");
-       }
 
-       ppc_proc_freq = DEFAULT_PROC_FREQ;      /* hardcoded default */
-
-       if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
-           !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
-
-               printk(KERN_ERR "WARNING: Estimating processor frequency "
-                               "(not found)\n");
+       ppc_proc_freq = DEFAULT_PROC_FREQ;
+       node_found = 0;
+       if (cpu) {
+               fp = (unsigned int *)get_property(cpu, "clock-frequency",
+                                                 NULL);
+               if (fp) {
+                       node_found = 1;
+                       ppc_proc_freq = *fp;
+               }
        }
-
 #ifdef CONFIG_BOOKE
        /* Set the time base to zero */
        mtspr(SPRN_TBWL, 0);
@@ -912,6 +669,11 @@ void __init generic_calibrate_decr(void)
        /* Enable decrementer interrupt */
        mtspr(SPRN_TCR, TCR_DIE);
 #endif
+       if (!node_found)
+               printk(KERN_ERR "WARNING: Estimating processor frequency "
+                               "(not found)\n");
+
+       of_node_put(cpu);
 }
 
 unsigned long get_boot_time(void)
@@ -942,22 +704,22 @@ void __init time_init(void)
        if (__USE_RTC()) {
                /* 601 processor: dec counts down by 128 every 128ns */
                ppc_tb_freq = 1000000000;
-               tb_last_jiffy = get_rtcl();
+               tb_last_stamp = get_rtcl();
+               tb_last_jiffy = tb_last_stamp;
        } else {
                /* Normal PowerPC with timebase register */
                ppc_md.calibrate_decr();
-               printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
+               printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
                       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
-               printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
+               printk(KERN_INFO "time_init: processor frequency   = %lu.%.6lu MHz\n",
                       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
-               tb_last_jiffy = get_tb();
+               tb_last_stamp = tb_last_jiffy = get_tb();
        }
 
        tb_ticks_per_jiffy = ppc_tb_freq / HZ;
        tb_ticks_per_sec = ppc_tb_freq;
        tb_ticks_per_usec = ppc_tb_freq / 1000000;
        tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
-       calc_cputime_factors();
 
        /*
         * Calculate the length of each tick in ns.  It will not be
@@ -1011,7 +773,10 @@ void __init time_init(void)
        tb_to_ns_scale = scale;
        tb_to_ns_shift = shift;
 
-       tm = get_boot_time();
+#ifdef CONFIG_PPC_ISERIES
+       if (!piranha_simulator)
+#endif
+               tm = get_boot_time();
 
        write_seqlock_irqsave(&xtime_lock, flags);
 
@@ -1027,7 +792,7 @@ void __init time_init(void)
        do_gtod.varp = &do_gtod.vars[0];
        do_gtod.var_idx = 0;
        do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
-       __get_cpu_var(last_jiffy) = tb_last_jiffy;
+       __get_cpu_var(last_jiffy) = tb_last_stamp;
        do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
        do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
        do_gtod.varp->tb_to_xs = tb_to_xs;