time.c 26 KB

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