From f4adb109de87ab1ed88989b0ae60ee58145a4d54 Mon Sep 17 00:00:00 2001 From: dongshen Date: Thu, 24 Jan 2019 18:52:34 -0800 Subject: [PATCH] HV: store a pointer to pdev instead to save memory. As we store the physical pci device info in a global pdev array, the vdev struct can store a pointer to pdev instead. Tracked-On: #2431 Signed-off-by: dongshen Reviewed-by: Anthony Xu --- hypervisor/dm/vpci/msi.c | 4 +-- hypervisor/dm/vpci/msix.c | 14 ++++----- hypervisor/dm/vpci/partition_mode.c | 8 ++--- hypervisor/dm/vpci/pci_pt.c | 18 ++++++------ hypervisor/dm/vpci/sharing_mode.c | 15 +++++----- hypervisor/include/dm/pci.h | 2 +- hypervisor/include/dm/vpci.h | 5 +++- hypervisor/partition/apl-mrb/vm_description.c | 20 ++++--------- hypervisor/partition/dnv-cb2/vm_description.c | 29 +++++-------------- 9 files changed, 46 insertions(+), 69 deletions(-) diff --git a/hypervisor/dm/vpci/msi.c b/hypervisor/dm/vpci/msi.c index 934435c99..d16a65698 100644 --- a/hypervisor/dm/vpci/msi.c +++ b/hypervisor/dm/vpci/msi.c @@ -46,7 +46,7 @@ static inline bool msicap_access(const struct pci_vdev *vdev, uint32_t offset) static int32_t vmsi_remap(const struct pci_vdev *vdev, bool enable) { struct ptirq_msi_info info; - union pci_bdf pbdf = vdev->pdev.bdf; + union pci_bdf pbdf = vdev->pdev->bdf; struct acrn_vm *vm = vdev->vpci->vm; uint32_t capoff = vdev->msi.capoff; uint32_t msgctrl, msgdata; @@ -185,7 +185,7 @@ static void buf_write32(uint8_t buf[], uint32_t val) void populate_msi_struct(struct pci_vdev *vdev) { - struct pci_pdev *pdev = &vdev->pdev; + struct pci_pdev *pdev = vdev->pdev; uint32_t val; /* Copy MSI/MSI-X capability struct into virtual device */ diff --git a/hypervisor/dm/vpci/msix.c b/hypervisor/dm/vpci/msix.c index bd19777ec..ca1053221 100644 --- a/hypervisor/dm/vpci/msix.c +++ b/hypervisor/dm/vpci/msix.c @@ -92,7 +92,7 @@ static inline void enable_disable_msix(const struct pci_vdev *vdev, bool enable) } else { msgctrl &= ~PCIM_MSIXCTRL_MSIX_ENABLE; } - pci_pdev_write_cfg(vdev->pdev.bdf, vdev->msix.capoff + PCIR_MSIX_CTRL, 2U, msgctrl); + pci_pdev_write_cfg(vdev->pdev->bdf, vdev->msix.capoff + PCIR_MSIX_CTRL, 2U, msgctrl); } /* Do MSI-X remap for all MSI-X table entries in the target device */ @@ -114,7 +114,7 @@ static int32_t vmsix_remap(const struct pci_vdev *vdev, bool enable) /* If MSI Enable is being set, make sure INTxDIS bit is set */ if (ret == 0) { if (enable) { - enable_disable_pci_intx(vdev->pdev.bdf, false); + enable_disable_pci_intx(vdev->pdev->bdf, false); } enable_disable_msix(vdev, enable); } @@ -135,13 +135,13 @@ static int32_t vmsix_remap_one_entry(const struct pci_vdev *vdev, uint32_t index if (ret == 0) { /* If MSI Enable is being set, make sure INTxDIS bit is set */ if (enable) { - enable_disable_pci_intx(vdev->pdev.bdf, false); + enable_disable_pci_intx(vdev->pdev->bdf, false); } /* Restore MSI-X Enable bit */ msgctrl = pci_vdev_read_cfg(vdev, vdev->msix.capoff + PCIR_MSIX_CTRL, 2U); if ((msgctrl & PCIM_MSIXCTRL_MSIX_ENABLE) == PCIM_MSIXCTRL_MSIX_ENABLE) { - pci_pdev_write_cfg(vdev->pdev.bdf, vdev->msix.capoff + PCIR_MSIX_CTRL, 2U, msgctrl); + pci_pdev_write_cfg(vdev->pdev->bdf, vdev->msix.capoff + PCIR_MSIX_CTRL, 2U, msgctrl); } } @@ -186,7 +186,7 @@ static int32_t vmsix_cfgwrite(struct pci_vdev *vdev, uint32_t offset, uint32_t b } if (((msgctrl ^ val) & PCIM_MSIXCTRL_FUNCTION_MASK) != 0U) { - pci_pdev_write_cfg(vdev->pdev.bdf, offset, 2U, val); + pci_pdev_write_cfg(vdev->pdev->bdf, offset, 2U, val); } } ret = 0; @@ -316,7 +316,7 @@ static int32_t vmsix_init(struct pci_vdev *vdev) uint32_t i; uint64_t addr_hi, addr_lo; struct pci_msix *msix = &vdev->msix; - struct pci_pdev *pdev = &vdev->pdev; + struct pci_pdev *pdev = vdev->pdev; struct pci_bar *bar; int32_t ret; @@ -367,7 +367,7 @@ static int32_t vmsix_init(struct pci_vdev *vdev) } ret = 0; } else { - pr_err("%s, MSI-X device (%x) invalid table BIR %d", __func__, vdev->pdev.bdf.value, msix->table_bar); + pr_err("%s, MSI-X device (%x) invalid table BIR %d", __func__, vdev->pdev->bdf.value, msix->table_bar); vdev->msix.capoff = 0U; ret = -EIO; } diff --git a/hypervisor/dm/vpci/partition_mode.c b/hypervisor/dm/vpci/partition_mode.c index ec8a8d35b..2e5e1bd1e 100644 --- a/hypervisor/dm/vpci/partition_mode.c +++ b/hypervisor/dm/vpci/partition_mode.c @@ -72,13 +72,13 @@ static void partition_mode_pdev_init(struct pci_vdev *vdev) uint32_t idx; struct pci_bar *pbar, *vbar; - pdev_ref = find_pci_pdev(vdev->pdev.bdf); + pdev_ref = find_pci_pdev(vdev->pbdf); if (pdev_ref != NULL) { - (void)memcpy_s((void *)&vdev->pdev, sizeof(struct pci_pdev), (void *)pdev_ref, sizeof(struct pci_pdev)); + vdev->pdev = pdev_ref; /* Sanity checking for vbar */ for (idx = 0U; idx < (uint32_t)PCI_BAR_COUNT; idx++) { - pbar = &vdev->pdev.bar[idx]; + pbar = &vdev->pdev->bar[idx]; vbar = &vdev->bar[idx]; if (is_valid_bar(pbar)) { @@ -116,7 +116,7 @@ static int32_t partition_mode_vpci_init(const struct acrn_vm *vm) if (vdev->ops->init != NULL) { if (vdev->ops->init(vdev) != 0) { - pr_err("%s() failed at PCI device (bdf %x)!", __func__, + pr_err("%s() failed at PCI device (vbdf %x)!", __func__, vdev->vbdf); } } diff --git a/hypervisor/dm/vpci/pci_pt.c b/hypervisor/dm/vpci/pci_pt.c index 005cc0fb3..5b580d900 100644 --- a/hypervisor/dm/vpci/pci_pt.c +++ b/hypervisor/dm/vpci/pci_pt.c @@ -75,13 +75,13 @@ static int32_t vdev_pt_init(struct pci_vdev *vdev) hva2hpa(vm->arch_vm.nworld_eptp), 48U); } - ret = assign_iommu_device(vm->iommu, (uint8_t)vdev->pdev.bdf.bits.b, - (uint8_t)(vdev->pdev.bdf.value & 0xFFU)); + ret = assign_iommu_device(vm->iommu, (uint8_t)vdev->pdev->bdf.bits.b, + (uint8_t)(vdev->pdev->bdf.value & 0xFFU)); - pci_command = (uint16_t)pci_pdev_read_cfg(vdev->pdev.bdf, PCIR_COMMAND, 2U); + pci_command = (uint16_t)pci_pdev_read_cfg(vdev->pdev->bdf, PCIR_COMMAND, 2U); /* Disable INTX */ pci_command |= 0x400U; - pci_pdev_write_cfg(vdev->pdev.bdf, PCIR_COMMAND, 2U, pci_command); + pci_pdev_write_cfg(vdev->pdev->bdf, PCIR_COMMAND, 2U, pci_command); return ret; } @@ -91,8 +91,8 @@ static int32_t vdev_pt_deinit(struct pci_vdev *vdev) int32_t ret; struct acrn_vm *vm = vdev->vpci->vm; - ret = unassign_iommu_device(vm->iommu, (uint8_t)vdev->pdev.bdf.bits.b, - (uint8_t)(vdev->pdev.bdf.value & 0xFFU)); + ret = unassign_iommu_device(vm->iommu, (uint8_t)vdev->pdev->bdf.bits.b, + (uint8_t)(vdev->pdev->bdf.value & 0xFFU)); return ret; } @@ -110,7 +110,7 @@ static int32_t vdev_pt_cfgread(const struct pci_vdev *vdev, uint32_t offset, if (pci_bar_access(offset)) { *val = pci_vdev_read_cfg(vdev, offset, bytes); } else { - *val = pci_pdev_read_cfg(vdev->pdev.bdf, offset, bytes); + *val = pci_pdev_read_cfg(vdev->pdev->bdf, offset, bytes); } return 0; @@ -130,7 +130,7 @@ static void vdev_pt_remap_bar(struct pci_vdev *vdev, uint32_t idx, if (new_base != 0U) { /* Map the physical BAR in the guest MMIO space */ ept_mr_add(vm, (uint64_t *)vm->arch_vm.nworld_eptp, - vdev->pdev.bar[idx].base, /* HPA */ + vdev->pdev->bar[idx].base, /* HPA */ new_base, /*GPA*/ vdev->bar[idx].size, EPT_WR | EPT_RD | EPT_UNCACHED); @@ -189,7 +189,7 @@ static int32_t vdev_pt_cfgwrite(struct pci_vdev *vdev, uint32_t offset, vdev_pt_cfgwrite_bar(vdev, offset, bytes, val); } else { /* Write directly to physical device's config space */ - pci_pdev_write_cfg(vdev->pdev.bdf, offset, bytes, val); + pci_pdev_write_cfg(vdev->pdev->bdf, offset, bytes, val); } return 0; diff --git a/hypervisor/dm/vpci/sharing_mode.c b/hypervisor/dm/vpci/sharing_mode.c index 796311cc1..8a49bf6c0 100644 --- a/hypervisor/dm/vpci/sharing_mode.c +++ b/hypervisor/dm/vpci/sharing_mode.c @@ -40,7 +40,7 @@ struct pci_vdev *sharing_mode_find_vdev(union pci_bdf pbdf) /* in SOS_VM, it uses phys BDF */ for (i = 0U; i < num_pci_vdev; i++) { - if (sharing_mode_vdev_array[i].pdev.bdf.value == pbdf.value) { + if (sharing_mode_vdev_array[i].pdev->bdf.value == pbdf.value) { vdev = &sharing_mode_vdev_array[i]; } } @@ -71,7 +71,7 @@ static void sharing_mode_cfgread(__unused struct acrn_vpci *vpci, union pci_bdf /* Not handled by any handlers. Passthru to physical device */ if (!handled) { - *val = pci_pdev_read_cfg(vdev->pdev.bdf, offset, bytes); + *val = pci_pdev_read_cfg(vdev->pdev->bdf, offset, bytes); } } } @@ -96,13 +96,13 @@ static void sharing_mode_cfgwrite(__unused struct acrn_vpci *vpci, union pci_bdf /* Not handled by any handlers. Passthru to physical device */ if (!handled) { - pci_pdev_write_cfg(vdev->pdev.bdf, offset, bytes, val); + pci_pdev_write_cfg(vdev->pdev->bdf, offset, bytes, val); } } } } -static struct pci_vdev *alloc_pci_vdev(const struct acrn_vm *vm, const struct pci_pdev *pdev_ref) +static struct pci_vdev *alloc_pci_vdev(const struct acrn_vm *vm, struct pci_pdev *pdev_ref) { struct pci_vdev *vdev = NULL; @@ -114,15 +114,14 @@ static struct pci_vdev *alloc_pci_vdev(const struct acrn_vm *vm, const struct pc vdev->vpci = &vm->vpci; /* vbdf equals to pbdf otherwise remapped */ vdev->vbdf = pdev_ref->bdf; - (void)memcpy_s((void *)&vdev->pdev, sizeof(struct pci_pdev), - (const void *)pdev_ref, sizeof(struct pci_pdev)); + vdev->pdev = pdev_ref; } } return vdev; } -static void init_vdev_for_pdev(const struct pci_pdev *pdev, const void *cb_data) +static void init_vdev_for_pdev(struct pci_pdev *pdev, const void *cb_data) { const struct acrn_vm *vm = (const struct acrn_vm *)cb_data; struct pci_vdev *vdev; @@ -209,7 +208,7 @@ void vpci_set_ptdev_intr_info(const struct acrn_vm *target_vm, uint16_t vbdf, ui /* UOS may do BDF mapping */ vdev->vpci = (struct acrn_vpci *)&(target_vm->vpci); vdev->vbdf.value = vbdf; - vdev->pdev.bdf.value = pbdf; + vdev->pdev->bdf.value = pbdf; } } diff --git a/hypervisor/include/dm/pci.h b/hypervisor/include/dm/pci.h index 064e28aa6..cdbaea23f 100644 --- a/hypervisor/include/dm/pci.h +++ b/hypervisor/include/dm/pci.h @@ -179,7 +179,7 @@ struct pci_pdev { struct pci_msix_cap msix; }; -typedef void (*pci_pdev_enumeration_cb)(const struct pci_pdev *pdev, const void *data); +typedef void (*pci_pdev_enumeration_cb)(struct pci_pdev *pdev, const void *data); static inline uint32_t pci_bar_offset(uint32_t idx) { diff --git a/hypervisor/include/dm/vpci.h b/hypervisor/include/dm/vpci.h index aadd71b8b..036dcd596 100644 --- a/hypervisor/include/dm/vpci.h +++ b/hypervisor/include/dm/vpci.h @@ -91,7 +91,10 @@ struct pci_vdev { /* The bus/device/function triple of the virtual PCI device. */ union pci_bdf vbdf; - struct pci_pdev pdev; + /* The bus/device/function triple of the physical PCI device. */ + union pci_bdf pbdf; + + struct pci_pdev *pdev; union pci_cfgdata cfgdata; diff --git a/hypervisor/partition/apl-mrb/vm_description.c b/hypervisor/partition/apl-mrb/vm_description.c index 2c0a8bd7b..62802e9e7 100644 --- a/hypervisor/partition/apl-mrb/vm_description.c +++ b/hypervisor/partition/apl-mrb/vm_description.c @@ -13,16 +13,12 @@ static struct vpci_vdev_array vpci_vdev_array1 = { .vpci_vdev_list = { {/*vdev 0: hostbridge */ .vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, }, {/*vdev 1: SATA controller*/ .vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x00U, .d = 0x12U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x00U, .d = 0x12U, .f = 0x0U}, }, } }; @@ -33,23 +29,17 @@ static struct vpci_vdev_array vpci_vdev_array2 = { .vpci_vdev_list = { {/*vdev 0: hostbridge*/ .vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, }, {/*vdev 1: USB controller*/ .vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x00U, .d = 0x15U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x00U, .d = 0x15U, .f = 0x0U}, }, {/*vdev 2: Ethernet*/ .vbdf.bits = {.b = 0x00U, .d = 0x02U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x02U, .d = 0x00U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x02U, .d = 0x00U, .f = 0x0U}, }, } }; diff --git a/hypervisor/partition/dnv-cb2/vm_description.c b/hypervisor/partition/dnv-cb2/vm_description.c index 236ea0835..3b7879517 100644 --- a/hypervisor/partition/dnv-cb2/vm_description.c +++ b/hypervisor/partition/dnv-cb2/vm_description.c @@ -9,27 +9,21 @@ static struct vpci_vdev_array vpci_vdev_array1 = { .num_pci_vdev = 3, + .vpci_vdev_list = { {/*vdev 0: hostbridge */ .vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, }, {/*vdev 1: Ethernet*/ .vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x03U, .d = 0x00U, .f = 0x1U}, - } + .pbdf.bits = {.b = 0x03U, .d = 0x00U, .f = 0x1U}, }, {/*vdev 2: USB*/ .vbdf.bits = {.b = 0x00U, .d = 0x02U, .f = 0x0U}, - - .pdev = { - .bdf.bits = {.b = 0x00U, .d = 0x15U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x00U, .d = 0x15U, .f = 0x0U}, }, } }; @@ -40,27 +34,18 @@ static struct vpci_vdev_array vpci_vdev_array2 = { .vpci_vdev_list = { {/*vdev 0: hostbridge*/ .vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, }, {/*vdev 1: SATA controller*/ .vbdf.bits = {.b = 0x00U, .d = 0x05U, .f = 0x0U}, - .pdev = { - .bdf.bits = {.b = 0x00U, .d = 0x14U, .f = 0x0U}, - } + .pbdf.bits = {.b = 0x00U, .d = 0x14U, .f = 0x0U}, }, {/*vdev 2: Ethernet*/ .vbdf.bits = {.b = 0x00U, .d = 0x06U, .f = 0x0U}, - - .pdev = { - .bdf.bits = {.b = 0x03U, .d = 0x00U, .f = 0x0U}, - } - + .pbdf.bits = {.b = 0x03U, .d = 0x00U, .f = 0x0U}, }, - } };