blob: d08c1590d2144ffb79d0e4fcd0e9738c0468b16f [file] [log] [blame]
Paul Brook53c25ce2009-05-18 14:51:59 +01001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010014 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
Paul Brook53c25ce2009-05-18 14:51:59 +010016 */
17
18#include <inttypes.h>
19
20#include "virtio.h"
Michael S. Tsirkin81725392010-01-10 13:52:53 +020021#include "virtio-blk.h"
22#include "virtio-net.h"
Amit Shah6b331ef2011-02-03 11:22:32 +053023#include "virtio-serial.h"
Stefan Hajnoczi973abc72011-02-11 08:40:59 +000024#include "virtio-scsi.h"
Paul Brook53c25ce2009-05-18 14:51:59 +010025#include "pci.h"
Markus Armbruster2f792012010-02-18 16:24:31 +010026#include "qemu-error.h"
Jan Kiszka7d37d352012-05-17 10:32:39 -030027#include "msi.h"
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +030028#include "msix.h"
Gerd Hoffmanna1e0fea2009-07-15 13:48:25 +020029#include "net.h"
Gerd Hoffmann97b15622009-10-21 15:25:35 +020030#include "loader.h"
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +020031#include "kvm.h"
Blue Swirl24463332010-08-24 15:22:24 +000032#include "blockdev.h"
Aneesh Kumar K.V9fe1ebe2011-06-01 12:35:13 +053033#include "virtio-pci.h"
Michael S. Tsirkin11297142011-07-27 11:08:20 +030034#include "range.h"
Paul Brook53c25ce2009-05-18 14:51:59 +010035
36/* from Linux's linux/virtio_pci.h */
37
38/* A 32-bit r/o bitmask of the features supported by the host */
39#define VIRTIO_PCI_HOST_FEATURES 0
40
41/* A 32-bit r/w bitmask of features activated by the guest */
42#define VIRTIO_PCI_GUEST_FEATURES 4
43
44/* A 32-bit r/w PFN for the currently selected queue */
45#define VIRTIO_PCI_QUEUE_PFN 8
46
47/* A 16-bit r/o queue size for the currently selected queue */
48#define VIRTIO_PCI_QUEUE_NUM 12
49
50/* A 16-bit r/w queue selector */
51#define VIRTIO_PCI_QUEUE_SEL 14
52
53/* A 16-bit r/w queue notifier */
54#define VIRTIO_PCI_QUEUE_NOTIFY 16
55
56/* An 8-bit device status register. */
57#define VIRTIO_PCI_STATUS 18
58
59/* An 8-bit r/o interrupt status register. Reading the value will return the
60 * current contents of the ISR and will also clear it. This is effectively
61 * a read-and-acknowledge. */
62#define VIRTIO_PCI_ISR 19
63
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +030064/* MSI-X registers: only enabled if MSI-X is enabled. */
65/* A 16-bit vector for configuration changes. */
66#define VIRTIO_MSI_CONFIG_VECTOR 20
67/* A 16-bit vector for selected queue notifications. */
68#define VIRTIO_MSI_QUEUE_VECTOR 22
69
70/* Config space size */
71#define VIRTIO_PCI_CONFIG_NOMSI 20
72#define VIRTIO_PCI_CONFIG_MSI 24
73#define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
74 VIRTIO_PCI_CONFIG_MSI : \
75 VIRTIO_PCI_CONFIG_NOMSI)
76
77/* The remaining space is defined by each driver as the per-driver
78 * configuration space */
79#define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
80 VIRTIO_PCI_CONFIG_MSI : \
81 VIRTIO_PCI_CONFIG_NOMSI)
Paul Brook53c25ce2009-05-18 14:51:59 +010082
Paul Brook53c25ce2009-05-18 14:51:59 +010083/* How many bits to shift physical queue address written to QUEUE_PFN.
84 * 12 is historical, and due to x86 page size. */
85#define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
86
Stefan Hajnoczi3dbca8e2010-12-17 12:01:49 +000087/* Flags track per-device state like workarounds for quirks in older guests. */
88#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
Alexander Grafc81131d2010-03-16 19:18:07 +010089
Paul Brook53c25ce2009-05-18 14:51:59 +010090/* 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
Benjamin Herrenschmidt82afa582012-01-10 01:35:11 +000096/* HACK for virtio to determine if it's running a big endian guest */
97bool virtio_is_big_endian(void);
98
Paul Brook53c25ce2009-05-18 14:51:59 +010099/* virtio device */
100
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300101static void virtio_pci_notify(void *opaque, uint16_t vector)
Paul Brook53c25ce2009-05-18 14:51:59 +0100102{
103 VirtIOPCIProxy *proxy = opaque;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300104 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);
Paul Brook53c25ce2009-05-18 14:51:59 +0100108}
109
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +0300110static 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
119static 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
126static 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);
Michael S. Tsirkine6da7682009-07-05 16:02:34 +0300131 if (ret) {
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +0300132 return ret;
Michael S. Tsirkine6da7682009-07-05 16:02:34 +0300133 }
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +0300134 msix_load(&proxy->pci_dev, f);
Michael S. Tsirkine6da7682009-07-05 16:02:34 +0300135 if (msix_present(&proxy->pci_dev)) {
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +0300136 qemu_get_be16s(f, &proxy->vdev->config_vector);
Michael S. Tsirkine6da7682009-07-05 16:02:34 +0300137 } 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 }
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +0300143 return 0;
144}
145
146static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
147{
148 VirtIOPCIProxy *proxy = opaque;
149 uint16_t vector;
Michael S. Tsirkine6da7682009-07-05 16:02:34 +0300150 if (msix_present(&proxy->pci_dev)) {
151 qemu_get_be16s(f, &vector);
152 } else {
153 vector = VIRTIO_NO_VECTOR;
154 }
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +0300155 virtio_queue_set_vector(proxy->vdev, n, vector);
Michael S. Tsirkine6da7682009-07-05 16:02:34 +0300156 if (vector != VIRTIO_NO_VECTOR) {
157 return msix_vector_use(&proxy->pci_dev, vector);
158 }
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +0300159 return 0;
160}
161
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000162static 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);
Avi Kivityda146d02011-08-08 16:09:13 +0300167 int r = 0;
168
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000169 if (assign) {
170 r = event_notifier_init(notifier, 1);
171 if (r < 0) {
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200172 error_report("%s: unable to init event notifier: %d",
173 __func__, r);
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000174 return r;
175 }
Avi Kivityda146d02011-08-08 16:09:13 +0300176 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
177 true, n, event_notifier_get_fd(notifier));
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000178 } else {
Avi Kivityda146d02011-08-08 16:09:13 +0300179 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
180 true, n, event_notifier_get_fd(notifier));
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000181 /* Handle the race condition where the guest kicked and we deassigned
182 * before we got around to handling the kick.
183 */
184 if (event_notifier_test_and_clear(notifier)) {
185 virtio_queue_notify_vq(vq);
186 }
187
188 event_notifier_cleanup(notifier);
189 }
190 return r;
191}
192
193static void virtio_pci_host_notifier_read(void *opaque)
194{
195 VirtQueue *vq = opaque;
196 EventNotifier *n = virtio_queue_get_host_notifier(vq);
197 if (event_notifier_test_and_clear(n)) {
198 virtio_queue_notify_vq(vq);
199 }
200}
201
202static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
203 int n, bool assign)
204{
205 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
206 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
207 if (assign) {
208 qemu_set_fd_handler(event_notifier_get_fd(notifier),
209 virtio_pci_host_notifier_read, NULL, vq);
210 } else {
211 qemu_set_fd_handler(event_notifier_get_fd(notifier),
212 NULL, NULL, NULL);
213 }
214}
215
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200216static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000217{
218 int n, r;
219
220 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
221 proxy->ioeventfd_disabled ||
222 proxy->ioeventfd_started) {
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200223 return;
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000224 }
225
226 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
227 if (!virtio_queue_get_num(proxy->vdev, n)) {
228 continue;
229 }
230
231 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
232 if (r < 0) {
233 goto assign_error;
234 }
235
236 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
237 }
238 proxy->ioeventfd_started = true;
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200239 return;
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000240
241assign_error:
242 while (--n >= 0) {
243 if (!virtio_queue_get_num(proxy->vdev, n)) {
244 continue;
245 }
246
247 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200248 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
249 assert(r >= 0);
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000250 }
251 proxy->ioeventfd_started = false;
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200252 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000253}
254
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200255static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000256{
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200257 int r;
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000258 int n;
259
260 if (!proxy->ioeventfd_started) {
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200261 return;
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000262 }
263
264 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
265 if (!virtio_queue_get_num(proxy->vdev, n)) {
266 continue;
267 }
268
269 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
Michael S. Tsirkinb36e3912011-01-11 14:10:15 +0200270 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
271 assert(r >= 0);
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000272 }
273 proxy->ioeventfd_started = false;
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000274}
275
Aneesh Kumar K.V8798d6c2011-12-04 22:35:28 +0530276void virtio_pci_reset(DeviceState *d)
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300277{
Michael S. Tsirkine4890302009-09-16 13:40:37 +0300278 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000279 virtio_pci_stop_ioeventfd(proxy);
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300280 virtio_reset(proxy->vdev);
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300281 msix_reset(&proxy->pci_dev);
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000282 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300283}
284
Paul Brook53c25ce2009-05-18 14:51:59 +0100285static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
286{
287 VirtIOPCIProxy *proxy = opaque;
288 VirtIODevice *vdev = proxy->vdev;
Anthony Liguoric227f092009-10-01 16:12:16 -0500289 target_phys_addr_t pa;
Paul Brook53c25ce2009-05-18 14:51:59 +0100290
Paul Brook53c25ce2009-05-18 14:51:59 +0100291 switch (addr) {
292 case VIRTIO_PCI_GUEST_FEATURES:
293 /* Guest does not negotiate properly? We have to assume nothing. */
294 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
Paolo Bonziniad0c9332011-11-24 13:28:52 +0100295 val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
Paul Brook53c25ce2009-05-18 14:51:59 +0100296 }
Paolo Bonziniad0c9332011-11-24 13:28:52 +0100297 virtio_set_features(vdev, val);
Paul Brook53c25ce2009-05-18 14:51:59 +0100298 break;
299 case VIRTIO_PCI_QUEUE_PFN:
Anthony Liguoric227f092009-10-01 16:12:16 -0500300 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
Michael S. Tsirkin1b8e9b22009-11-24 16:45:35 +0200301 if (pa == 0) {
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000302 virtio_pci_stop_ioeventfd(proxy);
Michael S. Tsirkin1b8e9b22009-11-24 16:45:35 +0200303 virtio_reset(proxy->vdev);
304 msix_unuse_all_vectors(&proxy->pci_dev);
305 }
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300306 else
307 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
Paul Brook53c25ce2009-05-18 14:51:59 +0100308 break;
309 case VIRTIO_PCI_QUEUE_SEL:
310 if (val < VIRTIO_PCI_QUEUE_MAX)
311 vdev->queue_sel = val;
312 break;
313 case VIRTIO_PCI_QUEUE_NOTIFY:
Stefan Hajnoczi7157e2e2011-05-08 22:29:07 +0100314 if (val < VIRTIO_PCI_QUEUE_MAX) {
315 virtio_queue_notify(vdev, val);
316 }
Paul Brook53c25ce2009-05-18 14:51:59 +0100317 break;
318 case VIRTIO_PCI_STATUS:
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000319 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
320 virtio_pci_stop_ioeventfd(proxy);
321 }
322
Michael S. Tsirkin3e607cb2010-03-17 13:08:05 +0200323 virtio_set_status(vdev, val & 0xFF);
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000324
325 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
326 virtio_pci_start_ioeventfd(proxy);
327 }
328
Michael S. Tsirkin1b8e9b22009-11-24 16:45:35 +0200329 if (vdev->status == 0) {
330 virtio_reset(proxy->vdev);
331 msix_unuse_all_vectors(&proxy->pci_dev);
332 }
Alexander Grafc81131d2010-03-16 19:18:07 +0100333
334 /* Linux before 2.6.34 sets the device as OK without enabling
335 the PCI device bus master bit. In this case we need to disable
336 some safety checks. */
337 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
338 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
Stefan Hajnoczi3dbca8e2010-12-17 12:01:49 +0000339 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
Alexander Grafc81131d2010-03-16 19:18:07 +0100340 }
Paul Brook53c25ce2009-05-18 14:51:59 +0100341 break;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300342 case VIRTIO_MSI_CONFIG_VECTOR:
343 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
344 /* Make it possible for guest to discover an error took place. */
345 if (msix_vector_use(&proxy->pci_dev, val) < 0)
346 val = VIRTIO_NO_VECTOR;
347 vdev->config_vector = val;
348 break;
349 case VIRTIO_MSI_QUEUE_VECTOR:
350 msix_vector_unuse(&proxy->pci_dev,
351 virtio_queue_vector(vdev, vdev->queue_sel));
352 /* Make it possible for guest to discover an error took place. */
353 if (msix_vector_use(&proxy->pci_dev, val) < 0)
354 val = VIRTIO_NO_VECTOR;
355 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
356 break;
357 default:
Stefan Hajnoczi4e02d462010-11-15 20:44:38 +0000358 error_report("%s: unexpected address 0x%x value 0x%x",
359 __func__, addr, val);
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300360 break;
Paul Brook53c25ce2009-05-18 14:51:59 +0100361 }
362}
363
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300364static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
Paul Brook53c25ce2009-05-18 14:51:59 +0100365{
Paul Brook53c25ce2009-05-18 14:51:59 +0100366 VirtIODevice *vdev = proxy->vdev;
367 uint32_t ret = 0xFFFFFFFF;
368
Paul Brook53c25ce2009-05-18 14:51:59 +0100369 switch (addr) {
370 case VIRTIO_PCI_HOST_FEATURES:
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200371 ret = proxy->host_features;
Paul Brook53c25ce2009-05-18 14:51:59 +0100372 break;
373 case VIRTIO_PCI_GUEST_FEATURES:
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200374 ret = vdev->guest_features;
Paul Brook53c25ce2009-05-18 14:51:59 +0100375 break;
376 case VIRTIO_PCI_QUEUE_PFN:
377 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
378 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
379 break;
380 case VIRTIO_PCI_QUEUE_NUM:
381 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
382 break;
383 case VIRTIO_PCI_QUEUE_SEL:
384 ret = vdev->queue_sel;
385 break;
386 case VIRTIO_PCI_STATUS:
387 ret = vdev->status;
388 break;
389 case VIRTIO_PCI_ISR:
390 /* reading from the ISR also clears it. */
391 ret = vdev->isr;
392 vdev->isr = 0;
Michael S. Tsirkin7055e682009-06-21 19:50:13 +0300393 qemu_set_irq(proxy->pci_dev.irq[0], 0);
Paul Brook53c25ce2009-05-18 14:51:59 +0100394 break;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300395 case VIRTIO_MSI_CONFIG_VECTOR:
396 ret = vdev->config_vector;
397 break;
398 case VIRTIO_MSI_QUEUE_VECTOR:
399 ret = virtio_queue_vector(vdev, vdev->queue_sel);
400 break;
Paul Brook53c25ce2009-05-18 14:51:59 +0100401 default:
402 break;
403 }
404
405 return ret;
406}
407
408static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
409{
410 VirtIOPCIProxy *proxy = opaque;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300411 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300412 if (addr < config)
413 return virtio_ioport_read(proxy, addr);
414 addr -= config;
Paul Brook53c25ce2009-05-18 14:51:59 +0100415 return virtio_config_readb(proxy->vdev, addr);
416}
417
418static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
419{
420 VirtIOPCIProxy *proxy = opaque;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300421 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
Benjamin Herrenschmidt82afa582012-01-10 01:35:11 +0000422 uint16_t val;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300423 if (addr < config)
424 return virtio_ioport_read(proxy, addr);
425 addr -= config;
Benjamin Herrenschmidt82afa582012-01-10 01:35:11 +0000426 val = virtio_config_readw(proxy->vdev, addr);
427 if (virtio_is_big_endian()) {
428 /*
429 * virtio is odd, ioports are LE but config space is target native
430 * endian. However, in qemu, all PIO is LE, so we need to re-swap
431 * on BE targets
432 */
433 val = bswap16(val);
434 }
435 return val;
Paul Brook53c25ce2009-05-18 14:51:59 +0100436}
437
438static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
439{
440 VirtIOPCIProxy *proxy = opaque;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300441 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
Benjamin Herrenschmidt82afa582012-01-10 01:35:11 +0000442 uint32_t val;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300443 if (addr < config)
444 return virtio_ioport_read(proxy, addr);
445 addr -= config;
Benjamin Herrenschmidt82afa582012-01-10 01:35:11 +0000446 val = virtio_config_readl(proxy->vdev, addr);
447 if (virtio_is_big_endian()) {
448 val = bswap32(val);
449 }
450 return val;
Paul Brook53c25ce2009-05-18 14:51:59 +0100451}
452
453static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
454{
455 VirtIOPCIProxy *proxy = opaque;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300456 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300457 if (addr < config) {
458 virtio_ioport_write(proxy, addr, val);
459 return;
460 }
461 addr -= config;
Paul Brook53c25ce2009-05-18 14:51:59 +0100462 virtio_config_writeb(proxy->vdev, addr, val);
463}
464
465static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
466{
467 VirtIOPCIProxy *proxy = opaque;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300468 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300469 if (addr < config) {
470 virtio_ioport_write(proxy, addr, val);
471 return;
472 }
473 addr -= config;
Benjamin Herrenschmidt82afa582012-01-10 01:35:11 +0000474 if (virtio_is_big_endian()) {
475 val = bswap16(val);
476 }
Paul Brook53c25ce2009-05-18 14:51:59 +0100477 virtio_config_writew(proxy->vdev, addr, val);
478}
479
480static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
481{
482 VirtIOPCIProxy *proxy = opaque;
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300483 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300484 if (addr < config) {
485 virtio_ioport_write(proxy, addr, val);
486 return;
487 }
488 addr -= config;
Benjamin Herrenschmidt82afa582012-01-10 01:35:11 +0000489 if (virtio_is_big_endian()) {
490 val = bswap32(val);
491 }
Paul Brook53c25ce2009-05-18 14:51:59 +0100492 virtio_config_writel(proxy->vdev, addr, val);
493}
494
Blue Swirl4636b9d2012-05-12 19:45:22 +0000495static const MemoryRegionPortio virtio_portio[] = {
Avi Kivityda146d02011-08-08 16:09:13 +0300496 { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
497 { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
498 { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
499 { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
500 { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
501 { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
502 PORTIO_END_OF_LIST()
503};
Paul Brook53c25ce2009-05-18 14:51:59 +0100504
Avi Kivityda146d02011-08-08 16:09:13 +0300505static const MemoryRegionOps virtio_pci_config_ops = {
506 .old_portio = virtio_portio,
507 .endianness = DEVICE_LITTLE_ENDIAN,
508};
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300509
510static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
511 uint32_t val, int len)
512{
Yan Vugenfirered757e12009-09-08 10:49:41 -0400513 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
514
Michael S. Tsirkin11297142011-07-27 11:08:20 +0300515 pci_default_write_config(pci_dev, address, val, len);
516
517 if (range_covers_byte(address, len, PCI_COMMAND) &&
518 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
519 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
520 virtio_pci_stop_ioeventfd(proxy);
521 virtio_set_status(proxy->vdev,
522 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
Yan Vugenfirered757e12009-09-08 10:49:41 -0400523 }
524
Michael S. Tsirkin85352472009-09-22 13:35:28 +0300525 msix_write_config(pci_dev, address, val, len);
Paul Brook53c25ce2009-05-18 14:51:59 +0100526}
527
Michael S. Tsirkin6d74ca52009-12-08 20:07:48 +0200528static unsigned virtio_pci_get_features(void *opaque)
529{
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200530 VirtIOPCIProxy *proxy = opaque;
531 return proxy->host_features;
Michael S. Tsirkin6d74ca52009-12-08 20:07:48 +0200532}
533
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +0200534static void virtio_pci_guest_notifier_read(void *opaque)
535{
536 VirtQueue *vq = opaque;
537 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
538 if (event_notifier_test_and_clear(n)) {
539 virtio_irq(vq);
540 }
541}
542
Jan Kiszka7d37d352012-05-17 10:32:39 -0300543static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
544 unsigned int queue_no,
545 unsigned int vector,
546 MSIMessage msg)
547{
548 VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
549 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
550 int fd, ret;
551
552 fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vq));
553
554 if (irqfd->users == 0) {
555 ret = kvm_irqchip_add_msi_route(kvm_state, msg);
556 if (ret < 0) {
557 return ret;
558 }
559 irqfd->virq = ret;
560 }
561 irqfd->users++;
562
563 ret = kvm_irqchip_add_irqfd(kvm_state, fd, irqfd->virq);
564 if (ret < 0) {
565 if (--irqfd->users == 0) {
566 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
567 }
568 return ret;
569 }
570
571 qemu_set_fd_handler(fd, NULL, NULL, NULL);
572
573 return 0;
574}
575
576static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
577 unsigned int queue_no,
578 unsigned int vector)
579{
580 VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
581 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
582 int fd, ret;
583
584 fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vq));
585
586 ret = kvm_irqchip_remove_irqfd(kvm_state, fd, irqfd->virq);
587 assert(ret == 0);
588
589 if (--irqfd->users == 0) {
590 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
591 }
592
593 qemu_set_fd_handler(fd, virtio_pci_guest_notifier_read, NULL, vq);
594}
595
596static int kvm_virtio_pci_vector_use(PCIDevice *dev, unsigned vector,
597 MSIMessage msg)
598{
599 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
600 VirtIODevice *vdev = proxy->vdev;
601 int ret, queue_no;
602
603 for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
604 if (!virtio_queue_get_num(vdev, queue_no)) {
605 break;
606 }
607 if (virtio_queue_vector(vdev, queue_no) != vector) {
608 continue;
609 }
610 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
611 if (ret < 0) {
612 goto undo;
613 }
614 }
615 return 0;
616
617undo:
618 while (--queue_no >= 0) {
619 if (virtio_queue_vector(vdev, queue_no) != vector) {
620 continue;
621 }
622 kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector);
623 }
624 return ret;
625}
626
627static void kvm_virtio_pci_vector_release(PCIDevice *dev, unsigned vector)
628{
629 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
630 VirtIODevice *vdev = proxy->vdev;
631 int queue_no;
632
633 for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
634 if (!virtio_queue_get_num(vdev, queue_no)) {
635 break;
636 }
637 if (virtio_queue_vector(vdev, queue_no) != vector) {
638 continue;
639 }
640 kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector);
641 }
642}
643
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +0200644static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
645{
646 VirtIOPCIProxy *proxy = opaque;
647 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
648 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
649
650 if (assign) {
651 int r = event_notifier_init(notifier, 0);
652 if (r < 0) {
653 return r;
654 }
655 qemu_set_fd_handler(event_notifier_get_fd(notifier),
656 virtio_pci_guest_notifier_read, NULL, vq);
657 } else {
658 qemu_set_fd_handler(event_notifier_get_fd(notifier),
659 NULL, NULL, NULL);
Jan Kiszka7d37d352012-05-17 10:32:39 -0300660 /* Test and clear notifier before closing it,
661 * in case poll callback didn't have time to run. */
662 virtio_pci_guest_notifier_read(vq);
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +0200663 event_notifier_cleanup(notifier);
664 }
665
666 return 0;
667}
668
mst@redhat.com5430a282011-02-01 22:13:42 +0200669static bool virtio_pci_query_guest_notifiers(void *opaque)
670{
671 VirtIOPCIProxy *proxy = opaque;
672 return msix_enabled(&proxy->pci_dev);
673}
674
Michael S. Tsirkin54dd9322010-10-06 15:20:17 +0200675static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
676{
677 VirtIOPCIProxy *proxy = opaque;
678 VirtIODevice *vdev = proxy->vdev;
679 int r, n;
680
Jan Kiszka7d37d352012-05-17 10:32:39 -0300681 /* Must unset vector notifier while guest notifier is still assigned */
682 if (kvm_irqchip_in_kernel() && !assign) {
683 msix_unset_vector_notifiers(&proxy->pci_dev);
684 g_free(proxy->vector_irqfd);
685 proxy->vector_irqfd = NULL;
686 }
687
Michael S. Tsirkin54dd9322010-10-06 15:20:17 +0200688 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
689 if (!virtio_queue_get_num(vdev, n)) {
690 break;
691 }
692
693 r = virtio_pci_set_guest_notifier(opaque, n, assign);
694 if (r < 0) {
695 goto assign_error;
696 }
697 }
698
Jan Kiszka7d37d352012-05-17 10:32:39 -0300699 /* Must set vector notifier after guest notifier has been assigned */
700 if (kvm_irqchip_in_kernel() && assign) {
701 proxy->vector_irqfd =
702 g_malloc0(sizeof(*proxy->vector_irqfd) *
703 msix_nr_vectors_allocated(&proxy->pci_dev));
704 r = msix_set_vector_notifiers(&proxy->pci_dev,
705 kvm_virtio_pci_vector_use,
706 kvm_virtio_pci_vector_release);
707 if (r < 0) {
708 goto assign_error;
709 }
710 }
711
Michael S. Tsirkin54dd9322010-10-06 15:20:17 +0200712 return 0;
713
714assign_error:
715 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
Jan Kiszka7d37d352012-05-17 10:32:39 -0300716 assert(assign);
Michael S. Tsirkin54dd9322010-10-06 15:20:17 +0200717 while (--n >= 0) {
718 virtio_pci_set_guest_notifier(opaque, n, !assign);
719 }
720 return r;
721}
722
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +0200723static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
724{
725 VirtIOPCIProxy *proxy = opaque;
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000726
727 /* Stop using ioeventfd for virtqueue kick if the device starts using host
728 * notifiers. This makes it easy to avoid stepping on each others' toes.
729 */
730 proxy->ioeventfd_disabled = assign;
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +0200731 if (assign) {
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000732 virtio_pci_stop_ioeventfd(proxy);
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +0200733 }
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000734 /* We don't need to start here: it's not needed because backend
735 * currently only stops on status change away from ok,
736 * reset, vmstop and such. If we do add code to start here,
737 * need to check vmstate, device state etc. */
738 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
739}
740
741static void virtio_pci_vmstate_change(void *opaque, bool running)
742{
743 VirtIOPCIProxy *proxy = opaque;
744
745 if (running) {
Michael S. Tsirkin89c473f2011-03-19 19:28:19 +0200746 /* Try to find out if the guest has bus master disabled, but is
747 in ready state. Then we have a buggy guest OS. */
748 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
749 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
750 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
751 }
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000752 virtio_pci_start_ioeventfd(proxy);
753 } else {
754 virtio_pci_stop_ioeventfd(proxy);
755 }
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +0200756}
757
Paul Brook53c25ce2009-05-18 14:51:59 +0100758static const VirtIOBindings virtio_pci_bindings = {
Michael S. Tsirkinff24bd52009-06-21 19:50:40 +0300759 .notify = virtio_pci_notify,
760 .save_config = virtio_pci_save_config,
761 .load_config = virtio_pci_load_config,
762 .save_queue = virtio_pci_save_queue,
763 .load_queue = virtio_pci_load_queue,
Michael S. Tsirkin6d74ca52009-12-08 20:07:48 +0200764 .get_features = virtio_pci_get_features,
mst@redhat.com5430a282011-02-01 22:13:42 +0200765 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
Michael S. Tsirkinade80dc2010-03-17 13:08:13 +0200766 .set_host_notifier = virtio_pci_set_host_notifier,
Michael S. Tsirkin54dd9322010-10-06 15:20:17 +0200767 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000768 .vmstate_change = virtio_pci_vmstate_change,
Paul Brook53c25ce2009-05-18 14:51:59 +0100769};
770
Michael S. Tsirkinbefeac42011-06-14 17:51:11 +0300771void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
Paul Brook53c25ce2009-05-18 14:51:59 +0100772{
773 uint8_t *config;
774 uint32_t size;
775
776 proxy->vdev = vdev;
777
778 config = proxy->pci_dev.config;
Paul Brook53c25ce2009-05-18 14:51:59 +0100779
Isaku Yamahatae75ccf22011-05-25 10:58:36 +0900780 if (proxy->class_code) {
781 pci_config_set_class(config, proxy->class_code);
782 }
Hui Kai Ranad3d11e2011-12-08 13:49:13 +0800783 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
784 pci_get_word(config + PCI_VENDOR_ID));
785 pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
786 config[PCI_INTERRUPT_PIN] = 1;
Paul Brook53c25ce2009-05-18 14:51:59 +0100787
Avi Kivity95524ae2011-08-08 16:09:26 +0300788 memory_region_init(&proxy->msix_bar, "virtio-msix", 4096);
789 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors,
790 &proxy->msix_bar, 1, 0)) {
Avi Kivitye824b2c2011-08-08 16:09:31 +0300791 pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
792 &proxy->msix_bar);
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300793 } else
794 vdev->nvectors = 0;
795
Yan Vugenfirered757e12009-09-08 10:49:41 -0400796 proxy->pci_dev.config_write = virtio_write_config;
797
Michael S. Tsirkinaba800a2009-06-21 19:50:26 +0300798 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
Paul Brook53c25ce2009-05-18 14:51:59 +0100799 if (size & (size-1))
800 size = 1 << qemu_fls(size);
801
Avi Kivityda146d02011-08-08 16:09:13 +0300802 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
803 "virtio-pci", size);
Avi Kivitye824b2c2011-08-08 16:09:31 +0300804 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
805 &proxy->bar);
Paul Brook53c25ce2009-05-18 14:51:59 +0100806
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000807 if (!kvm_has_many_ioeventfds()) {
808 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
809 }
810
Paul Brook53c25ce2009-05-18 14:51:59 +0100811 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200812 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
813 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
814 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
Paul Brook53c25ce2009-05-18 14:51:59 +0100815}
816
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200817static int virtio_blk_init_pci(PCIDevice *pci_dev)
Paul Brook53c25ce2009-05-18 14:51:59 +0100818{
819 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
820 VirtIODevice *vdev;
821
Mark McLoughlin85c2c732009-07-30 11:29:13 +0100822 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
823 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
824 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
825
Paolo Bonzini12c56742012-05-16 12:54:05 +0200826 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->blk);
Markus Armbrusterac0c14d2010-07-06 14:37:42 +0200827 if (!vdev) {
828 return -1;
829 }
Gerd Hoffmann177539e2009-08-12 12:47:24 +0200830 vdev->nvectors = proxy->nvectors;
Isaku Yamahatae75ccf22011-05-25 10:58:36 +0900831 virtio_init_pci(proxy, vdev);
Gerd Hoffmann177539e2009-08-12 12:47:24 +0200832 /* make the actual value visible */
833 proxy->nvectors = vdev->nvectors;
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200834 return 0;
Mark McLoughlin21d58b52009-07-07 12:09:58 +0100835}
836
Michael S. Tsirkin0f457d92009-10-05 23:02:20 +0200837static int virtio_exit_pci(PCIDevice *pci_dev)
838{
Avi Kivityda146d02011-08-08 16:09:13 +0300839 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
Avi Kivity95524ae2011-08-08 16:09:26 +0300840 int r;
Avi Kivityda146d02011-08-08 16:09:13 +0300841
842 memory_region_destroy(&proxy->bar);
Avi Kivity95524ae2011-08-08 16:09:26 +0300843 r = msix_uninit(pci_dev, &proxy->msix_bar);
844 memory_region_destroy(&proxy->msix_bar);
845 return r;
Michael S. Tsirkin0f457d92009-10-05 23:02:20 +0200846}
847
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200848static int virtio_blk_exit_pci(PCIDevice *pci_dev)
849{
850 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
851
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000852 virtio_pci_stop_ioeventfd(proxy);
Alex Williamson9d0d3132010-07-20 11:14:22 -0600853 virtio_blk_exit(proxy->vdev);
Michael S. Tsirkin0f457d92009-10-05 23:02:20 +0200854 return virtio_exit_pci(pci_dev);
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200855}
856
Amit Shah98b19252010-01-20 00:36:52 +0530857static int virtio_serial_init_pci(PCIDevice *pci_dev)
Mark McLoughlin21d58b52009-07-07 12:09:58 +0100858{
Gerd Hoffmannd6beee92009-07-15 13:48:24 +0200859 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
Mark McLoughlin85c2c732009-07-30 11:29:13 +0100860 VirtIODevice *vdev;
861
Gerd Hoffmannd6beee92009-07-15 13:48:24 +0200862 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
863 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
864 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
865 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
866
Amit Shah6b331ef2011-02-03 11:22:32 +0530867 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
Amit Shah25fe3652009-09-29 15:51:04 +0530868 if (!vdev) {
869 return -1;
870 }
Amit Shah573fb602010-02-25 17:24:44 +0530871 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
Amit Shah6b331ef2011-02-03 11:22:32 +0530872 ? proxy->serial.max_virtserial_ports + 1
Amit Shah573fb602010-02-25 17:24:44 +0530873 : proxy->nvectors;
Isaku Yamahatae75ccf22011-05-25 10:58:36 +0900874 virtio_init_pci(proxy, vdev);
Amit Shaha1829202010-01-20 00:36:58 +0530875 proxy->nvectors = vdev->nvectors;
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200876 return 0;
Paul Brook53c25ce2009-05-18 14:51:59 +0100877}
878
Amit Shah8b53a862010-08-19 06:51:04 +0530879static int virtio_serial_exit_pci(PCIDevice *pci_dev)
880{
881 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
882
Amit Shah32059222011-02-17 12:31:10 +0530883 virtio_pci_stop_ioeventfd(proxy);
Amit Shah8b53a862010-08-19 06:51:04 +0530884 virtio_serial_exit(proxy->vdev);
885 return virtio_exit_pci(pci_dev);
886}
887
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200888static int virtio_net_init_pci(PCIDevice *pci_dev)
Paul Brook53c25ce2009-05-18 14:51:59 +0100889{
890 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
891 VirtIODevice *vdev;
892
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600893 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
Gerd Hoffmanna1e0fea2009-07-15 13:48:25 +0200894
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200895 vdev->nvectors = proxy->nvectors;
Isaku Yamahatae75ccf22011-05-25 10:58:36 +0900896 virtio_init_pci(proxy, vdev);
Gerd Hoffmanna1e0fea2009-07-15 13:48:25 +0200897
898 /* make the actual value visible */
899 proxy->nvectors = vdev->nvectors;
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200900 return 0;
Paul Brook53c25ce2009-05-18 14:51:59 +0100901}
902
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200903static int virtio_net_exit_pci(PCIDevice *pci_dev)
904{
905 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
906
Stefan Hajnoczi25db9eb2010-12-17 12:01:50 +0000907 virtio_pci_stop_ioeventfd(proxy);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200908 virtio_net_exit(proxy->vdev);
909 return virtio_exit_pci(pci_dev);
910}
911
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200912static int virtio_balloon_init_pci(PCIDevice *pci_dev)
Paul Brook53c25ce2009-05-18 14:51:59 +0100913{
914 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
915 VirtIODevice *vdev;
916
David Gibson2ba1d382012-04-03 17:24:11 +0300917 if (proxy->class_code != PCI_CLASS_OTHERS &&
918 proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
919 proxy->class_code = PCI_CLASS_OTHERS;
920 }
921
Paul Brook53c25ce2009-05-18 14:51:59 +0100922 vdev = virtio_balloon_init(&pci_dev->qdev);
Amit Shahf76f6652011-07-27 12:29:33 +0530923 if (!vdev) {
924 return -1;
925 }
Isaku Yamahatae75ccf22011-05-25 10:58:36 +0900926 virtio_init_pci(proxy, vdev);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200927 return 0;
Paul Brook53c25ce2009-05-18 14:51:59 +0100928}
929
Amit Shah855d7e22011-07-27 13:50:41 +0530930static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
931{
932 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
933
934 virtio_pci_stop_ioeventfd(proxy);
935 virtio_balloon_exit(proxy->vdev);
936 return virtio_exit_pci(pci_dev);
937}
938
Anthony Liguori40021f02011-12-04 12:22:06 -0600939static Property virtio_blk_properties[] = {
940 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
Paolo Bonzini12c56742012-05-16 12:54:05 +0200941 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, blk.conf),
942 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, blk.serial),
Paolo Bonzinia6c5c842012-05-16 12:54:06 +0200943#ifdef __linux__
944 DEFINE_PROP_BIT("scsi", VirtIOPCIProxy, blk.scsi, 0, true),
945#endif
Anthony Liguori40021f02011-12-04 12:22:06 -0600946 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
947 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
948 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
949 DEFINE_PROP_END_OF_LIST(),
Anthony Liguorie8557612011-12-06 19:32:44 -0600950};
951
Anthony Liguori40021f02011-12-04 12:22:06 -0600952static void virtio_blk_class_init(ObjectClass *klass, void *data)
953{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600954 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600955 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
956
957 k->init = virtio_blk_init_pci;
958 k->exit = virtio_blk_exit_pci;
959 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
960 k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
961 k->revision = VIRTIO_PCI_ABI_VERSION;
962 k->class_id = PCI_CLASS_STORAGE_SCSI;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600963 dc->reset = virtio_pci_reset;
964 dc->props = virtio_blk_properties;
Anthony Liguori40021f02011-12-04 12:22:06 -0600965}
966
Anthony Liguori39bffca2011-12-07 21:34:16 -0600967static TypeInfo virtio_blk_info = {
968 .name = "virtio-blk-pci",
969 .parent = TYPE_PCI_DEVICE,
970 .instance_size = sizeof(VirtIOPCIProxy),
971 .class_init = virtio_blk_class_init,
Anthony Liguorie8557612011-12-06 19:32:44 -0600972};
973
Anthony Liguori40021f02011-12-04 12:22:06 -0600974static Property virtio_net_properties[] = {
975 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
976 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
977 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
978 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
979 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL),
980 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST),
981 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
982 DEFINE_PROP_END_OF_LIST(),
Anthony Liguorie8557612011-12-06 19:32:44 -0600983};
984
Anthony Liguori40021f02011-12-04 12:22:06 -0600985static void virtio_net_class_init(ObjectClass *klass, void *data)
986{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600987 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600988 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
989
990 k->init = virtio_net_init_pci;
991 k->exit = virtio_net_exit_pci;
992 k->romfile = "pxe-virtio.rom";
993 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
994 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
995 k->revision = VIRTIO_PCI_ABI_VERSION;
996 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600997 dc->reset = virtio_pci_reset;
998 dc->props = virtio_net_properties;
Anthony Liguori40021f02011-12-04 12:22:06 -0600999}
1000
Anthony Liguori39bffca2011-12-07 21:34:16 -06001001static TypeInfo virtio_net_info = {
1002 .name = "virtio-net-pci",
1003 .parent = TYPE_PCI_DEVICE,
1004 .instance_size = sizeof(VirtIOPCIProxy),
1005 .class_init = virtio_net_class_init,
Anthony Liguori40021f02011-12-04 12:22:06 -06001006};
1007
1008static Property virtio_serial_properties[] = {
1009 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1010 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
1011 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1012 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1013 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31),
1014 DEFINE_PROP_END_OF_LIST(),
1015};
1016
1017static void virtio_serial_class_init(ObjectClass *klass, void *data)
1018{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001019 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -06001020 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1021
1022 k->init = virtio_serial_init_pci;
1023 k->exit = virtio_serial_exit_pci;
1024 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1025 k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1026 k->revision = VIRTIO_PCI_ABI_VERSION;
1027 k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
Anthony Liguori39bffca2011-12-07 21:34:16 -06001028 dc->reset = virtio_pci_reset;
1029 dc->props = virtio_serial_properties;
Anthony Liguori40021f02011-12-04 12:22:06 -06001030}
1031
Anthony Liguori39bffca2011-12-07 21:34:16 -06001032static TypeInfo virtio_serial_info = {
1033 .name = "virtio-serial-pci",
1034 .parent = TYPE_PCI_DEVICE,
1035 .instance_size = sizeof(VirtIOPCIProxy),
1036 .class_init = virtio_serial_class_init,
Anthony Liguori40021f02011-12-04 12:22:06 -06001037};
1038
1039static Property virtio_balloon_properties[] = {
1040 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
David Gibson2ba1d382012-04-03 17:24:11 +03001041 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
Anthony Liguori40021f02011-12-04 12:22:06 -06001042 DEFINE_PROP_END_OF_LIST(),
1043};
1044
1045static void virtio_balloon_class_init(ObjectClass *klass, void *data)
1046{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001047 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -06001048 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1049
1050 k->init = virtio_balloon_init_pci;
1051 k->exit = virtio_balloon_exit_pci;
1052 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1053 k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1054 k->revision = VIRTIO_PCI_ABI_VERSION;
David Gibson2ba1d382012-04-03 17:24:11 +03001055 k->class_id = PCI_CLASS_OTHERS;
Anthony Liguori39bffca2011-12-07 21:34:16 -06001056 dc->reset = virtio_pci_reset;
1057 dc->props = virtio_balloon_properties;
Anthony Liguori40021f02011-12-04 12:22:06 -06001058}
1059
Anthony Liguori39bffca2011-12-07 21:34:16 -06001060static TypeInfo virtio_balloon_info = {
1061 .name = "virtio-balloon-pci",
1062 .parent = TYPE_PCI_DEVICE,
1063 .instance_size = sizeof(VirtIOPCIProxy),
1064 .class_init = virtio_balloon_class_init,
Gerd Hoffmann0aab0d32009-06-30 14:12:07 +02001065};
1066
Stefan Hajnoczi973abc72011-02-11 08:40:59 +00001067static int virtio_scsi_init_pci(PCIDevice *pci_dev)
1068{
1069 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1070 VirtIODevice *vdev;
1071
1072 vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi);
1073 if (!vdev) {
1074 return -EINVAL;
1075 }
1076
1077 vdev->nvectors = proxy->nvectors;
1078 virtio_init_pci(proxy, vdev);
1079
1080 /* make the actual value visible */
1081 proxy->nvectors = vdev->nvectors;
1082 return 0;
1083}
1084
1085static int virtio_scsi_exit_pci(PCIDevice *pci_dev)
1086{
1087 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1088
1089 virtio_scsi_exit(proxy->vdev);
1090 return virtio_exit_pci(pci_dev);
1091}
1092
1093static Property virtio_scsi_properties[] = {
1094 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1095 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
1096 DEFINE_PROP_END_OF_LIST(),
1097};
1098
1099static void virtio_scsi_class_init(ObjectClass *klass, void *data)
1100{
1101 DeviceClass *dc = DEVICE_CLASS(klass);
1102 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1103
1104 k->init = virtio_scsi_init_pci;
1105 k->exit = virtio_scsi_exit_pci;
1106 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1107 k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1108 k->revision = 0x00;
1109 k->class_id = PCI_CLASS_STORAGE_SCSI;
1110 dc->reset = virtio_pci_reset;
1111 dc->props = virtio_scsi_properties;
1112}
1113
1114static TypeInfo virtio_scsi_info = {
1115 .name = "virtio-scsi-pci",
1116 .parent = TYPE_PCI_DEVICE,
1117 .instance_size = sizeof(VirtIOPCIProxy),
1118 .class_init = virtio_scsi_class_init,
1119};
1120
Andreas Färber83f7d432012-02-09 15:20:55 +01001121static void virtio_pci_register_types(void)
Paul Brook53c25ce2009-05-18 14:51:59 +01001122{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001123 type_register_static(&virtio_blk_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -06001124 type_register_static(&virtio_net_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -06001125 type_register_static(&virtio_serial_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -06001126 type_register_static(&virtio_balloon_info);
Stefan Hajnoczi973abc72011-02-11 08:40:59 +00001127 type_register_static(&virtio_scsi_info);
Paul Brook53c25ce2009-05-18 14:51:59 +01001128}
1129
Andreas Färber83f7d432012-02-09 15:20:55 +01001130type_init(virtio_pci_register_types)