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