blob: 1c7ea5e0631146c37b929e552128d55e042b8516 [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>
Paul Mackerras2249ca92005-11-07 13:18:13 +110064#include <asm/smp.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100065#ifdef CONFIG_PPC64
66#include <asm/systemcfg.h>
67#include <asm/firmware.h>
68#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#ifdef CONFIG_PPC_ISERIES
Kelly Daly8875ccf2005-11-02 14:13:34 +110070#include <asm/iseries/it_lp_queue.h>
Kelly Daly8021b8a2005-11-02 11:41:12 +110071#include <asm/iseries/hv_call_xm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* keep track of when we need to update the rtc */
75time_t last_rtc_update;
76extern int piranha_simulator;
77#ifdef CONFIG_PPC_ISERIES
78unsigned long iSeries_recal_titan = 0;
79unsigned long iSeries_recal_tb = 0;
80static unsigned long first_settimeofday = 1;
81#endif
82
Paul Mackerrasf2783c12005-10-20 09:23:26 +100083/* The decrementer counts down by 128 every 128ns on a 601. */
84#define DECREMENTER_COUNT_601 (1000000000 / HZ)
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define XSEC_PER_SEC (1024*1024)
87
Paul Mackerrasf2783c12005-10-20 09:23:26 +100088#ifdef CONFIG_PPC64
89#define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
90#else
91/* compute ((xsec << 12) * max) >> 32 */
92#define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
93#endif
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095unsigned long tb_ticks_per_jiffy;
96unsigned long tb_ticks_per_usec = 100; /* sane default */
97EXPORT_SYMBOL(tb_ticks_per_usec);
98unsigned long tb_ticks_per_sec;
Paul Mackerrasf2783c12005-10-20 09:23:26 +100099u64 tb_to_xs;
100unsigned tb_to_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101unsigned long processor_freq;
102DEFINE_SPINLOCK(rtc_lock);
Benjamin Herrenschmidt6ae3db12005-06-27 14:36:35 -0700103EXPORT_SYMBOL_GPL(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000105u64 tb_to_ns_scale;
106unsigned tb_to_ns_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108struct gettimeofday_struct do_gtod;
109
110extern unsigned long wall_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112extern struct timezone sys_tz;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000113static long timezone_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115void ppc_adjtimex(void);
116
117static unsigned adjusting_time = 0;
118
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000119unsigned long ppc_proc_freq;
120unsigned long ppc_tb_freq;
121
Paul Mackerras96c44502005-10-23 17:14:56 +1000122u64 tb_last_jiffy __cacheline_aligned_in_smp;
123unsigned long tb_last_stamp;
124
125/*
126 * Note that on ppc32 this only stores the bottom 32 bits of
127 * the timebase value, but that's enough to tell when a jiffy
128 * has passed.
129 */
130DEFINE_PER_CPU(unsigned long, last_jiffy);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static __inline__ void timer_check_rtc(void)
133{
134 /*
135 * update the rtc when needed, this should be performed on the
136 * right fraction of a second. Half or full second ?
137 * Full second works on mk48t59 clocks, others need testing.
138 * Note that this update is basically only used through
139 * the adjtimex system calls. Setting the HW clock in
140 * any other way is a /dev/rtc and userland business.
141 * This is still wrong by -0.5/+1.5 jiffies because of the
142 * timer interrupt resolution and possible delay, but here we
143 * hit a quantization limit which can only be solved by higher
144 * resolution timers and decoupling time management from timer
145 * interrupts. This is also wrong on the clocks
146 * which require being written at the half second boundary.
147 * We should have an rtc call that only sets the minutes and
148 * seconds like on Intel to avoid problems with non UTC clocks.
149 */
Kumar Galad2e61512005-10-20 11:43:33 -0500150 if (ppc_md.set_rtc_time && ntp_synced() &&
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000151 xtime.tv_sec - last_rtc_update >= 659 &&
152 abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ &&
153 jiffies - wall_jiffies == 1) {
154 struct rtc_time tm;
155 to_tm(xtime.tv_sec + 1 + timezone_offset, &tm);
156 tm.tm_year -= 1900;
157 tm.tm_mon -= 1;
158 if (ppc_md.set_rtc_time(&tm) == 0)
159 last_rtc_update = xtime.tv_sec + 1;
160 else
161 /* Try again one minute later */
162 last_rtc_update += 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164}
165
166/*
167 * This version of gettimeofday has microsecond resolution.
168 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000169static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000171 unsigned long sec, usec;
172 u64 tb_ticks, xsec;
173 struct gettimeofday_vars *temp_varp;
174 u64 temp_tb_to_xs, temp_stamp_xsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /*
177 * These calculations are faster (gets rid of divides)
178 * if done in units of 1/2^20 rather than microseconds.
179 * The conversion to microseconds at the end is done
180 * without a divide (and in fact, without a multiply)
181 */
182 temp_varp = do_gtod.varp;
183 tb_ticks = tb_val - temp_varp->tb_orig_stamp;
184 temp_tb_to_xs = temp_varp->tb_to_xs;
185 temp_stamp_xsec = temp_varp->stamp_xsec;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000186 xsec = temp_stamp_xsec + mulhdu(tb_ticks, temp_tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 sec = xsec / XSEC_PER_SEC;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000188 usec = (unsigned long)xsec & (XSEC_PER_SEC - 1);
189 usec = SCALE_XSEC(usec, 1000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 tv->tv_sec = sec;
192 tv->tv_usec = usec;
193}
194
195void do_gettimeofday(struct timeval *tv)
196{
Paul Mackerras96c44502005-10-23 17:14:56 +1000197 if (__USE_RTC()) {
198 /* do this the old way */
199 unsigned long flags, seq;
200 unsigned int sec, nsec, usec, lost;
201
202 do {
203 seq = read_seqbegin_irqsave(&xtime_lock, flags);
204 sec = xtime.tv_sec;
205 nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp);
206 lost = jiffies - wall_jiffies;
207 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
208 usec = nsec / 1000 + lost * (1000000 / HZ);
209 while (usec >= 1000000) {
210 usec -= 1000000;
211 ++sec;
212 }
213 tv->tv_sec = sec;
214 tv->tv_usec = usec;
215 return;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 __do_gettimeofday(tv, get_tb());
218}
219
220EXPORT_SYMBOL(do_gettimeofday);
221
222/* Synchronize xtime with do_gettimeofday */
223
224static inline void timer_sync_xtime(unsigned long cur_tb)
225{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000226#ifdef CONFIG_PPC64
227 /* why do we do this? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct timeval my_tv;
229
230 __do_gettimeofday(&my_tv, cur_tb);
231
232 if (xtime.tv_sec <= my_tv.tv_sec) {
233 xtime.tv_sec = my_tv.tv_sec;
234 xtime.tv_nsec = my_tv.tv_usec * 1000;
235 }
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000236#endif
237}
238
239/*
240 * There are two copies of tb_to_xs and stamp_xsec so that no
241 * lock is needed to access and use these values in
242 * do_gettimeofday. We alternate the copies and as long as a
243 * reasonable time elapses between changes, there will never
244 * be inconsistent values. ntpd has a minimum of one minute
245 * between updates.
246 */
247static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec,
Paul Mackerras5d14a182005-10-20 22:33:06 +1000248 u64 new_tb_to_xs)
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000249{
250 unsigned temp_idx;
251 struct gettimeofday_vars *temp_varp;
252
253 temp_idx = (do_gtod.var_idx == 0);
254 temp_varp = &do_gtod.vars[temp_idx];
255
256 temp_varp->tb_to_xs = new_tb_to_xs;
257 temp_varp->tb_orig_stamp = new_tb_stamp;
258 temp_varp->stamp_xsec = new_stamp_xsec;
259 smp_mb();
260 do_gtod.varp = temp_varp;
261 do_gtod.var_idx = temp_idx;
262
263#ifdef CONFIG_PPC64
264 /*
265 * tb_update_count is used to allow the userspace gettimeofday code
266 * to assure itself that it sees a consistent view of the tb_to_xs and
267 * stamp_xsec variables. It reads the tb_update_count, then reads
268 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
269 * the two values of tb_update_count match and are even then the
270 * tb_to_xs and stamp_xsec values are consistent. If not, then it
271 * loops back and reads them again until this criteria is met.
272 */
273 ++(systemcfg->tb_update_count);
274 smp_wmb();
275 systemcfg->tb_orig_stamp = new_tb_stamp;
276 systemcfg->stamp_xsec = new_stamp_xsec;
277 systemcfg->tb_to_xs = new_tb_to_xs;
278 smp_wmb();
279 ++(systemcfg->tb_update_count);
280#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/*
284 * When the timebase - tb_orig_stamp gets too big, we do a manipulation
285 * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
286 * difference tb - tb_orig_stamp small enough to always fit inside a
287 * 32 bits number. This is a requirement of our fast 32 bits userland
288 * implementation in the vdso. If we "miss" a call to this function
289 * (interrupt latency, CPU locked in a spinlock, ...) and we end up
290 * with a too big difference, then the vdso will fallback to calling
291 * the syscall
292 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000293static __inline__ void timer_recalc_offset(u64 cur_tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000295 unsigned long offset;
296 u64 new_stamp_xsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Paul Mackerras96c44502005-10-23 17:14:56 +1000298 if (__USE_RTC())
299 return;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000300 offset = cur_tb - do_gtod.varp->tb_orig_stamp;
301 if ((offset & 0x80000000u) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000303 new_stamp_xsec = do_gtod.varp->stamp_xsec
304 + mulhdu(offset, do_gtod.varp->tb_to_xs);
305 update_gtod(cur_tb, new_stamp_xsec, do_gtod.varp->tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308#ifdef CONFIG_SMP
309unsigned long profile_pc(struct pt_regs *regs)
310{
311 unsigned long pc = instruction_pointer(regs);
312
313 if (in_lock_functions(pc))
314 return regs->link;
315
316 return pc;
317}
318EXPORT_SYMBOL(profile_pc);
319#endif
320
321#ifdef CONFIG_PPC_ISERIES
322
323/*
324 * This function recalibrates the timebase based on the 49-bit time-of-day
325 * value in the Titan chip. The Titan is much more accurate than the value
326 * returned by the service processor for the timebase frequency.
327 */
328
329static void iSeries_tb_recal(void)
330{
331 struct div_result divres;
332 unsigned long titan, tb;
333 tb = get_tb();
334 titan = HvCallXm_loadTod();
335 if ( iSeries_recal_titan ) {
336 unsigned long tb_ticks = tb - iSeries_recal_tb;
337 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
338 unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
339 unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
340 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
341 char sign = '+';
342 /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
343 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
344
345 if ( tick_diff < 0 ) {
346 tick_diff = -tick_diff;
347 sign = '-';
348 }
349 if ( tick_diff ) {
350 if ( tick_diff < tb_ticks_per_jiffy/25 ) {
351 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
352 new_tb_ticks_per_jiffy, sign, tick_diff );
353 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
354 tb_ticks_per_sec = new_tb_ticks_per_sec;
355 div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
356 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
357 tb_to_xs = divres.result_low;
358 do_gtod.varp->tb_to_xs = tb_to_xs;
359 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
360 systemcfg->tb_to_xs = tb_to_xs;
361 }
362 else {
363 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
364 " new tb_ticks_per_jiffy = %lu\n"
365 " old tb_ticks_per_jiffy = %lu\n",
366 new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
367 }
368 }
369 }
370 iSeries_recal_titan = titan;
371 iSeries_recal_tb = tb;
372}
373#endif
374
375/*
376 * For iSeries shared processors, we have to let the hypervisor
377 * set the hardware decrementer. We set a virtual decrementer
378 * in the lppaca and call the hypervisor if the virtual
379 * decrementer is less than the current value in the hardware
380 * decrementer. (almost always the new decrementer value will
381 * be greater than the current hardware decementer so the hypervisor
382 * call will not be needed)
383 */
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
386 * timer_interrupt - gets called when the decrementer overflows,
387 * with interrupts disabled.
388 */
Kumar Galac7aeffc2005-09-19 09:30:27 -0500389void timer_interrupt(struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 int next_dec;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000392 int cpu = smp_processor_id();
393 unsigned long ticks;
394
395#ifdef CONFIG_PPC32
396 if (atomic_read(&ppc_n_lost_interrupts) != 0)
397 do_IRQ(regs);
398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 irq_enter();
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 profile_tick(CPU_PROFILING, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000404#ifdef CONFIG_PPC_ISERIES
405 get_paca()->lppaca.int_dword.fields.decr_int = 0;
406#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000408 while ((ticks = tb_ticks_since(per_cpu(last_jiffy, cpu)))
409 >= tb_ticks_per_jiffy) {
410 /* Update last_jiffy */
411 per_cpu(last_jiffy, cpu) += tb_ticks_per_jiffy;
412 /* Handle RTCL overflow on 601 */
413 if (__USE_RTC() && per_cpu(last_jiffy, cpu) >= 1000000000)
414 per_cpu(last_jiffy, cpu) -= 1000000000;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /*
417 * We cannot disable the decrementer, so in the period
418 * between this cpu's being marked offline in cpu_online_map
419 * and calling stop-self, it is taking timer interrupts.
420 * Avoid calling into the scheduler rebalancing code if this
421 * is the case.
422 */
423 if (!cpu_is_offline(cpu))
424 update_process_times(user_mode(regs));
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /*
427 * No need to check whether cpu is offline here; boot_cpuid
428 * should have been fixed up by now.
429 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000430 if (cpu != boot_cpuid)
431 continue;
432
433 write_seqlock(&xtime_lock);
Paul Mackerras96c44502005-10-23 17:14:56 +1000434 tb_last_jiffy += tb_ticks_per_jiffy;
435 tb_last_stamp = per_cpu(last_jiffy, cpu);
436 timer_recalc_offset(tb_last_jiffy);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000437 do_timer(regs);
Paul Mackerras96c44502005-10-23 17:14:56 +1000438 timer_sync_xtime(tb_last_jiffy);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000439 timer_check_rtc();
440 write_sequnlock(&xtime_lock);
441 if (adjusting_time && (time_adjust == 0))
442 ppc_adjtimex();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000445 next_dec = tb_ticks_per_jiffy - ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 set_dec(next_dec);
447
448#ifdef CONFIG_PPC_ISERIES
Michael Ellerman937b31b2005-06-30 15:15:42 +1000449 if (hvlpevent_is_pending())
Michael Ellerman74889802005-06-30 15:15:53 +1000450 process_hvlpevents(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#endif
452
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000453#ifdef CONFIG_PPC64
Stephen Rothwell8d15a3e2005-08-03 14:40:16 +1000454 /* collect purr register values often, for accurate calculations */
Stephen Rothwell1ababe12005-08-03 14:35:25 +1000455 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
457 cu->current_tb = mfspr(SPRN_PURR);
458 }
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000464void wakeup_decrementer(void)
465{
466 int i;
467
468 set_dec(tb_ticks_per_jiffy);
469 /*
470 * We don't expect this to be called on a machine with a 601,
471 * so using get_tbl is fine.
472 */
Paul Mackerras96c44502005-10-23 17:14:56 +1000473 tb_last_stamp = tb_last_jiffy = get_tb();
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000474 for_each_cpu(i)
475 per_cpu(last_jiffy, i) = tb_last_stamp;
476}
477
Paul Mackerrasa5b518e2005-10-22 14:55:23 +1000478#ifdef CONFIG_SMP
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000479void __init smp_space_timers(unsigned int max_cpus)
480{
481 int i;
482 unsigned long offset = tb_ticks_per_jiffy / max_cpus;
483 unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid);
484
485 for_each_cpu(i) {
486 if (i != boot_cpuid) {
487 previous_tb += offset;
488 per_cpu(last_jiffy, i) = previous_tb;
489 }
490 }
491}
492#endif
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * Scheduler clock - returns current time in nanosec units.
496 *
497 * Note: mulhdu(a, b) (multiply high double unsigned) returns
498 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
499 * are 64-bit unsigned numbers.
500 */
501unsigned long long sched_clock(void)
502{
Paul Mackerras96c44502005-10-23 17:14:56 +1000503 if (__USE_RTC())
504 return get_rtc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
506}
507
508int do_settimeofday(struct timespec *tv)
509{
510 time_t wtm_sec, new_sec = tv->tv_sec;
511 long wtm_nsec, new_nsec = tv->tv_nsec;
512 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 long int tb_delta;
Paul Mackerras5f6b5b92005-10-30 22:55:52 +1100514 u64 new_xsec, tb_delta_xs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
517 return -EINVAL;
518
519 write_seqlock_irqsave(&xtime_lock, flags);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000520
521 /*
522 * Updating the RTC is not the job of this code. If the time is
523 * stepped under NTP, the RTC will be updated after STA_UNSYNC
524 * is cleared. Tools like clock/hwclock either copy the RTC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * to the system time, in which case there is no point in writing
526 * to the RTC again, or write to the RTC but then they don't call
527 * settimeofday to perform this operation.
528 */
529#ifdef CONFIG_PPC_ISERIES
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000530 if (first_settimeofday) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 iSeries_tb_recal();
532 first_settimeofday = 0;
533 }
534#endif
535 tb_delta = tb_ticks_since(tb_last_stamp);
536 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
Paul Mackerras5f6b5b92005-10-30 22:55:52 +1100537 tb_delta_xs = mulhdu(tb_delta, do_gtod.varp->tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
540 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
541
542 set_normalized_timespec(&xtime, new_sec, new_nsec);
543 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
544
545 /* In case of a large backwards jump in time with NTP, we want the
546 * clock to be updated as soon as the PLL is again in lock.
547 */
548 last_rtc_update = new_sec - 658;
549
john stultzb149ee22005-09-06 15:17:46 -0700550 ntp_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Paul Mackerras5f6b5b92005-10-30 22:55:52 +1100552 new_xsec = 0;
553 if (new_nsec != 0) {
554 new_xsec = (u64)new_nsec * XSEC_PER_SEC;
555 do_div(new_xsec, NSEC_PER_SEC);
556 }
557 new_xsec += (u64)new_sec * XSEC_PER_SEC - tb_delta_xs;
Paul Mackerras96c44502005-10-23 17:14:56 +1000558 update_gtod(tb_last_jiffy, new_xsec, do_gtod.varp->tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000560#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
562 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 write_sequnlock_irqrestore(&xtime_lock, flags);
566 clock_was_set();
567 return 0;
568}
569
570EXPORT_SYMBOL(do_settimeofday);
571
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000572void __init generic_calibrate_decr(void)
573{
574 struct device_node *cpu;
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000575 unsigned int *fp;
576 int node_found;
577
578 /*
579 * The cpu node should have a timebase-frequency property
580 * to tell us the rate at which the decrementer counts.
581 */
582 cpu = of_find_node_by_type(NULL, "cpu");
583
584 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
585 node_found = 0;
586 if (cpu != 0) {
587 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
588 NULL);
589 if (fp != 0) {
590 node_found = 1;
591 ppc_tb_freq = *fp;
592 }
593 }
594 if (!node_found)
595 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
596 "(not found)\n");
597
598 ppc_proc_freq = DEFAULT_PROC_FREQ;
599 node_found = 0;
600 if (cpu != 0) {
601 fp = (unsigned int *)get_property(cpu, "clock-frequency",
602 NULL);
603 if (fp != 0) {
604 node_found = 1;
605 ppc_proc_freq = *fp;
606 }
607 }
Kumar Gala0fd6f712005-10-25 23:02:59 -0500608#ifdef CONFIG_BOOKE
609 /* Set the time base to zero */
610 mtspr(SPRN_TBWL, 0);
611 mtspr(SPRN_TBWU, 0);
612
613 /* Clear any pending timer interrupts */
614 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
615
616 /* Enable decrementer interrupt */
617 mtspr(SPRN_TCR, TCR_DIE);
618#endif
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000619 if (!node_found)
620 printk(KERN_ERR "WARNING: Estimating processor frequency "
621 "(not found)\n");
622
623 of_node_put(cpu);
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000624}
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000625
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000626unsigned long get_boot_time(void)
627{
628 struct rtc_time tm;
629
630 if (ppc_md.get_boot_time)
631 return ppc_md.get_boot_time();
632 if (!ppc_md.get_rtc_time)
633 return 0;
634 ppc_md.get_rtc_time(&tm);
635 return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
636 tm.tm_hour, tm.tm_min, tm.tm_sec);
637}
638
639/* This function is only called on the boot processor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640void __init time_init(void)
641{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 unsigned long flags;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000643 unsigned long tm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct div_result res;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000645 u64 scale;
646 unsigned shift;
647
648 if (ppc_md.time_init != NULL)
649 timezone_offset = ppc_md.time_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Paul Mackerras96c44502005-10-23 17:14:56 +1000651 if (__USE_RTC()) {
652 /* 601 processor: dec counts down by 128 every 128ns */
653 ppc_tb_freq = 1000000000;
654 tb_last_stamp = get_rtcl();
655 tb_last_jiffy = tb_last_stamp;
656 } else {
657 /* Normal PowerPC with timebase register */
658 ppc_md.calibrate_decr();
659 printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
660 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
661 printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
662 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
663 tb_last_stamp = tb_last_jiffy = get_tb();
664 }
Paul Mackerras374e99d2005-10-20 21:04:51 +1000665
666 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
667 tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
668 tb_ticks_per_usec = ppc_tb_freq / 1000000;
669 tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
670 div128_by_32(1024*1024, 0, tb_ticks_per_sec, &res);
671 tb_to_xs = res.result_low;
672
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000673#ifdef CONFIG_PPC64
674 get_paca()->default_decr = tb_ticks_per_jiffy;
675#endif
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * Compute scale factor for sched_clock.
679 * The calibrate_decr() function has set tb_ticks_per_sec,
680 * which is the timebase frequency.
681 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
682 * the 128-bit result as a 64.64 fixed-point number.
683 * We then shift that number right until it is less than 1.0,
684 * giving us the scale factor and shift count to use in
685 * sched_clock().
686 */
687 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
688 scale = res.result_low;
689 for (shift = 0; res.result_high != 0; ++shift) {
690 scale = (scale >> 1) | (res.result_high << 63);
691 res.result_high >>= 1;
692 }
693 tb_to_ns_scale = scale;
694 tb_to_ns_shift = shift;
695
696#ifdef CONFIG_PPC_ISERIES
697 if (!piranha_simulator)
698#endif
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000699 tm = get_boot_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 write_seqlock_irqsave(&xtime_lock, flags);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000702 xtime.tv_sec = tm;
703 xtime.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 do_gtod.varp = &do_gtod.vars[0];
705 do_gtod.var_idx = 0;
Paul Mackerras96c44502005-10-23 17:14:56 +1000706 do_gtod.varp->tb_orig_stamp = tb_last_jiffy;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000707 __get_cpu_var(last_jiffy) = tb_last_stamp;
708 do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
710 do_gtod.varp->tb_to_xs = tb_to_xs;
711 do_gtod.tb_to_us = tb_to_us;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000712#ifdef CONFIG_PPC64
Paul Mackerras96c44502005-10-23 17:14:56 +1000713 systemcfg->tb_orig_stamp = tb_last_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 systemcfg->tb_update_count = 0;
715 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
716 systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
717 systemcfg->tb_to_xs = tb_to_xs;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 time_freq = 0;
721
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000722 /* If platform provided a timezone (pmac), we correct the time */
723 if (timezone_offset) {
724 sys_tz.tz_minuteswest = -timezone_offset / 60;
725 sys_tz.tz_dsttime = 0;
726 xtime.tv_sec -= timezone_offset;
727 }
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 last_rtc_update = xtime.tv_sec;
730 set_normalized_timespec(&wall_to_monotonic,
731 -xtime.tv_sec, -xtime.tv_nsec);
732 write_sequnlock_irqrestore(&xtime_lock, flags);
733
734 /* Not exact, but the timer interrupt takes care of this */
735 set_dec(tb_ticks_per_jiffy);
736}
737
738/*
739 * After adjtimex is called, adjust the conversion of tb ticks
740 * to microseconds to keep do_gettimeofday synchronized
741 * with ntpd.
742 *
743 * Use the time_adjust, time_freq and time_offset computed by adjtimex to
744 * adjust the frequency.
745 */
746
747/* #define DEBUG_PPC_ADJTIMEX 1 */
748
749void ppc_adjtimex(void)
750{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000751#ifdef CONFIG_PPC64
752 unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec,
753 new_tb_to_xs, new_xsec, new_stamp_xsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 unsigned long tb_ticks_per_sec_delta;
755 long delta_freq, ltemp;
756 struct div_result divres;
757 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 long singleshot_ppm = 0;
759
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000760 /*
761 * Compute parts per million frequency adjustment to
762 * accomplish the time adjustment implied by time_offset to be
763 * applied over the elapsed time indicated by time_constant.
764 * Use SHIFT_USEC to get it into the same units as
765 * time_freq.
766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if ( time_offset < 0 ) {
768 ltemp = -time_offset;
769 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
770 ltemp >>= SHIFT_KG + time_constant;
771 ltemp = -ltemp;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000772 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ltemp = time_offset;
774 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
775 ltemp >>= SHIFT_KG + time_constant;
776 }
777
778 /* If there is a single shot time adjustment in progress */
779 if ( time_adjust ) {
780#ifdef DEBUG_PPC_ADJTIMEX
781 printk("ppc_adjtimex: ");
782 if ( adjusting_time == 0 )
783 printk("starting ");
784 printk("single shot time_adjust = %ld\n", time_adjust);
785#endif
786
787 adjusting_time = 1;
788
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000789 /*
790 * Compute parts per million frequency adjustment
791 * to match time_adjust
792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 singleshot_ppm = tickadj * HZ;
794 /*
795 * The adjustment should be tickadj*HZ to match the code in
796 * linux/kernel/timer.c, but experiments show that this is too
797 * large. 3/4 of tickadj*HZ seems about right
798 */
799 singleshot_ppm -= singleshot_ppm / 4;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000800 /* Use SHIFT_USEC to get it into the same units as time_freq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 singleshot_ppm <<= SHIFT_USEC;
802 if ( time_adjust < 0 )
803 singleshot_ppm = -singleshot_ppm;
804 }
805 else {
806#ifdef DEBUG_PPC_ADJTIMEX
807 if ( adjusting_time )
808 printk("ppc_adjtimex: ending single shot time_adjust\n");
809#endif
810 adjusting_time = 0;
811 }
812
813 /* Add up all of the frequency adjustments */
814 delta_freq = time_freq + ltemp + singleshot_ppm;
815
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000816 /*
817 * Compute a new value for tb_ticks_per_sec based on
818 * the frequency adjustment
819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 den = 1000000 * (1 << (SHIFT_USEC - 8));
821 if ( delta_freq < 0 ) {
822 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
823 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
824 }
825 else {
826 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
827 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
828 }
829
830#ifdef DEBUG_PPC_ADJTIMEX
831 printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
832 printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /*
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000836 * Compute a new value of tb_to_xs (used to convert tb to
837 * microseconds) and a new value of stamp_xsec which is the
838 * time (in 1/2^20 second units) corresponding to
839 * tb_orig_stamp. This new value of stamp_xsec compensates
840 * for the change in frequency (implied by the new tb_to_xs)
841 * which guarantees that the current time remains the same.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000843 write_seqlock_irqsave( &xtime_lock, flags );
844 tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
845 div128_by_32(1024*1024, 0, new_tb_ticks_per_sec, &divres);
846 new_tb_to_xs = divres.result_low;
847 new_xsec = mulhdu(tb_ticks, new_tb_to_xs);
848
849 old_xsec = mulhdu(tb_ticks, do_gtod.varp->tb_to_xs);
850 new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
851
852 update_gtod(do_gtod.varp->tb_orig_stamp, new_stamp_xsec, new_tb_to_xs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 write_sequnlock_irqrestore( &xtime_lock, flags );
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000855#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859#define FEBRUARY 2
860#define STARTOFTIME 1970
861#define SECDAY 86400L
862#define SECYR (SECDAY * 365)
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000863#define leapyear(year) ((year) % 4 == 0 && \
864 ((year) % 100 != 0 || (year) % 400 == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865#define days_in_year(a) (leapyear(a) ? 366 : 365)
866#define days_in_month(a) (month_days[(a) - 1])
867
868static int month_days[12] = {
869 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
870};
871
872/*
873 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
874 */
875void GregorianDay(struct rtc_time * tm)
876{
877 int leapsToDate;
878 int lastYear;
879 int day;
880 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
881
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000882 lastYear = tm->tm_year - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /*
885 * Number of leap corrections to apply up to end of last year
886 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000887 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /*
890 * This year is a leap year if it is divisible by 4 except when it is
891 * divisible by 100 unless it is divisible by 400
892 *
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000893 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000895 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
898 tm->tm_mday;
899
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000900 tm->tm_wday = day % 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903void to_tm(int tim, struct rtc_time * tm)
904{
905 register int i;
906 register long hms, day;
907
908 day = tim / SECDAY;
909 hms = tim % SECDAY;
910
911 /* Hours, minutes, seconds are easy */
912 tm->tm_hour = hms / 3600;
913 tm->tm_min = (hms % 3600) / 60;
914 tm->tm_sec = (hms % 3600) % 60;
915
916 /* Number of years in days */
917 for (i = STARTOFTIME; day >= days_in_year(i); i++)
918 day -= days_in_year(i);
919 tm->tm_year = i;
920
921 /* Number of months in days left */
922 if (leapyear(tm->tm_year))
923 days_in_month(FEBRUARY) = 29;
924 for (i = 1; day >= days_in_month(i); i++)
925 day -= days_in_month(i);
926 days_in_month(FEBRUARY) = 28;
927 tm->tm_mon = i;
928
929 /* Days are what is left over (+1) from all that. */
930 tm->tm_mday = day + 1;
931
932 /*
933 * Determine the day of week
934 */
935 GregorianDay(tm);
936}
937
938/* Auxiliary function to compute scaling factors */
939/* Actually the choice of a timebase running at 1/4 the of the bus
940 * frequency giving resolution of a few tens of nanoseconds is quite nice.
941 * It makes this computation very precise (27-28 bits typically) which
942 * is optimistic considering the stability of most processor clock
943 * oscillators and the precision with which the timebase frequency
944 * is measured but does not harm.
945 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000946unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale)
947{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 unsigned mlt=0, tmp, err;
949 /* No concern for performance, it's done once: use a stupid
950 * but safe and compact method to find the multiplier.
951 */
952
953 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000954 if (mulhwu(inscale, mlt|tmp) < outscale)
955 mlt |= tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
958 /* We might still be off by 1 for the best approximation.
959 * A side effect of this is that if outscale is too large
960 * the returned value will be zero.
961 * Many corner cases have been checked and seem to work,
962 * some might have been forgotten in the test however.
963 */
964
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000965 err = inscale * (mlt+1);
966 if (err <= inscale/2)
967 mlt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return mlt;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000969}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971/*
972 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
973 * result.
974 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000975void div128_by_32(u64 dividend_high, u64 dividend_low,
976 unsigned divisor, struct div_result *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000978 unsigned long a, b, c, d;
979 unsigned long w, x, y, z;
980 u64 ra, rb, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 a = dividend_high >> 32;
983 b = dividend_high & 0xffffffff;
984 c = dividend_low >> 32;
985 d = dividend_low & 0xffffffff;
986
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000987 w = a / divisor;
988 ra = ((u64)(a - (w * divisor)) << 32) + b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000990 rb = ((u64) do_div(ra, divisor) << 32) + c;
991 x = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000993 rc = ((u64) do_div(rb, divisor) << 32) + d;
994 y = rb;
995
996 do_div(rc, divisor);
997 z = rc;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000998
999 dr->result_high = ((u64)w << 32) + x;
1000 dr->result_low = ((u64)y << 32) + z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002}