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