fasttimer.c 21 KB

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