Merge master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[powerpc.git] / arch / powerpc / kernel / time.c
1 /*
2  * Common time routines among all ppc machines.
3  *
4  * Written by Cort Dougan (cort@cs.nmt.edu) to merge
5  * Paul Mackerras' version and mine for PReP and Pmac.
6  * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
7  * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
8  *
9  * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
10  * to make clock more stable (2.4.0-test5). The only thing
11  * that this code assumes is that the timebases have been synchronized
12  * by firmware on SMP and are never stopped (never do sleep
13  * on SMP then, nap and doze are OK).
14  * 
15  * Speeded up do_gettimeofday by getting rid of references to
16  * xtime (which required locks for consistency). (mikejc@us.ibm.com)
17  *
18  * TODO (not necessarily in this file):
19  * - improve precision and reproducibility of timebase frequency
20  * measurement at boot time. (for iSeries, we calibrate the timebase
21  * against the Titan chip's clock.)
22  * - for astronomical applications: add a new function to get
23  * non ambiguous timestamps even around leap seconds. This needs
24  * a new timestamp format and a good name.
25  *
26  * 1997-09-10  Updated NTP code according to technical memorandum Jan '96
27  *             "A Kernel Model for Precision Timekeeping" by Dave Mills
28  *
29  *      This program is free software; you can redistribute it and/or
30  *      modify it under the terms of the GNU General Public License
31  *      as published by the Free Software Foundation; either version
32  *      2 of the License, or (at your option) any later version.
33  */
34
35 #include <linux/config.h>
36 #include <linux/errno.h>
37 #include <linux/module.h>
38 #include <linux/sched.h>
39 #include <linux/kernel.h>
40 #include <linux/param.h>
41 #include <linux/string.h>
42 #include <linux/mm.h>
43 #include <linux/interrupt.h>
44 #include <linux/timex.h>
45 #include <linux/kernel_stat.h>
46 #include <linux/time.h>
47 #include <linux/init.h>
48 #include <linux/profile.h>
49 #include <linux/cpu.h>
50 #include <linux/security.h>
51 #include <linux/percpu.h>
52 #include <linux/rtc.h>
53
54 #include <asm/io.h>
55 #include <asm/processor.h>
56 #include <asm/nvram.h>
57 #include <asm/cache.h>
58 #include <asm/machdep.h>
59 #include <asm/uaccess.h>
60 #include <asm/time.h>
61 #include <asm/prom.h>
62 #include <asm/irq.h>
63 #include <asm/div64.h>
64 #ifdef CONFIG_PPC64
65 #include <asm/systemcfg.h>
66 #include <asm/firmware.h>
67 #endif
68 #ifdef CONFIG_PPC_ISERIES
69 #include <asm/iseries/it_lp_queue.h>
70 #include <asm/iseries/hv_call_xm.h>
71 #endif
72 #include <asm/smp.h>
73
74 /* keep track of when we need to update the rtc */
75 time_t last_rtc_update;
76 extern int piranha_simulator;
77 #ifdef CONFIG_PPC_ISERIES
78 unsigned long iSeries_recal_titan = 0;
79 unsigned long iSeries_recal_tb = 0; 
80 static unsigned long first_settimeofday = 1;
81 #endif
82
83 /* The decrementer counts down by 128 every 128ns on a 601. */
84 #define DECREMENTER_COUNT_601   (1000000000 / HZ)
85
86 #define XSEC_PER_SEC (1024*1024)
87
88 #ifdef CONFIG_PPC64
89 #define SCALE_XSEC(xsec, max)   (((xsec) * max) / XSEC_PER_SEC)
90 #else
91 /* compute ((xsec << 12) * max) >> 32 */
92 #define SCALE_XSEC(xsec, max)   mulhwu((xsec) << 12, max)
93 #endif
94
95 unsigned long tb_ticks_per_jiffy;
96 unsigned long tb_ticks_per_usec = 100; /* sane default */
97 EXPORT_SYMBOL(tb_ticks_per_usec);
98 unsigned long tb_ticks_per_sec;
99 u64 tb_to_xs;
100 unsigned tb_to_us;
101 unsigned long processor_freq;
102 DEFINE_SPINLOCK(rtc_lock);
103 EXPORT_SYMBOL_GPL(rtc_lock);
104
105 u64 tb_to_ns_scale;
106 unsigned tb_to_ns_shift;
107
108 struct gettimeofday_struct do_gtod;
109
110 extern unsigned long wall_jiffies;
111
112 extern struct timezone sys_tz;
113 static long timezone_offset;
114
115 void ppc_adjtimex(void);
116
117 static unsigned adjusting_time = 0;
118
119 unsigned long ppc_proc_freq;
120 unsigned long ppc_tb_freq;
121
122 #ifdef CONFIG_PPC32     /* XXX for now */
123 #define boot_cpuid      0
124 #endif
125
126 u64 tb_last_jiffy __cacheline_aligned_in_smp;
127 unsigned long tb_last_stamp;
128
129 /*
130  * Note that on ppc32 this only stores the bottom 32 bits of
131  * the timebase value, but that's enough to tell when a jiffy
132  * has passed.
133  */
134 DEFINE_PER_CPU(unsigned long, last_jiffy);
135
136 static __inline__ void timer_check_rtc(void)
137 {
138         /*
139          * update the rtc when needed, this should be performed on the
140          * right fraction of a second. Half or full second ?
141          * Full second works on mk48t59 clocks, others need testing.
142          * Note that this update is basically only used through 
143          * the adjtimex system calls. Setting the HW clock in
144          * any other way is a /dev/rtc and userland business.
145          * This is still wrong by -0.5/+1.5 jiffies because of the
146          * timer interrupt resolution and possible delay, but here we 
147          * hit a quantization limit which can only be solved by higher
148          * resolution timers and decoupling time management from timer
149          * interrupts. This is also wrong on the clocks
150          * which require being written at the half second boundary.
151          * We should have an rtc call that only sets the minutes and
152          * seconds like on Intel to avoid problems with non UTC clocks.
153          */
154         if (ppc_md.set_rtc_time && ntp_synced() &&
155             xtime.tv_sec - last_rtc_update >= 659 &&
156             abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ &&
157             jiffies - wall_jiffies == 1) {
158                 struct rtc_time tm;
159                 to_tm(xtime.tv_sec + 1 + timezone_offset, &tm);
160                 tm.tm_year -= 1900;
161                 tm.tm_mon -= 1;
162                 if (ppc_md.set_rtc_time(&tm) == 0)
163                         last_rtc_update = xtime.tv_sec + 1;
164                 else
165                         /* Try again one minute later */
166                         last_rtc_update += 60;
167         }
168 }
169
170 /*
171  * This version of gettimeofday has microsecond resolution.
172  */
173 static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
174 {
175         unsigned long sec, usec;
176         u64 tb_ticks, xsec;
177         struct gettimeofday_vars *temp_varp;
178         u64 temp_tb_to_xs, temp_stamp_xsec;
179
180         /*
181          * These calculations are faster (gets rid of divides)
182          * if done in units of 1/2^20 rather than microseconds.
183          * The conversion to microseconds at the end is done
184          * without a divide (and in fact, without a multiply)
185          */
186         temp_varp = do_gtod.varp;
187         tb_ticks = tb_val - temp_varp->tb_orig_stamp;
188         temp_tb_to_xs = temp_varp->tb_to_xs;
189         temp_stamp_xsec = temp_varp->stamp_xsec;
190         xsec = temp_stamp_xsec + mulhdu(tb_ticks, temp_tb_to_xs);
191         sec = xsec / XSEC_PER_SEC;
192         usec = (unsigned long)xsec & (XSEC_PER_SEC - 1);
193         usec = SCALE_XSEC(usec, 1000000);
194
195         tv->tv_sec = sec;
196         tv->tv_usec = usec;
197 }
198
199 void do_gettimeofday(struct timeval *tv)
200 {
201         if (__USE_RTC()) {
202                 /* do this the old way */
203                 unsigned long flags, seq;
204                 unsigned int sec, nsec, usec, lost;
205
206                 do {
207                         seq = read_seqbegin_irqsave(&xtime_lock, flags);
208                         sec = xtime.tv_sec;
209                         nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp);
210                         lost = jiffies - wall_jiffies;
211                 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
212                 usec = nsec / 1000 + lost * (1000000 / HZ);
213                 while (usec >= 1000000) {
214                         usec -= 1000000;
215                         ++sec;
216                 }
217                 tv->tv_sec = sec;
218                 tv->tv_usec = usec;
219                 return;
220         }
221         __do_gettimeofday(tv, get_tb());
222 }
223
224 EXPORT_SYMBOL(do_gettimeofday);
225
226 /* Synchronize xtime with do_gettimeofday */ 
227
228 static inline void timer_sync_xtime(unsigned long cur_tb)
229 {
230 #ifdef CONFIG_PPC64
231         /* why do we do this? */
232         struct timeval my_tv;
233
234         __do_gettimeofday(&my_tv, cur_tb);
235
236         if (xtime.tv_sec <= my_tv.tv_sec) {
237                 xtime.tv_sec = my_tv.tv_sec;
238                 xtime.tv_nsec = my_tv.tv_usec * 1000;
239         }
240 #endif
241 }
242
243 /*
244  * There are two copies of tb_to_xs and stamp_xsec so that no
245  * lock is needed to access and use these values in
246  * do_gettimeofday.  We alternate the copies and as long as a
247  * reasonable time elapses between changes, there will never
248  * be inconsistent values.  ntpd has a minimum of one minute
249  * between updates.
250  */
251 static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
252                                u64 new_tb_to_xs)
253 {
254         unsigned temp_idx;
255         struct gettimeofday_vars *temp_varp;
256
257         temp_idx = (do_gtod.var_idx == 0);
258         temp_varp = &do_gtod.vars[temp_idx];
259
260         temp_varp->tb_to_xs = new_tb_to_xs;
261         temp_varp->tb_orig_stamp = new_tb_stamp;
262         temp_varp->stamp_xsec = new_stamp_xsec;
263         smp_mb();
264         do_gtod.varp = temp_varp;
265         do_gtod.var_idx = temp_idx;
266
267 #ifdef CONFIG_PPC64
268         /*
269          * tb_update_count is used to allow the userspace gettimeofday code
270          * to assure itself that it sees a consistent view of the tb_to_xs and
271          * stamp_xsec variables.  It reads the tb_update_count, then reads
272          * tb_to_xs and stamp_xsec and then reads tb_update_count again.  If
273          * the two values of tb_update_count match and are even then the
274          * tb_to_xs and stamp_xsec values are consistent.  If not, then it
275          * loops back and reads them again until this criteria is met.
276          */
277         ++(systemcfg->tb_update_count);
278         smp_wmb();
279         systemcfg->tb_orig_stamp = new_tb_stamp;
280         systemcfg->stamp_xsec = new_stamp_xsec;
281         systemcfg->tb_to_xs = new_tb_to_xs;
282         smp_wmb();
283         ++(systemcfg->tb_update_count);
284 #endif
285 }
286
287 /*
288  * When the timebase - tb_orig_stamp gets too big, we do a manipulation
289  * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
290  * difference tb - tb_orig_stamp small enough to always fit inside a
291  * 32 bits number. This is a requirement of our fast 32 bits userland
292  * implementation in the vdso. If we "miss" a call to this function
293  * (interrupt latency, CPU locked in a spinlock, ...) and we end up
294  * with a too big difference, then the vdso will fallback to calling
295  * the syscall
296  */
297 static __inline__ void timer_recalc_offset(u64 cur_tb)
298 {
299         unsigned long offset;
300         u64 new_stamp_xsec;
301
302         if (__USE_RTC())
303                 return;
304         offset = cur_tb - do_gtod.varp->tb_orig_stamp;
305         if ((offset & 0x80000000u) == 0)
306                 return;
307         new_stamp_xsec = do_gtod.varp->stamp_xsec
308                 + mulhdu(offset, do_gtod.varp->tb_to_xs);
309         update_gtod(cur_tb, new_stamp_xsec, do_gtod.varp->tb_to_xs);
310 }
311
312 #ifdef CONFIG_SMP
313 unsigned long profile_pc(struct pt_regs *regs)
314 {
315         unsigned long pc = instruction_pointer(regs);
316
317         if (in_lock_functions(pc))
318                 return regs->link;
319
320         return pc;
321 }
322 EXPORT_SYMBOL(profile_pc);
323 #endif
324
325 #ifdef CONFIG_PPC_ISERIES
326
327 /* 
328  * This function recalibrates the timebase based on the 49-bit time-of-day
329  * value in the Titan chip.  The Titan is much more accurate than the value
330  * returned by the service processor for the timebase frequency.  
331  */
332
333 static void iSeries_tb_recal(void)
334 {
335         struct div_result divres;
336         unsigned long titan, tb;
337         tb = get_tb();
338         titan = HvCallXm_loadTod();
339         if ( iSeries_recal_titan ) {
340                 unsigned long tb_ticks = tb - iSeries_recal_tb;
341                 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
342                 unsigned long new_tb_ticks_per_sec   = (tb_ticks * USEC_PER_SEC)/titan_usec;
343                 unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
344                 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
345                 char sign = '+';                
346                 /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
347                 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
348
349                 if ( tick_diff < 0 ) {
350                         tick_diff = -tick_diff;
351                         sign = '-';
352                 }
353                 if ( tick_diff ) {
354                         if ( tick_diff < tb_ticks_per_jiffy/25 ) {
355                                 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
356                                                 new_tb_ticks_per_jiffy, sign, tick_diff );
357                                 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
358                                 tb_ticks_per_sec   = new_tb_ticks_per_sec;
359                                 div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
360                                 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
361                                 tb_to_xs = divres.result_low;
362                                 do_gtod.varp->tb_to_xs = tb_to_xs;
363                                 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
364                                 systemcfg->tb_to_xs = tb_to_xs;
365                         }
366                         else {
367                                 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
368                                         "                   new tb_ticks_per_jiffy = %lu\n"
369                                         "                   old tb_ticks_per_jiffy = %lu\n",
370                                         new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
371                         }
372                 }
373         }
374         iSeries_recal_titan = titan;
375         iSeries_recal_tb = tb;
376 }
377 #endif
378
379 /*
380  * For iSeries shared processors, we have to let the hypervisor
381  * set the hardware decrementer.  We set a virtual decrementer
382  * in the lppaca and call the hypervisor if the virtual
383  * decrementer is less than the current value in the hardware
384  * decrementer. (almost always the new decrementer value will
385  * be greater than the current hardware decementer so the hypervisor
386  * call will not be needed)
387  */
388
389 /*
390  * timer_interrupt - gets called when the decrementer overflows,
391  * with interrupts disabled.
392  */
393 void timer_interrupt(struct pt_regs * regs)
394 {
395         int next_dec;
396         int cpu = smp_processor_id();
397         unsigned long ticks;
398
399 #ifdef CONFIG_PPC32
400         if (atomic_read(&ppc_n_lost_interrupts) != 0)
401                 do_IRQ(regs);
402 #endif
403
404         irq_enter();
405
406         profile_tick(CPU_PROFILING, regs);
407
408 #ifdef CONFIG_PPC_ISERIES
409         get_paca()->lppaca.int_dword.fields.decr_int = 0;
410 #endif
411
412         while ((ticks = tb_ticks_since(per_cpu(last_jiffy, cpu)))
413                >= tb_ticks_per_jiffy) {
414                 /* Update last_jiffy */
415                 per_cpu(last_jiffy, cpu) += tb_ticks_per_jiffy;
416                 /* Handle RTCL overflow on 601 */
417                 if (__USE_RTC() && per_cpu(last_jiffy, cpu) >= 1000000000)
418                         per_cpu(last_jiffy, cpu) -= 1000000000;
419
420                 /*
421                  * We cannot disable the decrementer, so in the period
422                  * between this cpu's being marked offline in cpu_online_map
423                  * and calling stop-self, it is taking timer interrupts.
424                  * Avoid calling into the scheduler rebalancing code if this
425                  * is the case.
426                  */
427                 if (!cpu_is_offline(cpu))
428                         update_process_times(user_mode(regs));
429
430                 /*
431                  * No need to check whether cpu is offline here; boot_cpuid
432                  * should have been fixed up by now.
433                  */
434                 if (cpu != boot_cpuid)
435                         continue;
436
437                 write_seqlock(&xtime_lock);
438                 tb_last_jiffy += tb_ticks_per_jiffy;
439                 tb_last_stamp = per_cpu(last_jiffy, cpu);
440                 timer_recalc_offset(tb_last_jiffy);
441                 do_timer(regs);
442                 timer_sync_xtime(tb_last_jiffy);
443                 timer_check_rtc();
444                 write_sequnlock(&xtime_lock);
445                 if (adjusting_time && (time_adjust == 0))
446                         ppc_adjtimex();
447         }
448         
449         next_dec = tb_ticks_per_jiffy - ticks;
450         set_dec(next_dec);
451
452 #ifdef CONFIG_PPC_ISERIES
453         if (hvlpevent_is_pending())
454                 process_hvlpevents(regs);
455 #endif
456
457 #ifdef CONFIG_PPC64
458         /* collect purr register values often, for accurate calculations */
459         if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
460                 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
461                 cu->current_tb = mfspr(SPRN_PURR);
462         }
463 #endif
464
465         irq_exit();
466 }
467
468 void wakeup_decrementer(void)
469 {
470         int i;
471
472         set_dec(tb_ticks_per_jiffy);
473         /*
474          * We don't expect this to be called on a machine with a 601,
475          * so using get_tbl is fine.
476          */
477         tb_last_stamp = tb_last_jiffy = get_tb();
478         for_each_cpu(i)
479                 per_cpu(last_jiffy, i) = tb_last_stamp;
480 }
481
482 #ifdef CONFIG_SMP
483 void __init smp_space_timers(unsigned int max_cpus)
484 {
485         int i;
486         unsigned long offset = tb_ticks_per_jiffy / max_cpus;
487         unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid);
488
489         for_each_cpu(i) {
490                 if (i != boot_cpuid) {
491                         previous_tb += offset;
492                         per_cpu(last_jiffy, i) = previous_tb;
493                 }
494         }
495 }
496 #endif
497
498 /*
499  * Scheduler clock - returns current time in nanosec units.
500  *
501  * Note: mulhdu(a, b) (multiply high double unsigned) returns
502  * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
503  * are 64-bit unsigned numbers.
504  */
505 unsigned long long sched_clock(void)
506 {
507         if (__USE_RTC())
508                 return get_rtc();
509         return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
510 }
511
512 int do_settimeofday(struct timespec *tv)
513 {
514         time_t wtm_sec, new_sec = tv->tv_sec;
515         long wtm_nsec, new_nsec = tv->tv_nsec;
516         unsigned long flags;
517         long int tb_delta;
518         u64 new_xsec, tb_delta_xs;
519
520         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
521                 return -EINVAL;
522
523         write_seqlock_irqsave(&xtime_lock, flags);
524
525         /*
526          * Updating the RTC is not the job of this code. If the time is
527          * stepped under NTP, the RTC will be updated after STA_UNSYNC
528          * is cleared.  Tools like clock/hwclock either copy the RTC
529          * to the system time, in which case there is no point in writing
530          * to the RTC again, or write to the RTC but then they don't call
531          * settimeofday to perform this operation.
532          */
533 #ifdef CONFIG_PPC_ISERIES
534         if (first_settimeofday) {
535                 iSeries_tb_recal();
536                 first_settimeofday = 0;
537         }
538 #endif
539         tb_delta = tb_ticks_since(tb_last_stamp);
540         tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
541         tb_delta_xs = mulhdu(tb_delta, do_gtod.varp->tb_to_xs);
542
543         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
544         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
545
546         set_normalized_timespec(&xtime, new_sec, new_nsec);
547         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
548
549         /* In case of a large backwards jump in time with NTP, we want the 
550          * clock to be updated as soon as the PLL is again in lock.
551          */
552         last_rtc_update = new_sec - 658;
553
554         ntp_clear();
555
556         new_xsec = 0;
557         if (new_nsec != 0) {
558                 new_xsec = (u64)new_nsec * XSEC_PER_SEC;
559                 do_div(new_xsec, NSEC_PER_SEC);
560         }
561         new_xsec += (u64)new_sec * XSEC_PER_SEC - tb_delta_xs;
562         update_gtod(tb_last_jiffy, new_xsec, do_gtod.varp->tb_to_xs);
563
564 #ifdef CONFIG_PPC64
565         systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
566         systemcfg->tz_dsttime = sys_tz.tz_dsttime;
567 #endif
568
569         write_sequnlock_irqrestore(&xtime_lock, flags);
570         clock_was_set();
571         return 0;
572 }
573
574 EXPORT_SYMBOL(do_settimeofday);
575
576 void __init generic_calibrate_decr(void)
577 {
578         struct device_node *cpu;
579         unsigned int *fp;
580         int node_found;
581
582         /*
583          * The cpu node should have a timebase-frequency property
584          * to tell us the rate at which the decrementer counts.
585          */
586         cpu = of_find_node_by_type(NULL, "cpu");
587
588         ppc_tb_freq = DEFAULT_TB_FREQ;          /* hardcoded default */
589         node_found = 0;
590         if (cpu != 0) {
591                 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
592                                                   NULL);
593                 if (fp != 0) {
594                         node_found = 1;
595                         ppc_tb_freq = *fp;
596                 }
597         }
598         if (!node_found)
599                 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
600                                 "(not found)\n");
601
602         ppc_proc_freq = DEFAULT_PROC_FREQ;
603         node_found = 0;
604         if (cpu != 0) {
605                 fp = (unsigned int *)get_property(cpu, "clock-frequency",
606                                                   NULL);
607                 if (fp != 0) {
608                         node_found = 1;
609                         ppc_proc_freq = *fp;
610                 }
611         }
612 #ifdef CONFIG_BOOKE
613         /* Set the time base to zero */
614         mtspr(SPRN_TBWL, 0);
615         mtspr(SPRN_TBWU, 0);
616
617         /* Clear any pending timer interrupts */
618         mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
619
620         /* Enable decrementer interrupt */
621         mtspr(SPRN_TCR, TCR_DIE);
622 #endif
623         if (!node_found)
624                 printk(KERN_ERR "WARNING: Estimating processor frequency "
625                                 "(not found)\n");
626
627         of_node_put(cpu);
628 }
629
630 unsigned long get_boot_time(void)
631 {
632         struct rtc_time tm;
633
634         if (ppc_md.get_boot_time)
635                 return ppc_md.get_boot_time();
636         if (!ppc_md.get_rtc_time)
637                 return 0;
638         ppc_md.get_rtc_time(&tm);
639         return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
640                       tm.tm_hour, tm.tm_min, tm.tm_sec);
641 }
642
643 /* This function is only called on the boot processor */
644 void __init time_init(void)
645 {
646         unsigned long flags;
647         unsigned long tm = 0;
648         struct div_result res;
649         u64 scale;
650         unsigned shift;
651
652         if (ppc_md.time_init != NULL)
653                 timezone_offset = ppc_md.time_init();
654
655         if (__USE_RTC()) {
656                 /* 601 processor: dec counts down by 128 every 128ns */
657                 ppc_tb_freq = 1000000000;
658                 tb_last_stamp = get_rtcl();
659                 tb_last_jiffy = tb_last_stamp;
660         } else {
661                 /* Normal PowerPC with timebase register */
662                 ppc_md.calibrate_decr();
663                 printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
664                        ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
665                 printk(KERN_INFO "time_init: processor frequency   = %lu.%.6lu MHz\n",
666                        ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
667                 tb_last_stamp = tb_last_jiffy = get_tb();
668         }
669
670         tb_ticks_per_jiffy = ppc_tb_freq / HZ;
671         tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
672         tb_ticks_per_usec = ppc_tb_freq / 1000000;
673         tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
674         div128_by_32(1024*1024, 0, tb_ticks_per_sec, &res);
675         tb_to_xs = res.result_low;
676
677 #ifdef CONFIG_PPC64
678         get_paca()->default_decr = tb_ticks_per_jiffy;
679 #endif
680
681         /*
682          * Compute scale factor for sched_clock.
683          * The calibrate_decr() function has set tb_ticks_per_sec,
684          * which is the timebase frequency.
685          * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
686          * the 128-bit result as a 64.64 fixed-point number.
687          * We then shift that number right until it is less than 1.0,
688          * giving us the scale factor and shift count to use in
689          * sched_clock().
690          */
691         div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
692         scale = res.result_low;
693         for (shift = 0; res.result_high != 0; ++shift) {
694                 scale = (scale >> 1) | (res.result_high << 63);
695                 res.result_high >>= 1;
696         }
697         tb_to_ns_scale = scale;
698         tb_to_ns_shift = shift;
699
700 #ifdef CONFIG_PPC_ISERIES
701         if (!piranha_simulator)
702 #endif
703                 tm = get_boot_time();
704
705         write_seqlock_irqsave(&xtime_lock, flags);
706         xtime.tv_sec = tm;
707         xtime.tv_nsec = 0;
708         do_gtod.varp = &do_gtod.vars[0];
709         do_gtod.var_idx = 0;
710         do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
711         __get_cpu_var(last_jiffy) = tb_last_stamp;
712         do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
713         do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
714         do_gtod.varp->tb_to_xs = tb_to_xs;
715         do_gtod.tb_to_us = tb_to_us;
716 #ifdef CONFIG_PPC64
717         systemcfg->tb_orig_stamp = tb_last_jiffy;
718         systemcfg->tb_update_count = 0;
719         systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
720         systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
721         systemcfg->tb_to_xs = tb_to_xs;
722 #endif
723
724         time_freq = 0;
725
726         /* If platform provided a timezone (pmac), we correct the time */
727         if (timezone_offset) {
728                 sys_tz.tz_minuteswest = -timezone_offset / 60;
729                 sys_tz.tz_dsttime = 0;
730                 xtime.tv_sec -= timezone_offset;
731         }
732
733         last_rtc_update = xtime.tv_sec;
734         set_normalized_timespec(&wall_to_monotonic,
735                                 -xtime.tv_sec, -xtime.tv_nsec);
736         write_sequnlock_irqrestore(&xtime_lock, flags);
737
738         /* Not exact, but the timer interrupt takes care of this */
739         set_dec(tb_ticks_per_jiffy);
740 }
741
742 /* 
743  * After adjtimex is called, adjust the conversion of tb ticks
744  * to microseconds to keep do_gettimeofday synchronized 
745  * with ntpd.
746  *
747  * Use the time_adjust, time_freq and time_offset computed by adjtimex to 
748  * adjust the frequency.
749  */
750
751 /* #define DEBUG_PPC_ADJTIMEX 1 */
752
753 void ppc_adjtimex(void)
754 {
755 #ifdef CONFIG_PPC64
756         unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec,
757                 new_tb_to_xs, new_xsec, new_stamp_xsec;
758         unsigned long tb_ticks_per_sec_delta;
759         long delta_freq, ltemp;
760         struct div_result divres; 
761         unsigned long flags;
762         long singleshot_ppm = 0;
763
764         /*
765          * Compute parts per million frequency adjustment to
766          * accomplish the time adjustment implied by time_offset to be
767          * applied over the elapsed time indicated by time_constant.
768          * Use SHIFT_USEC to get it into the same units as
769          * time_freq.
770          */
771         if ( time_offset < 0 ) {
772                 ltemp = -time_offset;
773                 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
774                 ltemp >>= SHIFT_KG + time_constant;
775                 ltemp = -ltemp;
776         } else {
777                 ltemp = time_offset;
778                 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
779                 ltemp >>= SHIFT_KG + time_constant;
780         }
781         
782         /* If there is a single shot time adjustment in progress */
783         if ( time_adjust ) {
784 #ifdef DEBUG_PPC_ADJTIMEX
785                 printk("ppc_adjtimex: ");
786                 if ( adjusting_time == 0 )
787                         printk("starting ");
788                 printk("single shot time_adjust = %ld\n", time_adjust);
789 #endif  
790         
791                 adjusting_time = 1;
792                 
793                 /*
794                  * Compute parts per million frequency adjustment
795                  * to match time_adjust
796                  */
797                 singleshot_ppm = tickadj * HZ;  
798                 /*
799                  * The adjustment should be tickadj*HZ to match the code in
800                  * linux/kernel/timer.c, but experiments show that this is too
801                  * large. 3/4 of tickadj*HZ seems about right
802                  */
803                 singleshot_ppm -= singleshot_ppm / 4;
804                 /* Use SHIFT_USEC to get it into the same units as time_freq */
805                 singleshot_ppm <<= SHIFT_USEC;
806                 if ( time_adjust < 0 )
807                         singleshot_ppm = -singleshot_ppm;
808         }
809         else {
810 #ifdef DEBUG_PPC_ADJTIMEX
811                 if ( adjusting_time )
812                         printk("ppc_adjtimex: ending single shot time_adjust\n");
813 #endif
814                 adjusting_time = 0;
815         }
816         
817         /* Add up all of the frequency adjustments */
818         delta_freq = time_freq + ltemp + singleshot_ppm;
819         
820         /*
821          * Compute a new value for tb_ticks_per_sec based on
822          * the frequency adjustment
823          */
824         den = 1000000 * (1 << (SHIFT_USEC - 8));
825         if ( delta_freq < 0 ) {
826                 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
827                 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
828         }
829         else {
830                 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
831                 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
832         }
833         
834 #ifdef DEBUG_PPC_ADJTIMEX
835         printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
836         printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld  new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
837 #endif
838
839         /*
840          * Compute a new value of tb_to_xs (used to convert tb to
841          * microseconds) and a new value of stamp_xsec which is the
842          * time (in 1/2^20 second units) corresponding to
843          * tb_orig_stamp.  This new value of stamp_xsec compensates
844          * for the change in frequency (implied by the new tb_to_xs)
845          * which guarantees that the current time remains the same.
846          */
847         write_seqlock_irqsave( &xtime_lock, flags );
848         tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
849         div128_by_32(1024*1024, 0, new_tb_ticks_per_sec, &divres);
850         new_tb_to_xs = divres.result_low;
851         new_xsec = mulhdu(tb_ticks, new_tb_to_xs);
852
853         old_xsec = mulhdu(tb_ticks, do_gtod.varp->tb_to_xs);
854         new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
855
856         update_gtod(do_gtod.varp->tb_orig_stamp, new_stamp_xsec, new_tb_to_xs);
857
858         write_sequnlock_irqrestore( &xtime_lock, flags );
859 #endif /* CONFIG_PPC64 */
860 }
861
862
863 #define FEBRUARY        2
864 #define STARTOFTIME     1970
865 #define SECDAY          86400L
866 #define SECYR           (SECDAY * 365)
867 #define leapyear(year)          ((year) % 4 == 0 && \
868                                  ((year) % 100 != 0 || (year) % 400 == 0))
869 #define days_in_year(a)         (leapyear(a) ? 366 : 365)
870 #define days_in_month(a)        (month_days[(a) - 1])
871
872 static int month_days[12] = {
873         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
874 };
875
876 /*
877  * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
878  */
879 void GregorianDay(struct rtc_time * tm)
880 {
881         int leapsToDate;
882         int lastYear;
883         int day;
884         int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
885
886         lastYear = tm->tm_year - 1;
887
888         /*
889          * Number of leap corrections to apply up to end of last year
890          */
891         leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
892
893         /*
894          * This year is a leap year if it is divisible by 4 except when it is
895          * divisible by 100 unless it is divisible by 400
896          *
897          * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
898          */
899         day = tm->tm_mon > 2 && leapyear(tm->tm_year);
900
901         day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
902                    tm->tm_mday;
903
904         tm->tm_wday = day % 7;
905 }
906
907 void to_tm(int tim, struct rtc_time * tm)
908 {
909         register int    i;
910         register long   hms, day;
911
912         day = tim / SECDAY;
913         hms = tim % SECDAY;
914
915         /* Hours, minutes, seconds are easy */
916         tm->tm_hour = hms / 3600;
917         tm->tm_min = (hms % 3600) / 60;
918         tm->tm_sec = (hms % 3600) % 60;
919
920         /* Number of years in days */
921         for (i = STARTOFTIME; day >= days_in_year(i); i++)
922                 day -= days_in_year(i);
923         tm->tm_year = i;
924
925         /* Number of months in days left */
926         if (leapyear(tm->tm_year))
927                 days_in_month(FEBRUARY) = 29;
928         for (i = 1; day >= days_in_month(i); i++)
929                 day -= days_in_month(i);
930         days_in_month(FEBRUARY) = 28;
931         tm->tm_mon = i;
932
933         /* Days are what is left over (+1) from all that. */
934         tm->tm_mday = day + 1;
935
936         /*
937          * Determine the day of week
938          */
939         GregorianDay(tm);
940 }
941
942 /* Auxiliary function to compute scaling factors */
943 /* Actually the choice of a timebase running at 1/4 the of the bus
944  * frequency giving resolution of a few tens of nanoseconds is quite nice.
945  * It makes this computation very precise (27-28 bits typically) which
946  * is optimistic considering the stability of most processor clock
947  * oscillators and the precision with which the timebase frequency
948  * is measured but does not harm.
949  */
950 unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
951 {
952         unsigned mlt=0, tmp, err;
953         /* No concern for performance, it's done once: use a stupid
954          * but safe and compact method to find the multiplier.
955          */
956   
957         for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
958                 if (mulhwu(inscale, mlt|tmp) < outscale)
959                         mlt |= tmp;
960         }
961   
962         /* We might still be off by 1 for the best approximation.
963          * A side effect of this is that if outscale is too large
964          * the returned value will be zero.
965          * Many corner cases have been checked and seem to work,
966          * some might have been forgotten in the test however.
967          */
968   
969         err = inscale * (mlt+1);
970         if (err <= inscale/2)
971                 mlt++;
972         return mlt;
973 }
974
975 /*
976  * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
977  * result.
978  */
979 void div128_by_32(u64 dividend_high, u64 dividend_low,
980                   unsigned divisor, struct div_result *dr)
981 {
982         unsigned long a, b, c, d;
983         unsigned long w, x, y, z;
984         u64 ra, rb, rc;
985
986         a = dividend_high >> 32;
987         b = dividend_high & 0xffffffff;
988         c = dividend_low >> 32;
989         d = dividend_low & 0xffffffff;
990
991         w = a / divisor;
992         ra = ((u64)(a - (w * divisor)) << 32) + b;
993
994         rb = ((u64) do_div(ra, divisor) << 32) + c;
995         x = ra;
996
997         rc = ((u64) do_div(rb, divisor) << 32) + d;
998         y = rb;
999
1000         do_div(rc, divisor);
1001         z = rc;
1002
1003         dr->result_high = ((u64)w << 32) + x;
1004         dr->result_low  = ((u64)y << 32) + z;
1005
1006 }