fasttimer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. static struct proc_dir_entry *fasttimer_proc_entry;
  392. #endif /* CONFIG_PROC_FS */
  393. #ifdef CONFIG_PROC_FS
  394. /* This value is very much based on testing */
  395. #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
  396. static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
  397. ,int *eof, void *data_unused)
  398. {
  399. unsigned long flags;
  400. int i = 0;
  401. int num_to_show;
  402. struct fasttime_t tv;
  403. struct fast_timer *t, *nextt;
  404. static char *bigbuf = NULL;
  405. static unsigned long used;
  406. if (!bigbuf) {
  407. bigbuf = vmalloc(BIG_BUF_SIZE);
  408. if (!bigbuf) {
  409. used = 0;
  410. if (buf)
  411. buf[0] = '\0';
  412. return 0;
  413. }
  414. }
  415. if (!offset || !used) {
  416. do_gettimeofday_fast(&tv);
  417. used = 0;
  418. used += sprintf(bigbuf + used, "Fast timers added: %i\n",
  419. fast_timers_added);
  420. used += sprintf(bigbuf + used, "Fast timers started: %i\n",
  421. fast_timers_started);
  422. used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
  423. fast_timer_ints);
  424. used += sprintf(bigbuf + used, "Fast timers expired: %i\n",
  425. fast_timers_expired);
  426. used += sprintf(bigbuf + used, "Fast timers deleted: %i\n",
  427. fast_timers_deleted);
  428. used += sprintf(bigbuf + used, "Fast timer running: %s\n",
  429. fast_timer_running ? "yes" : "no");
  430. used += sprintf(bigbuf + used, "Current time: %lu.%06lu\n",
  431. (unsigned long)tv.tv_jiff,
  432. (unsigned long)tv.tv_usec);
  433. #ifdef FAST_TIMER_SANITY_CHECKS
  434. used += sprintf(bigbuf + used, "Sanity failed: %i\n",
  435. sanity_failed);
  436. #endif
  437. used += sprintf(bigbuf + used, "\n");
  438. #ifdef DEBUG_LOG_INCLUDED
  439. {
  440. int end_i = debug_log_cnt;
  441. i = 0;
  442. if (debug_log_cnt_wrapped)
  443. i = debug_log_cnt;
  444. while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
  445. used+100 < BIG_BUF_SIZE)
  446. {
  447. used += sprintf(bigbuf + used, debug_log_string[i],
  448. debug_log_value[i]);
  449. i = (i+1) % DEBUG_LOG_MAX;
  450. }
  451. }
  452. used += sprintf(bigbuf + used, "\n");
  453. #endif
  454. num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
  455. NUM_TIMER_STATS);
  456. used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started);
  457. for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++)
  458. {
  459. int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
  460. #if 1 //ndef FAST_TIMER_LOG
  461. used += sprintf(bigbuf + used, "div: %i delay: %i"
  462. "\n",
  463. timer_div_settings[cur],
  464. timer_delay_settings[cur]
  465. );
  466. #endif
  467. #ifdef FAST_TIMER_LOG
  468. t = &timer_started_log[cur];
  469. used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  470. "d: %6li us data: 0x%08lX"
  471. "\n",
  472. t->name,
  473. (unsigned long)t->tv_set.tv_jiff,
  474. (unsigned long)t->tv_set.tv_usec,
  475. (unsigned long)t->tv_expires.tv_jiff,
  476. (unsigned long)t->tv_expires.tv_usec,
  477. t->delay_us,
  478. t->data
  479. );
  480. #endif
  481. }
  482. used += sprintf(bigbuf + used, "\n");
  483. #ifdef FAST_TIMER_LOG
  484. num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
  485. NUM_TIMER_STATS);
  486. used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added);
  487. for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
  488. {
  489. t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
  490. used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  491. "d: %6li us data: 0x%08lX"
  492. "\n",
  493. t->name,
  494. (unsigned long)t->tv_set.tv_jiff,
  495. (unsigned long)t->tv_set.tv_usec,
  496. (unsigned long)t->tv_expires.tv_jiff,
  497. (unsigned long)t->tv_expires.tv_usec,
  498. t->delay_us,
  499. t->data
  500. );
  501. }
  502. used += sprintf(bigbuf + used, "\n");
  503. num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
  504. NUM_TIMER_STATS);
  505. used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired);
  506. for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
  507. {
  508. t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
  509. used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  510. "d: %6li us data: 0x%08lX"
  511. "\n",
  512. t->name,
  513. (unsigned long)t->tv_set.tv_jiff,
  514. (unsigned long)t->tv_set.tv_usec,
  515. (unsigned long)t->tv_expires.tv_jiff,
  516. (unsigned long)t->tv_expires.tv_usec,
  517. t->delay_us,
  518. t->data
  519. );
  520. }
  521. used += sprintf(bigbuf + used, "\n");
  522. #endif
  523. used += sprintf(bigbuf + used, "Active timers:\n");
  524. local_irq_save(flags);
  525. t = fast_timer_list;
  526. while (t != NULL && (used+100 < BIG_BUF_SIZE))
  527. {
  528. nextt = t->next;
  529. local_irq_restore(flags);
  530. used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  531. "d: %6li us data: 0x%08lX"
  532. /* " func: 0x%08lX" */
  533. "\n",
  534. t->name,
  535. (unsigned long)t->tv_set.tv_jiff,
  536. (unsigned long)t->tv_set.tv_usec,
  537. (unsigned long)t->tv_expires.tv_jiff,
  538. (unsigned long)t->tv_expires.tv_usec,
  539. t->delay_us,
  540. t->data
  541. /* , t->function */
  542. );
  543. local_irq_save(flags);
  544. if (t->next != nextt)
  545. {
  546. printk("timer removed!\n");
  547. }
  548. t = nextt;
  549. }
  550. local_irq_restore(flags);
  551. }
  552. if (used - offset < len)
  553. {
  554. len = used - offset;
  555. }
  556. memcpy(buf, bigbuf + offset, len);
  557. *start = buf;
  558. *eof = 1;
  559. return len;
  560. }
  561. #endif /* PROC_FS */
  562. #ifdef FAST_TIMER_TEST
  563. static volatile unsigned long i = 0;
  564. static volatile int num_test_timeout = 0;
  565. static struct fast_timer tr[10];
  566. static int exp_num[10];
  567. static struct fasttime_t tv_exp[100];
  568. static void test_timeout(unsigned long data)
  569. {
  570. do_gettimeofday_fast(&tv_exp[data]);
  571. exp_num[data] = num_test_timeout;
  572. num_test_timeout++;
  573. }
  574. static void test_timeout1(unsigned long data)
  575. {
  576. do_gettimeofday_fast(&tv_exp[data]);
  577. exp_num[data] = num_test_timeout;
  578. if (data < 7)
  579. {
  580. start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
  581. i++;
  582. }
  583. num_test_timeout++;
  584. }
  585. DP(
  586. static char buf0[2000];
  587. static char buf1[2000];
  588. static char buf2[2000];
  589. static char buf3[2000];
  590. static char buf4[2000];
  591. );
  592. static char buf5[6000];
  593. static int j_u[1000];
  594. static void fast_timer_test(void)
  595. {
  596. int prev_num;
  597. int j;
  598. struct fasttime_t tv, tv0, tv1, tv2;
  599. printk("fast_timer_test() start\n");
  600. do_gettimeofday_fast(&tv);
  601. for (j = 0; j < 1000; j++)
  602. {
  603. j_u[j] = GET_JIFFIES_USEC();
  604. }
  605. for (j = 0; j < 100; j++)
  606. {
  607. do_gettimeofday_fast(&tv_exp[j]);
  608. }
  609. printk(KERN_DEBUG "fast_timer_test() %is %06i\n", tv.tv_jiff, tv.tv_usec);
  610. for (j = 0; j < 1000; j++)
  611. {
  612. printk(KERN_DEBUG "%i %i %i %i %i\n",
  613. j_u[j], j_u[j+1], j_u[j+2], j_u[j+3], j_u[j+4]);
  614. j += 4;
  615. }
  616. for (j = 0; j < 100; j++)
  617. {
  618. printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n",
  619. tv_exp[j].tv_jiff, tv_exp[j].tv_usec,
  620. tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
  621. tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
  622. tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
  623. tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
  624. j += 4;
  625. }
  626. do_gettimeofday_fast(&tv0);
  627. start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
  628. DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
  629. i++;
  630. start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
  631. DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
  632. i++;
  633. start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
  634. DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
  635. i++;
  636. start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
  637. DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
  638. i++;
  639. start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
  640. DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
  641. i++;
  642. do_gettimeofday_fast(&tv1);
  643. proc_fasttimer_read(buf5, NULL, 0, 0, 0);
  644. prev_num = num_test_timeout;
  645. while (num_test_timeout < i)
  646. {
  647. if (num_test_timeout != prev_num)
  648. prev_num = num_test_timeout;
  649. }
  650. do_gettimeofday_fast(&tv2);
  651. printk(KERN_INFO "Timers started %is %06i\n",
  652. tv0.tv_jiff, tv0.tv_usec);
  653. printk(KERN_INFO "Timers started at %is %06i\n",
  654. tv1.tv_jiff, tv1.tv_usec);
  655. printk(KERN_INFO "Timers done %is %06i\n",
  656. tv2.tv_jiff, tv2.tv_usec);
  657. DP(printk("buf0:\n");
  658. printk(buf0);
  659. printk("buf1:\n");
  660. printk(buf1);
  661. printk("buf2:\n");
  662. printk(buf2);
  663. printk("buf3:\n");
  664. printk(buf3);
  665. printk("buf4:\n");
  666. printk(buf4);
  667. );
  668. printk("buf5:\n");
  669. printk(buf5);
  670. printk("timers set:\n");
  671. for(j = 0; j<i; j++)
  672. {
  673. struct fast_timer *t = &tr[j];
  674. printk("%-10s set: %6is %06ius exp: %6is %06ius "
  675. "data: 0x%08X func: 0x%08X\n",
  676. t->name,
  677. t->tv_set.tv_jiff,
  678. t->tv_set.tv_usec,
  679. t->tv_expires.tv_jiff,
  680. t->tv_expires.tv_usec,
  681. t->data,
  682. t->function
  683. );
  684. printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
  685. t->delay_us,
  686. tv_exp[j].tv_jiff,
  687. tv_exp[j].tv_usec,
  688. exp_num[j],
  689. (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
  690. 1000000 + tv_exp[j].tv_usec -
  691. t->tv_expires.tv_usec);
  692. }
  693. proc_fasttimer_read(buf5, NULL, 0, 0, 0);
  694. printk("buf5 after all done:\n");
  695. printk(buf5);
  696. printk("fast_timer_test() done\n");
  697. }
  698. #endif
  699. int fast_timer_init(void)
  700. {
  701. /* For some reason, request_irq() hangs when called froom time_init() */
  702. if (!fast_timer_is_init)
  703. {
  704. printk("fast_timer_init()\n");
  705. #ifdef CONFIG_PROC_FS
  706. fasttimer_proc_entry = create_proc_entry("fasttimer", 0, 0);
  707. if (fasttimer_proc_entry)
  708. fasttimer_proc_entry->read_proc = proc_fasttimer_read;
  709. #endif /* PROC_FS */
  710. if (request_irq(TIMER0_INTR_VECT, timer_trig_interrupt,
  711. IRQF_SHARED | IRQF_DISABLED,
  712. "fast timer int", &fast_timer_list))
  713. printk(KERN_ERR "err: fasttimer irq\n");
  714. fast_timer_is_init = 1;
  715. #ifdef FAST_TIMER_TEST
  716. printk("do test\n");
  717. fast_timer_test();
  718. #endif
  719. }
  720. return 0;
  721. }
  722. __initcall(fast_timer_init);