diff --git a/hypervisor/dm/vpci/partition_mode.c b/hypervisor/dm/vpci/partition_mode.c index 2ecc0f828..c1e76f5bc 100644 --- a/hypervisor/dm/vpci/partition_mode.c +++ b/hypervisor/dm/vpci/partition_mode.c @@ -50,6 +50,49 @@ static struct pci_vdev *partition_mode_find_vdev(struct acrn_vpci *vpci, union p return NULL; } +static inline bool is_valid_bar_type(const struct pci_bar *bar) +{ + return (bar->type == PCIBAR_MEM32) || (bar->type == PCIBAR_MEM64); +} + +static inline bool is_valid_bar_size(const struct pci_bar *bar) +{ + return (bar->size > 0UL) && (bar->size <= 0xffffffffU); +} + +/* Only MMIO is supported and bar size cannot be greater than 4GB */ +static inline bool is_valid_bar(const struct pci_bar *bar) +{ + return (is_valid_bar_type(bar) && is_valid_bar_size(bar)); +} + +static void partition_mode_pdev_init(struct pci_vdev *vdev) +{ + struct pci_pdev *pdev_ref; + uint32_t idx; + struct pci_bar *pbar, *vbar; + + pdev_ref = find_pci_pdev(vdev->pdev.bdf); + if (pdev_ref != NULL) { + (void)memcpy_s((void *)&vdev->pdev, sizeof(struct pci_pdev), (void *)pdev_ref, sizeof(struct pci_pdev)); + + /* Sanity checking for vbar */ + for (idx = 0U; idx < (uint32_t)PCI_BAR_COUNT; idx++) { + pbar = &vdev->pdev.bar[idx]; + vbar = &vdev->bar[idx]; + + if (is_valid_bar(pbar)) { + vbar->size = (pbar->size < 0x1000U) ? 0x1000U : pbar->size; + vbar->type = PCIBAR_MEM32; + } else { + /* Mark this vbar as invalid */ + vbar->size = 0UL; + vbar->type = PCIBAR_NONE; + } + } + } +} + static int32_t partition_mode_vpci_init(const struct acrn_vm *vm) { struct vpci_vdev_array *vdev_array; @@ -64,6 +107,11 @@ static int32_t partition_mode_vpci_init(const struct acrn_vm *vm) vdev = &vdev_array->vpci_vdev_list[i]; vdev->vpci = vpci; + /* Exclude hostbridge */ + if (vdev->vbdf.value != 0U) { + partition_mode_pdev_init(vdev); + } + if ((vdev->ops != NULL) && (vdev->ops->init != NULL)) { if (vdev->ops->init(vdev) != 0) { pr_err("%s() failed at PCI device (bdf %x)!", __func__, @@ -98,6 +146,7 @@ static void partition_mode_cfgread(struct acrn_vpci *vpci, union pci_bdf vbdf, uint32_t offset, uint32_t bytes, uint32_t *val) { struct pci_vdev *vdev = partition_mode_find_vdev(vpci, vbdf); + if ((vdev != NULL) && (vdev->ops != NULL) && (vdev->ops->cfgread != NULL)) { (void)vdev->ops->cfgread(vdev, offset, bytes, val); @@ -108,6 +157,7 @@ static void partition_mode_cfgwrite(struct acrn_vpci *vpci, union pci_bdf vbdf, uint32_t offset, uint32_t bytes, uint32_t val) { struct pci_vdev *vdev = partition_mode_find_vdev(vpci, vbdf); + if ((vdev != NULL) && (vdev->ops != NULL) && (vdev->ops->cfgwrite != NULL)) { (void)vdev->ops->cfgwrite(vdev, offset, bytes, val); diff --git a/hypervisor/hw/pci.c b/hypervisor/hw/pci.c index 25589934e..cacad3f16 100644 --- a/hypervisor/hw/pci.c +++ b/hypervisor/hw/pci.c @@ -401,3 +401,17 @@ void pci_pdev_foreach(pci_pdev_enumeration_cb cb_func, const void *ctx) } } +struct pci_pdev *find_pci_pdev(union pci_bdf pbdf) +{ + struct pci_pdev *pdev = NULL; + uint32_t i; + + for (i = 0U; i < num_pci_pdev; i++) { + if (pci_pdev_array[i].bdf.value == pbdf.value) { + pdev = &pci_pdev_array[i]; + break; + } + } + + return pdev; +} diff --git a/hypervisor/include/dm/pci.h b/hypervisor/include/dm/pci.h index 233e04899..064e28aa6 100644 --- a/hypervisor/include/dm/pci.h +++ b/hypervisor/include/dm/pci.h @@ -225,6 +225,7 @@ void pci_pdev_write_cfg(union pci_bdf bdf, uint32_t offset, uint32_t bytes, uint void enable_disable_pci_intx(union pci_bdf bdf, bool enable); void pci_pdev_foreach(pci_pdev_enumeration_cb cb, const void *ctx); +struct pci_pdev *find_pci_pdev(union pci_bdf pbdf); void init_pci_pdev_list(void);