]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/cris/arch-v10/kernel/fasttimer.c
make vfree() safe to call from interrupt contexts
[karo-tx-linux.git] / arch / cris / arch-v10 / kernel / fasttimer.c
1 /*
2  * linux/arch/cris/kernel/fasttimer.c
3  *
4  * Fast timers for ETRAX100/ETRAX100LX
5  *
6  * Copyright (C) 2000-2007 Axis Communications AB, Lund, Sweden
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>
24
25 #include <arch/svinto.h>
26 #include <asm/fasttimer.h>
27 #include <linux/proc_fs.h>
28
29
30 #define DEBUG_LOG_INCLUDED
31 #define FAST_TIMER_LOG
32 /* #define FAST_TIMER_TEST */
33
34 #define FAST_TIMER_SANITY_CHECKS
35
36 #ifdef FAST_TIMER_SANITY_CHECKS
37 static int sanity_failed;
38 #endif
39
40 #define D1(x)
41 #define D2(x)
42 #define DP(x)
43
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;
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];
58 static unsigned int debug_log_cnt;
59 static unsigned int debug_log_cnt_wrapped;
60
61 #define DEBUG_LOG(string, value) \
62 { \
63   unsigned long log_flags; \
64   local_irq_save(log_flags); \
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   } \
72   local_irq_restore(log_flags); \
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 */
114 inline void do_gettimeofday_fast(struct fasttime_t *tv)
115 {
116         tv->tv_jiff = jiffies;
117         tv->tv_usec = GET_JIFFIES_USEC();
118 }
119
120 inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1)
121 {
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;
134 }
135
136 inline void start_timer1(unsigned long delay_us)
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
177         D1(printk(KERN_DEBUG "start_timer1 : %d us freq: %i div: %i\n",
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
219   local_irq_save(flags);
220
221   do_gettimeofday_fast(&t->tv_set);
222   tmp = fast_timer_list;
223
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
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;
244         t->tv_expires.tv_jiff = t->tv_set.tv_jiff + delay_us / 1000000 / HZ;
245   if (t->tv_expires.tv_usec > 1000000)
246   {
247     t->tv_expires.tv_usec -= 1000000;
248                 t->tv_expires.tv_jiff += HZ;
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 */
256         if (tmp == NULL || fasttime_cmp(&t->tv_expires, &tmp->tv_expires) < 0)
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 */
272                 while (tmp->next && fasttime_cmp(&t->tv_expires,
273                                 &tmp->next->tv_expires) > 0)
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
289 done:
290   local_irq_restore(flags);
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   
320   local_irq_save(flags);
321   ret = detach_fast_timer(t);
322   t->next = t->prev = NULL;
323   local_irq_restore(flags);
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
333 timer1_handler(int irq, void *dev_id)
334 {
335   struct fast_timer *t;
336   unsigned long flags;
337
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          */
345   local_irq_save(flags);
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
362   t = fast_timer_list;
363   while (t)
364   {
365                 struct fasttime_t tv;
366                 fast_timer_function_type *f;
367                 unsigned long d;
368
369     /* Has it really expired? */
370     do_gettimeofday_fast(&tv);
371                 D1(printk(KERN_DEBUG "t: %is %06ius\n",
372                         tv.tv_jiff, tv.tv_usec));
373
374                 if (fasttime_cmp(&t->tv_expires, &tv) <= 0)
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 */
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;
397
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
413         DEBUG_LOG("!timer1 %i function==NULL!\n", fast_timer_ints);
414     }
415     else
416     {
417       /* Timer is to early, let's set it again using the normal routines */
418       D1(printk(".\n"));
419     }
420
421     if ((t = fast_timer_list) != NULL)
422     {
423       /* Start next timer.. */
424                         long us = 0;
425                         struct fasttime_t tv;
426
427       do_gettimeofday_fast(&tv);
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
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         }
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     }
454   }
455
456         local_irq_restore(flags);
457
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 {
468   wait_queue_head_t *sleep_wait_p = (wait_queue_head_t *)data;
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;
478   wait_queue_head_t sleep_wait;
479   init_waitqueue_head(&sleep_wait);
480
481   D1(printk("schedule_usleep(%d)\n", us));
482   start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
483                        "usleep");
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
488   D1(printk("done schedule_usleep(%d)\n", us));
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);
494 static struct proc_dir_entry *fasttimer_proc_entry;
495 #endif /* CONFIG_PROC_FS */
496
497 #ifdef CONFIG_PROC_FS
498
499 /* This value is very much based on testing */
500 #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
501
502 static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
503                        ,int *eof, void *data_unused)
504 {
505   unsigned long flags;
506   int i = 0;
507   int num_to_show;
508         struct fasttime_t tv;
509   struct fast_timer *t, *nextt;
510   static char *bigbuf = NULL;
511   static unsigned long used;
512
513   if (!bigbuf && !(bigbuf = vmalloc(BIG_BUF_SIZE)))
514   {
515     used = 0;
516         if (buf)
517                 buf[0] = '\0';
518     return 0;
519   }
520
521   if (!offset || !used)
522   {
523     do_gettimeofday_fast(&tv);
524
525     used = 0;
526     used += sprintf(bigbuf + used, "Fast timers added:     %i\n",
527                     fast_timers_added);
528     used += sprintf(bigbuf + used, "Fast timers started:   %i\n",
529                     fast_timers_started);
530     used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
531                     fast_timer_ints);
532     used += sprintf(bigbuf + used, "Fast timers expired:   %i\n",
533                     fast_timers_expired);
534     used += sprintf(bigbuf + used, "Fast timers deleted:   %i\n",
535                     fast_timers_deleted);
536     used += sprintf(bigbuf + used, "Fast timer running:    %s\n",
537                     fast_timer_running ? "yes" : "no");
538     used += sprintf(bigbuf + used, "Current time:          %lu.%06lu\n",
539                         (unsigned long)tv.tv_jiff,
540                     (unsigned long)tv.tv_usec);
541 #ifdef FAST_TIMER_SANITY_CHECKS
542     used += sprintf(bigbuf + used, "Sanity failed:         %i\n",
543                     sanity_failed);
544 #endif
545     used += sprintf(bigbuf + used, "\n");
546
547 #ifdef DEBUG_LOG_INCLUDED
548     {
549       int end_i = debug_log_cnt;
550       i = 0;
551
552       if (debug_log_cnt_wrapped)
553       {
554         i = debug_log_cnt;
555       }
556
557       while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
558              used+100 < BIG_BUF_SIZE)
559       {
560         used += sprintf(bigbuf + used, debug_log_string[i],
561                         debug_log_value[i]);
562         i = (i+1) % DEBUG_LOG_MAX;
563       }
564     }
565     used += sprintf(bigbuf + used, "\n");
566 #endif
567
568     num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
569                    NUM_TIMER_STATS);
570     used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started);
571     for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++)
572     {
573       int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
574
575 #if 1 //ndef FAST_TIMER_LOG
576       used += sprintf(bigbuf + used, "div: %i freq: %i delay: %i"
577                       "\n",
578                       timer_div_settings[cur],
579                       timer_freq_settings[cur],
580                       timer_delay_settings[cur]
581                       );
582 #endif
583 #ifdef FAST_TIMER_LOG
584       t = &timer_started_log[cur];
585       used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
586                       "d: %6li us data: 0x%08lX"
587                       "\n",
588                       t->name,
589                         (unsigned long)t->tv_set.tv_jiff,
590                       (unsigned long)t->tv_set.tv_usec,
591                         (unsigned long)t->tv_expires.tv_jiff,
592                       (unsigned long)t->tv_expires.tv_usec,
593                       t->delay_us,
594                       t->data
595                       );
596 #endif
597     }
598     used += sprintf(bigbuf + used, "\n");
599
600 #ifdef FAST_TIMER_LOG
601     num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
602                    NUM_TIMER_STATS);
603     used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added);
604     for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
605     {
606       t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
607       used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
608                       "d: %6li us data: 0x%08lX"
609                       "\n",
610                       t->name,
611                         (unsigned long)t->tv_set.tv_jiff,
612                       (unsigned long)t->tv_set.tv_usec,
613                         (unsigned long)t->tv_expires.tv_jiff,
614                       (unsigned long)t->tv_expires.tv_usec,
615                       t->delay_us,
616                       t->data
617                       );
618     }
619     used += sprintf(bigbuf + used, "\n");
620
621     num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
622                    NUM_TIMER_STATS);
623     used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired);
624     for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
625     {
626       t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
627       used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
628                       "d: %6li us data: 0x%08lX"
629                       "\n",
630                       t->name,
631                         (unsigned long)t->tv_set.tv_jiff,
632                       (unsigned long)t->tv_set.tv_usec,
633                         (unsigned long)t->tv_expires.tv_jiff,
634                       (unsigned long)t->tv_expires.tv_usec,
635                       t->delay_us,
636                       t->data
637                       );
638     }
639     used += sprintf(bigbuf + used, "\n");
640 #endif
641
642     used += sprintf(bigbuf + used, "Active timers:\n");
643     local_irq_save(flags);
644     t = fast_timer_list;
645     while (t != NULL && (used+100 < BIG_BUF_SIZE))
646     {
647       nextt = t->next;
648       local_irq_restore(flags);
649       used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
650                       "d: %6li us data: 0x%08lX"
651 /*                      " func: 0x%08lX" */
652                       "\n",
653                       t->name,
654                         (unsigned long)t->tv_set.tv_jiff,
655                       (unsigned long)t->tv_set.tv_usec,
656                         (unsigned long)t->tv_expires.tv_jiff,
657                       (unsigned long)t->tv_expires.tv_usec,
658                       t->delay_us,
659                       t->data
660 /*                      , t->function */
661                       );
662         local_irq_save(flags);
663       if (t->next != nextt)
664       {
665         printk(KERN_WARNING "timer removed!\n");
666       }
667       t = nextt;
668     }
669     local_irq_restore(flags);
670   }
671
672   if (used - offset < len)
673   {
674     len = used - offset;
675   }
676
677   memcpy(buf, bigbuf + offset, len);
678   *start = buf;
679   *eof = 1;
680
681   return len;
682 }
683 #endif /* PROC_FS */
684
685 #ifdef FAST_TIMER_TEST
686 static volatile unsigned long i = 0;
687 static volatile int num_test_timeout = 0;
688 static struct fast_timer tr[10];
689 static int exp_num[10];
690
691 static struct fasttime_t tv_exp[100];
692
693 static void test_timeout(unsigned long data)
694 {
695   do_gettimeofday_fast(&tv_exp[data]);
696   exp_num[data] = num_test_timeout;
697
698   num_test_timeout++;
699 }
700
701 static void test_timeout1(unsigned long data)
702 {
703   do_gettimeofday_fast(&tv_exp[data]);
704   exp_num[data] = num_test_timeout;
705   if (data < 7)
706   {
707     start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
708     i++;
709   }
710   num_test_timeout++;
711 }
712
713 DP(
714 static char buf0[2000];
715 static char buf1[2000];
716 static char buf2[2000];
717 static char buf3[2000];
718 static char buf4[2000];
719 );
720
721 static char buf5[6000];
722 static int j_u[1000];
723
724 static void fast_timer_test(void)
725 {
726   int prev_num;
727   int j;
728
729         struct fasttime_t tv, tv0, tv1, tv2;
730
731   printk("fast_timer_test() start\n");
732   do_gettimeofday_fast(&tv);
733
734   for (j = 0; j < 1000; j++)
735   {
736     j_u[j] = GET_JIFFIES_USEC();
737   }
738   for (j = 0; j < 100; j++)
739   {
740     do_gettimeofday_fast(&tv_exp[j]);
741   }
742         printk(KERN_DEBUG "fast_timer_test() %is %06i\n",
743                 tv.tv_jiff, tv.tv_usec);
744
745   for (j = 0; j < 1000; j++)
746   {
747     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]);
748     j += 4;
749   }
750   for (j = 0; j < 100; j++)
751   {
752                 printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n",
753                         tv_exp[j].tv_jiff, tv_exp[j].tv_usec,
754                         tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
755                         tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
756                         tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
757                         tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
758     j += 4;
759   }
760   do_gettimeofday_fast(&tv0);
761   start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
762   DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
763   i++;
764   start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
765   DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
766   i++;
767   start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
768   DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
769   i++;
770   start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
771   DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
772   i++;
773   start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
774   DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
775   i++;
776   do_gettimeofday_fast(&tv1);
777
778   proc_fasttimer_read(buf5, NULL, 0, 0, 0);
779
780   prev_num = num_test_timeout;
781   while (num_test_timeout < i)
782   {
783     if (num_test_timeout != prev_num)
784     {
785       prev_num = num_test_timeout;
786     }
787   }
788   do_gettimeofday_fast(&tv2);
789         printk(KERN_DEBUG "Timers started    %is %06i\n",
790                 tv0.tv_jiff, tv0.tv_usec);
791         printk(KERN_DEBUG "Timers started at %is %06i\n",
792                 tv1.tv_jiff, tv1.tv_usec);
793         printk(KERN_DEBUG "Timers done       %is %06i\n",
794                 tv2.tv_jiff, tv2.tv_usec);
795   DP(printk("buf0:\n");
796      printk(buf0);
797      printk("buf1:\n");
798      printk(buf1);
799      printk("buf2:\n");
800      printk(buf2);
801      printk("buf3:\n");
802      printk(buf3);
803      printk("buf4:\n");
804      printk(buf4);
805   );
806   printk("buf5:\n");
807   printk(buf5);
808
809   printk("timers set:\n");
810   for(j = 0; j<i; j++)
811   {
812     struct fast_timer *t = &tr[j];
813     printk("%-10s set: %6is %06ius exp: %6is %06ius "
814            "data: 0x%08X func: 0x%08X\n",
815            t->name,
816                         t->tv_set.tv_jiff,
817            t->tv_set.tv_usec,
818                         t->tv_expires.tv_jiff,
819            t->tv_expires.tv_usec,
820            t->data,
821            t->function
822            );
823
824     printk("           del: %6ius     did exp: %6is %06ius as #%i error: %6li\n",
825            t->delay_us,
826                         tv_exp[j].tv_jiff,
827            tv_exp[j].tv_usec,
828            exp_num[j],
829                         (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
830                                 1000000 + tv_exp[j].tv_usec -
831                                 t->tv_expires.tv_usec);
832   }
833   proc_fasttimer_read(buf5, NULL, 0, 0, 0);
834   printk("buf5 after all done:\n");
835   printk(buf5);
836   printk("fast_timer_test() done\n");
837 }
838 #endif
839
840
841 int fast_timer_init(void)
842 {
843   /* For some reason, request_irq() hangs when called froom time_init() */
844   if (!fast_timer_is_init)
845   {
846 #if 0 && defined(FAST_TIMER_TEST)
847     int i;
848 #endif
849
850     printk(KERN_INFO "fast_timer_init()\n");
851
852 #if 0 && defined(FAST_TIMER_TEST)
853     for (i = 0; i <= TIMER0_DIV; i++)
854     {
855       /* We must be careful not to get overflow... */
856       printk("%3i %6u\n", i, timer0_value_us[i]);
857     }
858 #endif
859 #ifdef CONFIG_PROC_FS
860    if ((fasttimer_proc_entry = create_proc_entry( "fasttimer", 0, 0 )))
861      fasttimer_proc_entry->read_proc = proc_fasttimer_read;
862 #endif /* PROC_FS */
863     if(request_irq(TIMER1_IRQ_NBR, timer1_handler, 0,
864                    "fast timer int", NULL))
865     {
866       printk("err: timer1 irq\n");
867     }
868     fast_timer_is_init = 1;
869 #ifdef FAST_TIMER_TEST
870     printk("do test\n");
871     fast_timer_test();
872 #endif
873   }
874         return 0;
875 }
876 __initcall(fast_timer_init);