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