[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / arch / ppc / kernel / time.c
blob735866559199cb39a2a19c59dd38295e4715c082
1 /*
2 * Common time routines among all ppc machines.
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).
8 * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
9 * to make clock more stable (2.4.0-test5). The only thing
10 * that this code assumes is that the timebases have been synchronized
11 * by firmware on SMP and are never stopped (never do sleep
12 * on SMP then, nap and doze are OK).
14 * TODO (not necessarily in this file):
15 * - improve precision and reproducibility of timebase frequency
16 * measurement at boot time.
17 * - get rid of xtime_lock for gettimeofday (generic kernel problem
18 * to be implemented on all architectures for SMP scalability and
19 * eventually implementing gettimeofday without entering the kernel).
20 * - put all time/clock related variables in a single structure
21 * to minimize number of cache lines touched by gettimeofday()
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.
27 * The following comment is partially obsolete (at least the long wait
28 * is no more a valid reason):
29 * Since the MPC8xx has a programmable interrupt timer, I decided to
30 * use that rather than the decrementer. Two reasons: 1.) the clock
31 * frequency is low, causing 2.) a long wait in the timer interrupt
32 * while ((d = get_dec()) == dval)
33 * loop. The MPC8xx can be driven from a variety of input clocks,
34 * so a number of assumptions have been made here because the kernel
35 * parameter HZ is a constant. We assume (correctly, today :-) that
36 * the MPC8xx on the MBX board is driven from a 32.768 kHz crystal.
37 * This is then divided by 4, providing a 8192 Hz clock into the PIT.
38 * Since it is not possible to get a nice 100 Hz clock out of this, without
39 * creating a software PLL, I have set HZ to 128. -- Dan
41 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
42 * "A Kernel Model for Precision Timekeeping" by Dave Mills
45 #include <linux/config.h>
46 #include <linux/errno.h>
47 #include <linux/sched.h>
48 #include <linux/kernel.h>
49 #include <linux/param.h>
50 #include <linux/string.h>
51 #include <linux/mm.h>
52 #include <linux/module.h>
53 #include <linux/interrupt.h>
54 #include <linux/timex.h>
55 #include <linux/kernel_stat.h>
56 #include <linux/mc146818rtc.h>
57 #include <linux/time.h>
58 #include <linux/init.h>
59 #include <linux/profile.h>
61 #include <asm/segment.h>
62 #include <asm/io.h>
63 #include <asm/nvram.h>
64 #include <asm/cache.h>
65 #include <asm/8xx_immap.h>
66 #include <asm/machdep.h>
68 #include <asm/time.h>
70 /* XXX false sharing with below? */
71 u64 jiffies_64 = INITIAL_JIFFIES;
73 EXPORT_SYMBOL(jiffies_64);
75 unsigned long disarm_decr[NR_CPUS];
77 extern struct timezone sys_tz;
79 /* keep track of when we need to update the rtc */
80 time_t last_rtc_update;
82 /* The decrementer counts down by 128 every 128ns on a 601. */
83 #define DECREMENTER_COUNT_601 (1000000000 / HZ)
85 unsigned tb_ticks_per_jiffy;
86 unsigned tb_to_us;
87 unsigned tb_last_stamp;
88 unsigned long tb_to_ns_scale;
90 extern unsigned long wall_jiffies;
92 DEFINE_SPINLOCK(rtc_lock);
94 EXPORT_SYMBOL(rtc_lock);
96 /* Timer interrupt helper function */
97 static inline int tb_delta(unsigned *jiffy_stamp) {
98 int delta;
99 if (__USE_RTC()) {
100 delta = get_rtcl();
101 if (delta < *jiffy_stamp) *jiffy_stamp -= 1000000000;
102 delta -= *jiffy_stamp;
103 } else {
104 delta = get_tbl() - *jiffy_stamp;
106 return delta;
109 #ifdef CONFIG_SMP
110 unsigned long profile_pc(struct pt_regs *regs)
112 unsigned long pc = instruction_pointer(regs);
114 if (in_lock_functions(pc))
115 return regs->link;
117 return pc;
119 EXPORT_SYMBOL(profile_pc);
120 #endif
123 * timer_interrupt - gets called when the decrementer overflows,
124 * with interrupts disabled.
125 * We set it up to overflow again in 1/HZ seconds.
127 void timer_interrupt(struct pt_regs * regs)
129 int next_dec;
130 unsigned long cpu = smp_processor_id();
131 unsigned jiffy_stamp = last_jiffy_stamp(cpu);
132 extern void do_IRQ(struct pt_regs *);
134 if (atomic_read(&ppc_n_lost_interrupts) != 0)
135 do_IRQ(regs);
137 irq_enter();
139 while ((next_dec = tb_ticks_per_jiffy - tb_delta(&jiffy_stamp)) <= 0) {
140 jiffy_stamp += tb_ticks_per_jiffy;
142 profile_tick(CPU_PROFILING, regs);
143 update_process_times(user_mode(regs));
145 if (smp_processor_id())
146 continue;
148 /* We are in an interrupt, no need to save/restore flags */
149 write_seqlock(&xtime_lock);
150 tb_last_stamp = jiffy_stamp;
151 do_timer(regs);
154 * update the rtc when needed, this should be performed on the
155 * right fraction of a second. Half or full second ?
156 * Full second works on mk48t59 clocks, others need testing.
157 * Note that this update is basically only used through
158 * the adjtimex system calls. Setting the HW clock in
159 * any other way is a /dev/rtc and userland business.
160 * This is still wrong by -0.5/+1.5 jiffies because of the
161 * timer interrupt resolution and possible delay, but here we
162 * hit a quantization limit which can only be solved by higher
163 * resolution timers and decoupling time management from timer
164 * interrupts. This is also wrong on the clocks
165 * which require being written at the half second boundary.
166 * We should have an rtc call that only sets the minutes and
167 * seconds like on Intel to avoid problems with non UTC clocks.
169 if ( ppc_md.set_rtc_time && (time_status & STA_UNSYNC) == 0 &&
170 xtime.tv_sec - last_rtc_update >= 659 &&
171 abs((xtime.tv_nsec / 1000) - (1000000-1000000/HZ)) < 500000/HZ &&
172 jiffies - wall_jiffies == 1) {
173 if (ppc_md.set_rtc_time(xtime.tv_sec+1 + time_offset) == 0)
174 last_rtc_update = xtime.tv_sec+1;
175 else
176 /* Try again one minute later */
177 last_rtc_update += 60;
179 write_sequnlock(&xtime_lock);
181 if ( !disarm_decr[smp_processor_id()] )
182 set_dec(next_dec);
183 last_jiffy_stamp(cpu) = jiffy_stamp;
185 if (ppc_md.heartbeat && !ppc_md.heartbeat_count--)
186 ppc_md.heartbeat();
188 irq_exit();
192 * This version of gettimeofday has microsecond resolution.
194 void do_gettimeofday(struct timeval *tv)
196 unsigned long flags;
197 unsigned long seq;
198 unsigned delta, lost_ticks, usec, sec;
200 do {
201 seq = read_seqbegin_irqsave(&xtime_lock, flags);
202 sec = xtime.tv_sec;
203 usec = (xtime.tv_nsec / 1000);
204 delta = tb_ticks_since(tb_last_stamp);
205 #ifdef CONFIG_SMP
206 /* As long as timebases are not in sync, gettimeofday can only
207 * have jiffy resolution on SMP.
209 if (!smp_tb_synchronized)
210 delta = 0;
211 #endif /* CONFIG_SMP */
212 lost_ticks = jiffies - wall_jiffies;
213 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
215 usec += mulhwu(tb_to_us, tb_ticks_per_jiffy * lost_ticks + delta);
216 while (usec >= 1000000) {
217 sec++;
218 usec -= 1000000;
220 tv->tv_sec = sec;
221 tv->tv_usec = usec;
224 EXPORT_SYMBOL(do_gettimeofday);
226 int do_settimeofday(struct timespec *tv)
228 time_t wtm_sec, new_sec = tv->tv_sec;
229 long wtm_nsec, new_nsec = tv->tv_nsec;
230 unsigned long flags;
231 int tb_delta;
233 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
234 return -EINVAL;
236 write_seqlock_irqsave(&xtime_lock, flags);
237 /* Updating the RTC is not the job of this code. If the time is
238 * stepped under NTP, the RTC will be update after STA_UNSYNC
239 * is cleared. Tool like clock/hwclock either copy the RTC
240 * to the system time, in which case there is no point in writing
241 * to the RTC again, or write to the RTC but then they don't call
242 * settimeofday to perform this operation. Note also that
243 * we don't touch the decrementer since:
244 * a) it would lose timer interrupt synchronization on SMP
245 * (if it is working one day)
246 * b) it could make one jiffy spuriously shorter or longer
247 * which would introduce another source of uncertainty potentially
248 * harmful to relatively short timers.
251 /* This works perfectly on SMP only if the tb are in sync but
252 * guarantees an error < 1 jiffy even if they are off by eons,
253 * still reasonable when gettimeofday resolution is 1 jiffy.
255 tb_delta = tb_ticks_since(last_jiffy_stamp(smp_processor_id()));
256 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
258 new_nsec -= 1000 * mulhwu(tb_to_us, tb_delta);
260 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
261 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
263 set_normalized_timespec(&xtime, new_sec, new_nsec);
264 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
266 /* In case of a large backwards jump in time with NTP, we want the
267 * clock to be updated as soon as the PLL is again in lock.
269 last_rtc_update = new_sec - 658;
271 time_adjust = 0; /* stop active adjtime() */
272 time_status |= STA_UNSYNC;
273 time_maxerror = NTP_PHASE_LIMIT;
274 time_esterror = NTP_PHASE_LIMIT;
275 write_sequnlock_irqrestore(&xtime_lock, flags);
276 clock_was_set();
277 return 0;
280 EXPORT_SYMBOL(do_settimeofday);
282 /* This function is only called on the boot processor */
283 void __init time_init(void)
285 time_t sec, old_sec;
286 unsigned old_stamp, stamp, elapsed;
288 if (ppc_md.time_init != NULL)
289 time_offset = ppc_md.time_init();
291 if (__USE_RTC()) {
292 /* 601 processor: dec counts down by 128 every 128ns */
293 tb_ticks_per_jiffy = DECREMENTER_COUNT_601;
294 /* mulhwu_scale_factor(1000000000, 1000000) is 0x418937 */
295 tb_to_us = 0x418937;
296 } else {
297 ppc_md.calibrate_decr();
298 tb_to_ns_scale = mulhwu(tb_to_us, 1000 << 10);
301 /* Now that the decrementer is calibrated, it can be used in case the
302 * clock is stuck, but the fact that we have to handle the 601
303 * makes things more complex. Repeatedly read the RTC until the
304 * next second boundary to try to achieve some precision. If there
305 * is no RTC, we still need to set tb_last_stamp and
306 * last_jiffy_stamp(cpu 0) to the current stamp.
308 stamp = get_native_tbl();
309 if (ppc_md.get_rtc_time) {
310 sec = ppc_md.get_rtc_time();
311 elapsed = 0;
312 do {
313 old_stamp = stamp;
314 old_sec = sec;
315 stamp = get_native_tbl();
316 if (__USE_RTC() && stamp < old_stamp)
317 old_stamp -= 1000000000;
318 elapsed += stamp - old_stamp;
319 sec = ppc_md.get_rtc_time();
320 } while ( sec == old_sec && elapsed < 2*HZ*tb_ticks_per_jiffy);
321 if (sec==old_sec)
322 printk("Warning: real time clock seems stuck!\n");
323 xtime.tv_sec = sec;
324 xtime.tv_nsec = 0;
325 /* No update now, we just read the time from the RTC ! */
326 last_rtc_update = xtime.tv_sec;
328 last_jiffy_stamp(0) = tb_last_stamp = stamp;
330 /* Not exact, but the timer interrupt takes care of this */
331 set_dec(tb_ticks_per_jiffy);
333 /* If platform provided a timezone (pmac), we correct the time */
334 if (time_offset) {
335 sys_tz.tz_minuteswest = -time_offset / 60;
336 sys_tz.tz_dsttime = 0;
337 xtime.tv_sec -= time_offset;
339 set_normalized_timespec(&wall_to_monotonic,
340 -xtime.tv_sec, -xtime.tv_nsec);
343 #define FEBRUARY 2
344 #define STARTOFTIME 1970
345 #define SECDAY 86400L
346 #define SECYR (SECDAY * 365)
349 * Note: this is wrong for 2100, but our signed 32-bit time_t will
350 * have overflowed long before that, so who cares. -- paulus
352 #define leapyear(year) ((year) % 4 == 0)
353 #define days_in_year(a) (leapyear(a) ? 366 : 365)
354 #define days_in_month(a) (month_days[(a) - 1])
356 static int month_days[12] = {
357 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
360 void to_tm(int tim, struct rtc_time * tm)
362 register int i;
363 register long hms, day, gday;
365 gday = day = tim / SECDAY;
366 hms = tim % SECDAY;
368 /* Hours, minutes, seconds are easy */
369 tm->tm_hour = hms / 3600;
370 tm->tm_min = (hms % 3600) / 60;
371 tm->tm_sec = (hms % 3600) % 60;
373 /* Number of years in days */
374 for (i = STARTOFTIME; day >= days_in_year(i); i++)
375 day -= days_in_year(i);
376 tm->tm_year = i;
378 /* Number of months in days left */
379 if (leapyear(tm->tm_year))
380 days_in_month(FEBRUARY) = 29;
381 for (i = 1; day >= days_in_month(i); i++)
382 day -= days_in_month(i);
383 days_in_month(FEBRUARY) = 28;
384 tm->tm_mon = i;
386 /* Days are what is left over (+1) from all that. */
387 tm->tm_mday = day + 1;
390 * Determine the day of week. Jan. 1, 1970 was a Thursday.
392 tm->tm_wday = (gday + 4) % 7;
395 /* Auxiliary function to compute scaling factors */
396 /* Actually the choice of a timebase running at 1/4 the of the bus
397 * frequency giving resolution of a few tens of nanoseconds is quite nice.
398 * It makes this computation very precise (27-28 bits typically) which
399 * is optimistic considering the stability of most processor clock
400 * oscillators and the precision with which the timebase frequency
401 * is measured but does not harm.
403 unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) {
404 unsigned mlt=0, tmp, err;
405 /* No concern for performance, it's done once: use a stupid
406 * but safe and compact method to find the multiplier.
408 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
409 if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
411 /* We might still be off by 1 for the best approximation.
412 * A side effect of this is that if outscale is too large
413 * the returned value will be zero.
414 * Many corner cases have been checked and seem to work,
415 * some might have been forgotten in the test however.
417 err = inscale*(mlt+1);
418 if (err <= inscale/2) mlt++;
419 return mlt;
422 unsigned long long sched_clock(void)
424 unsigned long lo, hi, hi2;
425 unsigned long long tb;
427 if (!__USE_RTC()) {
428 do {
429 hi = get_tbu();
430 lo = get_tbl();
431 hi2 = get_tbu();
432 } while (hi2 != hi);
433 tb = ((unsigned long long) hi << 32) | lo;
434 tb = (tb * tb_to_ns_scale) >> 10;
435 } else {
436 do {
437 hi = get_rtcu();
438 lo = get_rtcl();
439 hi2 = get_rtcu();
440 } while (hi2 != hi);
441 tb = ((unsigned long long) hi) * 1000000000 + lo;
443 return tb;