fasttimer.c 23 KB

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