[POWERPC] Disable broken PPC_PTRACE_GETFPREGS on 32 bits
[safe/jmp/linux-2.6] / arch / powerpc / kernel / time.c
index 1886045..2c8564d 100644 (file)
@@ -32,7 +32,6 @@
  *      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,6 +49,9 @@
 #include <linux/security.h>
 #include <linux/percpu.h>
 #include <linux/rtc.h>
+#include <linux/jiffies.h>
+#include <linux/posix-timers.h>
+#include <linux/irq.h>
 
 #include <asm/io.h>
 #include <asm/processor.h>
@@ -74,7 +76,6 @@
 
 /* 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,9 +98,18 @@ 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;
-unsigned long processor_freq;
+
+#define TICKLEN_SCALE  TICK_LENGTH_SHIFT
+u64 last_tick_len;     /* units are ns / 2^TICKLEN_SCALE */
+u64 ticklen_to_xs;     /* 0.64 fraction */
+
+/* If last_tick_len corresponds to about 1/HZ seconds, then
+   last_tick_len << TICKLEN_SHIFT will be about 2^63. */
+#define TICKLEN_SHIFT  (63 - 30 - TICKLEN_SCALE + SHIFT_HZ)
+
 DEFINE_SPINLOCK(rtc_lock);
 EXPORT_SYMBOL_GPL(rtc_lock);
 
@@ -108,27 +118,201 @@ unsigned tb_to_ns_shift;
 
 struct gettimeofday_struct do_gtod;
 
-extern unsigned long wall_jiffies;
-
 extern struct timezone sys_tz;
 static long timezone_offset;
 
-void ppc_adjtimex(void);
-
-static unsigned adjusting_time = 0;
-
 unsigned long ppc_proc_freq;
 unsigned long ppc_tb_freq;
 
-u64 tb_last_jiffy __cacheline_aligned_in_smp;
-unsigned long tb_last_stamp;
+static u64 tb_last_jiffy __cacheline_aligned_in_smp;
+static DEFINE_PER_CPU(u64, last_jiffy);
+
+#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.
+ */
+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     tb;                     /* last TB value read */
+       u64     purr;                   /* last PURR value read */
+       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->tb = mftb();
+       p->purr = mfspr(SPRN_PURR);
+       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;
+       s64 stolen;
+       struct cpu_purr_data *pme;
+
+       if (!cpu_has_feature(CPU_FTR_PURR))
+               return;
+       pme = &per_cpu(cpu_purr_data, smp_processor_id());
+       if (!pme->initialized)
+               return;         /* this can happen in early boot */
+       spin_lock(&pme->lock);
+       tb = mftb();
+       purr = mfspr(SPRN_PURR);
+       stolen = (tb - pme->tb) - (purr - pme->purr);
+       if (stolen > 0)
+               account_steal_time(current, stolen);
+       pme->tb = tb;
+       pme->purr = purr;
+       spin_unlock(&pme->lock);
+}
 
 /*
- * 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.
+ * Must be called before the cpu is added to the online map when
+ * a cpu is being brought up at runtime.
  */
