vga.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* VGA: VESA LFB (from boot) with shadow buffer and 30Hz USWC flush. */
  2. #include "vga.h"
  3. #include "pci.h"
  4. #include "gpu.h"
  5. #include "ati.h"
  6. /* Cached boot framebuffer info (set once when vga_fb_* first used). */
  7. static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
  8. static unsigned int vga_fb_vram_kb_val;
  9. static volatile unsigned char *vga_fb_lfb;
  10. static int vga_fb_cached;
  11. /* Shadow buffer state. */
  12. static unsigned char *vga_shadow_buf;
  13. static unsigned int vga_shadow_pitch;
  14. static unsigned int vga_shadow_size;
  15. /* Dirty row bitmap: one bit per row. Max 2048 rows = 256 bytes. */
  16. #define MAX_DIRTY_ROWS 2048
  17. static unsigned char vga_shadow_dirty_rows[MAX_DIRTY_ROWS / 8];
  18. static void mark_dirty_row(unsigned int y)
  19. {
  20. if (y < MAX_DIRTY_ROWS)
  21. vga_shadow_dirty_rows[y / 8] |= (1 << (y % 8));
  22. }
  23. static void mark_dirty_rect(unsigned int y, unsigned int h)
  24. {
  25. unsigned int i;
  26. for (i = 0; i < h; i++)
  27. mark_dirty_row(y + i);
  28. }
  29. static int any_dirty_rows(void)
  30. {
  31. unsigned int i;
  32. unsigned int max_row = vga_fb_h;
  33. if (max_row > MAX_DIRTY_ROWS)
  34. max_row = MAX_DIRTY_ROWS;
  35. for (i = 0; i < (max_row + 7) / 8; i++)
  36. if (vga_shadow_dirty_rows[i])
  37. return 1;
  38. return 0;
  39. }
  40. static void clear_dirty_rows(void)
  41. {
  42. unsigned int i;
  43. unsigned int max_row = vga_fb_h;
  44. if (max_row > MAX_DIRTY_ROWS)
  45. max_row = MAX_DIRTY_ROWS;
  46. for (i = 0; i < (max_row + 7) / 8; i++)
  47. vga_shadow_dirty_rows[i] = 0;
  48. }
  49. /*
  50. * Force CRTC Start Address to 0 and Line Compare to max (0x3FF).
  51. * Fixes real hardware where VBE mode set leaves stale CRTC state,
  52. * causing the display to appear rotated (bottom portion at top).
  53. */
  54. static void vga_reset_crtc(void)
  55. {
  56. unsigned char v;
  57. /* Start Address High/Low = 0 */
  58. port_outb(0x3D4, 0x0C);
  59. port_outb(0x3D5, 0x00);
  60. port_outb(0x3D4, 0x0D);
  61. port_outb(0x3D5, 0x00);
  62. /* Unlock CRTC registers 0-7: clear bit 7 of register 0x11 */
  63. port_outb(0x3D4, 0x11);
  64. v = port_inb(0x3D5);
  65. port_outb(0x3D5, v & 0x7F);
  66. /* Line Compare bits 7:0 = 0xFF (register 0x18) */
  67. port_outb(0x3D4, 0x18);
  68. port_outb(0x3D5, 0xFF);
  69. /* Line Compare bit 8 = 1 (register 0x07, bit 4) */
  70. port_outb(0x3D4, 0x07);
  71. v = port_inb(0x3D5);
  72. port_outb(0x3D5, v | 0x10);
  73. /* Line Compare bit 9 = 1 (register 0x09, bit 6) */
  74. port_outb(0x3D4, 0x09);
  75. v = port_inb(0x3D5);
  76. port_outb(0x3D5, v | 0x40);
  77. }
  78. static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
  79. {
  80. volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
  81. __asm__ volatile ("" : "+r"(p));
  82. return p;
  83. }
  84. static void vga_cache_boot_fb(void)
  85. {
  86. volatile struct vga_boot_fb_info *info;
  87. if (vga_fb_cached)
  88. return;
  89. vga_fb_cached = 1;
  90. info = vga_get_boot_fb();
  91. if (!info->valid) {
  92. vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
  93. vga_fb_lfb = 0;
  94. gpu_init();
  95. return;
  96. }
  97. vga_fb_w = info->width;
  98. vga_fb_h = info->height;
  99. vga_fb_pitch_val = info->pitch;
  100. vga_fb_bpp_val = info->bpp;
  101. /* Some BIOSes (e.g. 1024x768) report pitch in pixels or too small; enforce minimum. */
  102. {
  103. unsigned int min_pitch = vga_fb_w;
  104. if (vga_fb_bpp_val == 16)
  105. min_pitch *= 2;
  106. else if (vga_fb_bpp_val == 24)
  107. min_pitch *= 3;
  108. else if (vga_fb_bpp_val == 32)
  109. min_pitch *= 4;
  110. if (vga_fb_pitch_val < min_pitch)
  111. vga_fb_pitch_val = min_pitch;
  112. }
  113. vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
  114. vga_fb_vram_kb_val = (unsigned int)info->vram_64k * 64;
  115. /* Clamp height to what fits in VRAM (prevents wrap on small VRAM cards) */
  116. if (vga_fb_vram_kb_val > 0 && vga_fb_pitch_val > 0) {
  117. unsigned int vram_bytes = vga_fb_vram_kb_val * 1024;
  118. unsigned int max_h = vram_bytes / vga_fb_pitch_val;
  119. if (max_h < vga_fb_h)
  120. vga_fb_h = max_h;
  121. }
  122. gpu_init();
  123. if (gpu_vendor() == GPU_VENDOR_ATI)
  124. vga_reset_crtc();
  125. }
  126. /* ---- LFB API ---- */
  127. unsigned int vga_fb_width(void)
  128. {
  129. vga_cache_boot_fb();
  130. return vga_fb_w;
  131. }
  132. unsigned int vga_fb_height(void)
  133. {
  134. vga_cache_boot_fb();
  135. return vga_fb_h;
  136. }
  137. unsigned int vga_fb_pitch(void)
  138. {
  139. vga_cache_boot_fb();
  140. return vga_fb_pitch_val;
  141. }
  142. unsigned int vga_fb_bpp(void)
  143. {
  144. vga_cache_boot_fb();
  145. return vga_fb_bpp_val;
  146. }
  147. unsigned int vga_fb_vram_kb(void)
  148. {
  149. vga_cache_boot_fb();
  150. return vga_fb_vram_kb_val;
  151. }
  152. unsigned int vga_fb_pci_id(void)
  153. {
  154. vga_cache_boot_fb();
  155. return gpu_pci_id();
  156. }
  157. void *vga_fb_ptr(void)
  158. {
  159. vga_cache_boot_fb();
  160. if (vga_shadow_buf)
  161. return (void *)vga_shadow_buf;
  162. return (void *)vga_fb_lfb;
  163. }
  164. int vga_has_vesa(void)
  165. {
  166. vga_cache_boot_fb();
  167. return (vga_fb_w != 0 && vga_fb_lfb != 0);
  168. }
  169. /* 16bpp: RGB 5:6:5. 32bpp: BGRX byte order (standard VBE / QEMU). */
  170. static void shadow_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  171. {
  172. unsigned int pitch = vga_shadow_pitch;
  173. unsigned int bpp = vga_fb_bpp_val;
  174. unsigned char *p;
  175. if (x >= vga_fb_w || y >= vga_fb_h || !vga_shadow_buf)
  176. return;
  177. p = vga_shadow_buf + (unsigned long)y * pitch;
  178. if (bpp == 16) {
  179. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  180. unsigned int off = x * 2;
  181. p[off] = (unsigned char)(rgb16 & 0xFF);
  182. p[off + 1] = (unsigned char)(rgb16 >> 8);
  183. } else if (bpp == 24) {
  184. unsigned int off = x * 3;
  185. p[off] = (unsigned char)b;
  186. p[off + 1] = (unsigned char)g;
  187. p[off + 2] = (unsigned char)r;
  188. } else if (bpp == 32) {
  189. unsigned int off = x * 4;
  190. p[off] = (unsigned char)b;
  191. p[off + 1] = (unsigned char)g;
  192. p[off + 2] = (unsigned char)r;
  193. p[off + 3] = 0;
  194. }
  195. mark_dirty_row(y);
  196. }
  197. void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  198. {
  199. vga_cache_boot_fb();
  200. if (!vga_shadow_buf)
  201. return;
  202. shadow_put_pixel(x, y, r, g, b);
  203. }
  204. void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  205. unsigned int r, unsigned int g, unsigned int b)
  206. {
  207. unsigned int row, x1, y1;
  208. unsigned int pitch;
  209. unsigned int bpp;
  210. unsigned char *fb;
  211. vga_cache_boot_fb();
  212. pitch = vga_shadow_pitch;
  213. bpp = vga_fb_bpp_val;
  214. fb = vga_shadow_buf;
  215. if (!fb || w == 0 || h == 0)
  216. return;
  217. if (x >= vga_fb_w || y >= vga_fb_h)
  218. return;
  219. x1 = x + w;
  220. y1 = y + h;
  221. if (x1 > vga_fb_w) x1 = vga_fb_w;
  222. if (y1 > vga_fb_h) y1 = vga_fb_h;
  223. if (bpp == 16) {
  224. unsigned short val = (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
  225. for (row = y; row < y1; row++) {
  226. unsigned short *p = (unsigned short *)(fb + (unsigned long)row * pitch + x * 2);
  227. unsigned int xx;
  228. for (xx = x; xx < x1; xx++)
  229. *p++ = val;
  230. }
  231. } else if (bpp == 24) {
  232. for (row = y; row < y1; row++) {
  233. unsigned char *p = fb + (unsigned long)row * pitch + x * 3;
  234. unsigned int xx;
  235. for (xx = x; xx < x1; xx++) {
  236. p[0] = (unsigned char)b;
  237. p[1] = (unsigned char)g;
  238. p[2] = (unsigned char)r;
  239. p += 3;
  240. }
  241. }
  242. } else {
  243. unsigned int val = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
  244. for (row = y; row < y1; row++) {
  245. unsigned int *p = (unsigned int *)(fb + (unsigned long)row * pitch + x * 4);
  246. unsigned int xx;
  247. for (xx = x; xx < x1; xx++)
  248. *p++ = val;
  249. }
  250. }
  251. mark_dirty_rect(y, y1 - y);
  252. }
  253. static unsigned int vga_pixel_bytes(void)
  254. {
  255. if (vga_fb_bpp_val == 16)
  256. return 2;
  257. if (vga_fb_bpp_val == 24)
  258. return 3;
  259. return 4;
  260. }
  261. void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  262. unsigned char *buf)
  263. {
  264. unsigned int pitch, pb, row, col;
  265. unsigned char *fb;
  266. const unsigned char *src;
  267. vga_cache_boot_fb();
  268. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  269. return;
  270. pitch = vga_shadow_pitch;
  271. pb = vga_pixel_bytes();
  272. fb = vga_shadow_buf;
  273. for (row = 0; row < h; row++) {
  274. src = fb + (unsigned long)(y + row) * pitch + x * pb;
  275. for (col = 0; col < w * pb; col++)
  276. buf[row * w * pb + col] = src[col];
  277. }
  278. }
  279. void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  280. const unsigned char *buf)
  281. {
  282. unsigned int pitch, pb, row, col;
  283. unsigned char *dst;
  284. vga_cache_boot_fb();
  285. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  286. return;
  287. pitch = vga_shadow_pitch;
  288. pb = vga_pixel_bytes();
  289. for (row = 0; row < h; row++) {
  290. dst = vga_shadow_buf + (unsigned long)(y + row) * pitch + x * pb;
  291. for (col = 0; col < w * pb; col++)
  292. dst[col] = buf[row * w * pb + col];
  293. }
  294. mark_dirty_rect(y, h);
  295. }
  296. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
  297. {
  298. unsigned int pitch = vga_shadow_pitch;
  299. unsigned int bpp = vga_fb_bpp_val;
  300. vga_cache_boot_fb();
  301. if (!vga_shadow_buf)
  302. return;
  303. if (bpp == 16) {
  304. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  305. unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
  306. unsigned short *fb = (unsigned short *)vga_shadow_buf;
  307. unsigned int n = (pitch / 2) * vga_fb_h;
  308. unsigned int i;
  309. for (i = 0; i < n; i++)
  310. fb[i] = val;
  311. } else if (bpp == 24) {
  312. unsigned char *p = vga_shadow_buf;
  313. unsigned int row, x;
  314. for (row = 0; row < vga_fb_h; row++) {
  315. unsigned int row_off = row * pitch;
  316. for (x = 0; x < vga_fb_w; x++) {
  317. unsigned int off = row_off + x * 3;
  318. p[off] = (unsigned char)b;
  319. p[off + 1] = (unsigned char)g;
  320. p[off + 2] = (unsigned char)r;
  321. }
  322. }
  323. } else if (bpp == 32) {
  324. unsigned int *fb = (unsigned int *)vga_shadow_buf;
  325. unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
  326. unsigned int stride = pitch / 4;
  327. unsigned int row, x;
  328. for (row = 0; row < vga_fb_h; row++)
  329. for (x = 0; x < vga_fb_w; x++)
  330. fb[row * stride + x] = pixel;
  331. }
  332. mark_dirty_rect(0, vga_fb_h);
  333. }
  334. /* ---- Shadow buffer init and flush ---- */
  335. /* Assembly row copy: rep movsl optimized for P5 USWC write-combining to UC memory. */
  336. extern void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
  337. int vga_shadow_flush_pending;
  338. void vga_shadow_init(void)
  339. {
  340. vga_cache_boot_fb();
  341. if (!vga_fb_lfb || vga_fb_w == 0 || vga_fb_h == 0)
  342. return;
  343. vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
  344. vga_shadow_pitch = vga_fb_pitch_val;
  345. vga_shadow_size = vga_fb_h * vga_shadow_pitch;
  346. vga_shadow_flush_pending = 0;
  347. clear_dirty_rows();
  348. }
  349. /* Copy only dirty rows from shadow to VRAM. */
  350. static void shadow_copy_dirty_rows(void)
  351. {
  352. unsigned int row;
  353. unsigned int pb = vga_pixel_bytes();
  354. unsigned int row_bytes = vga_fb_w * pb;
  355. unsigned int dwords = row_bytes / 4;
  356. unsigned char *src = vga_shadow_buf;
  357. unsigned char *dst = (unsigned char *)vga_fb_lfb;
  358. unsigned int max_row = vga_fb_h;
  359. if (max_row > MAX_DIRTY_ROWS)
  360. max_row = MAX_DIRTY_ROWS;
  361. for (row = 0; row < max_row; row++) {
  362. if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
  363. vga_fb_copy_row(
  364. (void *)(dst + (unsigned long)row * vga_fb_pitch_val),
  365. (void *)(src + (unsigned long)row * vga_shadow_pitch),
  366. dwords
  367. );
  368. }
  369. }
  370. }
  371. /* Debug square: 32x32 at bottom-right, cycles color each flush. Drawn into shadow buffer
  372. so it gets copied to VRAM along with everything else. Uses bright colors to be obvious. */
  373. static void draw_flush_debug_square(void)
  374. {
  375. static unsigned int flush_color_counter;
  376. unsigned int r, g, b;
  377. unsigned int x, y;
  378. unsigned int sx = vga_fb_w - 32;
  379. unsigned int sy = vga_fb_h - 32;
  380. unsigned int pitch = vga_shadow_pitch;
  381. unsigned int bpp = vga_fb_bpp_val;
  382. unsigned char *fb = vga_shadow_buf;
  383. flush_color_counter++;
  384. /* Cycle through bright primary/secondary colors */
  385. switch (flush_color_counter % 6) {
  386. case 0: r = 255; g = 0; b = 0; break; /* red */
  387. case 1: r = 0; g = 255; b = 0; break; /* green */
  388. case 2: r = 0; g = 0; b = 255; break; /* blue */
  389. case 3: r = 255; g = 255; b = 0; break; /* yellow */
  390. case 4: r = 255; g = 0; b = 255; break; /* magenta */
  391. default: r = 0; g = 255; b = 255; break; /* cyan */
  392. }
  393. for (y = 0; y < 32; y++) {
  394. unsigned char *row = fb + (sy + y) * pitch;
  395. for (x = 0; x < 32; x++) {
  396. if (bpp == 16) {
  397. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  398. unsigned short *p = (unsigned short *)(row + (sx + x) * 2);
  399. *p = (unsigned short)rgb16;
  400. } else if (bpp == 24) {
  401. row[(sx + x) * 3] = (unsigned char)b;
  402. row[(sx + x) * 3 + 1] = (unsigned char)g;
  403. row[(sx + x) * 3 + 2] = (unsigned char)r;
  404. } else if (bpp == 32) {
  405. unsigned int *p = (unsigned int *)(row + (sx + x) * 4);
  406. *p = (unsigned int)b | ((unsigned int)g << 8) | ((unsigned int)r << 16);
  407. }
  408. }
  409. }
  410. mark_dirty_rect(sy, 32);
  411. }
  412. void vga_fb_flush_pending_check(void)
  413. {
  414. if (!vga_shadow_flush_pending)
  415. return;
  416. vga_shadow_flush_pending = 0;
  417. if (!any_dirty_rows())
  418. return;
  419. draw_flush_debug_square();
  420. shadow_copy_dirty_rows();
  421. clear_dirty_rows();
  422. }
  423. void vga_fb_flush(void)
  424. {
  425. vga_cache_boot_fb();
  426. if (!vga_shadow_buf || !vga_fb_lfb)
  427. return;
  428. if (!any_dirty_rows())
  429. return;
  430. draw_flush_debug_square();
  431. shadow_copy_dirty_rows();
  432. clear_dirty_rows();
  433. }
  434. int vga_shadow_is_active(void)
  435. {
  436. return vga_shadow_buf != 0;
  437. }
  438. void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h)
  439. {
  440. mark_dirty_rect(y, h);
  441. }