Просмотр исходного кода

Fix the display wrapping on ATI cards

Johannes Zellner 8 месяцев назад
Родитель
Сommit
082990d6cb
8 измененных файлов с 345 добавлено и 9 удалено
  1. 7 4
      Makefile
  2. 113 0
      ati.c
  3. 11 0
      ati.h
  4. 54 4
      boot.S
  5. 33 0
      display.c
  6. 37 0
      pci.h
  7. 86 0
      vga.c
  8. 4 1
      vga.h

+ 7 - 4
Makefile

@@ -56,13 +56,16 @@ cat.o: cat.c fat.h io.h
 rtc.o: rtc.c rtc.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-vga.o: vga.c vga.h
+vga.o: vga.c vga.h pci.h ati.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+ati.o: ati.c ati.h pci.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 clock.o: clock.c io.h rtc.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-display.o: display.c io.h vga.h fbtext.h
+display.o: display.c io.h vga.h ati.h fbtext.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 boot.elf: boot.o
@@ -71,7 +74,7 @@ boot.elf: boot.o
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o vga.o font_8x8.o fbtext.o clock.o display.o
+kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o vga.o ati.o font_8x8.o fbtext.o clock.o display.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -110,4 +113,4 @@ run: image.bin root.img
 	qemu-system-x86_64 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o disk.o vga.o font_8x8.o fbtext.o clock.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img
+	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o disk.o vga.o ati.o font_8x8.o fbtext.o clock.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img

+ 113 - 0
ati.c

