From 5767d1e141cfd7af410a5f0113b7bc79fee858ac Mon Sep 17 00:00:00 2001 From: dongshen Date: Tue, 5 Mar 2019 17:31:57 -0800 Subject: [PATCH] HV: extract common code blocks to has_msi_cap and has_msix_cap functions Define has_msi_cap and has_msix_cap inline functions to do sanity checking for msi and msix ops, the corresponding code block in existing code is replaced with a call to these new functions. A few minor coding style fix. Tracked-On: #2534 Signed-off-by: dongshen Acked-by: Eddie Dong --- hypervisor/dm/vpci/msi.c | 26 ++++++++++++++------- hypervisor/dm/vpci/msix.c | 39 +++++++++++++++++++++++-------- hypervisor/dm/vpci/sharing_mode.c | 6 ++--- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/hypervisor/dm/vpci/msi.c b/hypervisor/dm/vpci/msi.c index 601680821..1a48dcb9c 100644 --- a/hypervisor/dm/vpci/msi.c +++ b/hypervisor/dm/vpci/msi.c @@ -35,13 +35,22 @@ #include "pci_priv.h" +/** + * @pre vdev != NULL + */ +static inline bool has_msi_cap(const struct pci_vdev *vdev) +{ + return (vdev->msi.capoff != 0U); +} + +/** + * @pre vdev != NULL + */ static inline bool msicap_access(const struct pci_vdev *vdev, uint32_t offset) { - bool ret; + bool ret = false; - if (vdev->msi.capoff == 0U) { - ret = 0; - } else { + if (has_msi_cap(vdev)) { ret = in_range(offset, vdev->msi.capoff, vdev->msi.caplen); } @@ -160,7 +169,7 @@ int32_t vmsi_cfgwrite(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, ui int32_t vmsi_deinit(struct pci_vdev *vdev) { - if (vdev->msi.capoff != 0U) { + if (has_msi_cap(vdev)) { ptirq_remove_msix_remapping(vdev->vpci->vm, vdev->vbdf.value, 1U); } @@ -194,11 +203,10 @@ int32_t vmsi_init(struct pci_vdev *vdev) struct pci_pdev *pdev = vdev->pdev; uint32_t val; - /* Copy MSI/MSI-X capability struct into virtual device */ - if (pdev->msi.capoff != 0U) { - vdev->msi.capoff = pdev->msi.capoff; - vdev->msi.caplen = pdev->msi.caplen; + vdev->msi.capoff = pdev->msi.capoff; + vdev->msi.caplen = pdev->msi.caplen; + if (has_msi_cap(vdev)) { (void)memcpy_s((void *)&vdev->cfgdata.data_8[pdev->msi.capoff], pdev->msi.caplen, (void *)&pdev->msi.cap[0U], pdev->msi.caplen); diff --git a/hypervisor/dm/vpci/msix.c b/hypervisor/dm/vpci/msix.c index fbfed05de..a67fddec3 100644 --- a/hypervisor/dm/vpci/msix.c +++ b/hypervisor/dm/vpci/msix.c @@ -38,13 +38,22 @@ #include #include "pci_priv.h" +/** + * @pre vdev != NULL + */ +static inline bool has_msix_cap(const struct pci_vdev *vdev) +{ + return (vdev->msix.capoff != 0U); +} + +/** + * @pre vdev != NULL + */ static inline bool msixcap_access(const struct pci_vdev *vdev, uint32_t offset) { - bool ret; + bool ret = false; - if (vdev->msix.capoff == 0U) { - ret = false; - } else { + if (has_msix_cap(vdev)) { ret = in_range(offset, vdev->msix.capoff, vdev->msix.caplen); } @@ -383,15 +392,18 @@ static int32_t vmsix_init_helper(struct pci_vdev *vdev) return ret; } +/** + * @pre vdev != NULL + */ int32_t vmsix_init(struct pci_vdev *vdev) { struct pci_pdev *pdev = vdev->pdev; int32_t ret = 0; - if (pdev->msix.capoff != 0U) { - vdev->msix.capoff = pdev->msix.capoff; - vdev->msix.caplen = pdev->msix.caplen; + vdev->msix.capoff = pdev->msix.capoff; + vdev->msix.caplen = pdev->msix.caplen; + if (has_msix_cap(vdev)) { (void)memcpy_s((void *)&vdev->cfgdata.data_8[pdev->msix.capoff], pdev->msix.caplen, (void *)&pdev->msix.cap[0U], pdev->msix.caplen); @@ -401,12 +413,19 @@ int32_t vmsix_init(struct pci_vdev *vdev) return ret; } +/** + * @pre vdev != NULL + * @pre vdev->vpci != NULL + * @pre vdev->vpci->vm != NULL + */ int32_t vmsix_deinit(struct pci_vdev *vdev) { - vdev->msix.intercepted_size = 0U; + if (has_msix_cap(vdev)) { + vdev->msix.intercepted_size = 0U; - if (vdev->msix.table_count != 0U) { - ptirq_remove_msix_remapping(vdev->vpci->vm, vdev->vbdf.value, vdev->msix.table_count); + if (vdev->msix.table_count != 0U) { + ptirq_remove_msix_remapping(vdev->vpci->vm, vdev->vbdf.value, vdev->msix.table_count); + } } return 0; diff --git a/hypervisor/dm/vpci/sharing_mode.c b/hypervisor/dm/vpci/sharing_mode.c index f3cc4afaa..4a67924e3 100644 --- a/hypervisor/dm/vpci/sharing_mode.c +++ b/hypervisor/dm/vpci/sharing_mode.c @@ -152,15 +152,13 @@ static int32_t sharing_mode_vpci_init(const struct acrn_vm *vm) { struct pci_vdev *vdev; uint32_t i, j; - int32_t ret; + int32_t ret = -ENODEV; /* * Only set up IO bitmap for SOS. * IO/MMIO requests from non-sos_vm guests will be injected to device model. */ - if (!is_sos_vm(vm)) { - ret = -ENODEV; - } else { + if (is_sos_vm(vm)) { /* Build up vdev array for sos_vm */ pci_pdev_foreach(init_vdev_for_pdev, vm);