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

Add NVIDIA ION identification and display-engine probes

The ASUS AT3IONT-I (Atom 330 + first-gen ION, C79/MCP79, 10DE:087D)
boots and displays fine on VBE alone, but the GPU was a no-op
placeholder.  Identify what the card actually is and make the
display-engine situation visible without touching the live VBIOS mode.

nvidia.c/h:
- nvidia_init(bus, dev, device_id) names the device (C79 [ION] 087D and
  the C79/ION2/GM206 table), validates BAR0 by reading NV_PMC_BOOT_0
  (chipset id decode), maps BAR0/BAR1/BAR3 with probed-and-restored
  sizes, and reads the MCP77/79/89 stolen-VRAM base/size from
  0x100E10/0x100E14.
- gpu.c passes the PCI location through; the boot log and `display`
  show the card, regs, BAR3 and the raster's VRAM offset.

nv50.c/h (new): read-only nv50-family display-engine probe at boot --
object table pointer, per-head cursor channel state, PIO, VBIOS
hand-off (INTR_1 bit 8, 0x6194e8) and the caps shadow/active
differences, plus the EVO channel control/DMA-CB registers -- so the
serial/netmon log carries the engine state for future work.

Hardware cursor: deliberately not driven on this family.  The VBIOS
keeps the display engine; EVO DMA channels accept DMA_CB and the
USER_PUT doorbell but never fetch until nouveau's full takeover (which
re-programs the mode itself), the cursor staging registers are
engine-only, and a channel that has run cannot be restarted.  The
software cursor is used; mouse.c/h and mousecmd.c report the backend.

peek.c: add `poke <addr> <val>` (32-bit MMIO/RAM write with read-back)
for driving register experiments from the shell.

mtrr.c: reuse an existing write-combining entry that already covers
the LFB base instead of consuming another variable slot.  Variable
MTRRs survive a kernel reload, and the kexec dev loop had leaked six
entries on the board, leaving the framebuffer uncombineable.

Kernel region: grow 0x40000 -> 0x42000 and move the protected-mode
stack (0x42800), program API (0x42F00) and user load address (0x43000)
in vesa_stage.S, kexec_tramp.S, tss.c, process.c, run.c, serial_recv.c,
run_api.h, user.ld and bootstrap.c (+ comments in kexec.*, colors.c,
radeon.c, uhci.c, netcmd.c).  ~10 KB of headroom again.

Also picks up the in-tree 64x32 terminal geometry and two-column help
rework, which were already in the working tree.
Johannes Zellner 1 месяц назад
Родитель
Сommit
28466e50d1
31 измененных файлов с 646 добавлено и 96 удалено
  1. 23 11
      AGENTS.md
  2. 8 5
      Makefile
  3. 4 4
      bootstrap.c
  4. 1 1
      colors.c
  5. 28 0
      display.c
  6. 2 1
      gpu.c
  7. 1 1
      kernel.ld
  8. 1 1
      kexec.c
  9. 1 1
      kexec.h
  10. 1 1
      kexec_tramp.S
  11. 17 0
      mouse.c
  12. 4 0
      mouse.h
  13. 3 0
      mousecmd.c
  14. 18 0
      mtrr.c
  15. 1 1
      netcmd.c
  16. 109 0
      nv50.c
  17. 11 0
      nv50.h
  18. 242 10
      nvidia.c
  19. 34 2
      nvidia.h
  20. 40 0
      peek.c
  21. 1 1
      process.c
  22. 1 1
      radeon.c
  23. 3 3
      run.c
  24. 1 1
      run_api.h
  25. 1 1
      screen.h
  26. 2 2
      serial_recv.c
  27. 83 43
      terminal.c
  28. 1 1
      tss.c
  29. 1 1
      uhci.c
  30. 2 2
      user.ld
  31. 1 1
      vesa_stage.S

+ 23 - 11
AGENTS.md

@@ -46,10 +46,10 @@ make clean
 | `0x1000`  | E820 memory map (`struct e820_map`, written by `vesa_stage.S`, parsed by `mm.c`) |
 | `0x1000`  | E820 memory map (`struct e820_map`, written by `vesa_stage.S`, parsed by `mm.c`) |
 | `0x3000`  | UHCI frame list (1024 × 4 B, 4 KB-aligned; `uhci.c`) |
 | `0x3000`  | UHCI frame list (1024 × 4 B, 4 KB-aligned; `uhci.c`) |
 | `0x4000`  | ISA DMA buffer, one sector (`disk.c`) |
 | `0x4000`  | ISA DMA buffer, one sector (`disk.c`) |
-| `0x40000` | Kernel upper bound (`kernel.ld` ASSERT) |
-| `0x40800` | Protected-mode stack top (`vesa_stage.S`, `kexec_tramp.S`) |
-| `0x40F00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
-| `0x41000` | User program load address (`run.c`, `user.ld`) |
+| `0x42000` | Kernel upper bound (`kernel.ld` ASSERT) |
+| `0x42800` | Protected-mode stack top (`vesa_stage.S`, `kexec_tramp.S`) |
+| `0x42F00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
+| `0x43000` | User program load address (`run.c`, `user.ld`) |
 | `0x50000` | Fallback process stack pool (8 × 4 KB; primary stacks come from the frame allocator, ≥ `0x900000`, see `process.c`/`mm.c`) |
 | `0x50000` | Fallback process stack pool (8 × 4 KB; primary stacks come from the frame allocator, ≥ `0x900000`, see `process.c`/`mm.c`) |
 | `0x58000` | kexec staging buffer (received kernel image, up to 0x38200 B; ends below the VGA window) |
 | `0x58000` | kexec staging buffer (received kernel image, up to 0x38200 B; ends below the VGA window) |
 | `0x100000`| VGA shadow buffer (`VGA_SHADOW_BASE`, `vga.h`) |
 | `0x100000`| VGA shadow buffer (`VGA_SHADOW_BASE`, `vga.h`) |
@@ -74,11 +74,19 @@ make clean
   (`kexec_tramp.S`) clears CR0.PG before the copy so it can overwrite the old
   (`kexec_tramp.S`) clears CR0.PG before the copy so it can overwrite the old
   kernel's page tables safely.
   kernel's page tables safely.
 - **Window manager**: `wm.c/h` — slot 0 runs `wm_run()`; fixed-size draggable
 - **Window manager**: `wm.c/h` — slot 0 runs `wm_run()`; fixed-size draggable
-  terminal windows (64×20) with a system bar (banner + purple "+ New" button +
+  terminal windows (64×32) with a system bar (banner + purple "+ New" button +
   clock + rainbow). Click-to-focus, drag-by-title-bar, close (×). The "+ New"
   clock + rainbow). Click-to-focus, drag-by-title-bar, close (×). The "+ New"
   button and first boot spawn terminals; there is no `term` command.
   button and first boot spawn terminals; there is no `term` command.
-- **Graphics**: `vga.c/h`, `vga_copy.S`, `gpu.c`, `ati.c`, `nvidia.c`, `pci.h` —
-  VESA LFB + shadow buffer, 30 Hz dirty-row flush. ATI Mach64 CRTC fixup; NVIDIA no-op.
+- **Graphics**: `vga.c/h`, `vga_copy.S`, `gpu.c`, `ati.c`, `nvidia.c`, `nv50.c`,
+  `pci.h` — VESA LFB + shadow buffer, 30 Hz dirty-row flush. ATI Mach64 CRTC
+  fixup; NVIDIA is VBE-only (never touch the legacy VGA CRTC). `nvidia.c`
+  identifies the card, maps BAR0/BAR1/BAR3 and reads the MCP7x stolen-VRAM
+  geometry; `nv50.c` probes the nv50-family display engine at boot (read-only,
+  serial/netmon). The ION/AT3IONT-I hardware cursor is deliberately not driven:
+  the VBIOS keeps the display engine (INTR_1 bit 8, `0x6194e8` = 0x803), EVO
+  DMA channels never fetch until nouveau's full takeover (which re-programs the
+  mode itself), the cursor staging registers are engine-only, and a channel
+  that has run cannot be restarted — see the header of `nv50.c`.
 - **Text console**: `fbtext.c`, `font_8x8.c/h`, `io.c/h`, `screen.c/h` — 8×8 font,
 - **Text console**: `fbtext.c`, `font_8x8.c/h`, `io.c/h`, `screen.c/h` — 8×8 font,
   framebuffer glyph blitting (`fbtext_draw_cells`) and per-terminal cell buffers
   framebuffer glyph blitting (`fbtext_draw_cells`) and per-terminal cell buffers
   (`struct text_screen`).
   (`struct text_screen`).
