devarch.c 17 KB

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