fasttimer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. 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. /* 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. if (seq_printf(m, debug_log_string[i], debug_log_value[i]) < 0)
  422. return 0;
  423. i = (i+1) % DEBUG_LOG_MAX;
  424. }
  425. }
  426. seq_putc(m, '\n');
  427. #endif
  428. num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
  429. NUM_TIMER_STATS);
  430. seq_printf(m, "Timers started: %i\n", fast_timers_started);
  431. for (i = 0; i < num_to_show; i++) {
  432. int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
  433. #if 1 //ndef FAST_TIMER_LOG
  434. seq_printf(m, "div: %i delay: %i"
  435. "\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. if (seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  442. "d: %6li us data: 0x%08lX"
  443. "\n",
  444. t->name,
  445. (unsigned long)t->tv_set.tv_jiff,
  446. (unsigned long)t->tv_set.tv_usec,
  447. (unsigned long)t->tv_expires.tv_jiff,
  448. (unsigned long)t->tv_expires.tv_usec,
  449. t->delay_us,
  450. t->data) < 0)
  451. return 0;
  452. #endif
  453. }
  454. seq_putc(m, '\n');
  455. #ifdef FAST_TIMER_LOG
  456. num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
  457. NUM_TIMER_STATS);
  458. seq_printf(m, "Timers added: %i\n", fast_timers_added);
  459. for (i = 0; i < num_to_show; i++) {
  460. t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
  461. if (seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  462. "d: %6li us data: 0x%08lX"
  463. "\n",
  464. t->name,
  465. (unsigned long)t->tv_set.tv_jiff,
  466. (unsigned long)t->tv_set.tv_usec,
  467. (unsigned long)t->tv_expires.tv_jiff,
  468. (unsigned long)t->tv_expires.tv_usec,
  469. t->delay_us,
  470. t->data) < 0)
  471. return 0;
  472. }
  473. seq_putc(m, '\n');
  474. num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
  475. NUM_TIMER_STATS);
  476. seq_printf(m, "Timers expired: %i\n", fast_timers_expired);
  477. for (i = 0; i < num_to_show; i++){
  478. t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
  479. if (seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  480. "d: %6li us data: 0x%08lX"
  481. "\n",
  482. t->name,
  483. (unsigned long)t->tv_set.tv_jiff,
  484. (unsigned long)t->tv_set.tv_usec,
  485. (unsigned long)t->tv_expires.tv_jiff,
  486. (unsigned long)t->tv_expires.tv_usec,
  487. t->delay_us,
  488. t->data) < 0)
  489. return 0;
  490. }
  491. seq_putc(m, '\n');
  492. #endif
  493. seq_puts(m, "Active timers:\n");
  494. local_irq_save(flags);
  495. t = fast_timer_list;
  496. while (t != NULL){
  497. nextt = t->next;
  498. local_irq_restore(flags);
  499. if (seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  500. "d: %6li us data: 0x%08lX"
  501. /* " func: 0x%08lX" */
  502. "\n",
  503. t->name,
  504. (unsigned long)t->tv_set.tv_jiff,
  505. (unsigned long)t->tv_set.tv_usec,
  506. (unsigned long)t->tv_expires.tv_jiff,
  507. (unsigned long)t->tv_expires.tv_usec,
  508. t->delay_us,
  509. t->data
  510. /* , t->function */
  511. ) < 0)
  512. return 0;
  513. local_irq_save(flags);
  514. if (t->next != nextt)
  515. printk("timer removed!\n");
  516. t = nextt;
  517. }
  518. local_irq_restore(flags);
  519. return 0;
  520. }
  521. static int proc_fasttimer_open(struct inode *inode, struct file *file)
  522. {
  523. return single_open_size(file, proc_fasttimer_show, PDE_DATA(inode), BIG_BUF_SIZE);
  524. }
  525. static const struct file_operations proc_fasttimer_fops = {
  526. .open = proc_fasttimer_open,
  527. .read = seq_read,
  528. .llseek = seq_lseek,
  529. .release = seq_release,
  530. };
  531. #endif /* PROC_FS */
  532. #ifdef FAST_TIMER_TEST
  533. static volatile unsigned long i = 0;
  534. static volatile int num_test_timeout = 0;
  535. static struct fast_timer tr[10];
  536. static int exp_num[10];
  537. static struct fasttime_t tv_exp[100];
  538. static void test_timeout(unsigned long data)
  539. {
  540. do_gettimeofday_fast(&tv_exp[data]);
  541. exp_num[data] = num_test_timeout;
  542. num_test_timeout++;
  543. }
  544. static void test_timeout1(unsigned long data)
  545. {
  546. do_gettimeofday_fast(&tv_exp[data]);
  547. exp_num[data] = num_test_timeout;
  548. if (data < 7)
  549. {
  550. start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
  551. i++;
  552. }
  553. num_test_timeout++;
  554. }
  555. DP(
  556. static char buf0[2000];
  557. static char buf1[2000];
  558. static char buf2[2000];
  559. static char buf3[2000];
  560. static char buf4[2000];
  561. );
  562. static char buf5[6000];
  563. static int j_u[1000];
  564. static void fast_timer_test(void)
  565. {
  566. int prev_num;
  567. int j;
  568. struct fasttime_t tv, tv0, tv1, tv2;
  569. printk("fast_timer_test() start\n");
  570. do_gettimeofday_fast(&tv);
  571. for (j = 0; j < 1000; j++)
  572. {
  573. j_u[j] = GET_JIFFIES_USEC();
  574. }
  575. for (j = 0; j < 100; j++)
  576. {
  577. do_gettimeofday_fast(&tv_exp[j]);
  578. }
  579. printk(KERN_DEBUG "fast_timer_test() %is %06i\n", tv.tv_jiff, tv.tv_usec);
  580. for (j = 0; j < 1000; j++)
  581. {
  582. printk(KERN_DEBUG "%i %i %i %i %i\n",
  583. j_u[j], j_u[j+1], j_u[j+2], j_u[j+3], j_u[j+4]);
  584. j += 4;
  585. }
  586. for (j = 0; j < 100; j++)
  587. {
  588. printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n",
  589. tv_exp[j].tv_jiff, tv_exp[j].tv_usec,
  590. tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
  591. tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
  592. tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
  593. tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
  594. j += 4;
  595. }
  596. do_gettimeofday_fast(&tv0);
  597. start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
  598. DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
  599. i++;
  600. start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
  601. DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
  602. i++;
  603. start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
  604. DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
  605. i++;
  606. start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
  607. DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
  608. i++;
  609. start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
  610. DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
  611. i++;
  612. do_gettimeofday_fast(&tv1);
  613. proc_fasttimer_read(buf5, NULL, 0, 0, 0);
  614. prev_num = num_test_timeout;
  615. while (num_test_timeout < i)
  616. {
  617. if (num_test_timeout != prev_num)
  618. prev_num = num_test_timeout;
  619. }
  620. do_gettimeofday_fast(&tv2);
  621. printk(KERN_INFO "Timers started %is %06i\n",
  622. tv0.tv_jiff, tv0.tv_usec);
  623. printk(KERN_INFO "Timers started at %is %06i\n",
  624. tv1.tv_jiff, tv1.tv_usec);
  625. printk(KERN_INFO "Timers done %is %06i\n",
  626. tv2.tv_jiff, tv2.tv_usec);
  627. DP(printk("buf0:\n");
  628. printk(buf0);
  629. printk("buf1:\n");
  630. printk(buf1);
  631. printk("buf2:\n");
  632. printk(buf2);
  633. printk("buf3:\n");
  634. printk(buf3);
  635. printk("buf4:\n");
  636. printk(buf4);
  637. );
  638. printk("buf5:\n");
  639. printk(buf5);
  640. printk("timers set:\n");
  641. for(j = 0; j<i; j++)
  642. {
  643. struct fast_timer *t = &tr[j];
  644. printk("%-10s set: %6is %06ius exp: %6is %06ius "
  645. "data: 0x%08X func: 0x%08X\n",
  646. t->name,
  647. t->tv_set.tv_jiff,
  648. t->tv_set.tv_usec,
  649. t->tv_expires.tv_jiff,
  650. t->tv_expires.tv_usec,
  651. t->data,
  652. t->function
  653. );
  654. printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
  655. t->delay_us,
  656. tv_exp[j].tv_jiff,
  657. tv_exp[j].tv_usec,
  658. exp_num[j],
  659. (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
  660. 1000000 + tv_exp[j].tv_usec -
  661. t->tv_expires.tv_usec);
  662. }
  663. proc_fasttimer_read(buf5, NULL, 0, 0, 0);
  664. printk("buf5 after all done:\n");
  665. printk(buf5);
  666. printk("fast_timer_test() done\n");
  667. }
  668. #endif
  669. int fast_timer_init(void)
  670. {
  671. /* For some reason, request_irq() hangs when called froom time_init() */
  672. if (!fast_timer_is_init)
  673. {
  674. printk("fast_timer_init()\n");
  675. #ifdef CONFIG_PROC_FS
  676. proc_create("fasttimer", 0, NULL, &proc_fasttimer_fops);
  677. #endif /* PROC_FS */
  678. if (request_irq(TIMER0_INTR_VECT, timer_trig_interrupt,
  679. IRQF_SHARED | IRQF_DISABLED,
  680. "fast timer int", &fast_timer_list))
  681. printk(KERN_ERR "err: fasttimer irq\n");
  682. fast_timer_is_init = 1;
  683. #ifdef FAST_TIMER_TEST
  684. printk("do test\n");
  685. fast_timer_test();
  686. #endif
  687. }
  688. return 0;
  689. }
  690. __initcall(fast_timer_init);