vgas3.c 12 KB

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