]>
Commit | Line | Data |
---|---|---|
d8e5219f | 1 | /* |
1da177e4 LT |
2 | * linux/arch/cris/kernel/fasttimer.c |
3 | * | |
4 | * Fast timers for ETRAX100/ETRAX100LX | |
1da177e4 | 5 | * |
d8e5219f | 6 | * Copyright (C) 2000-2007 Axis Communications AB, Lund, Sweden |
1da177e4 LT |
7 | */ |
8 | ||
9 | #include <linux/errno.h> | |
10 | #include <linux/sched.h> | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/param.h> | |
13 | #include <linux/string.h> | |
14 | #include <linux/mm.h> | |
15 | #include <linux/vmalloc.h> | |
16 | #include <linux/interrupt.h> | |
17 | #include <linux/time.h> | |
18 | #include <linux/delay.h> | |
19 | ||
20 | #include <asm/segment.h> | |
21 | #include <asm/io.h> | |
22 | #include <asm/irq.h> | |
23 | #include <asm/delay.h> | |
1da177e4 | 24 | |
556dcee7 | 25 | #include <arch/svinto.h> |
1da177e4 LT |
26 | #include <asm/fasttimer.h> |
27 | #include <linux/proc_fs.h> | |
28 | ||
29 | ||
30 | #define DEBUG_LOG_INCLUDED | |
31 | #define FAST_TIMER_LOG | |
4200c35d | 32 | /* #define FAST_TIMER_TEST */ |
1da177e4 LT |
33 | |
34 | #define FAST_TIMER_SANITY_CHECKS | |
35 | ||
36 | #ifdef FAST_TIMER_SANITY_CHECKS | |
d8e5219f | 37 | static int sanity_failed; |
1da177e4 LT |
38 | #endif |
39 | ||
40 | #define D1(x) | |
41 | #define D2(x) | |
42 | #define DP(x) | |
43 | ||
d8e5219f JN |
44 | static unsigned int fast_timer_running; |
45 | static unsigned int fast_timers_added; | |
46 | static unsigned int fast_timers_started; | |
47 | static unsigned int fast_timers_expired; | |
48 | static unsigned int fast_timers_deleted; | |
49 | static unsigned int fast_timer_is_init; | |
50 | static unsigned int fast_timer_ints; | |
1da177e4 LT |
51 | |
52 | struct fast_timer *fast_timer_list = NULL; | |
53 | ||
54 | #ifdef DEBUG_LOG_INCLUDED | |
55 | #define DEBUG_LOG_MAX 128 | |
56 | static const char * debug_log_string[DEBUG_LOG_MAX]; | |
57 | static unsigned long debug_log_value[DEBUG_LOG_MAX]; | |
d8e5219f JN |
58 | static unsigned int debug_log_cnt; |
59 | static unsigned int debug_log_cnt_wrapped; | |
1da177e4 LT |
60 | |
61 | #define DEBUG_LOG(string, value) \ | |
62 | { \ | |
63 | unsigned long log_flags; \ | |
7e920426 | 64 | local_irq_save(log_flags); \ |
1da177e4 LT |
65 | debug_log_string[debug_log_cnt] = (string); \ |
66 | debug_log_value[debug_log_cnt] = (unsigned long)(value); \ | |
67 | if (++debug_log_cnt >= DEBUG_LOG_MAX) \ | |
68 | { \ | |
69 | debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \ | |
70 | debug_log_cnt_wrapped = 1; \ | |
71 | } \ | |
7e920426 | 72 | local_irq_restore(log_flags); \ |
1da177e4 LT |
73 | } |
74 | #else | |
75 | #define DEBUG_LOG(string, value) | |
76 | #endif | |
77 | ||
78 | ||
79 | /* The frequencies for index = clkselx number in R_TIMER_CTRL */ | |
80 | #define NUM_TIMER_FREQ 15 | |
81 | #define MAX_USABLE_TIMER_FREQ 7 | |
82 | #define MAX_DELAY_US 853333L | |
83 | const unsigned long timer_freq_100[NUM_TIMER_FREQ] = | |
84 | { | |
85 | 3, /* 0 3333 - 853333 us */ | |
86 | 6, /* 1 1666 - 426666 us */ | |
87 | 12, /* 2 833 - 213333 us */ | |
88 | 24, /* 3 416 - 106666 us */ | |
89 | 48, /* 4 208 - 53333 us */ | |
90 | 96, /* 5 104 - 26666 us */ | |
91 | 192, /* 6 52 - 13333 us */ | |
92 | 384, /* 7 26 - 6666 us */ | |
93 | 576, | |
94 | 1152, | |
95 | 2304, | |
96 | 4608, | |
97 | 9216, | |
98 | 18432, | |
99 | 62500, | |
100 | /* 15 = cascade */ | |
101 | }; | |
102 | #define NUM_TIMER_STATS 16 | |
103 | #ifdef FAST_TIMER_LOG | |
104 | struct fast_timer timer_added_log[NUM_TIMER_STATS]; | |
105 | struct fast_timer timer_started_log[NUM_TIMER_STATS]; | |
106 | struct fast_timer timer_expired_log[NUM_TIMER_STATS]; | |
107 | #endif | |
108 | ||
109 | int timer_div_settings[NUM_TIMER_STATS]; | |
110 | int timer_freq_settings[NUM_TIMER_STATS]; | |
111 | int timer_delay_settings[NUM_TIMER_STATS]; | |
112 | ||
113 | /* Not true gettimeofday, only checks the jiffies (uptime) + useconds */ | |
2f1f53bd | 114 | inline void do_gettimeofday_fast(struct fasttime_t *tv) |
1da177e4 | 115 | { |
d8e5219f JN |
116 | tv->tv_jiff = jiffies; |
117 | tv->tv_usec = GET_JIFFIES_USEC(); | |
1da177e4 LT |
118 | } |
119 | ||
2f1f53bd | 120 | inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1) |
1da177e4 | 121 | { |
d8e5219f JN |
122 | /* Compare jiffies. Takes care of wrapping */ |
123 | if (time_before(t0->tv_jiff, t1->tv_jiff)) | |
124 | return -1; | |
125 | else if (time_after(t0->tv_jiff, t1->tv_jiff)) | |
126 | return 1; | |
127 | ||
128 | /* Compare us */ | |
129 | if (t0->tv_usec < t1->tv_usec) | |
130 | return -1; | |
131 | else if (t0->tv_usec > t1->tv_usec) | |
132 | return 1; | |
133 | return 0; | |
1da177e4 LT |
134 | } |
135 | ||
2f1f53bd | 136 | inline void start_timer1(unsigned long delay_us) |
1da177e4 LT |
137 | { |
138 | int freq_index = 0; /* This is the lowest resolution */ | |
139 | unsigned long upper_limit = MAX_DELAY_US; | |
140 | ||
141 | unsigned long div; | |
142 | /* Start/Restart the timer to the new shorter value */ | |
143 | /* t = 1/freq = 1/19200 = 53us | |
144 | * T=div*t, div = T/t = delay_us*freq/1000000 | |
145 | */ | |
146 | #if 1 /* Adaptive timer settings */ | |
147 | while (delay_us < upper_limit && freq_index < MAX_USABLE_TIMER_FREQ) | |
148 | { | |
149 | freq_index++; | |
150 | upper_limit >>= 1; /* Divide by 2 using shift */ | |
151 | } | |
152 | if (freq_index > 0) | |
153 | { | |
154 | freq_index--; | |
155 | } | |
156 | #else | |
157 | freq_index = 6; | |
158 | #endif | |
159 | div = delay_us * timer_freq_100[freq_index]/10000; | |
160 | if (div < 2) | |
161 | { | |
162 | /* Maybe increase timer freq? */ | |
163 | div = 2; | |
164 | } | |
165 | if (div > 255) | |
166 | { | |
167 | div = 0; /* This means 256, the max the timer takes */ | |
168 | /* If a longer timeout than the timer can handle is used, | |
169 | * then we must restart it when it goes off. | |
170 | */ | |
171 | } | |
172 | ||
173 | timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = div; | |
174 | timer_freq_settings[fast_timers_started % NUM_TIMER_STATS] = freq_index; | |
175 | timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us; | |
176 | ||
d8e5219f | 177 | D1(printk(KERN_DEBUG "start_timer1 : %d us freq: %i div: %i\n", |
1da177e4 LT |
178 | delay_us, freq_index, div)); |
179 | /* Clear timer1 irq */ | |
180 | *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr); | |
181 | ||
182 | /* Set timer values */ | |
183 | *R_TIMER_CTRL = r_timer_ctrl_shadow = | |
184 | (r_timer_ctrl_shadow & | |
185 | ~IO_MASK(R_TIMER_CTRL, timerdiv1) & | |
186 | ~IO_MASK(R_TIMER_CTRL, tm1) & | |
187 | ~IO_MASK(R_TIMER_CTRL, clksel1)) | | |
188 | IO_FIELD(R_TIMER_CTRL, timerdiv1, div) | | |
189 | IO_STATE(R_TIMER_CTRL, tm1, stop_ld) | | |
190 | IO_FIELD(R_TIMER_CTRL, clksel1, freq_index ); /* 6=c19k2Hz */ | |
191 | ||
192 | /* Ack interrupt */ | |
193 | *R_TIMER_CTRL = r_timer_ctrl_shadow | | |
194 | IO_STATE(R_TIMER_CTRL, i1, clr); | |
195 | ||
196 | /* Start timer */ | |
197 | *R_TIMER_CTRL = r_timer_ctrl_shadow = | |
198 | (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) | | |
199 | IO_STATE(R_TIMER_CTRL, tm1, run); | |
200 | ||
201 | /* Enable timer1 irq */ | |
202 | *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, timer1, set); | |
203 | fast_timers_started++; | |
204 | fast_timer_running = 1; | |
205 | } | |
206 | ||
207 | /* In version 1.4 this function takes 27 - 50 us */ | |
208 | void start_one_shot_timer(struct fast_timer *t, | |
209 | fast_timer_function_type *function, | |
210 | unsigned long data, | |
211 | unsigned long delay_us, | |
212 | const char *name) | |
213 | { | |
214 | unsigned long flags; | |
215 | struct fast_timer *tmp; | |
216 | ||
217 | D1(printk("sft %s %d us\n", name, delay_us)); | |
218 | ||
7e920426 | 219 | local_irq_save(flags); |
1da177e4 LT |
220 | |
221 | do_gettimeofday_fast(&t->tv_set); | |
222 | tmp = fast_timer_list; | |
223 | ||
4200c35d JN |
224 | #ifdef FAST_TIMER_SANITY_CHECKS |
225 | /* Check so this is not in the list already... */ | |
226 | while (tmp != NULL) { | |
227 | if (tmp == t) { | |
228 | printk(KERN_WARNING "timer name: %s data: " | |
229 | "0x%08lX already in list!\n", name, data); | |
230 | sanity_failed++; | |
231 | goto done; | |
232 | } else | |
233 | tmp = tmp->next; | |
234 | } | |
235 | tmp = fast_timer_list; | |
236 | #endif | |
1da177e4 LT |
237 | |
238 | t->delay_us = delay_us; | |
239 | t->function = function; | |
240 | t->data = data; | |
241 | t->name = name; | |
242 | ||
243 | t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000; | |
d8e5219f | 244 | t->tv_expires.tv_jiff = t->tv_set.tv_jiff + delay_us / 1000000 / HZ; |
1da177e4 LT |
245 | if (t->tv_expires.tv_usec > 1000000) |
246 | { | |
247 | t->tv_expires.tv_usec -= 1000000; | |
d8e5219f | 248 | t->tv_expires.tv_jiff += HZ; |
1da177e4 LT |
249 | } |
250 | #ifdef FAST_TIMER_LOG | |
251 | timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t; | |
252 | #endif | |
253 | fast_timers_added++; | |
254 | ||
255 | /* Check if this should timeout before anything else */ | |
2f1f53bd | 256 | if (tmp == NULL || fasttime_cmp(&t->tv_expires, &tmp->tv_expires) < 0) |
1da177e4 LT |
257 | { |
258 | /* Put first in list and modify the timer value */ | |
259 | t->prev = NULL; | |
260 | t->next = fast_timer_list; | |
261 | if (fast_timer_list) | |
262 | { | |
263 | fast_timer_list->prev = t; | |
264 | } | |
265 | fast_timer_list = t; | |
266 | #ifdef FAST_TIMER_LOG | |
267 | timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t; | |
268 | #endif | |
269 | start_timer1(delay_us); | |
270 | } else { | |
271 | /* Put in correct place in list */ | |
2f1f53bd JN |
272 | while (tmp->next && fasttime_cmp(&t->tv_expires, |
273 | &tmp->next->tv_expires) > 0) | |
1da177e4 LT |
274 | { |
275 | tmp = tmp->next; | |
276 | } | |
277 | /* Insert t after tmp */ | |
278 | t->prev = tmp; | |
279 | t->next = tmp->next; | |
280 | if (tmp->next) | |
281 | { | |
282 | tmp->next->prev = t; | |
283 | } | |
284 | tmp->next = t; | |
285 | } | |
286 | ||
287 | D2(printk("start_one_shot_timer: %d us done\n", delay_us)); | |
288 | ||
d8e5219f | 289 | done: |
7e920426 | 290 | local_irq_restore(flags); |
1da177e4 LT |
291 | } /* start_one_shot_timer */ |
292 | ||
293 | static inline int fast_timer_pending (const struct fast_timer * t) | |
294 | { | |
295 | return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list); | |
296 | } | |
297 | ||
298 | static inline int detach_fast_timer (struct fast_timer *t) | |
299 | { | |
300 | struct fast_timer *next, *prev; | |
301 | if (!fast_timer_pending(t)) | |
302 | return 0; | |
303 | next = t->next; | |
304 | prev = t->prev; | |
305 | if (next) | |
306 | next->prev = prev; | |
307 | if (prev) | |
308 | prev->next = next; | |
309 | else | |
310 | fast_timer_list = next; | |
311 | fast_timers_deleted++; | |
312 | return 1; | |
313 | } | |
314 | ||
315 | int del_fast_timer(struct fast_timer * t) | |
316 | { | |
317 | unsigned long flags; | |
318 | int ret; | |
319 | ||
7e920426 | 320 | local_irq_save(flags); |
1da177e4 LT |
321 | ret = detach_fast_timer(t); |
322 | t->next = t->prev = NULL; | |
7e920426 | 323 | local_irq_restore(flags); |
1da177e4 LT |
324 | return ret; |
325 | } /* del_fast_timer */ | |
326 | ||
327 | ||
328 | /* Interrupt routines or functions called in interrupt context */ | |
329 | ||
330 | /* Timer 1 interrupt handler */ | |
331 | ||
332 | static irqreturn_t | |
d8e5219f | 333 | timer1_handler(int irq, void *dev_id) |
1da177e4 LT |
334 | { |
335 | struct fast_timer *t; | |
336 | unsigned long flags; | |
337 | ||
d8e5219f JN |
338 | /* We keep interrupts disabled not only when we modify the |
339 | * fast timer list, but any time we hold a reference to a | |
340 | * timer in the list, since del_fast_timer may be called | |
341 | * from (another) interrupt context. Thus, the only time | |
342 | * when interrupts are enabled is when calling the timer | |
343 | * callback function. | |
344 | */ | |
7e920426 | 345 | local_irq_save(flags); |
1da177e4 LT |
346 | |
347 | /* Clear timer1 irq */ | |
348 | *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr); | |
349 | ||
350 | /* First stop timer, then ack interrupt */ | |
351 | /* Stop timer */ | |
352 | *R_TIMER_CTRL = r_timer_ctrl_shadow = | |
353 | (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) | | |
354 | IO_STATE(R_TIMER_CTRL, tm1, stop_ld); | |
355 | ||
356 | /* Ack interrupt */ | |
357 | *R_TIMER_CTRL = r_timer_ctrl_shadow | IO_STATE(R_TIMER_CTRL, i1, clr); | |
358 | ||
359 | fast_timer_running = 0; | |
360 | fast_timer_ints++; | |
361 | ||
1da177e4 LT |
362 | t = fast_timer_list; |
363 | while (t) | |
364 | { | |
d8e5219f JN |
365 | struct fasttime_t tv; |
366 | fast_timer_function_type *f; | |
367 | unsigned long d; | |
1da177e4 LT |
368 | |
369 | /* Has it really expired? */ | |
370 | do_gettimeofday_fast(&tv); | |
d8e5219f JN |
371 | D1(printk(KERN_DEBUG "t: %is %06ius\n", |
372 | tv.tv_jiff, tv.tv_usec)); | |
1da177e4 | 373 | |
2f1f53bd | 374 | if (fasttime_cmp(&t->tv_expires, &tv) <= 0) |
1da177e4 LT |
375 | { |
376 | /* Yes it has expired */ | |
377 | #ifdef FAST_TIMER_LOG | |
378 | timer_expired_log[fast_timers_expired % NUM_TIMER_STATS] = *t; | |
379 | #endif | |
380 | fast_timers_expired++; | |
381 | ||
382 | /* Remove this timer before call, since it may reuse the timer */ | |
1da177e4 LT |
383 | if (t->prev) |
384 | { | |
385 | t->prev->next = t->next; | |
386 | } | |
387 | else | |
388 | { | |
389 | fast_timer_list = t->next; | |
390 | } | |
391 | if (t->next) | |
392 | { | |
393 | t->next->prev = t->prev; | |
394 | } | |
395 | t->prev = NULL; | |
396 | t->next = NULL; | |
1da177e4 | 397 | |
d8e5219f JN |
398 | /* Save function callback data before enabling |
399 | * interrupts, since the timer may be removed and | |
400 | * we don't know how it was allocated | |
401 | * (e.g. ->function and ->data may become overwritten | |
402 | * after deletion if the timer was stack-allocated). | |
403 | */ | |
404 | f = t->function; | |
405 | d = t->data; | |
406 | ||
407 | if (f != NULL) { | |
408 | /* Run callback with interrupts enabled. */ | |
409 | local_irq_restore(flags); | |
410 | f(d); | |
411 | local_irq_save(flags); | |
412 | } else | |
1da177e4 | 413 | DEBUG_LOG("!timer1 %i function==NULL!\n", fast_timer_ints); |
1da177e4 LT |
414 | } |
415 | else | |
416 | { | |
417 | /* Timer is to early, let's set it again using the normal routines */ | |
418 | D1(printk(".\n")); | |
419 | } | |
420 | ||
1da177e4 LT |
421 | if ((t = fast_timer_list) != NULL) |
422 | { | |
423 | /* Start next timer.. */ | |
d8e5219f JN |
424 | long us = 0; |
425 | struct fasttime_t tv; | |
1da177e4 LT |
426 | |
427 | do_gettimeofday_fast(&tv); | |
d8e5219f JN |
428 | |
429 | /* time_after_eq takes care of wrapping */ | |
430 | if (time_after_eq(t->tv_expires.tv_jiff, tv.tv_jiff)) | |
431 | us = ((t->tv_expires.tv_jiff - tv.tv_jiff) * | |
432 | 1000000 / HZ + t->tv_expires.tv_usec - | |
433 | tv.tv_usec); | |
434 | ||
1da177e4 LT |
435 | if (us > 0) |
436 | { | |
437 | if (!fast_timer_running) | |
438 | { | |
439 | #ifdef FAST_TIMER_LOG | |
440 | timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t; | |
441 | #endif | |
442 | start_timer1(us); | |
443 | } | |
1da177e4 LT |
444 | break; |
445 | } | |
446 | else | |
447 | { | |
448 | /* Timer already expired, let's handle it better late than never. | |
449 | * The normal loop handles it | |
450 | */ | |
451 | D1(printk("e! %d\n", us)); | |
452 | } | |
453 | } | |
1da177e4 LT |
454 | } |
455 | ||
d8e5219f JN |
456 | local_irq_restore(flags); |
457 | ||
1da177e4 LT |
458 | if (!t) |
459 | { | |
460 | D1(printk("t1 stop!\n")); | |
461 | } | |
462 | ||
463 | return IRQ_HANDLED; | |
464 | } | |
465 | ||
466 | static void wake_up_func(unsigned long data) | |
467 | { | |
d77eab8c | 468 | wait_queue_head_t *sleep_wait_p = (wait_queue_head_t *)data; |
1da177e4 LT |
469 | wake_up(sleep_wait_p); |
470 | } | |
471 | ||
472 | ||
473 | /* Useful API */ | |
474 | ||
475 | void schedule_usleep(unsigned long us) | |
476 | { | |
477 | struct fast_timer t; | |
1da177e4 LT |
478 | wait_queue_head_t sleep_wait; |
479 | init_waitqueue_head(&sleep_wait); | |
1da177e4 LT |
480 | |
481 | D1(printk("schedule_usleep(%d)\n", us)); | |
1da177e4 LT |
482 | start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us, |
483 | "usleep"); | |
d8e5219f JN |
484 | /* Uninterruptible sleep on the fast timer. (The condition is somewhat |
485 | * redundant since the timer is what wakes us up.) */ | |
486 | wait_event(sleep_wait, !fast_timer_pending(&t)); | |
487 | ||
1da177e4 | 488 | D1(printk("done schedule_usleep(%d)\n", us)); |
1da177e4 LT |
489 | } |
490 | ||
491 | #ifdef CONFIG_PROC_FS | |
492 | static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len | |
493 | ,int *eof, void *data_unused); | |
1da177e4 LT |
494 | #endif /* CONFIG_PROC_FS */ |
495 | ||
496 | #ifdef CONFIG_PROC_FS | |
497 | ||
498 | /* This value is very much based on testing */ | |
499 | #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300) | |
500 | ||
501 | static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len | |
502 | ,int *eof, void *data_unused) | |
503 | { | |
504 | unsigned long flags; | |
505 | int i = 0; | |
506 | int num_to_show; | |
d8e5219f | 507 | struct fasttime_t tv; |
1da177e4 LT |
508 | struct fast_timer *t, *nextt; |
509 | static char *bigbuf = NULL; | |
510 | static unsigned long used; | |
511 | ||
512 | if (!bigbuf && !(bigbuf = vmalloc(BIG_BUF_SIZE))) | |
513 | { | |
514 | used = 0; | |
d8e5219f JN |
515 | if (buf) |
516 | buf[0] = '\0'; | |
1da177e4 LT |
517 | return 0; |
518 | } | |
519 | ||
520 | if (!offset || !used) | |
521 | { | |
522 | do_gettimeofday_fast(&tv); | |
523 | ||
524 | used = 0; | |
525 | used += sprintf(bigbuf + used, "Fast timers added: %i\n", | |
526 | fast_timers_added); | |
527 | used += sprintf(bigbuf + used, "Fast timers started: %i\n", | |
528 | fast_timers_started); | |
529 | used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n", | |
530 | fast_timer_ints); | |
531 | used += sprintf(bigbuf + used, "Fast timers expired: %i\n", | |
532 | fast_timers_expired); | |
533 | used += sprintf(bigbuf + used, "Fast timers deleted: %i\n", | |
534 | fast_timers_deleted); | |
535 | used += sprintf(bigbuf + used, "Fast timer running: %s\n", | |
536 | fast_timer_running ? "yes" : "no"); | |
537 | used += sprintf(bigbuf + used, "Current time: %lu.%06lu\n", | |
d8e5219f | 538 | (unsigned long)tv.tv_jiff, |
1da177e4 LT |
539 | (unsigned long)tv.tv_usec); |
540 | #ifdef FAST_TIMER_SANITY_CHECKS | |
541 | used += sprintf(bigbuf + used, "Sanity failed: %i\n", | |
542 | sanity_failed); | |
543 | #endif | |
544 | used += sprintf(bigbuf + used, "\n"); | |
545 | ||
546 | #ifdef DEBUG_LOG_INCLUDED | |
547 | { | |
548 | int end_i = debug_log_cnt; | |
549 | i = 0; | |
550 | ||
551 | if (debug_log_cnt_wrapped) | |
552 | { | |
553 | i = debug_log_cnt; | |
554 | } | |
555 | ||
556 | while ((i != end_i || (debug_log_cnt_wrapped && !used)) && | |
557 | used+100 < BIG_BUF_SIZE) | |
558 | { | |
559 | used += sprintf(bigbuf + used, debug_log_string[i], | |
560 | debug_log_value[i]); | |
561 | i = (i+1) % DEBUG_LOG_MAX; | |
562 | } | |
563 | } | |
564 | used += sprintf(bigbuf + used, "\n"); | |
565 | #endif | |
566 | ||
567 | num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started: | |
568 | NUM_TIMER_STATS); | |
569 | used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started); | |
570 | for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++) | |
571 | { | |
572 | int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS; | |
573 | ||
574 | #if 1 //ndef FAST_TIMER_LOG | |
575 | used += sprintf(bigbuf + used, "div: %i freq: %i delay: %i" | |
576 | "\n", | |
577 | timer_div_settings[cur], | |
578 | timer_freq_settings[cur], | |
579 | timer_delay_settings[cur] | |
580 | ); | |
581 | #endif | |
582 | #ifdef FAST_TIMER_LOG | |
583 | t = &timer_started_log[cur]; | |
584 | used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu " | |
585 | "d: %6li us data: 0x%08lX" | |
586 | "\n", | |
587 | t->name, | |
d8e5219f | 588 | (unsigned long)t->tv_set.tv_jiff, |
1da177e4 | 589 | (unsigned long)t->tv_set.tv_usec, |
d8e5219f | 590 | (unsigned long)t->tv_expires.tv_jiff, |
1da177e4 LT |
591 | (unsigned long)t->tv_expires.tv_usec, |
592 | t->delay_us, | |
593 | t->data | |
594 | ); | |
595 | #endif | |
596 | } | |
597 | used += sprintf(bigbuf + used, "\n"); | |
598 | ||
599 | #ifdef FAST_TIMER_LOG | |
600 | num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added: | |
601 | NUM_TIMER_STATS); | |
602 | used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added); | |
603 | for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++) | |
604 | { | |
605 | t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS]; | |
606 | used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu " | |
607 | "d: %6li us data: 0x%08lX" | |
608 | "\n", | |
609 | t->name, | |
d8e5219f | 610 | (unsigned long)t->tv_set.tv_jiff, |
1da177e4 | 611 | (unsigned long)t->tv_set.tv_usec, |
d8e5219f | 612 | (unsigned long)t->tv_expires.tv_jiff, |
1da177e4 LT |
613 | (unsigned long)t->tv_expires.tv_usec, |
614 | t->delay_us, | |
615 | t->data | |
616 | ); | |
617 | } | |
618 | used += sprintf(bigbuf + used, "\n"); | |
619 | ||
620 | num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired: | |
621 | NUM_TIMER_STATS); | |
622 | used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired); | |
623 | for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++) | |
624 | { | |
625 | t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS]; | |
626 | used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu " | |
627 | "d: %6li us data: 0x%08lX" | |
628 | "\n", | |
629 | t->name, | |
d8e5219f | 630 | (unsigned long)t->tv_set.tv_jiff, |
1da177e4 | 631 | (unsigned long)t->tv_set.tv_usec, |
d8e5219f | 632 | (unsigned long)t->tv_expires.tv_jiff, |
1da177e4 LT |
633 | (unsigned long)t->tv_expires.tv_usec, |
634 | t->delay_us, | |
635 | t->data | |
636 | ); | |
637 | } | |
638 | used += sprintf(bigbuf + used, "\n"); | |
639 | #endif | |
640 | ||
641 | used += sprintf(bigbuf + used, "Active timers:\n"); | |
7e920426 | 642 | local_irq_save(flags); |
1da177e4 LT |
643 | t = fast_timer_list; |
644 | while (t != NULL && (used+100 < BIG_BUF_SIZE)) | |
645 | { | |
646 | nextt = t->next; | |
7e920426 | 647 | local_irq_restore(flags); |
1da177e4 LT |
648 | used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu " |
649 | "d: %6li us data: 0x%08lX" | |
650 | /* " func: 0x%08lX" */ | |
651 | "\n", | |
652 | t->name, | |
d8e5219f | 653 | (unsigned long)t->tv_set.tv_jiff, |
1da177e4 | 654 | (unsigned long)t->tv_set.tv_usec, |
d8e5219f | 655 | (unsigned long)t->tv_expires.tv_jiff, |
1da177e4 LT |
656 | (unsigned long)t->tv_expires.tv_usec, |
657 | t->delay_us, | |
658 | t->data | |
659 | /* , t->function */ | |
660 | ); | |
d8e5219f | 661 | local_irq_save(flags); |
1da177e4 LT |
662 | if (t->next != nextt) |
663 | { | |
664 | printk(KERN_WARNING "timer removed!\n"); | |
665 | } | |
666 | t = nextt; | |
667 | } | |
7e920426 | 668 | local_irq_restore(flags); |
1da177e4 LT |
669 | } |
670 | ||
671 | if (used - offset < len) | |
672 | { | |
673 | len = used - offset; | |
674 | } | |
675 | ||
676 | memcpy(buf, bigbuf + offset, len); | |
677 | *start = buf; | |
678 | *eof = 1; | |
679 | ||
680 | return len; | |
681 | } | |
682 | #endif /* PROC_FS */ | |
683 | ||
684 | #ifdef FAST_TIMER_TEST | |
685 | static volatile unsigned long i = 0; | |
686 | static volatile int num_test_timeout = 0; | |
687 | static struct fast_timer tr[10]; | |
688 | static int exp_num[10]; | |
689 | ||
d8e5219f | 690 | static struct fasttime_t tv_exp[100]; |
1da177e4 LT |
691 | |
692 | static void test_timeout(unsigned long data) | |
693 | { | |
694 | do_gettimeofday_fast(&tv_exp[data]); | |
695 | exp_num[data] = num_test_timeout; | |
696 | ||
697 | num_test_timeout++; | |
698 | } | |
699 | ||
700 | static void test_timeout1(unsigned long data) | |
701 | { | |
702 | do_gettimeofday_fast(&tv_exp[data]); | |
703 | exp_num[data] = num_test_timeout; | |
704 | if (data < 7) | |
705 | { | |
706 | start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1"); | |
707 | i++; | |
708 | } | |
709 | num_test_timeout++; | |
710 | } | |
711 | ||
712 | DP( | |
713 | static char buf0[2000]; | |
714 | static char buf1[2000]; | |
715 | static char buf2[2000]; | |
716 | static char buf3[2000]; | |
717 | static char buf4[2000]; | |
718 | ); | |
719 | ||
720 | static char buf5[6000]; | |
721 | static int j_u[1000]; | |
722 | ||
723 | static void fast_timer_test(void) | |
724 | { | |
725 | int prev_num; | |
726 | int j; | |
727 | ||
d8e5219f | 728 | struct fasttime_t tv, tv0, tv1, tv2; |
1da177e4 LT |
729 | |
730 | printk("fast_timer_test() start\n"); | |
731 | do_gettimeofday_fast(&tv); | |
732 | ||
733 | for (j = 0; j < 1000; j++) | |
734 | { | |
735 | j_u[j] = GET_JIFFIES_USEC(); | |
736 | } | |
737 | for (j = 0; j < 100; j++) | |
738 | { | |
739 | do_gettimeofday_fast(&tv_exp[j]); | |
740 | } | |
d8e5219f JN |
741 | printk(KERN_DEBUG "fast_timer_test() %is %06i\n", |
742 | tv.tv_jiff, tv.tv_usec); | |
1da177e4 LT |
743 | |
744 | for (j = 0; j < 1000; j++) | |
745 | { | |
746 | printk("%i %i %i %i %i\n",j_u[j], j_u[j+1], j_u[j+2], j_u[j+3], j_u[j+4]); | |
747 | j += 4; | |
748 | } | |
749 | for (j = 0; j < 100; j++) | |
750 | { | |
d8e5219f JN |
751 | printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n", |
752 | tv_exp[j].tv_jiff, tv_exp[j].tv_usec, | |
753 | tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec, | |
754 | tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec, | |
755 | tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec, | |
756 | tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec); | |
1da177e4 LT |
757 | j += 4; |
758 | } | |
759 | do_gettimeofday_fast(&tv0); | |
760 | start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0"); | |
761 | DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0)); | |
762 | i++; | |
763 | start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1"); | |
764 | DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0)); | |
765 | i++; | |
766 | start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2"); | |
767 | DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0)); | |
768 | i++; | |
769 | start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3"); | |
770 | DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0)); | |
771 | i++; | |
772 | start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx"); | |
773 | DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0)); | |
774 | i++; | |
775 | do_gettimeofday_fast(&tv1); | |
776 | ||
777 | proc_fasttimer_read(buf5, NULL, 0, 0, 0); | |
778 | ||
779 | prev_num = num_test_timeout; | |
780 | while (num_test_timeout < i) | |
781 | { | |
782 | if (num_test_timeout != prev_num) | |
783 | { | |
784 | prev_num = num_test_timeout; | |
785 | } | |
786 | } | |
787 | do_gettimeofday_fast(&tv2); | |
d8e5219f JN |
788 | printk(KERN_DEBUG "Timers started %is %06i\n", |
789 | tv0.tv_jiff, tv0.tv_usec); | |
790 | printk(KERN_DEBUG "Timers started at %is %06i\n", | |
791 | tv1.tv_jiff, tv1.tv_usec); | |
792 | printk(KERN_DEBUG "Timers done %is %06i\n", | |
793 | tv2.tv_jiff, tv2.tv_usec); | |
1da177e4 LT |
794 | DP(printk("buf0:\n"); |
795 | printk(buf0); | |
796 | printk("buf1:\n"); | |
797 | printk(buf1); | |
798 | printk("buf2:\n"); | |
799 | printk(buf2); | |
800 | printk("buf3:\n"); | |
801 | printk(buf3); | |
802 | printk("buf4:\n"); | |
803 | printk(buf4); | |
804 | ); | |
805 | printk("buf5:\n"); | |
806 | printk(buf5); | |
807 | ||
808 | printk("timers set:\n"); | |
809 | for(j = 0; j<i; j++) | |
810 | { | |
811 | struct fast_timer *t = &tr[j]; | |
812 | printk("%-10s set: %6is %06ius exp: %6is %06ius " | |
813 | "data: 0x%08X func: 0x%08X\n", | |
814 | t->name, | |
d8e5219f | 815 | t->tv_set.tv_jiff, |
1da177e4 | 816 | t->tv_set.tv_usec, |
d8e5219f | 817 | t->tv_expires.tv_jiff, |
1da177e4 LT |
818 | t->tv_expires.tv_usec, |
819 | t->data, | |
820 | t->function | |
821 | ); | |
822 | ||
823 | printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n", | |
824 | t->delay_us, | |
d8e5219f | 825 | tv_exp[j].tv_jiff, |
1da177e4 LT |
826 | tv_exp[j].tv_usec, |
827 | exp_num[j], | |
d8e5219f JN |
828 | (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) * |
829 | 1000000 + tv_exp[j].tv_usec - | |
830 | t->tv_expires.tv_usec); | |
1da177e4 LT |
831 | } |
832 | proc_fasttimer_read(buf5, NULL, 0, 0, 0); | |
833 | printk("buf5 after all done:\n"); | |
834 | printk(buf5); | |
835 | printk("fast_timer_test() done\n"); | |
836 | } | |
837 | #endif | |
838 | ||
839 | ||
d8e5219f | 840 | int fast_timer_init(void) |
1da177e4 LT |
841 | { |
842 | /* For some reason, request_irq() hangs when called froom time_init() */ | |
843 | if (!fast_timer_is_init) | |
844 | { | |
845 | #if 0 && defined(FAST_TIMER_TEST) | |
846 | int i; | |
847 | #endif | |
848 | ||
849 | printk(KERN_INFO "fast_timer_init()\n"); | |
850 | ||
851 | #if 0 && defined(FAST_TIMER_TEST) | |
852 | for (i = 0; i <= TIMER0_DIV; i++) | |
853 | { | |
854 | /* We must be careful not to get overflow... */ | |
855 | printk("%3i %6u\n", i, timer0_value_us[i]); | |
856 | } | |
857 | #endif | |
858 | #ifdef CONFIG_PROC_FS | |
e784788d | 859 | create_proc_read_entry("fasttimer", 0, NULL, proc_fasttimer_read, NULL); |
1da177e4 | 860 | #endif /* PROC_FS */ |
7e920426 | 861 | if(request_irq(TIMER1_IRQ_NBR, timer1_handler, 0, |
1da177e4 LT |
862 | "fast timer int", NULL)) |
863 | { | |
864 | printk("err: timer1 irq\n"); | |
865 | } | |
866 | fast_timer_is_init = 1; | |
867 | #ifdef FAST_TIMER_TEST | |
868 | printk("do test\n"); | |
869 | fast_timer_test(); | |
870 | #endif | |
871 | } | |
d8e5219f | 872 | return 0; |
1da177e4 | 873 | } |
d8e5219f | 874 | __initcall(fast_timer_init); |