time.c 27 KB

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