2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
22 #include <devices/lnx_virtio.h>
23 #include <devices/pci.h>
34 } __attribute__((packed));
38 struct blk_config cfg;
40 struct vm_device * pci_bus;
45 static int virtio_pci_write(uint16_t port, void * src, uint_t length, struct vm_device * dev) {
50 static int virtio_pci_read(uint16_t port, void * dst, uint_t length, struct vm_device * dev) {
56 static int virtio_free(struct vm_device * dev) {
62 static struct v3_device_ops dev_ops = {
73 static int virtio_init(struct guest_info * vm, void * cfg_data) {
74 struct blk_state * virtio = NULL;
75 struct vm_device * pci_bus = (struct vm_device *)cfg_data;
78 PrintDebug("Initializing VIRTIO Block device\n");
80 if (pci_bus == NULL) {
81 PrintError("VirtIO requires a PCI bus\n");
85 virtio = (struct blk_state *)V3_Malloc(sizeof(struct blk_state));
87 virtio->pci_bus = pci_bus;
89 struct vm_device * dev = v3_allocate_device("VIRTIO_BLK", &dev_ops, virtio);
90 if (v3_attach_device(vm, dev) == -1) {
91 PrintError("Could not attach device %s\n", "LNX_VIRTIO_BLK");
97 if (virtio->pci_bus != NULL) {
98 struct v3_pci_bar bars[6];
99 struct pci_device * pci_dev = NULL;
101 int num_ports_pow2 = 1;
102 int num_ports = sizeof(struct virtio_config) + sizeof(struct blk_config);
105 // This gets the number of ports, rounded up to a power of 2
106 while (num_ports > 0) {
108 num_ports_pow2 <<= 1;
112 num_ports = sizeof(struct virtio_config) + sizeof(struct blk_config);
114 if (num_ports & ((num_ports_pow2 >> 1) - 1)) {
115 num_ports_pow2 >>= 1;
119 for (i = 0; i < 6; i++) {
120 bars[i].type = PCI_BAR_NONE;
123 bars[0].type = PCI_BAR_IO;
124 bars[0].default_base_port = -1;
125 bars[0].num_ports = num_ports_pow2;
127 bars[0].io_read = virtio_pci_read;
128 bars[0].io_write = virtio_pci_write;
130 pci_dev = v3_pci_register_device(virtio->pci_bus, PCI_STD_DEVICE,
131 0, PCI_AUTO_DEV_NUM, 0,
133 NULL, NULL, NULL, dev);
136 PrintError("Could not register PCI Device\n");
140 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
141 pci_dev->config_header.device_id = VIRTIO_BLOCK_DEV_ID;
142 pci_dev->config_header.class = PCI_CLASS_STORAGE;
143 pci_dev->config_header.subclass = PCI_STORAGE_SUBCLASS_OTHER;
145 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
146 pci_dev->config_header.subsystem_id = VIRTIO_BLOCK_SUBDEVICE_ID;
148 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
157 device_register("LNX_VIRTIO_BLK", virtio_init)