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