diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index d8219bb..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "files.associations": { - "*.h": "c", - "type_traits": "c", - "array": "c", - "string_view": "c", - "initializer_list": "c", - "utility": "c", - "*.tcc": "c" - } -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index d8219bb..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "files.associations": { - "*.h": "c", - "type_traits": "c", - "array": "c", - "string_view": "c", - "initializer_list": "c", - "utility": "c", - "*.tcc": "c" - } -} \ No newline at end of file diff --git a/src/include/list.h b/src/include/list.h index fe8c91f..5a6a0ff 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,6 +1,8 @@ #ifndef LIST_H #define LIST_H +#include + #define NULL (void *)0 typedef struct ListElement { @@ -10,6 +12,7 @@ extern void listAdd(ListElement **list, void *data); extern void listAddSet(ListElement **list, void *data); +extern uint32_t listCount(ListElement *list); extern void *popBeginning(ListElement **list); diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index d8219bb..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "files.associations": { - "*.h": "c", - "type_traits": "c", - "array": "c", - "string_view": "c", - "initializer_list": "c", - "utility": "c", - "*.tcc": "c" - } -} \ No newline at end of file diff --git a/src/include/list.h b/src/include/list.h index fe8c91f..5a6a0ff 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,6 +1,8 @@ #ifndef LIST_H #define LIST_H +#include + #define NULL (void *)0 typedef struct ListElement { @@ -10,6 +12,7 @@ extern void listAdd(ListElement **list, void *data); extern void listAddSet(ListElement **list, void *data); +extern uint32_t listCount(ListElement *list); extern void *popBeginning(ListElement **list); diff --git a/src/include/pci.h b/src/include/pci.h index 8377ed7..ec03a8e 100644 --- a/src/include/pci.h +++ b/src/include/pci.h @@ -1,6 +1,14 @@ #ifndef PCI_H #define PCI_H +#include + +typedef struct { + uint8_t bus, device, function; + uint8_t class, subclass; + uint16_t deviceId, vendorId; +} PciDevice; + extern void scanPCIDevices(); #endif diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index d8219bb..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "files.associations": { - "*.h": "c", - "type_traits": "c", - "array": "c", - "string_view": "c", - "initializer_list": "c", - "utility": "c", - "*.tcc": "c" - } -} \ No newline at end of file diff --git a/src/include/list.h b/src/include/list.h index fe8c91f..5a6a0ff 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,6 +1,8 @@ #ifndef LIST_H #define LIST_H +#include + #define NULL (void *)0 typedef struct ListElement { @@ -10,6 +12,7 @@ extern void listAdd(ListElement **list, void *data); extern void listAddSet(ListElement **list, void *data); +extern uint32_t listCount(ListElement *list); extern void *popBeginning(ListElement **list); diff --git a/src/include/pci.h b/src/include/pci.h index 8377ed7..ec03a8e 100644 --- a/src/include/pci.h +++ b/src/include/pci.h @@ -1,6 +1,14 @@ #ifndef PCI_H #define PCI_H +#include + +typedef struct { + uint8_t bus, device, function; + uint8_t class, subclass; + uint16_t deviceId, vendorId; +} PciDevice; + extern void scanPCIDevices(); #endif diff --git a/src/kernel/drivers/pci/pci.c b/src/kernel/drivers/pci/pci.c index eb49162..9874077 100644 --- a/src/kernel/drivers/pci/pci.c +++ b/src/kernel/drivers/pci/pci.c @@ -1,7 +1,8 @@ #include <_stdio.h> +#include #include #include -#include +#include #include #include @@ -28,6 +29,12 @@ "non-essential instrumentation", }; +bool checkedBuses[256]; + +uint32_t deviceCount = 0; + +ListElement *pciDevices = NULL; + uint8_t pciConfigReadByte(uint32_t bus, uint32_t device, uint32_t function, uint8_t offset) { uint32_t address = ((bus << 16) | (device << 11) | (function << 8) | @@ -54,11 +61,23 @@ return pciConfigReadWord(bus, device, function, 0); } +void checkBus(uint8_t); + void checkFunction(uint8_t bus, uint8_t device, uint8_t function) { uint8_t class = pciConfigReadByte(bus, device, function, 0xB); uint8_t subclass = pciConfigReadByte(bus, device, function, 0xA); - printf("function %i:%i:%i -> %i:%i, a %s\n", bus, device, function, class, - subclass, classNames[class]); + PciDevice *pciDevice = malloc(sizeof(PciDevice)); + pciDevice->bus = bus; + pciDevice->device = device; + pciDevice->function = function; + pciDevice->class = class; + pciDevice->subclass = subclass; + pciDevice->vendorId = pciConfigReadWord(bus, device, function, 0x00); + pciDevice->deviceId = pciConfigReadWord(bus, device, function, 0x02); + listAdd(&pciDevices, pciDevice); + if (class == 6 && subclass == 4) { + checkBus(pciConfigReadByte(bus, device, function, 0x19)); + } } void checkDevice(uint8_t bus, uint8_t device) { @@ -66,9 +85,9 @@ if (vendorID == 0xFFFF) { return; } - if (getHeaderType(bus, device, 0) & 0x08) { + if (getHeaderType(bus, device, 0) & 0x80) { // multifunction device - for (int function = 0; function < 8; function++) { + for (uint16_t function = 0; function < 256; function++) { if (getVendorID(bus, device, function) != 0xFFFF) { checkFunction(bus, device, function); } @@ -79,20 +98,22 @@ } void checkBus(uint8_t bus) { - for (uint8_t device = 0; device < 32; device++) { + if (checkedBuses[bus]) { + return; + } + checkedBuses[bus] = true; + for (uint16_t device = 0; device < 256; device++) { checkDevice(bus, device); } } void scanPCIDevices() { - printf("scanning pci devices...\n"); if (!(getHeaderType(0, 0, 0) & 0x80)) { - printf("discovored a single pci host controller\n"); checkBus(0); } else { for (uint8_t bus = 0; bus < 8; bus++) { checkBus(bus); } } - yields(); + printf("enumerated %i pci devices\n", listCount(pciDevices)); } diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index d8219bb..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "files.associations": { - "*.h": "c", - "type_traits": "c", - "array": "c", - "string_view": "c", - "initializer_list": "c", - "utility": "c", - "*.tcc": "c" - } -} \ No newline at end of file diff --git a/src/include/list.h b/src/include/list.h index fe8c91f..5a6a0ff 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,6 +1,8 @@ #ifndef LIST_H #define LIST_H +#include + #define NULL (void *)0 typedef struct ListElement { @@ -10,6 +12,7 @@ extern void listAdd(ListElement **list, void *data); extern void listAddSet(ListElement **list, void *data); +extern uint32_t listCount(ListElement *list); extern void *popBeginning(ListElement **list); diff --git a/src/include/pci.h b/src/include/pci.h index 8377ed7..ec03a8e 100644 --- a/src/include/pci.h +++ b/src/include/pci.h @@ -1,6 +1,14 @@ #ifndef PCI_H #define PCI_H +#include + +typedef struct { + uint8_t bus, device, function; + uint8_t class, subclass; + uint16_t deviceId, vendorId; +} PciDevice; + extern void scanPCIDevices(); #endif diff --git a/src/kernel/drivers/pci/pci.c b/src/kernel/drivers/pci/pci.c index eb49162..9874077 100644 --- a/src/kernel/drivers/pci/pci.c +++ b/src/kernel/drivers/pci/pci.c @@ -1,7 +1,8 @@ #include <_stdio.h> +#include #include #include -#include +#include #include #include @@ -28,6 +29,12 @@ "non-essential instrumentation", }; +bool checkedBuses[256]; + +uint32_t deviceCount = 0; + +ListElement *pciDevices = NULL; + uint8_t pciConfigReadByte(uint32_t bus, uint32_t device, uint32_t function, uint8_t offset) { uint32_t address = ((bus << 16) | (device << 11) | (function << 8) | @@ -54,11 +61,23 @@ return pciConfigReadWord(bus, device, function, 0); } +void checkBus(uint8_t); + void checkFunction(uint8_t bus, uint8_t device, uint8_t function) { uint8_t class = pciConfigReadByte(bus, device, function, 0xB); uint8_t subclass = pciConfigReadByte(bus, device, function, 0xA); - printf("function %i:%i:%i -> %i:%i, a %s\n", bus, device, function, class, - subclass, classNames[class]); + PciDevice *pciDevice = malloc(sizeof(PciDevice)); + pciDevice->bus = bus; + pciDevice->device = device; + pciDevice->function = function; + pciDevice->class = class; + pciDevice->subclass = subclass; + pciDevice->vendorId = pciConfigReadWord(bus, device, function, 0x00); + pciDevice->deviceId = pciConfigReadWord(bus, device, function, 0x02); + listAdd(&pciDevices, pciDevice); + if (class == 6 && subclass == 4) { + checkBus(pciConfigReadByte(bus, device, function, 0x19)); + } } void checkDevice(uint8_t bus, uint8_t device) { @@ -66,9 +85,9 @@ if (vendorID == 0xFFFF) { return; } - if (getHeaderType(bus, device, 0) & 0x08) { + if (getHeaderType(bus, device, 0) & 0x80) { // multifunction device - for (int function = 0; function < 8; function++) { + for (uint16_t function = 0; function < 256; function++) { if (getVendorID(bus, device, function) != 0xFFFF) { checkFunction(bus, device, function); } @@ -79,20 +98,22 @@ } void checkBus(uint8_t bus) { - for (uint8_t device = 0; device < 32; device++) { + if (checkedBuses[bus]) { + return; + } + checkedBuses[bus] = true; + for (uint16_t device = 0; device < 256; device++) { checkDevice(bus, device); } } void scanPCIDevices() { - printf("scanning pci devices...\n"); if (!(getHeaderType(0, 0, 0) & 0x80)) { - printf("discovored a single pci host controller\n"); checkBus(0); } else { for (uint8_t bus = 0; bus < 8; bus++) { checkBus(bus); } } - yields(); + printf("enumerated %i pci devices\n", listCount(pciDevices)); } diff --git a/src/kernel/util/list.c b/src/kernel/util/list.c index b14b60c..2c29877 100644 --- a/src/kernel/util/list.c +++ b/src/kernel/util/list.c @@ -35,6 +35,14 @@ element->data = data; } +uint32_t listCount(ListElement *list) { + int count = 0; + for (ListElement *current = list; current->next; current = current->next) { + count++; + } + return count; +} + void *popBeginning(ListElement **list) { if (*list == NULL) { return NULL;