time.c 28 KB

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