vga.c 13 KB

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