blob: 41c40f0e4796fea795e42c571ce370e4f55d82cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/timex.h>
Tony Luck0aa366f2007-07-20 11:22:30 -070022#include <linux/clocksource.h>
dann frazier5e3fd9e2009-03-31 15:24:48 -070023#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/machvec.h>
26#include <asm/delay.h>
27#include <asm/hw_irq.h>
Isaku Yamahata00d21d82008-05-19 22:13:44 +090028#include <asm/paravirt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/ptrace.h>
30#include <asm/sal.h>
31#include <asm/sections.h>
32#include <asm/system.h>
33
Tony Luck0aa366f2007-07-20 11:22:30 -070034#include "fsyscall_gtod_data.h"
35
Magnus Damm8e196082009-04-21 12:24:00 -070036static cycle_t itc_get_cycles(struct clocksource *cs);
Tony Luck0aa366f2007-07-20 11:22:30 -070037
38struct fsyscall_gtod_data_t fsyscall_gtod_data = {
39 .lock = SEQLOCK_UNLOCKED,
40};
41
42struct itc_jitter_data_t itc_jitter_data;
43
Ashok Rajff741902005-11-11 14:32:40 -080044volatile int time_keeper_id = 0; /* smp_processor_id() of time-keeper */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#ifdef CONFIG_IA64_DEBUG_IRQ
47
48unsigned long last_cli_ip;
49EXPORT_SYMBOL(last_cli_ip);
50
51#endif
52
Isaku Yamahata00d21d82008-05-19 22:13:44 +090053#ifdef CONFIG_PARAVIRT
Isaku Yamahataf927da12009-03-04 21:05:40 +090054/* We need to define a real function for sched_clock, to override the
55 weak default version */
56unsigned long long sched_clock(void)
57{
58 return paravirt_sched_clock();
59}
60#endif
61
62#ifdef CONFIG_PARAVIRT
Isaku Yamahata00d21d82008-05-19 22:13:44 +090063static void
Magnus Damm17622332010-02-02 14:41:39 -080064paravirt_clocksource_resume(struct clocksource *cs)
Isaku Yamahata00d21d82008-05-19 22:13:44 +090065{
66 if (pv_time_ops.clocksource_resume)
67 pv_time_ops.clocksource_resume();
68}
69#endif
70
Tony Luck0aa366f2007-07-20 11:22:30 -070071static struct clocksource clocksource_itc = {
Li Zefan3eb05672008-02-08 04:19:25 -080072 .name = "itc",
73 .rating = 350,
74 .read = itc_get_cycles,
75 .mask = CLOCKSOURCE_MASK(64),
Li Zefan3eb05672008-02-08 04:19:25 -080076 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Isaku Yamahata00d21d82008-05-19 22:13:44 +090077#ifdef CONFIG_PARAVIRT
78 .resume = paravirt_clocksource_resume,
79#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
Tony Luck0aa366f2007-07-20 11:22:30 -070081static struct clocksource *itc_clocksource;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Hidetoshi Setob64f34c2008-01-29 14:27:30 +090083#ifdef CONFIG_VIRT_CPU_ACCOUNTING
84
85#include <linux/kernel_stat.h>
86
87extern cputime_t cycle_to_cputime(u64 cyc);
88
89/*
90 * Called from the context switch with interrupts disabled, to charge all
91 * accumulated times to the current process, and to prepare accounting on
92 * the next process.
93 */
94void ia64_account_on_switch(struct task_struct *prev, struct task_struct *next)
95{
96 struct thread_info *pi = task_thread_info(prev);
97 struct thread_info *ni = task_thread_info(next);
98 cputime_t delta_stime, delta_utime;
99 __u64 now;
100
101 now = ia64_get_itc();
102
103 delta_stime = cycle_to_cputime(pi->ac_stime + (now - pi->ac_stamp));
Martin Schwidefsky79741dd2008-12-31 15:11:38 +0100104 if (idle_task(smp_processor_id()) != prev)
105 account_system_time(prev, 0, delta_stime, delta_stime);
106 else
107 account_idle_time(delta_stime);
Hidetoshi Setob64f34c2008-01-29 14:27:30 +0900108
109 if (pi->ac_utime) {
110 delta_utime = cycle_to_cputime(pi->ac_utime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +0100111 account_user_time(prev, delta_utime, delta_utime);
Hidetoshi Setob64f34c2008-01-29 14:27:30 +0900112 }
113
114 pi->ac_stamp = ni->ac_stamp = now;
115 ni->ac_stime = ni->ac_utime = 0;
116}
117
118/*
119 * Account time for a transition between system, hard irq or soft irq state.
120 * Note that this function is called with interrupts enabled.
121 */
122void account_system_vtime(struct task_struct *tsk)
123{
124 struct thread_info *ti = task_thread_info(tsk);
125 unsigned long flags;
126 cputime_t delta_stime;
127 __u64 now;
128
129 local_irq_save(flags);
130
131 now = ia64_get_itc();
132
133 delta_stime = cycle_to_cputime(ti->ac_stime + (now - ti->ac_stamp));
Martin Schwidefsky79741dd2008-12-31 15:11:38 +0100134 if (irq_count() || idle_task(smp_processor_id()) != tsk)
135 account_system_time(tsk, 0, delta_stime, delta_stime);
136 else
137 account_idle_time(delta_stime);
Hidetoshi Setob64f34c2008-01-29 14:27:30 +0900138 ti->ac_stime = 0;
139
140 ti->ac_stamp = now;
141
142 local_irq_restore(flags);
143}
Doug Chapman3a677d22008-06-30 15:06:48 -0700144EXPORT_SYMBOL_GPL(account_system_vtime);
Hidetoshi Setob64f34c2008-01-29 14:27:30 +0900145
146/*
147 * Called from the timer interrupt handler to charge accumulated user time
148 * to the current process. Must be called with interrupts disabled.
149 */
150void account_process_tick(struct task_struct *p, int user_tick)
151{
152 struct thread_info *ti = task_thread_info(p);
153 cputime_t delta_utime;
154
155 if (ti->ac_utime) {
156 delta_utime = cycle_to_cputime(ti->ac_utime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +0100157 account_user_time(p, delta_utime, delta_utime);
Hidetoshi Setob64f34c2008-01-29 14:27:30 +0900158 ti->ac_utime = 0;
159 }
160}
161
162#endif /* CONFIG_VIRT_CPU_ACCOUNTING */
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100165timer_interrupt (int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned long new_itm;
168
Joe Perchese7d28252010-12-09 23:16:55 -0800169 if (cpu_is_offline(smp_processor_id())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return IRQ_HANDLED;
171 }
172
David Howells7d12e782006-10-05 14:55:46 +0100173 platform_timer_interrupt(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 new_itm = local_cpu_data->itm_next;
176
177 if (!time_after(ia64_get_itc(), new_itm))
178 printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
179 ia64_get_itc(), new_itm);
180
David Howells7d12e782006-10-05 14:55:46 +0100181 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Isaku Yamahata00d21d82008-05-19 22:13:44 +0900183 if (paravirt_do_steal_accounting(&new_itm))
184 goto skip_process_time_accounting;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 while (1) {
David Howells7d12e782006-10-05 14:55:46 +0100187 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 new_itm += local_cpu_data->itm_delta;
190
Ashok Rajff741902005-11-11 14:32:40 -0800191 if (smp_processor_id() == time_keeper_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Here we are in the timer irq handler. We have irqs locally
194 * disabled, but we don't know if the timer_bh is running on
195 * another CPU. We need to avoid to SMP race by acquiring the
196 * xtime_lock.
197 */
198 write_seqlock(&xtime_lock);
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700199 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 local_cpu_data->itm_next = new_itm;
201 write_sequnlock(&xtime_lock);
202 } else
203 local_cpu_data->itm_next = new_itm;
204
205 if (time_after(new_itm, ia64_get_itc()))
206 break;
Jack Steineraccaddb2006-10-16 12:56:54 -0500207
208 /*
209 * Allow IPIs to interrupt the timer loop.
210 */
211 local_irq_enable();
212 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
Isaku Yamahata00d21d82008-05-19 22:13:44 +0900215skip_process_time_accounting:
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 do {
218 /*
219 * If we're too close to the next clock tick for
220 * comfort, we increase the safety margin by
221 * intentionally dropping the next tick(s). We do NOT
222 * update itm.next because that would force us to call
223 * do_timer() which in turn would let our clock run
224 * too fast (with the potentially devastating effect
225 * of losing monotony of time).
226 */
227 while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
228 new_itm += local_cpu_data->itm_delta;
229 ia64_set_itm(new_itm);
230 /* double check, in case we got hit by a (slow) PMI: */
231 } while (time_after_eq(ia64_get_itc(), new_itm));
232 return IRQ_HANDLED;
233}
234
235/*
236 * Encapsulate access to the itm structure for SMP.
237 */
238void
239ia64_cpu_local_tick (void)
240{
241 int cpu = smp_processor_id();
242 unsigned long shift = 0, delta;
243
244 /* arrange for the cycle counter to generate a timer interrupt: */
245 ia64_set_itv(IA64_TIMER_VECTOR);
246
247 delta = local_cpu_data->itm_delta;
248 /*
249 * Stagger the timer tick for each CPU so they don't occur all at (almost) the
250 * same time:
251 */
252 if (cpu) {
253 unsigned long hi = 1UL << ia64_fls(cpu);
254 shift = (2*(cpu - hi) + 1) * delta/hi/2;
255 }
256 local_cpu_data->itm_next = ia64_get_itc() + delta + shift;
257 ia64_set_itm(local_cpu_data->itm_next);
258}
259
260static int nojitter;
261
262static int __init nojitter_setup(char *str)
263{
264 nojitter = 1;
265 printk("Jitter checking for ITC timers disabled\n");
266 return 1;
267}
268
269__setup("nojitter", nojitter_setup);
270
271
272void __devinit
273ia64_init_itm (void)
274{
275 unsigned long platform_base_freq, itc_freq;
276 struct pal_freq_ratio itc_ratio, proc_ratio;
277 long status, platform_base_drift, itc_drift;
278
279 /*
280 * According to SAL v2.6, we need to use a SAL call to determine the platform base
281 * frequency and then a PAL call to determine the frequency ratio between the ITC
282 * and the base frequency.
283 */
284 status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
285 &platform_base_freq, &platform_base_drift);
286 if (status != 0) {
287 printk(KERN_ERR "SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status));
288 } else {
289 status = ia64_pal_freq_ratios(&proc_ratio, NULL, &itc_ratio);
290 if (status != 0)
291 printk(KERN_ERR "PAL_FREQ_RATIOS failed with status=%ld\n", status);
292 }
293 if (status != 0) {
294 /* invent "random" values */
295 printk(KERN_ERR
296 "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
297 platform_base_freq = 100000000;
298 platform_base_drift = -1; /* no drift info */
299 itc_ratio.num = 3;
300 itc_ratio.den = 1;
301 }
302 if (platform_base_freq < 40000000) {
303 printk(KERN_ERR "Platform base frequency %lu bogus---resetting to 75MHz!\n",
304 platform_base_freq);
305 platform_base_freq = 75000000;
306 platform_base_drift = -1;
307 }
308 if (!proc_ratio.den)
309 proc_ratio.den = 1; /* avoid division by zero */
310 if (!itc_ratio.den)
311 itc_ratio.den = 1; /* avoid division by zero */
312
313 itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
314
315 local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
Tony Luck2ab93912006-03-31 10:28:29 -0800316 printk(KERN_DEBUG "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%u/%u, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 "ITC freq=%lu.%03luMHz", 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 if (platform_base_drift != -1) {
322 itc_drift = platform_base_drift*itc_ratio.num/itc_ratio.den;
323 printk("+/-%ldppm\n", itc_drift);
324 } else {
325 itc_drift = -1;
326 printk("\n");
327 }
328
329 local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
330 local_cpu_data->itc_freq = itc_freq;
331 local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
332 local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
333 + itc_freq/2)/itc_freq;
334
335 if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#ifdef CONFIG_SMP
337 /* On IA64 in an SMP configuration ITCs are never accurately synchronized.
338 * Jitter compensation requires a cmpxchg which may limit
339 * the scalability of the syscalls for retrieving time.
340 * The ITC synchronization is usually successful to within a few
341 * ITC ticks but this is not a sure thing. If you need to improve
342 * timer performance in SMP situations then boot the kernel with the
343 * "nojitter" option. However, doing so may result in time fluctuating (maybe
344 * even going backward) if the ITC offsets between the individual CPUs
345 * are too large.
346 */
Tony Luck0aa366f2007-07-20 11:22:30 -0700347 if (!nojitter)
348 itc_jitter_data.itc_jitter = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#endif
Christoph Lameterb718f912007-08-01 13:49:45 -0700350 } else
351 /*
352 * ITC is drifty and we have not synchronized the ITCs in smpboot.c.
353 * ITC values may fluctuate significantly between processors.
354 * Clock should not be used for hrtimers. Mark itc as only
355 * useful for boot and testing.
356 *
357 * Note that jitter compensation is off! There is no point of
358 * synchronizing ITCs since they may be large differentials
359 * that change over time.
360 *
361 * The only way to fix this would be to repeatedly sync the
362 * ITCs. Until that time we have to avoid ITC.
363 */
364 clocksource_itc.rating = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Isaku Yamahata00d21d82008-05-19 22:13:44 +0900366 paravirt_init_missing_ticks_accounting(smp_processor_id());
367
368 /* avoid softlock up message when cpu is unplug and plugged again. */
369 touch_softlockup_watchdog();
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* Setup the CPU local timer tick */
372 ia64_cpu_local_tick();
Tony Luck0aa366f2007-07-20 11:22:30 -0700373
374 if (!itc_clocksource) {
John Stultzd60c3042010-04-26 20:20:47 -0700375 clocksource_register_hz(&clocksource_itc,
376 local_cpu_data->itc_freq);
Tony Luck0aa366f2007-07-20 11:22:30 -0700377 itc_clocksource = &clocksource_itc;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Magnus Damm8e196082009-04-21 12:24:00 -0700381static cycle_t itc_get_cycles(struct clocksource *cs)
Tony Luck0aa366f2007-07-20 11:22:30 -0700382{
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -0700383 unsigned long lcycle, now, ret;
Tony Luck0aa366f2007-07-20 11:22:30 -0700384
385 if (!itc_jitter_data.itc_jitter)
386 return get_cycles();
387
388 lcycle = itc_jitter_data.itc_lastcycle;
389 now = get_cycles();
390 if (lcycle && time_after(lcycle, now))
391 return lcycle;
392
393 /*
394 * Keep track of the last timer value returned.
395 * In an SMP environment, you could lose out in contention of
396 * cmpxchg. If so, your cmpxchg returns new value which the
397 * winner of contention updated to. Use the new value instead.
398 */
399 ret = cmpxchg(&itc_jitter_data.itc_lastcycle, lcycle, now);
400 if (unlikely(ret != lcycle))
401 return ret;
402
403 return now;
404}
405
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static struct irqaction timer_irqaction = {
408 .handler = timer_interrupt,
Bernhard Walled217c262007-05-08 00:35:31 -0700409 .flags = IRQF_DISABLED | IRQF_IRQPOLL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 .name = "timer"
411};
412
dann frazier5e3fd9e2009-03-31 15:24:48 -0700413static struct platform_device rtc_efi_dev = {
414 .name = "rtc-efi",
415 .id = -1,
416};
417
418static int __init rtc_init(void)
419{
420 if (platform_device_register(&rtc_efi_dev) < 0)
421 printk(KERN_ERR "unable to register rtc device...\n");
422
423 /* not necessarily an error */
424 return 0;
425}
426module_init(rtc_init);
427
John Stultz6ffdc572010-03-03 19:57:22 -0800428void read_persistent_clock(struct timespec *ts)
429{
430 efi_gettimeofday(ts);
431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433void __init
434time_init (void)
435{
436 register_percpu_irq(IA64_TIMER_VECTOR, &timer_irqaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 ia64_init_itm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
John Hawkesf5899b52005-12-16 10:00:24 -0800439
hawkes@sgi.comdefbb2c2006-02-14 10:40:17 -0800440/*
441 * Generic udelay assumes that if preemption is allowed and the thread
442 * migrates to another CPU, that the ITC values are synchronized across
443 * all CPUs.
444 */
445static void
446ia64_itc_udelay (unsigned long usecs)
447{
448 unsigned long start = ia64_get_itc();
449 unsigned long end = start + usecs*local_cpu_data->cyc_per_usec;
450
451 while (time_before(ia64_get_itc(), end))
452 cpu_relax();
453}
454
455void (*ia64_udelay)(unsigned long usecs) = &ia64_itc_udelay;
John Hawkesf5899b52005-12-16 10:00:24 -0800456
457void
458udelay (unsigned long usecs)
459{
hawkes@sgi.comdefbb2c2006-02-14 10:40:17 -0800460 (*ia64_udelay)(usecs);
John Hawkesf5899b52005-12-16 10:00:24 -0800461}
462EXPORT_SYMBOL(udelay);
Tony Luckd6e56a22006-02-07 15:25:57 -0800463
Tony Breeds2c622142007-10-18 03:04:57 -0700464/* IA64 doesn't cache the timezone */
465void update_vsyscall_tz(void)
466{
467}
468
John Stultz76158562010-07-13 17:56:23 -0700469void update_vsyscall(struct timespec *wall, struct timespec *wtm,
470 struct clocksource *c, u32 mult)
Tony Luck0aa366f2007-07-20 11:22:30 -0700471{
472 unsigned long flags;
473
474 write_seqlock_irqsave(&fsyscall_gtod_data.lock, flags);
475
476 /* copy fsyscall clock data */
477 fsyscall_gtod_data.clk_mask = c->mask;
Lin Ming0696b712009-11-17 13:49:50 +0800478 fsyscall_gtod_data.clk_mult = mult;
Tony Luck0aa366f2007-07-20 11:22:30 -0700479 fsyscall_gtod_data.clk_shift = c->shift;
480 fsyscall_gtod_data.clk_fsys_mmio = c->fsys_mmio;
481 fsyscall_gtod_data.clk_cycle_last = c->cycle_last;
482
483 /* copy kernel time structures */
484 fsyscall_gtod_data.wall_time.tv_sec = wall->tv_sec;
485 fsyscall_gtod_data.wall_time.tv_nsec = wall->tv_nsec;
John Stultz76158562010-07-13 17:56:23 -0700486 fsyscall_gtod_data.monotonic_time.tv_sec = wtm->tv_sec
Tony Luck0aa366f2007-07-20 11:22:30 -0700487 + wall->tv_sec;
John Stultz76158562010-07-13 17:56:23 -0700488 fsyscall_gtod_data.monotonic_time.tv_nsec = wtm->tv_nsec
Tony Luck0aa366f2007-07-20 11:22:30 -0700489 + wall->tv_nsec;
490
491 /* normalize */
492 while (fsyscall_gtod_data.monotonic_time.tv_nsec >= NSEC_PER_SEC) {
493 fsyscall_gtod_data.monotonic_time.tv_nsec -= NSEC_PER_SEC;
494 fsyscall_gtod_data.monotonic_time.tv_sec++;
495 }
496
497 write_sequnlock_irqrestore(&fsyscall_gtod_data.lock, flags);
498}
499