Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Common time routines among all ppc machines. |
| 4 | * |
| 5 | * Written by Cort Dougan (cort@cs.nmt.edu) to merge |
| 6 | * Paul Mackerras' version and mine for PReP and Pmac. |
| 7 | * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net). |
| 8 | * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com) |
| 9 | * |
| 10 | * First round of bugfixes by Gabriel Paubert (paubert@iram.es) |
| 11 | * to make clock more stable (2.4.0-test5). The only thing |
| 12 | * that this code assumes is that the timebases have been synchronized |
| 13 | * by firmware on SMP and are never stopped (never do sleep |
| 14 | * on SMP then, nap and doze are OK). |
| 15 | * |
| 16 | * Speeded up do_gettimeofday by getting rid of references to |
| 17 | * xtime (which required locks for consistency). (mikejc@us.ibm.com) |
| 18 | * |
| 19 | * TODO (not necessarily in this file): |
| 20 | * - improve precision and reproducibility of timebase frequency |
| 21 | * measurement at boot time. (for iSeries, we calibrate the timebase |
| 22 | * against the Titan chip's clock.) |
| 23 | * - for astronomical applications: add a new function to get |
| 24 | * non ambiguous timestamps even around leap seconds. This needs |
| 25 | * a new timestamp format and a good name. |
| 26 | * |
| 27 | * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 |
| 28 | * "A Kernel Model for Precision Timekeeping" by Dave Mills |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or |
| 31 | * modify it under the terms of the GNU General Public License |
| 32 | * as published by the Free Software Foundation; either version |
| 33 | * 2 of the License, or (at your option) any later version. |
| 34 | */ |
| 35 | |
| 36 | #include <linux/config.h> |
| 37 | #include <linux/errno.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/sched.h> |
| 40 | #include <linux/kernel.h> |
| 41 | #include <linux/param.h> |
| 42 | #include <linux/string.h> |
| 43 | #include <linux/mm.h> |
| 44 | #include <linux/interrupt.h> |
| 45 | #include <linux/timex.h> |
| 46 | #include <linux/kernel_stat.h> |
| 47 | #include <linux/mc146818rtc.h> |
| 48 | #include <linux/time.h> |
| 49 | #include <linux/init.h> |
| 50 | #include <linux/profile.h> |
| 51 | #include <linux/cpu.h> |
| 52 | #include <linux/security.h> |
| 53 | |
| 54 | #include <asm/segment.h> |
| 55 | #include <asm/io.h> |
| 56 | #include <asm/processor.h> |
| 57 | #include <asm/nvram.h> |
| 58 | #include <asm/cache.h> |
| 59 | #include <asm/machdep.h> |
| 60 | #ifdef CONFIG_PPC_ISERIES |
| 61 | #include <asm/iSeries/ItLpQueue.h> |
| 62 | #include <asm/iSeries/HvCallXm.h> |
| 63 | #endif |
| 64 | #include <asm/uaccess.h> |
| 65 | #include <asm/time.h> |
| 66 | #include <asm/ppcdebug.h> |
| 67 | #include <asm/prom.h> |
| 68 | #include <asm/sections.h> |
| 69 | #include <asm/systemcfg.h> |
| 70 | |
| 71 | u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; |
| 72 | |
| 73 | EXPORT_SYMBOL(jiffies_64); |
| 74 | |
| 75 | /* keep track of when we need to update the rtc */ |
| 76 | time_t last_rtc_update; |
| 77 | extern int piranha_simulator; |
| 78 | #ifdef CONFIG_PPC_ISERIES |
| 79 | unsigned long iSeries_recal_titan = 0; |
| 80 | unsigned long iSeries_recal_tb = 0; |
| 81 | static unsigned long first_settimeofday = 1; |
| 82 | #endif |
| 83 | |
| 84 | #define XSEC_PER_SEC (1024*1024) |
| 85 | |
| 86 | unsigned long tb_ticks_per_jiffy; |
| 87 | unsigned long tb_ticks_per_usec = 100; /* sane default */ |
| 88 | EXPORT_SYMBOL(tb_ticks_per_usec); |
| 89 | unsigned long tb_ticks_per_sec; |
| 90 | unsigned long tb_to_xs; |
| 91 | unsigned tb_to_us; |
| 92 | unsigned long processor_freq; |
| 93 | DEFINE_SPINLOCK(rtc_lock); |
| 94 | |
| 95 | unsigned long tb_to_ns_scale; |
| 96 | unsigned long tb_to_ns_shift; |
| 97 | |
| 98 | struct gettimeofday_struct do_gtod; |
| 99 | |
| 100 | extern unsigned long wall_jiffies; |
| 101 | extern unsigned long lpevent_count; |
| 102 | extern int smp_tb_synchronized; |
| 103 | |
| 104 | extern struct timezone sys_tz; |
| 105 | |
| 106 | void ppc_adjtimex(void); |
| 107 | |
| 108 | static unsigned adjusting_time = 0; |
| 109 | |
| 110 | static __inline__ void timer_check_rtc(void) |
| 111 | { |
| 112 | /* |
| 113 | * update the rtc when needed, this should be performed on the |
| 114 | * right fraction of a second. Half or full second ? |
| 115 | * Full second works on mk48t59 clocks, others need testing. |
| 116 | * Note that this update is basically only used through |
| 117 | * the adjtimex system calls. Setting the HW clock in |
| 118 | * any other way is a /dev/rtc and userland business. |
| 119 | * This is still wrong by -0.5/+1.5 jiffies because of the |
| 120 | * timer interrupt resolution and possible delay, but here we |
| 121 | * hit a quantization limit which can only be solved by higher |
| 122 | * resolution timers and decoupling time management from timer |
| 123 | * interrupts. This is also wrong on the clocks |
| 124 | * which require being written at the half second boundary. |
| 125 | * We should have an rtc call that only sets the minutes and |
| 126 | * seconds like on Intel to avoid problems with non UTC clocks. |
| 127 | */ |
| 128 | if ( (time_status & STA_UNSYNC) == 0 && |
| 129 | xtime.tv_sec - last_rtc_update >= 659 && |
| 130 | abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ && |
| 131 | jiffies - wall_jiffies == 1) { |
| 132 | struct rtc_time tm; |
| 133 | to_tm(xtime.tv_sec+1, &tm); |
| 134 | tm.tm_year -= 1900; |
| 135 | tm.tm_mon -= 1; |
| 136 | if (ppc_md.set_rtc_time(&tm) == 0) |
| 137 | last_rtc_update = xtime.tv_sec+1; |
| 138 | else |
| 139 | /* Try again one minute later */ |
| 140 | last_rtc_update += 60; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * This version of gettimeofday has microsecond resolution. |
| 146 | */ |
| 147 | static inline void __do_gettimeofday(struct timeval *tv, unsigned long tb_val) |
| 148 | { |
| 149 | unsigned long sec, usec, tb_ticks; |
| 150 | unsigned long xsec, tb_xsec; |
| 151 | struct gettimeofday_vars * temp_varp; |
| 152 | unsigned long temp_tb_to_xs, temp_stamp_xsec; |
| 153 | |
| 154 | /* |
| 155 | * These calculations are faster (gets rid of divides) |
| 156 | * if done in units of 1/2^20 rather than microseconds. |
| 157 | * The conversion to microseconds at the end is done |
| 158 | * without a divide (and in fact, without a multiply) |
| 159 | */ |
| 160 | temp_varp = do_gtod.varp; |
| 161 | tb_ticks = tb_val - temp_varp->tb_orig_stamp; |
| 162 | temp_tb_to_xs = temp_varp->tb_to_xs; |
| 163 | temp_stamp_xsec = temp_varp->stamp_xsec; |
| 164 | tb_xsec = mulhdu( tb_ticks, temp_tb_to_xs ); |
| 165 | xsec = temp_stamp_xsec + tb_xsec; |
| 166 | sec = xsec / XSEC_PER_SEC; |
| 167 | xsec -= sec * XSEC_PER_SEC; |
| 168 | usec = (xsec * USEC_PER_SEC)/XSEC_PER_SEC; |
| 169 | |
| 170 | tv->tv_sec = sec; |
| 171 | tv->tv_usec = usec; |
| 172 | } |
| 173 | |
| 174 | void do_gettimeofday(struct timeval *tv) |
| 175 | { |
| 176 | __do_gettimeofday(tv, get_tb()); |
| 177 | } |
| 178 | |
| 179 | EXPORT_SYMBOL(do_gettimeofday); |
| 180 | |
| 181 | /* Synchronize xtime with do_gettimeofday */ |
| 182 | |
| 183 | static inline void timer_sync_xtime(unsigned long cur_tb) |
| 184 | { |
| 185 | struct timeval my_tv; |
| 186 | |
| 187 | __do_gettimeofday(&my_tv, cur_tb); |
| 188 | |
| 189 | if (xtime.tv_sec <= my_tv.tv_sec) { |
| 190 | xtime.tv_sec = my_tv.tv_sec; |
| 191 | xtime.tv_nsec = my_tv.tv_usec * 1000; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * When the timebase - tb_orig_stamp gets too big, we do a manipulation |
| 197 | * between tb_orig_stamp and stamp_xsec. The goal here is to keep the |
| 198 | * difference tb - tb_orig_stamp small enough to always fit inside a |
| 199 | * 32 bits number. This is a requirement of our fast 32 bits userland |
| 200 | * implementation in the vdso. If we "miss" a call to this function |
| 201 | * (interrupt latency, CPU locked in a spinlock, ...) and we end up |
| 202 | * with a too big difference, then the vdso will fallback to calling |
| 203 | * the syscall |
| 204 | */ |
| 205 | static __inline__ void timer_recalc_offset(unsigned long cur_tb) |
| 206 | { |
| 207 | struct gettimeofday_vars * temp_varp; |
| 208 | unsigned temp_idx; |
| 209 | unsigned long offset, new_stamp_xsec, new_tb_orig_stamp; |
| 210 | |
| 211 | if (((cur_tb - do_gtod.varp->tb_orig_stamp) & 0x80000000u) == 0) |
| 212 | return; |
| 213 | |
| 214 | temp_idx = (do_gtod.var_idx == 0); |
| 215 | temp_varp = &do_gtod.vars[temp_idx]; |
| 216 | |
| 217 | new_tb_orig_stamp = cur_tb; |
| 218 | offset = new_tb_orig_stamp - do_gtod.varp->tb_orig_stamp; |
| 219 | new_stamp_xsec = do_gtod.varp->stamp_xsec + mulhdu(offset, do_gtod.varp->tb_to_xs); |
| 220 | |
| 221 | temp_varp->tb_to_xs = do_gtod.varp->tb_to_xs; |
| 222 | temp_varp->tb_orig_stamp = new_tb_orig_stamp; |
| 223 | temp_varp->stamp_xsec = new_stamp_xsec; |
Anton Blanchard | 0d8d4d4 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 224 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | do_gtod.varp = temp_varp; |
| 226 | do_gtod.var_idx = temp_idx; |
| 227 | |
| 228 | ++(systemcfg->tb_update_count); |
Anton Blanchard | 0d8d4d4 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 229 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | systemcfg->tb_orig_stamp = new_tb_orig_stamp; |
| 231 | systemcfg->stamp_xsec = new_stamp_xsec; |
Anton Blanchard | 0d8d4d4 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 232 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | ++(systemcfg->tb_update_count); |
| 234 | } |
| 235 | |
| 236 | #ifdef CONFIG_SMP |
| 237 | unsigned long profile_pc(struct pt_regs *regs) |
| 238 | { |
| 239 | unsigned long pc = instruction_pointer(regs); |
| 240 | |
| 241 | if (in_lock_functions(pc)) |
| 242 | return regs->link; |
| 243 | |
| 244 | return pc; |
| 245 | } |
| 246 | EXPORT_SYMBOL(profile_pc); |
| 247 | #endif |
| 248 | |
| 249 | #ifdef CONFIG_PPC_ISERIES |
| 250 | |
| 251 | /* |
| 252 | * This function recalibrates the timebase based on the 49-bit time-of-day |
| 253 | * value in the Titan chip. The Titan is much more accurate than the value |
| 254 | * returned by the service processor for the timebase frequency. |
| 255 | */ |
| 256 | |
| 257 | static void iSeries_tb_recal(void) |
| 258 | { |
| 259 | struct div_result divres; |
| 260 | unsigned long titan, tb; |
| 261 | tb = get_tb(); |
| 262 | titan = HvCallXm_loadTod(); |
| 263 | if ( iSeries_recal_titan ) { |
| 264 | unsigned long tb_ticks = tb - iSeries_recal_tb; |
| 265 | unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12; |
| 266 | unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec; |
| 267 | unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ; |
| 268 | long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy; |
| 269 | char sign = '+'; |
| 270 | /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */ |
| 271 | new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ; |
| 272 | |
| 273 | if ( tick_diff < 0 ) { |
| 274 | tick_diff = -tick_diff; |
| 275 | sign = '-'; |
| 276 | } |
| 277 | if ( tick_diff ) { |
| 278 | if ( tick_diff < tb_ticks_per_jiffy/25 ) { |
| 279 | printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n", |
| 280 | new_tb_ticks_per_jiffy, sign, tick_diff ); |
| 281 | tb_ticks_per_jiffy = new_tb_ticks_per_jiffy; |
| 282 | tb_ticks_per_sec = new_tb_ticks_per_sec; |
| 283 | div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres ); |
| 284 | do_gtod.tb_ticks_per_sec = tb_ticks_per_sec; |
| 285 | tb_to_xs = divres.result_low; |
| 286 | do_gtod.varp->tb_to_xs = tb_to_xs; |
| 287 | systemcfg->tb_ticks_per_sec = tb_ticks_per_sec; |
| 288 | systemcfg->tb_to_xs = tb_to_xs; |
| 289 | } |
| 290 | else { |
| 291 | printk( "Titan recalibrate: FAILED (difference > 4 percent)\n" |
| 292 | " new tb_ticks_per_jiffy = %lu\n" |
| 293 | " old tb_ticks_per_jiffy = %lu\n", |
| 294 | new_tb_ticks_per_jiffy, tb_ticks_per_jiffy ); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | iSeries_recal_titan = titan; |
| 299 | iSeries_recal_tb = tb; |
| 300 | } |
| 301 | #endif |
| 302 | |
| 303 | /* |
| 304 | * For iSeries shared processors, we have to let the hypervisor |
| 305 | * set the hardware decrementer. We set a virtual decrementer |
| 306 | * in the lppaca and call the hypervisor if the virtual |
| 307 | * decrementer is less than the current value in the hardware |
| 308 | * decrementer. (almost always the new decrementer value will |
| 309 | * be greater than the current hardware decementer so the hypervisor |
| 310 | * call will not be needed) |
| 311 | */ |
| 312 | |
| 313 | unsigned long tb_last_stamp __cacheline_aligned_in_smp; |
| 314 | |
| 315 | /* |
| 316 | * timer_interrupt - gets called when the decrementer overflows, |
| 317 | * with interrupts disabled. |
| 318 | */ |
| 319 | int timer_interrupt(struct pt_regs * regs) |
| 320 | { |
| 321 | int next_dec; |
| 322 | unsigned long cur_tb; |
| 323 | struct paca_struct *lpaca = get_paca(); |
| 324 | unsigned long cpu = smp_processor_id(); |
| 325 | |
| 326 | irq_enter(); |
| 327 | |
| 328 | #ifndef CONFIG_PPC_ISERIES |
| 329 | profile_tick(CPU_PROFILING, regs); |
| 330 | #endif |
| 331 | |
| 332 | lpaca->lppaca.int_dword.fields.decr_int = 0; |
| 333 | |
| 334 | while (lpaca->next_jiffy_update_tb <= (cur_tb = get_tb())) { |
| 335 | /* |
| 336 | * We cannot disable the decrementer, so in the period |
| 337 | * between this cpu's being marked offline in cpu_online_map |
| 338 | * and calling stop-self, it is taking timer interrupts. |
| 339 | * Avoid calling into the scheduler rebalancing code if this |
| 340 | * is the case. |
| 341 | */ |
| 342 | if (!cpu_is_offline(cpu)) |
| 343 | update_process_times(user_mode(regs)); |
| 344 | /* |
| 345 | * No need to check whether cpu is offline here; boot_cpuid |
| 346 | * should have been fixed up by now. |
| 347 | */ |
| 348 | if (cpu == boot_cpuid) { |
| 349 | write_seqlock(&xtime_lock); |
| 350 | tb_last_stamp = lpaca->next_jiffy_update_tb; |
| 351 | timer_recalc_offset(lpaca->next_jiffy_update_tb); |
| 352 | do_timer(regs); |
| 353 | timer_sync_xtime(lpaca->next_jiffy_update_tb); |
| 354 | timer_check_rtc(); |
| 355 | write_sequnlock(&xtime_lock); |
| 356 | if ( adjusting_time && (time_adjust == 0) ) |
| 357 | ppc_adjtimex(); |
| 358 | } |
| 359 | lpaca->next_jiffy_update_tb += tb_ticks_per_jiffy; |
| 360 | } |
| 361 | |
| 362 | next_dec = lpaca->next_jiffy_update_tb - cur_tb; |
| 363 | if (next_dec > lpaca->default_decr) |
| 364 | next_dec = lpaca->default_decr; |
| 365 | set_dec(next_dec); |
| 366 | |
| 367 | #ifdef CONFIG_PPC_ISERIES |
| 368 | { |
| 369 | struct ItLpQueue *lpq = lpaca->lpqueue_ptr; |
| 370 | if (lpq && ItLpQueue_isLpIntPending(lpq)) |
| 371 | lpevent_count += ItLpQueue_process(lpq, regs); |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | /* collect purr register values often, for accurate calculations */ |
| 376 | #if defined(CONFIG_PPC_PSERIES) |
| 377 | if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) { |
| 378 | struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array); |
| 379 | cu->current_tb = mfspr(SPRN_PURR); |
| 380 | } |
| 381 | #endif |
| 382 | |
| 383 | irq_exit(); |
| 384 | |
| 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Scheduler clock - returns current time in nanosec units. |
| 390 | * |
| 391 | * Note: mulhdu(a, b) (multiply high double unsigned) returns |
| 392 | * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b |
| 393 | * are 64-bit unsigned numbers. |
| 394 | */ |
| 395 | unsigned long long sched_clock(void) |
| 396 | { |
| 397 | return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift; |
| 398 | } |
| 399 | |
| 400 | int do_settimeofday(struct timespec *tv) |
| 401 | { |
| 402 | time_t wtm_sec, new_sec = tv->tv_sec; |
| 403 | long wtm_nsec, new_nsec = tv->tv_nsec; |
| 404 | unsigned long flags; |
| 405 | unsigned long delta_xsec; |
| 406 | long int tb_delta; |
| 407 | unsigned long new_xsec; |
| 408 | |
| 409 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | write_seqlock_irqsave(&xtime_lock, flags); |
| 413 | /* Updating the RTC is not the job of this code. If the time is |
| 414 | * stepped under NTP, the RTC will be update after STA_UNSYNC |
| 415 | * is cleared. Tool like clock/hwclock either copy the RTC |
| 416 | * to the system time, in which case there is no point in writing |
| 417 | * to the RTC again, or write to the RTC but then they don't call |
| 418 | * settimeofday to perform this operation. |
| 419 | */ |
| 420 | #ifdef CONFIG_PPC_ISERIES |
| 421 | if ( first_settimeofday ) { |
| 422 | iSeries_tb_recal(); |
| 423 | first_settimeofday = 0; |
| 424 | } |
| 425 | #endif |
| 426 | tb_delta = tb_ticks_since(tb_last_stamp); |
| 427 | tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy; |
| 428 | |
| 429 | new_nsec -= tb_delta / tb_ticks_per_usec / 1000; |
| 430 | |
| 431 | wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec); |
| 432 | wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec); |
| 433 | |
| 434 | set_normalized_timespec(&xtime, new_sec, new_nsec); |
| 435 | set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); |
| 436 | |
| 437 | /* In case of a large backwards jump in time with NTP, we want the |
| 438 | * clock to be updated as soon as the PLL is again in lock. |
| 439 | */ |
| 440 | last_rtc_update = new_sec - 658; |
| 441 | |
| 442 | time_adjust = 0; /* stop active adjtime() */ |
| 443 | time_status |= STA_UNSYNC; |
| 444 | time_maxerror = NTP_PHASE_LIMIT; |
| 445 | time_esterror = NTP_PHASE_LIMIT; |
| 446 | |
| 447 | delta_xsec = mulhdu( (tb_last_stamp-do_gtod.varp->tb_orig_stamp), |
| 448 | do_gtod.varp->tb_to_xs ); |
| 449 | |
| 450 | new_xsec = (new_nsec * XSEC_PER_SEC) / NSEC_PER_SEC; |
| 451 | new_xsec += new_sec * XSEC_PER_SEC; |
| 452 | if ( new_xsec > delta_xsec ) { |
| 453 | do_gtod.varp->stamp_xsec = new_xsec - delta_xsec; |
| 454 | systemcfg->stamp_xsec = new_xsec - delta_xsec; |
| 455 | } |
| 456 | else { |
| 457 | /* This is only for the case where the user is setting the time |
| 458 | * way back to a time such that the boot time would have been |
| 459 | * before 1970 ... eg. we booted ten days ago, and we are setting |
| 460 | * the time to Jan 5, 1970 */ |
| 461 | do_gtod.varp->stamp_xsec = new_xsec; |
| 462 | do_gtod.varp->tb_orig_stamp = tb_last_stamp; |
| 463 | systemcfg->stamp_xsec = new_xsec; |
| 464 | systemcfg->tb_orig_stamp = tb_last_stamp; |
| 465 | } |
| 466 | |
| 467 | systemcfg->tz_minuteswest = sys_tz.tz_minuteswest; |
| 468 | systemcfg->tz_dsttime = sys_tz.tz_dsttime; |
| 469 | |
| 470 | write_sequnlock_irqrestore(&xtime_lock, flags); |
| 471 | clock_was_set(); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | EXPORT_SYMBOL(do_settimeofday); |
| 476 | |
| 477 | void __init time_init(void) |
| 478 | { |
| 479 | /* This function is only called on the boot processor */ |
| 480 | unsigned long flags; |
| 481 | struct rtc_time tm; |
| 482 | struct div_result res; |
| 483 | unsigned long scale, shift; |
| 484 | |
| 485 | ppc_md.calibrate_decr(); |
| 486 | |
| 487 | /* |
| 488 | * Compute scale factor for sched_clock. |
| 489 | * The calibrate_decr() function has set tb_ticks_per_sec, |
| 490 | * which is the timebase frequency. |
| 491 | * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret |
| 492 | * the 128-bit result as a 64.64 fixed-point number. |
| 493 | * We then shift that number right until it is less than 1.0, |
| 494 | * giving us the scale factor and shift count to use in |
| 495 | * sched_clock(). |
| 496 | */ |
| 497 | div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); |
| 498 | scale = res.result_low; |
| 499 | for (shift = 0; res.result_high != 0; ++shift) { |
| 500 | scale = (scale >> 1) | (res.result_high << 63); |
| 501 | res.result_high >>= 1; |
| 502 | } |
| 503 | tb_to_ns_scale = scale; |
| 504 | tb_to_ns_shift = shift; |
| 505 | |
| 506 | #ifdef CONFIG_PPC_ISERIES |
| 507 | if (!piranha_simulator) |
| 508 | #endif |
| 509 | ppc_md.get_boot_time(&tm); |
| 510 | |
| 511 | write_seqlock_irqsave(&xtime_lock, flags); |
| 512 | xtime.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 513 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 514 | tb_last_stamp = get_tb(); |
| 515 | do_gtod.varp = &do_gtod.vars[0]; |
| 516 | do_gtod.var_idx = 0; |
| 517 | do_gtod.varp->tb_orig_stamp = tb_last_stamp; |
Stephen Rothwell | 8f80e5c | 2005-05-25 13:41:26 +1000 | [diff] [blame] | 518 | get_paca()->next_jiffy_update_tb = tb_last_stamp + tb_ticks_per_jiffy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | do_gtod.varp->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC; |
| 520 | do_gtod.tb_ticks_per_sec = tb_ticks_per_sec; |
| 521 | do_gtod.varp->tb_to_xs = tb_to_xs; |
| 522 | do_gtod.tb_to_us = tb_to_us; |
| 523 | systemcfg->tb_orig_stamp = tb_last_stamp; |
| 524 | systemcfg->tb_update_count = 0; |
| 525 | systemcfg->tb_ticks_per_sec = tb_ticks_per_sec; |
| 526 | systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC; |
| 527 | systemcfg->tb_to_xs = tb_to_xs; |
| 528 | |
| 529 | time_freq = 0; |
| 530 | |
| 531 | xtime.tv_nsec = 0; |
| 532 | last_rtc_update = xtime.tv_sec; |
| 533 | set_normalized_timespec(&wall_to_monotonic, |
| 534 | -xtime.tv_sec, -xtime.tv_nsec); |
| 535 | write_sequnlock_irqrestore(&xtime_lock, flags); |
| 536 | |
| 537 | /* Not exact, but the timer interrupt takes care of this */ |
| 538 | set_dec(tb_ticks_per_jiffy); |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * After adjtimex is called, adjust the conversion of tb ticks |
| 543 | * to microseconds to keep do_gettimeofday synchronized |
| 544 | * with ntpd. |
| 545 | * |
| 546 | * Use the time_adjust, time_freq and time_offset computed by adjtimex to |
| 547 | * adjust the frequency. |
| 548 | */ |
| 549 | |
| 550 | /* #define DEBUG_PPC_ADJTIMEX 1 */ |
| 551 | |
| 552 | void ppc_adjtimex(void) |
| 553 | { |
| 554 | unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec, new_tb_to_xs, new_xsec, new_stamp_xsec; |
| 555 | unsigned long tb_ticks_per_sec_delta; |
| 556 | long delta_freq, ltemp; |
| 557 | struct div_result divres; |
| 558 | unsigned long flags; |
| 559 | struct gettimeofday_vars * temp_varp; |
| 560 | unsigned temp_idx; |
| 561 | long singleshot_ppm = 0; |
| 562 | |
| 563 | /* Compute parts per million frequency adjustment to accomplish the time adjustment |
| 564 | implied by time_offset to be applied over the elapsed time indicated by time_constant. |
| 565 | Use SHIFT_USEC to get it into the same units as time_freq. */ |
| 566 | if ( time_offset < 0 ) { |
| 567 | ltemp = -time_offset; |
| 568 | ltemp <<= SHIFT_USEC - SHIFT_UPDATE; |
| 569 | ltemp >>= SHIFT_KG + time_constant; |
| 570 | ltemp = -ltemp; |
| 571 | } |
| 572 | else { |
| 573 | ltemp = time_offset; |
| 574 | ltemp <<= SHIFT_USEC - SHIFT_UPDATE; |
| 575 | ltemp >>= SHIFT_KG + time_constant; |
| 576 | } |
| 577 | |
| 578 | /* If there is a single shot time adjustment in progress */ |
| 579 | if ( time_adjust ) { |
| 580 | #ifdef DEBUG_PPC_ADJTIMEX |
| 581 | printk("ppc_adjtimex: "); |
| 582 | if ( adjusting_time == 0 ) |
| 583 | printk("starting "); |
| 584 | printk("single shot time_adjust = %ld\n", time_adjust); |
| 585 | #endif |
| 586 | |
| 587 | adjusting_time = 1; |
| 588 | |
| 589 | /* Compute parts per million frequency adjustment to match time_adjust */ |
| 590 | singleshot_ppm = tickadj * HZ; |
| 591 | /* |
| 592 | * The adjustment should be tickadj*HZ to match the code in |
| 593 | * linux/kernel/timer.c, but experiments show that this is too |
| 594 | * large. 3/4 of tickadj*HZ seems about right |
| 595 | */ |
| 596 | singleshot_ppm -= singleshot_ppm / 4; |
| 597 | /* Use SHIFT_USEC to get it into the same units as time_freq */ |
| 598 | singleshot_ppm <<= SHIFT_USEC; |
| 599 | if ( time_adjust < 0 ) |
| 600 | singleshot_ppm = -singleshot_ppm; |
| 601 | } |
| 602 | else { |
| 603 | #ifdef DEBUG_PPC_ADJTIMEX |
| 604 | if ( adjusting_time ) |
| 605 | printk("ppc_adjtimex: ending single shot time_adjust\n"); |
| 606 | #endif |
| 607 | adjusting_time = 0; |
| 608 | } |
| 609 | |
| 610 | /* Add up all of the frequency adjustments */ |
| 611 | delta_freq = time_freq + ltemp + singleshot_ppm; |
| 612 | |
| 613 | /* Compute a new value for tb_ticks_per_sec based on the frequency adjustment */ |
| 614 | den = 1000000 * (1 << (SHIFT_USEC - 8)); |
| 615 | if ( delta_freq < 0 ) { |
| 616 | tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den; |
| 617 | new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta; |
| 618 | } |
| 619 | else { |
| 620 | tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den; |
| 621 | new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta; |
| 622 | } |
| 623 | |
| 624 | #ifdef DEBUG_PPC_ADJTIMEX |
| 625 | printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm); |
| 626 | printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec); |
| 627 | #endif |
| 628 | |
| 629 | /* Compute a new value of tb_to_xs (used to convert tb to microseconds and a new value of |
| 630 | stamp_xsec which is the time (in 1/2^20 second units) corresponding to tb_orig_stamp. This |
| 631 | new value of stamp_xsec compensates for the change in frequency (implied by the new tb_to_xs) |
| 632 | which guarantees that the current time remains the same */ |
| 633 | write_seqlock_irqsave( &xtime_lock, flags ); |
| 634 | tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp; |
| 635 | div128_by_32( 1024*1024, 0, new_tb_ticks_per_sec, &divres ); |
| 636 | new_tb_to_xs = divres.result_low; |
| 637 | new_xsec = mulhdu( tb_ticks, new_tb_to_xs ); |
| 638 | |
| 639 | old_xsec = mulhdu( tb_ticks, do_gtod.varp->tb_to_xs ); |
| 640 | new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec; |
| 641 | |
| 642 | /* There are two copies of tb_to_xs and stamp_xsec so that no lock is needed to access and use these |
| 643 | values in do_gettimeofday. We alternate the copies and as long as a reasonable time elapses between |
| 644 | changes, there will never be inconsistent values. ntpd has a minimum of one minute between updates */ |
| 645 | |
| 646 | temp_idx = (do_gtod.var_idx == 0); |
| 647 | temp_varp = &do_gtod.vars[temp_idx]; |
| 648 | |
| 649 | temp_varp->tb_to_xs = new_tb_to_xs; |
| 650 | temp_varp->stamp_xsec = new_stamp_xsec; |
| 651 | temp_varp->tb_orig_stamp = do_gtod.varp->tb_orig_stamp; |
Anton Blanchard | 0d8d4d4 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 652 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | do_gtod.varp = temp_varp; |
| 654 | do_gtod.var_idx = temp_idx; |
| 655 | |
| 656 | /* |
| 657 | * tb_update_count is used to allow the problem state gettimeofday code |
| 658 | * to assure itself that it sees a consistent view of the tb_to_xs and |
| 659 | * stamp_xsec variables. It reads the tb_update_count, then reads |
| 660 | * tb_to_xs and stamp_xsec and then reads tb_update_count again. If |
| 661 | * the two values of tb_update_count match and are even then the |
| 662 | * tb_to_xs and stamp_xsec values are consistent. If not, then it |
| 663 | * loops back and reads them again until this criteria is met. |
| 664 | */ |
| 665 | ++(systemcfg->tb_update_count); |
Anton Blanchard | 0d8d4d4 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 666 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | systemcfg->tb_to_xs = new_tb_to_xs; |
| 668 | systemcfg->stamp_xsec = new_stamp_xsec; |
Anton Blanchard | 0d8d4d4 | 2005-05-01 08:58:47 -0700 | [diff] [blame] | 669 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | ++(systemcfg->tb_update_count); |
| 671 | |
| 672 | write_sequnlock_irqrestore( &xtime_lock, flags ); |
| 673 | |
| 674 | } |
| 675 | |
| 676 | |
| 677 | #define TICK_SIZE tick |
| 678 | #define FEBRUARY 2 |
| 679 | #define STARTOFTIME 1970 |
| 680 | #define SECDAY 86400L |
| 681 | #define SECYR (SECDAY * 365) |
| 682 | #define leapyear(year) ((year) % 4 == 0) |
| 683 | #define days_in_year(a) (leapyear(a) ? 366 : 365) |
| 684 | #define days_in_month(a) (month_days[(a) - 1]) |
| 685 | |
| 686 | static int month_days[12] = { |
| 687 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 688 | }; |
| 689 | |
| 690 | /* |
| 691 | * This only works for the Gregorian calendar - i.e. after 1752 (in the UK) |
| 692 | */ |
| 693 | void GregorianDay(struct rtc_time * tm) |
| 694 | { |
| 695 | int leapsToDate; |
| 696 | int lastYear; |
| 697 | int day; |
| 698 | int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; |
| 699 | |
| 700 | lastYear=tm->tm_year-1; |
| 701 | |
| 702 | /* |
| 703 | * Number of leap corrections to apply up to end of last year |
| 704 | */ |
| 705 | leapsToDate = lastYear/4 - lastYear/100 + lastYear/400; |
| 706 | |
| 707 | /* |
| 708 | * This year is a leap year if it is divisible by 4 except when it is |
| 709 | * divisible by 100 unless it is divisible by 400 |
| 710 | * |
| 711 | * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be |
| 712 | */ |
| 713 | if((tm->tm_year%4==0) && |
| 714 | ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) && |
| 715 | (tm->tm_mon>2)) |
| 716 | { |
| 717 | /* |
| 718 | * We are past Feb. 29 in a leap year |
| 719 | */ |
| 720 | day=1; |
| 721 | } |
| 722 | else |
| 723 | { |
| 724 | day=0; |
| 725 | } |
| 726 | |
| 727 | day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + |
| 728 | tm->tm_mday; |
| 729 | |
| 730 | tm->tm_wday=day%7; |
| 731 | } |
| 732 | |
| 733 | void to_tm(int tim, struct rtc_time * tm) |
| 734 | { |
| 735 | register int i; |
| 736 | register long hms, day; |
| 737 | |
| 738 | day = tim / SECDAY; |
| 739 | hms = tim % SECDAY; |
| 740 | |
| 741 | /* Hours, minutes, seconds are easy */ |
| 742 | tm->tm_hour = hms / 3600; |
| 743 | tm->tm_min = (hms % 3600) / 60; |
| 744 | tm->tm_sec = (hms % 3600) % 60; |
| 745 | |
| 746 | /* Number of years in days */ |
| 747 | for (i = STARTOFTIME; day >= days_in_year(i); i++) |
| 748 | day -= days_in_year(i); |
| 749 | tm->tm_year = i; |
| 750 | |
| 751 | /* Number of months in days left */ |
| 752 | if (leapyear(tm->tm_year)) |
| 753 | days_in_month(FEBRUARY) = 29; |
| 754 | for (i = 1; day >= days_in_month(i); i++) |
| 755 | day -= days_in_month(i); |
| 756 | days_in_month(FEBRUARY) = 28; |
| 757 | tm->tm_mon = i; |
| 758 | |
| 759 | /* Days are what is left over (+1) from all that. */ |
| 760 | tm->tm_mday = day + 1; |
| 761 | |
| 762 | /* |
| 763 | * Determine the day of week |
| 764 | */ |
| 765 | GregorianDay(tm); |
| 766 | } |
| 767 | |
| 768 | /* Auxiliary function to compute scaling factors */ |
| 769 | /* Actually the choice of a timebase running at 1/4 the of the bus |
| 770 | * frequency giving resolution of a few tens of nanoseconds is quite nice. |
| 771 | * It makes this computation very precise (27-28 bits typically) which |
| 772 | * is optimistic considering the stability of most processor clock |
| 773 | * oscillators and the precision with which the timebase frequency |
| 774 | * is measured but does not harm. |
| 775 | */ |
| 776 | unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) { |
| 777 | unsigned mlt=0, tmp, err; |
| 778 | /* No concern for performance, it's done once: use a stupid |
| 779 | * but safe and compact method to find the multiplier. |
| 780 | */ |
| 781 | |
| 782 | for (tmp = 1U<<31; tmp != 0; tmp >>= 1) { |
| 783 | if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp; |
| 784 | } |
| 785 | |
| 786 | /* We might still be off by 1 for the best approximation. |
| 787 | * A side effect of this is that if outscale is too large |
| 788 | * the returned value will be zero. |
| 789 | * Many corner cases have been checked and seem to work, |
| 790 | * some might have been forgotten in the test however. |
| 791 | */ |
| 792 | |
| 793 | err = inscale*(mlt+1); |
| 794 | if (err <= inscale/2) mlt++; |
| 795 | return mlt; |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit |
| 800 | * result. |
| 801 | */ |
| 802 | |
| 803 | void div128_by_32( unsigned long dividend_high, unsigned long dividend_low, |
| 804 | unsigned divisor, struct div_result *dr ) |
| 805 | { |
| 806 | unsigned long a,b,c,d, w,x,y,z, ra,rb,rc; |
| 807 | |
| 808 | a = dividend_high >> 32; |
| 809 | b = dividend_high & 0xffffffff; |
| 810 | c = dividend_low >> 32; |
| 811 | d = dividend_low & 0xffffffff; |
| 812 | |
| 813 | w = a/divisor; |
| 814 | ra = (a - (w * divisor)) << 32; |
| 815 | |
| 816 | x = (ra + b)/divisor; |
| 817 | rb = ((ra + b) - (x * divisor)) << 32; |
| 818 | |
| 819 | y = (rb + c)/divisor; |
| 820 | rc = ((rb + b) - (y * divisor)) << 32; |
| 821 | |
| 822 | z = (rc + d)/divisor; |
| 823 | |
| 824 | dr->result_high = (w << 32) + x; |
| 825 | dr->result_low = (y << 32) + z; |
| 826 | |
| 827 | } |
| 828 | |