devarch.c 17 KB

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