pci: pci_getenum, pci_getcapoffset and finding msi and msix on function enumeration

This commit is contained in:
Matheus
2023-07-15 18:02:17 -03:00
parent d202e152db
commit 6491d1bd52
3 changed files with 109 additions and 6 deletions
+23
View File
@@ -4,6 +4,8 @@
// 2 byte fields
#define PCI_CONFIG_VENDOR 0x0
#define PCI_CONFIG_DEVICEID 0x2
#define PCI_CONFIG_STATUS 0x6
#define PCI_STATUS_HASCAP 0x10
// 1 byte fields
#define PCI_CONFIG_REVISION 0x8
@@ -16,6 +18,16 @@
#define PCI_HEADERTYPE_MASK 0x7f
#define PCI_HEADERTYPE_STANDARD 0
#define PCI_CONFIG_CAP 0x34
#define PCI_CAP_MSI 0x5
#define PCI_CAP_MSIX 0x11
typedef struct {
bool exists;
int offset;
} pcicap_t;
typedef struct pcienum_t {
struct pcienum_t *next;
int bus;
@@ -28,14 +40,25 @@ typedef struct pcienum_t {
int vendor;
int deviceid;
int revision;
pcicap_t msi;
pcicap_t msix;
} pcienum_t;
typedef struct {
uintptr_t address;
size_t length;
bool mmio;
} pcibar_t;
uint8_t pci_read8(int bus, int device, int function, uint32_t offset);
uint16_t pci_read16(int bus, int device, int function, uint32_t offset);
uint32_t pci_read32(int bus, int device, int function, uint32_t offset);
void pci_write8(int bus, int device, int function, uint32_t offset, uint8_t v);
void pci_write16(int bus, int device, int function, uint32_t offset, uint16_t v);
void pci_write32(int bus, int device, int function, uint32_t offset, uint32_t value);
pcienum_t *pci_getenum(int class, int subclass, int progif, int vendor, int deviceid, int revision, int n);
int pci_getcapoffset(pcienum_t *e, int cap, int n);
pcibar_t pci_getbar(pcienum_t *e, int bar);
void pci_init();
#endif
+2 -2
View File
@@ -51,7 +51,7 @@ static uint32_t mcfg_read32(int bus, int device, int function, uint32_t offset)
return 0xffffffff;
uint64_t base = (uint64_t)MAKE_HHDM(entry->address);
uint32_t *address = (uint32_t *)(base + ((bus - entry->startbus) << 20) + (device << 15) + (function << 12));
uint32_t *address = (uint32_t *)(base | ((bus - entry->startbus) << 20) | (device << 15) | (function << 12) | (offset & ~0x3));
return *address;
}
@@ -60,7 +60,7 @@ static void mcfg_write32(int bus, int device, int function, uint32_t offset, uin
mcfgentry_t *entry = getmcfgentry(bus);
uint64_t base = (uint64_t)MAKE_HHDM(entry->address);
uint32_t *address = (uint32_t *)(base + ((bus - entry->startbus) << 20) + (device << 15) + (function << 12));
uint32_t *address = (uint32_t *)(base | ((bus - entry->startbus) << 20) | (device << 15) | (function << 12) | (offset & ~0x3));
*address = value;
}