devarch.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. {6, 9, 23, "Via C3 Eden-N",},
  535. { -1, -1, 23, "unknown", }, /* total default */
  536. };
  537. /*
  538. * SiS 55x
  539. */
  540. static X86type x86sis[] =
  541. {
  542. {5, 0, 23, "SiS 55x",}, /* guesswork */
  543. { -1, -1, 23, "unknown", }, /* total default */
  544. };
  545. static X86type *cputype;
  546. static void simplecycles(uvlong*);
  547. void (*cycles)(uvlong*) = simplecycles;
  548. void _cycles(uvlong*); /* in l.s */
  549. static void
  550. simplecycles(uvlong*x)
  551. {
  552. *x = m->ticks;
  553. }
  554. void
  555. cpuidprint(void)
  556. {
  557. int i;
  558. char buf[128];
  559. i = sprint(buf, "cpu%d: %dMHz ", m->machno, m->cpumhz);
  560. if(m->cpuidid[0])
  561. i += sprint(buf+i, "%12.12s ", m->cpuidid);
  562. sprint(buf+i, "%s (cpuid: AX 0x%4.4uX DX 0x%4.4uX)\n",
  563. m->cpuidtype, m->cpuidax, m->cpuiddx);
  564. print(buf);
  565. }
  566. /*
  567. * figure out:
  568. * - cpu type
  569. * - whether or not we have a TSC (cycle counter)
  570. * - whether or not it supports page size extensions
  571. * (if so turn it on)
  572. * - whether or not it supports machine check exceptions
  573. * (if so turn it on)
  574. * - whether or not it supports the page global flag
  575. * (if so turn it on)
  576. */
  577. int
  578. cpuidentify(void)
  579. {
  580. char *p;
  581. int family, model, nomce;
  582. X86type *t, *tab;
  583. ulong cr4;
  584. vlong mca, mct;
  585. cpuid(m->cpuidid, &m->cpuidax, &m->cpuiddx);
  586. if(strncmp(m->cpuidid, "AuthenticAMD", 12) == 0)
  587. tab = x86amd;
  588. else if(strncmp(m->cpuidid, "CentaurHauls", 12) == 0)
  589. tab = x86winchip;
  590. else if(strncmp(m->cpuidid, "SiS SiS SiS ", 12) == 0)
  591. tab = x86sis;
  592. else
  593. tab = x86intel;
  594. family = X86FAMILY(m->cpuidax);
  595. model = X86MODEL(m->cpuidax);
  596. for(t=tab; t->name; t++)
  597. if((t->family == family && t->model == model)
  598. || (t->family == family && t->model == -1)
  599. || (t->family == -1))
  600. break;
  601. m->cpuidtype = t->name;
  602. /*
  603. * if there is one, set tsc to a known value
  604. */
  605. if(m->cpuiddx & 0x10){
  606. m->havetsc = 1;
  607. cycles = _cycles;
  608. if(m->cpuiddx & 0x20)
  609. wrmsr(0x10, 0);
  610. }
  611. /*
  612. * use i8253 to guess our cpu speed
  613. */
  614. guesscpuhz(t->aalcycles);
  615. /*
  616. * If machine check exception, page size extensions or page global bit
  617. * are supported enable them in CR4 and clear any other set extensions.
  618. * If machine check was enabled clear out any lingering status.
  619. */
  620. if(m->cpuiddx & 0x2088){
  621. cr4 = 0;
  622. if(m->cpuiddx & 0x08)
  623. cr4 |= 0x10; /* page size extensions */
  624. if(p = getconf("*nomce"))
  625. nomce = strtoul(p, 0, 0);
  626. else
  627. nomce = 0;
  628. if((m->cpuiddx & 0x80) && !nomce){
  629. cr4 |= 0x40; /* machine check enable */
  630. if(family == 5){
  631. rdmsr(0x00, &mca);
  632. rdmsr(0x01, &mct);
  633. }
  634. }
  635. /*
  636. * Detect whether the chip supports the global bit
  637. * in page directory and page table entries. When set
  638. * in a particular entry, it means ``don't bother removing
  639. * this from the TLB when CR3 changes.''
  640. *
  641. * We flag all kernel pages with this bit. Doing so lessens the
  642. * overhead of switching processes on bare hardware,
  643. * even more so on VMware. See mmu.c:/^memglobal.
  644. *
  645. * For future reference, should we ever need to do a
  646. * full TLB flush, it can be accomplished by clearing
  647. * the PGE bit in CR4, writing to CR3, and then
  648. * restoring the PGE bit.
  649. */
  650. if(m->cpuiddx & 0x2000){
  651. cr4 |= 0x80; /* page global enable bit */
  652. m->havepge = 1;
  653. }
  654. putcr4(cr4);
  655. if(m->cpuiddx & 0x80)
  656. rdmsr(0x01, &mct);
  657. }
  658. cputype = t;
  659. return t->family;
  660. }
  661. static long
  662. cputyperead(Chan*, void *a, long n, vlong offset)
  663. {
  664. char str[32];
  665. ulong mhz;
  666. mhz = (m->cpuhz+999999)/1000000;
  667. snprint(str, sizeof(str), "%s %lud\n", cputype->name, mhz);
  668. return readstr(offset, a, n, str);
  669. }
  670. static long
  671. archctlread(Chan*, void *a, long nn, vlong offset)
  672. {
  673. char buf[256];
  674. int n;
  675. n = snprint(buf, sizeof buf, "cpu %s %lud%s\n",
  676. cputype->name, (ulong)(m->cpuhz+999999)/1000000,
  677. m->havepge ? " pge" : "");
  678. n += snprint(buf+n, sizeof buf-n, "pge %s\n", getcr4()&0x80 ? "on" : "off");
  679. n += snprint(buf+n, sizeof buf-n, "coherence ");
  680. if(coherence == mb386)
  681. n += snprint(buf+n, sizeof buf-n, "mb386\n");
  682. else if(coherence == mb586)
  683. n += snprint(buf+n, sizeof buf-n, "mb586\n");
  684. else if(coherence == nop)
  685. n += snprint(buf+n, sizeof buf-n, "nop\n");
  686. else
  687. n += snprint(buf+n, sizeof buf-n, "0x%p\n", coherence);
  688. n += snprint(buf+n, sizeof buf-n, "i8253set %s\n", doi8253set ? "on" : "off");
  689. buf[n] = 0;
  690. return readstr(offset, a, nn, buf);
  691. }
  692. enum
  693. {
  694. CMpge,
  695. CMcoherence,
  696. CMi8253set,
  697. };
  698. static Cmdtab archctlmsg[] =
  699. {
  700. CMpge, "pge", 2,
  701. CMcoherence, "coherence", 2,
  702. CMi8253set, "i8253set", 2,
  703. };
  704. static long
  705. archctlwrite(Chan*, void *a, long n, vlong)
  706. {
  707. Cmdbuf *cb;
  708. Cmdtab *ct;
  709. cb = parsecmd(a, n);
  710. if(waserror()){
  711. free(cb);
  712. nexterror();
  713. }
  714. ct = lookupcmd(cb, archctlmsg, nelem(archctlmsg));
  715. switch(ct->index){
  716. case CMpge:
  717. if(!m->havepge)
  718. error("processor does not support pge");
  719. if(strcmp(cb->f[1], "on") == 0)
  720. putcr4(getcr4() | 0x80);
  721. else if(strcmp(cb->f[1], "off") == 0)
  722. putcr4(getcr4() & ~0x80);
  723. else
  724. cmderror(cb, "invalid pge ctl");
  725. break;
  726. case CMcoherence:
  727. if(strcmp(cb->f[1], "mb386") == 0)
  728. coherence = mb386;
  729. else if(strcmp(cb->f[1], "mb586") == 0){
  730. if(X86FAMILY(m->cpuidax) < 5)
  731. error("invalid coherence ctl on this cpu family");
  732. coherence = mb586;
  733. }
  734. else if(strcmp(cb->f[1], "nop") == 0){
  735. /* only safe on vmware */
  736. if(conf.nmach > 1)
  737. error("cannot disable coherence on a multiprocessor");
  738. coherence = nop;
  739. }else
  740. cmderror(cb, "invalid coherence ctl");
  741. break;
  742. case CMi8253set:
  743. if(strcmp(cb->f[1], "on") == 0)
  744. doi8253set = 1;
  745. else if(strcmp(cb->f[1], "off") == 0){
  746. doi8253set = 0;
  747. (*arch->timerset)(0);
  748. }else
  749. cmderror(cb, "invalid i2853set ctl");
  750. break;
  751. }
  752. free(cb);
  753. poperror();
  754. return n;
  755. }
  756. void
  757. archinit(void)
  758. {
  759. PCArch **p;
  760. arch = 0;
  761. for(p = knownarch; *p; p++){
  762. if((*p)->ident && (*p)->ident() == 0){
  763. arch = *p;
  764. break;
  765. }
  766. }
  767. if(arch == 0)
  768. arch = &archgeneric;
  769. else{
  770. if(arch->id == 0)
  771. arch->id = archgeneric.id;
  772. if(arch->reset == 0)
  773. arch->reset = archgeneric.reset;
  774. if(arch->serialpower == 0)
  775. arch->serialpower = archgeneric.serialpower;
  776. if(arch->modempower == 0)
  777. arch->modempower = archgeneric.modempower;
  778. if(arch->intrinit == 0)
  779. arch->intrinit = archgeneric.intrinit;
  780. if(arch->intrenable == 0)
  781. arch->intrenable = archgeneric.intrenable;
  782. }
  783. /*
  784. * Decide whether to use copy-on-reference (386 and mp).
  785. * We get another chance to set it in mpinit() for a
  786. * multiprocessor.
  787. */
  788. if(X86FAMILY(m->cpuidax) == 3)
  789. conf.copymode = 1;
  790. if(X86FAMILY(m->cpuidax) >= 5)
  791. coherence = mb586;
  792. addarchfile("cputype", 0444, cputyperead, nil);
  793. addarchfile("archctl", 0664, archctlread, archctlwrite);
  794. }
  795. /*
  796. * call either the pcmcia or pccard device setup
  797. */
  798. int
  799. pcmspecial(char *idstr, ISAConf *isa)
  800. {
  801. return (_pcmspecial != nil)? _pcmspecial(idstr, isa): -1;
  802. }
  803. /*
  804. * call either the pcmcia or pccard device teardown
  805. */
  806. void
  807. pcmspecialclose(int a)
  808. {
  809. if (_pcmspecialclose != nil)
  810. _pcmspecialclose(a);
  811. }
  812. /*
  813. * return value and speed of timer set in arch->clockenable
  814. */
  815. uvlong
  816. fastticks(uvlong *hz)
  817. {
  818. return (*arch->fastclock)(hz);
  819. }
  820. /*
  821. * set next timer interrupt
  822. */
  823. void
  824. timerset(uvlong x)
  825. {
  826. if(doi8253set)
  827. (*arch->timerset)(x);
  828. }