@@ -129,7 +137,7 @@ make clean
   scan generation counter.
   scan generation counter.
 - **Power/idle**: `idle.c` — `mwait` C-states, `hlt` fallback.
 - **Power/idle**: `idle.c` — `mwait` C-states, `hlt` fallback.
 - **User programs**: `run.c`, `run_api.h`, `prog.c`, `space.c` — raw binaries at
 - **User programs**: `run.c`, `run_api.h`, `prog.c`, `space.c` — raw binaries at
-  `0x41000`, API function-pointer struct at `0x40F00`.  `prog`/`space` are
+  `0x43000`, API function-pointer struct at `0x42F00`.  `prog`/`space` are
   **embedded in the kernel image** (`prog_embed.o`/`space_embed.o`, like
   **embedded in the kernel image** (`prog_embed.o`/`space_embed.o`, like
   `hello.elf`), so `make dev`/`make net-dev` update them too and a board with
   `hello.elf`), so `make dev`/`make net-dev` update them too and a board with
   no readable filesystem can still run them: `run <name>` and the `prog`/
   no readable filesystem can still run them: `run <name>` and the `prog`/
@@ -143,7 +151,7 @@ make clean
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`, `run <name>|<file>`, `space`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`, `run <name>|<file>`, `space`,
 `prog`,
 `prog`,
-`clock`, `colors`, `display`, `cpuinfo`, `mouseinfo`, `lsusb`, `lspci [id]`, `peek <addr> [len]`, `storage`, `mem`, `format <device>`,
+`clock`, `colors`, `display`, `cpuinfo`, `mouseinfo`, `lsusb`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `mem`, `format <device>`,
 `ps`, `kill <pid>`, `help`, `clear`, `serial test|recv`, `kexec`, `dbg`.
 `ps`, `kill <pid>`, `help`, `clear`, `serial test|recv`, `kexec`, `dbg`.
 
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
@@ -159,6 +167,8 @@ answers ARP/ICMP for its IP while idle.
 `lspci` lists every PCI function (one line each); with a filter (`lspci 8168`,
 `lspci` lists every PCI function (one line each); with a filter (`lspci 8168`,
 `lspci 02`) it also prints BAR details, type and size for matching functions.
 `lspci 02`) it also prints BAR details, type and size for matching functions.
 `peek <addr> [len]` hex-dumps memory (identity map, so MMIO registers too).
 `peek <addr> [len]` hex-dumps memory (identity map, so MMIO registers too).
+`poke <addr> <val>` writes one 32-bit dword and prints the read-back (for
+driving MMIO experiments from the shell).
 
 
 Each terminal has a current working directory (shown in the prompt as `[dir] > `),
 Each terminal has a current working directory (shown in the prompt as `[dir] > `),
 changed with `cd`; paths may be absolute (`/floppy1/…`) or relative (`.`, `..`,
 changed with `cd`; paths may be absolute (`/floppy1/…`) or relative (`.`, `..`,
@@ -428,7 +438,9 @@ the MTRR dump from `mtrr.c`: `MTRRdef` = default-type MSR + number of variable
 slots; `M0..M7` = the BIOS's own MTRR entries as found at boot (`-` = free
 slots; `M0..M7` = the BIOS's own MTRR entries as found at boot (`-` = free
 slot; `b`/`m` = base/address mask; type `UC`/`WB`/`WC`/`WT`/`WP`); `WC=eN ...`
 slot; `b`/`m` = base/address mask; type `UC`/`WB`/`WC`/`WT`/`WP`); `WC=eN ...`
 = the write-combining entry the kernel added in free slot N for the
 = the write-combining entry the kernel added in free slot N for the
-framebuffer (or `WC skipped: no free MTRR entry`). Never overwrite
+framebuffer (or `WC skipped: no free MTRR entry`). An entry that already
+covers the LFB base (left by a previous kexec boot -- variable MTRRs survive
+a kernel reload) is reused instead of consuming another slot. Never overwrite
 BIOS-populated MTRR entries — chipset/MMIO uncacheability (and the USB-legacy
 BIOS-populated MTRR entries — chipset/MMIO uncacheability (and the USB-legacy
 SMM) breaks otherwise.
 SMM) breaks otherwise.
 
 
@@ -455,7 +467,7 @@ Caveats:
 
 
 - Pixels are written directly to the LFB; WM full redraws overwrite them but
 - Pixels are written directly to the LFB; WM full redraws overwrite them but
   the 1 Hz refresh brings them back.
   the 1 Hz refresh brings them back.
-- Info rows cost ~0.5 KB BSS; the kernel must stay under the `0x40000`
+- Info rows cost ~0.5 KB BSS; the kernel must stay under the `0x42000`
   boundary (`kernel.ld` ASSERT).
   boundary (`kernel.ld` ASSERT).
 
 
 ## Conventions & gotchas
 ## Conventions & gotchas

+ 8 - 5
Makefile

@@ -98,8 +98,11 @@ ati.o: ati.c ati.h pci.h radeon.h
 cirrus.o: cirrus.c cirrus.h pci.h vga.h
 cirrus.o: cirrus.c cirrus.h pci.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-nvidia.o: nvidia.c nvidia.h
-	$(CC) $(CFLAGS) -o $@ $<
+nvidia.o: nvidia.c nvidia.h nv50.h pci.h process.h serial.h
+	$(CC) $(CFLAGS) -Os -o $@ $<
+
+nv50.o: nv50.c nv50.h nvidia.h serial.h
+	$(CC) $(CFLAGS) -Os -o $@ $<
 
 
 radeon.o: radeon.c radeon.h pci.h paging.h serial.h vga.h
 radeon.o: radeon.c radeon.h pci.h paging.h serial.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
@@ -113,7 +116,7 @@ clock.o: clock.c io.h rtc.h timer.h vga.h
 colors.o: colors.c fbtext.h io.h timer.h vga.h
 colors.o: colors.c fbtext.h io.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-display.o: display.c io.h vga.h gpu.h ati.h radeon.h fbtext.h
+display.o: display.c io.h vga.h gpu.h ati.h nvidia.h radeon.h mouse.h fbtext.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 cpuinfo.o: cpuinfo.c io.h timer.h
 cpuinfo.o: cpuinfo.c io.h timer.h
@@ -257,7 +260,7 @@ syscall_stub.o: syscall_stub.S
 mem.o: mem.c mm.h io.h
 mem.o: mem.c mm.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(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 radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.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 peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o hello_embed.o prog_embed.o space_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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.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 peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o hello_embed.o prog_embed.o space_embed.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -482,4 +485,4 @@ net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 
 clean:
 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 radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.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 peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.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 prog_embed.o space_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	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 nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.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 peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.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 prog_embed.o space_embed.o bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 4 - 4
bootstrap.c

@@ -3,8 +3,8 @@
  * full hardware access: it switches COM1 to 115200, receives a new kernel image,
  * full hardware access: it switches COM1 to 115200, receives a new kernel image,
  * copies it over 0x7E00, and jumps to its kernel_main.
  * copies it over 0x7E00, and jumps to its kernel_main.
  *
  *
- * Self-contained: no kernel symbols, no run_api dependency. We live at 0x41000
- * (outside the 0x7E00..0x40000 kernel region), so we can overwrite the kernel
+ * Self-contained: no kernel symbols, no run_api dependency. We live at 0x43000
+ * (outside the 0x7E00..0x42000 kernel region), so we can overwrite the kernel
  * directly from our own code without a relocated trampoline.
  * directly from our own code without a relocated trampoline.
  *
  *
  * Wire protocol (host -> us), after we emit 'R':
  * Wire protocol (host -> us), after we emit 'R':
@@ -18,7 +18,7 @@
 #define UART_BASE        0x3F8
 #define UART_BASE        0x3F8
 #define STAGE_ADDR       0x58000
 #define STAGE_ADDR       0x58000
 #define KERNEL_LOAD_ADDR 0x7E00
 #define KERNEL_LOAD_ADDR 0x7E00
-#define BOOT_STACK       0x40800
+#define BOOT_STACK       0x42800
 #define MAGIC            0x4345584B /* "KXEC" little-endian */
 #define MAGIC            0x4345584B /* "KXEC" little-endian */
 #define MAX_SIZE         0x38200
 #define MAX_SIZE         0x38200
 
 
@@ -148,7 +148,7 @@ void _start(void)
 	}
 	}
 
 
 	/* Copy staged kernel over 0x7E00, then jump to its kernel_main.
 	/* Copy staged kernel over 0x7E00, then jump to its kernel_main.
-	 * We live at 0x41000, outside the overwritten region, so this is safe. */
+	 * We live at 0x43000, outside the overwritten region, so this is safe. */
 	setup_flat_gdt();
 	setup_flat_gdt();
 	for (i = 0; i < size; i++)
 	for (i = 0; i < size; i++)
 		((volatile unsigned char *)KERNEL_LOAD_ADDR)[i] =
 		((volatile unsigned char *)KERNEL_LOAD_ADDR)[i] =

