@@ -43,12 +43,11 @@ irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
4343 unsigned long now ;
4444 unsigned long next_tick ;
4545 unsigned long cycles_elapsed ;
46- unsigned long cycles_remainder ;
47- unsigned long ticks_elapsed = 1 ; /* at least one elapsed */
48- int cpu = smp_processor_id ();
46+ unsigned long cycles_remainder ;
47+ unsigned int cpu = smp_processor_id ();
4948
5049 /* gcc can optimize for "read-only" case with a local clocktick */
51- unsigned long local_ct = clocktick ;
50+ unsigned long cpt = clocktick ;
5251
5352 profile_tick (CPU_PROFILING , regs );
5453
@@ -63,28 +62,16 @@ irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
6362
6463 cycles_elapsed = now - next_tick ;
6564
66- /* Determine how much time elapsed. */
67- if (now < next_tick ) {
68- /* Scenario 2: CR16 wrapped after clock tick.
69- * 1's complement will give us the "elapse cycles".
70- *
71- * This "cr16 wrapped" cruft is primarily for 32-bit kernels.
72- * So think "unsigned long is u32" when reading the code.
73- * And yes, of course 64-bit will someday wrap, but only
74- * every 198841 days on a 1GHz machine.
65+ if ((cycles_elapsed >> 5 ) < cpt ) {
66+ /* use "cheap" math (add/subtract) instead
67+ * of the more expensive div/mul method
7568 */
76- cycles_elapsed = ~cycles_elapsed ; /* off by one cycle - don't care */
77- }
78-
79- if (likely (cycles_elapsed < local_ct )) {
80- /* ticks_elapsed = 1 -- We already assumed one tick elapsed. */
8169 cycles_remainder = cycles_elapsed ;
70+ while (cycles_remainder > cpt ) {
71+ cycles_remainder -= cpt ;
72+ }
8273 } 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 );
74+ cycles_remainder = cycles_elapsed % cpt ;
8875 }
8976
9077 /* Can we differentiate between "early CR16" (aka Scenario 1) and
@@ -94,51 +81,65 @@ irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
9481 * cycles after the IT fires. But it's arbitrary how much time passes
9582 * before we call it "late". I've picked one second.
9683 */
97- if (ticks_elapsed > HZ ) {
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+ {
9896 /* Scenario 3: very long delay? bad in any case */
9997 printk (KERN_CRIT "timer_interrupt(CPU %d): delayed!"
100- " ticks %ld cycles %lX rem %lX"
98+ " cycles %lX rem %lX "
10199 " next/now %lX/%lX\n" ,
102100 cpu ,
103- ticks_elapsed , cycles_elapsed , cycles_remainder ,
101+ cycles_elapsed , cycles_remainder ,
104102 next_tick , now );
105103 }
106104
105+ /* convert from "division remainder" to "remainder of clock tick" */
106+ cycles_remainder = cpt - cycles_remainder ;
107107
108108 /* Determine when (in CR16 cycles) next IT interrupt will fire.
109109 * We want IT to fire modulo clocktick even if we miss/skip some.
110110 * But those interrupts don't in fact get delivered that regularly.
111111 */
112- next_tick = now + (local_ct - cycles_remainder );
112+ next_tick = now + cycles_remainder ;
113+
114+ cpu_data [cpu ].it_value = next_tick ;
113115
114116 /* 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 ;
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 ;
120123
121124 /* Program the IT when to deliver the next interrupt. */
122125 /* Only bottom 32-bits of next_tick are written to cr16. */
123- cpu_data [cpu ].it_value = next_tick ;
124126 mtctl (next_tick , 16 );
125127
126- /* Now that we are done mucking with unreliable delivery of interrupts,
127- * go do system house keeping.
128+
129+ /* Done mucking with unreliable delivery of interrupts.
130+ * Go do system house keeping.
128131 */
129- while (ticks_elapsed -- ) {
130132#ifdef CONFIG_SMP
131- smp_do_timer (regs );
133+ smp_do_timer (regs );
132134#else
133- update_process_times (user_mode (regs ));
135+ update_process_times (user_mode (regs ));
134136#endif
135- if (cpu == 0 ) {
136- write_seqlock (& xtime_lock );
137- do_timer (1 );
138- write_sequnlock (& xtime_lock );
139- }
137+ if (cpu == 0 ) {
138+ write_seqlock (& xtime_lock );
139+ do_timer (regs );
140+ write_sequnlock (& xtime_lock );
140141 }
141-
142+
142143 /* check soft power switch status */
143144 if (cpu == 0 && !atomic_read (& power_tasklet .count ))
144145 tasklet_schedule (& power_tasklet );
@@ -164,14 +165,12 @@ unsigned long profile_pc(struct pt_regs *regs)
164165EXPORT_SYMBOL (profile_pc );
165166
166167
167- /*** converted from ia64 ***/
168168/*
169169 * Return the number of micro-seconds that elapsed since the last
170170 * update to wall time (aka xtime). The xtime_lock
171171 * must be at least read-locked when calling this routine.
172172 */
173- static inline unsigned long
174- gettimeoffset (void )
173+ static inline unsigned long gettimeoffset (void )
175174{
176175#ifndef CONFIG_SMP
177176 /*
@@ -185,36 +184,40 @@ gettimeoffset (void)
185184 unsigned long elapsed_cycles ;
186185 unsigned long usec ;
187186 unsigned long cpuid = smp_processor_id ();
188- unsigned long local_ct = clocktick ;
187+ unsigned long cpt = clocktick ;
189188
190189 next_tick = cpu_data [cpuid ].it_value ;
191190 now = mfctl (16 ); /* Read the hardware interval timer. */
192191
193- prev_tick = next_tick - local_ct ;
192+ prev_tick = next_tick - cpt ;
194193
195194 /* Assume Scenario 1: "now" is later than prev_tick. */
196195 elapsed_cycles = now - prev_tick ;
197196
198- if (now < prev_tick ) {
199- /* Scenario 2: CR16 wrapped!
200- * ones complement is off-by-one. Don't care.
201- */
202- elapsed_cycles = ~elapsed_cycles ;
203- }
204-
205- if (elapsed_cycles > (HZ * local_ct )) {
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+ {
206209 /* Scenario 3: clock ticks are missing. */
207- printk (KERN_CRIT "gettimeoffset(CPU %d ): missing ticks!"
208- "cycles %lX prev/now/next %lX/%lX/%lX clock %lX\n" ,
209- cpuid ,
210- elapsed_cycles , prev_tick , now , next_tick , local_ct );
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 );
211214 }
212215
213216 /* FIXME: Can we improve the precision? Not with PAGE0. */
214217 usec = (elapsed_cycles * 10000 ) / PAGE0 -> mem_10msec ;
215218
216219 /* add in "lost" jiffies */
217- usec += local_ct * (jiffies - wall_jiffies );
220+ usec += cpt * (jiffies - wall_jiffies );
218221 return usec ;
219222#else
220223 return 0 ;
0 commit comments