vga.c 18 KB

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