initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / s390 / kernel / time.c
blob8f279421646f6da5809c9e7fd6b32c076b6e4551
1 /*
2 * arch/s390/kernel/time.c
3 * Time of day based timer functions.
5 * S390 version
6 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Hartmut Penner (hp@de.ibm.com),
8 * Martin Schwidefsky (schwidefsky@de.ibm.com),
9 * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
11 * Derived from "arch/i386/kernel/time.c"
12 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
15 #include <linux/config.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/param.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/interrupt.h>
24 #include <linux/time.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/smp.h>
28 #include <linux/types.h>
29 #include <linux/profile.h>
30 #include <linux/timex.h>
31 #include <linux/notifier.h>
33 #include <asm/uaccess.h>
34 #include <asm/delay.h>
35 #include <asm/s390_ext.h>
36 #include <asm/div64.h>
37 #include <asm/irq.h>
38 #include <asm/timer.h>
40 /* change this if you have some constant time drift */
41 #define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
42 #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
45 * Create a small time difference between the timer interrupts
46 * on the different cpus to avoid lock contention.
48 #define CPU_DEVIATION (smp_processor_id() << 12)
50 #define TICK_SIZE tick
52 u64 jiffies_64 = INITIAL_JIFFIES;
54 EXPORT_SYMBOL(jiffies_64);
56 static ext_int_info_t ext_int_info_cc;
57 static u64 init_timer_cc;
58 static u64 jiffies_timer_cc;
59 static u64 xtime_cc;
61 extern unsigned long wall_jiffies;
64 * Scheduler clock - returns current time in nanosec units.
66 unsigned long long sched_clock(void)
68 return ((get_clock() - jiffies_timer_cc) * 1000) >> 12;
71 void tod_to_timeval(__u64 todval, struct timespec *xtime)
73 unsigned long long sec;
75 sec = todval >> 12;
76 do_div(sec, 1000000);
77 xtime->tv_sec = sec;
78 todval -= (sec * 1000000) << 12;
79 xtime->tv_nsec = ((todval * 1000) >> 12);
82 static inline unsigned long do_gettimeoffset(void)
84 __u64 now;
86 now = (get_clock() - jiffies_timer_cc) >> 12;
87 /* We require the offset from the latest update of xtime */
88 now -= (__u64) wall_jiffies*USECS_PER_JIFFY;
89 return (unsigned long) now;
93 * This version of gettimeofday has microsecond resolution.
95 void do_gettimeofday(struct timeval *tv)
97 unsigned long flags;
98 unsigned long seq;
99 unsigned long usec, sec;
101 do {
102 seq = read_seqbegin_irqsave(&xtime_lock, flags);
104 sec = xtime.tv_sec;
105 usec = xtime.tv_nsec / 1000 + do_gettimeoffset();
106 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
108 while (usec >= 1000000) {
109 usec -= 1000000;
110 sec++;
113 tv->tv_sec = sec;
114 tv->tv_usec = usec;
117 EXPORT_SYMBOL(do_gettimeofday);
119 int do_settimeofday(struct timespec *tv)
121 time_t wtm_sec, sec = tv->tv_sec;
122 long wtm_nsec, nsec = tv->tv_nsec;
124 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
125 return -EINVAL;
127 write_seqlock_irq(&xtime_lock);
128 /* This is revolting. We need to set the xtime.tv_nsec
129 * correctly. However, the value in this location is
130 * is value at the last tick.
131 * Discover what correction gettimeofday
132 * would have done, and then undo it!
134 nsec -= do_gettimeoffset() * 1000;
136 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
137 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
139 set_normalized_timespec(&xtime, sec, nsec);
140 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
142 time_adjust = 0; /* stop active adjtime() */
143 time_status |= STA_UNSYNC;
144 time_maxerror = NTP_PHASE_LIMIT;
145 time_esterror = NTP_PHASE_LIMIT;
146 write_sequnlock_irq(&xtime_lock);
147 clock_was_set();
148 return 0;
151 EXPORT_SYMBOL(do_settimeofday);
153 #ifndef CONFIG_ARCH_S390X
155 static inline __u32
156 __calculate_ticks(__u64 elapsed)
158 register_pair rp;
160 rp.pair = elapsed >> 1;
161 asm ("dr %0,%1" : "+d" (rp) : "d" (CLK_TICKS_PER_JIFFY >> 1));
162 return rp.subreg.odd;
165 #else /* CONFIG_ARCH_S390X */
167 static inline __u32
168 __calculate_ticks(__u64 elapsed)
170 return elapsed / CLK_TICKS_PER_JIFFY;
173 #endif /* CONFIG_ARCH_S390X */
176 #ifdef CONFIG_PROFILING
177 #define s390_do_profile(regs) profile_tick(CPU_PROFILING, regs)
178 #else
179 #define s390_do_profile(regs) do { ; } while(0)
180 #endif /* CONFIG_PROFILING */
184 * timer_interrupt() needs to keep up the real-time clock,
185 * as well as call the "do_timer()" routine every clocktick
187 void account_ticks(struct pt_regs *regs)
189 __u64 tmp;
190 __u32 ticks;
192 /* Calculate how many ticks have passed. */
193 if (S390_lowcore.int_clock < S390_lowcore.jiffy_timer)
194 return;
195 tmp = S390_lowcore.int_clock - S390_lowcore.jiffy_timer;
196 if (tmp >= 2*CLK_TICKS_PER_JIFFY) { /* more than two ticks ? */
197 ticks = __calculate_ticks(tmp) + 1;
198 S390_lowcore.jiffy_timer +=
199 CLK_TICKS_PER_JIFFY * (__u64) ticks;
200 } else if (tmp >= CLK_TICKS_PER_JIFFY) {
201 ticks = 2;
202 S390_lowcore.jiffy_timer += 2*CLK_TICKS_PER_JIFFY;
203 } else {
204 ticks = 1;
205 S390_lowcore.jiffy_timer += CLK_TICKS_PER_JIFFY;
208 /* set clock comparator for next tick */
209 tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
210 asm volatile ("SCKC %0" : : "m" (tmp));
212 #ifdef CONFIG_SMP
214 * Do not rely on the boot cpu to do the calls to do_timer.
215 * Spread it over all cpus instead.
217 write_seqlock(&xtime_lock);
218 if (S390_lowcore.jiffy_timer > xtime_cc) {
219 __u32 xticks;
221 tmp = S390_lowcore.jiffy_timer - xtime_cc;
222 if (tmp >= 2*CLK_TICKS_PER_JIFFY) {
223 xticks = __calculate_ticks(tmp);
224 xtime_cc += (__u64) xticks * CLK_TICKS_PER_JIFFY;
225 } else {
226 xticks = 1;
227 xtime_cc += CLK_TICKS_PER_JIFFY;
229 while (xticks--)
230 do_timer(regs);
232 write_sequnlock(&xtime_lock);
233 while (ticks--)
234 update_process_times(user_mode(regs));
235 #else
236 while (ticks--)
237 do_timer(regs);
238 #endif
239 s390_do_profile(regs);
242 #ifdef CONFIG_NO_IDLE_HZ
244 #ifdef CONFIG_NO_IDLE_HZ_INIT
245 int sysctl_hz_timer = 0;
246 #else
247 int sysctl_hz_timer = 1;
248 #endif
251 * Stop the HZ tick on the current CPU.
252 * Only cpu_idle may call this function.
254 static inline void stop_hz_timer(void)
256 __u64 timer;
258 if (sysctl_hz_timer != 0)
259 return;
262 * Leave the clock comparator set up for the next timer
263 * tick if either rcu or a softirq is pending.
265 if (rcu_pending(smp_processor_id()) || local_softirq_pending())
266 return;
269 * This cpu is going really idle. Set up the clock comparator
270 * for the next event.
272 cpu_set(smp_processor_id(), nohz_cpu_mask);
273 timer = (__u64) (next_timer_interrupt() - jiffies) + jiffies_64;
274 timer = jiffies_timer_cc + timer * CLK_TICKS_PER_JIFFY;
275 asm volatile ("SCKC %0" : : "m" (timer));
279 * Start the HZ tick on the current CPU.
280 * Only cpu_idle may call this function.
282 static inline void start_hz_timer(void)
284 if (!cpu_isset(smp_processor_id(), nohz_cpu_mask))
285 return;
286 account_ticks(__KSTK_PTREGS(current));
287 cpu_clear(smp_processor_id(), nohz_cpu_mask);
290 static int nohz_idle_notify(struct notifier_block *self,
291 unsigned long action, void *hcpu)
293 switch (action) {
294 case CPU_IDLE:
295 stop_hz_timer();
296 break;
297 case CPU_NOT_IDLE:
298 start_hz_timer();
299 break;
301 return NOTIFY_OK;
304 static struct notifier_block nohz_idle_nb = {
305 .notifier_call = nohz_idle_notify,
308 void __init nohz_init(void)
310 if (register_idle_notifier(&nohz_idle_nb))
311 panic("Couldn't register idle notifier");
314 #endif
317 * Start the clock comparator on the current CPU.
319 void init_cpu_timer(void)
321 unsigned long cr0;
322 __u64 timer;
324 timer = jiffies_timer_cc + jiffies_64 * CLK_TICKS_PER_JIFFY;
325 S390_lowcore.jiffy_timer = timer + CLK_TICKS_PER_JIFFY;
326 timer += CLK_TICKS_PER_JIFFY + CPU_DEVIATION;
327 asm volatile ("SCKC %0" : : "m" (timer));
328 /* allow clock comparator timer interrupt */
329 __ctl_store(cr0, 0, 0);
330 cr0 |= 0x800;
331 __ctl_load(cr0, 0, 0);
334 extern void vtime_init(void);
337 * Initialize the TOD clock and the CPU timer of
338 * the boot cpu.
340 void __init time_init(void)
342 __u64 set_time_cc;
343 int cc;
345 /* kick the TOD clock */
346 asm volatile ("STCK 0(%1)\n\t"
347 "IPM %0\n\t"
348 "SRL %0,28" : "=r" (cc) : "a" (&init_timer_cc)
349 : "memory", "cc");
350 switch (cc) {
351 case 0: /* clock in set state: all is fine */
352 break;
353 case 1: /* clock in non-set state: FIXME */
354 printk("time_init: TOD clock in non-set state\n");
355 break;
356 case 2: /* clock in error state: FIXME */
357 printk("time_init: TOD clock in error state\n");
358 break;
359 case 3: /* clock in stopped or not-operational state: FIXME */
360 printk("time_init: TOD clock stopped/non-operational\n");
361 break;
363 jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;
365 /* set xtime */
366 xtime_cc = init_timer_cc + CLK_TICKS_PER_JIFFY;
367 set_time_cc = init_timer_cc - 0x8126d60e46000000LL +
368 (0x3c26700LL*1000000*4096);
369 tod_to_timeval(set_time_cc, &xtime);
370 set_normalized_timespec(&wall_to_monotonic,
371 -xtime.tv_sec, -xtime.tv_nsec);
373 /* request the clock comparator external interrupt */
374 if (register_early_external_interrupt(0x1004, 0,
375 &ext_int_info_cc) != 0)
376 panic("Couldn't request external interrupt 0x1004");
378 init_cpu_timer();
380 #ifdef CONFIG_NO_IDLE_HZ
381 nohz_init();
382 #endif
384 #ifdef CONFIG_VIRT_TIMER
385 vtime_init();
386 #endif