-DEFINE_PER_CPU(unsigned long, last_jiffy);
+static void snapshot_purr(void)
+{
+       struct cpu_purr_data *pme;
+       unsigned long flags;
+
+       if (!cpu_has_feature(CPU_FTR_PURR))
+               return;
+       pme = &per_cpu(cpu_purr_data, smp_processor_id());
+       spin_lock_irqsave(&pme->lock, flags);
+       pme->tb = mftb();
+       pme->purr = mfspr(SPRN_PURR);
+       pme->initialized = 1;
+       spin_unlock_irqrestore(&pme->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();
+}
 
 void __delay(unsigned long loops)
 {
@@ -178,8 +362,7 @@ static __inline__ void timer_check_rtc(void)
          */
         if (ppc_md.set_rtc_time && ntp_synced() &&
            xtime.tv_sec - last_rtc_update >= 659 &&
-           abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ &&
-           jiffies - wall_jiffies == 1) {
+           abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ) {
                struct rtc_time tm;
                to_tm(xtime.tv_sec + 1 + timezone_offset, &tm);
                tm.tm_year -= 1900;
@@ -195,7 +378,7 @@ static __inline__ void timer_check_rtc(void)
 /*
  * This version of gettimeofday has microsecond resolution.
  */
-static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
+static inline void __do_gettimeofday(struct timeval *tv)
 {
        unsigned long sec, usec;
        u64 tb_ticks, xsec;
@@ -209,7 +392,12 @@ static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
         * without a divide (and in fact, without a multiply)
         */
        temp_varp = do_gtod.varp;
-       tb_ticks = tb_val - temp_varp->tb_orig_stamp;
+
+       /* 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;
        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);
@@ -226,15 +414,14 @@ void do_gettimeofday(struct timeval *tv)
        if (__USE_RTC()) {
                /* do this the old way */
                unsigned long flags, seq;
-               unsigned int sec, nsec, usec, lost;
+               unsigned int sec, nsec, usec;
 
                do {
                        seq = read_seqbegin_irqsave(&xtime_lock, flags);
                        sec = xtime.tv_sec;
-                       nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp);
-                       lost = jiffies - wall_jiffies;
+                       nsec = xtime.tv_nsec + tb_ticks_since(tb_last_jiffy);
                } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
-               usec = nsec / 1000 + lost * (1000000 / HZ);
+               usec = nsec / 1000;
                while (usec >= 1000000) {
                        usec -= 1000000;
                        ++sec;
@@ -243,28 +430,11 @@ void do_gettimeofday(struct timeval *tv)
                tv->tv_usec = usec;
                return;
        }
-       __do_gettimeofday(tv, get_tb());
+       __do_gettimeofday(tv);
 }
 
 EXPORT_SYMBOL(do_gettimeofday);
 
-/* Synchronize xtime with do_gettimeofday */ 
-
-static inline void timer_sync_xtime(unsigned long cur_tb)
-{
-#ifdef CONFIG_PPC64
-       /* why do we do this? */
-       struct timeval my_tv;
-
-       __do_gettimeofday(&my_tv, cur_tb);
-
-       if (xtime.tv_sec <= my_tv.tv_sec) {
-               xtime.tv_sec = my_tv.tv_sec;
-               xtime.tv_nsec = my_tv.tv_usec * 1000;
-       }
-#endif
-}
-
 /*
  * There are two copies of tb_to_xs and stamp_xsec so that no
  * lock is needed to access and use these values in
@@ -297,9 +467,9 @@ static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
         * the two values of tb_update_count match and are even then the
         * tb_to_xs and stamp_xsec values are consistent.  If not, then it
         * loops back and reads them again until this criteria is met.
+        * We expect the caller to have done the first increment of
+        * vdso_data->tb_update_count already.
         */
-       ++(vdso_data->tb_update_count);
-       smp_wmb();
        vdso_data->tb_orig_stamp = new_tb_stamp;
        vdso_data->stamp_xsec = new_stamp_xsec;
        vdso_data->tb_to_xs = new_tb_to_xs;
@@ -323,15 +493,40 @@ static __inline__ void timer_recalc_offset(u64 cur_tb)
 {
        unsigned long offset;
        u64 new_stamp_xsec;
+       u64 tlen, t2x;
+       u64 tb, xsec_old, xsec_new;
+       struct gettimeofday_vars *varp;
 
        if (__USE_RTC())
                return;
+       tlen = current_tick_length();
        offset = cur_tb - do_gtod.varp->tb_orig_stamp;
-       if ((offset & 0x80000000u) == 0)
+       if (tlen == last_tick_len && offset < 0x80000000u)
                return;
-       new_stamp_xsec = do_gtod.varp->stamp_xsec
-               + mulhdu(offset, do_gtod.varp->tb_to_xs);
-       update_gtod(cur_tb, new_stamp_xsec, do_gtod.varp->tb_to_xs);
+       if (tlen != last_tick_len) {
+               t2x = mulhdu(tlen << TICKLEN_SHIFT, ticklen_to_xs);
+               last_tick_len = tlen;
+       } else
+               t2x = do_gtod.varp->tb_to_xs;
+       new_stamp_xsec = (u64) xtime.tv_nsec * XSEC_PER_SEC;
+       do_div(new_stamp_xsec, 1000000000);
+       new_stamp_xsec += (u64) xtime.tv_sec * XSEC_PER_SEC;
+
+       ++vdso_data->tb_update_count;
+       smp_mb();
+
+       /*
+        * Make sure time doesn't go backwards for userspace gettimeofday.
+        */
+       tb = get_tb();
+       varp = do_gtod.varp;
+       xsec_old = mulhdu(tb - varp->tb_orig_stamp, varp->tb_to_xs)
+               + varp->stamp_xsec;
+       xsec_new = mulhdu(tb - cur_tb, t2x) + new_stamp_xsec;
+       if (xsec_new < xsec_old)
+               new_stamp_xsec += xsec_old - xsec_new;
+
+       update_gtod(cur_tb, new_stamp_xsec, t2x);
 }
 
 #ifdef CONFIG_SMP
@@ -381,6 +576,7 @@ 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;
@@ -417,21 +613,26 @@ static void iSeries_tb_recal(void)
  */
 void timer_interrupt(struct pt_regs * regs)
 {
+       struct pt_regs *old_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)
                do_IRQ(regs);
 #endif
 
+       old_regs = set_irq_regs(regs);
        irq_enter();
 
-       profile_tick(CPU_PROFILING, regs);
+       profile_tick(CPU_PROFILING);
+       calculate_steal_time();
 
 #ifdef CONFIG_PPC_ISERIES
-       get_lppaca()->int_dword.fields.decr_int = 0;
+       if (firmware_has_feature(FW_FEATURE_ISERIES))
+               get_lppaca()->int_dword.fields.decr_int = 0;
 #endif
 
        while ((ticks = tb_ticks_since(per_cpu(last_jiffy, cpu)))
@@ -450,7 +651,7 @@ void timer_interrupt(struct pt_regs * regs)
                 * is the case.
                 */
                if (!cpu_is_offline(cpu))
-                       update_process_times(user_mode(regs));
+                       account_process_time(regs);
 
                /*
                 * No need to check whether cpu is offline here; boot_cpuid
@@ -460,23 +661,22 @@ void timer_interrupt(struct pt_regs * regs)
                        continue;
 
                write_seqlock(&xtime_lock);
-               tb_last_jiffy += tb_ticks_per_jiffy;
-               tb_last_stamp = per_cpu(last_jiffy, cpu);
-               timer_recalc_offset(tb_last_jiffy);
-               do_timer(regs);
-               timer_sync_xtime(tb_last_jiffy);
-               timer_check_rtc();
+               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(1);
+                       timer_recalc_offset(tb_last_jiffy);
+                       timer_check_rtc();
+               }
                write_sequnlock(&xtime_lock);
-               if (adjusting_time && (time_adjust == 0))
-                       ppc_adjtimex();
        }
        
        next_dec = tb_ticks_per_jiffy - ticks;
        set_dec(next_dec);
 
 #ifdef CONFIG_PPC_ISERIES
-       if (hvlpevent_is_pending())
-               process_hvlpevents(regs);
+       if (firmware_has_feature(FW_FEATURE_ISERIES) && hvlpevent_is_pending())
+               process_hvlpevents();
 #endif
 
 #ifdef CONFIG_PPC64
@@ -488,36 +688,38 @@ void timer_interrupt(struct pt_regs * regs)
 #endif
 
        irq_exit();
+       set_irq_regs(old_regs);
 }
 
 void wakeup_decrementer(void)
 {
-       int i;
+       unsigned long ticks;
 
-       set_dec(tb_ticks_per_jiffy);
        /*
-        * We don't expect this to be called on a machine with a 601,
-        * so using get_tbl is fine.
+        * The timebase gets saved on sleep and restored on wakeup,
+        * so all we need to do is to reset the decrementer.
         */
-       tb_last_stamp = tb_last_jiffy = get_tb();
-       for_each_cpu(i)
-               per_cpu(last_jiffy, i) = tb_last_stamp;
+       ticks = tb_ticks_since(__get_cpu_var(last_jiffy));
+       if (ticks < tb_ticks_per_jiffy)
+               ticks = tb_ticks_per_jiffy - ticks;
+       else
+               ticks = 1;
+       set_dec(ticks);
 }
 
 #ifdef CONFIG_SMP
 void __init smp_space_timers(unsigned int max_cpus)
 {
        int i;
-       unsigned long offset = tb_ticks_per_jiffy / max_cpus;
-       unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid);
+       u64 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;
-       for_each_cpu(i) {
-               if (i != boot_cpuid) {
-                       previous_tb += offset;
-                       per_cpu(last_jiffy, i) = previous_tb;
-               }
+
+       for_each_possible_cpu(i) {
+               if (i == boot_cpuid)
+                       continue;
+               per_cpu(last_jiffy, i) = previous_tb;
        }
 }
 #endif
@@ -541,8 +743,8 @@ int do_settimeofday(struct timespec *tv)
        time_t wtm_sec, new_sec = tv->tv_sec;
        long wtm_nsec, new_nsec = tv->tv_nsec;
        unsigned long flags;
-       long int tb_delta;
-       u64 new_xsec, tb_delta_xs;
+       u64 new_xsec;
+       unsigned long tb_delta;
 
        if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
                return -EINVAL;
@@ -558,14 +760,23 @@ int do_settimeofday(struct timespec *tv)
         * settimeofday to perform this operation.
         */
 #ifdef CONFIG_PPC_ISERIES
-       if (first_settimeofday) {
+       if (firmware_has_feature(FW_FEATURE_ISERIES) && first_settimeofday) {
                iSeries_tb_recal();
                first_settimeofday = 0;
        }
 #endif
-       tb_delta = tb_ticks_since(tb_last_stamp);
-       tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
-       tb_delta_xs = mulhdu(tb_delta, do_gtod.varp->tb_to_xs);
+
+       /* Make userspace gettimeofday spin until we're done. */
+       ++vdso_data->tb_update_count;
+       smp_mb();
+
+       /*
+        * Subtract off the number of nanoseconds since the
+        * beginning of the last tick.
+        */
+       tb_delta = tb_ticks_since(tb_last_jiffy);
+       tb_delta = mulhdu(tb_delta, do_gtod.varp->tb_to_xs); /* in xsec */
+       new_nsec -= SCALE_XSEC(tb_delta, 1000000000);
 
        wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
        wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
@@ -580,12 +791,12 @@ int do_settimeofday(struct timespec *tv)
 
        ntp_clear();
 
-       new_xsec = 0;
-       if (new_nsec != 0) {
-               new_xsec = (u64)new_nsec * XSEC_PER_SEC;
+       new_xsec = xtime.tv_nsec;
+       if (new_xsec != 0) {
+               new_xsec *= XSEC_PER_SEC;
                do_div(new_xsec, NSEC_PER_SEC);
        }
-       new_xsec += (u64)new_sec * XSEC_PER_SEC - tb_delta_xs;
+       new_xsec += (u64)xtime.tv_sec * XSEC_PER_SEC;
        update_gtod(tb_last_jiffy, new_xsec, do_gtod.varp->tb_to_xs);
 
        vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
@@ -598,42 +809,48 @@ int do_settimeofday(struct timespec *tv)
 
 EXPORT_SYMBOL(do_settimeofday);
 
-void __init generic_calibrate_decr(void)
+static int __init get_freq(char *name, int cells, unsigned long *val)
 {
        struct device_node *cpu;
-       unsigned int *fp;
-       int node_found;
+       const unsigned int *fp;
+       int found = 0;
 
-       /*
-        * The cpu node should have a timebase-frequency property
-        * to tell us the rate at which the decrementer counts.
-        */
+       /* The cpu node should have timebase and clock frequency properties */
        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, "timebase-frequency",
-                                                 NULL);
+               fp = of_get_property(cpu, name, NULL);
                if (fp) {
-                       node_found = 1;
-                       ppc_tb_freq = *fp;
+                       found = 1;
+                       *val = of_read_ulong(fp, cells);
                }
+
+               of_node_put(cpu);
        }
-       if (!node_found)
+
+       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)) {
+
                printk(KERN_ERR "WARNING: Estimating decrementer 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;
-               }
+       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");
        }
+
 #ifdef CONFIG_BOOKE
        /* Set the time base to zero */
        mtspr(SPRN_TBWL, 0);
@@ -645,11 +862,6 @@ 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)
@@ -671,7 +883,7 @@ void __init time_init(void)
        unsigned long flags;
        unsigned long tm = 0;
        struct div_result res;
