devarch.c 18 KB

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