vga.c 23 KB

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