blob: 2c8c690688cba51ebd917ae8e6c5e19fb6855812 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Heiko Carstens5a489b92006-10-06 16:38:35 +020037#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/timer.h>
39
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)
43
44/*
45 * Create a small time difference between the timer interrupts
46 * on the different cpus to avoid lock contention.
47 */
48#define CPU_DEVIATION (smp_processor_id() << 12)
49
50#define TICK_SIZE tick
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static ext_int_info_t ext_int_info_cc;
53static u64 init_timer_cc;
54static u64 jiffies_timer_cc;
55static u64 xtime_cc;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Scheduler clock - returns current time in nanosec units.
59 */
60unsigned long long sched_clock(void)
61{
Jan Glauber9dbafa52006-02-01 03:06:32 -080062 return ((get_clock() - jiffies_timer_cc) * 125) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Jan Glauber32f65f22006-02-01 03:06:33 -080065/*
66 * Monotonic_clock - returns # of nanoseconds passed since time_init()
67 */
68unsigned long long monotonic_clock(void)
69{
70 return sched_clock();
71}
72EXPORT_SYMBOL(monotonic_clock);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074void tod_to_timeval(__u64 todval, struct timespec *xtime)
75{
76 unsigned long long sec;
77
78 sec = todval >> 12;
79 do_div(sec, 1000000);
80 xtime->tv_sec = sec;
81 todval -= (sec * 1000000) << 12;
82 xtime->tv_nsec = ((todval * 1000) >> 12);
83}
84
85static inline unsigned long do_gettimeoffset(void)
86{
87 __u64 now;
88
Atsushi Nemoto8ef38602006-09-30 23:28:31 -070089 now = (get_clock() - jiffies_timer_cc) >> 12;
90 now -= (__u64) jiffies * USECS_PER_JIFFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return (unsigned long) now;
92}
93
94/*
95 * This version of gettimeofday has microsecond resolution.
96 */
97void do_gettimeofday(struct timeval *tv)
98{
99 unsigned long flags;
100 unsigned long seq;
101 unsigned long usec, sec;
102
103 do {
104 seq = read_seqbegin_irqsave(&xtime_lock, flags);
105
106 sec = xtime.tv_sec;
107 usec = xtime.tv_nsec / 1000 + do_gettimeoffset();
108 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
109
110 while (usec >= 1000000) {
111 usec -= 1000000;
112 sec++;
113 }
114
115 tv->tv_sec = sec;
116 tv->tv_usec = usec;
117}
118
119EXPORT_SYMBOL(do_gettimeofday);
120
121int do_settimeofday(struct timespec *tv)
122{
123 time_t wtm_sec, sec = tv->tv_sec;
124 long wtm_nsec, nsec = tv->tv_nsec;
125
126 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
127 return -EINVAL;
128
129 write_seqlock_irq(&xtime_lock);
130 /* This is revolting. We need to set the xtime.tv_nsec
131 * correctly. However, the value in this location is
132 * is value at the last tick.
133 * Discover what correction gettimeofday
134 * would have done, and then undo it!
135 */
136 nsec -= do_gettimeoffset() * 1000;
137
138 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
139 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
140
141 set_normalized_timespec(&xtime, sec, nsec);
142 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
143
john stultzb149ee22005-09-06 15:17:46 -0700144 ntp_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 write_sequnlock_irq(&xtime_lock);
146 clock_was_set();
147 return 0;
148}
149
150EXPORT_SYMBOL(do_settimeofday);
151
152
153#ifdef CONFIG_PROFILING
Heiko Carstens5a489b92006-10-06 16:38:35 +0200154#define s390_do_profile() profile_tick(CPU_PROFILING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#else
Heiko Carstens5a489b92006-10-06 16:38:35 +0200156#define s390_do_profile() do { ; } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#endif /* CONFIG_PROFILING */
158
159
160/*
161 * timer_interrupt() needs to keep up the real-time clock,
162 * as well as call the "do_timer()" routine every clocktick
163 */
Heiko Carstens5a489b92006-10-06 16:38:35 +0200164void account_ticks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 __u64 tmp;
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700167 __u32 ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* Calculate how many ticks have passed. */
170 if (S390_lowcore.int_clock < S390_lowcore.jiffy_timer) {
171 /*
172 * We have to program the clock comparator even if
173 * no tick has passed. That happens if e.g. an i/o
174 * interrupt wakes up an idle processor that has
175 * switched off its hz timer.
176 */
177 tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
178 asm volatile ("SCKC %0" : : "m" (tmp));
179 return;
180 }
181 tmp = S390_lowcore.int_clock - S390_lowcore.jiffy_timer;
182 if (tmp >= 2*CLK_TICKS_PER_JIFFY) { /* more than two ticks ? */
183 ticks = __div(tmp, CLK_TICKS_PER_JIFFY) + 1;
184 S390_lowcore.jiffy_timer +=
185 CLK_TICKS_PER_JIFFY * (__u64) ticks;
186 } else if (tmp >= CLK_TICKS_PER_JIFFY) {
187 ticks = 2;
188 S390_lowcore.jiffy_timer += 2*CLK_TICKS_PER_JIFFY;
189 } else {
190 ticks = 1;
191 S390_lowcore.jiffy_timer += CLK_TICKS_PER_JIFFY;
192 }
193
194 /* set clock comparator for next tick */
195 tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
196 asm volatile ("SCKC %0" : : "m" (tmp));
197
198#ifdef CONFIG_SMP
199 /*
200 * Do not rely on the boot cpu to do the calls to do_timer.
201 * Spread it over all cpus instead.
202 */
203 write_seqlock(&xtime_lock);
204 if (S390_lowcore.jiffy_timer > xtime_cc) {
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700205 __u32 xticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 tmp = S390_lowcore.jiffy_timer - xtime_cc;
207 if (tmp >= 2*CLK_TICKS_PER_JIFFY) {
208 xticks = __div(tmp, CLK_TICKS_PER_JIFFY);
209 xtime_cc += (__u64) xticks * CLK_TICKS_PER_JIFFY;
210 } else {
211 xticks = 1;
212 xtime_cc += CLK_TICKS_PER_JIFFY;
213 }
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700214 do_timer(xticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 write_sequnlock(&xtime_lock);
217#else
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700218 do_timer(ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif
220
221#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Martin Schwidefsky1f1c12a2006-01-14 13:21:03 -0800222 account_tick_vtime(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#else
224 while (ticks--)
Heiko Carstens5a489b92006-10-06 16:38:35 +0200225 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#endif
227
Heiko Carstens5a489b92006-10-06 16:38:35 +0200228 s390_do_profile();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231#ifdef CONFIG_NO_IDLE_HZ
232
233#ifdef CONFIG_NO_IDLE_HZ_INIT
234int sysctl_hz_timer = 0;
235#else
236int sysctl_hz_timer = 1;
237#endif
238
239/*
240 * Stop the HZ tick on the current CPU.
241 * Only cpu_idle may call this function.
242 */
243static inline void stop_hz_timer(void)
244{
Martin Schwidefsky1b44e982005-11-07 00:59:02 -0800245 unsigned long flags;
246 unsigned long seq, next;
Martin Schwidefsky4b7e0702005-05-01 08:58:57 -0700247 __u64 timer, todval;
Heiko Carstens5afdbd62006-05-15 09:43:59 -0700248 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (sysctl_hz_timer != 0)
251 return;
252
Heiko Carstens5afdbd62006-05-15 09:43:59 -0700253 cpu_set(cpu, nohz_cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /*
256 * Leave the clock comparator set up for the next timer
257 * tick if either rcu or a softirq is pending.
258 */
Heiko Carstens5afdbd62006-05-15 09:43:59 -0700259 if (rcu_needs_cpu(cpu) || local_softirq_pending()) {
260 cpu_clear(cpu, nohz_cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return;
262 }
263
264 /*
265 * This cpu is going really idle. Set up the clock comparator
266 * for the next event.
267 */
Martin Schwidefsky1b44e982005-11-07 00:59:02 -0800268 next = next_timer_interrupt();
269 do {
270 seq = read_seqbegin_irqsave(&xtime_lock, flags);
Martin Schwidefsky705af302006-05-23 09:22:42 +0200271 timer = ((__u64) next) - ((__u64) jiffies) + jiffies_64;
Martin Schwidefsky1b44e982005-11-07 00:59:02 -0800272 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
Martin Schwidefsky4b7e0702005-05-01 08:58:57 -0700273 todval = -1ULL;
274 /* Be careful about overflows. */
275 if (timer < (-1ULL / CLK_TICKS_PER_JIFFY)) {
276 timer = jiffies_timer_cc + timer * CLK_TICKS_PER_JIFFY;
277 if (timer >= jiffies_timer_cc)
278 todval = timer;
279 }
280 asm volatile ("SCKC %0" : : "m" (todval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/*
284 * Start the HZ tick on the current CPU.
285 * Only cpu_idle may call this function.
286 */
287static inline void start_hz_timer(void)
288{
Heiko Carstens5a489b92006-10-06 16:38:35 +0200289 BUG_ON(!in_interrupt());
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!cpu_isset(smp_processor_id(), nohz_cpu_mask))
292 return;
Heiko Carstens5a489b92006-10-06 16:38:35 +0200293 account_ticks();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 cpu_clear(smp_processor_id(), nohz_cpu_mask);
295}
296
297static int nohz_idle_notify(struct notifier_block *self,
298 unsigned long action, void *hcpu)
299{
300 switch (action) {
301 case CPU_IDLE:
302 stop_hz_timer();
303 break;
304 case CPU_NOT_IDLE:
305 start_hz_timer();
306 break;
307 }
308 return NOTIFY_OK;
309}
310
311static struct notifier_block nohz_idle_nb = {
312 .notifier_call = nohz_idle_notify,
313};
314
315void __init nohz_init(void)
316{
317 if (register_idle_notifier(&nohz_idle_nb))
318 panic("Couldn't register idle notifier");
319}
320
321#endif
322
323/*
324 * Start the clock comparator on the current CPU.
325 */
326void init_cpu_timer(void)
327{
328 unsigned long cr0;
329 __u64 timer;
330
331 timer = jiffies_timer_cc + jiffies_64 * CLK_TICKS_PER_JIFFY;
332 S390_lowcore.jiffy_timer = timer + CLK_TICKS_PER_JIFFY;
333 timer += CLK_TICKS_PER_JIFFY + CPU_DEVIATION;
334 asm volatile ("SCKC %0" : : "m" (timer));
335 /* allow clock comparator timer interrupt */
336 __ctl_store(cr0, 0, 0);
337 cr0 |= 0x800;
338 __ctl_load(cr0, 0, 0);
339}
340
341extern void vtime_init(void);
342
343/*
344 * Initialize the TOD clock and the CPU timer of
345 * the boot cpu.
346 */
347void __init time_init(void)
348{
349 __u64 set_time_cc;
350 int cc;
351
352 /* kick the TOD clock */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200353 asm volatile(
354 " stck 0(%2)\n"
355 " ipm %0\n"
356 " srl %0,28"
357 : "=d" (cc), "=m" (init_timer_cc)
358 : "a" (&init_timer_cc) : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 switch (cc) {
360 case 0: /* clock in set state: all is fine */
361 break;
362 case 1: /* clock in non-set state: FIXME */
363 printk("time_init: TOD clock in non-set state\n");
364 break;
365 case 2: /* clock in error state: FIXME */
366 printk("time_init: TOD clock in error state\n");
367 break;
368 case 3: /* clock in stopped or not-operational state: FIXME */
369 printk("time_init: TOD clock stopped/non-operational\n");
370 break;
371 }
372 jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;
373
374 /* set xtime */
375 xtime_cc = init_timer_cc + CLK_TICKS_PER_JIFFY;
376 set_time_cc = init_timer_cc - 0x8126d60e46000000LL +
377 (0x3c26700LL*1000000*4096);
378 tod_to_timeval(set_time_cc, &xtime);
379 set_normalized_timespec(&wall_to_monotonic,
380 -xtime.tv_sec, -xtime.tv_nsec);
381
382 /* request the clock comparator external interrupt */
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200383 if (register_early_external_interrupt(0x1004, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 &ext_int_info_cc) != 0)
385 panic("Couldn't request external interrupt 0x1004");
386
387 init_cpu_timer();
388
389#ifdef CONFIG_NO_IDLE_HZ
390 nohz_init();
391#endif
392
393#ifdef CONFIG_VIRT_TIMER
394 vtime_init();
395#endif
396}
397