@@ -0,0 +1,113 @@
+/*
+ * ATI Mach64 (264VT / 3D Rage) -- GPU-specific CRTC fixups.
+ *
+ * The CRTC_OFF_PITCH register (byte offset 0x14 in the MMIO block) controls
+ * display start offset and scanline pitch.  Standard VGA CRTC writes and
+ * VBE 4F07h are ignored by these chips.  We zero the offset field directly
+ * through PCI MMIO or block I/O.
+ *
+ * CRTC_OFF_PITCH layout (32 bits):
+ *   bits  0-19: CRTC_OFFSET  (display start in 8-byte units)
+ *   bits 22-31: CRTC_PITCH   (scanline pitch in 8-pixel units)
+ *
+ * Access is verified by reading CONFIG_CHIP_ID (byte offset 0xE0) and
+ * checking the lower 16 bits match the PCI device ID.
+ *
+ * Methods tried in order:
+ *   1 - BAR2 separate MMIO aperture
+ *   2 - MMIO at end of LFB aperture (LFB + 8 MB - 1 KB)
+ *   3 - MMIO at end of LFB aperture (LFB + 4 MB - 1 KB)
+ *   4 - BAR1 block I/O
+ */
+
+#include "ati.h"
+#include "pci.h"
+
+#define ATI_VENDOR_ID            0x1002
+#define ATI_CRTC_OFF_PITCH_OFS   0x14
+#define ATI_CONFIG_CHIP_ID_OFS   0xE0
+
+static unsigned int ati_id;
+static unsigned int off_pitch_before;
+static unsigned int off_pitch_after;
+static unsigned int mmio_method;   /* 0 = not found, 1..4 per header */
+
+static int try_mmio(volatile unsigned int *mmio, unsigned int device_id)
+{
+	unsigned int chip_id = mmio[ATI_CONFIG_CHIP_ID_OFS / 4];
+	if ((chip_id & 0xFFFF) != device_id)
+		return 0;
+	off_pitch_before = mmio[ATI_CRTC_OFF_PITCH_OFS / 4];
+	mmio[ATI_CRTC_OFF_PITCH_OFS / 4] = off_pitch_before & 0xFFF00000;
+	off_pitch_after = mmio[ATI_CRTC_OFF_PITCH_OFS / 4];
+	return 1;
+}
+
+void ati_init(void)
+{
+	unsigned int bus, dev, id, class_rev;
+	unsigned int bar0, bar1, bar2;
+	unsigned int device_id;
+
+	for (bus = 0; bus < 4; bus++) {
+		for (dev = 0; dev < 32; dev++) {
+			id = pci_cfg_read32(bus, dev, 0, 0x00);
+			if ((id & 0xFFFF) != ATI_VENDOR_ID)
+				continue;
+			class_rev = pci_cfg_read32(bus, dev, 0, 0x08);
+			if ((class_rev >> 24) != 0x03)
+				continue;
+
+			ati_id = id;
+			device_id = (id >> 16) & 0xFFFF;
+
+			bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
+			bar2 = pci_cfg_read32(bus, dev, 0, 0x18);
+
+			/* Method 1: BAR2 (separate MMIO aperture) */
+			if (!(bar2 & 1) && (bar2 & 0xFFFFFFF0)) {
+				if (try_mmio((volatile unsigned int *)(bar2 & 0xFFFFFFF0),
+					     device_id)) {
+					mmio_method = 1;
+					return;
+				}
+			}
+
+			/* Method 2-3: MMIO at end of LFB aperture (264VT/GT) */
+			if (!(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
+				unsigned int lfb = bar0 & 0xFFFFFFF0;
+				if (try_mmio((volatile unsigned int *)(lfb + 0x7FFC00),
+					     device_id)) {
+					mmio_method = 2;
+					return;
+				}
+				if (try_mmio((volatile unsigned int *)(lfb + 0x3FFC00),
+					     device_id)) {
+					mmio_method = 3;
+					return;
+				}
+			}
+
+			/* Method 4: BAR1 block I/O */
+			bar1 = pci_cfg_read32(bus, dev, 0, 0x14);
+			if ((bar1 & 1) && (bar1 & 0xFFFC)) {
+				unsigned short io = (unsigned short)(bar1 & 0xFFFC);
+				unsigned int chip_id = port_inl(io + ATI_CONFIG_CHIP_ID_OFS);
+				if ((chip_id & 0xFFFF) == device_id) {
+					off_pitch_before = port_inl(io + ATI_CRTC_OFF_PITCH_OFS);
+					port_outl(io + ATI_CRTC_OFF_PITCH_OFS,
+						  off_pitch_before & 0xFFF00000);
+					off_pitch_after = port_inl(io + ATI_CRTC_OFF_PITCH_OFS);
+					mmio_method = 4;
+					return;
+				}
+			}
+			return;
+		}
+	}
+}
+
+unsigned int ati_pci_id(void)          { return ati_id; }
+unsigned int ati_off_pitch_before(void){ return off_pitch_before; }
+unsigned int ati_off_pitch_after(void) { return off_pitch_after; }
+unsigned int ati_mmio_method(void)     { return mmio_method; }

+ 11 - 0
ati.h

@@ -0,0 +1,11 @@
+/* ATI Mach64 / Rage: GPU-specific CRTC fixups via PCI MMIO. */
+#ifndef ATI_H
+#define ATI_H
+
+void ati_init(void);
+unsigned int ati_pci_id(void);
+unsigned int ati_off_pitch_before(void);
+unsigned int ati_off_pitch_after(void);
+unsigned int ati_mmio_method(void);
+
+#endif

+ 54 - 4
boot.S

@@ -23,11 +23,24 @@ loader_start:
 	movw  $0x0003, %ax
 	int   $0x10
 
-	/* VESA: try fixed mode list, use 0x6000 for mode info (256 bytes). */
+	/* VESA: query VRAM size, then try mode list. Buffer at ES:DI = 0:0x6000. */
 	xorw  %ax, %ax
 	movw  %ax, %es
 	movw  $0x6000, %di
-	/* Try modes: 0x11C, 0x11B, 0x118, 0x114 (descending res, >= 16bpp). */
+	/* Get VBE controller info (TotalMemory at offset 0x12, in 64KB blocks) */
+	movw  $0xFFFF, %si	/* default: assume unlimited VRAM */
+	movw  $0x4256, (%di)	/* "VB" */
+	movw  $0x3245, 2(%di)	/* "E2" */
+	movw  $0x4F00, %ax
+	int   $0x10
+	cmpw  $0x004F, %ax
+	jnz   4f
+	movw  0x12(%di), %ax
+	testw %ax, %ax
+	jz    4f
+	movw  %ax, %si		/* SI = VRAM in 64KB blocks */
+4:
+	/* Try modes: 0x11C..0x110 (descending res, >= 16bpp, must fit in VRAM). */
 	movw  $0x11C, %cx
 1:	movw  $0x4F01, %ax
 	int   $0x10
@@ -37,20 +50,33 @@ loader_start:
 	jb    2f
 	testw $0x80, (%di)	/* ModeAttributes bit 7 = LFB */
 	jz    2f
+	/* Check if pitch * height fits in VRAM */
+	movw  0x10(%di), %ax	/* BytesPerScanLine */
+	mulw  0x14(%di)		/* DX:AX = pitch * height */
+	testw %ax, %ax
+	jz    3f
+	incw  %dx		/* round up partial 64KB block */
+3:	cmpw  %si, %dx		/* needed_64k > total_64k? */
+	ja    2f		/* skip: doesn't fit */
 	movw  %cx, %bx
 	orw   $0x4000, %bx	/* Set mode with LFB */
 	movw  $0x4F02, %ax
 	int   $0x10
 	cmpw  $0x004F, %ax
 	jz    vesa_ok
-2:	cmpw  $0x114, %cx
+2:	cmpw  $0x110, %cx
 	jz   vesa_fail
 	subw  $1, %cx		/* 0x11C -> 0x11B -> 0x118 -> 0x114 */
 	jmp   1b
 vesa_fail:
 	jmp   halt
 vesa_ok:
-	movw  $0x6000, %di	/* Reload: int 0x10/4F02 may trash DI */
+	/* Re-query mode info (CX still has mode number). ES:DI = 0:0x6000. */
+	xorw  %ax, %ax
+	movw  %ax, %es
+	movw  $0x6000, %di
+	movw  $0x4F01, %ax
+	int   $0x10
 	movw  $0x600, %bx
 	movb  $1, (%bx)		/* valid = 1 */
 	movw  0x12(%di), %ax
@@ -65,6 +91,7 @@ vesa_ok:
 	movw  %ax, 8(%bx)	/* lfb_base lo */
 	movw  0x2a(%di), %ax
 	movw  %ax, 10(%bx)	/* lfb_base hi */
+	movw  %si, 12(%bx)	/* vram_64k */
 
 load_kernel:
 	/* Load kernel: sectors 1..kernel_sectors to 0x7E00. */
@@ -112,6 +139,23 @@ load_done:
 	movb  boot_drive, %al
 	movb  %al, (%bx)
 
+	/* Enable A20 gate (address bit 20).  Try three methods. */
+	movw  $0x2401, %ax	/* Method 1: BIOS */
+	int   $0x15
+	call  a20_8042_wait	/* Method 2: keyboard controller (8042) */
+	movb  $0xD1, %al
+	outb  %al, $0x64
+	call  a20_8042_wait
+	movb  $0xDF, %al
+	outb  %al, $0x60
+	call  a20_8042_wait
+	inb   $0x92, %al	/* Method 3: fast A20 gate */
+	testb $0x02, %al
+	jnz   5f
+	orb   $0x02, %al
+	andb  $0xFE, %al
+	outb  %al, $0x92
+5:
 	cli
 	lgdt  gdtr
 	movl  %cr0, %eax
@@ -137,6 +181,12 @@ gdtr:
 	.word gdt_end - gdt - 1
 	.long gdt
 
+a20_8042_wait:
+	inb   $0x64, %al
+	testb $0x02, %al
+	jnz   a20_8042_wait
+	ret
+
 halt:
 	cli
 1:	hlt

+ 33 - 0
display.c

@@ -2,6 +2,7 @@
 
 #include "io.h"
 #include "vga.h"
+#include "ati.h"
 #include "fbtext.h"
 
 static void put_uint(unsigned int n)
@@ -55,9 +56,41 @@ void program_display_main(void)
 	io_putstr("pitch ");
 	put_uint(vga_fb_pitch());
 	io_putstr(" bytes/line\n");
+	io_putstr("VRAM ");
+	put_uint(vga_fb_vram_kb());
+	io_putstr(" KB\n");
 	io_putstr("LFB ");
 	put_hex((unsigned int)(unsigned long)vga_fb_ptr());
 	io_putchar('\n');
+	{
+		unsigned int pci_id = vga_fb_pci_id();
+		if (pci_id) {
+			io_putstr("PCI ");
+			put_hex(pci_id & 0xFFFF);
+			io_putchar(':');
+			put_hex(pci_id >> 16);
+			io_putchar('\n');
+		}
+	}
+	{
+		unsigned int method = ati_mmio_method();
+		if (method) {
+			io_putstr("ATI CRTC ");
+			put_hex(ati_off_pitch_before());
+			io_putstr(" -> ");
+			put_hex(ati_off_pitch_after());
+			io_putstr(" via ");
+			if (method == 1)
+				io_putstr("BAR2");
+			else if (method == 2)
+				io_putstr("LFB+8M");
+			else if (method == 3)
+				io_putstr("LFB+4M");
+			else if (method == 4)
+				io_putstr("IO");
+			io_putchar('\n');
+		}
+	}
 	io_putstr("text ");
 	put_uint(fbtext_get_cols());
 	io_putstr(" x ");

+ 37 - 0
pci.h

@@ -0,0 +1,37 @@
+/* PCI configuration space and low-level port I/O helpers. */
+#ifndef PCI_H
+#define PCI_H
+
+static inline void port_outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+static inline unsigned char port_inb(unsigned short port)
+{
+	unsigned char v;
+	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+static inline void port_outl(unsigned short port, unsigned int v)
+{
+	__asm__ volatile ("outl %0, %1" : : "a"(v), "Nd"(port));
+}
+
+static inline unsigned int port_inl(unsigned short port)
+{
+	unsigned int v;
+	__asm__ volatile ("inl %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+static inline unsigned int pci_cfg_read32(unsigned int bus, unsigned int dev,
+					  unsigned int func, unsigned int reg)
+{
+	port_outl(0xCF8, 0x80000000 | (bus << 16) | (dev << 11) |
+		  (func << 8) | (reg & 0xFC));
+	return port_inl(0xCFC);
+}
+
+#endif

+ 86 - 0
vga.c

@@ -1,12 +1,73 @@
 /* VGA: VESA LFB (from boot). */
 
 #include "vga.h"
+#include "pci.h"
+#include "ati.h"
+
+static unsigned int vga_pci_id;  /* vendor(lo16) | device(hi16) */
 
 /* 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;
 
+/*
+ * 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);
+}
+
+/* Find PCI VGA device and cache its ID (for non-ATI cards). */
+static void vga_find_pci_vga(void)
+{
+	unsigned int bus, dev, id, class_rev;
+
+	if (vga_pci_id)
+		return;
+	for (bus = 0; bus < 4; bus++) {
+		for (dev = 0; dev < 32; dev++) {
+			id = pci_cfg_read32(bus, dev, 0, 0x00);
+			if (id == 0xFFFFFFFF || id == 0)
+				continue;
+			class_rev = pci_cfg_read32(bus, dev, 0, 0x08);
+			if ((class_rev >> 24) == 0x03) {
+				vga_pci_id = id;
+				return;
+			}
+		}
+	}
+}
+
 static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
 {
 	volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
@@ -44,6 +105,19 @@ static void vga_cache_boot_fb(void)
 			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;
+	}
+	vga_reset_crtc();
+	ati_init();
+	vga_find_pci_vga();
+	if (ati_pci_id())
+		vga_pci_id = ati_pci_id();
 }
 
 /* ---- LFB API ---- */
@@ -72,6 +146,18 @@ unsigned int vga_fb_bpp(void)
 	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 vga_pci_id;
+}
+
 void *vga_fb_ptr(void)
 {
 	vga_cache_boot_fb();

+ 4 - 1
vga.h

@@ -3,7 +3,7 @@
 #define VGA_H
 
 /* Boot video info at 0x600 (written by boot.S in safe conventional memory).
-   Layout: valid(1) width(2) height(2) pitch(2) bpp(1) lfb_base(4) = 12 bytes. */
+   Layout: valid(1) width(2) height(2) pitch(2) bpp(1) lfb_base(4) vram_64k(2) = 14 bytes. */
 struct vga_boot_fb_info {
 	unsigned char  valid;
 	unsigned short width;
@@ -11,6 +11,7 @@ struct vga_boot_fb_info {
 	unsigned short pitch;
 	unsigned char  bpp;
 	unsigned int   lfb_base;
+	unsigned short vram_64k;   /* total VRAM in 64KB blocks (from VBE) */
 } __attribute__((packed));
 
 #define VGA_BOOT_FB ((volatile struct vga_boot_fb_info *)0x600)
@@ -20,6 +21,8 @@ unsigned int vga_fb_width(void);
 unsigned int vga_fb_height(void);
 unsigned int vga_fb_pitch(void);
 unsigned int vga_fb_bpp(void);
+unsigned int vga_fb_vram_kb(void);
+unsigned int vga_fb_pci_id(void);
 void *vga_fb_ptr(void);
 int vga_has_vesa(void);