devarch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "ureg.h"
  16. typedef struct IOMap IOMap;
  17. struct IOMap
  18. {
  19. IOMap *next;
  20. int reserved;
  21. char tag[13];
  22. uint32_t start;
  23. uint32_t end;
  24. };
  25. static struct
  26. {
  27. Lock l;
  28. IOMap *map;
  29. IOMap *free;
  30. IOMap maps[32]; // some initial free maps
  31. QLock ql; // lock for reading map
  32. } iomap;
  33. enum {
  34. Qdir = 0,
  35. Qioalloc = 1,
  36. Qiob,
  37. Qiow,
  38. Qiol,
  39. Qbase,
  40. Qmapram,
  41. Qmax = 32,
  42. };
  43. typedef int32_t Rdwrfn(Chan*, void*, int32_t, int64_t);
  44. static Rdwrfn *readfn[Qmax];
  45. static Rdwrfn *writefn[Qmax];
  46. static Dirtab archdir[Qmax] = {
  47. {".", { Qdir, 0, QTDIR }, 0, 0555},
  48. {"ioalloc", { Qioalloc, 0 }, 0, 0444},
  49. /* NOTE: kludge until we have real permissions. */
  50. {"iob", { Qiob, 0 }, 0, 0660 | 6},
  51. {"iow", { Qiow, 0 }, 0, 0660 | 6},
  52. {"iol", { Qiol, 0 }, 0, 0660 | 6},
  53. {"mapram", { Qmapram, 0 }, 0, 0444},
  54. };
  55. Lock archwlock; /* the lock is only for changing archdir */
  56. int narchdir = Qbase;
  57. /*
  58. * Add a file to the #P listing. Once added, you can't delete it.
  59. * You can't add a file with the same name as one already there,
  60. * and you get a pointer to the Dirtab entry so you can do things
  61. * like change the Qid version. Changing the Qid path is disallowed.
  62. */
  63. Dirtab*
  64. addarchfile(char *name, int perm, Rdwrfn *rdfn, Rdwrfn *wrfn)
  65. {
  66. int i;
  67. Dirtab d;
  68. Dirtab *dp;
  69. memset(&d, 0, sizeof d);
  70. strcpy(d.name, name);
  71. d.perm = perm;
  72. lock(&archwlock);
  73. if(narchdir >= Qmax){
  74. panic("addarchfile: %s: no more slots available: increase Qmax");
  75. }
  76. for(i=0; i<narchdir; i++)
  77. if(strcmp(archdir[i].name, name) == 0){
  78. unlock(&archwlock);
  79. return nil;
  80. }
  81. d.qid.path = narchdir;
  82. archdir[narchdir] = d;
  83. readfn[narchdir] = rdfn;
  84. writefn[narchdir] = wrfn;
  85. dp = &archdir[narchdir++];
  86. unlock(&archwlock);
  87. return dp;
  88. }
  89. void
  90. ioinit(void)
  91. {
  92. char *excluded;
  93. int i;
  94. for(i = 0; i < nelem(iomap.maps)-1; i++)
  95. iomap.maps[i].next = &iomap.maps[i+1];
  96. iomap.maps[i].next = nil;
  97. iomap.free = iomap.maps;
  98. /*
  99. * Someone needs to explain why this was here...
  100. */
  101. ioalloc(0x0fff, 1, 0, "dummy"); // i82557 is at 0x1000, the dummy
  102. // entry is needed for swappable devs.
  103. if (0) {// (excluded = getconf("ioexclude")) != nil) {
  104. char *s;
  105. s = excluded;
  106. while (s && *s != '\0' && *s != '\n') {
  107. char *ends;
  108. int io_s, io_e;
  109. io_s = (int)strtol(s, &ends, 0);
  110. if (ends == nil || ends == s || *ends != '-') {
  111. print("ioinit: cannot parse option string\n");
  112. break;
  113. }
  114. s = ++ends;
  115. io_e = (int)strtol(s, &ends, 0);
  116. if (ends && *ends == ',')
  117. *ends++ = '\0';
  118. s = ends;
  119. ioalloc(io_s, io_e - io_s + 1, 0, "pre-allocated");
  120. }
  121. }
  122. }
  123. // Reserve a range to be ioalloced later.
  124. // This is in particular useful for exchangable cards, such
  125. // as pcmcia and cardbus cards.
  126. int
  127. ioreserve(int n, int size, int align, char *tag)
  128. {
  129. IOMap *map, **l;
  130. int i, port;
  131. lock(&iomap.l);
  132. // find a free port above 0x400 and below 0x1000
  133. port = 0x400;
  134. for(l = &iomap.map; *l; l = &(*l)->next){
  135. map = *l;
  136. if (map->start < 0x400)
  137. continue;
  138. i = map->start - port;
  139. if(i > size)
  140. break;
  141. if(align > 0)
  142. port = ((port+align-1)/align)*align;
  143. else
  144. port = map->end;
  145. }
  146. if(*l == nil){
  147. unlock(&iomap.l);
  148. return -1;
  149. }
  150. map = iomap.free;
  151. if(map == nil){
  152. print("ioalloc: out of maps");
  153. unlock(&iomap.l);
  154. return port;
  155. }
  156. iomap.free = map->next;
  157. map->next = *l;
  158. map->start = port;
  159. map->end = port + size;
  160. map->reserved = 1;
  161. strncpy(map->tag, tag, sizeof(map->tag));
  162. map->tag[sizeof(map->tag)-1] = 0;
  163. *l = map;
  164. archdir[0].qid.vers++;
  165. unlock(&iomap.l);
  166. return map->start;
  167. }
  168. //
  169. // alloc some io port space and remember who it was
  170. // alloced to. if port < 0, find a free region.
  171. //
  172. int
  173. ioalloc(int port, int size, int align, char *tag)
  174. {
  175. IOMap *map, **l;
  176. int i;
  177. lock(&iomap.l);
  178. if(port < 0){
  179. // find a free port above 0x400 and below 0x1000
  180. port = 0x400;
  181. for(l = &iomap.map; *l; l = &(*l)->next){
  182. map = *l;
  183. if (map->start < 0x400)
  184. continue;
  185. i = map->start - port;
  186. if(i > size)
  187. break;
  188. if(align > 0)
  189. port = ((port+align-1)/align)*align;
  190. else
  191. port = map->end;
  192. }
  193. if(*l == nil){
  194. unlock(&iomap.l);
  195. return -1;
  196. }
  197. } else {
  198. // Only 64KB I/O space on the x86.
  199. if((port+size) > 0x10000){
  200. unlock(&iomap.l);
  201. return -1;
  202. }
  203. // see if the space clashes with previously allocated ports
  204. for(l = &iomap.map; *l; l = &(*l)->next){
  205. map = *l;
  206. if(map->end <= port)
  207. continue;
  208. if(map->reserved && map->start == port && map->end == port + size) {
  209. map->reserved = 0;
  210. unlock(&iomap.l);
  211. return map->start;
  212. }
  213. if(map->start >= port+size)
  214. break;
  215. unlock(&iomap.l);
  216. return -1;
  217. }
  218. }
  219. map = iomap.free;
  220. if(map == nil){
  221. print("ioalloc: out of maps");
  222. unlock(&iomap.l);
  223. return port;
  224. }
  225. iomap.free = map->next;
  226. map->next = *l;
  227. map->start = port;
  228. map->end = port + size;
  229. strncpy(map->tag, tag, sizeof(map->tag));
  230. map->tag[sizeof(map->tag)-1] = 0;
  231. *l = map;
  232. archdir[0].qid.vers++;
  233. unlock(&iomap.l);
  234. return map->start;
  235. }
  236. void
  237. iofree(int port)
  238. {
  239. IOMap *map, **l;
  240. lock(&iomap.l);
  241. for(l = &iomap.map; *l; l = &(*l)->next){
  242. if((*l)->start == port){
  243. map = *l;
  244. *l = map->next;
  245. map->next = iomap.free;
  246. iomap.free = map;
  247. break;
  248. }
  249. if((*l)->start > port)
  250. break;
  251. }
  252. archdir[0].qid.vers++;
  253. unlock(&iomap.l);
  254. }
  255. int
  256. iounused(int start, int end)
  257. {
  258. IOMap *map;
  259. for(map = iomap.map; map != nil; map = map->next){
  260. if((start >= map->start && start < map->end)
  261. || (start <= map->start && end > map->start))
  262. return 0;
  263. }
  264. return 1;
  265. }
  266. static void
  267. checkport(int start, int end)
  268. {
  269. /* standard vga regs are OK */
  270. if(start >= 0x2b0 && end <= 0x2df+1)
  271. return;
  272. if(start >= 0x3c0 && end <= 0x3da+1)
  273. return;
  274. if(iounused(start, end))
  275. return;
  276. error(Eperm);
  277. }
  278. static Chan*
  279. archattach(char* spec)
  280. {
  281. return devattach('P', spec);
  282. }
  283. Walkqid*
  284. archwalk(Chan* c, Chan *nc, char** name, int nname)
  285. {
  286. return devwalk(c, nc, name, nname, archdir, narchdir, devgen);
  287. }
  288. static int32_t
  289. archstat(Chan* c, uint8_t* dp, int32_t n)
  290. {
  291. return devstat(c, dp, n, archdir, narchdir, devgen);
  292. }
  293. static Chan*
  294. archopen(Chan* c, int omode)
  295. {
  296. return devopen(c, omode, archdir, narchdir, devgen);
  297. }
  298. static void
  299. archclose(Chan* c)
  300. {
  301. }
  302. enum
  303. {
  304. Linelen= 31,
  305. };
  306. static int32_t
  307. archread(Chan *c, void *a, int32_t n, int64_t offset)
  308. {
  309. char *buf, *p;
  310. int port;
  311. uint16_t *sp;
  312. uint32_t *lp;
  313. IOMap *map;
  314. Rdwrfn *fn;
  315. switch((uint32_t)c->qid.path){
  316. case Qdir:
  317. return devdirread(c, a, n, archdir, narchdir, devgen);
  318. case Qiob:
  319. port = offset;
  320. checkport(offset, offset+n);
  321. for(p = a; port < offset+n; port++)
  322. *p++ = inb(port);
  323. return n;
  324. case Qiow:
  325. if(n & 1)
  326. error(Ebadarg);
  327. checkport(offset, offset+n);
  328. sp = a;
  329. for(port = offset; port < offset+n; port += 2)
  330. *sp++ = ins(port);
  331. return n;
  332. case Qiol:
  333. if(n & 3)
  334. error(Ebadarg);
  335. checkport(offset, offset+n);
  336. lp = a;
  337. for(port = offset; port < offset+n; port += 4)
  338. *lp++ = inl(port);
  339. return n;
  340. case Qioalloc:
  341. break;
  342. default:
  343. if(c->qid.path < narchdir && (fn = readfn[c->qid.path]))
  344. return fn(c, a, n, offset);
  345. error(Eperm);
  346. break;
  347. }
  348. if((buf = malloc(n)) == nil)
  349. error(Enomem);
  350. p = buf;
  351. n = n/Linelen;
  352. offset = offset/Linelen;
  353. switch((uint32_t)c->qid.path){
  354. case Qioalloc:
  355. lock(&iomap.l);
  356. for(map = iomap.map; n > 0 && map != nil; map = map->next){
  357. if(offset-- > 0)
  358. continue;
  359. sprint(p, "%#8lx %#8lx %-12.12s\n", map->start, map->end-1, map->tag);
  360. p += Linelen;
  361. n--;
  362. }
  363. unlock(&iomap.l);
  364. break;
  365. case Qmapram:
  366. /* shit */
  367. #ifdef NOTYET
  368. for(mp = rmapram.map; mp->size; mp++){
  369. /*
  370. * Up to MemMinMiB is already set up.
  371. */
  372. if(mp->addr < MemMinMiB*MiB){
  373. if(mp->addr+mp->size <= MemMinMiB*MiB)
  374. continue;
  375. pa = MemMinMiB*MiB;
  376. size = mp->size - MemMinMiB*MiB-mp->addr;
  377. }
  378. else{
  379. pa = mp->addr;
  380. size = mp->size;
  381. }
  382. }
  383. #endif
  384. error("Not yet");
  385. break;
  386. }
  387. n = p - buf;
  388. memmove(a, buf, n);
  389. free(buf);
  390. return n;
  391. }
  392. static int32_t
  393. archwrite(Chan *c, void *a, int32_t n, int64_t offset)
  394. {
  395. char *p;
  396. int port;
  397. uint16_t *sp;
  398. uint32_t *lp;
  399. Rdwrfn *fn;
  400. switch((uint32_t)c->qid.path){
  401. case Qiob:
  402. p = a;
  403. checkport(offset, offset+n);
  404. for(port = offset; port < offset+n; port++)
  405. outb(port, *p++);
  406. return n;
  407. case Qiow:
  408. if(n & 1)
  409. error(Ebadarg);
  410. checkport(offset, offset+n);
  411. sp = a;
  412. for(port = offset; port < offset+n; port += 2)
  413. outs(port, *sp++);
  414. return n;
  415. case Qiol:
  416. if(n & 3)
  417. error(Ebadarg);
  418. checkport(offset, offset+n);
  419. lp = a;
  420. for(port = offset; port < offset+n; port += 4)
  421. outl(port, *lp++);
  422. return n;
  423. default:
  424. if(c->qid.path < narchdir && (fn = writefn[c->qid.path]))
  425. return fn(c, a, n, offset);
  426. error(Eperm);
  427. break;
  428. }
  429. return 0;
  430. }
  431. Dev archdevtab = {
  432. .dc = 'P',
  433. .name = "arch",
  434. .reset = devreset,
  435. .init = devinit,
  436. .shutdown = devshutdown,
  437. .attach = archattach,
  438. .walk = archwalk,
  439. .stat = archstat,
  440. .open = archopen,
  441. .create = devcreate,
  442. .close = archclose,
  443. .read = archread,
  444. .bread = devbread,
  445. .write = archwrite,
  446. .bwrite = devbwrite,
  447. .remove = devremove,
  448. .wstat = devwstat,
  449. };
  450. /*
  451. */
  452. void
  453. nop(void)
  454. {
  455. }
  456. void (*coherence)(void) = mfence;
  457. static int32_t
  458. cputyperead(Chan* c, void *a, int32_t n, int64_t off)
  459. {
  460. char buf[512], *s, *e;
  461. int i, k;
  462. e = buf+sizeof buf;
  463. s = seprint(buf, e, "%s %u\n", "AMD64", machp()->cpumhz);
  464. k = machp()->CPU.ncpuinfoe - machp()->CPU.ncpuinfos;
  465. if(k > 4)
  466. k = 4;
  467. for(i = 0; i < k; i++)
  468. s = seprint(s, e, "%#8.8x %#8.8x %#8.8x %#8.8x\n",
  469. machp()->CPU.cpuinfo[i][0], machp()->CPU.cpuinfo[i][1],
  470. machp()->CPU.cpuinfo[i][2], machp()->CPU.cpuinfo[i][3]);
  471. return readstr(off, a, n, buf);
  472. }
  473. void
  474. archinit(void)
  475. {
  476. addarchfile("cputype", 0444, cputyperead, nil);
  477. addarchfile("numcores", 0444, numcoresread, nil);
  478. addarchfile("mtags", 0444, mtagsread, nil);
  479. }
  480. void
  481. archreset(void)
  482. {
  483. int i;
  484. /*
  485. * And sometimes there is no keyboard...
  486. *
  487. * The reset register (0xcf9) is usually in one of the bridge
  488. * chips. The actual location and sequence could be extracted from
  489. * ACPI but why bother, this is the end of the line anyway.
  490. print("Takes a licking and keeps on ticking...\n");
  491. */
  492. i = inb(0xcf9); /* ICHx reset control */
  493. i &= 0x06;
  494. outb(0xcf9, i|0x02); /* SYS_RST */
  495. millidelay(1);
  496. outb(0xcf9, i|0x06); /* RST_CPU transition */
  497. for(;;)
  498. ;
  499. }
  500. /*
  501. * return value and speed of timer
  502. */
  503. uint64_t
  504. fastticks(uint64_t* hz)
  505. {
  506. if(hz != nil)
  507. *hz = machp()->cpuhz;
  508. return rdtsc();
  509. }
  510. uint32_t
  511. ms(void)
  512. {
  513. return fastticks2us(rdtsc());
  514. }
  515. /*
  516. * set next timer interrupt
  517. */
  518. void
  519. timerset(uint64_t x)
  520. {
  521. extern void apictimerset(uint64_t);
  522. apictimerset(x);
  523. }
  524. void
  525. cycles(uint64_t* t)
  526. {
  527. *t = rdtsc();
  528. }
  529. void
  530. delay(int millisecs)
  531. {
  532. uint64_t r, t;
  533. if(millisecs <= 0)
  534. millisecs = 1;
  535. r = rdtsc();
  536. for(t = r + (sys->cyclefreq*millisecs)/1000ull; r < t; r = rdtsc())
  537. ;
  538. }
  539. /*
  540. * performance measurement ticks. must be low overhead.
  541. * doesn't have to count over a second.
  542. */
  543. uint32_t
  544. perfticks(void)
  545. {
  546. uint64_t x;
  547. // if(m->havetsc)
  548. cycles(&x);
  549. // else
  550. // x = 0;
  551. return x;
  552. }