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>
21 #include <palacios/vmm_dev_mgr.h>
22 #include <devices/lnx_virtio_pci.h>
25 #include <devices/pci.h>
35 } __attribute__((packed));
40 /* Host Feature flags */
41 #define VIRTIO_BARRIER 0x01 /* Does host support barriers? */
42 #define VIRTIO_SIZE_MAX 0x02 /* Indicates maximum segment size */
43 #define VIRTIO_SEG_MAX 0x04 /* Indicates maximum # of segments */
44 #define VIRTIO_LEGACY_GEOM 0x10 /* Indicates support of legacy geometry */
48 struct virtio_blk_state {
49 struct blk_config block_cfg;
50 struct virtio_config virtio_cfg;
52 struct vm_device * pci_bus;
53 struct pci_device * pci_dev;
55 struct virtio_device * virtio_dev; // the virtio device struction for _this_ device
64 static int virtio_io_write(uint16_t port, void * src, uint_t length, struct vm_device * dev) {
65 struct virtio_blk_state * virtio = (struct virtio_blk_state *)dev->private_data;
66 int port_idx = port % virtio->io_range_size;
69 PrintDebug("VIRTIO BLOCK Write for port %d (index=%d) len=%d, value=%x\n",
70 port, port_idx, length, *(uint32_t *)src);
75 case VRING_Q_NOTIFY_PORT:
77 PrintError("Notification\n");
80 case VIRTIO_STATUS_PORT:
81 if (virtio->virtio_cfg.status == 0) {
82 PrintDebug("Resetting device\n");
99 static int virtio_io_read(uint16_t port, void * dst, uint_t length, struct vm_device * dev) {
100 struct virtio_blk_state * virtio = (struct virtio_blk_state *)dev->private_data;
101 int port_idx = port % virtio->io_range_size;
104 PrintDebug("VIRTIO BLOCK Read for port %d (index =%d), length=%d\n",
105 port, port_idx, length);
108 // search for device....
109 // call and return dev config read
119 static int virtio_free(struct vm_device * dev) {
126 static struct v3_device_ops dev_ops = {
138 static int virtio_init(struct guest_info * vm, void * cfg_data) {
139 struct vm_device * pci_bus = v3_find_dev(vm, (char *)cfg_data);
140 struct virtio_blk_state * virtio_state = NULL;
141 struct pci_device * pci_dev = NULL;
143 PrintDebug("Initializing VIRTIO Block device\n");
145 if (pci_bus == NULL) {
146 PrintError("VirtIO devices require a PCI Bus");
151 virtio_state = (struct virtio_blk_state *)V3_Malloc(sizeof(struct virtio_blk_state));
152 memset(virtio_state, 0, sizeof(struct virtio_blk_state));
155 struct vm_device * dev = v3_allocate_device("LNX_VIRTIO_BLK", &dev_ops, virtio_state);
156 if (v3_attach_device(vm, dev) == -1) {
157 PrintError("Could not attach device %s\n", "LNX_VIRTIO_BLK");
162 // PCI initialization
164 struct v3_pci_bar bars[6];
165 int num_ports = sizeof(struct virtio_config) + sizeof(struct blk_config);
166 int tmp_ports = num_ports;
171 // This gets the number of ports, rounded up to a power of 2
172 virtio_state->io_range_size = 1; // must be a power of 2
174 while (tmp_ports > 0) {
176 virtio_state->io_range_size <<= 1;
179 // this is to account for any low order bits being set in num_ports
180 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
181 if ((num_ports & ((virtio_state->io_range_size >> 1) - 1)) == 0) {
182 virtio_state->io_range_size >>= 1;
186 for (i = 0; i < 6; i++) {
187 bars[i].type = PCI_BAR_NONE;
190 bars[0].type = PCI_BAR_IO;
191 bars[0].default_base_port = -1;
192 bars[0].num_ports = virtio_state->io_range_size;
194 bars[0].io_read = virtio_io_read;
195 bars[0].io_write = virtio_io_write;
197 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
198 0, PCI_AUTO_DEV_NUM, 0,
199 "LNX_VIRTIO_BLK", bars,
200 NULL, NULL, NULL, dev);
203 PrintError("Could not register PCI Device\n");
207 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
208 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
211 pci_dev->config_header.device_id = VIRTIO_BLOCK_DEV_ID;
212 pci_dev->config_header.class = PCI_CLASS_STORAGE;
213 pci_dev->config_header.subclass = PCI_STORAGE_SUBCLASS_OTHER;
215 pci_dev->config_header.subsystem_id = VIRTIO_BLOCK_SUBDEVICE_ID;
218 pci_dev->config_header.intr_pin = 1;
220 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
223 virtio_state->pci_dev = pci_dev;
224 virtio_state->pci_bus = pci_bus;
226 /* Block configuration */
233 device_register("LNX_VIRTIO_BLK", virtio_init)