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;
  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. "iob", { Qiob, 0 }, 0, 0660,
  50. "iow", { Qiow, 0 }, 0, 0660,
  51. "iol", { Qiol, 0 }, 0, 0660,
  52. "mapram", { Qmapram, 0 }, 0, 0444,
  53. };
  54. Lock archwlock; /* the lock is only for changing archdir */
  55. int narchdir = Qbase;
  56. /*
  57. * Add a file to the #P listing. Once added, you can't delete it.
  58. * You can't add a file with the same name as one already there,
  59. * and you get a pointer to the Dirtab entry so you can do things
  60. * like change the Qid version. Changing the Qid path is disallowed.
  61. */
  62. Dirtab*
  63. addarchfile(char *name, int perm, Rdwrfn *rdfn, Rdwrfn *wrfn)
  64. {
  65. int i;
  66. Dirtab d;
  67. Dirtab *dp;
  68. memset(&d, 0, sizeof d);
  69. strcpy(d.name, name);
  70. d.perm = perm;
  71. lock(&archwlock);
  72. if(narchdir >= Qmax){
  73. unlock(&archwlock);
  74. return nil;
  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);
  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);
  148. return -1;
  149. }
  150. map = iomap.free;
  151. if(map == nil){
  152. print("ioalloc: out of maps");
  153. unlock(&iomap);
  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);
  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);
  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);
  195. return -1;
  196. }
  197. } else {
  198. // Only 64KB I/O space on the x86.
  199. if((port+size) > 0x10000){
  200. unlock(&iomap);
  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);
  211. return map->start;
  212. }
  213. if(map->start >= port+size)
  214. break;
  215. unlock(&iomap);
  216. return -1;
  217. }
  218. }
  219. map = iomap.free;
  220. if(map == nil){
  221. print("ioalloc: out of maps");
  222. unlock(&iomap);
  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);
  234. return map->start;
  235. }
  236. void
  237. iofree(int port)
  238. {
  239. IOMap *map, **l;
  240. lock(&iomap);
  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);
  254. }
  255. int
  256. iounused(int start, int end)
  257. {
  258. IOMap *map;
  259. for(map = iomap.map; map; 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);
  356. for(map = iomap.map; n > 0 && map != nil; map = map->next){
  357. if(offset-- > 0)
  358. continue;
  359. sprint(p, "%#8lux %#8lux %-12.12s\n", map->start, map->end-1, map->tag);
  360. p += Linelen;
  361. n--;
  362. }
  363. unlock(&iomap);
  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. #endif
  383. error("Not yet");
  384. break;
  385. }
  386. n = p - buf;
  387. memmove(a, buf, n);
  388. free(buf);
  389. return n;
  390. }
  391. static int32_t
  392. archwrite(Chan *c, void *a, int32_t n, int64_t offset)
  393. {
  394. char *p;
  395. int port;
  396. uint16_t *sp;
  397. uint32_t *lp;
  398. Rdwrfn *fn;
  399. switch((uint32_t)c->qid.path){
  400. case Qiob:
  401. p = a;
  402. checkport(offset, offset+n);
  403. for(port = offset; port < offset+n; port++)
  404. outb(port, *p++);
  405. return n;
  406. case Qiow:
  407. if(n & 1)
  408. error(Ebadarg);
  409. checkport(offset, offset+n);
  410. sp = a;
  411. for(port = offset; port < offset+n; port += 2)
  412. outs(port, *sp++);
  413. return n;
  414. case Qiol:
  415. if(n & 3)
  416. error(Ebadarg);
  417. checkport(offset, offset+n);
  418. lp = a;
  419. for(port = offset; port < offset+n; port += 4)
  420. outl(port, *lp++);
  421. return n;
  422. default:
  423. if(c->qid.path < narchdir && (fn = writefn[c->qid.path]))
  424. return fn(c, a, n, offset);
  425. error(Eperm);
  426. break;
  427. }
  428. return 0;
  429. }
  430. Dev archdevtab = {
  431. 'P',
  432. "arch",
  433. devreset,
  434. devinit,
  435. devshutdown,
  436. archattach,
  437. archwalk,
  438. archstat,
  439. archopen,
  440. devcreate,
  441. archclose,
  442. archread,
  443. devbread,
  444. archwrite,
  445. devbwrite,
  446. devremove,
  447. devwstat,
  448. };
  449. /*
  450. */
  451. void
  452. nop(void)
  453. {
  454. }
  455. void (*coherence)(void) = mfence;
  456. static int32_t
  457. cputyperead(Chan* c, void *a, int32_t n, int64_t off)
  458. {
  459. Proc *up = externup();
  460. char buf[512], *s, *e;
  461. int i, k;
  462. e = buf+sizeof buf;
  463. s = seprint(buf, e, "%s %ud\n", "AMD64", m->cpumhz);
  464. k = m->ncpuinfoe - m->ncpuinfos;
  465. if(k > 4)
  466. k = 4;
  467. for(i = 0; i < k; i++)
  468. s = seprint(s, e, "%#8.8ux %#8.8ux %#8.8ux %#8.8ux\n",
  469. m->cpuinfo[i][0], m->cpuinfo[i][1],
  470. m->cpuinfo[i][2], m->cpuinfo[i][3]);
  471. return readstr(off, a, n, buf);
  472. }
  473. void
  474. archinit(void)
  475. {
  476. addarchfile("cputype", 0444, cputyperead, nil);
  477. }
  478. void
  479. archreset(void)
  480. {
  481. int i;
  482. /*
  483. * And sometimes there is no keyboard...
  484. *
  485. * The reset register (0xcf9) is usually in one of the bridge
  486. * chips. The actual location and sequence could be extracted from
  487. * ACPI but why bother, this is the end of the line anyway.
  488. print("Takes a licking and keeps on ticking...\n");
  489. */
  490. i = inb(0xcf9); /* ICHx reset control */
  491. i &= 0x06;
  492. outb(0xcf9, i|0x02); /* SYS_RST */
  493. millidelay(1);
  494. outb(0xcf9, i|0x06); /* RST_CPU transition */
  495. for(;;)
  496. ;
  497. }
  498. /*
  499. * return value and speed of timer
  500. */
  501. uint64_t
  502. fastticks(uint64_t* hz)
  503. {
  504. Proc *up = externup();
  505. if(hz != nil)
  506. *hz = m->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. Proc *up = externup();
  532. uint64_t r, t;
  533. if(millisecs <= 0)
  534. millisecs = 1;
  535. r = rdtsc();
  536. for(t = r + m->cpumhz*1000ull*millisecs; 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. }