diff options
Diffstat (limited to 'arch/cris/arch-v10/kernel/fasttimer.c')
-rw-r--r-- | arch/cris/arch-v10/kernel/fasttimer.c | 977 |
1 files changed, 977 insertions, 0 deletions
diff --git a/arch/cris/arch-v10/kernel/fasttimer.c b/arch/cris/arch-v10/kernel/fasttimer.c new file mode 100644 index 000000000000..4717f7ae8e51 --- /dev/null +++ b/arch/cris/arch-v10/kernel/fasttimer.c | |||
@@ -0,0 +1,977 @@ | |||
1 | /* $Id: fasttimer.c,v 1.6 2004/05/14 10:18:39 starvik Exp $ | ||
2 | * linux/arch/cris/kernel/fasttimer.c | ||
3 | * | ||
4 | * Fast timers for ETRAX100/ETRAX100LX | ||
5 | * This may be useful in other OS than Linux so use 2 space indentation... | ||
6 | * | ||
7 | * $Log: fasttimer.c,v $ | ||
8 | * Revision 1.6 2004/05/14 10:18:39 starvik | ||
9 | * Export fast_timer_list | ||
10 | * | ||
11 | * Revision 1.5 2004/05/14 07:58:01 starvik | ||
12 | * Merge of changes from 2.4 | ||
13 | * | ||
14 | * Revision 1.4 2003/07/04 08:27:41 starvik | ||
15 | * Merge of Linux 2.5.74 | ||
16 | * | ||
17 | * Revision 1.3 2002/12/12 08:26:32 starvik | ||
18 | * Don't use C-comments inside CVS comments | ||
19 | * | ||
20 | * Revision 1.2 2002/12/11 15:42:02 starvik | ||
21 | * Extracted v10 (ETRAX 100LX) specific stuff from arch/cris/kernel/ | ||
22 | * | ||
23 | * Revision 1.1 2002/11/18 07:58:06 starvik | ||
24 | * Fast timers (from Linux 2.4) | ||
25 | * | ||
26 | * Revision 1.5 2002/10/15 06:21:39 starvik | ||
27 | * Added call to init_waitqueue_head | ||
28 | * | ||
29 | * Revision 1.4 2002/05/28 17:47:59 johana | ||
30 | * Added del_fast_timer() | ||
31 | * | ||
32 | * Revision 1.3 2002/05/28 16:16:07 johana | ||
33 | * Handle empty fast_timer_list | ||
34 | * | ||
35 | * Revision 1.2 2002/05/27 15:38:42 johana | ||
36 | * Made it compile without warnings on Linux 2.4. | ||
37 | * (includes, wait_queue, PROC_FS and snprintf) | ||
38 | * | ||
39 | * Revision 1.1 2002/05/27 15:32:25 johana | ||
40 | * arch/etrax100/kernel/fasttimer.c v1.8 from the elinux tree. | ||
41 | * | ||
42 | * Revision 1.8 2001/11/27 13:50:40 pkj | ||
43 | * Disable interrupts while stopping the timer and while modifying the | ||
44 | * list of active timers in timer1_handler() as it may be interrupted | ||
45 | * by other interrupts (e.g., the serial interrupt) which may add fast | ||
46 | * timers. | ||
47 | * | ||
48 | * Revision 1.7 2001/11/22 11:50:32 pkj | ||
49 | * * Only store information about the last 16 timers. | ||
50 | * * proc_fasttimer_read() now uses an allocated buffer, since it | ||
51 | * requires more space than just a page even for only writing the | ||
52 | * last 16 timers. The buffer is only allocated on request, so | ||
53 | * unless /proc/fasttimer is read, it is never allocated. | ||
54 | * * Renamed fast_timer_started to fast_timers_started to match | ||
55 | * fast_timers_added and fast_timers_expired. | ||
56 | * * Some clean-up. | ||
57 | * | ||
58 | * Revision 1.6 2000/12/13 14:02:08 johana | ||
59 | * Removed volatile for fast_timer_list | ||
60 | * | ||
61 | * Revision 1.5 2000/12/13 13:55:35 johana | ||
62 | * Added DEBUG_LOG, added som cli() and cleanup | ||
63 | * | ||
64 | * Revision 1.4 2000/12/05 13:48:50 johana | ||
65 | * Added range check when writing proc file, modified timer int handling | ||
66 | * | ||
67 | * Revision 1.3 2000/11/23 10:10:20 johana | ||
68 | * More debug/logging possibilities. | ||
69 | * Moved GET_JIFFIES_USEC() to timex.h and time.c | ||
70 | * | ||
71 | * Revision 1.2 2000/11/01 13:41:04 johana | ||
72 | * Clean up and bugfixes. | ||
73 | * Created new do_gettimeofday_fast() that gets a timeval struct | ||
74 | * with time based on jiffies and *R_TIMER0_DATA, uses a table | ||
75 | * for fast conversion of timer value to microseconds. | ||
76 | * (Much faster the standard do_gettimeofday() and we don't really | ||
77 | * wan't to use the true time - we wan't the "uptime" so timers don't screw up | ||
78 | * when we change the time. | ||
79 | * TODO: Add efficient support for continuous timers as well. | ||
80 | * | ||
81 | * Revision 1.1 2000/10/26 15:49:16 johana | ||
82 | * Added fasttimer, highresolution timers. | ||
83 | * | ||
84 | * Copyright (C) 2000,2001 2002 Axis Communications AB, Lund, Sweden | ||
85 | */ | ||
86 | |||
87 | #include <linux/errno.h> | ||
88 | #include <linux/sched.h> | ||
89 | #include <linux/kernel.h> | ||
90 | #include <linux/param.h> | ||
91 | #include <linux/string.h> | ||
92 | #include <linux/mm.h> | ||
93 | #include <linux/vmalloc.h> | ||
94 | #include <linux/interrupt.h> | ||
95 | #include <linux/time.h> | ||
96 | #include <linux/delay.h> | ||
97 | |||
98 | #include <asm/segment.h> | ||
99 | #include <asm/io.h> | ||
100 | #include <asm/irq.h> | ||
101 | #include <asm/delay.h> | ||
102 | #include <asm/rtc.h> | ||
103 | |||
104 | #include <linux/config.h> | ||
105 | #include <linux/version.h> | ||
106 | |||
107 | #include <asm/arch/svinto.h> | ||
108 | #include <asm/fasttimer.h> | ||
109 | #include <linux/proc_fs.h> | ||
110 | |||
111 | |||
112 | #define DEBUG_LOG_INCLUDED | ||
113 | #define FAST_TIMER_LOG | ||
114 | //#define FAST_TIMER_TEST | ||
115 | |||
116 | #define FAST_TIMER_SANITY_CHECKS | ||
117 | |||
118 | #ifdef FAST_TIMER_SANITY_CHECKS | ||
119 | #define SANITYCHECK(x) x | ||
120 | static int sanity_failed = 0; | ||
121 | #else | ||
122 | #define SANITYCHECK(x) | ||
123 | #endif | ||
124 | |||
125 | #define D1(x) | ||
126 | #define D2(x) | ||
127 | #define DP(x) | ||
128 | |||
129 | #define __INLINE__ inline | ||
130 | |||
131 | static int fast_timer_running = 0; | ||
132 | static int fast_timers_added = 0; | ||
133 | static int fast_timers_started = 0; | ||
134 | static int fast_timers_expired = 0; | ||
135 | static int fast_timers_deleted = 0; | ||
136 | static int fast_timer_is_init = 0; | ||
137 | static int fast_timer_ints = 0; | ||
138 | |||
139 | struct fast_timer *fast_timer_list = NULL; | ||
140 | |||
141 | #ifdef DEBUG_LOG_INCLUDED | ||
142 | #define DEBUG_LOG_MAX 128 | ||
143 | static const char * debug_log_string[DEBUG_LOG_MAX]; | ||
144 | static unsigned long debug_log_value[DEBUG_LOG_MAX]; | ||
145 | static int debug_log_cnt = 0; | ||
146 | static int debug_log_cnt_wrapped = 0; | ||
147 | |||
148 | #define DEBUG_LOG(string, value) \ | ||
149 | { \ | ||
150 | unsigned long log_flags; \ | ||
151 | save_flags(log_flags); \ | ||
152 | cli(); \ | ||
153 | debug_log_string[debug_log_cnt] = (string); \ | ||
154 | debug_log_value[debug_log_cnt] = (unsigned long)(value); \ | ||
155 | if (++debug_log_cnt >= DEBUG_LOG_MAX) \ | ||
156 | { \ | ||
157 | debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \ | ||
158 | debug_log_cnt_wrapped = 1; \ | ||
159 | } \ | ||
160 | restore_flags(log_flags); \ | ||
161 | } | ||
162 | #else | ||
163 | #define DEBUG_LOG(string, value) | ||
164 | #endif | ||
165 | |||
166 | |||
167 | /* The frequencies for index = clkselx number in R_TIMER_CTRL */ | ||
168 | #define NUM_TIMER_FREQ 15 | ||
169 | #define MAX_USABLE_TIMER_FREQ 7 | ||
170 | #define MAX_DELAY_US 853333L | ||
171 | const unsigned long timer_freq_100[NUM_TIMER_FREQ] = | ||
172 | { | ||
173 | 3, /* 0 3333 - 853333 us */ | ||
174 | 6, /* 1 1666 - 426666 us */ | ||
175 | 12, /* 2 833 - 213333 us */ | ||
176 | 24, /* 3 416 - 106666 us */ | ||
177 | 48, /* 4 208 - 53333 us */ | ||
178 | 96, /* 5 104 - 26666 us */ | ||
179 | 192, /* 6 52 - 13333 us */ | ||
180 | 384, /* 7 26 - 6666 us */ | ||
181 | 576, | ||
182 | 1152, | ||
183 | 2304, | ||
184 | 4608, | ||
185 | 9216, | ||
186 | 18432, | ||
187 | 62500, | ||
188 | /* 15 = cascade */ | ||
189 | }; | ||
190 | #define NUM_TIMER_STATS 16 | ||
191 | #ifdef FAST_TIMER_LOG | ||
192 | struct fast_timer timer_added_log[NUM_TIMER_STATS]; | ||
193 | struct fast_timer timer_started_log[NUM_TIMER_STATS]; | ||
194 | struct fast_timer timer_expired_log[NUM_TIMER_STATS]; | ||
195 | #endif | ||
196 | |||
197 | int timer_div_settings[NUM_TIMER_STATS]; | ||
198 | int timer_freq_settings[NUM_TIMER_STATS]; | ||
199 | int timer_delay_settings[NUM_TIMER_STATS]; | ||
200 | |||
201 | /* Not true gettimeofday, only checks the jiffies (uptime) + useconds */ | ||
202 | void __INLINE__ do_gettimeofday_fast(struct timeval *tv) | ||
203 | { | ||
204 | unsigned long sec = jiffies; | ||
205 | unsigned long usec = GET_JIFFIES_USEC(); | ||
206 | |||
207 | usec += (sec % HZ) * (1000000 / HZ); | ||
208 | sec = sec / HZ; | ||
209 | |||
210 | if (usec > 1000000) | ||
211 | { | ||
212 | usec -= 1000000; | ||
213 | sec++; | ||
214 | } | ||
215 | tv->tv_sec = sec; | ||
216 | tv->tv_usec = usec; | ||
217 | } | ||
218 | |||
219 | int __INLINE__ timeval_cmp(struct timeval *t0, struct timeval *t1) | ||
220 | { | ||
221 | if (t0->tv_sec < t1->tv_sec) | ||
222 | { | ||
223 | return -1; | ||
224 | } | ||
225 | else if (t0->tv_sec > t1->tv_sec) | ||
226 | { | ||
227 | return 1; | ||
228 | } | ||
229 | if (t0->tv_usec < t1->tv_usec) | ||
230 | { | ||
231 | return -1; | ||
232 | } | ||
233 | else if (t0->tv_usec > t1->tv_usec) | ||
234 | { | ||
235 | return 1; | ||
236 | } | ||
237 | return 0; | ||
238 | } | ||
239 | |||
240 | void __INLINE__ start_timer1(unsigned long delay_us) | ||
241 | { | ||
242 | int freq_index = 0; /* This is the lowest resolution */ | ||
243 | unsigned long upper_limit = MAX_DELAY_US; | ||
244 | |||
245 | unsigned long div; | ||
246 | /* Start/Restart the timer to the new shorter value */ | ||
247 | /* t = 1/freq = 1/19200 = 53us | ||
248 | * T=div*t, div = T/t = delay_us*freq/1000000 | ||
249 | */ | ||
250 | #if 1 /* Adaptive timer settings */ | ||
251 | while (delay_us < upper_limit && freq_index < MAX_USABLE_TIMER_FREQ) | ||
252 | { | ||
253 | freq_index++; | ||
254 | upper_limit >>= 1; /* Divide by 2 using shift */ | ||
255 | } | ||
256 | if (freq_index > 0) | ||
257 | { | ||
258 | freq_index--; | ||
259 | } | ||
260 | #else | ||
261 | freq_index = 6; | ||
262 | #endif | ||
263 | div = delay_us * timer_freq_100[freq_index]/10000; | ||
264 | if (div < 2) | ||
265 | { | ||
266 | /* Maybe increase timer freq? */ | ||
267 | div = 2; | ||
268 | } | ||
269 | if (div > 255) | ||
270 | { | ||
271 | div = 0; /* This means 256, the max the timer takes */ | ||
272 | /* If a longer timeout than the timer can handle is used, | ||
273 | * then we must restart it when it goes off. | ||
274 | */ | ||
275 | } | ||
276 | |||
277 | timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = div; | ||
278 | timer_freq_settings[fast_timers_started % NUM_TIMER_STATS] = freq_index; | ||
279 | timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us; | ||
280 | |||
281 | D1(printk("start_timer1 : %d us freq: %i div: %i\n", | ||
282 | delay_us, freq_index, div)); | ||
283 | /* Clear timer1 irq */ | ||
284 | *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr); | ||
285 | |||
286 | /* Set timer values */ | ||
287 | *R_TIMER_CTRL = r_timer_ctrl_shadow = | ||
288 | (r_timer_ctrl_shadow & | ||
289 | ~IO_MASK(R_TIMER_CTRL, timerdiv1) & | ||
290 | ~IO_MASK(R_TIMER_CTRL, tm1) & | ||
291 | ~IO_MASK(R_TIMER_CTRL, clksel1)) | | ||
292 | IO_FIELD(R_TIMER_CTRL, timerdiv1, div) | | ||
293 | IO_STATE(R_TIMER_CTRL, tm1, stop_ld) | | ||
294 | IO_FIELD(R_TIMER_CTRL, clksel1, freq_index ); /* 6=c19k2Hz */ | ||
295 | |||
296 | /* Ack interrupt */ | ||
297 | *R_TIMER_CTRL = r_timer_ctrl_shadow | | ||
298 | IO_STATE(R_TIMER_CTRL, i1, clr); | ||
299 | |||
300 | /* Start timer */ | ||
301 | *R_TIMER_CTRL = r_timer_ctrl_shadow = | ||
302 | (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) | | ||
303 | IO_STATE(R_TIMER_CTRL, tm1, run); | ||
304 | |||
305 | /* Enable timer1 irq */ | ||
306 | *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, timer1, set); | ||
307 | fast_timers_started++; | ||
308 | fast_timer_running = 1; | ||
309 | } | ||
310 | |||
311 | /* In version 1.4 this function takes 27 - 50 us */ | ||
312 | void start_one_shot_timer(struct fast_timer *t, | ||
313 | fast_timer_function_type *function, | ||
314 | unsigned long data, | ||
315 | unsigned long delay_us, | ||
316 | const char *name) | ||
317 | { | ||
318 | unsigned long flags; | ||
319 | struct fast_timer *tmp; | ||
320 | |||
321 | D1(printk("sft %s %d us\n", name, delay_us)); | ||
322 | |||
323 | save_flags(flags); | ||
324 | cli(); | ||
325 | |||
326 | do_gettimeofday_fast(&t->tv_set); | ||
327 | tmp = fast_timer_list; | ||
328 | |||
329 | SANITYCHECK({ /* Check so this is not in the list already... */ | ||
330 | while (tmp != NULL) | ||
331 | { | ||
332 | if (tmp == t) | ||
333 | { | ||
334 | printk(KERN_WARNING | ||
335 | "timer name: %s data: 0x%08lX already in list!\n", name, data); | ||
336 | sanity_failed++; | ||
337 | return; | ||
338 | } | ||
339 | else | ||
340 | { | ||
341 | tmp = tmp->next; | ||
342 | } | ||
343 | } | ||
344 | tmp = fast_timer_list; | ||
345 | }); | ||
346 | |||
347 | t->delay_us = delay_us; | ||
348 | t->function = function; | ||
349 | t->data = data; | ||
350 | t->name = name; | ||
351 | |||
352 | t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000; | ||
353 | t->tv_expires.tv_sec = t->tv_set.tv_sec + delay_us / 1000000; | ||
354 | if (t->tv_expires.tv_usec > 1000000) | ||
355 | { | ||
356 | t->tv_expires.tv_usec -= 1000000; | ||
357 | t->tv_expires.tv_sec++; | ||
358 | } | ||
359 | #ifdef FAST_TIMER_LOG | ||
360 | timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t; | ||
361 | #endif | ||
362 | fast_timers_added++; | ||
363 | |||
364 | /* Check if this should timeout before anything else */ | ||
365 | if (tmp == NULL || timeval_cmp(&t->tv_expires, &tmp->tv_expires) < 0) | ||
366 | { | ||
367 | /* Put first in list and modify the timer value */ | ||
368 | t->prev = NULL; | ||
369 | t->next = fast_timer_list; | ||
370 | if (fast_timer_list) | ||
371 | { | ||
372 | fast_timer_list->prev = t; | ||
373 | } | ||
374 | fast_timer_list = t; | ||
375 | #ifdef FAST_TIMER_LOG | ||
376 | timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t; | ||
377 | #endif | ||
378 | start_timer1(delay_us); | ||
379 | } else { | ||
380 | /* Put in correct place in list */ | ||
381 | while (tmp->next && | ||
382 | timeval_cmp(&t->tv_expires, &tmp->next->tv_expires) > 0) | ||
383 | { | ||
384 | tmp = tmp->next; | ||
385 | } | ||
386 | /* Insert t after tmp */ | ||
387 | t->prev = tmp; | ||
388 | t->next = tmp->next; | ||
389 | if (tmp->next) | ||
390 | { | ||
391 | tmp->next->prev = t; | ||
392 | } | ||
393 | tmp->next = t; | ||
394 | } | ||
395 | |||
396 | D2(printk("start_one_shot_timer: %d us done\n", delay_us)); | ||
397 | |||
398 | restore_flags(flags); | ||
399 | } /* start_one_shot_timer */ | ||
400 | |||
401 | static inline int fast_timer_pending (const struct fast_timer * t) | ||
402 | { | ||
403 | return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list); | ||
404 | } | ||
405 | |||
406 | static inline int detach_fast_timer (struct fast_timer *t) | ||
407 | { | ||
408 | struct fast_timer *next, *prev; | ||
409 | if (!fast_timer_pending(t)) | ||
410 | return 0; | ||
411 | next = t->next; | ||
412 | prev = t->prev; | ||
413 | if (next) | ||
414 | next->prev = prev; | ||
415 | if (prev) | ||
416 | prev->next = next; | ||
417 | else | ||
418 | fast_timer_list = next; | ||
419 | fast_timers_deleted++; | ||
420 | return 1; | ||
421 | } | ||
422 | |||
423 | int del_fast_timer(struct fast_timer * t) | ||
424 | { | ||
425 | unsigned long flags; | ||
426 | int ret; | ||
427 | |||
428 | save_flags(flags); | ||
429 | cli(); | ||
430 | ret = detach_fast_timer(t); | ||
431 | t->next = t->prev = NULL; | ||
432 | restore_flags(flags); | ||
433 | return ret; | ||
434 | } /* del_fast_timer */ | ||
435 | |||
436 | |||
437 | /* Interrupt routines or functions called in interrupt context */ | ||
438 | |||
439 | /* Timer 1 interrupt handler */ | ||
440 | |||
441 | static irqreturn_t | ||
442 | timer1_handler(int irq, void *dev_id, struct pt_regs *regs) | ||
443 | { | ||
444 | struct fast_timer *t; | ||
445 | unsigned long flags; | ||
446 | |||
447 | save_flags(flags); | ||
448 | cli(); | ||
449 | |||
450 | /* Clear timer1 irq */ | ||
451 | *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr); | ||
452 | |||
453 | /* First stop timer, then ack interrupt */ | ||
454 | /* Stop timer */ | ||
455 | *R_TIMER_CTRL = r_timer_ctrl_shadow = | ||
456 | (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) | | ||
457 | IO_STATE(R_TIMER_CTRL, tm1, stop_ld); | ||
458 | |||
459 | /* Ack interrupt */ | ||
460 | *R_TIMER_CTRL = r_timer_ctrl_shadow | IO_STATE(R_TIMER_CTRL, i1, clr); | ||
461 | |||
462 | fast_timer_running = 0; | ||
463 | fast_timer_ints++; | ||
464 | |||
465 | restore_flags(flags); | ||
466 | |||
467 | t = fast_timer_list; | ||
468 | while (t) | ||
469 | { | ||
470 | struct timeval tv; | ||
471 | |||
472 | /* Has it really expired? */ | ||
473 | do_gettimeofday_fast(&tv); | ||
474 | D1(printk("t: %is %06ius\n", tv.tv_sec, tv.tv_usec)); | ||
475 | |||
476 | if (timeval_cmp(&t->tv_expires, &tv) <= 0) | ||
477 | { | ||
478 | /* Yes it has expired */ | ||
479 | #ifdef FAST_TIMER_LOG | ||
480 | timer_expired_log[fast_timers_expired % NUM_TIMER_STATS] = *t; | ||
481 | #endif | ||
482 | fast_timers_expired++; | ||
483 | |||
484 | /* Remove this timer before call, since it may reuse the timer */ | ||
485 | save_flags(flags); | ||
486 | cli(); | ||
487 | if (t->prev) | ||
488 | { | ||
489 | t->prev->next = t->next; | ||
490 | } | ||
491 | else | ||
492 | { | ||
493 | fast_timer_list = t->next; | ||
494 | } | ||
495 | if (t->next) | ||
496 | { | ||
497 | t->next->prev = t->prev; | ||
498 | } | ||
499 | t->prev = NULL; | ||
500 | t->next = NULL; | ||
501 | restore_flags(flags); | ||
502 | |||
503 | if (t->function != NULL) | ||
504 | { | ||
505 | t->function(t->data); | ||
506 | } | ||
507 | else | ||
508 | { | ||
509 | DEBUG_LOG("!timer1 %i function==NULL!\n", fast_timer_ints); | ||
510 | } | ||
511 | } | ||
512 | else | ||
513 | { | ||
514 | /* Timer is to early, let's set it again using the normal routines */ | ||
515 | D1(printk(".\n")); | ||
516 | } | ||
517 | |||
518 | save_flags(flags); | ||
519 | cli(); | ||
520 | if ((t = fast_timer_list) != NULL) | ||
521 | { | ||
522 | /* Start next timer.. */ | ||
523 | long us; | ||
524 | struct timeval tv; | ||
525 | |||
526 | do_gettimeofday_fast(&tv); | ||
527 | us = ((t->tv_expires.tv_sec - tv.tv_sec) * 1000000 + | ||
528 | t->tv_expires.tv_usec - tv.tv_usec); | ||
529 | if (us > 0) | ||
530 | { | ||
531 | if (!fast_timer_running) | ||
532 | { | ||
533 | #ifdef FAST_TIMER_LOG | ||
534 | timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t; | ||
535 | #endif | ||
536 | start_timer1(us); | ||
537 | } | ||
538 | restore_flags(flags); | ||
539 | break; | ||
540 | } | ||
541 | else | ||
542 | { | ||
543 | /* Timer already expired, let's handle it better late than never. | ||
544 | * The normal loop handles it | ||
545 | */ | ||
546 | D1(printk("e! %d\n", us)); | ||
547 | } | ||
548 | } | ||
549 | restore_flags(flags); | ||
550 | } | ||
551 | |||
552 | if (!t) | ||
553 | { | ||
554 | D1(printk("t1 stop!\n")); | ||
555 | } | ||
556 | |||
557 | return IRQ_HANDLED; | ||
558 | } | ||
559 | |||
560 | static void wake_up_func(unsigned long data) | ||
561 | { | ||
562 | #ifdef DECLARE_WAITQUEUE | ||
563 | wait_queue_head_t *sleep_wait_p = (wait_queue_head_t*)data; | ||
564 | #else | ||
565 | struct wait_queue **sleep_wait_p = (struct wait_queue **)data; | ||
566 | #endif | ||
567 | wake_up(sleep_wait_p); | ||
568 | } | ||
569 | |||
570 | |||
571 | /* Useful API */ | ||
572 | |||
573 | void schedule_usleep(unsigned long us) | ||
574 | { | ||
575 | struct fast_timer t; | ||
576 | #ifdef DECLARE_WAITQUEUE | ||
577 | wait_queue_head_t sleep_wait; | ||
578 | init_waitqueue_head(&sleep_wait); | ||
579 | { | ||
580 | DECLARE_WAITQUEUE(wait, current); | ||
581 | #else | ||
582 | struct wait_queue *sleep_wait = NULL; | ||
583 | struct wait_queue wait = { current, NULL }; | ||
584 | #endif | ||
585 | |||
586 | D1(printk("schedule_usleep(%d)\n", us)); | ||
587 | add_wait_queue(&sleep_wait, &wait); | ||
588 | set_current_state(TASK_INTERRUPTIBLE); | ||
589 | start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us, | ||
590 | "usleep"); | ||
591 | schedule(); | ||
592 | set_current_state(TASK_RUNNING); | ||
593 | remove_wait_queue(&sleep_wait, &wait); | ||
594 | D1(printk("done schedule_usleep(%d)\n", us)); | ||
595 | #ifdef DECLARE_WAITQUEUE | ||
596 | } | ||
597 | #endif | ||
598 | } | ||
599 | |||
600 | #ifdef CONFIG_PROC_FS | ||
601 | static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len | ||
602 | ,int *eof, void *data_unused); | ||
603 | static struct proc_dir_entry *fasttimer_proc_entry; | ||
604 | #endif /* CONFIG_PROC_FS */ | ||
605 | |||
606 | #ifdef CONFIG_PROC_FS | ||
607 | |||
608 | /* This value is very much based on testing */ | ||
609 | #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300) | ||
610 | |||
611 | static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len | ||
612 | ,int *eof, void *data_unused) | ||
613 | { | ||
614 | unsigned long flags; | ||
615 | int i = 0; | ||
616 | int num_to_show; | ||
617 | struct timeval tv; | ||
618 | struct fast_timer *t, *nextt; | ||
619 | static char *bigbuf = NULL; | ||
620 | static unsigned long used; | ||
621 | |||
622 | if (!bigbuf && !(bigbuf = vmalloc(BIG_BUF_SIZE))) | ||
623 | { | ||
624 | used = 0; | ||
625 | bigbuf[0] = '\0'; | ||
626 | return 0; | ||
627 | } | ||
628 | |||
629 | if (!offset || !used) | ||
630 | { | ||
631 | do_gettimeofday_fast(&tv); | ||
632 | |||
633 | used = 0; | ||
634 | used += sprintf(bigbuf + used, "Fast timers added: %i\n", | ||
635 | fast_timers_added); | ||
636 | used += sprintf(bigbuf + used, "Fast timers started: %i\n", | ||
637 | fast_timers_started); | ||
638 | used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n", | ||
639 | fast_timer_ints); | ||
640 | used += sprintf(bigbuf + used, "Fast timers expired: %i\n", | ||
641 | fast_timers_expired); | ||
642 | used += sprintf(bigbuf + used, "Fast timers deleted: %i\n", | ||
643 | fast_timers_deleted); | ||
644 | used += sprintf(bigbuf + used, "Fast timer running: %s\n", | ||
645 | fast_timer_running ? "yes" : "no"); | ||
646 | used += sprintf(bigbuf + used, "Current time: %lu.%06lu\n", | ||
647 | (unsigned long)tv.tv_sec, | ||
648 | (unsigned long)tv.tv_usec); | ||
649 | #ifdef FAST_TIMER_SANITY_CHECKS | ||
650 | used += sprintf(bigbuf + used, "Sanity failed: %i\n", | ||
651 | sanity_failed); | ||
652 | #endif | ||
653 | used += sprintf(bigbuf + used, "\n"); | ||
654 | |||
655 | #ifdef DEBUG_LOG_INCLUDED | ||
656 | { | ||
657 | int end_i = debug_log_cnt; | ||
658 | i = 0; | ||
659 | |||
660 | if (debug_log_cnt_wrapped) | ||
661 | { | ||
662 | i = debug_log_cnt; | ||
663 | } | ||
664 | |||
665 | while ((i != end_i || (debug_log_cnt_wrapped && !used)) && | ||
666 | used+100 < BIG_BUF_SIZE) | ||
667 | { | ||
668 | used += sprintf(bigbuf + used, debug_log_string[i], | ||
669 | debug_log_value[i]); | ||
670 | i = (i+1) % DEBUG_LOG_MAX; | ||
671 | } | ||
672 | } | ||
673 | used += sprintf(bigbuf + used, "\n"); | ||
674 | #endif | ||
675 | |||
676 | num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started: | ||
677 | NUM_TIMER_STATS); | ||
678 | used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started); | ||
679 | for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++) | ||
680 | { | ||
681 | int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS; | ||
682 | |||
683 | #if 1 //ndef FAST_TIMER_LOG | ||
684 | used += sprintf(bigbuf + used, "div: %i freq: %i delay: %i" | ||
685 | "\n", | ||
686 | timer_div_settings[cur], | ||
687 | timer_freq_settings[cur], | ||
688 | timer_delay_settings[cur] | ||
689 | ); | ||
690 | #endif | ||
691 | #ifdef FAST_TIMER_LOG | ||
692 | t = &timer_started_log[cur]; | ||
693 | used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu " | ||
694 | "d: %6li us data: 0x%08lX" | ||
695 | "\n", | ||
696 | t->name, | ||
697 | (unsigned long)t->tv_set.tv_sec, | ||
698 | (unsigned long)t->tv_set.tv_usec, | ||
699 | (unsigned long)t->tv_expires.tv_sec, | ||
700 | (unsigned long)t->tv_expires.tv_usec, | ||
701 | t->delay_us, | ||
702 | t->data | ||
703 | ); | ||
704 | #endif | ||
705 | } | ||
706 | used += sprintf(bigbuf + used, "\n"); | ||
707 | |||
708 | #ifdef FAST_TIMER_LOG | ||
709 | num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added: | ||
710 | NUM_TIMER_STATS); | ||
711 | used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added); | ||
712 | for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++) | ||
713 | { | ||
714 | t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS]; | ||
715 | used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu " | ||
716 | "d: %6li us data: 0x%08lX" | ||
717 | "\n", | ||
718 | t->name, | ||
719 | (unsigned long)t->tv_set.tv_sec, | ||
720 | (unsigned long)t->tv_set.tv_usec, | ||
721 | (unsigned long)t->tv_expires.tv_sec, | ||
722 | (unsigned long)t->tv_expires.tv_usec, | ||
723 | t->delay_us, | ||
724 | t->data | ||
725 | ); | ||
726 | } | ||
727 | used += sprintf(bigbuf + used, "\n"); | ||
728 | |||
729 | num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired: | ||
730 | NUM_TIMER_STATS); | ||
731 | used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired); | ||
732 | for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++) | ||
733 | { | ||
734 | t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS]; | ||
735 | used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu " | ||
736 | "d: %6li us data: 0x%08lX" | ||
737 | "\n", | ||
738 | t->name, | ||
739 | (unsigned long)t->tv_set.tv_sec, | ||
740 | (unsigned long)t->tv_set.tv_usec, | ||
741 | (unsigned long)t->tv_expires.tv_sec, | ||
742 | (unsigned long)t->tv_expires.tv_usec, | ||
743 | t->delay_us, | ||
744 | t->data | ||
745 | ); | ||
746 | } | ||
747 | used += sprintf(bigbuf + used, "\n"); | ||
748 | #endif | ||
749 | |||
750 | used += sprintf(bigbuf + used, "Active timers:\n"); | ||
751 | save_flags(flags); | ||
752 | cli(); | ||
753 | t = fast_timer_list; | ||
754 | while (t != NULL && (used+100 < BIG_BUF_SIZE)) | ||
755 | { | ||
756 | nextt = t->next; | ||
757 | restore_flags(flags); | ||
758 | used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu " | ||
759 | "d: %6li us data: 0x%08lX" | ||
760 | /* " func: 0x%08lX" */ | ||
761 | "\n", | ||
762 | t->name, | ||
763 | (unsigned long)t->tv_set.tv_sec, | ||
764 | (unsigned long)t->tv_set.tv_usec, | ||
765 | (unsigned long)t->tv_expires.tv_sec, | ||
766 | (unsigned long)t->tv_expires.tv_usec, | ||
767 | t->delay_us, | ||
768 | t->data | ||
769 | /* , t->function */ | ||
770 | ); | ||
771 | cli(); | ||
772 | if (t->next != nextt) | ||
773 | { | ||
774 | printk(KERN_WARNING "timer removed!\n"); | ||
775 | } | ||
776 | t = nextt; | ||
777 | } | ||
778 | restore_flags(flags); | ||
779 | } | ||
780 | |||
781 | if (used - offset < len) | ||
782 | { | ||
783 | len = used - offset; | ||
784 | } | ||
785 | |||
786 | memcpy(buf, bigbuf + offset, len); | ||
787 | *start = buf; | ||
788 | *eof = 1; | ||
789 | |||
790 | return len; | ||
791 | } | ||
792 | #endif /* PROC_FS */ | ||
793 | |||
794 | #ifdef FAST_TIMER_TEST | ||
795 | static volatile unsigned long i = 0; | ||
796 | static volatile int num_test_timeout = 0; | ||
797 | static struct fast_timer tr[10]; | ||
798 | static int exp_num[10]; | ||
799 | |||
800 | static struct timeval tv_exp[100]; | ||
801 | |||
802 | static void test_timeout(unsigned long data) | ||
803 | { | ||
804 | do_gettimeofday_fast(&tv_exp[data]); | ||
805 | exp_num[data] = num_test_timeout; | ||
806 | |||
807 | num_test_timeout++; | ||
808 | } | ||
809 | |||
810 | static void test_timeout1(unsigned long data) | ||
811 | { | ||
812 | do_gettimeofday_fast(&tv_exp[data]); | ||
813 | exp_num[data] = num_test_timeout; | ||
814 | if (data < 7) | ||
815 | { | ||
816 | start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1"); | ||
817 | i++; | ||
818 | } | ||
819 | num_test_timeout++; | ||
820 | } | ||
821 | |||
822 | DP( | ||
823 | static char buf0[2000]; | ||
824 | static char buf1[2000]; | ||
825 | static char buf2[2000]; | ||
826 | static char buf3[2000]; | ||
827 | static char buf4[2000]; | ||
828 | ); | ||
829 | |||
830 | static char buf5[6000]; | ||
831 | static int j_u[1000]; | ||
832 | |||
833 | static void fast_timer_test(void) | ||
834 | { | ||
835 | int prev_num; | ||
836 | int j; | ||
837 | |||
838 | struct timeval tv, tv0, tv1, tv2; | ||
839 | |||
840 | printk("fast_timer_test() start\n"); | ||
841 | do_gettimeofday_fast(&tv); | ||
842 | |||
843 | for (j = 0; j < 1000; j++) | ||
844 | { | ||
845 | j_u[j] = GET_JIFFIES_USEC(); | ||
846 | } | ||
847 | for (j = 0; j < 100; j++) | ||
848 | { | ||
849 | do_gettimeofday_fast(&tv_exp[j]); | ||
850 | } | ||
851 | printk("fast_timer_test() %is %06i\n", tv.tv_sec, tv.tv_usec); | ||
852 | |||
853 | for (j = 0; j < 1000; j++) | ||
854 | { | ||
855 | 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]); | ||
856 | j += 4; | ||
857 | } | ||
858 | for (j = 0; j < 100; j++) | ||
859 | { | ||
860 | printk("%i.%i %i.%i %i.%i %i.%i %i.%i\n", | ||
861 | tv_exp[j].tv_sec,tv_exp[j].tv_usec, | ||
862 | tv_exp[j+1].tv_sec,tv_exp[j+1].tv_usec, | ||
863 | tv_exp[j+2].tv_sec,tv_exp[j+2].tv_usec, | ||
864 | tv_exp[j+3].tv_sec,tv_exp[j+3].tv_usec, | ||
865 | tv_exp[j+4].tv_sec,tv_exp[j+4].tv_usec); | ||
866 | j += 4; | ||
867 | } | ||
868 | do_gettimeofday_fast(&tv0); | ||
869 | start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0"); | ||
870 | DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0)); | ||
871 | i++; | ||
872 | start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1"); | ||
873 | DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0)); | ||
874 | i++; | ||
875 | start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2"); | ||
876 | DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0)); | ||
877 | i++; | ||
878 | start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3"); | ||
879 | DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0)); | ||
880 | i++; | ||
881 | start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx"); | ||
882 | DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0)); | ||
883 | i++; | ||
884 | do_gettimeofday_fast(&tv1); | ||
885 | |||
886 | proc_fasttimer_read(buf5, NULL, 0, 0, 0); | ||
887 | |||
888 | prev_num = num_test_timeout; | ||
889 | while (num_test_timeout < i) | ||
890 | { | ||
891 | if (num_test_timeout != prev_num) | ||
892 | { | ||
893 | prev_num = num_test_timeout; | ||
894 | } | ||
895 | } | ||
896 | do_gettimeofday_fast(&tv2); | ||
897 | printk("Timers started %is %06i\n", tv0.tv_sec, tv0.tv_usec); | ||
898 | printk("Timers started at %is %06i\n", tv1.tv_sec, tv1.tv_usec); | ||
899 | printk("Timers done %is %06i\n", tv2.tv_sec, tv2.tv_usec); | ||
900 | DP(printk("buf0:\n"); | ||
901 | printk(buf0); | ||
902 | printk("buf1:\n"); | ||
903 | printk(buf1); | ||
904 | printk("buf2:\n"); | ||
905 | printk(buf2); | ||
906 | printk("buf3:\n"); | ||
907 | printk(buf3); | ||
908 | printk("buf4:\n"); | ||
909 | printk(buf4); | ||
910 | ); | ||
911 | printk("buf5:\n"); | ||
912 | printk(buf5); | ||
913 | |||
914 | printk("timers set:\n"); | ||
915 | for(j = 0; j<i; j++) | ||
916 | { | ||
917 | struct fast_timer *t = &tr[j]; | ||
918 | printk("%-10s set: %6is %06ius exp: %6is %06ius " | ||
919 | "data: 0x%08X func: 0x%08X\n", | ||
920 | t->name, | ||
921 | t->tv_set.tv_sec, | ||
922 | t->tv_set.tv_usec, | ||
923 | t->tv_expires.tv_sec, | ||
924 | t->tv_expires.tv_usec, | ||
925 | t->data, | ||
926 | t->function | ||
927 | ); | ||
928 | |||
929 | printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n", | ||
930 | t->delay_us, | ||
931 | tv_exp[j].tv_sec, | ||
932 | tv_exp[j].tv_usec, | ||
933 | exp_num[j], | ||
934 | (tv_exp[j].tv_sec - t->tv_expires.tv_sec)*1000000 + tv_exp[j].tv_usec - t->tv_expires.tv_usec); | ||
935 | } | ||
936 | proc_fasttimer_read(buf5, NULL, 0, 0, 0); | ||
937 | printk("buf5 after all done:\n"); | ||
938 | printk(buf5); | ||
939 | printk("fast_timer_test() done\n"); | ||
940 | } | ||
941 | #endif | ||
942 | |||
943 | |||
944 | void fast_timer_init(void) | ||
945 | { | ||
946 | /* For some reason, request_irq() hangs when called froom time_init() */ | ||
947 | if (!fast_timer_is_init) | ||
948 | { | ||
949 | #if 0 && defined(FAST_TIMER_TEST) | ||
950 | int i; | ||
951 | #endif | ||
952 | |||
953 | printk(KERN_INFO "fast_timer_init()\n"); | ||
954 | |||
955 | #if 0 && defined(FAST_TIMER_TEST) | ||
956 | for (i = 0; i <= TIMER0_DIV; i++) | ||
957 | { | ||
958 | /* We must be careful not to get overflow... */ | ||
959 | printk("%3i %6u\n", i, timer0_value_us[i]); | ||
960 | } | ||
961 | #endif | ||
962 | #ifdef CONFIG_PROC_FS | ||
963 | if ((fasttimer_proc_entry = create_proc_entry( "fasttimer", 0, 0 ))) | ||
964 | fasttimer_proc_entry->read_proc = proc_fasttimer_read; | ||
965 | #endif /* PROC_FS */ | ||
966 | if(request_irq(TIMER1_IRQ_NBR, timer1_handler, SA_SHIRQ, | ||
967 | "fast timer int", NULL)) | ||
968 | { | ||
969 | printk("err: timer1 irq\n"); | ||
970 | } | ||
971 | fast_timer_is_init = 1; | ||
972 | #ifdef FAST_TIMER_TEST | ||
973 | printk("do test\n"); | ||
974 | fast_timer_test(); | ||
975 | #endif | ||
976 | } | ||
977 | } | ||