fasttimer.c 23 KB

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