-       u64 scale;
+       u64 scale, x;
        unsigned shift;
 
         if (ppc_md.time_init != NULL)
@@ -680,24 +892,55 @@ void __init time_init(void)
        if (__USE_RTC()) {
                /* 601 processor: dec counts down by 128 every 128ns */
                ppc_tb_freq = 1000000000;
-               tb_last_stamp = get_rtcl();
-               tb_last_jiffy = tb_last_stamp;
+               tb_last_jiffy = get_rtcl();
        } else {
                /* Normal PowerPC with timebase register */
                ppc_md.calibrate_decr();
-               printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
+               printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
                       ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
-               printk(KERN_INFO "time_init: processor frequency   = %lu.%.6lu MHz\n",
+               printk(KERN_DEBUG "time_init: processor frequency   = %lu.%.6lu MHz\n",
                       ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
-               tb_last_stamp = tb_last_jiffy = get_tb();
+               tb_last_jiffy = get_tb();
        }
 
        tb_ticks_per_jiffy = ppc_tb_freq / HZ;
-       tb_ticks_per_sec = tb_ticks_per_jiffy * 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);
-       div128_by_32(1024*1024, 0, tb_ticks_per_sec, &res);
-       tb_to_xs = res.result_low;
+       calc_cputime_factors();
+
+       /*
+        * Calculate the length of each tick in ns.  It will not be
+        * exactly 1e9/HZ unless ppc_tb_freq is divisible by HZ.
+        * We compute 1e9 * tb_ticks_per_jiffy / ppc_tb_freq,
+        * rounded up.
+        */
+       x = (u64) NSEC_PER_SEC * tb_ticks_per_jiffy + ppc_tb_freq - 1;
+       do_div(x, ppc_tb_freq);
+       tick_nsec = x;
+       last_tick_len = x << TICKLEN_SCALE;
+
+       /*
+        * Compute ticklen_to_xs, which is a factor which gets multiplied
+        * by (last_tick_len << TICKLEN_SHIFT) to get a tb_to_xs value.
+        * It is computed as:
+        * ticklen_to_xs = 2^N / (tb_ticks_per_jiffy * 1e9)
+        * where N = 64 + 20 - TICKLEN_SCALE - TICKLEN_SHIFT
+        * which turns out to be N = 51 - SHIFT_HZ.
+        * This gives the result as a 0.64 fixed-point fraction.
+        * That value is reduced by an offset amounting to 1 xsec per
+        * 2^31 timebase ticks to avoid problems with time going backwards
+        * by 1 xsec when we do timer_recalc_offset due to losing the
+        * fractional xsec.  That offset is equal to ppc_tb_freq/2^51
+        * since there are 2^20 xsec in a second.
+        */
+       div128_by_32((1ULL << 51) - ppc_tb_freq, 0,
+                    tb_ticks_per_jiffy << SHIFT_HZ, &res);
+       div128_by_32(res.result_high, res.result_low, NSEC_PER_SEC, &res);
+       ticklen_to_xs = res.result_low;
+
+       /* Compute tb_to_xs from tick_nsec */
+       tb_to_xs = mulhdu(last_tick_len << TICKLEN_SHIFT, ticklen_to_xs);
 
        /*
         * Compute scale factor for sched_clock.
@@ -718,18 +961,23 @@ void __init time_init(void)
        tb_to_ns_scale = scale;
        tb_to_ns_shift = shift;
 
-#ifdef CONFIG_PPC_ISERIES
-       if (!piranha_simulator)
-#endif
-               tm = get_boot_time();
+       tm = get_boot_time();
 
        write_seqlock_irqsave(&xtime_lock, flags);
+
+       /* If platform provided a timezone (pmac), we correct the time */
+        if (timezone_offset) {
+               sys_tz.tz_minuteswest = -timezone_offset / 60;
+               sys_tz.tz_dsttime = 0;
+               tm -= timezone_offset;
+        }
+
        xtime.tv_sec = tm;
        xtime.tv_nsec = 0;
        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_stamp;
+       __get_cpu_var(last_jiffy) = tb_last_jiffy;
        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;
@@ -738,18 +986,11 @@ void __init time_init(void)
        vdso_data->tb_orig_stamp = tb_last_jiffy;
        vdso_data->tb_update_count = 0;
        vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
-       vdso_data->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
+       vdso_data->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
        vdso_data->tb_to_xs = tb_to_xs;
 
        time_freq = 0;
 
-       /* If platform provided a timezone (pmac), we correct the time */
-        if (timezone_offset) {
-               sys_tz.tz_minuteswest = -timezone_offset / 60;
-               sys_tz.tz_dsttime = 0;
-               xtime.tv_sec -= timezone_offset;
-        }
-
        last_rtc_update = xtime.tv_sec;
        set_normalized_timespec(&wall_to_monotonic,
                                -xtime.tv_sec, -xtime.tv_nsec);
@@ -759,126 +1000,6 @@ void __init time_init(void)
        set_dec(tb_ticks_per_jiffy);
 }
 
