Johannes Zellner 1 месяц назад
Родитель
Сommit
666ee39e2f
15 измененных файлов с 626 добавлено и 179 удалено
  1. 13 7
      Makefile
  2. 90 73
      ati.c
  3. 7 2
      ati.h
  4. 6 0
      display.c
  5. 79 20
      gpu.c
  6. 1 1
      gpu.h
  7. 17 2
      mouse.c
  8. 101 0
      mtrr.c
  9. 10 0
      mtrr.h
  10. 14 0
      paging.c
  11. 5 0
      paging.h
  12. 190 0
      radeon.c
  13. 14 0
      radeon.h
  14. 57 18
      vga.c
  15. 22 56
      wm.c

+ 13 - 7
Makefile

@@ -89,10 +89,10 @@ vga.o: vga.c vga.h pci.h gpu.h ati.h
 vga_copy.o: vga_copy.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-gpu.o: gpu.c gpu.h pci.h ati.h nvidia.h
+gpu.o: gpu.c gpu.h pci.h ati.h cirrus.h nvidia.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-ati.o: ati.c ati.h pci.h
+ati.o: ati.c ati.h pci.h radeon.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 cirrus.o: cirrus.c cirrus.h pci.h vga.h
@@ -101,13 +101,19 @@ cirrus.o: cirrus.c cirrus.h pci.h vga.h
 nvidia.o: nvidia.c nvidia.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+radeon.o: radeon.c radeon.h pci.h paging.h serial.h vga.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+mtrr.o: mtrr.c mtrr.h serial.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 gpu.h ati.h fbtext.h
+display.o: display.c io.h vga.h gpu.h ati.h radeon.h fbtext.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 cpuinfo.o: cpuinfo.c io.h timer.h
@@ -155,7 +161,7 @@ fatformat.o: fatformat.c fatformat.h blkdev.h
 format.o: format.c blkdev.h disk.h fatformat.h io.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-mouse.o: mouse.c mouse.h timer.h vga.h
+mouse.o: mouse.c mouse.h cirrus.h radeon.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 idle.o: idle.c idle.h
@@ -206,7 +212,7 @@ mm.o: mm.c mm.h
 kmalloc.o: kmalloc.c kmalloc.h mm.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-paging.o: paging.c paging.h mm.h vga.h serial.h
+paging.o: paging.c paging.h mm.h mtrr.h vga.h serial.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 elf.o: elf.c elf.h mm.h paging.h
@@ -224,7 +230,7 @@ syscall_stub.o: syscall_stub.S
 mem.o: mem.c mm.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o net.o hello_embed.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o net.o hello_embed.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -380,4 +386,4 @@ dev: kernel.bin kexec_send
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o net.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.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 hello.elf hello_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o lspci.o ne2000.o netcmd.o net.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.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 hello.elf hello_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send

+ 90 - 73
ati.c

@@ -1,37 +1,49 @@
 /*
- * ATI Mach64 (264VT / 3D Rage) -- GPU-specific CRTC fixups.
+ * ATI GPU init.
  *
- * 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.
+ * Two distinct ATI families need different handling:
  *
- * 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)
+ *  - Mach64 / 3D Rage: the CRTC_OFF_PITCH register (MMIO offset 0x14) drives
+ *    display start + scanline pitch, and ignores standard VGA CRTC writes and
+ *    VBE 4F07h.  We zero the offset field directly through PCI MMIO or block
+ *    I/O (see the header comment in the old ati.c for the method ladder).
  *
- * Access is verified by reading CONFIG_CHIP_ID (byte offset 0xE0) and
- * checking the lower 16 bits match the PCI device ID.
+ *  - Radeon (R200 family, incl. the RV280 / Radeon 9200/9250): the VBIOS sets
+ *    a clean mode and needs no CRTC fixup.  We instead set up the hardware
+ *    cursor (radeon.c).
  *
- * 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
+ * Access is verified by reading CONFIG_CHIP_ID (byte offset 0xE0) and checking
+ * the lower 16 bits match the PCI device ID.
  */
 
 #include "ati.h"
 #include "pci.h"
+#include "radeon.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 ati_is_radeon_flag;
 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 ati_device_is_radeon(unsigned int device_id)
+{
+	switch (device_id) {
+	case 0x5960:  /* Radeon 9200 PRO */
+	case 0x5961:  /* Radeon 9200 */
+	case 0x5962:  /* Radeon 9200 SE */
+	case 0x5964:  /* Radeon 9200 / 9250 (RV280) */
+	case 0x5965:  /* FireMV 2200 (RV280) */
+		return 1;
+	default:
+		return 0;
+	}
+}
+
 static int try_mmio(volatile unsigned int *mmio, unsigned int device_id)
 {
 	unsigned int chip_id = mmio[ATI_CONFIG_CHIP_ID_OFS / 4];
@@ -43,71 +55,76 @@ static int try_mmio(volatile unsigned int *mmio, unsigned int device_id)
 	return 1;
 }
 
