vga.c 23 KB

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