+ 1 - 1
colors.c

@@ -16,7 +16,7 @@
 /* Precomputed hue-to-RGB lookup table. Packed as 0x00BBGGRR for fast 32-bit load.
 /* Precomputed hue-to-RGB lookup table. Packed as 0x00BBGGRR for fast 32-bit load.
  * The hue cycle is RAINBOW_FULL entries, but the table is decimated by
  * The hue cycle is RAINBOW_FULL entries, but the table is decimated by
  * LUT_STEP: a 4-hue step is ~1.5% of a color segment and invisible, and the
  * LUT_STEP: a 4-hue step is ~1.5% of a color segment and invisible, and the
- * smaller table keeps the kernel image under the 0x40000 memory-bound. */
+ * smaller table keeps the kernel image under the 0x42000 memory-bound. */
 #define LUT_STEP      4
 #define LUT_STEP      4
 #define LUT_SIZE      (RAINBOW_FULL / LUT_STEP)
 #define LUT_SIZE      (RAINBOW_FULL / LUT_STEP)
 
 

+ 28 - 0
display.c

@@ -4,7 +4,9 @@
 #include "vga.h"
 #include "vga.h"
 #include "gpu.h"
 #include "gpu.h"
 #include "ati.h"
 #include "ati.h"
+#include "nvidia.h"
 #include "radeon.h"
 #include "radeon.h"
+#include "mouse.h"
 #include "fbtext.h"
 #include "fbtext.h"
 
 
 static void put_uint(unsigned int n)
 static void put_uint(unsigned int n)
@@ -64,6 +66,9 @@ void program_display_main(void)
 	io_putstr("LFB ");
 	io_putstr("LFB ");
 	put_hex((unsigned int)(unsigned long)vga_fb_ptr());
 	put_hex((unsigned int)(unsigned long)vga_fb_ptr());
 	io_putchar('\n');
 	io_putchar('\n');
+	io_putstr("LFB phys ");
+	put_hex((unsigned int)vga_fb_lfb_addr());
+	io_putchar('\n');
 	io_putstr("rendering: ");
 	io_putstr("rendering: ");
 	io_putstr(vga_shadow_is_active() ? "shadow buffer" : "direct");
 	io_putstr(vga_shadow_is_active() ? "shadow buffer" : "direct");
 	io_putchar('\n');
 	io_putchar('\n');
@@ -103,6 +108,29 @@ void program_display_main(void)
 			io_putchar('\n');
 			io_putchar('\n');
 		}
 		}
 	}
 	}
+	if (gpu_vendor() == GPU_VENDOR_NVIDIA) {
+		unsigned int lfb = (unsigned int)vga_fb_lfb_addr();
+		unsigned int bar3 = nvidia_bar3();
+
+		io_putstr("NVIDIA ");
+		io_putstr(nvidia_chip_name());
+		io_putstr(" chipset ");
+		put_hex(nvidia_chipset());
+		io_putstr(" regs ");
+		put_hex(nvidia_bar0());
+		io_putstr(" bar3 ");
+		put_hex(bar3);
+		if (bar3 && lfb >= bar3) {
+			/* VRAM offset of the raster (BAR3 maps VRAM 1:1):
+			   the base for offscreen VRAM (cursor image etc.). */
+			io_putstr(" FB ofs ");
+			put_hex(lfb - bar3);
+		}
+		io_putchar('\n');
+	}
+	io_putstr("cursor backend ");
+	io_putstr(mouse_cursor_backend_name());
+	io_putchar('\n');
 	io_putstr("text ");
 	io_putstr("text ");
 	put_uint(fbtext_get_cols());
 	put_uint(fbtext_get_cols());
 	io_putstr(" x ");
 	io_putstr(" x ");

+ 2 - 1
gpu.c

@@ -94,7 +94,8 @@ void gpu_init(unsigned int lfb_base)
 		ati_init(active_bus, active_dev);
 		ati_init(active_bus, active_dev);
 	} else if (active_vendor == PCI_VENDOR_NVIDIA) {
 	} else if (active_vendor == PCI_VENDOR_NVIDIA) {
 		gpu_vendor_id = GPU_VENDOR_NVIDIA;
 		gpu_vendor_id = GPU_VENDOR_NVIDIA;
-		nvidia_init();
+		nvidia_init(active_bus, active_dev,
+			    (active_id >> 16) & 0xFFFF);
 	} else if (active_vendor == PCI_VENDOR_INTEL) {
 	} else if (active_vendor == PCI_VENDOR_INTEL) {
 		gpu_vendor_id = GPU_VENDOR_INTEL;
 		gpu_vendor_id = GPU_VENDOR_INTEL;
 	} else if (active_vendor == PCI_VENDOR_CIRRUS) {
 	} else if (active_vendor == PCI_VENDOR_CIRRUS) {

+ 1 - 1
kernel.ld

@@ -17,4 +17,4 @@ SECTIONS
 	}
 	}
 	_end = .;
 	_end = .;
 }
 }
-ASSERT(_end <= 0x40000, "Kernel too large would overwrite stack/DMA at 0x40000");
+ASSERT(_end <= 0x42000, "Kernel too large would overwrite stack/DMA at 0x42000");

+ 1 - 1
kexec.c

@@ -5,7 +5,7 @@
  * LFB are still valid) and jump straight to the new kernel's kernel_main,
  * LFB are still valid) and jump straight to the new kernel's kernel_main,
  * which re-inits IDT/PIC/PIT/mouse/process/VGA-shadow/terminal from scratch.
  * which re-inits IDT/PIC/PIT/mouse/process/VGA-shadow/terminal from scratch.
  *
  *
- * Because this code lives at 0x7E00..0x40000 (inside the region we overwrite),
+ * Because this code lives at 0x7E00..0x42000 (inside the region we overwrite),
  * the final "copy image to 0x7E00 and jump" is done by a small
  * the final "copy image to 0x7E00 and jump" is done by a small
  * position-independent trampoline relocated to 0x800 (below the kernel).
  * position-independent trampoline relocated to 0x800 (below the kernel).
  *
  *

+ 1 - 1
kexec.h

@@ -5,7 +5,7 @@
 void program_kexec_main(void);
 void program_kexec_main(void);
 
 
 /* Staging buffer for a received kernel image, and the maximum image size
 /* Staging buffer for a received kernel image, and the maximum image size
- * (the kernel region 0x7E00..0x40000).  Below the VGA window; see AGENTS.md. */
+ * (the kernel region 0x7E00..0x42000).  Below the VGA window; see AGENTS.md. */
 #define KEXEC_STAGE 0x58000
 #define KEXEC_STAGE 0x58000
 #define KEXEC_LOAD  0x7E00
 #define KEXEC_LOAD  0x7E00
 #define KEXEC_MAX   0x38200
 #define KEXEC_MAX   0x38200

+ 1 - 1
kexec_tramp.S

