SchOS/kernel/drivers/gpu/vga/vga_pci.c
Nexoniarz 817d666068 gfx: the console and the crash screen stop drawing on a driver by name
Same disease as the network stack, one subsystem over. kernel/dev/vga.c
was three things wearing one name: the owner of the framebuffer, a
bitmap drawing library, and the text console. Only the first is a
driver, and because they shared a file they shared a prefix -- so
panic.c drew the crash screen with vga_fill_rect, console.c printed with
vga_putc, /dev/fb0 handed out vga_fb_phys, and every one of those files
named a display driver it had no business knowing about.

Split three ways:

  - kernel/drivers/gpu/vga/vga_fb.c owns the VBE framebuffer -- the mode
    the loader programmed and handed over, the aperture, and the display
    class vtable. It is a driver and now lives with the driver that
    finds the card on the bus.
  - kernel/dev/gfx.c draws: pixels, shapes, glyphs, the cursor, scroll,
    the ANSI erase operations. It names no driver. The framebuffer
    arrives through gfx_attach_scanout(), so a driver publishes what it
    owns and this file draws on it without knowing what it is.
  - include/gfx.h is the interface, and says why it is deliberately not
    the display class: that one is about frames -- acquire, present,
    vblank -- and the crash screen has to work when nothing else does.

main() no longer calls a display driver. platform_register_devices()
takes the framebuffer address and brings up whatever owns it, which is
where driver names belong; kernel/drivers/registry.c is the file whose
entire job is naming every driver in the kernel.

virtio-gpu got the e1000 treatment as well: instances in an array, the
instance hung on dev->priv by an `attached` hook, and every op reading
it rather than a file-static. It refused the second adapter outright
before, which is not hypothetical -- integrated plus discrete is an
ordinary laptop. The VGA driver deliberately has no such hook, and says
so: there is one framebuffer the loader programmed however many
VGA-class controllers the bus turns up, and a second one publishing the
same scanout is the truth rather than a bug.

Private headers moved next to their drivers: include/virtio.h and
include/xhci.h each had exactly one includer. include/ is for headers
the rest of the kernel needs.

Deleted rather than moved: consoleinit() programmed the sixteen ANSI
colours into the VGA DAC and disabled text-mode blink, by writing ports
0x3C0-0x3C9 from the console. The comment three lines above the colour
table already said this does nothing in a 32bpp linear mode -- there is
no palette to index -- so it was both dead and in the wrong file.

Verified, and this is a refactor so the evidence is that nothing moved:
  - bootcheck -Fb -Net: 59 [ok], no failures. That covers the console
    through every command, /dev/fb0's geometry, writing pixels through
    write() and mapping the framebuffer through mmap.
  - make crashshot: the crash screen renders exactly as before --
    shapes, glyphs, logo and QR code -- which is the whole of gfx.c
    exercised at once, in a panic, with the scanout attached by a driver.
  - make schcom-test: five pixel assertions from the host oracle still
    pass, so the display class and the compositor above it are unmoved.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-15 17:04:10 +02:00

100 lines
3.7 KiB
C

// Display controller -- the first driver to find its hardware by asking.
//
// It does not draw anything: vga_fb.c in this directory owns the framebuffer,
// and it learned where that is from the bootloader's VBE call. What this adds
// is a second, independent answer to the same question, from the bus rather
// than from the BIOS -- and a complaint when the two disagree.
//
// No `attached` hook and no per-instance state, unlike the e1000 and
// virtio-gpu: there is exactly one framebuffer the loader programmed, however
// many VGA-class controllers the bus turns up. A second one binding here
// publishes the same scanout, which is the truth -- it is the same screen.
//
// That is worth more than it sounds. Three places in the kernel used to
// hardcode 0xfd000000 as the framebuffer address, and the only reason nobody
// noticed was that QEMU's stdvga happens to put it there. A disagreement
// between VBE and the card's own BAR is exactly the symptom that would have
// shown up first on hardware where the guess was wrong, and until now there
// was nothing in the kernel capable of noticing it.
#include "types.h"
#include "defs.h"
#include "param.h"
#include "x86.h"
#include "spinlock.h"
#include "sleeplock.h"
#include "fs.h"
#include "file.h"
#include "pci.h"
#include "display.h"
// The framebuffer lives in the first memory BAR big enough to hold a screen.
// Display controllers commonly expose two apertures -- the framebuffer and a
// much smaller register block -- and which index they land on varies, so the
// choice is made by size rather than by position.
static struct pci_bar *
framebuffer_bar(struct pci_dev *d)
{
uint64 need = vga_fb_size();
for (int i = 0; i < 6; i++) {
struct pci_bar *b = &d->bar[i];
if (b->base == 0 || b->is_io)
continue;
if (b->size >= need)
return b;
}
return 0;
}
static int
vga_pci_probe(struct pci_dev *d)
{
struct pci_bar *bar = framebuffer_bar(d);
uint64 vbe = vga_fb_phys();
// Memory decoding has to be on for the aperture to respond at all. The BIOS
// will have done this already to put up a boot splash, but a device we only
// reached through enumeration cannot be assumed to have been touched.
pci_enable(d, PCI_CMD_MEMORY);
if (bar == 0) {
cprintf("vga: display controller at %x:%x.%x has no framebuffer aperture\n",
d->bus, d->slot, d->func);
return 0; // still ours; nothing more to do
}
if (vbe != 0 && bar->base != vbe) {
// Trust VBE: it describes the mode that is actually programmed and being
// displayed, which is the thing vga.c is writing into. The BAR is the
// card's whole aperture and the mode may sit at an offset within it. Say
// so rather than silently preferring one.
cprintf("vga: framebuffer disagreement -- VBE says %x, BAR says %x\n",
(uint32)vbe, (uint32)bar->base);
}
cprintf("vga: display controller %x:%x, aperture %x (%d MB)\n",
d->vendor, d->device, (uint32)bar->base,
(uint32)(bar->size / (1024 * 1024)));
return 0;
}
// Matched by class, not by model: every VGA-compatible controller answers
// class 0x03, and this driver cares about none of the differences between
// them.
static const struct pci_match vga_pci_match[] = {
{ PCI_ANY, PCI_ANY, PCI_CLASS_DISPLAY, 0xFF },
{ 0, 0, 0, 0 },
};
// The display vtable lives in kernel/dev/vga.c, which owns the framebuffer;
// this file only finds the card on the bus. Publishing it here is what makes
// device_find_class(DEVCLASS_DISPLAY) return something a compositor can use.
extern const struct display_ops vga_display_ops;
const struct pci_driver vga_pci_driver = {
.name = "vga",
.match = vga_pci_match,
.ops = &vga_display_ops,
.probe = vga_pci_probe,
};