vga.c 11 KB

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