fasttimer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. #include <linux/seq_file.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. fast_timer_function_type *f;
  274. unsigned long d;
  275. unsigned long flags;
  276. /* We keep interrupts disabled not only when we modify the
  277. * fast timer list, but any time we hold a reference to a
  278. * timer in the list, since del_fast_timer may be called
  279. * from (another) interrupt context. Thus, the only time
  280. * when interrupts are enabled is when calling the timer
  281. * callback function.
  282. */
  283. local_irq_save(flags);
  284. /* Clear timer trig interrupt */
  285. intr_mask = REG_RD(timer, regi_timer0, rw_intr_mask);
  286. intr_mask.trig = 0;
  287. REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
  288. /* First stop timer, then ack interrupt */
  289. /* Stop timer */
  290. trig_cfg.tmr = regk_timer_off;
  291. REG_WR(timer, regi_timer0, rw_trig_cfg, trig_cfg);
  292. /* Ack interrupt */
  293. ack_intr.trig = 1;
  294. REG_WR(timer, regi_timer0, rw_ack_intr, ack_intr);
  295. fast_timer_running = 0;
  296. fast_timer_ints++;
  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. /* This value is very much based on testing */
  391. #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
  392. static int proc_fasttimer_show(struct seq_file *m, void *v)
  393. {
  394. unsigned long flags;
  395. int i = 0;
  396. int num_to_show;
  397. struct fasttime_t tv;
  398. struct fast_timer *t, *nextt;
  399. do_gettimeofday_fast(&tv);
  400. seq_printf(m, "Fast timers added: %i\n", fast_timers_added);
  401. seq_printf(m, "Fast timers started: %i\n", fast_timers_started);
  402. seq_printf(m, "Fast timer interrupts: %i\n", fast_timer_ints);
  403. seq_printf(m, "Fast timers expired: %i\n", fast_timers_expired);
  404. seq_printf(m, "Fast timers deleted: %i\n", fast_timers_deleted);
  405. seq_printf(m, "Fast timer running: %s\n",
  406. fast_timer_running ? "yes" : "no");
  407. seq_printf(m, "Current time: %lu.%06lu\n",
  408. (unsigned long)tv.tv_jiff,
  409. (unsigned long)tv.tv_usec);
  410. #ifdef FAST_TIMER_SANITY_CHECKS
  411. seq_printf(m, "Sanity failed: %i\n", sanity_failed);
  412. #endif
  413. seq_putc(m, '\n');
  414. #ifdef DEBUG_LOG_INCLUDED
  415. {
  416. int end_i = debug_log_cnt;
  417. i = 0;
  418. if (debug_log_cnt_wrapped)
  419. i = debug_log_cnt;
  420. while ((i != end_i || debug_log_cnt_wrapped)) {
  421. seq_printf(m, debug_log_string[i], debug_log_value[i]);
  422. if (seq_has_overflowed(m))
  423. return 0;
  424. i = (i+1) % DEBUG_LOG_MAX;
  425. }
  426. }
  427. seq_putc(m, '\n');
  428. #endif
  429. num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
  430. NUM_TIMER_STATS);
  431. seq_printf(m, "Timers started: %i\n", fast_timers_started);
  432. for (i = 0; i < num_to_show; i++) {
  433. int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
  434. #if 1 //ndef FAST_TIMER_LOG
  435. seq_printf(m, "div: %i delay: %i\n",
  436. timer_div_settings[cur],
  437. timer_delay_settings[cur]);
  438. #endif
  439. #ifdef FAST_TIMER_LOG
  440. t = &timer_started_log[cur];
  441. seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu d: %6li us data: 0x%08lX\n",
  442. t->name,
  443. (unsigned long)t->tv_set.tv_jiff,
  444. (unsigned long)t->tv_set.tv_usec,
  445. (unsigned long)t->tv_expires.tv_jiff,
  446. (unsigned long)t->tv_expires.tv_usec,
  447. t->delay_us,
  448. t->data);
  449. if (seq_has_overflowed(m))
  450. return 0;
  451. #endif
  452. }
  453. seq_putc(m, '\n');
  454. #ifdef FAST_TIMER_LOG
  455. num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
  456. NUM_TIMER_STATS);
  457. seq_printf(m, "Timers added: %i\n", fast_timers_added);
  458. for (i = 0; i < num_to_show; i++) {
  459. t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
  460. seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu d: %6li us data: 0x%08lX\n",
  461. t->name,
  462. (unsigned long)t->tv_set.tv_jiff,
  463. (unsigned long)t->tv_set.tv_usec,
  464. (unsigned long)t->tv_expires.tv_jiff,
  465. (unsigned long)t->tv_expires.tv_usec,
  466. t->delay_us,
  467. t->data);
  468. if (seq_has_overflowed(m))
  469. return 0;
  470. }
  471. seq_putc(m, '\n');
  472. num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
  473. NUM_TIMER_STATS);
  474. seq_printf(m, "Timers expired: %i\n", fast_timers_expired);
  475. for (i = 0; i < num_to_show; i++){
  476. t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
  477. seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu d: %6li us data: 0x%08lX\n",
  478. t->name,
  479. (unsigned long)t->tv_set.tv_jiff,
  480. (unsigned long)t->tv_set.tv_usec,
  481. (unsigned long)t->tv_expires.tv_jiff,
  482. (unsigned long)t->tv_expires.tv_usec,
  483. t->delay_us,
  484. t->data);
  485. if (seq_has_overflowed(m))
  486. return 0;
  487. }
  488. seq_putc(m, '\n');
  489. #endif
  490. seq_puts(m, "Active timers:\n");
  491. local_irq_save(flags);
  492. t = fast_timer_list;
  493. while (t != NULL){
  494. nextt = t->next;
  495. local_irq_restore(flags);
  496. seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu d: %6li us data: 0x%08lX\n",
  497. t->name,
  498. (unsigned long)t->tv_set.tv_jiff,
  499. (unsigned long)t->tv_set.tv_usec,
  500. (unsigned long)t->tv_expires.tv_jiff,
  501. (unsigned long)t->tv_expires.tv_usec,
  502. t->delay_us,
  503. t->data);
  504. if (seq_has_overflowed(m))
  505. return 0;
  506. local_irq_save(flags);
  507. if (t->next != nextt)
  508. printk("timer removed!\n");
  509. t = nextt;
  510. }
  511. local_irq_restore(flags);
  512. return 0;
  513. }
  514. static int proc_fasttimer_open(struct inode *inode, struct file *file)
  515. {
  516. return single_open_size(file, proc_fasttimer_show, PDE_DATA(inode), BIG_BUF_SIZE);
  517. }
  518. static const struct file_operations proc_fasttimer_fops = {
  519. .open = proc_fasttimer_open,
  520. .read = seq_read,
  521. .llseek = seq_lseek,
  522. .release = single_release,
  523. };
  524. #endif /* PROC_FS */
  525. #ifdef FAST_TIMER_TEST
  526. static volatile unsigned long i = 0;
  527. static volatile int num_test_timeout = 0;
  528. static struct fast_timer tr[10];
  529. static int exp_num[10];
  530. static struct fasttime_t tv_exp[100];
  531. static void test_timeout(unsigned long data)
  532. {
  533. do_gettimeofday_fast(&tv_exp[data]);
  534. exp_num[data] = num_test_timeout;
  535. num_test_timeout++;
  536. }
  537. static void test_timeout1(unsigned long data)
  538. {
  539. do_gettimeofday_fast(&tv_exp[data]);
  540. exp_num[data] = num_test_timeout;
  541. if (data < 7)
  542. {
  543. start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
  544. i++;
  545. }
  546. num_test_timeout++;
  547. }
  548. DP(
  549. static char buf0[2000];
  550. static char buf1[2000];
  551. static char buf2[2000];
  552. static char buf3[2000];
  553. static char buf4[2000];
  554. );
  555. static char buf5[6000];
  556. static int j_u[1000];
  557. static void fast_timer_test(void)
  558. {
  559. int prev_num;
  560. int j;
  561. struct fasttime_t tv, tv0, tv1, tv2;
  562. printk("fast_timer_test() start\n");
  563. do_gettimeofday_fast(&tv);
  564. for (j = 0; j < 1000; j++)
  565. {
  566. j_u[j] = GET_JIFFIES_USEC();
  567. }
  568. for (j = 0; j < 100; j++)
  569. {
  570. do_gettimeofday_fast(&tv_exp[j]);
  571. }
  572. printk(KERN_DEBUG "fast_timer_test() %is %06i\n", tv.tv_jiff, tv.tv_usec);
  573. for (j = 0; j < 1000; j++)
  574. {
  575. printk(KERN_DEBUG "%i %i %i %i %i\n",
  576. j_u[j], j_u[j+1], j_u[j+2], j_u[j+3], j_u[j+4]);
  577. j += 4;
  578. }
  579. for (j = 0; j < 100; j++)
  580. {
  581. printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n",
  582. tv_exp[j].tv_jiff, tv_exp[j].tv_usec,
  583. tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
  584. tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
  585. tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
  586. tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
  587. j += 4;
  588. }
  589. do_gettimeofday_fast(&tv0);
  590. start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
  591. DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
  592. i++;
  593. start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
  594. DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
  595. i++;
  596. start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
  597. DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
  598. i++;
  599. start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
  600. DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
  601. i++;
  602. start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
  603. DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
  604. i++;
  605. do_gettimeofday_fast(&tv1);
  606. proc_fasttimer_read(buf5, NULL, 0, 0, 0);
  607. prev_num = num_test_timeout;
  608. while (num_test_timeout < i)
  609. {
  610. if (num_test_timeout != prev_num)
  611. prev_num = num_test_timeout;
  612. }
  613. do_gettimeofday_fast(&tv2);
  614. printk(KERN_INFO "Timers started %is %06i\n",
  615. tv0.tv_jiff, tv0.tv_usec);
  616. printk(KERN_INFO "Timers started at %is %06i\n",
  617. tv1.tv_jiff, tv1.tv_usec);
  618. printk(KERN_INFO "Timers done %is %06i\n",
  619. tv2.tv_jiff, tv2.tv_usec);
  620. DP(printk("buf0:\n");
  621. printk(buf0);
  622. printk("buf1:\n");
  623. printk(buf1);
  624. printk("buf2:\n");
  625. printk(buf2);
  626. printk("buf3:\n");
  627. printk(buf3);
  628. printk("buf4:\n");
  629. printk(buf4);
  630. );
  631. printk("buf5:\n");
  632. printk(buf5);
  633. printk("timers set:\n");
  634. for(j = 0; j<i; j++)
  635. {
  636. struct fast_timer *t = &tr[j];
  637. printk("%-10s set: %6is %06ius exp: %6is %06ius "
  638. "data: 0x%08X func: 0x%08X\n",
  639. t->name,
  640. t->tv_set.tv_jiff,
  641. t->tv_set.tv_usec,
  642. t->tv_expires.tv_jiff,
  643. t->tv_expires.tv_usec,
  644. t->data,
  645. t->function
  646. );
  647. printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
  648. t->delay_us,
  649. tv_exp[j].tv_jiff,
  650. tv_exp[j].tv_usec,
  651. exp_num[j],
  652. (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
  653. 1000000 + tv_exp[j].tv_usec -
  654. t->tv_expires.tv_usec);
  655. }
  656. proc_fasttimer_read(buf5, NULL, 0, 0, 0);
  657. printk("buf5 after all done:\n");
  658. printk(buf5);
  659. printk("fast_timer_test() done\n");
  660. }
  661. #endif
  662. int fast_timer_init(void)
  663. {
  664. /* For some reason, request_irq() hangs when called froom time_init() */
  665. if (!fast_timer_is_init)
  666. {
  667. printk("fast_timer_init()\n");
  668. #ifdef CONFIG_PROC_FS
  669. proc_create("fasttimer", 0, NULL, &proc_fasttimer_fops);
  670. #endif /* PROC_FS */
  671. if (request_irq(TIMER0_INTR_VECT, timer_trig_interrupt,
  672. IRQF_SHARED,
  673. "fast timer int", &fast_timer_list))
  674. printk(KERN_ERR "err: fasttimer irq\n");
  675. fast_timer_is_init = 1;
  676. #ifdef FAST_TIMER_TEST
  677. printk("do test\n");
  678. fast_timer_test();
  679. #endif
  680. }
  681. return 0;
  682. }
  683. __initcall(fast_timer_init);