summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLizzy Fleckenstein <lizzy@vlhl.dev>2023-12-27 00:32:18 +0100
committerLizzy Fleckenstein <lizzy@vlhl.dev>2023-12-27 00:35:45 +0100
commitbe6222437bef9955c9dc3b136e56ac70f080bf47 (patch)
tree4514e93a90770c8f916eb306e072f2fc1ad2d4b1
parent241857a79e1a42e23ec29389862f39c92a5dc93e (diff)
downloadcuddles-be6222437bef9955c9dc3b136e56ac70f080bf47.tar.xz
store pci enumeration results
-rw-r--r--stage3/main.c1
-rw-r--r--stage3/pci.c31
-rw-r--r--stage3/pci.h16
-rw-r--r--stage3/shell.c11
4 files changed, 48 insertions, 11 deletions
diff --git a/stage3/main.c b/stage3/main.c
index eec822a..ae94abf 100644
--- a/stage3/main.c
+++ b/stage3/main.c
@@ -133,6 +133,7 @@ void kmain()
ata_init();
ps2_init();
debug_init();
+ pci_init();
shell_run_cmd(S("run init"));
diff --git a/stage3/pci.c b/stage3/pci.c
index d764171..5cf5165 100644
--- a/stage3/pci.c
+++ b/stage3/pci.c
@@ -2,11 +2,15 @@
#include "def.h"
#include "io.h"
#include "font.h"
+#include "heap.h"
#define PCI_CONFIG_ADDRESS 0xCF8
#define PCI_CONFIG_DATA 0xCFC
-void pci_enumerate()
+usize pci_num_devices = 0;
+pci_dev *pci_devices = nil;
+
+void pci_init()
{
typedef struct __attribute__((packed)) {
u8 offset;
@@ -28,13 +32,22 @@ void pci_enumerate()
};
outl(PCI_CONFIG_ADDRESS, BITCAST(addr, pci_config_addr, u32));
- u32 x = inl(PCI_CONFIG_DATA);
- if (x != 0xFFFFFFFF) {
- print(S("bus: ")); print_num_pad(bus, 16, 2, ' ');
- print(S(" dev: ")); print_num_pad(dev, 16, 1, ' ');
- print(S(" vendor: ")); print_num_pad(x & 0xFFFF, 16, 4, ' ');
- print(S(" id: ")); print_num_pad((x >> 16) & 0xFFFF, 16, 4, ' ');
- print(S("\n"));
- }
+ u32 reg_0 = inl(PCI_CONFIG_DATA);
+ if (reg_0 == 0xFFFFFFFF)
+ continue;
+
+ addr.offset = 0x8;
+ outl(PCI_CONFIG_ADDRESS, BITCAST(addr, pci_config_addr, u32));
+ u32 reg_2 = inl(PCI_CONFIG_DATA);
+
+ pci_devices = realloc(pci_devices, ++pci_num_devices * sizeof *pci_devices);
+ pci_devices[pci_num_devices-1] = (pci_dev) {
+ .bus = addr.bus,
+ .dev = addr.dev,
+ .vendor = reg_0 & 0xFFFF,
+ .id = (reg_0 >> 16) & 0xFFFF,
+ .class = reg_2 >> 24,
+ .subclass = (reg_2 >> 16) & 0xff,
+ };
}
}
diff --git a/stage3/pci.h b/stage3/pci.h
index ac893ee..2d2c909 100644
--- a/stage3/pci.h
+++ b/stage3/pci.h
@@ -1,6 +1,20 @@
#ifndef PCI_H
#define PCI_H
-void pci_enumerate();
+#include "def.h"
+
+typedef struct {
+ u8 bus;
+ unsigned int dev : 5;
+ u16 vendor;
+ u16 id;
+ u8 class;
+ u8 subclass;
+} pci_dev;
+
+extern usize pci_num_devices;
+extern pci_dev *pci_devices;
+
+void pci_init();
#endif
diff --git a/stage3/shell.c b/stage3/shell.c
index 28de4f9..c2fd377 100644
--- a/stage3/shell.c
+++ b/stage3/shell.c
@@ -93,7 +93,16 @@ static void cmd_img(str arg)
static void cmd_lspci(str arg)
{
(void) arg;
- pci_enumerate();
+ for (usize i = 0; i < pci_num_devices; i++) {
+ pci_dev *dev = &pci_devices[i];
+ print(S("bus: ")); print_num_pad(dev->bus, 16, 2, ' ');
+ print(S(" dev: ")); print_num_pad(dev->dev, 16, 1, ' ');
+ print(S(" vendor: ")); print_num_pad(dev->vendor, 16, 4, ' ');
+ print(S(" id: ")); print_num_pad(dev->id, 16, 4, ' ');
+ print(S(" class: ")); print_num_pad(dev->class, 16, 2, ' ');
+ print(S(" subclass: ")); print_num_pad(dev->subclass, 16, 2, ' ');
+ print(S("\n"));
+ }
}
static void cmd_run(str arg)