diff --git a/hypervisor/hw/pci.c b/hypervisor/hw/pci.c index a04883fee..3d500aad5 100644 --- a/hypervisor/hw/pci.c +++ b/hypervisor/hw/pci.c @@ -49,8 +49,6 @@ static uint32_t num_pci_pdev; static struct pci_pdev pci_pdev_array[CONFIG_MAX_PCI_DEV_NUM]; static uint64_t pci_mmcfg_base = DEFAULT_PCI_MMCFG_BASE; -static void init_pdev(uint16_t pbdf, uint32_t drhd_index); - #ifdef CONFIG_ACPI_PARSE_ENABLED void set_mmcfg_base(uint64_t mmcfg_base) { @@ -607,11 +605,22 @@ static void pci_read_cap(struct pci_pdev *pdev) } } -static void init_pdev(uint16_t pbdf, uint32_t drhd_index) +/* + * @brief Initialize a pdev data structure. + * + * Initialize a pdev data structure with a physical device BDF(pbdf) and DRHD index(drhd_index). + * The caller of the function init_pdev should guarantee execution atomically. + * + * @param pbdf Physical device BDF + * @param drhd_index DRHD index + * + * @return If there's a successfully initialized pdev return it, otherwise return NULL; + */ +struct pci_pdev *init_pdev(uint16_t pbdf, uint32_t drhd_index) { uint8_t hdr_type; union pci_bdf bdf; - struct pci_pdev *pdev; + struct pci_pdev *pdev = NULL; if (num_pci_pdev < CONFIG_MAX_PCI_DEV_NUM) { bdf.value = pbdf; @@ -640,4 +649,6 @@ static void init_pdev(uint16_t pbdf, uint32_t drhd_index) } else { pr_err("%s, failed to alloc pci_pdev!\n", __func__); } + + return pdev; } diff --git a/hypervisor/include/hw/pci.h b/hypervisor/include/hw/pci.h index 6715aac4a..b67fdd62e 100644 --- a/hypervisor/include/hw/pci.h +++ b/hypervisor/include/hw/pci.h @@ -301,6 +301,7 @@ void set_mmcfg_base(uint64_t mmcfg_base); #endif uint64_t get_mmcfg_base(void); +struct pci_pdev *init_pdev(uint16_t pbdf, uint32_t drhd_index); uint32_t pci_pdev_read_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes); void pci_pdev_write_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes, uint32_t val); void enable_disable_pci_intx(union pci_bdf bdf, bool enable);