fasttimer.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /* $Id: fasttimer.c,v 1.11 2005/01/04 11:15:46 starvik Exp $
  2. * linux/arch/cris/kernel/fasttimer.c
  3. *
  4. * Fast timers for ETRAX FS
  5. * This may be useful in other OS than Linux so use 2 space indentation...
  6. *
  7. * $Log: fasttimer.c,v $
  8. * Revision 1.11 2005/01/04 11:15:46 starvik
  9. * Don't share timer IRQ.
  10. *
  11. * Revision 1.10 2004/12/07 09:19:38 starvik
  12. * Corrected includes.
  13. * Use correct interrupt macros.
  14. *
  15. * Revision 1.9 2004/05/14 10:18:58 starvik
  16. * Export fast_timer_list
  17. *
  18. * Revision 1.8 2004/05/14 07:58:03 starvik
  19. * Merge of changes from 2.4
  20. *
  21. * Revision 1.7 2003/07/10 12:06:14 starvik
  22. * Return IRQ_NONE if irq wasn't handled
  23. *
  24. * Revision 1.6 2003/07/04 08:27:49 starvik
  25. * Merge of Linux 2.5.74
  26. *
  27. * Revision 1.5 2003/06/05 10:16:22 johana
  28. * New INTR_VECT macros.
  29. *
  30. * Revision 1.4 2003/06/03 08:49:45 johana
  31. * Fixed typo.
  32. *
  33. * Revision 1.3 2003/06/02 12:51:27 johana
  34. * Now compiles.
  35. * Commented some include files that probably can be removed.
  36. *
  37. * Revision 1.2 2003/06/02 12:09:41 johana
  38. * Ported to ETRAX FS using the trig interrupt instead of timer1.
  39. *
  40. * Revision 1.3 2002/12/12 08:26:32 starvik
  41. * Don't use C-comments inside CVS comments
  42. *
  43. * Revision 1.2 2002/12/11 15:42:02 starvik
  44. * Extracted v10 (ETRAX 100LX) specific stuff from arch/cris/kernel/
  45. *
  46. * Revision 1.1 2002/11/18 07:58:06 starvik
  47. * Fast timers (from Linux 2.4)
  48. *
  49. * Revision 1.5 2002/10/15 06:21:39 starvik
  50. * Added call to init_waitqueue_head
  51. *
  52. * Revision 1.4 2002/05/28 17:47:59 johana
  53. * Added del_fast_timer()
  54. *
  55. * Revision 1.3 2002/05/28 16:16:07 johana
  56. * Handle empty fast_timer_list
  57. *
  58. * Revision 1.2 2002/05/27 15:38:42 johana
  59. * Made it compile without warnings on Linux 2.4.
  60. * (includes, wait_queue, PROC_FS and snprintf)
  61. *
  62. * Revision 1.1 2002/05/27 15:32:25 johana
  63. * arch/etrax100/kernel/fasttimer.c v1.8 from the elinux tree.
  64. *
  65. * Revision 1.8 2001/11/27 13:50:40 pkj
  66. * Disable interrupts while stopping the timer and while modifying the
  67. * list of active timers in timer1_handler() as it may be interrupted
  68. * by other interrupts (e.g., the serial interrupt) which may add fast
  69. * timers.
  70. *
  71. * Revision 1.7 2001/11/22 11:50:32 pkj
  72. * * Only store information about the last 16 timers.
  73. * * proc_fasttimer_read() now uses an allocated buffer, since it
  74. * requires more space than just a page even for only writing the
  75. * last 16 timers. The buffer is only allocated on request, so
  76. * unless /proc/fasttimer is read, it is never allocated.
  77. * * Renamed fast_timer_started to fast_timers_started to match
  78. * fast_timers_added and fast_timers_expired.
  79. * * Some clean-up.
  80. *
  81. * Revision 1.6 2000/12/13 14:02:08 johana
  82. * Removed volatile for fast_timer_list
  83. *
  84. * Revision 1.5 2000/12/13 13:55:35 johana
  85. * Added DEBUG_LOG, added som cli() and cleanup
  86. *
  87. * Revision 1.4 2000/12/05 13:48:50 johana
  88. * Added range check when writing proc file, modified timer int handling
  89. *
  90. * Revision 1.3 2000/11/23 10:10:20 johana
  91. * More debug/logging possibilities.
  92. * Moved GET_JIFFIES_USEC() to timex.h and time.c
  93. *
  94. * Revision 1.2 2000/11/01 13:41:04 johana
  95. * Clean up and bugfixes.
  96. * Created new do_gettimeofday_fast() that gets a timeval struct
  97. * with time based on jiffies and *R_TIMER0_DATA, uses a table
  98. * for fast conversion of timer value to microseconds.
  99. * (Much faster the standard do_gettimeofday() and we don't really
  100. * wan't to use the true time - we wan't the "uptime" so timers don't screw up
  101. * when we change the time.
  102. * TODO: Add efficient support for continuous timers as well.
  103. *
  104. * Revision 1.1 2000/10/26 15:49:16 johana
  105. * Added fasttimer, highresolution timers.
  106. *
  107. * Copyright (C) 2000,2001 2002, 2003 Axis Communications AB, Lund, Sweden
  108. */
  109. #include <linux/errno.h>
  110. #include <linux/sched.h>
  111. #include <linux/kernel.h>
  112. #include <linux/param.h>
  113. #include <linux/string.h>
  114. #include <linux/vmalloc.h>
  115. #include <linux/interrupt.h>
  116. #include <linux/time.h>
  117. #include <linux/delay.h>
  118. #include <asm/irq.h>
  119. #include <asm/system.h>
  120. #include <linux/config.h>
  121. #include <linux/version.h>
  122. #include <asm/arch/hwregs/reg_map.h>
  123. #include <asm/arch/hwregs/reg_rdwr.h>
  124. #include <asm/arch/hwregs/timer_defs.h>
  125. #include <asm/fasttimer.h>
  126. #include <linux/proc_fs.h>
  127. /*
  128. * timer0 is running at 100MHz and generating jiffies timer ticks
  129. * at 100 or 1000 HZ.
  130. * fasttimer gives an API that gives timers that expire "between" the jiffies
  131. * giving microsecond resolution (10 ns).
  132. * fasttimer uses reg_timer_rw_trig register to get interrupt when
  133. * r_time reaches a certain value.
  134. */
  135. #define DEBUG_LOG_INCLUDED
  136. #define FAST_TIMER_LOG
  137. //#define FAST_TIMER_TEST
  138. #define FAST_TIMER_SANITY_CHECKS
  139. #ifdef FAST_TIMER_SANITY_CHECKS
  140. #define SANITYCHECK(x) x
  141. static int sanity_failed = 0;
  142. #else
  143. #define SANITYCHECK(x)
  144. #endif
  145. #define D1(x)
  146. #define D2(x)
  147. #define DP(x)
  148. #define __INLINE__ inline
  149. static int fast_timer_running = 0;
  150. static int fast_timers_added = 0;
  151. static int fast_timers_started = 0;
  152. static int fast_timers_expired = 0;
  153. static int fast_timers_deleted = 0;
  154. static int fast_timer_is_init = 0;
  155. static int fast_timer_ints = 0;
  156. struct fast_timer *fast_timer_list = NULL;
  157. #ifdef DEBUG_LOG_INCLUDED
  158. #define DEBUG_LOG_MAX 128
  159. static const char * debug_log_string[DEBUG_LOG_MAX];
  160. static unsigned long debug_log_value[DEBUG_LOG_MAX];
  161. static int debug_log_cnt = 0;
  162. static int debug_log_cnt_wrapped = 0;
  163. #define DEBUG_LOG(string, value) \
  164. { \
  165. unsigned long log_flags; \
  166. local_irq_save(log_flags); \
  167. debug_log_string[debug_log_cnt] = (string); \
  168. debug_log_value[debug_log_cnt] = (unsigned long)(value); \
  169. if (++debug_log_cnt >= DEBUG_LOG_MAX) \
  170. { \
  171. debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \
  172. debug_log_cnt_wrapped = 1; \
  173. } \
  174. local_irq_restore(log_flags); \
  175. }
  176. #else
  177. #define DEBUG_LOG(string, value)
  178. #endif
  179. #define NUM_TIMER_STATS 16
  180. #ifdef FAST_TIMER_LOG
  181. struct fast_timer timer_added_log[NUM_TIMER_STATS];
  182. struct fast_timer timer_started_log[NUM_TIMER_STATS];
  183. struct fast_timer timer_expired_log[NUM_TIMER_STATS];
  184. #endif
  185. int timer_div_settings[NUM_TIMER_STATS];
  186. int timer_delay_settings[NUM_TIMER_STATS];
  187. static void
  188. timer_trig_handler(void);
  189. /* Not true gettimeofday, only checks the jiffies (uptime) + useconds */
  190. void __INLINE__ do_gettimeofday_fast(struct timeval *tv)
  191. {
  192. unsigned long sec = jiffies;
  193. unsigned long usec = GET_JIFFIES_USEC();
  194. usec += (sec % HZ) * (1000000 / HZ);
  195. sec = sec / HZ;
  196. if (usec > 1000000)
  197. {
  198. usec -= 1000000;
  199. sec++;
  200. }
  201. tv->tv_sec = sec;
  202. tv->tv_usec = usec;
  203. }
  204. int __INLINE__ timeval_cmp(struct timeval *t0, struct timeval *t1)
  205. {
  206. if (t0->tv_sec < t1->tv_sec)
  207. {
  208. return -1;
  209. }
  210. else if (t0->tv_sec > t1->tv_sec)
  211. {
  212. return 1;
  213. }
  214. if (t0->tv_usec < t1->tv_usec)
  215. {
  216. return -1;
  217. }
  218. else if (t0->tv_usec > t1->tv_usec)
  219. {
  220. return 1;
  221. }
  222. return 0;
  223. }
  224. /* Called with ints off */
  225. void __INLINE__ start_timer_trig(unsigned long delay_us)
  226. {
  227. reg_timer_rw_ack_intr ack_intr = { 0 };
  228. reg_timer_rw_intr_mask intr_mask;
  229. reg_timer_rw_trig trig;
  230. reg_timer_rw_trig_cfg trig_cfg = { 0 };
  231. reg_timer_r_time r_time;
  232. r_time = REG_RD(timer, regi_timer, r_time);
  233. D1(printk("start_timer_trig : %d us freq: %i div: %i\n",
  234. delay_us, freq_index, div));
  235. /* Clear trig irq */
  236. intr_mask = REG_RD(timer, regi_timer, rw_intr_mask);
  237. intr_mask.trig = 0;
  238. REG_WR(timer, regi_timer, rw_intr_mask, intr_mask);
  239. /* Set timer values */
  240. /* r_time is 100MHz (10 ns resolution) */
  241. trig = r_time + delay_us*(1000/10);
  242. timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = trig;
  243. timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us;
  244. /* Ack interrupt */
  245. ack_intr.trig = 1;
  246. REG_WR(timer, regi_timer, rw_ack_intr, ack_intr);
  247. /* Start timer */
  248. REG_WR(timer, regi_timer, rw_trig, trig);
  249. trig_cfg.tmr = regk_timer_time;
  250. REG_WR(timer, regi_timer, rw_trig_cfg, trig_cfg);
  251. /* Check if we have already passed the trig time */
  252. r_time = REG_RD(timer, regi_timer, r_time);
  253. if (r_time < trig) {
  254. /* No, Enable trig irq */
  255. intr_mask = REG_RD(timer, regi_timer, rw_intr_mask);
  256. intr_mask.trig = 1;
  257. REG_WR(timer, regi_timer, rw_intr_mask, intr_mask);
  258. fast_timers_started++;
  259. fast_timer_running = 1;
  260. }
  261. else
  262. {
  263. /* We have passed the time, disable trig point, ack intr */
  264. trig_cfg.tmr = regk_timer_off;
  265. REG_WR(timer, regi_timer, rw_trig_cfg, trig_cfg);
  266. REG_WR(timer, regi_timer, rw_ack_intr, ack_intr);
  267. /* call the int routine directly */
  268. timer_trig_handler();
  269. }
  270. }
  271. /* In version 1.4 this function takes 27 - 50 us */
  272. void start_one_shot_timer(struct fast_timer *t,
  273. fast_timer_function_type *function,
  274. unsigned long data,
  275. unsigned long delay_us,
  276. const char *name)
  277. {
  278. unsigned long flags;
  279. struct fast_timer *tmp;
  280. D1(printk("sft %s %d us\n", name, delay_us));
  281. local_irq_save(flags);
  282. do_gettimeofday_fast(&t->tv_set);
  283. tmp = fast_timer_list;
  284. SANITYCHECK({ /* Check so this is not in the list already... */
  285. while (tmp != NULL)
  286. {
  287. if (tmp == t)
  288. {
  289. printk("timer name: %s data: 0x%08lX already in list!\n", name, data);
  290. sanity_failed++;
  291. return;
  292. }
  293. else
  294. {
  295. tmp = tmp->next;
  296. }
  297. }
  298. tmp = fast_timer_list;
  299. });
  300. t->delay_us = delay_us;
  301. t->function = function;
  302. t->data = data;
  303. t->name = name;
  304. t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000;
  305. t->tv_expires.tv_sec = t->tv_set.tv_sec + delay_us / 1000000;
  306. if (t->tv_expires.tv_usec > 1000000)
  307. {
  308. t->tv_expires.tv_usec -= 1000000;
  309. t->tv_expires.tv_sec++;
  310. }
  311. #ifdef FAST_TIMER_LOG
  312. timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t;
  313. #endif
  314. fast_timers_added++;
  315. /* Check if this should timeout before anything else */
  316. if (tmp == NULL || timeval_cmp(&t->tv_expires, &tmp->tv_expires) < 0)
  317. {
  318. /* Put first in list and modify the timer value */
  319. t->prev = NULL;
  320. t->next = fast_timer_list;
  321. if (fast_timer_list)
  322. {
  323. fast_timer_list->prev = t;
  324. }
  325. fast_timer_list = t;
  326. #ifdef FAST_TIMER_LOG
  327. timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
  328. #endif
  329. start_timer_trig(delay_us);
  330. } else {
  331. /* Put in correct place in list */
  332. while (tmp->next &&
  333. timeval_cmp(&t->tv_expires, &tmp->next->tv_expires) > 0)
  334. {
  335. tmp = tmp->next;
  336. }
  337. /* Insert t after tmp */
  338. t->prev = tmp;
  339. t->next = tmp->next;
  340. if (tmp->next)
  341. {
  342. tmp->next->prev = t;
  343. }
  344. tmp->next = t;
  345. }
  346. D2(printk("start_one_shot_timer: %d us done\n", delay_us));
  347. local_irq_restore(flags);
  348. } /* start_one_shot_timer */
  349. static inline int fast_timer_pending (const struct fast_timer * t)
  350. {
  351. return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list);
  352. }
  353. static inline int detach_fast_timer (struct fast_timer *t)
  354. {
  355. struct fast_timer *next, *prev;
  356. if (!fast_timer_pending(t))
  357. return 0;
  358. next = t->next;
  359. prev = t->prev;
  360. if (next)
  361. next->prev = prev;
  362. if (prev)
  363. prev->next = next;
  364. else
  365. fast_timer_list = next;
  366. fast_timers_deleted++;
  367. return 1;
  368. }
  369. int del_fast_timer(struct fast_timer * t)
  370. {
  371. unsigned long flags;
  372. int ret;
  373. local_irq_save(flags);
  374. ret = detach_fast_timer(t);
  375. t->next = t->prev = NULL;
  376. local_irq_restore(flags);
  377. return ret;
  378. } /* del_fast_timer */
  379. /* Interrupt routines or functions called in interrupt context */
  380. /* Timer interrupt handler for trig interrupts */
  381. static irqreturn_t
  382. timer_trig_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  383. {
  384. reg_timer_r_masked_intr masked_intr;
  385. /* Check if the timer interrupt is for us (a trig int) */
  386. masked_intr = REG_RD(timer, regi_timer, r_masked_intr);
  387. if (!masked_intr.trig)
  388. return IRQ_NONE;
  389. timer_trig_handler();
  390. return IRQ_HANDLED;
  391. }
  392. static void timer_trig_handler(void)
  393. {
  394. reg_timer_rw_ack_intr ack_intr = { 0 };
  395. reg_timer_rw_intr_mask intr_mask;
  396. reg_timer_rw_trig_cfg trig_cfg = { 0 };
  397. struct fast_timer *t;
  398. unsigned long flags;
  399. local_irq_save(flags);
  400. /* Clear timer trig interrupt */
  401. intr_mask = REG_RD(timer, regi_timer, rw_intr_mask);
  402. intr_mask.trig = 0;
  403. REG_WR(timer, regi_timer, rw_intr_mask, intr_mask);
  404. /* First stop timer, then ack interrupt */
  405. /* Stop timer */
  406. trig_cfg.tmr = regk_timer_off;
  407. REG_WR(timer, regi_timer, rw_trig_cfg, trig_cfg);
  408. /* Ack interrupt */
  409. ack_intr.trig = 1;
  410. REG_WR(timer, regi_timer, rw_ack_intr, ack_intr);
  411. fast_timer_running = 0;
  412. fast_timer_ints++;
  413. local_irq_restore(flags);
  414. t = fast_timer_list;
  415. while (t)
  416. {
  417. struct timeval tv;
  418. /* Has it really expired? */
  419. do_gettimeofday_fast(&tv);
  420. D1(printk("t: %is %06ius\n", tv.tv_sec, tv.tv_usec));
  421. if (timeval_cmp(&t->tv_expires, &tv) <= 0)
  422. {
  423. /* Yes it has expired */
  424. #ifdef FAST_TIMER_LOG
  425. timer_expired_log[fast_timers_expired % NUM_TIMER_STATS] = *t;
  426. #endif
  427. fast_timers_expired++;
  428. /* Remove this timer before call, since it may reuse the timer */
  429. local_irq_save(flags);
  430. if (t->prev)
  431. {
  432. t->prev->next = t->next;
  433. }
  434. else
  435. {
  436. fast_timer_list = t->next;
  437. }
  438. if (t->next)
  439. {
  440. t->next->prev = t->prev;
  441. }
  442. t->prev = NULL;
  443. t->next = NULL;
  444. local_irq_restore(flags);
  445. if (t->function != NULL)
  446. {
  447. t->function(t->data);
  448. }
  449. else
  450. {
  451. DEBUG_LOG("!trimertrig %i function==NULL!\n", fast_timer_ints);
  452. }
  453. }
  454. else
  455. {
  456. /* Timer is to early, let's set it again using the normal routines */
  457. D1(printk(".\n"));
  458. }
  459. local_irq_save(flags);
  460. if ((t = fast_timer_list) != NULL)
  461. {
  462. /* Start next timer.. */
  463. long us;
  464. struct timeval tv;
  465. do_gettimeofday_fast(&tv);
  466. us = ((t->tv_expires.tv_sec - tv.tv_sec) * 1000000 +
  467. t->tv_expires.tv_usec - tv.tv_usec);
  468. if (us > 0)
  469. {
  470. if (!fast_timer_running)
  471. {
  472. #ifdef FAST_TIMER_LOG
  473. timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
  474. #endif
  475. start_timer_trig(us);
  476. }
  477. local_irq_restore(flags);
  478. break;
  479. }
  480. else
  481. {
  482. /* Timer already expired, let's handle it better late than never.
  483. * The normal loop handles it
  484. */
  485. D1(printk("e! %d\n", us));
  486. }
  487. }
  488. local_irq_restore(flags);
  489. }
  490. if (!t)
  491. {
  492. D1(printk("ttrig stop!\n"));
  493. }
  494. }
  495. static void wake_up_func(unsigned long data)
  496. {
  497. #ifdef DECLARE_WAITQUEUE
  498. wait_queue_head_t *sleep_wait_p = (wait_queue_head_t*)data;
  499. #else
  500. struct wait_queue **sleep_wait_p = (struct wait_queue **)data;
  501. #endif
  502. wake_up(sleep_wait_p);
  503. }
  504. /* Useful API */
  505. void schedule_usleep(unsigned long us)
  506. {
  507. struct fast_timer t;
  508. #ifdef DECLARE_WAITQUEUE
  509. wait_queue_head_t sleep_wait;
  510. init_waitqueue_head(&sleep_wait);
  511. {
  512. DECLARE_WAITQUEUE(wait, current);
  513. #else
  514. struct wait_queue *sleep_wait = NULL;
  515. struct wait_queue wait = { current, NULL };
  516. #endif
  517. D1(printk("schedule_usleep(%d)\n", us));
  518. add_wait_queue(&sleep_wait, &wait);
  519. set_current_state(TASK_INTERRUPTIBLE);
  520. start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
  521. "usleep");
  522. schedule();
  523. set_current_state(TASK_RUNNING);
  524. remove_wait_queue(&sleep_wait, &wait);
  525. D1(printk("done schedule_usleep(%d)\n", us));
  526. #ifdef DECLARE_WAITQUEUE
  527. }
  528. #endif
  529. }
  530. #ifdef CONFIG_PROC_FS
  531. static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
  532. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
  533. ,int *eof, void *data_unused
  534. #else
  535. ,int unused
  536. #endif
  537. );
  538. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
  539. static struct proc_dir_entry *fasttimer_proc_entry;
  540. #else
  541. static struct proc_dir_entry fasttimer_proc_entry =
  542. {
  543. 0, 9, "fasttimer",
  544. S_IFREG | S_IRUGO, 1, 0, 0,
  545. 0, NULL /* ops -- default to array */,
  546. &proc_fasttimer_read /* get_info */,
  547. };
  548. #endif
  549. #endif /* CONFIG_PROC_FS */
  550. #ifdef CONFIG_PROC_FS
  551. /* This value is very much based on testing */
  552. #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
  553. static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
  554. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
  555. ,int *eof, void *data_unused
  556. #else
  557. ,int unused
  558. #endif
  559. )
  560. {
  561. unsigned long flags;
  562. int i = 0;
  563. int num_to_show;
  564. struct timeval tv;
  565. struct fast_timer *t, *nextt;
  566. static char *bigbuf = NULL;
  567. static unsigned long used;
  568. if (!bigbuf && !(bigbuf = vmalloc(BIG_BUF_SIZE)))
  569. {
  570. used = 0;
  571. bigbuf[0] = '\0';
  572. return 0;
  573. }
  574. if (!offset || !used)
  575. {
  576. do_gettimeofday_fast(&tv);
  577. used = 0;
  578. used += sprintf(bigbuf + used, "Fast timers added: %i\n",
  579. fast_timers_added);
  580. used += sprintf(bigbuf + used, "Fast timers started: %i\n",
  581. fast_timers_started);
  582. used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
  583. fast_timer_ints);
  584. used += sprintf(bigbuf + used, "Fast timers expired: %i\n",
  585. fast_timers_expired);
  586. used += sprintf(bigbuf + used, "Fast timers deleted: %i\n",
  587. fast_timers_deleted);
  588. used += sprintf(bigbuf + used, "Fast timer running: %s\n",
  589. fast_timer_running ? "yes" : "no");
  590. used += sprintf(bigbuf + used, "Current time: %lu.%06lu\n",
  591. (unsigned long)tv.tv_sec,
  592. (unsigned long)tv.tv_usec);
  593. #ifdef FAST_TIMER_SANITY_CHECKS
  594. used += sprintf(bigbuf + used, "Sanity failed: %i\n",
  595. sanity_failed);
  596. #endif
  597. used += sprintf(bigbuf + used, "\n");
  598. #ifdef DEBUG_LOG_INCLUDED
  599. {
  600. int end_i = debug_log_cnt;
  601. i = 0;
  602. if (debug_log_cnt_wrapped)
  603. {
  604. i = debug_log_cnt;
  605. }
  606. while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
  607. used+100 < BIG_BUF_SIZE)
  608. {
  609. used += sprintf(bigbuf + used, debug_log_string[i],
  610. debug_log_value[i]);
  611. i = (i+1) % DEBUG_LOG_MAX;
  612. }
  613. }
  614. used += sprintf(bigbuf + used, "\n");
  615. #endif
  616. num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
  617. NUM_TIMER_STATS);
  618. used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started);
  619. for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++)
  620. {
  621. int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
  622. #if 1 //ndef FAST_TIMER_LOG
  623. used += sprintf(bigbuf + used, "div: %i delay: %i"
  624. "\n",
  625. timer_div_settings[cur],
  626. timer_delay_settings[cur]
  627. );
  628. #endif
  629. #ifdef FAST_TIMER_LOG
  630. t = &timer_started_log[cur];
  631. used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  632. "d: %6li us data: 0x%08lX"
  633. "\n",
  634. t->name,
  635. (unsigned long)t->tv_set.tv_sec,
  636. (unsigned long)t->tv_set.tv_usec,
  637. (unsigned long)t->tv_expires.tv_sec,
  638. (unsigned long)t->tv_expires.tv_usec,
  639. t->delay_us,
  640. t->data
  641. );
  642. #endif
  643. }
  644. used += sprintf(bigbuf + used, "\n");
  645. #ifdef FAST_TIMER_LOG
  646. num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
  647. NUM_TIMER_STATS);
  648. used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added);
  649. for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
  650. {
  651. t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
  652. used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  653. "d: %6li us data: 0x%08lX"
  654. "\n",
  655. t->name,
  656. (unsigned long)t->tv_set.tv_sec,
  657. (unsigned long)t->tv_set.tv_usec,
  658. (unsigned long)t->tv_expires.tv_sec,
  659. (unsigned long)t->tv_expires.tv_usec,
  660. t->delay_us,
  661. t->data
  662. );
  663. }
  664. used += sprintf(bigbuf + used, "\n");
  665. num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
  666. NUM_TIMER_STATS);
  667. used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired);
  668. for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
  669. {
  670. t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
  671. used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  672. "d: %6li us data: 0x%08lX"
  673. "\n",
  674. t->name,
  675. (unsigned long)t->tv_set.tv_sec,
  676. (unsigned long)t->tv_set.tv_usec,
  677. (unsigned long)t->tv_expires.tv_sec,
  678. (unsigned long)t->tv_expires.tv_usec,
  679. t->delay_us,
  680. t->data
  681. );
  682. }
  683. used += sprintf(bigbuf + used, "\n");
  684. #endif
  685. used += sprintf(bigbuf + used, "Active timers:\n");
  686. local_irq_save(flags);
  687. local_irq_save(flags);
  688. t = fast_timer_list;
  689. while (t != NULL && (used+100 < BIG_BUF_SIZE))
  690. {
  691. nextt = t->next;
  692. local_irq_restore(flags);
  693. used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
  694. "d: %6li us data: 0x%08lX"
  695. /* " func: 0x%08lX" */
  696. "\n",
  697. t->name,
  698. (unsigned long)t->tv_set.tv_sec,
  699. (unsigned long)t->tv_set.tv_usec,
  700. (unsigned long)t->tv_expires.tv_sec,
  701. (unsigned long)t->tv_expires.tv_usec,
  702. t->delay_us,
  703. t->data
  704. /* , t->function */
  705. );
  706. local_irq_disable();
  707. if (t->next != nextt)
  708. {
  709. printk("timer removed!\n");
  710. }
  711. t = nextt;
  712. }
  713. local_irq_restore(flags);
  714. }
  715. if (used - offset < len)
  716. {
  717. len = used - offset;
  718. }
  719. memcpy(buf, bigbuf + offset, len);
  720. *start = buf;
  721. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
  722. *eof = 1;
  723. #endif
  724. return len;
  725. }
  726. #endif /* PROC_FS */
  727. #ifdef FAST_TIMER_TEST
  728. static volatile unsigned long i = 0;
  729. static volatile int num_test_timeout = 0;
  730. static struct fast_timer tr[10];
  731. static int exp_num[10];
  732. static struct timeval tv_exp[100];
  733. static void test_timeout(unsigned long data)
  734. {
  735. do_gettimeofday_fast(&tv_exp[data]);
  736. exp_num[data] = num_test_timeout;
  737. num_test_timeout++;
  738. }
  739. static void test_timeout1(unsigned long data)
  740. {
  741. do_gettimeofday_fast(&tv_exp[data]);
  742. exp_num[data] = num_test_timeout;
  743. if (data < 7)
  744. {
  745. start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
  746. i++;
  747. }
  748. num_test_timeout++;
  749. }
  750. DP(
  751. static char buf0[2000];
  752. static char buf1[2000];
  753. static char buf2[2000];
  754. static char buf3[2000];
  755. static char buf4[2000];
  756. );
  757. static char buf5[6000];
  758. static int j_u[1000];
  759. static void fast_timer_test(void)
  760. {
  761. int prev_num;
  762. int j;
  763. struct timeval tv, tv0, tv1, tv2;
  764. printk("fast_timer_test() start\n");
  765. do_gettimeofday_fast(&tv);
  766. for (j = 0; j < 1000; j++)
  767. {
  768. j_u[j] = GET_JIFFIES_USEC();
  769. }
  770. for (j = 0; j < 100; j++)
  771. {
  772. do_gettimeofday_fast(&tv_exp[j]);
  773. }
  774. printk("fast_timer_test() %is %06i\n", tv.tv_sec, tv.tv_usec);
  775. for (j = 0; j < 1000; j++)
  776. {
  777. 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]);
  778. j += 4;
  779. }
  780. for (j = 0; j < 100; j++)
  781. {
  782. printk("%i.%i %i.%i %i.%i %i.%i %i.%i\n",
  783. tv_exp[j].tv_sec,tv_exp[j].tv_usec,
  784. tv_exp[j+1].tv_sec,tv_exp[j+1].tv_usec,
  785. tv_exp[j+2].tv_sec,tv_exp[j+2].tv_usec,
  786. tv_exp[j+3].tv_sec,tv_exp[j+3].tv_usec,
  787. tv_exp[j+4].tv_sec,tv_exp[j+4].tv_usec);
  788. j += 4;
  789. }
  790. do_gettimeofday_fast(&tv0);
  791. start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
  792. DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
  793. i++;
  794. start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
  795. DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
  796. i++;
  797. start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
  798. DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
  799. i++;
  800. start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
  801. DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
  802. i++;
  803. start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
  804. DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
  805. i++;
  806. do_gettimeofday_fast(&tv1);
  807. proc_fasttimer_read(buf5, NULL, 0, 0, 0);
  808. prev_num = num_test_timeout;
  809. while (num_test_timeout < i)
  810. {
  811. if (num_test_timeout != prev_num)
  812. {
  813. prev_num = num_test_timeout;
  814. }
  815. }
  816. do_gettimeofday_fast(&tv2);
  817. printk("Timers started %is %06i\n", tv0.tv_sec, tv0.tv_usec);
  818. printk("Timers started at %is %06i\n", tv1.tv_sec, tv1.tv_usec);
  819. printk("Timers done %is %06i\n", tv2.tv_sec, tv2.tv_usec);
  820. DP(printk("buf0:\n");
  821. printk(buf0);
  822. printk("buf1:\n");
  823. printk(buf1);
  824. printk("buf2:\n");
  825. printk(buf2);
  826. printk("buf3:\n");
  827. printk(buf3);
  828. printk("buf4:\n");
  829. printk(buf4);
  830. );
  831. printk("buf5:\n");
  832. printk(buf5);
  833. printk("timers set:\n");
  834. for(j = 0; j<i; j++)
  835. {
  836. struct fast_timer *t = &tr[j];
  837. printk("%-10s set: %6is %06ius exp: %6is %06ius "
  838. "data: 0x%08X func: 0x%08X\n",
  839. t->name,
  840. t->tv_set.tv_sec,
  841. t->tv_set.tv_usec,
  842. t->tv_expires.tv_sec,
  843. t->tv_expires.tv_usec,
  844. t->data,
  845. t->function
  846. );
  847. printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
  848. t->delay_us,
  849. tv_exp[j].tv_sec,
  850. tv_exp[j].tv_usec,
  851. exp_num[j],
  852. (tv_exp[j].tv_sec - t->tv_expires.tv_sec)*1000000 + tv_exp[j].tv_usec - t->tv_expires.tv_usec);
  853. }
  854. proc_fasttimer_read(buf5, NULL, 0, 0, 0);
  855. printk("buf5 after all done:\n");
  856. printk(buf5);
  857. printk("fast_timer_test() done\n");
  858. }
  859. #endif
  860. void fast_timer_init(void)
  861. {
  862. /* For some reason, request_irq() hangs when called froom time_init() */
  863. if (!fast_timer_is_init)
  864. {
  865. printk("fast_timer_init()\n");
  866. #ifdef CONFIG_PROC_FS
  867. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
  868. if ((fasttimer_proc_entry = create_proc_entry( "fasttimer", 0, 0 )))
  869. fasttimer_proc_entry->read_proc = proc_fasttimer_read;
  870. #else
  871. proc_register_dynamic(&proc_root, &fasttimer_proc_entry);
  872. #endif
  873. #endif /* PROC_FS */
  874. if(request_irq(TIMER_INTR_VECT, timer_trig_interrupt, SA_INTERRUPT,
  875. "fast timer int", NULL))
  876. {
  877. printk("err: timer1 irq\n");
  878. }
  879. fast_timer_is_init = 1;
  880. #ifdef FAST_TIMER_TEST
  881. printk("do test\n");
  882. fast_timer_test();
  883. #endif
  884. }
  885. }