fasttimer.c 23 KB

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