devarch.c 10 KB

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