| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- /* VGA: VESA LFB (from boot) with shadow buffer and 30Hz USWC flush. */
- #include "vga.h"
- #include "pci.h"
- #include "gpu.h"
- #include "ati.h"
- /* Cached boot framebuffer info (set once when vga_fb_* first used). */
- static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
- static unsigned int vga_fb_vram_kb_val;
- static volatile unsigned char *vga_fb_lfb;
- static int vga_fb_cached;
- /* Shadow buffer state. */
- static unsigned char *vga_shadow_buf;
- static unsigned int vga_shadow_pitch;
- static unsigned int vga_shadow_size;
- /* Dirty row bitmap: one bit per row. Max 2048 rows = 256 bytes. */
- #define MAX_DIRTY_ROWS 2048
- static unsigned char vga_shadow_dirty_rows[MAX_DIRTY_ROWS / 8];
- static void mark_dirty_row(unsigned int y)
- {
- if (y < MAX_DIRTY_ROWS)
- vga_shadow_dirty_rows[y / 8] |= (1 << (y % 8));
- }
- static void mark_dirty_rect(unsigned int y, unsigned int h)
- {
- unsigned int i;
- for (i = 0; i < h; i++)
- mark_dirty_row(y + i);
- }
- static int any_dirty_rows(void)
- {
- unsigned int i;
- unsigned int max_row = vga_fb_h;
- if (max_row > MAX_DIRTY_ROWS)
- max_row = MAX_DIRTY_ROWS;
- for (i = 0; i < (max_row + 7) / 8; i++)
- if (vga_shadow_dirty_rows[i])
- return 1;
- return 0;
- }
- static void clear_dirty_rows(void)
- {
- unsigned int i;
- unsigned int max_row = vga_fb_h;
- if (max_row > MAX_DIRTY_ROWS)
- max_row = MAX_DIRTY_ROWS;
- for (i = 0; i < (max_row + 7) / 8; i++)
- vga_shadow_dirty_rows[i] = 0;
- }
- /*
- * Force CRTC Start Address to 0 and Line Compare to max (0x3FF).
- * Fixes real hardware where VBE mode set leaves stale CRTC state,
- * causing the display to appear rotated (bottom portion at top).
- */
- static void vga_reset_crtc(void)
- {
- unsigned char v;
- /* Start Address High/Low = 0 */
- port_outb(0x3D4, 0x0C);
- port_outb(0x3D5, 0x00);
- port_outb(0x3D4, 0x0D);
- port_outb(0x3D5, 0x00);
- /* Unlock CRTC registers 0-7: clear bit 7 of register 0x11 */
- port_outb(0x3D4, 0x11);
- v = port_inb(0x3D5);
- port_outb(0x3D5, v & 0x7F);
- /* Line Compare bits 7:0 = 0xFF (register 0x18) */
- port_outb(0x3D4, 0x18);
- port_outb(0x3D5, 0xFF);
- /* Line Compare bit 8 = 1 (register 0x07, bit 4) */
- port_outb(0x3D4, 0x07);
- v = port_inb(0x3D5);
- port_outb(0x3D5, v | 0x10);
- /* Line Compare bit 9 = 1 (register 0x09, bit 6) */
- port_outb(0x3D4, 0x09);
- v = port_inb(0x3D5);
- port_outb(0x3D5, v | 0x40);
- }
- static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
- {
- volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
- __asm__ volatile ("" : "+r"(p));
- return p;
- }
- static void vga_cache_boot_fb(void)
- {
- volatile struct vga_boot_fb_info *info;
- if (vga_fb_cached)
- return;
- vga_fb_cached = 1;
- info = vga_get_boot_fb();
- if (!info->valid) {
- vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
- vga_fb_lfb = 0;
- gpu_init();
- return;
- }
- vga_fb_w = info->width;
- vga_fb_h = info->height;
- vga_fb_pitch_val = info->pitch;
- vga_fb_bpp_val = info->bpp;
- /* Some BIOSes (e.g. 1024x768) report pitch in pixels or too small; enforce minimum. */
- {
- unsigned int min_pitch = vga_fb_w;
- if (vga_fb_bpp_val == 16)
- min_pitch *= 2;
- else if (vga_fb_bpp_val == 24)
- min_pitch *= 3;
- else if (vga_fb_bpp_val == 32)
- min_pitch *= 4;
- if (vga_fb_pitch_val < min_pitch)
- vga_fb_pitch_val = min_pitch;
- }
- vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
- vga_fb_vram_kb_val = (unsigned int)info->vram_64k * 64;
- /* Clamp height to what fits in VRAM (prevents wrap on small VRAM cards) */
- if (vga_fb_vram_kb_val > 0 && vga_fb_pitch_val > 0) {
- unsigned int vram_bytes = vga_fb_vram_kb_val * 1024;
- unsigned int max_h = vram_bytes / vga_fb_pitch_val;
- if (max_h < vga_fb_h)
- vga_fb_h = max_h;
- }
- gpu_init();
- if (gpu_vendor() == GPU_VENDOR_ATI)
- vga_reset_crtc();
- }
- /* ---- LFB API ---- */
- unsigned int vga_fb_width(void)
- {
- vga_cache_boot_fb();
- return vga_fb_w;
- }
- unsigned int vga_fb_height(void)
- {
- vga_cache_boot_fb();
- return vga_fb_h;
- }
- unsigned int vga_fb_pitch(void)
- {
- vga_cache_boot_fb();
- return vga_fb_pitch_val;
- }
- unsigned int vga_fb_bpp(void)
- {
- vga_cache_boot_fb();
- return vga_fb_bpp_val;
- }
- unsigned int vga_fb_vram_kb(void)
- {
- vga_cache_boot_fb();
- return vga_fb_vram_kb_val;
- }
- unsigned int vga_fb_pci_id(void)
- {
- vga_cache_boot_fb();
- return gpu_pci_id();
- }
- void *vga_fb_ptr(void)
- {
- vga_cache_boot_fb();
- if (vga_shadow_buf)
- return (void *)vga_shadow_buf;
- return (void *)vga_fb_lfb;
- }
- int vga_has_vesa(void)
- {
- vga_cache_boot_fb();
- return (vga_fb_w != 0 && vga_fb_lfb != 0);
- }
- /* 16bpp: RGB 5:6:5. 32bpp: BGRX byte order (standard VBE / QEMU). */
- static void shadow_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
- {
- unsigned int pitch = vga_shadow_pitch;
- unsigned int bpp = vga_fb_bpp_val;
- unsigned char *p;
- if (x >= vga_fb_w || y >= vga_fb_h || !vga_shadow_buf)
- return;
- p = vga_shadow_buf + (unsigned long)y * pitch;
- if (bpp == 16) {
- unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
- unsigned int off = x * 2;
- p[off] = (unsigned char)(rgb16 & 0xFF);
- p[off + 1] = (unsigned char)(rgb16 >> 8);
- } else if (bpp == 24) {
- unsigned int off = x * 3;
- p[off] = (unsigned char)b;
- p[off + 1] = (unsigned char)g;
- p[off + 2] = (unsigned char)r;
- } else if (bpp == 32) {
- unsigned int off = x * 4;
- p[off] = (unsigned char)b;
- p[off + 1] = (unsigned char)g;
- p[off + 2] = (unsigned char)r;
- p[off + 3] = 0;
- }
- mark_dirty_row(y);
- }
- void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
- {
- vga_cache_boot_fb();
- if (!vga_shadow_buf)
- return;
- shadow_put_pixel(x, y, r, g, b);
- }
- void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
- unsigned int r, unsigned int g, unsigned int b)
- {
- unsigned int row, x1, y1;
- unsigned int pitch;
- unsigned int bpp;
- unsigned char *fb;
- vga_cache_boot_fb();
- pitch = vga_shadow_pitch;
- bpp = vga_fb_bpp_val;
- fb = vga_shadow_buf;
- if (!fb || w == 0 || h == 0)
- return;
- if (x >= vga_fb_w || y >= vga_fb_h)
- return;
- x1 = x + w;
- y1 = y + h;
- if (x1 > vga_fb_w) x1 = vga_fb_w;
- if (y1 > vga_fb_h) y1 = vga_fb_h;
- if (bpp == 16) {
- unsigned short val = (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
- for (row = y; row < y1; row++) {
- unsigned short *p = (unsigned short *)(fb + (unsigned long)row * pitch + x * 2);
- unsigned int xx;
- for (xx = x; xx < x1; xx++)
- *p++ = val;
- }
- } else if (bpp == 24) {
- for (row = y; row < y1; row++) {
- unsigned char *p = fb + (unsigned long)row * pitch + x * 3;
- unsigned int xx;
- for (xx = x; xx < x1; xx++) {
- p[0] = (unsigned char)b;
- p[1] = (unsigned char)g;
- p[2] = (unsigned char)r;
- p += 3;
- }
- }
- } else {
- unsigned int val = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
- for (row = y; row < y1; row++) {
- unsigned int *p = (unsigned int *)(fb + (unsigned long)row * pitch + x * 4);
- unsigned int xx;
- for (xx = x; xx < x1; xx++)
- *p++ = val;
- }
- }
- mark_dirty_rect(y, y1 - y);
- }
- static unsigned int vga_pixel_bytes(void)
- {
- if (vga_fb_bpp_val == 16)
- return 2;
- if (vga_fb_bpp_val == 24)
- return 3;
- return 4;
- }
- void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
- unsigned char *buf)
- {
- unsigned int pitch, pb, row, col;
- unsigned char *fb;
- const unsigned char *src;
- vga_cache_boot_fb();
- if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
- return;
- pitch = vga_shadow_pitch;
- pb = vga_pixel_bytes();
- fb = vga_shadow_buf;
- for (row = 0; row < h; row++) {
- src = fb + (unsigned long)(y + row) * pitch + x * pb;
- for (col = 0; col < w * pb; col++)
- buf[row * w * pb + col] = src[col];
- }
- }
- void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
- const unsigned char *buf)
- {
- unsigned int pitch, pb, row, col;
- unsigned char *dst;
- vga_cache_boot_fb();
- if (!vga_shadow_buf || vga_fb_w == 0 || w == 0 || h == 0)
- return;
- pitch = vga_shadow_pitch;
- pb = vga_pixel_bytes();
- for (row = 0; row < h; row++) {
- dst = vga_shadow_buf + (unsigned long)(y + row) * pitch + x * pb;
- for (col = 0; col < w * pb; col++)
- dst[col] = buf[row * w * pb + col];
- }
- mark_dirty_rect(y, h);
- }
- void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
- {
- unsigned int pitch = vga_shadow_pitch;
- unsigned int bpp = vga_fb_bpp_val;
- vga_cache_boot_fb();
- if (!vga_shadow_buf)
- return;
- if (bpp == 16) {
- unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
- unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
- unsigned short *fb = (unsigned short *)vga_shadow_buf;
- unsigned int n = (pitch / 2) * vga_fb_h;
- unsigned int i;
- for (i = 0; i < n; i++)
- fb[i] = val;
- } else if (bpp == 24) {
- unsigned char *p = vga_shadow_buf;
- unsigned int row, x;
- for (row = 0; row < vga_fb_h; row++) {
- unsigned int row_off = row * pitch;
- for (x = 0; x < vga_fb_w; x++) {
- unsigned int off = row_off + x * 3;
- p[off] = (unsigned char)b;
- p[off + 1] = (unsigned char)g;
- p[off + 2] = (unsigned char)r;
- }
- }
- } else if (bpp == 32) {
- unsigned int *fb = (unsigned int *)vga_shadow_buf;
- unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
- unsigned int stride = pitch / 4;
- unsigned int row, x;
- for (row = 0; row < vga_fb_h; row++)
- for (x = 0; x < vga_fb_w; x++)
- fb[row * stride + x] = pixel;
- }
- mark_dirty_rect(0, vga_fb_h);
- }
- /* ---- Shadow buffer init and flush ---- */
- /* Assembly row copy: rep movsl optimized for P5 USWC write-combining to UC memory. */
- extern void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
- int vga_shadow_flush_pending;
- void vga_shadow_init(void)
- {
- vga_cache_boot_fb();
- if (!vga_fb_lfb || vga_fb_w == 0 || vga_fb_h == 0)
- return;
- vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
- vga_shadow_pitch = vga_fb_pitch_val;
- vga_shadow_size = vga_fb_h * vga_shadow_pitch;
- vga_shadow_flush_pending = 0;
- clear_dirty_rows();
- }
- /* Copy only dirty rows from shadow to VRAM. */
- static void shadow_copy_dirty_rows(void)
- {
- unsigned int row;
- unsigned int pb = vga_pixel_bytes();
- unsigned int row_bytes = vga_fb_w * pb;
- unsigned int dwords = row_bytes / 4;
- unsigned char *src = vga_shadow_buf;
- unsigned char *dst = (unsigned char *)vga_fb_lfb;
- unsigned int max_row = vga_fb_h;
- if (max_row > MAX_DIRTY_ROWS)
- max_row = MAX_DIRTY_ROWS;
- for (row = 0; row < max_row; row++) {
- if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
- vga_fb_copy_row(
- (void *)(dst + (unsigned long)row * vga_fb_pitch_val),
- (void *)(src + (unsigned long)row * vga_shadow_pitch),
- dwords
- );
- }
- }
- }
- /* Debug square: 32x32 at bottom-right, cycles color each flush. Drawn into shadow buffer
- so it gets copied to VRAM along with everything else. Uses bright colors to be obvious. */
- static void draw_flush_debug_square(void)
- {
- static unsigned int flush_color_counter;
- unsigned int r, g, b;
- unsigned int x, y;
- unsigned int sx = vga_fb_w - 32;
- unsigned int sy = vga_fb_h - 32;
- unsigned int pitch = vga_shadow_pitch;
- unsigned int bpp = vga_fb_bpp_val;
- unsigned char *fb = vga_shadow_buf;
- flush_color_counter++;
- /* Cycle through bright primary/secondary colors */
- switch (flush_color_counter % 6) {
- case 0: r = 255; g = 0; b = 0; break; /* red */
- case 1: r = 0; g = 255; b = 0; break; /* green */
- case 2: r = 0; g = 0; b = 255; break; /* blue */
- case 3: r = 255; g = 255; b = 0; break; /* yellow */
- case 4: r = 255; g = 0; b = 255; break; /* magenta */
- default: r = 0; g = 255; b = 255; break; /* cyan */
- }
- for (y = 0; y < 32; y++) {
- unsigned char *row = fb + (sy + y) * pitch;
- for (x = 0; x < 32; x++) {
- if (bpp == 16) {
- unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
- unsigned short *p = (unsigned short *)(row + (sx + x) * 2);
- *p = (unsigned short)rgb16;
- } else if (bpp == 24) {
- row[(sx + x) * 3] = (unsigned char)b;
- row[(sx + x) * 3 + 1] = (unsigned char)g;
- row[(sx + x) * 3 + 2] = (unsigned char)r;
- } else if (bpp == 32) {
- unsigned int *p = (unsigned int *)(row + (sx + x) * 4);
- *p = (unsigned int)b | ((unsigned int)g << 8) | ((unsigned int)r << 16);
- }
- }
- }
- mark_dirty_rect(sy, 32);
- }
- void vga_fb_flush_pending_check(void)
- {
- if (!vga_shadow_flush_pending)
- return;
- vga_shadow_flush_pending = 0;
- if (!any_dirty_rows())
- return;
- draw_flush_debug_square();
- shadow_copy_dirty_rows();
- clear_dirty_rows();
- }
- void vga_fb_flush(void)
- {
- vga_cache_boot_fb();
- if (!vga_shadow_buf || !vga_fb_lfb)
- return;
- if (!any_dirty_rows())
- return;
- draw_flush_debug_square();
- shadow_copy_dirty_rows();
- clear_dirty_rows();
- }
- int vga_shadow_is_active(void)
- {
- return vga_shadow_buf != 0;
- }
- void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h)
- {
- mark_dirty_rect(y, h);
- }
|