root / hw / virtio-pci.c @ 7d932dfd
History | View | Annotate | Download (23.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 "pci.h" |
22 |
#include "qemu-error.h" |
23 |
#include "msix.h" |
24 |
#include "net.h" |
25 |
#include "block_int.h" |
26 |
#include "loader.h" |
27 |
#include "kvm.h" |
28 |
|
29 |
/* from Linux's linux/virtio_pci.h */
|
30 |
|
31 |
/* A 32-bit r/o bitmask of the features supported by the host */
|
32 |
#define VIRTIO_PCI_HOST_FEATURES 0 |
33 |
|
34 |
/* A 32-bit r/w bitmask of features activated by the guest */
|
35 |
#define VIRTIO_PCI_GUEST_FEATURES 4 |
36 |
|
37 |
/* A 32-bit r/w PFN for the currently selected queue */
|
38 |
#define VIRTIO_PCI_QUEUE_PFN 8 |
39 |
|
40 |
/* A 16-bit r/o queue size for the currently selected queue */
|
41 |
#define VIRTIO_PCI_QUEUE_NUM 12 |
42 |
|
43 |
/* A 16-bit r/w queue selector */
|
44 |
#define VIRTIO_PCI_QUEUE_SEL 14 |
45 |
|
46 |
/* A 16-bit r/w queue notifier */
|
47 |
#define VIRTIO_PCI_QUEUE_NOTIFY 16 |
48 |
|
49 |
/* An 8-bit device status register. */
|
50 |
#define VIRTIO_PCI_STATUS 18 |
51 |
|
52 |
/* An 8-bit r/o interrupt status register. Reading the value will return the
|
53 |
* current contents of the ISR and will also clear it. This is effectively
|
54 |
* a read-and-acknowledge. */
|
55 |
#define VIRTIO_PCI_ISR 19 |
56 |
|
57 |
/* MSI-X registers: only enabled if MSI-X is enabled. */
|
58 |
/* A 16-bit vector for configuration changes. */
|
59 |
#define VIRTIO_MSI_CONFIG_VECTOR 20 |
60 |
/* A 16-bit vector for selected queue notifications. */
|
61 |
#define VIRTIO_MSI_QUEUE_VECTOR 22 |
62 |
|
63 |
/* Config space size */
|
64 |
#define VIRTIO_PCI_CONFIG_NOMSI 20 |
65 |
#define VIRTIO_PCI_CONFIG_MSI 24 |
66 |
#define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
|
67 |
VIRTIO_PCI_CONFIG_MSI : \ |
68 |
VIRTIO_PCI_CONFIG_NOMSI) |
69 |
|
70 |
/* The remaining space is defined by each driver as the per-driver
|
71 |
* configuration space */
|
72 |
#define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
|
73 |
VIRTIO_PCI_CONFIG_MSI : \ |
74 |
VIRTIO_PCI_CONFIG_NOMSI) |
75 |
|
76 |
/* Virtio ABI version, if we increment this, we break the guest driver. */
|
77 |
#define VIRTIO_PCI_ABI_VERSION 0 |
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 |
/* We can catch some guest bugs inside here so we continue supporting older
|
84 |
guests. */
|
85 |
#define VIRTIO_PCI_BUG_BUS_MASTER (1 << 0) |
86 |
|
87 |
/* QEMU doesn't strictly need write barriers since everything runs in
|
88 |
* lock-step. We'll leave the calls to wmb() in though to make it obvious for
|
89 |
* KVM or if kqemu gets SMP support.
|
90 |
*/
|
91 |
#define wmb() do { } while (0) |
92 |
|
93 |
/* PCI bindings. */
|
94 |
|
95 |
typedef struct { |
96 |
PCIDevice pci_dev; |
97 |
VirtIODevice *vdev; |
98 |
uint32_t bugs; |
99 |
uint32_t addr; |
100 |
uint32_t class_code; |
101 |
uint32_t nvectors; |
102 |
BlockConf block; |
103 |
NICConf nic; |
104 |
uint32_t host_features; |
105 |
#ifdef CONFIG_LINUX
|
106 |
V9fsConf fsconf; |
107 |
#endif
|
108 |
/* Max. number of ports we can have for a the virtio-serial device */
|
109 |
uint32_t max_virtserial_ports; |
110 |
} VirtIOPCIProxy; |
111 |
|
112 |
/* virtio device */
|
113 |
|
114 |
static void virtio_pci_notify(void *opaque, uint16_t vector) |
115 |
{ |
116 |
VirtIOPCIProxy *proxy = opaque; |
117 |
if (msix_enabled(&proxy->pci_dev))
|
118 |
msix_notify(&proxy->pci_dev, vector); |
119 |
else
|
120 |
qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1); |
121 |
} |
122 |
|
123 |
static void virtio_pci_save_config(void * opaque, QEMUFile *f) |
124 |
{ |
125 |
VirtIOPCIProxy *proxy = opaque; |
126 |
pci_device_save(&proxy->pci_dev, f); |
127 |
msix_save(&proxy->pci_dev, f); |
128 |
if (msix_present(&proxy->pci_dev))
|
129 |
qemu_put_be16(f, proxy->vdev->config_vector); |
130 |
} |
131 |
|
132 |
static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f) |
133 |
{ |
134 |
VirtIOPCIProxy *proxy = opaque; |
135 |
if (msix_present(&proxy->pci_dev))
|
136 |
qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n)); |
137 |
} |
138 |
|
139 |
static int virtio_pci_load_config(void * opaque, QEMUFile *f) |
140 |
{ |
141 |
VirtIOPCIProxy *proxy = opaque; |
142 |
int ret;
|
143 |
ret = pci_device_load(&proxy->pci_dev, f); |
144 |
if (ret) {
|
145 |
return ret;
|
146 |
} |
147 |
msix_load(&proxy->pci_dev, f); |
148 |
if (msix_present(&proxy->pci_dev)) {
|
149 |
qemu_get_be16s(f, &proxy->vdev->config_vector); |
150 |
} else {
|
151 |
proxy->vdev->config_vector = VIRTIO_NO_VECTOR; |
152 |
} |
153 |
if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
|
154 |
return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
|
155 |
} |
156 |
|
157 |
/* Try to find out if the guest has bus master disabled, but is
|
158 |
in ready state. Then we have a buggy guest OS. */
|
159 |
if (!(proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
|
160 |
!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { |
161 |
proxy->bugs |= VIRTIO_PCI_BUG_BUS_MASTER; |
162 |
} |
163 |
return 0; |
164 |
} |
165 |
|
166 |
static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f) |
167 |
{ |
168 |
VirtIOPCIProxy *proxy = opaque; |
169 |
uint16_t vector; |
170 |
if (msix_present(&proxy->pci_dev)) {
|
171 |
qemu_get_be16s(f, &vector); |
172 |
} else {
|
173 |
vector = VIRTIO_NO_VECTOR; |
174 |
} |
175 |
virtio_queue_set_vector(proxy->vdev, n, vector); |
176 |
if (vector != VIRTIO_NO_VECTOR) {
|
177 |
return msix_vector_use(&proxy->pci_dev, vector);
|
178 |
} |
179 |
return 0; |
180 |
} |
181 |
|
182 |
static void virtio_pci_reset(DeviceState *d) |
183 |
{ |
184 |
VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev); |
185 |
virtio_reset(proxy->vdev); |
186 |
msix_reset(&proxy->pci_dev); |
187 |
proxy->bugs = 0;
|
188 |
} |
189 |
|
190 |
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
191 |
{ |
192 |
VirtIOPCIProxy *proxy = opaque; |
193 |
VirtIODevice *vdev = proxy->vdev; |
194 |
target_phys_addr_t pa; |
195 |
|
196 |
switch (addr) {
|
197 |
case VIRTIO_PCI_GUEST_FEATURES:
|
198 |
/* Guest does not negotiate properly? We have to assume nothing. */
|
199 |
if (val & (1 << VIRTIO_F_BAD_FEATURE)) { |
200 |
if (vdev->bad_features)
|
201 |
val = proxy->host_features & vdev->bad_features(vdev); |
202 |
else
|
203 |
val = 0;
|
204 |
} |
205 |
if (vdev->set_features)
|
206 |
vdev->set_features(vdev, val); |
207 |
vdev->guest_features = val; |
208 |
break;
|
209 |
case VIRTIO_PCI_QUEUE_PFN:
|
210 |
pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
211 |
if (pa == 0) { |
212 |
virtio_reset(proxy->vdev); |
213 |
msix_unuse_all_vectors(&proxy->pci_dev); |
214 |
} |
215 |
else
|
216 |
virtio_queue_set_addr(vdev, vdev->queue_sel, pa); |
217 |
break;
|
218 |
case VIRTIO_PCI_QUEUE_SEL:
|
219 |
if (val < VIRTIO_PCI_QUEUE_MAX)
|
220 |
vdev->queue_sel = val; |
221 |
break;
|
222 |
case VIRTIO_PCI_QUEUE_NOTIFY:
|
223 |
virtio_queue_notify(vdev, val); |
224 |
break;
|
225 |
case VIRTIO_PCI_STATUS:
|
226 |
virtio_set_status(vdev, val & 0xFF);
|
227 |
if (vdev->status == 0) { |
228 |
virtio_reset(proxy->vdev); |
229 |
msix_unuse_all_vectors(&proxy->pci_dev); |
230 |
} |
231 |
|
232 |
/* Linux before 2.6.34 sets the device as OK without enabling
|
233 |
the PCI device bus master bit. In this case we need to disable
|
234 |
some safety checks. */
|
235 |
if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
|
236 |
!(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { |
237 |
proxy->bugs |= VIRTIO_PCI_BUG_BUS_MASTER; |
238 |
} |
239 |
break;
|
240 |
case VIRTIO_MSI_CONFIG_VECTOR:
|
241 |
msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); |
242 |
/* Make it possible for guest to discover an error took place. */
|
243 |
if (msix_vector_use(&proxy->pci_dev, val) < 0) |
244 |
val = VIRTIO_NO_VECTOR; |
245 |
vdev->config_vector = val; |
246 |
break;
|
247 |
case VIRTIO_MSI_QUEUE_VECTOR:
|
248 |
msix_vector_unuse(&proxy->pci_dev, |
249 |
virtio_queue_vector(vdev, vdev->queue_sel)); |
250 |
/* Make it possible for guest to discover an error took place. */
|
251 |
if (msix_vector_use(&proxy->pci_dev, val) < 0) |
252 |
val = VIRTIO_NO_VECTOR; |
253 |
virtio_queue_set_vector(vdev, vdev->queue_sel, val); |
254 |
break;
|
255 |
default:
|
256 |
fprintf(stderr, "%s: unexpected address 0x%x value 0x%x\n",
|
257 |
__func__, addr, val); |
258 |
break;
|
259 |
} |
260 |
} |
261 |
|
262 |
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
|
263 |
{ |
264 |
VirtIODevice *vdev = proxy->vdev; |
265 |
uint32_t ret = 0xFFFFFFFF;
|
266 |
|
267 |
switch (addr) {
|
268 |
case VIRTIO_PCI_HOST_FEATURES:
|
269 |
ret = proxy->host_features; |
270 |
break;
|
271 |
case VIRTIO_PCI_GUEST_FEATURES:
|
272 |
ret = vdev->guest_features; |
273 |
break;
|
274 |
case VIRTIO_PCI_QUEUE_PFN:
|
275 |
ret = virtio_queue_get_addr(vdev, vdev->queue_sel) |
276 |
>> VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
277 |
break;
|
278 |
case VIRTIO_PCI_QUEUE_NUM:
|
279 |
ret = virtio_queue_get_num(vdev, vdev->queue_sel); |
280 |
break;
|
281 |
case VIRTIO_PCI_QUEUE_SEL:
|
282 |
ret = vdev->queue_sel; |
283 |
break;
|
284 |
case VIRTIO_PCI_STATUS:
|
285 |
ret = vdev->status; |
286 |
break;
|
287 |
case VIRTIO_PCI_ISR:
|
288 |
/* reading from the ISR also clears it. */
|
289 |
ret = vdev->isr; |
290 |
vdev->isr = 0;
|
291 |
qemu_set_irq(proxy->pci_dev.irq[0], 0); |
292 |
break;
|
293 |
case VIRTIO_MSI_CONFIG_VECTOR:
|
294 |
ret = vdev->config_vector; |
295 |
break;
|
296 |
case VIRTIO_MSI_QUEUE_VECTOR:
|
297 |
ret = virtio_queue_vector(vdev, vdev->queue_sel); |
298 |
break;
|
299 |
default:
|
300 |
break;
|
301 |
} |
302 |
|
303 |
return ret;
|
304 |
} |
305 |
|
306 |
static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr) |
307 |
{ |
308 |
VirtIOPCIProxy *proxy = opaque; |
309 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
310 |
addr -= proxy->addr; |
311 |
if (addr < config)
|
312 |
return virtio_ioport_read(proxy, addr);
|
313 |
addr -= config; |
314 |
return virtio_config_readb(proxy->vdev, addr);
|
315 |
} |
316 |
|
317 |
static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr) |
318 |
{ |
319 |
VirtIOPCIProxy *proxy = opaque; |
320 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
321 |
addr -= proxy->addr; |
322 |
if (addr < config)
|
323 |
return virtio_ioport_read(proxy, addr);
|
324 |
addr -= config; |
325 |
return virtio_config_readw(proxy->vdev, addr);
|
326 |
} |
327 |
|
328 |
static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr) |
329 |
{ |
330 |
VirtIOPCIProxy *proxy = opaque; |
331 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
332 |
addr -= proxy->addr; |
333 |
if (addr < config)
|
334 |
return virtio_ioport_read(proxy, addr);
|
335 |
addr -= config; |
336 |
return virtio_config_readl(proxy->vdev, addr);
|
337 |
} |
338 |
|
339 |
static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val) |
340 |
{ |
341 |
VirtIOPCIProxy *proxy = opaque; |
342 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
343 |
addr -= proxy->addr; |
344 |
if (addr < config) {
|
345 |
virtio_ioport_write(proxy, addr, val); |
346 |
return;
|
347 |
} |
348 |
addr -= config; |
349 |
virtio_config_writeb(proxy->vdev, addr, val); |
350 |
} |
351 |
|
352 |
static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val) |
353 |
{ |
354 |
VirtIOPCIProxy *proxy = opaque; |
355 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
356 |
addr -= proxy->addr; |
357 |
if (addr < config) {
|
358 |
virtio_ioport_write(proxy, addr, val); |
359 |
return;
|
360 |
} |
361 |
addr -= config; |
362 |
virtio_config_writew(proxy->vdev, addr, val); |
363 |
} |
364 |
|
365 |
static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val) |
366 |
{ |
367 |
VirtIOPCIProxy *proxy = opaque; |
368 |
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
369 |
addr -= proxy->addr; |
370 |
if (addr < config) {
|
371 |
virtio_ioport_write(proxy, addr, val); |
372 |
return;
|
373 |
} |
374 |
addr -= config; |
375 |
virtio_config_writel(proxy->vdev, addr, val); |
376 |
} |
377 |
|
378 |
static void virtio_map(PCIDevice *pci_dev, int region_num, |
379 |
pcibus_t addr, pcibus_t size, int type)
|
380 |
{ |
381 |
VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev); |
382 |
VirtIODevice *vdev = proxy->vdev; |
383 |
unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
|
384 |
|
385 |
proxy->addr = addr; |
386 |
|
387 |
register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
|
388 |
register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
|
389 |
register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
|
390 |
register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
|
391 |
register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
|
392 |
register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
|
393 |
|
394 |
if (vdev->config_len)
|
395 |
vdev->get_config(vdev, vdev->config); |
396 |
} |
397 |
|
398 |
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, |
399 |
uint32_t val, int len)
|
400 |
{ |
401 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
402 |
|
403 |
if (PCI_COMMAND == address) {
|
404 |
if (!(val & PCI_COMMAND_MASTER)) {
|
405 |
if (!(proxy->bugs & VIRTIO_PCI_BUG_BUS_MASTER)) {
|
406 |
virtio_set_status(proxy->vdev, |
407 |
proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); |
408 |
} |
409 |
} |
410 |
} |
411 |
|
412 |
pci_default_write_config(pci_dev, address, val, len); |
413 |
msix_write_config(pci_dev, address, val, len); |
414 |
} |
415 |
|
416 |
static unsigned virtio_pci_get_features(void *opaque) |
417 |
{ |
418 |
VirtIOPCIProxy *proxy = opaque; |
419 |
return proxy->host_features;
|
420 |
} |
421 |
|
422 |
static void virtio_pci_guest_notifier_read(void *opaque) |
423 |
{ |
424 |
VirtQueue *vq = opaque; |
425 |
EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
426 |
if (event_notifier_test_and_clear(n)) {
|
427 |
virtio_irq(vq); |
428 |
} |
429 |
} |
430 |
|
431 |
static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign) |
432 |
{ |
433 |
VirtIOPCIProxy *proxy = opaque; |
434 |
VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
435 |
EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
436 |
|
437 |
if (assign) {
|
438 |
int r = event_notifier_init(notifier, 0); |
439 |
if (r < 0) { |
440 |
return r;
|
441 |
} |
442 |
qemu_set_fd_handler(event_notifier_get_fd(notifier), |
443 |
virtio_pci_guest_notifier_read, NULL, vq);
|
444 |
} else {
|
445 |
qemu_set_fd_handler(event_notifier_get_fd(notifier), |
446 |
NULL, NULL, NULL); |
447 |
event_notifier_cleanup(notifier); |
448 |
} |
449 |
|
450 |
return 0; |
451 |
} |
452 |
|
453 |
static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign) |
454 |
{ |
455 |
VirtIOPCIProxy *proxy = opaque; |
456 |
VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
457 |
EventNotifier *notifier = virtio_queue_get_host_notifier(vq); |
458 |
int r;
|
459 |
if (assign) {
|
460 |
r = event_notifier_init(notifier, 1);
|
461 |
if (r < 0) { |
462 |
return r;
|
463 |
} |
464 |
r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier), |
465 |
proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY, |
466 |
n, assign); |
467 |
if (r < 0) { |
468 |
event_notifier_cleanup(notifier); |
469 |
} |
470 |
} else {
|
471 |
r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier), |
472 |
proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY, |
473 |
n, assign); |
474 |
if (r < 0) { |
475 |
return r;
|
476 |
} |
477 |
event_notifier_cleanup(notifier); |
478 |
} |
479 |
return r;
|
480 |
} |
481 |
|
482 |
static const VirtIOBindings virtio_pci_bindings = { |
483 |
.notify = virtio_pci_notify, |
484 |
.save_config = virtio_pci_save_config, |
485 |
.load_config = virtio_pci_load_config, |
486 |
.save_queue = virtio_pci_save_queue, |
487 |
.load_queue = virtio_pci_load_queue, |
488 |
.get_features = virtio_pci_get_features, |
489 |
.set_host_notifier = virtio_pci_set_host_notifier, |
490 |
.set_guest_notifier = virtio_pci_set_guest_notifier, |
491 |
}; |
492 |
|
493 |
static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev, |
494 |
uint16_t vendor, uint16_t device, |
495 |
uint16_t class_code, uint8_t pif) |
496 |
{ |
497 |
uint8_t *config; |
498 |
uint32_t size; |
499 |
|
500 |
proxy->vdev = vdev; |
501 |
|
502 |
config = proxy->pci_dev.config; |
503 |
pci_config_set_vendor_id(config, vendor); |
504 |
pci_config_set_device_id(config, device); |
505 |
|
506 |
config[0x08] = VIRTIO_PCI_ABI_VERSION;
|
507 |
|
508 |
config[0x09] = pif;
|
509 |
pci_config_set_class(config, class_code); |
510 |
config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; |
511 |
|
512 |
config[0x2c] = vendor & 0xFF; |
513 |
config[0x2d] = (vendor >> 8) & 0xFF; |
514 |
config[0x2e] = vdev->device_id & 0xFF; |
515 |
config[0x2f] = (vdev->device_id >> 8) & 0xFF; |
516 |
|
517 |
config[0x3d] = 1; |
518 |
|
519 |
if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) { |
520 |
pci_register_bar(&proxy->pci_dev, 1,
|
521 |
msix_bar_size(&proxy->pci_dev), |
522 |
PCI_BASE_ADDRESS_SPACE_MEMORY, |
523 |
msix_mmio_map); |
524 |
} else
|
525 |
vdev->nvectors = 0;
|
526 |
|
527 |
proxy->pci_dev.config_write = virtio_write_config; |
528 |
|
529 |
size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len; |
530 |
if (size & (size-1)) |
531 |
size = 1 << qemu_fls(size);
|
532 |
|
533 |
pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
|
534 |
virtio_map); |
535 |
|
536 |
virtio_bind_device(vdev, &virtio_pci_bindings, proxy); |
537 |
proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
|
538 |
proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
|
539 |
proxy->host_features = vdev->get_features(vdev, proxy->host_features); |
540 |
} |
541 |
|
542 |
static int virtio_blk_init_pci(PCIDevice *pci_dev) |
543 |
{ |
544 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
545 |
VirtIODevice *vdev; |
546 |
|
547 |
if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
|
548 |
proxy->class_code != PCI_CLASS_STORAGE_OTHER) |
549 |
proxy->class_code = PCI_CLASS_STORAGE_SCSI; |
550 |
|
551 |
if (!proxy->block.dinfo) {
|
552 |
error_report("virtio-blk-pci: drive property not set");
|
553 |
return -1; |
554 |
} |
555 |
vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block); |
556 |
vdev->nvectors = proxy->nvectors; |
557 |
virtio_init_pci(proxy, vdev, |
558 |
PCI_VENDOR_ID_REDHAT_QUMRANET, |
559 |
PCI_DEVICE_ID_VIRTIO_BLOCK, |
560 |
proxy->class_code, 0x00);
|
561 |
/* make the actual value visible */
|
562 |
proxy->nvectors = vdev->nvectors; |
563 |
return 0; |
564 |
} |
565 |
|
566 |
static int virtio_exit_pci(PCIDevice *pci_dev) |
567 |
{ |
568 |
return msix_uninit(pci_dev);
|
569 |
} |
570 |
|
571 |
static int virtio_blk_exit_pci(PCIDevice *pci_dev) |
572 |
{ |
573 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
574 |
|
575 |
drive_uninit(proxy->block.dinfo); |
576 |
return virtio_exit_pci(pci_dev);
|
577 |
} |
578 |
|
579 |
static int virtio_serial_init_pci(PCIDevice *pci_dev) |
580 |
{ |
581 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
582 |
VirtIODevice *vdev; |
583 |
|
584 |
if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
|
585 |
proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
|
586 |
proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
|
587 |
proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER; |
588 |
|
589 |
vdev = virtio_serial_init(&pci_dev->qdev, proxy->max_virtserial_ports); |
590 |
if (!vdev) {
|
591 |
return -1; |
592 |
} |
593 |
vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED |
594 |
? proxy->max_virtserial_ports + 1
|
595 |
: proxy->nvectors; |
596 |
virtio_init_pci(proxy, vdev, |
597 |
PCI_VENDOR_ID_REDHAT_QUMRANET, |
598 |
PCI_DEVICE_ID_VIRTIO_CONSOLE, |
599 |
proxy->class_code, 0x00);
|
600 |
proxy->nvectors = vdev->nvectors; |
601 |
return 0; |
602 |
} |
603 |
|
604 |
static int virtio_net_init_pci(PCIDevice *pci_dev) |
605 |
{ |
606 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
607 |
VirtIODevice *vdev; |
608 |
|
609 |
vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic); |
610 |
|
611 |
vdev->nvectors = proxy->nvectors; |
612 |
virtio_init_pci(proxy, vdev, |
613 |
PCI_VENDOR_ID_REDHAT_QUMRANET, |
614 |
PCI_DEVICE_ID_VIRTIO_NET, |
615 |
PCI_CLASS_NETWORK_ETHERNET, |
616 |
0x00);
|
617 |
|
618 |
/* make the actual value visible */
|
619 |
proxy->nvectors = vdev->nvectors; |
620 |
return 0; |
621 |
} |
622 |
|
623 |
static int virtio_net_exit_pci(PCIDevice *pci_dev) |
624 |
{ |
625 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
626 |
|
627 |
virtio_net_exit(proxy->vdev); |
628 |
return virtio_exit_pci(pci_dev);
|
629 |
} |
630 |
|
631 |
static int virtio_balloon_init_pci(PCIDevice *pci_dev) |
632 |
{ |
633 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
634 |
VirtIODevice *vdev; |
635 |
|
636 |
vdev = virtio_balloon_init(&pci_dev->qdev); |
637 |
virtio_init_pci(proxy, vdev, |
638 |
PCI_VENDOR_ID_REDHAT_QUMRANET, |
639 |
PCI_DEVICE_ID_VIRTIO_BALLOON, |
640 |
PCI_CLASS_MEMORY_RAM, |
641 |
0x00);
|
642 |
return 0; |
643 |
} |
644 |
|
645 |
#ifdef CONFIG_LINUX
|
646 |
static int virtio_9p_init_pci(PCIDevice *pci_dev) |
647 |
{ |
648 |
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
649 |
VirtIODevice *vdev; |
650 |
|
651 |
vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf); |
652 |
virtio_init_pci(proxy, vdev, |
653 |
PCI_VENDOR_ID_REDHAT_QUMRANET, |
654 |
0x1009,
|
655 |
0x2,
|
656 |
0x00);
|
657 |
|
658 |
return 0; |
659 |
} |
660 |
#endif
|
661 |
|
662 |
static PCIDeviceInfo virtio_info[] = {
|
663 |
{ |
664 |
.qdev.name = "virtio-blk-pci",
|
665 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
666 |
.init = virtio_blk_init_pci, |
667 |
.exit = virtio_blk_exit_pci, |
668 |
.qdev.props = (Property[]) { |
669 |
DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
670 |
DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block), |
671 |
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
672 |
DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features), |
673 |
DEFINE_PROP_END_OF_LIST(), |
674 |
}, |
675 |
.qdev.reset = virtio_pci_reset, |
676 |
},{ |
677 |
.qdev.name = "virtio-net-pci",
|
678 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
679 |
.init = virtio_net_init_pci, |
680 |
.exit = virtio_net_exit_pci, |
681 |
.romfile = "pxe-virtio.bin",
|
682 |
.qdev.props = (Property[]) { |
683 |
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), |
684 |
DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), |
685 |
DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic), |
686 |
DEFINE_PROP_END_OF_LIST(), |
687 |
}, |
688 |
.qdev.reset = virtio_pci_reset, |
689 |
},{ |
690 |
.qdev.name = "virtio-serial-pci",
|
691 |
.qdev.alias = "virtio-serial",
|
692 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
693 |
.init = virtio_serial_init_pci, |
694 |
.exit = virtio_exit_pci, |
695 |
.qdev.props = (Property[]) { |
696 |
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
|
697 |
DEV_NVECTORS_UNSPECIFIED), |
698 |
DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
699 |
DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
700 |
DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, max_virtserial_ports,
|
701 |
31),
|
702 |
DEFINE_PROP_END_OF_LIST(), |
703 |
}, |
704 |
.qdev.reset = virtio_pci_reset, |
705 |
},{ |
706 |
.qdev.name = "virtio-balloon-pci",
|
707 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
708 |
.init = virtio_balloon_init_pci, |
709 |
.exit = virtio_exit_pci, |
710 |
.qdev.props = (Property[]) { |
711 |
DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
712 |
DEFINE_PROP_END_OF_LIST(), |
713 |
}, |
714 |
.qdev.reset = virtio_pci_reset, |
715 |
},{ |
716 |
#ifdef CONFIG_LINUX
|
717 |
.qdev.name = "virtio-9p-pci",
|
718 |
.qdev.size = sizeof(VirtIOPCIProxy),
|
719 |
.init = virtio_9p_init_pci, |
720 |
.qdev.props = (Property[]) { |
721 |
DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
722 |
DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
|
723 |
DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
|
724 |
DEFINE_PROP_END_OF_LIST(), |
725 |
}, |
726 |
}, { |
727 |
#endif
|
728 |
/* end of list */
|
729 |
} |
730 |
}; |
731 |
|
732 |
static void virtio_pci_register_devices(void) |
733 |
{ |
734 |
pci_qdev_register_many(virtio_info); |
735 |
} |
736 |
|
737 |
device_init(virtio_pci_register_devices) |