-void ati_init(void)
+static void ati_mach64_fixup(unsigned int bus, unsigned int dev,
+			     unsigned int device_id)
 {
-	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;
-				}
-			}
+
+	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;
+		}
+	}
+}
+
+void ati_init(unsigned int bus, unsigned int dev)
+{
+	unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
+	unsigned int device_id = (id >> 16) & 0xFFFF;
+
+	ati_id = id;
+	off_pitch_before = 0;
+	off_pitch_after = 0;
+	mmio_method = 0;
+
+	ati_is_radeon_flag = ati_device_is_radeon(device_id);
+
+	if (ati_is_radeon_flag) {
+		radeon_init(bus, dev, device_id);
+		return;
+	}
+
+	ati_mach64_fixup(bus, dev, device_id);
 }
 
-unsigned int ati_pci_id(void)          { return ati_id; }
+int ati_is_radeon(void)          { return ati_is_radeon_flag; }
+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; }

+ 7 - 2
ati.h

@@ -1,8 +1,13 @@
-/* ATI Mach64 / Rage: GPU-specific CRTC fixups via PCI MMIO. */
+/* ATI GPU init: Mach64/Rage CRTC fixups + Radeon (R200/RV280) dispatch. */
 #ifndef ATI_H
 #define ATI_H
 
-void ati_init(void);
+/* Called by gpu_init() with the active adapter's bus/device. */
+void ati_init(unsigned int bus, unsigned int dev);
+
+/* 1 if the active ATI adapter is a Radeon (needs no Mach64 CRTC fixup). */
+int  ati_is_radeon(void);
+
 unsigned int ati_pci_id(void);
 unsigned int ati_off_pitch_before(void);
 unsigned int ati_off_pitch_after(void);

+ 6 - 0
display.c

@@ -4,6 +4,7 @@
 #include "vga.h"
 #include "gpu.h"
 #include "ati.h"
+#include "radeon.h"
 #include "fbtext.h"
 
 static void put_uint(unsigned int n)
@@ -95,6 +96,11 @@ void program_display_main(void)
 			else if (method == 4)
 				io_putstr("IO");
 			io_putchar('\n');
+		} else if (ati_is_radeon()) {
+			io_putstr("ATI Radeon (R200/RV280)");
+			if (radeon_available())
+				io_putstr(" hardware cursor");
+			io_putchar('\n');
 		}
 	}
 	io_putstr("text ");

+ 79 - 20
gpu.c

@@ -9,41 +9,100 @@
 static int gpu_vendor_id;
 static unsigned int gpu_pci_id_val;
 
