clock.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. { 0xF, 6, 16, "PentiumIV/Xeon", },
  81. { 3, -1, 32, "386", }, /* family defaults */
  82. { 4, -1, 22, "486", },
  83. { 5, -1, 23, "P5", },
  84. { 6, -1, 16, "P6", },
  85. { 0xF, -1, 16, "P4", }, /* P4 */
  86. { -1, -1, 16, "unknown", }, /* total default */
  87. };
  88. /*
  89. * The AMD processors all implement the CPUID instruction.
  90. * The later ones also return the processor name via functions
  91. * 0x80000002, 0x80000003 and 0x80000004 in registers AX, BX, CX
  92. * and DX:
  93. * K5 "AMD-K5(tm) Processor"
  94. * K6 "AMD-K6tm w/ multimedia extensions"
  95. * K6 3D "AMD-K6(tm) 3D processor"
  96. * K6 3D+ ?
  97. */
  98. static X86type x86amd[] =
  99. {
  100. { 5, 0, 23, "AMD-K5", }, /* guesswork */
  101. { 5, 1, 23, "AMD-K5", }, /* guesswork */
  102. { 5, 2, 23, "AMD-K5", }, /* guesswork */
  103. { 5, 3, 23, "AMD-K5", }, /* guesswork */
  104. { 5, 4, 23, "AMD Geode GX1", }, /* guesswork */
  105. { 5, 5, 23, "AMD Geode GX2", }, /* guesswork */
  106. { 5, 6, 11, "AMD-K6", }, /* trial and error */
  107. { 5, 7, 11, "AMD-K6", }, /* trial and error */
  108. { 5, 8, 11, "AMD-K6-2", }, /* trial and error */
  109. { 5, 9, 11, "AMD-K6-III", },/* trial and error */
  110. { 5, 0xa, 23, "AMD Geode LX", }, /* guesswork */
  111. { 6, 1, 11, "AMD-Athlon", },/* trial and error */
  112. { 6, 2, 11, "AMD-Athlon", },/* trial and error */
  113. { 4, -1, 22, "Am486", }, /* guesswork */
  114. { 5, -1, 23, "AMD-K5/K6", }, /* guesswork */
  115. { 6, -1, 11, "AMD-Athlon", },/* guesswork */
  116. { 0xF, -1, 11, "AMD64", }, /* guesswork */
  117. { -1, -1, 11, "unknown", }, /* total default */
  118. };
  119. static X86type *cputype;
  120. void
  121. delay(int millisecs)
  122. {
  123. millisecs *= loopconst;
  124. if(millisecs <= 0)
  125. millisecs = 1;
  126. aamloop(millisecs);
  127. }
  128. void
  129. microdelay(int microsecs)
  130. {
  131. microsecs *= loopconst;
  132. microsecs /= 1000;
  133. if(microsecs <= 0)
  134. microsecs = 1;
  135. aamloop(microsecs);
  136. }
  137. extern void cpuid(char*, int*, int*);
  138. X86type*
  139. cpuidentify(void)
  140. {
  141. int family, model;
  142. X86type *t;
  143. char cpuidid[16];
  144. int cpuidax, cpuiddx;
  145. cpuid(cpuidid, &cpuidax, &cpuiddx);
  146. if(strncmp(cpuidid, "AuthenticAMD", 12) == 0 ||
  147. strncmp(cpuidid, "Geode by NSC", 12) == 0)
  148. t = x86amd;
  149. else
  150. t = x86intel;
  151. family = X86FAMILY(cpuidax);
  152. model = X86MODEL(cpuidax);
  153. if (0)
  154. print("cpuidentify: cpuidax 0x%ux cpuiddx 0x%ux\n",
  155. cpuidax, cpuiddx);
  156. while(t->name){
  157. if((t->family == family && t->model == model)
  158. || (t->family == family && t->model == -1)
  159. || (t->family == -1))
  160. break;
  161. t++;
  162. }
  163. if(t->name == nil)
  164. panic("cpuidentify");
  165. if(cpuiddx & 0x10){
  166. havetsc = 1;
  167. if(cpuiddx & 0x20)
  168. wrmsr(0x10, 0);
  169. }
  170. return t;
  171. }
  172. void
  173. prcpuid(void)
  174. {
  175. if (cputype == nil)
  176. panic("prcpuid: clockinit not called");
  177. print("cpu0: %dMHz %s loop %d\n", cpumhz, cputype->name, loopconst);
  178. }
  179. void
  180. clockinit(void)
  181. {
  182. uvlong a, b, cpufreq;
  183. int loops, incr, x, y;
  184. X86type *t;
  185. /*
  186. * set vector for clock interrupts
  187. */
  188. setvec(VectorCLOCK, clockintr, 0);
  189. cputype = t = cpuidentify();
  190. /*
  191. * set clock for 1/HZ seconds
  192. */
  193. outb(Tmode, Load0|Square);
  194. outb(T0cntr, (Freq/HZ)); /* low byte */
  195. outb(T0cntr, (Freq/HZ)>>8); /* high byte */
  196. /*
  197. * Introduce a little delay to make sure the count is
  198. * latched and the timer is counting down; with a fast
  199. * enough processor this may not be the case.
  200. * The i8254 (which this probably is) has a read-back
  201. * command which can be used to make sure the counting
  202. * register has been written into the counting element.
  203. */
  204. x = (Freq/HZ);
  205. for(loops = 0; loops < 100000 && x >= (Freq/HZ); loops++){
  206. outb(Tmode, Latch0);
  207. x = inb(T0cntr);
  208. x |= inb(T0cntr)<<8;
  209. }
  210. /* find biggest loop that doesn't wrap */
  211. incr = 16000000/(t->aalcycles*HZ*2);
  212. x = 2000;
  213. for(loops = incr; loops < 64*1024; loops += incr) {
  214. /*
  215. * measure time for the loop
  216. *
  217. * MOVL loops,CX
  218. * aaml1: AAM
  219. * LOOP aaml1
  220. *
  221. * the time for the loop should be independent of external
  222. * cache and memory system since it fits in the execution
  223. * prefetch buffer.
  224. *
  225. */
  226. outb(Tmode, Latch0);
  227. if(havetsc)
  228. _cycles(&a);
  229. x = inb(T0cntr);
  230. x |= inb(T0cntr)<<8;
  231. aamloop(loops);
  232. outb(Tmode, Latch0);
  233. if(havetsc)
  234. _cycles(&b);
  235. y = inb(T0cntr);
  236. y |= inb(T0cntr)<<8;
  237. x -= y;
  238. if(x < 0)
  239. x += Freq/HZ;
  240. if(x > Freq/(3*HZ))
  241. break;
  242. }
  243. /*
  244. * figure out clock frequency and a loop multiplier for delay().
  245. * counter goes at twice the frequency, once per transition,
  246. * i.e., twice per square wave
  247. */
  248. cpufreq = (vlong)loops*((t->aalcycles*2*Freq)/x);
  249. loopconst = (cpufreq/1000)/t->aalcycles; /* AAM+LOOP's for 1 ms */
  250. if(havetsc){
  251. /* counter goes up by 2*Freq */
  252. b = (b-a)<<1;
  253. b *= Freq;
  254. b /= x;
  255. /*
  256. * round to the nearest megahz
  257. */
  258. cpumhz = (b+500000)/1000000L;
  259. cpuhz = b;
  260. }
  261. else{
  262. /*
  263. * add in possible .5% error and convert to MHz
  264. */
  265. cpumhz = (cpufreq + cpufreq/200)/1000000;
  266. cpuhz = cpufreq;
  267. }
  268. if(debug){
  269. int timeo;
  270. print("%dMHz %s loop %d\n", cpumhz, t->name, loopconst);
  271. print("tick...");
  272. for(timeo = 0; timeo < 10; timeo++)
  273. delay(1000);
  274. print("tock...\n");
  275. }
  276. }