Skip to content

Commit 86180bd

Browse files
donshenglijinxia
authored andcommitted
HV: Calling into VPCI init/unit functions for partition hypervisor
V4: - Clear address cache info after a full cf8/cfc access - Add NULL pointer checking when calling init/deinit ops V3: - Do not use ASSERT - Loop through the vdev list defined in vm_desctiption table to call the vdev init/unit functions - Make the cached vbdf info struct per vm instead of per pcpu V2: - Fixed MISRA violations Reviewed-by: Anthony Xu <anthony.xu@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com> Signed-off-by: dongshen <dongsheng.x.zhang@intel.com>
1 parent 65bd038 commit 86180bd

File tree

4 files changed

+267
-0
lines changed

4 files changed

+267
-0
lines changed

hypervisor/arch/x86/guest/vm.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
186186
}
187187
vm->vpic = vpic_init(vm);
188188

189+
#ifdef CONFIG_PARTITION_MODE
190+
vpci_init(vm);
191+
#endif
192+
189193
/* vpic wire_mode default is INTR */
190194
vm->wire_mode = VPIC_WIRE_INTR;
191195

@@ -290,6 +294,10 @@ int shutdown_vm(struct vm *vm)
290294
vpic_cleanup(vm);
291295
}
292296

297+
#ifdef CONFIG_PARTITION_MODE
298+
vpci_cleanup(vm);
299+
#endif
300+
293301
free(vm->hw.vcpu_array);
294302

295303
/* TODO: De-Configure HV-SW */

hypervisor/dm/vpci/pci_priv.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*-
2+
* Copyright (c) 2011 NetApp, Inc.
3+
* Copyright (c) 2018 Intel Corporation
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
* SUCH DAMAGE.
26+
*
27+
* $FreeBSD$
28+
*/
29+
30+
#ifndef PCI_PRIV_H_
31+
#define PCI_PRIV_H_
32+
33+
#include <hv_debug.h>
34+
#include "vpci.h"
35+
36+
#define PCIM_BAR_MEM_BASE 0xfffffff0U
37+
#define PCI_BAR_BASE(val) ((val) & PCIM_BAR_MEM_BASE)
38+
#define PCI_BAR(base, type) ((base) | (type))
39+
40+
#define PCI_BUS(bdf) (((bdf) >> 8) & 0xFFU)
41+
#define PCI_SLOT(bdf) (((bdf) >> 3) & 0x1FU)
42+
#define PCI_FUNC(bdf) ((bdf) & 0x07U)
43+
44+
#define LOBYTE(w) ((uint8_t)((w) & 0xffU))
45+
46+
#define PCI_BUSMAX 0xffU
47+
#define PCI_SLOTMAX 0x1fU
48+
#define PCI_FUNCMAX 0x7U
49+
50+
#define MAXBUSES (PCI_BUSMAX + 1U)
51+
#define MAXSLOTS (PCI_SLOTMAX + 1U)
52+
#define MAXFUNCS (PCI_FUNCMAX + 1U)
53+
54+
#define PCIR_VENDOR 0x00U
55+
#define PCIR_DEVICE 0x02U
56+
#define PCIR_COMMAND 0x04U
57+
#define PCIM_CMD_MEMEN 0x0002U
58+
#define PCIR_REVID 0x08U
59+
#define PCIR_SUBCLASS 0x0aU
60+
#define PCIR_CLASS 0x0bU
61+
#define PCIR_HDRTYPE 0x0eU
62+
#define PCIM_HDRTYPE_NORMAL 0x00U
63+
#define PCIM_MFDEV 0x80U
64+
65+
#define PCIR_BARS 0x10U
66+
#define PCIR_BAR(x) (PCIR_BARS + ((x) * 4U))
67+
68+
#define PCIM_BAR_MEM_SPACE 0U
69+
70+
#define PCIC_BRIDGE 0x06U
71+
#define PCIS_BRIDGE_HOST 0x00U
72+
73+
#define PCI_CONFIG_ADDR 0xcf8U
74+
#define PCI_CONFIG_DATA 0xcfcU
75+
76+
#define PCI_CFG_ENABLE 0x80000000U
77+
78+
void pci_vdev_cfg_handler(struct vpci *vpci, uint32_t in, uint16_t vbdf,
79+
uint32_t offset, uint32_t bytes, uint32_t *val);
80+
81+
#endif /* PCI_PRIV_H_ */

