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