devarch.c 18 KB

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