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 static struct time_interpolator itc_interpolator = {
49         .shift = 16,
50         .mask = 0xffffffffffffffffLL,
51         .source = TIME_SOURCE_CPU
52 };
53
54 static irqreturn_t
55 timer_interrupt (int irq, void *dev_id, struct pt_regs *regs)
56 {
57         unsigned long new_itm;
58
59         if (unlikely(cpu_is_offline(smp_processor_id()))) {
60                 return IRQ_HANDLED;
61         }
62
63         platform_timer_interrupt(irq, dev_id, regs);
64
65         new_itm = local_cpu_data->itm_next;
66
67         if (!time_after(ia64_get_itc(), new_itm))
68                 printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
69                        ia64_get_itc(), new_itm);
70
71         profile_tick(CPU_PROFILING, regs);
72
73         while (1) {
74 #ifdef CONFIG_SMP
75                 /*
76                  * For UP, this is done in do_timer().  Weird, but
77                  * fixing that would require updates to all
78                  * platforms.
79                  */
80                 update_process_times(user_mode(regs));
81 #endif
82                 new_itm += local_cpu_data->itm_delta;
83
84                 if (smp_processor_id() == TIME_KEEPER_ID) {
85                         /*
86                          * Here we are in the timer irq handler. We have irqs locally
87                          * disabled, but we don't know if the timer_bh is running on
88                          * another CPU. We need to avoid to SMP race by acquiring the
89                          * xtime_lock.
90                          */
91                         write_seqlock(&xtime_lock);
92                         do_timer(regs);
93                         local_cpu_data->itm_next = new_itm;
94                         write_sequnlock(&xtime_lock);
95                 } else
96                         local_cpu_data->itm_next = new_itm;
97
98                 if (time_after(new_itm, ia64_get_itc()))
99                         break;
100         }
101
102         do {
103                 /*
104                  * If we're too close to the next clock tick for
105                  * comfort, we increase the safety margin by
106                  * intentionally dropping the next tick(s).  We do NOT
107                  * update itm.next because that would force us to call
108                  * do_timer() which in turn would let our clock run
109                  * too fast (with the potentially devastating effect
110                  * of losing monotony of time).
111                  */
112                 while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
113                         new_itm += local_cpu_data->itm_delta;
114                 ia64_set_itm(new_itm);
115                 /* double check, in case we got hit by a (slow) PMI: */
116         } while (time_after_eq(ia64_get_itc(), new_itm));
117         return IRQ_HANDLED;
118 }
119
120 /*
121  * Encapsulate access to the itm structure for SMP.
122  */
123 void
124 ia64_cpu_local_tick (void)
125 {
126         int cpu = smp_processor_id();
127         unsigned long shift = 0, delta;
128
129         /* arrange for the cycle counter to generate a timer interrupt: */
130         ia64_set_itv(IA64_TIMER_VECTOR);
131
132         delta = local_cpu_data->itm_delta;
133         /*
134          * Stagger the timer tick for each CPU so they don't occur all at (almost) the
135          * same time:
136          */
137         if (cpu) {
138                 unsigned long hi = 1UL << ia64_fls(cpu);
139                 shift = (2*(cpu - hi) + 1) * delta/hi/2;
140         }
141         local_cpu_data->itm_next = ia64_get_itc() + delta + shift;
142         ia64_set_itm(local_cpu_data->itm_next);
143 }
144
145 static int nojitter;
146
147 static int __init nojitter_setup(char *str)
148 {
149         nojitter = 1;
150         printk("Jitter checking for ITC timers disabled\n");
151         return 1;
152 }
153
154 __setup("nojitter", nojitter_setup);
155
156
157 void __devinit
158 ia64_init_itm (void)
159 {
160         unsigned long platform_base_freq, itc_freq;
161         struct pal_freq_ratio itc_ratio, proc_ratio;
162         long status, platform_base_drift, itc_drift;
163
164         /*
165          * According to SAL v2.6, we need to use a SAL call to determine the platform base
166          * frequency and then a PAL call to determine the frequency ratio between the ITC
167          * and the base frequency.
168          */
169         status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
170                                     &platform_base_freq, &platform_base_drift);
171         if (status != 0) {
172                 printk(KERN_ERR "SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status));
173         } else {
174                 status = ia64_pal_freq_ratios(&proc_ratio, NULL, &itc_ratio);
175                 if (status != 0)
176                         printk(KERN_ERR "PAL_FREQ_RATIOS failed with status=%ld\n", status);
177         }
178         if (status != 0) {
179                 /* invent "random" values */
180                 printk(KERN_ERR
181                        "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
182                 platform_base_freq = 100000000;
183                 platform_base_drift = -1;       /* no drift info */
184                 itc_ratio.num = 3;
185                 itc_ratio.den = 1;
186         }
187         if (platform_base_freq < 40000000) {
188                 printk(KERN_ERR "Platform base frequency %lu bogus---resetting to 75MHz!\n",
189                        platform_base_freq);
190                 platform_base_freq = 75000000;
191                 platform_base_drift = -1;
192         }
193         if (!proc_ratio.den)
194                 proc_ratio.den = 1;     /* avoid division by zero */
195         if (!itc_ratio.den)
196                 itc_ratio.den = 1;      /* avoid division by zero */
197
198         itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
199         if (platform_base_drift != -1)
200                 itc_drift = platform_base_drift*itc_ratio.num/itc_ratio.den;
201         else
202                 itc_drift = -1;
203
204         local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
205         printk(KERN_DEBUG "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%lu/%lu, "
206                "ITC freq=%lu.%03luMHz+/-%ldppm\n", smp_processor_id(),
207                platform_base_freq / 1000000, (platform_base_freq / 1000) % 1000,
208                itc_ratio.num, itc_ratio.den, itc_freq / 1000000, (itc_freq / 1000) % 1000,
209                itc_drift);
210
211         local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
212         local_cpu_data->itc_freq = itc_freq;
213         local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
214         local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
215                                         + itc_freq/2)/itc_freq;
216
217         if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
218                 itc_interpolator.frequency = local_cpu_data->itc_freq;
219                 itc_interpolator.drift = itc_drift;
220 #ifdef CONFIG_SMP
221                 /* On IA64 in an SMP configuration ITCs are never accurately synchronized.
222                  * Jitter compensation requires a cmpxchg which may limit
223                  * the scalability of the syscalls for retrieving time.
224                  * The ITC synchronization is usually successful to within a few
225                  * ITC ticks but this is not a sure thing. If you need to improve
226                  * timer performance in SMP situations then boot the kernel with the
227                  * "nojitter" option. However, doing so may result in time fluctuating (maybe
228                  * even going backward) if the ITC offsets between the individual CPUs
229                  * are too large.
230                  */
231                 if (!nojitter) itc_interpolator.jitter = 1;
232 #endif
233                 register_time_interpolator(&itc_interpolator);
234         }
235
236         /* Setup the CPU local timer tick */
237         ia64_cpu_local_tick();
238 }
239
240 static struct irqaction timer_irqaction = {
241         .handler =      timer_interrupt,
242         .flags =        SA_INTERRUPT,
243         .name =         "timer"
244 };
245
246 void __init
247 time_init (void)
248 {
249         register_percpu_irq(IA64_TIMER_VECTOR, &timer_irqaction);
250         efi_gettimeofday(&xtime);
251         ia64_init_itm();
252
253         /*
254          * Initialize wall_to_monotonic such that adding it to xtime will yield zero, the
255          * tv_nsec field must be normalized (i.e., 0 <= nsec < NSEC_PER_SEC).
256          */
257         set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
258 }