time.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 ( (time_status & STA_UNSYNC) == 0 &&
  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. time_adjust = 0; /* stop active adjtime() */
  381. time_status |= STA_UNSYNC;
  382. time_maxerror = NTP_PHASE_LIMIT;
  383. time_esterror = NTP_PHASE_LIMIT;
  384. delta_xsec = mulhdu( (tb_last_stamp-do_gtod.varp->tb_orig_stamp),
  385. do_gtod.varp->tb_to_xs );
  386. new_xsec = (new_nsec * XSEC_PER_SEC) / NSEC_PER_SEC;
  387. new_xsec += new_sec * XSEC_PER_SEC;
  388. if ( new_xsec > delta_xsec ) {
  389. do_gtod.varp->stamp_xsec = new_xsec - delta_xsec;
  390. systemcfg->stamp_xsec = new_xsec - delta_xsec;
  391. }
  392. else {
  393. /* This is only for the case where the user is setting the time
  394. * way back to a time such that the boot time would have been
  395. * before 1970 ... eg. we booted ten days ago, and we are setting
  396. * the time to Jan 5, 1970 */
  397. do_gtod.varp->stamp_xsec = new_xsec;
  398. do_gtod.varp->tb_orig_stamp = tb_last_stamp;
  399. systemcfg->stamp_xsec = new_xsec;
  400. systemcfg->tb_orig_stamp = tb_last_stamp;
  401. }
  402. systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
  403. systemcfg->tz_dsttime = sys_tz.tz_dsttime;
  404. write_sequnlock_irqrestore(&xtime_lock, flags);
  405. clock_was_set();
  406. return 0;
  407. }
  408. EXPORT_SYMBOL(do_settimeofday);
  409. #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_MAPLE) || defined(CONFIG_PPC_BPA)
  410. void __init generic_calibrate_decr(void)
  411. {
  412. struct device_node *cpu;
  413. struct div_result divres;
  414. unsigned int *fp;
  415. int node_found;
  416. /*
  417. * The cpu node should have a timebase-frequency property
  418. * to tell us the rate at which the decrementer counts.
  419. */
  420. cpu = of_find_node_by_type(NULL, "cpu");
  421. ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
  422. node_found = 0;
  423. if (cpu != 0) {
  424. fp = (unsigned int *)get_property(cpu, "timebase-frequency",
  425. NULL);
  426. if (fp != 0) {
  427. node_found = 1;
  428. ppc_tb_freq = *fp;
  429. }
  430. }
  431. if (!node_found)
  432. printk(KERN_ERR "WARNING: Estimating decrementer frequency "
  433. "(not found)\n");
  434. ppc_proc_freq = DEFAULT_PROC_FREQ;
  435. node_found = 0;
  436. if (cpu != 0) {
  437. fp = (unsigned int *)get_property(cpu, "clock-frequency",
  438. NULL);
  439. if (fp != 0) {
  440. node_found = 1;
  441. ppc_proc_freq = *fp;
  442. }
  443. }
  444. if (!node_found)
  445. printk(KERN_ERR "WARNING: Estimating processor frequency "
  446. "(not found)\n");
  447. of_node_put(cpu);
  448. printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
  449. ppc_tb_freq/1000000, ppc_tb_freq%1000000);
  450. printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
  451. ppc_proc_freq/1000000, ppc_proc_freq%1000000);
  452. tb_ticks_per_jiffy = ppc_tb_freq / HZ;
  453. tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
  454. tb_ticks_per_usec = ppc_tb_freq / 1000000;
  455. tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
  456. div128_by_32(1024*1024, 0, tb_ticks_per_sec, &divres);
  457. tb_to_xs = divres.result_low;
  458. setup_default_decr();
  459. }
  460. #endif
  461. void __init time_init(void)
  462. {
  463. /* This function is only called on the boot processor */
  464. unsigned long flags;
  465. struct rtc_time tm;
  466. struct div_result res;
  467. unsigned long scale, shift;
  468. ppc_md.calibrate_decr();
  469. /*
  470. * Compute scale factor for sched_clock.
  471. * The calibrate_decr() function has set tb_ticks_per_sec,
  472. * which is the timebase frequency.
  473. * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
  474. * the 128-bit result as a 64.64 fixed-point number.
  475. * We then shift that number right until it is less than 1.0,
  476. * giving us the scale factor and shift count to use in
  477. * sched_clock().
  478. */
  479. div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
  480. scale = res.result_low;
  481. for (shift = 0; res.result_high != 0; ++shift) {
  482. scale = (scale >> 1) | (res.result_high << 63);
  483. res.result_high >>= 1;
  484. }
  485. tb_to_ns_scale = scale;
  486. tb_to_ns_shift = shift;
  487. #ifdef CONFIG_PPC_ISERIES
  488. if (!piranha_simulator)
  489. #endif
  490. ppc_md.get_boot_time(&tm);
  491. write_seqlock_irqsave(&xtime_lock, flags);
  492. xtime.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  493. tm.tm_hour, tm.tm_min, tm.tm_sec);
  494. tb_last_stamp = get_tb();
  495. do_gtod.varp = &do_gtod.vars[0];
  496. do_gtod.var_idx = 0;
  497. do_gtod.varp->tb_orig_stamp = tb_last_stamp;
  498. get_paca()->next_jiffy_update_tb = tb_last_stamp + tb_ticks_per_jiffy;
  499. do_gtod.varp->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
  500. do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
  501. do_gtod.varp->tb_to_xs = tb_to_xs;
  502. do_gtod.tb_to_us = tb_to_us;
  503. systemcfg->tb_orig_stamp = tb_last_stamp;
  504. systemcfg->tb_update_count = 0;
  505. systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
  506. systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
  507. systemcfg->tb_to_xs = tb_to_xs;
  508. time_freq = 0;
  509. xtime.tv_nsec = 0;
  510. last_rtc_update = xtime.tv_sec;
  511. set_normalized_timespec(&wall_to_monotonic,
  512. -xtime.tv_sec, -xtime.tv_nsec);
  513. write_sequnlock_irqrestore(&xtime_lock, flags);
  514. /* Not exact, but the timer interrupt takes care of this */
  515. set_dec(tb_ticks_per_jiffy);
  516. }
  517. /*
  518. * After adjtimex is called, adjust the conversion of tb ticks
  519. * to microseconds to keep do_gettimeofday synchronized
  520. * with ntpd.
  521. *
  522. * Use the time_adjust, time_freq and time_offset computed by adjtimex to
  523. * adjust the frequency.
  524. */
  525. /* #define DEBUG_PPC_ADJTIMEX 1 */
  526. void ppc_adjtimex(void)
  527. {
  528. unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec, new_tb_to_xs, new_xsec, new_stamp_xsec;
  529. unsigned long tb_ticks_per_sec_delta;
  530. long delta_freq, ltemp;
  531. struct div_result divres;
  532. unsigned long flags;
  533. struct gettimeofday_vars * temp_varp;
  534. unsigned temp_idx;
  535. long singleshot_ppm = 0;
  536. /* Compute parts per million frequency adjustment to accomplish the time adjustment
  537. implied by time_offset to be applied over the elapsed time indicated by time_constant.
  538. Use SHIFT_USEC to get it into the same units as time_freq. */
  539. if ( time_offset < 0 ) {
  540. ltemp = -time_offset;
  541. ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
  542. ltemp >>= SHIFT_KG + time_constant;
  543. ltemp = -ltemp;
  544. }
  545. else {
  546. ltemp = time_offset;
  547. ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
  548. ltemp >>= SHIFT_KG + time_constant;
  549. }
  550. /* If there is a single shot time adjustment in progress */
  551. if ( time_adjust ) {
  552. #ifdef DEBUG_PPC_ADJTIMEX
  553. printk("ppc_adjtimex: ");
  554. if ( adjusting_time == 0 )
  555. printk("starting ");
  556. printk("single shot time_adjust = %ld\n", time_adjust);
  557. #endif
  558. adjusting_time = 1;
  559. /* Compute parts per million frequency adjustment to match time_adjust */
  560. singleshot_ppm = tickadj * HZ;
  561. /*
  562. * The adjustment should be tickadj*HZ to match the code in
  563. * linux/kernel/timer.c, but experiments show that this is too
  564. * large. 3/4 of tickadj*HZ seems about right
  565. */
  566. singleshot_ppm -= singleshot_ppm / 4;
  567. /* Use SHIFT_USEC to get it into the same units as time_freq */
  568. singleshot_ppm <<= SHIFT_USEC;
  569. if ( time_adjust < 0 )
  570. singleshot_ppm = -singleshot_ppm;
  571. }
  572. else {
  573. #ifdef DEBUG_PPC_ADJTIMEX
  574. if ( adjusting_time )
  575. printk("ppc_adjtimex: ending single shot time_adjust\n");
  576. #endif
  577. adjusting_time = 0;
  578. }
  579. /* Add up all of the frequency adjustments */
  580. delta_freq = time_freq + ltemp + singleshot_ppm;
  581. /* Compute a new value for tb_ticks_per_sec based on the frequency adjustment */
  582. den = 1000000 * (1 << (SHIFT_USEC - 8));
  583. if ( delta_freq < 0 ) {
  584. tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
  585. new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
  586. }
  587. else {
  588. tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
  589. new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
  590. }
  591. #ifdef DEBUG_PPC_ADJTIMEX
  592. printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
  593. printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
  594. #endif
  595. /* Compute a new value of tb_to_xs (used to convert tb to microseconds and a new value of
  596. stamp_xsec which is the time (in 1/2^20 second units) corresponding to tb_orig_stamp. This
  597. new value of stamp_xsec compensates for the change in frequency (implied by the new tb_to_xs)
  598. which guarantees that the current time remains the same */
  599. write_seqlock_irqsave( &xtime_lock, flags );
  600. tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
  601. div128_by_32( 1024*1024, 0, new_tb_ticks_per_sec, &divres );
  602. new_tb_to_xs = divres.result_low;
  603. new_xsec = mulhdu( tb_ticks, new_tb_to_xs );
  604. old_xsec = mulhdu( tb_ticks, do_gtod.varp->tb_to_xs );
  605. new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
  606. /* There are two copies of tb_to_xs and stamp_xsec so that no lock is needed to access and use these
  607. values in do_gettimeofday. We alternate the copies and as long as a reasonable time elapses between
  608. changes, there will never be inconsistent values. ntpd has a minimum of one minute between updates */
  609. temp_idx = (do_gtod.var_idx == 0);
  610. temp_varp = &do_gtod.vars[temp_idx];
  611. temp_varp->tb_to_xs = new_tb_to_xs;
  612. temp_varp->stamp_xsec = new_stamp_xsec;
  613. temp_varp->tb_orig_stamp = do_gtod.varp->tb_orig_stamp;
  614. smp_mb();
  615. do_gtod.varp = temp_varp;
  616. do_gtod.var_idx = temp_idx;
  617. /*
  618. * tb_update_count is used to allow the problem state gettimeofday code
  619. * to assure itself that it sees a consistent view of the tb_to_xs and
  620. * stamp_xsec variables. It reads the tb_update_count, then reads
  621. * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
  622. * the two values of tb_update_count match and are even then the
  623. * tb_to_xs and stamp_xsec values are consistent. If not, then it
  624. * loops back and reads them again until this criteria is met.
  625. */
  626. ++(systemcfg->tb_update_count);
  627. smp_wmb();
  628. systemcfg->tb_to_xs = new_tb_to_xs;
  629. systemcfg->stamp_xsec = new_stamp_xsec;
  630. smp_wmb();
  631. ++(systemcfg->tb_update_count);
  632. write_sequnlock_irqrestore( &xtime_lock, flags );
  633. }
  634. #define TICK_SIZE tick
  635. #define FEBRUARY 2
  636. #define STARTOFTIME 1970
  637. #define SECDAY 86400L
  638. #define SECYR (SECDAY * 365)
  639. #define leapyear(year) ((year) % 4 == 0)
  640. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  641. #define days_in_month(a) (month_days[(a) - 1])
  642. static int month_days[12] = {
  643. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  644. };
  645. /*
  646. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  647. */
  648. void GregorianDay(struct rtc_time * tm)
  649. {
  650. int leapsToDate;
  651. int lastYear;
  652. int day;
  653. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  654. lastYear=tm->tm_year-1;
  655. /*
  656. * Number of leap corrections to apply up to end of last year
  657. */
  658. leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
  659. /*
  660. * This year is a leap year if it is divisible by 4 except when it is
  661. * divisible by 100 unless it is divisible by 400
  662. *
  663. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
  664. */
  665. if((tm->tm_year%4==0) &&
  666. ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
  667. (tm->tm_mon>2))
  668. {
  669. /*
  670. * We are past Feb. 29 in a leap year
  671. */
  672. day=1;
  673. }
  674. else
  675. {
  676. day=0;
  677. }
  678. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  679. tm->tm_mday;
  680. tm->tm_wday=day%7;
  681. }
  682. void to_tm(int tim, struct rtc_time * tm)
  683. {
  684. register int i;
  685. register long hms, day;
  686. day = tim / SECDAY;
  687. hms = tim % SECDAY;
  688. /* Hours, minutes, seconds are easy */
  689. tm->tm_hour = hms / 3600;
  690. tm->tm_min = (hms % 3600) / 60;
  691. tm->tm_sec = (hms % 3600) % 60;
  692. /* Number of years in days */
  693. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  694. day -= days_in_year(i);
  695. tm->tm_year = i;
  696. /* Number of months in days left */
  697. if (leapyear(tm->tm_year))
  698. days_in_month(FEBRUARY) = 29;
  699. for (i = 1; day >= days_in_month(i); i++)
  700. day -= days_in_month(i);
  701. days_in_month(FEBRUARY) = 28;
  702. tm->tm_mon = i;
  703. /* Days are what is left over (+1) from all that. */
  704. tm->tm_mday = day + 1;
  705. /*
  706. * Determine the day of week
  707. */
  708. GregorianDay(tm);
  709. }
  710. /* Auxiliary function to compute scaling factors */
  711. /* Actually the choice of a timebase running at 1/4 the of the bus
  712. * frequency giving resolution of a few tens of nanoseconds is quite nice.
  713. * It makes this computation very precise (27-28 bits typically) which
  714. * is optimistic considering the stability of most processor clock
  715. * oscillators and the precision with which the timebase frequency
  716. * is measured but does not harm.
  717. */
  718. unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) {
  719. unsigned mlt=0, tmp, err;
  720. /* No concern for performance, it's done once: use a stupid
  721. * but safe and compact method to find the multiplier.
  722. */
  723. for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
  724. if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
  725. }
  726. /* We might still be off by 1 for the best approximation.
  727. * A side effect of this is that if outscale is too large
  728. * the returned value will be zero.
  729. * Many corner cases have been checked and seem to work,
  730. * some might have been forgotten in the test however.
  731. */
  732. err = inscale*(mlt+1);
  733. if (err <= inscale/2) mlt++;
  734. return mlt;
  735. }
  736. /*
  737. * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
  738. * result.
  739. */
  740. void div128_by_32( unsigned long dividend_high, unsigned long dividend_low,
  741. unsigned divisor, struct div_result *dr )
  742. {
  743. unsigned long a,b,c,d, w,x,y,z, ra,rb,rc;
  744. a = dividend_high >> 32;
  745. b = dividend_high & 0xffffffff;
  746. c = dividend_low >> 32;
  747. d = dividend_low & 0xffffffff;
  748. w = a/divisor;
  749. ra = (a - (w * divisor)) << 32;
  750. x = (ra + b)/divisor;
  751. rb = ((ra + b) - (x * divisor)) << 32;
  752. y = (rb + c)/divisor;
  753. rc = ((rb + b) - (y * divisor)) << 32;
  754. z = (rc + d)/divisor;
  755. dr->result_high = (w << 32) + x;
  756. dr->result_low = (y << 32) + z;
  757. }