2 * linux/arch/ia64/kernel/time.c
4 * Copyright (C) 1998-2003 Hewlett-Packard Co
5 * Stephane Eranian <eranian@hpl.hp.com>
6 * David Mosberger <davidm@hpl.hp.com>
7 * Copyright (C) 1999 Don Dugger <don.dugger@intel.com>
8 * Copyright (C) 1999-2000 VA Linux Systems
9 * Copyright (C) 1999-2000 Walt Drummond <drummond@valinux.com>
11 #include <linux/config.h>
13 #include <linux/cpu.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/profile.h>
18 #include <linux/sched.h>
19 #include <linux/time.h>
20 #include <linux/interrupt.h>
21 #include <linux/efi.h>
22 #include <linux/profile.h>
23 #include <linux/timex.h>
25 #include <asm/machvec.h>
26 #include <asm/delay.h>
27 #include <asm/hw_irq.h>
28 #include <asm/ptrace.h>
30 #include <asm/sections.h>
31 #include <asm/system.h>
33 extern unsigned long wall_jiffies;
35 u64 jiffies_64 = INITIAL_JIFFIES;
37 EXPORT_SYMBOL(jiffies_64);
39 #define TIME_KEEPER_ID 0 /* smp_processor_id() of time-keeper */
41 #ifdef CONFIG_IA64_DEBUG_IRQ
43 unsigned long last_cli_ip;
44 EXPORT_SYMBOL(last_cli_ip);
54 * Adjust for the fact that xtime has been advanced by delta_nsec (may be negative and/or
55 * larger than NSEC_PER_SEC.
58 itc_update (long delta_nsec)
63 * Return the number of nano-seconds that elapsed since the last
64 * update to jiffy. It is quite possible that the timer interrupt
65 * will interrupt this and result in a race for any of jiffies,
66 * wall_jiffies or itm_next. Thus, the xtime_lock must be at least
67 * read synchronised when calling this routine (see do_gettimeofday()
68 * below for an example).
73 unsigned long elapsed_cycles, lost = jiffies - wall_jiffies;
74 unsigned long now = ia64_get_itc(), last_tick;
76 last_tick = (cpu_data(TIME_KEEPER_ID)->itm_next
77 - (lost + 1)*cpu_data(TIME_KEEPER_ID)->itm_delta);
79 elapsed_cycles = now - last_tick;
80 return (elapsed_cycles*local_cpu_data->nsec_per_cyc) >> IA64_NSEC_PER_CYC_SHIFT;
83 static struct time_interpolator itc_interpolator = {
84 .get_offset = itc_get_offset,
90 do_settimeofday (struct timespec *tv)
92 time_t wtm_sec, sec = tv->tv_sec;
93 long wtm_nsec, nsec = tv->tv_nsec;
95 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
98 write_seqlock_irq(&xtime_lock);
101 * This is revolting. We need to set "xtime" correctly. However, the value
102 * in this location is the value at the most recent update of wall time.
103 * Discover what correction gettimeofday would have done, and then undo
106 nsec -= time_interpolator_get_offset();
108 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
109 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
111 set_normalized_timespec(&xtime, sec, nsec);
112 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
114 time_adjust = 0; /* stop active adjtime() */
115 time_status |= STA_UNSYNC;
116 time_maxerror = NTP_PHASE_LIMIT;
117 time_esterror = NTP_PHASE_LIMIT;
118 time_interpolator_reset();
120 write_sequnlock_irq(&xtime_lock);
125 EXPORT_SYMBOL(do_settimeofday);
128 do_gettimeofday (struct timeval *tv)
130 unsigned long seq, nsec, usec, sec, old, offset;
133 seq = read_seqbegin(&xtime_lock);
135 old = last_nsec_offset;
136 offset = time_interpolator_get_offset();
138 nsec = xtime.tv_nsec;
140 if (unlikely(read_seqretry(&xtime_lock, seq)))
143 * Ensure that for any pair of causally ordered gettimeofday() calls, time
144 * never goes backwards (even when ITC on different CPUs are not perfectly
145 * synchronized). (A pair of concurrent calls to gettimeofday() is by
146 * definition non-causal and hence it makes no sense to talk about
147 * time-continuity for such calls.)
149 * Doing this in a lock-free and race-free manner is tricky. Here is why
150 * it works (most of the time): read_seqretry() just succeeded, which
151 * implies we calculated a consistent (valid) value for "offset". If the
152 * cmpxchg() below succeeds, we further know that last_nsec_offset still
153 * has the same value as at the beginning of the loop, so there was
154 * presumably no timer-tick or other updates to last_nsec_offset in the
155 * meantime. This isn't 100% true though: there _is_ a possibility of a
156 * timer-tick occurring right right after read_seqretry() and then getting
157 * zero or more other readers which will set last_nsec_offset to the same
158 * value as the one we read at the beginning of the loop. If this
159 * happens, we'll end up returning a slightly newer time than we ought to
160 * (the jump forward is at most "offset" nano-seconds). There is no
161 * danger of causing time to go backwards, though, so we are safe in that
162 * sense. We could make the probability of this unlucky case occurring
163 * arbitrarily small by encoding a version number in last_nsec_offset, but
164 * even without versioning, the probability of this unlucky case should be
165 * so small that we won't worry about it.
170 } else if (likely(cmpxchg(&last_nsec_offset, old, offset) == old))
173 /* someone else beat us to updating last_nsec_offset; try again */
176 usec = (nsec + offset) / 1000;
178 while (unlikely(usec >= USEC_PER_SEC)) {
179 usec -= USEC_PER_SEC;
187 EXPORT_SYMBOL(do_gettimeofday);
190 * The profiling function is SMP safe. (nothing can mess
191 * around with "current", and the profiling counters are
192 * updated with atomic operations). This is especially
193 * useful with a profiling multiplier != 1
196 ia64_do_profile (struct pt_regs * regs)
198 unsigned long ip, slot;
199 extern cpumask_t prof_cpu_mask;
209 ip = instruction_pointer(regs);
210 /* Conserve space in histogram by encoding slot bits in address
211 * bits 2 and 3 rather than bits 0 and 1.
214 ip = (ip & ~3UL) + 4*slot;
217 * Only measure the CPUs specified by /proc/irq/prof_cpu_mask.
218 * (default is all CPUs.)
220 if (!cpu_isset(smp_processor_id(), prof_cpu_mask))
223 ip -= (unsigned long) &_stext;
226 * Don't ignore out-of-bounds IP values silently,
227 * put them into the last histogram slot, so if
228 * present, they will show up as a sharp peak.
232 atomic_inc((atomic_t *)&prof_buffer[ip]);
236 timer_interrupt (int irq, void *dev_id, struct pt_regs *regs)
238 unsigned long new_itm;
240 if (unlikely(cpu_is_offline(smp_processor_id()))) {
244 platform_timer_interrupt(irq, dev_id, regs);
246 new_itm = local_cpu_data->itm_next;
248 if (!time_after(ia64_get_itc(), new_itm))
249 printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
250 ia64_get_itc(), new_itm);
252 ia64_do_profile(regs);
257 * For UP, this is done in do_timer(). Weird, but
258 * fixing that would require updates to all
261 update_process_times(user_mode(regs));
263 new_itm += local_cpu_data->itm_delta;
265 if (smp_processor_id() == TIME_KEEPER_ID) {
267 * Here we are in the timer irq handler. We have irqs locally
268 * disabled, but we don't know if the timer_bh is running on
269 * another CPU. We need to avoid to SMP race by acquiring the
272 write_seqlock(&xtime_lock);
274 local_cpu_data->itm_next = new_itm;
275 write_sequnlock(&xtime_lock);
277 local_cpu_data->itm_next = new_itm;
279 if (time_after(new_itm, ia64_get_itc()))
285 * If we're too close to the next clock tick for
286 * comfort, we increase the safety margin by
287 * intentionally dropping the next tick(s). We do NOT
288 * update itm.next because that would force us to call
289 * do_timer() which in turn would let our clock run
290 * too fast (with the potentially devastating effect
291 * of losing monotony of time).
293 while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
294 new_itm += local_cpu_data->itm_delta;
295 ia64_set_itm(new_itm);
296 /* double check, in case we got hit by a (slow) PMI: */
297 } while (time_after_eq(ia64_get_itc(), new_itm));
302 * Encapsulate access to the itm structure for SMP.
305 ia64_cpu_local_tick (void)
307 int cpu = smp_processor_id();
308 unsigned long shift = 0, delta;
310 /* arrange for the cycle counter to generate a timer interrupt: */
311 ia64_set_itv(IA64_TIMER_VECTOR);
313 delta = local_cpu_data->itm_delta;
315 * Stagger the timer tick for each CPU so they don't occur all at (almost) the
319 unsigned long hi = 1UL << ia64_fls(cpu);
320 shift = (2*(cpu - hi) + 1) * delta/hi/2;
322 local_cpu_data->itm_next = ia64_get_itc() + delta + shift;
323 ia64_set_itm(local_cpu_data->itm_next);
329 unsigned long platform_base_freq, itc_freq;
330 struct pal_freq_ratio itc_ratio, proc_ratio;
331 long status, platform_base_drift, itc_drift;
334 * According to SAL v2.6, we need to use a SAL call to determine the platform base
335 * frequency and then a PAL call to determine the frequency ratio between the ITC
336 * and the base frequency.
338 status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
339 &platform_base_freq, &platform_base_drift);
341 printk(KERN_ERR "SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status));
343 status = ia64_pal_freq_ratios(&proc_ratio, 0, &itc_ratio);
345 printk(KERN_ERR "PAL_FREQ_RATIOS failed with status=%ld\n", status);
348 /* invent "random" values */
350 "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
351 platform_base_freq = 100000000;
352 platform_base_drift = -1; /* no drift info */
356 if (platform_base_freq < 40000000) {
357 printk(KERN_ERR "Platform base frequency %lu bogus---resetting to 75MHz!\n",
359 platform_base_freq = 75000000;
360 platform_base_drift = -1;
363 proc_ratio.den = 1; /* avoid division by zero */
365 itc_ratio.den = 1; /* avoid division by zero */
367 itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
368 if (platform_base_drift != -1)
369 itc_drift = platform_base_drift*itc_ratio.num/itc_ratio.den;
373 local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
374 printk(KERN_INFO "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%lu/%lu, "
375 "ITC freq=%lu.%03luMHz+/-%ldppm\n", smp_processor_id(),
376 platform_base_freq / 1000000, (platform_base_freq / 1000) % 1000,
377 itc_ratio.num, itc_ratio.den, itc_freq / 1000000, (itc_freq / 1000) % 1000,
380 local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
381 local_cpu_data->itc_freq = itc_freq;
382 local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
383 local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
384 + itc_freq/2)/itc_freq;
386 if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
387 itc_interpolator.frequency = local_cpu_data->itc_freq;
388 itc_interpolator.drift = itc_drift;
389 register_time_interpolator(&itc_interpolator);
392 /* Setup the CPU local timer tick */
393 ia64_cpu_local_tick();
396 static struct irqaction timer_irqaction = {
397 .handler = timer_interrupt,
398 .flags = SA_INTERRUPT,
405 register_percpu_irq(IA64_TIMER_VECTOR, &timer_irqaction);
406 efi_gettimeofday(&xtime);
410 * Initialize wall_to_monotonic such that adding it to xtime will yield zero, the
411 * tv_nsec field must be normalized (i.e., 0 <= nsec < NSEC_PER_SEC).
413 set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);