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