Skip to content

Commit a7f528c

Browse files
donshengEddie Dong
authored andcommitted
HV: remove vdev ops for partition mode
Remove vdev ops for partition mode, change related code to directly call the corresponding functions instead Remove struct pci_vdev_ops from vpci.h Add @pre for pci_find_vdev_by_pbdf and pci_find_vdev_by_vbdf/partition_mode_vpci_init Change the return value from int32_t to void to comply with misra c and add ASSERT/panic in the functions (if necessary): vdev_hostbridge_init vdev_hostbridge_deinit vdev_pt_init vdev_pt_deinit Still use pr_err in partition_mode_cfgread and partition_mode_cfgwrite to check if vdev cfgread/cfgwrite access is aligned on 1/2/4 bytes, which is the only case that vdev cfgread/cfgwrite will return nonzero, pr_err will be removed in subsequent patch titled "unify the sharing mode and partition mode coding style for similar functions" Remove @pre for local variables Add ASSERT in partition_mode_pdev_init to check if pdev is NULL (user config error) Tracked-On: #2534 Signed-off-by: dongshen <dongsheng.x.zhang@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent b1cc188 commit a7f528c

File tree

6 files changed

+75
-101
lines changed

6 files changed

+75
-101
lines changed

hypervisor/dm/vpci/hostbridge.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include <pci.h>
4040
#include "pci_priv.h"
4141

42-
int32_t vdev_hostbridge_init(struct pci_vdev *vdev)
42+
void vdev_hostbridge_init(struct pci_vdev *vdev)
4343
{
4444
/* PCI config space */
4545
pci_vdev_write_cfg_u16(vdev, PCIR_VENDOR, (uint16_t)0x8086U);
@@ -82,13 +82,10 @@ int32_t vdev_hostbridge_init(struct pci_vdev *vdev)
8282
pci_vdev_write_cfg_u8(vdev, 0xf5U, (uint8_t)0xfU);
8383
pci_vdev_write_cfg_u8(vdev, 0xf6U, (uint8_t)0x1cU);
8484
pci_vdev_write_cfg_u8(vdev, 0xf7U, (uint8_t)0x1U);
85-
86-
return 0;
8785
}
8886

89-
int32_t vdev_hostbridge_deinit(__unused const struct pci_vdev *vdev)
87+
void vdev_hostbridge_deinit(__unused const struct pci_vdev *vdev)
9088
{
91-
return 0;
9289
}
9390