-void gpu_init(void)
+/*
+ * Resolve the *active* display adapter: the device whose framebuffer BAR
+ * contains the VBE linear framebuffer base.  A machine can carry several
+ * VGA-class devices (e.g. an onboard Cirrus plus a plug-in ATI card), and the
+ * one that served the VBE mode set is not necessarily the first one the PCI
+ * scan hits.  When no BAR contains the LFB (no VBE mode, or a card that maps
+ * its framebuffer through a non-BAR0 window), fall back to the first VGA
+ * device found.
+ */
+static unsigned int gpu_bar_size(unsigned int bus, unsigned int dev, unsigned int bar_reg)
 {
-	unsigned int bus, dev, id, class_rev;
-	unsigned int vendor;
+	unsigned int bar = pci_cfg_read32(bus, dev, 0, bar_reg);
+	unsigned int raw;
+
+	if ((bar & 1) || !(bar & 0xFFFFFFF0))
+		return 0;
+	pci_cfg_write32(bus, dev, 0, bar_reg, 0xFFFFFFFF);
+	raw = pci_cfg_read32(bus, dev, 0, bar_reg);
+	pci_cfg_write32(bus, dev, 0, bar_reg, bar);
+	return (~(raw & 0xFFFFFFF0)) + 1;
+}
+
+void gpu_init(unsigned int lfb_base)
+{
+	unsigned int bus, dev;
+	unsigned int active_bus = 0, active_dev = 0, active_vendor = 0, active_id = 0;
+	unsigned int fb_bus = 0, fb_dev = 0, fb_vendor = 0, fb_id = 0;
 
 	for (bus = 0; bus < 256; bus++) {
 		for (dev = 0; dev < 32; dev++) {
-			id = pci_cfg_read32(bus, dev, 0, 0x00);
+			unsigned int id = pci_cfg_read32(bus, dev, 0, 0x00);
+			unsigned int class_rev, vendor, bar0, bar0_base, bar0_size;
+
 			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 if (vendor == PCI_VENDOR_CIRRUS) {
-				gpu_vendor_id = GPU_VENDOR_CIRRUS;
-				cirrus_init();
-			} else {
-				gpu_vendor_id = GPU_VENDOR_OTHER;
+			/* Remember the first VGA device as a fallback. */
+			if (!active_vendor) {
+				active_bus = bus;
+				active_dev = dev;
+				active_vendor = vendor;
+				active_id = id;
+			}
+
+			/* Match the framebuffer BAR against the LFB base. */
+			bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
+			if (lfb_base && !(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
+				bar0_base = bar0 & 0xFFFFFFF0;
+				bar0_size = gpu_bar_size(bus, dev, 0x10);
+				if (bar0_size && lfb_base >= bar0_base &&
+				    lfb_base < bar0_base + bar0_size) {
+					fb_bus = bus;
+					fb_dev = dev;
+					fb_vendor = vendor;
+					fb_id = id;
+				}
 			}
-			return;
 		}
 	}
-	gpu_vendor_id = GPU_VENDOR_UNKNOWN;
+
+	if (fb_vendor) {
+		active_bus = fb_bus;
+		active_dev = fb_dev;
+		active_vendor = fb_vendor;
+		active_id = fb_id;
+	}
+
+	if (!active_vendor) {
+		gpu_vendor_id = GPU_VENDOR_UNKNOWN;
+		gpu_pci_id_val = 0;
+		return;
+	}
+
+	gpu_pci_id_val = active_id;
+
+	if (active_vendor == PCI_VENDOR_ATI) {
+		gpu_vendor_id = GPU_VENDOR_ATI;
+		ati_init(active_bus, active_dev);
+	} else if (active_vendor == PCI_VENDOR_NVIDIA) {
+		gpu_vendor_id = GPU_VENDOR_NVIDIA;
+		nvidia_init();
+	} else if (active_vendor == PCI_VENDOR_INTEL) {
+		gpu_vendor_id = GPU_VENDOR_INTEL;
+	} else if (active_vendor == PCI_VENDOR_CIRRUS) {
+		gpu_vendor_id = GPU_VENDOR_CIRRUS;
+		cirrus_init();
+	} else {
+		gpu_vendor_id = GPU_VENDOR_OTHER;
+	}
 }
 
 int gpu_vendor(void)

+ 1 - 1
gpu.h

@@ -14,7 +14,7 @@
 #define PCI_VENDOR_INTEL    0x8086
 #define PCI_VENDOR_CIRRUS   0x1013
 
-void gpu_init(void);
+void gpu_init(unsigned int lfb_base);
 int gpu_vendor(void);
 unsigned int gpu_pci_id(void);
 const char *gpu_vendor_name(void);

+ 17 - 2
mouse.c

@@ -1,5 +1,6 @@
 #include "mouse.h"
 #include "cirrus.h"
+#include "radeon.h"
 #include "timer.h"
 #include "vga.h"
 
@@ -45,7 +46,7 @@ static unsigned int mouse_id;
 static int mouse_cursor_flag;
 static unsigned int mouse_pkt_size;
 static unsigned int mouse_buttons;  /* bit0=left bit1=right bit2=middle */
-static int mouse_hw_cursor;         /* 1 = Cirrus hardware cursor in use */
+static int mouse_hw_cursor;         /* 1 = Cirrus, 2 = Radeon hardware cursor */
 
 static unsigned int fbw, fbh;
 static unsigned int cur_x, cur_y;
@@ -184,6 +185,17 @@ static void mouse_cursor_setup(void)
 		return;
 	}
 
+	/* Radeon (R200/RV280) hardware cursor. */
+	if (radeon_available()) {
+		mouse_hw_cursor = 2;
+		cur_x = 100;
+		cur_y = 100;
+		radeon_cursor_setup();
+		radeon_cursor_move(cur_x, cur_y);
+		mouse_cursor_flag = 1;
+		return;
+	}
+
 	mouse_hw_cursor = 0;
 	cur_x = 100;
 	cur_y = 100;
@@ -286,7 +298,10 @@ void mouse_poll_update(void)
 				if (mouse_hw_cursor) {
 					cur_x = (unsigned int)nx;
 					cur_y = (unsigned int)ny;
-					cirrus_cursor_move(cur_x, cur_y);
+					if (mouse_hw_cursor == 2)
+						radeon_cursor_move(cur_x, cur_y);
+					else
+						cirrus_cursor_move(cur_x, cur_y);
 				} else {
 					vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
 						cur_saved);

+ 101 - 0
mtrr.c

@@ -0,0 +1,101 @@
+/*
+ * MTRR write-combining.
+ *
+ * The VESA LFB is mapped uncacheable (UC) by paging.c, so every dword the
+ * 30 Hz shadow flush writes to VRAM is a separate uncached PCI transaction.
+ * Marking the LFB range write-combining (WC) via an MTRR lets the CPU batch
+ * those writes into burst transfers, which is the single biggest rendering
+ * win on real hardware.  (The "P5 USWC" write-combining comment in vga_copy.S
+ * does not hold on the Pentium 4 / Athlon class machines that ship with
+ * Radeon RV280 cards.)
+ */
+
+#include "mtrr.h"
+#include "serial.h"
+
+#define MSR_MTRRCAP        0x0FE
+#define MSR_MTRR_DEF_TYPE  0x2FF
+#define MSR_MTRR_PHYSBASE0 0x200
+#define MSR_MTRR_PHYSMASK0 0x201
+
+#define MTRR_TYPE_WC       0x01
+
+static void rdmsr(unsigned int msr, unsigned int *lo, unsigned int *hi)
+{
+	__asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
+}
+
+static void wrmsr(unsigned int msr, unsigned int lo, unsigned int hi)
+{
+	__asm__ volatile ("wrmsr" : : "a"(lo), "d"(hi), "c"(msr));
+}
+
+static int cpu_has_mtrr(void)
+{
+	unsigned int eax, ebx, ecx, edx;
+	__asm__ volatile ("cpuid"
+		: "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
+		: "a"(1), "c"(0));
+	return (edx & (1u << 12)) != 0;
+}
+
+void mtrr_set_wc(unsigned int base, unsigned int size)
+{
+	unsigned int lo, hi;
+	unsigned int def_type;
+	unsigned int vcnt;
+	unsigned int region_size, align;
+	unsigned int physmask;
+
+	if (!cpu_has_mtrr())
+		return;
+	if (size == 0)
+		return;
+
+	rdmsr(MSR_MTRRCAP, &lo, &hi);
+	vcnt = lo & 0xFF;
+	if (vcnt == 0)
+		return;
+
+	/* Round size up to the next power of two (min 4 KB). */
+	region_size = 0x1000;
+	while (region_size < size) {
+		region_size <<= 1;
+		if (region_size == 0)
+			return;   /* overflow (size > 2 GB) -- give up */
+	}
+
+	/* A variable-range MTRR must be aligned to its own size.  The largest
+	 * region we may use is therefore the base address's own power-of-two
+	 * alignment (base & -base). */
+	align = base & (~base + 1u);
+	if (align == 0 || align > 0x80000000u)
+		align = 0x80000000u;
+	if (region_size > align)
+		region_size = align;
+	if (region_size < 0x1000)
+		return;
+
+	base &= ~(region_size - 1);
+
+	/* PhysMask: address bits [11:0] are implicitly don't-care; bits
+	 * [35:12] are the mask, with a 1 = don't-care.  For a <4 GB region the
+	 * high 32-bit mask is 0 (bits 32..35 must match physbase == 0). */
+	physmask = ((region_size - 1) & 0xFFFFF000u) | 0x800;
+
+	/* Update MTRR with the global enable temporarily cleared so an
+	 * in-progress region never exposes a bogus memory type. */
+	rdmsr(MSR_MTRR_DEF_TYPE, &def_type, &hi);
+	wrmsr(MSR_MTRR_DEF_TYPE, def_type & ~0x800u, 0);
+
+	wrmsr(MSR_MTRR_PHYSBASE0, base | MTRR_TYPE_WC, 0);
+	wrmsr(MSR_MTRR_PHYSMASK0, physmask, 0);
+
+	wrmsr(MSR_MTRR_DEF_TYPE, def_type, 0);
+
+	serial_puts(SERIAL_COM1, "mtrr: LFB WC [");
+	serial_puthex(SERIAL_COM1, base);
+	serial_puts(SERIAL_COM1, " +");
+	serial_puthex(SERIAL_COM1, region_size);
+	serial_puts(SERIAL_COM1, "]\n");
+}

+ 10 - 0
mtrr.h

@@ -0,0 +1,10 @@
+/* MTRR write-combining for MMIO framebuffer ranges. */
+#ifndef MTRR_H
+#define MTRR_H
+
+/* Program one variable-range MTRR as write-combining over [base, base+size).
+ * No-op if the CPU lacks MTRR, has no free variable ranges, or the range
+ * cannot be covered by a single size-aligned region. */
+void mtrr_set_wc(unsigned int base, unsigned int size);
+
+#endif

+ 14 - 0
paging.c

@@ -10,6 +10,7 @@
 
 #include "paging.h"
 #include "mm.h"
+#include "mtrr.h"
 #include "serial.h"
 #include "vga.h"
 
@@ -60,6 +61,8 @@ void paging_init(void)
 		for (i = p0; i < p1 && i < 1024; i++)
 			page_dir[i] = (i << 22) | PDE_PS | PDE_PCD |
 				      PDE_WRITABLE | PDE_PRESENT;
+		/* Mark the framebuffer write-combining so VRAM writes batch. */
+		mtrr_set_wc(base, LFB_MAP_SIZE);
 	}
 
 	/* Load CR3, enable PSE, then PG + WP.  The jump flushes the prefetch
@@ -122,3 +125,14 @@ int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
 	pt[pte] = (phys & 0xFFFFF000) | flags;
 	return 1;
 }
+
+void paging_map_mmio(unsigned int phys_base, unsigned int size)
+{
+	unsigned int p0 = phys_base >> 22;
+	unsigned int p1 = (phys_base + size + 0x3FFFFFu) >> 22;
+	unsigned int i;
+
+	for (i = p0; i < p1 && i < 1024; i++)
+		page_dir[i] = (i << 22) | PDE_PS | PDE_PCD |
+			      PDE_WRITABLE | PDE_PRESENT;
+}

+ 5 - 0
paging.h

@@ -21,4 +21,9 @@ unsigned int paging_new_directory(void);
 int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
 		    unsigned int flags);
 
+/* Identity-map an MMIO range (e.g. a GPU register aperture) uncacheable into
+ * the current page directory.  Uses 4 MB pages; the range is rounded up to a
+ * whole number of 4 MB pages. */
+void paging_map_mmio(unsigned int phys_base, unsigned int size);
+
 #endif

