Skip to content

PCI Subsystem

NtinosTheGamer2324 edited this page Dec 11, 2025 · 1 revision

PCI Subsystem

The PCI (Peripheral Component Interconnect) subsystem handles device enumeration and configuration.

Overview

PCI provides a standardized way to discover and configure hardware devices.

┌────────────────────────────────────┐
│      PCI Device Detection          │
└───────────┬────────────────────────┘
            │
┌───────────▼────────────────────────┐
│  Scan Bus 0-255                    │
│  └─ Device 0-31                    │
│     └─ Function 0-7                │
└───────────┬────────────────────────┘
            │
┌───────────▼────────────────────────┐
│  Read Device Info                  │
│  - Vendor ID / Device ID           │
│  - Class / Subclass                │
│  - BARs (Base Address Registers)   │
└───────────┬────────────────────────┘
            │
┌───────────▼────────────────────────┐
│  Initialize Driver                 │
│  (AHCI, USB, Network, etc.)        │
└────────────────────────────────────┘

PCI Configuration Space

Accessing PCI

Ports:

#define PCI_CONFIG_ADDRESS  0xCF8
#define PCI_CONFIG_DATA     0xCFC

Reading Configuration

uint32_t pci_config_read_dword(uint8_t bus, uint8_t device, 
                                uint8_t func, uint8_t offset) {
    uint32_t address = (1 << 31)           // Enable bit
                     | (bus << 16)
                     | (device << 11)
                     | (func << 8)
                     | (offset & 0xFC);
    
    outl(PCI_CONFIG_ADDRESS, address);
    return inl(PCI_CONFIG_DATA);
}

PCI Device Classes

Class Description
0x00 Unclassified
0x01 Mass Storage Controller
0x02 Network Controller
0x03 Display Controller
0x06 Bridge Device
0x0C Serial Bus Controller (USB)

Device Enumeration

void pci_init(void) {
    for (int bus = 0; bus < 256; bus++) {
        for (int device = 0; device < 32; device++) {
            uint16_t vendor = pci_config_read_word(bus, device, 0, 0);
            if (vendor != 0xFFFF) {
                // Device exists
                pci_probe_device(bus, device);
            }
        }
    }
}

Next Steps

Clone this wiki locally