@@ -66,7 +66,7 @@ kexec_trampoline:
 	 * this the jump below can execute stale instructions. */
 	 * this the jump below can execute stale instructions. */
 	wbinvd
 	wbinvd
 
 
-	movl  $0x40800, %esp
+	movl  $0x42800, %esp
 	jmp   *%edx
 	jmp   *%edx
 
 
 .globl kexec_trampoline_end
 .globl kexec_trampoline_end

+ 17 - 0
mouse.c

@@ -197,6 +197,9 @@ static void mouse_cursor_setup(void)
 		return;
 		return;
 	}
 	}
 
 
+	/* NVIDIA nv50-family (ION/MCP79): no hardware cursor -- the VBIOS keeps
+	   the display engine (see nv50.c); the software cursor is used. */
+
 	mouse_hw_cursor = 0;
 	mouse_hw_cursor = 0;
 	cur_x = 100;
 	cur_x = 100;
 	cur_y = 100;
 	cur_y = 100;
@@ -376,6 +379,20 @@ int mouse_cursor_visible(void)
 	return mouse_cursor_flag;
 	return mouse_cursor_flag;
 }
 }
 
 
+int mouse_cursor_backend(void)
+{
+	return mouse_hw_cursor;
+}
+
+const char *mouse_cursor_backend_name(void)
+{
+	switch (mouse_hw_cursor) {
+	case 1:  return "cirrus hw";
+	case 2:  return "radeon hw";
+	default: return "software";
+	}
+}
+
 unsigned int mouse_packet_size(void)
 unsigned int mouse_packet_size(void)
 {
 {
 	return mouse_pkt_size;
 	return mouse_pkt_size;

+ 4 - 0
mouse.h

@@ -11,6 +11,10 @@ int mouse_ps2_present(void);
 int mouse_streaming_enabled(void);
 int mouse_streaming_enabled(void);
 unsigned int mouse_device_id(void);
 unsigned int mouse_device_id(void);
 int mouse_cursor_visible(void);
 int mouse_cursor_visible(void);
+/* Active cursor backend: 0 = software, 1 = Cirrus, 2 = Radeon. */
+int mouse_cursor_backend(void);
+/* Human-readable form of the above ("software", "nvidia hw", ...). */
+const char *mouse_cursor_backend_name(void);
 unsigned int mouse_packet_size(void);
 unsigned int mouse_packet_size(void);
 
 
 /* Current cursor position (hotspot) in framebuffer pixels. */
 /* Current cursor position (hotspot) in framebuffer pixels. */

+ 3 - 0
mousecmd.c

@@ -80,4 +80,7 @@ void program_mouseinfo_main(void)
 
 
 	io_putstr("mouse cursor ");
 	io_putstr("mouse cursor ");
 	io_putstr(mouse_cursor_visible() ? "visible\n" : "hidden\n");
 	io_putstr(mouse_cursor_visible() ? "visible\n" : "hidden\n");
+	io_putstr("mouse cursor backend ");
+	io_putstr(mouse_cursor_backend_name());
+	io_putchar('\n');
 }
 }

+ 18 - 0
mtrr.c

@@ -202,6 +202,24 @@ void mtrr_set_wc(unsigned int base, unsigned int size)
 		return;
 		return;
 	base &= ~(region_size - 1);
 	base &= ~(region_size - 1);
 
 
+	/* A kernel reload does not reset the MTRRs: reuse the WC entry an
+	 * earlier boot left for this base instead of burning another variable
+	 * slot (the kexec dev loop would otherwise exhaust the entries). */
+	for (i = 0; i < vcnt; i++) {
+		unsigned int esize;
+
+		rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
+		rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &hi);
+		if (!(mlo & MTRR_VALID) || (lo & 0xFF) != MTRR_TYPE_WC ||
+		    (lo & 0xFFFFF000u) != base)
+			continue;
+		esize = ((~mlo) & 0xFFFFF000u) + 0x1000;
+		if (esize < region_size)
+			continue;
+		serial_puts(SERIAL_COM1, "mtrr: LFB already write-combining\n");
+		return;
+	}
+
 	/* Only ever use a free variable entry.  BIOS-populated entries may
 	/* Only ever use a free variable entry.  BIOS-populated entries may
 	 * keep chipset MMIO uncached and must not be disturbed. */
 	 * keep chipset MMIO uncached and must not be disturbed. */
 	for (i = 0; i < vcnt; i++) {
 	for (i = 0; i < vcnt; i++) {

+ 1 - 1
netcmd.c

@@ -10,7 +10,7 @@
 
 
 /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
 /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
    hold a full-size frame, and keeping them out of BSS-as-much-as-possible
    hold a full-size frame, and keeping them out of BSS-as-much-as-possible
-   avoids bloating the kernel past its 0x40000 limit). Max Ethernet frame is
+   avoids bloating the kernel past its 0x42000 limit). Max Ethernet frame is
    1518 bytes (14 header + 1500 payload + 4 VLAN). */
    1518 bytes (14 header + 1500 payload + 4 VLAN). */
 #define NET_FRAME_MAX 1518
 #define NET_FRAME_MAX 1518
 static unsigned char net_rx_buf[NET_FRAME_MAX];
 static unsigned char net_rx_buf[NET_FRAME_MAX];

+ 109 - 0
nv50.c

@@ -0,0 +1,109 @@
+/*
+ * NVIDIA nv50-family (ION/MCP79) display engine diagnostics.
+ *
+ * The hardware cursor is deliberately NOT driven on this family.  What was
+ * learned while trying (ASUS AT3IONT-I, C79/ION, VBIOS-owned display):
+ *
+ *  - the VBIOS never hands the engine over: INTR_1 bit 8 stays set and
+ *    0x6194e8 reads 0x803, so nothing has "stolen" the display;
+ *  - EVO DMA channels accept DMA_CB and the USER_PUT doorbell, but never
+ *    fetch (USER_GET stays) until the display is taken over the way nouveau
+ *    does in nv50_disp_init -- which includes re-programming the whole mode;
+ *  - the cursor control/offset registers (0x610a58/0x610a9c) are engine-only
+ *    mirrors: plain writes do not stick, and the PIO method interface
+ *    (0x610300/0x610304) does not execute them either;
+ *  - once an EVO channel has run a batch it cannot be restarted (its busy
+ *    bit stays set), so even a takeover yields a single attempt per boot.
+ *
+ * The software cursor has none of these problems.  This module only probes
+ * the engine state at boot (serial / `make netmon`) so the situation stays
+ * visible for future work.
+ */
+
+#include "nv50.h"
+#include "nvidia.h"
+#include "serial.h"
+
+#define NV_PDISP_OBJECTS      0x610010
+#define NV_PDISP_INTR_1       0x610024
+#define NV_PDISP_PIO_CTRL     0x610300
+#define NV_PDISP_CURSOR_CTRL2(i)  (0x610270 + (i) * 0x10)
+#define NV_PDISP_EVO_CTRL(i)      (0x610200 + (i) * 0x10)
+#define NV_PDISP_EVO_DMA_CB(i)    (0x610204 + (i) * 0x10)
+#define NV_PDISP_VBIOS_HANDOFF    0x6194e8
+
+static unsigned int nv_mmio;
+
+static unsigned int mmio_rd(unsigned int ofs)
+{
+	return *(volatile unsigned int *)(nv_mmio + ofs);
+}
+
+static void log_hex(const char *s, unsigned int v)
+{
+	serial_puts(SERIAL_COM1, s);
+	serial_puthex(SERIAL_COM1, v);
+}
+
+/* Count the capability shadow/active register differences (the 0x6101XX
+ * "main control area that enables things" is all-zero while its 0x61XXXX
+ * shadow reads 0xFFFFFFFF on this VBIOS). */
+static unsigned int caps_diff(void)
+{
+	unsigned int i, d = 0;
+
+	if (mmio_rd(0x614004) != mmio_rd(0x610184))
+		d++;
+	for (i = 0; i < 2; i++) {
+		if (mmio_rd(0x616100 + i * 0x800) != mmio_rd(0x610190 + i * 0x10))
+			d++;
+		if (mmio_rd(0x616104 + i * 0x800) != mmio_rd(0x610194 + i * 0x10))
+			d++;
+		if (mmio_rd(0x616108 + i * 0x800) != mmio_rd(0x610198 + i * 0x10))
+			d++;
+		if (mmio_rd(0x61610c + i * 0x800) != mmio_rd(0x61019c + i * 0x10))
+			d++;
+	}
+	for (i = 0; i < 3; i++)
+		if (mmio_rd(0x61a000 + i * 0x800) != mmio_rd(0x6101d0 + i * 4))
+			d++;
+	for (i = 0; i < 2; i++)
+		if (mmio_rd(0x61c000 + i * 0x800) != mmio_rd(0x6101e0 + i * 4))
+			d++;
+	for (i = 0; i < 3; i++)
+		if (mmio_rd(0x61e000 + i * 0x800) != mmio_rd(0x6101f0 + i * 4))
+			d++;
+	return d;
+}
+
+void nv50_probe(void)
+{
+	unsigned int i;
+
+	if (!nvidia_available())
+		return;
+	nv_mmio = nvidia_bar0();
+
+	log_hex("nv50: pdobj ", mmio_rd(NV_PDISP_OBJECTS));
+	log_hex(" cursor2 ", mmio_rd(NV_PDISP_CURSOR_CTRL2(0)));
+	log_hex("/", mmio_rd(NV_PDISP_CURSOR_CTRL2(1)));
+	log_hex(" pio ", mmio_rd(NV_PDISP_PIO_CTRL));
+	log_hex(" intr1 ", mmio_rd(NV_PDISP_INTR_1));
+	log_hex(" vbios ", mmio_rd(NV_PDISP_VBIOS_HANDOFF));
+	serial_puts(SERIAL_COM1, " caps ");
+	serial_putdec(SERIAL_COM1, caps_diff());
+	serial_puts(SERIAL_COM1, "\n");
+
+	for (i = 0; i < 8; i++) {
+		unsigned int ctl = mmio_rd(NV_PDISP_EVO_CTRL(i));
+		unsigned int cb = mmio_rd(NV_PDISP_EVO_DMA_CB(i));
+
+		if (!ctl && !cb)
+			continue;
+		serial_puts(SERIAL_COM1, "nv50: evo");
+		serial_putdec(SERIAL_COM1, i);
+		log_hex(" ctrl ", ctl);
+		log_hex(" cb ", cb);
+		serial_puts(SERIAL_COM1, "\n");
+	}
+}

