Skip to content

Commit

Permalink
HV: add uint32_t nr_bars to struct pci_vdev
Browse files Browse the repository at this point in the history
Use nr_bars instead of PCI_BAR_COUNT to check bar access offset.
As while normal pci device has max 6 bars, pci bridge only has 2 bars,
so for pci normal pci device, pci cfg offsets 0x10-0x24 are for bar access,
but for pci bridge, only 0x10-0x14 are for bar access (0x18-0x24 are
for other accesses).

Rename function:
 pci_bar_access --> is_bar_offset

Tracked-On: #3241
Signed-off-by: dongshen <dongsheng.x.zhang@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
  • Loading branch information
donsheng authored and wenlingz committed Jun 27, 2019
1 parent 28d454b commit f7c4d2b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
16 changes: 10 additions & 6 deletions hypervisor/dm/vpci/pci_pt.c
Expand Up @@ -47,8 +47,7 @@ int32_t vdev_pt_read_cfg(const struct pci_vdev *vdev, uint32_t offset, uint32_t
{
int32_t ret = -ENODEV;

/* PCI BARs is emulated */
if (is_prelaunched_vm(vdev->vpci->vm) && pci_bar_access(offset)) {
if (is_prelaunched_vm(vdev->vpci->vm) && is_bar_offset(vdev->nr_bars, offset)) {
*val = pci_vdev_read_cfg(vdev, offset, bytes);
ret = 0;
}
Expand All @@ -61,7 +60,7 @@ int32_t vdev_pt_read_cfg(const struct pci_vdev *vdev, uint32_t offset, uint32_t
* @pre vdev->vpci != NULL
* @pre vdev->vpci->vm != NULL
* @pre vdev->pdev != NULL
* @pre vdev->pdev->msix.table_bar < (PCI_BAR_COUNT - 1U)
* @pre vdev->pdev->msix.table_bar < vdev->nr_bars
*/
void vdev_pt_remap_msix_table_bar(struct pci_vdev *vdev)
{
Expand All @@ -71,7 +70,7 @@ void vdev_pt_remap_msix_table_bar(struct pci_vdev *vdev)
struct pci_pdev *pdev = vdev->pdev;
struct pci_bar *bar;

ASSERT(vdev->pdev->msix.table_bar < (PCI_BAR_COUNT - 1U), "msix->table_bar is out of range");
ASSERT(vdev->pdev->msix.table_bar < vdev->nr_bars, "msix->table_bar is out of range");

/* Mask all table entries */
for (i = 0U; i < msix->table_count; i++) {
Expand Down Expand Up @@ -246,7 +245,8 @@ int32_t vdev_pt_write_cfg(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes
int32_t ret = -ENODEV;

/* bar write access must be 4 bytes and offset must also be 4 bytes aligned */
if (is_prelaunched_vm(vdev->vpci->vm) && pci_bar_access(offset) && (bytes == 4U) && ((offset & 0x3U) == 0U)) {
if (is_prelaunched_vm(vdev->vpci->vm) && is_bar_offset(vdev->nr_bars, offset)
&& (bytes == 4U) && ((offset & 0x3U) == 0U)) {
vdev_pt_write_vbar(vdev, offset, val);
ret = 0;
}
Expand Down Expand Up @@ -294,8 +294,12 @@ void init_vdev_pt(struct pci_vdev *vdev)
struct pci_bar *pbar, *vbar;
uint16_t pci_command;

vdev->nr_bars = vdev->pdev->nr_bars;

ASSERT(vdev->nr_bars > 0U, "vdev->nr_bars should be greater than 0!");

if (is_prelaunched_vm(vdev->vpci->vm)) {
for (idx = 0U; idx < (uint32_t)PCI_BAR_COUNT; idx++) {
for (idx = 0U; idx < vdev->nr_bars; idx++) {
pbar = &vdev->pdev->bar[idx];
vbar = &vdev->bar[idx];

Expand Down
2 changes: 1 addition & 1 deletion hypervisor/dm/vpci/vhostbridge.c
Expand Up @@ -127,7 +127,7 @@ int32_t vhostbridge_write_cfg(struct pci_vdev *vdev, uint32_t offset,
int32_t ret = -ENODEV;

if (is_hostbridge(vdev) && is_prelaunched_vm(vdev->vpci->vm)) {
if (!pci_bar_access(offset)) {
if (!is_bar_offset(PCI_BAR_COUNT, offset)) {
pci_vdev_write_cfg(vdev, offset, bytes, val);
}

Expand Down
1 change: 1 addition & 0 deletions hypervisor/include/dm/vpci.h
Expand Up @@ -74,6 +74,7 @@ struct pci_vdev {
union pci_cfgdata cfgdata;

/* The bar info of the virtual PCI device. */
uint32_t nr_bars; /* 6 for normal device, 2 for bridge, 1 for cardbus */
struct pci_bar bar[PCI_BAR_COUNT];

struct pci_msi msi;
Expand Down
4 changes: 2 additions & 2 deletions hypervisor/include/hw/pci.h
Expand Up @@ -227,12 +227,12 @@ static inline uint32_t pci_bar_offset(uint32_t idx)
return PCIR_BARS + (idx << 2U);
}

static inline bool pci_bar_access(uint32_t offset)
static inline bool is_bar_offset(uint32_t nr_bars, uint32_t offset)
{
bool ret;

if ((offset >= pci_bar_offset(0U))
&& (offset < pci_bar_offset(PCI_BAR_COUNT))) {
&& (offset < pci_bar_offset(nr_bars))) {
ret = true;
} else {
ret = false;
Expand Down

0 comments on commit f7c4d2b

Please sign in to comment.