Skip to content

Commit 19c5342

Browse files
donshengwenlingz
authored andcommitted
HV: remove vdev ops for sharing mode
Remove vdev ops for sharing mode, directly call the corresponding functions instead of calling the ops callbacks (indirectly) Remove alloc_pci_vdev() and merge its code into init_vdev_for_pdev() to simplify code Remove @pre for local variables Change the return value from int32_t to void to comply with misra c and add ASSERT in the functions (if necessary) to verify the assumptions for debug build: vmsi_init vmsix_init vmsi_deinit vmsix_deinit Add @pre for vmsix_init_helper and make it a void function, use ASSERT to verify the assumption for debug build. Add ASSERT in get_sos_vm Tracked-On: #2534 Signed-off-by: dongshen <dongsheng.x.zhang@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent eb4f469 commit 19c5342

File tree

6 files changed

+79
-175
lines changed

6 files changed

+79
-175
lines changed

hypervisor/arch/x86/guest/vm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ struct acrn_vm *get_vm_from_vmid(uint16_t vm_id)
110110
/* return a pointer to the virtual machine structure of SOS VM */
111111
struct acrn_vm *get_sos_vm(void)
112112
{
113+
ASSERT(sos_vm_ptr != NULL, "sos_vm_ptr is NULL");
114+
113115
return sos_vm_ptr;
114116
}
115117

hypervisor/dm/vpci/msi.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,13 @@ int32_t vmsi_cfgwrite(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, ui
167167
return ret;
168168
}
169169

170-
int32_t vmsi_deinit(const struct pci_vdev *vdev)
170+
void vmsi_deinit(const struct pci_vdev *vdev)
171171
{
172172
if (has_msi_cap(vdev)) {
173173
ptirq_remove_msix_remapping(vdev->vpci->vm, vdev->vbdf.value, 1U);
174174
}
175-
176-
return 0;
177175
}
178176

179-
const struct pci_vdev_ops pci_ops_vdev_msi = {
180-
.init = vmsi_init,
181-
.deinit = vmsi_deinit,
182-
.cfgwrite = vmsi_cfgwrite,
183-
.cfgread = vmsi_cfgread,
184-
};
185-
186177
/* Read a uint32_t from buffer (little endian) */
187178
static uint32_t buf_read32(const uint8_t buf[])
188179
{
@@ -198,7 +189,7 @@ static void buf_write32(uint8_t buf[], uint32_t val)
198189
buf[3] = (uint8_t)((val >> 24U) & 0xFFU);
199190
}
200191

201-
int32_t vmsi_init(struct pci_vdev *vdev)
192+
void vmsi_init(struct pci_vdev *vdev)
202193
{
203194
struct pci_pdev *pdev = vdev->pdev;
204195
uint32_t val;
@@ -216,7 +207,5 @@ int32_t vmsi_init(struct pci_vdev *vdev)
216207

217208
buf_write32(&vdev->cfgdata.data_8[pdev->msi.capoff], val);
218209
}
219-
220-
return 0;
221210
}
222211

hypervisor/dm/vpci/msix.c

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -328,74 +328,71 @@ static int32_t vmsix_table_mmio_access_handler(struct io_request *io_req, void *
328328
return ret;
329329
}
330330