9491
int32_t vdev_hostbridge_cfgread(const struct pci_vdev *vdev, uint32_t offset,
@@ -119,11 +116,3 @@ int32_t vdev_hostbridge_cfgwrite(struct pci_vdev *vdev, uint32_t offset,
119116

120117
return 0;
121118
}
122-
123-
const struct pci_vdev_ops pci_ops_vdev_hostbridge = {
124-
.init = vdev_hostbridge_init,
125-
.deinit = vdev_hostbridge_deinit,
126-
.cfgwrite = vdev_hostbridge_cfgwrite,
127-
.cfgread = vdev_hostbridge_cfgread,
128-
};
129-

hypervisor/dm/vpci/partition_mode.c

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <logmsg.h>
3434
#include "pci_priv.h"
3535

36+
3637
static inline bool is_hostbridge(const struct pci_vdev *vdev)
3738
{
3839
return (vdev->vbdf.value == 0U);
@@ -54,39 +55,41 @@ static inline bool is_valid_bar(const struct pci_bar *bar)
5455
return (is_valid_bar_type(bar) && is_valid_bar_size(bar));
5556
}
5657

58+
/**
59+
* @pre vdev != NULL
60+
*/
5761
static void partition_mode_pdev_init(struct pci_vdev *vdev, union pci_bdf pbdf)
5862
{
5963
struct pci_pdev *pdev;
6064
uint32_t idx;
6165
struct pci_bar *pbar, *vbar;
6266

6367
pdev = find_pci_pdev(pbdf);
64-
if (pdev != NULL) {
65-
vdev->pdev = pdev;
66-
67-
/* Sanity checking for vbar */
68-
for (idx = 0U; idx < (uint32_t)PCI_BAR_COUNT; idx++) {
69-
pbar = &vdev->pdev->bar[idx];
70-
vbar = &vdev->bar[idx];
71-
72-
if (is_valid_bar(pbar)) {
73-
vbar->size = (pbar->size < 0x1000U) ? 0x1000U : pbar->size;
74-
vbar->type = PCIBAR_MEM32;
75-
} else {
76-
/* Mark this vbar as invalid */
77-
vbar->size = 0UL;
78-
vbar->type = PCIBAR_NONE;
79-
}
68+
ASSERT(pdev != NULL, "pdev is NULL");
69+
70+
vdev->pdev = pdev;
71+
72+
/* Sanity checking for vbar */
73+
for (idx = 0U; idx < (uint32_t)PCI_BAR_COUNT; idx++) {
74+
pbar = &vdev->pdev->bar[idx];
75+
vbar = &vdev->bar[idx];
76+
77+
if (is_valid_bar(pbar)) {
78+
vbar->size = (pbar->size < 0x1000U) ? 0x1000U : pbar->size;
79+
vbar->type = PCIBAR_MEM32;
80+
} else {
81+
/* Mark this vbar as invalid */
82+
vbar->size = 0UL;
83+
vbar->type = PCIBAR_NONE;
8084
}
8185
}
86+
87+
vdev_pt_init(vdev);
8288
}
8389

8490
/**
8591
* @pre vm != NULL
86-
* @pre vm->vpci->pci_vdev_cnt <= CONFIG_MAX_PCI_DEV_NUM
87-
* @pre vm_config != NULL
88-
* @pre vdev != NULL
89-
* @pre ptdev_config != NULL
92+
* @pre vm->vpci.pci_vdev_cnt <= CONFIG_MAX_PCI_DEV_NUM
9093
*/
9194
static int32_t partition_mode_vpci_init(const struct acrn_vm *vm)
9295
{
@@ -105,25 +108,18 @@ static int32_t partition_mode_vpci_init(const struct acrn_vm *vm)
105108
vdev->vbdf.value = ptdev_config->vbdf.value;
106109

107110
if (is_hostbridge(vdev)) {
108-
vdev->ops = &pci_ops_vdev_hostbridge;
111+
vdev_hostbridge_init(vdev);
109112
} else {
110113
partition_mode_pdev_init(vdev, ptdev_config->pbdf);
111-
vdev->ops = &pci_ops_vdev_pt;
112-
}
113-
114-
if (vdev->ops->init != NULL) {
115-
if (vdev->ops->init(vdev) != 0) {
116-
pr_err("%s() failed at PCI device (vbdf %x)!", __func__,
117-
vdev->vbdf);
118-
}
119114
}
120115
}
121116

122117
return 0;
123118
}
124119

125120
/**
126-
* @pre vdev != NULL
121+
* @pre vm != NULL
122+
* @pre vm->vpci.pci_vdev_cnt <= CONFIG_MAX_PCI_DEV_NUM
127123
*/
128124
static void partition_mode_vpci_deinit(const struct acrn_vm *vm)
129125
{
@@ -132,10 +128,11 @@ static void partition_mode_vpci_deinit(const struct acrn_vm *vm)
132128

133129
for (i = 0U; i < vm->vpci.pci_vdev_cnt; i++) {
134130
vdev = (struct pci_vdev *) &(vm->vpci.pci_vdevs[i]);
135-
if ((vdev->ops != NULL) && (vdev->ops->deinit != NULL)) {
136-
if (vdev->ops->deinit(vdev) != 0) {
137-
pr_err("vdev->ops->deinit failed!");
138-
}
131+
132+
if (is_hostbridge(vdev)) {
133+
vdev_hostbridge_deinit(vdev);
134+
} else {
135+
vdev_pt_deinit(vdev);
139136
}
140137
}
141138
}
@@ -145,9 +142,16 @@ static void partition_mode_cfgread(struct acrn_vpci *vpci, union pci_bdf vbdf,
145142
{
146143
struct pci_vdev *vdev = pci_find_vdev_by_vbdf(vpci, vbdf);
147144

148-
if ((vdev != NULL) && (vdev->ops != NULL)
149-
&& (vdev->ops->cfgread != NULL)) {
150-
(void)vdev->ops->cfgread(vdev, offset, bytes, val);
145+
if (vdev != NULL) {
146+
if (is_hostbridge(vdev)) {
147+
if (vdev_hostbridge_cfgread(vdev, offset, bytes, val) != 0) {
148+
pr_err("vdev_hostbridge_cfgread failed!");
149+
}
150+
} else {
151+
if (vdev_pt_cfgread(vdev, offset, bytes, val) != 0) {
152+
pr_err("vdev_pt_cfgread failed!");
153+
}
154+
}
151155
}
152156
}
153157

@@ -156,9 +160,16 @@ static void partition_mode_cfgwrite(struct acrn_vpci *vpci, union pci_bdf vbdf,
156160
{
157161
struct pci_vdev *vdev = pci_find_vdev_by_vbdf(vpci, vbdf);
158162

159-
if ((vdev != NULL) && (vdev->ops != NULL)
160-
&& (vdev->ops->cfgwrite != NULL)) {
161-
(void)vdev->ops->cfgwrite(vdev, offset, bytes, val);
163+
if (vdev != NULL) {
164+
if (is_hostbridge(vdev)) {
165+
if (vdev_hostbridge_cfgwrite(vdev, offset, bytes, val) != 0) {
166+
pr_err("vdev_hostbridge_cfgwrite failed!");
167+
}
168+
} else {
169+
if (vdev_pt_cfgwrite(vdev, offset, bytes, val) != 0){
170+
pr_err("vdev_pt_cfgwrite failed!");
171+
}
172+
}
162173
}
163174
}
164175

hypervisor/dm/vpci/pci_priv.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ static inline void pci_vdev_write_cfg_u32(struct pci_vdev *vdev, uint32_t offset
6969

7070
#ifdef CONFIG_PARTITION_MODE
7171
extern const struct vpci_ops partition_mode_vpci_ops;
72-
int32_t vdev_hostbridge_init(struct pci_vdev *vdev);
72+
void vdev_hostbridge_init(struct pci_vdev *vdev);
7373
int32_t vdev_hostbridge_cfgread(const struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t *val);
7474
int32_t vdev_hostbridge_cfgwrite(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t val);
75-
int32_t vdev_hostbridge_deinit(__unused const struct pci_vdev *vdev);
75+
void vdev_hostbridge_deinit(__unused const struct pci_vdev *vdev);
7676

77-
int32_t vdev_pt_init(struct pci_vdev *vdev);
77+
void vdev_pt_init(struct pci_vdev *vdev);
7878
int32_t vdev_pt_cfgread(const struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t *val);
7979
int32_t vdev_pt_cfgwrite(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes, uint32_t val);
80-
int32_t vdev_pt_deinit(const struct pci_vdev *vdev);
80+
void vdev_pt_deinit(const struct pci_vdev *vdev);
8181

8282
#else
8383
extern const struct vpci_ops sharing_mode_vpci_ops;

hypervisor/dm/vpci/pci_pt.c

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,31 @@ static inline uint32_t pci_bar_base(uint32_t bar)
4242
return bar & PCIM_BAR_MEM_BASE;
4343
}
4444

45-
static int32_t vdev_pt_init_validate(struct pci_vdev *vdev)
45+
static int32_t validate(const struct pci_vdev *vdev)
4646
{
4747
uint32_t idx;
48+
int32_t ret = 0;
4849

4950
for (idx = 0U; idx < PCI_BAR_COUNT; idx++) {
5051
if ((vdev->bar[idx].base != 0x0UL)
5152
|| ((vdev->bar[idx].size & 0xFFFUL) != 0x0UL)
5253
|| ((vdev->bar[idx].type != PCIBAR_MEM32)
5354
&& (vdev->bar[idx].type != PCIBAR_NONE))) {
54-
return -EINVAL;
55+
ret = -EINVAL;
56+
break;
5557
}
5658
}
5759

58-
return 0;
60+
return ret;
5961
}
6062

61-
int32_t vdev_pt_init(struct pci_vdev *vdev)
63+
void vdev_pt_init(struct pci_vdev *vdev)
6264
{
6365
int32_t ret;
6466
struct acrn_vm *vm = vdev->vpci->vm;
6567
uint16_t pci_command;
6668

67-
ret = vdev_pt_init_validate(vdev);
68-
if (ret != 0) {
69-
pr_err("Error, invalid bar defined");
70-
return ret;
71-
}
69+
ASSERT(validate(vdev) == 0, "Error, invalid bar defined");
7270

7371
/* Create an iommu domain for target VM if not created */
7472
if (vm->iommu == NULL) {
@@ -82,24 +80,26 @@ int32_t vdev_pt_init(struct pci_vdev *vdev)
8280

8381
ret = assign_iommu_device(vm->iommu, (uint8_t)vdev->pdev->bdf.bits.b,
8482
(uint8_t)(vdev->pdev->bdf.value & 0xFFU));
83+
if (ret != 0) {
84+
panic("failed to assign iommu device!");
85+
}
8586

8687
pci_command = (uint16_t)pci_pdev_read_cfg(vdev->pdev->bdf, PCIR_COMMAND, 2U);
8788
/* Disable INTX */
8889
pci_command |= 0x400U;
8990
pci_pdev_write_cfg(vdev->pdev->bdf, PCIR_COMMAND, 2U, pci_command);
90-
91-
return ret;
9291
}
9392

94-
int32_t vdev_pt_deinit(const struct pci_vdev *vdev)
93+
void vdev_pt_deinit(const struct pci_vdev *vdev)
9594
{
9695
int32_t ret;
9796
struct acrn_vm *vm = vdev->vpci->vm;
9897

9998
ret = unassign_iommu_device(vm->iommu, (uint8_t)vdev->pdev->bdf.bits.b,
10099
(uint8_t)(vdev->pdev->bdf.value & 0xFFU));
101-
102-
return ret;
100+
if (ret != 0) {
101+
panic("failed to unassign iommu device!");
102+
}
103103
}
104104

105105
int32_t vdev_pt_cfgread(const struct pci_vdev *vdev, uint32_t offset,
@@ -200,10 +200,3 @@ int32_t vdev_pt_cfgwrite(struct pci_vdev *vdev, uint32_t offset,
200200
return 0;
201201
}
202202

203-
const struct pci_vdev_ops pci_ops_vdev_pt = {
204-
.init = vdev_pt_init,
205-
.deinit = vdev_pt_deinit,
206-
.cfgwrite = vdev_pt_cfgwrite,
207-
.cfgread = vdev_pt_cfgread,
208-
};
209-

hypervisor/dm/vpci/vdev.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ void pci_vdev_write_cfg(struct pci_vdev *vdev, uint32_t offset, uint32_t bytes,
6565
}
6666

6767
/**
68-
* @pre tmp != NULL
68+
* @pre vpci != NULL
69+
* @pre vpci->pci_vdev_cnt <= CONFIG_MAX_PCI_DEV_NUM
6970
*/
7071
struct pci_vdev *pci_find_vdev_by_vbdf(const struct acrn_vpci *vpci, union pci_bdf vbdf)
7172
{
@@ -86,7 +87,8 @@ struct pci_vdev *pci_find_vdev_by_vbdf(const struct acrn_vpci *vpci, union pci_b
8687
}
8788

8889
/**
89-
* @pre tmp != NULL
90+
* @pre vpci != NULL
91+
* @pre vpci->pci_vdev_cnt <= CONFIG_MAX_PCI_DEV_NUM
9092
*/
9193
struct pci_vdev *pci_find_vdev_by_pbdf(const struct acrn_vpci *vpci, union pci_bdf pbdf)
9294
{

hypervisor/include/dm/vpci.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@
3232

3333
#include <pci.h>
3434

35-
struct pci_vdev;
36-
struct pci_vdev_ops {
37-
int32_t (*init)(struct pci_vdev *vdev);
38-
39-
int32_t (*deinit)(const struct pci_vdev *vdev);
40-
41-
int32_t (*cfgwrite)(struct pci_vdev *vdev, uint32_t offset,
42-
uint32_t bytes, uint32_t val);
43-
44-
int32_t (*cfgread)(const struct pci_vdev *vdev, uint32_t offset,
45-
uint32_t bytes, uint32_t *val);
46-
};
4735

4836
struct msix_table_entry {
4937
uint64_t addr;
@@ -77,10 +65,6 @@ union pci_cfgdata {
7765
};
7866

7967
struct pci_vdev {
80-
#ifdef CONFIG_PARTITION_MODE
81-
const struct pci_vdev_ops *ops;
82-
#endif
83-
8468
const struct acrn_vpci *vpci;
8569
/* The bus/device/function triple of the virtual PCI device. */
8670
union pci_bdf vbdf;
@@ -121,11 +105,6 @@ struct acrn_vpci {
121105
struct pci_vdev pci_vdevs[CONFIG_MAX_PCI_DEV_NUM];
122106
};
123107

124-
#ifdef CONFIG_PARTITION_MODE
125-
extern const struct pci_vdev_ops pci_ops_vdev_hostbridge;
126-
extern const struct pci_vdev_ops pci_ops_vdev_pt;
127-
#endif
128-
129108
void vpci_init(struct acrn_vm *vm);
130109
void vpci_cleanup(const struct acrn_vm *vm);
131110
void vpci_set_ptdev_intr_info(const struct acrn_vm *target_vm, uint16_t vbdf, uint16_t pbdf);

0 commit comments

Comments
 (0)