ftape-calibr.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *      Copyright (C) 1993-1996 Bas Laarhoven.
  3.  This program is free software; you can redistribute it and/or modify
  4.  it under the terms of the GNU General Public License as published by
  5.  the Free Software Foundation; either version 2, or (at your option)
  6.  any later version.
  7.  This program is distributed in the hope that it will be useful,
  8.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  GNU General Public License for more details.
  11.  You should have received a copy of the GNU General Public License
  12.  along with this program; see the file COPYING.  If not, write to
  13.  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  14.  *
  15.  * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/ftape-calibr.c,v $
  16.  * $Revision: 1.2 $
  17.  * $Date: 1997/10/05 19:18:08 $
  18.  *
  19.  *      GP calibration routine for processor speed dependent
  20.  *      functions.
  21.  */
  22. #include <linux/config.h>
  23. #include <linux/errno.h>
  24. #include <linux/sched.h>
  25. #include <asm/system.h>
  26. #include <asm/io.h>
  27. #if defined(__alpha__)
  28. # include <asm/hwrpb.h>
  29. #elif defined(__x86_64__)
  30. # include <asm/msr.h>
  31. # include <asm/timex.h>
  32. #elif defined(__i386__) 
  33. # include <linux/timex.h>
  34. #endif
  35. #include <linux/ftape.h>
  36. #include "../lowlevel/ftape-tracing.h"
  37. #include "../lowlevel/ftape-calibr.h"
  38. #include "../lowlevel/fdc-io.h"
  39. #undef DEBUG
  40. #if !defined(__alpha__) && !defined(__i386__) && !defined(__x86_64__)
  41. # error Ftape is not implemented for this architecture!
  42. #endif
  43. #if defined(__alpha__) || defined(__x86_64__)
  44. static unsigned long ps_per_cycle = 0;
  45. #endif
  46. #if defined(__i386__)
  47. extern spinlock_t i8253_lock;
  48. #endif
  49. /*
  50.  * Note: On Intel PCs, the clock ticks at 100 Hz (HZ==100) which is
  51.  * too slow for certain timeouts (and that clock doesn't even tick
  52.  * when interrupts are disabled).  For that reason, the 8254 timer is
  53.  * used directly to implement fine-grained timeouts.  However, on
  54.  * Alpha PCs, the 8254 is *not* used to implement the clock tick
  55.  * (which is 1024 Hz, normally) and the 8254 timer runs at some
  56.  * "random" frequency (it seems to run at 18Hz, but its not safe to
  57.  * rely on this value).  Instead, we use the Alpha's "rpcc"
  58.  * instruction to read cycle counts.  As this is a 32 bit counter,
  59.  * it will overflow only once per 30 seconds (on a 200MHz machine),
  60.  * which is plenty.
  61.  */
  62. unsigned int ftape_timestamp(void)
  63. {
  64. #if defined(__alpha__)
  65. unsigned long r;
  66. asm volatile ("rpcc %0" : "=r" (r));
  67. return r;
  68. #elif defined(__x86_64__)
  69. unsigned long r;
  70. rdtscl(r);
  71. return r;
  72. #elif defined(__i386__)
  73. /*
  74.  * Note that there is some time between counter underflowing and jiffies
  75.  * increasing, so the code below won't always give correct output.
  76.  * -Vojtech
  77.  */
  78. unsigned long flags;
  79. __u16 lo;
  80. __u16 hi;
  81. spin_lock_irqsave(&i8253_lock, flags); 
  82. outb_p(0x00, 0x43); /* latch the count ASAP */
  83. lo = inb_p(0x40); /* read the latched count */
  84. lo |= inb(0x40) << 8;
  85. hi = jiffies;
  86. spin_unlock_irqrestore(&i8253_lock, flags); 
  87. return ((hi + 1) * (unsigned int) LATCH) - lo;  /* downcounter ! */
  88. #endif
  89. }
  90. static unsigned int short_ftape_timestamp(void)
  91. {
  92. #if defined(__alpha__) || defined(__x86_64__)
  93. return ftape_timestamp();
  94. #elif defined(__i386__)
  95. unsigned int count;
  96.   unsigned long flags;
  97.  
  98. spin_lock_irqsave(&i8253_lock, flags); 
  99.   outb_p(0x00, 0x43); /* latch the count ASAP */
  100. count = inb_p(0x40); /* read the latched count */
  101. count |= inb(0x40) << 8;
  102. spin_unlock_irqrestore(&i8253_lock, flags); 
  103. return (LATCH - count); /* normal: downcounter */
  104. #endif
  105. }
  106. static unsigned int diff(unsigned int t0, unsigned int t1)
  107. {
  108. #if defined(__alpha__) || defined(__x86_64__)
  109. return (t1 - t0);
  110. #elif defined(__i386__)
  111. /*
  112.  * This is tricky: to work for both short and full ftape_timestamps
  113.  * we'll have to discriminate between these.
  114.  * If it _looks_ like short stamps with wrapping around we'll
  115.  * asume it are. This will generate a small error if it really
  116.  * was a (very large) delta from full ftape_timestamps.
  117.  */
  118. return (t1 <= t0 && t0 <= LATCH) ? t1 + LATCH - t0 : t1 - t0;
  119. #endif
  120. }
  121. static unsigned int usecs(unsigned int count)
  122. {
  123. #if defined(__alpha__) || defined(__x86_64__)
  124. return (ps_per_cycle * count) / 1000000UL;
  125. #elif defined(__i386__)
  126. return (10000 * count) / ((CLOCK_TICK_RATE + 50) / 100);
  127. #endif
  128. }
  129. unsigned int ftape_timediff(unsigned int t0, unsigned int t1)
  130. {
  131. /*
  132.  *  Calculate difference in usec for ftape_timestamp results t0 & t1.
  133.  *  Note that on the i386 platform with short time-stamps, the
  134.  *  maximum allowed timespan is 1/HZ or we'll lose ticks!
  135.  */
  136. return usecs(diff(t0, t1));
  137. }
  138. /*      To get an indication of the I/O performance,
  139.  *      measure the duration of the inb() function.
  140.  */
  141. static void time_inb(void)
  142. {
  143. int i;
  144. int t0, t1;
  145. unsigned long flags;
  146. int status;
  147. TRACE_FUN(ft_t_any);
  148. save_flags(flags);
  149. cli();
  150. t0 = short_ftape_timestamp();
  151. for (i = 0; i < 1000; ++i)
  152. status = inb(fdc.msr);
  153. t1 = short_ftape_timestamp();
  154. restore_flags(flags);
  155. TRACE(ft_t_info, "inb() duration: %d nsec", ftape_timediff(t0, t1));
  156. TRACE_EXIT;
  157. }
  158. static void init_clock(void)
  159. {
  160. TRACE_FUN(ft_t_any);
  161. #if defined(__x86_64__)
  162. ps_per_cycle = 1000000000UL / cpu_khz;
  163. #elif defined(__alpha__)
  164. extern struct hwrpb_struct *hwrpb;
  165. ps_per_cycle = (1000*1000*1000*1000UL) / hwrpb->cycle_freq;
  166. #endif
  167. TRACE_EXIT;
  168. }
  169. /*
  170.  *      Input:  function taking int count as parameter.
  171.  *              pointers to calculated calibration variables.
  172.  */
  173. void ftape_calibrate(char *name,
  174.     void (*fun) (unsigned int), 
  175.     unsigned int *calibr_count, 
  176.     unsigned int *calibr_time)
  177. {
  178. static int first_time = 1;
  179. int i;
  180. unsigned int tc = 0;
  181. unsigned int count;
  182. unsigned int time;
  183. #if defined(__i386__)
  184. unsigned int old_tc = 0;
  185. unsigned int old_count = 1;
  186. unsigned int old_time = 1;
  187. #endif
  188. TRACE_FUN(ft_t_flow);
  189. if (first_time) {             /* get idea of I/O performance */
  190. init_clock();
  191. time_inb();
  192. first_time = 0;
  193. }
  194. /*    value of timeout must be set so that on very slow systems
  195.  *    it will give a time less than one jiffy, and on
  196.  *    very fast systems it'll give reasonable precision.
  197.  */
  198. count = 40;
  199. for (i = 0; i < 15; ++i) {
  200. unsigned int t0;
  201. unsigned int t1;
  202. unsigned int once;
  203. unsigned int multiple;
  204. unsigned long flags;
  205. *calibr_count =
  206. *calibr_time = count; /* set TC to 1 */
  207. save_flags(flags);
  208. cli();
  209. fun(0); /* dummy, get code into cache */
  210. t0 = short_ftape_timestamp();
  211. fun(0); /* overhead + one test */
  212. t1 = short_ftape_timestamp();
  213. once = diff(t0, t1);
  214. t0 = short_ftape_timestamp();
  215. fun(count); /* overhead + count tests */
  216. t1 = short_ftape_timestamp();
  217. multiple = diff(t0, t1);
  218. restore_flags(flags);
  219. time = ftape_timediff(0, multiple - once);
  220. tc = (1000 * time) / (count - 1);
  221. TRACE(ft_t_any, "once:%3d us,%6d times:%6d us, TC:%5d ns",
  222. usecs(once), count - 1, usecs(multiple), tc);
  223. #if defined(__alpha__) || defined(__x86_64__)
  224. /*
  225.  * Increase the calibration count exponentially until the
  226.  * calibration time exceeds 100 ms.
  227.  */
  228. if (time >= 100*1000)
  229. break;
  230. #elif defined(__i386__)
  231. /*
  232.  * increase the count until the resulting time nears 2/HZ,
  233.  * then the tc will drop sharply because we lose LATCH counts.
  234.  */
  235. if (tc <= old_tc / 2) {
  236. time = old_time;
  237. count = old_count;
  238. break;
  239. }
  240. old_tc = tc;
  241. old_count = count;
  242. old_time = time;
  243. #endif
  244. count *= 2;
  245. }
  246. *calibr_count = count - 1;
  247. *calibr_time  = time;
  248. TRACE(ft_t_info, "TC for `%s()' = %d nsec (at %d counts)",
  249.      name, (1000 * *calibr_time) / *calibr_count, *calibr_count);
  250. TRACE_EXIT;
  251. }