devarch.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 "../port/error.h"
  8. #include "axp.h"
  9. typedef struct IOMap IOMap;
  10. struct IOMap
  11. {
  12. IOMap *next;
  13. char tag[13];
  14. ulong start;
  15. ulong end;
  16. };
  17. static struct
  18. {
  19. Lock;
  20. IOMap *m;
  21. IOMap *free;
  22. IOMap maps[32]; // some initial free maps
  23. QLock ql; // lock for reading map
  24. } iomap;
  25. enum {
  26. Qdir = 0,
  27. Qioalloc = 1,
  28. Qiob,
  29. Qiow,
  30. Qiol,
  31. Qbase,
  32. Qmax = 16,
  33. };
  34. typedef long Rdwrfn(Chan*, void*, long, vlong);
  35. static Rdwrfn *readfn[Qmax];
  36. static Rdwrfn *writefn[Qmax];
  37. static Dirtab archdir[] = {
  38. ".", { Qdir, 0, QTDIR }, 0, 0555,
  39. "ioalloc", { Qioalloc, 0 }, 0, 0444,
  40. "iob", { Qiob, 0 }, 0, 0660,
  41. "iow", { Qiow, 0 }, 0, 0660,
  42. "iol", { Qiol, 0 }, 0, 0660,
  43. };
  44. Lock archwlock; /* the lock is only for changing archdir */
  45. int narchdir = Qbase;
  46. int (*_pcmspecial)(char *, ISAConf *);
  47. void (*_pcmspecialclose)(int);
  48. /*
  49. * Add a file to the #P listing. Once added, you can't delete it.
  50. * You can't add a file with the same name as one already there,
  51. * and you get a pointer to the Dirtab entry so you can do things
  52. * like change the Qid version. Changing the Qid path is disallowed.
  53. */
  54. Dirtab*
  55. addarchfile(char *name, int perm, Rdwrfn *rdfn, Rdwrfn *wrfn)
  56. {
  57. int i;
  58. Dirtab d;
  59. Dirtab *dp;
  60. memset(&d, 0, sizeof d);
  61. strcpy(d.name, name);
  62. d.perm = perm;
  63. lock(&archwlock);
  64. if(narchdir >= Qmax){
  65. unlock(&archwlock);
  66. return nil;
  67. }
  68. for(i=0; i<narchdir; i++)
  69. if(strcmp(archdir[i].name, name) == 0){
  70. unlock(&archwlock);
  71. return nil;
  72. }
  73. d.qid.path = narchdir;
  74. archdir[narchdir] = d;
  75. readfn[narchdir] = rdfn;
  76. writefn[narchdir] = wrfn;
  77. dp = &archdir[narchdir++];
  78. unlock(&archwlock);
  79. return dp;
  80. }
  81. void
  82. ioinit(void)
  83. {
  84. int i;
  85. for(i = 0; i < nelem(iomap.maps)-1; i++)
  86. iomap.maps[i].next = &iomap.maps[i+1];
  87. iomap.maps[i].next = nil;
  88. iomap.free = iomap.maps;
  89. // a dummy entry at 2^17
  90. ioalloc(0x20000, 1, 0, "dummy");
  91. }
  92. //
  93. // alloc some io port space and remember who it was
  94. // alloced to. if port < 0, find a free region.
  95. //
  96. int
  97. ioalloc(int port, int size, int align, char *tag)
  98. {
  99. IOMap *m, **l;
  100. int i;
  101. lock(&iomap);
  102. if(port < 0){
  103. // find a free port above 0x400 and below 0x1000
  104. port = 0x400;
  105. for(l = &iomap.m; *l; l = &(*l)->next){
  106. m = *l;
  107. i = m->start - port;
  108. if(i > size)
  109. break;
  110. if(align > 0)
  111. port = ((port+align-1)/align)*align;
  112. else
  113. port = m->end;
  114. }
  115. if(*l == nil){
  116. unlock(&iomap);
  117. return -1;
  118. }
  119. } else {
  120. // see if the space clashes with previously allocated ports
  121. for(l = &iomap.m; *l; l = &(*l)->next){
  122. m = *l;
  123. if(m->end <= port)
  124. continue;
  125. if(m->start >= port+size)
  126. break;
  127. unlock(&iomap);
  128. return -1;
  129. }
  130. }
  131. m = iomap.free;
  132. if(m == nil){
  133. print("ioalloc: out of maps");
  134. unlock(&iomap);
  135. return port;
  136. }
  137. iomap.free = m->next;
  138. m->next = *l;
  139. m->start = port;
  140. m->end = port + size;
  141. strncpy(m->tag, tag, sizeof(m->tag));
  142. m->tag[sizeof(m->tag)-1] = 0;
  143. *l = m;
  144. archdir[0].qid.vers++;
  145. unlock(&iomap);
  146. return m->start;
  147. }
  148. void
  149. iofree(int port)
  150. {
  151. IOMap *m, **l;
  152. lock(&iomap);
  153. for(l = &iomap.m; *l; l = &(*l)->next){
  154. if((*l)->start == port){
  155. m = *l;
  156. *l = m->next;
  157. m->next = iomap.free;
  158. iomap.free = m;
  159. break;
  160. }
  161. if((*l)->start > port)
  162. break;
  163. }
  164. archdir[0].qid.vers++;
  165. unlock(&iomap);
  166. }
  167. int
  168. iounused(int start, int end)
  169. {
  170. IOMap *m;
  171. for(m = iomap.m; m; m = m->next){
  172. if(start >= m->start && start < m->end
  173. || start <= m->start && end > m->start)
  174. return 0;
  175. }
  176. return 1;
  177. }
  178. static void
  179. checkport(int start, int end)
  180. {
  181. /* standard vga regs are OK */
  182. if(start >= 0x2b0 && end <= 0x2df+1)
  183. return;
  184. if(start >= 0x3c0 && end <= 0x3da+1)
  185. return;
  186. if(iounused(start, end))
  187. return;
  188. error(Eperm);
  189. }
  190. static Chan*
  191. archattach(char* spec)
  192. {
  193. return devattach('P', spec);
  194. }
  195. Walkqid*
  196. archwalk(Chan* c, Chan *nc, char** name, int nname)
  197. {
  198. return devwalk(c, nc, name, nname, archdir, narchdir, devgen);
  199. }
  200. static int
  201. archstat(Chan* c, uchar* dp, int n)
  202. {
  203. return devstat(c, dp, n, archdir, narchdir, devgen);
  204. }
  205. static Chan*
  206. archopen(Chan* c, int omode)
  207. {
  208. return devopen(c, omode, archdir, nelem(archdir), devgen);
  209. }
  210. static void
  211. archclose(Chan*)
  212. {
  213. }
  214. enum
  215. {
  216. Linelen= 31,
  217. };
  218. static long
  219. archread(Chan *c, void *a, long n, vlong offset)
  220. {
  221. char buf[Linelen+1], *p;
  222. int port;
  223. ushort *sp;
  224. ulong *lp;
  225. IOMap *m;
  226. Rdwrfn *fn;
  227. switch((ulong)c->qid.path){
  228. case Qdir:
  229. return devdirread(c, a, n, archdir, nelem(archdir), devgen);
  230. case Qiob:
  231. port = offset;
  232. checkport(offset, offset+n);
  233. for(p = a; port < offset+n; port++)
  234. *p++ = inb(port);
  235. return n;
  236. case Qiow:
  237. if((n & 0x01) || (offset & 0x01))
  238. error(Ebadarg);
  239. checkport(offset, offset+n+1);
  240. n /= 2;
  241. sp = a;
  242. for(port = offset; port < offset+n; port += 2)
  243. *sp++ = ins(port);
  244. return n*2;
  245. case Qiol:
  246. if((n & 0x03) || (offset & 0x03))
  247. error(Ebadarg);
  248. checkport(offset, offset+n+3);
  249. n /= 4;
  250. lp = a;
  251. for(port = offset; port < offset+n; port += 4)
  252. *lp++ = inl(port);
  253. return n*4;
  254. case Qioalloc:
  255. break;
  256. default:
  257. if(c->qid.path < narchdir && (fn = readfn[c->qid.path]))
  258. return fn(c, a, n, offset);
  259. error(Eperm);
  260. break;
  261. }
  262. offset = offset/Linelen;
  263. n = n/Linelen;
  264. p = a;
  265. lock(&iomap);
  266. for(m = iomap.m; n > 0 && m != nil; m = m->next){
  267. if(offset-- > 0)
  268. continue;
  269. if(strcmp(m->tag, "dummy") == 0)
  270. break;
  271. sprint(buf, "%8lux %8lux %-12.12s\n", m->start, m->end-1, m->tag);
  272. memmove(p, buf, Linelen);
  273. p += Linelen;
  274. n--;
  275. }
  276. unlock(&iomap);
  277. return p - (char*)a;
  278. }
  279. static long
  280. archwrite(Chan *c, void *a, long n, vlong offset)
  281. {
  282. char *p;
  283. int port;
  284. ushort *sp;
  285. ulong *lp;
  286. Rdwrfn *fn;
  287. switch((ulong)c->qid.path){
  288. case Qiob:
  289. p = a;
  290. checkport(offset, offset+n);
  291. for(port = offset; port < offset+n; port++)
  292. outb(port, *p++);
  293. return n;
  294. case Qiow:
  295. if((n & 01) || (offset & 01))
  296. error(Ebadarg);
  297. checkport(offset, offset+n+1);
  298. n /= 2;
  299. sp = a;
  300. for(port = offset; port < offset+n; port += 2)
  301. outs(port, *sp++);
  302. return n*2;
  303. case Qiol:
  304. if((n & 0x03) || (offset & 0x03))
  305. error(Ebadarg);
  306. checkport(offset, offset+n+3);
  307. n /= 4;
  308. lp = a;
  309. for(port = offset; port < offset+n; port += 4)
  310. outl(port, *lp++);
  311. return n*4;
  312. default:
  313. if(c->qid.path < narchdir && (fn = writefn[c->qid.path]))
  314. return fn(c, a, n, offset);
  315. error(Eperm);
  316. break;
  317. }
  318. return 0;
  319. }
  320. Dev archdevtab = {
  321. 'P',
  322. "arch",
  323. devreset,
  324. devinit,
  325. devshutdown,
  326. archattach,
  327. archwalk,
  328. archstat,
  329. archopen,
  330. devcreate,
  331. archclose,
  332. archread,
  333. devbread,
  334. archwrite,
  335. devbwrite,
  336. devremove,
  337. devwstat,
  338. };
  339. PCArch* arch;
  340. extern PCArch* knownarch[];
  341. PCArch archgeneric = {
  342. "generic", /* id */
  343. 0, /* ident */
  344. 0, /* coreinit */
  345. 0, /* coredetach */
  346. };
  347. static char *sysnames[] =
  348. {
  349. [1] "Alpha Demo. Unit",
  350. [2] "DEC 4000; Cobra",
  351. [3] "DEC 7000; Ruby",
  352. [4] "DEC 3000/500; Flamingo family (TC)",
  353. [6] "DEC 2000/300; Jensen (EISA/ISA)",
  354. [7] "DEC 3000/300; Pelican (TC)",
  355. [8] "Avalon A12; Avalon Multicomputer",
  356. [9] "DEC 2100/A500; Sable",
  357. [10] "DEC APXVME/64; AXPvme (VME?)",
  358. [11] "DEC AXPPCI/33; NoName (PCI/ISA)",
  359. [12] "DEC 21000; TurboLaser (PCI/EISA)",
  360. [13] "DEC 2100/A50; Avanti (PCI/ISA)",
  361. [14] "DEC MUSTANG; Mustang",
  362. [15] "DEC KN20AA; kn20aa (PCI/EISA)",
  363. [17] "DEC 1000; Mikasa (PCI/ISA?)",
  364. [19] "EB66; EB66 (PCI/ISA?)", // DEC?
  365. [20] "EB64P; EB64+ (PCI/ISA?)", // DEC?
  366. [21] "Alphabook1; Alphabook",
  367. [22] "DEC 4100; Rawhide (PCI/EISA)",
  368. [23] "DEC EV45/PBP; Lego",
  369. [24] "DEC 2100A/A500; Lynx",
  370. [26] "DEC AlphaPC 164", // only supported one: "EB164 (PCI/ISA)"
  371. [27] "DEC 1000A; Noritake",
  372. [28] "DEC AlphaVME/224; Cortex",
  373. [30] "DEC 550; Miata (PCI/ISA)",
  374. [32] "DEC EV56/PBP; Takara",
  375. [33] "DEC AlphaVME/320; Yukon (VME?)",
  376. [34] "DEC 6600; MonetGoldrush",
  377. // 200 and up is Alpha Processor Inc. machines
  378. // [201] "API UP1000; Nautilus",
  379. };
  380. static char *cpunames[] =
  381. {
  382. [1] "EV3",
  383. [2] "EV4: 21064",
  384. [3] "Simulation",
  385. [4] "LCA4: 2106[68]",
  386. [5] "EV5: 21164",
  387. [6] "EV45: 21064A",
  388. [7] "21164A", /* only supported one: EV56 */
  389. [8] "EV6: 21264",
  390. [9] "PCA256: 21164PC",
  391. };
  392. void
  393. cpuidprint(void)
  394. {
  395. int i, maj, min;
  396. Hwcpu *cpu;
  397. Hwdsr *dsr;
  398. char *s;
  399. print("\n");
  400. if (hwrpb->rev >= 6) {
  401. dsr = (Hwdsr*)((ulong)hwrpb + hwrpb->dsroff);
  402. s = (char*)dsr + dsr->sysnameoff + 8;
  403. print("%s\n", s);
  404. }
  405. else {
  406. s = "<unknown>";
  407. if (hwrpb->systype < nelem(sysnames))
  408. s = sysnames[hwrpb->systype];
  409. print("%s (%llux, %llux, %llux)\n", s, hwrpb->systype, hwrpb->sysvar, hwrpb->sysrev);
  410. }
  411. for (i = 0; i < hwrpb->ncpu; i++) {
  412. cpu = (Hwcpu*) ((ulong)hwrpb + hwrpb->cpuoff + i*hwrpb->cpulen);
  413. s = "<unknown>";
  414. maj = (ulong)cpu->cputype;
  415. min = (ulong)(cpu->cputype>>32);
  416. if (maj < nelem(cpunames))
  417. s = cpunames[maj];
  418. print("cpu%d: %s-%d (%d.%d, %llux, %llux)\n",
  419. i, s, min, maj, min, cpu->cpuvar, cpu->cpurev);
  420. }
  421. print("\n");
  422. }
  423. static long
  424. cputyperead(Chan*, void *a, long n, vlong offset)
  425. {
  426. char str[32], *cputype;
  427. ulong mhz, maj;
  428. Hwcpu *cpu;
  429. mhz = (m->cpuhz+999999)/1000000;
  430. cpu = (Hwcpu*) ((ulong)hwrpb + hwrpb->cpuoff); /* NB CPU 0 */
  431. cputype = "unknown";
  432. maj = (ulong)cpu->cputype;
  433. if (maj < nelem(cpunames))
  434. cputype = cpunames[maj];
  435. snprint(str, sizeof(str), "%s %lud\n", cputype, mhz);
  436. return readstr(offset, a, n, str);
  437. }
  438. void
  439. archinit(void)
  440. {
  441. PCArch **p;
  442. arch = 0;
  443. for(p = knownarch; *p; p++){
  444. if((*p)->ident && (*p)->ident() == 0){
  445. arch = *p;
  446. break;
  447. }
  448. }
  449. if(arch == 0)
  450. arch = &archgeneric;
  451. addarchfile("cputype", 0444, cputyperead, nil);
  452. }
  453. int
  454. pcmspecial(char *idstr, ISAConf *isa)
  455. {
  456. return (_pcmspecial != nil)? _pcmspecial(idstr, isa): -1;
  457. }
  458. void
  459. pcmspecialclose(int a)
  460. {
  461. if (_pcmspecialclose != nil)
  462. _pcmspecialclose(a);
  463. }