hypervisor/dm/vpci/vpci.c

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*-
2+
* Copyright (c) 2011 NetApp, Inc.
3+
* Copyright (c) 2018 Intel Corporation
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
* SUCH DAMAGE.
26+
*
27+
* $FreeBSD$
28+
*/
29+
30+
#include <hypervisor.h>
31+
#include <hv_lib.h>
32+
#include <acrn_common.h>
33+
#include <hv_arch.h>
34+
#include <hv_debug.h>
35+
#include "pci_priv.h"
36+
37+
38+
static bool is_cfg_addr(uint16_t addr)
39+
{
40+
return (addr >= PCI_CONFIG_ADDR) && (addr < (PCI_CONFIG_ADDR + 4));
41+
}
42+
43+
static bool is_cfg_data(uint16_t addr)
44+
{
45+
return (addr >= PCI_CONFIG_DATA) && (addr < (PCI_CONFIG_DATA + 4));
46+
}
47+
48+
static void pci_cfg_clear_cache(struct pci_addr_info *pi)
49+
{
50+
pi->cached_bdf = 0xffffU;
51+
pi->cached_reg = 0U;
52+
pi->cached_enable = 0U;
53+
}
54+
55+
static uint32_t pci_cfg_io_read(__unused struct vm_io_handler *hdlr,
56+
struct vm *vm, uint16_t addr, size_t bytes)
57+
{
58+
uint32_t val = 0xffffffffU;
59+
struct vpci *vpci = &vm->vpci;
60+
struct pci_addr_info *pi = &vpci->addr_info;
61+
62+
if (is_cfg_addr(addr)) {
63+
/* TODO: handling the non 4 bytes access */
64+
if (bytes == 4U) {
65+
val = (PCI_BUS(pi->cached_bdf) << 16)
66+
| (PCI_SLOT(pi->cached_bdf) << 11)
67+
| (PCI_FUNC(pi->cached_bdf) << 8)
68+
| pi->cached_reg;
69+
70+
if (pi->cached_enable) {
71+
val |= PCI_CFG_ENABLE;
72+
}
73+
}
74+
} else if (is_cfg_data(addr)) {
75+
if (pi->cached_enable) {
76+
uint16_t offset = addr - 0xcfc;
77+
78+
pci_vdev_cfg_handler(&vm->vpci, 1U, pi->cached_bdf,
79+
pi->cached_reg + offset, bytes, &val);
80+
81+
pci_cfg_clear_cache(pi);
82+
}
83+
} else {
84+
val = 0xffffffffU;
85+
}
86+
87+
return val;
88+
}
89+
90+
static void pci_cfg_io_write(__unused struct vm_io_handler *hdlr,
91+
struct vm *vm, uint16_t addr, size_t bytes, uint32_t val)
92+
{
93+
struct vpci *vpci = &vm->vpci;
94+
struct pci_addr_info *pi = &vpci->addr_info;
95+
96+
if (is_cfg_addr(addr)) {
97+
/* TODO: handling the non 4 bytes access */
98+
if (bytes == 4U) {
99+
pi->cached_bdf = PCI_BDF(
100+
((val >> 16) & PCI_BUSMAX),
101+
((val >> 11) & PCI_SLOTMAX),
102+
((val >> 8) & PCI_FUNCMAX));
103+
104+
pi->cached_reg = val & PCI_REGMAX;
105+
pi->cached_enable =
106+
(val & PCI_CFG_ENABLE) == PCI_CFG_ENABLE;
107+
}
108+
} else if (is_cfg_data(addr)) {
109+
if (pi->cached_enable) {
110+
uint16_t offset = addr - 0xcfc;
111+
112+
pci_vdev_cfg_handler(&vm->vpci, 0U, pi->cached_bdf,
113+
pi->cached_reg + offset, bytes, &val);
114+
115+
pci_cfg_clear_cache(pi);
116+
}
117+
} else {
118+
pr_err("Not PCI cfg data/addr port access!");
119+
}
120+
121+
}
122+
123+
void vpci_init(struct vm *vm)
124+
{
125+
struct vpci *vpci = &vm->vpci;
126+
struct vpci_vdev_array *vdev_array;
127+
struct pci_vdev *vdev;
128+
int i;
129+
int ret;
130+
struct vm_io_range pci_cfg_range = {.flags = IO_ATTR_RW,
131+
.base = PCI_CONFIG_ADDR, .len = 8U};
132+
133+
vpci->vm = vm;
134+
vdev_array = vm->vm_desc->vpci_vdev_array;
135+
136+
for (i = 0; i < vdev_array->num_pci_vdev; i++) {
137+
vdev = &vdev_array->vpci_vdev_list[i];
138+
vdev->vpci = vpci;
139+
if ((vdev->ops != NULL) && (vdev->ops->init != NULL)) {
140+
ret = vdev->ops->init(vdev);
141+
if (ret) {
142+
pr_err("vdev->ops->init failed!");
143+
}
144+
}
145+
}
146+
147+
register_io_emulation_handler(vm, &pci_cfg_range,
148+
&pci_cfg_io_read, &pci_cfg_io_write);
149+
}
150+
151+
void vpci_cleanup(struct vm *vm)
152+
{
153+
struct vpci_vdev_array *vdev_array;
154+
struct pci_vdev *vdev;
155+
int i;
156+
int ret;
157+
158+
vdev_array = vm->vm_desc->vpci_vdev_array;
159+
160+
for (i = 0; i < vdev_array->num_pci_vdev; i++) {
161+
vdev = &vdev_array->vpci_vdev_list[i];
162+
if ((vdev->ops != NULL) && (vdev->ops->deinit != NULL)) {
163+
ret = vdev->ops->deinit(vdev);
164+
if (ret) {
165+
pr_err("vdev->ops->deinit failed!");
166+
}
167+
}
168+
}
169+
}
170+
171+
void pci_vdev_cfg_handler(struct vpci *vpci, uint32_t in, uint16_t vbdf,
172+
uint32_t offset, uint32_t bytes, uint32_t *val)
173+
{
174+
/* vm-exit handler */
175+
}

hypervisor/include/dm/vpci/vpci.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,7 @@ struct vpci {
9292
struct pci_addr_info addr_info;
9393
};
9494

95+
void vpci_init(struct vm *vm);
96+
void vpci_cleanup(struct vm *vm);
97+
9598
#endif /* VPCI_H_ */

0 commit comments

Comments
 (0)