331-
static int32_t vmsix_init_helper(struct pci_vdev *vdev)
331+
332+
/**
333+
* @pre vdev != NULL
334+
* @pre vdev->pdev != NULL
335+
* @pre vdev->pdev->msix.table_bar < (PCI_BAR_COUNT - 1U)
336+
*/
337+
static void vmsix_init_helper(struct pci_vdev *vdev)
332338
{
333339
uint32_t i;
334340
uint64_t addr_hi, addr_lo;
335341
struct pci_msix *msix = &vdev->msix;
336342
struct pci_pdev *pdev = vdev->pdev;
337343
struct pci_bar *bar;
338-
int32_t ret;
344+
345+
ASSERT(vdev->pdev->msix.table_bar < (PCI_BAR_COUNT - 1U), "msix->table_bar out of range");
339346

340347
msix->table_bar = pdev->msix.table_bar;
341348
msix->table_offset = pdev->msix.table_offset;
342349
msix->table_count = pdev->msix.table_count;
343350

344-
if (msix->table_bar < (PCI_BAR_COUNT - 1U)) {
345-
/* Mask all table entries */
346-
for (i = 0U; i < msix->table_count; i++) {
347-
msix->tables[i].vector_control = PCIM_MSIX_VCTRL_MASK;
348-
msix->tables[i].addr = 0U;
349-
msix->tables[i].data = 0U;
350-
}
351-
352-
bar = &pdev->bar[msix->table_bar];
353-
if (bar != NULL) {
354-
vdev->msix.mmio_hpa = bar->base;
355-
vdev->msix.mmio_gpa = sos_vm_hpa2gpa(bar->base);
356-
vdev->msix.mmio_size = bar->size;
357-
}
351+
/* Mask all table entries */
352+
for (i = 0U; i < msix->table_count; i++) {
353+
msix->tables[i].vector_control = PCIM_MSIX_VCTRL_MASK;
354+
msix->tables[i].addr = 0U;
355+
msix->tables[i].data = 0U;
356+
}
358357

359-
if (msix->mmio_gpa != 0U) {
360-
/*
361-
* PCI Spec: a BAR may also map other usable address space that is not associated
362-
* with MSI-X structures, but it must not share any naturally aligned 4 KB
363-
* address range with one where either MSI-X structure resides.
364-
* The MSI-X Table and MSI-X PBA are permitted to co-reside within a naturally
365-
* aligned 4 KB address range.
366-
*
367-
* If PBA or others reside in the same BAR with MSI-X Table, devicemodel could
368-
* emulate them and maps these memory range at the 4KB boundary. Here, we should
369-
* make sure only intercept the minimum number of 4K pages needed for MSI-X table.
370-
*/
371-
372-
/* The higher boundary of the 4KB aligned address range for MSI-X table */
373-
addr_hi = msix->mmio_gpa + msix->table_offset + (msix->table_count * MSIX_TABLE_ENTRY_SIZE);
374-
addr_hi = round_page_up(addr_hi);
375-
376-
/* The lower boundary of the 4KB aligned address range for MSI-X table */
377-
addr_lo = round_page_down(msix->mmio_gpa + msix->table_offset);
378-
379-
(void)register_mmio_emulation_handler(vdev->vpci->vm, vmsix_table_mmio_access_handler,
380-
addr_lo, addr_hi, vdev);
381-
}
382-
ret = 0;
383-
} else {
384-
pr_err("%s, MSI-X device (%x) invalid table BIR %d", __func__, vdev->pdev->bdf.value, msix->table_bar);
385-
vdev->msix.capoff = 0U;
386-
ret = -EIO;
358+
bar = &pdev->bar[msix->table_bar];
359+
if (bar != NULL) {
360+
vdev->msix.mmio_hpa = bar->base;
361+
vdev->msix.mmio_gpa = sos_vm_hpa2gpa(bar->base);
362+
vdev->msix.mmio_size = bar->size;
387363
}
388364

389-
return ret;
365+
if (msix->mmio_gpa != 0U) {
366+
/*
367+
* PCI Spec: a BAR may also map other usable address space that is not associated
368+
* with MSI-X structures, but it must not share any naturally aligned 4 KB
369+
* address range with one where either MSI-X structure resides.
370+
* The MSI-X Table and MSI-X PBA are permitted to co-reside within a naturally
371+
* aligned 4 KB address range.
372+
*
373+
* If PBA or others reside in the same BAR with MSI-X Table, devicemodel could
374+
* emulate them and maps these memory range at the 4KB boundary. Here, we should
375+
* make sure only intercept the minimum number of 4K pages needed for MSI-X table.
376+
*/
377+
378+
/* The higher boundary of the 4KB aligned address range for MSI-X table */
379+
addr_hi = msix->mmio_gpa + msix->table_offset + (msix->table_count * MSIX_TABLE_ENTRY_SIZE);
380+
addr_hi = round_page_up(addr_hi);
381+
382+
/* The lower boundary of the 4KB aligned address range for MSI-X table */
383+
addr_lo = round_page_down(msix->mmio_gpa + msix->table_offset);
384+
385+
(void)register_mmio_emulation_handler(vdev->vpci->vm, vmsix_table_mmio_access_handler,
386+
addr_lo, addr_hi, vdev);
387+
}
390388
}
391389

392390
/**
393391
* @pre vdev != NULL
394392
*/
395-
int32_t vmsix_init(struct pci_vdev *vdev)
393+
void vmsix_init(struct pci_vdev *vdev)
396394
{
397395
struct pci_pdev *pdev = vdev->pdev;
398-
int32_t ret = 0;
399396

400397
vdev->msix.capoff = pdev->msix.capoff;
401398
vdev->msix.caplen = pdev->msix.caplen;
@@ -404,31 +401,20 @@ int32_t vmsix_init(struct pci_vdev *vdev)
404401
(void)memcpy_s((void *)&vdev->cfgdata.data_8[pdev->msix.capoff], pdev->msix.caplen,
405402
(void *)&pdev->msix.cap[0U], pdev->msix.caplen);
406403

407-
ret = vmsix_init_helper(vdev);
404+
vmsix_init_helper(vdev);
408405
}
409-
410-
return ret;
411406
}
412407

