devarch.c 12 KB

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