Skip to content

Commit bed583f

Browse files
Grant GrundlerMatthew Wilcox
authored andcommitted
[PARISC] Rewrite timer_interrupt() and gettimeoffset() using "unsigned" math.
It's just a bit easier to follow and timer code is complex enough. So far, only tested on A500-5x (64-bit SMP), ie: gettimeoffset() code hasn't been tested at all. Signed-off-by: Grant Grundler <grundler@parisc-linux.org> Signed-off-by: Kyle McMartin <kyle@parisc-linux.org>
1 parent 65ee8f0 commit bed583f

1 file changed

Lines changed: 96 additions & 44 deletions

File tree

‎arch/parisc/kernel/time.c‎

Lines changed: 96 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,86 @@
3232

3333
#include <linux/timex.h>
3434

35-
static long clocktick __read_mostly; /* timer cycles per tick */
36-
static long halftick __read_mostly;
35+
static unsigned long clocktick __read_mostly; /* timer cycles per tick */
36+
static unsigned long halftick __read_mostly;
3737

3838
#ifdef CONFIG_SMP
3939
extern void smp_do_timer(struct pt_regs *regs);
4040
#endif
4141

4242
irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
4343
{
44-
long now;
45-
long next_tick;
46-
int nticks;
44+
unsigned long now;
45+
unsigned long next_tick;
46+
unsigned long cycles_elapsed;
47+
unsigned long cycles_remainder;
48+
unsigned long ticks_elapsed = 1; /* at least one elapsed */
4749
int cpu = smp_processor_id();
4850

4951
profile_tick(CPU_PROFILING, regs);
5052

51-
now = mfctl(16);
52-
/* initialize next_tick to time at last clocktick */
53+
/* Initialize next_tick to the expected tick time. */
5354
next_tick = cpu_data[cpu].it_value;
5455

55-
/* since time passes between the interrupt and the mfctl()
56-
* above, it is never true that last_tick + clocktick == now. If we
57-
* never miss a clocktick, we could set next_tick = last_tick + clocktick
58-
* but maybe we'll miss ticks, hence the loop.
59-
*
60-
* Variables are *signed*.
56+
/* Get current interval timer.
57+
* CR16 reads as 64 bits in CPU wide mode.
58+
* CR16 reads as 32 bits in CPU narrow mode.
6159
*/
60+
now = mfctl(16);
6261

63-
nticks = 0;
64-
while((next_tick - now) < halftick) {
65-
next_tick += clocktick;
66-
nticks++;
62+
cycles_elapsed = now - next_tick;
63+
64+
/* Determine how much time elapsed. */
65+
if (now < next_tick) {
66+
/* Scenario 2: CR16 wrapped after clock tick.
67+
* 1's complement will give us the "elapse cycles".
68+
*
69+
* This "cr16 wrapped" cruft is primarily for 32-bit kernels.
70+
* So think "unsigned long is u32" when reading the code.
71+
* And yes, of course 64-bit will someday wrap, but only
72+
* every 198841 days on a 1GHz machine.
73+
*/
74+
cycles_elapsed = ~cycles_elapsed; /* off by one cycle - don't care */
6775
}
76+
77+
ticks_elapsed += cycles_elapsed / clocktick;
78+
cycles_remainder = cycles_elapsed % clocktick;
79+
80+
/* Can we differentiate between "early CR16" (aka Scenario 1) and
81+
* "long delay" (aka Scenario 3)? I don't think so.
82+
*
83+
* We expected timer_interrupt to be delivered at least a few hundred
84+
* cycles after the IT fires. But it's arbitrary how much time passes
85+
* before we call it "late". I've picked one second.
86+
*/
87+
if (ticks_elapsed > HZ) {
88+
/* Scenario 3: very long delay? bad in any case */
89+
printk (KERN_CRIT "timer_interrupt(CPU %d): delayed! run ntpdate"
90+
" ticks %ld cycles %lX rem %lX"
91+
" next/now %lX/%lX\n",
92+
cpu,
93+
ticks_elapsed, cycles_elapsed, cycles_remainder,
94+
next_tick, now );
95+
96+
ticks_elapsed = 1; /* hack to limit damage in loop below */
97+
}
98+
99+
100+
/* Determine when (in CR16 cycles) next IT interrupt will fire.
101+
* We want IT to fire modulo clocktick even if we miss/skip some.
102+
* But those interrupts don't in fact get delivered that regularly.
103+
*/
104+
next_tick = now + (clocktick - cycles_remainder);
105+
106+
/* Program the IT when to deliver the next interrupt. */
107+
/* Only bottom 32-bits of next_tick are written to cr16. */
68108
mtctl(next_tick, 16);
69109
cpu_data[cpu].it_value = next_tick;
70110

71-
while (nticks--) {
111+
/* Now that we are done mucking with unreliable delivery of interrupts,
112+
* go do system house keeping.
113+
*/
114+
while (ticks_elapsed--) {
72115
#ifdef CONFIG_SMP
73116
smp_do_timer(regs);
74117
#else
@@ -121,21 +164,41 @@ gettimeoffset (void)
121164
* Once parisc-linux learns the cr16 difference between processors,
122165
* this could be made to work.
123166
*/
124-
long last_tick;
125-
long elapsed_cycles;
167+
unsigned long now;
168+
unsigned long prev_tick;
169+
unsigned long next_tick;
170+
unsigned long elapsed_cycles;
171+
unsigned long usec;
126172

127-
/* it_value is the intended time of the next tick */
128-
last_tick = cpu_data[smp_processor_id()].it_value;
173+
next_tick = cpu_data[smp_processor_id()].it_value;
174+
now = mfctl(16); /* Read the hardware interval timer. */
129175

130-
/* Subtract one tick and account for possible difference between
131-
* when we expected the tick and when it actually arrived.
132-
* (aka wall vs real)
133-
*/
134-
last_tick -= clocktick * (jiffies - wall_jiffies + 1);
135-
elapsed_cycles = mfctl(16) - last_tick;
176+
prev_tick = next_tick - clocktick;
177+
178+
/* Assume Scenario 1: "now" is later than prev_tick. */
179+
elapsed_cycles = now - prev_tick;
180+
181+
if (now < prev_tick) {
182+
/* Scenario 2: CR16 wrapped!
183+
* 1's complement is close enough.
184+
*/
185+
elapsed_cycles = ~elapsed_cycles;
186+
}
136187

137-
/* the precision of this math could be improved */
138-
return elapsed_cycles / (PAGE0->mem_10msec / 10000);
188+
if (elapsed_cycles > (HZ * clocktick)) {
189+
/* Scenario 3: clock ticks are missing. */
190+
printk (KERN_CRIT "gettimeoffset(CPU %d): missing ticks!"
191+
"cycles %lX prev/now/next %lX/%lX/%lX clock %lX\n",
192+
cpuid,
193+
elapsed_cycles, prev_tick, now, next_tick, clocktick);
194+
}
195+
196+
/* FIXME: Can we improve the precision? Not with PAGE0. */
197+
usec = (elapsed_cycles * 10000) / PAGE0->mem_10msec;
198+
199+
/* add in "lost" jiffies */
200+
usec += clocktick * (jiffies - wall_jiffies);
201+
return usec;
139202
#else
140203
return 0;
141204
#endif
@@ -146,32 +209,21 @@ do_gettimeofday (struct timeval *tv)
146209
{
147210
unsigned long flags, seq, usec, sec;
148211

212+
/* Hold xtime_lock and adjust timeval. */
149213
do {
150214
seq = read_seqbegin_irqsave(&xtime_lock, flags);
151215
usec = gettimeoffset();
152216
sec = xtime.tv_sec;
153217
usec += (xtime.tv_nsec / 1000);
154218
} while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
155219

156-
if (unlikely(usec > LONG_MAX)) {
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-
220+
/* Move adjusted usec's into sec's. */
170221
while (usec >= USEC_PER_SEC) {
171222
usec -= USEC_PER_SEC;
172223
++sec;
173224
}
174225

226+
/* Return adjusted result. */
175227
tv->tv_sec = sec;
176228
tv->tv_usec = usec;
177229
}

0 commit comments

Comments
 (0)