+ 11 - 0
nv50.h

@@ -0,0 +1,11 @@
+/* NVIDIA nv50-family (ION/MCP79) display-engine diagnostics.
+ *
+ * The hardware cursor is not driven on this family (the VBIOS keeps the
+ * display engine; see nv50.c for the details).  This is a boot-time,
+ * read-only probe of the engine state for serial / `make netmon`. */
+#ifndef NV50_H
+#define NV50_H
+
+void nv50_probe(void);
+
+#endif

+ 242 - 10
nvidia.c

@@ -1,19 +1,251 @@
 /*
 /*
- * NVIDIA GPU init.
+ * NVIDIA GPU support.
  *
  *
- * 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.
+ * Unlike ATI Mach64/Rage which need manual CRTC_OFF_PITCH fixups, NVIDIA
+ * cards handle VBE mode sets cleanly through their VBIOS: the real-mode stage
+ * provides a working LFB and no display fixup is needed.  The standard VGA
+ * CRTC registers at 0x3D4/0x3D5 must NOT be touched on NVIDIA -- the display
+ * engine ignores or misinterprets legacy VGA CRTC writes, which can cause
+ * display corruption or a 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.
+ * What this file adds on top of the (VBE-only) no-op is identification: the
+ * register aperture (BAR0, 16 MB on nv50+) is mapped and validated by reading
+ * NV_PMC_BOOT_0, from which the chipset id is decoded.  That aperture is also
+ * what the nv50-family display engine (ION/MCP79) is programmed through.
+ *
+ * NOTE: nv50-era VBIOSes under-report VBE TotalMemory (the AT3IONT-I reports
+ * 14 MB for a mode that needs 5 MB); don't trust it for VRAM sizing.
  */
  */
 
 
 #include "nvidia.h"
 #include "nvidia.h"
