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
31 #include "device-assignment.h"
39 pci_set_irq_fn set_irq
;
40 pci_map_irq_fn map_irq
;
41 uint32_t config_reg
; /* XXX: suppress */
43 SetIRQFunc
*low_set_irq
;
45 PCIDevice
*devices
[256];
46 PCIDevice
*parent_dev
;
48 /* The bus IRQ state is the logical OR of the connected devices.
49 Keep a count of the number of devices with raised IRQs. */
54 static void pci_update_mappings(PCIDevice
*d
);
55 static void pci_set_irq(void *opaque
, int irq_num
, int level
);
57 target_phys_addr_t pci_mem_base
;
58 static uint16_t pci_default_sub_vendor_id
= PCI_SUBVENDOR_ID_REDHAT_QUMRANET
;
59 static uint16_t pci_default_sub_device_id
= PCI_SUBDEVICE_ID_QEMU
;
60 static PCIBus
*first_bus
;
62 static void pcibus_save(QEMUFile
*f
, void *opaque
)
64 PCIBus
*bus
= (PCIBus
*)opaque
;
67 qemu_put_be32(f
, bus
->nirq
);
68 for (i
= 0; i
< bus
->nirq
; i
++)
69 qemu_put_be32(f
, bus
->irq_count
[i
]);
72 static int pcibus_load(QEMUFile
*f
, void *opaque
, int version_id
)
74 PCIBus
*bus
= (PCIBus
*)opaque
;
80 nirq
= qemu_get_be32(f
);
81 if (bus
->nirq
!= nirq
) {
82 fprintf(stderr
, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
87 for (i
= 0; i
< nirq
; i
++)
88 bus
->irq_count
[i
] = qemu_get_be32(f
);
93 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
94 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
95 qemu_irq
*pic
, int devfn_min
, int nirq
)
100 bus
= FROM_QBUS(PCIBus
, qbus_create(BUS_TYPE_PCI
,
101 sizeof(PCIBus
) + (nirq
* sizeof(int)),
103 bus
->set_irq
= set_irq
;
104 bus
->map_irq
= map_irq
;
105 bus
->irq_opaque
= pic
;
106 bus
->devfn_min
= devfn_min
;
108 bus
->next
= first_bus
;
110 register_savevm("PCIBUS", nbus
++, 1, pcibus_save
, pcibus_load
, bus
);
114 static PCIBus
*pci_register_secondary_bus(PCIDevice
*dev
, pci_map_irq_fn map_irq
)
117 bus
= qemu_mallocz(sizeof(PCIBus
));
118 bus
->map_irq
= map_irq
;
119 bus
->parent_dev
= dev
;
120 bus
->next
= dev
->bus
->next
;
121 dev
->bus
->next
= bus
;
125 int pci_bus_num(PCIBus
*s
)
130 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
134 qemu_put_be32(f
, 2); /* PCI device version */
135 qemu_put_buffer(f
, s
->config
, 256);
136 for (i
= 0; i
< 4; i
++)
137 qemu_put_be32(f
, s
->irq_state
[i
]);
140 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
145 version_id
= qemu_get_be32(f
);
148 qemu_get_buffer(f
, s
->config
, 256);
149 pci_update_mappings(s
);
152 for (i
= 0; i
< 4; i
++)
153 s
->irq_state
[i
] = qemu_get_be32(f
);
158 static int pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
162 id
= (void*)(&pci_dev
->config
[PCI_SUBVENDOR_ID
]);
163 id
[0] = cpu_to_le16(pci_default_sub_vendor_id
);
164 id
[1] = cpu_to_le16(pci_default_sub_device_id
);
169 * Parse pci address in qemu command
170 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
172 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
177 unsigned long dom
= 0, bus
= 0;
181 val
= strtoul(p
, &e
, 16);
187 val
= strtoul(p
, &e
, 16);
194 val
= strtoul(p
, &e
, 16);
200 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
208 /* Note: QEMU doesn't implement domains other than 0 */
209 if (dom
!= 0 || pci_find_bus(bus
) == NULL
)
219 * Parse device bdf in device assignment command:
221 * -pcidevice host=bus:dev.func
223 * Parse <bus>:<slot>.<func> return -1 on error
225 int pci_parse_host_devaddr(const char *addr
, int *busp
,
226 int *slotp
, int *funcp
)
231 int bus
= 0, slot
= 0, func
= 0;
234 val
= strtoul(p
, &e
, 16);
240 val
= strtoul(p
, &e
, 16);
246 val
= strtoul(p
, &e
, 16);
255 if (bus
> 0xff || slot
> 0x1f || func
> 0x7)
267 int pci_read_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
271 if (!get_param_value(devaddr
, sizeof(devaddr
), "pci_addr", addr
))
274 return pci_parse_devaddr(devaddr
, domp
, busp
, slotp
);
277 int pci_assign_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
281 if (!get_param_value(devaddr
, sizeof(devaddr
), "pci_addr", addr
))
284 if (!strcmp(devaddr
, "auto")) {
287 /* want to support dom/bus auto-assign at some point */
291 return pci_parse_devaddr(devaddr
, domp
, busp
, slotp
);
294 /* -1 for devfn means auto assign */
295 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
296 const char *name
, int devfn
,
297 PCIConfigReadFunc
*config_read
,
298 PCIConfigWriteFunc
*config_write
)
301 for(devfn
= bus
->devfn_min
; devfn
< 256; devfn
+= 8) {
302 if (!bus
->devices
[devfn
])
309 pci_dev
->devfn
= devfn
;
310 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
311 memset(pci_dev
->irq_state
, 0, sizeof(pci_dev
->irq_state
));
312 pci_set_default_subsystem_id(pci_dev
);
315 config_read
= pci_default_read_config
;
317 config_write
= pci_default_write_config
;
318 pci_dev
->config_read
= config_read
;
319 pci_dev
->config_write
= config_write
;
320 bus
->devices
[devfn
] = pci_dev
;
321 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, 4);
325 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
326 int instance_size
, int devfn
,
327 PCIConfigReadFunc
*config_read
,
328 PCIConfigWriteFunc
*config_write
)
332 pci_dev
= qemu_mallocz(instance_size
);
333 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
334 config_read
, config_write
);
337 static target_phys_addr_t
pci_to_cpu_addr(target_phys_addr_t addr
)
339 return addr
+ pci_mem_base
;
342 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
347 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
348 r
= &pci_dev
->io_regions
[i
];
349 if (!r
->size
|| r
->addr
== -1)
351 if (r
->type
== PCI_ADDRESS_SPACE_IO
) {
352 isa_unassign_ioport(r
->addr
, r
->size
);
354 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
361 int pci_unregister_device(PCIDevice
*pci_dev
, int assigned
)
365 if (pci_dev
->unregister
)
366 ret
= pci_dev
->unregister(pci_dev
);
370 pci_unregister_io_regions(pci_dev
);
372 qemu_free_irqs(pci_dev
->irq
);
373 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
378 qdev_free(&pci_dev
->qdev
);
382 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
383 uint32_t size
, int type
,
384 PCIMapIORegionFunc
*map_func
)
389 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
392 if (size
& (size
-1)) {
393 fprintf(stderr
, "ERROR: PCI region size must be pow2 "
394 "type=0x%x, size=0x%x\n", type
, size
);
398 r
= &pci_dev
->io_regions
[region_num
];
402 r
->map_func
= map_func
;
403 if (region_num
== PCI_ROM_SLOT
) {
406 addr
= 0x10 + region_num
* 4;
408 *(uint32_t *)(pci_dev
->config
+ addr
) = cpu_to_le32(type
);
411 static void pci_update_mappings(PCIDevice
*d
)
415 uint32_t last_addr
, new_addr
, config_ofs
;
417 cmd
= le16_to_cpu(*(uint16_t *)(d
->config
+ PCI_COMMAND
));
418 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
419 r
= &d
->io_regions
[i
];
420 if (i
== PCI_ROM_SLOT
) {
423 config_ofs
= 0x10 + i
* 4;
426 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
427 if (cmd
& PCI_COMMAND_IO
) {
428 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
430 new_addr
= new_addr
& ~(r
->size
- 1);
431 last_addr
= new_addr
+ r
->size
- 1;
432 /* NOTE: we have only 64K ioports on PC */
433 if (last_addr
<= new_addr
|| new_addr
== 0 ||
434 last_addr
>= 0x10000) {
441 if (cmd
& PCI_COMMAND_MEMORY
) {
442 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
444 /* the ROM slot has a specific enable bit */
445 if (i
== PCI_ROM_SLOT
&& !(new_addr
& 1))
447 new_addr
= new_addr
& ~(r
->size
- 1);
448 last_addr
= new_addr
+ r
->size
- 1;
449 /* NOTE: we do not support wrapping */
450 /* XXX: as we cannot support really dynamic
451 mappings, we handle specific values as invalid
453 if (last_addr
<= new_addr
|| new_addr
== 0 ||
462 /* now do the real mapping */
463 if (new_addr
!= r
->addr
) {
465 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
467 /* NOTE: specific hack for IDE in PC case:
468 only one byte must be mapped. */
469 class = d
->config
[0x0a] | (d
->config
[0x0b] << 8);
470 if (class == 0x0101 && r
->size
== 4) {
471 isa_unassign_ioport(r
->addr
+ 2, 1);
473 isa_unassign_ioport(r
->addr
, r
->size
);
476 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
479 qemu_unregister_coalesced_mmio(r
->addr
, r
->size
);
484 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
491 static uint32_t pci_read_config(PCIDevice
*d
,
492 uint32_t address
, int len
)
499 if (address
<= 0xfc) {
500 val
= le32_to_cpu(*(uint32_t *)(d
->config
+ address
));
505 if (address
<= 0xfe) {
506 val
= le16_to_cpu(*(uint16_t *)(d
->config
+ address
));
511 val
= d
->config
[address
];
517 static void pci_write_config(PCIDevice
*pci_dev
,
518 uint32_t address
, uint32_t val
, int len
)
521 for (i
= 0; i
< len
; i
++) {
522 pci_dev
->config
[address
+ i
] = val
& 0xff;
527 int pci_access_cap_config(PCIDevice
*pci_dev
, uint32_t address
, int len
)
529 if (pci_dev
->cap
.supported
&& address
>= pci_dev
->cap
.start
&&
530 (address
+ len
) < pci_dev
->cap
.start
+ pci_dev
->cap
.length
)
535 uint32_t pci_default_cap_read_config(PCIDevice
*pci_dev
,
536 uint32_t address
, int len
)
538 return pci_read_config(pci_dev
, address
, len
);
541 void pci_default_cap_write_config(PCIDevice
*pci_dev
,
542 uint32_t address
, uint32_t val
, int len
)
544 pci_write_config(pci_dev
, address
, val
, len
);
547 uint32_t pci_default_read_config(PCIDevice
*d
,
548 uint32_t address
, int len
)
550 if (pci_access_cap_config(d
, address
, len
))
551 return d
->cap
.config_read(d
, address
, len
);
553 return pci_read_config(d
, address
, len
);
556 void pci_default_write_config(PCIDevice
*d
,
557 uint32_t address
, uint32_t val
, int len
)
562 if (len
== 4 && ((address
>= 0x10 && address
< 0x10 + 4 * 6) ||
563 (address
>= 0x30 && address
< 0x34))) {
567 if ( address
>= 0x30 ) {
570 reg
= (address
- 0x10) >> 2;
572 r
= &d
->io_regions
[reg
];
575 /* compute the stored value */
576 if (reg
== PCI_ROM_SLOT
) {
577 /* keep ROM enable bit */
578 val
&= (~(r
->size
- 1)) | 1;
580 val
&= ~(r
->size
- 1);
583 *(uint32_t *)(d
->config
+ address
) = cpu_to_le32(val
);
584 pci_update_mappings(d
);
588 if (pci_access_cap_config(d
, address
, len
)) {
589 d
->cap
.config_write(d
, address
, val
, len
);
593 /* not efficient, but simple */
595 for(i
= 0; i
< len
; i
++) {
596 /* default read/write accesses */
597 switch(d
->config
[0x0e]) {
612 case 0x10 ... 0x27: /* base */
613 case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
614 case 0x30 ... 0x33: /* rom */
637 case 0x2c ... 0x2f: /* read-only subsystem ID & vendor ID */
638 case 0x38 ... 0x3b: /* rom */
649 /* Mask out writes to reserved bits in registers */
652 val
&= ~PCI_COMMAND_RESERVED_MASK_HI
;
655 val
&= ~PCI_STATUS_RESERVED_MASK_LO
;
658 val
&= ~PCI_STATUS_RESERVED_MASK_HI
;
661 d
->config
[addr
] = val
;
668 #ifdef USE_KVM_DEVICE_ASSIGNMENT
669 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel() &&
670 address
>= PIIX_CONFIG_IRQ_ROUTE
&&
671 address
< PIIX_CONFIG_IRQ_ROUTE
+ 4)
672 assigned_dev_update_irqs();
673 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
676 if (end
> PCI_COMMAND
&& address
< (PCI_COMMAND
+ 2)) {
677 /* if the command register is modified, we must modify the mappings */
678 pci_update_mappings(d
);
682 void pci_data_write(void *opaque
, uint32_t addr
, uint32_t val
, int len
)
686 int config_addr
, bus_num
;
688 #if defined(DEBUG_PCI) && 0
689 printf("pci_data_write: addr=%08x val=%08x len=%d\n",
692 bus_num
= (addr
>> 16) & 0xff;
693 while (s
&& s
->bus_num
!= bus_num
)
697 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
700 config_addr
= addr
& 0xff;
701 #if defined(DEBUG_PCI)
702 printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
703 pci_dev
->name
, config_addr
, val
, len
);
705 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
708 uint32_t pci_data_read(void *opaque
, uint32_t addr
, int len
)
712 int config_addr
, bus_num
;
715 bus_num
= (addr
>> 16) & 0xff;
716 while (s
&& s
->bus_num
!= bus_num
)
720 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
737 config_addr
= addr
& 0xff;
738 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
739 #if defined(DEBUG_PCI)
740 printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
741 pci_dev
->name
, config_addr
, val
, len
);
744 #if defined(DEBUG_PCI) && 0
745 printf("pci_data_read: addr=%08x val=%08x len=%d\n",
751 /***********************************************************/
752 /* generic PCI irq support */
754 /* 0 <= irq_num <= 3. level must be 0 or 1 */
755 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
757 PCIDevice
*pci_dev
= (PCIDevice
*)opaque
;
761 change
= level
- pci_dev
->irq_state
[irq_num
];
765 pci_dev
->irq_state
[irq_num
] = level
;
767 #if defined(TARGET_IA64)
768 ioapic_set_irq(pci_dev
, irq_num
, level
);
773 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
776 pci_dev
= bus
->parent_dev
;
778 bus
->irq_count
[irq_num
] += change
;
779 bus
->set_irq(bus
->irq_opaque
, irq_num
, bus
->irq_count
[irq_num
] != 0);
782 int pci_map_irq(PCIDevice
*pci_dev
, int pin
)
784 return pci_dev
->bus
->map_irq(pci_dev
, pin
);
787 /***********************************************************/
788 /* monitor info on PCI */
795 static const pci_class_desc pci_class_descriptions
[] =
797 { 0x0100, "SCSI controller"},
798 { 0x0101, "IDE controller"},
799 { 0x0102, "Floppy controller"},
800 { 0x0103, "IPI controller"},
801 { 0x0104, "RAID controller"},
802 { 0x0106, "SATA controller"},
803 { 0x0107, "SAS controller"},
804 { 0x0180, "Storage controller"},
805 { 0x0200, "Ethernet controller"},
806 { 0x0201, "Token Ring controller"},
807 { 0x0202, "FDDI controller"},
808 { 0x0203, "ATM controller"},
809 { 0x0280, "Network controller"},
810 { 0x0300, "VGA controller"},
811 { 0x0301, "XGA controller"},
812 { 0x0302, "3D controller"},
813 { 0x0380, "Display controller"},
814 { 0x0400, "Video controller"},
815 { 0x0401, "Audio controller"},
817 { 0x0480, "Multimedia controller"},
818 { 0x0500, "RAM controller"},
819 { 0x0501, "Flash controller"},
820 { 0x0580, "Memory controller"},
821 { 0x0600, "Host bridge"},
822 { 0x0601, "ISA bridge"},
823 { 0x0602, "EISA bridge"},
824 { 0x0603, "MC bridge"},
825 { 0x0604, "PCI bridge"},
826 { 0x0605, "PCMCIA bridge"},
827 { 0x0606, "NUBUS bridge"},
828 { 0x0607, "CARDBUS bridge"},
829 { 0x0608, "RACEWAY bridge"},
831 { 0x0c03, "USB controller"},
835 static void pci_info_device(PCIDevice
*d
)
837 Monitor
*mon
= cur_mon
;
840 const pci_class_desc
*desc
;
842 monitor_printf(mon
, " Bus %2d, device %3d, function %d:\n",
843 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7);
844 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
845 monitor_printf(mon
, " ");
846 desc
= pci_class_descriptions
;
847 while (desc
->desc
&& class != desc
->class)
850 monitor_printf(mon
, "%s", desc
->desc
);
852 monitor_printf(mon
, "Class %04x", class);
854 monitor_printf(mon
, ": PCI device %04x:%04x\n",
855 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
856 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))));
858 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
859 monitor_printf(mon
, " IRQ %d.\n",
860 d
->config
[PCI_INTERRUPT_LINE
]);
862 if (class == 0x0604) {
863 monitor_printf(mon
, " BUS %d.\n", d
->config
[0x19]);
865 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
866 r
= &d
->io_regions
[i
];
868 monitor_printf(mon
, " BAR%d: ", i
);
869 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
870 monitor_printf(mon
, "I/O at 0x%04x [0x%04x].\n",
871 r
->addr
, r
->addr
+ r
->size
- 1);
873 monitor_printf(mon
, "32 bit memory at 0x%08x [0x%08x].\n",
874 r
->addr
, r
->addr
+ r
->size
- 1);
878 if (class == 0x0604 && d
->config
[0x19] != 0) {
879 pci_for_each_device(d
->config
[0x19], pci_info_device
);
883 void pci_for_each_device(int bus_num
, void (*fn
)(PCIDevice
*d
))
885 PCIBus
*bus
= first_bus
;
889 while (bus
&& bus
->bus_num
!= bus_num
)
892 for(devfn
= 0; devfn
< 256; devfn
++) {
893 d
= bus
->devices
[devfn
];
900 void pci_info(Monitor
*mon
)
902 pci_for_each_device(0, pci_info_device
);
905 static const char * const pci_nic_models
[] = {
917 static const char * const pci_nic_names
[] = {
929 /* Initialize a PCI NIC. */
930 PCIDevice
*pci_nic_init(PCIBus
*bus
, NICInfo
*nd
, int devfn
,
931 const char *default_model
)
936 qemu_check_nic_model_list(nd
, pci_nic_models
, default_model
);
938 for (i
= 0; pci_nic_models
[i
]; i
++) {
939 if (strcmp(nd
->model
, pci_nic_models
[i
]) == 0) {
940 dev
= qdev_create(&bus
->qbus
, pci_nic_names
[i
]);
941 qdev_set_prop_int(dev
, "devfn", devfn
);
942 qdev_set_netdev(dev
, nd
);
945 return (PCIDevice
*)dev
;
957 static void pci_bridge_write_config(PCIDevice
*d
,
958 uint32_t address
, uint32_t val
, int len
)
960 PCIBridge
*s
= (PCIBridge
*)d
;
962 if (address
== 0x19 || (address
== 0x18 && len
> 1)) {
964 s
->bus
->bus_num
= val
& 0xff;
966 s
->bus
->bus_num
= (val
>> 8) & 0xff;
967 #if defined(DEBUG_PCI)
968 printf ("pci-bridge: %s: Assigned bus %d\n", d
->name
, s
->bus
->bus_num
);
971 pci_default_write_config(d
, address
, val
, len
);
974 PCIBus
*pci_find_bus(int bus_num
)
976 PCIBus
*bus
= first_bus
;
978 while (bus
&& bus
->bus_num
!= bus_num
)
984 PCIDevice
*pci_find_device(int bus_num
, int slot
, int function
)
986 PCIBus
*bus
= pci_find_bus(bus_num
);
991 return bus
->devices
[PCI_DEVFN(slot
, function
)];
994 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, uint16_t vid
, uint16_t did
,
995 pci_map_irq_fn map_irq
, const char *name
)
998 s
= (PCIBridge
*)pci_register_device(bus
, name
, sizeof(PCIBridge
),
999 devfn
, NULL
, pci_bridge_write_config
);
1001 pci_config_set_vendor_id(s
->dev
.config
, vid
);
1002 pci_config_set_device_id(s
->dev
.config
, did
);
1004 s
->dev
.config
[0x04] = 0x06; // command = bus master, pci mem
1005 s
->dev
.config
[0x05] = 0x00;
1006 s
->dev
.config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
1007 s
->dev
.config
[0x07] = 0x00; // status = fast devsel
1008 s
->dev
.config
[0x08] = 0x00; // revision
1009 s
->dev
.config
[0x09] = 0x00; // programming i/f
1010 pci_config_set_class(s
->dev
.config
, PCI_CLASS_BRIDGE_PCI
);
1011 s
->dev
.config
[0x0D] = 0x10; // latency_timer
1012 s
->dev
.config
[PCI_HEADER_TYPE
] =
1013 PCI_HEADER_TYPE_MULTI_FUNCTION
| PCI_HEADER_TYPE_BRIDGE
; // header_type
1014 s
->dev
.config
[0x1E] = 0xa0; // secondary status
1016 s
->bus
= pci_register_secondary_bus(&s
->dev
, map_irq
);
1022 pci_qdev_initfn init
;
1025 static void pci_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
1027 PCIDevice
*pci_dev
= (PCIDevice
*)qdev
;
1028 PCIDeviceInfo
*info
= container_of(base
, PCIDeviceInfo
, qdev
);
1032 bus
= FROM_QBUS(PCIBus
, qdev_get_parent_bus(qdev
));
1033 devfn
= qdev_get_prop_int(qdev
, "devfn", -1);
1034 pci_dev
= do_pci_register_device(pci_dev
, bus
, "FIXME", devfn
,
1035 NULL
, NULL
);//FIXME:config_read, config_write);
1037 info
->init(pci_dev
);
1040 void pci_qdev_register(const char *name
, int size
, pci_qdev_initfn init
)
1042 PCIDeviceInfo
*info
;
1044 info
= qemu_mallocz(sizeof(*info
));
1045 info
->qdev
.name
= qemu_strdup(name
);
1046 info
->qdev
.size
= size
;
1048 info
->qdev
.init
= pci_qdev_init
;
1049 info
->qdev
.bus_type
= BUS_TYPE_PCI
;
1051 qdev_register(&info
->qdev
);
1054 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
1058 dev
= qdev_create(&bus
->qbus
, name
);
1059 qdev_set_prop_int(dev
, "devfn", devfn
);
1062 return (PCIDevice
*)dev
;
1065 int pci_enable_capability_support(PCIDevice
*pci_dev
,
1066 uint32_t config_start
,
1067 PCICapConfigReadFunc
*config_read
,
1068 PCICapConfigWriteFunc
*config_write
,
1069 PCICapConfigInitFunc
*config_init
)
1074 pci_dev
->config
[0x06] |= 0x10; // status = capabilities
1076 if (config_start
== 0)
1077 pci_dev
->cap
.start
= PCI_CAPABILITY_CONFIG_DEFAULT_START_ADDR
;
1078 else if (config_start
>= 0x40 && config_start
< 0xff)
1079 pci_dev
->cap
.start
= config_start
;
1084 pci_dev
->cap
.config_read
= config_read
;
1086 pci_dev
->cap
.config_read
= pci_default_cap_read_config
;
1088 pci_dev
->cap
.config_write
= config_write
;
1090 pci_dev
->cap
.config_write
= pci_default_cap_write_config
;
1091 pci_dev
->cap
.supported
= 1;
1092 pci_dev
->config
[PCI_CAPABILITY_LIST
] = pci_dev
->cap
.start
;
1093 return config_init(pci_dev
);