io.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <libc.h>
  11. #include <bio.h>
  12. #include "pci.h"
  13. #include "vga.h"
  14. int curprintindex;
  15. static int iobfd = -1;
  16. static int iowfd = -1;
  17. static int iolfd = -1;
  18. static int biosfd = -1;
  19. static uint32_t biosoffset = 0;
  20. enum {
  21. Nctlchar = 256,
  22. Nattr = 16,
  23. };
  24. static int ctlfd = -1;
  25. static char ctlbuf[Nctlchar];
  26. static int ctlclean;
  27. static struct {
  28. char* attr;
  29. char* val;
  30. } attr[Nattr];
  31. static int
  32. devopen(char* device, int mode)
  33. {
  34. int fd;
  35. if((fd = open(device, mode)) < 0)
  36. error("devopen(%s, %d): %r\n", device, mode);
  37. return fd;
  38. }
  39. uint8_t
  40. inportb(int32_t port)
  41. {
  42. uint8_t data;
  43. if(iobfd == -1)
  44. iobfd = devopen("#P/iob", ORDWR);
  45. if(pread(iobfd, &data, sizeof(data), port) != sizeof(data))
  46. error("inportb(0x%4.4lx): %r\n", port);
  47. return data;
  48. }
  49. void
  50. outportb(int32_t port, uint8_t data)
  51. {
  52. if(iobfd == -1)
  53. iobfd = devopen("#P/iob", ORDWR);
  54. if(pwrite(iobfd, &data, sizeof(data), port) != sizeof(data))
  55. error("outportb(0x%4.4lx, 0x%2.2uX): %r\n", port, data);
  56. }
  57. uint16_t
  58. inportw(int32_t port)
  59. {
  60. uint16_t data;
  61. if(iowfd == -1)
  62. iowfd = devopen("#P/iow", ORDWR);
  63. if(pread(iowfd, &data, sizeof(data), port) != sizeof(data))
  64. error("inportw(0x%4.4lx): %r\n", port);
  65. return data;
  66. }
  67. void
  68. outportw(int32_t port, uint16_t data)
  69. {
  70. if(iowfd == -1)
  71. iowfd = devopen("#P/iow", ORDWR);
  72. if(pwrite(iowfd, &data, sizeof(data), port) != sizeof(data))
  73. error("outportw(0x%4.4lx, 0x%2.2uX): %r\n", port, data);
  74. }
  75. uint32_t
  76. inportl(int32_t port)
  77. {
  78. uint32_t data;
  79. if(iolfd == -1)
  80. iolfd = devopen("#P/iol", ORDWR);
  81. if(pread(iolfd, &data, sizeof(data), port) != sizeof(data))
  82. error("inportl(0x%4.4lx): %r\n", port);
  83. return data;
  84. }
  85. void
  86. outportl(int32_t port, uint32_t data)
  87. {
  88. if(iolfd == -1)
  89. iolfd = devopen("#P/iol", ORDWR);
  90. if(pwrite(iolfd, &data, sizeof(data), port) != sizeof(data))
  91. error("outportl(0x%4.4lx, 0x%2.2luX): %r\n", port, data);
  92. }
  93. static void
  94. vgactlinit(void)
  95. {
  96. int nattr;
  97. char *nl, *p, *vp;
  98. if(ctlclean)
  99. return;
  100. if(ctlfd == -1){
  101. ctlfd = devopen("#v/vgactl", ORDWR);
  102. memset(attr, 0, sizeof(attr));
  103. }
  104. seek(ctlfd, 0, 0);
  105. nattr = read(ctlfd, ctlbuf, Nctlchar-1);
  106. if(nattr < 0)
  107. error("vgactlr: read: %r\n");
  108. ctlbuf[nattr] = 0;
  109. nattr = 0;
  110. vp = ctlbuf;
  111. for(nl = strchr(ctlbuf, '\n'); nl; nl = strchr(nl, '\n')){
  112. *nl = '\0';
  113. if(p = strchr(vp, ' ')){
  114. *p++ = '\0';
  115. attr[nattr].attr = vp;
  116. if(*p == '\0')
  117. error("vgactlr: bad format: <%s>\n", vp);
  118. attr[nattr].val = p;
  119. }
  120. else
  121. error("vgactlr: bad format: <%s>\n", vp);
  122. if(++nattr >= Nattr-2)
  123. error("vgactlr: too many attributes: %d\n", nattr);
  124. attr[nattr].attr = 0;
  125. vp = ++nl;
  126. }
  127. ctlclean = 1;
  128. }
  129. char*
  130. vgactlr(char* a, char* v)
  131. {
  132. int i;
  133. trace("vgactlr: look for %s\n", a);
  134. vgactlinit();
  135. for(i = 0; attr[i].attr; i++){
  136. if(strcmp(attr[i].attr, a) == 0){
  137. strcpy(v, attr[i].val);
  138. trace("vgactlr: value %s\n", v);
  139. return v;
  140. }
  141. }
  142. trace("vgactlr: %s not found\n", a);
  143. return 0;
  144. }
  145. void
  146. vgactlw(char* attr, char* val)
  147. {
  148. int len;
  149. char buf[128];
  150. if(ctlfd == -1)
  151. ctlfd = devopen("#v/vgactl", ORDWR);
  152. seek(ctlfd, 0, 0);
  153. len = sprint(buf, "%s %s", attr, val);
  154. trace("+vgactlw %s\n", buf);
  155. if(write(ctlfd, buf, len) != len)
  156. error("vgactlw: <%s>: %r\n", buf);
  157. trace("-vgactlw %s\n", buf);
  158. ctlclean = 0;
  159. }
  160. void
  161. setpalette(int p, int r, int g, int b)
  162. {
  163. vgao(PaddrW, p);
  164. vgao(Pdata, r);
  165. vgao(Pdata, g);
  166. vgao(Pdata, b);
  167. }
  168. static int32_t
  169. doreadbios(char* buf, int32_t len, int32_t offset)
  170. {
  171. char file[64];
  172. if(biosfd == -1){
  173. biosfd = open("#v/vgabios", OREAD);
  174. biosoffset = 0;
  175. }
  176. if(biosfd == -1){
  177. snprint(file, sizeof file, "#p/%d/mem", getpid());
  178. biosfd = devopen(file, OREAD);
  179. biosoffset = 0x80000000;
  180. }
  181. if(biosfd == -1)
  182. return -1;
  183. seek(biosfd, biosoffset+offset, 0);
  184. return read(biosfd, buf, len);
  185. }
  186. char*
  187. readbios(int32_t len, int32_t offset)
  188. {
  189. static char bios[0x10000];
  190. static int32_t biosoffset;
  191. static int32_t bioslen;
  192. int n;
  193. if(biosoffset <= offset && offset+len <= biosoffset+bioslen)
  194. return bios+(offset - biosoffset);
  195. if(len > sizeof(bios))
  196. error("enormous bios len %ld at %lx\n", len, offset);
  197. n = doreadbios(bios, sizeof(bios), offset);
  198. if(n < len)
  199. error("short bios read %ld at %lx got %d\n", len,offset, n);
  200. biosoffset = offset;
  201. bioslen = n;
  202. return bios;
  203. }
  204. void
  205. dumpbios(int32_t size)
  206. {
  207. uint8_t *buf;
  208. int32_t offset;
  209. int i, n;
  210. char c;
  211. buf = alloc(size);
  212. offset = 0xC0000;
  213. if(doreadbios((char*)buf, size, offset) != size)
  214. error("short bios read in dumpbios\n");
  215. if(buf[0] != 0x55 || buf[1] != 0xAA){
  216. offset = 0xE0000;
  217. if(doreadbios((char*)buf, size, offset) != size)
  218. error("short bios read in dumpbios\n");
  219. if(buf[0] != 0x55 || buf[1] != 0xAA){
  220. free(buf);
  221. return;
  222. }
  223. }
  224. for(i = 0; i < size; i += 16){
  225. Bprint(&stdout, "0x%lX", offset+i);
  226. for(n = 0; n < 16; n++)
  227. Bprint(&stdout, " %2.2uX", buf[i+n]);
  228. Bprint(&stdout, " ");
  229. for(n = 0; n < 16; n++){
  230. c = buf[i+n];
  231. if(c < 0x20 || c >= 0x7F)
  232. c = '.';
  233. Bprint(&stdout, "%c", c);
  234. }
  235. Bprint(&stdout, "\n");
  236. }
  237. free(buf);
  238. }
  239. void*
  240. alloc(uint32_t nbytes)
  241. {
  242. void *v;
  243. if((v = malloc(nbytes)) == 0)
  244. error("alloc: %lu bytes - %r\n", nbytes);
  245. return memset(v, 0, nbytes);
  246. }
  247. void
  248. printitem(char* ctlr, char* item)
  249. {
  250. int n;
  251. if(curprintindex){
  252. curprintindex = 0;
  253. Bprint(&stdout, "\n");
  254. }
  255. n = 0;
  256. if(ctlr && *ctlr)
  257. n = Bprint(&stdout, "%s ", ctlr);
  258. Bprint(&stdout, "%-*s", 20-n, item);
  259. }
  260. void
  261. printreg(uint32_t data)
  262. {
  263. int width;
  264. width = 3;
  265. if((curprintindex % 16) == 0 && curprintindex){
  266. Bprint(&stdout, "\n");
  267. curprintindex = 0;
  268. width = 23;
  269. }
  270. if(curprintindex == 8)
  271. Bprint(&stdout, " -");
  272. Bprint(&stdout, "%*.2luX", width, data);
  273. curprintindex++;
  274. }
  275. static char *flagname[32] = {
  276. [0x00] = "Fsnarf",
  277. [0x01] = "Foptions",
  278. [0x02] = "Finit",
  279. [0x03] = "Fload",
  280. [0x04] = "Fdump",
  281. [0x08] = "Hpclk2x8",
  282. [0x09] = "Upclk2x8",
  283. [0x0A] = "Henhanced",
  284. [0x0B] = "Uenhanced",
  285. [0x0C] = "Hpvram",
  286. [0x0D] = "Upvram",
  287. [0x0E] = "Hextsid",
  288. [0x0F] = "Uextsid",
  289. [0x10] = "Hclk2",
  290. [0x11] = "Uclk2",
  291. [0x12] = "Hlinear",
  292. [0x13] = "Ulinear",
  293. [0x14] = "Hclkdiv",
  294. [0x15] = "Uclkdiv",
  295. [0x16] = "Hsid32",
  296. };
  297. void
  298. printflag(uint32_t flag)
  299. {
  300. int i;
  301. char first;
  302. first = ' ';
  303. for(i = 31; i >= 0; i--){
  304. if((flag & (1<<i)) == 0)
  305. continue;
  306. if(flagname[i])
  307. Bprint(&stdout, "%c%s", first, flagname[i]);
  308. else
  309. Bprint(&stdout, "%c0x%x", first, 1<<i);
  310. first = '|';
  311. }
  312. }