fasttimer.c 22 KB

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