time.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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_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. /*
  615. * Compute scale factor for sched_clock.
  616. * The calibrate_decr() function has set tb_ticks_per_sec,
  617. * which is the timebase frequency.
  618. * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
  619. * the 128-bit result as a 64.64 fixed-point number.
  620. * We then shift that number right until it is less than 1.0,
  621. * giving us the scale factor and shift count to use in
  622. * sched_clock().
  623. */
  624. div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
  625. scale = res.result_low;
  626. for (shift = 0; res.result_high != 0; ++shift) {
  627. scale = (scale >> 1) | (res.result_high << 63);
  628. res.result_high >>= 1;
  629. }
  630. tb_to_ns_scale = scale;
  631. tb_to_ns_shift = shift;
  632. #ifdef CONFIG_PPC_ISERIES
  633. if (!piranha_simulator)
  634. #endif
  635. tm = get_boot_time();
  636. write_seqlock_irqsave(&xtime_lock, flags);
  637. xtime.tv_sec = tm;
  638. xtime.tv_nsec = 0;
  639. do_gtod.varp = &do_gtod.vars[0];
  640. do_gtod.var_idx = 0;
  641. do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
  642. __get_cpu_var(last_jiffy) = tb_last_stamp;
  643. do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
  644. do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
  645. do_gtod.varp->tb_to_xs = tb_to_xs;
  646. do_gtod.tb_to_us = tb_to_us;
  647. vdso_data->tb_orig_stamp = tb_last_jiffy;
  648. vdso_data->tb_update_count = 0;
  649. vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
  650. vdso_data->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
  651. vdso_data->tb_to_xs = tb_to_xs;
  652. time_freq = 0;
  653. /* If platform provided a timezone (pmac), we correct the time */
  654. if (timezone_offset) {
  655. sys_tz.tz_minuteswest = -timezone_offset / 60;
  656. sys_tz.tz_dsttime = 0;
  657. xtime.tv_sec -= timezone_offset;
  658. }
  659. last_rtc_update = xtime.tv_sec;
  660. set_normalized_timespec(&wall_to_monotonic,
  661. -xtime.tv_sec, -xtime.tv_nsec);
  662. write_sequnlock_irqrestore(&xtime_lock, flags);
  663. /* Not exact, but the timer interrupt takes care of this */
  664. set_dec(tb_ticks_per_jiffy);
  665. }
  666. /*
  667. * After adjtimex is called, adjust the conversion of tb ticks
  668. * to microseconds to keep do_gettimeofday synchronized
  669. * with ntpd.
  670. *
  671. * Use the time_adjust, time_freq and time_offset computed by adjtimex to
  672. * adjust the frequency.
  673. */
  674. /* #define DEBUG_PPC_ADJTIMEX 1 */
  675. void ppc_adjtimex(void)
  676. {
  677. #ifdef CONFIG_PPC64
  678. unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec,
  679. new_tb_to_xs, new_xsec, new_stamp_xsec;
  680. unsigned long tb_ticks_per_sec_delta;
  681. long delta_freq, ltemp;
  682. struct div_result divres;
  683. unsigned long flags;
  684. long singleshot_ppm = 0;
  685. /*
  686. * Compute parts per million frequency adjustment to
  687. * accomplish the time adjustment implied by time_offset to be
  688. * applied over the elapsed time indicated by time_constant.
  689. * Use SHIFT_USEC to get it into the same units as
  690. * time_freq.
  691. */
  692. if ( time_offset < 0 ) {
  693. ltemp = -time_offset;
  694. ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
  695. ltemp >>= SHIFT_KG + time_constant;
  696. ltemp = -ltemp;
  697. } else {
  698. ltemp = time_offset;
  699. ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
  700. ltemp >>= SHIFT_KG + time_constant;
  701. }
  702. /* If there is a single shot time adjustment in progress */
  703. if ( time_adjust ) {
  704. #ifdef DEBUG_PPC_ADJTIMEX
  705. printk("ppc_adjtimex: ");
  706. if ( adjusting_time == 0 )
  707. printk("starting ");
  708. printk("single shot time_adjust = %ld\n", time_adjust);
  709. #endif
  710. adjusting_time = 1;
  711. /*
  712. * Compute parts per million frequency adjustment
  713. * to match time_adjust
  714. */
  715. singleshot_ppm = tickadj * HZ;
  716. /*
  717. * The adjustment should be tickadj*HZ to match the code in
  718. * linux/kernel/timer.c, but experiments show that this is too
  719. * large. 3/4 of tickadj*HZ seems about right
  720. */
  721. singleshot_ppm -= singleshot_ppm / 4;
  722. /* Use SHIFT_USEC to get it into the same units as time_freq */
  723. singleshot_ppm <<= SHIFT_USEC;
  724. if ( time_adjust < 0 )
  725. singleshot_ppm = -singleshot_ppm;
  726. }
  727. else {
  728. #ifdef DEBUG_PPC_ADJTIMEX
  729. if ( adjusting_time )
  730. printk("ppc_adjtimex: ending single shot time_adjust\n");
  731. #endif
  732. adjusting_time = 0;
  733. }
  734. /* Add up all of the frequency adjustments */
  735. delta_freq = time_freq + ltemp + singleshot_ppm;
  736. /*
  737. * Compute a new value for tb_ticks_per_sec based on
  738. * the frequency adjustment
  739. */
  740. den = 1000000 * (1 << (SHIFT_USEC - 8));
  741. if ( delta_freq < 0 ) {
  742. tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
  743. new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
  744. }
  745. else {
  746. tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
  747. new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
  748. }
  749. #ifdef DEBUG_PPC_ADJTIMEX
  750. printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
  751. printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
  752. #endif
  753. /*
  754. * Compute a new value of tb_to_xs (used to convert tb to
  755. * microseconds) and a new value of stamp_xsec which is the
  756. * time (in 1/2^20 second units) corresponding to
  757. * tb_orig_stamp. This new value of stamp_xsec compensates
  758. * for the change in frequency (implied by the new tb_to_xs)
  759. * which guarantees that the current time remains the same.
  760. */
  761. write_seqlock_irqsave( &xtime_lock, flags );
  762. tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
  763. div128_by_32(1024*1024, 0, new_tb_ticks_per_sec, &divres);
  764. new_tb_to_xs = divres.result_low;
  765. new_xsec = mulhdu(tb_ticks, new_tb_to_xs);
  766. old_xsec = mulhdu(tb_ticks, do_gtod.varp->tb_to_xs);
  767. new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
  768. update_gtod(do_gtod.varp->tb_orig_stamp, new_stamp_xsec, new_tb_to_xs);
  769. write_sequnlock_irqrestore( &xtime_lock, flags );
  770. #endif /* CONFIG_PPC64 */
  771. }
  772. #define FEBRUARY 2
  773. #define STARTOFTIME 1970
  774. #define SECDAY 86400L
  775. #define SECYR (SECDAY * 365)
  776. #define leapyear(year) ((year) % 4 == 0 && \
  777. ((year) % 100 != 0 || (year) % 400 == 0))
  778. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  779. #define days_in_month(a) (month_days[(a) - 1])
  780. static int month_days[12] = {
  781. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  782. };
  783. /*
  784. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  785. */
  786. void GregorianDay(struct rtc_time * tm)
  787. {
  788. int leapsToDate;
  789. int lastYear;
  790. int day;
  791. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  792. lastYear = tm->tm_year - 1;
  793. /*
  794. * Number of leap corrections to apply up to end of last year
  795. */
  796. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  797. /*
  798. * This year is a leap year if it is divisible by 4 except when it is
  799. * divisible by 100 unless it is divisible by 400
  800. *
  801. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  802. */
  803. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  804. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  805. tm->tm_mday;
  806. tm->tm_wday = day % 7;
  807. }
  808. void to_tm(int tim, struct rtc_time * tm)
  809. {
  810. register int i;
  811. register long hms, day;
  812. day = tim / SECDAY;
  813. hms = tim % SECDAY;
  814. /* Hours, minutes, seconds are easy */
  815. tm->tm_hour = hms / 3600;
  816. tm->tm_min = (hms % 3600) / 60;
  817. tm->tm_sec = (hms % 3600) % 60;
  818. /* Number of years in days */
  819. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  820. day -= days_in_year(i);
  821. tm->tm_year = i;
  822. /* Number of months in days left */
  823. if (leapyear(tm->tm_year))
  824. days_in_month(FEBRUARY) = 29;
  825. for (i = 1; day >= days_in_month(i); i++)
  826. day -= days_in_month(i);
  827. days_in_month(FEBRUARY) = 28;
  828. tm->tm_mon = i;
  829. /* Days are what is left over (+1) from all that. */
  830. tm->tm_mday = day + 1;
  831. /*
  832. * Determine the day of week
  833. */
  834. GregorianDay(tm);
  835. }
  836. /* Auxiliary function to compute scaling factors */
  837. /* Actually the choice of a timebase running at 1/4 the of the bus
  838. * frequency giving resolution of a few tens of nanoseconds is quite nice.
  839. * It makes this computation very precise (27-28 bits typically) which
  840. * is optimistic considering the stability of most processor clock
  841. * oscillators and the precision with which the timebase frequency
  842. * is measured but does not harm.
  843. */
  844. unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
  845. {
  846. unsigned mlt=0, tmp, err;
  847. /* No concern for performance, it's done once: use a stupid
  848. * but safe and compact method to find the multiplier.
  849. */
  850. for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
  851. if (mulhwu(inscale, mlt|tmp) < outscale)
  852. mlt |= tmp;
  853. }
  854. /* We might still be off by 1 for the best approximation.
  855. * A side effect of this is that if outscale is too large
  856. * the returned value will be zero.
  857. * Many corner cases have been checked and seem to work,
  858. * some might have been forgotten in the test however.
  859. */
  860. err = inscale * (mlt+1);
  861. if (err <= inscale/2)
  862. mlt++;
  863. return mlt;
  864. }
  865. /*
  866. * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
  867. * result.
  868. */
  869. void div128_by_32(u64 dividend_high, u64 dividend_low,
  870. unsigned divisor, struct div_result *dr)
  871. {
  872. unsigned long a, b, c, d;
  873. unsigned long w, x, y, z;
  874. u64 ra, rb, rc;
  875. a = dividend_high >> 32;
  876. b = dividend_high & 0xffffffff;
  877. c = dividend_low >> 32;
  878. d = dividend_low & 0xffffffff;
  879. w = a / divisor;
  880. ra = ((u64)(a - (w * divisor)) << 32) + b;
  881. rb = ((u64) do_div(ra, divisor) << 32) + c;
  882. x = ra;
  883. rc = ((u64) do_div(rb, divisor) << 32) + d;
  884. y = rb;
  885. do_div(rc, divisor);
  886. z = rc;
  887. dr->result_high = ((u64)w << 32) + x;
  888. dr->result_low = ((u64)y << 32) + z;
  889. }