This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / ia64 / kernel / time.c
1 /*
2  * linux/arch/ia64/kernel/time.c
3  *
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>
10  */
11 #include <linux/config.h>
12
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>
24
25 #include <asm/machvec.h>
26 #include <asm/delay.h>
27 #include <asm/hw_irq.h>
28 #include <asm/ptrace.h>
29 #include <asm/sal.h>
30 #include <asm/sections.h>
31 #include <asm/system.h>
32
33 extern unsigned long wall_jiffies;
34
35 u64 jiffies_64 = INITIAL_JIFFIES;
36
37 EXPORT_SYMBOL(jiffies_64);
38
39 #define TIME_KEEPER_ID  0       /* smp_processor_id() of time-keeper */
40
41 #ifdef CONFIG_IA64_DEBUG_IRQ
42
43 unsigned long last_cli_ip;
44 EXPORT_SYMBOL(last_cli_ip);
45
46 #endif
47
48 unsigned long long
49 sched_clock (void)
50 {
51         unsigned long offset = ia64_get_itc();
52
53         return (offset * local_cpu_data->nsec_per_cyc) >> IA64_NSEC_PER_CYC_SHIFT;
54 }
55
56 static void
57 itc_reset (void)
58 {
59 }
60
61 /*
62  * Adjust for the fact that xtime has been advanced by delta_nsec (may be negative and/or
63  * larger than NSEC_PER_SEC.
64  */
65 static void
66 itc_update (long delta_nsec)
67 {
68 }
69
70 /*
71  * Return the number of nano-seconds that elapsed since the last
72  * update to jiffy.  It is quite possible that the timer interrupt
73  * will interrupt this and result in a race for any of jiffies,
74  * wall_jiffies or itm_next.  Thus, the xtime_lock must be at least
75  * read synchronised when calling this routine (see do_gettimeofday()
76  * below for an example).
77  */
78 unsigned long
79 itc_get_offset (void)
80 {
81         unsigned long elapsed_cycles, lost = jiffies - wall_jiffies;
82         unsigned long now = ia64_get_itc(), last_tick;
83
84         last_tick = (cpu_data(TIME_KEEPER_ID)->itm_next
85                      - (lost + 1)*cpu_data(TIME_KEEPER_ID)->itm_delta);
86
87         elapsed_cycles = now - last_tick;
88         return (elapsed_cycles*local_cpu_data->nsec_per_cyc) >> IA64_NSEC_PER_CYC_SHIFT;
89 }
90
91 static struct time_interpolator itc_interpolator = {
92         .get_offset =   itc_get_offset,
93         .update =       itc_update,
94         .reset =        itc_reset
95 };
96
97 int
98 do_settimeofday (struct timespec *tv)
99 {
100         time_t wtm_sec, sec = tv->tv_sec;
101         long wtm_nsec, nsec = tv->tv_nsec;
102
103         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
104                 return -EINVAL;
105
106         write_seqlock_irq(&xtime_lock);
107         {
108                 /*
109                  * This is revolting. We need to set "xtime" correctly. However, the value
110                  * in this location is the value at the most recent update of wall time.
111                  * Discover what correction gettimeofday would have done, and then undo
112                  * it!
113                  */
114                 nsec -= time_interpolator_get_offset();
115
116                 wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
117                 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
118
119                 set_normalized_timespec(&xtime, sec, nsec);
120                 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
121
122                 time_adjust = 0;                /* stop active adjtime() */
123                 time_status |= STA_UNSYNC;
124                 time_maxerror = NTP_PHASE_LIMIT;
125                 time_esterror = NTP_PHASE_LIMIT;
126                 time_interpolator_reset();
127         }
128         write_sequnlock_irq(&xtime_lock);
129         clock_was_set();
130         return 0;
131 }
132
133 EXPORT_SYMBOL(do_settimeofday);
134
135 void
136 do_gettimeofday (struct timeval *tv)
137 {
138         unsigned long seq, nsec, usec, sec, old, offset;
139
140         while (1) {
141                 seq = read_seqbegin(&xtime_lock);
142                 {
143                         old = last_nsec_offset;
144                         offset = time_interpolator_get_offset();
145                         sec = xtime.tv_sec;
146                         nsec = xtime.tv_nsec;
147                 }
148                 if (unlikely(read_seqretry(&xtime_lock, seq)))
149                         continue;
150                 /*
151                  * Ensure that for any pair of causally ordered gettimeofday() calls, time
152                  * never goes backwards (even when ITC on different CPUs are not perfectly
153                  * synchronized).  (A pair of concurrent calls to gettimeofday() is by
154                  * definition non-causal and hence it makes no sense to talk about
155                  * time-continuity for such calls.)
156                  *
157                  * Doing this in a lock-free and race-free manner is tricky.  Here is why
158                  * it works (most of the time): read_seqretry() just succeeded, which
159                  * implies we calculated a consistent (valid) value for "offset".  If the
160                  * cmpxchg() below succeeds, we further know that last_nsec_offset still
161                  * has the same value as at the beginning of the loop, so there was
162                  * presumably no timer-tick or other updates to last_nsec_offset in the
163                  * meantime.  This isn't 100% true though: there _is_ a possibility of a
164                  * timer-tick occurring right right after read_seqretry() and then getting
165                  * zero or more other readers which will set last_nsec_offset to the same
166                  * value as the one we read at the beginning of the loop.  If this
167                  * happens, we'll end up returning a slightly newer time than we ought to
168                  * (the jump forward is at most "offset" nano-seconds).  There is no
169                  * danger of causing time to go backwards, though, so we are safe in that
170                  * sense.  We could make the probability of this unlucky case occurring
171                  * arbitrarily small by encoding a version number in last_nsec_offset, but
172                  * even without versioning, the probability of this unlucky case should be
173                  * so small that we won't worry about it.
174                  */
175                 if (offset <= old) {
176                         offset = old;
177                         break;
178                 } else if (likely(cmpxchg(&last_nsec_offset, old, offset) == old))
179                         break;
180
181                 /* someone else beat us to updating last_nsec_offset; try again */
182         }
183
184         usec = (nsec + offset) / 1000;
185
186         while (unlikely(usec >= USEC_PER_SEC)) {
187                 usec -= USEC_PER_SEC;
188                 ++sec;
189         }
190
191         tv->tv_sec = sec;
192         tv->tv_usec = usec;
193 }
194
195 EXPORT_SYMBOL(do_gettimeofday);
196
197 /*
198  * The profiling function is SMP safe. (nothing can mess
199  * around with "current", and the profiling counters are
200  * updated with atomic operations). This is especially
201  * useful with a profiling multiplier != 1
202  */
203 static inline void
204 ia64_do_profile (struct pt_regs * regs)
205 {
206         unsigned long ip, slot;
207         extern cpumask_t prof_cpu_mask;
208
209         profile_hook(regs);
210
211         if (user_mode(regs))
212                 return;
213
214         if (!prof_buffer)
215                 return;
216
217         ip = instruction_pointer(regs);
218         /* Conserve space in histogram by encoding slot bits in address
219          * bits 2 and 3 rather than bits 0 and 1.
220          */
221         slot = ip & 3;
222         ip = (ip & ~3UL) + 4*slot;
223
224         /*
225          * Only measure the CPUs specified by /proc/irq/prof_cpu_mask.
226          * (default is all CPUs.)
227          */
228         if (!cpu_isset(smp_processor_id(), prof_cpu_mask))
229                 return;
230
231         ip -= (unsigned long) &_stext;
232         ip >>= prof_shift;
233         /*
234          * Don't ignore out-of-bounds IP values silently,
235          * put them into the last histogram slot, so if
236          * present, they will show up as a sharp peak.
237          */
238         if (ip > prof_len-1)
239                 ip = prof_len-1;
240         atomic_inc((atomic_t *)&prof_buffer[ip]);
241 }
242
243 static irqreturn_t
244 timer_interrupt (int irq, void *dev_id, struct pt_regs *regs)
245 {
246         unsigned long new_itm;
247
248         if (unlikely(cpu_is_offline(smp_processor_id()))) {
249                 return IRQ_HANDLED;
250         }
251
252         platform_timer_interrupt(irq, dev_id, regs);
253
254         new_itm = local_cpu_data->itm_next;
255
256         if (!time_after(ia64_get_itc(), new_itm))
257                 printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
258                        ia64_get_itc(), new_itm);
259
260         ia64_do_profile(regs);
261
262         while (1) {
263 #ifdef CONFIG_SMP
264                 /*
265                  * For UP, this is done in do_timer().  Weird, but
266                  * fixing that would require updates to all
267                  * platforms.
268                  */
269                 update_process_times(user_mode(regs));
270 #endif
271                 new_itm += local_cpu_data->itm_delta;
272
273                 if (smp_processor_id() == TIME_KEEPER_ID) {
274                         /*
275                          * Here we are in the timer irq handler. We have irqs locally
276                          * disabled, but we don't know if the timer_bh is running on
277                          * another CPU. We need to avoid to SMP race by acquiring the
278                          * xtime_lock.
279                          */
280                         write_seqlock(&xtime_lock);
281                         do_timer(regs);
282                         local_cpu_data->itm_next = new_itm;
283                         write_sequnlock(&xtime_lock);
284                 } else
285                         local_cpu_data->itm_next = new_itm;
286
287                 if (time_after(new_itm, ia64_get_itc()))
288                         break;
289         }
290
291         do {
292                 /*
293                  * If we're too close to the next clock tick for
294                  * comfort, we increase the safety margin by
295                  * intentionally dropping the next tick(s).  We do NOT
296                  * update itm.next because that would force us to call
297                  * do_timer() which in turn would let our clock run
298                  * too fast (with the potentially devastating effect
299                  * of losing monotony of time).
300                  */
301                 while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
302                         new_itm += local_cpu_data->itm_delta;
303                 ia64_set_itm(new_itm);
304                 /* double check, in case we got hit by a (slow) PMI: */
305         } while (time_after_eq(ia64_get_itc(), new_itm));
306         return IRQ_HANDLED;
307 }
308
309 /*
310  * Encapsulate access to the itm structure for SMP.
311  */
312 void
313 ia64_cpu_local_tick (void)
314 {
315         int cpu = smp_processor_id();
316         unsigned long shift = 0, delta;
317
318         /* arrange for the cycle counter to generate a timer interrupt: */
319         ia64_set_itv(IA64_TIMER_VECTOR);
320
321         delta = local_cpu_data->itm_delta;
322         /*
323          * Stagger the timer tick for each CPU so they don't occur all at (almost) the
324          * same time:
325          */
326         if (cpu) {
327                 unsigned long hi = 1UL << ia64_fls(cpu);
328                 shift = (2*(cpu - hi) + 1) * delta/hi/2;
329         }
330         local_cpu_data->itm_next = ia64_get_itc() + delta + shift;
331         ia64_set_itm(local_cpu_data->itm_next);
332 }
333
334 void __devinit
335 ia64_init_itm (void)
336 {
337         unsigned long platform_base_freq, itc_freq;
338         struct pal_freq_ratio itc_ratio, proc_ratio;
339         long status, platform_base_drift, itc_drift;
340
341         /*
342          * According to SAL v2.6, we need to use a SAL call to determine the platform base
343          * frequency and then a PAL call to determine the frequency ratio between the ITC
344          * and the base frequency.
345          */
346         status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
347                                     &platform_base_freq, &platform_base_drift);
348         if (status != 0) {
349                 printk(KERN_ERR "SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status));
350         } else {
351                 status = ia64_pal_freq_ratios(&proc_ratio, 0, &itc_ratio);
352                 if (status != 0)
353                         printk(KERN_ERR "PAL_FREQ_RATIOS failed with status=%ld\n", status);
354         }
355         if (status != 0) {
356                 /* invent "random" values */
357                 printk(KERN_ERR
358                        "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
359                 platform_base_freq = 100000000;
360                 platform_base_drift = -1;       /* no drift info */
361                 itc_ratio.num = 3;
362                 itc_ratio.den = 1;
363         }
364         if (platform_base_freq < 40000000) {
365                 printk(KERN_ERR "Platform base frequency %lu bogus---resetting to 75MHz!\n",
366                        platform_base_freq);
367                 platform_base_freq = 75000000;
368                 platform_base_drift = -1;
369         }
370         if (!proc_ratio.den)
371                 proc_ratio.den = 1;     /* avoid division by zero */
372         if (!itc_ratio.den)
373                 itc_ratio.den = 1;      /* avoid division by zero */
374
375         itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
376         if (platform_base_drift != -1)
377                 itc_drift = platform_base_drift*itc_ratio.num/itc_ratio.den;
378         else
379                 itc_drift = -1;
380
381         local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
382         printk(KERN_INFO "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%lu/%lu, "
383                "ITC freq=%lu.%03luMHz+/-%ldppm\n", smp_processor_id(),
384                platform_base_freq / 1000000, (platform_base_freq / 1000) % 1000,
385                itc_ratio.num, itc_ratio.den, itc_freq / 1000000, (itc_freq / 1000) % 1000,
386                itc_drift);
387
388         local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
389         local_cpu_data->itc_freq = itc_freq;
390         local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
391         local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
392                                         + itc_freq/2)/itc_freq;
393
394         if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
395                 itc_interpolator.frequency = local_cpu_data->itc_freq;
396                 itc_interpolator.drift = itc_drift;
397                 register_time_interpolator(&itc_interpolator);
398         }
399
400         /* Setup the CPU local timer tick */
401         ia64_cpu_local_tick();
402 }
403
404 static struct irqaction timer_irqaction = {
405         .handler =      timer_interrupt,
406         .flags =        SA_INTERRUPT,
407         .name =         "timer"
408 };
409
410 void __init
411 time_init (void)
412 {
413         register_percpu_irq(IA64_TIMER_VECTOR, &timer_irqaction);
414         efi_gettimeofday(&xtime);
415         ia64_init_itm();
416
417         /*
418          * Initialize wall_to_monotonic such that adding it to xtime will yield zero, the
419          * tv_nsec field must be normalized (i.e., 0 <= nsec < NSEC_PER_SEC).
420          */
421         set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
422 }