413408
/**
414409
* @pre vdev != NULL
415410
* @pre vdev->vpci != NULL
416411
* @pre vdev->vpci->vm != NULL
417412
*/
418-
int32_t vmsix_deinit(const struct pci_vdev *vdev)
413+
void vmsix_deinit(const struct pci_vdev *vdev)
419414
{
420415
if (has_msix_cap(vdev)) {
421416
if (vdev->msix.table_count != 0U) {
422417
ptirq_remove_msix_remapping(vdev->vpci->vm, vdev->vbdf.value, vdev->msix.table_count);
423418
}
424419
}
425-
426-
return 0;
427420
}
428-
429-
const struct pci_vdev_ops pci_ops_vdev_msix = {
430-
.init = vmsix_init,
431-
.deinit = vmsix_deinit,
432-
.cfgwrite = vmsix_cfgwrite,
433-
.cfgread = vmsix_cfgread,
434-
};

hypervisor/dm/vpci/pci_priv.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,20 @@ int32_t vdev_pt_deinit(const struct pci_vdev *vdev);
8181

8282
#else
8383
extern const struct vpci_ops sharing_mode_vpci_ops;
84-
extern const struct pci_vdev_ops pci_ops_vdev_msi;
85-
extern const struct pci_vdev_ops pci_ops_vdev_msix;
8684

87-
int32_t vmsi_init(struct pci_vdev *vdev);
85+
void vmsi_init(struct pci_vdev *vdev);
8886
int32_t vmsi_cfgread(const struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t *val);
8987
int32_t vmsi_cfgwrite(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t val);
90-
int32_t vmsi_deinit(const struct pci_vdev *vdev);
91-
92-
int32_t vmsix_init(struct pci_vdev *vdev);
88+
void vmsi_deinit(const struct pci_vdev *vdev);
89+
void vmsix_init(struct pci_vdev *vdev);
9390
int32_t vmsix_cfgread(const struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t *val);
9491
int32_t vmsix_cfgwrite(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t val);
95-
int32_t vmsix_deinit(const struct pci_vdev *vdev);
92+
void vmsix_deinit(const struct pci_vdev *vdev);
9693
#endif
9794

9895
uint32_t pci_vdev_read_cfg(const struct pci_vdev *vdev, uint32_t offset, uint32_t bytes);
9996
void pci_vdev_write_cfg(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t val);
10097

101-
void add_vdev_handler(struct pci_vdev *vdev, const struct pci_vdev_ops *ops);
102-
10398
struct pci_vdev *pci_find_vdev_by_vbdf(const struct acrn_vpci *vpci, union pci_bdf vbdf);
10499

105100
struct pci_vdev *pci_find_vdev_by_pbdf(const struct acrn_vpci *vpci, union pci_bdf pbdf);

0 commit comments

Comments
 (0)