fasttimer.c 26 KB

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