+#include "nv50.h"
+#include "pci.h"
+#include "process.h"
+#include "serial.h"
+
+#define NV_VENDOR_ID     0x10DE
+#define NV_BAR0_SIZE     0x1000000   /* register aperture (16 MB on nv50+) */
+
+static unsigned int nv_present;      /* BAR0 mapped + PMC_BOOT_0 sane */
+static unsigned int nv_boot0;
+static unsigned int nv_chipset;
+static unsigned int nv_bar0;
+static unsigned int nv_bar1;
+static unsigned int nv_bar1_size;
+static unsigned int nv_bar3;
+static unsigned int nv_bar3_size;
+static unsigned int nv_vram_base;
+static unsigned int nv_vram_size;
+static const char  *nv_name = "NVIDIA";
+
+struct nv_name {
+	unsigned short device_id;
+	const char *name;
+};
+
+/* Known display devices.  Not exhaustive -- unknown ids fall back to
+ * "NVIDIA"; the PCI id is printed separately by `display`. */
+static const struct nv_name nv_names[] = {
+	{ 0x0865, "C79 [GeForce 9300 / ION]" },
+	{ 0x0869, "MCP7A [GeForce 9400]" },
+	{ 0x086D, "C79 [GeForce 9200]" },
+	{ 0x0870, "C79 [GeForce 9400M]" },
+	{ 0x0874, "C79 [ION]" },
+	{ 0x0876, "C79 [GeForce 9400M / ION]" },
+	{ 0x087D, "C79 [ION]" },
+	{ 0x087E, "C79 [ION LE]" },
+	{ 0x0A64, "GT218 [ION]" },
+	{ 0x0A6F, "GT218M [ION]" },
+	{ 0x0A76, "GT218M [ION 2]" },
+	{ 0x1401, "GM206 [GeForce GTX 960]" },
+};
+
+/* Read a memory BAR's size in bytes (probe-and-restore, like lspci; handles
+ * 64-bit BARs by clearing/restoring the high dword as well). */
+static unsigned int nv_bar_size(unsigned int bus, unsigned int dev,
+				unsigned int reg, unsigned int is64)
+{
+	unsigned int val = pci_cfg_read32(bus, dev, 0, reg);
+	unsigned int hi = 0, raw;
+
+	if (val & 1)
+		return 0;                       /* I/O BAR */
+	if (is64)
+		hi = pci_cfg_read32(bus, dev, 0, reg + 4);
+	pci_cfg_write32(bus, dev, 0, reg, 0xFFFFFFFF);
+	if (is64)
+		pci_cfg_write32(bus, dev, 0, reg + 4, 0xFFFFFFFF);
+	raw = pci_cfg_read32(bus, dev, 0, reg) & 0xFFFFFFF0u;
+	pci_cfg_write32(bus, dev, 0, reg, val);
+	if (is64)
+		pci_cfg_write32(bus, dev, 0, reg + 4, hi);
+
+	if (raw == 0)
+		return 0;
+	return ~raw + 1;
+}
+
+void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id)
+{
+	unsigned int bar0, boot0, i;
+
+	nv_present = 0;
+	nv_boot0 = 0;
+	nv_chipset = 0;
+	nv_bar0 = 0;
+	nv_bar1 = 0;
+	nv_bar1_size = 0;
+	nv_bar3 = 0;
+	nv_bar3_size = 0;
+	nv_vram_base = 0;
+	nv_vram_size = 0;
+	nv_name = "NVIDIA";
+
+	for (i = 0; i < sizeof(nv_names) / sizeof(nv_names[0]); i++) {
+		if (nv_names[i].device_id == device_id) {
+			nv_name = nv_names[i].name;
+			break;
+		}
+	}
+
+	/* BAR0: register aperture.  The nv50+ display engine registers
+	 * (EVO/PDISPLAY) live in it starting at 0x00610000. */
+	bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
+	if (!(bar0 & 1) && (bar0 & 0xFFFFFFF0)) {
+		nv_bar0 = bar0 & 0xFFFFFFF0;
+		/* Map into every process page directory: gpu_init() runs
+		 * before the first process exists, but drivers may run in a
+		 * terminal context later (same reason r8168 does this). */
+		process_map_mmio_all(nv_bar0, NV_BAR0_SIZE);
+
+		boot0 = *(volatile unsigned int *)nv_bar0;
+		if (boot0 != 0 && boot0 != 0xFFFFFFFF) {
+			nv_boot0 = boot0;
+			nv_chipset = (boot0 >> 20) & 0xFF;
+			nv_present = 1;
+		}
+	}
+
+	/* BAR1: VRAM window (64-bit prefetchable on nv50+); BAR3: a second
+	 * (32 MB) VRAM alias the VBIOS uses for the VBE linear framebuffer. */
+	{
+		unsigned int b1 = pci_cfg_read32(bus, dev, 0, 0x14);
+		unsigned int b1hi = pci_cfg_read32(bus, dev, 0, 0x18);
+		unsigned int b3, b3hi;
+
+		if (!(b1 & 1) && (b1 & 0xFFFFFFF0) && b1hi == 0)
+			nv_bar1 = b1 & 0xFFFFFFF0;
+		if (nv_bar1) {
+			nv_bar1_size = nv_bar_size(bus, dev, 0x14, b1 & 4);
+			if (nv_bar1_size > 0x10000000u)   /* cap the mapping */
+				nv_bar1_size = 0x10000000u;
+			process_map_mmio_all(nv_bar1, nv_bar1_size);
+		}
+
+		b3 = pci_cfg_read32(bus, dev, 0, 0x1C);
+		b3hi = pci_cfg_read32(bus, dev, 0, 0x20);
+		if (!(b3 & 1) && (b3 & 0xFFFFFFF0) && b3hi == 0)
+			nv_bar3 = b3 & 0xFFFFFFF0;
+		if (nv_bar3) {
+			nv_bar3_size = nv_bar_size(bus, dev, 0x1C, b3 & 4);
+			if (nv_bar3_size > 0x04000000u)
+				nv_bar3_size = 0x04000000u;
+			process_map_mmio_all(nv_bar3, nv_bar3_size);
+		}
+	}
+
+	/* MCP77/78/79/89 (UMA): the memory controller reports the stolen
+	 * region's physical base and size at 0x100E10/0x100E14 (1 MB units).
+	 * The VBE framebuffer sits at its start, so subtracting the base gives
+	 * the display engine's VRAM address for the LFB. */
+	if (nv_present && (nv_chipset == 0xaa || nv_chipset == 0xac ||
+			   nv_chipset == 0xaf)) {
+		unsigned int vb = *(volatile unsigned int *)(nv_bar0 + 0x100E10);
+		unsigned int vs = *(volatile unsigned int *)(nv_bar0 + 0x100E14);
+
+		nv_vram_base = vb << 12;
+		nv_vram_size = vs << 12;
+	}
+
+	serial_puts(SERIAL_COM1, "nvidia: ");
+	serial_puts(SERIAL_COM1, nv_name);
+	serial_puts(SERIAL_COM1, " dev ");
+	serial_puthex(SERIAL_COM1, (NV_VENDOR_ID << 16) | device_id);
+	if (nv_present) {
+		serial_puts(SERIAL_COM1, " chipset ");
+		serial_puthex(SERIAL_COM1, nv_chipset);
+		serial_puts(SERIAL_COM1, " boot0 ");
+		serial_puthex(SERIAL_COM1, nv_boot0);
+		serial_puts(SERIAL_COM1, " regs ");
+		serial_puthex(SERIAL_COM1, nv_bar0);
+		serial_puts(SERIAL_COM1, " bar1 ");
+		serial_puthex(SERIAL_COM1, nv_bar1);
+		serial_puts(SERIAL_COM1, " bar3 ");
+		serial_puthex(SERIAL_COM1, nv_bar3);
+	} else {
+		serial_puts(SERIAL_COM1, " regs unavailable");
+	}
+	serial_puts(SERIAL_COM1, "\n");
+
+	if (nv_vram_size) {
+		serial_puts(SERIAL_COM1, "nvidia: vram base ");
+		serial_puthex(SERIAL_COM1, nv_vram_base);
+		serial_puts(SERIAL_COM1, " size ");
+		serial_puthex(SERIAL_COM1, nv_vram_size);
+		serial_puts(SERIAL_COM1, "\n");
+	}
+
+	/* Read-only look at the display engine (EVO channels, object table,
+	 * cursor state) for boards with no serial console (netmon mirror). */
+	nv50_probe();
+}
+
+int nvidia_available(void)
+{
+	return (int)nv_present;
+}
+
+const char *nvidia_chip_name(void)
+{
+	return nv_name;
+}
+
+unsigned int nvidia_chipset(void)
+{
+	return nv_chipset;
+}
+
+unsigned int nvidia_bar0(void)
+{
+	return nv_bar0;
+}
+
+unsigned int nvidia_bar1(void)
+{
+	return nv_bar1;
+}
+
+unsigned int nvidia_bar1_size(void)
+{
+	return nv_bar1_size;
+}
+
+unsigned int nvidia_bar3(void)
+{
+	return nv_bar3;
+}
+
+unsigned int nvidia_bar3_size(void)
+{
+	return nv_bar3_size;
+}
+
+unsigned int nvidia_vram_base(void)
+{
+	return nv_vram_base;
+}
 
 
-void nvidia_init(void)
+unsigned int nvidia_vram_size(void)
 {
 {
+	return nv_vram_size;
 }
 }

+ 34 - 2
nvidia.h

@@ -1,7 +1,39 @@
-/* NVIDIA GPU init: safe post-VBE setup for GeForce cards. */
+/* NVIDIA GPU support.
+ *
+ * The VBE mode set (vesa_stage.S) is all the *display* setup NVIDIA cards
+ * need; the legacy VGA CRTC registers at 0x3D4/0x3D5 must not be touched
+ * (see nvidia.c).  This file identifies the card and maps its register
+ * aperture (BAR0) so the chip can be named and the nv50-family (ION/MCP79)
+ * display engine can be driven through it.
+ */
 #ifndef NVIDIA_H
 #ifndef NVIDIA_H
 #define NVIDIA_H
 #define NVIDIA_H
 
 
-void nvidia_init(void);
+/* Identify the NVIDIA adapter at bus/dev and map its register aperture.
+ * device_id = high 16 bits of PCI config 0x00 (e.g. 0x087D for ION). */
+void nvidia_init(unsigned int bus, unsigned int dev, unsigned int device_id);
+
+/* 1 when the register aperture was mapped and NV_PMC_BOOT_0 looks valid. */
+int nvidia_available(void);
+
+/* Human-readable device name ("C79 [ION]", "GM206 [GeForce GTX 960]", ...). */
+const char *nvidia_chip_name(void);
+
+/* NV_PMC_BOOT_0 chipset id (0 when MMIO could not be validated). */
+unsigned int nvidia_chipset(void);
+
+/* BAR0 register aperture / BAR1 VRAM window / BAR3 VBE window bases
+ * (0 = absent) and their probed sizes. */
+unsigned int nvidia_bar0(void);
+unsigned int nvidia_bar1(void);
+unsigned int nvidia_bar1_size(void);
+unsigned int nvidia_bar3(void);
+unsigned int nvidia_bar3_size(void);
+
+/* Stolen-VRAM geometry (MCP77/79/89): physical base and size in bytes
+ * (0 when not applicable).  VRAM addresses used by the display engine are
+ * relative to this base. */
+unsigned int nvidia_vram_base(void);
+unsigned int nvidia_vram_size(void);
 
 
 #endif
 #endif

+ 40 - 0
peek.c

@@ -2,6 +2,8 @@
  * MMIO registers, e.g. a driver's BAR once mapped).
  * MMIO registers, e.g. a driver's BAR once mapped).
  *
  *
  *   peek <addr> [len]      default len 64, max 256
  *   peek <addr> [len]      default len 64, max 256
+ *   poke <addr> <val>      write one dword (e.g. an MMIO register), print
+ *                          the read-back
  *
  *
  * Reading an unmapped address faults; the exception handler shows EIP/CR2.
  * Reading an unmapped address faults; the exception handler shows EIP/CR2.
  */
  */
