Skip to content

Commit 6ebc222

Browse files
lifeixacrnsi
authored andcommitted
hv: vPCI: cache PCI BAR physical base address
PCI BAR physical base address will never changed. Cache it to avoid calculating it every time when we access it. Tracked-On: #3475 Signed-off-by: Li, Fei1 <fei1.li@intel.com> Acked-by: Eddie Dong <eddie.dong@Intel.com>
1 parent 5083aba commit 6ebc222

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

hypervisor/dm/vpci/pci_pt.c

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,6 @@ static uint64_t get_vbar_base(const struct pci_vdev *vdev, uint32_t idx)
115115
return pci_bar_2_bar_base(&vdev->bar[0], vdev->nr_bars, idx);
116116
}
117117

118-
/**
119-
* @brief get pbar's full address in 64-bit
120-
* For 64-bit MMIO bar, its lower 32-bits base address and upper 32-bits base are combined
121-
* into one 64-bit base address
122-
* @pre pdev != NULL
123-
*/
124-
static uint64_t get_pbar_base(const struct pci_pdev *pdev, uint32_t idx)
125-
{
126-
return pci_bar_2_bar_base(&pdev->bar[0], pdev->nr_bars, idx);
127-
}
128-
129118
/**
130119
* @pre vdev != NULL
131120
*/
@@ -143,17 +132,12 @@ void vdev_pt_read_cfg(const struct pci_vdev *vdev, uint32_t offset, uint32_t byt
143132
* @pre vdev != NULL
144133
* @pre vdev->vpci != NULL
145134
* @pre vdev->vpci->vm != NULL
146-
* @pre vdev->pdev != NULL
147-
* @pre vdev->pdev->msix.table_bar < vdev->nr_bars
148135
*/
149136
static void vdev_pt_remap_msix_table_vbar(struct pci_vdev *vdev)
150137
{
151138
uint32_t i;
152139
struct pci_msix *msix = &vdev->msix;
153-
struct pci_pdev *pdev = vdev->pdev;
154-
struct pci_bar *pbar;
155-
156-
ASSERT(vdev->pdev->msix.table_bar < vdev->nr_bars, "msix->table_bar is out of range");
140+
struct pci_bar *vbar;
157141

158142
/* Mask all table entries */
159143
for (i = 0U; i < msix->table_count; i++) {
@@ -162,17 +146,17 @@ static void vdev_pt_remap_msix_table_vbar(struct pci_vdev *vdev)
162146
msix->table_entries[i].data = 0U;
163147
}
164148

165-
pbar = &pdev->bar[msix->table_bar];
166-
if (pbar != NULL) {
167-
uint64_t pbar_base = get_pbar_base(pdev, msix->table_bar); /* pbar (hpa) */
149+
vbar = &vdev->bar[msix->table_bar];
150+
if (vbar->size != 0UL) {
151+
uint64_t pbar_base = vbar->base_hpa; /* pbar (hpa) */
168152

169153
msix->mmio_hpa = pbar_base;
170154
if (is_prelaunched_vm(vdev->vpci->vm)) {
171155
msix->mmio_gpa = get_vbar_base(vdev, msix->table_bar);
172156
} else {
173157
msix->mmio_gpa = sos_vm_hpa2gpa(pbar_base);
174158
}
175-
msix->mmio_size = pbar->size;
159+
msix->mmio_size = vbar->size;
176160
}
177161

178162
/*
@@ -279,7 +263,7 @@ static void vdev_pt_remap_generic_mem_vbar(struct pci_vdev *vdev, uint32_t idx)
279263
/* If a new vbar is set (nonzero), set the EPT mapping accordingly */
280264
if (vbar_base != 0UL) {
281265
uint64_t hpa = gpa2hpa(vdev->vpci->vm, vbar_base);
282-
uint64_t pbar_base = get_pbar_base(vdev->pdev, idx); /* pbar (hpa) */
266+
uint64_t pbar_base = vbar->base_hpa; /* pbar (hpa) */
283267

284268
if (hpa != pbar_base) {
285269
/* Unmap the existing mapping for new vbar */
@@ -489,13 +473,14 @@ void init_vdev_pt(struct pci_vdev *vdev)
489473
vbar->size = 0UL;
490474
vbar->reg.value = pbar->reg.value;
491475
vbar->is_64bit_high = pbar->is_64bit_high;
476+
vbar->base_hpa = pbar->base_hpa;
492477

493478
if (pbar->is_64bit_high) {
494479
ASSERT(idx > 0U, "idx for upper 32-bit of the 64-bit bar should be greater than 0!");
495480

496481
if (is_sos_vm(vdev->vpci->vm)) {
497482
/* For SOS: vbar base (GPA) = pbar base (HPA) */
498-
vbar_base = get_pbar_base(vdev->pdev, idx);
483+
vbar_base = vdev->bar[idx - 1U].base_hpa;
499484
} else if (idx > 0U) {
500485
/* For pre-launched VMs: vbar base is predefined in vm_config */
501486
vbar_base = vdev->pci_dev_config->vbar_base[idx - 1U];
@@ -519,7 +504,7 @@ void init_vdev_pt(struct pci_vdev *vdev)
519504

520505
if (is_sos_vm(vdev->vpci->vm)) {
521506
/* For SOS: vbar base (GPA) = pbar base (HPA) */
522-
vbar_base = get_pbar_base(vdev->pdev, idx);
507+
vbar_base = vbar->base_hpa;
523508
} else {
524509
/* For pre-launched VMs: vbar base is predefined in vm_config */
525510
vbar_base = vdev->pci_dev_config->vbar_base[idx];
@@ -529,7 +514,7 @@ void init_vdev_pt(struct pci_vdev *vdev)
529514

530515
case PCIBAR_IO_SPACE:
531516
vbar->size = pbar->size;
532-
vdev_pt_write_vbar(vdev, pci_bar_offset(idx), (uint32_t)get_pbar_base(vdev->pdev, idx));
517+
vdev_pt_write_vbar(vdev, pci_bar_offset(idx), (uint32_t)vbar->base_hpa);
533518
break;
534519

535520
default:

hypervisor/dm/vpci/vpci.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,14 @@ static struct pci_vdev *find_vdev(const struct acrn_vpci *vpci, union pci_bdf bd
327327

328328
static void vpci_init_pt_dev(struct pci_vdev *vdev)
329329
{
330-
init_vmsi(vdev);
331-
init_vmsix(vdev);
332-
333330
/*
334-
* Here init_vdev_pt() needs to be called after init_vmsix() for the following reason:
335-
* init_vdev_pt() will indirectly call has_msix_cap(), which
336-
* requires init_vmsix() to be called first.
331+
* init_vdev_pt() must be called before init_vmsix() because init_vmsix
332+
* assigns BAR base hpa to MSI-X mmio_hpa which is initialized in init_vdev_pt().
337333
*/
338334
init_vdev_pt(vdev);
335+
init_vmsi(vdev);
336+
init_vmsix(vdev);
337+
339338
assign_vdev_pt_iommu_domain(vdev);
340339
}
341340

hypervisor/hw/pci.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ static uint32_t pci_pdev_read_bar(union pci_bdf bdf, uint32_t idx, struct pci_ba
288288
}
289289

290290
bar->size = size;
291+
bar->base_hpa = base;
291292

292293
return (type == PCIBAR_MEM64)?2U:1U;
293294
}

hypervisor/include/hw/pci.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ struct pci_bar {
185185
/* Base Address Register */
186186
union pci_bar_reg reg;
187187
uint64_t size;
188+
uint64_t base_hpa;
188189
bool is_64bit_high; /* true if this is the upper 32-bit of a 64-bit bar */
189190
};
190191

0 commit comments

Comments
 (0)