blob: ed5c38fb146ca5916afb86c46574bb0739d74270 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
35#include <linux/config.h>
36#include <linux/errno.h>
37#include <linux/module.h>
38#include <linux/sched.h>
39#include <linux/kernel.h>
40#include <linux/param.h>
41#include <linux/string.h>
42#include <linux/mm.h>
43#include <linux/interrupt.h>
44#include <linux/timex.h>
45#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/time.h>
47#include <linux/init.h>
48#include <linux/profile.h>
49#include <linux/cpu.h>
50#include <linux/security.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100051#include <linux/percpu.h>
52#include <linux/rtc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/io.h>
55#include <asm/processor.h>
56#include <asm/nvram.h>
57#include <asm/cache.h>
58#include <asm/machdep.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100059#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#ifdef CONFIG_PPC64
65#include <asm/systemcfg.h>
66#include <asm/firmware.h>
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifdef CONFIG_PPC_ISERIES
69#include <asm/iSeries/ItLpQueue.h>
70#include <asm/iSeries/HvCallXm.h>
71#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
74
75EXPORT_SYMBOL(jiffies_64);
76
77/* keep track of when we need to update the rtc */
78time_t last_rtc_update;
79extern int piranha_simulator;
80#ifdef CONFIG_PPC_ISERIES
81unsigned long iSeries_recal_titan = 0;
82unsigned long iSeries_recal_tb = 0;
83static unsigned long first_settimeofday = 1;
84#endif
85
Paul Mackerrasf2783c12005-10-20 09:23:26 +100086/* The decrementer counts down by 128 every 128ns on a 601. */
87#define DECREMENTER_COUNT_601 (1000000000 / HZ)
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define XSEC_PER_SEC (1024*1024)
90
Paul Mackerrasf2783c12005-10-20 09:23:26 +100091#ifdef CONFIG_PPC64
92#define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
93#else
94/* compute ((xsec << 12) * max) >> 32 */
95#define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
96#endif
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098unsigned long tb_ticks_per_jiffy;
99unsigned long tb_ticks_per_usec = 100; /* sane default */
100EXPORT_SYMBOL(tb_ticks_per_usec);
101unsigned long tb_ticks_per_sec;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000102u64 tb_to_xs;
103unsigned tb_to_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104unsigned long processor_freq;
105DEFINE_SPINLOCK(rtc_lock);
Benjamin Herrenschmidt6ae3db12005-06-27 14:36:35 -0700106EXPORT_SYMBOL_GPL(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000108u64 tb_to_ns_scale;
109unsigned tb_to_ns_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111struct gettimeofday_struct do_gtod;
112
113extern unsigned long wall_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115extern struct timezone sys_tz;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000116static long timezone_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118void ppc_adjtimex(void);
119
120static unsigned adjusting_time = 0;
121
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000122unsigned long ppc_proc_freq;
123unsigned long ppc_tb_freq;
124
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000125#ifdef CONFIG_PPC32 /* XXX for now */
126#define boot_cpuid 0
127#endif
128
Paul Mackerras96c44502005-10-23 17:14:56 +1000129u64 tb_last_jiffy __cacheline_aligned_in_smp;
130unsigned long tb_last_stamp;
131
132/*
133 * Note that on ppc32 this only stores the bottom 32 bits of
134 * the timebase value, but that's enough to tell when a jiffy
135 * has passed.
136 */
137DEFINE_PER_CPU(unsigned long, last_jiffy);
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static __inline__ void timer_check_rtc(void)
140{
141 /*
142 * update the rtc when needed, this should be performed on the
143 * right fraction of a second. Half or full second ?
144 * Full second works on mk48t59 clocks, others need testing.
145 * Note that this update is basically only used through
146 * the adjtimex system calls. Setting the HW clock in
147 * any other way is a /dev/rtc and userland business.
148 * This is still wrong by -0.5/+1.5 jiffies because of the
149 * timer interrupt resolution and possible delay, but here we
150 * hit a quantization limit which can only be solved by higher
151 * resolution timers and decoupling time management from timer
152 * interrupts. This is also wrong on the clocks
153 * which require being written at the half second boundary.
154 * We should have an rtc call that only sets the minutes and
155 * seconds like on Intel to avoid problems with non UTC clocks.
156 */
Kumar Galad2e61512005-10-20 11:43:33 -0500157 if (ppc_md.set_rtc_time && ntp_synced() &&
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000158 xtime.tv_sec - last_rtc_update >= 659 &&
159 abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ &&
160 jiffies - wall_jiffies == 1) {
161 struct rtc_time tm;
162 to_tm(xtime.tv_sec + 1 + timezone_offset, &tm);
163 tm.tm_year -= 1900;
164 tm.tm_mon -= 1;
165 if (ppc_md.set_rtc_time(&tm) == 0)
166 last_rtc_update = xtime.tv_sec + 1;
167 else
168 /* Try again one minute later */
169 last_rtc_update += 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171}
172
173/*
174 * This version of gettimeofday has microsecond resolution.
175 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000176static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000178 unsigned long sec, usec;
179 u64 tb_ticks, xsec;
180 struct gettimeofday_vars *temp_varp;
181 u64 temp_tb_to_xs, temp_stamp_xsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
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;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000193 xsec = temp_stamp_xsec + mulhdu(tb_ticks, temp_tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 sec = xsec / XSEC_PER_SEC;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000195 usec = (unsigned long)xsec & (XSEC_PER_SEC - 1);
196 usec = SCALE_XSEC(usec, 1000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 tv->tv_sec = sec;
199 tv->tv_usec = usec;
200}
201
202void do_gettimeofday(struct timeval *tv)
203{
Paul Mackerras96c44502005-10-23 17:14:56 +1000204 if (__USE_RTC()) {
205 /* do this the old way */
206 unsigned long flags, seq;
207 unsigned int sec, nsec, usec, lost;
208
209 do {
210 seq = read_seqbegin_irqsave(&xtime_lock, flags);
211 sec = xtime.tv_sec;
212 nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp);
213 lost = jiffies - wall_jiffies;
214 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
215 usec = nsec / 1000 + lost * (1000000 / HZ);
216 while (usec >= 1000000) {
217 usec -= 1000000;
218 ++sec;
219 }
220 tv->tv_sec = sec;
221 tv->tv_usec = usec;
222 return;
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 __do_gettimeofday(tv, get_tb());
225}
226
227EXPORT_SYMBOL(do_gettimeofday);
228
229/* Synchronize xtime with do_gettimeofday */
230
231static inline void timer_sync_xtime(unsigned long cur_tb)
232{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000233#ifdef CONFIG_PPC64
234 /* why do we do this? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct timeval my_tv;
236
237 __do_gettimeofday(&my_tv, cur_tb);
238
239 if (xtime.tv_sec <= my_tv.tv_sec) {
240 xtime.tv_sec = my_tv.tv_sec;
241 xtime.tv_nsec = my_tv.tv_usec * 1000;
242 }
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000243#endif
244}
245
246/*
247 * There are two copies of tb_to_xs and stamp_xsec so that no
248 * lock is needed to access and use these values in
249 * do_gettimeofday. We alternate the copies and as long as a
250 * reasonable time elapses between changes, there will never
251 * be inconsistent values. ntpd has a minimum of one minute
252 * between updates.
253 */
254static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
Paul Mackerras5d14a182005-10-20 22:33:06 +1000255 u64 new_tb_to_xs)
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000256{
257 unsigned temp_idx;
258 struct gettimeofday_vars *temp_varp;
259
260 temp_idx = (do_gtod.var_idx == 0);
261 temp_varp = &do_gtod.vars[temp_idx];
262
263 temp_varp->tb_to_xs = new_tb_to_xs;
264 temp_varp->tb_orig_stamp = new_tb_stamp;
265 temp_varp->stamp_xsec = new_stamp_xsec;
266 smp_mb();
267 do_gtod.varp = temp_varp;
268 do_gtod.var_idx = temp_idx;
269
270#ifdef CONFIG_PPC64
271 /*
272 * tb_update_count is used to allow the userspace gettimeofday code
273 * to assure itself that it sees a consistent view of the tb_to_xs and
274 * stamp_xsec variables. It reads the tb_update_count, then reads
275 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
276 * the two values of tb_update_count match and are even then the
277 * tb_to_xs and stamp_xsec values are consistent. If not, then it
278 * loops back and reads them again until this criteria is met.
279 */
280 ++(systemcfg->tb_update_count);
281 smp_wmb();
282 systemcfg->tb_orig_stamp = new_tb_stamp;
283 systemcfg->stamp_xsec = new_stamp_xsec;
284 systemcfg->tb_to_xs = new_tb_to_xs;
285 smp_wmb();
286 ++(systemcfg->tb_update_count);
287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/*
291 * When the timebase - tb_orig_stamp gets too big, we do a manipulation
292 * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
293 * difference tb - tb_orig_stamp small enough to always fit inside a
294 * 32 bits number. This is a requirement of our fast 32 bits userland
295 * implementation in the vdso. If we "miss" a call to this function
296 * (interrupt latency, CPU locked in a spinlock, ...) and we end up
297 * with a too big difference, then the vdso will fallback to calling
298 * the syscall
299 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000300static __inline__ void timer_recalc_offset(u64 cur_tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000302 unsigned long offset;
303 u64 new_stamp_xsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Paul Mackerras96c44502005-10-23 17:14:56 +1000305 if (__USE_RTC())
306 return;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000307 offset = cur_tb - do_gtod.varp->tb_orig_stamp;
308 if ((offset & 0x80000000u) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000310 new_stamp_xsec = do_gtod.varp->stamp_xsec
311 + mulhdu(offset, do_gtod.varp->tb_to_xs);
312 update_gtod(cur_tb, new_stamp_xsec, do_gtod.varp->tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315#ifdef CONFIG_SMP
316unsigned long profile_pc(struct pt_regs *regs)
317{
318 unsigned long pc = instruction_pointer(regs);
319
320 if (in_lock_functions(pc))
321 return regs->link;
322
323 return pc;
324}
325EXPORT_SYMBOL(profile_pc);
326#endif
327
328#ifdef CONFIG_PPC_ISERIES
329
330/*
331 * This function recalibrates the timebase based on the 49-bit time-of-day
332 * value in the Titan chip. The Titan is much more accurate than the value
333 * returned by the service processor for the timebase frequency.
334 */
335
336static void iSeries_tb_recal(void)
337{
338 struct div_result divres;
339 unsigned long titan, tb;
340 tb = get_tb();
341 titan = HvCallXm_loadTod();
342 if ( iSeries_recal_titan ) {
343 unsigned long tb_ticks = tb - iSeries_recal_tb;
344 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
345 unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
346 unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
347 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
348 char sign = '+';
349 /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
350 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
351
352 if ( tick_diff < 0 ) {
353 tick_diff = -tick_diff;
354 sign = '-';
355 }
356 if ( tick_diff ) {
357 if ( tick_diff < tb_ticks_per_jiffy/25 ) {
358 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
359 new_tb_ticks_per_jiffy, sign, tick_diff );
360 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
361 tb_ticks_per_sec = new_tb_ticks_per_sec;
362 div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
363 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
364 tb_to_xs = divres.result_low;
365 do_gtod.varp->tb_to_xs = tb_to_xs;
366 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
367 systemcfg->tb_to_xs = tb_to_xs;
368 }
369 else {
370 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
371 " new tb_ticks_per_jiffy = %lu\n"
372 " old tb_ticks_per_jiffy = %lu\n",
373 new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
374 }
375 }
376 }
377 iSeries_recal_titan = titan;
378 iSeries_recal_tb = tb;
379}
380#endif
381
382/*
383 * For iSeries shared processors, we have to let the hypervisor
384 * set the hardware decrementer. We set a virtual decrementer
385 * in the lppaca and call the hypervisor if the virtual
386 * decrementer is less than the current value in the hardware
387 * decrementer. (almost always the new decrementer value will
388 * be greater than the current hardware decementer so the hypervisor
389 * call will not be needed)
390 */
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
393 * timer_interrupt - gets called when the decrementer overflows,
394 * with interrupts disabled.
395 */
Kumar Galac7aeffc2005-09-19 09:30:27 -0500396void timer_interrupt(struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 int next_dec;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000399 int cpu = smp_processor_id();
400 unsigned long ticks;
401
402#ifdef CONFIG_PPC32
403 if (atomic_read(&ppc_n_lost_interrupts) != 0)
404 do_IRQ(regs);
405#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 irq_enter();
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 profile_tick(CPU_PROFILING, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000411#ifdef CONFIG_PPC_ISERIES
412 get_paca()->lppaca.int_dword.fields.decr_int = 0;
413#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000415 while ((ticks = tb_ticks_since(per_cpu(last_jiffy, cpu)))
416 >= tb_ticks_per_jiffy) {
417 /* Update last_jiffy */
418 per_cpu(last_jiffy, cpu) += tb_ticks_per_jiffy;
419 /* Handle RTCL overflow on 601 */
420 if (__USE_RTC() && per_cpu(last_jiffy, cpu) >= 1000000000)
421 per_cpu(last_jiffy, cpu) -= 1000000000;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
424 * We cannot disable the decrementer, so in the period
425 * between this cpu's being marked offline in cpu_online_map
426 * and calling stop-self, it is taking timer interrupts.
427 * Avoid calling into the scheduler rebalancing code if this
428 * is the case.
429 */
430 if (!cpu_is_offline(cpu))
431 update_process_times(user_mode(regs));
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /*
434 * No need to check whether cpu is offline here; boot_cpuid
435 * should have been fixed up by now.
436 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000437 if (cpu != boot_cpuid)
438 continue;
439
440 write_seqlock(&xtime_lock);
Paul Mackerras96c44502005-10-23 17:14:56 +1000441 tb_last_jiffy += tb_ticks_per_jiffy;
442 tb_last_stamp = per_cpu(last_jiffy, cpu);
443 timer_recalc_offset(tb_last_jiffy);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000444 do_timer(regs);
Paul Mackerras96c44502005-10-23 17:14:56 +1000445 timer_sync_xtime(tb_last_jiffy);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000446 timer_check_rtc();
447 write_sequnlock(&xtime_lock);
448 if (adjusting_time && (time_adjust == 0))
449 ppc_adjtimex();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000452 next_dec = tb_ticks_per_jiffy - ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 set_dec(next_dec);
454
455#ifdef CONFIG_PPC_ISERIES
Michael Ellerman937b31b2005-06-30 15:15:42 +1000456 if (hvlpevent_is_pending())
Michael Ellerman74889802005-06-30 15:15:53 +1000457 process_hvlpevents(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif
459
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000460#ifdef CONFIG_PPC64
Stephen Rothwell8d15a3e2005-08-03 14:40:16 +1000461 /* collect purr register values often, for accurate calculations */
Stephen Rothwell1ababe112005-08-03 14:35:25 +1000462 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
464 cu->current_tb = mfspr(SPRN_PURR);
465 }
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000466#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000471void wakeup_decrementer(void)
472{
473 int i;
474
475 set_dec(tb_ticks_per_jiffy);
476 /*
477 * We don't expect this to be called on a machine with a 601,
478 * so using get_tbl is fine.
479 */
Paul Mackerras96c44502005-10-23 17:14:56 +1000480 tb_last_stamp = tb_last_jiffy = get_tb();
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000481 for_each_cpu(i)
482 per_cpu(last_jiffy, i) = tb_last_stamp;
483}
484
Paul Mackerrasa5b518e2005-10-22 14:55:23 +1000485#ifdef CONFIG_SMP
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000486void __init smp_space_timers(unsigned int max_cpus)
487{
488 int i;
489 unsigned long offset = tb_ticks_per_jiffy / max_cpus;
490 unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid);
491
492 for_each_cpu(i) {
493 if (i != boot_cpuid) {
494 previous_tb += offset;
495 per_cpu(last_jiffy, i) = previous_tb;
496 }
497 }
498}
499#endif
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
502 * Scheduler clock - returns current time in nanosec units.
503 *
504 * Note: mulhdu(a, b) (multiply high double unsigned) returns
505 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
506 * are 64-bit unsigned numbers.
507 */
508unsigned long long sched_clock(void)
509{
Paul Mackerras96c44502005-10-23 17:14:56 +1000510 if (__USE_RTC())
511 return get_rtc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
513}
514
515int do_settimeofday(struct timespec *tv)
516{
517 time_t wtm_sec, new_sec = tv->tv_sec;
518 long wtm_nsec, new_nsec = tv->tv_nsec;
519 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 long int tb_delta;
Paul Mackerras5f6b5b92005-10-30 22:55:52 +1100521 u64 new_xsec, tb_delta_xs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
524 return -EINVAL;
525
526 write_seqlock_irqsave(&xtime_lock, flags);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000527
528 /*
529 * Updating the RTC is not the job of this code. If the time is
530 * stepped under NTP, the RTC will be updated after STA_UNSYNC
531 * is cleared. Tools like clock/hwclock either copy the RTC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * to the system time, in which case there is no point in writing
533 * to the RTC again, or write to the RTC but then they don't call
534 * settimeofday to perform this operation.
535 */
536#ifdef CONFIG_PPC_ISERIES
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000537 if (first_settimeofday) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 iSeries_tb_recal();
539 first_settimeofday = 0;
540 }
541#endif
542 tb_delta = tb_ticks_since(tb_last_stamp);
543 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
Paul Mackerras5f6b5b92005-10-30 22:55:52 +1100544 tb_delta_xs = mulhdu(tb_delta, do_gtod.varp->tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
547 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
548
549 set_normalized_timespec(&xtime, new_sec, new_nsec);
550 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
551
552 /* In case of a large backwards jump in time with NTP, we want the
553 * clock to be updated as soon as the PLL is again in lock.
554 */
555 last_rtc_update = new_sec - 658;
556
john stultzb149ee22005-09-06 15:17:46 -0700557 ntp_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Paul Mackerras5f6b5b92005-10-30 22:55:52 +1100559 new_xsec = 0;
560 if (new_nsec != 0) {
561 new_xsec = (u64)new_nsec * XSEC_PER_SEC;
562 do_div(new_xsec, NSEC_PER_SEC);
563 }
564 new_xsec += (u64)new_sec * XSEC_PER_SEC - tb_delta_xs;
Paul Mackerras96c44502005-10-23 17:14:56 +1000565 update_gtod(tb_last_jiffy, new_xsec, do_gtod.varp->tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000567#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
569 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 write_sequnlock_irqrestore(&xtime_lock, flags);
573 clock_was_set();
574 return 0;
575}
576
577EXPORT_SYMBOL(do_settimeofday);
578
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000579void __init generic_calibrate_decr(void)
580{
581 struct device_node *cpu;
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000582 unsigned int *fp;
583 int node_found;
584
585 /*
586 * The cpu node should have a timebase-frequency property
587 * to tell us the rate at which the decrementer counts.
588 */
589 cpu = of_find_node_by_type(NULL, "cpu");
590
591 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
592 node_found = 0;
593 if (cpu != 0) {
594 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
595 NULL);
596 if (fp != 0) {
597 node_found = 1;
598 ppc_tb_freq = *fp;
599 }
600 }
601 if (!node_found)
602 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
603 "(not found)\n");
604
605 ppc_proc_freq = DEFAULT_PROC_FREQ;
606 node_found = 0;
607 if (cpu != 0) {
608 fp = (unsigned int *)get_property(cpu, "clock-frequency",
609 NULL);
610 if (fp != 0) {
611 node_found = 1;
612 ppc_proc_freq = *fp;
613 }
614 }
Kumar Gala0fd6f712005-10-25 23:02:59 -0500615#ifdef CONFIG_BOOKE
616 /* Set the time base to zero */
617 mtspr(SPRN_TBWL, 0);
618 mtspr(SPRN_TBWU, 0);
619
620 /* Clear any pending timer interrupts */
621 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
622
623 /* Enable decrementer interrupt */
624 mtspr(SPRN_TCR, TCR_DIE);
625#endif
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000626 if (!node_found)
627 printk(KERN_ERR "WARNING: Estimating processor frequency "
628 "(not found)\n");
629
630 of_node_put(cpu);
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000631}
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000632
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000633unsigned long get_boot_time(void)
634{
635 struct rtc_time tm;
636
637 if (ppc_md.get_boot_time)
638 return ppc_md.get_boot_time();
639 if (!ppc_md.get_rtc_time)
640 return 0;
641 ppc_md.get_rtc_time(&tm);
642 return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
643 tm.tm_hour, tm.tm_min, tm.tm_sec);
644}
645
646/* This function is only called on the boot processor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647void __init time_init(void)
648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 unsigned long flags;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000650 unsigned long tm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct div_result res;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000652 u64 scale;
653 unsigned shift;
654
655 if (ppc_md.time_init != NULL)
656 timezone_offset = ppc_md.time_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Paul Mackerras96c44502005-10-23 17:14:56 +1000658 if (__USE_RTC()) {
659 /* 601 processor: dec counts down by 128 every 128ns */
660 ppc_tb_freq = 1000000000;
661 tb_last_stamp = get_rtcl();
662 tb_last_jiffy = tb_last_stamp;
663 } else {
664 /* Normal PowerPC with timebase register */
665 ppc_md.calibrate_decr();
666 printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
667 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
668 printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
669 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
670 tb_last_stamp = tb_last_jiffy = get_tb();
671 }
Paul Mackerras374e99d2005-10-20 21:04:51 +1000672
673 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
674 tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
675 tb_ticks_per_usec = ppc_tb_freq / 1000000;
676 tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
677 div128_by_32(1024*1024, 0, tb_ticks_per_sec, &res);
678 tb_to_xs = res.result_low;
679
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000680#ifdef CONFIG_PPC64
681 get_paca()->default_decr = tb_ticks_per_jiffy;
682#endif
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Compute scale factor for sched_clock.
686 * The calibrate_decr() function has set tb_ticks_per_sec,
687 * which is the timebase frequency.
688 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
689 * the 128-bit result as a 64.64 fixed-point number.
690 * We then shift that number right until it is less than 1.0,
691 * giving us the scale factor and shift count to use in
692 * sched_clock().
693 */
694 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
695 scale = res.result_low;
696 for (shift = 0; res.result_high != 0; ++shift) {
697 scale = (scale >> 1) | (res.result_high << 63);
698 res.result_high >>= 1;
699 }
700 tb_to_ns_scale = scale;
701 tb_to_ns_shift = shift;
702
703#ifdef CONFIG_PPC_ISERIES
704 if (!piranha_simulator)
705#endif
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000706 tm = get_boot_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 write_seqlock_irqsave(&xtime_lock, flags);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000709 xtime.tv_sec = tm;
710 xtime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 do_gtod.varp = &do_gtod.vars[0];
712 do_gtod.var_idx = 0;
Paul Mackerras96c44502005-10-23 17:14:56 +1000713 do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000714 __get_cpu_var(last_jiffy) = tb_last_stamp;
715 do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
717 do_gtod.varp->tb_to_xs = tb_to_xs;
718 do_gtod.tb_to_us = tb_to_us;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000719#ifdef CONFIG_PPC64
Paul Mackerras96c44502005-10-23 17:14:56 +1000720 systemcfg->tb_orig_stamp = tb_last_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 systemcfg->tb_update_count = 0;
722 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
723 systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
724 systemcfg->tb_to_xs = tb_to_xs;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 time_freq = 0;
728
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000729 /* If platform provided a timezone (pmac), we correct the time */
730 if (timezone_offset) {
731 sys_tz.tz_minuteswest = -timezone_offset / 60;
732 sys_tz.tz_dsttime = 0;
733 xtime.tv_sec -= timezone_offset;
734 }
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 last_rtc_update = xtime.tv_sec;
737 set_normalized_timespec(&wall_to_monotonic,
738 -xtime.tv_sec, -xtime.tv_nsec);
739 write_sequnlock_irqrestore(&xtime_lock, flags);
740
741 /* Not exact, but the timer interrupt takes care of this */
742 set_dec(tb_ticks_per_jiffy);
743}
744
745/*
746 * After adjtimex is called, adjust the conversion of tb ticks
747 * to microseconds to keep do_gettimeofday synchronized
748 * with ntpd.
749 *
750 * Use the time_adjust, time_freq and time_offset computed by adjtimex to
751 * adjust the frequency.
752 */
753
754/* #define DEBUG_PPC_ADJTIMEX 1 */
755
756void ppc_adjtimex(void)
757{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000758#ifdef CONFIG_PPC64
759 unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec,
760 new_tb_to_xs, new_xsec, new_stamp_xsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 unsigned long tb_ticks_per_sec_delta;
762 long delta_freq, ltemp;
763 struct div_result divres;
764 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 long singleshot_ppm = 0;
766
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000767 /*
768 * Compute parts per million frequency adjustment to
769 * accomplish the time adjustment implied by time_offset to be
770 * applied over the elapsed time indicated by time_constant.
771 * Use SHIFT_USEC to get it into the same units as
772 * time_freq.
773 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if ( time_offset < 0 ) {
775 ltemp = -time_offset;
776 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
777 ltemp >>= SHIFT_KG + time_constant;
778 ltemp = -ltemp;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000779 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ltemp = time_offset;
781 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
782 ltemp >>= SHIFT_KG + time_constant;
783 }
784
785 /* If there is a single shot time adjustment in progress */
786 if ( time_adjust ) {
787#ifdef DEBUG_PPC_ADJTIMEX
788 printk("ppc_adjtimex: ");
789 if ( adjusting_time == 0 )
790 printk("starting ");
791 printk("single shot time_adjust = %ld\n", time_adjust);
792#endif
793
794 adjusting_time = 1;
795
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000796 /*
797 * Compute parts per million frequency adjustment
798 * to match time_adjust
799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 singleshot_ppm = tickadj * HZ;
801 /*
802 * The adjustment should be tickadj*HZ to match the code in
803 * linux/kernel/timer.c, but experiments show that this is too
804 * large. 3/4 of tickadj*HZ seems about right
805 */
806 singleshot_ppm -= singleshot_ppm / 4;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000807 /* Use SHIFT_USEC to get it into the same units as time_freq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 singleshot_ppm <<= SHIFT_USEC;
809 if ( time_adjust < 0 )
810 singleshot_ppm = -singleshot_ppm;
811 }
812 else {
813#ifdef DEBUG_PPC_ADJTIMEX
814 if ( adjusting_time )
815 printk("ppc_adjtimex: ending single shot time_adjust\n");
816#endif
817 adjusting_time = 0;
818 }
819
820 /* Add up all of the frequency adjustments */
821 delta_freq = time_freq + ltemp + singleshot_ppm;
822
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000823 /*
824 * Compute a new value for tb_ticks_per_sec based on
825 * the frequency adjustment
826 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 den = 1000000 * (1 << (SHIFT_USEC - 8));
828 if ( delta_freq < 0 ) {
829 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
830 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
831 }
832 else {
833 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
834 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
835 }
836
837#ifdef DEBUG_PPC_ADJTIMEX
838 printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
839 printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
840#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /*
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000843 * Compute a new value of tb_to_xs (used to convert tb to
844 * microseconds) and a new value of stamp_xsec which is the
845 * time (in 1/2^20 second units) corresponding to
846 * tb_orig_stamp. This new value of stamp_xsec compensates
847 * for the change in frequency (implied by the new tb_to_xs)
848 * which guarantees that the current time remains the same.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000850 write_seqlock_irqsave( &xtime_lock, flags );
851 tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
852 div128_by_32(1024*1024, 0, new_tb_ticks_per_sec, &divres);
853 new_tb_to_xs = divres.result_low;
854 new_xsec = mulhdu(tb_ticks, new_tb_to_xs);
855
856 old_xsec = mulhdu(tb_ticks, do_gtod.varp->tb_to_xs);
857 new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
858
859 update_gtod(do_gtod.varp->tb_orig_stamp, new_stamp_xsec, new_tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 write_sequnlock_irqrestore( &xtime_lock, flags );
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000862#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866#define FEBRUARY 2
867#define STARTOFTIME 1970
868#define SECDAY 86400L
869#define SECYR (SECDAY * 365)
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000870#define leapyear(year) ((year) % 4 == 0 && \
871 ((year) % 100 != 0 || (year) % 400 == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#define days_in_year(a) (leapyear(a) ? 366 : 365)
873#define days_in_month(a) (month_days[(a) - 1])
874
875static int month_days[12] = {
876 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
877};
878
879/*
880 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
881 */
882void GregorianDay(struct rtc_time * tm)
883{
884 int leapsToDate;
885 int lastYear;
886 int day;
887 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
888
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000889 lastYear = tm->tm_year - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /*
892 * Number of leap corrections to apply up to end of last year
893 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000894 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /*
897 * This year is a leap year if it is divisible by 4 except when it is
898 * divisible by 100 unless it is divisible by 400
899 *
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000900 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000902 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
905 tm->tm_mday;
906
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000907 tm->tm_wday = day % 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910void to_tm(int tim, struct rtc_time * tm)
911{
912 register int i;
913 register long hms, day;
914
915 day = tim / SECDAY;
916 hms = tim % SECDAY;
917
918 /* Hours, minutes, seconds are easy */
919 tm->tm_hour = hms / 3600;
920 tm->tm_min = (hms % 3600) / 60;
921 tm->tm_sec = (hms % 3600) % 60;
922
923 /* Number of years in days */
924 for (i = STARTOFTIME; day >= days_in_year(i); i++)
925 day -= days_in_year(i);
926 tm->tm_year = i;
927
928 /* Number of months in days left */
929 if (leapyear(tm->tm_year))
930 days_in_month(FEBRUARY) = 29;
931 for (i = 1; day >= days_in_month(i); i++)
932 day -= days_in_month(i);
933 days_in_month(FEBRUARY) = 28;
934 tm->tm_mon = i;
935
936 /* Days are what is left over (+1) from all that. */
937 tm->tm_mday = day + 1;
938
939 /*
940 * Determine the day of week
941 */
942 GregorianDay(tm);
943}
944
945/* Auxiliary function to compute scaling factors */
946/* Actually the choice of a timebase running at 1/4 the of the bus
947 * frequency giving resolution of a few tens of nanoseconds is quite nice.
948 * It makes this computation very precise (27-28 bits typically) which
949 * is optimistic considering the stability of most processor clock
950 * oscillators and the precision with which the timebase frequency
951 * is measured but does not harm.
952 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000953unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
954{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 unsigned mlt=0, tmp, err;
956 /* No concern for performance, it's done once: use a stupid
957 * but safe and compact method to find the multiplier.
958 */
959
960 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000961 if (mulhwu(inscale, mlt|tmp) < outscale)
962 mlt |= tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
965 /* We might still be off by 1 for the best approximation.
966 * A side effect of this is that if outscale is too large
967 * the returned value will be zero.
968 * Many corner cases have been checked and seem to work,
969 * some might have been forgotten in the test however.
970 */
971
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000972 err = inscale * (mlt+1);
973 if (err <= inscale/2)
974 mlt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return mlt;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000976}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978/*
979 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
980 * result.
981 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000982void div128_by_32(u64 dividend_high, u64 dividend_low,
983 unsigned divisor, struct div_result *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000985 unsigned long a, b, c, d;
986 unsigned long w, x, y, z;
987 u64 ra, rb, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 a = dividend_high >> 32;
990 b = dividend_high & 0xffffffff;
991 c = dividend_low >> 32;
992 d = dividend_low & 0xffffffff;
993
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000994 w = a / divisor;
995 ra = ((u64)(a - (w * divisor)) << 32) + b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000997 rb = ((u64) do_div(ra, divisor) << 32) + c;
998 x = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Paul Mackerrasf2783c12005-10-20 09:23:26 +10001000 rc = ((u64) do_div(rb, divisor) << 32) + d;
1001 y = rb;
1002
1003 do_div(rc, divisor);
1004 z = rc;
Paul Mackerrasf2783c12005-10-20 09:23:26 +10001005
1006 dr->result_high = ((u64)w << 32) + x;
1007 dr->result_low = ((u64)y << 32) + z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009}