vga.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /* VGA: VESA LFB (from boot) with a shadow buffer and a damage-rect flush. */
  2. #include "vga.h"
  3. #include "blit.h"
  4. #include "pci.h"
  5. #include "gpu.h"
  6. #include "ati.h"
  7. #include "process.h"
  8. #include "serial.h"
  9. /* Cached boot framebuffer info (set once when vga_fb_* first used). */
  10. static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
  11. static unsigned int vga_fb_vram_kb_val;
  12. static volatile unsigned char *vga_fb_lfb;
  13. static int vga_fb_cached;
  14. /* Shadow buffer state. */
  15. static unsigned char *vga_shadow_buf;
  16. static unsigned int vga_shadow_pitch;
  17. static unsigned int vga_shadow_size;
  18. /* Damage rect list. Each entry is a half-open [x0,x1) x [y0,y1) region of
  19. * the shadow buffer that has not been copied to VRAM yet. Entries merge when
  20. * they overlap, touch or would barely waste space, so related marks (the
  21. * per-pixel writes of one glyph or cursor draw, the rectangles of one window
  22. * repaint) collapse into a few rects, while unrelated damage (a window and the
  23. * cursor on the other side of the screen) stays separate and the flush copies
  24. * each row only over the columns that changed. A rect that is only partially
  25. * flushed keeps its remaining rows and moves behind the newer damage. */
  26. #define DAMAGE_SLOTS 16
  27. struct damage_rect {
  28. unsigned int x0, y0, x1, y1;
  29. };
  30. static struct damage_rect damage_rects[DAMAGE_SLOTS];
  31. static unsigned int damage_count;
  32. static void damage_add(unsigned int x0, unsigned int y0,
  33. unsigned int x1, unsigned int y1)
  34. {
  35. unsigned int i;
  36. if (x1 > vga_fb_w)
  37. x1 = vga_fb_w;
  38. if (y1 > vga_fb_h)
  39. y1 = vga_fb_h;
  40. if (x0 >= x1 || y0 >= y1)
  41. return;
  42. /* Already covered? Scanning backwards catches the repeated per-pixel
  43. marks of a glyph or cursor draw in one step. */
  44. for (i = damage_count; i-- > 0; ) {
  45. struct damage_rect *d = &damage_rects[i];
  46. if (x0 >= d->x0 && y0 >= d->y0 && x1 <= d->x1 && y1 <= d->y1)
  47. return;
  48. }
  49. /* Merge with every rect this one touches; a merge can bring another
  50. rect into reach, so restart the scan after each one. */
  51. for (i = 0; i < damage_count; ) {
  52. struct damage_rect *d = &damage_rects[i];
  53. unsigned int nx0 = (x0 < d->x0) ? x0 : d->x0;
  54. unsigned int ny0 = (y0 < d->y0) ? y0 : d->y0;
  55. unsigned int nx1 = (x1 > d->x1) ? x1 : d->x1;
  56. unsigned int ny1 = (y1 > d->y1) ? y1 : d->y1;
  57. unsigned int merged_area = (nx1 - nx0) * (ny1 - ny0);
  58. unsigned int area = (d->x1 - d->x0) * (d->y1 - d->y0) +
  59. (x1 - x0) * (y1 - y0);
  60. if (nx0 <= d->x1 && d->x0 <= nx1 &&
  61. ny0 <= d->y1 && d->y0 <= ny1 &&
  62. merged_area <= 2 * area) {
  63. x0 = nx0;
  64. y0 = ny0;
  65. x1 = nx1;
  66. y1 = ny1;
  67. damage_rects[i] = damage_rects[--damage_count];
  68. i = 0;
  69. continue;
  70. }
  71. i++;
  72. }
  73. if (damage_count < DAMAGE_SLOTS) {
  74. damage_rects[damage_count].x0 = x0;
  75. damage_rects[damage_count].y0 = y0;
  76. damage_rects[damage_count].x1 = x1;
  77. damage_rects[damage_count].y1 = y1;
  78. damage_count++;
  79. return;
  80. }
  81. /* List full: extend whichever rect grows the least (correct, just a
  82. few redundant pixels copied). */
  83. {
  84. unsigned int best = 0, best_grow = 0xFFFFFFFFu;
  85. for (i = 0; i < DAMAGE_SLOTS; i++) {
  86. struct damage_rect *d = &damage_rects[i];
  87. unsigned int nx0 = (x0 < d->x0) ? x0 : d->x0;
  88. unsigned int ny0 = (y0 < d->y0) ? y0 : d->y0;
  89. unsigned int nx1 = (x1 > d->x1) ? x1 : d->x1;
  90. unsigned int ny1 = (y1 > d->y1) ? y1 : d->y1;
  91. unsigned int grow = (nx1 - nx0) * (ny1 - ny0) -
  92. (d->x1 - d->x0) * (d->y1 - d->y0);
  93. if (grow < best_grow) {
  94. best_grow = grow;
  95. best = i;
  96. }
  97. }
  98. {
  99. struct damage_rect *d = &damage_rects[best];
  100. if (x0 < d->x0) d->x0 = x0;
  101. if (y0 < d->y0) d->y0 = y0;
  102. if (x1 > d->x1) d->x1 = x1;
  103. if (y1 > d->y1) d->y1 = y1;
  104. }
  105. }
  106. }
  107. static int damage_any(void)
  108. {
  109. return damage_count != 0;
  110. }
  111. static void damage_clear(void)
  112. {
  113. damage_count = 0;
  114. }
  115. static void mark_dirty_rect_xy(unsigned int y, unsigned int h,
  116. unsigned int x0, unsigned int x1)
  117. {
  118. damage_add(x0, y, x1, y + h);
  119. }
  120. /* Full-width dirty rows (used where the x-extent isn't tracked). */
  121. static void mark_dirty_rect(unsigned int y, unsigned int h)
  122. {
  123. damage_add(0, y, vga_fb_w, y + h);
  124. }
  125. /*
  126. * Force CRTC Start Address to 0 and Line Compare to max (0x3FF).
  127. * Fixes real hardware where VBE mode set leaves stale CRTC state,
  128. * causing the display to appear rotated (bottom portion at top).
  129. */
  130. static void vga_reset_crtc(void)
  131. {
  132. unsigned char v;
  133. /* Start Address High/Low = 0 */
  134. port_outb(0x3D4, 0x0C);
  135. port_outb(0x3D5, 0x00);
  136. port_outb(0x3D4, 0x0D);
  137. port_outb(0x3D5, 0x00);
  138. /* Unlock CRTC registers 0-7: clear bit 7 of register 0x11 */
  139. port_outb(0x3D4, 0x11);
  140. v = port_inb(0x3D5);
  141. port_outb(0x3D5, v & 0x7F);
  142. /* Line Compare bits 7:0 = 0xFF (register 0x18) */
  143. port_outb(0x3D4, 0x18);
  144. port_outb(0x3D5, 0xFF);
  145. /* Line Compare bit 8 = 1 (register 0x07, bit 4) */
  146. port_outb(0x3D4, 0x07);
  147. v = port_inb(0x3D5);
  148. port_outb(0x3D5, v | 0x10);
  149. /* Line Compare bit 9 = 1 (register 0x09, bit 6) */
  150. port_outb(0x3D4, 0x09);
  151. v = port_inb(0x3D5);
  152. port_outb(0x3D5, v | 0x40);
  153. }
  154. static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
  155. {
  156. volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
  157. __asm__ volatile ("" : "+r"(p));
  158. return p;
  159. }
  160. static void vga_cache_boot_fb(void)
  161. {
  162. volatile struct vga_boot_fb_info *info;
  163. if (vga_fb_cached)
  164. return;
  165. vga_fb_cached = 1;
  166. info = vga_get_boot_fb();
  167. if (!info->valid) {
  168. vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
  169. vga_fb_lfb = 0;
  170. gpu_init(0);
  171. return;
  172. }
  173. vga_fb_w = info->width;
  174. vga_fb_h = info->height;
  175. vga_fb_pitch_val = info->pitch;
  176. vga_fb_bpp_val = info->bpp;
  177. /* Some BIOSes (e.g. 1024x768) report pitch in pixels or too small; enforce minimum. */
  178. {
  179. unsigned int min_pitch = vga_fb_w;
  180. if (vga_fb_bpp_val == 16)
  181. min_pitch *= 2;
  182. else if (vga_fb_bpp_val == 24)
  183. min_pitch *= 3;
  184. else if (vga_fb_bpp_val == 32)
  185. min_pitch *= 4;
  186. if (vga_fb_pitch_val < min_pitch)
  187. vga_fb_pitch_val = min_pitch;
  188. }
  189. vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
  190. vga_fb_vram_kb_val = (unsigned int)info->vram_64k * 64;
  191. /* Clamp height to what fits in VRAM (prevents wrap on small VRAM cards) */
  192. if (vga_fb_vram_kb_val > 0 && vga_fb_pitch_val > 0) {
  193. unsigned int vram_bytes = vga_fb_vram_kb_val * 1024;
  194. unsigned int max_h = vram_bytes / vga_fb_pitch_val;
  195. if (max_h < vga_fb_h)
  196. vga_fb_h = max_h;
  197. }
  198. gpu_init(info->lfb_base);
  199. if (gpu_vendor() == GPU_VENDOR_ATI && !ati_is_radeon())
  200. vga_reset_crtc();
  201. }
  202. /* ---- LFB API ---- */
  203. unsigned int vga_fb_width(void)
  204. {
  205. vga_cache_boot_fb();
  206. return vga_fb_w;
  207. }
  208. unsigned int vga_fb_height(void)
  209. {
  210. vga_cache_boot_fb();
  211. return vga_fb_h;
  212. }
  213. unsigned int vga_fb_pitch(void)
  214. {
  215. vga_cache_boot_fb();
  216. return vga_fb_pitch_val;
  217. }
  218. unsigned int vga_fb_bpp(void)
  219. {
  220. vga_cache_boot_fb();
  221. return vga_fb_bpp_val;
  222. }
  223. unsigned int vga_fb_vram_kb(void)
  224. {
  225. vga_cache_boot_fb();
  226. return vga_fb_vram_kb_val;
  227. }
  228. unsigned int vga_fb_pci_id(void)
  229. {
  230. vga_cache_boot_fb();
  231. return gpu_pci_id();
  232. }
  233. void *vga_fb_ptr(void)
  234. {
  235. vga_cache_boot_fb();
  236. if (vga_shadow_buf)
  237. return (void *)vga_shadow_buf;
  238. return (void *)vga_fb_lfb;
  239. }
  240. unsigned long vga_fb_lfb_addr(void)
  241. {
  242. vga_cache_boot_fb();
  243. return (unsigned long)vga_fb_lfb;
  244. }
  245. int vga_has_vesa(void)
  246. {
  247. vga_cache_boot_fb();
  248. return (vga_fb_w != 0 && vga_fb_lfb != 0);
  249. }
  250. /* ---- Surfaces and the render target ---- */
  251. /* The screen surface: the shadow buffer (or the LFB when there is none).
  252. * Kept in sync by vga_shadow_init() invalidating it. */
  253. static struct surface screen_surf;
  254. struct surface *vga_screen_surface(void)
  255. {
  256. vga_cache_boot_fb();
  257. if (!screen_surf.px) {
  258. screen_surf.w = vga_fb_w;
  259. screen_surf.h = vga_fb_h;
  260. screen_surf.pitch = vga_shadow_buf ? vga_shadow_pitch
  261. : vga_fb_pitch_val;
  262. screen_surf.bpp = vga_fb_bpp_val;
  263. screen_surf.alpha = 255;
  264. screen_surf.px = vga_shadow_buf ? vga_shadow_buf
  265. : (unsigned char *)vga_fb_lfb;
  266. }
  267. return &screen_surf;
  268. }
  269. struct surface *vga_target(void)
  270. {
  271. struct surface *t = process_render_target();
  272. return t ? t : vga_screen_surface();
  273. }
  274. void vga_set_target(struct surface *s)
  275. {
  276. process_set_render_target(s);
  277. }
  278. int vga_target_is_screen(void)
  279. {
  280. return vga_target() == vga_screen_surface();
  281. }
  282. void vga_target_damage(unsigned int x, unsigned int y, unsigned int w,
  283. unsigned int h)
  284. {
  285. if (w == 0 || h == 0 || !vga_target_is_screen())
  286. return;
  287. mark_dirty_rect_xy(y, h, x, x + w);
  288. }
  289. /* 16bpp: RGB 5:6:5. 32bpp: BGRX byte order (standard VBE / QEMU). */
  290. static void target_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  291. {
  292. struct surface *t = vga_target();
  293. unsigned int pitch, bpp;
  294. unsigned char *p;
  295. if (!t->px || x >= t->w || y >= t->h)
  296. return;
  297. pitch = t->pitch;
  298. bpp = t->bpp;
  299. p = t->px + (unsigned long)y * pitch;
  300. if (bpp == 16) {
  301. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  302. unsigned int off = x * 2;
  303. p[off] = (unsigned char)(rgb16 & 0xFF);
  304. p[off + 1] = (unsigned char)(rgb16 >> 8);
  305. } else if (bpp == 24) {
  306. unsigned int off = x * 3;
  307. p[off] = (unsigned char)b;
  308. p[off + 1] = (unsigned char)g;
  309. p[off + 2] = (unsigned char)r;
  310. } else if (bpp == 32) {
  311. unsigned int off = x * 4;
  312. p[off] = (unsigned char)b;
  313. p[off + 1] = (unsigned char)g;
  314. p[off + 2] = (unsigned char)r;
  315. p[off + 3] = t->alpha;
  316. }
  317. vga_target_damage(x, y, 1, 1);
  318. }
  319. void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  320. {
  321. vga_cache_boot_fb();
  322. target_put_pixel(x, y, r, g, b);
  323. }
  324. void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  325. unsigned int r, unsigned int g, unsigned int b)
  326. {
  327. vga_cache_boot_fb();
  328. if (!blit || !blit->fill)
  329. return;
  330. blit->fill(x, y, w, h, ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF));
  331. }
  332. static unsigned int vga_pixel_bytes(void)
  333. {
  334. if (vga_fb_bpp_val == 16)
  335. return 2;
  336. if (vga_fb_bpp_val == 24)
  337. return 3;
  338. return 4;
  339. }
  340. /* The software cursor saves and restores pixels of the *screen*: these two
  341. * keep using the shadow buffer directly, never the render target. */
  342. void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  343. unsigned char *buf)
  344. {
  345. unsigned int pitch, pb, row, col;
  346. unsigned char *fb;
  347. const unsigned char *src;
  348. vga_cache_boot_fb();
  349. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  350. return;
  351. pitch = vga_shadow_pitch;
  352. pb = vga_pixel_bytes();
  353. fb = vga_shadow_buf;
  354. for (row = 0; row < h; row++) {
  355. src = fb + (unsigned long)(y + row) * pitch + x * pb;
  356. for (col = 0; col < w * pb; col++)
  357. buf[row * w * pb + col] = src[col];
  358. }
  359. }
  360. void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  361. const unsigned char *buf)
  362. {
  363. unsigned int pitch, pb, row, col;
  364. unsigned char *dst;
  365. vga_cache_boot_fb();
  366. if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
  367. return;
  368. pitch = vga_shadow_pitch;
  369. pb = vga_pixel_bytes();
  370. for (row = 0; row < h; row++) {
  371. dst = vga_shadow_buf + (unsigned long)(y + row) * pitch + x * pb;
  372. for (col = 0; col < w * pb; col++)
  373. dst[col] = buf[row * w * pb + col];
  374. }
  375. {
  376. unsigned int xe = x + w;
  377. if (xe > vga_fb_w)
  378. xe = vga_fb_w;
  379. mark_dirty_rect_xy(y, h, x, xe);
  380. }
  381. }
  382. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
  383. {
  384. struct surface *t;
  385. vga_cache_boot_fb();
  386. t = vga_target();
  387. if (!t->px)
  388. return;
  389. vga_fill_rect(0, 0, t->w, t->h, r, g, b);
  390. }
  391. /* ---- Shadow buffer init and flush ---- */
  392. /* Assembly row copy: rep movsl optimized for P5 USWC write-combining to UC memory. */
  393. extern void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
  394. void vga_shadow_init(void)
  395. {
  396. vga_cache_boot_fb();
  397. if (!vga_fb_lfb || vga_fb_w == 0 || vga_fb_h == 0)
  398. return;
  399. vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
  400. vga_shadow_pitch = vga_fb_pitch_val;
  401. vga_shadow_size = vga_fb_h * vga_shadow_pitch;
  402. screen_surf.px = 0; /* re-derive from the shadow buffer */
  403. damage_clear();
  404. serial_puts(SERIAL_COM1, "blit backend: ");
  405. serial_puts(SERIAL_COM1, blit ? blit->name : "none");
  406. serial_puts(SERIAL_COM1, "\r\n");
  407. }
  408. /* Copy damage rects from the shadow to VRAM, at most about `max_bytes` this
  409. * call (0 = no limit), and drop the rects that are done. A rect that fits
  410. * the budget is copied whole even when that overshoots a little, so a
  411. * window-sized repaint is never split across iterations; only a rect larger
  412. * than the budget (a full-screen repaint) is chunked, and its remaining rows
  413. * move to the back of the list so newer, smaller damage is flushed first. */
  414. static unsigned int shadow_copy_damage(unsigned int max_bytes)
  415. {
  416. unsigned int i = 0, bytes = 0;
  417. unsigned int pb = vga_pixel_bytes();
  418. unsigned int align = (pb == 2) ? 2 : (pb == 3) ? 4 : 1;
  419. unsigned char *src = vga_shadow_buf;
  420. unsigned char *dst = (unsigned char *)vga_fb_lfb;
  421. while (i < damage_count) {
  422. struct damage_rect *d = &damage_rects[i];
  423. unsigned int rx0 = d->x0 & ~(align - 1);
  424. unsigned int rx1 = (d->x1 + align - 1) & ~(align - 1);
  425. unsigned int row_bytes, rect_bytes, dwords, tail, y;
  426. if (rx1 > vga_fb_w)
  427. rx1 = vga_fb_w;
  428. if (rx0 >= rx1) {
  429. damage_rects[i] = damage_rects[--damage_count];
  430. continue;
  431. }
  432. row_bytes = (rx1 - rx0) * pb;
  433. rect_bytes = row_bytes * (d->y1 - d->y0);
  434. dwords = row_bytes / 4;
  435. tail = row_bytes % 4;
  436. /* The budget is spent and this rect is bigger than a whole
  437. call: leave it (rows already copied keep their progress). */
  438. if (max_bytes && bytes > 0 && rect_bytes > max_bytes)
  439. break;
  440. for (y = d->y0; y < d->y1; y++) {
  441. unsigned char *s = src + (unsigned long)y * vga_shadow_pitch + rx0 * pb;
  442. unsigned char *t = dst + (unsigned long)y * vga_fb_pitch_val + rx0 * pb;
  443. vga_fb_copy_row(t, s, dwords);
  444. if (tail) {
  445. unsigned int k;
  446. for (k = dwords * 4; k < row_bytes; k++)
  447. t[k] = s[k];
  448. }
  449. bytes += row_bytes;
  450. if (max_bytes && rect_bytes > max_bytes && bytes >= max_bytes) {
  451. d->y0 = y + 1;
  452. break;
  453. }
  454. }
  455. if (y < d->y1) {
  456. /* Chunked: keep the remaining rows, behind the rest. */
  457. struct damage_rect rest = *d;
  458. damage_rects[i] = damage_rects[--damage_count];
  459. damage_rects[damage_count++] = rest;
  460. return bytes;
  461. }
  462. damage_rects[i] = damage_rects[--damage_count];
  463. if (max_bytes && bytes >= max_bytes)
  464. break;
  465. }
  466. return bytes;
  467. }
  468. /* Copy up to `max_bytes` of the pending damage to VRAM. Called by the main
  469. * loop on every iteration: small updates (a cursor move is ~1 KB) reach the
  470. * screen within one loop instead of waiting for a fixed 30 Hz tick, while a
  471. * full-screen repaint is chunked so it cannot stall input. */
  472. void vga_present_shadow(unsigned int max_bytes)
  473. {
  474. vga_cache_boot_fb();
  475. if (!vga_shadow_buf || !vga_fb_lfb)
  476. return;
  477. if (!damage_any())
  478. return;
  479. shadow_copy_damage(max_bytes);
  480. }
  481. /* Make drawn content visible: the backend's present() (the software one copies
  482. * the pending damage rects to VRAM). */
  483. void vga_fb_flush_budget(unsigned int max_bytes)
  484. {
  485. vga_cache_boot_fb();
  486. if (!blit || !blit->present)
  487. return;
  488. blit->present(max_bytes);
  489. }
  490. void vga_fb_flush(void)
  491. {
  492. vga_fb_flush_budget(0);
  493. }
  494. int vga_shadow_is_active(void)
  495. {
  496. return vga_shadow_buf != 0;
  497. }
  498. void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h)
  499. {
  500. vga_cache_boot_fb();
  501. mark_dirty_rect(y, h);
  502. }
  503. /* Mark a rectangle dirty, limited to the horizontal span [x0, x1). */
  504. void vga_fb_mark_dirty_rect_xy(unsigned int y, unsigned int h,
  505. unsigned int x0, unsigned int x1)
  506. {
  507. vga_cache_boot_fb();
  508. mark_dirty_rect_xy(y, h, x0, x1);
  509. }
  510. void vga_fb_clear_dirty(void)
  511. {
  512. damage_clear();
  513. }