vga.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. static unsigned int vga_pixel_bytes(void)
  205. {
  206. if (vga_fb_bpp_val == 16)
  207. return 2;
  208. if (vga_fb_bpp_val == 24)
  209. return 3;
  210. return 4;
  211. }
  212. void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  213. unsigned char *buf)
  214. {
  215. unsigned int pitch, pb, row, col;
  216. unsigned char *fb;
  217. const unsigned char *src;
  218. vga_cache_boot_fb();
  219. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  220. return;
  221. pitch = vga_shadow_pitch;
  222. pb = vga_pixel_bytes();
  223. fb = vga_shadow_buf;
  224. for (row = 0; row < h; row++) {
  225. src = fb + (unsigned long)(y + row) * pitch + x * pb;
  226. for (col = 0; col < w * pb; col++)
  227. buf[row * w * pb + col] = src[col];
  228. }
  229. }
  230. void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  231. const unsigned char *buf)
  232. {
  233. unsigned int pitch, pb, row, col;
  234. unsigned char *dst;
  235. vga_cache_boot_fb();
  236. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  237. return;
  238. pitch = vga_shadow_pitch;
  239. pb = vga_pixel_bytes();
  240. for (row = 0; row < h; row++) {
  241. dst = vga_shadow_buf + (unsigned long)(y + row) * pitch + x * pb;
  242. for (col = 0; col < w * pb; col++)
  243. dst[col] = buf[row * w * pb + col];
  244. }
  245. mark_dirty_rect(y, h);
  246. }
  247. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
  248. {
  249. unsigned int pitch = vga_shadow_pitch;
  250. unsigned int bpp = vga_fb_bpp_val;
  251. vga_cache_boot_fb();
  252. if (!vga_shadow_buf)
  253. return;
  254. if (bpp == 16) {
  255. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  256. unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
  257. unsigned short *fb = (unsigned short *)vga_shadow_buf;
  258. unsigned int n = (pitch / 2) * vga_fb_h;
  259. unsigned int i;
  260. for (i = 0; i < n; i++)
  261. fb[i] = val;
  262. } else if (bpp == 24) {
  263. unsigned char *p = vga_shadow_buf;
  264. unsigned int row, x;
  265. for (row = 0; row < vga_fb_h; row++) {
  266. unsigned int row_off = row * pitch;
  267. for (x = 0; x < vga_fb_w; x++) {
  268. unsigned int off = row_off + x * 3;
  269. p[off] = (unsigned char)b;
  270. p[off + 1] = (unsigned char)g;
  271. p[off + 2] = (unsigned char)r;
  272. }
  273. }
  274. } else if (bpp == 32) {
  275. unsigned int *fb = (unsigned int *)vga_shadow_buf;
  276. unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
  277. unsigned int stride = pitch / 4;
  278. unsigned int row, x;
  279. for (row = 0; row < vga_fb_h; row++)
  280. for (x = 0; x < vga_fb_w; x++)
  281. fb[row * stride + x] = pixel;
  282. }
  283. mark_dirty_rect(0, vga_fb_h);
  284. }
  285. /* ---- Shadow buffer init and flush ---- */
  286. /* Assembly row copy: rep movsl optimized for P5 USWC write-combining to UC memory. */
  287. extern void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
  288. int vga_shadow_flush_pending;
  289. void vga_shadow_init(void)
  290. {
  291. vga_cache_boot_fb();
  292. if (!vga_fb_lfb || vga_fb_w == 0 || vga_fb_h == 0)
  293. return;
  294. vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
  295. vga_shadow_pitch = vga_fb_pitch_val;
  296. vga_shadow_size = vga_fb_h * vga_shadow_pitch;
  297. vga_shadow_flush_pending = 0;
  298. clear_dirty_rows();
  299. }
  300. /* Copy only dirty rows from shadow to VRAM. */
  301. static void shadow_copy_dirty_rows(void)
  302. {
  303. unsigned int row;
  304. unsigned int pb = vga_pixel_bytes();
  305. unsigned int row_bytes = vga_fb_w * pb;
  306. unsigned int dwords = row_bytes / 4;
  307. unsigned char *src = vga_shadow_buf;
  308. unsigned char *dst = (unsigned char *)vga_fb_lfb;
  309. unsigned int max_row = vga_fb_h;
  310. if (max_row > MAX_DIRTY_ROWS)
  311. max_row = MAX_DIRTY_ROWS;
  312. for (row = 0; row < max_row; row++) {
  313. if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
  314. vga_fb_copy_row(
  315. (void *)(dst + (unsigned long)row * vga_fb_pitch_val),
  316. (void *)(src + (unsigned long)row * vga_shadow_pitch),
  317. dwords
  318. );
  319. }
  320. }
  321. }
  322. /* Debug square: 32x32 at bottom-right, cycles color each flush. Drawn into shadow buffer
  323. so it gets copied to VRAM along with everything else. Uses bright colors to be obvious. */
  324. static void draw_flush_debug_square(void)
  325. {
  326. static unsigned int flush_color_counter;
  327. unsigned int r, g, b;
  328. unsigned int x, y;
  329. unsigned int sx = vga_fb_w - 32;
  330. unsigned int sy = vga_fb_h - 32;
  331. unsigned int pitch = vga_shadow_pitch;
  332. unsigned int bpp = vga_fb_bpp_val;
  333. unsigned char *fb = vga_shadow_buf;
  334. flush_color_counter++;
  335. /* Cycle through bright primary/secondary colors */
  336. switch (flush_color_counter % 6) {
  337. case 0: r = 255; g = 0; b = 0; break; /* red */
  338. case 1: r = 0; g = 255; b = 0; break; /* green */
  339. case 2: r = 0; g = 0; b = 255; break; /* blue */
  340. case 3: r = 255; g = 255; b = 0; break; /* yellow */
  341. case 4: r = 255; g = 0; b = 255; break; /* magenta */
  342. default: r = 0; g = 255; b = 255; break; /* cyan */
  343. }
  344. for (y = 0; y < 32; y++) {
  345. unsigned char *row = fb + (sy + y) * pitch;
  346. for (x = 0; x < 32; x++) {
  347. if (bpp == 16) {
  348. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  349. unsigned short *p = (unsigned short *)(row + (sx + x) * 2);
  350. *p = (unsigned short)rgb16;
  351. } else if (bpp == 24) {
  352. row[(sx + x) * 3] = (unsigned char)b;
  353. row[(sx + x) * 3 + 1] = (unsigned char)g;
  354. row[(sx + x) * 3 + 2] = (unsigned char)r;
  355. } else if (bpp == 32) {
  356. unsigned int *p = (unsigned int *)(row + (sx + x) * 4);
  357. *p = (unsigned int)b | ((unsigned int)g << 8) | ((unsigned int)r << 16);
  358. }
  359. }
  360. }
  361. mark_dirty_rect(sy, 32);
  362. }
  363. void vga_fb_flush_pending_check(void)
  364. {
  365. if (!vga_shadow_flush_pending)
  366. return;
  367. vga_shadow_flush_pending = 0;
  368. if (!any_dirty_rows())
  369. return;
  370. draw_flush_debug_square();
  371. shadow_copy_dirty_rows();
  372. clear_dirty_rows();
  373. }
  374. void vga_fb_flush(void)
  375. {
  376. vga_cache_boot_fb();
  377. if (!vga_shadow_buf || !vga_fb_lfb)
  378. return;
  379. if (!any_dirty_rows())
  380. return;
  381. draw_flush_debug_square();
  382. shadow_copy_dirty_rows();
  383. clear_dirty_rows();
  384. }
  385. int vga_shadow_is_active(void)
  386. {
  387. return vga_shadow_buf != 0;
  388. }
  389. void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h)
  390. {
  391. mark_dirty_rect(y, h);
  392. }