time.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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/errno.h>
  35. #include <linux/module.h>
  36. #include <linux/sched.h>
  37. #include <linux/kernel.h>
  38. #include <linux/param.h>
  39. #include <linux/string.h>
  40. #include <linux/mm.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/timex.h>
  43. #include <linux/kernel_stat.h>
  44. #include <linux/time.h>
  45. #include <linux/init.h>
  46. #include <linux/profile.h>
  47. #include <linux/cpu.h>
  48. #include <linux/security.h>
  49. #include <linux/percpu.h>
  50. #include <linux/rtc.h>
  51. #include <linux/jiffies.h>
  52. #include <linux/posix-timers.h>
  53. #include <linux/irq.h>
  54. #include <asm/io.h>
  55. #include <asm/processor.h>
  56. #include <asm/nvram.h>
  57. #include <asm/cache.h>
  58. #include <asm/machdep.h>
  59. #include <asm/uaccess.h>
  60. #include <asm/time.h>
  61. #include <asm/prom.h>
  62. #include <asm/irq.h>
  63. #include <asm/div64.h>
  64. #include <asm/smp.h>
  65. #include <asm/vdso_datapage.h>
  66. #include <asm/firmware.h>
  67. #ifdef CONFIG_PPC_ISERIES
  68. #include <asm/iseries/it_lp_queue.h>
  69. #include <asm/iseries/hv_call_xm.h>
  70. #endif
  71. /* powerpc clocksource/clockevent code */
  72. #include <linux/clockchips.h>
  73. #include <linux/clocksource.h>
  74. static cycle_t rtc_read(void);
  75. static struct clocksource clocksource_rtc = {
  76. .name = "rtc",
  77. .rating = 400,
  78. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  79. .mask = CLOCKSOURCE_MASK(64),
  80. .shift = 22,
  81. .mult = 0, /* To be filled in */
  82. .read = rtc_read,
  83. };
  84. static cycle_t timebase_read(void);
  85. static struct clocksource clocksource_timebase = {
  86. .name = "timebase",
  87. .rating = 400,
  88. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  89. .mask = CLOCKSOURCE_MASK(64),
  90. .shift = 22,
  91. .mult = 0, /* To be filled in */
  92. .read = timebase_read,
  93. };
  94. #define DECREMENTER_MAX 0x7fffffff
  95. static int decrementer_set_next_event(unsigned long evt,
  96. struct clock_event_device *dev);
  97. static void decrementer_set_mode(enum clock_event_mode mode,
  98. struct clock_event_device *dev);
  99. static struct clock_event_device decrementer_clockevent = {
  100. .name = "decrementer",
  101. .rating = 200,
  102. .shift = 16,
  103. .mult = 0, /* To be filled in */
  104. .irq = 0,
  105. .set_next_event = decrementer_set_next_event,
  106. .set_mode = decrementer_set_mode,
  107. .features = CLOCK_EVT_FEAT_ONESHOT,
  108. };
  109. static DEFINE_PER_CPU(struct clock_event_device, decrementers);
  110. void init_decrementer_clockevent(void);
  111. static DEFINE_PER_CPU(u64, decrementer_next_tb);
  112. #ifdef CONFIG_PPC_ISERIES
  113. static unsigned long __initdata iSeries_recal_titan;
  114. static signed long __initdata iSeries_recal_tb;
  115. /* Forward declaration is only needed for iSereis compiles */
  116. void __init clocksource_init(void);
  117. #endif
  118. #define XSEC_PER_SEC (1024*1024)
  119. #ifdef CONFIG_PPC64
  120. #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
  121. #else
  122. /* compute ((xsec << 12) * max) >> 32 */
  123. #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
  124. #endif
  125. unsigned long tb_ticks_per_jiffy;
  126. unsigned long tb_ticks_per_usec = 100; /* sane default */
  127. EXPORT_SYMBOL(tb_ticks_per_usec);
  128. unsigned long tb_ticks_per_sec;
  129. EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */
  130. u64 tb_to_xs;
  131. unsigned tb_to_us;
  132. #define TICKLEN_SCALE TICK_LENGTH_SHIFT
  133. u64 last_tick_len; /* units are ns / 2^TICKLEN_SCALE */
  134. u64 ticklen_to_xs; /* 0.64 fraction */
  135. /* If last_tick_len corresponds to about 1/HZ seconds, then
  136. last_tick_len << TICKLEN_SHIFT will be about 2^63. */
  137. #define TICKLEN_SHIFT (63 - 30 - TICKLEN_SCALE + SHIFT_HZ)
  138. DEFINE_SPINLOCK(rtc_lock);
  139. EXPORT_SYMBOL_GPL(rtc_lock);
  140. static u64 tb_to_ns_scale __read_mostly;
  141. static unsigned tb_to_ns_shift __read_mostly;
  142. static unsigned long boot_tb __read_mostly;
  143. struct gettimeofday_struct do_gtod;
  144. extern struct timezone sys_tz;
  145. static long timezone_offset;
  146. unsigned long ppc_proc_freq;
  147. EXPORT_SYMBOL(ppc_proc_freq);
  148. unsigned long ppc_tb_freq;
  149. static u64 tb_last_jiffy __cacheline_aligned_in_smp;
  150. static DEFINE_PER_CPU(u64, last_jiffy);
  151. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  152. /*
  153. * Factors for converting from cputime_t (timebase ticks) to
  154. * jiffies, milliseconds, seconds, and clock_t (1/USER_HZ seconds).
  155. * These are all stored as 0.64 fixed-point binary fractions.
  156. */
  157. u64 __cputime_jiffies_factor;
  158. EXPORT_SYMBOL(__cputime_jiffies_factor);
  159. u64 __cputime_msec_factor;
  160. EXPORT_SYMBOL(__cputime_msec_factor);
  161. u64 __cputime_sec_factor;
  162. EXPORT_SYMBOL(__cputime_sec_factor);
  163. u64 __cputime_clockt_factor;
  164. EXPORT_SYMBOL(__cputime_clockt_factor);
  165. static void calc_cputime_factors(void)
  166. {
  167. struct div_result res;
  168. div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
  169. __cputime_jiffies_factor = res.result_low;
  170. div128_by_32(1000, 0, tb_ticks_per_sec, &res);
  171. __cputime_msec_factor = res.result_low;
  172. div128_by_32(1, 0, tb_ticks_per_sec, &res);
  173. __cputime_sec_factor = res.result_low;
  174. div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
  175. __cputime_clockt_factor = res.result_low;
  176. }
  177. /*
  178. * Read the PURR on systems that have it, otherwise the timebase.
  179. */
  180. static u64 read_purr(void)
  181. {
  182. if (cpu_has_feature(CPU_FTR_PURR))
  183. return mfspr(SPRN_PURR);
  184. return mftb();
  185. }
  186. /*
  187. * Account time for a transition between system, hard irq
  188. * or soft irq state.
  189. */
  190. void account_system_vtime(struct task_struct *tsk)
  191. {
  192. u64 now, delta;
  193. unsigned long flags;
  194. local_irq_save(flags);
  195. now = read_purr();
  196. delta = now - get_paca()->startpurr;
  197. get_paca()->startpurr = now;
  198. if (!in_interrupt()) {
  199. delta += get_paca()->system_time;
  200. get_paca()->system_time = 0;
  201. }
  202. account_system_time(tsk, 0, delta);
  203. local_irq_restore(flags);
  204. }
  205. /*
  206. * Transfer the user and system times accumulated in the paca
  207. * by the exception entry and exit code to the generic process
  208. * user and system time records.
  209. * Must be called with interrupts disabled.
  210. */
  211. void account_process_vtime(struct task_struct *tsk)
  212. {
  213. cputime_t utime;
  214. utime = get_paca()->user_time;
  215. get_paca()->user_time = 0;
  216. account_user_time(tsk, utime);
  217. }
  218. static void account_process_time(struct pt_regs *regs)
  219. {
  220. int cpu = smp_processor_id();
  221. account_process_vtime(current);
  222. run_local_timers();
  223. if (rcu_pending(cpu))
  224. rcu_check_callbacks(cpu, user_mode(regs));
  225. scheduler_tick();
  226. run_posix_cpu_timers(current);
  227. }
  228. /*
  229. * Stuff for accounting stolen time.
  230. */
  231. struct cpu_purr_data {
  232. int initialized; /* thread is running */
  233. u64 tb; /* last TB value read */
  234. u64 purr; /* last PURR value read */
  235. };
  236. /*
  237. * Each entry in the cpu_purr_data array is manipulated only by its
  238. * "owner" cpu -- usually in the timer interrupt but also occasionally
  239. * in process context for cpu online. As long as cpus do not touch
  240. * each others' cpu_purr_data, disabling local interrupts is
  241. * sufficient to serialize accesses.
  242. */
  243. static DEFINE_PER_CPU(struct cpu_purr_data, cpu_purr_data);
  244. static void snapshot_tb_and_purr(void *data)
  245. {
  246. unsigned long flags;
  247. struct cpu_purr_data *p = &__get_cpu_var(cpu_purr_data);
  248. local_irq_save(flags);
  249. p->tb = get_tb_or_rtc();
  250. p->purr = mfspr(SPRN_PURR);
  251. wmb();
  252. p->initialized = 1;
  253. local_irq_restore(flags);
  254. }
  255. /*
  256. * Called during boot when all cpus have come up.
  257. */
  258. void snapshot_timebases(void)
  259. {
  260. if (!cpu_has_feature(CPU_FTR_PURR))
  261. return;
  262. on_each_cpu(snapshot_tb_and_purr, NULL, 0, 1);
  263. }
  264. /*
  265. * Must be called with interrupts disabled.
  266. */
  267. void calculate_steal_time(void)
  268. {
  269. u64 tb, purr;
  270. s64 stolen;
  271. struct cpu_purr_data *pme;
  272. if (!cpu_has_feature(CPU_FTR_PURR))
  273. return;
  274. pme = &per_cpu(cpu_purr_data, smp_processor_id());
  275. if (!pme->initialized)
  276. return; /* this can happen in early boot */
  277. tb = mftb();
  278. purr = mfspr(SPRN_PURR);
  279. stolen = (tb - pme->tb) - (purr - pme->purr);
  280. if (stolen > 0)
  281. account_steal_time(current, stolen);
  282. pme->tb = tb;
  283. pme->purr = purr;
  284. }
  285. #ifdef CONFIG_PPC_SPLPAR
  286. /*
  287. * Must be called before the cpu is added to the online map when
  288. * a cpu is being brought up at runtime.
  289. */
  290. static void snapshot_purr(void)
  291. {
  292. struct cpu_purr_data *pme;
  293. unsigned long flags;
  294. if (!cpu_has_feature(CPU_FTR_PURR))
  295. return;
  296. local_irq_save(flags);
  297. pme = &per_cpu(cpu_purr_data, smp_processor_id());
  298. pme->tb = mftb();
  299. pme->purr = mfspr(SPRN_PURR);
  300. pme->initialized = 1;
  301. local_irq_restore(flags);
  302. }
  303. #endif /* CONFIG_PPC_SPLPAR */
  304. #else /* ! CONFIG_VIRT_CPU_ACCOUNTING */
  305. #define calc_cputime_factors()
  306. #define account_process_time(regs) update_process_times(user_mode(regs))
  307. #define calculate_steal_time() do { } while (0)
  308. #endif
  309. #if !(defined(CONFIG_VIRT_CPU_ACCOUNTING) && defined(CONFIG_PPC_SPLPAR))
  310. #define snapshot_purr() do { } while (0)
  311. #endif
  312. /*
  313. * Called when a cpu comes up after the system has finished booting,
  314. * i.e. as a result of a hotplug cpu action.
  315. */
  316. void snapshot_timebase(void)
  317. {
  318. __get_cpu_var(last_jiffy) = get_tb_or_rtc();
  319. snapshot_purr();
  320. }
  321. void __delay(unsigned long loops)
  322. {
  323. unsigned long start;
  324. int diff;
  325. if (__USE_RTC()) {
  326. start = get_rtcl();
  327. do {
  328. /* the RTCL register wraps at 1000000000 */
  329. diff = get_rtcl() - start;
  330. if (diff < 0)
  331. diff += 1000000000;
  332. } while (diff < loops);
  333. } else {
  334. start = get_tbl();
  335. while (get_tbl() - start < loops)
  336. HMT_low();
  337. HMT_medium();
  338. }
  339. }
  340. EXPORT_SYMBOL(__delay);
  341. void udelay(unsigned long usecs)
  342. {
  343. __delay(tb_ticks_per_usec * usecs);
  344. }
  345. EXPORT_SYMBOL(udelay);
  346. /*
  347. * There are two copies of tb_to_xs and stamp_xsec so that no
  348. * lock is needed to access and use these values in
  349. * do_gettimeofday. We alternate the copies and as long as a
  350. * reasonable time elapses between changes, there will never
  351. * be inconsistent values. ntpd has a minimum of one minute
  352. * between updates.
  353. */
  354. static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
  355. u64 new_tb_to_xs)
  356. {
  357. unsigned temp_idx;
  358. struct gettimeofday_vars *temp_varp;
  359. temp_idx = (do_gtod.var_idx == 0);
  360. temp_varp = &do_gtod.vars[temp_idx];
  361. temp_varp->tb_to_xs = new_tb_to_xs;
  362. temp_varp->tb_orig_stamp = new_tb_stamp;
  363. temp_varp->stamp_xsec = new_stamp_xsec;
  364. smp_mb();
  365. do_gtod.varp = temp_varp;
  366. do_gtod.var_idx = temp_idx;
  367. /*
  368. * tb_update_count is used to allow the userspace gettimeofday code
  369. * to assure itself that it sees a consistent view of the tb_to_xs and
  370. * stamp_xsec variables. It reads the tb_update_count, then reads
  371. * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
  372. * the two values of tb_update_count match and are even then the
  373. * tb_to_xs and stamp_xsec values are consistent. If not, then it
  374. * loops back and reads them again until this criteria is met.
  375. * We expect the caller to have done the first increment of
  376. * vdso_data->tb_update_count already.
  377. */
  378. vdso_data->tb_orig_stamp = new_tb_stamp;
  379. vdso_data->stamp_xsec = new_stamp_xsec;
  380. vdso_data->tb_to_xs = new_tb_to_xs;
  381. vdso_data->wtom_clock_sec = wall_to_monotonic.tv_sec;
  382. vdso_data->wtom_clock_nsec = wall_to_monotonic.tv_nsec;
  383. smp_wmb();
  384. ++(vdso_data->tb_update_count);
  385. }
  386. #ifdef CONFIG_SMP
  387. unsigned long profile_pc(struct pt_regs *regs)
  388. {
  389. unsigned long pc = instruction_pointer(regs);
  390. if (in_lock_functions(pc))
  391. return regs->link;
  392. return pc;
  393. }
  394. EXPORT_SYMBOL(profile_pc);
  395. #endif
  396. #ifdef CONFIG_PPC_ISERIES
  397. /*
  398. * This function recalibrates the timebase based on the 49-bit time-of-day
  399. * value in the Titan chip. The Titan is much more accurate than the value
  400. * returned by the service processor for the timebase frequency.
  401. */
  402. static int __init iSeries_tb_recal(void)
  403. {
  404. struct div_result divres;
  405. unsigned long titan, tb;
  406. /* Make sure we only run on iSeries */
  407. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  408. return -ENODEV;
  409. tb = get_tb();
  410. titan = HvCallXm_loadTod();
  411. if ( iSeries_recal_titan ) {
  412. unsigned long tb_ticks = tb - iSeries_recal_tb;
  413. unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
  414. unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
  415. unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
  416. long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
  417. char sign = '+';
  418. /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
  419. new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
  420. if ( tick_diff < 0 ) {
  421. tick_diff = -tick_diff;
  422. sign = '-';
  423. }
  424. if ( tick_diff ) {
  425. if ( tick_diff < tb_ticks_per_jiffy/25 ) {
  426. printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
  427. new_tb_ticks_per_jiffy, sign, tick_diff );
  428. tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
  429. tb_ticks_per_sec = new_tb_ticks_per_sec;
  430. calc_cputime_factors();
  431. div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
  432. do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
  433. tb_to_xs = divres.result_low;
  434. do_gtod.varp->tb_to_xs = tb_to_xs;
  435. vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
  436. vdso_data->tb_to_xs = tb_to_xs;
  437. }
  438. else {
  439. printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
  440. " new tb_ticks_per_jiffy = %lu\n"
  441. " old tb_ticks_per_jiffy = %lu\n",
  442. new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
  443. }
  444. }
  445. }
  446. iSeries_recal_titan = titan;
  447. iSeries_recal_tb = tb;
  448. /* Called here as now we know accurate values for the timebase */
  449. clocksource_init();
  450. return 0;
  451. }
  452. late_initcall(iSeries_tb_recal);
  453. /* Called from platform early init */
  454. void __init iSeries_time_init_early(void)
  455. {
  456. iSeries_recal_tb = get_tb();
  457. iSeries_recal_titan = HvCallXm_loadTod();
  458. }
  459. #endif /* CONFIG_PPC_ISERIES */
  460. /*
  461. * For iSeries shared processors, we have to let the hypervisor
  462. * set the hardware decrementer. We set a virtual decrementer
  463. * in the lppaca and call the hypervisor if the virtual
  464. * decrementer is less than the current value in the hardware
  465. * decrementer. (almost always the new decrementer value will
  466. * be greater than the current hardware decementer so the hypervisor
  467. * call will not be needed)
  468. */
  469. /*
  470. * timer_interrupt - gets called when the decrementer overflows,
  471. * with interrupts disabled.
  472. */
  473. void timer_interrupt(struct pt_regs * regs)
  474. {
  475. struct pt_regs *old_regs;
  476. int cpu = smp_processor_id();
  477. struct clock_event_device *evt = &per_cpu(decrementers, cpu);
  478. u64 now;
  479. /* Ensure a positive value is written to the decrementer, or else
  480. * some CPUs will continuue to take decrementer exceptions */
  481. set_dec(DECREMENTER_MAX);
  482. #ifdef CONFIG_PPC32
  483. if (atomic_read(&ppc_n_lost_interrupts) != 0)
  484. do_IRQ(regs);
  485. #endif
  486. now = get_tb_or_rtc();
  487. if (now < per_cpu(decrementer_next_tb, cpu)) {
  488. /* not time for this event yet */
  489. now = per_cpu(decrementer_next_tb, cpu) - now;
  490. if (now <= DECREMENTER_MAX)
  491. set_dec((unsigned int)now - 1);
  492. return;
  493. }
  494. old_regs = set_irq_regs(regs);
  495. irq_enter();
  496. calculate_steal_time();
  497. #ifdef CONFIG_PPC_ISERIES
  498. if (firmware_has_feature(FW_FEATURE_ISERIES))
  499. get_lppaca()->int_dword.fields.decr_int = 0;
  500. #endif
  501. /*
  502. * We cannot disable the decrementer, so in the period
  503. * between this cpu's being marked offline in cpu_online_map
  504. * and calling stop-self, it is taking timer interrupts.
  505. * Avoid calling into the scheduler rebalancing code if this
  506. * is the case.
  507. */
  508. if (!cpu_is_offline(cpu))
  509. account_process_time(regs);
  510. if (evt->event_handler)
  511. evt->event_handler(evt);
  512. else
  513. evt->set_next_event(DECREMENTER_MAX, evt);
  514. #ifdef CONFIG_PPC_ISERIES
  515. if (firmware_has_feature(FW_FEATURE_ISERIES) && hvlpevent_is_pending())
  516. process_hvlpevents();
  517. #endif
  518. #ifdef CONFIG_PPC64
  519. /* collect purr register values often, for accurate calculations */
  520. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  521. struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
  522. cu->current_tb = mfspr(SPRN_PURR);
  523. }
  524. #endif
  525. irq_exit();
  526. set_irq_regs(old_regs);
  527. }
  528. void wakeup_decrementer(void)
  529. {
  530. unsigned long ticks;
  531. /*
  532. * The timebase gets saved on sleep and restored on wakeup,
  533. * so all we need to do is to reset the decrementer.
  534. */
  535. ticks = tb_ticks_since(__get_cpu_var(last_jiffy));
  536. if (ticks < tb_ticks_per_jiffy)
  537. ticks = tb_ticks_per_jiffy - ticks;
  538. else
  539. ticks = 1;
  540. set_dec(ticks);
  541. }
  542. #ifdef CONFIG_SMP
  543. void __init smp_space_timers(unsigned int max_cpus)
  544. {
  545. int i;
  546. u64 previous_tb = per_cpu(last_jiffy, boot_cpuid);
  547. /* make sure tb > per_cpu(last_jiffy, cpu) for all cpus always */
  548. previous_tb -= tb_ticks_per_jiffy;
  549. for_each_possible_cpu(i) {
  550. if (i == boot_cpuid)
  551. continue;
  552. per_cpu(last_jiffy, i) = previous_tb;
  553. }
  554. }
  555. #endif
  556. /*
  557. * Scheduler clock - returns current time in nanosec units.
  558. *
  559. * Note: mulhdu(a, b) (multiply high double unsigned) returns
  560. * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
  561. * are 64-bit unsigned numbers.
  562. */
  563. unsigned long long sched_clock(void)
  564. {
  565. if (__USE_RTC())
  566. return get_rtc();
  567. return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
  568. }
  569. static int __init get_freq(char *name, int cells, unsigned long *val)
  570. {
  571. struct device_node *cpu;
  572. const unsigned int *fp;
  573. int found = 0;
  574. /* The cpu node should have timebase and clock frequency properties */
  575. cpu = of_find_node_by_type(NULL, "cpu");
  576. if (cpu) {
  577. fp = of_get_property(cpu, name, NULL);
  578. if (fp) {
  579. found = 1;
  580. *val = of_read_ulong(fp, cells);
  581. }
  582. of_node_put(cpu);
  583. }
  584. return found;
  585. }
  586. void __init generic_calibrate_decr(void)
  587. {
  588. ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
  589. if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
  590. !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
  591. printk(KERN_ERR "WARNING: Estimating decrementer frequency "
  592. "(not found)\n");
  593. }
  594. ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */
  595. if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
  596. !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
  597. printk(KERN_ERR "WARNING: Estimating processor frequency "
  598. "(not found)\n");
  599. }
  600. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  601. /* Set the time base to zero */
  602. mtspr(SPRN_TBWL, 0);
  603. mtspr(SPRN_TBWU, 0);
  604. /* Clear any pending timer interrupts */
  605. mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
  606. /* Enable decrementer interrupt */
  607. mtspr(SPRN_TCR, TCR_DIE);
  608. #endif
  609. }
  610. int update_persistent_clock(struct timespec now)
  611. {
  612. struct rtc_time tm;
  613. if (!ppc_md.set_rtc_time)
  614. return 0;
  615. to_tm(now.tv_sec + 1 + timezone_offset, &tm);
  616. tm.tm_year -= 1900;
  617. tm.tm_mon -= 1;
  618. return ppc_md.set_rtc_time(&tm);
  619. }
  620. unsigned long read_persistent_clock(void)
  621. {
  622. struct rtc_time tm;
  623. static int first = 1;
  624. /* XXX this is a litle fragile but will work okay in the short term */
  625. if (first) {
  626. first = 0;
  627. if (ppc_md.time_init)
  628. timezone_offset = ppc_md.time_init();
  629. /* get_boot_time() isn't guaranteed to be safe to call late */
  630. if (ppc_md.get_boot_time)
  631. return ppc_md.get_boot_time() -timezone_offset;
  632. }
  633. if (!ppc_md.get_rtc_time)
  634. return 0;
  635. ppc_md.get_rtc_time(&tm);
  636. return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  637. tm.tm_hour, tm.tm_min, tm.tm_sec);
  638. }
  639. /* clocksource code */
  640. static cycle_t rtc_read(void)
  641. {
  642. return (cycle_t)get_rtc();
  643. }
  644. static cycle_t timebase_read(void)
  645. {
  646. return (cycle_t)get_tb();
  647. }
  648. void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
  649. {
  650. u64 t2x, stamp_xsec;
  651. if (clock != &clocksource_timebase)
  652. return;
  653. /* Make userspace gettimeofday spin until we're done. */
  654. ++vdso_data->tb_update_count;
  655. smp_mb();
  656. /* XXX this assumes clock->shift == 22 */
  657. /* 4611686018 ~= 2^(20+64-22) / 1e9 */
  658. t2x = (u64) clock->mult * 4611686018ULL;
  659. stamp_xsec = (u64) xtime.tv_nsec * XSEC_PER_SEC;
  660. do_div(stamp_xsec, 1000000000);
  661. stamp_xsec += (u64) xtime.tv_sec * XSEC_PER_SEC;
  662. update_gtod(clock->cycle_last, stamp_xsec, t2x);
  663. }
  664. void update_vsyscall_tz(void)
  665. {
  666. /* Make userspace gettimeofday spin until we're done. */
  667. ++vdso_data->tb_update_count;
  668. smp_mb();
  669. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  670. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  671. smp_mb();
  672. ++vdso_data->tb_update_count;
  673. }
  674. void __init clocksource_init(void)
  675. {
  676. struct clocksource *clock;
  677. if (__USE_RTC())
  678. clock = &clocksource_rtc;
  679. else
  680. clock = &clocksource_timebase;
  681. clock->mult = clocksource_hz2mult(tb_ticks_per_sec, clock->shift);
  682. if (clocksource_register(clock)) {
  683. printk(KERN_ERR "clocksource: %s is already registered\n",
  684. clock->name);
  685. return;
  686. }
  687. printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
  688. clock->name, clock->mult, clock->shift);
  689. }
  690. static int decrementer_set_next_event(unsigned long evt,
  691. struct clock_event_device *dev)
  692. {
  693. __get_cpu_var(decrementer_next_tb) = get_tb_or_rtc() + evt;
  694. /* The decrementer interrupts on the 0 -> -1 transition */
  695. if (evt)
  696. --evt;
  697. set_dec(evt);
  698. return 0;
  699. }
  700. static void decrementer_set_mode(enum clock_event_mode mode,
  701. struct clock_event_device *dev)
  702. {
  703. if (mode != CLOCK_EVT_MODE_ONESHOT)
  704. decrementer_set_next_event(DECREMENTER_MAX, dev);
  705. }
  706. static void register_decrementer_clockevent(int cpu)
  707. {
  708. struct clock_event_device *dec = &per_cpu(decrementers, cpu);
  709. *dec = decrementer_clockevent;
  710. dec->cpumask = cpumask_of_cpu(cpu);
  711. printk(KERN_ERR "clockevent: %s mult[%lx] shift[%d] cpu[%d]\n",
  712. dec->name, dec->mult, dec->shift, cpu);
  713. clockevents_register_device(dec);
  714. }
  715. void init_decrementer_clockevent(void)
  716. {
  717. int cpu = smp_processor_id();
  718. decrementer_clockevent.mult = div_sc(ppc_tb_freq, NSEC_PER_SEC,
  719. decrementer_clockevent.shift);
  720. decrementer_clockevent.max_delta_ns =
  721. clockevent_delta2ns(DECREMENTER_MAX, &decrementer_clockevent);
  722. decrementer_clockevent.min_delta_ns = 1000;
  723. register_decrementer_clockevent(cpu);
  724. }
  725. void secondary_cpu_time_init(void)
  726. {
  727. /* FIME: Should make unrelatred change to move snapshot_timebase
  728. * call here ! */
  729. register_decrementer_clockevent(smp_processor_id());
  730. }
  731. /* This function is only called on the boot processor */
  732. void __init time_init(void)
  733. {
  734. unsigned long flags;
  735. struct div_result res;
  736. u64 scale, x;
  737. unsigned shift;
  738. if (__USE_RTC()) {
  739. /* 601 processor: dec counts down by 128 every 128ns */
  740. ppc_tb_freq = 1000000000;
  741. tb_last_jiffy = get_rtcl();
  742. } else {
  743. /* Normal PowerPC with timebase register */
  744. ppc_md.calibrate_decr();
  745. printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
  746. ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
  747. printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
  748. ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
  749. tb_last_jiffy = get_tb();
  750. }
  751. tb_ticks_per_jiffy = ppc_tb_freq / HZ;
  752. tb_ticks_per_sec = ppc_tb_freq;
  753. tb_ticks_per_usec = ppc_tb_freq / 1000000;
  754. tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
  755. calc_cputime_factors();
  756. /*
  757. * Calculate the length of each tick in ns. It will not be
  758. * exactly 1e9/HZ unless ppc_tb_freq is divisible by HZ.
  759. * We compute 1e9 * tb_ticks_per_jiffy / ppc_tb_freq,
  760. * rounded up.
  761. */
  762. x = (u64) NSEC_PER_SEC * tb_ticks_per_jiffy + ppc_tb_freq - 1;
  763. do_div(x, ppc_tb_freq);
  764. tick_nsec = x;
  765. last_tick_len = x << TICKLEN_SCALE;
  766. /*
  767. * Compute ticklen_to_xs, which is a factor which gets multiplied
  768. * by (last_tick_len << TICKLEN_SHIFT) to get a tb_to_xs value.
  769. * It is computed as:
  770. * ticklen_to_xs = 2^N / (tb_ticks_per_jiffy * 1e9)
  771. * where N = 64 + 20 - TICKLEN_SCALE - TICKLEN_SHIFT
  772. * which turns out to be N = 51 - SHIFT_HZ.
  773. * This gives the result as a 0.64 fixed-point fraction.
  774. * That value is reduced by an offset amounting to 1 xsec per
  775. * 2^31 timebase ticks to avoid problems with time going backwards
  776. * by 1 xsec when we do timer_recalc_offset due to losing the
  777. * fractional xsec. That offset is equal to ppc_tb_freq/2^51
  778. * since there are 2^20 xsec in a second.
  779. */
  780. div128_by_32((1ULL << 51) - ppc_tb_freq, 0,
  781. tb_ticks_per_jiffy << SHIFT_HZ, &res);
  782. div128_by_32(res.result_high, res.result_low, NSEC_PER_SEC, &res);
  783. ticklen_to_xs = res.result_low;
  784. /* Compute tb_to_xs from tick_nsec */
  785. tb_to_xs = mulhdu(last_tick_len << TICKLEN_SHIFT, ticklen_to_xs);
  786. /*
  787. * Compute scale factor for sched_clock.
  788. * The calibrate_decr() function has set tb_ticks_per_sec,
  789. * which is the timebase frequency.
  790. * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
  791. * the 128-bit result as a 64.64 fixed-point number.
  792. * We then shift that number right until it is less than 1.0,
  793. * giving us the scale factor and shift count to use in
  794. * sched_clock().
  795. */
  796. div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
  797. scale = res.result_low;
  798. for (shift = 0; res.result_high != 0; ++shift) {
  799. scale = (scale >> 1) | (res.result_high << 63);
  800. res.result_high >>= 1;
  801. }
  802. tb_to_ns_scale = scale;
  803. tb_to_ns_shift = shift;
  804. /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
  805. boot_tb = get_tb_or_rtc();
  806. write_seqlock_irqsave(&xtime_lock, flags);
  807. /* If platform provided a timezone (pmac), we correct the time */
  808. if (timezone_offset) {
  809. sys_tz.tz_minuteswest = -timezone_offset / 60;
  810. sys_tz.tz_dsttime = 0;
  811. }
  812. do_gtod.varp = &do_gtod.vars[0];
  813. do_gtod.var_idx = 0;
  814. do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
  815. __get_cpu_var(last_jiffy) = tb_last_jiffy;
  816. do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
  817. do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
  818. do_gtod.varp->tb_to_xs = tb_to_xs;
  819. do_gtod.tb_to_us = tb_to_us;
  820. vdso_data->tb_orig_stamp = tb_last_jiffy;
  821. vdso_data->tb_update_count = 0;
  822. vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
  823. vdso_data->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
  824. vdso_data->tb_to_xs = tb_to_xs;
  825. time_freq = 0;
  826. write_sequnlock_irqrestore(&xtime_lock, flags);
  827. /* Register the clocksource, if we're not running on iSeries */
  828. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  829. clocksource_init();
  830. init_decrementer_clockevent();
  831. }
  832. #define FEBRUARY 2
  833. #define STARTOFTIME 1970
  834. #define SECDAY 86400L
  835. #define SECYR (SECDAY * 365)
  836. #define leapyear(year) ((year) % 4 == 0 && \
  837. ((year) % 100 != 0 || (year) % 400 == 0))
  838. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  839. #define days_in_month(a) (month_days[(a) - 1])
  840. static int month_days[12] = {
  841. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  842. };
  843. /*
  844. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  845. */
  846. void GregorianDay(struct rtc_time * tm)
  847. {
  848. int leapsToDate;
  849. int lastYear;
  850. int day;
  851. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  852. lastYear = tm->tm_year - 1;
  853. /*
  854. * Number of leap corrections to apply up to end of last year
  855. */
  856. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  857. /*
  858. * This year is a leap year if it is divisible by 4 except when it is
  859. * divisible by 100 unless it is divisible by 400
  860. *
  861. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  862. */
  863. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  864. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  865. tm->tm_mday;
  866. tm->tm_wday = day % 7;
  867. }
  868. void to_tm(int tim, struct rtc_time * tm)
  869. {
  870. register int i;
  871. register long hms, day;
  872. day = tim / SECDAY;
  873. hms = tim % SECDAY;
  874. /* Hours, minutes, seconds are easy */
  875. tm->tm_hour = hms / 3600;
  876. tm->tm_min = (hms % 3600) / 60;
  877. tm->tm_sec = (hms % 3600) % 60;
  878. /* Number of years in days */
  879. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  880. day -= days_in_year(i);
  881. tm->tm_year = i;
  882. /* Number of months in days left */
  883. if (leapyear(tm->tm_year))
  884. days_in_month(FEBRUARY) = 29;
  885. for (i = 1; day >= days_in_month(i); i++)
  886. day -= days_in_month(i);
  887. days_in_month(FEBRUARY) = 28;
  888. tm->tm_mon = i;
  889. /* Days are what is left over (+1) from all that. */
  890. tm->tm_mday = day + 1;
  891. /*
  892. * Determine the day of week
  893. */
  894. GregorianDay(tm);
  895. }
  896. /* Auxiliary function to compute scaling factors */
  897. /* Actually the choice of a timebase running at 1/4 the of the bus
  898. * frequency giving resolution of a few tens of nanoseconds is quite nice.
  899. * It makes this computation very precise (27-28 bits typically) which
  900. * is optimistic considering the stability of most processor clock
  901. * oscillators and the precision with which the timebase frequency
  902. * is measured but does not harm.
  903. */
  904. unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
  905. {
  906. unsigned mlt=0, tmp, err;
  907. /* No concern for performance, it's done once: use a stupid
  908. * but safe and compact method to find the multiplier.
  909. */
  910. for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
  911. if (mulhwu(inscale, mlt|tmp) < outscale)
  912. mlt |= tmp;
  913. }
  914. /* We might still be off by 1 for the best approximation.
  915. * A side effect of this is that if outscale is too large
  916. * the returned value will be zero.
  917. * Many corner cases have been checked and seem to work,
  918. * some might have been forgotten in the test however.
  919. */
  920. err = inscale * (mlt+1);
  921. if (err <= inscale/2)
  922. mlt++;
  923. return mlt;
  924. }
  925. /*
  926. * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
  927. * result.
  928. */
  929. void div128_by_32(u64 dividend_high, u64 dividend_low,
  930. unsigned divisor, struct div_result *dr)
  931. {
  932. unsigned long a, b, c, d;
  933. unsigned long w, x, y, z;
  934. u64 ra, rb, rc;
  935. a = dividend_high >> 32;
  936. b = dividend_high & 0xffffffff;
  937. c = dividend_low >> 32;
  938. d = dividend_low & 0xffffffff;
  939. w = a / divisor;
  940. ra = ((u64)(a - (w * divisor)) << 32) + b;
  941. rb = ((u64) do_div(ra, divisor) << 32) + c;
  942. x = ra;
  943. rc = ((u64) do_div(rb, divisor) << 32) + d;
  944. y = rb;
  945. do_div(rc, divisor);
  946. z = rc;
  947. dr->result_high = ((u64)w << 32) + x;
  948. dr->result_low = ((u64)y << 32) + z;
  949. }