📄 pci.c
字号:
default: can_write = 1; break; } break; default: case 0x01: switch(addr) { case 0x00: case 0x01: case 0x02: case 0x03: case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0e: case 0x38 ... 0x3b: /* rom */ case 0x3d: can_write = 0; break; default: can_write = 1; break; } break; } if (can_write) { if( addr == 0x05 ) { /* In Command Register, bits 15:11 are reserved */ val &= 0x07; } else if ( addr == 0x06 ) { /* In Status Register, bits 6, 2:0 are reserved, */ /* and bits 7,5,4,3 are read only */ val = d->config[addr]; } else if ( addr == 0x07 ) { /* In Status Register, bits 10,9 are reserved, */ val = (val & ~0x06) | (d->config[addr] & 0x06); } d->config[addr] = val; } if (++addr > 0xff) break; val >>= 8; } end = address + len; if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) { /* if the command register is modified, we must modify the mappings */ pci_update_mappings(d); }}void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len){ PCIBus *s = opaque; PCIDevice *pci_dev; int config_addr, bus_num; #if defined(DEBUG_PCI) && 0 printf("pci_data_write: addr=%08x val=%08x len=%d\n", addr, val, len);#endif bus_num = (addr >> 16) & 0xff; while (s && s->bus_num != bus_num) s = s->next; if (!s) return; pci_dev = s->devices[(addr >> 8) & 0xff]; if (!pci_dev) return; config_addr = addr & 0xff;#if defined(DEBUG_PCI) printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n", pci_dev->name, config_addr, val, len);#endif pci_dev->config_write(pci_dev, config_addr, val, len);}uint32_t pci_data_read(void *opaque, uint32_t addr, int len){ PCIBus *s = opaque; PCIDevice *pci_dev; int config_addr, bus_num; uint32_t val; bus_num = (addr >> 16) & 0xff; while (s && s->bus_num != bus_num) s= s->next; if (!s) goto fail; pci_dev = s->devices[(addr >> 8) & 0xff]; if (!pci_dev) { fail: switch(len) { case 1: val = 0xff; break; case 2: val = 0xffff; break; default: case 4: val = 0xffffffff; break; } goto the_end; } config_addr = addr & 0xff; val = pci_dev->config_read(pci_dev, config_addr, len);#if defined(DEBUG_PCI) printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n", pci_dev->name, config_addr, val, len);#endif the_end:#if defined(DEBUG_PCI) && 0 printf("pci_data_read: addr=%08x val=%08x len=%d\n", addr, val, len);#endif return val;}/***********************************************************//* generic PCI irq support *//* 0 <= irq_num <= 3. level must be 0 or 1 */void pci_set_irq(PCIDevice *pci_dev, int irq_num, int level){ PCIBus *bus; int change; change = level - pci_dev->irq_state[irq_num]; if (!change) return; pci_dev->irq_state[irq_num] = level; for (;;) { bus = pci_dev->bus; irq_num = bus->map_irq(pci_dev, irq_num); if (bus->set_irq) break; pci_dev = bus->parent_dev; } bus->irq_count[irq_num] += change; bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);}/***********************************************************//* monitor info on PCI */typedef struct { uint16_t class; const char *desc;} pci_class_desc;static pci_class_desc pci_class_descriptions[] = { { 0x0100, "SCSI controller"}, { 0x0101, "IDE controller"}, { 0x0200, "Ethernet controller"}, { 0x0300, "VGA controller"}, { 0x0600, "Host bridge"}, { 0x0601, "ISA bridge"}, { 0x0604, "PCI bridge"}, { 0x0c03, "USB controller"}, { 0, NULL}};static void pci_info_device(PCIDevice *d){ int i, class; PCIIORegion *r; pci_class_desc *desc; term_printf(" Bus %2d, device %3d, function %d:\n", d->bus->bus_num, d->devfn >> 3, d->devfn & 7); class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE))); term_printf(" "); desc = pci_class_descriptions; while (desc->desc && class != desc->class) desc++; if (desc->desc) { term_printf("%s", desc->desc); } else { term_printf("Class %04x", class); } term_printf(": PCI device %04x:%04x\n", le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))), le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID)))); if (d->config[PCI_INTERRUPT_PIN] != 0) { term_printf(" IRQ %d.\n", d->config[PCI_INTERRUPT_LINE]); } if (class == 0x0604) { term_printf(" BUS %d.\n", d->config[0x19]); } for(i = 0;i < PCI_NUM_REGIONS; i++) { r = &d->io_regions[i]; if (r->size != 0) { term_printf(" BAR%d: ", i); if (r->type & PCI_ADDRESS_SPACE_IO) { term_printf("I/O at 0x%04x [0x%04x].\n", r->addr, r->addr + r->size - 1); } else { term_printf("32 bit memory at 0x%08x [0x%08x].\n", r->addr, r->addr + r->size - 1); } } } if (class == 0x0604 && d->config[0x19] != 0) { pci_for_each_device(d->config[0x19], pci_info_device); }}void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d)){ PCIBus *bus = first_bus; PCIDevice *d; int devfn; while (bus && bus->bus_num != bus_num) bus = bus->next; if (bus) { for(devfn = 0; devfn < 256; devfn++) { d = bus->devices[devfn]; if (d) fn(d); } }}void pci_info(void){ pci_for_each_device(0, pci_info_device);}/* Initialize a PCI NIC. */void pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn){ if (strcmp(nd->model, "ne2k_pci") == 0) { pci_ne2000_init(bus, nd, devfn); } else if (strcmp(nd->model, "rtl8139") == 0) { pci_rtl8139_init(bus, nd, devfn); } else if (strcmp(nd->model, "pcnet") == 0) { pci_pcnet_init(bus, nd, devfn); } else if (strcmp(nd->model, "e100") == 0) { pci_e100_init(bus, nd); } else if (strcmp(nd->model, "e1000") == 0) { pci_e1000_init(bus, nd, devfn); } else { fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model); exit (1); }}typedef struct { PCIDevice dev; PCIBus *bus;} PCIBridge;void pci_bridge_write_config(PCIDevice *d, uint32_t address, uint32_t val, int len){ PCIBridge *s = (PCIBridge *)d; if (address == 0x19 || (address == 0x18 && len > 1)) { if (address == 0x19) s->bus->bus_num = val & 0xff; else s->bus->bus_num = (val >> 8) & 0xff;#if defined(DEBUG_PCI) printf ("pci-bridge: %s: Assigned bus %d\n", d->name, s->bus->bus_num);#endif } pci_default_write_config(d, address, val, len);}PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint32_t id, pci_map_irq_fn map_irq, const char *name){ PCIBridge *s; s = (PCIBridge *)pci_register_device(bus, name, sizeof(PCIBridge), devfn, NULL, pci_bridge_write_config); s->dev.config[0x00] = id >> 16; s->dev.config[0x01] = id > 24; s->dev.config[0x02] = id; // device_id s->dev.config[0x03] = id >> 8; s->dev.config[0x04] = 0x06; // command = bus master, pci mem s->dev.config[0x05] = 0x00; s->dev.config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error s->dev.config[0x07] = 0x00; // status = fast devsel s->dev.config[0x08] = 0x00; // revision s->dev.config[0x09] = 0x00; // programming i/f s->dev.config[0x0A] = 0x04; // class_sub = PCI to PCI bridge s->dev.config[0x0B] = 0x06; // class_base = PCI_bridge s->dev.config[0x0D] = 0x10; // latency_timer s->dev.config[0x0E] = 0x81; // header_type s->dev.config[0x1E] = 0xa0; // secondary status s->bus = pci_register_secondary_bus(&s->dev, map_irq); return s->bus;}int pt_chk_bar_overlap(PCIBus *bus, int devfn, uint32_t addr, uint32_t size){ PCIDevice *devices = NULL; PCIIORegion *r; int ret = 0; int i, j; /* check Overlapped to Base Address */ for (i=0; i<256; i++) { if ( !(devices = bus->devices[i]) ) continue; /* skip itself */ if (devices->devfn == devfn) continue; for (j=0; j<PCI_NUM_REGIONS; j++) { r = &devices->io_regions[j]; if ((addr < (r->addr + r->size)) && ((addr + size) > r->addr)) { printf("Overlapped to device[%02x:%02x.%x][Region:%d]" "[Address:%08xh][Size:%08xh]\n", bus->bus_num, (devices->devfn >> 3) & 0x1F, (devices->devfn & 0x7), j, r->addr, r->size); ret = 1; goto out; } } }out: return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -