Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / arch / s390 / kernel / time.c
1 /*
2  *  arch/s390/kernel/time.c
3  *    Time of day based timer functions.
4  *
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)
10  *
11  *  Derived from "arch/i386/kernel/time.c"
12  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
13  */
14
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/kernel.h>
19 #include <linux/param.h>
20 #include <linux/string.h>
21 #include <linux/mm.h>
22 #include <linux/interrupt.h>
23 #include <linux/time.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/smp.h>
27 #include <linux/types.h>
28 #include <linux/profile.h>
29 #include <linux/timex.h>
30 #include <linux/notifier.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/delay.h>
34 #include <asm/s390_ext.h>
35 #include <asm/div64.h>
36 #include <asm/irq.h>
37 #include <asm/timer.h>
38
39 /* change this if you have some constant time drift */
40 #define USECS_PER_JIFFY     ((unsigned long) 1000000/HZ)
41 #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
42
43 /*
44  * Create a small time difference between the timer interrupts
45  * on the different cpus to avoid lock contention.
46  */
47 #define CPU_DEVIATION       (smp_processor_id() << 12)
48
49 #define TICK_SIZE tick
50
51 static ext_int_info_t ext_int_info_cc;
52 static u64 init_timer_cc;
53 static u64 jiffies_timer_cc;
54 static u64 xtime_cc;
55
56 /*
57  * Scheduler clock - returns current time in nanosec units.
58  */
59 unsigned long long sched_clock(void)
60 {
61         return ((get_clock() - jiffies_timer_cc) * 125) >> 9;
62 }
63
64 /*
65  * Monotonic_clock - returns # of nanoseconds passed since time_init()
66  */
67 unsigned long long monotonic_clock(void)
68 {
69         return sched_clock();
70 }
71 EXPORT_SYMBOL(monotonic_clock);
72
73 void tod_to_timeval(__u64 todval, struct timespec *xtime)
74 {
75         unsigned long long sec;
76
77         sec = todval >> 12;
78         do_div(sec, 1000000);
79         xtime->tv_sec = sec;
80         todval -= (sec * 1000000) << 12;
81         xtime->tv_nsec = ((todval * 1000) >> 12);
82 }
83
84 static inline unsigned long do_gettimeoffset(void) 
85 {
86         __u64 now;
87
88         now = (get_clock() - jiffies_timer_cc) >> 12;
89         now -= (__u64) jiffies * USECS_PER_JIFFY;
90         return (unsigned long) now;
91 }
92
93 /*
94  * This version of gettimeofday has microsecond resolution.
95  */
96 void do_gettimeofday(struct timeval *tv)
97 {
98         unsigned long flags;
99         unsigned long seq;
100         unsigned long usec, sec;
101
102         do {
103                 seq = read_seqbegin_irqsave(&xtime_lock, flags);
104
105                 sec = xtime.tv_sec;
106                 usec = xtime.tv_nsec / 1000 + do_gettimeoffset();
107         } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
108
109         while (usec >= 1000000) {
110                 usec -= 1000000;
111                 sec++;
112         }
113
114         tv->tv_sec = sec;
115         tv->tv_usec = usec;
116 }
117
118 EXPORT_SYMBOL(do_gettimeofday);
119
120 int do_settimeofday(struct timespec *tv)
121 {
122         time_t wtm_sec, sec = tv->tv_sec;
123         long wtm_nsec, nsec = tv->tv_nsec;
124
125         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
126                 return -EINVAL;
127
128         write_seqlock_irq(&xtime_lock);
129         /* This is revolting. We need to set the xtime.tv_nsec
130          * correctly. However, the value in this location is
131          * is value at the last tick.
132          * Discover what correction gettimeofday
133          * would have done, and then undo it!
134          */
135         nsec -= do_gettimeoffset() * 1000;
136
137         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
138         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
139
140         set_normalized_timespec(&xtime, sec, nsec);
141         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
142
143         ntp_clear();
144         write_sequnlock_irq(&xtime_lock);
145         clock_was_set();
146         return 0;
147 }
148
149 EXPORT_SYMBOL(do_settimeofday);
150
151
152 #ifdef CONFIG_PROFILING
153 #define s390_do_profile(regs)   profile_tick(CPU_PROFILING, regs)
154 #else
155 #define s390_do_profile(regs)  do { ; } while(0)
156 #endif /* CONFIG_PROFILING */
157
158
159 /*
160  * timer_interrupt() needs to keep up the real-time clock,
161  * as well as call the "do_timer()" routine every clocktick
162  */
163 void account_ticks(struct pt_regs *regs)
164 {
165         __u64 tmp;
166         __u32 ticks;
167
168         /* Calculate how many ticks have passed. */
169         if (S390_lowcore.int_clock < S390_lowcore.jiffy_timer) {
170                 /*
171                  * We have to program the clock comparator even if
172                  * no tick has passed. That happens if e.g. an i/o
173                  * interrupt wakes up an idle processor that has
174                  * switched off its hz timer.
175                  */
176                 tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
177                 asm volatile ("SCKC %0" : : "m" (tmp));
178                 return;
179         }
180         tmp = S390_lowcore.int_clock - S390_lowcore.jiffy_timer;
181         if (tmp >= 2*CLK_TICKS_PER_JIFFY) {  /* more than two ticks ? */
182                 ticks = __div(tmp, CLK_TICKS_PER_JIFFY) + 1;
183                 S390_lowcore.jiffy_timer +=
184                         CLK_TICKS_PER_JIFFY * (__u64) ticks;
185         } else if (tmp >= CLK_TICKS_PER_JIFFY) {
186                 ticks = 2;
187                 S390_lowcore.jiffy_timer += 2*CLK_TICKS_PER_JIFFY;
188         } else {
189                 ticks = 1;
190                 S390_lowcore.jiffy_timer += CLK_TICKS_PER_JIFFY;
191         }
192
193         /* set clock comparator for next tick */
194         tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
195         asm volatile ("SCKC %0" : : "m" (tmp));
196
197 #ifdef CONFIG_SMP
198         /*
199          * Do not rely on the boot cpu to do the calls to do_timer.
200          * Spread it over all cpus instead.
201          */
202         write_seqlock(&xtime_lock);
203         if (S390_lowcore.jiffy_timer > xtime_cc) {
204                 __u32 xticks;
205                 tmp = S390_lowcore.jiffy_timer - xtime_cc;
206                 if (tmp >= 2*CLK_TICKS_PER_JIFFY) {
207                         xticks = __div(tmp, CLK_TICKS_PER_JIFFY);
208                         xtime_cc += (__u64) xticks * CLK_TICKS_PER_JIFFY;
209                 } else {
210                         xticks = 1;
211                         xtime_cc += CLK_TICKS_PER_JIFFY;
212                 }
213                 do_timer(xticks);
214         }
215         write_sequnlock(&xtime_lock);
216 #else
217         do_timer(ticks);
218 #endif
219
220 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
221         account_tick_vtime(current);
222 #else
223         while (ticks--)
224                 update_process_times(user_mode(regs));
225 #endif
226
227         s390_do_profile(regs);
228 }
229
230 #ifdef CONFIG_NO_IDLE_HZ
231
232 #ifdef CONFIG_NO_IDLE_HZ_INIT
233 int sysctl_hz_timer = 0;
234 #else
235 int sysctl_hz_timer = 1;
236 #endif
237
238 /*
239  * Stop the HZ tick on the current CPU.
240  * Only cpu_idle may call this function.
241  */
242 static inline void stop_hz_timer(void)
243 {
244         unsigned long flags;
245         unsigned long seq, next;
246         __u64 timer, todval;
247         int cpu = smp_processor_id();
248
249         if (sysctl_hz_timer != 0)
250                 return;
251
252         cpu_set(cpu, nohz_cpu_mask);
253
254         /*
255          * Leave the clock comparator set up for the next timer
256          * tick if either rcu or a softirq is pending.
257          */
258         if (rcu_needs_cpu(cpu) || local_softirq_pending()) {
259                 cpu_clear(cpu, nohz_cpu_mask);
260                 return;
261         }
262
263         /*
264          * This cpu is going really idle. Set up the clock comparator
265          * for the next event.
266          */
267         next = next_timer_interrupt();
268         do {
269                 seq = read_seqbegin_irqsave(&xtime_lock, flags);
270                 timer = ((__u64) next) - ((__u64) jiffies) + jiffies_64;
271         } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
272         todval = -1ULL;
273         /* Be careful about overflows. */
274         if (timer < (-1ULL / CLK_TICKS_PER_JIFFY)) {
275                 timer = jiffies_timer_cc + timer * CLK_TICKS_PER_JIFFY;
276                 if (timer >= jiffies_timer_cc)
277                         todval = timer;
278         }
279         asm volatile ("SCKC %0" : : "m" (todval));
280 }
281
282 /*
283  * Start the HZ tick on the current CPU.
284  * Only cpu_idle may call this function.
285  */
286 static inline void start_hz_timer(void)
287 {
288         if (!cpu_isset(smp_processor_id(), nohz_cpu_mask))
289                 return;
290         account_ticks(task_pt_regs(current));
291         cpu_clear(smp_processor_id(), nohz_cpu_mask);
292 }
293
294 static int nohz_idle_notify(struct notifier_block *self,
295                             unsigned long action, void *hcpu)
296 {
297         switch (action) {
298         case CPU_IDLE:
299                 stop_hz_timer();
300                 break;
301         case CPU_NOT_IDLE:
302                 start_hz_timer();
303                 break;
304         }
305         return NOTIFY_OK;
306 }
307
308 static struct notifier_block nohz_idle_nb = {
309         .notifier_call = nohz_idle_notify,
310 };
311
312 void __init nohz_init(void)
313 {
314         if (register_idle_notifier(&nohz_idle_nb))
315                 panic("Couldn't register idle notifier");
316 }
317
318 #endif
319
320 /*
321  * Start the clock comparator on the current CPU.
322  */
323 void init_cpu_timer(void)
324 {
325         unsigned long cr0;
326         __u64 timer;
327
328         timer = jiffies_timer_cc + jiffies_64 * CLK_TICKS_PER_JIFFY;
329         S390_lowcore.jiffy_timer = timer + CLK_TICKS_PER_JIFFY;
330         timer += CLK_TICKS_PER_JIFFY + CPU_DEVIATION;
331         asm volatile ("SCKC %0" : : "m" (timer));
332         /* allow clock comparator timer interrupt */
333         __ctl_store(cr0, 0, 0);
334         cr0 |= 0x800;
335         __ctl_load(cr0, 0, 0);
336 }
337
338 extern void vtime_init(void);
339
340 /*
341  * Initialize the TOD clock and the CPU timer of
342  * the boot cpu.
343  */
344 void __init time_init(void)
345 {
346         __u64 set_time_cc;
347         int cc;
348
349         /* kick the TOD clock */
350         asm volatile(
351                 "       stck    0(%2)\n"
352                 "       ipm     %0\n"
353                 "       srl     %0,28"
354                 : "=d" (cc), "=m" (init_timer_cc)
355                 : "a" (&init_timer_cc) : "cc");
356         switch (cc) {
357         case 0: /* clock in set state: all is fine */
358                 break;
359         case 1: /* clock in non-set state: FIXME */
360                 printk("time_init: TOD clock in non-set state\n");
361                 break;
362         case 2: /* clock in error state: FIXME */
363                 printk("time_init: TOD clock in error state\n");
364                 break;
365         case 3: /* clock in stopped or not-operational state: FIXME */
366                 printk("time_init: TOD clock stopped/non-operational\n");
367                 break;
368         }
369         jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;
370
371         /* set xtime */
372         xtime_cc = init_timer_cc + CLK_TICKS_PER_JIFFY;
373         set_time_cc = init_timer_cc - 0x8126d60e46000000LL +
374                 (0x3c26700LL*1000000*4096);
375         tod_to_timeval(set_time_cc, &xtime);
376         set_normalized_timespec(&wall_to_monotonic,
377                                 -xtime.tv_sec, -xtime.tv_nsec);
378
379         /* request the clock comparator external interrupt */
380         if (register_early_external_interrupt(0x1004, NULL,
381                                               &ext_int_info_cc) != 0)
382                 panic("Couldn't request external interrupt 0x1004");
383
384         init_cpu_timer();
385
386 #ifdef CONFIG_NO_IDLE_HZ
387         nohz_init();
388 #endif
389
390 #ifdef CONFIG_VIRT_TIMER
391         vtime_init();
392 #endif
393 }
394