root / hw / virtio-pci.c @ 7157e2e2
History | View | Annotate | Download (29.6 kB)
1 |
/*
|
---|---|
2 |
* Virtio PCI Bindings
|
3 |
*
|
4 |
* Copyright IBM, Corp. 2007
|
5 |
* Copyright (c) 2009 CodeSourcery
|
6 |
*
|
7 |
* Authors:
|
8 |
* Anthony Liguori <aliguori@us.ibm.com>
|
9 |
* Paul Brook <paul@codesourcery.com>
|
10 |
*
|
11 |
* This work is licensed under the terms of the GNU GPL, version 2. See
|
12 |
* the COPYING file in the top-level directory.
|
13 |
*
|
14 |
*/
|
15 |
|
16 |
#include <inttypes.h> |
17 |
|
18 |
#include "virtio.h" |
19 |
#include "virtio-blk.h" |
20 |
#include "virtio-net.h" |
21 |
#include "virtio-serial.h" |
22 |
#include "pci.h" |
23 |
#include "qemu-error.h" |
24 |
#include "msix.h" |
25 |
#include "net.h" |
26 |
#include "loader.h" |
27 |
#include "kvm.h" |
28 |
#include "blockdev.h" |
29 |
|
30 |
/* from Linux's linux/virtio_pci.h */
|
31 |
|
32 |
/* A 32-bit r/o bitmask of the features supported by the host */
|
33 |
#define VIRTIO_PCI_HOST_FEATURES 0 |
34 |
|
35 |
/* A 32-bit r/w bitmask of features activated by the guest */
|
36 |
#define VIRTIO_PCI_GUEST_FEATURES 4 |
37 |
|
38 |
/* A 32-bit r/w PFN for the currently selected queue */
|
39 |
#define VIRTIO_PCI_QUEUE_PFN 8 |
40 |
|
41 |
/* A 16-bit r/o queue size for the currently selected queue */
|
42 |
#define VIRTIO_PCI_QUEUE_NUM 12 |
43 |
|
44 |
/* A 16-bit r/w queue selector */
|
45 |
#define VIRTIO_PCI_QUEUE_SEL 14 |
46 |
|
47 |
/* A 16-bit r/w queue notifier */
|
48 |
#define VIRTIO_PCI_QUEUE_NOTIFY 16 |
49 |
|
50 |
/* An 8-bit device status register. */
|
51 |
#define VIRTIO_PCI_STATUS 18 |
52 |
|
53 |
/* An 8-bit r/o interrupt status register. Reading the value will return the
|
54 |
* current contents of the ISR and will also clear it. This is effectively
|
55 |
* a read-and-acknowledge. */
|
56 |
#define VIRTIO_PCI_ISR 19 |
57 |
|
58 |
/* MSI-X registers: only enabled if MSI-X is enabled. */
|
59 |
/* A 16-bit vector for configuration changes. */
|
60 |
#define VIRTIO_MSI_CONFIG_VECTOR 20 |
61 |
/* A 16-bit vector for selected queue notifications. */
|
62 |
#define VIRTIO_MSI_QUEUE_VECTOR 22 |
63 |
|
64 |
/* Config space size */
|
65 |
#define VIRTIO_PCI_CONFIG_NOMSI 20 |
66 |
#define VIRTIO_PCI_CONFIG_MSI 24 |
67 |
#define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
|
68 |
VIRTIO_PCI_CONFIG_MSI : \ |
69 |
VIRTIO_PCI_CONFIG_NOMSI) |
70 |
|
71 |
/* The remaining space is defined by each driver as the per-driver
|
72 |
* configuration space */
|
73 |
#define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
|
74 |
VIRTIO_PCI_CONFIG_MSI : \ |
75 |
VIRTIO_PCI_CONFIG_NOMSI) |
76 |
|
77 |
/* Virtio ABI version, if we increment this, we break the guest driver. */
|
78 |
#define VIRTIO_PCI_ABI_VERSION 0 |
79 |
|
80 |
/* How many bits to shift physical queue address written to QUEUE_PFN.
|
81 |
* 12 is historical, and due to x86 page size. */
|
82 |
#define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 |
83 |
|
84 |
/* Flags track per-device state like workarounds for quirks in older guests. */
|
85 |
#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0) |
86 |
|
87 |
/* Performance improves when virtqueue kick processing is decoupled from the
|
88 |
* vcpu thread using ioeventfd for some devices. */
|
89 |
#define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1 |
90 |
#define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT) |
91 |
|
92 |
/* QEMU doesn't strictly need write barriers since everything runs in
|
93 |
* lock-step. We'll leave the calls to wmb() in though to make it obvious for
|
94 |
* KVM or if kqemu gets SMP support.
|
95 |
*/
|
96 |
#define wmb() do { } while (0) |
97 |
|
98 |
/* PCI bindings. */
|
99 |
|
100 |
typedef struct { |
101 |
PCIDevice pci_dev; |
102 |
VirtIODevice *vdev; |
103 |
uint32_t flags; |
104 |
uint32_t addr; |
105 |
uint32_t class_code; |
106 |
uint32_t nvectors; |
107 |
BlockConf block; |
108 |
NICConf nic; |
109 |
uint32_t host_features; |
110 |
#ifdef CONFIG_LINUX
|
111 |
V9fsConf fsconf; |
112 |
#endif
|
113 |
virtio_serial_conf serial; |
114 |
virtio_net_conf net; |
115 |
bool ioeventfd_disabled;
|
116 |
bool ioeventfd_started;
|
117 |
} VirtIOPCIProxy; |
118 |
|
119 |
/* virtio device */
|
120 |
|
121 |
static void virtio_pci_notify(void *opaque, uint16_t vector) |
122 |
{ |
123 |
VirtIOPCIProxy *proxy = opaque; |
124 |
if (msix_enabled(&proxy->pci_dev))
|
125 |
msix_notify(&proxy->pci_dev, vector); |
126 |
else
|
127 |
qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1); |
128 |
} |
129 |
|
130 |
static void virtio_pci_save_config(void * opaque, QEMUFile *f) |
131 |
{ |
132 |
VirtIOPCIProxy *proxy = opaque; |
133 |
pci_device_save(&proxy->pci_dev, f); |
134 |
msix_save(&proxy->pci_dev, f); |
135 |
if (msix_present(&proxy->pci_dev))
|
136 |
qemu_put_be16(f, proxy->vdev->config_vector); |
137 |
} |
138 |
|
139 |
static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f) |
140 |
{ |
141 |
VirtIOPCIProxy *proxy = opaque; |
142 |
if (msix_present(&proxy->pci_dev))
|
143 |
qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n)); |
144 |
} |
145 |
|
146 |
static int virtio_pci_load_config(void * opaque, QEMUFile *f) |
147 |
{ |
148 |
VirtIOPCIProxy *proxy = opaque; |
149 |
int ret;
|
150 |
ret = pci_device_load(&proxy->pci_dev, f); |
151 |
if (ret) {
|
152 |
return ret;
|
153 |
} |
154 |
msix_load(&proxy->pci_dev, f); |
155 |
if (msix_present(&proxy->pci_dev)) {
|
156 |
qemu_get_be16s(f, &proxy->vdev->config_vector); |
157 |
} else {
|
158 |
proxy->vdev->config_vector = VIRTIO_NO_VECTOR; |
159 |
} |
160 |
if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
|
161 |
return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
|
162 |
} |
163 |
return 0; |
164 |
} |
165 |
|
166 |
static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f) |
167 |
{ |
168 |
VirtIOPCIProxy *proxy = opaque; |
169 |
uint16_t vector; |
170 |
if (msix_present(&proxy->pci_dev)) {
|
171 |
qemu_get_be16s(f, &vector); |
172 |
} else {
|
173 |
vector = VIRTIO_NO_VECTOR; |
174 |
} |
175 |
virtio_queue_set_vector(proxy->vdev, n, vector); |
176 |
if (vector != VIRTIO_NO_VECTOR) {
|
177 |
return msix_vector_use(&proxy->pci_dev, vector);
|
178 |
} |
179 |
return 0; |
180 |
} |
181 |
|
182 |
static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy, |
183 |
int n, bool assign) |
184 |
{ |
185 |
VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
186 |
EventNotifier *notifier = virtio_queue_get_host_notifier(vq); |
187 |
int r;
|
188 |
if (assign) {
|
189 |
r = event_notifier_init(notifier, 1);
|
190 |
if (r < 0) { |
191 |
error_report("%s: unable to init event notifier: %d",
|
192 |
__func__, r); |
193 |
return r;
|
194 |
} |
195 |
r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier), |
196 |
proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY, |
197 |
n, assign); |
198 |
if (r < 0) { |
199 |
error_report("%s: unable to map ioeventfd: %d",
|
200 |
__func__, r); |
201 |
event_notifier_cleanup(notifier); |
202 |
} |
203 |
} else {
|
204 |
r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier), |
205 |
proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY, |
206 |
n, assign); |
207 |
if (r < 0) { |
208 |
error_report("%s: unable to unmap ioeventfd: %d",
|
209 |
__func__, r); |
210 |
return r;
|
211 |
} |
212 |
|
213 |
/* Handle the race condition where the guest kicked and we deassigned
|
214 |
* before we got around to handling the kick.
|
215 |
*/
|
216 |
if (event_notifier_test_and_clear(notifier)) {
|
217 |
virtio_queue_notify_vq(vq); |
218 |
} |
219 |
|
220 |
event_notifier_cleanup(notifier); |
221 |
} |
222 |
return r;
|
223 |
} |
224 |
|
225 |
static void virtio_pci_host_notifier_read(void *opaque) |
226 |
{ |
227 |
VirtQueue *vq = opaque; |
228 |
EventNotifier *n = virtio_queue_get_host_notifier(vq); |
229 |
if (event_notifier_test_and_clear(n)) {
|
230 |
virtio_queue_notify_vq(vq); |
231 |
} |
232 |
} |
233 |
|
234 |
static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy, |
235 |
int n, bool assign) |
236 |
{ |
237 |
VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
238 |
EventNotifier *notifier = virtio_queue_get_host_notifier(vq); |
239 |
if (assign) {
|
240 |
qemu_set_fd_handler(event_notifier_get_fd(notifier), |
241 |
virtio_pci_host_notifier_read, NULL, vq);
|
242 |
} else {
|
243 |
qemu_set_fd_handler(event_notifier_get_fd(notifier), |
244 |
NULL, NULL, NULL); |
245 |
} |
246 |
} |
247 |
|
248 |
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) |
249 |
{ |
250 |
int n, r;
|
251 |
|
252 |
if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
|
253 |
proxy->ioeventfd_disabled || |
254 |
proxy->ioeventfd_started) { |
255 |
return;
|
256 |
} |
257 |
|
258 |
for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { |
259 |
if (!virtio_queue_get_num(proxy->vdev, n)) {
|
260 |
continue;
|
261 |
} |
262 |
|
263 |
r = virtio_pci_set_host_notifier_internal(proxy, n, true);
|
264 |
if (r < 0) { |
265 |
goto assign_error;
|
266 |
} |
267 |
|
268 |
virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
|
269 |
} |
270 |
proxy->ioeventfd_started = true;
|
271 |
return;
|
272 |
|
273 |
assign_error:
|
274 |
while (--n >= 0) { |
275 |
if (!virtio_queue_get_num(proxy->vdev, n)) {
|
276 |
continue;
|
277 |
} |
278 |
|
279 |
virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
|
280 |
r = virtio_pci_set_host_notifier_internal(proxy, n, false);
|
281 |
assert(r >= 0);
|
282 |
} |
283 |
proxy->ioeventfd_started = false;
|
284 |
error_report("%s: failed. Fallback to a userspace (slower).", __func__);
|
285 |
} |
286 |
|
287 |
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) |
288 |
{ |
289 |
int r;
|
290 |
int n;
|
291 |
|
292 |
if (!proxy->ioeventfd_started) {
|
293 |
return;
|
294 |
} |
295 |
|
296 |
for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { |
297 |
if (!virtio_queue_get_num(proxy->vdev, n)) {
|
298 |
continue;
|
299 |
} |
300 |
|
301 |
virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
|
302 |
r = virtio_pci_set_host_notifier_internal(proxy, n, false);
|
303 |
assert(r >= 0);
|
304 |
} |
305 |
proxy->ioeventfd_started = false;
|
306 |
} |
307 |
|
308 |
static void virtio_pci_reset(DeviceState *d) |
309 |
{ |
310 |
VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev); |
311 |
virtio_pci_stop_ioeventfd(proxy); |
312 |
virtio_reset(proxy->vdev); |
313 |
msix_reset(&proxy->pci_dev); |
314 |
proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
315 |
} |
316 |
|
317 |
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
318 |
{ |
319 |
VirtIOPCIProxy *proxy = opaque; |
320 |
VirtIODevice *vdev = proxy->vdev; |
321 |
target_phys_addr_t pa; |
322 |
|
323 |
switch (addr) {
|
324 |
case VIRTIO_PCI_GUEST_FEATURES:
|
325 |
/* Guest does not negotiate properly? We have to assume nothing. */
|
326 |
if (val & (1 << VIRTIO_F_BAD_FEATURE)) { |
327 |
if (vdev->bad_features)
|
328 |
val = proxy->host_features & vdev->bad_features(vdev); |
329 |
else
|
330 |
val = 0;
|
331 |
} |
332 |
if (vdev->set_features)
|
333 |
vdev->set_features(vdev, val); |
334 |
vdev->guest_features = val; |
335 |
break;
|
336 |
case VIRTIO_PCI_QUEUE_PFN:
|
337 |
pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
338 |
if (pa == 0) { |
339 |
virtio_pci_stop_ioeventfd(proxy); |
340 |
virtio_reset(proxy->vdev); |
341 |
msix_unuse_all_vectors(&proxy->pci_dev); |
342 |
} |
343 |
else
|
344 |
virtio_queue_set_addr(vdev, vdev->queue_sel, pa); |
345 |
break;
|
346 |
case VIRTIO_PCI_QUEUE_SEL:
|
347 |
if (val < VIRTIO_PCI_QUEUE_MAX)
|
348 |
vdev->queue_sel = val; |
349 |
break;
|
350 |
case VIRTIO_PCI_QUEUE_NOTIFY:
|
351 |
if (val < VIRTIO_PCI_QUEUE_MAX) {
|
352 |
virtio_queue_notify(vdev, val); |
353 |
} |
354 |
break;
|
355 |
case VIRTIO_PCI_STATUS:
|
356 |
if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
|
357 |
virtio_pci_stop_ioeventfd(proxy); |
358 |
} |
359 |
|
360 |
virtio_set_status(vdev, val & 0xFF);
|
361 |
|
362 |
if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
|
363 |
virtio_pci_start_ioeventfd(proxy); |
364 |
} |
365 |
|
366 |
if (vdev->status == 0) { |
367 |
virtio_reset(proxy->vdev); |
368 |
msix_unuse_all_vectors(&proxy->pci_dev); |
369 |
} |
370 |
|
371 |
/* Linux before 2.6.34 sets the device as OK without enabling
|
372 |
the PCI device bus master bit. In this case we need to disable
|
373 |
some safety checks. */
|
374 |
if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
|
375 |
!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { |
376 |
proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
377 |
} |
378 |
break;
|
379 |
case VIRTIO_MSI_CONFIG_VECTOR:
|
380 |
msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); |
381 |
/* Make it possible for guest to discover an error took place. */
|
382 |
if (msix_vector_use(&proxy->pci_dev, val) < 0) |
383 |
val = VIRTIO_NO_VECTOR; |
384 |
vdev->config_vector = val; |
385 |
break;
|
386 |
case VIRTIO_MSI_QUEUE_VECTOR:
|
387 |
msix_vector_unuse(&proxy->pci_dev, |
388 |
virtio_queue_vector(vdev, vdev->queue_sel)); |
389 |
/* Make it possible for guest to discover an error took place. */
|
390 |
if (msix_vector_use(&proxy->pci_dev, val) < 0) |
391 |
val = VIRTIO_NO_VECTOR; |
392 |
virtio_queue_set_vector(vdev, vdev->queue_sel, val); |
393 |
break;
|
394 |
default:
|
395 |
error_report("%s: unexpected address 0x%x value 0x%x",
|
396 |
__func__, addr, val); |
397 |
break;
|
398 |
} |
399 |
} |
400 |
|
401 |
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
|
402 |
{ |
403 |
VirtIODevice *vdev = proxy->vdev; |
404 |
uint32_t ret = 0xFFFFFFFF;
|
405 |
|
406 |
switch (addr) {
|
407 |
case VIRTIO_PCI_HOST_FEATURES:
|
408 |
ret = proxy->host_features; |
409 |
break;
|
410 |
case VIRTIO_PCI_GUEST_FEATURES:
|
411 |
ret = vdev->guest_features; |
412 |
break;
|
413 |
case VIRTIO_PCI_QUEUE_PFN:
|
414 |
ret = virtio_queue_get_addr(vdev, vdev->queue_sel) |
415 |
>> VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
416 |
break;
|
417 |
case VIRTIO_PCI_QUEUE_NUM:
|
418 |
ret = virtio_queue_get_num(vdev, vdev->queue_sel); |
419 |
break;
|
420 |
case VIRTIO_PCI_QUEUE_SEL:
|
421 |
ret = vdev->queue_sel; |
422 |
break;
|
423 |
case VIRTIO_PCI_STATUS:
|
424 |
ret = vdev->status; |
425 |
break;
|
426 |
case VIRTIO_PCI_ISR:
|
427 |
/* reading from the ISR also clears it. */
|
428 |
ret = vdev->isr; |
429 |
vdev->isr = 0;
|
430 |
qemu_set_irq(proxy->pci_dev.irq[0], 0); |
431 |
break;
|
432 |
case VIRTIO_MSI_CONFIG_VECTOR:
|
433 |
ret = vdev->config_vector; |
434 |
break;
|
435 |
case VIRTIO_MSI_QUEUE_VECTOR:
|
436 |
ret = virtio_queue_vector(vdev, vdev->queue_sel); |
437 |
break;
|
438 |
default:
|
439 |
break;
|
440 |
} |
441 |
|
442 |
return ret;
|
443 |
} |
444 |
|
445 |
static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr) |
446 |
{ |
447 |
VirtIOPCIProxy *proxy = opaque; |
448 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
449 |
addr -= proxy->addr; |
450 |
if (addr < config)
|
451 |
return virtio_ioport_read(proxy, addr);
|
452 |
addr -= config; |
453 |
return virtio_config_readb(proxy->vdev, addr);
|
454 |
} |
455 |
|
456 |
static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr) |
457 |
{ |
458 |
VirtIOPCIProxy *proxy = opaque; |
459 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
460 |
addr -= proxy->addr; |
461 |
if (addr < config)
|
462 |
return virtio_ioport_read(proxy, addr);
|
463 |
addr -= config; |
464 |
return virtio_config_readw(proxy->vdev, addr);
|
465 |
} |
466 |
|
467 |
static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr) |
468 |
{ |
469 |
VirtIOPCIProxy *proxy = opaque; |
470 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
471 |
addr -= proxy->addr; |
472 |
if (addr < config)
|
473 |
return virtio_ioport_read(proxy, addr);
|
474 |
addr -= config; |
475 |
return virtio_config_readl(proxy->vdev, addr);
|
476 |
} |
477 |
|
478 |
static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val) |
479 |
{ |
480 |
VirtIOPCIProxy *proxy = opaque; |
481 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
482 |
addr -= proxy->addr; |
483 |
if (addr < config) {
|
484 |
virtio_ioport_write(proxy, addr, val); |
485 |
return;
|
486 |
} |
487 |
addr -= config; |
488 |
virtio_config_writeb(proxy->vdev, addr, val); |
489 |
} |
490 |
|
491 |
static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val) |
492 |
{ |
493 |
VirtIOPCIProxy *proxy = opaque; |
494 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
495 |
addr -= proxy->addr; |
496 |
if (addr < config) {
|
497 |
virtio_ioport_write(proxy, addr, val); |
498 |
return;
|
499 |
} |
500 |
addr -= config; |
501 |
virtio_config_writew(proxy->vdev, addr, val); |
502 |
} |
503 |
|
504 |
static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val) |
505 |
{ |
506 |
VirtIOPCIProxy *proxy = opaque; |
507 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
508 |
addr -= proxy->addr; |
509 |
if (addr < config) {
|
510 |
virtio_ioport_write(proxy, addr, val); |
511 |
return;
|
512 |
} |
513 |
addr -= config; |
514 |
virtio_config_writel(proxy->vdev, addr, val); |
515 |
} |
516 |
|
517 |
static void virtio_map(PCIDevice *pci_dev, int region_num, |
518 |
pcibus_t addr, pcibus_t size, int type)
|
519 |
{ |
520 |
VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev); |
521 |
VirtIODevice *vdev = proxy->vdev; |
522 |
unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
|
523 |
|
524 |
proxy->addr = addr; |
525 |
|
526 |
register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
|
527 |
register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
|
528 |
register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
|
529 |
register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
|
530 |
register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
|
531 |
register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
|
532 |
|
533 |
if (vdev->config_len)
|
534 |
vdev->get_config(vdev, vdev->config); |
535 |
} |
536 |
|
537 |
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, |
538 |
uint32_t val, int len)
|
539 |
{ |
540 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
541 |
|
542 |
if (PCI_COMMAND == address) {
|
543 |
if (!(val & PCI_COMMAND_MASTER)) {
|
544 |
if (!(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
|
545 |
virtio_pci_stop_ioeventfd(proxy); |
546 |
virtio_set_status(proxy->vdev, |
547 |
proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); |
548 |
} |
549 |
} |
550 |
} |
551 |
|
552 |
pci_default_write_config(pci_dev, address, val, len); |
553 |
msix_write_config(pci_dev, address, val, len); |
554 |
} |
555 |
|
556 |
static unsigned virtio_pci_get_features(void *opaque) |
557 |
{ |
558 |
VirtIOPCIProxy *proxy = opaque; |
559 |
return proxy->host_features;
|
560 |
} |
561 |
|
562 |
static void virtio_pci_guest_notifier_read(void *opaque) |
563 |
{ |
564 |
VirtQueue *vq = opaque; |
565 |
EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
566 |
if (event_notifier_test_and_clear(n)) {
|
567 |
virtio_irq(vq); |
568 |
} |
569 |
} |
570 |
|
571 |
static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign) |
572 |
{ |
573 |
VirtIOPCIProxy *proxy = opaque; |
574 |
VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
575 |
EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
576 |
|
577 |
if (assign) {
|
578 |
int r = event_notifier_init(notifier, 0); |
579 |
if (r < 0) { |
580 |
return r;
|
581 |
} |
582 |
qemu_set_fd_handler(event_notifier_get_fd(notifier), |
583 |
virtio_pci_guest_notifier_read, NULL, vq);
|
584 |
} else {
|
585 |
qemu_set_fd_handler(event_notifier_get_fd(notifier), |
586 |
NULL, NULL, NULL); |
587 |
event_notifier_cleanup(notifier); |
588 |
} |
589 |
|
590 |
return 0; |
591 |
} |
592 |
|
593 |
static bool virtio_pci_query_guest_notifiers(void *opaque) |
594 |
{ |
595 |
VirtIOPCIProxy *proxy = opaque; |
596 |
return msix_enabled(&proxy->pci_dev);
|
597 |
} |
598 |
|
599 |
static int virtio_pci_set_guest_notifiers(void *opaque, bool assign) |
600 |
{ |
601 |
VirtIOPCIProxy *proxy = opaque; |
602 |
VirtIODevice *vdev = proxy->vdev; |
603 |
int r, n;
|
604 |
|
605 |
for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { |
606 |
if (!virtio_queue_get_num(vdev, n)) {
|
607 |
break;
|
608 |
} |
609 |
|
610 |
r = virtio_pci_set_guest_notifier(opaque, n, assign); |
611 |
if (r < 0) { |
612 |
goto assign_error;
|
613 |
} |
614 |
} |
615 |
|
616 |
return 0; |
617 |
|
618 |
assign_error:
|
619 |
/* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
|
620 |
while (--n >= 0) { |
621 |
virtio_pci_set_guest_notifier(opaque, n, !assign); |
622 |
} |
623 |
return r;
|
624 |
} |
625 |
|
626 |
static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign) |
627 |
{ |
628 |
VirtIOPCIProxy *proxy = opaque; |
629 |
|
630 |
/* Stop using ioeventfd for virtqueue kick if the device starts using host
|
631 |
* notifiers. This makes it easy to avoid stepping on each others' toes.
|
632 |
*/
|
633 |
proxy->ioeventfd_disabled = assign; |
634 |
if (assign) {
|
635 |
virtio_pci_stop_ioeventfd(proxy); |
636 |
} |
637 |
/* We don't need to start here: it's not needed because backend
|
638 |
* currently only stops on status change away from ok,
|
639 |
* reset, vmstop and such. If we do add code to start here,
|
640 |
* need to check vmstate, device state etc. */
|
641 |
return virtio_pci_set_host_notifier_internal(proxy, n, assign);
|
642 |
} |
643 |
|
644 |
static void virtio_pci_vmstate_change(void *opaque, bool running) |
645 |
{ |
646 |
VirtIOPCIProxy *proxy = opaque; |
647 |
|
648 |
if (running) {
|
649 |
/* Try to find out if the guest has bus master disabled, but is
|
650 |
in ready state. Then we have a buggy guest OS. */
|
651 |
if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
|
652 |
!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { |
653 |
proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
654 |
} |
655 |
virtio_pci_start_ioeventfd(proxy); |
656 |
} else {
|
657 |
virtio_pci_stop_ioeventfd(proxy); |
658 |
} |
659 |
} |
660 |
|
661 |
static const VirtIOBindings virtio_pci_bindings = { |
662 |
.notify = virtio_pci_notify, |
663 |
.save_config = virtio_pci_save_config, |
664 |
.load_config = virtio_pci_load_config, |
665 |
.save_queue = virtio_pci_save_queue, |
666 |
.load_queue = virtio_pci_load_queue, |
667 |
.get_features = virtio_pci_get_features, |
668 |
.query_guest_notifiers = virtio_pci_query_guest_notifiers, |
669 |
.set_host_notifier = virtio_pci_set_host_notifier, |
670 |
.set_guest_notifiers = virtio_pci_set_guest_notifiers, |
671 |
.vmstate_change = virtio_pci_vmstate_change, |
672 |
}; |
673 |
|
674 |
static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev) |
675 |
{ |
676 |
uint8_t *config; |
677 |
uint32_t size; |
678 |
|
679 |
proxy->vdev = vdev; |
680 |
|
681 |
config = proxy->pci_dev.config; |
682 |
|
683 |
if (proxy->class_code) {
|
684 |
pci_config_set_class(config, proxy->class_code); |
685 |
} |
686 |
pci_set_word(config + 0x2c, pci_get_word(config + PCI_VENDOR_ID));
|
687 |
pci_set_word(config + 0x2e, vdev->device_id);
|
688 |
config[0x3d] = 1; |
689 |
|
690 |
if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) { |
691 |
pci_register_bar(&proxy->pci_dev, 1,
|
692 |
msix_bar_size(&proxy->pci_dev), |
693 |
PCI_BASE_ADDRESS_SPACE_MEMORY, |
694 |
msix_mmio_map); |
695 |
} else
|
696 |
vdev->nvectors = 0;
|
697 |
|
698 |
proxy->pci_dev.config_write = virtio_write_config; |
699 |
|
700 |
size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len; |
701 |
if (size & (size-1)) |
702 |
size = 1 << qemu_fls(size);
|
703 |
|
704 |
pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
|
705 |
virtio_map); |
706 |
|
707 |
if (!kvm_has_many_ioeventfds()) {
|
708 |
proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; |
709 |
} |
710 |
|
711 |
virtio_bind_device(vdev, &virtio_pci_bindings, proxy); |
712 |
proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
|
713 |
proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
|
714 |
proxy->host_features = vdev->get_features(vdev, proxy->host_features); |
715 |
} |
716 |
|
717 |
static int virtio_blk_init_pci(PCIDevice *pci_dev) |
718 |
{ |
719 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
720 |
VirtIODevice *vdev; |
721 |
|
722 |
if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
|
723 |
proxy->class_code != PCI_CLASS_STORAGE_OTHER) |
724 |
proxy->class_code = PCI_CLASS_STORAGE_SCSI; |
725 |
|
726 |
vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block); |
727 |
if (!vdev) {
|
728 |
return -1; |
729 |
} |
730 |
vdev->nvectors = proxy->nvectors; |
731 |
virtio_init_pci(proxy, vdev); |
732 |
/* make the actual value visible */
|
733 |
proxy->nvectors = vdev->nvectors; |
734 |
return 0; |
735 |
} |
736 |
|
737 |
static int virtio_exit_pci(PCIDevice *pci_dev) |
738 |
{ |
739 |
return msix_uninit(pci_dev);
|
740 |
} |
741 |
|
742 |
static int virtio_blk_exit_pci(PCIDevice *pci_dev) |
743 |
{ |
744 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
745 |
|
746 |
virtio_pci_stop_ioeventfd(proxy); |
747 |
virtio_blk_exit(proxy->vdev); |
748 |
blockdev_mark_auto_del(proxy->block.bs); |
749 |
return virtio_exit_pci(pci_dev);
|
750 |
} |
751 |
|
752 |
static int virtio_serial_init_pci(PCIDevice *pci_dev) |
753 |
{ |
754 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
755 |
VirtIODevice *vdev; |
756 |
|
757 |
if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
|
758 |
proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
|
759 |
proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
|
760 |
proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER; |
761 |
|
762 |
vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial); |
763 |
if (!vdev) {
|
764 |
return -1; |
765 |
} |
766 |
vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED |
767 |
? proxy->serial.max_virtserial_ports + 1
|
768 |
: proxy->nvectors; |
769 |
virtio_init_pci(proxy, vdev); |
770 |
proxy->nvectors = vdev->nvectors; |
771 |
return 0; |
772 |
} |
773 |
|
774 |
static int virtio_serial_exit_pci(PCIDevice *pci_dev) |
775 |
{ |
776 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
777 |
|
778 |
virtio_pci_stop_ioeventfd(proxy); |
779 |
virtio_serial_exit(proxy->vdev); |
780 |
return virtio_exit_pci(pci_dev);
|
781 |
} |
782 |
|
783 |
static int virtio_net_init_pci(PCIDevice *pci_dev) |
784 |
{ |
785 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
786 |
VirtIODevice *vdev; |
787 |
|
788 |
vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net); |
789 |
|
790 |
vdev->nvectors = proxy->nvectors; |
791 |
virtio_init_pci(proxy, vdev); |
792 |
|
793 |
/* make the actual value visible */
|
794 |
proxy->nvectors = vdev->nvectors; |
795 |
return 0; |
796 |
} |
797 |
|
798 |
static int virtio_net_exit_pci(PCIDevice *pci_dev) |
799 |
{ |
800 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
801 |
|
802 |
virtio_pci_stop_ioeventfd(proxy); |
803 |
virtio_net_exit(proxy->vdev); |
804 |
return virtio_exit_pci(pci_dev);
|
805 |
} |
806 |
|
807 |
static int virtio_balloon_init_pci(PCIDevice *pci_dev) |
808 |
{ |
809 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
810 |
VirtIODevice *vdev; |
811 |
|
812 |
vdev = virtio_balloon_init(&pci_dev->qdev); |
813 |
virtio_init_pci(proxy, vdev); |
814 |
return 0; |
815 |
} |
816 |
|
817 |
#ifdef CONFIG_VIRTFS
|
818 |
static int virtio_9p_init_pci(PCIDevice *pci_dev) |
819 |
{ |
820 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
821 |
VirtIODevice *vdev; |
822 |
|
823 |
vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf); |
824 |
vdev->nvectors = proxy->nvectors; |
825 |
virtio_init_pci(proxy, vdev); |
826 |
/* make the actual value visible */
|
827 |
proxy->nvectors = vdev->nvectors; |
828 |
return 0; |
829 |
} |
830 |
#endif
|
831 |
|
832 |
static PCIDeviceInfo virtio_info[] = {
|
833 |
{ |
834 |
.qdev.name = "virtio-blk-pci",
|
835 |
.qdev.alias = "virtio-blk",
|
836 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
837 |
.init = virtio_blk_init_pci, |
838 |
.exit = virtio_blk_exit_pci, |
839 |
.vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
840 |
.device_id = PCI_DEVICE_ID_VIRTIO_BLOCK, |
841 |
.revision = VIRTIO_PCI_ABI_VERSION, |
842 |
.class_id = PCI_CLASS_STORAGE_SCSI, |
843 |
.qdev.props = (Property[]) { |
844 |
DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
845 |
DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block), |
846 |
DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
|
847 |
VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
|
848 |
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
849 |
DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features), |
850 |
DEFINE_PROP_END_OF_LIST(), |
851 |
}, |
852 |
.qdev.reset = virtio_pci_reset, |
853 |
},{ |
854 |
.qdev.name = "virtio-net-pci",
|
855 |
.qdev.alias = "virtio-net",
|
856 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
857 |
.init = virtio_net_init_pci, |
858 |
.exit = virtio_net_exit_pci, |
859 |
.romfile = "pxe-virtio.rom",
|
860 |
.vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
861 |
.device_id = PCI_DEVICE_ID_VIRTIO_NET, |
862 |
.revision = VIRTIO_PCI_ABI_VERSION, |
863 |
.class_id = PCI_CLASS_NETWORK_ETHERNET, |
864 |
.qdev.props = (Property[]) { |
865 |
DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
|
866 |
VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
|
867 |
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), |
868 |
DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), |
869 |
DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic), |
870 |
DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
|
871 |
net.txtimer, TX_TIMER_INTERVAL), |
872 |
DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
|
873 |
net.txburst, TX_BURST), |
874 |
DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
|
875 |
DEFINE_PROP_END_OF_LIST(), |
876 |
}, |
877 |
.qdev.reset = virtio_pci_reset, |
878 |
},{ |
879 |
.qdev.name = "virtio-serial-pci",
|
880 |
.qdev.alias = "virtio-serial",
|
881 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
882 |
.init = virtio_serial_init_pci, |
883 |
.exit = virtio_serial_exit_pci, |
884 |
.vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
885 |
.device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE, |
886 |
.revision = VIRTIO_PCI_ABI_VERSION, |
887 |
.class_id = PCI_CLASS_COMMUNICATION_OTHER, |
888 |
.qdev.props = (Property[]) { |
889 |
DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
|
890 |
VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
|
891 |
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
|
892 |
DEV_NVECTORS_UNSPECIFIED), |
893 |
DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
894 |
DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
895 |
DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
|
896 |
serial.max_virtserial_ports, 31),
|
897 |
DEFINE_PROP_END_OF_LIST(), |
898 |
}, |
899 |
.qdev.reset = virtio_pci_reset, |
900 |
},{ |
901 |
.qdev.name = "virtio-balloon-pci",
|
902 |
.qdev.alias = "virtio-balloon",
|
903 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
904 |
.init = virtio_balloon_init_pci, |
905 |
.exit = virtio_exit_pci, |
906 |
.vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
907 |
.device_id = PCI_DEVICE_ID_VIRTIO_BALLOON, |
908 |
.revision = VIRTIO_PCI_ABI_VERSION, |
909 |
.class_id = PCI_CLASS_MEMORY_RAM, |
910 |
.qdev.props = (Property[]) { |
911 |
DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
912 |
DEFINE_PROP_END_OF_LIST(), |
913 |
}, |
914 |
.qdev.reset = virtio_pci_reset, |
915 |
},{ |
916 |
#ifdef CONFIG_VIRTFS
|
917 |
.qdev.name = "virtio-9p-pci",
|
918 |
.qdev.alias = "virtio-9p",
|
919 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
920 |
.init = virtio_9p_init_pci, |
921 |
.vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
922 |
.device_id = 0x1009,
|
923 |
.revision = VIRTIO_PCI_ABI_VERSION, |
924 |
.class_id = 0x2,
|
925 |
.qdev.props = (Property[]) { |
926 |
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
927 |
DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
928 |
DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
|
929 |
DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
|
930 |
DEFINE_PROP_END_OF_LIST(), |
931 |
}, |
932 |
}, { |
933 |
#endif
|
934 |
/* end of list */
|
935 |
} |
936 |
}; |
937 |
|
938 |
static void virtio_pci_register_devices(void) |
939 |
{ |
940 |
pci_qdev_register_many(virtio_info); |
941 |
} |
942 |
|
943 |
device_init(virtio_pci_register_devices) |