Reset HPET config register on hpet_reset
[qemu-kvm/fedora.git] / hw / virtio-pci.c
blobd605b5fb1f79ff7f21124a1406a0c32c375c68af
1 /*
2 * Virtio PCI Bindings
4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #include <inttypes.h>
18 #include "virtio.h"
19 #include "pci.h"
20 //#include "sysemu.h"
21 #include "msix.h"
23 /* from Linux's linux/virtio_pci.h */
25 /* A 32-bit r/o bitmask of the features supported by the host */
26 #define VIRTIO_PCI_HOST_FEATURES 0
28 /* A 32-bit r/w bitmask of features activated by the guest */
29 #define VIRTIO_PCI_GUEST_FEATURES 4
31 /* A 32-bit r/w PFN for the currently selected queue */
32 #define VIRTIO_PCI_QUEUE_PFN 8
34 /* A 16-bit r/o queue size for the currently selected queue */
35 #define VIRTIO_PCI_QUEUE_NUM 12
37 /* A 16-bit r/w queue selector */
38 #define VIRTIO_PCI_QUEUE_SEL 14
40 /* A 16-bit r/w queue notifier */
41 #define VIRTIO_PCI_QUEUE_NOTIFY 16
43 /* An 8-bit device status register. */
44 #define VIRTIO_PCI_STATUS 18
46 /* An 8-bit r/o interrupt status register. Reading the value will return the
47 * current contents of the ISR and will also clear it. This is effectively
48 * a read-and-acknowledge. */
49 #define VIRTIO_PCI_ISR 19
51 /* MSI-X registers: only enabled if MSI-X is enabled. */
52 /* A 16-bit vector for configuration changes. */
53 #define VIRTIO_MSI_CONFIG_VECTOR 20
54 /* A 16-bit vector for selected queue notifications. */
55 #define VIRTIO_MSI_QUEUE_VECTOR 22
57 /* Config space size */
58 #define VIRTIO_PCI_CONFIG_NOMSI 20
59 #define VIRTIO_PCI_CONFIG_MSI 24
60 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
61 VIRTIO_PCI_CONFIG_MSI : \
62 VIRTIO_PCI_CONFIG_NOMSI)
64 /* The remaining space is defined by each driver as the per-driver
65 * configuration space */
66 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
67 VIRTIO_PCI_CONFIG_MSI : \
68 VIRTIO_PCI_CONFIG_NOMSI)
70 /* Virtio ABI version, if we increment this, we break the guest driver. */
71 #define VIRTIO_PCI_ABI_VERSION 0
73 /* How many bits to shift physical queue address written to QUEUE_PFN.
74 * 12 is historical, and due to x86 page size. */
75 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
77 /* QEMU doesn't strictly need write barriers since everything runs in
78 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
79 * KVM or if kqemu gets SMP support.
81 #define wmb() do { } while (0)
83 /* PCI bindings. */
85 typedef struct {
86 PCIDevice pci_dev;
87 VirtIODevice *vdev;
88 uint32_t addr;
90 uint16_t vendor;
91 uint16_t device;
92 uint16_t subvendor;
93 uint16_t class_code;
94 uint8_t pif;
95 } VirtIOPCIProxy;
97 /* virtio device */
99 static void virtio_pci_notify(void *opaque, uint16_t vector)
101 VirtIOPCIProxy *proxy = opaque;
102 if (msix_enabled(&proxy->pci_dev))
103 msix_notify(&proxy->pci_dev, vector);
104 else
105 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
108 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
110 VirtIOPCIProxy *proxy = opaque;
111 pci_device_save(&proxy->pci_dev, f);
112 msix_save(&proxy->pci_dev, f);
113 if (msix_present(&proxy->pci_dev))
114 qemu_put_be16(f, proxy->vdev->config_vector);
117 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
119 VirtIOPCIProxy *proxy = opaque;
120 if (msix_present(&proxy->pci_dev))
121 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
124 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
126 VirtIOPCIProxy *proxy = opaque;
127 int ret;
128 ret = pci_device_load(&proxy->pci_dev, f);
129 if (ret) {
130 return ret;
132 msix_load(&proxy->pci_dev, f);
133 if (msix_present(&proxy->pci_dev)) {
134 qemu_get_be16s(f, &proxy->vdev->config_vector);
135 } else {
136 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
138 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
139 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
141 return 0;
144 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
146 VirtIOPCIProxy *proxy = opaque;
147 uint16_t vector;
148 if (msix_present(&proxy->pci_dev)) {
149 qemu_get_be16s(f, &vector);
150 } else {
151 vector = VIRTIO_NO_VECTOR;
153 virtio_queue_set_vector(proxy->vdev, n, vector);
154 if (vector != VIRTIO_NO_VECTOR) {
155 return msix_vector_use(&proxy->pci_dev, vector);
157 return 0;
160 static void virtio_pci_reset(void *opaque)
162 VirtIOPCIProxy *proxy = opaque;
163 virtio_reset(proxy->vdev);
164 msix_reset(&proxy->pci_dev);
167 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
169 VirtIOPCIProxy *proxy = opaque;
170 VirtIODevice *vdev = proxy->vdev;
171 target_phys_addr_t pa;
173 switch (addr) {
174 case VIRTIO_PCI_GUEST_FEATURES:
175 /* Guest does not negotiate properly? We have to assume nothing. */
176 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
177 if (vdev->bad_features)
178 val = vdev->bad_features(vdev);
179 else
180 val = 0;
182 if (vdev->set_features)
183 vdev->set_features(vdev, val);
184 vdev->features = val;
185 break;
186 case VIRTIO_PCI_QUEUE_PFN:
187 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
188 if (pa == 0)
189 virtio_pci_reset(proxy);
190 else
191 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
192 break;
193 case VIRTIO_PCI_QUEUE_SEL:
194 if (val < VIRTIO_PCI_QUEUE_MAX)
195 vdev->queue_sel = val;
196 break;
197 case VIRTIO_PCI_QUEUE_NOTIFY:
198 virtio_queue_notify(vdev, val);
199 break;
200 case VIRTIO_PCI_STATUS:
201 vdev->status = val & 0xFF;
202 if (vdev->status == 0)
203 virtio_pci_reset(proxy);
204 break;
205 case VIRTIO_MSI_CONFIG_VECTOR:
206 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
207 /* Make it possible for guest to discover an error took place. */
208 if (msix_vector_use(&proxy->pci_dev, val) < 0)
209 val = VIRTIO_NO_VECTOR;
210 vdev->config_vector = val;
211 break;
212 case VIRTIO_MSI_QUEUE_VECTOR:
213 msix_vector_unuse(&proxy->pci_dev,
214 virtio_queue_vector(vdev, vdev->queue_sel));
215 /* Make it possible for guest to discover an error took place. */
216 if (msix_vector_use(&proxy->pci_dev, val) < 0)
217 val = VIRTIO_NO_VECTOR;
218 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
219 break;
220 default:
221 fprintf(stderr, "%s: unexpected address 0x%x value 0x%x\n",
222 __func__, addr, val);
223 break;
227 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
229 VirtIODevice *vdev = proxy->vdev;
230 uint32_t ret = 0xFFFFFFFF;
232 switch (addr) {
233 case VIRTIO_PCI_HOST_FEATURES:
234 ret = vdev->get_features(vdev);
235 ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
236 ret |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
237 ret |= (1 << VIRTIO_F_BAD_FEATURE);
238 break;
239 case VIRTIO_PCI_GUEST_FEATURES:
240 ret = vdev->features;
241 break;
242 case VIRTIO_PCI_QUEUE_PFN:
243 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
244 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
245 break;
246 case VIRTIO_PCI_QUEUE_NUM:
247 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
248 break;
249 case VIRTIO_PCI_QUEUE_SEL:
250 ret = vdev->queue_sel;
251 break;
252 case VIRTIO_PCI_STATUS:
253 ret = vdev->status;
254 break;
255 case VIRTIO_PCI_ISR:
256 /* reading from the ISR also clears it. */
257 ret = vdev->isr;
258 vdev->isr = 0;
259 qemu_set_irq(proxy->pci_dev.irq[0], 0);
260 break;
261 case VIRTIO_MSI_CONFIG_VECTOR:
262 ret = vdev->config_vector;
263 break;
264 case VIRTIO_MSI_QUEUE_VECTOR:
265 ret = virtio_queue_vector(vdev, vdev->queue_sel);
266 break;
267 default:
268 break;
271 return ret;
274 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
276 VirtIOPCIProxy *proxy = opaque;
277 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
278 addr -= proxy->addr;
279 if (addr < config)
280 return virtio_ioport_read(proxy, addr);
281 addr -= config;
282 return virtio_config_readb(proxy->vdev, addr);
285 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
287 VirtIOPCIProxy *proxy = opaque;
288 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
289 addr -= proxy->addr;
290 if (addr < config)
291 return virtio_ioport_read(proxy, addr);
292 addr -= config;
293 return virtio_config_readw(proxy->vdev, addr);
296 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
298 VirtIOPCIProxy *proxy = opaque;
299 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
300 addr -= proxy->addr;
301 if (addr < config)
302 return virtio_ioport_read(proxy, addr);
303 addr -= config;
304 return virtio_config_readl(proxy->vdev, addr);
307 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
309 VirtIOPCIProxy *proxy = opaque;
310 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
311 addr -= proxy->addr;
312 if (addr < config) {
313 virtio_ioport_write(proxy, addr, val);
314 return;
316 addr -= config;
317 virtio_config_writeb(proxy->vdev, addr, val);
320 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
322 VirtIOPCIProxy *proxy = opaque;
323 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
324 addr -= proxy->addr;
325 if (addr < config) {
326 virtio_ioport_write(proxy, addr, val);
327 return;
329 addr -= config;
330 virtio_config_writew(proxy->vdev, addr, val);
333 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
335 VirtIOPCIProxy *proxy = opaque;
336 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
337 addr -= proxy->addr;
338 if (addr < config) {
339 virtio_ioport_write(proxy, addr, val);
340 return;
342 addr -= config;
343 virtio_config_writel(proxy->vdev, addr, val);
346 static void virtio_map(PCIDevice *pci_dev, int region_num,
347 uint32_t addr, uint32_t size, int type)
349 VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
350 VirtIODevice *vdev = proxy->vdev;
351 unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
353 proxy->addr = addr;
355 register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
356 register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
357 register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
358 register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
359 register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
360 register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
362 if (vdev->config_len)
363 vdev->get_config(vdev, vdev->config);
366 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
367 uint32_t val, int len)
369 pci_default_write_config(pci_dev, address, val, len);
370 msix_write_config(pci_dev, address, val, len);
373 static const VirtIOBindings virtio_pci_bindings = {
374 .notify = virtio_pci_notify,
375 .save_config = virtio_pci_save_config,
376 .load_config = virtio_pci_load_config,
377 .save_queue = virtio_pci_save_queue,
378 .load_queue = virtio_pci_load_queue,
381 static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
382 uint16_t vendor, uint16_t device,
383 uint16_t class_code, uint8_t pif)
385 uint8_t *config;
386 uint32_t size;
388 proxy->vdev = vdev;
390 config = proxy->pci_dev.config;
391 pci_config_set_vendor_id(config, vendor);
392 pci_config_set_device_id(config, device);
394 config[0x08] = VIRTIO_PCI_ABI_VERSION;
396 config[0x09] = pif;
397 pci_config_set_class(config, class_code);
398 config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;
400 config[0x2c] = vendor & 0xFF;
401 config[0x2d] = (vendor >> 8) & 0xFF;
402 config[0x2e] = vdev->device_id & 0xFF;
403 config[0x2f] = (vdev->device_id >> 8) & 0xFF;
405 config[0x3d] = 1;
407 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
408 pci_register_bar(&proxy->pci_dev, 1,
409 msix_bar_size(&proxy->pci_dev),
410 PCI_ADDRESS_SPACE_MEM,
411 msix_mmio_map);
412 proxy->pci_dev.config_write = virtio_write_config;
413 proxy->pci_dev.unregister = msix_uninit;
414 } else
415 vdev->nvectors = 0;
417 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
418 if (size & (size-1))
419 size = 1 << qemu_fls(size);
421 pci_register_bar(&proxy->pci_dev, 0, size, PCI_ADDRESS_SPACE_IO,
422 virtio_map);
424 qemu_register_reset(virtio_pci_reset, proxy);
426 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
429 static void virtio_blk_init_pci_with_class(PCIDevice *pci_dev,
430 uint16_t class_code)
432 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
433 VirtIODevice *vdev;
435 vdev = virtio_blk_init(&pci_dev->qdev);
436 virtio_init_pci(proxy, vdev,
437 PCI_VENDOR_ID_REDHAT_QUMRANET,
438 PCI_DEVICE_ID_VIRTIO_BLOCK,
439 class_code, 0x00);
442 static void virtio_blk_init_pci(PCIDevice *pci_dev)
444 virtio_blk_init_pci_with_class(pci_dev, PCI_CLASS_STORAGE_SCSI);
447 static void virtio_blk_init_pci_0_10(PCIDevice *pci_dev)
449 virtio_blk_init_pci_with_class(pci_dev, PCI_CLASS_STORAGE_OTHER);
452 static void virtio_console_init_pci_with_class(PCIDevice *pci_dev,
453 uint16_t class_code)
455 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
456 VirtIODevice *vdev;
458 vdev = virtio_console_init(&pci_dev->qdev);
459 virtio_init_pci(proxy, vdev,
460 PCI_VENDOR_ID_REDHAT_QUMRANET,
461 PCI_DEVICE_ID_VIRTIO_CONSOLE,
462 class_code, 0x00);
465 static void virtio_console_init_pci(PCIDevice *pci_dev)
467 virtio_console_init_pci_with_class(pci_dev, PCI_CLASS_SERIAL_OTHER);
470 static void virtio_console_init_pci_0_10(PCIDevice *pci_dev)
472 virtio_console_init_pci_with_class(pci_dev, PCI_CLASS_DISPLAY_OTHER);
475 static void virtio_net_init_pci(PCIDevice *pci_dev)
477 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
478 VirtIODevice *vdev;
480 vdev = virtio_net_init(&pci_dev->qdev);
481 virtio_init_pci(proxy, vdev,
482 PCI_VENDOR_ID_REDHAT_QUMRANET,
483 PCI_DEVICE_ID_VIRTIO_NET,
484 PCI_CLASS_NETWORK_ETHERNET,
485 0x00);
488 static void virtio_balloon_init_pci(PCIDevice *pci_dev)
490 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
491 VirtIODevice *vdev;
493 vdev = virtio_balloon_init(&pci_dev->qdev);
494 virtio_init_pci(proxy, vdev,
495 PCI_VENDOR_ID_REDHAT_QUMRANET,
496 PCI_DEVICE_ID_VIRTIO_BALLOON,
497 PCI_CLASS_MEMORY_RAM,
498 0x00);
501 static PCIDeviceInfo virtio_info[] = {
503 .qdev.name = "virtio-blk-pci",
504 .qdev.size = sizeof(VirtIOPCIProxy),
505 .init = virtio_blk_init_pci,
507 .qdev.name = "virtio-net-pci",
508 .qdev.size = sizeof(VirtIOPCIProxy),
509 .init = virtio_net_init_pci,
511 .qdev.name = "virtio-console-pci",
512 .qdev.size = sizeof(VirtIOPCIProxy),
513 .init = virtio_console_init_pci,
515 .qdev.name = "virtio-balloon-pci",
516 .qdev.size = sizeof(VirtIOPCIProxy),
517 .init = virtio_balloon_init_pci,
519 /* For compatibility with 0.10 */
520 .qdev.name = "virtio-blk-pci-0-10",
521 .qdev.size = sizeof(VirtIOPCIProxy),
522 .init = virtio_blk_init_pci_0_10,
524 .qdev.name = "virtio-console-pci-0-10",
525 .qdev.size = sizeof(VirtIOPCIProxy),
526 .init = virtio_console_init_pci_0_10,
528 /* end of list */
532 static void virtio_pci_register_devices(void)
534 pci_qdev_register_many(virtio_info);
537 device_init(virtio_pci_register_devices)