fasttimer.c 26 KB

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