]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
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 | ||
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> | |
1da177e4 LT |
46 | #include <linux/time.h> |
47 | #include <linux/init.h> | |
48 | #include <linux/profile.h> | |
49 | #include <linux/cpu.h> | |
50 | #include <linux/security.h> | |
f2783c15 PM |
51 | #include <linux/percpu.h> |
52 | #include <linux/rtc.h> | |
1da177e4 | 53 | |
1da177e4 LT |
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> | |
1da177e4 LT |
59 | #include <asm/uaccess.h> |
60 | #include <asm/time.h> | |
1da177e4 | 61 | #include <asm/prom.h> |
f2783c15 PM |
62 | #include <asm/irq.h> |
63 | #include <asm/div64.h> | |
2249ca9d | 64 | #include <asm/smp.h> |
a7f290da | 65 | #include <asm/vdso_datapage.h> |
f2783c15 | 66 | #ifdef CONFIG_PPC64 |
1ababe11 | 67 | #include <asm/firmware.h> |
f2783c15 PM |
68 | #endif |
69 | #ifdef CONFIG_PPC_ISERIES | |
8875ccfb | 70 | #include <asm/iseries/it_lp_queue.h> |
8021b8a7 | 71 | #include <asm/iseries/hv_call_xm.h> |
f2783c15 | 72 | #endif |
732ee21f | 73 | #include <asm/smp.h> |
1da177e4 | 74 | |
1da177e4 LT |
75 | /* keep track of when we need to update the rtc */ |
76 | time_t last_rtc_update; | |
77 | extern int piranha_simulator; | |
78 | #ifdef CONFIG_PPC_ISERIES | |
79 | unsigned long iSeries_recal_titan = 0; | |
80 | unsigned long iSeries_recal_tb = 0; | |
81 | static unsigned long first_settimeofday = 1; | |
82 | #endif | |
83 | ||
f2783c15 PM |
84 | /* The decrementer counts down by 128 every 128ns on a 601. */ |
85 | #define DECREMENTER_COUNT_601 (1000000000 / HZ) | |
86 | ||
1da177e4 LT |
87 | #define XSEC_PER_SEC (1024*1024) |
88 | ||
f2783c15 PM |
89 | #ifdef CONFIG_PPC64 |
90 | #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) | |
91 | #else | |
92 | /* compute ((xsec << 12) * max) >> 32 */ | |
93 | #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) | |
94 | #endif | |
95 | ||
1da177e4 LT |
96 | unsigned long tb_ticks_per_jiffy; |
97 | unsigned long tb_ticks_per_usec = 100; /* sane default */ | |
98 | EXPORT_SYMBOL(tb_ticks_per_usec); | |
99 | unsigned long tb_ticks_per_sec; | |
f2783c15 PM |
100 | u64 tb_to_xs; |
101 | unsigned tb_to_us; | |
1da177e4 LT |
102 | unsigned long processor_freq; |
103 | DEFINE_SPINLOCK(rtc_lock); | |
6ae3db11 | 104 | EXPORT_SYMBOL_GPL(rtc_lock); |
1da177e4 | 105 | |
f2783c15 PM |
106 | u64 tb_to_ns_scale; |
107 | unsigned tb_to_ns_shift; | |
1da177e4 LT |
108 | |
109 | struct gettimeofday_struct do_gtod; | |
110 | ||
111 | extern unsigned long wall_jiffies; | |
1da177e4 LT |
112 | |
113 | extern struct timezone sys_tz; | |
f2783c15 | 114 | static long timezone_offset; |
1da177e4 LT |
115 | |
116 | void ppc_adjtimex(void); | |
117 | ||
118 | static unsigned adjusting_time = 0; | |
119 | ||
10f7e7c1 AB |
120 | unsigned long ppc_proc_freq; |
121 | unsigned long ppc_tb_freq; | |
122 | ||
96c44507 PM |
123 | u64 tb_last_jiffy __cacheline_aligned_in_smp; |
124 | unsigned long tb_last_stamp; | |
125 | ||
126 | /* | |
127 | * Note that on ppc32 this only stores the bottom 32 bits of | |
128 | * the timebase value, but that's enough to tell when a jiffy | |
129 | * has passed. | |
130 | */ | |
131 | DEFINE_PER_CPU(unsigned long, last_jiffy); | |
132 | ||
1da177e4 LT |
133 | static __inline__ void timer_check_rtc(void) |
134 | { | |
135 | /* | |
136 | * update the rtc when needed, this should be performed on the | |
137 | * right fraction of a second. Half or full second ? | |
138 | * Full second works on mk48t59 clocks, others need testing. | |
139 | * Note that this update is basically only used through | |
140 | * the adjtimex system calls. Setting the HW clock in | |
141 | * any other way is a /dev/rtc and userland business. | |
142 | * This is still wrong by -0.5/+1.5 jiffies because of the | |
143 | * timer interrupt resolution and possible delay, but here we | |
144 | * hit a quantization limit which can only be solved by higher | |
145 | * resolution timers and decoupling time management from timer | |
146 | * interrupts. This is also wrong on the clocks | |
147 | * which require being written at the half second boundary. | |
148 | * We should have an rtc call that only sets the minutes and | |
149 | * seconds like on Intel to avoid problems with non UTC clocks. | |
150 | */ | |
d2e61512 | 151 | if (ppc_md.set_rtc_time && ntp_synced() && |
f2783c15 PM |
152 | xtime.tv_sec - last_rtc_update >= 659 && |
153 | abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ && | |
154 | jiffies - wall_jiffies == 1) { | |
155 | struct rtc_time tm; | |
156 | to_tm(xtime.tv_sec + 1 + timezone_offset, &tm); | |
157 | tm.tm_year -= 1900; | |
158 | tm.tm_mon -= 1; | |
159 | if (ppc_md.set_rtc_time(&tm) == 0) | |
160 | last_rtc_update = xtime.tv_sec + 1; | |
161 | else | |
162 | /* Try again one minute later */ | |
163 | last_rtc_update += 60; | |
1da177e4 LT |
164 | } |
165 | } | |
166 | ||
167 | /* | |
168 | * This version of gettimeofday has microsecond resolution. | |
169 | */ | |
f2783c15 | 170 | static inline void __do_gettimeofday(struct timeval *tv, u64 tb_val) |
1da177e4 | 171 | { |
f2783c15 PM |
172 | unsigned long sec, usec; |
173 | u64 tb_ticks, xsec; | |
174 | struct gettimeofday_vars *temp_varp; | |
175 | u64 temp_tb_to_xs, temp_stamp_xsec; | |
1da177e4 LT |
176 | |
177 | /* | |
178 | * These calculations are faster (gets rid of divides) | |
179 | * if done in units of 1/2^20 rather than microseconds. | |
180 | * The conversion to microseconds at the end is done | |
181 | * without a divide (and in fact, without a multiply) | |
182 | */ | |
183 | temp_varp = do_gtod.varp; | |
184 | tb_ticks = tb_val - temp_varp->tb_orig_stamp; | |
185 | temp_tb_to_xs = temp_varp->tb_to_xs; | |
186 | temp_stamp_xsec = temp_varp->stamp_xsec; | |
f2783c15 | 187 | xsec = temp_stamp_xsec + mulhdu(tb_ticks, temp_tb_to_xs); |
1da177e4 | 188 | sec = xsec / XSEC_PER_SEC; |
f2783c15 PM |
189 | usec = (unsigned long)xsec & (XSEC_PER_SEC - 1); |
190 | usec = SCALE_XSEC(usec, 1000000); | |
1da177e4 LT |
191 | |
192 | tv->tv_sec = sec; | |
193 | tv->tv_usec = usec; | |
194 | } | |
195 | ||
196 | void do_gettimeofday(struct timeval *tv) | |
197 | { | |
96c44507 PM |
198 | if (__USE_RTC()) { |
199 | /* do this the old way */ | |
200 | unsigned long flags, seq; | |
201 | unsigned int sec, nsec, usec, lost; | |
202 | ||
203 | do { | |
204 | seq = read_seqbegin_irqsave(&xtime_lock, flags); | |
205 | sec = xtime.tv_sec; | |
206 | nsec = xtime.tv_nsec + tb_ticks_since(tb_last_stamp); | |
207 | lost = jiffies - wall_jiffies; | |
208 | } while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); | |
209 | usec = nsec / 1000 + lost * (1000000 / HZ); | |
210 | while (usec >= 1000000) { | |
211 | usec -= 1000000; | |
212 | ++sec; | |
213 | } | |
214 | tv->tv_sec = sec; | |
215 | tv->tv_usec = usec; | |
216 | return; | |
217 | } | |
1da177e4 LT |
218 | __do_gettimeofday(tv, get_tb()); |
219 | } | |
220 | ||
221 | EXPORT_SYMBOL(do_gettimeofday); | |
222 | ||
223 | /* Synchronize xtime with do_gettimeofday */ | |
224 | ||
225 | static inline void timer_sync_xtime(unsigned long cur_tb) | |
226 | { | |
f2783c15 PM |
227 | #ifdef CONFIG_PPC64 |
228 | /* why do we do this? */ | |
1da177e4 LT |
229 | struct timeval my_tv; |
230 | ||
231 | __do_gettimeofday(&my_tv, cur_tb); | |
232 | ||
233 | if (xtime.tv_sec <= my_tv.tv_sec) { | |
234 | xtime.tv_sec = my_tv.tv_sec; | |
235 | xtime.tv_nsec = my_tv.tv_usec * 1000; | |
236 | } | |
f2783c15 | 237 | #endif |
1da177e4 LT |
238 | } |
239 | ||
240 | /* | |
f2783c15 PM |
241 | * There are two copies of tb_to_xs and stamp_xsec so that no |
242 | * lock is needed to access and use these values in | |
243 | * do_gettimeofday. We alternate the copies and as long as a | |
244 | * reasonable time elapses between changes, there will never | |
245 | * be inconsistent values. ntpd has a minimum of one minute | |
246 | * between updates. | |
1da177e4 | 247 | */ |
f2783c15 | 248 | static inline void update_gtod(u64 new_tb_stamp, u64 new_stamp_xsec, |
5d14a18d | 249 | u64 new_tb_to_xs) |
1da177e4 | 250 | { |
1da177e4 | 251 | unsigned temp_idx; |
f2783c15 | 252 | struct gettimeofday_vars *temp_varp; |
1da177e4 LT |
253 | |
254 | temp_idx = (do_gtod.var_idx == 0); | |
255 | temp_varp = &do_gtod.vars[temp_idx]; | |
256 | ||
f2783c15 PM |
257 | temp_varp->tb_to_xs = new_tb_to_xs; |
258 | temp_varp->tb_orig_stamp = new_tb_stamp; | |
1da177e4 | 259 | temp_varp->stamp_xsec = new_stamp_xsec; |
0d8d4d42 | 260 | smp_mb(); |
1da177e4 LT |
261 | do_gtod.varp = temp_varp; |
262 | do_gtod.var_idx = temp_idx; | |
263 | ||
f2783c15 PM |
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 | */ | |
a7f290da | 273 | ++(vdso_data->tb_update_count); |
0d8d4d42 | 274 | smp_wmb(); |
a7f290da BH |
275 | vdso_data->tb_orig_stamp = new_tb_stamp; |
276 | vdso_data->stamp_xsec = new_stamp_xsec; | |
277 | vdso_data->tb_to_xs = new_tb_to_xs; | |
278 | vdso_data->wtom_clock_sec = wall_to_monotonic.tv_sec; | |
279 | vdso_data->wtom_clock_nsec = wall_to_monotonic.tv_nsec; | |
0d8d4d42 | 280 | smp_wmb(); |
a7f290da | 281 | ++(vdso_data->tb_update_count); |
f2783c15 PM |
282 | } |
283 | ||
284 | /* | |
285 | * When the timebase - tb_orig_stamp gets too big, we do a manipulation | |
286 | * between tb_orig_stamp and stamp_xsec. The goal here is to keep the | |
287 | * difference tb - tb_orig_stamp small enough to always fit inside a | |
288 | * 32 bits number. This is a requirement of our fast 32 bits userland | |
289 | * implementation in the vdso. If we "miss" a call to this function | |
290 | * (interrupt latency, CPU locked in a spinlock, ...) and we end up | |
291 | * with a too big difference, then the vdso will fallback to calling | |
292 | * the syscall | |
293 | */ | |
294 | static __inline__ void timer_recalc_offset(u64 cur_tb) | |
295 | { | |
296 | unsigned long offset; | |
297 | u64 new_stamp_xsec; | |
298 | ||
96c44507 PM |
299 | if (__USE_RTC()) |
300 | return; | |
f2783c15 PM |
301 | offset = cur_tb - do_gtod.varp->tb_orig_stamp; |
302 | if ((offset & 0x80000000u) == 0) | |
303 | return; | |
304 | new_stamp_xsec = do_gtod.varp->stamp_xsec | |
305 | + mulhdu(offset, do_gtod.varp->tb_to_xs); | |
306 | update_gtod(cur_tb, new_stamp_xsec, do_gtod.varp->tb_to_xs); | |
1da177e4 LT |
307 | } |
308 | ||
309 | #ifdef CONFIG_SMP | |
310 | unsigned long profile_pc(struct pt_regs *regs) | |
311 | { | |
312 | unsigned long pc = instruction_pointer(regs); | |
313 | ||
314 | if (in_lock_functions(pc)) | |
315 | return regs->link; | |
316 | ||
317 | return pc; | |
318 | } | |
319 | EXPORT_SYMBOL(profile_pc); | |
320 | #endif | |
321 | ||
322 | #ifdef CONFIG_PPC_ISERIES | |
323 | ||
324 | /* | |
325 | * This function recalibrates the timebase based on the 49-bit time-of-day | |
326 | * value in the Titan chip. The Titan is much more accurate than the value | |
327 | * returned by the service processor for the timebase frequency. | |
328 | */ | |
329 | ||
330 | static void iSeries_tb_recal(void) | |
331 | { | |
332 | struct div_result divres; | |
333 | unsigned long titan, tb; | |
334 | tb = get_tb(); | |
335 | titan = HvCallXm_loadTod(); | |
336 | if ( iSeries_recal_titan ) { | |
337 | unsigned long tb_ticks = tb - iSeries_recal_tb; | |
338 | unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12; | |
339 | unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec; | |
340 | unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ; | |
341 | long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy; | |
342 | char sign = '+'; | |
343 | /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */ | |
344 | new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ; | |
345 | ||
346 | if ( tick_diff < 0 ) { | |
347 | tick_diff = -tick_diff; | |
348 | sign = '-'; | |
349 | } | |
350 | if ( tick_diff ) { | |
351 | if ( tick_diff < tb_ticks_per_jiffy/25 ) { | |
352 | printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n", | |
353 | new_tb_ticks_per_jiffy, sign, tick_diff ); | |
354 | tb_ticks_per_jiffy = new_tb_ticks_per_jiffy; | |
355 | tb_ticks_per_sec = new_tb_ticks_per_sec; | |
356 | div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres ); | |
357 | do_gtod.tb_ticks_per_sec = tb_ticks_per_sec; | |
358 | tb_to_xs = divres.result_low; | |
359 | do_gtod.varp->tb_to_xs = tb_to_xs; | |
a7f290da BH |
360 | vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; |
361 | vdso_data->tb_to_xs = tb_to_xs; | |
1da177e4 LT |
362 | } |
363 | else { | |
364 | printk( "Titan recalibrate: FAILED (difference > 4 percent)\n" | |
365 | " new tb_ticks_per_jiffy = %lu\n" | |
366 | " old tb_ticks_per_jiffy = %lu\n", | |
367 | new_tb_ticks_per_jiffy, tb_ticks_per_jiffy ); | |
368 | } | |
369 | } | |
370 | } | |
371 | iSeries_recal_titan = titan; | |
372 | iSeries_recal_tb = tb; | |
373 | } | |
374 | #endif | |
375 | ||
376 | /* | |
377 | * For iSeries shared processors, we have to let the hypervisor | |
378 | * set the hardware decrementer. We set a virtual decrementer | |
379 | * in the lppaca and call the hypervisor if the virtual | |
380 | * decrementer is less than the current value in the hardware | |
381 | * decrementer. (almost always the new decrementer value will | |
382 | * be greater than the current hardware decementer so the hypervisor | |
383 | * call will not be needed) | |
384 | */ | |
385 | ||
1da177e4 LT |
386 | /* |
387 | * timer_interrupt - gets called when the decrementer overflows, | |
388 | * with interrupts disabled. | |
389 | */ | |
c7aeffc4 | 390 | void timer_interrupt(struct pt_regs * regs) |
1da177e4 LT |
391 | { |
392 | int next_dec; | |
f2783c15 PM |
393 | int cpu = smp_processor_id(); |
394 | unsigned long ticks; | |
395 | ||
396 | #ifdef CONFIG_PPC32 | |
397 | if (atomic_read(&ppc_n_lost_interrupts) != 0) | |
398 | do_IRQ(regs); | |
399 | #endif | |
1da177e4 LT |
400 | |
401 | irq_enter(); | |
402 | ||
1da177e4 | 403 | profile_tick(CPU_PROFILING, regs); |
1da177e4 | 404 | |
f2783c15 PM |
405 | #ifdef CONFIG_PPC_ISERIES |
406 | get_paca()->lppaca.int_dword.fields.decr_int = 0; | |
407 | #endif | |
408 | ||
409 | while ((ticks = tb_ticks_since(per_cpu(last_jiffy, cpu))) | |
410 | >= tb_ticks_per_jiffy) { | |
411 | /* Update last_jiffy */ | |
412 | per_cpu(last_jiffy, cpu) += tb_ticks_per_jiffy; | |
413 | /* Handle RTCL overflow on 601 */ | |
414 | if (__USE_RTC() && per_cpu(last_jiffy, cpu) >= 1000000000) | |
415 | per_cpu(last_jiffy, cpu) -= 1000000000; | |
1da177e4 | 416 | |
1da177e4 LT |
417 | /* |
418 | * We cannot disable the decrementer, so in the period | |
419 | * between this cpu's being marked offline in cpu_online_map | |
420 | * and calling stop-self, it is taking timer interrupts. | |
421 | * Avoid calling into the scheduler rebalancing code if this | |
422 | * is the case. | |
423 | */ | |
424 | if (!cpu_is_offline(cpu)) | |
425 | update_process_times(user_mode(regs)); | |
f2783c15 | 426 | |
1da177e4 LT |
427 | /* |
428 | * No need to check whether cpu is offline here; boot_cpuid | |
429 | * should have been fixed up by now. | |
430 | */ | |
f2783c15 PM |
431 | if (cpu != boot_cpuid) |
432 | continue; | |
433 | ||
434 | write_seqlock(&xtime_lock); | |
96c44507 PM |
435 | tb_last_jiffy += tb_ticks_per_jiffy; |
436 | tb_last_stamp = per_cpu(last_jiffy, cpu); | |
437 | timer_recalc_offset(tb_last_jiffy); | |
f2783c15 | 438 | do_timer(regs); |
96c44507 | 439 | timer_sync_xtime(tb_last_jiffy); |
f2783c15 PM |
440 | timer_check_rtc(); |
441 | write_sequnlock(&xtime_lock); | |
442 | if (adjusting_time && (time_adjust == 0)) | |
443 | ppc_adjtimex(); | |
1da177e4 LT |
444 | } |
445 | ||
f2783c15 | 446 | next_dec = tb_ticks_per_jiffy - ticks; |
1da177e4 LT |
447 | set_dec(next_dec); |
448 | ||
449 | #ifdef CONFIG_PPC_ISERIES | |
937b31b1 | 450 | if (hvlpevent_is_pending()) |
74889802 | 451 | process_hvlpevents(regs); |
1da177e4 LT |
452 | #endif |
453 | ||
f2783c15 | 454 | #ifdef CONFIG_PPC64 |
8d15a3e5 | 455 | /* collect purr register values often, for accurate calculations */ |
1ababe11 | 456 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) { |
1da177e4 LT |
457 | struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array); |
458 | cu->current_tb = mfspr(SPRN_PURR); | |
459 | } | |
f2783c15 | 460 | #endif |
1da177e4 LT |
461 | |
462 | irq_exit(); | |
1da177e4 LT |
463 | } |
464 | ||
f2783c15 PM |
465 | void wakeup_decrementer(void) |
466 | { | |
467 | int i; | |
468 | ||
469 | set_dec(tb_ticks_per_jiffy); | |
470 | /* | |
471 | * We don't expect this to be called on a machine with a 601, | |
472 | * so using get_tbl is fine. | |
473 | */ | |
96c44507 | 474 | tb_last_stamp = tb_last_jiffy = get_tb(); |
f2783c15 PM |
475 | for_each_cpu(i) |
476 | per_cpu(last_jiffy, i) = tb_last_stamp; | |
477 | } | |
478 | ||
a5b518ed | 479 | #ifdef CONFIG_SMP |
f2783c15 PM |
480 | void __init smp_space_timers(unsigned int max_cpus) |
481 | { | |
482 | int i; | |
483 | unsigned long offset = tb_ticks_per_jiffy / max_cpus; | |
484 | unsigned long previous_tb = per_cpu(last_jiffy, boot_cpuid); | |
485 | ||
cbe62e2b PM |
486 | /* make sure tb > per_cpu(last_jiffy, cpu) for all cpus always */ |
487 | previous_tb -= tb_ticks_per_jiffy; | |
f2783c15 PM |
488 | for_each_cpu(i) { |
489 | if (i != boot_cpuid) { | |
490 | previous_tb += offset; | |
491 | per_cpu(last_jiffy, i) = previous_tb; | |
492 | } | |
493 | } | |
494 | } | |
495 | #endif | |
496 | ||
1da177e4 LT |
497 | /* |
498 | * Scheduler clock - returns current time in nanosec units. | |
499 | * | |
500 | * Note: mulhdu(a, b) (multiply high double unsigned) returns | |
501 | * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b | |
502 | * are 64-bit unsigned numbers. | |
503 | */ | |
504 | unsigned long long sched_clock(void) | |
505 | { | |
96c44507 PM |
506 | if (__USE_RTC()) |
507 | return get_rtc(); | |
1da177e4 LT |
508 | return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift; |
509 | } | |
510 | ||
511 | int do_settimeofday(struct timespec *tv) | |
512 | { | |
513 | time_t wtm_sec, new_sec = tv->tv_sec; | |
514 | long wtm_nsec, new_nsec = tv->tv_nsec; | |
515 | unsigned long flags; | |
1da177e4 | 516 | long int tb_delta; |
5f6b5b97 | 517 | u64 new_xsec, tb_delta_xs; |
1da177e4 LT |
518 | |
519 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | |
520 | return -EINVAL; | |
521 | ||
522 | write_seqlock_irqsave(&xtime_lock, flags); | |
f2783c15 PM |
523 | |
524 | /* | |
525 | * Updating the RTC is not the job of this code. If the time is | |
526 | * stepped under NTP, the RTC will be updated after STA_UNSYNC | |
527 | * is cleared. Tools like clock/hwclock either copy the RTC | |
1da177e4 LT |
528 | * to the system time, in which case there is no point in writing |
529 | * to the RTC again, or write to the RTC but then they don't call | |
530 | * settimeofday to perform this operation. | |
531 | */ | |
532 | #ifdef CONFIG_PPC_ISERIES | |
f2783c15 | 533 | if (first_settimeofday) { |
1da177e4 LT |
534 | iSeries_tb_recal(); |
535 | first_settimeofday = 0; | |
536 | } | |
537 | #endif | |
538 | tb_delta = tb_ticks_since(tb_last_stamp); | |
539 | tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy; | |
5f6b5b97 | 540 | tb_delta_xs = mulhdu(tb_delta, do_gtod.varp->tb_to_xs); |
1da177e4 LT |
541 | |
542 | wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec); | |
543 | wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec); | |
544 | ||
545 | set_normalized_timespec(&xtime, new_sec, new_nsec); | |
546 | set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); | |
547 | ||
548 | /* In case of a large backwards jump in time with NTP, we want the | |
549 | * clock to be updated as soon as the PLL is again in lock. | |
550 | */ | |
551 | last_rtc_update = new_sec - 658; | |
552 | ||
b149ee22 | 553 | ntp_clear(); |
1da177e4 | 554 | |
5f6b5b97 PM |
555 | new_xsec = 0; |
556 | if (new_nsec != 0) { | |
557 | new_xsec = (u64)new_nsec * XSEC_PER_SEC; | |
558 | do_div(new_xsec, NSEC_PER_SEC); | |
559 | } | |
560 | new_xsec += (u64)new_sec * XSEC_PER_SEC - tb_delta_xs; | |
96c44507 | 561 | update_gtod(tb_last_jiffy, new_xsec, do_gtod.varp->tb_to_xs); |
1da177e4 | 562 | |
a7f290da BH |
563 | vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; |
564 | vdso_data->tz_dsttime = sys_tz.tz_dsttime; | |
1da177e4 LT |
565 | |
566 | write_sequnlock_irqrestore(&xtime_lock, flags); | |
567 | clock_was_set(); | |
568 | return 0; | |
569 | } | |
570 | ||
571 | EXPORT_SYMBOL(do_settimeofday); | |
572 | ||
10f7e7c1 AB |
573 | void __init generic_calibrate_decr(void) |
574 | { | |
575 | struct device_node *cpu; | |
10f7e7c1 AB |
576 | unsigned int *fp; |
577 | int node_found; | |
578 | ||
579 | /* | |
580 | * The cpu node should have a timebase-frequency property | |
581 | * to tell us the rate at which the decrementer counts. | |
582 | */ | |
583 | cpu = of_find_node_by_type(NULL, "cpu"); | |
584 | ||
585 | ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ | |
586 | node_found = 0; | |
587 | if (cpu != 0) { | |
588 | fp = (unsigned int *)get_property(cpu, "timebase-frequency", | |
589 | NULL); | |
590 | if (fp != 0) { | |
591 | node_found = 1; | |
592 | ppc_tb_freq = *fp; | |
593 | } | |
594 | } | |
595 | if (!node_found) | |
596 | printk(KERN_ERR "WARNING: Estimating decrementer frequency " | |
597 | "(not found)\n"); | |
598 | ||
599 | ppc_proc_freq = DEFAULT_PROC_FREQ; | |
600 | node_found = 0; | |
601 | if (cpu != 0) { | |
602 | fp = (unsigned int *)get_property(cpu, "clock-frequency", | |
603 | NULL); | |
604 | if (fp != 0) { | |
605 | node_found = 1; | |
606 | ppc_proc_freq = *fp; | |
607 | } | |
608 | } | |
0fd6f717 KG |
609 | #ifdef CONFIG_BOOKE |
610 | /* Set the time base to zero */ | |
611 | mtspr(SPRN_TBWL, 0); | |
612 | mtspr(SPRN_TBWU, 0); | |
613 | ||
614 | /* Clear any pending timer interrupts */ | |
615 | mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); | |
616 | ||
617 | /* Enable decrementer interrupt */ | |
618 | mtspr(SPRN_TCR, TCR_DIE); | |
619 | #endif | |
10f7e7c1 AB |
620 | if (!node_found) |
621 | printk(KERN_ERR "WARNING: Estimating processor frequency " | |
622 | "(not found)\n"); | |
623 | ||
624 | of_node_put(cpu); | |
10f7e7c1 | 625 | } |
10f7e7c1 | 626 | |
f2783c15 PM |
627 | unsigned long get_boot_time(void) |
628 | { | |
629 | struct rtc_time tm; | |
630 | ||
631 | if (ppc_md.get_boot_time) | |
632 | return ppc_md.get_boot_time(); | |
633 | if (!ppc_md.get_rtc_time) | |
634 | return 0; | |
635 | ppc_md.get_rtc_time(&tm); | |
636 | return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, | |
637 | tm.tm_hour, tm.tm_min, tm.tm_sec); | |
638 | } | |
639 | ||
640 | /* This function is only called on the boot processor */ | |
1da177e4 LT |
641 | void __init time_init(void) |
642 | { | |
1da177e4 | 643 | unsigned long flags; |
f2783c15 | 644 | unsigned long tm = 0; |
1da177e4 | 645 | struct div_result res; |
f2783c15 PM |
646 | u64 scale; |
647 | unsigned shift; | |
648 | ||
649 | if (ppc_md.time_init != NULL) | |
650 | timezone_offset = ppc_md.time_init(); | |
1da177e4 | 651 | |
96c44507 PM |
652 | if (__USE_RTC()) { |
653 | /* 601 processor: dec counts down by 128 every 128ns */ | |
654 | ppc_tb_freq = 1000000000; | |
655 | tb_last_stamp = get_rtcl(); | |
656 | tb_last_jiffy = tb_last_stamp; | |
657 | } else { | |
658 | /* Normal PowerPC with timebase register */ | |
659 | ppc_md.calibrate_decr(); | |
660 | printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n", | |
661 | ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); | |
662 | printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n", | |
663 | ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); | |
664 | tb_last_stamp = tb_last_jiffy = get_tb(); | |
665 | } | |
374e99d4 PM |
666 | |
667 | tb_ticks_per_jiffy = ppc_tb_freq / HZ; | |
668 | tb_ticks_per_sec = tb_ticks_per_jiffy * HZ; | |
669 | tb_ticks_per_usec = ppc_tb_freq / 1000000; | |
670 | tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000); | |
671 | div128_by_32(1024*1024, 0, tb_ticks_per_sec, &res); | |
672 | tb_to_xs = res.result_low; | |
673 | ||
f2783c15 PM |
674 | #ifdef CONFIG_PPC64 |
675 | get_paca()->default_decr = tb_ticks_per_jiffy; | |
676 | #endif | |
677 | ||
1da177e4 LT |
678 | /* |
679 | * Compute scale factor for sched_clock. | |
680 | * The calibrate_decr() function has set tb_ticks_per_sec, | |
681 | * which is the timebase frequency. | |
682 | * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret | |
683 | * the 128-bit result as a 64.64 fixed-point number. | |
684 | * We then shift that number right until it is less than 1.0, | |
685 | * giving us the scale factor and shift count to use in | |
686 | * sched_clock(). | |
687 | */ | |
688 | div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); | |
689 | scale = res.result_low; | |
690 | for (shift = 0; res.result_high != 0; ++shift) { | |
691 | scale = (scale >> 1) | (res.result_high << 63); | |
692 | res.result_high >>= 1; | |
693 | } | |
694 | tb_to_ns_scale = scale; | |
695 | tb_to_ns_shift = shift; | |
696 | ||
697 | #ifdef CONFIG_PPC_ISERIES | |
698 | if (!piranha_simulator) | |
699 | #endif | |
f2783c15 | 700 | tm = get_boot_time(); |
1da177e4 LT |
701 | |
702 | write_seqlock_irqsave(&xtime_lock, flags); | |
f2783c15 PM |
703 | xtime.tv_sec = tm; |
704 | xtime.tv_nsec = 0; | |
1da177e4 LT |
705 | do_gtod.varp = &do_gtod.vars[0]; |
706 | do_gtod.var_idx = 0; | |
96c44507 | 707 | do_gtod.varp->tb_orig_stamp = tb_last_jiffy; |
f2783c15 PM |
708 | __get_cpu_var(last_jiffy) = tb_last_stamp; |
709 | do_gtod.varp->stamp_xsec = (u64) xtime.tv_sec * XSEC_PER_SEC; | |
1da177e4 LT |
710 | do_gtod.tb_ticks_per_sec = tb_ticks_per_sec; |
711 | do_gtod.varp->tb_to_xs = tb_to_xs; | |
712 | do_gtod.tb_to_us = tb_to_us; | |
a7f290da BH |
713 | |
714 | vdso_data->tb_orig_stamp = tb_last_jiffy; | |
715 | vdso_data->tb_update_count = 0; | |
716 | vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; | |
717 | vdso_data->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC; | |
718 | vdso_data->tb_to_xs = tb_to_xs; | |
1da177e4 LT |
719 | |
720 | time_freq = 0; | |
721 | ||
f2783c15 PM |
722 | /* 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 | ||
1da177e4 LT |
729 | 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 | ||
749 | void ppc_adjtimex(void) | |
750 | { | |
f2783c15 PM |
751 | #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; | |
1da177e4 LT |
754 | unsigned long tb_ticks_per_sec_delta; |
755 | long delta_freq, ltemp; | |
756 | struct div_result divres; | |
757 | unsigned long flags; | |
1da177e4 LT |
758 | long singleshot_ppm = 0; |
759 | ||
f2783c15 PM |
760 | /* |
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 | */ | |
1da177e4 LT |
767 | if ( time_offset < 0 ) { |
768 | ltemp = -time_offset; | |
769 | ltemp <<= SHIFT_USEC - SHIFT_UPDATE; | |
770 | ltemp >>= SHIFT_KG + time_constant; | |
771 | ltemp = -ltemp; | |
f2783c15 | 772 | } else { |
1da177e4 LT |
773 | 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 | ||
f2783c15 PM |
789 | /* |
790 | * Compute parts per million frequency adjustment | |
791 | * to match time_adjust | |
792 | */ | |
1da177e4 LT |
793 | 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; | |
f2783c15 | 800 | /* Use SHIFT_USEC to get it into the same units as time_freq */ |
1da177e4 LT |
801 | 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 | ||
f2783c15 PM |
816 | /* |
817 | * Compute a new value for tb_ticks_per_sec based on | |
818 | * the frequency adjustment | |
819 | */ | |
1da177e4 LT |
820 | 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 | |
f2783c15 PM |
834 | |
835 | /* | |
836 | * 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. | |
842 | */ | |
1da177e4 LT |
843 | write_seqlock_irqsave( &xtime_lock, flags ); |
844 | tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp; | |
f2783c15 | 845 | div128_by_32(1024*1024, 0, new_tb_ticks_per_sec, &divres); |
1da177e4 | 846 | new_tb_to_xs = divres.result_low; |
f2783c15 | 847 | new_xsec = mulhdu(tb_ticks, new_tb_to_xs); |
1da177e4 | 848 | |
f2783c15 | 849 | old_xsec = mulhdu(tb_ticks, do_gtod.varp->tb_to_xs); |
1da177e4 LT |
850 | new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec; |
851 | ||
f2783c15 | 852 | update_gtod(do_gtod.varp->tb_orig_stamp, new_stamp_xsec, new_tb_to_xs); |
1da177e4 LT |
853 | |
854 | write_sequnlock_irqrestore( &xtime_lock, flags ); | |
f2783c15 | 855 | #endif /* CONFIG_PPC64 */ |
1da177e4 LT |
856 | } |
857 | ||
858 | ||
1da177e4 LT |
859 | #define FEBRUARY 2 |
860 | #define STARTOFTIME 1970 | |
861 | #define SECDAY 86400L | |
862 | #define SECYR (SECDAY * 365) | |
f2783c15 PM |
863 | #define leapyear(year) ((year) % 4 == 0 && \ |
864 | ((year) % 100 != 0 || (year) % 400 == 0)) | |
1da177e4 LT |
865 | #define days_in_year(a) (leapyear(a) ? 366 : 365) |
866 | #define days_in_month(a) (month_days[(a) - 1]) | |
867 | ||
868 | static 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 | */ | |
875 | void 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 | ||
f2783c15 | 882 | lastYear = tm->tm_year - 1; |
1da177e4 LT |
883 | |
884 | /* | |
885 | * Number of leap corrections to apply up to end of last year | |
886 | */ | |
f2783c15 | 887 | leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400; |
1da177e4 LT |
888 | |
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 | * | |
f2783c15 | 893 | * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was |
1da177e4 | 894 | */ |
f2783c15 | 895 | day = tm->tm_mon > 2 && leapyear(tm->tm_year); |
1da177e4 LT |
896 | |
897 | day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + | |
898 | tm->tm_mday; | |
899 | ||
f2783c15 | 900 | tm->tm_wday = day % 7; |
1da177e4 LT |
901 | } |
902 | ||
903 | void 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 | */ | |
f2783c15 PM |
946 | unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) |
947 | { | |
1da177e4 LT |
948 | 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) { | |
f2783c15 PM |
954 | if (mulhwu(inscale, mlt|tmp) < outscale) |
955 | mlt |= tmp; | |
1da177e4 LT |
956 | } |
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 | ||
f2783c15 PM |
965 | err = inscale * (mlt+1); |
966 | if (err <= inscale/2) | |
967 | mlt++; | |
1da177e4 | 968 | return mlt; |
f2783c15 | 969 | } |
1da177e4 LT |
970 | |
971 | /* | |
972 | * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit | |
973 | * result. | |
974 | */ | |
f2783c15 PM |
975 | void div128_by_32(u64 dividend_high, u64 dividend_low, |
976 | unsigned divisor, struct div_result *dr) | |
1da177e4 | 977 | { |
f2783c15 PM |
978 | unsigned long a, b, c, d; |
979 | unsigned long w, x, y, z; | |
980 | u64 ra, rb, rc; | |
1da177e4 LT |
981 | |
982 | a = dividend_high >> 32; | |
983 | b = dividend_high & 0xffffffff; | |
984 | c = dividend_low >> 32; | |
985 | d = dividend_low & 0xffffffff; | |
986 | ||
f2783c15 PM |
987 | w = a / divisor; |
988 | ra = ((u64)(a - (w * divisor)) << 32) + b; | |
989 | ||
f2783c15 PM |
990 | rb = ((u64) do_div(ra, divisor) << 32) + c; |
991 | x = ra; | |
1da177e4 | 992 | |
f2783c15 PM |
993 | rc = ((u64) do_div(rb, divisor) << 32) + d; |
994 | y = rb; | |
995 | ||
996 | do_div(rc, divisor); | |
997 | z = rc; | |
1da177e4 | 998 | |
f2783c15 PM |
999 | dr->result_high = ((u64)w << 32) + x; |
1000 | dr->result_low = ((u64)y << 32) + z; | |
1da177e4 LT |
1001 | |
1002 | } |