X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Fpci.c;h=986cea4cbe83e8595b4250553933c79b345b65a3;hb=fb7ae05e81eee43ecb4b1888b32d4d4a17b12f28;hp=148a0380088527f3495668f7a9351e70e2e63359;hpb=b35cb9f7d22a06d849820c8d4487c66f74ec42e2;p=palacios.git diff --git a/palacios/src/devices/pci.c b/palacios/src/devices/pci.c index 148a038..986cea4 100644 --- a/palacios/src/devices/pci.c +++ b/palacios/src/devices/pci.c @@ -88,10 +88,34 @@ struct pci_internal { -#ifdef PCI_DEBUG -static void pci_dump_state(struct pci_internal * pci_state); + + +#ifdef DEBUG_PCI + +static void pci_dump_state(struct pci_internal * pci_state) { + struct rb_node * node = v3_rb_first(&(pci_state->bus_list[0].devices)); + struct pci_device * tmp_dev = NULL; + + PrintDebug("===PCI: Dumping state Begin ==========\n"); + + do { + tmp_dev = rb_entry(node, struct pci_device, dev_tree_node); + + PrintDebug("PCI Device Number: %d (%s):\n", tmp_dev->dev_num, tmp_dev->name); + PrintDebug("irq = %d\n", tmp_dev->config_header.intr_line); + PrintDebug("Vend ID: 0x%x\n", tmp_dev->config_header.vendor_id); + PrintDebug("Device ID: 0x%x\n", tmp_dev->config_header.device_id); + + } while ((node = v3_rb_next(node))); + + PrintDebug("====PCI: Dumping state End==========\n"); +} + #endif + + + // Scan the dev_map bitmap for the first '0' bit static int get_free_dev_num(struct pci_bus * bus) { int i, j; @@ -101,7 +125,7 @@ static int get_free_dev_num(struct pci_bus * bus) { // availability for (j = 0; j < 8; j++) { if (!(bus->dev_map[i] & (0x1 << j))) { - return i * 8 + j; + return ((i * 8) + j); } } } @@ -111,7 +135,7 @@ static int get_free_dev_num(struct pci_bus * bus) { } static void allocate_dev_num(struct pci_bus * bus, int dev_num) { - int major = dev_num / 8; + int major = (dev_num / 8); int minor = dev_num % 8; bus->dev_map[major] |= (0x1 << minor); @@ -183,46 +207,8 @@ static struct pci_device * get_device(struct pci_bus * bus, int dev_num) { -static int read_pci_header(struct pci_device * pci_dev, int reg_num, void * dst, int length) { - - if (length == 4) { - *(uint32_t *)dst = *(uint32_t *)(pci_dev->header_space + reg_num); - } else if (length == 2) { - *(uint16_t *)dst = *(uint16_t *)(pci_dev->header_space + reg_num); - } else if (length == 1) { - *(uint8_t *)dst = pci_dev->header_space[reg_num]; - } else { - PrintError("Invalid Read length (%d) for PCI configration header\n", length); - return -1; - } - - return length; -} - - -static int write_pci_header(struct pci_device * pci_dev, int reg_num, void * src, int length) { - - if (length == 4) { - *(uint32_t *)(pci_dev->header_space + reg_num) = *(uint32_t *)src; - } else if (length == 2) { - *(uint16_t *)(pci_dev->header_space + reg_num) = *(uint16_t *)src; - } else if (length == 1) { - pci_dev->header_space[reg_num] = *(uint8_t *)src; - } else { - PrintError("Invalid Read length (%d) for PCI configration header\n", length); - return -1; - } - - // This is kind of ugly... - if ((reg_num >= 0x10) && (reg_num < 0x27)) { - int bar_num = (reg_num & ~0x3) - 0x10; - uint32_t val = *(uint32_t *)(pci_dev->header_space + (reg_num & ~0x3)); - pci_dev->bar_update(pci_dev, bar_num, val); - } - return length; -} static int addr_port_read(ushort_t port, void * dst, uint_t length, struct vm_device * dev) { @@ -261,12 +247,15 @@ static int addr_port_write(ushort_t port, void * src, uint_t length, struct vm_d int reg_offset = port & 0x3; uint8_t * reg_addr = ((uint8_t *)&(pci_state->addr_reg.val)) + reg_offset; + if (length == 4) { if (reg_offset != 0) { PrintError("Invalid Address Port Write\n"); return -1; } + PrintDebug("Writing PCI 4 bytes Val=%x\n", *(uint32_t *)src); + *(uint32_t *)reg_addr = *(uint32_t *)src; } else if (length == 2) { if (reg_offset > 2) { @@ -274,15 +263,18 @@ static int addr_port_write(ushort_t port, void * src, uint_t length, struct vm_d return -1; } + PrintDebug("Writing PCI 2 byte Val=%x\n", *(uint16_t *)src); + *(uint16_t *)reg_addr = *(uint16_t *)src; } else if (length == 1) { + PrintDebug("Writing PCI 1 byte Val=%x\n", *(uint8_t *)src); *(uint8_t *)reg_addr = *(uint8_t *)src; } else { PrintError("Invalid write length (%d) for PCI address register\n", length); return -1; } - PrintDebug("Writing PCI Address Port(%x): %x\n", port, pci_state->addr_reg.val); + PrintDebug("Writing PCI Address Port(%x): %x\n", port, pci_state->addr_reg.val); return length; @@ -292,71 +284,76 @@ static int addr_port_write(ushort_t port, void * src, uint_t length, struct vm_d static int data_port_read(ushort_t port, void * dst, uint_t length, struct vm_device * vmdev) { struct pci_internal * pci_state = (struct pci_internal *)vmdev->private_data;; struct pci_device * pci_dev = NULL; - uint_t reg_num = pci_state->addr_reg.reg_num; + uint_t reg_num = pci_state->addr_reg.reg_num + (port & 0x3); + int i; - - PrintDebug("Reading PCI Data register. bus = %d, dev = %d, reg = %d (%x)\n", + PrintDebug("Reading PCI Data register. bus = %d, dev = %d, reg = %d (%x), cfg_reg = %x\n", pci_state->addr_reg.bus_num, pci_state->addr_reg.dev_num, - reg_num); + reg_num, reg_num, + pci_state->addr_reg.val); - - if (port != CONFIG_DATA_PORT) { - PrintError("Weird Data port Read: %x\n", port); - return -1; - } pci_dev = get_device(&(pci_state->bus_list[0]), pci_state->addr_reg.dev_num); if (pci_dev == NULL) { - //*(uint32_t *)dst = 0xffffffff; - - PrintError("Reading configuration space for non-present device (dev_num=%d)\n", - pci_state->addr_reg.dev_num); - - return -1; - } + for (i = 0; i < length; i++) { + *((uint8_t *)dst + i) = 0xff; + } - // Header register - if (reg_num < 0x40) { - return read_pci_header(pci_dev, reg_num, dst, length); + return length; } - if (pci_dev->config_read) { - return pci_dev->config_read(pci_dev, reg_num, dst, length); + for (i = 0; i < length; i++) { + *((uint8_t *)dst + i) = pci_dev->config_space[reg_num + i]; } + + return length; +} - if (length == 4) { - *(uint32_t *)dst = *(uint32_t *)(pci_dev->config_space + reg_num - 0x40); - } else if (length == 2) { - *(uint16_t *)dst = *(uint16_t *)(pci_dev->config_space + reg_num - 0x40); - } else if (length == 1) { - *(uint8_t *)dst = pci_dev->config_space[reg_num - 0x40]; +static inline int is_cfg_reg_writable(uchar_t header_type, int reg_num) { + if (header_type == 0x00) { + switch (reg_num) { + case 0x00: + case 0x01: + case 0x02: + case 0x03: + case 0x08: + case 0x09: + case 0x0a: + case 0x0b: + case 0x0e: + case 0x3d: + return 0; + + default: + return 1; + + } } else { - PrintError("Invalid Read length (%d) for PCI data register", length); + // PCI to PCI Bridge = 0x01 + // CardBus Bridge = 0x02 + + // huh? + PrintError("Invalid PCI Header type (0x%.2x)\n", header_type); + return -1; } - - return length; } static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_device * vmdev) { - struct pci_internal * pci_state = (struct pci_internal *)vmdev->private_data;; + struct pci_internal * pci_state = (struct pci_internal *)vmdev->private_data; struct pci_device * pci_dev = NULL; - uint_t reg_num = pci_state->addr_reg.reg_num; - - - PrintDebug("Writing PCI Data register. bus = %d, dev = %d, reg = %d (%x)\n", + uint_t reg_num = pci_state->addr_reg.reg_num + (port & 0x3); + int i; + + PrintDebug("Writing PCI Data register. bus = %d, dev = %d, reg = %d (%x) addr_reg = %x\n", pci_state->addr_reg.bus_num, pci_state->addr_reg.dev_num, - reg_num); - - if (port != CONFIG_DATA_PORT) { - PrintError("Weird Data port Write: %x\n", port); - return -1; - } + reg_num, reg_num, + pci_state->addr_reg.val); pci_dev = get_device(&(pci_state->bus_list[0]), pci_state->addr_reg.dev_num); @@ -367,28 +364,68 @@ static int data_port_write(ushort_t port, void * src, uint_t length, struct vm_d return -1; } - // Header register - if (reg_num < 0x40) { - return write_pci_header(pci_dev, reg_num, src, length); + + for (i = 0; i < length; i++) { + uint_t cur_reg = reg_num + i; + + if (is_cfg_reg_writable(pci_dev->config_header.header_type, cur_reg)) { + pci_dev->config_space[cur_reg] = *((uint8_t *)src + i); + + if ((cur_reg >= 0x10) && (cur_reg < 0x28)) { + // BAR Reg + int bar_reg = (cur_reg & ~0x3) - 0x10; + + if (pci_dev->bar[bar_reg].bar_update) { + pci_dev->bar_update_flag = 1; + pci_dev->bar[bar_reg].updated = 1; + } + } else if ((cur_reg >= 0x30) && (cur_reg < 0x34)) { + pci_dev->ext_rom_update_flag = 1; + } else if (cur_reg == 0x04) { + // COMMAND update + uint8_t command = *((uint8_t *)src + i); + + pci_dev->config_space[cur_reg] = command; + + if (pci_dev->cmd_update) { + pci_dev->cmd_update(pci_dev, (command & 0x01), (command & 0x02)); + } + + + } else if (cur_reg == 0x0f) { + // BIST update + pci_dev->config_header.BIST = 0x00; + } + } } - - if (pci_dev->config_write) { - return pci_dev->config_write(pci_dev, reg_num, src, length); + if (pci_dev->config_update) { + pci_dev->config_update(pci_dev, reg_num, length); } + // Scan for BAR updated + if (pci_dev->bar_update_flag) { + for (i = 0; i < 6; i++) { + if (pci_dev->bar[i].updated) { + int bar_offset = 0x10 + 4 * i; - if (length == 4) { - *(uint32_t *)(pci_dev->config_space + reg_num - 0x40) = *(uint32_t *)src; - } else if (length == 2) { - *(uint16_t *)(pci_dev->config_space + reg_num - 0x40) = *(uint16_t *)src; - } else if (length == 1) { - pci_dev->config_space[reg_num - 0x40] = *(uint8_t *)src; - } else { - PrintError("Invalid Write length (%d) for PCI data register", length); - return -1; + *(uint32_t *)(pci_dev->config_space + bar_offset) &= pci_dev->bar[i].mask; + + if (pci_dev->bar[i].bar_update) { + pci_dev->bar[i].bar_update(pci_dev, i); + } + pci_dev->bar[i].updated = 0; + } + } + pci_dev->bar_update_flag = 0; } - + + if ((pci_dev->ext_rom_update_flag) && (pci_dev->ext_rom_update)) { + pci_dev->ext_rom_update(pci_dev); + pci_dev->ext_rom_update_flag = 0; + } + + return length; } @@ -428,26 +465,73 @@ static int pci_deinit_device(struct vm_device * dev) { static int init_i440fx(struct vm_device * dev) { - struct pci_device * pci_dev = v3_pci_register_device(dev, 0, "i440FX", 0, - NULL, NULL, NULL, NULL); + struct pci_device * pci_dev = NULL; + struct v3_pci_bar bars[6]; + int i; + + for (i = 0; i < 6; i++) { + bars[i].type = PCI_BAR_NONE; + bars[i].mem_hook = 0; + bars[i].num_pages = 0; + bars[i].bar_update = NULL; + } + + pci_dev = v3_pci_register_device(dev, PCI_STD_DEVICE, 0, "i440FX", 0, bars, + NULL, NULL, NULL, NULL); if (!pci_dev) { - return -1; + return -1; } - pci_dev->header.vendor_id = 0x8086; - pci_dev->header.device_id = 0x1237; - pci_dev->header.revision = 0x0002; - pci_dev->header.subclass = 0x00; // SubClass: host2pci - pci_dev->header.class = 0x06; // Class: PCI bridge - pci_dev->header.header_type = 0x00; + pci_dev->config_header.vendor_id = 0x8086; + pci_dev->config_header.device_id = 0x1237; + pci_dev->config_header.revision = 0x0002; + pci_dev->config_header.subclass = 0x00; // SubClass: host2pci + pci_dev->config_header.class = 0x06; // Class: PCI bridge pci_dev->bus_num = 0; - return 0; } +/* +static void test_devices(struct vm_device * dev) { + struct pci_device * pci_dev = NULL; + struct v3_pci_bar bars[6]; + int i; + + for (i = 0; i < 6; i++) { + bars[i].type = PCI_BAR_NONE; + bars[i].mem_hook = 0; + bars[i].num_pages = 0; + bars[i].bar_update = NULL; + } + + + pci_dev = v3_pci_register_device(dev, PCI_STD_DEVICE, 0, "", 0, bars, + NULL, NULL, NULL, NULL); + + pci_dev->config_header.vendor_id = 0x8086; + pci_dev->config_header.device_id = 0x0101; + pci_dev->config_header.revision = 0x0002; + pci_dev->config_header.subclass = 0x01; // SubClass: host2pci + pci_dev->config_header.class = 0x01; // Class: PCI bridge + + pci_dev = v3_pci_register_device(dev, PCI_STD_DEVICE, 0, "", 0, bars, + NULL, NULL, NULL, NULL); + + pci_dev->config_header.vendor_id = 0x8086; + pci_dev->config_header.device_id = 0x0101; + pci_dev->config_header.revision = 0x0002; + pci_dev->config_header.subclass = 0x00; // SubClass: host2pci + pci_dev->config_header.class = 0x02; // Class: PCI bridge + + + + +} + +*/ static void init_pci_busses(struct pci_internal * pci_state) { int i; @@ -478,6 +562,10 @@ static int pci_init_device(struct vm_device * dev) { PrintError("Could not intialize i440fx\n"); return -1; } + + //test_devices(dev); + + 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); @@ -509,21 +597,57 @@ struct vm_device * v3_create_pci() { +static inline int init_bars(struct pci_device * pci_dev) { + int i = 0; + + for (i = 0; i < 6; i++) { + int bar_offset = 0x10 + 4 * i; + + if (pci_dev->bar[i].type == PCI_BAR_IO) { + //pci_dev->bar[i].mask = 0x0000fffd; + pci_dev->bar[i].mask = (~((pci_dev->bar[i].num_io_ports) - 1)) | 0x01; + + *(uint32_t *)(pci_dev->config_space + bar_offset) = 0x00000001; + } else if (pci_dev->bar[i].type == PCI_BAR_MEM32) { + pci_dev->bar[i].mask = ~((pci_dev->bar[i].num_pages << 12) - 1); + pci_dev->bar[i].mask |= 0xf; // preserve the configuration flags + + *(uint32_t *)(pci_dev->config_space + bar_offset) = 0x00000008; + + if (pci_dev->bar[i].mem_hook) { + // clear the prefetchable flag... + *(uint8_t *)(pci_dev->config_space + bar_offset) &= ~0x00000008; + } + } else if (pci_dev->bar[i].type == PCI_BAR_MEM16) { + PrintError("16 Bit memory ranges not supported (reg: %d)\n", i); + } else if (pci_dev->bar[i].type == PCI_BAR_NONE) { + *(uint32_t *)(pci_dev->config_space + bar_offset) = 0x00000000; + } else { + PrintError("Invalid BAR type for bar #%d\n", i); + return -1; + } + } + + return 0; +} // if dev_num == -1, auto assign struct pci_device * v3_pci_register_device(struct vm_device * pci, + pci_device_type_t dev_type, uint_t bus_num, const char * name, int dev_num, - int (*config_read)(struct pci_device * pci_dev, uint_t reg_num, void * dst, int len), - int (*config_write)(struct pci_device * pci_dev, uint_t reg_num, void * src, int len), - int (*bar_update)(struct pci_device * pci_dev, uint_t bar_reg, uint32_t val), + struct v3_pci_bar * bars, + int (*config_update)(struct pci_device * pci_dev, uint_t reg_num, int length), + int (*cmd_update)(struct pci_device *pci_dev, uchar_t io_enabled, uchar_t mem_enabled), + int (*ext_rom_update)(struct pci_device * pci_dev), void * private_data) { struct pci_internal * pci_state = (struct pci_internal *)pci->private_data; struct pci_bus * bus = &(pci_state->bus_list[bus_num]); struct pci_device * pci_dev = NULL; + int i; if (dev_num > MAX_BUS_DEVICES) { PrintError("Requested Invalid device number (%d)\n", dev_num); @@ -551,23 +675,53 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci, } memset(pci_dev, 0, sizeof(struct pci_device)); - + + switch (dev_type) { + case PCI_STD_DEVICE: + pci_dev->config_header.header_type = 0x00; + break; + default: + PrintError("Unhandled PCI Device Type: %d\n", dev_type); + return NULL; + } + pci_dev->bus_num = bus_num; pci_dev->dev_num = dev_num; strncpy(pci_dev->name, name, sizeof(pci_dev->name)); pci_dev->vm_dev = pci; - pci_dev->config_read = config_read; - pci_dev->config_write = config_write; - pci_dev->bar_update = bar_update; + // register update callbacks + pci_dev->config_update = config_update; + pci_dev->cmd_update = cmd_update; + pci_dev->ext_rom_update = ext_rom_update; pci_dev->priv_data = private_data; + + //copy bars + for (i = 0; i < 6; i ++){ + pci_dev->bar[i].type = bars[i].type; + + if (pci_dev->bar[i].type == PCI_BAR_IO) { + pci_dev->bar[i].num_io_ports = bars[i].num_io_ports; + } else { + pci_dev->bar[i].num_pages = bars[i].num_pages; + } + + pci_dev->bar[i].mem_hook = bars[i].mem_hook; + pci_dev->bar[i].bar_update = bars[i].bar_update; + } + + if (init_bars(pci_dev) == -1) { + PrintError("could not initialize bar registers\n"); + return NULL; + } + // add the device add_device_to_bus(bus, pci_dev); - + #ifdef DEBUG_PCI pci_dump_state(pci_state); #endif @@ -577,25 +731,3 @@ struct pci_device * v3_pci_register_device(struct vm_device * pci, -#ifdef DEBUG_PCI - -static void pci_dump_state(struct pci_internal * pci_state) { - struct rb_node * node = v3_rb_first(&(pci_state->bus_list[0].devices)); - struct pci_device * tmp_dev = NULL; - - PrintDebug("===PCI: Dumping state Begin ==========\n"); - - do { - tmp_dev = rb_entry(node, struct pci_device, dev_tree_node); - - PrintDebug("PCI Device Number: %d (%s):\n", tmp_dev->dev_num, tmp_dev->name); - PrintDebug("irq = %d\n", tmp_dev->header.irq_line); - PrintDebug("Vend ID: 0x%x\n", tmp_dev->header.vendor_id); - PrintDebug("Device ID: 0x%x\n", tnp_dev->header.device_id); - - } while ((node = v3_rb_next(node))); - - PrintDebug("====PCI: Dumping state End==========\n"); -} - -#endif