devarch.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "ureg.h"
  8. #include "../port/error.h"
  9. typedef struct IOMap IOMap;
  10. struct IOMap
  11. {
  12. IOMap *next;
  13. int reserved;
  14. char tag[13];
  15. ulong start;
  16. ulong end;
  17. };
  18. static struct
  19. {
  20. Lock;
  21. IOMap *m;
  22. IOMap *free;
  23. IOMap maps[32]; // some initial free maps
  24. QLock ql; // lock for reading map
  25. } iomap;
  26. enum {
  27. Qdir = 0,
  28. Qioalloc = 1,
  29. Qiob,
  30. Qiow,
  31. Qiol,
  32. Qbase,
  33. Qmax = 16,
  34. };
  35. typedef long Rdwrfn(Chan*, void*, long, vlong);
  36. static Rdwrfn *readfn[Qmax];
  37. static Rdwrfn *writefn[Qmax];
  38. static Dirtab archdir[Qmax] = {
  39. ".", { Qdir, 0, QTDIR }, 0, 0555,
  40. "ioalloc", { Qioalloc, 0 }, 0, 0444,
  41. "iob", { Qiob, 0 }, 0, 0660,
  42. "iow", { Qiow, 0 }, 0, 0660,
  43. "iol", { Qiol, 0 }, 0, 0660,
  44. };
  45. Lock archwlock; /* the lock is only for changing archdir */
  46. int narchdir = Qbase;
  47. int (*_pcmspecial)(char*, ISAConf*);
  48. void (*_pcmspecialclose)(int);
  49. static int doi8253set = 1;
  50. /*
  51. * Add a file to the #P listing. Once added, you can't delete it.
  52. * You can't add a file with the same name as one already there,
  53. * and you get a pointer to the Dirtab entry so you can do things
  54. * like change the Qid version. Changing the Qid path is disallowed.
  55. */
  56. Dirtab*
  57. addarchfile(char *name, int perm, Rdwrfn *rdfn, Rdwrfn *wrfn)
  58. {
  59. int i;
  60. Dirtab d;
  61. Dirtab *dp;
  62. memset(&d, 0, sizeof d);
  63. strcpy(d.name, name);
  64. d.perm = perm;
  65. lock(&archwlock);
  66. if(narchdir >= Qmax){
  67. unlock(&archwlock);
  68. return nil;
  69. }
  70. for(i=0; i<narchdir; i++)
  71. if(strcmp(archdir[i].name, name) == 0){
  72. unlock(&archwlock);
  73. return nil;
  74. }
  75. d.qid.path = narchdir;
  76. archdir[narchdir] = d;
  77. readfn[narchdir] = rdfn;
  78. writefn[narchdir] = wrfn;
  79. dp = &archdir[narchdir++];
  80. unlock(&archwlock);
  81. return dp;
  82. }
  83. void
  84. ioinit(void)
  85. {
  86. char *excluded;
  87. int i;
  88. for(i = 0; i < nelem(iomap.maps)-1; i++)
  89. iomap.maps[i].next = &iomap.maps[i+1];
  90. iomap.maps[i].next = nil;
  91. iomap.free = iomap.maps;
  92. /*
  93. * This is necessary to make the IBM X20 boot.
  94. * Have not tracked down the reason.
  95. */
  96. ioalloc(0x0fff, 1, 0, "dummy"); // i82557 is at 0x1000, the dummy
  97. // entry is needed for swappable devs.
  98. if ((excluded = getconf("ioexclude")) != nil) {
  99. char *s;
  100. s = excluded;
  101. while (s && *s != '\0' && *s != '\n') {
  102. char *ends;
  103. int io_s, io_e;
  104. io_s = (int)strtol(s, &ends, 0);
  105. if (ends == nil || ends == s || *ends != '-') {
  106. print("ioinit: cannot parse option string\n");
  107. break;
  108. }
  109. s = ++ends;
  110. io_e = (int)strtol(s, &ends, 0);
  111. if (ends && *ends == ',')
  112. *ends++ = '\0';
  113. s = ends;
  114. ioalloc(io_s, io_e - io_s + 1, 0, "pre-allocated");
  115. }
  116. }
  117. }
  118. // Reserve a range to be ioalloced later.
  119. // This is in particular useful for exchangable cards, such
  120. // as pcmcia and cardbus cards.
  121. int
  122. ioreserve(int, int size, int align, char *tag)
  123. {
  124. IOMap *m, **l;
  125. int i, port;
  126. lock(&iomap);
  127. // find a free port above 0x400 and below 0x1000
  128. port = 0x400;
  129. for(l = &iomap.m; *l; l = &(*l)->next){
  130. m = *l;
  131. if (m->start < 0x400) continue;
  132. i = m->start - port;
  133. if(i > size)
  134. break;
  135. if(align > 0)
  136. port = ((port+align-1)/align)*align;
  137. else
  138. port = m->end;
  139. }
  140. if(*l == nil){
  141. unlock(&iomap);
  142. return -1;
  143. }
  144. m = iomap.free;
  145. if(m == nil){
  146. print("ioalloc: out of maps");
  147. unlock(&iomap);
  148. return port;
  149. }
  150. iomap.free = m->next;
  151. m->next = *l;
  152. m->start = port;
  153. m->end = port + size;
  154. m->reserved = 1;
  155. strncpy(m->tag, tag, sizeof(m->tag));
  156. m->tag[sizeof(m->tag)-1] = 0;
  157. *l = m;
  158. archdir[0].qid.vers++;
  159. unlock(&iomap);
  160. return m->start;
  161. }
  162. //
  163. // alloc some io port space and remember who it was
  164. // alloced to. if port < 0, find a free region.
  165. //
  166. int
  167. ioalloc(int port, int size, int align, char *tag)
  168. {
  169. IOMap *m, **l;
  170. int i;
  171. lock(&iomap);
  172. if(port < 0){
  173. // find a free port above 0x400 and below 0x1000
  174. port = 0x400;
  175. for(l = &iomap.m; *l; l = &(*l)->next){
  176. m = *l;
  177. if (m->start < 0x400) continue;
  178. i = m->start - port;
  179. if(i > size)
  180. break;
  181. if(align > 0)
  182. port = ((port+align-1)/align)*align;
  183. else
  184. port = m->end;
  185. }
  186. if(*l == nil){
  187. unlock(&iomap);
  188. return -1;
  189. }
  190. } else {
  191. // Only 64KB I/O space on the x86.
  192. if((port+size) > 0x10000){
  193. unlock(&iomap);
  194. return -1;
  195. }
  196. // see if the space clashes with previously allocated ports
  197. for(l = &iomap.m; *l; l = &(*l)->next){
  198. m = *l;
  199. if(m->end <= port)
  200. continue;
  201. if(m->reserved && m->start == port && m->end == port + size) {
  202. m->reserved = 0;
  203. unlock(&iomap);
  204. return m->start;
  205. }
  206. if(m->start >= port+size)
  207. break;
  208. unlock(&iomap);
  209. return -1;
  210. }
  211. }
  212. m = iomap.free;
  213. if(m == nil){
  214. print("ioalloc: out of maps");
  215. unlock(&iomap);
  216. return port;
  217. }
  218. iomap.free = m->next;
  219. m->next = *l;
  220. m->start = port;
  221. m->end = port + size;
  222. strncpy(m->tag, tag, sizeof(m->tag));
  223. m->tag[sizeof(m->tag)-1] = 0;
  224. *l = m;
  225. archdir[0].qid.vers++;
  226. unlock(&iomap);
  227. return m->start;
  228. }
  229. void
  230. iofree(int port)
  231. {
  232. IOMap *m, **l;
  233. lock(&iomap);
  234. for(l = &iomap.m; *l; l = &(*l)->next){
  235. if((*l)->start == port){
  236. m = *l;
  237. *l = m->next;
  238. m->next = iomap.free;
  239. iomap.free = m;
  240. break;
  241. }
  242. if((*l)->start > port)
  243. break;
  244. }
  245. archdir[0].qid.vers++;
  246. unlock(&iomap);
  247. }
  248. int
  249. iounused(int start, int end)
  250. {
  251. IOMap *m;
  252. for(m = iomap.m; m; m = m->next){
  253. if(start >= m->start && start < m->end
  254. || start <= m->start && end > m->start)
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. static void
  260. checkport(int start, int end)
  261. {
  262. /* standard vga regs are OK */
  263. if(start >= 0x2b0 && end <= 0x2df+1)
  264. return;
  265. if(start >= 0x3c0 && end <= 0x3da+1)
  266. return;
  267. if(iounused(start, end))
  268. return;
  269. error(Eperm);
  270. }
  271. static Chan*
  272. archattach(char* spec)
  273. {
  274. return devattach('P', spec);
  275. }
  276. Walkqid*
  277. archwalk(Chan* c, Chan *nc, char** name, int nname)
  278. {
  279. return devwalk(c, nc, name, nname, archdir, narchdir, devgen);
  280. }
  281. static int
  282. archstat(Chan* c, uchar* dp, int n)
  283. {
  284. return devstat(c, dp, n, archdir, narchdir, devgen);
  285. }
  286. static Chan*
  287. archopen(Chan* c, int omode)
  288. {
  289. return devopen(c, omode, archdir, narchdir, devgen);
  290. }
  291. static void
  292. archclose(Chan*)
  293. {
  294. }
  295. enum
  296. {
  297. Linelen= 31,
  298. };
  299. static long
  300. archread(Chan *c, void *a, long n, vlong offset)
  301. {
  302. char *buf, *p;
  303. int port;
  304. ushort *sp;
  305. ulong *lp;
  306. IOMap *m;
  307. Rdwrfn *fn;
  308. switch((ulong)c->qid.path){
  309. case Qdir:
  310. return devdirread(c, a, n, archdir, narchdir, devgen);
  311. case Qiob:
  312. port = offset;
  313. checkport(offset, offset+n);
  314. for(p = a; port < offset+n; port++)
  315. *p++ = inb(port);
  316. return n;
  317. case Qiow:
  318. if(n & 1)
  319. error(Ebadarg);
  320. checkport(offset, offset+n);
  321. sp = a;
  322. for(port = offset; port < offset+n; port += 2)
  323. *sp++ = ins(port);
  324. return n;
  325. case Qiol:
  326. if(n & 3)
  327. error(Ebadarg);
  328. checkport(offset, offset+n);
  329. lp = a;
  330. for(port = offset; port < offset+n; port += 4)
  331. *lp++ = inl(port);
  332. return n;
  333. case Qioalloc:
  334. break;
  335. default:
  336. if(c->qid.path < narchdir && (fn = readfn[c->qid.path]))
  337. return fn(c, a, n, offset);
  338. error(Eperm);
  339. break;
  340. }
  341. if((buf = malloc(n)) == nil)
  342. error(Enomem);
  343. p = buf;
  344. n = n/Linelen;
  345. offset = offset/Linelen;
  346. lock(&iomap);
  347. for(m = iomap.m; n > 0 && m != nil; m = m->next){
  348. if(offset-- > 0)
  349. continue;
  350. sprint(p, "%8lux %8lux %-12.12s\n", m->start, m->end-1, m->tag);
  351. p += Linelen;
  352. n--;
  353. }
  354. unlock(&iomap);
  355. n = p - buf;
  356. memmove(a, buf, n);
  357. free(buf);
  358. return n;
  359. }
  360. static long
  361. archwrite(Chan *c, void *a, long n, vlong offset)
  362. {
  363. char *p;
  364. int port;
  365. ushort *sp;
  366. ulong *lp;
  367. Rdwrfn *fn;
  368. switch((ulong)c->qid.path){
  369. case Qiob:
  370. p = a;
  371. checkport(offset, offset+n);
  372. for(port = offset; port < offset+n; port++)
  373. outb(port, *p++);
  374. return n;
  375. case Qiow:
  376. if(n & 1)
  377. error(Ebadarg);
  378. checkport(offset, offset+n);
  379. sp = a;
  380. for(port = offset; port < offset+n; port += 2)
  381. outs(port, *sp++);
  382. return n;
  383. case Qiol:
  384. if(n & 3)
  385. error(Ebadarg);
  386. checkport(offset, offset+n);
  387. lp = a;
  388. for(port = offset; port < offset+n; port += 4)
  389. outl(port, *lp++);
  390. return n;
  391. default:
  392. if(c->qid.path < narchdir && (fn = writefn[c->qid.path]))
  393. return fn(c, a, n, offset);
  394. error(Eperm);
  395. break;
  396. }
  397. return 0;
  398. }
  399. Dev archdevtab = {
  400. 'P',
  401. "arch",
  402. devreset,
  403. devinit,
  404. devshutdown,
  405. archattach,
  406. archwalk,
  407. archstat,
  408. archopen,
  409. devcreate,
  410. archclose,
  411. archread,
  412. devbread,
  413. archwrite,
  414. devbwrite,
  415. devremove,
  416. devwstat,
  417. };
  418. /*
  419. * the following is a generic version of the
  420. * architecture specific stuff
  421. */
  422. static int
  423. unimplemented(int)
  424. {
  425. return 0;
  426. }
  427. static void
  428. nop(void)
  429. {
  430. }
  431. /*
  432. * On a uniprocessor, you'd think that coherence could be nop,
  433. * but it can't. We still need a barrier when using coherence() in
  434. * device drivers.
  435. *
  436. * On VMware, it's safe (and a huge win) to set this to nop.
  437. * Aux/vmware does this via the #P/archctl file.
  438. */
  439. void (*coherence)(void) = nop;
  440. PCArch* arch;
  441. extern PCArch* knownarch[];
  442. PCArch archgeneric = {
  443. .id= "generic",
  444. .ident= 0,
  445. .reset= i8042reset,
  446. .serialpower= unimplemented,
  447. .modempower= unimplemented,
  448. .intrinit= i8259init,
  449. .intrenable= i8259enable,
  450. .intrvecno= i8259vecno,
  451. .intrdisable= i8259disable,
  452. .intron= i8259on,
  453. .introff= i8259off,
  454. .clockenable= i8253enable,
  455. .fastclock= i8253read,
  456. .timerset= i8253timerset,
  457. };
  458. typedef struct X86type X86type;
  459. struct X86type {
  460. int family;
  461. int model;
  462. int aalcycles;
  463. char* name;
  464. };
  465. static X86type x86intel[] =
  466. {
  467. { 4, 0, 22, "486DX", }, /* known chips */
  468. { 4, 1, 22, "486DX50", },
  469. { 4, 2, 22, "486SX", },
  470. { 4, 3, 22, "486DX2", },
  471. { 4, 4, 22, "486SL", },
  472. { 4, 5, 22, "486SX2", },
  473. { 4, 7, 22, "DX2WB", }, /* P24D */
  474. { 4, 8, 22, "DX4", }, /* P24C */
  475. { 4, 9, 22, "DX4WB", }, /* P24CT */
  476. { 5, 0, 23, "P5", },
  477. { 5, 1, 23, "P5", },
  478. { 5, 2, 23, "P54C", },
  479. { 5, 3, 23, "P24T", },
  480. { 5, 4, 23, "P55C MMX", },
  481. { 5, 7, 23, "P54C VRT", },
  482. { 6, 1, 16, "PentiumPro", },/* trial and error */
  483. { 6, 3, 16, "PentiumII", },
  484. { 6, 5, 16, "PentiumII/Xeon", },
  485. { 6, 6, 16, "Celeron", },
  486. { 6, 7, 16, "PentiumIII/Xeon", },
  487. { 6, 8, 16, "PentiumIII/Xeon", },
  488. { 6, 0xB, 16, "PentiumIII/Xeon", },
  489. { 0xF, 1, 16, "P4", }, /* P4 */
  490. { 0xF, 2, 16, "PentiumIV/Xeon", },
  491. { 3, -1, 32, "386", }, /* family defaults */
  492. { 4, -1, 22, "486", },
  493. { 5, -1, 23, "P5", },
  494. { 6, -1, 16, "P6", },
  495. { 0xF, -1, 16, "P4", }, /* P4 */
  496. { -1, -1, 16, "unknown", }, /* total default */
  497. };
  498. /*
  499. * The AMD processors all implement the CPUID instruction.
  500. * The later ones also return the processor name via functions
  501. * 0x80000002, 0x80000003 and 0x80000004 in registers AX, BX, CX
  502. * and DX:
  503. * K5 "AMD-K5(tm) Processor"
  504. * K6 "AMD-K6tm w/ multimedia extensions"
  505. * K6 3D "AMD-K6(tm) 3D processor"
  506. * K6 3D+ ?
  507. */
  508. static X86type x86amd[] =
  509. {
  510. { 5, 0, 23, "AMD-K5", }, /* guesswork */
  511. { 5, 1, 23, "AMD-K5", }, /* guesswork */
  512. { 5, 2, 23, "AMD-K5", }, /* guesswork */
  513. { 5, 3, 23, "AMD-K5", }, /* guesswork */
  514. { 5, 6, 11, "AMD-K6", }, /* trial and error */
  515. { 5, 7, 11, "AMD-K6", }, /* trial and error */
  516. { 5, 8, 11, "AMD-K6-2", }, /* trial and error */
  517. { 5, 9, 11, "AMD-K6-III", },/* trial and error */
  518. { 6, 1, 11, "AMD-Athlon", },/* trial and error */
  519. { 6, 2, 11, "AMD-Athlon", },/* trial and error */
  520. { 4, -1, 22, "Am486", }, /* guesswork */
  521. { 5, -1, 23, "AMD-K5/K6", }, /* guesswork */
  522. { 6, -1, 11, "AMD-Athlon", },/* guesswork */
  523. { 0xF, -1, 11, "AMD64", }, /* guesswork */
  524. { -1, -1, 11, "unknown", }, /* total default */
  525. };
  526. /*
  527. * WinChip 240MHz
  528. */
  529. static X86type x86winchip[] =
  530. {
  531. {5, 4, 23, "Winchip",}, /* guesswork */
  532. {6, 7, 23, "Via C3 Samuel 2 or Ezra",},
  533. {6, 8, 23, "Via C3 Ezra-T",},
  534. { -1, -1, 23, "unknown", }, /* total default */
  535. };
  536. /*
  537. * SiS 55x
  538. */
  539. static X86type x86sis[] =
  540. {
  541. {5, 0, 23, "SiS 55x",}, /* guesswork */
  542. { -1, -1, 23, "unknown", }, /* total default */
  543. };
  544. static X86type *cputype;
  545. static void simplecycles(uvlong*);
  546. void (*cycles)(uvlong*) = simplecycles;
  547. void _cycles(uvlong*); /* in l.s */
  548. static void
  549. simplecycles(uvlong*x)
  550. {
  551. *x = m->ticks;
  552. }
  553. void
  554. cpuidprint(void)
  555. {
  556. int i;
  557. char buf[128];
  558. i = sprint(buf, "cpu%d: %dMHz ", m->machno, m->cpumhz);
  559. if(m->cpuidid[0])
  560. i += sprint(buf+i, "%12.12s ", m->cpuidid);
  561. sprint(buf+i, "%s (cpuid: AX 0x%4.4uX DX 0x%4.4uX)\n",
  562. m->cpuidtype, m->cpuidax, m->cpuiddx);
  563. print(buf);
  564. }
  565. /*
  566. * figure out:
  567. * - cpu type
  568. * - whether or not we have a TSC (cycle counter)
  569. * - whether or not it supports page size extensions
  570. * (if so turn it on)
  571. * - whether or not it supports machine check exceptions
  572. * (if so turn it on)
  573. * - whether or not it supports the page global flag
  574. * (if so turn it on)
  575. */
  576. int
  577. cpuidentify(void)
  578. {
  579. char *p;
  580. int family, model, nomce;
  581. X86type *t, *tab;
  582. ulong cr4;
  583. vlong mca, mct;
  584. cpuid(m->cpuidid, &m->cpuidax, &m->cpuiddx);
  585. if(strncmp(m->cpuidid, "AuthenticAMD", 12) == 0)
  586. tab = x86amd;
  587. else if(strncmp(m->cpuidid, "CentaurHauls", 12) == 0)
  588. tab = x86winchip;
  589. else if(strncmp(m->cpuidid, "SiS SiS SiS ", 12) == 0)
  590. tab = x86sis;
  591. else
  592. tab = x86intel;
  593. family = X86FAMILY(m->cpuidax);
  594. model = X86MODEL(m->cpuidax);
  595. for(t=tab; t->name; t++)
  596. if((t->family == family && t->model == model)
  597. || (t->family == family && t->model == -1)
  598. || (t->family == -1))
  599. break;
  600. m->cpuidtype = t->name;
  601. /*
  602. * if there is one, set tsc to a known value
  603. */
  604. if(m->cpuiddx & 0x10){
  605. m->havetsc = 1;
  606. cycles = _cycles;
  607. if(m->cpuiddx & 0x20)
  608. wrmsr(0x10, 0);
  609. }
  610. /*
  611. * use i8253 to guess our cpu speed
  612. */
  613. guesscpuhz(t->aalcycles);
  614. /*
  615. * If machine check exception, page size extensions or page global bit
  616. * are supported enable them in CR4 and clear any other set extensions.
  617. * If machine check was enabled clear out any lingering status.
  618. */
  619. if(m->cpuiddx & 0x2088){
  620. cr4 = 0;
  621. if(m->cpuiddx & 0x08)
  622. cr4 |= 0x10; /* page size extensions */
  623. if(p = getconf("*nomce"))
  624. nomce = strtoul(p, 0, 0);
  625. else
  626. nomce = 0;
  627. if((m->cpuiddx & 0x80) && !nomce){
  628. cr4 |= 0x40; /* machine check enable */
  629. if(family == 5){
  630. rdmsr(0x00, &mca);
  631. rdmsr(0x01, &mct);
  632. }
  633. }
  634. /*
  635. * Detect whether the chip supports the global bit
  636. * in page directory and page table entries. When set
  637. * in a particular entry, it means ``don't bother removing
  638. * this from the TLB when CR3 changes.''
  639. *
  640. * We flag all kernel pages with this bit. Doing so lessens the
  641. * overhead of switching processes on bare hardware,
  642. * even more so on VMware. See mmu.c:/^memglobal.
  643. *
  644. * For future reference, should we ever need to do a
  645. * full TLB flush, it can be accomplished by clearing
  646. * the PGE bit in CR4, writing to CR3, and then
  647. * restoring the PGE bit.
  648. */
  649. if(m->cpuiddx & 0x2000){
  650. cr4 |= 0x80; /* page global enable bit */
  651. m->havepge = 1;
  652. }
  653. putcr4(cr4);
  654. if(m->cpuiddx & 0x80)
  655. rdmsr(0x01, &mct);
  656. }
  657. cputype = t;
  658. return t->family;
  659. }
  660. static long
  661. cputyperead(Chan*, void *a, long n, vlong offset)
  662. {
  663. char str[32];
  664. ulong mhz;
  665. mhz = (m->cpuhz+999999)/1000000;
  666. snprint(str, sizeof(str), "%s %lud\n", cputype->name, mhz);
  667. return readstr(offset, a, n, str);
  668. }
  669. static long
  670. archctlread(Chan*, void *a, long nn, vlong offset)
  671. {
  672. char buf[256];
  673. int n;
  674. n = snprint(buf, sizeof buf, "cpu %s %lud%s\n",
  675. cputype->name, (ulong)(m->cpuhz+999999)/1000000,
  676. m->havepge ? " pge" : "");
  677. n += snprint(buf+n, sizeof buf-n, "pge %s\n", getcr4()&0x80 ? "on" : "off");
  678. n += snprint(buf+n, sizeof buf-n, "coherence ");
  679. if(coherence == mb386)
  680. n += snprint(buf+n, sizeof buf-n, "mb386\n");
  681. else if(coherence == mb586)
  682. n += snprint(buf+n, sizeof buf-n, "mb586\n");
  683. else if(coherence == nop)
  684. n += snprint(buf+n, sizeof buf-n, "nop\n");
  685. else
  686. n += snprint(buf+n, sizeof buf-n, "0x%p\n", coherence);
  687. n += snprint(buf+n, sizeof buf-n, "i8253set %s\n", doi8253set ? "on" : "off");
  688. buf[n] = 0;
  689. return readstr(offset, a, nn, buf);
  690. }
  691. enum
  692. {
  693. CMpge,
  694. CMcoherence,
  695. CMi8253set,
  696. };
  697. static Cmdtab archctlmsg[] =
  698. {
  699. CMpge, "pge", 2,
  700. CMcoherence, "coherence", 2,
  701. CMi8253set, "i8253set", 2,
  702. };
  703. static long
  704. archctlwrite(Chan*, void *a, long n, vlong)
  705. {
  706. Cmdbuf *cb;
  707. Cmdtab *ct;
  708. cb = parsecmd(a, n);
  709. if(waserror()){
  710. free(cb);
  711. nexterror();
  712. }
  713. ct = lookupcmd(cb, archctlmsg, nelem(archctlmsg));
  714. switch(ct->index){
  715. case CMpge:
  716. if(!m->havepge)
  717. error("processor does not support pge");
  718. if(strcmp(cb->f[1], "on") == 0)
  719. putcr4(getcr4() | 0x80);
  720. else if(strcmp(cb->f[1], "off") == 0)
  721. putcr4(getcr4() & ~0x80);
  722. else
  723. cmderror(cb, "invalid pge ctl");
  724. break;
  725. case CMcoherence:
  726. if(strcmp(cb->f[1], "mb386") == 0)
  727. coherence = mb386;
  728. else if(strcmp(cb->f[1], "mb586") == 0){
  729. if(X86FAMILY(m->cpuidax) < 5)
  730. error("invalid coherence ctl on this cpu family");
  731. coherence = mb586;
  732. }
  733. else if(strcmp(cb->f[1], "nop") == 0){
  734. /* only safe on vmware */
  735. if(conf.nmach > 1)
  736. error("cannot disable coherence on a multiprocessor");
  737. coherence = nop;
  738. }else
  739. cmderror(cb, "invalid coherence ctl");
  740. break;
  741. case CMi8253set:
  742. if(strcmp(cb->f[1], "on") == 0)
  743. doi8253set = 1;
  744. else if(strcmp(cb->f[1], "off") == 0){
  745. doi8253set = 0;
  746. (*arch->timerset)(0);
  747. }else
  748. cmderror(cb, "invalid i2853set ctl");
  749. break;
  750. }
  751. free(cb);
  752. poperror();
  753. return n;
  754. }
  755. void
  756. archinit(void)
  757. {
  758. PCArch **p;
  759. arch = 0;
  760. for(p = knownarch; *p; p++){
  761. if((*p)->ident && (*p)->ident() == 0){
  762. arch = *p;
  763. break;
  764. }
  765. }
  766. if(arch == 0)
  767. arch = &archgeneric;
  768. else{
  769. if(arch->id == 0)
  770. arch->id = archgeneric.id;
  771. if(arch->reset == 0)
  772. arch->reset = archgeneric.reset;
  773. if(arch->serialpower == 0)
  774. arch->serialpower = archgeneric.serialpower;
  775. if(arch->modempower == 0)
  776. arch->modempower = archgeneric.modempower;
  777. if(arch->intrinit == 0)
  778. arch->intrinit = archgeneric.intrinit;
  779. if(arch->intrenable == 0)
  780. arch->intrenable = archgeneric.intrenable;
  781. }
  782. /*
  783. * Decide whether to use copy-on-reference (386 and mp).
  784. * We get another chance to set it in mpinit() for a
  785. * multiprocessor.
  786. */
  787. if(X86FAMILY(m->cpuidax) == 3)
  788. conf.copymode = 1;
  789. if(X86FAMILY(m->cpuidax) >= 5)
  790. coherence = mb586;
  791. addarchfile("cputype", 0444, cputyperead, nil);
  792. addarchfile("archctl", 0664, archctlread, archctlwrite);
  793. }
  794. /*
  795. * call either the pcmcia or pccard device setup
  796. */
  797. int
  798. pcmspecial(char *idstr, ISAConf *isa)
  799. {
  800. return (_pcmspecial != nil)? _pcmspecial(idstr, isa): -1;
  801. }
  802. /*
  803. * call either the pcmcia or pccard device teardown
  804. */
  805. void
  806. pcmspecialclose(int a)
  807. {
  808. if (_pcmspecialclose != nil)
  809. _pcmspecialclose(a);
  810. }
  811. /*
  812. * return value and speed of timer set in arch->clockenable
  813. */
  814. uvlong
  815. fastticks(uvlong *hz)
  816. {
  817. return (*arch->fastclock)(hz);
  818. }
  819. /*
  820. * set next timer interrupt
  821. */
  822. void
  823. timerset(uvlong x)
  824. {
  825. if(doi8253set)
  826. (*arch->timerset)(x);
  827. }