time.c 29 KB

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