vga.c 10 KB

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