Răsfoiți Sursa

Attempt to add nvidia support

Johannes Zellner 8 luni în urmă
părinte
comite
ef4f44e67e
13 a modificat fișierele cu 426 adăugiri și 168 ștergeri
  1. 17 5
      Makefile
  2. 4 129
      boot.S
  3. 5 2
      display.c
  4. 64 0
      gpu.c
  5. 20 0
      gpu.h
  6. 4 1
      kernel.c
  7. 3 3
      kernel.ld
  8. 19 0
      nvidia.c
  9. 7 0
      nvidia.h
  10. 58 0
      serial.c
  11. 11 0
      serial.h
  12. 208 0
      vesa_stage.S
  13. 6 28
      vga.c

+ 17 - 5
Makefile

@@ -20,7 +20,7 @@ all: floppy.img usb.img final.bin root.img
 boot.o: boot.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.o: kernel.c io.h process.h terminal.h timer.h
+kernel.o: kernel.c io.h process.h terminal.h timer.h serial.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 io.o: io.c io.h fbtext.h
@@ -59,24 +59,36 @@ run.o: run.c fat.h io.h fbtext.h run_api.h timer.h vga.h
 rtc.o: rtc.c rtc.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-vga.o: vga.c vga.h pci.h ati.h
+serial.o: serial.c serial.h pci.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+vga.o: vga.c vga.h pci.h gpu.h ati.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+gpu.o: gpu.c gpu.h pci.h ati.h nvidia.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 ati.o: ati.c ati.h pci.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+nvidia.o: nvidia.c nvidia.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 clock.o: clock.c io.h rtc.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 colors.o: colors.c fbtext.h io.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-display.o: display.c io.h vga.h ati.h fbtext.h
+display.o: display.c io.h vga.h gpu.h ati.h fbtext.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 boot.elf: boot.o
 	$(LD) $(BOOT_LDFLAGS) -o $@ $<
 
+vesa_stage.o: vesa_stage.S
+	$(AS) $(ASFLAGS) -o $@ $<
+
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
@@ -86,7 +98,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -167,4 +179,4 @@ run-usb: usb.img root.img
 	qemu-system-x86_64 -M pc -boot c -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin

+ 4 - 129
boot.S

@@ -1,4 +1,5 @@
-/* Boot sector: load contiguous kernel sectors 1..N to 0x7E00, then PM. No FAT. */
+/* Boot sector: load contiguous kernel sectors 1..N to 0x7E00,
+   then jump to 0x7E00 in real mode (VESA stage handles the rest). */
 
 .code16
 .section .text
@@ -23,78 +24,7 @@ loader_start:
 	movw  $0x0003, %ax
 	int   $0x10
 
-	/* VESA: query VRAM size, then try mode list. Buffer at ES:DI = 0:0x6000. */
-	xorw  %ax, %ax
-	movw  %ax, %es
-	movw  $0x6000, %di
-	/* 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
-	cmpw  $0x004F, %ax
-	jnz   2f
-	cmpb  $16, 0x19(%di)	/* BitsPerPixel >= 16 */
-	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  $0x110, %cx
-	jz   vesa_fail
-	subw  $1, %cx		/* 0x11C -> 0x11B -> 0x118 -> 0x114 */
-	jmp   1b
-vesa_fail:
-	jmp   halt
-vesa_ok:
-	/* 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
-	movw  %ax, 1(%bx)	/* width */
-	movw  0x14(%di), %ax
-	movw  %ax, 3(%bx)	/* height */
-	movw  0x10(%di), %ax
-	movw  %ax, 5(%bx)	/* pitch */
-	movb  0x19(%di), %al
-	movb  %al, 7(%bx)	/* bpp */
-	movw  0x28(%di), %ax
-	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. */
 	movw  kernel_sectors, %cx
 	movw  $1, %si
 	movw  $0x07e0, %ax
@@ -109,7 +39,6 @@ load_loop:
 	movb  boot_drive, %dl
 	cmpb  $0x80, %dl
 	jb    read_chs
-	/* LBA read via INT 13h AH=42h. DAP at 0:0x500. */
 	xorw  %ax, %ax
 	movw  %ax, 0x50a
 	movw  %ax, 0x50c
@@ -154,70 +83,16 @@ do_int13:
 	jnz   load_loop
 
 load_done:
