devarch.c 11 KB

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