kv2: initialize memory manager, early pci devices

Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
Michael Shaw 2026-05-12 02:59:37 -04:00
commit a252a3f8e9
58 changed files with 1270 additions and 505 deletions

View file

@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += acpi/
obj-y += pci/
obj-y += tty/
obj-y += ahci.o

View file

@ -1,8 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "kernel/page.h"
#include <kernel/common.h>
#include <kernel/mem.h>
#include <kernel/requests.h>
#include <drivers/pci.h>
#include <drivers/acpi.h>
#include <lib/string.h>
@ -49,10 +51,18 @@ void acpi_load_pci()
for (u32 i = 0; i < n_entries; i++) {
struct acpi_mcfg_entry *entry = &mcfg->entries[i];
u64 pci_virt_base = TO_VIRT(entry->base_address);
kdebug("MCFG entry: base=%p, start_bus=%d,end_bus=%d,seg_group=%d\n", pci_virt_base, entry->start_bus_number,
kdebug("MCFG entry: base=%p, start_bus=%d,end_bus=%d,seg_group=%d\n", entry->base_address, entry->start_bus_number,
entry->end_bus_number, entry->pci_segment_group);
struct pci_host host = {
.base = entry->base_address,
.start_bus = entry->start_bus_number,
.end_bus = entry->end_bus_number,
.segment_group = entry->pci_segment_group,
};
pci_register_host(&host);
}
}
@ -69,5 +79,5 @@ void acpi_init()
if (rsdp->revision == 2)
xsdt = (struct xsdt *)(rsdp->xsdt_address | hhdm_start);
acpi_load_pci();
acpi_load_pci();
}

View file

