feat: refactored some pci code and added pretty print of classes

This commit is contained in:
2021-03-06 15:10:21 +01:00
parent 310f3621a2
commit 38b5b611fa
9 changed files with 459 additions and 15 deletions

View File

@@ -10,6 +10,12 @@
#include <libk/libk.h>
#include <libc/kprintf.h>
#ifdef ENABLE_PCIPP
#include <drivers/pci_devices.h>
#endif
#define PCI_CONFIG_ENABLE (1 << 31)
#define PCI_CONFIG_SHIFT_BUS_NUMBER 16
@@ -28,6 +34,17 @@
#define MASK_BAR_IOSPACE 0xFFFFFFFC
#define MASK_BAR_MEMSPACE 0xFFFFFFF0
const pci_driver pci_internal_secondary_bus = {
.name = "pci-secondary-bus",
.description = "A PCI bus connected to the primary bus",
.order = 0,
.pci_class = 0x06,
.pci_subclass = 0x04,
.pci_use_subclass = true,
.validate = NULL,
.initialize = NULL,
};
const pci_driver *pci_drivers[MAX_PCI_DRIVERS];
int last_pci_device_index = 0;
pci_device pci_devices[MAX_PCI_DEVICES];
@@ -114,6 +131,16 @@ uint8_t pci_get_header_type(uint8_t bus, uint8_t slot, uint8_t func) {
}
void pci_pick_driver(pci_device *device) {
// check special drivers
// PCI Secondary bus
if (device->class == pci_internal_secondary_bus.pci_class && device->subclass == pci_internal_secondary_bus.pci_subclass) {
uint8_t secondary_bus = pci_config_read_byte(device->bus, device->slot, device->func, PCI_CONFIG_SECONDARY_BUS_NUMBER);
device->pci_driver = &pci_internal_secondary_bus;
pci_check_bus(secondary_bus);
return;
}
// use normal drivers
for (int i = 0; i < MAX_PCI_DRIVERS; ++i) {
if (pci_drivers[i] == NULL) {
continue;
@@ -124,6 +151,9 @@ void pci_pick_driver(pci_device *device) {
if (pci_drivers[i]->pci_use_subclass && device->subclass != pci_drivers[i]->pci_subclass) {
continue;
}
if (!pci_drivers[i]->validatable) {
continue;
}
if (pci_drivers[i]->validate(device) != PCI_VALIDATE_OK) {
continue;
}
@@ -141,13 +171,7 @@ void pci_check_function(uint8_t bus, uint8_t slot, uint8_t func) {
pci_devices[last_pci_device_index].device_state.present = 1;
last_pci_device_index++;
if (pci_devices[last_pci_device_index-1].class == 0x06 && pci_devices[last_pci_device_index-1].subclass == 0x04) {
uint8_t secondary_bus = pci_config_read_byte(bus, slot, func, PCI_CONFIG_SECONDARY_BUS_NUMBER);
printf("Found secondary bus: %d\n", secondary_bus);
pci_check_bus(secondary_bus);
} else {
pci_pick_driver(&pci_devices[last_pci_device_index-1]);
}
pci_pick_driver(&pci_devices[last_pci_device_index - 1]);
// todo do something with the function
}
@@ -203,7 +227,9 @@ void pci_init_drivers() {
if (pci_devices[device_index].pci_driver == NULL) {
continue; // no driver found
}
pci_devices[device_index].pci_driver->initialize(&pci_devices[device_index]);
if (pci_devices[device_index].pci_driver->initialisable) {
pci_devices[device_index].pci_driver->initialize(&pci_devices[device_index]);
}
pci_devices[device_index].driver_state.initialized = 1;
}
}
@@ -243,6 +269,55 @@ void pci_dump_caps() {
}
}
#ifdef ENABLE_PCIPP
void pci_pretty_print() {
for (int i = 0; i < last_pci_device_index; ++i) {
char *class = NULL;
char *subclass = NULL;
char *function = NULL;
pci_device_info *class_info = NULL;
pci_device_info *subclass_info = NULL;
pci_device_info *function_info = NULL;
pci_device_info *current = pci_root_info;
while (current->name != NULL) {
if (current->code == pci_devices[i].class) {
class = current->name;
class_info = current;
break;
}
current = current + 1;
}
if (class_info != NULL && class_info->sub != NULL) {
current = class_info->sub;
while (current->name != NULL) {
if (current->code == pci_devices[i].subclass) {
subclass = current->name;
subclass_info = current;
break;
}
current = current + 1;
}
}
if (subclass_info != NULL && subclass_info->sub != NULL) {
current = subclass_info->sub;
while (current->name != NULL) {
if (current->code == pci_devices[i].programInterface) {
function = current->name;
function_info = current;
break;
}
current = current + 1;
}
}
printf("PCI BSF: %2x/%2x/%2x %s %s %s\n",
pci_devices[i].bus, pci_devices[i].slot, pci_devices[i].func,
class, subclass, function);
}
}
#endif
void pci_init_bar(pci_device *device, uint8_t bar_index) {
if (device->headerType != 0x00) {
k_panics("Only header 0x00 supported for now");