@@ -89,3 +91,41 @@ void program_peek_main(const char *arg)
 		io_putchar('\n');
 		io_putchar('\n');
 	}
 	}
 }
 }
+
+/* poke <addr> <val>: write one 32-bit dword and print the read-back.  Used to
+ * drive MMIO experiments (e.g. the GPU's PIO method interface) from the
+ * shell. */
+void program_poke_main(const char *arg)
+{
+	const char *p = arg;
+	volatile unsigned int *mem;
+	unsigned int addr, val;
+
+	if (parse_hex(p, &addr, &p) != 0) {
+		io_putstr("poke: usage poke <hexaddr> <hexval>\n");
+		return;
+	}
+	while (*p == ' ')
+		p++;
+	if (parse_hex(p, &val, &p) != 0) {
+		io_putstr("poke: usage poke <hexaddr> <hexval>\n");
+		return;
+	}
+	while (*p == ' ')
+		p++;
+	if (*p) {
+		io_putstr("poke: usage poke <hexaddr> <hexval>\n");
+		return;
+	}
+
+	mem = (volatile unsigned int *)(addr & ~3u);
+	*mem = val;
+
+	io_putstr("poke ");
+	put_hex8(addr & ~3u);
+	io_putstr(" <- ");
+	put_hex8(val);
+	io_putstr(" reads ");
+	put_hex8(*mem);
+	io_putchar('\n');
+}

+ 1 - 1
process.c

@@ -118,7 +118,7 @@ void process_init(void)
 	procs[0].screen     = (void *)0;
 	procs[0].screen     = (void *)0;
 	procs[0].stack_base = 0;  /* bootstrap stack, never freed */
 	procs[0].stack_base = 0;  /* bootstrap stack, never freed */
 	procs[0].cr3        = paging_current_cr3();
 	procs[0].cr3        = paging_current_cr3();
-	procs[0].kstack_top = 0x40800;  /* bootstrap stack top */
+	procs[0].kstack_top = 0x42800;  /* bootstrap stack top */
 	name_copy(procs[0].name, "wm");
 	name_copy(procs[0].name, "wm");
 	current_pid = 0;
 	current_pid = 0;
 }
 }

+ 1 - 1
radeon.c

@@ -17,7 +17,7 @@
  *  - Cursor mode is RADEON_CRTC_CUR_MODE_24BPP (2) in bits 22:20.
  *  - 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
  * NOTE: the bitmap is written straight into VRAM.  Do not use a local
- * [16 KB] array -- the kernel stack is only 2 KB (0x40800..0x40000) and a
+ * [16 KB] array -- the kernel stack is only 2 KB (0x42800..0x42000) and a
  * 16 KB stack allocation overflows it.
  * 16 KB stack allocation overflows it.
  */
  */
 
 

+ 3 - 3
run.c

@@ -1,4 +1,4 @@
-/* run command: load raw binary from a FAT filesystem to 0x41000 and execute.
+/* run command: load raw binary from a FAT filesystem to 0x43000 and execute.
  * Usage: run /<device>/<file>. */
  * Usage: run /<device>/<file>. */
 
 
 #include "fat.h"
 #include "fat.h"
@@ -10,7 +10,7 @@
 #include "vga.h"
 #include "vga.h"
 #include "vfs.h"
 #include "vfs.h"
 
 
