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) {
54 static int virtio_init(struct vm_device * dev) {
55 struct blk_state * virtio = (struct blk_state *)(dev->private_data);
57 PrintDebug("Initializing VIRTIO Block device\n");
59 if (virtio->pci_bus != NULL) {
60 struct v3_pci_bar bars[6];
61 struct pci_device * pci_dev = NULL;
63 int num_ports_pow2 = 1;
64 int num_ports = sizeof(struct virtio_config) + sizeof(struct blk_config);
67 // This gets the number of ports, rounded up to a power of 2
68 while (num_ports > 0) {
74 num_ports = sizeof(struct virtio_config) + sizeof(struct blk_config);
76 if (num_ports & ((num_ports_pow2 >> 1) - 1)) {
81 for (i = 0; i < 6; i++) {
82 bars[i].type = PCI_BAR_NONE;
85 bars[0].type = PCI_BAR_IO;
86 bars[0].default_base_port = -1;
87 bars[0].num_ports = num_ports_pow2;
89 bars[0].io_read = virtio_pci_read;
90 bars[0].io_write = virtio_pci_write;
92 pci_dev = v3_pci_register_device(virtio->pci_bus, PCI_STD_DEVICE,
93 0, PCI_AUTO_DEV_NUM, 0,
95 NULL, NULL, NULL, dev);
98 PrintError("Could not register PCI Device\n");
102 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
103 pci_dev->config_header.device_id = VIRTIO_BLOCK_DEV_ID;
104 pci_dev->config_header.class = PCI_CLASS_STORAGE;
105 pci_dev->config_header.subclass = PCI_STORAGE_SUBCLASS_OTHER;
107 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
108 pci_dev->config_header.subsystem_id = VIRTIO_BLOCK_SUBDEVICE_ID;
110 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
118 static int virtio_deinit(struct vm_device * dev) {
124 static struct vm_device_ops dev_ops = {
126 .deinit = virtio_deinit,
134 struct vm_device * v3_create_virtio(struct vm_device * pci_bus) {
135 struct blk_state * virtio = NULL;
137 PrintDebug("Creating VirtIO Block device\n");
139 if (pci_bus == NULL) {
140 PrintError("VirtIO requires a PCI bus\n");
145 virtio = (struct blk_state *)V3_Malloc(sizeof(struct blk_state));
147 virtio->pci_bus = pci_bus;
149 return v3_create_device("VIRTIO_BLK", &dev_ops, virtio);