diff options
Diffstat (limited to 'arch/parisc/kernel/time.c')
| -rw-r--r-- | arch/parisc/kernel/time.c | 208 |
1 files changed, 139 insertions, 69 deletions
diff --git a/arch/parisc/kernel/time.c b/arch/parisc/kernel/time.c index ab641d67f551..b3496b592a2d 100644 --- a/arch/parisc/kernel/time.c +++ b/arch/parisc/kernel/time.c | |||
| @@ -32,8 +32,7 @@ | |||
| 32 | 32 | ||
| 33 | #include <linux/timex.h> | 33 | #include <linux/timex.h> |
| 34 | 34 | ||
| 35 | static long clocktick __read_mostly; /* timer cycles per tick */ | 35 | static unsigned long clocktick __read_mostly; /* timer cycles per tick */ |
| 36 | static long halftick __read_mostly; | ||
| 37 | 36 | ||
| 38 | #ifdef CONFIG_SMP | 37 | #ifdef CONFIG_SMP |
| 39 | extern void smp_do_timer(struct pt_regs *regs); | 38 | extern void smp_do_timer(struct pt_regs *regs); |
| @@ -41,46 +40,106 @@ extern void smp_do_timer(struct pt_regs *regs); | |||
| 41 | 40 | ||
| 42 | irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs) | 41 | irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs) |
| 43 | { | 42 | { |
| 44 | long now; | 43 | unsigned long now; |
| 45 | long next_tick; | 44 | unsigned long next_tick; |
| 46 | int nticks; | 45 | unsigned long cycles_elapsed; |
| 47 | int cpu = smp_processor_id(); | 46 | unsigned long cycles_remainder; |
| 47 | unsigned int cpu = smp_processor_id(); | ||
| 48 | |||
| 49 | /* gcc can optimize for "read-only" case with a local clocktick */ | ||
| 50 | unsigned long cpt = clocktick; | ||
| 48 | 51 | ||
| 49 | profile_tick(CPU_PROFILING, regs); | 52 | profile_tick(CPU_PROFILING, regs); |
| 50 | 53 | ||
| 51 | now = mfctl(16); | 54 | /* Initialize next_tick to the expected tick time. */ |
| 52 | /* initialize next_tick to time at last clocktick */ | ||
| 53 | next_tick = cpu_data[cpu].it_value; | 55 | next_tick = cpu_data[cpu].it_value; |
| 54 | 56 | ||
| 55 | /* since time passes between the interrupt and the mfctl() | 57 | /* Get current interval timer. |
| 56 | * above, it is never true that last_tick + clocktick == now. If we | 58 | * CR16 reads as 64 bits in CPU wide mode. |
| 57 | * never miss a clocktick, we could set next_tick = last_tick + clocktick | 59 | * CR16 reads as 32 bits in CPU narrow mode. |
| 58 | * but maybe we'll miss ticks, hence the loop. | ||
| 59 | * | ||
| 60 | * Variables are *signed*. | ||
| 61 | */ | 60 | */ |
| 61 | now = mfctl(16); | ||
| 62 | |||
| 63 | cycles_elapsed = now - next_tick; | ||
| 62 | 64 | ||
| 63 | nticks = 0; | 65 | if ((cycles_elapsed >> 5) < cpt) { |
| 64 | while((next_tick - now) < halftick) { | 66 | /* use "cheap" math (add/subtract) instead |
| 65 | next_tick += clocktick; | 67 | * of the more expensive div/mul method |
| 66 | nticks++; | 68 | */ |
| 69 | cycles_remainder = cycles_elapsed; | ||
| 70 | while (cycles_remainder > cpt) { | ||
| 71 | cycles_remainder -= cpt; | ||
| 72 | } | ||
| 73 | } else { | ||
| 74 | cycles_remainder = cycles_elapsed % cpt; | ||
| 67 | } | 75 | } |
| 68 | mtctl(next_tick, 16); | 76 | |
| 77 | /* Can we differentiate between "early CR16" (aka Scenario 1) and | ||
| 78 | * "long delay" (aka Scenario 3)? I don't think so. | ||
| 79 | * | ||
| 80 | * We expected timer_interrupt to be delivered at least a few hundred | ||
| 81 | * cycles after the IT fires. But it's arbitrary how much time passes | ||
| 82 | * before we call it "late". I've picked one second. | ||
| 83 | */ | ||
| 84 | /* aproximate HZ with shifts. Intended math is "(elapsed/clocktick) > HZ" */ | ||
| 85 | #if HZ == 1000 | ||
| 86 | if (cycles_elapsed > (cpt << 10) ) | ||
| 87 | #elif HZ == 250 | ||
| 88 | if (cycles_elapsed > (cpt << 8) ) | ||
| 89 | #elif HZ == 100 | ||
| 90 | if (cycles_elapsed > (cpt << 7) ) | ||
| 91 | #else | ||
| 92 | #warn WTF is HZ set to anyway? | ||
| 93 | if (cycles_elapsed > (HZ * cpt) ) | ||
| 94 | #endif | ||
| 95 | { | ||
| 96 | /* Scenario 3: very long delay? bad in any case */ | ||
| 97 | printk (KERN_CRIT "timer_interrupt(CPU %d): delayed!" | ||
| 98 | " cycles %lX rem %lX " | ||
| 99 | " next/now %lX/%lX\n", | ||
| 100 | cpu, | ||
| 101 | cycles_elapsed, cycles_remainder, | ||
| 102 | next_tick, now ); | ||
| 103 | } | ||
| 104 | |||
| 105 | /* convert from "division remainder" to "remainder of clock tick" */ | ||
| 106 | cycles_remainder = cpt - cycles_remainder; | ||
| 107 | |||
| 108 | /* Determine when (in CR16 cycles) next IT interrupt will fire. | ||
| 109 | * We want IT to fire modulo clocktick even if we miss/skip some. | ||
| 110 | * But those interrupts don't in fact get delivered that regularly. | ||
| 111 | */ | ||
| 112 | next_tick = now + cycles_remainder; | ||
| 113 | |||
| 69 | cpu_data[cpu].it_value = next_tick; | 114 | cpu_data[cpu].it_value = next_tick; |
| 70 | 115 | ||
| 71 | while (nticks--) { | 116 | /* Skip one clocktick on purpose if we are likely to miss next_tick. |
| 117 | * We want to avoid the new next_tick being less than CR16. | ||
| 118 | * If that happened, itimer wouldn't fire until CR16 wrapped. | ||
| 119 | * We'll catch the tick we missed on the tick after that. | ||
| 120 | */ | ||
| 121 | if (!(cycles_remainder >> 13)) | ||
| 122 | next_tick += cpt; | ||
| 123 | |||
| 124 | /* Program the IT when to deliver the next interrupt. */ | ||
| 125 | /* Only bottom 32-bits of next_tick are written to cr16. */ | ||
| 126 | mtctl(next_tick, 16); | ||
| 127 | |||
| 128 | |||
| 129 | /* Done mucking with unreliable delivery of interrupts. | ||
| 130 | * Go do system house keeping. | ||
| 131 | */ | ||
| 72 | #ifdef CONFIG_SMP | 132 | #ifdef CONFIG_SMP |
| 73 | smp_do_timer(regs); | 133 | smp_do_timer(regs); |
| 74 | #else | 134 | #else |
| 75 | update_process_times(user_mode(regs)); | 135 | update_process_times(user_mode(regs)); |
| 76 | #endif | 136 | #endif |
| 77 | if (cpu == 0) { | 137 | if (cpu == 0) { |
| 78 | write_seqlock(&xtime_lock); | 138 | write_seqlock(&xtime_lock); |
| 79 | do_timer(1); | 139 | do_timer(regs); |
| 80 | write_sequnlock(&xtime_lock); | 140 | write_sequnlock(&xtime_lock); |
| 81 | } | ||
| 82 | } | 141 | } |
| 83 | 142 | ||
| 84 | /* check soft power switch status */ | 143 | /* check soft power switch status */ |
| 85 | if (cpu == 0 && !atomic_read(&power_tasklet.count)) | 144 | if (cpu == 0 && !atomic_read(&power_tasklet.count)) |
| 86 | tasklet_schedule(&power_tasklet); | 145 | tasklet_schedule(&power_tasklet); |
| @@ -106,14 +165,12 @@ unsigned long profile_pc(struct pt_regs *regs) | |||
| 106 | EXPORT_SYMBOL(profile_pc); | 165 | EXPORT_SYMBOL(profile_pc); |
| 107 | 166 | ||
| 108 | 167 | ||
| 109 | /*** converted from ia64 ***/ | ||
| 110 | /* | 168 | /* |
| 111 | * Return the number of micro-seconds that elapsed since the last | 169 | * Return the number of micro-seconds that elapsed since the last |
| 112 | * update to wall time (aka xtime). The xtime_lock | 170 | * update to wall time (aka xtime). The xtime_lock |
| 113 | * must be at least read-locked when calling this routine. | 171 | * must be at least read-locked when calling this routine. |
| 114 | */ | 172 | */ |
| 115 | static inline unsigned long | 173 | static inline unsigned long gettimeoffset (void) |
| 116 | gettimeoffset (void) | ||
| 117 | { | 174 | { |
| 118 | #ifndef CONFIG_SMP | 175 | #ifndef CONFIG_SMP |
| 119 | /* | 176 | /* |
| @@ -121,21 +178,44 @@ gettimeoffset (void) | |||
| 121 | * Once parisc-linux learns the cr16 difference between processors, | 178 | * Once parisc-linux learns the cr16 difference between processors, |
| 122 | * this could be made to work. | 179 | * this could be made to work. |
| 123 | */ | 180 | */ |
| 124 | long last_tick; | 181 | unsigned long now; |
| 125 | long elapsed_cycles; | 182 | unsigned long prev_tick; |
| 126 | 183 | unsigned long next_tick; | |
| 127 | /* it_value is the intended time of the next tick */ | 184 | unsigned long elapsed_cycles; |
| 128 | last_tick = cpu_data[smp_processor_id()].it_value; | 185 | unsigned long usec; |
| 129 | 186 | unsigned long cpuid = smp_processor_id(); | |
| 130 | /* Subtract one tick and account for possible difference between | 187 | unsigned long cpt = clocktick; |
| 131 | * when we expected the tick and when it actually arrived. | 188 | |
| 132 | * (aka wall vs real) | 189 | next_tick = cpu_data[cpuid].it_value; |
| 133 | */ | 190 | now = mfctl(16); /* Read the hardware interval timer. */ |
| 134 | last_tick -= clocktick * (jiffies - wall_jiffies + 1); | 191 | |
| 135 | elapsed_cycles = mfctl(16) - last_tick; | 192 | prev_tick = next_tick - cpt; |
| 193 | |||
| 194 | /* Assume Scenario 1: "now" is later than prev_tick. */ | ||
| 195 | elapsed_cycles = now - prev_tick; | ||
| 196 | |||
| 197 | /* aproximate HZ with shifts. Intended math is "(elapsed/clocktick) > HZ" */ | ||
| 198 | #if HZ == 1000 | ||
| 199 | if (elapsed_cycles > (cpt << 10) ) | ||
| 200 | #elif HZ == 250 | ||
| 201 | if (elapsed_cycles > (cpt << 8) ) | ||
| 202 | #elif HZ == 100 | ||
| 203 | if (elapsed_cycles > (cpt << 7) ) | ||
| 204 | #else | ||
| 205 | #warn WTF is HZ set to anyway? | ||
| 206 | if (elapsed_cycles > (HZ * cpt) ) | ||
| 207 | #endif | ||
| 208 | { | ||
| 209 | /* Scenario 3: clock ticks are missing. */ | ||
| 210 | printk (KERN_CRIT "gettimeoffset(CPU %ld): missing %ld ticks!" | ||
| 211 | " cycles %lX prev/now/next %lX/%lX/%lX clock %lX\n", | ||
| 212 | cpuid, elapsed_cycles / cpt, | ||
| 213 | elapsed_cycles, prev_tick, now, next_tick, cpt); | ||
| 214 | } | ||
| 136 | 215 | ||
| 137 | /* the precision of this math could be improved */ | 216 | /* FIXME: Can we improve the precision? Not with PAGE0. */ |
| 138 | return elapsed_cycles / (PAGE0->mem_10msec / 10000); | 217 | usec = (elapsed_cycles * 10000) / PAGE0->mem_10msec; |
| 218 | return usec; | ||
| 139 | #else | 219 | #else |
| 140 | return 0; | 220 | return 0; |
| 141 | #endif | 221 | #endif |
| @@ -146,6 +226,7 @@ do_gettimeofday (struct timeval *tv) | |||
| 146 | { | 226 | { |
| 147 | unsigned long flags, seq, usec, sec; | 227 | unsigned long flags, seq, usec, sec; |
| 148 | 228 | ||
| 229 | /* Hold xtime_lock and adjust timeval. */ | ||
| 149 | do { | 230 | do { |
| 150 | seq = read_seqbegin_irqsave(&xtime_lock, flags); | 231 | seq = read_seqbegin_irqsave(&xtime_lock, flags); |
| 151 | usec = gettimeoffset(); | 232 | usec = gettimeoffset(); |
| @@ -153,25 +234,13 @@ do_gettimeofday (struct timeval *tv) | |||
| 153 | usec += (xtime.tv_nsec / 1000); | 234 | usec += (xtime.tv_nsec / 1000); |
| 154 | } while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); | 235 | } while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); |
| 155 | 236 | ||
| 156 | if (unlikely(usec > LONG_MAX)) { | 237 | /* Move adjusted usec's into sec's. */ |
| 157 | /* This can happen if the gettimeoffset adjustment is | ||
| 158 | * negative and xtime.tv_nsec is smaller than the | ||
| 159 | * adjustment */ | ||
| 160 | printk(KERN_ERR "do_gettimeofday() spurious xtime.tv_nsec of %ld\n", usec); | ||
| 161 | usec += USEC_PER_SEC; | ||
| 162 | --sec; | ||
| 163 | /* This should never happen, it means the negative | ||
| 164 | * time adjustment was more than a second, so there's | ||
| 165 | * something seriously wrong */ | ||
| 166 | BUG_ON(usec > LONG_MAX); | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | while (usec >= USEC_PER_SEC) { | 238 | while (usec >= USEC_PER_SEC) { |
| 171 | usec -= USEC_PER_SEC; | 239 | usec -= USEC_PER_SEC; |
| 172 | ++sec; | 240 | ++sec; |
| 173 | } | 241 | } |
| 174 | 242 | ||
| 243 | /* Return adjusted result. */ | ||
| 175 | tv->tv_sec = sec; | 244 | tv->tv_sec = sec; |
| 176 | tv->tv_usec = usec; | 245 | tv->tv_usec = usec; |
| 177 | } | 246 | } |
| @@ -223,22 +292,23 @@ unsigned long long sched_clock(void) | |||
| 223 | } | 292 | } |
| 224 | 293 | ||
| 225 | 294 | ||
| 295 | void __init start_cpu_itimer(void) | ||
| 296 | { | ||
| 297 | unsigned int cpu = smp_processor_id(); | ||
| 298 | unsigned long next_tick = mfctl(16) + clocktick; | ||
| 299 | |||
| 300 | mtctl(next_tick, 16); /* kick off Interval Timer (CR16) */ | ||
| 301 | |||
| 302 | cpu_data[cpu].it_value = next_tick; | ||
| 303 | } | ||
| 304 | |||
| 226 | void __init time_init(void) | 305 | void __init time_init(void) |
| 227 | { | 306 | { |
| 228 | unsigned long next_tick; | ||
| 229 | static struct pdc_tod tod_data; | 307 | static struct pdc_tod tod_data; |
| 230 | 308 | ||
| 231 | clocktick = (100 * PAGE0->mem_10msec) / HZ; | 309 | clocktick = (100 * PAGE0->mem_10msec) / HZ; |
| 232 | halftick = clocktick / 2; | ||
| 233 | 310 | ||
| 234 | /* Setup clock interrupt timing */ | 311 | start_cpu_itimer(); /* get CPU 0 started */ |
| 235 | |||
| 236 | next_tick = mfctl(16); | ||
| 237 | next_tick += clocktick; | ||
| 238 | cpu_data[smp_processor_id()].it_value = next_tick; | ||
| 239 | |||
| 240 | /* kick off Itimer (CR16) */ | ||
| 241 | mtctl(next_tick, 16); | ||
| 242 | 312 | ||
| 243 | if(pdc_tod_read(&tod_data) == 0) { | 313 | if(pdc_tod_read(&tod_data) == 0) { |
| 244 | write_seqlock_irq(&xtime_lock); | 314 | write_seqlock_irq(&xtime_lock); |
