fasttimer.c 26 KB

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