vgas3.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "../port/error.h"
  8. #define Image IMAGE
  9. #include <draw.h>
  10. #include <memdraw.h>
  11. #include <cursor.h>
  12. #include "screen.h"
  13. enum {
  14. PCIS3 = 0x5333, /* PCI VID */
  15. SAVAGE3D = 0x8A20, /* PCI DID */
  16. SAVAGE3DMV = 0x8A21,
  17. SAVAGE4 = 0x8A22,
  18. PROSAVAGEP = 0x8A25,
  19. PROSAVAGEK = 0x8A26,
  20. SAVAGEMXMV = 0x8C10,
  21. SAVAGEMX = 0x8C11,
  22. SAVAGEIXMV = 0x8C12,
  23. SAVAGEIX = 0x8C13,
  24. SUPERSAVAGEIXC16 = 0x8C2E,
  25. SAVAGE2000 = 0x9102,
  26. VIRGE = 0x5631,
  27. VIRGEGX2 = 0x8A10,
  28. VIRGEDXGX = 0x8A01,
  29. VIRGEVX = 0x883D,
  30. VIRGEMX = 0x8C01,
  31. VIRGEMXP = 0x8C03,
  32. AURORA64VPLUS = 0x8812,
  33. };
  34. static int
  35. s3pageset(VGAscr* scr, int page)
  36. {
  37. uchar crt35, crt51;
  38. int opage;
  39. crt35 = vgaxi(Crtx, 0x35);
  40. if(scr->gscreen->depth >= 8){
  41. /*
  42. * The S3 registers need to be unlocked for this.
  43. * Let's hope they are already:
  44. * vgaxo(Crtx, 0x38, 0x48);
  45. * vgaxo(Crtx, 0x39, 0xA0);
  46. *
  47. * The page is 6 bits, the lower 4 bits in Crt35<3:0>,
  48. * the upper 2 in Crt51<3:2>.
  49. */
  50. vgaxo(Crtx, 0x35, page & 0x0F);
  51. crt51 = vgaxi(Crtx, 0x51);
  52. vgaxo(Crtx, 0x51, (crt51 & ~0x0C)|((page & 0x30)>>2));
  53. opage = ((crt51 & 0x0C)<<2)|(crt35 & 0x0F);
  54. }
  55. else{
  56. vgaxo(Crtx, 0x35, (page<<2) & 0x0C);
  57. opage = (crt35>>2) & 0x03;
  58. }
  59. return opage;
  60. }
  61. static void
  62. s3page(VGAscr* scr, int page)
  63. {
  64. int id;
  65. id = (vgaxi(Crtx, 0x2D)<<8)|vgaxi(Crtx, 0x2E);
  66. switch(id){
  67. case VIRGEGX2:
  68. break;
  69. default:
  70. lock(&scr->devlock);
  71. s3pageset(scr, page);
  72. unlock(&scr->devlock);
  73. break;
  74. }
  75. }
  76. static ulong
  77. s3linear(VGAscr* scr, int* size, int* align)
  78. {
  79. char *mmioname;
  80. ulong aperture, oaperture, mmiobase, mmiosize;
  81. int i, id, j, osize, oapsize, wasupamem;
  82. Pcidev *p;
  83. osize = *size;
  84. oaperture = scr->aperture;
  85. oapsize = scr->apsize;
  86. wasupamem = scr->isupamem;
  87. mmiosize = 0;
  88. mmiobase = 0;
  89. mmioname = nil;
  90. /*
  91. * S3 makes cards other than display controllers, so
  92. * look for the first S3 display controller (device class 3)
  93. * and not one of their sound cards.
  94. */
  95. p = nil;
  96. while(p = pcimatch(p, PCIS3, 0)){
  97. if(p->ccrb == 0x03)
  98. break;
  99. }
  100. if(p != nil){
  101. for(i=0; i<nelem(p->mem); i++){
  102. if(p->mem[i].size >= *size
  103. && ((p->mem[i].bar & ~0x0F) & (*align-1)) == 0)
  104. break;
  105. }
  106. if(i >= nelem(p->mem)){
  107. print("vgas3: aperture not found\n");
  108. return 0;
  109. }
  110. aperture = p->mem[i].bar & ~0x0F;
  111. *size = p->mem[i].size;
  112. id = (vgaxi(Crtx, 0x2D)<<8)|vgaxi(Crtx, 0x2E);
  113. switch(id){ /* find mmio */
  114. case SAVAGE4:
  115. case PROSAVAGEP:
  116. case PROSAVAGEK:
  117. case SUPERSAVAGEIXC16:
  118. /*
  119. * We could assume that the MMIO registers
  120. * will be in the screen segment and just use
  121. * that, but PCI software is allowed to move them
  122. * if it feels like it, so we look for an aperture of
  123. * the right size; only the first 512k actually means
  124. * anything. The S3 engineers overestimated how
  125. * much space they would need in the first design.
  126. */
  127. for(j=0; j<nelem(p->mem); j++){
  128. if(i == j)
  129. continue;
  130. if(p->mem[j].size==512*1024 || p->mem[j].size==16*1024*1024){
  131. mmiobase = p->mem[j].bar & ~0x0F;
  132. mmiosize = 512*1024;
  133. scr->mmio = (ulong*)upamalloc(mmiobase, mmiosize, 0);
  134. mmioname = "savagemmio";
  135. break;
  136. }
  137. }
  138. if(mmiosize == 0){
  139. print("savage4: mmio not found\n");
  140. return 0;
  141. }
  142. }
  143. }else
  144. aperture = 0;
  145. if(wasupamem)
  146. upafree(oaperture, oapsize);
  147. scr->isupamem = 0;
  148. aperture = upamalloc(aperture, *size, *align);
  149. if(aperture == 0){
  150. if(wasupamem && upamalloc(oaperture, oapsize, 0))
  151. scr->isupamem = 1;
  152. }
  153. else
  154. scr->isupamem = 1;
  155. if(oaperture && oaperture != aperture)
  156. print("warning (BUG): redefinition of aperture does not change s3screen segment\n");
  157. addvgaseg("s3screen", aperture, osize);
  158. if(mmiosize)
  159. addvgaseg(mmioname, mmiobase, mmiosize);
  160. return aperture;
  161. }
  162. static void
  163. s3vsyncactive(void)
  164. {
  165. /*
  166. * Hardware cursor information is fetched from display memory
  167. * during the horizontal blank active time. The 80x chips may hang
  168. * if the cursor is turned on or off during this period.
  169. */
  170. while((vgai(Status1) & 0x08) == 0)
  171. ;
  172. }
  173. static void
  174. s3disable(VGAscr*)
  175. {
  176. uchar crt45;
  177. /*
  178. * Turn cursor off.
  179. */
  180. crt45 = vgaxi(Crtx, 0x45) & 0xFE;
  181. s3vsyncactive();
  182. vgaxo(Crtx, 0x45, crt45);
  183. }
  184. static void
  185. s3load(VGAscr* scr, Cursor* curs)
  186. {
  187. uchar *p;
  188. int id, dolock, opage, x, y;
  189. /*
  190. * Disable the cursor and
  191. * set the pointer to the two planes.
  192. */
  193. s3disable(scr);
  194. opage = 0;
  195. p = KADDR(scr->aperture);
  196. id = (vgaxi(Crtx, 0x2D)<<8)|vgaxi(Crtx, 0x2E);
  197. switch(id){
  198. case VIRGE:
  199. case VIRGEDXGX:
  200. case VIRGEGX2:
  201. case VIRGEVX:
  202. case SAVAGEMXMV:
  203. case SAVAGEIXMV:
  204. case SAVAGE4:
  205. case PROSAVAGEP:
  206. case PROSAVAGEK:
  207. case SUPERSAVAGEIXC16:
  208. dolock = 0;
  209. p += scr->storage;
  210. break;
  211. default:
  212. dolock = 1;
  213. lock(&scr->devlock);
  214. opage = s3pageset(scr, scr->storage>>16);
  215. p += (scr->storage & 0xFFFF);
  216. break;
  217. }
  218. /*
  219. * The cursor is set in Microsoft Windows format (the ViRGE/GX2 doesn't
  220. * support the X11 format) which gives the following truth table:
  221. * and xor colour
  222. * 0 0 background colour
  223. * 0 1 foreground colour
  224. * 1 0 current screen pixel
  225. * 1 1 NOT current screen pixel
  226. * Put the cursor into the top-left of the 64x64 array.
  227. *
  228. * The cursor pattern in memory is interleaved words of
  229. * AND and XOR patterns.
  230. */
  231. for(y = 0; y < 64; y++){
  232. for(x = 0; x < 64/8; x += 2){
  233. if(x < 16/8 && y < 16){
  234. *p++ = ~(curs->clr[2*y + x]|curs->set[2*y + x]);
  235. *p++ = ~(curs->clr[2*y + x+1]|curs->set[2*y + x+1]);
  236. *p++ = curs->set[2*y + x];
  237. *p++ = curs->set[2*y + x+1];
  238. }
  239. else {
  240. *p++ = 0xFF;
  241. *p++ = 0xFF;
  242. *p++ = 0x00;
  243. *p++ = 0x00;
  244. }
  245. }
  246. }
  247. if(dolock){
  248. s3pageset(scr, opage);
  249. unlock(&scr->devlock);
  250. }
  251. /*
  252. * Save the cursor hotpoint and enable the cursor.
  253. */
  254. scr->offset = curs->offset;
  255. s3vsyncactive();
  256. vgaxo(Crtx, 0x45, 0x01);
  257. }
  258. static int
  259. s3move(VGAscr* scr, Point p)
  260. {
  261. int x, xo, y, yo;
  262. /*
  263. * Mustn't position the cursor offscreen even partially,
  264. * or it disappears. Therefore, if x or y is -ve, adjust the
  265. * cursor offset instead.
  266. * There seems to be a bug in that if the offset is 1, the
  267. * cursor doesn't disappear off the left edge properly, so
  268. * round it up to be even.
  269. */
  270. if((x = p.x+scr->offset.x) < 0){
  271. xo = -x;
  272. xo = ((xo+1)/2)*2;
  273. x = 0;
  274. }
  275. else
  276. xo = 0;
  277. if((y = p.y+scr->offset.y) < 0){
  278. yo = -y;
  279. y = 0;
  280. }
  281. else
  282. yo = 0;
  283. vgaxo(Crtx, 0x46, (x>>8) & 0x07);
  284. vgaxo(Crtx, 0x47, x & 0xFF);
  285. vgaxo(Crtx, 0x49, y & 0xFF);
  286. vgaxo(Crtx, 0x4E, xo);
  287. vgaxo(Crtx, 0x4F, yo);
  288. vgaxo(Crtx, 0x48, (y>>8) & 0x07);
  289. return 0;
  290. }
  291. static void
  292. s3enable(VGAscr* scr)
  293. {
  294. int i;
  295. ulong storage;
  296. s3disable(scr);
  297. /*
  298. * Cursor colours. Set both the CR0[EF] and the colour
  299. * stack in case we are using a 16-bit RAMDAC.
  300. */
  301. vgaxo(Crtx, 0x0E, Pwhite);
  302. vgaxo(Crtx, 0x0F, Pblack);
  303. vgaxi(Crtx, 0x45);
  304. for(i = 0; i < 3; i++)
  305. vgaxo(Crtx, 0x4A, Pblack);
  306. vgaxi(Crtx, 0x45);
  307. for(i = 0; i < 3; i++)
  308. vgaxo(Crtx, 0x4B, Pwhite);
  309. /*
  310. * Find a place for the cursor data in display memory.
  311. * Must be on a 1024-byte boundary.
  312. */
  313. storage = (scr->gscreen->width*BY2WD*scr->gscreen->r.max.y+1023)/1024;
  314. vgaxo(Crtx, 0x4C, storage>>8);
  315. vgaxo(Crtx, 0x4D, storage & 0xFF);
  316. storage *= 1024;
  317. scr->storage = storage;
  318. /*
  319. * Load, locate and enable the cursor
  320. * in Microsoft Windows format.
  321. */
  322. s3load(scr, &arrow);
  323. s3move(scr, ZP);
  324. vgaxo(Crtx, 0x55, vgaxi(Crtx, 0x55) & ~0x10);
  325. s3vsyncactive();
  326. vgaxo(Crtx, 0x45, 0x01);
  327. }
  328. /*
  329. * The manual gives byte offsets, but we want ulong offsets, hence /4.
  330. */
  331. enum {
  332. SrcBase = 0xA4D4/4,
  333. DstBase = 0xA4D8/4,
  334. Stride = 0xA4E4/4,
  335. FgrdData = 0xA4F4/4,
  336. WidthHeight = 0xA504/4,
  337. SrcXY = 0xA508/4,
  338. DestXY = 0xA50C/4,
  339. Command = 0xA500/4,
  340. SubStat = 0x8504/4,
  341. FifoStat = 0x850C/4,
  342. };
  343. /*
  344. * Wait for writes to VGA memory via linear aperture to flush.
  345. */
  346. enum {Maxloop = 1<<24};
  347. struct {
  348. ulong linear;
  349. ulong fifo;
  350. ulong idle;
  351. ulong lineartimeout;
  352. ulong fifotimeout;
  353. ulong idletimeout;
  354. } waitcount;
  355. static void
  356. waitforlinearfifo(VGAscr *scr)
  357. {
  358. ulong *mmio;
  359. long x;
  360. static ulong nwaitforlinearfifo;
  361. ulong mask, val;
  362. switch(scr->id){
  363. default:
  364. panic("unknown scr->id in s3 waitforlinearfifo");
  365. case 0x5631: /* ViRGE */
  366. case 0x883D: /* ViRGE/VX */
  367. mask = 0x0F<<6;
  368. val = 0x08<<6;
  369. break;
  370. case 0x8A10: /* ViRGE/GX2 */
  371. mask = 0x1F<<6;
  372. val = 0x10<<6;
  373. break;
  374. }
  375. mmio = scr->mmio;
  376. x = 0;
  377. while((mmio[FifoStat]&mask) != val && x++ < Maxloop)
  378. waitcount.linear++;
  379. if(x >= Maxloop)
  380. waitcount.lineartimeout++;
  381. }
  382. static void
  383. waitforfifo(VGAscr *scr, int entries)
  384. {
  385. ulong *mmio;
  386. long x;
  387. static ulong nwaitforfifo;
  388. mmio = scr->mmio;
  389. x = 0;
  390. while((mmio[SubStat]&0x1F00) < ((entries+2)<<8) && x++ < Maxloop)
  391. waitcount.fifo++;
  392. if(x >= Maxloop)
  393. waitcount.fifotimeout++;
  394. }
  395. static void
  396. waitforidle(VGAscr *scr)
  397. {
  398. ulong *mmio;
  399. long x;
  400. mmio = scr->mmio;
  401. x = 0;
  402. while((mmio[SubStat]&0x3F00) != 0x3000 && x++ < Maxloop)
  403. waitcount.idle++;
  404. if(x >= Maxloop)
  405. waitcount.idletimeout++;
  406. }
  407. static int
  408. hwscroll(VGAscr *scr, Rectangle r, Rectangle sr)
  409. {
  410. enum { Bitbltop = 0xCC }; /* copy source */
  411. ulong *mmio;
  412. ulong cmd, stride;
  413. Point dp, sp;
  414. int did, d;
  415. d = scr->gscreen->depth;
  416. did = (d-8)/8;
  417. cmd = 0x00000020|(Bitbltop<<17)|(did<<2);
  418. stride = Dx(scr->gscreen->r)*d/8;
  419. if(r.min.x <= sr.min.x){
  420. cmd |= 1<<25;
  421. dp.x = r.min.x;
  422. sp.x = sr.min.x;
  423. }else{
  424. dp.x = r.max.x-1;
  425. sp.x = sr.max.x-1;
  426. }
  427. if(r.min.y <= sr.min.y){
  428. cmd |= 1<<26;
  429. dp.y = r.min.y;
  430. sp.y = sr.min.y;
  431. }else{
  432. dp.y = r.max.y-1;
  433. sp.y = sr.max.y-1;
  434. }
  435. mmio = scr->mmio;
  436. waitforlinearfifo(scr);
  437. waitforfifo(scr, 7);
  438. mmio[SrcBase] = scr->aperture;
  439. mmio[DstBase] = scr->aperture;
  440. mmio[Stride] = (stride<<16)|stride;
  441. mmio[WidthHeight] = ((Dx(r)-1)<<16)|Dy(r);
  442. mmio[SrcXY] = (sp.x<<16)|sp.y;
  443. mmio[DestXY] = (dp.x<<16)|dp.y;
  444. mmio[Command] = cmd;
  445. waitforidle(scr);
  446. return 1;
  447. }
  448. static int
  449. hwfill(VGAscr *scr, Rectangle r, ulong sval)
  450. {
  451. enum { Bitbltop = 0xCC }; /* copy source */
  452. ulong *mmio;
  453. ulong cmd, stride;
  454. int did, d;
  455. d = scr->gscreen->depth;
  456. did = (d-8)/8;
  457. cmd = 0x16000120|(Bitbltop<<17)|(did<<2);
  458. stride = Dx(scr->gscreen->r)*d/8;
  459. mmio = scr->mmio;
  460. waitforlinearfifo(scr);
  461. waitforfifo(scr, 8);
  462. mmio[SrcBase] = scr->aperture;
  463. mmio[DstBase] = scr->aperture;
  464. mmio[DstBase] = scr->aperture;
  465. mmio[Stride] = (stride<<16)|stride;
  466. mmio[FgrdData] = sval;
  467. mmio[WidthHeight] = ((Dx(r)-1)<<16)|Dy(r);
  468. mmio[DestXY] = (r.min.x<<16)|r.min.y;
  469. mmio[Command] = cmd;
  470. waitforidle(scr);
  471. return 1;
  472. }
  473. enum {
  474. CursorSyncCtl = 0x0D, /* in Seqx */
  475. VsyncHi = 0x80,
  476. VsyncLo = 0x40,
  477. HsyncHi = 0x20,
  478. HsyncLo = 0x10,
  479. };
  480. static void
  481. s3blank(VGAscr*, int blank)
  482. {
  483. uchar x;
  484. x = vgaxi(Seqx, CursorSyncCtl);
  485. x &= ~0xF0;
  486. if(blank)
  487. x |= VsyncLo | HsyncLo;
  488. vgaxo(Seqx, CursorSyncCtl, x);
  489. }
  490. static void
  491. s3drawinit(VGAscr *scr)
  492. {
  493. extern void savageinit(VGAscr*); /* vgasavage.c */
  494. ulong id;
  495. id = (vgaxi(Crtx, 0x2D)<<8)|vgaxi(Crtx, 0x2E);
  496. scr->id = id;
  497. /*
  498. * It's highly likely that other ViRGEs will work without
  499. * change to the driver, with the exception of the size of
  500. * the linear aperture memory write FIFO. Since we don't
  501. * know that size, I'm not turning them on. See waitforlinearfifo
  502. * above.
  503. */
  504. scr->blank = s3blank;
  505. /* hwblank = 1; not known to work well */
  506. switch(id){
  507. case VIRGE:
  508. case VIRGEVX:
  509. case VIRGEGX2:
  510. scr->mmio = (ulong*)(scr->aperture+0x1000000);
  511. scr->fill = hwfill;
  512. scr->scroll = hwscroll;
  513. break;
  514. case SAVAGEMXMV:
  515. case SAVAGEIXMV:
  516. scr->mmio = (ulong*)(scr->aperture+0x1000000);
  517. savageinit(scr);
  518. break;
  519. case SUPERSAVAGEIXC16:
  520. case SAVAGE4:
  521. case PROSAVAGEP:
  522. case PROSAVAGEK:
  523. /* scr->mmio is set by s3linear */
  524. savageinit(scr);
  525. break;
  526. }
  527. }
  528. VGAdev vgas3dev = {
  529. "s3",
  530. 0,
  531. 0,
  532. s3page,
  533. s3linear,
  534. s3drawinit,
  535. };
  536. VGAcur vgas3cur = {
  537. "s3hwgc",
  538. s3enable,
  539. s3disable,
  540. s3load,
  541. s3move,
  542. };