clock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "ureg.h"
  8. /*
  9. * 8253 timer
  10. */
  11. enum
  12. {
  13. T0cntr= 0x40, /* counter ports */
  14. T1cntr= 0x41, /* ... */
  15. T2cntr= 0x42, /* ... */
  16. Tmode= 0x43, /* mode port */
  17. /* commands */
  18. Latch0= 0x00, /* latch counter 0's value */
  19. Load0= 0x30, /* load counter 0 with 2 bytes */
  20. /* modes */
  21. Square= 0x36, /* perioic square wave */
  22. Freq= 1193182, /* Real clock frequency */
  23. };
  24. static uvlong cpuhz = 66000000;
  25. static int cpumhz = 66;
  26. static int loopconst = 100;
  27. int cpuidax, cpuiddx;
  28. int havetsc;
  29. extern void _cycles(uvlong*); /* in l.s */
  30. extern void wrmsr(int, vlong);
  31. static void
  32. clockintr(Ureg*, void*)
  33. {
  34. m->ticks++;
  35. checkalarms();
  36. }
  37. #define STEPPING(x) ((x)&0xf)
  38. #define X86MODEL(x) (((x)>>4)&0xf)
  39. #define X86FAMILY(x) (((x)>>8)&0xf)
  40. enum
  41. {
  42. /* flags */
  43. CpuidFPU = 0x001, /* on-chip floating point unit */
  44. CpuidMCE = 0x080, /* machine check exception */
  45. CpuidCX8 = 0x100, /* CMPXCHG8B instruction */
  46. };
  47. typedef struct
  48. {
  49. int family;
  50. int model;
  51. int aalcycles;
  52. char *name;
  53. } X86type;
  54. X86type x86intel[] =
  55. {
  56. { 4, 0, 22, "486DX", }, /* known chips */
  57. { 4, 1, 22, "486DX50", },
  58. { 4, 2, 22, "486SX", },
  59. { 4, 3, 22, "486DX2", },
  60. { 4, 4, 22, "486SL", },
  61. { 4, 5, 22, "486SX2", },
  62. { 4, 7, 22, "DX2WB", }, /* P24D */
  63. { 4, 8, 22, "DX4", }, /* P24C */
  64. { 4, 9, 22, "DX4WB", }, /* P24CT */
  65. { 5, 0, 23, "P5", },
  66. { 5, 1, 23, "P5", },
  67. { 5, 2, 23, "P54C", },
  68. { 5, 3, 23, "P24T", },
  69. { 5, 4, 23, "P55C MMX", },
  70. { 5, 7, 23, "P54C VRT", },
  71. { 6, 1, 16, "PentiumPro", },/* trial and error */
  72. { 6, 3, 16, "PentiumII", },
  73. { 6, 5, 16, "PentiumII/Xeon", },
  74. { 6, 6, 16, "Celeron", },
  75. { 6, 7, 16, "PentiumIII/Xeon", },
  76. { 6, 8, 16, "PentiumIII/Xeon", },
  77. { 6, 0xB, 16, "PentiumIII/Xeon", },
  78. { 0xF, 1, 16, "P4", }, /* P4 */
  79. { 0xF, 2, 16, "PentiumIV/Xeon", },
  80. { 3, -1, 32, "386", }, /* family defaults */
  81. { 4, -1, 22, "486", },
  82. { 5, -1, 23, "P5", },
  83. { 6, -1, 16, "P6", },
  84. { 0xF, -1, 16, "P4", }, /* P4 */
  85. { -1, -1, 16, "unknown", }, /* total default */
  86. };
  87. /*
  88. * The AMD processors all implement the CPUID instruction.
  89. * The later ones also return the processor name via functions
  90. * 0x80000002, 0x80000003 and 0x80000004 in registers AX, BX, CX
  91. * and DX:
  92. * K5 "AMD-K5(tm) Processor"
  93. * K6 "AMD-K6tm w/ multimedia extensions"
  94. * K6 3D "AMD-K6(tm) 3D processor"
  95. * K6 3D+ ?
  96. */
  97. static X86type x86amd[] =
  98. {
  99. { 5, 0, 23, "AMD-K5", }, /* guesswork */
  100. { 5, 1, 23, "AMD-K5", }, /* guesswork */
  101. { 5, 2, 23, "AMD-K5", }, /* guesswork */
  102. { 5, 3, 23, "AMD-K5", }, /* guesswork */
  103. { 5, 4, 23, "AMD Geode GX1", }, /* guesswork */
  104. { 5, 5, 23, "AMD Geode GX2", }, /* guesswork */
  105. { 5, 6, 11, "AMD-K6", }, /* trial and error */
  106. { 5, 7, 11, "AMD-K6", }, /* trial and error */
  107. { 5, 8, 11, "AMD-K6-2", }, /* trial and error */
  108. { 5, 9, 11, "AMD-K6-III", },/* trial and error */
  109. { 5, 0xa, 23, "AMD Geode LX", }, /* guesswork */
  110. { 6, 1, 11, "AMD-Athlon", },/* trial and error */
  111. { 6, 2, 11, "AMD-Athlon", },/* trial and error */
  112. { 4, -1, 22, "Am486", }, /* guesswork */
  113. { 5, -1, 23, "AMD-K5/K6", }, /* guesswork */
  114. { 6, -1, 11, "AMD-Athlon", },/* guesswork */
  115. { 0xF, -1, 11, "AMD64", }, /* guesswork */
  116. { -1, -1, 11, "unknown", }, /* total default */
  117. };
  118. static X86type *cputype;
  119. void
  120. delay(int millisecs)
  121. {
  122. millisecs *= loopconst;
  123. if(millisecs <= 0)
  124. millisecs = 1;
  125. aamloop(millisecs);
  126. }
  127. void
  128. microdelay(int microsecs)
  129. {
  130. microsecs *= loopconst;
  131. microsecs /= 1000;
  132. if(microsecs <= 0)
  133. microsecs = 1;
  134. aamloop(microsecs);
  135. }
  136. extern void cpuid(char*, int*, int*);
  137. X86type*
  138. cpuidentify(void)
  139. {
  140. int family, model;
  141. X86type *t;
  142. char cpuidid[16];
  143. int cpuidax, cpuiddx;
  144. cpuid(cpuidid, &cpuidax, &cpuiddx);
  145. if(strncmp(cpuidid, "AuthenticAMD", 12) == 0 ||
  146. strncmp(cpuidid, "Geode by NSC", 12) == 0)
  147. t = x86amd;
  148. else
  149. t = x86intel;
  150. family = X86FAMILY(cpuidax);
  151. model = X86MODEL(cpuidax);
  152. if (0)
  153. print("cpuidentify: cpuidax 0x%ux cpuiddx 0x%ux\n",
  154. cpuidax, cpuiddx);
  155. while(t->name){
  156. if((t->family == family && t->model == model)
  157. || (t->family == family && t->model == -1)
  158. || (t->family == -1))
  159. break;
  160. t++;
  161. }
  162. if(t->name == nil)
  163. panic("cpuidentify");
  164. if(cpuiddx & 0x10){
  165. havetsc = 1;
  166. if(cpuiddx & 0x20)
  167. wrmsr(0x10, 0);
  168. }
  169. return t;
  170. }
  171. void
  172. clockinit(void)
  173. {
  174. uvlong a, b, cpufreq;
  175. int loops, incr, x, y;
  176. X86type *t;
  177. /*
  178. * set vector for clock interrupts
  179. */
  180. setvec(VectorCLOCK, clockintr, 0);
  181. t = cpuidentify();
  182. /*
  183. * set clock for 1/HZ seconds
  184. */
  185. outb(Tmode, Load0|Square);
  186. outb(T0cntr, (Freq/HZ)); /* low byte */
  187. outb(T0cntr, (Freq/HZ)>>8); /* high byte */
  188. /*
  189. * Introduce a little delay to make sure the count is
  190. * latched and the timer is counting down; with a fast
  191. * enough processor this may not be the case.
  192. * The i8254 (which this probably is) has a read-back
  193. * command which can be used to make sure the counting
  194. * register has been written into the counting element.
  195. */
  196. x = (Freq/HZ);
  197. for(loops = 0; loops < 100000 && x >= (Freq/HZ); loops++){
  198. outb(Tmode, Latch0);
  199. x = inb(T0cntr);
  200. x |= inb(T0cntr)<<8;
  201. }
  202. /* find biggest loop that doesn't wrap */
  203. incr = 16000000/(t->aalcycles*HZ*2);
  204. x = 2000;
  205. for(loops = incr; loops < 64*1024; loops += incr) {
  206. /*
  207. * measure time for the loop
  208. *
  209. * MOVL loops,CX
  210. * aaml1: AAM
  211. * LOOP aaml1
  212. *
  213. * the time for the loop should be independent of external
  214. * cache and memory system since it fits in the execution
  215. * prefetch buffer.
  216. *
  217. */
  218. outb(Tmode, Latch0);
  219. if(havetsc)
  220. _cycles(&a);
  221. x = inb(T0cntr);
  222. x |= inb(T0cntr)<<8;
  223. aamloop(loops);
  224. outb(Tmode, Latch0);
  225. if(havetsc)
  226. _cycles(&b);
  227. y = inb(T0cntr);
  228. y |= inb(T0cntr)<<8;
  229. x -= y;
  230. if(x < 0)
  231. x += Freq/HZ;
  232. if(x > Freq/(3*HZ))
  233. break;
  234. }
  235. /*
  236. * figure out clock frequency and a loop multiplier for delay().
  237. * counter goes at twice the frequency, once per transition,
  238. * i.e., twice per square wave
  239. */
  240. cpufreq = (vlong)loops*((t->aalcycles*2*Freq)/x);
  241. loopconst = (cpufreq/1000)/t->aalcycles; /* AAM+LOOP's for 1 ms */
  242. if(havetsc){
  243. /* counter goes up by 2*Freq */
  244. b = (b-a)<<1;
  245. b *= Freq;
  246. b /= x;
  247. /*
  248. * round to the nearest megahz
  249. */
  250. cpumhz = (b+500000)/1000000L;
  251. cpuhz = b;
  252. }
  253. else{
  254. /*
  255. * add in possible .5% error and convert to MHz
  256. */
  257. cpumhz = (cpufreq + cpufreq/200)/1000000;
  258. cpuhz = cpufreq;
  259. }
  260. if(debug){
  261. int timeo;
  262. print("%dMHz %s loop %d\n", cpumhz, t->name, loopconst);
  263. print("tick...");
  264. for(timeo = 0; timeo < 10; timeo++)
  265. delay(1000);
  266. print("tock...\n");
  267. }
  268. }