+ 190 - 0
radeon.c

@@ -0,0 +1,190 @@
+/*
+ * ATI Radeon (R200-family / RV280) hardware cursor.
+ *
+ * The RV280's VBIOS sets a clean VBE mode, so (unlike Mach64/Rage) it needs
+ * no CRTC fixup.  What it can give us is a hardware cursor: a 64x64 ARGB8888
+ * overlay composited by the display engine, so moving the pointer no longer
+ * costs a save/restore + dirty-row flush on the CPU.
+ *
+ * Register semantics follow the Linux "radeon" driver (R100/R200 legacy
+ * cursor path):
+ *
+ *  - Cursor image: 64x64 ARGB8888 (32 bpp, 16 KB), byte order B,G,R,A.
+ *  - RADEON_CUR_* are direct-MMIO registers in BAR2.
+ *  - RADEON_CRTC_GEN_CNTL is an MM register file entry, accessed via
+ *    RADEON_MM_INDEX / RADEON_MM_DATA.
+ *  - Position format is (x << 16) | y (x in bits 30:16, y in bits 15:0).
+ *  - Cursor mode is RADEON_CRTC_CUR_MODE_24BPP (2) in bits 22:20.
+ *
+ * NOTE: the bitmap is written straight into VRAM.  Do not use a local
+ * [16 KB] array -- the kernel stack is only 2 KB (0x30800..0x30000) and a
+ * 16 KB stack allocation overflows it, corrupting the ISA DMA buffer.
+ */
+
+#include "radeon.h"
+#include "pci.h"
+#include "paging.h"
+#include "serial.h"
+#include "vga.h"
+
+static unsigned int radeon_mmio;   /* BAR2 register aperture (1:1 mapped) */
+static int radeon_present;
+
+#define RADEON_MM_INDEX              0x0000
+#define RADEON_MM_DATA               0x0004
+
+#define RADEON_CRTC_GEN_CNTL         0x0050
+#define  RADEON_CRTC_CUR_EN          (1u << 16)
+#define  RADEON_CRTC_CUR_MODE_MASK   (7u << 20)
+#define  RADEON_CRTC_CUR_MODE_24BPP  (2u << 20)
+
+#define RADEON_CUR_OFFSET            0x0260
+#define RADEON_CUR_HORZ_VERT_POSN    0x0264
+#define RADEON_CUR_HORZ_VERT_OFF     0x0268
+
+#define RADEON_CUR_SIZE              64          /* 64x64 pixels */
+#define RADEON_CUR_BYTES             (RADEON_CUR_SIZE * RADEON_CUR_SIZE * 4)
+
+/* Cursor bitmap byte offset within card memory (offscreen, above the visible
+   scanout and inside the 16 MB mapped LFB window). */
+#define RADEON_CUR_VRAM_OFS          0x800000
+
+static inline unsigned int mmio_read(unsigned int ofs)
+{
+	volatile unsigned int *p =
+		(volatile unsigned int *)(radeon_mmio + ofs);
+	return *p;
+}
+
+static inline void mmio_write(unsigned int ofs, unsigned int v)
+{
+	volatile unsigned int *p =
+		(volatile unsigned int *)(radeon_mmio + ofs);
+	*p = v;
+}
+
+/* Access an MM register-file entry via MM_INDEX / MM_DATA. */
+static inline unsigned int mmio_mm_read(unsigned int reg)
+{
+	mmio_write(RADEON_MM_INDEX, reg);
+	return mmio_read(RADEON_MM_DATA);
+}
+
+static inline void mmio_mm_write(unsigned int reg, unsigned int v)
+{
+	mmio_write(RADEON_MM_INDEX, reg);
+	mmio_write(RADEON_MM_DATA, v);
+}
+
+/* 16x16 arrow (MSB = column 0), matching the software cursor shape. */
+static const unsigned short radeon_arrow[16] = {
+	0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00,
+	0xFC00, 0xE000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+};
+
+void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id)
+{
+	unsigned int bar2, bar2_base, bar2_size;
+
+	radeon_present = 0;
+	radeon_mmio = 0;
+	(void)device_id;
+
+	/* BAR2 is the register aperture on Radeon. */
+	bar2 = pci_cfg_read32(bus, dev, 0, 0x18);
+	if ((bar2 & 1) || !(bar2 & 0xFFFFFFF0))
+		return;
+	bar2_base = bar2 & 0xFFFFFFF0;
+
+	pci_cfg_write32(bus, dev, 0, 0x18, 0xFFFFFFFF);
+	bar2_size = pci_cfg_read32(bus, dev, 0, 0x18);
+	pci_cfg_write32(bus, dev, 0, 0x18, bar2);
+	bar2_size = (~(bar2_size & 0xFFFFFFF0)) + 1;
+	if (bar2_size < 0x10000)
+		return;
+
+	paging_map_mmio(bar2_base, bar2_size);
+
+	radeon_mmio = bar2_base;
+
+	/* Validate the aperture: a master abort reads back all-ones. */
+	mmio_write(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
+	if (mmio_read(RADEON_MM_DATA) == 0xFFFFFFFF) {
+		radeon_mmio = 0;
+		return;
+	}
+
+	radeon_present = 1;
+	serial_puts(SERIAL_COM1, "radeon: cursor MMIO ");
+	serial_puthex(SERIAL_COM1, radeon_mmio);
+	serial_puts(SERIAL_COM1, "\n");
+}
+
+int radeon_available(void)
+{
+	return radeon_present;
+}
+
+void radeon_cursor_setup(void)
+{
+	unsigned char *dst;
+	unsigned int vram;
+	unsigned int i, r, c, g;
+
+	if (!radeon_present)
+		return;
+
+	vram = vga_fb_vram_kb() * 1024;
+	if (vram < RADEON_CUR_VRAM_OFS + RADEON_CUR_BYTES) {
+		radeon_present = 0;
+		return;
+	}
+
+	dst = (unsigned char *)(vga_fb_lfb_addr() + RADEON_CUR_VRAM_OFS);
+
+	/* Transparent background (alpha 0), then the opaque white arrow. */
+	for (i = 0; i < RADEON_CUR_BYTES; i++)
+		dst[i] = 0;
+	for (r = 0; r < 16; r++) {
+		unsigned short bits = radeon_arrow[r];
+		for (c = 0; c < 16; c++) {
+			if (bits & (0x8000u >> c)) {
+				unsigned int off = (r * RADEON_CUR_SIZE + c) * 4;
+				dst[off + 0] = 0xFF;  /* B */
+				dst[off + 1] = 0xFF;  /* G */
+				dst[off + 2] = 0xFF;  /* R */
+				dst[off + 3] = 0xFF;  /* A */
+			}
+		}
+	}
+
+	mmio_write(RADEON_CUR_OFFSET, RADEON_CUR_VRAM_OFS);
+	mmio_write(RADEON_CUR_HORZ_VERT_OFF, 0);    /* hotspot at (0,0) */
+	mmio_write(RADEON_CUR_HORZ_VERT_POSN, 0);   /* position set on move */
+
+	/* Enable the 24 bpp (ARGB8888) hardware cursor. */
+	g = mmio_mm_read(RADEON_CRTC_GEN_CNTL);
+	g &= ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_MASK);
+	g |= RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_24BPP;
+	mmio_mm_write(RADEON_CRTC_GEN_CNTL, g);
+}
+
+void radeon_cursor_move(unsigned int x, unsigned int y)
+{
+	if (!radeon_present)
+		return;
+	mmio_write(RADEON_CUR_HORZ_VERT_POSN, (x << 16) | y);
+}
+
+void radeon_cursor_show(int show)
+{
+	unsigned int g;
+	if (!radeon_present)
+		return;
+	g = mmio_mm_read(RADEON_CRTC_GEN_CNTL);
+	if (show)
+		g |= RADEON_CRTC_CUR_EN;
+	else
+		g &= ~RADEON_CRTC_CUR_EN;
+	mmio_mm_write(RADEON_CRTC_GEN_CNTL, g);
+}

