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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/errno.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/sched.h> |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/param.h> |
| 40 | #include <linux/string.h> |
| 41 | #include <linux/mm.h> |
| 42 | #include <linux/interrupt.h> |
| 43 | #include <linux/timex.h> |
| 44 | #include <linux/kernel_stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/time.h> |
| 46 | #include <linux/init.h> |
| 47 | #include <linux/profile.h> |
| 48 | #include <linux/cpu.h> |
| 49 | #include <linux/security.h> |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 50 | #include <linux/percpu.h> |
| 51 | #include <linux/rtc.h> |
Paul Mackerras | 092b8f3 | 2006-02-20 10:38:56 +1100 | [diff] [blame] | 52 | #include <linux/jiffies.h> |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 53 | #include <linux/posix-timers.h> |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 54 | #include <linux/irq.h> |
Benjamin Herrenschmidt | 177996e | 2009-06-09 21:12:00 +0000 | [diff] [blame] | 55 | #include <linux/delay.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 56 | #include <linux/perf_event.h> |
Anton Blanchard | 6795b85 | 2009-10-26 18:49:14 +0000 | [diff] [blame] | 57 | #include <asm/trace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <asm/io.h> |
| 60 | #include <asm/processor.h> |
| 61 | #include <asm/nvram.h> |
| 62 | #include <asm/cache.h> |
| 63 | #include <asm/machdep.h> |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 64 | #include <asm/uaccess.h> |
| 65 | #include <asm/time.h> |
| 66 | #include <asm/prom.h> |
| 67 | #include <asm/irq.h> |
| 68 | #include <asm/div64.h> |
Paul Mackerras | 2249ca9 | 2005-11-07 13:18:13 +1100 | [diff] [blame] | 69 | #include <asm/smp.h> |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 70 | #include <asm/vdso_datapage.h> |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 71 | #include <asm/firmware.h> |
Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 72 | #include <asm/cputime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #ifdef CONFIG_PPC_ISERIES |
Kelly Daly | 8875ccf | 2005-11-02 14:13:34 +1100 | [diff] [blame] | 74 | #include <asm/iseries/it_lp_queue.h> |
Kelly Daly | 8021b8a | 2005-11-02 11:41:12 +1100 | [diff] [blame] | 75 | #include <asm/iseries/hv_call_xm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 78 | /* powerpc clocksource/clockevent code */ |
| 79 | |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 80 | #include <linux/clockchips.h> |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 81 | #include <linux/clocksource.h> |
| 82 | |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 83 | static cycle_t rtc_read(struct clocksource *); |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 84 | static struct clocksource clocksource_rtc = { |
| 85 | .name = "rtc", |
| 86 | .rating = 400, |
| 87 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 88 | .mask = CLOCKSOURCE_MASK(64), |
| 89 | .shift = 22, |
| 90 | .mult = 0, /* To be filled in */ |
| 91 | .read = rtc_read, |
| 92 | }; |
| 93 | |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 94 | static cycle_t timebase_read(struct clocksource *); |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 95 | static struct clocksource clocksource_timebase = { |
| 96 | .name = "timebase", |
| 97 | .rating = 400, |
| 98 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 99 | .mask = CLOCKSOURCE_MASK(64), |
| 100 | .shift = 22, |
| 101 | .mult = 0, /* To be filled in */ |
| 102 | .read = timebase_read, |
| 103 | }; |
| 104 | |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 105 | #define DECREMENTER_MAX 0x7fffffff |
| 106 | |
| 107 | static int decrementer_set_next_event(unsigned long evt, |
| 108 | struct clock_event_device *dev); |
| 109 | static void decrementer_set_mode(enum clock_event_mode mode, |
| 110 | struct clock_event_device *dev); |
| 111 | |
| 112 | static struct clock_event_device decrementer_clockevent = { |
| 113 | .name = "decrementer", |
| 114 | .rating = 200, |
Anton Blanchard | 8d165db | 2009-05-10 13:37:36 +0000 | [diff] [blame] | 115 | .shift = 0, /* To be filled in */ |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 116 | .mult = 0, /* To be filled in */ |
| 117 | .irq = 0, |
| 118 | .set_next_event = decrementer_set_next_event, |
| 119 | .set_mode = decrementer_set_mode, |
| 120 | .features = CLOCK_EVT_FEAT_ONESHOT, |
| 121 | }; |
| 122 | |
Milton Miller | 6e6b44e | 2007-12-14 15:52:15 +1100 | [diff] [blame] | 123 | struct decrementer_clock { |
| 124 | struct clock_event_device event; |
| 125 | u64 next_tb; |
| 126 | }; |
| 127 | |
| 128 | static DEFINE_PER_CPU(struct decrementer_clock, decrementers); |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | #ifdef CONFIG_PPC_ISERIES |
Tony Breeds | 71712b4 | 2007-06-22 16:54:30 +1000 | [diff] [blame] | 131 | static unsigned long __initdata iSeries_recal_titan; |
| 132 | static signed long __initdata iSeries_recal_tb; |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 133 | |
| 134 | /* Forward declaration is only needed for iSereis compiles */ |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 135 | static void __init clocksource_init(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | #define XSEC_PER_SEC (1024*1024) |
| 139 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 140 | #ifdef CONFIG_PPC64 |
| 141 | #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC) |
| 142 | #else |
| 143 | /* compute ((xsec << 12) * max) >> 32 */ |
| 144 | #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max) |
| 145 | #endif |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | unsigned long tb_ticks_per_jiffy; |
| 148 | unsigned long tb_ticks_per_usec = 100; /* sane default */ |
| 149 | EXPORT_SYMBOL(tb_ticks_per_usec); |
| 150 | unsigned long tb_ticks_per_sec; |
Paul Mackerras | 2cf82c0 | 2006-02-27 15:41:47 +1100 | [diff] [blame] | 151 | EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */ |
Paul Mackerras | 092b8f3 | 2006-02-20 10:38:56 +1100 | [diff] [blame] | 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | DEFINE_SPINLOCK(rtc_lock); |
Benjamin Herrenschmidt | 6ae3db1 | 2005-06-27 14:36:35 -0700 | [diff] [blame] | 154 | EXPORT_SYMBOL_GPL(rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Tony Breeds | fc9069fe | 2007-07-04 14:04:31 +1000 | [diff] [blame] | 156 | static u64 tb_to_ns_scale __read_mostly; |
| 157 | static unsigned tb_to_ns_shift __read_mostly; |
| 158 | static unsigned long boot_tb __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | extern struct timezone sys_tz; |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 161 | static long timezone_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 163 | unsigned long ppc_proc_freq; |
Bob Nelson | 1474855 | 2007-07-20 21:39:53 +0200 | [diff] [blame] | 164 | EXPORT_SYMBOL(ppc_proc_freq); |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 165 | unsigned long ppc_tb_freq; |
| 166 | |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 167 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING |
| 168 | /* |
| 169 | * Factors for converting from cputime_t (timebase ticks) to |
| 170 | * jiffies, milliseconds, seconds, and clock_t (1/USER_HZ seconds). |
| 171 | * These are all stored as 0.64 fixed-point binary fractions. |
| 172 | */ |
| 173 | u64 __cputime_jiffies_factor; |
Paul Mackerras | 2cf82c0 | 2006-02-27 15:41:47 +1100 | [diff] [blame] | 174 | EXPORT_SYMBOL(__cputime_jiffies_factor); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 175 | u64 __cputime_msec_factor; |
Paul Mackerras | 2cf82c0 | 2006-02-27 15:41:47 +1100 | [diff] [blame] | 176 | EXPORT_SYMBOL(__cputime_msec_factor); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 177 | u64 __cputime_sec_factor; |
Paul Mackerras | 2cf82c0 | 2006-02-27 15:41:47 +1100 | [diff] [blame] | 178 | EXPORT_SYMBOL(__cputime_sec_factor); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 179 | u64 __cputime_clockt_factor; |
Paul Mackerras | 2cf82c0 | 2006-02-27 15:41:47 +1100 | [diff] [blame] | 180 | EXPORT_SYMBOL(__cputime_clockt_factor); |
Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 181 | DEFINE_PER_CPU(unsigned long, cputime_last_delta); |
| 182 | DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 183 | |
Stanislaw Gruszka | a42548a | 2009-07-29 12:15:29 +0200 | [diff] [blame] | 184 | cputime_t cputime_one_jiffy; |
| 185 | |
Paul Mackerras | 872e439 | 2010-08-31 01:59:53 +0000 | [diff] [blame^] | 186 | void (*dtl_consumer)(struct dtl_entry *, u64); |
| 187 | |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 188 | static void calc_cputime_factors(void) |
| 189 | { |
| 190 | struct div_result res; |
| 191 | |
| 192 | div128_by_32(HZ, 0, tb_ticks_per_sec, &res); |
| 193 | __cputime_jiffies_factor = res.result_low; |
| 194 | div128_by_32(1000, 0, tb_ticks_per_sec, &res); |
| 195 | __cputime_msec_factor = res.result_low; |
| 196 | div128_by_32(1, 0, tb_ticks_per_sec, &res); |
| 197 | __cputime_sec_factor = res.result_low; |
| 198 | div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res); |
| 199 | __cputime_clockt_factor = res.result_low; |
| 200 | } |
| 201 | |
| 202 | /* |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 203 | * Read the SPURR on systems that have it, otherwise the PURR, |
| 204 | * or if that doesn't exist return the timebase value passed in. |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 205 | */ |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 206 | static u64 read_spurr(u64 tb) |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 207 | { |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 208 | if (cpu_has_feature(CPU_FTR_SPURR)) |
| 209 | return mfspr(SPRN_SPURR); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 210 | if (cpu_has_feature(CPU_FTR_PURR)) |
| 211 | return mfspr(SPRN_PURR); |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 212 | return tb; |
| 213 | } |
| 214 | |
| 215 | #ifdef CONFIG_PPC_SPLPAR |
| 216 | |
| 217 | /* |
| 218 | * Scan the dispatch trace log and count up the stolen time. |
| 219 | * Should be called with interrupts disabled. |
| 220 | */ |
| 221 | static u64 scan_dispatch_log(u64 stop_tb) |
| 222 | { |
Paul Mackerras | 872e439 | 2010-08-31 01:59:53 +0000 | [diff] [blame^] | 223 | u64 i = local_paca->dtl_ridx; |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 224 | struct dtl_entry *dtl = local_paca->dtl_curr; |
| 225 | struct dtl_entry *dtl_end = local_paca->dispatch_log_end; |
| 226 | struct lppaca *vpa = local_paca->lppaca_ptr; |
| 227 | u64 tb_delta; |
| 228 | u64 stolen = 0; |
| 229 | u64 dtb; |
| 230 | |
| 231 | if (i == vpa->dtl_idx) |
| 232 | return 0; |
| 233 | while (i < vpa->dtl_idx) { |
Paul Mackerras | 872e439 | 2010-08-31 01:59:53 +0000 | [diff] [blame^] | 234 | if (dtl_consumer) |
| 235 | dtl_consumer(dtl, i); |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 236 | dtb = dtl->timebase; |
| 237 | tb_delta = dtl->enqueue_to_dispatch_time + |
| 238 | dtl->ready_to_enqueue_time; |
| 239 | barrier(); |
| 240 | if (i + N_DISPATCH_LOG < vpa->dtl_idx) { |
| 241 | /* buffer has overflowed */ |
| 242 | i = vpa->dtl_idx - N_DISPATCH_LOG; |
| 243 | dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG); |
| 244 | continue; |
| 245 | } |
| 246 | if (dtb > stop_tb) |
| 247 | break; |
| 248 | stolen += tb_delta; |
| 249 | ++i; |
| 250 | ++dtl; |
| 251 | if (dtl == dtl_end) |
| 252 | dtl = local_paca->dispatch_log; |
| 253 | } |
| 254 | local_paca->dtl_ridx = i; |
| 255 | local_paca->dtl_curr = dtl; |
| 256 | return stolen; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 260 | * Accumulate stolen time by scanning the dispatch trace log. |
| 261 | * Called on entry from user mode. |
Michael Neuling | 4603ac1 | 2007-10-18 03:06:37 -0700 | [diff] [blame] | 262 | */ |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 263 | void accumulate_stolen_time(void) |
Michael Neuling | 4603ac1 | 2007-10-18 03:06:37 -0700 | [diff] [blame] | 264 | { |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 265 | u64 sst, ust; |
| 266 | |
| 267 | sst = scan_dispatch_log(get_paca()->starttime_user); |
| 268 | ust = scan_dispatch_log(get_paca()->starttime); |
| 269 | get_paca()->system_time -= sst; |
| 270 | get_paca()->user_time -= ust; |
| 271 | get_paca()->stolen_time += ust + sst; |
Michael Neuling | 4603ac1 | 2007-10-18 03:06:37 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 274 | static inline u64 calculate_stolen_time(u64 stop_tb) |
| 275 | { |
| 276 | u64 stolen = 0; |
| 277 | |
| 278 | if (get_paca()->dtl_ridx != get_paca()->lppaca_ptr->dtl_idx) { |
| 279 | stolen = scan_dispatch_log(stop_tb); |
| 280 | get_paca()->system_time -= stolen; |
| 281 | } |
| 282 | |
| 283 | stolen += get_paca()->stolen_time; |
| 284 | get_paca()->stolen_time = 0; |
| 285 | return stolen; |
| 286 | } |
| 287 | |
| 288 | #else /* CONFIG_PPC_SPLPAR */ |
| 289 | static inline u64 calculate_stolen_time(u64 stop_tb) |
| 290 | { |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | #endif /* CONFIG_PPC_SPLPAR */ |
| 295 | |
Michael Neuling | 4603ac1 | 2007-10-18 03:06:37 -0700 | [diff] [blame] | 296 | /* |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 297 | * Account time for a transition between system, hard irq |
| 298 | * or soft irq state. |
| 299 | */ |
| 300 | void account_system_vtime(struct task_struct *tsk) |
| 301 | { |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 302 | u64 now, nowscaled, delta, deltascaled; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 303 | unsigned long flags; |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 304 | u64 stolen, udelta, sys_scaled, user_scaled; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 305 | |
| 306 | local_irq_save(flags); |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 307 | now = mftb(); |
Michael Neuling | 4603ac1 | 2007-10-18 03:06:37 -0700 | [diff] [blame] | 308 | nowscaled = read_spurr(now); |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 309 | get_paca()->system_time += now - get_paca()->starttime; |
| 310 | get_paca()->starttime = now; |
Michael Neuling | 4603ac1 | 2007-10-18 03:06:37 -0700 | [diff] [blame] | 311 | deltascaled = nowscaled - get_paca()->startspurr; |
| 312 | get_paca()->startspurr = nowscaled; |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 313 | |
| 314 | stolen = calculate_stolen_time(now); |
| 315 | |
| 316 | delta = get_paca()->system_time; |
| 317 | get_paca()->system_time = 0; |
| 318 | udelta = get_paca()->user_time - get_paca()->utime_sspurr; |
| 319 | get_paca()->utime_sspurr = get_paca()->user_time; |
| 320 | |
| 321 | /* |
| 322 | * Because we don't read the SPURR on every kernel entry/exit, |
| 323 | * deltascaled includes both user and system SPURR ticks. |
| 324 | * Apportion these ticks to system SPURR ticks and user |
| 325 | * SPURR ticks in the same ratio as the system time (delta) |
| 326 | * and user time (udelta) values obtained from the timebase |
| 327 | * over the same interval. The system ticks get accounted here; |
| 328 | * the user ticks get saved up in paca->user_time_scaled to be |
| 329 | * used by account_process_tick. |
| 330 | */ |
| 331 | sys_scaled = delta; |
| 332 | user_scaled = udelta; |
| 333 | if (deltascaled != delta + udelta) { |
| 334 | if (udelta) { |
| 335 | sys_scaled = deltascaled * delta / (delta + udelta); |
| 336 | user_scaled = deltascaled - sys_scaled; |
| 337 | } else { |
| 338 | sys_scaled = deltascaled; |
| 339 | } |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 340 | } |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 341 | get_paca()->user_time_scaled += user_scaled; |
| 342 | |
| 343 | if (in_irq() || idle_task(smp_processor_id()) != tsk) { |
| 344 | account_system_time(tsk, 0, delta, sys_scaled); |
| 345 | if (stolen) |
| 346 | account_steal_time(stolen); |
| 347 | } else { |
| 348 | account_idle_time(delta + stolen); |
| 349 | } |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 350 | local_irq_restore(flags); |
| 351 | } |
Alexander Graf | 4ab79aa | 2009-10-30 05:47:19 +0000 | [diff] [blame] | 352 | EXPORT_SYMBOL_GPL(account_system_vtime); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * Transfer the user and system times accumulated in the paca |
| 356 | * by the exception entry and exit code to the generic process |
| 357 | * user and system time records. |
| 358 | * Must be called with interrupts disabled. |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 359 | * Assumes that account_system_vtime() has been called recently |
| 360 | * (i.e. since the last entry from usermode) so that |
| 361 | * get_paca()->user_time_scaled is up to date. |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 362 | */ |
Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 363 | void account_process_tick(struct task_struct *tsk, int user_tick) |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 364 | { |
Michael Neuling | 4603ac1 | 2007-10-18 03:06:37 -0700 | [diff] [blame] | 365 | cputime_t utime, utimescaled; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 366 | |
| 367 | utime = get_paca()->user_time; |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 368 | utimescaled = get_paca()->user_time_scaled; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 369 | get_paca()->user_time = 0; |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 370 | get_paca()->user_time_scaled = 0; |
| 371 | get_paca()->utime_sspurr = 0; |
Martin Schwidefsky | 457533a | 2008-12-31 15:11:37 +0100 | [diff] [blame] | 372 | account_user_time(tsk, utime, utimescaled); |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 373 | } |
| 374 | |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 375 | #else /* ! CONFIG_VIRT_CPU_ACCOUNTING */ |
| 376 | #define calc_cputime_factors() |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 377 | #endif |
| 378 | |
Paul Mackerras | 6defa38 | 2005-11-18 13:44:17 +1100 | [diff] [blame] | 379 | void __delay(unsigned long loops) |
| 380 | { |
| 381 | unsigned long start; |
| 382 | int diff; |
| 383 | |
| 384 | if (__USE_RTC()) { |
| 385 | start = get_rtcl(); |
| 386 | do { |
| 387 | /* the RTCL register wraps at 1000000000 */ |
| 388 | diff = get_rtcl() - start; |
| 389 | if (diff < 0) |
| 390 | diff += 1000000000; |
| 391 | } while (diff < loops); |
| 392 | } else { |
| 393 | start = get_tbl(); |
| 394 | while (get_tbl() - start < loops) |
| 395 | HMT_low(); |
| 396 | HMT_medium(); |
| 397 | } |
| 398 | } |
| 399 | EXPORT_SYMBOL(__delay); |
| 400 | |
| 401 | void udelay(unsigned long usecs) |
| 402 | { |
| 403 | __delay(tb_ticks_per_usec * usecs); |
| 404 | } |
| 405 | EXPORT_SYMBOL(udelay); |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | #ifdef CONFIG_SMP |
| 408 | unsigned long profile_pc(struct pt_regs *regs) |
| 409 | { |
| 410 | unsigned long pc = instruction_pointer(regs); |
| 411 | |
| 412 | if (in_lock_functions(pc)) |
| 413 | return regs->link; |
| 414 | |
| 415 | return pc; |
| 416 | } |
| 417 | EXPORT_SYMBOL(profile_pc); |
| 418 | #endif |
| 419 | |
| 420 | #ifdef CONFIG_PPC_ISERIES |
| 421 | |
| 422 | /* |
| 423 | * This function recalibrates the timebase based on the 49-bit time-of-day |
| 424 | * value in the Titan chip. The Titan is much more accurate than the value |
| 425 | * returned by the service processor for the timebase frequency. |
| 426 | */ |
| 427 | |
Tony Breeds | 71712b4 | 2007-06-22 16:54:30 +1000 | [diff] [blame] | 428 | static int __init iSeries_tb_recal(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | unsigned long titan, tb; |
Tony Breeds | 71712b4 | 2007-06-22 16:54:30 +1000 | [diff] [blame] | 431 | |
| 432 | /* Make sure we only run on iSeries */ |
| 433 | if (!firmware_has_feature(FW_FEATURE_ISERIES)) |
| 434 | return -ENODEV; |
| 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | tb = get_tb(); |
| 437 | titan = HvCallXm_loadTod(); |
| 438 | if ( iSeries_recal_titan ) { |
| 439 | unsigned long tb_ticks = tb - iSeries_recal_tb; |
| 440 | unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12; |
| 441 | unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec; |
Julia Lawall | 14ea58a | 2009-08-01 22:48:27 +0000 | [diff] [blame] | 442 | unsigned long new_tb_ticks_per_jiffy = |
| 443 | DIV_ROUND_CLOSEST(new_tb_ticks_per_sec, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy; |
| 445 | char sign = '+'; |
| 446 | /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */ |
| 447 | new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ; |
| 448 | |
| 449 | if ( tick_diff < 0 ) { |
| 450 | tick_diff = -tick_diff; |
| 451 | sign = '-'; |
| 452 | } |
| 453 | if ( tick_diff ) { |
| 454 | if ( tick_diff < tb_ticks_per_jiffy/25 ) { |
| 455 | printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n", |
| 456 | new_tb_ticks_per_jiffy, sign, tick_diff ); |
| 457 | tb_ticks_per_jiffy = new_tb_ticks_per_jiffy; |
| 458 | tb_ticks_per_sec = new_tb_ticks_per_sec; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 459 | calc_cputime_factors(); |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 460 | vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; |
Stanislaw Gruszka | a42548a | 2009-07-29 12:15:29 +0200 | [diff] [blame] | 461 | setup_cputime_one_jiffy(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | else { |
| 464 | printk( "Titan recalibrate: FAILED (difference > 4 percent)\n" |
| 465 | " new tb_ticks_per_jiffy = %lu\n" |
| 466 | " old tb_ticks_per_jiffy = %lu\n", |
| 467 | new_tb_ticks_per_jiffy, tb_ticks_per_jiffy ); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | iSeries_recal_titan = titan; |
| 472 | iSeries_recal_tb = tb; |
Tony Breeds | 71712b4 | 2007-06-22 16:54:30 +1000 | [diff] [blame] | 473 | |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 474 | /* Called here as now we know accurate values for the timebase */ |
| 475 | clocksource_init(); |
Tony Breeds | 71712b4 | 2007-06-22 16:54:30 +1000 | [diff] [blame] | 476 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
Tony Breeds | 71712b4 | 2007-06-22 16:54:30 +1000 | [diff] [blame] | 478 | late_initcall(iSeries_tb_recal); |
| 479 | |
| 480 | /* Called from platform early init */ |
| 481 | void __init iSeries_time_init_early(void) |
| 482 | { |
| 483 | iSeries_recal_tb = get_tb(); |
| 484 | iSeries_recal_titan = HvCallXm_loadTod(); |
| 485 | } |
| 486 | #endif /* CONFIG_PPC_ISERIES */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Paul Mackerras | 0fe1ac4 | 2010-04-13 20:46:04 +0000 | [diff] [blame] | 488 | #ifdef CONFIG_PERF_EVENTS |
Paul Mackerras | 105988c | 2009-06-17 21:50:04 +1000 | [diff] [blame] | 489 | |
Paul Mackerras | 0fe1ac4 | 2010-04-13 20:46:04 +0000 | [diff] [blame] | 490 | /* |
| 491 | * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable... |
| 492 | */ |
| 493 | #ifdef CONFIG_PPC64 |
| 494 | static inline unsigned long test_perf_event_pending(void) |
Paul Mackerras | 105988c | 2009-06-17 21:50:04 +1000 | [diff] [blame] | 495 | { |
Paul Mackerras | 0fe1ac4 | 2010-04-13 20:46:04 +0000 | [diff] [blame] | 496 | unsigned long x; |
| 497 | |
| 498 | asm volatile("lbz %0,%1(13)" |
| 499 | : "=r" (x) |
| 500 | : "i" (offsetof(struct paca_struct, perf_event_pending))); |
| 501 | return x; |
Paul Mackerras | 105988c | 2009-06-17 21:50:04 +1000 | [diff] [blame] | 502 | } |
| 503 | |
Paul Mackerras | 0fe1ac4 | 2010-04-13 20:46:04 +0000 | [diff] [blame] | 504 | static inline void set_perf_event_pending_flag(void) |
| 505 | { |
| 506 | asm volatile("stb %0,%1(13)" : : |
| 507 | "r" (1), |
| 508 | "i" (offsetof(struct paca_struct, perf_event_pending))); |
| 509 | } |
| 510 | |
| 511 | static inline void clear_perf_event_pending(void) |
| 512 | { |
| 513 | asm volatile("stb %0,%1(13)" : : |
| 514 | "r" (0), |
| 515 | "i" (offsetof(struct paca_struct, perf_event_pending))); |
| 516 | } |
| 517 | |
| 518 | #else /* 32-bit */ |
| 519 | |
| 520 | DEFINE_PER_CPU(u8, perf_event_pending); |
| 521 | |
| 522 | #define set_perf_event_pending_flag() __get_cpu_var(perf_event_pending) = 1 |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 523 | #define test_perf_event_pending() __get_cpu_var(perf_event_pending) |
| 524 | #define clear_perf_event_pending() __get_cpu_var(perf_event_pending) = 0 |
Paul Mackerras | 105988c | 2009-06-17 21:50:04 +1000 | [diff] [blame] | 525 | |
Paul Mackerras | 0fe1ac4 | 2010-04-13 20:46:04 +0000 | [diff] [blame] | 526 | #endif /* 32 vs 64 bit */ |
| 527 | |
| 528 | void set_perf_event_pending(void) |
| 529 | { |
| 530 | preempt_disable(); |
| 531 | set_perf_event_pending_flag(); |
| 532 | set_dec(1); |
| 533 | preempt_enable(); |
| 534 | } |
| 535 | |
| 536 | #else /* CONFIG_PERF_EVENTS */ |
Paul Mackerras | 105988c | 2009-06-17 21:50:04 +1000 | [diff] [blame] | 537 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 538 | #define test_perf_event_pending() 0 |
| 539 | #define clear_perf_event_pending() |
Paul Mackerras | 105988c | 2009-06-17 21:50:04 +1000 | [diff] [blame] | 540 | |
Paul Mackerras | 0fe1ac4 | 2010-04-13 20:46:04 +0000 | [diff] [blame] | 541 | #endif /* CONFIG_PERF_EVENTS */ |
Paul Mackerras | 105988c | 2009-06-17 21:50:04 +1000 | [diff] [blame] | 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | /* |
| 544 | * For iSeries shared processors, we have to let the hypervisor |
| 545 | * set the hardware decrementer. We set a virtual decrementer |
| 546 | * in the lppaca and call the hypervisor if the virtual |
| 547 | * decrementer is less than the current value in the hardware |
| 548 | * decrementer. (almost always the new decrementer value will |
| 549 | * be greater than the current hardware decementer so the hypervisor |
| 550 | * call will not be needed) |
| 551 | */ |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | /* |
| 554 | * timer_interrupt - gets called when the decrementer overflows, |
| 555 | * with interrupts disabled. |
| 556 | */ |
Kumar Gala | c7aeffc | 2005-09-19 09:30:27 -0500 | [diff] [blame] | 557 | void timer_interrupt(struct pt_regs * regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 559 | struct pt_regs *old_regs; |
Milton Miller | 6e6b44e | 2007-12-14 15:52:15 +1100 | [diff] [blame] | 560 | struct decrementer_clock *decrementer = &__get_cpu_var(decrementers); |
| 561 | struct clock_event_device *evt = &decrementer->event; |
Paul Mackerras | d968014 | 2007-10-09 09:59:17 +1000 | [diff] [blame] | 562 | u64 now; |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 563 | |
Anton Blanchard | 6795b85 | 2009-10-26 18:49:14 +0000 | [diff] [blame] | 564 | trace_timer_interrupt_entry(regs); |
| 565 | |
Anton Blanchard | 89713ed | 2010-01-31 20:34:06 +0000 | [diff] [blame] | 566 | __get_cpu_var(irq_stat).timer_irqs++; |
| 567 | |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 568 | /* Ensure a positive value is written to the decrementer, or else |
| 569 | * some CPUs will continuue to take decrementer exceptions */ |
| 570 | set_dec(DECREMENTER_MAX); |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 571 | |
Paul Mackerras | b0d278b | 2010-08-10 20:38:23 +0000 | [diff] [blame] | 572 | #if defined(CONFIG_PPC32) && defined(CONFIG_PMAC) |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 573 | if (atomic_read(&ppc_n_lost_interrupts) != 0) |
| 574 | do_IRQ(regs); |
| 575 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 577 | old_regs = set_irq_regs(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | irq_enter(); |
| 579 | |
Paul Mackerras | 0fe1ac4 | 2010-04-13 20:46:04 +0000 | [diff] [blame] | 580 | if (test_perf_event_pending()) { |
| 581 | clear_perf_event_pending(); |
| 582 | perf_event_do_pending(); |
| 583 | } |
| 584 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 585 | #ifdef CONFIG_PPC_ISERIES |
Stephen Rothwell | 501b6d2 | 2006-11-21 15:10:20 +1100 | [diff] [blame] | 586 | if (firmware_has_feature(FW_FEATURE_ISERIES)) |
| 587 | get_lppaca()->int_dword.fields.decr_int = 0; |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 588 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Paul Mackerras | b0d278b | 2010-08-10 20:38:23 +0000 | [diff] [blame] | 590 | now = get_tb_or_rtc(); |
| 591 | if (now >= decrementer->next_tb) { |
| 592 | decrementer->next_tb = ~(u64)0; |
| 593 | if (evt->event_handler) |
| 594 | evt->event_handler(evt); |
| 595 | } else { |
| 596 | now = decrementer->next_tb - now; |
| 597 | if (now <= DECREMENTER_MAX) |
| 598 | set_dec((int)now); |
| 599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 601 | #ifdef CONFIG_PPC_ISERIES |
Stephen Rothwell | 501b6d2 | 2006-11-21 15:10:20 +1100 | [diff] [blame] | 602 | if (firmware_has_feature(FW_FEATURE_ISERIES) && hvlpevent_is_pending()) |
Olaf Hering | 35a84c2 | 2006-10-07 22:08:26 +1000 | [diff] [blame] | 603 | process_hvlpevents(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | #endif |
| 605 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 606 | #ifdef CONFIG_PPC64 |
Stephen Rothwell | 8d15a3e | 2005-08-03 14:40:16 +1000 | [diff] [blame] | 607 | /* collect purr register values often, for accurate calculations */ |
Stephen Rothwell | 1ababe1 | 2005-08-03 14:35:25 +1000 | [diff] [blame] | 608 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array); |
| 610 | cu->current_tb = mfspr(SPRN_PURR); |
| 611 | } |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 612 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | irq_exit(); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 615 | set_irq_regs(old_regs); |
Anton Blanchard | 6795b85 | 2009-10-26 18:49:14 +0000 | [diff] [blame] | 616 | |
| 617 | trace_timer_interrupt_exit(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Scott Wood | 7ac5dde | 2007-12-13 04:35:19 +1100 | [diff] [blame] | 620 | #ifdef CONFIG_SUSPEND |
Paul Mackerras | d75d68c | 2010-06-20 19:04:14 +0000 | [diff] [blame] | 621 | static void generic_suspend_disable_irqs(void) |
Scott Wood | 7ac5dde | 2007-12-13 04:35:19 +1100 | [diff] [blame] | 622 | { |
Scott Wood | 7ac5dde | 2007-12-13 04:35:19 +1100 | [diff] [blame] | 623 | /* Disable the decrementer, so that it doesn't interfere |
| 624 | * with suspending. |
| 625 | */ |
| 626 | |
| 627 | set_dec(0x7fffffff); |
| 628 | local_irq_disable(); |
| 629 | set_dec(0x7fffffff); |
| 630 | } |
| 631 | |
Paul Mackerras | d75d68c | 2010-06-20 19:04:14 +0000 | [diff] [blame] | 632 | static void generic_suspend_enable_irqs(void) |
Scott Wood | 7ac5dde | 2007-12-13 04:35:19 +1100 | [diff] [blame] | 633 | { |
Scott Wood | 7ac5dde | 2007-12-13 04:35:19 +1100 | [diff] [blame] | 634 | local_irq_enable(); |
Scott Wood | 7ac5dde | 2007-12-13 04:35:19 +1100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* Overrides the weak version in kernel/power/main.c */ |
| 638 | void arch_suspend_disable_irqs(void) |
| 639 | { |
| 640 | if (ppc_md.suspend_disable_irqs) |
| 641 | ppc_md.suspend_disable_irqs(); |
| 642 | generic_suspend_disable_irqs(); |
| 643 | } |
| 644 | |
| 645 | /* Overrides the weak version in kernel/power/main.c */ |
| 646 | void arch_suspend_enable_irqs(void) |
| 647 | { |
| 648 | generic_suspend_enable_irqs(); |
| 649 | if (ppc_md.suspend_enable_irqs) |
| 650 | ppc_md.suspend_enable_irqs(); |
| 651 | } |
| 652 | #endif |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /* |
| 655 | * Scheduler clock - returns current time in nanosec units. |
| 656 | * |
| 657 | * Note: mulhdu(a, b) (multiply high double unsigned) returns |
| 658 | * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b |
| 659 | * are 64-bit unsigned numbers. |
| 660 | */ |
| 661 | unsigned long long sched_clock(void) |
| 662 | { |
Paul Mackerras | 96c4450 | 2005-10-23 17:14:56 +1000 | [diff] [blame] | 663 | if (__USE_RTC()) |
| 664 | return get_rtc(); |
Tony Breeds | fc9069fe | 2007-07-04 14:04:31 +1000 | [diff] [blame] | 665 | return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Anton Blanchard | 0bb474a4 | 2006-06-20 18:47:26 +1000 | [diff] [blame] | 668 | static int __init get_freq(char *name, int cells, unsigned long *val) |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 669 | { |
| 670 | struct device_node *cpu; |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 671 | const unsigned int *fp; |
Anton Blanchard | 0bb474a4 | 2006-06-20 18:47:26 +1000 | [diff] [blame] | 672 | int found = 0; |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 673 | |
Anton Blanchard | 0bb474a4 | 2006-06-20 18:47:26 +1000 | [diff] [blame] | 674 | /* The cpu node should have timebase and clock frequency properties */ |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 675 | cpu = of_find_node_by_type(NULL, "cpu"); |
| 676 | |
Olaf Hering | d8a8188 | 2006-02-04 10:34:56 +0100 | [diff] [blame] | 677 | if (cpu) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 678 | fp = of_get_property(cpu, name, NULL); |
Olaf Hering | d8a8188 | 2006-02-04 10:34:56 +0100 | [diff] [blame] | 679 | if (fp) { |
Anton Blanchard | 0bb474a4 | 2006-06-20 18:47:26 +1000 | [diff] [blame] | 680 | found = 1; |
Paul Mackerras | a4dc7ff | 2006-09-19 14:06:27 +1000 | [diff] [blame] | 681 | *val = of_read_ulong(fp, cells); |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 682 | } |
Anton Blanchard | 0bb474a4 | 2006-06-20 18:47:26 +1000 | [diff] [blame] | 683 | |
| 684 | of_node_put(cpu); |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 685 | } |
Anton Blanchard | 0bb474a4 | 2006-06-20 18:47:26 +1000 | [diff] [blame] | 686 | |
| 687 | return found; |
| 688 | } |
| 689 | |
Benjamin Herrenschmidt | 77c0a70 | 2009-08-28 14:25:04 +1000 | [diff] [blame] | 690 | /* should become __cpuinit when secondary_cpu_time_init also is */ |
| 691 | void start_cpu_decrementer(void) |
| 692 | { |
| 693 | #if defined(CONFIG_BOOKE) || defined(CONFIG_40x) |
| 694 | /* Clear any pending timer interrupts */ |
| 695 | mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS); |
| 696 | |
| 697 | /* Enable decrementer interrupt */ |
| 698 | mtspr(SPRN_TCR, TCR_DIE); |
| 699 | #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */ |
| 700 | } |
| 701 | |
Anton Blanchard | 0bb474a4 | 2006-06-20 18:47:26 +1000 | [diff] [blame] | 702 | void __init generic_calibrate_decr(void) |
| 703 | { |
| 704 | ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */ |
| 705 | |
| 706 | if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) && |
| 707 | !get_freq("timebase-frequency", 1, &ppc_tb_freq)) { |
| 708 | |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 709 | printk(KERN_ERR "WARNING: Estimating decrementer frequency " |
| 710 | "(not found)\n"); |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 711 | } |
Anton Blanchard | 0bb474a4 | 2006-06-20 18:47:26 +1000 | [diff] [blame] | 712 | |
| 713 | ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */ |
| 714 | |
| 715 | if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) && |
| 716 | !get_freq("clock-frequency", 1, &ppc_proc_freq)) { |
| 717 | |
| 718 | printk(KERN_ERR "WARNING: Estimating processor frequency " |
| 719 | "(not found)\n"); |
| 720 | } |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 721 | } |
Arnd Bergmann | 10f7e7c | 2005-06-23 09:43:07 +1000 | [diff] [blame] | 722 | |
Tony Breeds | aa3be5f | 2007-09-21 13:26:02 +1000 | [diff] [blame] | 723 | int update_persistent_clock(struct timespec now) |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 724 | { |
| 725 | struct rtc_time tm; |
| 726 | |
Tony Breeds | aa3be5f | 2007-09-21 13:26:02 +1000 | [diff] [blame] | 727 | if (!ppc_md.set_rtc_time) |
| 728 | return 0; |
| 729 | |
| 730 | to_tm(now.tv_sec + 1 + timezone_offset, &tm); |
| 731 | tm.tm_year -= 1900; |
| 732 | tm.tm_mon -= 1; |
| 733 | |
| 734 | return ppc_md.set_rtc_time(&tm); |
| 735 | } |
| 736 | |
Benjamin Herrenschmidt | 978d7eb | 2009-11-01 19:11:03 +0000 | [diff] [blame] | 737 | static void __read_persistent_clock(struct timespec *ts) |
Tony Breeds | aa3be5f | 2007-09-21 13:26:02 +1000 | [diff] [blame] | 738 | { |
| 739 | struct rtc_time tm; |
| 740 | static int first = 1; |
| 741 | |
Martin Schwidefsky | d90246c | 2009-08-22 22:23:13 +0200 | [diff] [blame] | 742 | ts->tv_nsec = 0; |
Tony Breeds | aa3be5f | 2007-09-21 13:26:02 +1000 | [diff] [blame] | 743 | /* XXX this is a litle fragile but will work okay in the short term */ |
| 744 | if (first) { |
| 745 | first = 0; |
| 746 | if (ppc_md.time_init) |
| 747 | timezone_offset = ppc_md.time_init(); |
| 748 | |
| 749 | /* get_boot_time() isn't guaranteed to be safe to call late */ |
Martin Schwidefsky | d90246c | 2009-08-22 22:23:13 +0200 | [diff] [blame] | 750 | if (ppc_md.get_boot_time) { |
| 751 | ts->tv_sec = ppc_md.get_boot_time() - timezone_offset; |
| 752 | return; |
| 753 | } |
Tony Breeds | aa3be5f | 2007-09-21 13:26:02 +1000 | [diff] [blame] | 754 | } |
Martin Schwidefsky | d90246c | 2009-08-22 22:23:13 +0200 | [diff] [blame] | 755 | if (!ppc_md.get_rtc_time) { |
| 756 | ts->tv_sec = 0; |
| 757 | return; |
| 758 | } |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 759 | ppc_md.get_rtc_time(&tm); |
Benjamin Herrenschmidt | 978d7eb | 2009-11-01 19:11:03 +0000 | [diff] [blame] | 760 | |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 761 | ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, |
| 762 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 763 | } |
| 764 | |
Benjamin Herrenschmidt | 978d7eb | 2009-11-01 19:11:03 +0000 | [diff] [blame] | 765 | void read_persistent_clock(struct timespec *ts) |
| 766 | { |
| 767 | __read_persistent_clock(ts); |
| 768 | |
| 769 | /* Sanitize it in case real time clock is set below EPOCH */ |
| 770 | if (ts->tv_sec < 0) { |
| 771 | ts->tv_sec = 0; |
| 772 | ts->tv_nsec = 0; |
| 773 | } |
| 774 | |
| 775 | } |
| 776 | |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 777 | /* clocksource code */ |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 778 | static cycle_t rtc_read(struct clocksource *cs) |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 779 | { |
| 780 | return (cycle_t)get_rtc(); |
| 781 | } |
| 782 | |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 783 | static cycle_t timebase_read(struct clocksource *cs) |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 784 | { |
| 785 | return (cycle_t)get_tb(); |
| 786 | } |
| 787 | |
John Stultz | 7615856 | 2010-07-13 17:56:23 -0700 | [diff] [blame] | 788 | void update_vsyscall(struct timespec *wall_time, struct timespec *wtm, |
| 789 | struct clocksource *clock, u32 mult) |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 790 | { |
John Stultz | b0797b6 | 2010-07-13 17:56:21 -0700 | [diff] [blame] | 791 | u64 new_tb_to_xs, new_stamp_xsec; |
Paul Mackerras | 0e469db | 2010-06-20 19:03:08 +0000 | [diff] [blame] | 792 | u32 frac_sec; |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 793 | |
| 794 | if (clock != &clocksource_timebase) |
| 795 | return; |
| 796 | |
| 797 | /* Make userspace gettimeofday spin until we're done. */ |
| 798 | ++vdso_data->tb_update_count; |
| 799 | smp_mb(); |
| 800 | |
| 801 | /* XXX this assumes clock->shift == 22 */ |
| 802 | /* 4611686018 ~= 2^(20+64-22) / 1e9 */ |
John Stultz | b0797b6 | 2010-07-13 17:56:21 -0700 | [diff] [blame] | 803 | new_tb_to_xs = (u64) mult * 4611686018ULL; |
John Stultz | 06d518e | 2010-07-13 17:56:22 -0700 | [diff] [blame] | 804 | new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC; |
John Stultz | b0797b6 | 2010-07-13 17:56:21 -0700 | [diff] [blame] | 805 | do_div(new_stamp_xsec, 1000000000); |
John Stultz | 06d518e | 2010-07-13 17:56:22 -0700 | [diff] [blame] | 806 | new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC; |
John Stultz | b0797b6 | 2010-07-13 17:56:21 -0700 | [diff] [blame] | 807 | |
Paul Mackerras | 0e469db | 2010-06-20 19:03:08 +0000 | [diff] [blame] | 808 | BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC); |
| 809 | /* this is tv_nsec / 1e9 as a 0.32 fraction */ |
| 810 | frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32; |
Thomas Gleixner | 47916be | 2010-07-28 21:49:22 +0200 | [diff] [blame] | 811 | |
John Stultz | b0797b6 | 2010-07-13 17:56:21 -0700 | [diff] [blame] | 812 | /* |
| 813 | * tb_update_count is used to allow the userspace gettimeofday code |
| 814 | * to assure itself that it sees a consistent view of the tb_to_xs and |
| 815 | * stamp_xsec variables. It reads the tb_update_count, then reads |
| 816 | * tb_to_xs and stamp_xsec and then reads tb_update_count again. If |
| 817 | * the two values of tb_update_count match and are even then the |
| 818 | * tb_to_xs and stamp_xsec values are consistent. If not, then it |
| 819 | * loops back and reads them again until this criteria is met. |
| 820 | * We expect the caller to have done the first increment of |
| 821 | * vdso_data->tb_update_count already. |
| 822 | */ |
| 823 | vdso_data->tb_orig_stamp = clock->cycle_last; |
| 824 | vdso_data->stamp_xsec = new_stamp_xsec; |
| 825 | vdso_data->tb_to_xs = new_tb_to_xs; |
John Stultz | 7615856 | 2010-07-13 17:56:23 -0700 | [diff] [blame] | 826 | vdso_data->wtom_clock_sec = wtm->tv_sec; |
| 827 | vdso_data->wtom_clock_nsec = wtm->tv_nsec; |
John Stultz | 06d518e | 2010-07-13 17:56:22 -0700 | [diff] [blame] | 828 | vdso_data->stamp_xtime = *wall_time; |
Thomas Gleixner | 47916be | 2010-07-28 21:49:22 +0200 | [diff] [blame] | 829 | vdso_data->stamp_sec_fraction = frac_sec; |
John Stultz | b0797b6 | 2010-07-13 17:56:21 -0700 | [diff] [blame] | 830 | smp_wmb(); |
| 831 | ++(vdso_data->tb_update_count); |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | void update_vsyscall_tz(void) |
| 835 | { |
| 836 | /* Make userspace gettimeofday spin until we're done. */ |
| 837 | ++vdso_data->tb_update_count; |
| 838 | smp_mb(); |
| 839 | vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; |
| 840 | vdso_data->tz_dsttime = sys_tz.tz_dsttime; |
| 841 | smp_mb(); |
| 842 | ++vdso_data->tb_update_count; |
| 843 | } |
| 844 | |
Michael Ellerman | 1c21a29 | 2008-05-08 14:27:19 +1000 | [diff] [blame] | 845 | static void __init clocksource_init(void) |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 846 | { |
| 847 | struct clocksource *clock; |
| 848 | |
| 849 | if (__USE_RTC()) |
| 850 | clock = &clocksource_rtc; |
| 851 | else |
| 852 | clock = &clocksource_timebase; |
| 853 | |
| 854 | clock->mult = clocksource_hz2mult(tb_ticks_per_sec, clock->shift); |
| 855 | |
| 856 | if (clocksource_register(clock)) { |
| 857 | printk(KERN_ERR "clocksource: %s is already registered\n", |
| 858 | clock->name); |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n", |
| 863 | clock->name, clock->mult, clock->shift); |
| 864 | } |
| 865 | |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 866 | static int decrementer_set_next_event(unsigned long evt, |
| 867 | struct clock_event_device *dev) |
| 868 | { |
Milton Miller | 6e6b44e | 2007-12-14 15:52:15 +1100 | [diff] [blame] | 869 | __get_cpu_var(decrementers).next_tb = get_tb_or_rtc() + evt; |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 870 | set_dec(evt); |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static void decrementer_set_mode(enum clock_event_mode mode, |
| 875 | struct clock_event_device *dev) |
| 876 | { |
| 877 | if (mode != CLOCK_EVT_MODE_ONESHOT) |
| 878 | decrementer_set_next_event(DECREMENTER_MAX, dev); |
| 879 | } |
| 880 | |
Stefan Roese | 3e7b484 | 2010-01-11 22:28:54 +0000 | [diff] [blame] | 881 | static inline uint64_t div_sc64(unsigned long ticks, unsigned long nsec, |
| 882 | int shift) |
| 883 | { |
| 884 | uint64_t tmp = ((uint64_t)ticks) << shift; |
| 885 | |
| 886 | do_div(tmp, nsec); |
| 887 | return tmp; |
| 888 | } |
| 889 | |
Anton Blanchard | 8d165db | 2009-05-10 13:37:36 +0000 | [diff] [blame] | 890 | static void __init setup_clockevent_multiplier(unsigned long hz) |
| 891 | { |
| 892 | u64 mult, shift = 32; |
| 893 | |
| 894 | while (1) { |
Stefan Roese | 3e7b484 | 2010-01-11 22:28:54 +0000 | [diff] [blame] | 895 | mult = div_sc64(hz, NSEC_PER_SEC, shift); |
Anton Blanchard | 8d165db | 2009-05-10 13:37:36 +0000 | [diff] [blame] | 896 | if (mult && (mult >> 32UL) == 0UL) |
| 897 | break; |
| 898 | |
| 899 | shift--; |
| 900 | } |
| 901 | |
| 902 | decrementer_clockevent.shift = shift; |
| 903 | decrementer_clockevent.mult = mult; |
| 904 | } |
| 905 | |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 906 | static void register_decrementer_clockevent(int cpu) |
| 907 | { |
Milton Miller | 6e6b44e | 2007-12-14 15:52:15 +1100 | [diff] [blame] | 908 | struct clock_event_device *dec = &per_cpu(decrementers, cpu).event; |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 909 | |
| 910 | *dec = decrementer_clockevent; |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 911 | dec->cpumask = cpumask_of(cpu); |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 912 | |
Anton Blanchard | b919ee8 | 2010-02-07 19:26:29 +0000 | [diff] [blame] | 913 | printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n", |
| 914 | dec->name, dec->mult, dec->shift, cpu); |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 915 | |
| 916 | clockevents_register_device(dec); |
| 917 | } |
| 918 | |
Milton Miller | c481887 | 2007-12-14 15:52:10 +1100 | [diff] [blame] | 919 | static void __init init_decrementer_clockevent(void) |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 920 | { |
| 921 | int cpu = smp_processor_id(); |
| 922 | |
Anton Blanchard | 8d165db | 2009-05-10 13:37:36 +0000 | [diff] [blame] | 923 | setup_clockevent_multiplier(ppc_tb_freq); |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 924 | decrementer_clockevent.max_delta_ns = |
| 925 | clockevent_delta2ns(DECREMENTER_MAX, &decrementer_clockevent); |
Paul Mackerras | 43875cc | 2007-10-31 22:25:35 +1100 | [diff] [blame] | 926 | decrementer_clockevent.min_delta_ns = |
| 927 | clockevent_delta2ns(2, &decrementer_clockevent); |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 928 | |
| 929 | register_decrementer_clockevent(cpu); |
| 930 | } |
| 931 | |
| 932 | void secondary_cpu_time_init(void) |
| 933 | { |
Benjamin Herrenschmidt | 77c0a70 | 2009-08-28 14:25:04 +1000 | [diff] [blame] | 934 | /* Start the decrementer on CPUs that have manual control |
| 935 | * such as BookE |
| 936 | */ |
| 937 | start_cpu_decrementer(); |
| 938 | |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 939 | /* FIME: Should make unrelatred change to move snapshot_timebase |
| 940 | * call here ! */ |
| 941 | register_decrementer_clockevent(smp_processor_id()); |
| 942 | } |
| 943 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 944 | /* This function is only called on the boot processor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | void __init time_init(void) |
| 946 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | struct div_result res; |
Paul Mackerras | d75d68c | 2010-06-20 19:04:14 +0000 | [diff] [blame] | 948 | u64 scale; |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 949 | unsigned shift; |
| 950 | |
Paul Mackerras | 96c4450 | 2005-10-23 17:14:56 +1000 | [diff] [blame] | 951 | if (__USE_RTC()) { |
| 952 | /* 601 processor: dec counts down by 128 every 128ns */ |
| 953 | ppc_tb_freq = 1000000000; |
Paul Mackerras | 96c4450 | 2005-10-23 17:14:56 +1000 | [diff] [blame] | 954 | } else { |
| 955 | /* Normal PowerPC with timebase register */ |
| 956 | ppc_md.calibrate_decr(); |
Olof Johansson | 224ad80 | 2006-04-12 15:20:27 -0500 | [diff] [blame] | 957 | printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n", |
Paul Mackerras | 96c4450 | 2005-10-23 17:14:56 +1000 | [diff] [blame] | 958 | ppc_tb_freq / 1000000, ppc_tb_freq % 1000000); |
Olof Johansson | 224ad80 | 2006-04-12 15:20:27 -0500 | [diff] [blame] | 959 | printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n", |
Paul Mackerras | 96c4450 | 2005-10-23 17:14:56 +1000 | [diff] [blame] | 960 | ppc_proc_freq / 1000000, ppc_proc_freq % 1000000); |
Paul Mackerras | 96c4450 | 2005-10-23 17:14:56 +1000 | [diff] [blame] | 961 | } |
Paul Mackerras | 374e99d | 2005-10-20 21:04:51 +1000 | [diff] [blame] | 962 | |
| 963 | tb_ticks_per_jiffy = ppc_tb_freq / HZ; |
Paul Mackerras | 092b8f3 | 2006-02-20 10:38:56 +1100 | [diff] [blame] | 964 | tb_ticks_per_sec = ppc_tb_freq; |
Paul Mackerras | 374e99d | 2005-10-20 21:04:51 +1000 | [diff] [blame] | 965 | tb_ticks_per_usec = ppc_tb_freq / 1000000; |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 966 | calc_cputime_factors(); |
Stanislaw Gruszka | a42548a | 2009-07-29 12:15:29 +0200 | [diff] [blame] | 967 | setup_cputime_one_jiffy(); |
Paul Mackerras | 092b8f3 | 2006-02-20 10:38:56 +1100 | [diff] [blame] | 968 | |
| 969 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | * Compute scale factor for sched_clock. |
| 971 | * The calibrate_decr() function has set tb_ticks_per_sec, |
| 972 | * which is the timebase frequency. |
| 973 | * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret |
| 974 | * the 128-bit result as a 64.64 fixed-point number. |
| 975 | * We then shift that number right until it is less than 1.0, |
| 976 | * giving us the scale factor and shift count to use in |
| 977 | * sched_clock(). |
| 978 | */ |
| 979 | div128_by_32(1000000000, 0, tb_ticks_per_sec, &res); |
| 980 | scale = res.result_low; |
| 981 | for (shift = 0; res.result_high != 0; ++shift) { |
| 982 | scale = (scale >> 1) | (res.result_high << 63); |
| 983 | res.result_high >>= 1; |
| 984 | } |
| 985 | tb_to_ns_scale = scale; |
| 986 | tb_to_ns_shift = shift; |
Tony Breeds | fc9069fe | 2007-07-04 14:04:31 +1000 | [diff] [blame] | 987 | /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */ |
Benjamin Herrenschmidt | c27da339 | 2007-09-19 14:21:56 +1000 | [diff] [blame] | 988 | boot_tb = get_tb_or_rtc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
Paul Mackerras | 092b8f3 | 2006-02-20 10:38:56 +1100 | [diff] [blame] | 990 | /* If platform provided a timezone (pmac), we correct the time */ |
| 991 | if (timezone_offset) { |
| 992 | sys_tz.tz_minuteswest = -timezone_offset / 60; |
| 993 | sys_tz.tz_dsttime = 0; |
Paul Mackerras | 092b8f3 | 2006-02-20 10:38:56 +1100 | [diff] [blame] | 994 | } |
| 995 | |
Benjamin Herrenschmidt | a7f290d | 2005-11-11 21:15:21 +1100 | [diff] [blame] | 996 | vdso_data->tb_update_count = 0; |
| 997 | vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
Benjamin Herrenschmidt | 77c0a70 | 2009-08-28 14:25:04 +1000 | [diff] [blame] | 999 | /* Start the decrementer on CPUs that have manual control |
| 1000 | * such as BookE |
| 1001 | */ |
| 1002 | start_cpu_decrementer(); |
| 1003 | |
Tony Breeds | 4a4cfe3 | 2007-09-22 07:35:52 +1000 | [diff] [blame] | 1004 | /* Register the clocksource, if we're not running on iSeries */ |
| 1005 | if (!firmware_has_feature(FW_FEATURE_ISERIES)) |
| 1006 | clocksource_init(); |
| 1007 | |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 1008 | init_decrementer_clockevent(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | #define FEBRUARY 2 |
| 1013 | #define STARTOFTIME 1970 |
| 1014 | #define SECDAY 86400L |
| 1015 | #define SECYR (SECDAY * 365) |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1016 | #define leapyear(year) ((year) % 4 == 0 && \ |
| 1017 | ((year) % 100 != 0 || (year) % 400 == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | #define days_in_year(a) (leapyear(a) ? 366 : 365) |
| 1019 | #define days_in_month(a) (month_days[(a) - 1]) |
| 1020 | |
| 1021 | static int month_days[12] = { |
| 1022 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 1023 | }; |
| 1024 | |
| 1025 | /* |
| 1026 | * This only works for the Gregorian calendar - i.e. after 1752 (in the UK) |
| 1027 | */ |
| 1028 | void GregorianDay(struct rtc_time * tm) |
| 1029 | { |
| 1030 | int leapsToDate; |
| 1031 | int lastYear; |
| 1032 | int day; |
| 1033 | int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; |
| 1034 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1035 | lastYear = tm->tm_year - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
| 1037 | /* |
| 1038 | * Number of leap corrections to apply up to end of last year |
| 1039 | */ |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1040 | leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | /* |
| 1043 | * This year is a leap year if it is divisible by 4 except when it is |
| 1044 | * divisible by 100 unless it is divisible by 400 |
| 1045 | * |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1046 | * 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] | 1047 | */ |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1048 | day = tm->tm_mon > 2 && leapyear(tm->tm_year); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
| 1050 | day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + |
| 1051 | tm->tm_mday; |
| 1052 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1053 | tm->tm_wday = day % 7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | void to_tm(int tim, struct rtc_time * tm) |
| 1057 | { |
| 1058 | register int i; |
| 1059 | register long hms, day; |
| 1060 | |
| 1061 | day = tim / SECDAY; |
| 1062 | hms = tim % SECDAY; |
| 1063 | |
| 1064 | /* Hours, minutes, seconds are easy */ |
| 1065 | tm->tm_hour = hms / 3600; |
| 1066 | tm->tm_min = (hms % 3600) / 60; |
| 1067 | tm->tm_sec = (hms % 3600) % 60; |
| 1068 | |
| 1069 | /* Number of years in days */ |
| 1070 | for (i = STARTOFTIME; day >= days_in_year(i); i++) |
| 1071 | day -= days_in_year(i); |
| 1072 | tm->tm_year = i; |
| 1073 | |
| 1074 | /* Number of months in days left */ |
| 1075 | if (leapyear(tm->tm_year)) |
| 1076 | days_in_month(FEBRUARY) = 29; |
| 1077 | for (i = 1; day >= days_in_month(i); i++) |
| 1078 | day -= days_in_month(i); |
| 1079 | days_in_month(FEBRUARY) = 28; |
| 1080 | tm->tm_mon = i; |
| 1081 | |
| 1082 | /* Days are what is left over (+1) from all that. */ |
| 1083 | tm->tm_mday = day + 1; |
| 1084 | |
| 1085 | /* |
| 1086 | * Determine the day of week |
| 1087 | */ |
| 1088 | GregorianDay(tm); |
| 1089 | } |
| 1090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | /* |
| 1092 | * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit |
| 1093 | * result. |
| 1094 | */ |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1095 | void div128_by_32(u64 dividend_high, u64 dividend_low, |
| 1096 | unsigned divisor, struct div_result *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | { |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1098 | unsigned long a, b, c, d; |
| 1099 | unsigned long w, x, y, z; |
| 1100 | u64 ra, rb, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
| 1102 | a = dividend_high >> 32; |
| 1103 | b = dividend_high & 0xffffffff; |
| 1104 | c = dividend_low >> 32; |
| 1105 | d = dividend_low & 0xffffffff; |
| 1106 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1107 | w = a / divisor; |
| 1108 | ra = ((u64)(a - (w * divisor)) << 32) + b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1110 | rb = ((u64) do_div(ra, divisor) << 32) + c; |
| 1111 | x = ra; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1113 | rc = ((u64) do_div(rb, divisor) << 32) + d; |
| 1114 | y = rb; |
| 1115 | |
| 1116 | do_div(rc, divisor); |
| 1117 | z = rc; |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 1118 | |
| 1119 | dr->result_high = ((u64)w << 32) + x; |
| 1120 | dr->result_low = ((u64)y << 32) + z; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
| 1122 | } |
Geert Uytterhoeven | bcd68a7 | 2009-02-19 16:50:46 +0100 | [diff] [blame] | 1123 | |
Benjamin Herrenschmidt | 177996e | 2009-06-09 21:12:00 +0000 | [diff] [blame] | 1124 | /* We don't need to calibrate delay, we use the CPU timebase for that */ |
| 1125 | void calibrate_delay(void) |
| 1126 | { |
| 1127 | /* Some generic code (such as spinlock debug) use loops_per_jiffy |
| 1128 | * as the number of __delay(1) in a jiffy, so make it so |
| 1129 | */ |
| 1130 | loops_per_jiffy = tb_ticks_per_jiffy; |
| 1131 | } |
| 1132 | |
Geert Uytterhoeven | bcd68a7 | 2009-02-19 16:50:46 +0100 | [diff] [blame] | 1133 | static int __init rtc_init(void) |
| 1134 | { |
| 1135 | struct platform_device *pdev; |
| 1136 | |
| 1137 | if (!ppc_md.get_rtc_time) |
| 1138 | return -ENODEV; |
| 1139 | |
| 1140 | pdev = platform_device_register_simple("rtc-generic", -1, NULL, 0); |
| 1141 | if (IS_ERR(pdev)) |
| 1142 | return PTR_ERR(pdev); |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | module_init(rtc_init); |