@ -3,13 +3,14 @@
#include <kernel/mem.h>
#include <kernel/slab.h>
#include <drivers/pci.h>
#include <drivers/ahci.h>
#include <lib/string.h>
#include <lib/stdio.h>
#include <drivers/ahci.h>
static uint8_t sata_device_count = 0;
static struct sata_device sata_devices[32] = { 0 };
static struct sata_device sata_devices[32] = {0};
static paddr_t ahci_pbase = 0;
static vaddr_t ahci_vbase = 0;
@ -70,15 +71,17 @@ static void ahci_rebase(hbaport_t *port, int n)
cmdheader[i].prdt_len = 8;
cmdheader[i].cmd_table_base = (ahci_pbase) + (n << 13) + (40 << 10) + (i << 8);
void *ctba = (void *)((uintptr_t)cmdheader[i].cmd_table_base | hhdm_start);
void *ctba = (void *)(TO_VIRT((paddr_t)cmdheader[i].cmd_table_base));
memset(ctba, 0, 256);
}
start_cmd(port);
}
static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
static void ahci_probe_ports(struct ahci_host *host)
{
struct pci_device *dev = host->device;
hbamem_t *abar = host->abar;
uint32_t pi = abar->pi;
for (int i = 0; i < 32; i++) {
if (pi & 1) {
@ -86,13 +89,12 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
if (type == AHCI_DEV_SATA) {
struct sata_device *sdev = &sata_devices[sata_device_count];
sdev->port = i;
sdev->controller = dev;
sdev->abar = abar;
sdev->host = host;
/* char *name = kzalloc(16, ALLOC_KERN);
char *name = kzalloc(16, ALLOC_KERN);
name[15] = 0;
snprintf(name, "ahci%d", 15, sata_device_count);*/
snprintf(name, 15, "ahci%d", sata_device_count);
size_t sector_count = ((size_t)abar->ports[i].ssts) << 32 | abar->ports[i].ssts;
sdev->sector_count = sector_count;
@ -100,7 +102,7 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
sdev->sector_size = 512;
/* bdev = block_register(name, &ahci_block_ops, sdev, sector_count, sdev->sector_size);
block_gpt_init(bdev);*/
block_gpt_init(bdev); */
ahci_rebase(&abar->ports[i], i);
sata_device_count++;
@ -132,7 +134,7 @@ static int cmdslot(hbaport_t *port)
int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write)
{
hbaport_t *port = &dev->abar->ports[dev->port];
hbaport_t *port = &dev->host->abar->ports[dev->port];
/* clear pending interrupt */
port->is = (uint32_t)-1;
@ -210,6 +212,20 @@ int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pa
return true;
}
void ahci_init()
void ahci_init_device(struct pci_device *device)
{
}
void ahci_init()
{
list_t dev_list;
list_create(&dev_list);
size_t n = pci_find_device(0x0001, 0x0006, &dev_list);
list_item_t *item = dev_list.head;
while (item) {
ahci_init_device(item->ptr);
item = item->next;
}
}

14
drivers/block.c Normal file
View file

@ -0,0 +1,14 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <drivers/block.h>
inline ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n)
{
return dev->ops.read(dev, buf, offset, n);
}
inline ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n)
{
return dev->ops.write(dev, buf, offset, n);
}

2
drivers/pci/Makefile Normal file
View file

@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += pci.o

View file

@ -1 +1,151 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/slab.h>
#include <kernel/page.h>
#include <drivers/pci.h>
#include <lib/list.h>
#include <lib/string.h>
static struct pci_host hosts[128];
static size_t n_hosts;
static list_t pci_devices = {NULL};
static mem_cache_t *pci_device_cache = NULL;
void pci_register_host(struct pci_host *host)
{
if (n_hosts < ARRAY_SIZE(hosts))
memcpy(hosts + n_hosts, host, sizeof(*host));
n_hosts += 1;
}
static inline void *pcie_get_config_addr(struct pci_host *host, u8 bus, u8 dev, u8 func, u16 reg)
{
return (void *)(host->base +
(((u64)bus << 20) | ((u64)(dev & 0x1F) << 15) | ((u64)(func & 0x07) << 12) | ((u64)reg & 0xFFF)));
}
u16 pci_config_read16(struct pci_host *host, u8 bus, u8 dev, u8 func, u16 reg)
{
volatile u16 *addr = (volatile u16 *)pcie_get_config_addr(host, bus, dev, func, reg);
return *addr;
}
u32 pci_config_read32(struct pci_host *host, u8 bus, u8 dev, u8 func, u16 reg)
{
volatile u32 *addr = (volatile u32 *)pcie_get_config_addr(host, bus, dev, func, reg);
return *addr;
}
u8 pci_config_read8(struct pci_host *host, u8 bus, u8 dev, u8 func, u16 reg)
{
volatile u8 *addr = (volatile u8 *)pcie_get_config_addr(host, bus, dev, func, reg);
return *addr;
}
void pci_config_write32(struct pci_host *host, u8 bus, u8 dev, u8 func, u16 reg, u32 val)
{
volatile u32 *addr = (volatile u32 *)pcie_get_config_addr(host, bus, dev, func, reg);
*addr = val;
}
size_t pci_find_device(u16 minor, u16 major, list_t *out)
{
size_t n = 0;
list_item_t *item = pci_devices.head;
while (item) {
struct pci_device *device = item->ptr;
if (device->class_minor == minor && device->class_major == major) {
list_insert(out, device);
n += 1;
}
item = item->next;
}
return n;
}
static void host_probe_devices(struct pci_host *host)
{
for (int bus = 0; bus < 256; bus++) {
for (int dev = 1; dev < 32; dev++) {
for (int func = 0; func < 8; func++) {
u32 id = pci_config_read32(host, bus, dev, func, 0);
u16 dev_id = id >> 16;
u32 class_reg = pci_config_read32(host, bus, dev, func, 0x08);
u16 vendor = id & 0xFFFF;
u16 minor = (class_reg >> 24) & 0xFF;
u16 major = (class_reg >> 24) & 0xFF;
if (minor == 0 || major == 0)
continue;
if (vendor == 0xFFFF || vendor == 0x0000 || dev_id == 0xFFFF || dev_id == 0x0000)
continue;
struct pci_device *pdev = slab_alloc(pci_device_cache);
if (!pdev)
panic("failed to allocate pci_device\n");
pdev->host = host;
pdev->vendor = vendor;
pdev->device_id = (id >> 16) & 0xFFFF;
pdev->bus = bus;
pdev->slot = dev;
pdev->func = func;
pdev->class_major = (class_reg >> 24) & 0xFF;
pdev->class_minor = (class_reg >> 16) & 0xFF;
u32 bars[6];
for (int i = 0; i < 6; i++) {
bars[i] = pci_config_read32(host, bus, dev, func, 0x10 + (i * 4));
}
pdev->bar0 = bars[0] & (~0xF);
pdev->bar1 = bars[1] & (~0xF);
pdev->bar2 = bars[2] & (~0xF);
pdev->bar3 = bars[3] & (~0xF);
pdev->bar4 = bars[4] & (~0xF);
pdev->bar5 = bars[5] & (~0xF);
/* 64-bit BARs */
if ((bars[0] & 0x6) == 0x4)
pdev->bar0 |= ((u64)bars[1] << 32);
if ((bars[2] & 0x6) == 0x4)
pdev->bar2 |= ((u64)bars[3] << 32);
list_insert(&pci_devices, pdev);
if (func == 0) {
uint8_t header = (pci_config_read32(host, bus, dev, 0, 0x0C) >> 16) & 0xFF;
if (!(header & 0x80))
break;
}
kdebug("PCI device: vendor=%04x, id=%x, major=%x, minor=%x\n", pdev->vendor, pdev->device_id,
pdev->class_major, pdev->class_minor);
}
}
}
}
void pci_init()
{
pci_device_cache = slab_create("pci_device_cache", sizeof(struct pci_device), 4 * KB, 8);
if (!pci_device_cache)
panic("failed to allocate pci_device_cache\n");
for (int i = 0; i < n_hosts; i++) {
struct pci_host *host = hosts + i;
vaddr_t hostbase = (vaddr_t)map_mmio_dev(host->base, 256 * MB);
host->base = hostbase;
host_probe_devices(host);
}
}

View file

@ -1,32 +1,23 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <drivers/serial.h>
#include <kernel/device.h>
#include <kernel/common.h>
#include <kernel/slab.h>
#include <drivers/serial.h>
#include <drivers/tty.h>
static mem_cache_t *tty_slab;
static struct tty_serial_ops arch_serial_ops = { .serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write };
static struct tty_serial_ops arch_serial_ops = {.serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write};
struct tty *tty_create(const char *name, dev_t serial_device)
struct tty *tty_create(const char *name, void *priv)
{
struct tty *tty = slab_alloc(tty_slab);
dev_t tty_device = device_create(tty, DEV_CLASS_TTY, name);
tty->dev = tty_device;
tty->serial_device = serial_device;
tty->serial_device_private = device_private_info(serial_device);
switch (device_class(serial_device)) {
case DEV_CLASS_NATIVE_SERIAL:
tty->serial_ops = &arch_serial_ops;
break;
default:
kwarn("unknown serial device: %s", name);
break;
}
tty->priv = priv;
tty->serial_ops = &arch_serial_ops;
return tty;
}
@ -50,7 +41,7 @@ void tty_write(struct tty *tty, const char *s)
void tty_layer_init()
{
tty_slab = slab_create(sizeof(struct tty), 256 * KB, 8);
tty_slab = slab_create("tty_layer", sizeof(struct tty), 256 * KB, 8);
if (!tty_slab) {
panic("tty_slab alloc failed\n");