+ 14 - 0
radeon.h

@@ -0,0 +1,14 @@
+/* ATI Radeon (R200 / RV280) hardware cursor. */
+#ifndef RADEON_H
+#define RADEON_H
+
+/* Called by ati_init() when the active adapter is a Radeon. */
+void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id);
+int  radeon_available(void);
+
+/* Hardware cursor (64x64, 2 bpp). */
+void radeon_cursor_setup(void);
+void radeon_cursor_move(unsigned int x, unsigned int y);
+void radeon_cursor_show(int show);
+
+#endif

+ 57 - 18
vga.c

@@ -20,17 +20,41 @@ static unsigned int   vga_shadow_size;
 #define MAX_DIRTY_ROWS 2048
 static unsigned char vga_shadow_dirty_rows[MAX_DIRTY_ROWS / 8];
 
+/* Horizontal span [x0, x1) of dirty pixels, so the flush copies only the
+   changed columns instead of whole rows. */
+static unsigned int vga_dirty_x0, vga_dirty_x1;
+
 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)
+static void expand_dirty_x(unsigned int x0, unsigned int x1)
+{
+	if (x0 > vga_fb_w)
+		x0 = vga_fb_w;
+	if (x1 > vga_fb_w)
+		x1 = vga_fb_w;
+	if (x0 < vga_dirty_x0)
+		vga_dirty_x0 = x0;
+	if (x1 > vga_dirty_x1)
+		vga_dirty_x1 = x1;
+}
+
+static void mark_dirty_rect_xy(unsigned int y, unsigned int h,
+			       unsigned int x0, unsigned int x1)
 {
 	unsigned int i;
 	for (i = 0; i < h; i++)
 		mark_dirty_row(y + i);
+	expand_dirty_x(x0, x1);
+}
+
+/* Full-width dirty rows (used where the x-extent isn't tracked). */
+static void mark_dirty_rect(unsigned int y, unsigned int h)
+{
+	mark_dirty_rect_xy(y, h, 0, vga_fb_w);
 }
 
 static int any_dirty_rows(void)
