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 <palacios/vm_guest_mem.h>
23 #include <devices/lnx_virtio_pci.h>
25 #include <devices/pci.h>
28 #define SYM_MAGIC_PORT 20
29 #define SYM_SWAP_BASE_PORT 24
30 #define SYM_SWAP_SIZE_PORT 32
33 #define QUEUE_SIZE 128
39 } __attribute__((packed));
43 struct virtio_sym_state {
44 struct sym_config sym_cfg;
45 struct virtio_config virtio_cfg;
48 struct vm_device * pci_bus;
49 struct pci_device * pci_dev;
51 struct virtio_queue queue[1];
53 struct virtio_queue * cur_queue;
62 static int virtio_reset(struct virtio_sym_state * virtio) {
64 memset(virtio->queue, 0, sizeof(struct virtio_queue));
66 virtio->cur_queue = &(virtio->queue[0]);
69 virtio->virtio_cfg.status = 0;
70 virtio->virtio_cfg.pci_isr = 0;
72 virtio->queue[1].queue_size = QUEUE_SIZE;
75 memset(&(virtio->sym_cfg), 0, sizeof(struct sym_config));
81 static int get_desc_count(struct virtio_queue * q, int index) {
82 struct vring_desc * tmp_desc = &(q->desc[index]);
85 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
86 tmp_desc = &(q->desc[tmp_desc->next]);
94 static int handle_kick(struct guest_info * core, struct virtio_sym_state * sym_state) {
95 struct virtio_queue * q = sym_state->cur_queue;
99 PrintDebug("VIRTIO Symbiotic KICK: cur_index=%d (mod=%d), avail_index=%d\n",
100 q->cur_avail_idx, q->cur_avail_idx % QUEUE_SIZE, q->avail->index);
102 while (q->cur_avail_idx < q->avail->index) {
103 struct vring_desc * tmp_desc = NULL;
104 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
105 int desc_cnt = get_desc_count(q, desc_idx);
107 uint32_t req_len = 0;
110 PrintDebug("Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE);
112 for (i = 0; i < desc_cnt; i++) {
114 tmp_desc = &(q->desc[desc_idx]);
116 PrintDebug("Header Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n",
118 (void *)(addr_t)(tmp_desc->addr_gpa), tmp_desc->length,
119 tmp_desc->flags, tmp_desc->next);
122 if (v3_gpa_to_hva(core, tmp_desc->addr_gpa, (addr_t *)&(page_addr)) == -1) {
123 PrintError("Could not translate block header address\n");
128 if (handle_sym_op(dev, tmp_desc, buf_desc, status_desc) == -1) {
129 PrintError("Error handling symbiotic operation\n");
134 PrintDebug("Symbiotic Device Currently Ignored\n");
137 req_len += tmp_desc->length;
138 desc_idx = tmp_desc->next;
141 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
142 q->used->ring[q->used->index % QUEUE_SIZE].length = req_len; // What do we set this to????
148 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
149 PrintDebug("Raising IRQ %d\n", sym_state->pci_dev->config_header.intr_line);
150 v3_pci_raise_irq(sym_state->pci_bus, 0, sym_state->pci_dev);
151 sym_state->virtio_cfg.pci_isr = VIRTIO_ISR_ACTIVE;
158 static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * private_data) {
159 struct virtio_sym_state * sym_state = (struct virtio_sym_state *)private_data;
160 int port_idx = port % sym_state->io_range_size;
164 PrintDebug("VIRTIO SYMBIOTIC Write for port %d (index=%d) len=%d, value=%x\n",
165 port, port_idx, length, *(uint32_t *)src);
170 case GUEST_FEATURES_PORT:
172 PrintError("Illegal write length for guest features\n");
176 sym_state->virtio_cfg.guest_features = *(uint32_t *)src;
179 case VRING_PG_NUM_PORT:
181 addr_t pfn = *(uint32_t *)src;
182 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
185 sym_state->cur_queue->pfn = pfn;
187 sym_state->cur_queue->ring_desc_addr = page_addr ;
188 sym_state->cur_queue->ring_avail_addr = page_addr + (QUEUE_SIZE * sizeof(struct vring_desc));
189 sym_state->cur_queue->ring_used_addr = ( sym_state->cur_queue->ring_avail_addr + \
190 sizeof(struct vring_avail) + \
191 (QUEUE_SIZE * sizeof(uint16_t)));
193 // round up to next page boundary.
194 sym_state->cur_queue->ring_used_addr = (sym_state->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
196 if (v3_gpa_to_hva(core, sym_state->cur_queue->ring_desc_addr, (addr_t *)&(sym_state->cur_queue->desc)) == -1) {
197 PrintError("Could not translate ring descriptor address\n");
202 if (v3_gpa_to_hva(core, sym_state->cur_queue->ring_avail_addr, (addr_t *)&(sym_state->cur_queue->avail)) == -1) {
203 PrintError("Could not translate ring available address\n");
208 if (v3_gpa_to_hva(core, sym_state->cur_queue->ring_used_addr, (addr_t *)&(sym_state->cur_queue->used)) == -1) {
209 PrintError("Could not translate ring used address\n");
213 PrintDebug("RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
214 (void *)(sym_state->cur_queue->ring_desc_addr),
215 (void *)(sym_state->cur_queue->ring_avail_addr),
216 (void *)(sym_state->cur_queue->ring_used_addr));
218 PrintDebug("RingDesc=%p, Avail=%p, Used=%p\n",
219 sym_state->cur_queue->desc, sym_state->cur_queue->avail, sym_state->cur_queue->used);
222 PrintError("Illegal write length for page frame number\n");
226 case VRING_Q_SEL_PORT:
227 sym_state->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
229 if (sym_state->virtio_cfg.vring_queue_selector > 0) {
230 PrintError("Virtio Symbiotic device has not qeueues. Selected %d\n",
231 sym_state->virtio_cfg.vring_queue_selector);
235 sym_state->cur_queue = &(sym_state->queue[sym_state->virtio_cfg.vring_queue_selector]);
238 case VRING_Q_NOTIFY_PORT:
239 PrintDebug("Handling Kick\n");
240 if (handle_kick(core, sym_state) == -1) {
241 PrintError("Could not handle Symbiotic Notification\n");
245 case VIRTIO_STATUS_PORT:
246 sym_state->virtio_cfg.status = *(uint8_t *)src;
248 if (sym_state->virtio_cfg.status == 0) {
249 PrintDebug("Resetting device\n");
250 virtio_reset(sym_state);
255 case VIRTIO_ISR_PORT:
256 sym_state->virtio_cfg.pci_isr = *(uint8_t *)src;
267 static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * private_data) {
269 struct virtio_sym_state * sym_state = (struct virtio_sym_state *)private_data;
270 int port_idx = port % sym_state->io_range_size;
273 PrintDebug("VIRTIO SYMBIOTIC Read for port %d (index =%d), length=%d\n",
274 port, port_idx, length);
277 case HOST_FEATURES_PORT:
279 PrintError("Illegal read length for host features\n");
283 *(uint32_t *)dst = sym_state->virtio_cfg.host_features;
286 case VRING_PG_NUM_PORT:
288 PrintError("Illegal read length for page frame number\n");
292 *(uint32_t *)dst = sym_state->cur_queue->pfn;
295 case VRING_SIZE_PORT:
297 PrintError("Illegal read length for vring size\n");
301 *(uint16_t *)dst = sym_state->cur_queue->queue_size;
305 case VIRTIO_STATUS_PORT:
307 PrintError("Illegal read length for status\n");
311 *(uint8_t *)dst = sym_state->virtio_cfg.status;
314 case VIRTIO_ISR_PORT:
315 *(uint8_t *)dst = sym_state->virtio_cfg.pci_isr;
316 sym_state->virtio_cfg.pci_isr = 0;
317 v3_pci_lower_irq(sym_state->pci_bus, 0, sym_state->pci_dev);
321 if ( (port_idx >= sizeof(struct virtio_config)) &&
322 (port_idx < (sizeof(struct virtio_config) + sizeof(struct sym_config))) ) {
323 int cfg_offset = port_idx - sizeof(struct virtio_config);
324 uint8_t * cfg_ptr = (uint8_t *)&(sym_state->sym_cfg);
326 memcpy(dst, cfg_ptr + cfg_offset, length);
329 PrintError("Read of Unhandled Virtio Read\n");
343 static struct v3_device_ops dev_ops = {
350 static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
351 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
352 struct virtio_sym_state * virtio_state = NULL;
353 struct pci_device * pci_dev = NULL;
354 char * dev_id = v3_cfg_val(cfg, "ID");
356 PrintDebug("Initializing VIRTIO Symbiotic device\n");
358 if (pci_bus == NULL) {
359 PrintError("VirtIO devices require a PCI Bus");
364 virtio_state = (struct virtio_sym_state *)V3_Malloc(sizeof(struct virtio_sym_state));
365 memset(virtio_state, 0, sizeof(struct virtio_sym_state));
368 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, virtio_state);
369 if (v3_attach_device(vm, dev) == -1) {
370 PrintError("Could not attach device %s\n", dev_id);
375 // PCI initialization
377 struct v3_pci_bar bars[6];
378 int num_ports = sizeof(struct virtio_config) + sizeof(struct sym_config);
379 int tmp_ports = num_ports;
384 // This gets the number of ports, rounded up to a power of 2
385 virtio_state->io_range_size = 1; // must be a power of 2
387 while (tmp_ports > 0) {
389 virtio_state->io_range_size <<= 1;
392 // this is to account for any low order bits being set in num_ports
393 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
394 if ((num_ports & ((virtio_state->io_range_size >> 1) - 1)) == 0) {
395 virtio_state->io_range_size >>= 1;
399 for (i = 0; i < 6; i++) {
400 bars[i].type = PCI_BAR_NONE;
403 bars[0].type = PCI_BAR_IO;
404 bars[0].default_base_port = -1;
405 bars[0].num_ports = virtio_state->io_range_size;
407 bars[0].io_read = virtio_io_read;
408 bars[0].io_write = virtio_io_write;
409 bars[0].private_data = virtio_state;
411 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
412 0, PCI_AUTO_DEV_NUM, 0,
413 "LNX_VIRTIO_SYM", bars,
414 NULL, NULL, NULL, virtio_state);
417 PrintError("Could not register PCI Device\n");
421 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
422 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
425 pci_dev->config_header.device_id = VIRTIO_SYMBIOTIC_DEV_ID;
426 pci_dev->config_header.class = PCI_CLASS_MEMORY;
427 pci_dev->config_header.subclass = PCI_MEM_SUBCLASS_RAM;
429 pci_dev->config_header.subsystem_id = VIRTIO_SYMBIOTIC_SUBDEVICE_ID;
432 pci_dev->config_header.intr_pin = 1;
434 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
437 virtio_state->pci_dev = pci_dev;
438 virtio_state->pci_bus = pci_bus;
441 virtio_reset(virtio_state);
448 device_register("LNX_VIRTIO_SYM", virtio_init)