vga.c 19 KB

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