187 lines
4.6 KiB
C
187 lines
4.6 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/common.h>
|
|
#include <kernel/slab.h>
|
|
#include <kernel/page.h>
|
|
#include <kernel/driver.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 major, u16 minor, list_t *out)
|
|
{
|
|
size_t n = 0;
|
|
|
|
list_lock(out);
|
|
|
|
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;
|
|
}
|
|
|
|
list_unlock(out);
|
|
|
|
return n;
|
|
}
|
|
|
|
static void device_init(struct pci_device *pdev)
|
|
{
|
|
u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if);
|
|
struct driver *driver = driver_find(DRIVER_PCI, driverclass);
|
|
|
|
if (driver && driver->init_device) {
|
|
pci_init_t f = driver->init_device;
|
|
f(pdev);
|
|
}
|
|
}
|
|
|
|
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;
|
|
pdev->prog_if = (class_reg >> 8) & 0xFF;
|
|
pdev->revision = (class_reg) & 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_lock(&pci_devices);
|
|
list_insert(&pci_devices, pdev);
|
|
list_unlock(&pci_devices);
|
|
if (func == 0) {
|
|
uint8_t header = (pci_config_read32(host, bus, dev, 0, 0x0C) >> 16) & 0xFF;
|
|
if (!(header & 0x80))
|
|
break;
|
|
}
|
|
|
|
device_init(pdev);
|
|
|
|
kdebug("PCI device: vendor=%04x, id=%x, major=%x, minor=%x\n", pdev->vendor, pdev->device_id,
|
|
pdev->class_major, pdev->class_minor);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
u32 pci_driver_class(u8 major, u8 minor, u8 prog_if)
|
|
{
|
|
u32 ret = major;
|
|
ret <<= 8;
|
|
ret |= minor;
|
|
ret <<= 8;
|
|
ret |= prog_if;
|
|
|
|
return ret;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
krdy("PCI ready\n");
|
|
}
|
|
DRIVER_ENTRY(pci_init);
|