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 <dongsheng.x.zhang@intel.com>
Reviewed-by: Anthony Xu <anthony.xu@intel.com>
This commit is contained in:
dongshen 2019-01-24 18:52:34 -08:00 committed by ACRN System Integration
parent 6ba07e64b9
commit f4adb109de
9 changed files with 46 additions and 69 deletions

View File

@ -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) static int32_t vmsi_remap(const struct pci_vdev *vdev, bool enable)
{ {
struct ptirq_msi_info info; 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; struct acrn_vm *vm = vdev->vpci->vm;
uint32_t capoff = vdev->msi.capoff; uint32_t capoff = vdev->msi.capoff;
uint32_t msgctrl, msgdata; 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) void populate_msi_struct(struct pci_vdev *vdev)
{ {
struct pci_pdev *pdev = &vdev->pdev; struct pci_pdev *pdev = vdev->pdev;
uint32_t val; uint32_t val;
/* Copy MSI/MSI-X capability struct into virtual device */ /* Copy MSI/MSI-X capability struct into virtual device */

View File

@ -92,7 +92,7 @@ static inline void enable_disable_msix(const struct pci_vdev *vdev, bool enable)
} else { } else {
msgctrl &= ~PCIM_MSIXCTRL_MSIX_ENABLE; 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 */ /* 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 MSI Enable is being set, make sure INTxDIS bit is set */
if (ret == 0) { if (ret == 0) {
if (enable) { if (enable) {
enable_disable_pci_intx(vdev->pdev.bdf, false); enable_disable_pci_intx(vdev->pdev->bdf, false);
} }
enable_disable_msix(vdev, enable); 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 (ret == 0) {
/* If MSI Enable is being set, make sure INTxDIS bit is set */ /* If MSI Enable is being set, make sure INTxDIS bit is set */
if (enable) { if (enable) {
enable_disable_pci_intx(vdev->pdev.bdf, false); enable_disable_pci_intx(vdev->pdev->bdf, false);
} }
/* Restore MSI-X Enable bit */ /* Restore MSI-X Enable bit */
msgctrl = pci_vdev_read_cfg(vdev, vdev->msix.capoff + PCIR_MSIX_CTRL, 2U); msgctrl = pci_vdev_read_cfg(vdev, vdev->msix.capoff + PCIR_MSIX_CTRL, 2U);
if ((msgctrl & PCIM_MSIXCTRL_MSIX_ENABLE) == PCIM_MSIXCTRL_MSIX_ENABLE) { 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) { 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; ret = 0;
@ -316,7 +316,7 @@ static int32_t vmsix_init(struct pci_vdev *vdev)
uint32_t i; uint32_t i;
uint64_t addr_hi, addr_lo; uint64_t addr_hi, addr_lo;
struct pci_msix *msix = &vdev->msix; struct pci_msix *msix = &vdev->msix;
struct pci_pdev *pdev = &vdev->pdev; struct pci_pdev *pdev = vdev->pdev;
struct pci_bar *bar; struct pci_bar *bar;
int32_t ret; int32_t ret;
@ -367,7 +367,7 @@ static int32_t vmsix_init(struct pci_vdev *vdev)
} }
ret = 0; ret = 0;
} else { } 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; vdev->msix.capoff = 0U;
ret = -EIO; ret = -EIO;
} }

View File

@ -72,13 +72,13 @@ static void partition_mode_pdev_init(struct pci_vdev *vdev)
uint32_t idx; uint32_t idx;
struct pci_bar *pbar, *vbar; struct pci_bar *pbar, *vbar;
pdev_ref = find_pci_pdev(vdev->pdev.bdf); pdev_ref = find_pci_pdev(vdev->pbdf);
if (pdev_ref != NULL) { 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 */ /* Sanity checking for vbar */
for (idx = 0U; idx < (uint32_t)PCI_BAR_COUNT; idx++) { for (idx = 0U; idx < (uint32_t)PCI_BAR_COUNT; idx++) {
pbar = &vdev->pdev.bar[idx]; pbar = &vdev->pdev->bar[idx];
vbar = &vdev->bar[idx]; vbar = &vdev->bar[idx];
if (is_valid_bar(pbar)) { 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 != NULL) {
if (vdev->ops->init(vdev) != 0) { 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); vdev->vbdf);
} }
} }

View File

@ -75,13 +75,13 @@ static int32_t vdev_pt_init(struct pci_vdev *vdev)
hva2hpa(vm->arch_vm.nworld_eptp), 48U); hva2hpa(vm->arch_vm.nworld_eptp), 48U);
} }
ret = assign_iommu_device(vm->iommu, (uint8_t)vdev->pdev.bdf.bits.b, ret = assign_iommu_device(vm->iommu, (uint8_t)vdev->pdev->bdf.bits.b,
(uint8_t)(vdev->pdev.bdf.value & 0xFFU)); (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 */ /* Disable INTX */
pci_command |= 0x400U; 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; return ret;
} }
@ -91,8 +91,8 @@ static int32_t vdev_pt_deinit(struct pci_vdev *vdev)
int32_t ret; int32_t ret;
struct acrn_vm *vm = vdev->vpci->vm; struct acrn_vm *vm = vdev->vpci->vm;
ret = unassign_iommu_device(vm->iommu, (uint8_t)vdev->pdev.bdf.bits.b, ret = unassign_iommu_device(vm->iommu, (uint8_t)vdev->pdev->bdf.bits.b,
(uint8_t)(vdev->pdev.bdf.value & 0xFFU)); (uint8_t)(vdev->pdev->bdf.value & 0xFFU));
return ret; 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)) { if (pci_bar_access(offset)) {
*val = pci_vdev_read_cfg(vdev, offset, bytes); *val = pci_vdev_read_cfg(vdev, offset, bytes);
} else { } else {
*val = pci_pdev_read_cfg(vdev->pdev.bdf, offset, bytes); *val = pci_pdev_read_cfg(vdev->pdev->bdf, offset, bytes);
} }
return 0; return 0;
@ -130,7 +130,7 @@ static void vdev_pt_remap_bar(struct pci_vdev *vdev, uint32_t idx,
if (new_base != 0U) { if (new_base != 0U) {
/* Map the physical BAR in the guest MMIO space */ /* Map the physical BAR in the guest MMIO space */
ept_mr_add(vm, (uint64_t *)vm->arch_vm.nworld_eptp, 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*/ new_base, /*GPA*/
vdev->bar[idx].size, vdev->bar[idx].size,
EPT_WR | EPT_RD | EPT_UNCACHED); 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); vdev_pt_cfgwrite_bar(vdev, offset, bytes, val);
} else { } else {
/* Write directly to physical device's config space */ /* 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; return 0;

View File

@ -40,7 +40,7 @@ struct pci_vdev *sharing_mode_find_vdev(union pci_bdf pbdf)
/* in SOS_VM, it uses phys BDF */ /* in SOS_VM, it uses phys BDF */
for (i = 0U; i < num_pci_vdev; i++) { 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]; 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 */ /* Not handled by any handlers. Passthru to physical device */
if (!handled) { 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 */ /* Not handled by any handlers. Passthru to physical device */
if (!handled) { 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; 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; vdev->vpci = &vm->vpci;
/* vbdf equals to pbdf otherwise remapped */ /* vbdf equals to pbdf otherwise remapped */
vdev->vbdf = pdev_ref->bdf; vdev->vbdf = pdev_ref->bdf;
(void)memcpy_s((void *)&vdev->pdev, sizeof(struct pci_pdev), vdev->pdev = pdev_ref;
(const void *)pdev_ref, sizeof(struct pci_pdev));
} }
} }
return vdev; 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; const struct acrn_vm *vm = (const struct acrn_vm *)cb_data;
struct pci_vdev *vdev; 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 */ /* UOS may do BDF mapping */
vdev->vpci = (struct acrn_vpci *)&(target_vm->vpci); vdev->vpci = (struct acrn_vpci *)&(target_vm->vpci);
vdev->vbdf.value = vbdf; vdev->vbdf.value = vbdf;
vdev->pdev.bdf.value = pbdf; vdev->pdev->bdf.value = pbdf;
} }
} }

