From a7b5a775fa47949175cdeb5d45f97d98edb58c28 Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 22 Nov 2022 13:37:42 -0300 Subject: [PATCH] pci: helper functions and a fix in get_devicecs() --- src/arch/x86_64/include/arch/pci.h | 31 ++++++++++++++++++++++++++++++ src/arch/x86_64/pci.c | 2 ++ 2 files changed, 33 insertions(+) diff --git a/src/arch/x86_64/include/arch/pci.h b/src/arch/x86_64/include/arch/pci.h index 792d8dd9..d55960bd 100644 --- a/src/arch/x86_64/include/arch/pci.h +++ b/src/arch/x86_64/include/arch/pci.h @@ -50,6 +50,37 @@ typedef struct _pci_enumeration{ pci_common* header; } pci_enumeration; +#define PCI_TYPE_MEM 1 +#define PCI_MEM_64 2 + +// bit 0 set if mem; bit 1 set if 64 bit (in case of mem) + +static inline int pci_bartype(uint32_t bar){ + + if(bar & 1) + return 0; + + + if(((bar >> 1) & 3) == 2) + return 3; + + return 1; + +} + +static inline void* getbarmemaddr(pci_deviceheader* e, int bar){ + + int type = pci_bartype(e->BAR[bar]); + + if(type == 0) + return NULL; + + if(type & PCI_MEM_64) + return (e->BAR[bar] & ~(0xF)) + ((uint64_t)e->BAR[bar + 1] << 32); + else + return e->BAR[bar] & ~(0xF); +} + pci_enumeration* pci_getdevicecs(int class, int subclass, int n); pci_enumeration* pci_getdevicecsp(int class, int subclass, int progif, int n); uint64_t pci_msi_build(uint64_t* data, uint8_t vector, uint8_t processor, uint8_t edgetrigger, uint8_t deassert); diff --git a/src/arch/x86_64/pci.c b/src/arch/x86_64/pci.c index 81ccb20a..5788561a 100644 --- a/src/arch/x86_64/pci.c +++ b/src/arch/x86_64/pci.c @@ -20,6 +20,8 @@ pci_enumeration* pci_getdevicecs(int class, int subclass, int n){ pci_common* common = ptr->header; if((common->type & 0x7F) == PCI_DEV_HEADER && common->class == class && common->subclass == subclass){ save = ptr; + if(ptr->next == NULL) + break; ptr = ptr->next; break;