fasttimer.c 26 KB

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