Skip to content

Commit 23c3fbd

Browse files
Liu Shuojren1
authored andcommitted
virtio-heci: Backend service for HECI virtualization
This patch implement HECI virtualization backend service based on virtio userspace framework. This service introduce a virtio-heci device model for DM, and can be enabled by DM parameter '-s [BUS NO],virtio-heci'. The HECI virtualization need both backend service in device-model and frontend driver in guest to work. Backend service mainly emulates HECI device's behaviors to satisfy frontend driver. They are based on virtio userspace framework. Currently, it is using two virtqueues, one is for TX and another is for RX. Signed-off-by: Liu Shuo <shuo.a.liu@intel.com> Reviewed-by: Li Hao <hao.l.li@intel.com> Reviewed-by: Wang Yu <yu1.wang@intel.com> Reviewed-by: Zhao, Yakui <yakui.zhao@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 06bd73c commit 23c3fbd

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

devicemodel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ SRCS += hw/pci/passthrough.c
7575
SRCS += hw/pci/virtio/virtio_net.c
7676
SRCS += hw/pci/virtio/virtio_rnd.c
7777
SRCS += hw/pci/virtio/virtio_hyper_dmabuf.c
78+
SRCS += hw/pci/virtio/virtio_heci.c
7879
SRCS += hw/pci/irq.c
7980
SRCS += hw/pci/uart.c
8081
SRCS += hw/acpi/acpi.c
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in
12+
* the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Intel Corporation nor the names of its
15+
* contributors may be used to endorse or promote products derived
16+
* from this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
*/
31+
/*
32+
* HECI device virtualization.
33+
*/
34+
35+
#include <sys/cdefs.h>
36+
#include <sys/param.h>
37+
38+
#include <stdio.h>
39+
#include <stdlib.h>
40+
#include <unistd.h>
41+
#include <string.h>
42+
#include <pthread.h>
43+
44+
#include "dm.h"
45+
#include "pci_core.h"
46+
#include "virtio.h"
47+
48+
#define VIRTIO_HECI_RXQ 0
49+
#define VIRTIO_HECI_TXQ 1
50+
#define VIRTIO_HECI_RXSEGS 1
51+
#define VIRTIO_HECI_TXSEGS 2
52+
53+
/*
54+
* HECI HW max support FIFO depth is 128
55+
* We might support larger depth, which need change MEI driver
56+
*/
57+
#define VIRTIO_HECI_FIFOSZ 128
58+
59+
#define VIRTIO_HECI_RINGSZ 64
60+
#define VIRTIO_HECI_VQNUM 2
61+
62+
struct virtio_heci_config {
63+
int buf_depth;
64+
uint8_t hw_ready;
65+
uint8_t host_reset;
66+
} __attribute__((packed));
67+
68+
struct virtio_heci {
69+
struct virtio_base base;
70+
struct virtio_vq_info vqs[VIRTIO_HECI_VQNUM];
71+
pthread_mutex_t mutex;
72+
73+
struct virtio_heci_config *config;
74+
};
75+
76+
/*
77+
* Debug printf
78+
*/
79+
static int virtio_heci_debug;
80+
#define DPRINTF(params) do { if (virtio_heci_debug) printf params; } while (0)
81+
#define WPRINTF(params) (printf params)
82+
83+
static void virtio_heci_reset(void *);
84+
static void virtio_heci_notify_rx(void *, struct virtio_vq_info *);
85+
static void virtio_heci_notify_tx(void *, struct virtio_vq_info *);
86+
static int virtio_heci_cfgread(void *, int, int, uint32_t *);
87+
static int virtio_heci_cfgwrite(void *, int, int, uint32_t);
88+
89+
static struct virtio_ops virtio_heci_ops = {
90+
"virtio_heci", /* our name */
91+
VIRTIO_HECI_VQNUM, /* we support several virtqueues */
92+
sizeof(struct virtio_heci_config), /* config reg size */
93+
virtio_heci_reset, /* reset */
94+
NULL, /* device-wide qnotify */
95+
virtio_heci_cfgread, /* read virtio config */
96+
virtio_heci_cfgwrite, /* write virtio config */
97+
NULL, /* apply negotiated features */
98+
NULL, /* called on guest set status */
99+
0, /* our capabilities */
100+
};
101+
102+
static void
103+
virtio_heci_reset(void *vth)
104+
{
105+
struct virtio_heci *vheci = vth;
106+
107+
DPRINTF(("vheci: device reset requested !\r\n"));
108+
virtio_reset_dev(&vheci->base);
109+
}
110+
111+
static void
112+
virtio_heci_notify_rx(void *heci, struct virtio_vq_info *vq)
113+
{
114+
/*
115+
* Any ring entries to process?
116+
*/
117+
if (!vq_has_descs(vq))
118+
return;
119+
}
120+
121+
static void
122+
virtio_heci_notify_tx(void *heci, struct virtio_vq_info *vq)
123+
{
124+
/*
125+
* Any ring entries to process?
126+
*/
127+
if (!vq_has_descs(vq))
128+
return;
129+
}
130+
131+
static int
132+
virtio_heci_cfgread(void *vsc, int offset, int size, uint32_t *retval)
133+
{
134+
struct virtio_heci *vheci = vsc;
135+
void *ptr;
136+
137+
ptr = (uint8_t *)vheci->config + offset;
138+
memcpy(retval, ptr, size);
139+
return 0;
140+
}
141+
142+
static int
143+
virtio_heci_cfgwrite(void *vsc, int offset, int size, uint32_t val)
144+
{
145+
/* TODO: need handle device config writing */
146+
return 0;
147+
}
148+
149+
static int
150+
virtio_heci_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
151+
{
152+
struct virtio_heci *vheci;
153+
pthread_mutexattr_t attr;
154+
int i, rc;
155+
156+
vheci = calloc(1, sizeof(struct virtio_heci));
157+
if (!vheci) {
158+
WPRINTF(("vheci init: fail to alloc virtio_heci!\r\n"));
159+
return -1;
160+
}
161+
vheci->config = calloc(1, sizeof(struct virtio_heci_config));
162+
if (!vheci->config) {
163+
WPRINTF(("vheci init: fail to alloc virtio_heci_config!\r\n"));
164+
goto fail;
165+
}
166+
vheci->config->buf_depth = VIRTIO_HECI_FIFOSZ;
167+
vheci->config->hw_ready = 1;
168+
169+
/* init mutex attribute properly */
170+
rc = pthread_mutexattr_init(&attr);
171+
if (rc) {
172+
WPRINTF(("vheci init: mutexattr init fail, erro %d!\r\n", rc));
173+
goto fail;
174+
}
175+
if (fbsdrun_virtio_msix()) {
176+
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
177+
if (rc) {
178+
WPRINTF(("vheci init: mutexattr_settype failed with "
179+
"error %d!\r\n", rc));
180+
goto fail;
181+
}
182+
} else {
183+
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
184+
if (rc) {
185+
WPRINTF(("vheci init: mutexattr_settype failed with "
186+
"error %d!\r\n", rc));
187+
goto fail;
188+
}
189+
}
190+
rc = pthread_mutex_init(&vheci->mutex, &attr);
191+
if (rc) {
192+
WPRINTF(("vheci init: mutex init failed, error %d!\r\n", rc));
193+
goto fail;
194+
}
195+
196+
virtio_linkup(&vheci->base, &virtio_heci_ops,
197+
vheci, dev, vheci->vqs);
198+
vheci->base.mtx = &vheci->mutex;
199+
200+
for (i = 0; i < VIRTIO_HECI_VQNUM; i++)
201+
vheci->vqs[i].qsize = VIRTIO_HECI_RINGSZ;
202+
vheci->vqs[VIRTIO_HECI_RXQ].notify = virtio_heci_notify_rx;
203+
vheci->vqs[VIRTIO_HECI_TXQ].notify = virtio_heci_notify_tx;
204+
205+
/* initialize config space */
206+
pci_set_cfgdata16(dev, PCIR_DEVICE, VIRTIO_DEV_HECI);
207+
pci_set_cfgdata16(dev, PCIR_VENDOR, INTEL_VENDOR_ID);
208+
pci_set_cfgdata8(dev, PCIR_CLASS, PCIC_SIMPLECOMM);
209+
pci_set_cfgdata8(dev, PCIR_SUBCLASS, PCIS_SIMPLECOMM_OTHER);
210+
pci_set_cfgdata16(dev, PCIR_SUBDEV_0, VIRTIO_TYPE_HECI);
211+
pci_set_cfgdata16(dev, PCIR_SUBVEND_0, INTEL_VENDOR_ID);
212+
213+
if (virtio_interrupt_init(&vheci->base, fbsdrun_virtio_msix()))
214+
goto setup_fail;
215+
virtio_set_io_bar(&vheci->base, 0);
216+
217+
return 0;
218+
setup_fail:
219+
pthread_mutex_destroy(&vheci->mutex);
220+
fail:
221+
free(vheci->config);
222+
free(vheci);
223+
return -1;
224+
}
225+
226+
static void
227+
virtio_heci_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
228+
{
229+
struct virtio_heci *vheci = (struct virtio_heci *)dev->arg;
230+
231+
pthread_mutex_destroy(&vheci->mutex);
232+
free(vheci->config);
233+
free(vheci);
234+
}
235+
236+
struct pci_vdev_ops pci_ops_vheci = {
237+
.class_name = "virtio-heci",
238+
.vdev_init = virtio_heci_init,
239+
.vdev_barwrite = virtio_pci_write,
240+
.vdev_barread = virtio_pci_read,
241+
.vdev_deinit = virtio_heci_deinit
242+
};
243+
DEFINE_PCI_DEVTYPE(pci_ops_vheci);

0 commit comments

Comments
 (0)