4 * Copyright (c) 2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 pci_set_irq_fn set_irq;
37 pci_map_irq_fn map_irq;
38 uint32_t config_reg; /* XXX: suppress */
40 SetIRQFunc *low_set_irq;
42 PCIDevice *devices[256];
43 PCIDevice *parent_dev;
45 /* The bus IRQ state is the logical OR of the connected devices.
46 Keep a count of the number of devices with raised IRQs. */
51 static void pci_update_mappings(PCIDevice *d);
52 static void pci_set_irq(void *opaque, int irq_num, int level);
54 target_phys_addr_t pci_mem_base;
55 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
56 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
57 static PCIBus *first_bus;
59 static void pcibus_save(QEMUFile *f, void *opaque)
61 PCIBus *bus = (PCIBus *)opaque;
64 qemu_put_be32(f, bus->nirq);
65 for (i = 0; i < bus->nirq; i++)
66 qemu_put_be32(f, bus->irq_count[i]);
69 static int pcibus_load(QEMUFile *f, void *opaque, int version_id)
71 PCIBus *bus = (PCIBus *)opaque;
77 nirq = qemu_get_be32(f);
78 if (bus->nirq != nirq) {
79 fprintf(stderr, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
84 for (i = 0; i < nirq; i++)
85 bus->irq_count[i] = qemu_get_be32(f);
90 static void pci_bus_reset(void *opaque)
92 PCIBus *bus = (PCIBus *)opaque;
95 for (i = 0; i < bus->nirq; i++) {
96 bus->irq_count[i] = 0;
98 for (i = 0; i < 256; i++) {
100 memset(bus->devices[i]->irq_state, 0,
101 sizeof(bus->devices[i]->irq_state));
105 PCIBus *pci_register_bus(DeviceState *parent, const char *name,
106 pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
107 qemu_irq *pic, int devfn_min, int nirq)
112 bus = FROM_QBUS(PCIBus, qbus_create(BUS_TYPE_PCI,
113 sizeof(PCIBus) + (nirq * sizeof(int)),
115 bus->set_irq = set_irq;
116 bus->map_irq = map_irq;
117 bus->irq_opaque = pic;
118 bus->devfn_min = devfn_min;
120 bus->next = first_bus;
122 register_savevm("PCIBUS", nbus++, 1, pcibus_save, pcibus_load, bus);
123 qemu_register_reset(pci_bus_reset, 0, bus);
127 static PCIBus *pci_register_secondary_bus(PCIDevice *dev, pci_map_irq_fn map_irq)
130 bus = qemu_mallocz(sizeof(PCIBus));
131 bus->map_irq = map_irq;
132 bus->parent_dev = dev;
133 bus->next = dev->bus->next;
134 dev->bus->next = bus;
138 int pci_bus_num(PCIBus *s)
143 void pci_device_save(PCIDevice *s, QEMUFile *f)
147 qemu_put_be32(f, 2); /* PCI device version */
148 qemu_put_buffer(f, s->config, 256);
149 for (i = 0; i < 4; i++)
150 qemu_put_be32(f, s->irq_state[i]);
153 int pci_device_load(PCIDevice *s, QEMUFile *f)
158 version_id = qemu_get_be32(f);
161 qemu_get_buffer(f, s->config, 256);
162 pci_update_mappings(s);
165 for (i = 0; i < 4; i ++)
166 s->irq_state[i] = qemu_get_be32(f);
171 static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
175 id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
176 id[0] = cpu_to_le16(pci_default_sub_vendor_id);
177 id[1] = cpu_to_le16(pci_default_sub_device_id);
182 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
184 static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
189 unsigned long dom = 0, bus = 0;
193 val = strtoul(p, &e, 16);
199 val = strtoul(p, &e, 16);
206 val = strtoul(p, &e, 16);
212 if (dom > 0xffff || bus > 0xff || val > 0x1f)
220 /* Note: QEMU doesn't implement domains other than 0 */
221 if (dom != 0 || pci_find_bus(bus) == NULL)
230 int pci_read_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
234 if (!get_param_value(devaddr, sizeof(devaddr), "pci_addr", addr))
237 return pci_parse_devaddr(devaddr, domp, busp, slotp);
240 static PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
247 return pci_find_bus(0);
250 if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
255 return pci_find_bus(bus);
258 /* -1 for devfn means auto assign */
259 static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
260 const char *name, int devfn,
261 PCIConfigReadFunc *config_read,
262 PCIConfigWriteFunc *config_write)
265 for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) {
266 if (!bus->devices[devfn])
271 } else if (bus->devices[devfn]) {
275 pci_dev->devfn = devfn;
276 pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
277 memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state));
278 pci_set_default_subsystem_id(pci_dev);
281 config_read = pci_default_read_config;
283 config_write = pci_default_write_config;
284 pci_dev->config_read = config_read;
285 pci_dev->config_write = config_write;
286 bus->devices[devfn] = pci_dev;
287 pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, 4);
291 PCIDevice *pci_register_device(PCIBus *bus, const char *name,
292 int instance_size, int devfn,
293 PCIConfigReadFunc *config_read,
294 PCIConfigWriteFunc *config_write)
298 pci_dev = qemu_mallocz(instance_size);
299 pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
300 config_read, config_write);
303 static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
305 return addr + pci_mem_base;
308 static void pci_unregister_io_regions(PCIDevice *pci_dev)
313 for(i = 0; i < PCI_NUM_REGIONS; i++) {
314 r = &pci_dev->io_regions[i];
315 if (!r->size || r->addr == -1)
317 if (r->type == PCI_ADDRESS_SPACE_IO) {
318 isa_unassign_ioport(r->addr, r->size);
320 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
327 int pci_unregister_device(PCIDevice *pci_dev)
331 if (pci_dev->unregister)
332 ret = pci_dev->unregister(pci_dev);
336 pci_unregister_io_regions(pci_dev);
338 qemu_free_irqs(pci_dev->irq);
339 pci_dev->bus->devices[pci_dev->devfn] = NULL;
340 qdev_free(&pci_dev->qdev);
344 void pci_register_bar(PCIDevice *pci_dev, int region_num,
345 uint32_t size, int type,
346 PCIMapIORegionFunc *map_func)
351 if ((unsigned int)region_num >= PCI_NUM_REGIONS)
354 if (size & (size-1)) {
355 fprintf(stderr, "ERROR: PCI region size must be pow2 "
356 "type=0x%x, size=0x%x\n", type, size);
360 r = &pci_dev->io_regions[region_num];
364 r->map_func = map_func;
365 if (region_num == PCI_ROM_SLOT) {
368 addr = 0x10 + region_num * 4;
370 *(uint32_t *)(pci_dev->config + addr) = cpu_to_le32(type);
373 static void pci_update_mappings(PCIDevice *d)
377 uint32_t last_addr, new_addr, config_ofs;
379 cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
380 for(i = 0; i < PCI_NUM_REGIONS; i++) {
381 r = &d->io_regions[i];
382 if (i == PCI_ROM_SLOT) {
385 config_ofs = 0x10 + i * 4;
388 if (r->type & PCI_ADDRESS_SPACE_IO) {
389 if (cmd & PCI_COMMAND_IO) {
390 new_addr = le32_to_cpu(*(uint32_t *)(d->config +
392 new_addr = new_addr & ~(r->size - 1);
393 last_addr = new_addr + r->size - 1;
394 /* NOTE: we have only 64K ioports on PC */
395 if (last_addr <= new_addr || new_addr == 0 ||
396 last_addr >= 0x10000) {
403 if (cmd & PCI_COMMAND_MEMORY) {
404 new_addr = le32_to_cpu(*(uint32_t *)(d->config +
406 /* the ROM slot has a specific enable bit */
407 if (i == PCI_ROM_SLOT && !(new_addr & 1))
409 new_addr = new_addr & ~(r->size - 1);
410 last_addr = new_addr + r->size - 1;
411 /* NOTE: we do not support wrapping */
412 /* XXX: as we cannot support really dynamic
413 mappings, we handle specific values as invalid
415 if (last_addr <= new_addr || new_addr == 0 ||
424 /* now do the real mapping */
425 if (new_addr != r->addr) {
427 if (r->type & PCI_ADDRESS_SPACE_IO) {
429 /* NOTE: specific hack for IDE in PC case:
430 only one byte must be mapped. */
431 class = d->config[0x0a] | (d->config[0x0b] << 8);
432 if (class == 0x0101 && r->size == 4) {
433 isa_unassign_ioport(r->addr + 2, 1);
435 isa_unassign_ioport(r->addr, r->size);
438 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
441 qemu_unregister_coalesced_mmio(r->addr, r->size);
446 r->map_func(d, i, r->addr, r->size, r->type);
453 uint32_t pci_default_read_config(PCIDevice *d,
454 uint32_t address, int len)
461 if (address <= 0xfc) {
462 val = le32_to_cpu(*(uint32_t *)(d->config + address));
467 if (address <= 0xfe) {
468 val = le16_to_cpu(*(uint16_t *)(d->config + address));
473 val = d->config[address];
479 void pci_default_write_config(PCIDevice *d,
480 uint32_t address, uint32_t val, int len)
485 if (len == 4 && ((address >= 0x10 && address < 0x10 + 4 * 6) ||
486 (address >= 0x30 && address < 0x34))) {
490 if ( address >= 0x30 ) {
493 reg = (address - 0x10) >> 2;
495 r = &d->io_regions[reg];
498 /* compute the stored value */
499 if (reg == PCI_ROM_SLOT) {
500 /* keep ROM enable bit */
501 val &= (~(r->size - 1)) | 1;
503 val &= ~(r->size - 1);
506 *(uint32_t *)(d->config + address) = cpu_to_le32(val);
507 pci_update_mappings(d);
511 /* not efficient, but simple */
513 for(i = 0; i < len; i++) {
514 /* default read/write accesses */
515 switch(d->config[0x0e]) {
530 case 0x10 ... 0x27: /* base */
531 case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
532 case 0x30 ... 0x33: /* rom */
555 case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
556 case 0x38 ... 0x3b: /* rom */
567 /* Mask out writes to reserved bits in registers */
570 val &= ~PCI_COMMAND_RESERVED_MASK_HI;
573 val &= ~PCI_STATUS_RESERVED_MASK_LO;
576 val &= ~PCI_STATUS_RESERVED_MASK_HI;
579 d->config[addr] = val;
587 if (end > PCI_COMMAND && address < (PCI_COMMAND + 2)) {
588 /* if the command register is modified, we must modify the mappings */
589 pci_update_mappings(d);
593 void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len)
597 int config_addr, bus_num;
599 #if defined(DEBUG_PCI) && 0
600 printf("pci_data_write: addr=%08x val=%08x len=%d\n",
603 bus_num = (addr >> 16) & 0xff;
604 while (s && s->bus_num != bus_num)
608 pci_dev = s->devices[(addr >> 8) & 0xff];
611 config_addr = addr & 0xff;
612 #if defined(DEBUG_PCI)
613 printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
614 pci_dev->name, config_addr, val, len);
616 pci_dev->config_write(pci_dev, config_addr, val, len);
619 uint32_t pci_data_read(void *opaque, uint32_t addr, int len)
623 int config_addr, bus_num;
626 bus_num = (addr >> 16) & 0xff;
627 while (s && s->bus_num != bus_num)
631 pci_dev = s->devices[(addr >> 8) & 0xff];
648 config_addr = addr & 0xff;
649 val = pci_dev->config_read(pci_dev, config_addr, len);
650 #if defined(DEBUG_PCI)
651 printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
652 pci_dev->name, config_addr, val, len);
655 #if defined(DEBUG_PCI) && 0
656 printf("pci_data_read: addr=%08x val=%08x len=%d\n",
662 /***********************************************************/
663 /* generic PCI irq support */
665 /* 0 <= irq_num <= 3. level must be 0 or 1 */
666 static void pci_set_irq(void *opaque, int irq_num, int level)
668 PCIDevice *pci_dev = (PCIDevice *)opaque;
672 change = level - pci_dev->irq_state[irq_num];
676 pci_dev->irq_state[irq_num] = level;
679 irq_num = bus->map_irq(pci_dev, irq_num);
682 pci_dev = bus->parent_dev;
684 bus->irq_count[irq_num] += change;
685 bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
688 /***********************************************************/
689 /* monitor info on PCI */
696 static const pci_class_desc pci_class_descriptions[] =
698 { 0x0100, "SCSI controller"},
699 { 0x0101, "IDE controller"},
700 { 0x0102, "Floppy controller"},
701 { 0x0103, "IPI controller"},
702 { 0x0104, "RAID controller"},
703 { 0x0106, "SATA controller"},
704 { 0x0107, "SAS controller"},
705 { 0x0180, "Storage controller"},
706 { 0x0200, "Ethernet controller"},
707 { 0x0201, "Token Ring controller"},
708 { 0x0202, "FDDI controller"},
709 { 0x0203, "ATM controller"},
710 { 0x0280, "Network controller"},
711 { 0x0300, "VGA controller"},
712 { 0x0301, "XGA controller"},
713 { 0x0302, "3D controller"},
714 { 0x0380, "Display controller"},
715 { 0x0400, "Video controller"},
716 { 0x0401, "Audio controller"},
718 { 0x0480, "Multimedia controller"},
719 { 0x0500, "RAM controller"},
720 { 0x0501, "Flash controller"},
721 { 0x0580, "Memory controller"},
722 { 0x0600, "Host bridge"},
723 { 0x0601, "ISA bridge"},
724 { 0x0602, "EISA bridge"},
725 { 0x0603, "MC bridge"},
726 { 0x0604, "PCI bridge"},
727 { 0x0605, "PCMCIA bridge"},
728 { 0x0606, "NUBUS bridge"},
729 { 0x0607, "CARDBUS bridge"},
730 { 0x0608, "RACEWAY bridge"},
732 { 0x0c03, "USB controller"},
736 static void pci_info_device(PCIDevice *d)
738 Monitor *mon = cur_mon;
741 const pci_class_desc *desc;
743 monitor_printf(mon, " Bus %2d, device %3d, function %d:\n",
744 d->bus->bus_num, d->devfn >> 3, d->devfn & 7);
745 class = le16_to_cpu(*((uint16_t *)(d->config + PCI_CLASS_DEVICE)));
746 monitor_printf(mon, " ");
747 desc = pci_class_descriptions;
748 while (desc->desc && class != desc->class)
751 monitor_printf(mon, "%s", desc->desc);
753 monitor_printf(mon, "Class %04x", class);
755 monitor_printf(mon, ": PCI device %04x:%04x\n",
756 le16_to_cpu(*((uint16_t *)(d->config + PCI_VENDOR_ID))),
757 le16_to_cpu(*((uint16_t *)(d->config + PCI_DEVICE_ID))));
759 if (d->config[PCI_INTERRUPT_PIN] != 0) {
760 monitor_printf(mon, " IRQ %d.\n",
761 d->config[PCI_INTERRUPT_LINE]);
763 if (class == 0x0604) {
764 monitor_printf(mon, " BUS %d.\n", d->config[0x19]);
766 for(i = 0;i < PCI_NUM_REGIONS; i++) {
767 r = &d->io_regions[i];
769 monitor_printf(mon, " BAR%d: ", i);
770 if (r->type & PCI_ADDRESS_SPACE_IO) {
771 monitor_printf(mon, "I/O at 0x%04x [0x%04x].\n",
772 r->addr, r->addr + r->size - 1);
774 monitor_printf(mon, "32 bit memory at 0x%08x [0x%08x].\n",
775 r->addr, r->addr + r->size - 1);
779 if (class == 0x0604 && d->config[0x19] != 0) {
780 pci_for_each_device(d->config[0x19], pci_info_device);
784 void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d))
786 PCIBus *bus = first_bus;
790 while (bus && bus->bus_num != bus_num)
793 for(devfn = 0; devfn < 256; devfn++) {
794 d = bus->devices[devfn];
801 void pci_info(Monitor *mon)
803 pci_for_each_device(0, pci_info_device);
806 PCIDevice *pci_create(const char *name, const char *devaddr)
812 bus = pci_get_bus_devfn(&devfn, devaddr);
814 fprintf(stderr, "Invalid PCI device address %s for device %s\n",
819 dev = qdev_create(&bus->qbus, name);
820 qdev_set_prop_int(dev, "devfn", devfn);
821 return (PCIDevice *)dev;
824 static const char * const pci_nic_models[] = {
836 static const char * const pci_nic_names[] = {
848 /* Initialize a PCI NIC. */
849 PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
850 const char *default_devaddr)
852 const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
857 qemu_check_nic_model_list(nd, pci_nic_models, default_model);
859 for (i = 0; pci_nic_models[i]; i++) {
860 if (strcmp(nd->model, pci_nic_models[i]) == 0) {
861 pci_dev = pci_create(pci_nic_names[i], devaddr);
862 dev = &pci_dev->qdev;
863 qdev_set_netdev(dev, nd);
878 static void pci_bridge_write_config(PCIDevice *d,
879 uint32_t address, uint32_t val, int len)
881 PCIBridge *s = (PCIBridge *)d;
883 if (address == 0x19 || (address == 0x18 && len > 1)) {
885 s->bus->bus_num = val & 0xff;
887 s->bus->bus_num = (val >> 8) & 0xff;
888 #if defined(DEBUG_PCI)
889 printf ("pci-bridge: %s: Assigned bus %d\n", d->name, s->bus->bus_num);
892 pci_default_write_config(d, address, val, len);
895 PCIBus *pci_find_bus(int bus_num)
897 PCIBus *bus = first_bus;
899 while (bus && bus->bus_num != bus_num)
905 PCIDevice *pci_find_device(int bus_num, int slot, int function)
907 PCIBus *bus = pci_find_bus(bus_num);
912 return bus->devices[PCI_DEVFN(slot, function)];
915 PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
916 pci_map_irq_fn map_irq, const char *name)
919 s = (PCIBridge *)pci_register_device(bus, name, sizeof(PCIBridge),
920 devfn, NULL, pci_bridge_write_config);
922 pci_config_set_vendor_id(s->dev.config, vid);
923 pci_config_set_device_id(s->dev.config, did);
925 s->dev.config[0x04] = 0x06; // command = bus master, pci mem
926 s->dev.config[0x05] = 0x00;
927 s->dev.config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
928 s->dev.config[0x07] = 0x00; // status = fast devsel
929 s->dev.config[0x08] = 0x00; // revision
930 s->dev.config[0x09] = 0x00; // programming i/f
931 pci_config_set_class(s->dev.config, PCI_CLASS_BRIDGE_PCI);
932 s->dev.config[0x0D] = 0x10; // latency_timer
933 s->dev.config[PCI_HEADER_TYPE] =
934 PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE; // header_type
935 s->dev.config[0x1E] = 0xa0; // secondary status
937 s->bus = pci_register_secondary_bus(&s->dev, map_irq);
943 pci_qdev_initfn init;
946 static void pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
948 PCIDevice *pci_dev = (PCIDevice *)qdev;
949 PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
953 bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
954 devfn = qdev_get_prop_int(qdev, "devfn", -1);
955 pci_dev = do_pci_register_device(pci_dev, bus, "FIXME", devfn,
956 NULL, NULL);//FIXME:config_read, config_write);
961 void pci_qdev_register(const char *name, int size, pci_qdev_initfn init)
965 info = qemu_mallocz(sizeof(*info));
966 info->qdev.name = qemu_strdup(name);
967 info->qdev.size = size;
969 info->qdev.init = pci_qdev_init;
970 info->qdev.bus_type = BUS_TYPE_PCI;
972 qdev_register(&info->qdev);
975 PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
979 dev = qdev_create(&bus->qbus, name);
980 qdev_set_prop_int(dev, "devfn", devfn);
983 return (PCIDevice *)dev;