@@ -53,6 +77,8 @@ static void clear_dirty_rows(void)
 		max_row = MAX_DIRTY_ROWS;
 	for (i = 0; i < (max_row + 7) / 8; i++)
 		vga_shadow_dirty_rows[i] = 0;
+	vga_dirty_x0 = vga_fb_w;
+	vga_dirty_x1 = 0;
 }
 
 /*
@@ -109,7 +135,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();
+		gpu_init(0);
 		return;
 	}
 	vga_fb_w = info->width;
@@ -137,8 +163,8 @@ static void vga_cache_boot_fb(void)
 		if (max_h < vga_fb_h)
 			vga_fb_h = max_h;
 	}
-	gpu_init();
-	if (gpu_vendor() == GPU_VENDOR_ATI)
+	gpu_init(info->lfb_base);
+	if (gpu_vendor() == GPU_VENDOR_ATI && !ati_is_radeon())
 		vga_reset_crtc();
 }
 
@@ -227,7 +253,7 @@ static void shadow_put_pixel(unsigned int x, unsigned int y, unsigned int r, uns
 		p[off + 2] = (unsigned char)r;
 		p[off + 3] = 0;
 	}
-	mark_dirty_row(y);
+	mark_dirty_rect_xy(y, 1, x, x + 1);
 }
 
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
@@ -288,7 +314,7 @@ void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int
 				*p++ = val;
 		}
 	}
-	mark_dirty_rect(y, y1 - y);
+	mark_dirty_rect_xy(y, y1 - y, x, x1);
 }
 
 static unsigned int vga_pixel_bytes(void)
@@ -336,7 +362,12 @@ void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned
 		for (col = 0; col < w * pb; col++)
 			dst[col] = buf[row * w * pb + col];
 	}
-	mark_dirty_rect(y, h);
+	{
+		unsigned int xe = x + w;
+		if (xe > vga_fb_w)
+			xe = vga_fb_w;
+		mark_dirty_rect_xy(y, h, x, xe);
+	}
 }
 
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
@@ -404,26 +435,34 @@ 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 int align = (pb == 2) ? 2 : (pb == 3) ? 4 : 1;
+	unsigned int x0 = vga_dirty_x0;
+	unsigned int x1 = vga_dirty_x1;
+	unsigned int dwords;
 	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;
 
+	/* Clamp and dword-align the horizontal span so each row copy is an
+	   integral number of dwords starting on a dword boundary. */
+	if (x1 > vga_fb_w)
+		x1 = vga_fb_w;
+	x0 &= ~(align - 1);
+	x1 = (x1 + align - 1) & ~(align - 1);
+	if (x1 > vga_fb_w)
+		x1 = vga_fb_w;
+	if (x0 >= x1)
+		return;
+	dwords = ((x1 - x0) * pb) / 4;
+
 	for (row = 0; row < max_row; row++) {
 		if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
-			unsigned int run = row + 1;
-			while (run < max_row &&
-			       (vga_shadow_dirty_rows[run / 8] & (1 << (run % 8))))
-				run++;
 			vga_fb_copy_row(
-				(void *)(dst + (unsigned long)row * vga_fb_pitch_val),
-				(void *)(src + (unsigned long)row * vga_shadow_pitch),
-				dwords * (run - row)
-			);
-			row = run - 1;
+				(void *)(dst + (unsigned long)row * vga_fb_pitch_val + x0 * pb),
+				(void *)(src + (unsigned long)row * vga_shadow_pitch + x0 * pb),
+				dwords);
 		}
 	}
 }