-#define PROGRAM_LOAD_ADDR 0x41000
+#define PROGRAM_LOAD_ADDR 0x43000
 #define PROGRAM_LOAD_MAX  0x10000
 #define PROGRAM_LOAD_MAX  0x10000
 
 
 /* User programs embedded in the kernel image (like hello.elf): they travel
 /* User programs embedded in the kernel image (like hello.elf): they travel
@@ -147,7 +147,7 @@ void program_run_main(const char *filename)
 	((void (*)(void))PROGRAM_LOAD_ADDR)();
 	((void (*)(void))PROGRAM_LOAD_ADDR)();
 }
 }
 
 
-/* run serial: execute the binary already loaded at 0x41000 via serial_recv. */
+/* run serial: execute the binary already loaded at 0x43000 via serial_recv. */
 
 
 void program_run_serial_main(const char *arg)
 void program_run_serial_main(const char *arg)
 {
 {

+ 1 - 1
run_api.h

@@ -3,7 +3,7 @@
 #ifndef RUN_API_H
 #ifndef RUN_API_H
 #define RUN_API_H
 #define RUN_API_H
 
 
-#define PROGRAM_API_ADDR 0x40F00
+#define PROGRAM_API_ADDR 0x42F00
 #define IO_KEY_ESC       0x1B
 #define IO_KEY_ESC       0x1B
 #define IO_KEY_SPACE     0x20
 #define IO_KEY_SPACE     0x20
 #define IO_KEY_LEFT      0x100
 #define IO_KEY_LEFT      0x100

+ 1 - 1
screen.h

@@ -5,7 +5,7 @@
 #define SCREEN_H
 #define SCREEN_H
 
 
 #define SCREEN_COLS 64
 #define SCREEN_COLS 64
-#define SCREEN_ROWS 20
+#define SCREEN_ROWS 32
 
 
 struct text_screen {
 struct text_screen {
 	unsigned short cells[SCREEN_COLS * SCREEN_ROWS];
 	unsigned short cells[SCREEN_COLS * SCREEN_ROWS];

+ 2 - 2
serial_recv.c

@@ -1,10 +1,10 @@
-/* Serial receive command: receives a binary via serial into 0x41000. */
+/* Serial receive command: receives a binary via serial into 0x43000. */
 
 
 #include "io.h"
 #include "io.h"
 #include "serial.h"
 #include "serial.h"
 #include "bin_recv.h"
 #include "bin_recv.h"
 
 
-#define PROGRAM_LOAD_ADDR 0x41000
+#define PROGRAM_LOAD_ADDR 0x43000
 #define PROGRAM_LOAD_MAX  0x10000
 #define PROGRAM_LOAD_MAX  0x10000
 
 
 unsigned int serial_recv_size;
 unsigned int serial_recv_size;

+ 83 - 43
terminal.c

@@ -35,6 +35,7 @@ extern void program_mem_main(void);
 extern void program_format_main(const char *arg);
 extern void program_format_main(const char *arg);
 extern void program_lspci_main(const char *filter);
 extern void program_lspci_main(const char *filter);
 extern void program_peek_main(const char *arg);
 extern void program_peek_main(const char *arg);
+extern void program_poke_main(const char *arg);
 extern void program_netinfo_main(void);
 extern void program_netinfo_main(void);
 extern void program_netrecv_main(void);
 extern void program_netrecv_main(void);
 extern void program_netsend_main(void);
 extern void program_netsend_main(void);
@@ -314,8 +315,34 @@ static int is_cmd_peek(const char *line, unsigned int line_len, char *arg_buf, u
 	return (i == line_len);
 	return (i == line_len);
 }
 }
 
 
-static int is_cmd_prog(const char *line, unsigned int line_len)
+/* poke <addr> <val>: write one dword to memory/MMIO. */
+static int is_cmd_poke(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
 {
 {
+	unsigned int i = 0, j = 0;
+
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 4 > line_len)
+		return 0;
+	if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'k' ||
+	    line[i+3] != 'e')
+		return 0;
+	i += 4;
+	if (i < line_len && line[i] != ' ')
+		return 0;  /* "poke" must be a whole word */
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len)
+		return 0;  /* address and value required */
+	while (i < line_len && j + 1 < buf_size)
+		arg_buf[j++] = line[i++];
+	arg_buf[j] = '\0';
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
+static int is_cmd_prog(const char *line, unsigned int line_len){
 	unsigned int i = 0;
 	unsigned int i = 0;
 	while (i < line_len && line[i] == ' ')
 	while (i < line_len && line[i] == ' ')
 		i++;
 		i++;
@@ -784,6 +811,58 @@ static int is_cmd_format(const char *line, unsigned int line_len, char *dev_buf,
 	return (i == line_len && j > 0);
 	return (i == line_len && j > 0);
 }
 }
 
 
+/* Help listing: printed as two 32-column columns so it fits the 64-column
+   terminal without scrolling. */
+static const char *const help_items[] = {
+	"cd [path]", "ls [path]", "cat <path>", "save <path> <text>",
+	"rm <path>", "mkdir <path>", "rmdir <path>", "hello", "reboot",
+	"poweroff", "run <name>|<file>", "space", "prog", "clock", "colors",
+	"display", "cpuinfo", "lsusb", "mouseinfo", "storage", "mem",
+	"format <device>", "lspci [id]", "peek <addr> [len]", "poke <addr> <val>",
+	"netinfo",
+	"netrecv", "netsend", "netip [a.b.c.d]", "netping <a.b.c.d>",
+	"netlog <text>", "netconsole [on|off|host <ip>]", "dhcp", "netregs",
+	"netloop", "netreset", "ping", "ps", "kill <pid>", "kexec", "dbg",
+	"serial test|recv", "clear",
+};
+
+static void help_puts(const char *s)
+{
+	while (*s)
+		io_putchar(*s++);
+}
+
+static void help_item_pad(const char *s)
+{
+	unsigned int n = 0;
+
+	while (s[n]) {
+		io_putchar(s[n]);
+		n++;
+	}
+	while (n < 32) {
+		io_putchar(' ');
+		n++;
+	}
+}
+
+static void help_print(void)
+{
+	unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
+	unsigned int half = (nitems + 1) / 2;
+	unsigned int k;
+
+	for (k = 0; k < half; k++) {
+		if (k + half < nitems) {
+			help_item_pad(help_items[k]);
+			help_puts(help_items[k + half]);
+		} else {
+			help_puts(help_items[k]);
+		}
+		io_putchar('\n');
+	}
+}
+
 static int is_cmd_help(const char *line, unsigned int line_len)
 static int is_cmd_help(const char *line, unsigned int line_len)
 {
 {
 	unsigned int i = 0;
 	unsigned int i = 0;
@@ -1225,6 +1304,8 @@ void term_run(struct term *t)
 						term_spawn_arg(t, "lspci", program_lspci_main, lspci_arg);
 						term_spawn_arg(t, "lspci", program_lspci_main, lspci_arg);
 					} else if (is_cmd_peek(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
 					} else if (is_cmd_peek(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
 						term_spawn_arg(t, "peek", program_peek_main, peek_arg);
 						term_spawn_arg(t, "peek", program_peek_main, peek_arg);
+					} else if (is_cmd_poke(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
+						term_spawn_arg(t, "poke", program_poke_main, peek_arg);
 					} else if (is_cmd_netinfo(t->line, t->line_len)) {
 					} else if (is_cmd_netinfo(t->line, t->line_len)) {
 						term_spawn(t, "netinfo", program_netinfo_main);
 						term_spawn(t, "netinfo", program_netinfo_main);
 					} else if (is_cmd_netrecv(t->line, t->line_len)) {
 					} else if (is_cmd_netrecv(t->line, t->line_len)) {
@@ -1326,48 +1407,7 @@ void term_run(struct term *t)
 					} else if (is_cmd_dbg(t->line, t->line_len)) {
 					} else if (is_cmd_dbg(t->line, t->line_len)) {
 						program_dbg_main();
 						program_dbg_main();
 					} else if (is_cmd_help(t->line, t->line_len)) {
 					} else if (is_cmd_help(t->line, t->line_len)) {
-						io_putstr("cd [path]\n");
-						io_putstr("ls [path]\n");
-						io_putstr("cat <path>\n");
-						io_putstr("save <path> <text>\n");
-						io_putstr("rm <path>\n");
-						io_putstr("mkdir <path>\n");
-						io_putstr("rmdir <path>\n");
-						io_putstr("hello\n");
-						io_putstr("reboot\n");
-						io_putstr("poweroff\n");
-						io_putstr("run <name>|<file>\n");
-						io_putstr("space\n");
-						io_putstr("prog\n");
-						io_putstr("clock\n");
-						io_putstr("colors\n");
-						io_putstr("display\n");
-						io_putstr("cpuinfo\n");
-						io_putstr("lsusb\n");
-						io_putstr("mouseinfo\n");
-						io_putstr("storage\n");
-						io_putstr("mem\n");
-						io_putstr("format <device>\n");
-						io_putstr("lspci [id]\n");
-						io_putstr("peek <addr> [len]\n");
-						io_putstr("netinfo\n");
-						io_putstr("netrecv\n");
-						io_putstr("netsend\n");
-						io_putstr("netip [a.b.c.d]\n");
-						io_putstr("netping <a.b.c.d>\n");
-						io_putstr("netlog <text>\n");
-						io_putstr("netconsole [on|off|host <ip>]\n");
-						io_putstr("dhcp\n");
-						io_putstr("netregs\n");
-						io_putstr("netloop\n");
-						io_putstr("netreset\n");
-						io_putstr("ping\n");
-						io_putstr("ps\n");
-						io_putstr("kill <pid>\n");
-						io_putstr("kexec\n");
-						io_putstr("dbg\n");
-						io_putstr("serial test|recv\n");
-						io_putstr("clear\n");
+						help_print();
 				} else if (is_cmd_clear(t->line, t->line_len)) {
 				} else if (is_cmd_clear(t->line, t->line_len)) {
 					io_clear_screen();
 					io_clear_screen();
 					term_print_prompt(t, 1);
 					term_print_prompt(t, 1);

+ 1 - 1
tss.c

@@ -57,7 +57,7 @@ void gdt_tss_init(void)
 	__asm__ volatile ("lgdt %0" : : "m"(gdtp));
 	__asm__ volatile ("lgdt %0" : : "m"(gdtp));
 
 
 	tss.ss0  = 0x10;    /* kernel data segment */
 	tss.ss0  = 0x10;    /* kernel data segment */
-	tss.esp0 = 0x40800; /* bootstrap stack; updated by switch.S per process */
+	tss.esp0 = 0x42800; /* bootstrap stack; updated by switch.S per process */
 	tss.iomap_base = 0xFFFF;  /* no I/O permission bitmap (deny ring-3 I/O) */
 	tss.iomap_base = 0xFFFF;  /* no I/O permission bitmap (deny ring-3 I/O) */
 	__asm__ volatile ("ltr %0" : : "r"((unsigned short)0x28));
 	__asm__ volatile ("ltr %0" : : "r"((unsigned short)0x28));
 }
 }

+ 1 - 1
uhci.c

@@ -72,7 +72,7 @@ struct uhci_qh {
 #define UHCI_NUM_TD 16
 #define UHCI_NUM_TD 16
 
 
 /* Frame list lives in low memory (below the kernel at 0x7E00) to avoid
 /* Frame list lives in low memory (below the kernel at 0x7E00) to avoid
-   bloating the kernel's BSS past the 0x40000 linker budget. It must be
+   bloating the kernel's BSS past the 0x42000 linker budget. It must be
    4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory map). */
    4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory map). */
 #define UHCI_FRAME_LIST_ADDR 0x3000u
 #define UHCI_FRAME_LIST_ADDR 0x3000u
 
 

+ 2 - 2
user.ld

@@ -1,9 +1,9 @@
-/* User program: load at 0x41000, same as run.c PROGRAM_LOAD_ADDR. */
+/* User program: load at 0x43000, same as run.c PROGRAM_LOAD_ADDR. */
 ENTRY(_start)
 ENTRY(_start)
 
 
 SECTIONS
 SECTIONS
 {
 {
-	. = 0x41000;
+	. = 0x43000;
 	.text : { *(.text .text.*) }
 	.text : { *(.text .text.*) }
 	.rodata : { *(.rodata .rodata.*) }
 	.rodata : { *(.rodata .rodata.*) }
 	.data : { *(.data .data.*) }
 	.data : { *(.data .data.*) }

+ 1 - 1
vesa_stage.S

@@ -223,7 +223,7 @@ stage2_pm:
 	movw  %ax, %fs
 	movw  %ax, %fs
 	movw  %ax, %gs
 	movw  %ax, %gs
 	movw  %ax, %ss
 	movw  %ax, %ss
-	movl  $0x40800, %esp
+	movl  $0x42800, %esp
 	jmp   kernel_main
 	jmp   kernel_main
 
 
 .align 4
 .align 4