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>
12 #include <linux/cpu.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/profile.h>
17 #include <linux/sched.h>
18 #include <linux/time.h>
19 #include <linux/interrupt.h>
20 #include <linux/efi.h>
21 #include <linux/profile.h>
22 #include <linux/timex.h>
24 #include <asm/machvec.h>
25 #include <asm/delay.h>
26 #include <asm/hw_irq.h>
27 #include <asm/ptrace.h>
29 #include <asm/sections.h>
30 #include <asm/system.h>
32 extern unsigned long wall_jiffies
;
34 volatile int time_keeper_id
= 0; /* smp_processor_id() of time-keeper */
36 #ifdef CONFIG_IA64_DEBUG_IRQ
38 unsigned long last_cli_ip
;
39 EXPORT_SYMBOL(last_cli_ip
);
43 static struct time_interpolator itc_interpolator
= {
45 .mask
= 0xffffffffffffffffLL
,
46 .source
= TIME_SOURCE_CPU
50 timer_interrupt (int irq
, void *dev_id
, struct pt_regs
*regs
)
52 unsigned long new_itm
;
54 if (unlikely(cpu_is_offline(smp_processor_id()))) {
58 platform_timer_interrupt(irq
, dev_id
, regs
);
60 new_itm
= local_cpu_data
->itm_next
;
62 if (!time_after(ia64_get_itc(), new_itm
))
63 printk(KERN_ERR
"Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
64 ia64_get_itc(), new_itm
);
66 profile_tick(CPU_PROFILING
, regs
);
69 update_process_times(user_mode(regs
));
71 new_itm
+= local_cpu_data
->itm_delta
;
73 if (smp_processor_id() == time_keeper_id
) {
75 * Here we are in the timer irq handler. We have irqs locally
76 * disabled, but we don't know if the timer_bh is running on
77 * another CPU. We need to avoid to SMP race by acquiring the
80 write_seqlock(&xtime_lock
);
82 local_cpu_data
->itm_next
= new_itm
;
83 write_sequnlock(&xtime_lock
);
85 local_cpu_data
->itm_next
= new_itm
;
87 if (time_after(new_itm
, ia64_get_itc()))
93 * If we're too close to the next clock tick for
94 * comfort, we increase the safety margin by
95 * intentionally dropping the next tick(s). We do NOT
96 * update itm.next because that would force us to call
97 * do_timer() which in turn would let our clock run
98 * too fast (with the potentially devastating effect
99 * of losing monotony of time).
101 while (!time_after(new_itm
, ia64_get_itc() + local_cpu_data
->itm_delta
/2))
102 new_itm
+= local_cpu_data
->itm_delta
;
103 ia64_set_itm(new_itm
);
104 /* double check, in case we got hit by a (slow) PMI: */
105 } while (time_after_eq(ia64_get_itc(), new_itm
));
110 * Encapsulate access to the itm structure for SMP.
113 ia64_cpu_local_tick (void)
115 int cpu
= smp_processor_id();
116 unsigned long shift
= 0, delta
;
118 /* arrange for the cycle counter to generate a timer interrupt: */
119 ia64_set_itv(IA64_TIMER_VECTOR
);
121 delta
= local_cpu_data
->itm_delta
;
123 * Stagger the timer tick for each CPU so they don't occur all at (almost) the
127 unsigned long hi
= 1UL << ia64_fls(cpu
);
128 shift
= (2*(cpu
- hi
) + 1) * delta
/hi
/2;
130 local_cpu_data
->itm_next
= ia64_get_itc() + delta
+ shift
;
131 ia64_set_itm(local_cpu_data
->itm_next
);
136 static int __init
nojitter_setup(char *str
)
139 printk("Jitter checking for ITC timers disabled\n");
143 __setup("nojitter", nojitter_setup
);
149 unsigned long platform_base_freq
, itc_freq
;
150 struct pal_freq_ratio itc_ratio
, proc_ratio
;
151 long status
, platform_base_drift
, itc_drift
;
154 * According to SAL v2.6, we need to use a SAL call to determine the platform base
155 * frequency and then a PAL call to determine the frequency ratio between the ITC
156 * and the base frequency.
158 status
= ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM
,
159 &platform_base_freq
, &platform_base_drift
);
161 printk(KERN_ERR
"SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status
));
163 status
= ia64_pal_freq_ratios(&proc_ratio
, NULL
, &itc_ratio
);
165 printk(KERN_ERR
"PAL_FREQ_RATIOS failed with status=%ld\n", status
);
168 /* invent "random" values */
170 "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
171 platform_base_freq
= 100000000;
172 platform_base_drift
= -1; /* no drift info */
176 if (platform_base_freq
< 40000000) {
177 printk(KERN_ERR
"Platform base frequency %lu bogus---resetting to 75MHz!\n",
179 platform_base_freq
= 75000000;
180 platform_base_drift
= -1;
183 proc_ratio
.den
= 1; /* avoid division by zero */
185 itc_ratio
.den
= 1; /* avoid division by zero */
187 itc_freq
= (platform_base_freq
*itc_ratio
.num
)/itc_ratio
.den
;
189 local_cpu_data
->itm_delta
= (itc_freq
+ HZ
/2) / HZ
;
190 printk(KERN_DEBUG
"CPU %d: base freq=%lu.%03luMHz, ITC ratio=%u/%u, "
191 "ITC freq=%lu.%03luMHz", smp_processor_id(),
192 platform_base_freq
/ 1000000, (platform_base_freq
/ 1000) % 1000,
193 itc_ratio
.num
, itc_ratio
.den
, itc_freq
/ 1000000, (itc_freq
/ 1000) % 1000);
195 if (platform_base_drift
!= -1) {
196 itc_drift
= platform_base_drift
*itc_ratio
.num
/itc_ratio
.den
;
197 printk("+/-%ldppm\n", itc_drift
);
203 local_cpu_data
->proc_freq
= (platform_base_freq
*proc_ratio
.num
)/proc_ratio
.den
;
204 local_cpu_data
->itc_freq
= itc_freq
;
205 local_cpu_data
->cyc_per_usec
= (itc_freq
+ USEC_PER_SEC
/2) / USEC_PER_SEC
;
206 local_cpu_data
->nsec_per_cyc
= ((NSEC_PER_SEC
<<IA64_NSEC_PER_CYC_SHIFT
)
207 + itc_freq
/2)/itc_freq
;
209 if (!(sal_platform_features
& IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT
)) {
210 itc_interpolator
.frequency
= local_cpu_data
->itc_freq
;
211 itc_interpolator
.drift
= itc_drift
;
213 /* On IA64 in an SMP configuration ITCs are never accurately synchronized.
214 * Jitter compensation requires a cmpxchg which may limit
215 * the scalability of the syscalls for retrieving time.
216 * The ITC synchronization is usually successful to within a few
217 * ITC ticks but this is not a sure thing. If you need to improve
218 * timer performance in SMP situations then boot the kernel with the
219 * "nojitter" option. However, doing so may result in time fluctuating (maybe
220 * even going backward) if the ITC offsets between the individual CPUs
223 if (!nojitter
) itc_interpolator
.jitter
= 1;
225 register_time_interpolator(&itc_interpolator
);
228 /* Setup the CPU local timer tick */
229 ia64_cpu_local_tick();
232 static struct irqaction timer_irqaction
= {
233 .handler
= timer_interrupt
,
234 .flags
= IRQF_DISABLED
,
238 void __devinit
ia64_disable_timer(void)
240 ia64_set_itv(1 << 16);
246 register_percpu_irq(IA64_TIMER_VECTOR
, &timer_irqaction
);
247 efi_gettimeofday(&xtime
);
251 * Initialize wall_to_monotonic such that adding it to xtime will yield zero, the
252 * tv_nsec field must be normalized (i.e., 0 <= nsec < NSEC_PER_SEC).
254 set_normalized_timespec(&wall_to_monotonic
, -xtime
.tv_sec
, -xtime
.tv_nsec
);
258 * Generic udelay assumes that if preemption is allowed and the thread
259 * migrates to another CPU, that the ITC values are synchronized across
263 ia64_itc_udelay (unsigned long usecs
)
265 unsigned long start
= ia64_get_itc();
266 unsigned long end
= start
+ usecs
*local_cpu_data
->cyc_per_usec
;
268 while (time_before(ia64_get_itc(), end
))
272 void (*ia64_udelay
)(unsigned long usecs
) = &ia64_itc_udelay
;
275 udelay (unsigned long usecs
)
277 (*ia64_udelay
)(usecs
);
279 EXPORT_SYMBOL(udelay
);
281 static unsigned long long ia64_itc_printk_clock(void)
283 if (ia64_get_kr(IA64_KR_PER_CPU_DATA
))
284 return sched_clock();
288 static unsigned long long ia64_default_printk_clock(void)
290 return (unsigned long long)(jiffies_64
- INITIAL_JIFFIES
) *
294 unsigned long long (*ia64_printk_clock
)(void) = &ia64_default_printk_clock
;
296 unsigned long long printk_clock(void)
298 return ia64_printk_clock();
302 ia64_setup_printk_clock(void)
304 if (!(sal_platform_features
& IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT
))
305 ia64_printk_clock
= ia64_itc_printk_clock
;