View File

@ -179,7 +179,7 @@ struct pci_pdev {
struct pci_msix_cap msix; 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) static inline uint32_t pci_bar_offset(uint32_t idx)
{ {

View File

@ -91,7 +91,10 @@ struct pci_vdev {
/* The bus/device/function triple of the virtual PCI device. */ /* The bus/device/function triple of the virtual PCI device. */
union pci_bdf vbdf; 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; union pci_cfgdata cfgdata;

View File

@ -13,16 +13,12 @@ static struct vpci_vdev_array vpci_vdev_array1 = {
.vpci_vdev_list = { .vpci_vdev_list = {
{/*vdev 0: hostbridge */ {/*vdev 0: hostbridge */
.vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
.bdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
}
}, },
{/*vdev 1: SATA controller*/ {/*vdev 1: SATA controller*/
.vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x00U, .d = 0x12U, .f = 0x0U},
.bdf.bits = {.b = 0x00U, .d = 0x12U, .f = 0x0U},
}
}, },
} }
}; };
@ -33,23 +29,17 @@ static struct vpci_vdev_array vpci_vdev_array2 = {
.vpci_vdev_list = { .vpci_vdev_list = {
{/*vdev 0: hostbridge*/ {/*vdev 0: hostbridge*/
.vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
.bdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
}
}, },
{/*vdev 1: USB controller*/ {/*vdev 1: USB controller*/
.vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x00U, .d = 0x15U, .f = 0x0U},
.bdf.bits = {.b = 0x00U, .d = 0x15U, .f = 0x0U},
}
}, },
{/*vdev 2: Ethernet*/ {/*vdev 2: Ethernet*/
.vbdf.bits = {.b = 0x00U, .d = 0x02U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x02U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x02U, .d = 0x00U, .f = 0x0U},
.bdf.bits = {.b = 0x02U, .d = 0x00U, .f = 0x0U},
}
}, },
} }
}; };

