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