+ 22 - 56
wm.c

@@ -25,12 +25,6 @@ static int drag_window;      /* index into windows[] */
 static int drag_dx, drag_dy; /* mouse offset from window origin */
 static unsigned int prev_buttons;
 
-/* Drag damage: accumulated y-band of the moving window, flushed as a region. */
-static int drag_render_needed;
-static int drag_band_valid;
-static unsigned int drag_band_y0, drag_band_y1;
-static unsigned int last_drag_render;
-
 void wm_request_redraw(void)
 {
 	wm_full_redraw = 1;
@@ -202,24 +196,6 @@ static void wm_clamp_window(struct window *w, int nx, int ny)
 	w->y = (unsigned int)ny;
 }
 
-/* Grow the drag's accumulated dirty band to cover the window's old+new rects.
-   Only the y-range matters: the flush copies whole rows. */
-static void wm_drag_accumulate(unsigned int oy, unsigned int ny, unsigned int ph)
-{
-	unsigned int y0 = (oy < ny) ? oy : ny;
-	unsigned int y1 = ((oy > ny) ? oy : ny) + ph;
-	if (!drag_band_valid) {
-		drag_band_y0 = y0;
-		drag_band_y1 = y1;
-		drag_band_valid = 1;
-	} else {
-		if (y0 < drag_band_y0)
-			drag_band_y0 = y0;
-		if (y1 > drag_band_y1)
-			drag_band_y1 = y1;
-	}
-}
-
 /* Byte copy that handles overlapping source/destination. */
 static void wm_memmove(unsigned char *dst, const unsigned char *src, unsigned int n)
 {
@@ -304,15 +280,18 @@ static void wm_redraw_sliver(unsigned int x, unsigned int y, unsigned int w, uns
 	}
 }
 
