fasttimer.c 22 KB

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