uint8_t dev_map[MAX_BUS_DEVICES / 8];
- int (*raise_pci_irq)(struct vm_device * dev, struct pci_device * pci_dev);
- int (*lower_pci_irq)(struct vm_device * dev, struct pci_device * pci_dev);
- struct vm_device * irq_bridge_dev;
+ int (*raise_pci_irq)(struct pci_device * pci_dev, void * dev_data);
+ int (*lower_pci_irq)(struct pci_device * pci_dev, void * dev_data);
+ void * irq_dev_data;
};
-static int addr_port_read(struct guest_info * core, ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
- struct pci_internal * pci_state = (struct pci_internal *)dev->private_data;
+static int addr_port_read(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
+ struct pci_internal * pci_state = priv_data;
int reg_offset = port & 0x3;
uint8_t * reg_addr = ((uint8_t *)&(pci_state->addr_reg.val)) + reg_offset;
}
-static int addr_port_write(struct guest_info * core, ushort_t port, void * src, uint_t length, struct vm_device * dev) {
- struct pci_internal * pci_state = (struct pci_internal *)dev->private_data;
+static int addr_port_write(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
+ struct pci_internal * pci_state = priv_data;
int reg_offset = port & 0x3;
uint8_t * reg_addr = ((uint8_t *)&(pci_state->addr_reg.val)) + reg_offset;
}
-static int data_port_read(struct guest_info * core, ushort_t port, void * dst, uint_t length, struct vm_device * vmdev) {
- struct pci_internal * pci_state = (struct pci_internal *)(vmdev->private_data);
+static int data_port_read(struct guest_info * core, ushort_t port, void * dst, uint_t length, void * priv_data) {
+ struct pci_internal * pci_state = priv_data;
struct pci_device * pci_dev = NULL;
uint_t reg_num = (pci_state->addr_reg.reg_num << 2) + (port & 0x3);
int i;
}
-static int data_port_write(struct guest_info * core, ushort_t port, void * src, uint_t length, struct vm_device * vmdev) {
- struct pci_internal * pci_state = (struct pci_internal *)vmdev->private_data;
+static int data_port_write(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
+ struct pci_internal * pci_state = priv_data;
struct pci_device * pci_dev = NULL;
uint_t reg_num = (pci_state->addr_reg.reg_num << 2) + (port & 0x3);
int i;
-static int pci_reset_device(struct vm_device * dev) {
- PrintDebug("pci: reset device\n");
- return 0;
-}
-
-
-static int pci_start_device(struct vm_device * dev) {
- PrintDebug("pci: start device\n");
- return 0;
-}
-
-
-static int pci_stop_device(struct vm_device * dev) {
- PrintDebug("pci: stop device\n");
- return 0;
-}
-
-
-
-static int pci_free(struct vm_device * dev) {
- int i = 0;
-
- for (i = 0; i < 4; i++){
- v3_dev_unhook_io(dev, CONFIG_ADDR_PORT + i);
- v3_dev_unhook_io(dev, CONFIG_DATA_PORT + i);
- }
-
- return 0;
-}
-
static void init_pci_busses(struct pci_internal * pci_state) {
}
+static int pci_free(struct pci_internal * pci_state) {
+ int i;
+
+
+ // cleanup devices
+ for (i = 0; i < PCI_BUS_COUNT; i++) {
+ struct pci_bus * bus = &(pci_state->bus_list[i]);
+ struct rb_node * node = v3_rb_first(&(bus->devices));
+ struct pci_device * dev = NULL;
+
+ while (node) {
+ dev = rb_entry(node, struct pci_device, dev_tree_node);
+ node = v3_rb_next(node);
+
+ v3_rb_erase(&(dev->dev_tree_node), &(bus->devices));
+ V3_Free(dev);
+ }
+
+ }
+
+ V3_Free(pci_state);
+ return 0;
+}
static struct v3_device_ops dev_ops = {
- .free = pci_free,
- .reset = pci_reset_device,
- .start = pci_start_device,
- .stop = pci_stop_device,
+ .free = (int (*)(void *))pci_free,
+
};
struct pci_internal * pci_state = V3_Malloc(sizeof(struct pci_internal));
int i = 0;
char * dev_id = v3_cfg_val(cfg, "ID");
+ int ret = 0;
PrintDebug("PCI internal at %p\n",(void *)pci_state);
- struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, pci_state);
+ struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, pci_state);
- if (v3_attach_device(vm, dev) == -1) {
+ if (dev == NULL) {
PrintError("Could not attach device %s\n", dev_id);
+ V3_Free(pci_state);
return -1;
}
PrintDebug("Sizeof config header=%d\n", (int)sizeof(struct pci_config_header));
for (i = 0; i < 4; i++) {
- v3_dev_hook_io(dev, CONFIG_ADDR_PORT + i, &addr_port_read, &addr_port_write);
- v3_dev_hook_io(dev, CONFIG_DATA_PORT + i, &data_port_read, &data_port_write);
+ ret |= v3_dev_hook_io(dev, CONFIG_ADDR_PORT + i, &addr_port_read, &addr_port_write);
+ ret |= v3_dev_hook_io(dev, CONFIG_DATA_PORT + i, &data_port_read, &data_port_write);
+ }
+
+ if (ret != 0) {
+ PrintError("Error hooking PCI IO ports\n");
+ v3_remove_device(dev);
+ return -1;
}
return 0;
int v3_pci_set_irq_bridge(struct vm_device * pci_bus, int bus_num,
- int (*raise_pci_irq)(struct vm_device * dev, struct pci_device * pci_dev),
- int (*lower_pci_irq)(struct vm_device * dev, struct pci_device * pci_dev),
- struct vm_device * bridge_dev) {
+ int (*raise_pci_irq)(struct pci_device * pci_dev, void * dev_data),
+ int (*lower_pci_irq)(struct pci_device * pci_dev, void * dev_data),
+ void * priv_data) {
struct pci_internal * pci_state = (struct pci_internal *)pci_bus->private_data;
pci_state->bus_list[bus_num].raise_pci_irq = raise_pci_irq;
pci_state->bus_list[bus_num].lower_pci_irq = lower_pci_irq;
- pci_state->bus_list[bus_num].irq_bridge_dev = bridge_dev;
+ pci_state->bus_list[bus_num].irq_dev_data = priv_data;
return 0;
}
struct pci_internal * pci_state = (struct pci_internal *)pci_bus->private_data;
struct pci_bus * bus = &(pci_state->bus_list[bus_num]);
- return bus->raise_pci_irq(bus->irq_bridge_dev, dev);
+ return bus->raise_pci_irq(dev, bus->irq_dev_data);
}
int v3_pci_lower_irq(struct vm_device * pci_bus, int bus_num, struct pci_device * dev) {
struct pci_internal * pci_state = (struct pci_internal *)pci_bus->private_data;
struct pci_bus * bus = &(pci_state->bus_list[bus_num]);
- return bus->lower_pci_irq(bus->irq_bridge_dev, dev);
+ return bus->lower_pci_irq(dev, bus->irq_dev_data);
}
// if dev_num == -1, auto assign