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