-/* 
- * After adjtimex is called, adjust the conversion of tb ticks
- * to microseconds to keep do_gettimeofday synchronized 
- * with ntpd.
- *
- * Use the time_adjust, time_freq and time_offset computed by adjtimex to 
- * adjust the frequency.
- */
-
-/* #define DEBUG_PPC_ADJTIMEX 1 */
-
-void ppc_adjtimex(void)
-{
-#ifdef CONFIG_PPC64
-       unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec,
-               new_tb_to_xs, new_xsec, new_stamp_xsec;
-       unsigned long tb_ticks_per_sec_delta;
-       long delta_freq, ltemp;
-       struct div_result divres; 
-       unsigned long flags;
-       long singleshot_ppm = 0;
-
-       /*
-        * Compute parts per million frequency adjustment to
-        * accomplish the time adjustment implied by time_offset to be
-        * applied over the elapsed time indicated by time_constant.
-        * Use SHIFT_USEC to get it into the same units as
-        * time_freq.
-        */
-       if ( time_offset < 0 ) {
-               ltemp = -time_offset;
-               ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
-               ltemp >>= SHIFT_KG + time_constant;
-               ltemp = -ltemp;
-       } else {
-               ltemp = time_offset;
-               ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
-               ltemp >>= SHIFT_KG + time_constant;
-       }
-       
-       /* If there is a single shot time adjustment in progress */
-       if ( time_adjust ) {
-#ifdef DEBUG_PPC_ADJTIMEX
-               printk("ppc_adjtimex: ");
-               if ( adjusting_time == 0 )
-                       printk("starting ");
-               printk("single shot time_adjust = %ld\n", time_adjust);
-#endif 
-       
-               adjusting_time = 1;
-               
-               /*
-                * Compute parts per million frequency adjustment
-                * to match time_adjust
-                */
-               singleshot_ppm = tickadj * HZ;  
-               /*
-                * The adjustment should be tickadj*HZ to match the code in
-                * linux/kernel/timer.c, but experiments show that this is too
-                * large. 3/4 of tickadj*HZ seems about right
-                */
-               singleshot_ppm -= singleshot_ppm / 4;
-               /* Use SHIFT_USEC to get it into the same units as time_freq */
-               singleshot_ppm <<= SHIFT_USEC;
-               if ( time_adjust < 0 )
-                       singleshot_ppm = -singleshot_ppm;
-       }
-       else {
-#ifdef DEBUG_PPC_ADJTIMEX
-               if ( adjusting_time )
-                       printk("ppc_adjtimex: ending single shot time_adjust\n");
-#endif
-               adjusting_time = 0;
-       }
-       
-       /* Add up all of the frequency adjustments */
-       delta_freq = time_freq + ltemp + singleshot_ppm;
-       
-       /*
-        * Compute a new value for tb_ticks_per_sec based on
-        * the frequency adjustment
-        */
-       den = 1000000 * (1 << (SHIFT_USEC - 8));
-       if ( delta_freq < 0 ) {
-               tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
-               new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
-       }
-       else {
-               tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
-               new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
-       }
-       
-#ifdef DEBUG_PPC_ADJTIMEX
-       printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
-       printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld  new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
-#endif
-
-       /*
-        * Compute a new value of tb_to_xs (used to convert tb to
-        * microseconds) and a new value of stamp_xsec which is the
-        * time (in 1/2^20 second units) corresponding to
-        * tb_orig_stamp.  This new value of stamp_xsec compensates
-        * for the change in frequency (implied by the new tb_to_xs)
-        * which guarantees that the current time remains the same.
-        */
-       write_seqlock_irqsave( &xtime_lock, flags );
-       tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
-       div128_by_32(1024*1024, 0, new_tb_ticks_per_sec, &divres);
-       new_tb_to_xs = divres.result_low;
-       new_xsec = mulhdu(tb_ticks, new_tb_to_xs);
-
-       old_xsec = mulhdu(tb_ticks, do_gtod.varp->tb_to_xs);
-       new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
-
-       update_gtod(do_gtod.varp->tb_orig_stamp, new_stamp_xsec, new_tb_to_xs);
-
-       write_sequnlock_irqrestore( &xtime_lock, flags );
-#endif /* CONFIG_PPC64 */
-}
-
 
 #define FEBRUARY       2
 #define        STARTOFTIME     1970