vga.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. unsigned long vga_fb_lfb_addr(void)
  165. {
  166. vga_cache_boot_fb();
  167. return (unsigned long)vga_fb_lfb;
  168. }
  169. int vga_has_vesa(void)
  170. {
  171. vga_cache_boot_fb();
  172. return (vga_fb_w != 0 && vga_fb_lfb != 0);
  173. }
  174. /* 16bpp: RGB 5:6:5. 32bpp: BGRX byte order (standard VBE / QEMU). */
  175. static void shadow_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  176. {
  177. unsigned int pitch = vga_shadow_pitch;
  178. unsigned int bpp = vga_fb_bpp_val;
  179. unsigned char *p;
  180. if (x >= vga_fb_w || y >= vga_fb_h || !vga_shadow_buf)
  181. return;
  182. p = vga_shadow_buf + (unsigned long)y * pitch;
  183. if (bpp == 16) {
  184. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  185. unsigned int off = x * 2;
  186. p[off] = (unsigned char)(rgb16 & 0xFF);
  187. p[off + 1] = (unsigned char)(rgb16 >> 8);
  188. } else if (bpp == 24) {
  189. unsigned int off = x * 3;
  190. p[off] = (unsigned char)b;
  191. p[off + 1] = (unsigned char)g;
  192. p[off + 2] = (unsigned char)r;
  193. } else if (bpp == 32) {
  194. unsigned int off = x * 4;
  195. p[off] = (unsigned char)b;
  196. p[off + 1] = (unsigned char)g;
  197. p[off + 2] = (unsigned char)r;
  198. p[off + 3] = 0;
  199. }
  200. mark_dirty_row(y);
  201. }
  202. void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  203. {
  204. vga_cache_boot_fb();
  205. if (!vga_shadow_buf)
  206. return;
  207. shadow_put_pixel(x, y, r, g, b);
  208. }
  209. void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  210. unsigned int r, unsigned int g, unsigned int b)
  211. {
  212. unsigned int row, x1, y1;
  213. unsigned int pitch;
  214. unsigned int bpp;
  215. unsigned char *fb;
  216. vga_cache_boot_fb();
  217. pitch = vga_shadow_pitch;
  218. bpp = vga_fb_bpp_val;
  219. fb = vga_shadow_buf;
  220. if (!fb || w == 0 || h == 0)
  221. return;
  222. if (x >= vga_fb_w || y >= vga_fb_h)
  223. return;
  224. x1 = x + w;
  225. y1 = y + h;
  226. if (x1 > vga_fb_w) x1 = vga_fb_w;
  227. if (y1 > vga_fb_h) y1 = vga_fb_h;
  228. if (bpp == 16) {
  229. unsigned short val = (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
  230. for (row = y; row < y1; row++) {
  231. unsigned short *p = (unsigned short *)(fb + (unsigned long)row * pitch + x * 2);
  232. unsigned int xx;
  233. for (xx = x; xx < x1; xx++)
  234. *p++ = val;
  235. }
  236. } else if (bpp == 24) {
  237. for (row = y; row < y1; row++) {
  238. unsigned char *p = fb + (unsigned long)row * pitch + x * 3;
  239. unsigned int xx;
  240. for (xx = x; xx < x1; xx++) {
  241. p[0] = (unsigned char)b;
  242. p[1] = (unsigned char)g;
  243. p[2] = (unsigned char)r;
  244. p += 3;
  245. }
  246. }
  247. } else {
  248. unsigned int val = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
  249. for (row = y; row < y1; row++) {
  250. unsigned int *p = (unsigned int *)(fb + (unsigned long)row * pitch + x * 4);
  251. unsigned int xx;
  252. for (xx = x; xx < x1; xx++)
  253. *p++ = val;
  254. }
  255. }
  256. mark_dirty_rect(y, y1 - y);
  257. }
  258. static unsigned int vga_pixel_bytes(void)
  259. {
  260. if (vga_fb_bpp_val == 16)
  261. return 2;
  262. if (vga_fb_bpp_val == 24)
  263. return 3;
  264. return 4;
  265. }
  266. void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  267. unsigned char *buf)
  268. {
  269. unsigned int pitch, pb, row, col;
  270. unsigned char *fb;
  271. const unsigned char *src;
  272. vga_cache_boot_fb();
  273. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  274. return;
  275. pitch = vga_shadow_pitch;
  276. pb = vga_pixel_bytes();
  277. fb = vga_shadow_buf;
  278. for (row = 0; row < h; row++) {
  279. src = fb + (unsigned long)(y + row) * pitch + x * pb;
  280. for (col = 0; col < w * pb; col++)
  281. buf[row * w * pb + col] = src[col];
  282. }
  283. }
  284. void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  285. const unsigned char *buf)
  286. {
  287. unsigned int pitch, pb, row, col;
  288. unsigned char *dst;
  289. vga_cache_boot_fb();
  290. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  291. return;
  292. pitch = vga_shadow_pitch;
  293. pb = vga_pixel_bytes();
  294. for (row = 0; row < h; row++) {
  295. dst = vga_shadow_buf + (unsigned long)(y + row) * pitch + x * pb;
  296. for (col = 0; col < w * pb; col++)
  297. dst[col] = buf[row * w * pb + col];
  298. }
  299. mark_dirty_rect(y, h);
  300. }
  301. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
  302. {
  303. unsigned int pitch = vga_shadow_pitch;
  304. unsigned int bpp = vga_fb_bpp_val;
  305. vga_cache_boot_fb();
  306. if (!vga_shadow_buf)
  307. return;
  308. if (bpp == 16) {
  309. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  310. unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
  311. unsigned short *fb = (unsigned short *)vga_shadow_buf;
  312. unsigned int n = (pitch / 2) * vga_fb_h;
  313. unsigned int i;
  314. for (i = 0; i < n; i++)
  315. fb[i] = val;
  316. } else if (bpp == 24) {
  317. unsigned char *p = vga_shadow_buf;
  318. unsigned int row, x;
  319. for (row = 0; row < vga_fb_h; row++) {
  320. unsigned int row_off = row * pitch;
  321. for (x = 0; x < vga_fb_w; x++) {
  322. unsigned int off = row_off + x * 3;
  323. p[off] = (unsigned char)b;
  324. p[off + 1] = (unsigned char)g;
  325. p[off + 2] = (unsigned char)r;
  326. }
  327. }
  328. } else if (bpp == 32) {
  329. unsigned int *fb = (unsigned int *)vga_shadow_buf;
  330. unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
  331. unsigned int stride = pitch / 4;
  332. unsigned int row, x;
  333. for (row = 0; row < vga_fb_h; row++)
  334. for (x = 0; x < vga_fb_w; x++)
  335. fb[row * stride + x] = pixel;
  336. }
  337. mark_dirty_rect(0, vga_fb_h);
  338. }
  339. /* ---- Shadow buffer init and flush ---- */
  340. /* Assembly row copy: rep movsl optimized for P5 USWC write-combining to UC memory. */
  341. extern void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
  342. int vga_shadow_flush_pending;
  343. void vga_shadow_init(void)
  344. {
  345. vga_cache_boot_fb();
  346. if (!vga_fb_lfb || vga_fb_w == 0 || vga_fb_h == 0)
  347. return;
  348. vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
  349. vga_shadow_pitch = vga_fb_pitch_val;
  350. vga_shadow_size = vga_fb_h * vga_shadow_pitch;
  351. vga_shadow_flush_pending = 0;
  352. clear_dirty_rows();
  353. }
  354. /* Copy only dirty rows from shadow to VRAM, batching contiguous runs. */
  355. static void shadow_copy_dirty_rows(void)
  356. {
  357. unsigned int row;
  358. unsigned int pb = vga_pixel_bytes();
  359. unsigned int row_bytes = vga_fb_w * pb;
  360. unsigned int dwords = row_bytes / 4;
  361. unsigned char *src = vga_shadow_buf;
  362. unsigned char *dst = (unsigned char *)vga_fb_lfb;
  363. unsigned int max_row = vga_fb_h;
  364. if (max_row > MAX_DIRTY_ROWS)
  365. max_row = MAX_DIRTY_ROWS;
  366. for (row = 0; row < max_row; row++) {
  367. if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
  368. unsigned int run = row + 1;
  369. while (run < max_row &&
  370. (vga_shadow_dirty_rows[run / 8] & (1 << (run % 8))))
  371. run++;
  372. vga_fb_copy_row(
  373. (void *)(dst + (unsigned long)row * vga_fb_pitch_val),
  374. (void *)(src + (unsigned long)row * vga_shadow_pitch),
  375. dwords * (run - row)
  376. );
  377. row = run - 1;
  378. }
  379. }
  380. }
  381. void vga_fb_flush_pending_check(void)
  382. {
  383. if (!vga_shadow_flush_pending)
  384. return;
  385. vga_shadow_flush_pending = 0;
  386. if (!any_dirty_rows())
  387. return;
  388. shadow_copy_dirty_rows();
  389. clear_dirty_rows();
  390. }
  391. void vga_fb_flush(void)
  392. {
  393. vga_cache_boot_fb();
  394. if (!vga_shadow_buf || !vga_fb_lfb)
  395. return;
  396. if (!any_dirty_rows())
  397. return;
  398. shadow_copy_dirty_rows();
  399. clear_dirty_rows();
  400. }
  401. int vga_shadow_is_active(void)
  402. {
  403. return vga_shadow_buf != 0;
  404. }
  405. void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h)
  406. {
  407. mark_dirty_rect(y, h);
  408. }
  409. void vga_fb_clear_dirty(void)
  410. {
  411. clear_dirty_rows();
  412. }