-	movb  boot_drive, %al
-	movb  %al, 0x600
-
-	/* 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
-	orl   $1, %eax
-	movl  %eax, %cr0
-	.byte 0x66, 0xea
-	.long pm_start
-	.word 0x08
+	/* Jump to VESA stage at 0x7E00 (real mode). */
+	ljmp  $0x0000, $0x7E00
 
 boot_drive:
 	.byte 0
-	.align 2
-gdt:
-	.quad 0
-	.word 0xffff, 0x0000
-	.byte 0x00, 0x9a, 0xcf, 0x00
-	.word 0xffff, 0x0000
-	.byte 0x00, 0x92, 0xcf, 0x00
-gdt_end:
-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
 	jmp   1b
 
-.code32
-pm_start:
-	movw  $0x10, %ax
-	movw  %ax, %ds
-	movw  %ax, %es
-	movw  %ax, %fs
-	movw  %ax, %gs
-	movw  %ax, %ss
-	movl  $0x20800, %esp
-	ljmp  $0x08, $0x7e00
-
 .section .bootsig
 .byte 0x55, 0xaa

+ 5 - 2
display.c

@@ -2,6 +2,7 @@
 
 #include "io.h"
 #include "vga.h"
+#include "gpu.h"
 #include "ati.h"
 #include "fbtext.h"
 
@@ -65,14 +66,16 @@ void program_display_main(void)
 	{
 		unsigned int pci_id = vga_fb_pci_id();
 		if (pci_id) {
-			io_putstr("PCI ");
+			io_putstr("GPU ");
+			io_putstr(gpu_vendor_name());
+			io_putstr(" PCI ");
 			put_hex(pci_id & 0xFFFF);
 			io_putchar(':');
 			put_hex(pci_id >> 16);
 			io_putchar('\n');
 		}
 	}