-/* Cirrus fast drag: move the window with the BitBLT, restore the exposed
-   sliver, and mirror both in the RAM shadow buffer. No full recomposite. */
-static void wm_drag_fast(struct window *w, unsigned int ox, unsigned int oy,
-			 unsigned int nx, unsigned int ny)
+/* Move a window in the shadow buffer: copy its rect, redraw the exposed
+   sliver, keep the cursor on top, and mark the swept rows dirty.  Does not
+   touch VRAM; the 30 Hz flush copies the affected rows. */
+static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
+			   unsigned int nx, unsigned int ny)
 {
 	int dx = (int)nx - (int)ox;
 	int dy = (int)ny - (int)oy;
+	unsigned int y0 = (oy < ny) ? oy : ny;
+	unsigned int y1 = ((oy > ny) ? oy : ny) + w->ph;
 
-	cirrus_blit(ox, oy, nx, ny, w->pw, w->ph);
+	mouse_cursor_erase();
 	wm_shadow_copy(ox, oy, nx, ny, w->pw, w->ph);
 
 	if (dy > 0)
@@ -324,6 +303,18 @@ static void wm_drag_fast(struct window *w, unsigned int ox, unsigned int oy,
 	else if (dx < 0)
 		wm_redraw_sliver(nx + w->pw, oy, (unsigned int)(-dx), w->ph);
 
+	/* Mark the swept band so the flush copies the moved window + sliver. */
+	vga_fb_mark_dirty_rect(y0, y1 - y0);
+	mouse_redraw_cursor();
+}
+
+/* Cirrus fast drag: also move the window in VRAM with the BitBLT engine, then
+   mirror in the shadow buffer and flush immediately. */
+static void wm_drag_fast(struct window *w, unsigned int ox, unsigned int oy,
+			 unsigned int nx, unsigned int ny)
+{
+	cirrus_blit(ox, oy, nx, ny, w->pw, w->ph);
+	wm_drag_shadow(w, ox, oy, nx, ny);
 	vga_fb_flush();
 }
 
@@ -355,8 +346,6 @@ static void wm_handle_mouse(void)
 					drag_window = idx;
 					drag_dx = (int)mx - (int)w->x;
 					drag_dy = (int)my - (int)w->y;
-					drag_band_valid = 0;
-					drag_render_needed = 0;
 				}
 			}
 		}
@@ -368,10 +357,8 @@ static void wm_handle_mouse(void)
 			if (ox != w->x || oy != w->y) {
 				if (cirrus_available())
 					wm_drag_fast(w, ox, oy, w->x, w->y);
-				else {
-					wm_drag_accumulate(oy, w->y, w->ph);
-					drag_render_needed = 1;
-				}
+				else
+					wm_drag_shadow(w, ox, oy, w->x, w->y);
 			}
 		} else {
 			drag_active = 0;
@@ -530,27 +517,6 @@ static void wm_redraw_if_needed(void)
 			if (windows[i].used)
 				windows[i].term->screen.dirty = 0;
 		wm_full_redraw = 0;
-		drag_render_needed = 0;
-		drag_band_valid = 0;
-		last_sec = now_sec;
-		return;
-	}
-
-	/* Drag: redraw the full scene into the shadow buffer, but flush only the
-	   accumulated y-band (the window's swept region) to VRAM. Throttled to the
-	   flush rate so rapid mouse moves coalesce. */
-	if (drag_render_needed && (now_ms - last_drag_render >= 33)) {
-		last_drag_render = now_ms;
-		wm_render_full();
-		vga_fb_clear_dirty();
-		if (drag_band_valid)
-			vga_fb_mark_dirty_rect(drag_band_y0, drag_band_y1 - drag_band_y0);
-		if (clock_changed)
-			vga_fb_mark_dirty_rect(0, WM_BAR_H);
-		for (i = 0; i < MAX_WINDOWS; i++)
-			if (windows[i].used)
-				windows[i].term->screen.dirty = 0;
-		drag_render_needed = 0;
 		last_sec = now_sec;
 		return;
 	}