fasttimer.c 26 KB

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