-	{
+	if (gpu_vendor() == GPU_VENDOR_ATI) {
 		unsigned int method = ati_mmio_method();
 		if (method) {
 			io_putstr("ATI CRTC ");

+ 64 - 0
gpu.c

@@ -0,0 +1,64 @@
+/* GPU vendor detection and vendor-specific init dispatch. */
+
+#include "gpu.h"
+#include "pci.h"
+#include "ati.h"
+#include "nvidia.h"
+
+static int gpu_vendor_id;
+static unsigned int gpu_pci_id_val;
+
+void gpu_init(void)
+{
+	unsigned int bus, dev, id, class_rev;
+	unsigned int vendor;
+
+	for (bus = 0; bus < 256; 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)
+				continue;
+
+			gpu_pci_id_val = id;
+			vendor = id & 0xFFFF;
+
+			if (vendor == PCI_VENDOR_ATI) {
+				gpu_vendor_id = GPU_VENDOR_ATI;
+				ati_init();
+			} else if (vendor == PCI_VENDOR_NVIDIA) {
+				gpu_vendor_id = GPU_VENDOR_NVIDIA;
+				nvidia_init();
+			} else if (vendor == PCI_VENDOR_INTEL) {
+				gpu_vendor_id = GPU_VENDOR_INTEL;
+			} else {
+				gpu_vendor_id = GPU_VENDOR_OTHER;
+			}
+			return;
+		}
+	}
+	gpu_vendor_id = GPU_VENDOR_UNKNOWN;
+}
+
+int gpu_vendor(void)
+{
+	return gpu_vendor_id;
+}
+
+unsigned int gpu_pci_id(void)
+{
+	return gpu_pci_id_val;
+}
+
+const char *gpu_vendor_name(void)
+{
+	switch (gpu_vendor_id) {
+	case GPU_VENDOR_ATI:     return "ATI/AMD";
+	case GPU_VENDOR_NVIDIA:  return "NVIDIA";
+	case GPU_VENDOR_INTEL:   return "Intel";
+	case GPU_VENDOR_OTHER:   return "Other";
+	default:                 return "Unknown";
+	}
+}

+ 20 - 0
gpu.h

@@ -0,0 +1,20 @@
+/* GPU vendor detection and unified init. */
+#ifndef GPU_H
+#define GPU_H
+
+#define GPU_VENDOR_UNKNOWN  0
+#define GPU_VENDOR_ATI      1
+#define GPU_VENDOR_NVIDIA   2
+#define GPU_VENDOR_INTEL    3
+#define GPU_VENDOR_OTHER    4
+
+#define PCI_VENDOR_ATI      0x1002
+#define PCI_VENDOR_NVIDIA   0x10DE
+#define PCI_VENDOR_INTEL    0x8086
+
+void gpu_init(void);
+int gpu_vendor(void);
+unsigned int gpu_pci_id(void);
+const char *gpu_vendor_name(void);
+
+#endif

+ 4 - 1
kernel.c

@@ -4,6 +4,8 @@
 #include "process.h"
 #include "terminal.h"
 #include "timer.h"
+#include "serial.h"
+#include "vga.h"
 
 static inline void outb(unsigned short port, unsigned char v)
 {
@@ -117,13 +119,14 @@ static void timer_init(void)
 
 extern unsigned char __bss_start[], __bss_end[];
 
-void kernel_main(void) __attribute__((section(".text.entry")));
+void kernel_main(void);
 void kernel_main(void)
 {
 	unsigned char *p = __bss_start;
 	while (p < __bss_end)
 		*p++ = 0;
 
+	serial_init();
 	io_init();
 	timer_init();
 	process_init();

+ 3 - 3
kernel.ld

@@ -1,11 +1,11 @@
-/* Kernel runs at 0x7E00; entry section first so 0x7E00 = kernel_main */
-ENTRY(kernel_main)
+/* VESA real-mode stage at 0x7E00, then kernel_main follows. */
+ENTRY(_vesa_stage)
 
 SECTIONS
 {
 	. = 0x7E00;
 	.text : AT(0x7E00) {
-		*(.text.entry)
+		*(.text.stage2)
 		*(.text .text.*)
 	}
 	.rodata : { *(.rodata .rodata.*) }

+ 19 - 0
nvidia.c

@@ -0,0 +1,19 @@
+/*
+ * NVIDIA GPU init.
+ *
+ * Unlike ATI Mach64/Rage which need manual CRTC_OFF_PITCH fixups,
+ * NVIDIA GeForce cards (especially Kepler and newer like GTX 960/GM206)
+ * handle VBE mode sets cleanly through their VBIOS.  The standard VGA
+ * CRTC registers at 0x3D4/0x3D5 should NOT be touched -- NVIDIA's
+ * display engine ignores or misinterprets legacy VGA CRTC writes,
+ * which can cause display corruption or system reboot.
+ *
+ * For now, this is intentionally a no-op.  The VBE mode set from
+ * boot.S provides a working LFB and the card needs no further fixup.
+ */
+
+#include "nvidia.h"
+
+void nvidia_init(void)
+{
+}

+ 7 - 0
nvidia.h

@@ -0,0 +1,7 @@
+/* NVIDIA GPU init: safe post-VBE setup for GeForce cards. */
+#ifndef NVIDIA_H
+#define NVIDIA_H
+
+void nvidia_init(void);
+
+#endif

+ 58 - 0
serial.c

@@ -0,0 +1,58 @@
+/* Serial port (COM1 0x3F8) debug output for bare-metal diagnostics. */
+
+#include "serial.h"
+#include "pci.h"
+
+#define COM1 0x3F8
+
+void serial_init(void)
+{
+	port_outb(COM1 + 1, 0x00);   /* disable interrupts */
+	port_outb(COM1 + 3, 0x80);   /* DLAB on */
+	port_outb(COM1 + 0, 0x0C);   /* divisor 12 = 9600 baud */
+	port_outb(COM1 + 1, 0x00);
+	port_outb(COM1 + 3, 0x03);   /* 8N1, DLAB off */
+	port_outb(COM1 + 2, 0xC7);   /* enable FIFO */
+	port_outb(COM1 + 4, 0x03);   /* RTS + DTR */
+}
+
+void serial_putc(char c)
+{
+	while (!(port_inb(COM1 + 5) & 0x20))
+		;
+	port_outb(COM1, (unsigned char)c);
+}
+
+void serial_puts(const char *s)
+{
+	while (*s) {
+		if (*s == '\n')
+			serial_putc('\r');
+		serial_putc(*s++);
+	}
+}
+
+void serial_puthex(unsigned int n)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	int i;
+	serial_puts("0x");
+	for (i = 28; i >= 0; i -= 4)
+		serial_putc(hex[(n >> i) & 0xF]);
+}
+
+void serial_putdec(unsigned int n)
+{
+	char buf[12];
+	char *p = buf + sizeof(buf) - 1;
+	*p = '\0';
+	if (n == 0) {
+		serial_putc('0');
+		return;
+	}
+	while (n) {
+		*--p = (char)('0' + (n % 10));
+		n /= 10;
+	}
+	serial_puts(p);
+}

+ 11 - 0
serial.h

@@ -0,0 +1,11 @@
+/* Serial port (COM1) debug output. */
+#ifndef SERIAL_H
+#define SERIAL_H
+
+void serial_init(void);
+void serial_putc(char c);
+void serial_puts(const char *s);
+void serial_puthex(unsigned int n);
+void serial_putdec(unsigned int n);
+
+#endif

+ 208 - 0
vesa_stage.S

@@ -0,0 +1,208 @@
+/*
+ * VESA real-mode stage: runs at 0x7E00 before protected mode.
+ *
+ * Called by the boot sector in real mode (CS=0, IP=0x7E00).
+ * Walks the VBE mode list from the BIOS, picks the best mode
+ * (highest resolution, >= 16 bpp, LFB, fits in VRAM), writes
+ * the mode info to 0x600, enables A20, enters PM, jumps to
+ * kernel_main.
+ *
+ * If no VESA mode works, boots in text mode (valid=0 at 0x600).
+ */
+
+.code16
+.section .text.stage2, "ax", @progbits
+.globl _vesa_stage
+_vesa_stage:
+	xorw  %ax, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+
+	/* ---- VBE Controller Info into 0:0x2000 ---- */
+	movw  $0x2000, %di
+	movw  $0x4256, (%di)	/* "VB" */
+	movw  $0x3245, 2(%di)	/* "E2" */
+	movw  $0x4F00, %ax
+	int   $0x10
+	cmpw  $0x004F, %ax
+	jnz   vesa_no_vbe
+
+	/* Save VRAM size (offset 0x12: TotalMemory in 64KB blocks). */
+	movw  0x12(%di), %ax
+	testw %ax, %ax
+	jnz   1f
+	movw  $0xFFFF, %ax
+1:	movw  %ax, vram_64k
+
+	/* Get mode list pointer (offset 0x0E: far pointer seg:off). */
+	movw  0x200E, %si	/* offset (buffer at 0x2000) */
+	movw  0x2010, %ax	/* segment */
+	movw  %ax, %fs		/* FS:SI = mode list */
+
+	/* ---- Walk mode list, pick best mode ---- */
+	movw  $0, best_mode
+	movl  $0, best_score
+
+walk_modes:
+	movw  %fs:(%si), %cx
+	cmpw  $0xFFFF, %cx
+	jz    walk_done
+	addw  $2, %si
+
+	/* Query mode info: VBE 4F01h into 0:0x2200. */
+	pushw %si
+	pushw %cx
+	xorw  %ax, %ax
+	movw  %ax, %es
+	movw  $0x2200, %di
+	movw  $0x4F01, %ax
+	int   $0x10
+	popw  %cx
+	popw  %si
+	cmpw  $0x004F, %ax
+	jnz   walk_modes
+
+	/* Check: supported (bit 0) + graphics (bit 4) + LFB (bit 7). */
+	movw  0x2200, %ax
+	andw  $0x0091, %ax
+	cmpw  $0x0091, %ax
+	jnz   walk_modes
+
+	/* Check: BitsPerPixel >= 16. */
+	cmpb  $16, 0x2219
+	jb    walk_modes
+
+	/* Check: fits in VRAM. */
+	movw  0x2210, %ax
+	mulw  0x2214
+	testw %ax, %ax
+	jz    2f
+	incw  %dx
+2:	cmpw  vram_64k, %dx
+	ja    walk_modes
+
+	/* Score = width * height + bpp. */
+	movzwl 0x2212, %eax
+	movzwl 0x2214, %edx
+	imull  %edx, %eax
+	movzbl 0x2219, %edx
+	addl   %edx, %eax
+	cmpl   best_score, %eax
+	jbe    walk_modes
+
+	movl   %eax, best_score
+	movw   %cx, best_mode
+
+	/* Save mode info block to 0:0x2400. */
+	pushw %si
+	pushw %cx
+	movw  $0x2200, %si
+	movw  $0x2400, %di
+	movw  $128, %cx
+	cld
+	rep movsw
+	popw  %cx
+	popw  %si
+	jmp   walk_modes
+
+walk_done:
+	/* Did we find a mode? */
+	movw  best_mode, %cx
+	testw %cx, %cx
+	jz    vesa_no_vbe
+
+	/* ---- Set the mode ---- */
+	movw  %cx, %bx
+	orw   $0x4000, %bx	/* LFB */
+	movw  $0x4F02, %ax
+	int   $0x10
+	cmpw  $0x004F, %ax
+	jnz   vesa_no_vbe
+
+	/* ---- Fill 0x600 boot FB info from saved block at 0x2400 ---- */
+	movw  $0x600, %bx
+	movb  $1, (%bx)
+	movw  0x2412, %ax
+	movw  %ax, 1(%bx)		/* width */
+	movw  0x2414, %ax
+	movw  %ax, 3(%bx)		/* height */
+	movw  0x2410, %ax
+	movw  %ax, 5(%bx)		/* pitch */
+	movb  0x2419, %al
+	movb  %al, 7(%bx)		/* bpp */
+	movw  0x2428, %ax
+	movw  %ax, 8(%bx)		/* lfb_base lo */
+	movw  0x242A, %ax
+	movw  %ax, 10(%bx)		/* lfb_base hi */
+	movw  vram_64k, %ax
+	movw  %ax, 12(%bx)		/* vram_64k */
+	jmp   enter_pm
+
+vesa_no_vbe:
+	/* No VESA: stay in text mode, mark fb info invalid. */
+	movw  $0x600, %bx
+	movb  $0, (%bx)
+
+enter_pm:
+	/* Enable A20 gate. */
+	movw  $0x2401, %ax
+	int   $0x15
+	call  a20_wait
+	movb  $0xD1, %al
+	outb  %al, $0x64
+	call  a20_wait
+	movb  $0xDF, %al
+	outb  %al, $0x60
+	call  a20_wait
+	inb   $0x92, %al
+	testb $0x02, %al
+	jnz   1f
+	orb   $0x02, %al
+	andb  $0xFE, %al
+	outb  %al, $0x92
+1:
+	cli
+	lgdt  stage2_gdtr
+	movl  %cr0, %eax
+	orl   $1, %eax
+	movl  %eax, %cr0
+	.byte 0x66, 0xea
+	.long stage2_pm
+	.word 0x08
+
+a20_wait:
+	inb   $0x64, %al
+	testb $0x02, %al
+	jnz   a20_wait
+	ret
+
+.code32
+stage2_pm:
+	movw  $0x10, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+	movw  %ax, %fs
+	movw  %ax, %gs
+	movw  %ax, %ss
+	movl  $0x20800, %esp
+	jmp   kernel_main
+
+.align 4
+stage2_gdt:
+	.quad 0
+	.word 0xffff, 0x0000
+	.byte 0x00, 0x9a, 0xcf, 0x00
+	.word 0xffff, 0x0000
+	.byte 0x00, 0x92, 0xcf, 0x00
+stage2_gdt_end:
+stage2_gdtr:
+	.word stage2_gdt_end - stage2_gdt - 1
+	.long stage2_gdt
+
+.align 2
+vram_64k:
+	.word 0
+best_mode:
+	.word 0
+best_score:
+	.long 0

+ 6 - 28
vga.c

@@ -2,10 +2,9 @@
 
 #include "vga.h"
 #include "pci.h"
+#include "gpu.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;
@@ -47,26 +46,6 @@ static void vga_reset_crtc(void)
 	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)
 {
@@ -86,6 +65,7 @@ static void vga_cache_boot_fb(void)
 	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;
@@ -113,11 +93,9 @@ static void vga_cache_boot_fb(void)
 		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();
+	gpu_init();
+	if (gpu_vendor() == GPU_VENDOR_ATI)
+		vga_reset_crtc();
 }
 
 /* ---- LFB API ---- */
@@ -155,7 +133,7 @@ unsigned int vga_fb_vram_kb(void)
 unsigned int vga_fb_pci_id(void)
 {
 	vga_cache_boot_fb();
-	return vga_pci_id;
+	return gpu_pci_id();
 }
 
 void *vga_fb_ptr(void)