]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - arch/powerpc/kernel/time.c
Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-serial
[linux-2.6-omap-h63xx.git] / arch / powerpc / kernel / time.c
index 86f7e3d154d8a11065e16064d4dc083fa99d115c..774c0a3c50191a8effd7e05d9b2b5072c3a94eba 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>
@@ -51,6 +50,7 @@
 #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,7 +75,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; 
@@ -98,10 +97,11 @@ 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  (SHIFT_SCALE - 10)
+#define TICKLEN_SCALE  TICK_LENGTH_SHIFT
 u64 last_tick_len;     /* units are ns / 2^TICKLEN_SCALE */
 u64 ticklen_to_xs;     /* 0.64 fraction */
 
@@ -135,6 +135,224 @@ unsigned long tb_last_stamp;
  */
 DEFINE_PER_CPU(unsigned long, 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     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();
+}
+
 void __delay(unsigned long loops)
 {
        unsigned long start;
@@ -392,6 +610,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;
@@ -440,6 +659,7 @@ 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;
@@ -461,7 +681,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
@@ -518,13 +738,27 @@ 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;
        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;
-       for_each_cpu(i) {
-               if (i != boot_cpuid) {
+       /*
+        * 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 {
                        previous_tb += offset;
                        per_cpu(last_jiffy, i) = previous_tb;
                }
@@ -622,42 +856,50 @@ 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;
+       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 = (unsigned int *)get_property(cpu, name, NULL);
                if (fp) {
-                       node_found = 1;
-                       ppc_tb_freq = *fp;
+                       found = 1;
+                       *val = 0;
+                       while (cells--)
+                               *val = (*val << 32) | *fp++;
                }
+
+               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);
@@ -669,11 +911,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)
@@ -709,9 +946,9 @@ void __init time_init(void)
        } 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();
        }
@@ -720,6 +957,7 @@ void __init time_init(void)
        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
@@ -773,10 +1011,7 @@ 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);