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