View File

@ -9,27 +9,21 @@
static struct vpci_vdev_array vpci_vdev_array1 = { static struct vpci_vdev_array vpci_vdev_array1 = {
.num_pci_vdev = 3, .num_pci_vdev = 3,
.vpci_vdev_list = { .vpci_vdev_list = {
{/*vdev 0: hostbridge */ {/*vdev 0: hostbridge */
.vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
.bdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
}
}, },
{/*vdev 1: Ethernet*/ {/*vdev 1: Ethernet*/
.vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x01U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x03U, .d = 0x00U, .f = 0x1U},
.bdf.bits = {.b = 0x03U, .d = 0x00U, .f = 0x1U},
}
}, },
{/*vdev 2: USB*/ {/*vdev 2: USB*/
.vbdf.bits = {.b = 0x00U, .d = 0x02U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x02U, .f = 0x0U},
.pbdf.bits = {.b = 0x00U, .d = 0x15U, .f = 0x0U},
.pdev = {
.bdf.bits = {.b = 0x00U, .d = 0x15U, .f = 0x0U},
}
}, },
} }
}; };
@ -40,27 +34,18 @@ static struct vpci_vdev_array vpci_vdev_array2 = {
.vpci_vdev_list = { .vpci_vdev_list = {
{/*vdev 0: hostbridge*/ {/*vdev 0: hostbridge*/
.vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
.bdf.bits = {.b = 0x00U, .d = 0x00U, .f = 0x0U},
}
}, },
{/*vdev 1: SATA controller*/ {/*vdev 1: SATA controller*/
.vbdf.bits = {.b = 0x00U, .d = 0x05U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x05U, .f = 0x0U},
.pdev = { .pbdf.bits = {.b = 0x00U, .d = 0x14U, .f = 0x0U},
.bdf.bits = {.b = 0x00U, .d = 0x14U, .f = 0x0U},
}
}, },
{/*vdev 2: Ethernet*/ {/*vdev 2: Ethernet*/
.vbdf.bits = {.b = 0x00U, .d = 0x06U, .f = 0x0U}, .vbdf.bits = {.b = 0x00U, .d = 0x06U, .f = 0x0U},
.pbdf.bits = {.b = 0x03U, .d = 0x00U, .f = 0x0U},
.pdev = {
.bdf.bits = {.b = 0x03U, .d = 0x00U, .f = 0x0U},
}
}, },
} }
}; };