time.c 28 KB

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