time.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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 <linux/jiffies.h>
  53. #include <linux/posix-timers.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. #ifdef CONFIG_PPC64
  67. #include <asm/firmware.h>
  68. #endif
  69. #ifdef CONFIG_PPC_ISERIES
  70. #include <asm/iseries/it_lp_queue.h>
  71. #include <asm/iseries/hv_call_xm.h>
  72. #endif
  73. #include <asm/smp.h>
  74. /* keep track of when we need to update the rtc */
  75. time_t last_rtc_update;
  76. extern int piranha_simulator;
  77. #ifdef CONFIG_PPC_ISERIES
  78. unsigned long iSeries_recal_titan = 0;
  79. unsigned long iSeries_recal_tb = 0;
  80. static unsigned long first_settimeofday = 1;
  81. #endif
  82. /* The decrementer counts down by 128 every 128ns on a 601. */
  83. #define DECREMENTER_COUNT_601 (1000000000 / HZ)
  84. #define XSEC_PER_SEC (1024*1024)
  85. #ifdef CONFIG_PPC64
  86. #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
  87. #else
  88. /* compute ((xsec << 12) * max) >> 32 */
  89. #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
  90. #endif
  91. unsigned long tb_ticks_per_jiffy;
  92. unsigned long tb_ticks_per_usec = 100; /* sane default */
  93. EXPORT_SYMBOL(tb_ticks_per_usec);
  94. unsigned long tb_ticks_per_sec;
  95. EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */
  96. u64 tb_to_xs;
  97. unsigned tb_to_us;
  98. #define TICKLEN_SCALE (SHIFT_SCALE - 10)
  99. u64 last_tick_len; /* units are ns / 2^TICKLEN_SCALE */
  100. u64 ticklen_to_xs; /* 0.64 fraction */
  101. /* If last_tick_len corresponds to about 1/HZ seconds, then
  102. last_tick_len << TICKLEN_SHIFT will be about 2^63. */
  103. #define TICKLEN_SHIFT (63 - 30 - TICKLEN_SCALE + SHIFT_HZ)
  104. DEFINE_SPINLOCK(rtc_lock);
  105. EXPORT_SYMBOL_GPL(rtc_lock);
  106. u64 tb_to_ns_scale;
  107. unsigned tb_to_ns_shift;
  108. struct gettimeofday_struct do_gtod;
  109. extern unsigned long wall_jiffies;
  110. extern struct timezone sys_tz;
  111. static long timezone_offset;
  112. unsigned long ppc_proc_freq;
  113. unsigned long ppc_tb_freq;
  114. u64 tb_last_jiffy __cacheline_aligned_in_smp;
  115. unsigned long tb_last_stamp;
  116. /*
  117. * Note that on ppc32 this only stores the bottom 32 bits of
  118. * the timebase value, but that's enough to tell when a jiffy
  119. * has passed.
  120. */
  121. DEFINE_PER_CPU(unsigned long, last_jiffy);
  122. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  123. /*
  124. * Factors for converting from cputime_t (timebase ticks) to
  125. * jiffies, milliseconds, seconds, and clock_t (1/USER_HZ seconds).
  126. * These are all stored as 0.64 fixed-point binary fractions.
  127. */
  128. u64 __cputime_jiffies_factor;
  129. EXPORT_SYMBOL(__cputime_jiffies_factor);
  130. u64 __cputime_msec_factor;
  131. EXPORT_SYMBOL(__cputime_msec_factor);
  132. u64 __cputime_sec_factor;
  133. EXPORT_SYMBOL(__cputime_sec_factor);
  134. u64 __cputime_clockt_factor;
  135. EXPORT_SYMBOL(__cputime_clockt_factor);
  136. static void calc_cputime_factors(void)
  137. {
  138. struct div_result res;
  139. div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
  140. __cputime_jiffies_factor = res.result_low;
  141. div128_by_32(1000, 0, tb_ticks_per_sec, &res);
  142. __cputime_msec_factor = res.result_low;
  143. div128_by_32(1, 0, tb_ticks_per_sec, &res);
  144. __cputime_sec_factor = res.result_low;
  145. div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
  146. __cputime_clockt_factor = res.result_low;
  147. }
  148. /*
  149. * Read the PURR on systems that have it, otherwise the timebase.
  150. */
  151. static u64 read_purr(void)
  152. {
  153. if (cpu_has_feature(CPU_FTR_PURR))
  154. return mfspr(SPRN_PURR);
  155. return mftb();
  156. }
  157. /*
  158. * Account time for a transition between system, hard irq
  159. * or soft irq state.
  160. */
  161. void account_system_vtime(struct task_struct *tsk)
  162. {
  163. u64 now, delta;
  164. unsigned long flags;
  165. local_irq_save(flags);
  166. now = read_purr();
  167. delta = now - get_paca()->startpurr;
  168. get_paca()->startpurr = now;
  169. if (!in_interrupt()) {
  170. delta += get_paca()->system_time;
  171. get_paca()->system_time = 0;
  172. }
  173. account_system_time(tsk, 0, delta);
  174. local_irq_restore(flags);
  175. }
  176. /*
  177. * Transfer the user and system times accumulated in the paca
  178. * by the exception entry and exit code to the generic process
  179. * user and system time records.
  180. * Must be called with interrupts disabled.
  181. */
  182. void account_process_vtime(struct task_struct *tsk)
  183. {
  184. cputime_t utime;
  185. utime = get_paca()->user_time;
  186. get_paca()->user_time = 0;
  187. account_user_time(tsk, utime);
  188. }
  189. static void account_process_time(struct pt_regs *regs)
  190. {
  191. int cpu = smp_processor_id();
  192. account_process_vtime(current);
  193. run_local_timers();
  194. if (rcu_pending(cpu))
  195. rcu_check_callbacks(cpu, user_mode(regs));
  196. scheduler_tick();
  197. run_posix_cpu_timers(current);
  198. }
  199. #ifdef CONFIG_PPC_SPLPAR
  200. /*
  201. * Stuff for accounting stolen time.
  202. */
  203. struct cpu_purr_data {
  204. int initialized; /* thread is running */
  205. u64 tb0; /* timebase at origin time */
  206. u64 purr0; /* PURR at origin time */
  207. u64 tb; /* last TB value read */
  208. u64 purr; /* last PURR value read */
  209. u64 stolen; /* stolen time so far */
  210. spinlock_t lock;
  211. };
  212. static DEFINE_PER_CPU(struct cpu_purr_data, cpu_purr_data);
  213. static void snapshot_tb_and_purr(void *data)
  214. {
  215. struct cpu_purr_data *p = &__get_cpu_var(cpu_purr_data);
  216. p->tb0 = mftb();
  217. p->purr0 = mfspr(SPRN_PURR);
  218. p->tb = p->tb0;
  219. p->purr = 0;
  220. wmb();
  221. p->initialized = 1;
  222. }
  223. /*
  224. * Called during boot when all cpus have come up.
  225. */
  226. void snapshot_timebases(void)
  227. {
  228. int cpu;
  229. if (!cpu_has_feature(CPU_FTR_PURR))
  230. return;
  231. for_each_possible_cpu(cpu)
  232. spin_lock_init(&per_cpu(cpu_purr_data, cpu).lock);
  233. on_each_cpu(snapshot_tb_and_purr, NULL, 0, 1);
  234. }
  235. void calculate_steal_time(void)
  236. {
  237. u64 tb, purr, t0;
  238. s64 stolen;
  239. struct cpu_purr_data *p0, *pme, *phim;
  240. int cpu;
  241. if (!cpu_has_feature(CPU_FTR_PURR))
  242. return;
  243. cpu = smp_processor_id();
  244. pme = &per_cpu(cpu_purr_data, cpu);
  245. if (!pme->initialized)
  246. return; /* this can happen in early boot */
  247. p0 = &per_cpu(cpu_purr_data, cpu & ~1);
  248. phim = &per_cpu(cpu_purr_data, cpu ^ 1);
  249. spin_lock(&p0->lock);
  250. tb = mftb();
  251. purr = mfspr(SPRN_PURR) - pme->purr0;
  252. if (!phim->initialized || !cpu_online(cpu ^ 1)) {
  253. stolen = (tb - pme->tb) - (purr - pme->purr);
  254. } else {
  255. t0 = pme->tb0;
  256. if (phim->tb0 < t0)
  257. t0 = phim->tb0;
  258. stolen = phim->tb - t0 - phim->purr - purr - p0->stolen;
  259. }
  260. if (stolen > 0) {
  261. account_steal_time(current, stolen);
  262. p0->stolen += stolen;
  263. }
  264. pme->tb = tb;
  265. pme->purr = purr;
  266. spin_unlock(&p0->lock);
  267. }
  268. /*
  269. * Must be called before the cpu is added to the online map when
  270. * a cpu is being brought up at runtime.
  271. */
  272. static void snapshot_purr(void)
  273. {
  274. int cpu;
  275. u64 purr;
  276. struct cpu_purr_data *p0, *pme, *phim;
  277. unsigned long flags;
  278. if (!cpu_has_feature(CPU_FTR_PURR))
  279. return;
  280. cpu = smp_processor_id();
  281. pme = &per_cpu(cpu_purr_data, cpu);
  282. p0 = &per_cpu(cpu_purr_data, cpu & ~1);
  283. phim = &per_cpu(cpu_purr_data, cpu ^ 1);
  284. spin_lock_irqsave(&p0->lock, flags);
  285. pme->tb = pme->tb0 = mftb();
  286. purr = mfspr(SPRN_PURR);
  287. if (!phim->initialized) {
  288. pme->purr = 0;
  289. pme->purr0 = purr;
  290. } else {
  291. /* set p->purr and p->purr0 for no change in p0->stolen */
  292. pme->purr = phim->tb - phim->tb0 - phim->purr - p0->stolen;
  293. pme->purr0 = purr - pme->purr;
  294. }
  295. pme->initialized = 1;
  296. spin_unlock_irqrestore(&p0->lock, flags);
  297. }
  298. #endif /* CONFIG_PPC_SPLPAR */
  299. #else /* ! CONFIG_VIRT_CPU_ACCOUNTING */
  300. #define calc_cputime_factors()
  301. #define account_process_time(regs) update_process_times(user_mode(regs))
  302. #define calculate_steal_time() do { } while (0)
  303. #endif
  304. #if !(defined(CONFIG_VIRT_CPU_ACCOUNTING) && defined(CONFIG_PPC_SPLPAR))
  305. #define snapshot_purr() do { } while (0)
  306. #endif
  307. /*
  308. * Called when a cpu comes up after the system has finished booting,
  309. * i.e. as a result of a hotplug cpu action.
  310. */
  311. void snapshot_timebase(void)
  312. {
  313. __get_cpu_var(last_jiffy) = get_tb();
  314. snapshot_purr();
  315. }
  316. void __delay(unsigned long loops)
  317. {
  318. unsigned long start;
  319. int diff;
  320. if (__USE_RTC()) {
  321. start = get_rtcl();
  322. do {
  323. /* the RTCL register wraps at 1000000000 */
  324. diff = get_rtcl() - start;
  325. if (diff < 0)
  326. diff += 1000000000;
  327. } while (diff < loops);
  328. } else {
  329. start = get_tbl();
  330. while (get_tbl() - start < loops)
  331. HMT_low();
  332. HMT_medium();
  333. }
  334. }
  335. EXPORT_SYMBOL(__delay);
  336. void udelay(unsigned long usecs)
  337. {
  338. __delay(tb_ticks_per_usec * usecs);
  339. }
  340. EXPORT_SYMBOL(udelay);
  341. static __inline__ void timer_check_rtc(void)
  342. {
  343. /*
  344. * update the rtc when needed, this should be performed on the
  345. * right fraction of a second. Half or full second ?
  346. * Full second works on mk48t59 clocks, others need testing.
  347. * Note that this update is basically only used through
  348. * the adjtimex system calls. Setting the HW clock in
  349. * any other way is a /dev/rtc and userland business.
  350. * This is still wrong by -0.5/+1.5 jiffies because of the
  351. * timer interrupt resolution and possible delay, but here we
  352. * hit a quantization limit which can only be solved by higher
  353. * resolution timers and decoupling time management from timer
  354. * interrupts. This is also wrong on the clocks
  355. * which require being written at the half second boundary.
  356. * We should have an rtc call that only sets the minutes and
  357. * seconds like on Intel to avoid problems with non UTC clocks.
  358. */
  359. if (ppc_md.set_rtc_time && ntp_synced() &&
  360. xtime.tv_sec - last_rtc_update >= 659 &&
  361. abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ) {
  362. struct rtc_time tm;
  363. to_tm(xtime.tv_sec + 1 + timezone_offset, &tm);
  364. tm.tm_year -= 1900;
  365. tm.tm_mon -= 1;
  366. if (ppc_md.set_rtc_time(&tm) == 0)
  367. last_rtc_update = xtime.tv_sec + 1;
  368. else
  369. /* Try again one minute later */
  370. last_rtc_update += 60;
  371. }
  372. }
  373. /*
  374. * This version of gettimeofday has microsecond resolution.
  375. */
  376. static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
  377. {
  378. unsigned long sec, usec;
  379. u64 tb_ticks, xsec;
  380. struct gettimeofday_vars *temp_varp;
  381. u64 temp_tb_to_xs, temp_stamp_xsec;
  382. /*
  383. * These calculations are faster (gets rid of divides)
  384. * if done in units of 1/2^20 rather than microseconds.
  385. * The conversion to microseconds at the end is done
  386. * without a divide (and in fact, without a multiply)
  387. */
  388. temp_varp = do_gtod.varp;
  389. tb_ticks = tb_val - temp_varp->tb_orig_stamp;
  390. temp_tb_to_xs = temp_varp->tb_to_xs;
  391. temp_stamp_xsec = temp_varp->stamp_xsec;
  392. xsec = temp_stamp_xsec + mulhdu(tb_ticks, temp_tb_to_xs);
  393. sec = xsec / XSEC_PER_SEC;
  394. usec = (unsigned long)xsec & (XSEC_PER_SEC - 1);
  395. usec = SCALE_XSEC(usec, 1000000);
  396. tv->tv_sec = sec;
  397. tv->tv_usec = usec;
  398. }
  399. void do_gettimeofday(struct timeval *tv)
  400. {
  401. if (__USE_RTC()) {
  402. /* do this the old way */
  403. unsigned long flags, seq;
  404. unsigned int sec, nsec, usec;
  405. do {
  406. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  407. sec = xtime.tv_sec;
  408. nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp);
  409. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  410. usec = nsec / 1000;
  411. while (usec >= 1000000) {
  412. usec -= 1000000;
  413. ++sec;
  414. }
  415. tv->tv_sec = sec;
  416. tv->tv_usec = usec;
  417. return;
  418. }
  419. __do_gettimeofday(tv, get_tb());
  420. }
  421. EXPORT_SYMBOL(do_gettimeofday);
  422. /*
  423. * There are two copies of tb_to_xs and stamp_xsec so that no
  424. * lock is needed to access and use these values in
  425. * do_gettimeofday. We alternate the copies and as long as a
  426. * reasonable time elapses between changes, there will never
  427. * be inconsistent values. ntpd has a minimum of one minute
  428. * between updates.
  429. */
  430. static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
  431. u64 new_tb_to_xs)
  432. {
  433. unsigned temp_idx;
  434. struct gettimeofday_vars *temp_varp;
  435. temp_idx = (do_gtod.var_idx == 0);
  436. temp_varp = &do_gtod.vars[temp_idx];
  437. temp_varp->tb_to_xs = new_tb_to_xs;
  438. temp_varp->tb_orig_stamp = new_tb_stamp;
  439. temp_varp->stamp_xsec = new_stamp_xsec;
  440. smp_mb();
  441. do_gtod.varp = temp_varp;
  442. do_gtod.var_idx = temp_idx;
  443. /*
  444. * tb_update_count is used to allow the userspace gettimeofday code
  445. * to assure itself that it sees a consistent view of the tb_to_xs and
  446. * stamp_xsec variables. It reads the tb_update_count, then reads
  447. * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
  448. * the two values of tb_update_count match and are even then the
  449. * tb_to_xs and stamp_xsec values are consistent. If not, then it
  450. * loops back and reads them again until this criteria is met.
  451. * We expect the caller to have done the first increment of
  452. * vdso_data->tb_update_count already.
  453. */
  454. vdso_data->tb_orig_stamp = new_tb_stamp;
  455. vdso_data->stamp_xsec = new_stamp_xsec;
  456. vdso_data->tb_to_xs = new_tb_to_xs;
  457. vdso_data->wtom_clock_sec = wall_to_monotonic.tv_sec;
  458. vdso_data->wtom_clock_nsec = wall_to_monotonic.tv_nsec;
  459. smp_wmb();
  460. ++(vdso_data->tb_update_count);
  461. }
  462. /*
  463. * When the timebase - tb_orig_stamp gets too big, we do a manipulation
  464. * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
  465. * difference tb - tb_orig_stamp small enough to always fit inside a
  466. * 32 bits number. This is a requirement of our fast 32 bits userland
  467. * implementation in the vdso. If we "miss" a call to this function
  468. * (interrupt latency, CPU locked in a spinlock, ...) and we end up
  469. * with a too big difference, then the vdso will fallback to calling
  470. * the syscall
  471. */
  472. static __inline__ void timer_recalc_offset(u64 cur_tb)
  473. {
  474. unsigned long offset;
  475. u64 new_stamp_xsec;
  476. u64 tlen, t2x;
  477. u64 tb, xsec_old, xsec_new;
  478. struct gettimeofday_vars *varp;
  479. if (__USE_RTC())
  480. return;
  481. tlen = current_tick_length();
  482. offset = cur_tb - do_gtod.varp->tb_orig_stamp;
  483. if (tlen == last_tick_len && offset < 0x80000000u)
  484. return;
  485. if (tlen != last_tick_len) {
  486. t2x = mulhdu(tlen << TICKLEN_SHIFT, ticklen_to_xs);
  487. last_tick_len = tlen;
  488. } else
  489. t2x = do_gtod.varp->tb_to_xs;
  490. new_stamp_xsec = (u64) xtime.tv_nsec * XSEC_PER_SEC;
  491. do_div(new_stamp_xsec, 1000000000);
  492. new_stamp_xsec += (u64) xtime.tv_sec * XSEC_PER_SEC;
  493. ++vdso_data->tb_update_count;
  494. smp_mb();
  495. /*
  496. * Make sure time doesn't go backwards for userspace gettimeofday.
  497. */
  498. tb = get_tb();
  499. varp = do_gtod.varp;
  500. xsec_old = mulhdu(tb - varp->tb_orig_stamp, varp->tb_to_xs)
  501. + varp->stamp_xsec;
  502. xsec_new = mulhdu(tb - cur_tb, t2x) + new_stamp_xsec;
  503. if (xsec_new < xsec_old)
  504. new_stamp_xsec += xsec_old - xsec_new;
  505. update_gtod(cur_tb, new_stamp_xsec, t2x);
  506. }
  507. #ifdef CONFIG_SMP
  508. unsigned long profile_pc(struct pt_regs *regs)
  509. {
  510. unsigned long pc = instruction_pointer(regs);
  511. if (in_lock_functions(pc))
  512. return regs->link;
  513. return pc;
  514. }
  515. EXPORT_SYMBOL(profile_pc);
  516. #endif
  517. #ifdef CONFIG_PPC_ISERIES
  518. /*
  519. * This function recalibrates the timebase based on the 49-bit time-of-day
  520. * value in the Titan chip. The Titan is much more accurate than the value
  521. * returned by the service processor for the timebase frequency.
  522. */
  523. static void iSeries_tb_recal(void)
  524. {
  525. struct div_result divres;
  526. unsigned long titan, tb;
  527. tb = get_tb();
  528. titan = HvCallXm_loadTod();
  529. if ( iSeries_recal_titan ) {
  530. unsigned long tb_ticks = tb - iSeries_recal_tb;
  531. unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
  532. unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
  533. unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
  534. long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
  535. char sign = '+';
  536. /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
  537. new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
  538. if ( tick_diff < 0 ) {
  539. tick_diff = -tick_diff;
  540. sign = '-';
  541. }
  542. if ( tick_diff ) {
  543. if ( tick_diff < tb_ticks_per_jiffy/25 ) {
  544. printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
  545. new_tb_ticks_per_jiffy, sign, tick_diff );
  546. tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
  547. tb_ticks_per_sec = new_tb_ticks_per_sec;
  548. calc_cputime_factors();
  549. div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
  550. do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
  551. tb_to_xs = divres.result_low;
  552. do_gtod.varp->tb_to_xs = tb_to_xs;
  553. vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
  554. vdso_data->tb_to_xs = tb_to_xs;
  555. }
  556. else {
  557. printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
  558. " new tb_ticks_per_jiffy = %lu\n"
  559. " old tb_ticks_per_jiffy = %lu\n",
  560. new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
  561. }
  562. }
  563. }
  564. iSeries_recal_titan = titan;
  565. iSeries_recal_tb = tb;
  566. }
  567. #endif
  568. /*
  569. * For iSeries shared processors, we have to let the hypervisor
  570. * set the hardware decrementer. We set a virtual decrementer
  571. * in the lppaca and call the hypervisor if the virtual
  572. * decrementer is less than the current value in the hardware
  573. * decrementer. (almost always the new decrementer value will
  574. * be greater than the current hardware decementer so the hypervisor
  575. * call will not be needed)
  576. */
  577. /*
  578. * timer_interrupt - gets called when the decrementer overflows,
  579. * with interrupts disabled.
  580. */
  581. void timer_interrupt(struct pt_regs * regs)
  582. {
  583. int next_dec;
  584. int cpu = smp_processor_id();
  585. unsigned long ticks;
  586. #ifdef CONFIG_PPC32
  587. if (atomic_read(&ppc_n_lost_interrupts) != 0)
  588. do_IRQ(regs);
  589. #endif
  590. irq_enter();
  591. profile_tick(CPU_PROFILING, regs);
  592. calculate_steal_time();
  593. #ifdef CONFIG_PPC_ISERIES
  594. get_lppaca()->int_dword.fields.decr_int = 0;
  595. #endif
  596. while ((ticks = tb_ticks_since(per_cpu(last_jiffy, cpu)))
  597. >= tb_ticks_per_jiffy) {
  598. /* Update last_jiffy */
  599. per_cpu(last_jiffy, cpu) += tb_ticks_per_jiffy;
  600. /* Handle RTCL overflow on 601 */
  601. if (__USE_RTC() && per_cpu(last_jiffy, cpu) >= 1000000000)
  602. per_cpu(last_jiffy, cpu) -= 1000000000;
  603. /*
  604. * We cannot disable the decrementer, so in the period
  605. * between this cpu's being marked offline in cpu_online_map
  606. * and calling stop-self, it is taking timer interrupts.
  607. * Avoid calling into the scheduler rebalancing code if this
  608. * is the case.
  609. */
  610. if (!cpu_is_offline(cpu))
  611. account_process_time(regs);
  612. /*
  613. * No need to check whether cpu is offline here; boot_cpuid
  614. * should have been fixed up by now.
  615. */
  616. if (cpu != boot_cpuid)
  617. continue;
  618. write_seqlock(&xtime_lock);
  619. tb_last_jiffy += tb_ticks_per_jiffy;
  620. tb_last_stamp = per_cpu(last_jiffy, cpu);
  621. do_timer(regs);
  622. timer_recalc_offset(tb_last_jiffy);
  623. timer_check_rtc();
  624. write_sequnlock(&xtime_lock);
  625. }
  626. next_dec = tb_ticks_per_jiffy - ticks;
  627. set_dec(next_dec);
  628. #ifdef CONFIG_PPC_ISERIES
  629. if (hvlpevent_is_pending())
  630. process_hvlpevents(regs);
  631. #endif
  632. #ifdef CONFIG_PPC64
  633. /* collect purr register values often, for accurate calculations */
  634. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  635. struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
  636. cu->current_tb = mfspr(SPRN_PURR);
  637. }
  638. #endif
  639. irq_exit();
  640. }
  641. void wakeup_decrementer(void)
  642. {
  643. unsigned long ticks;
  644. /*
  645. * The timebase gets saved on sleep and restored on wakeup,
  646. * so all we need to do is to reset the decrementer.
  647. */
  648. ticks = tb_ticks_since(__get_cpu_var(last_jiffy));
  649. if (ticks < tb_ticks_per_jiffy)
  650. ticks = tb_ticks_per_jiffy - ticks;
  651. else
  652. ticks = 1;
  653. set_dec(ticks);
  654. }
  655. #ifdef CONFIG_SMP
  656. void __init smp_space_timers(unsigned int max_cpus)
  657. {
  658. int i;
  659. unsigned long half = tb_ticks_per_jiffy / 2;
  660. unsigned long offset = tb_ticks_per_jiffy / max_cpus;
  661. unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid);
  662. /* make sure tb > per_cpu(last_jiffy, cpu) for all cpus always */
  663. previous_tb -= tb_ticks_per_jiffy;
  664. /*
  665. * The stolen time calculation for POWER5 shared-processor LPAR
  666. * systems works better if the two threads' timebase interrupts
  667. * are staggered by half a jiffy with respect to each other.
  668. */
  669. for_each_possible_cpu(i) {
  670. if (i == boot_cpuid)
  671. continue;
  672. if (i == (boot_cpuid ^ 1))
  673. per_cpu(last_jiffy, i) =
  674. per_cpu(last_jiffy, boot_cpuid) - half;
  675. else if (i & 1)
  676. per_cpu(last_jiffy, i) =
  677. per_cpu(last_jiffy, i ^ 1) + half;
  678. else {
  679. previous_tb += offset;
  680. per_cpu(last_jiffy, i) = previous_tb;
  681. }
  682. }
  683. }
  684. #endif
  685. /*
  686. * Scheduler clock - returns current time in nanosec units.
  687. *
  688. * Note: mulhdu(a, b) (multiply high double unsigned) returns
  689. * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
  690. * are 64-bit unsigned numbers.
  691. */
  692. unsigned long long sched_clock(void)
  693. {
  694. if (__USE_RTC())
  695. return get_rtc();
  696. return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
  697. }
  698. int do_settimeofday(struct timespec *tv)
  699. {
  700. time_t wtm_sec, new_sec = tv->tv_sec;
  701. long wtm_nsec, new_nsec = tv->tv_nsec;
  702. unsigned long flags;
  703. u64 new_xsec;
  704. unsigned long tb_delta;
  705. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  706. return -EINVAL;
  707. write_seqlock_irqsave(&xtime_lock, flags);
  708. /*
  709. * Updating the RTC is not the job of this code. If the time is
  710. * stepped under NTP, the RTC will be updated after STA_UNSYNC
  711. * is cleared. Tools like clock/hwclock either copy the RTC
  712. * to the system time, in which case there is no point in writing
  713. * to the RTC again, or write to the RTC but then they don't call
  714. * settimeofday to perform this operation.
  715. */
  716. #ifdef CONFIG_PPC_ISERIES
  717. if (first_settimeofday) {
  718. iSeries_tb_recal();
  719. first_settimeofday = 0;
  720. }
  721. #endif
  722. /* Make userspace gettimeofday spin until we're done. */
  723. ++vdso_data->tb_update_count;
  724. smp_mb();
  725. /*
  726. * Subtract off the number of nanoseconds since the
  727. * beginning of the last tick.
  728. * Note that since we don't increment jiffies_64 anywhere other
  729. * than in do_timer (since we don't have a lost tick problem),
  730. * wall_jiffies will always be the same as jiffies,
  731. * and therefore the (jiffies - wall_jiffies) computation
  732. * has been removed.
  733. */
  734. tb_delta = tb_ticks_since(tb_last_stamp);
  735. tb_delta = mulhdu(tb_delta, do_gtod.varp->tb_to_xs); /* in xsec */
  736. new_nsec -= SCALE_XSEC(tb_delta, 1000000000);
  737. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
  738. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
  739. set_normalized_timespec(&xtime, new_sec, new_nsec);
  740. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  741. /* In case of a large backwards jump in time with NTP, we want the
  742. * clock to be updated as soon as the PLL is again in lock.
  743. */
  744. last_rtc_update = new_sec - 658;
  745. ntp_clear();
  746. new_xsec = xtime.tv_nsec;
  747. if (new_xsec != 0) {
  748. new_xsec *= XSEC_PER_SEC;
  749. do_div(new_xsec, NSEC_PER_SEC);
  750. }
  751. new_xsec += (u64)xtime.tv_sec * XSEC_PER_SEC;
  752. update_gtod(tb_last_jiffy, new_xsec, do_gtod.varp->tb_to_xs);
  753. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  754. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  755. write_sequnlock_irqrestore(&xtime_lock, flags);
  756. clock_was_set();
  757. return 0;
  758. }
  759. EXPORT_SYMBOL(do_settimeofday);
  760. void __init generic_calibrate_decr(void)
  761. {
  762. struct device_node *cpu;
  763. unsigned int *fp;
  764. int node_found;
  765. /*
  766. * The cpu node should have a timebase-frequency property
  767. * to tell us the rate at which the decrementer counts.
  768. */
  769. cpu = of_find_node_by_type(NULL, "cpu");
  770. ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
  771. node_found = 0;
  772. if (cpu) {
  773. fp = (unsigned int *)get_property(cpu, "timebase-frequency",
  774. NULL);
  775. if (fp) {
  776. node_found = 1;
  777. ppc_tb_freq = *fp;
  778. }
  779. }
  780. if (!node_found)
  781. printk(KERN_ERR "WARNING: Estimating decrementer frequency "
  782. "(not found)\n");
  783. ppc_proc_freq = DEFAULT_PROC_FREQ;
  784. node_found = 0;
  785. if (cpu) {
  786. fp = (unsigned int *)get_property(cpu, "clock-frequency",
  787. NULL);
  788. if (fp) {
  789. node_found = 1;
  790. ppc_proc_freq = *fp;
  791. }
  792. }
  793. #ifdef CONFIG_BOOKE
  794. /* Set the time base to zero */
  795. mtspr(SPRN_TBWL, 0);
  796. mtspr(SPRN_TBWU, 0);
  797. /* Clear any pending timer interrupts */
  798. mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
  799. /* Enable decrementer interrupt */
  800. mtspr(SPRN_TCR, TCR_DIE);
  801. #endif
  802. if (!node_found)
  803. printk(KERN_ERR "WARNING: Estimating processor frequency "
  804. "(not found)\n");
  805. of_node_put(cpu);
  806. }
  807. unsigned long get_boot_time(void)
  808. {
  809. struct rtc_time tm;
  810. if (ppc_md.get_boot_time)
  811. return ppc_md.get_boot_time();
  812. if (!ppc_md.get_rtc_time)
  813. return 0;
  814. ppc_md.get_rtc_time(&tm);
  815. return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  816. tm.tm_hour, tm.tm_min, tm.tm_sec);
  817. }
  818. /* This function is only called on the boot processor */
  819. void __init time_init(void)
  820. {
  821. unsigned long flags;
  822. unsigned long tm = 0;
  823. struct div_result res;
  824. u64 scale, x;
  825. unsigned shift;
  826. if (ppc_md.time_init != NULL)
  827. timezone_offset = ppc_md.time_init();
  828. if (__USE_RTC()) {
  829. /* 601 processor: dec counts down by 128 every 128ns */
  830. ppc_tb_freq = 1000000000;
  831. tb_last_stamp = get_rtcl();
  832. tb_last_jiffy = tb_last_stamp;
  833. } else {
  834. /* Normal PowerPC with timebase register */
  835. ppc_md.calibrate_decr();
  836. printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
  837. ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
  838. printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
  839. ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
  840. tb_last_stamp = tb_last_jiffy = get_tb();
  841. }
  842. tb_ticks_per_jiffy = ppc_tb_freq / HZ;
  843. tb_ticks_per_sec = ppc_tb_freq;
  844. tb_ticks_per_usec = ppc_tb_freq / 1000000;
  845. tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
  846. calc_cputime_factors();
  847. /*
  848. * Calculate the length of each tick in ns. It will not be
  849. * exactly 1e9/HZ unless ppc_tb_freq is divisible by HZ.
  850. * We compute 1e9 * tb_ticks_per_jiffy / ppc_tb_freq,
  851. * rounded up.
  852. */
  853. x = (u64) NSEC_PER_SEC * tb_ticks_per_jiffy + ppc_tb_freq - 1;
  854. do_div(x, ppc_tb_freq);
  855. tick_nsec = x;
  856. last_tick_len = x << TICKLEN_SCALE;
  857. /*
  858. * Compute ticklen_to_xs, which is a factor which gets multiplied
  859. * by (last_tick_len << TICKLEN_SHIFT) to get a tb_to_xs value.
  860. * It is computed as:
  861. * ticklen_to_xs = 2^N / (tb_ticks_per_jiffy * 1e9)
  862. * where N = 64 + 20 - TICKLEN_SCALE - TICKLEN_SHIFT
  863. * which turns out to be N = 51 - SHIFT_HZ.
  864. * This gives the result as a 0.64 fixed-point fraction.
  865. * That value is reduced by an offset amounting to 1 xsec per
  866. * 2^31 timebase ticks to avoid problems with time going backwards
  867. * by 1 xsec when we do timer_recalc_offset due to losing the
  868. * fractional xsec. That offset is equal to ppc_tb_freq/2^51
  869. * since there are 2^20 xsec in a second.
  870. */
  871. div128_by_32((1ULL << 51) - ppc_tb_freq, 0,
  872. tb_ticks_per_jiffy << SHIFT_HZ, &res);
  873. div128_by_32(res.result_high, res.result_low, NSEC_PER_SEC, &res);
  874. ticklen_to_xs = res.result_low;
  875. /* Compute tb_to_xs from tick_nsec */
  876. tb_to_xs = mulhdu(last_tick_len << TICKLEN_SHIFT, ticklen_to_xs);
  877. /*
  878. * Compute scale factor for sched_clock.
  879. * The calibrate_decr() function has set tb_ticks_per_sec,
  880. * which is the timebase frequency.
  881. * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
  882. * the 128-bit result as a 64.64 fixed-point number.
  883. * We then shift that number right until it is less than 1.0,
  884. * giving us the scale factor and shift count to use in
  885. * sched_clock().
  886. */
  887. div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
  888. scale = res.result_low;
  889. for (shift = 0; res.result_high != 0; ++shift) {
  890. scale = (scale >> 1) | (res.result_high << 63);
  891. res.result_high >>= 1;
  892. }
  893. tb_to_ns_scale = scale;
  894. tb_to_ns_shift = shift;
  895. #ifdef CONFIG_PPC_ISERIES
  896. if (!piranha_simulator)
  897. #endif
  898. tm = get_boot_time();
  899. write_seqlock_irqsave(&xtime_lock, flags);
  900. /* If platform provided a timezone (pmac), we correct the time */
  901. if (timezone_offset) {
  902. sys_tz.tz_minuteswest = -timezone_offset / 60;
  903. sys_tz.tz_dsttime = 0;
  904. tm -= timezone_offset;
  905. }
  906. xtime.tv_sec = tm;
  907. xtime.tv_nsec = 0;
  908. do_gtod.varp = &do_gtod.vars[0];
  909. do_gtod.var_idx = 0;
  910. do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
  911. __get_cpu_var(last_jiffy) = tb_last_stamp;
  912. do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
  913. do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
  914. do_gtod.varp->tb_to_xs = tb_to_xs;
  915. do_gtod.tb_to_us = tb_to_us;
  916. vdso_data->tb_orig_stamp = tb_last_jiffy;
  917. vdso_data->tb_update_count = 0;
  918. vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
  919. vdso_data->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
  920. vdso_data->tb_to_xs = tb_to_xs;
  921. time_freq = 0;
  922. last_rtc_update = xtime.tv_sec;
  923. set_normalized_timespec(&wall_to_monotonic,
  924. -xtime.tv_sec, -xtime.tv_nsec);
  925. write_sequnlock_irqrestore(&xtime_lock, flags);
  926. /* Not exact, but the timer interrupt takes care of this */
  927. set_dec(tb_ticks_per_jiffy);
  928. }
  929. #define FEBRUARY 2
  930. #define STARTOFTIME 1970
  931. #define SECDAY 86400L
  932. #define SECYR (SECDAY * 365)
  933. #define leapyear(year) ((year) % 4 == 0 && \
  934. ((year) % 100 != 0 || (year) % 400 == 0))
  935. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  936. #define days_in_month(a) (month_days[(a) - 1])
  937. static int month_days[12] = {
  938. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  939. };
  940. /*
  941. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  942. */
  943. void GregorianDay(struct rtc_time * tm)
  944. {
  945. int leapsToDate;
  946. int lastYear;
  947. int day;
  948. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  949. lastYear = tm->tm_year - 1;
  950. /*
  951. * Number of leap corrections to apply up to end of last year
  952. */
  953. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  954. /*
  955. * This year is a leap year if it is divisible by 4 except when it is
  956. * divisible by 100 unless it is divisible by 400
  957. *
  958. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  959. */
  960. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  961. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  962. tm->tm_mday;
  963. tm->tm_wday = day % 7;
  964. }
  965. void to_tm(int tim, struct rtc_time * tm)
  966. {
  967. register int i;
  968. register long hms, day;
  969. day = tim / SECDAY;
  970. hms = tim % SECDAY;
  971. /* Hours, minutes, seconds are easy */
  972. tm->tm_hour = hms / 3600;
  973. tm->tm_min = (hms % 3600) / 60;
  974. tm->tm_sec = (hms % 3600) % 60;
  975. /* Number of years in days */
  976. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  977. day -= days_in_year(i);
  978. tm->tm_year = i;
  979. /* Number of months in days left */
  980. if (leapyear(tm->tm_year))
  981. days_in_month(FEBRUARY) = 29;
  982. for (i = 1; day >= days_in_month(i); i++)
  983. day -= days_in_month(i);
  984. days_in_month(FEBRUARY) = 28;
  985. tm->tm_mon = i;
  986. /* Days are what is left over (+1) from all that. */
  987. tm->tm_mday = day + 1;
  988. /*
  989. * Determine the day of week
  990. */
  991. GregorianDay(tm);
  992. }
  993. /* Auxiliary function to compute scaling factors */
  994. /* Actually the choice of a timebase running at 1/4 the of the bus
  995. * frequency giving resolution of a few tens of nanoseconds is quite nice.
  996. * It makes this computation very precise (27-28 bits typically) which
  997. * is optimistic considering the stability of most processor clock
  998. * oscillators and the precision with which the timebase frequency
  999. * is measured but does not harm.
  1000. */
  1001. unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
  1002. {
  1003. unsigned mlt=0, tmp, err;
  1004. /* No concern for performance, it's done once: use a stupid
  1005. * but safe and compact method to find the multiplier.
  1006. */
  1007. for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
  1008. if (mulhwu(inscale, mlt|tmp) < outscale)
  1009. mlt |= tmp;
  1010. }
  1011. /* We might still be off by 1 for the best approximation.
  1012. * A side effect of this is that if outscale is too large
  1013. * the returned value will be zero.
  1014. * Many corner cases have been checked and seem to work,
  1015. * some might have been forgotten in the test however.
  1016. */
  1017. err = inscale * (mlt+1);
  1018. if (err <= inscale/2)
  1019. mlt++;
  1020. return mlt;
  1021. }
  1022. /*
  1023. * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
  1024. * result.
  1025. */
  1026. void div128_by_32(u64 dividend_high, u64 dividend_low,
  1027. unsigned divisor, struct div_result *dr)
  1028. {
  1029. unsigned long a, b, c, d;
  1030. unsigned long w, x, y, z;
  1031. u64 ra, rb, rc;
  1032. a = dividend_high >> 32;
  1033. b = dividend_high & 0xffffffff;
  1034. c = dividend_low >> 32;
  1035. d = dividend_low & 0xffffffff;
  1036. w = a / divisor;
  1037. ra = ((u64)(a - (w * divisor)) << 32) + b;
  1038. rb = ((u64) do_div(ra, divisor) << 32) + c;
  1039. x = ra;
  1040. rc = ((u64) do_div(rb, divisor) << 32) + d;
  1041. y = rb;
  1042. do_div(rc, divisor);
  1043. z = rc;
  1044. dr->result_high = ((u64)w << 32) + x;
  1045. dr->result_low = ((u64)y << 32) + z;
  1046. }