3333#include <linux/timex.h>
3434
3535static unsigned long clocktick __read_mostly ; /* timer cycles per tick */
36- static unsigned long halftick __read_mostly ;
3736
3837#ifdef CONFIG_SMP
3938extern void smp_do_timer (struct pt_regs * regs );
@@ -48,6 +47,9 @@ irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
4847 unsigned long ticks_elapsed = 1 ; /* at least one elapsed */
4948 int cpu = smp_processor_id ();
5049
50+ /* gcc can optimize for "read-only" case with a local clocktick */
51+ unsigned long local_ct = clocktick ;
52+
5153 profile_tick (CPU_PROFILING , regs );
5254
5355 /* Initialize next_tick to the expected tick time. */
@@ -74,8 +76,16 @@ irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
7476 cycles_elapsed = ~cycles_elapsed ; /* off by one cycle - don't care */
7577 }
7678
77- ticks_elapsed += cycles_elapsed / clocktick ;
78- cycles_remainder = cycles_elapsed % clocktick ;
79+ if (likely (cycles_elapsed < local_ct )) {
80+ /* ticks_elapsed = 1 -- We already assumed one tick elapsed. */
81+ cycles_remainder = cycles_elapsed ;
82+ } else {
83+ /* more than one tick elapsed. Do "expensive" math. */
84+ ticks_elapsed += cycles_elapsed / local_ct ;
85+
86+ /* Faster version of "remainder = elapsed % clocktick" */
87+ cycles_remainder = cycles_elapsed - (ticks_elapsed * local_ct );
88+ }
7989
8090 /* Can we differentiate between "early CR16" (aka Scenario 1) and
8191 * "long delay" (aka Scenario 3)? I don't think so.
@@ -86,27 +96,32 @@ irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
8696 */
8797 if (ticks_elapsed > HZ ) {
8898 /* Scenario 3: very long delay? bad in any case */
89- printk (KERN_CRIT "timer_interrupt(CPU %d): delayed! run ntpdate "
99+ printk (KERN_CRIT "timer_interrupt(CPU %d): delayed!"
90100 " ticks %ld cycles %lX rem %lX"
91101 " next/now %lX/%lX\n" ,
92102 cpu ,
93103 ticks_elapsed , cycles_elapsed , cycles_remainder ,
94104 next_tick , now );
95-
96- ticks_elapsed = 1 ; /* hack to limit damage in loop below */
97105 }
98106
99107
100108 /* Determine when (in CR16 cycles) next IT interrupt will fire.
101109 * We want IT to fire modulo clocktick even if we miss/skip some.
102110 * But those interrupts don't in fact get delivered that regularly.
103111 */
104- next_tick = now + (clocktick - cycles_remainder );
112+ next_tick = now + (local_ct - cycles_remainder );
113+
114+ /* Skip one clocktick on purpose if we are likely to miss next_tick.
115+ * We'll catch what we missed on the tick after that.
116+ * We should never need 0x1000 cycles to read CR16, calc the
117+ * new next_tick, then write CR16 back. */
118+ if (!((local_ct - cycles_remainder ) >> 12 ))
119+ next_tick += local_ct ;
105120
106121 /* Program the IT when to deliver the next interrupt. */
107122 /* Only bottom 32-bits of next_tick are written to cr16. */
108- mtctl (next_tick , 16 );
109123 cpu_data [cpu ].it_value = next_tick ;
124+ mtctl (next_tick , 16 );
110125
111126 /* Now that we are done mucking with unreliable delivery of interrupts,
112127 * go do system house keeping.
@@ -169,35 +184,37 @@ gettimeoffset (void)
169184 unsigned long next_tick ;
170185 unsigned long elapsed_cycles ;
171186 unsigned long usec ;
187+ unsigned long cpuid = smp_processor_id ();
188+ unsigned long local_ct = clocktick ;
172189
173- next_tick = cpu_data [smp_processor_id () ].it_value ;
190+ next_tick = cpu_data [cpuid ].it_value ;
174191 now = mfctl (16 ); /* Read the hardware interval timer. */
175192
176- prev_tick = next_tick - clocktick ;
193+ prev_tick = next_tick - local_ct ;
177194
178195 /* Assume Scenario 1: "now" is later than prev_tick. */
179196 elapsed_cycles = now - prev_tick ;
180197
181198 if (now < prev_tick ) {
182199 /* Scenario 2: CR16 wrapped!
183- * 1's complement is close enough .
200+ * ones complement is off-by-one. Don't care .
184201 */
185202 elapsed_cycles = ~elapsed_cycles ;
186203 }
187204
188- if (elapsed_cycles > (HZ * clocktick )) {
205+ if (elapsed_cycles > (HZ * local_ct )) {
189206 /* Scenario 3: clock ticks are missing. */
190207 printk (KERN_CRIT "gettimeoffset(CPU %d): missing ticks!"
191208 "cycles %lX prev/now/next %lX/%lX/%lX clock %lX\n" ,
192209 cpuid ,
193- elapsed_cycles , prev_tick , now , next_tick , clocktick );
210+ elapsed_cycles , prev_tick , now , next_tick , local_ct );
194211 }
195212
196213 /* FIXME: Can we improve the precision? Not with PAGE0. */
197214 usec = (elapsed_cycles * 10000 ) / PAGE0 -> mem_10msec ;
198215
199216 /* add in "lost" jiffies */
200- usec += clocktick * (jiffies - wall_jiffies );
217+ usec += local_ct * (jiffies - wall_jiffies );
201218 return usec ;
202219#else
203220 return 0 ;
@@ -290,7 +307,6 @@ void __init time_init(void)
290307 static struct pdc_tod tod_data ;
291308
292309 clocktick = (100 * PAGE0 -> mem_10msec ) / HZ ;
293- halftick = clocktick / 2 ;
294310
295311 start_cpu_itimer (); /* get CPU 0 started */
296312
0 commit comments