fasttimer.c 22 KB

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