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 static void pci_bus_reset(void *opaque
)
95 PCIBus
*bus
= (PCIBus
*)opaque
;
98 for (i
= 0; i
< bus
->nirq
; i
++) {
99 bus
->irq_count
[i
] = 0;
101 for (i
= 0; i
< 256; i
++) {
103 memset(bus
->devices
[i
]->irq_state
, 0,
104 sizeof(bus
->devices
[i
]->irq_state
));
108 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
109 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
110 qemu_irq
*pic
, int devfn_min
, int nirq
)
115 bus
= FROM_QBUS(PCIBus
, qbus_create(BUS_TYPE_PCI
,
116 sizeof(PCIBus
) + (nirq
* sizeof(int)),
118 bus
->set_irq
= set_irq
;
119 bus
->map_irq
= map_irq
;
120 bus
->irq_opaque
= pic
;
121 bus
->devfn_min
= devfn_min
;
123 bus
->next
= first_bus
;
125 register_savevm("PCIBUS", nbus
++, 1, pcibus_save
, pcibus_load
, bus
);
126 qemu_register_reset(pci_bus_reset
, 0, bus
);
130 static PCIBus
*pci_register_secondary_bus(PCIDevice
*dev
, pci_map_irq_fn map_irq
)
133 bus
= qemu_mallocz(sizeof(PCIBus
));
134 bus
->map_irq
= map_irq
;
135 bus
->parent_dev
= dev
;
136 bus
->next
= dev
->bus
->next
;
137 dev
->bus
->next
= bus
;
141 int pci_bus_num(PCIBus
*s
)
146 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
150 qemu_put_be32(f
, 2); /* PCI device version */
151 qemu_put_buffer(f
, s
->config
, 256);
152 for (i
= 0; i
< 4; i
++)
153 qemu_put_be32(f
, s
->irq_state
[i
]);
156 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
161 version_id
= qemu_get_be32(f
);
164 qemu_get_buffer(f
, s
->config
, 256);
165 pci_update_mappings(s
);
168 for (i
= 0; i
< 4; i
++)
169 s
->irq_state
[i
] = qemu_get_be32(f
);
174 static int pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
178 id
= (void*)(&pci_dev
->config
[PCI_SUBVENDOR_ID
]);
179 id
[0] = cpu_to_le16(pci_default_sub_vendor_id
);
180 id
[1] = cpu_to_le16(pci_default_sub_device_id
);
185 * Parse pci address in qemu command
186 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
188 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
193 unsigned long dom
= 0, bus
= 0;
197 val
= strtoul(p
, &e
, 16);
203 val
= strtoul(p
, &e
, 16);
210 val
= strtoul(p
, &e
, 16);
216 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
224 /* Note: QEMU doesn't implement domains other than 0 */
225 if (dom
!= 0 || pci_find_bus(bus
) == NULL
)
235 * Parse device bdf in device assignment command:
237 * -pcidevice host=bus:dev.func
239 * Parse <bus>:<slot>.<func> return -1 on error
241 int pci_parse_host_devaddr(const char *addr
, int *busp
,
242 int *slotp
, int *funcp
)
247 int bus
= 0, slot
= 0, func
= 0;
250 val
= strtoul(p
, &e
, 16);
256 val
= strtoul(p
, &e
, 16);
262 val
= strtoul(p
, &e
, 16);
271 if (bus
> 0xff || slot
> 0x1f || func
> 0x7)
283 int pci_read_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
287 if (!get_param_value(devaddr
, sizeof(devaddr
), "pci_addr", addr
))
290 return pci_parse_devaddr(devaddr
, domp
, busp
, slotp
);
293 static PCIBus
*pci_get_bus_devfn(int *devfnp
, const char *devaddr
)
300 return pci_find_bus(0);
303 if (pci_parse_devaddr(devaddr
, &dom
, &bus
, &slot
) < 0) {
308 return pci_find_bus(bus
);
311 static void pci_init_wmask(PCIDevice
*dev
)
314 dev
->wmask
[PCI_CACHE_LINE_SIZE
] = 0xff;
315 dev
->wmask
[PCI_INTERRUPT_LINE
] = 0xff;
316 dev
->wmask
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
317 | PCI_COMMAND_MASTER
;
318 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
319 dev
->wmask
[i
] = 0xff;
322 /* -1 for devfn means auto assign */
323 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
324 const char *name
, int devfn
,
325 PCIConfigReadFunc
*config_read
,
326 PCIConfigWriteFunc
*config_write
)
329 for(devfn
= bus
->devfn_min
; devfn
< 256; devfn
+= 8) {
330 if (!bus
->devices
[devfn
])
335 } else if (bus
->devices
[devfn
]) {
339 pci_dev
->devfn
= devfn
;
340 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
341 memset(pci_dev
->irq_state
, 0, sizeof(pci_dev
->irq_state
));
342 pci_set_default_subsystem_id(pci_dev
);
343 pci_init_wmask(pci_dev
);
346 config_read
= pci_default_read_config
;
348 config_write
= pci_default_write_config
;
349 pci_dev
->config_read
= config_read
;
350 pci_dev
->config_write
= config_write
;
351 bus
->devices
[devfn
] = pci_dev
;
352 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, 4);
356 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
357 int instance_size
, int devfn
,
358 PCIConfigReadFunc
*config_read
,
359 PCIConfigWriteFunc
*config_write
)
363 pci_dev
= qemu_mallocz(instance_size
);
364 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
365 config_read
, config_write
);
368 static target_phys_addr_t
pci_to_cpu_addr(target_phys_addr_t addr
)
370 return addr
+ pci_mem_base
;
373 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
378 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
379 r
= &pci_dev
->io_regions
[i
];
380 if (!r
->size
|| r
->addr
== -1)
382 if (r
->type
== PCI_ADDRESS_SPACE_IO
) {
383 isa_unassign_ioport(r
->addr
, r
->size
);
385 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
392 int pci_unregister_device(PCIDevice
*pci_dev
, int assigned
)
396 if (pci_dev
->unregister
)
397 ret
= pci_dev
->unregister(pci_dev
);
401 pci_unregister_io_regions(pci_dev
);
403 qemu_free_irqs(pci_dev
->irq
);
404 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
409 qdev_free(&pci_dev
->qdev
);
413 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
414 uint32_t size
, int type
,
415 PCIMapIORegionFunc
*map_func
)
421 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
424 if (size
& (size
-1)) {
425 fprintf(stderr
, "ERROR: PCI region size must be pow2 "
426 "type=0x%x, size=0x%x\n", type
, size
);
430 r
= &pci_dev
->io_regions
[region_num
];
434 r
->map_func
= map_func
;
437 if (region_num
== PCI_ROM_SLOT
) {
439 /* ROM enable bit is writeable */
442 addr
= 0x10 + region_num
* 4;
444 *(uint32_t *)(pci_dev
->config
+ addr
) = cpu_to_le32(type
);
445 *(uint32_t *)(pci_dev
->wmask
+ addr
) = cpu_to_le32(wmask
);
448 static void pci_update_mappings(PCIDevice
*d
)
452 uint32_t last_addr
, new_addr
, config_ofs
;
454 cmd
= le16_to_cpu(*(uint16_t *)(d
->config
+ PCI_COMMAND
));
455 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
456 r
= &d
->io_regions
[i
];
457 if (i
== PCI_ROM_SLOT
) {
460 config_ofs
= 0x10 + i
* 4;
463 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
464 if (cmd
& PCI_COMMAND_IO
) {
465 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
467 new_addr
= new_addr
& ~(r
->size
- 1);
468 last_addr
= new_addr
+ r
->size
- 1;
469 /* NOTE: we have only 64K ioports on PC */
470 if (last_addr
<= new_addr
|| new_addr
== 0 ||
471 last_addr
>= 0x10000) {
478 if (cmd
& PCI_COMMAND_MEMORY
) {
479 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
481 /* the ROM slot has a specific enable bit */
482 if (i
== PCI_ROM_SLOT
&& !(new_addr
& 1))
484 new_addr
= new_addr
& ~(r
->size
- 1);
485 last_addr
= new_addr
+ r
->size
- 1;
486 /* NOTE: we do not support wrapping */
487 /* XXX: as we cannot support really dynamic
488 mappings, we handle specific values as invalid
490 if (last_addr
<= new_addr
|| new_addr
== 0 ||
499 /* now do the real mapping */
500 if (new_addr
!= r
->addr
) {
502 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
504 /* NOTE: specific hack for IDE in PC case:
505 only one byte must be mapped. */
506 class = d
->config
[0x0a] | (d
->config
[0x0b] << 8);
507 if (class == 0x0101 && r
->size
== 4) {
508 isa_unassign_ioport(r
->addr
+ 2, 1);
510 isa_unassign_ioport(r
->addr
, r
->size
);
513 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
516 qemu_unregister_coalesced_mmio(r
->addr
, r
->size
);
521 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
528 static uint32_t pci_read_config(PCIDevice
*d
,
529 uint32_t address
, int len
)
536 if (address
<= 0xfc) {
537 val
= le32_to_cpu(*(uint32_t *)(d
->config
+ address
));
542 if (address
<= 0xfe) {
543 val
= le16_to_cpu(*(uint16_t *)(d
->config
+ address
));
548 val
= d
->config
[address
];
554 static void pci_write_config(PCIDevice
*pci_dev
,
555 uint32_t address
, uint32_t val
, int len
)
558 for (i
= 0; i
< len
; i
++) {
559 pci_dev
->config
[address
+ i
] = val
& 0xff;
564 int pci_access_cap_config(PCIDevice
*pci_dev
, uint32_t address
, int len
)
566 if (pci_dev
->cap
.supported
&& address
>= pci_dev
->cap
.start
&&
567 (address
+ len
) < pci_dev
->cap
.start
+ pci_dev
->cap
.length
)
572 uint32_t pci_default_cap_read_config(PCIDevice
*pci_dev
,
573 uint32_t address
, int len
)
575 return pci_read_config(pci_dev
, address
, len
);
578 void pci_default_cap_write_config(PCIDevice
*pci_dev
,
579 uint32_t address
, uint32_t val
, int len
)
581 pci_write_config(pci_dev
, address
, val
, len
);
584 uint32_t pci_default_read_config(PCIDevice
*d
,
585 uint32_t address
, int len
)
587 if (pci_access_cap_config(d
, address
, len
))
588 return d
->cap
.config_read(d
, address
, len
);
590 return pci_read_config(d
, address
, len
);
593 void pci_default_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int l
)
595 uint8_t orig
[PCI_CONFIG_SPACE_SIZE
];
598 if (pci_access_cap_config(d
, addr
, l
)) {
599 d
->cap
.config_write(d
, addr
, val
, l
);
603 /* not efficient, but simple */
604 memcpy(orig
, d
->config
, PCI_CONFIG_SPACE_SIZE
);
605 for(i
= 0; i
< l
&& addr
< PCI_CONFIG_SPACE_SIZE
; val
>>= 8, ++i
, ++addr
) {
606 uint8_t wmask
= d
->wmask
[addr
];
607 d
->config
[addr
] = (d
->config
[addr
] & ~wmask
) | (val
& wmask
);
610 #ifdef USE_KVM_DEVICE_ASSIGNMENT
611 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel() &&
612 addr
>= PIIX_CONFIG_IRQ_ROUTE
&&
613 addr
< PIIX_CONFIG_IRQ_ROUTE
+ 4)
614 assigned_dev_update_irqs();
615 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
617 if (memcmp(orig
+ PCI_BASE_ADDRESS_0
, d
->config
+ PCI_BASE_ADDRESS_0
, 24)
618 || ((orig
[PCI_COMMAND
] ^ d
->config
[PCI_COMMAND
])
619 & (PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
)))
620 pci_update_mappings(d
);
624 void pci_data_write(void *opaque
, uint32_t addr
, uint32_t val
, int len
)
628 int config_addr
, bus_num
;
630 #if defined(DEBUG_PCI) && 0
631 printf("pci_data_write: addr=%08x val=%08x len=%d\n",
634 bus_num
= (addr
>> 16) & 0xff;
635 while (s
&& s
->bus_num
!= bus_num
)
639 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
642 config_addr
= addr
& 0xff;
643 #if defined(DEBUG_PCI)
644 printf("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
645 pci_dev
->name
, config_addr
, val
, len
);
647 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
650 uint32_t pci_data_read(void *opaque
, uint32_t addr
, int len
)
654 int config_addr
, bus_num
;
657 bus_num
= (addr
>> 16) & 0xff;
658 while (s
&& s
->bus_num
!= bus_num
)
662 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
679 config_addr
= addr
& 0xff;
680 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
681 #if defined(DEBUG_PCI)
682 printf("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
683 pci_dev
->name
, config_addr
, val
, len
);
686 #if defined(DEBUG_PCI) && 0
687 printf("pci_data_read: addr=%08x val=%08x len=%d\n",
693 /***********************************************************/
694 /* generic PCI irq support */
696 /* 0 <= irq_num <= 3. level must be 0 or 1 */
697 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
699 PCIDevice
*pci_dev
= (PCIDevice
*)opaque
;
703 change
= level
- pci_dev
->irq_state
[irq_num
];
707 pci_dev
->irq_state
[irq_num
] = level
;
709 #if defined(TARGET_IA64)
710 ioapic_set_irq(pci_dev
, irq_num
, level
);
715 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
718 pci_dev
= bus
->parent_dev
;
720 bus
->irq_count
[irq_num
] += change
;
721 bus
->set_irq(bus
->irq_opaque
, irq_num
, bus
->irq_count
[irq_num
] != 0);
724 int pci_map_irq(PCIDevice
*pci_dev
, int pin
)
726 return pci_dev
->bus
->map_irq(pci_dev
, pin
);
729 /***********************************************************/
730 /* monitor info on PCI */
737 static const pci_class_desc pci_class_descriptions
[] =
739 { 0x0100, "SCSI controller"},
740 { 0x0101, "IDE controller"},
741 { 0x0102, "Floppy controller"},
742 { 0x0103, "IPI controller"},
743 { 0x0104, "RAID controller"},
744 { 0x0106, "SATA controller"},
745 { 0x0107, "SAS controller"},
746 { 0x0180, "Storage controller"},
747 { 0x0200, "Ethernet controller"},
748 { 0x0201, "Token Ring controller"},
749 { 0x0202, "FDDI controller"},
750 { 0x0203, "ATM controller"},
751 { 0x0280, "Network controller"},
752 { 0x0300, "VGA controller"},
753 { 0x0301, "XGA controller"},
754 { 0x0302, "3D controller"},
755 { 0x0380, "Display controller"},
756 { 0x0400, "Video controller"},
757 { 0x0401, "Audio controller"},
759 { 0x0480, "Multimedia controller"},
760 { 0x0500, "RAM controller"},
761 { 0x0501, "Flash controller"},
762 { 0x0580, "Memory controller"},
763 { 0x0600, "Host bridge"},
764 { 0x0601, "ISA bridge"},
765 { 0x0602, "EISA bridge"},
766 { 0x0603, "MC bridge"},
767 { 0x0604, "PCI bridge"},
768 { 0x0605, "PCMCIA bridge"},
769 { 0x0606, "NUBUS bridge"},
770 { 0x0607, "CARDBUS bridge"},
771 { 0x0608, "RACEWAY bridge"},
773 { 0x0c03, "USB controller"},
777 static void pci_info_device(PCIDevice
*d
)
779 Monitor
*mon
= cur_mon
;
782 const pci_class_desc
*desc
;
784 monitor_printf(mon
, " Bus %2d, device %3d, function %d:\n",
785 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7);
786 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
787 monitor_printf(mon
, " ");
788 desc
= pci_class_descriptions
;
789 while (desc
->desc
&& class != desc
->class)
792 monitor_printf(mon
, "%s", desc
->desc
);
794 monitor_printf(mon
, "Class %04x", class);
796 monitor_printf(mon
, ": PCI device %04x:%04x\n",
797 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
798 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))));
800 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
801 monitor_printf(mon
, " IRQ %d.\n",
802 d
->config
[PCI_INTERRUPT_LINE
]);
804 if (class == 0x0604) {
805 monitor_printf(mon
, " BUS %d.\n", d
->config
[0x19]);
807 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
808 r
= &d
->io_regions
[i
];
810 monitor_printf(mon
, " BAR%d: ", i
);
811 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
812 monitor_printf(mon
, "I/O at 0x%04x [0x%04x].\n",
813 r
->addr
, r
->addr
+ r
->size
- 1);
815 monitor_printf(mon
, "32 bit memory at 0x%08x [0x%08x].\n",
816 r
->addr
, r
->addr
+ r
->size
- 1);
820 if (class == 0x0604 && d
->config
[0x19] != 0) {
821 pci_for_each_device(d
->config
[0x19], pci_info_device
);
825 void pci_for_each_device(int bus_num
, void (*fn
)(PCIDevice
*d
))
827 PCIBus
*bus
= first_bus
;
831 while (bus
&& bus
->bus_num
!= bus_num
)
834 for(devfn
= 0; devfn
< 256; devfn
++) {
835 d
= bus
->devices
[devfn
];
842 void pci_info(Monitor
*mon
)
844 pci_for_each_device(0, pci_info_device
);
847 PCIDevice
*pci_create(const char *name
, const char *devaddr
)
853 bus
= pci_get_bus_devfn(&devfn
, devaddr
);
855 fprintf(stderr
, "Invalid PCI device address %s for device %s\n",
860 dev
= qdev_create(&bus
->qbus
, name
);
861 qdev_set_prop_int(dev
, "devfn", devfn
);
862 return (PCIDevice
*)dev
;
865 static const char * const pci_nic_models
[] = {
877 static const char * const pci_nic_names
[] = {
889 /* Initialize a PCI NIC. */
890 PCIDevice
*pci_nic_init(NICInfo
*nd
, const char *default_model
,
891 const char *default_devaddr
)
893 const char *devaddr
= nd
->devaddr
? nd
->devaddr
: default_devaddr
;
898 qemu_check_nic_model_list(nd
, pci_nic_models
, default_model
);
900 for (i
= 0; pci_nic_models
[i
]; i
++) {
901 if (strcmp(nd
->model
, pci_nic_models
[i
]) == 0) {
902 pci_dev
= pci_create(pci_nic_names
[i
], devaddr
);
903 dev
= &pci_dev
->qdev
;
904 qdev_set_netdev(dev
, nd
);
919 static void pci_bridge_write_config(PCIDevice
*d
,
920 uint32_t address
, uint32_t val
, int len
)
922 PCIBridge
*s
= (PCIBridge
*)d
;
924 pci_default_write_config(d
, address
, val
, len
);
925 s
->bus
->bus_num
= d
->config
[PCI_SECONDARY_BUS
];
928 PCIBus
*pci_find_bus(int bus_num
)
930 PCIBus
*bus
= first_bus
;
932 while (bus
&& bus
->bus_num
!= bus_num
)
938 PCIDevice
*pci_find_device(int bus_num
, int slot
, int function
)
940 PCIBus
*bus
= pci_find_bus(bus_num
);
945 return bus
->devices
[PCI_DEVFN(slot
, function
)];
948 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, uint16_t vid
, uint16_t did
,
949 pci_map_irq_fn map_irq
, const char *name
)
952 s
= (PCIBridge
*)pci_register_device(bus
, name
, sizeof(PCIBridge
),
953 devfn
, NULL
, pci_bridge_write_config
);
955 pci_config_set_vendor_id(s
->dev
.config
, vid
);
956 pci_config_set_device_id(s
->dev
.config
, did
);
958 s
->dev
.config
[0x04] = 0x06; // command = bus master, pci mem
959 s
->dev
.config
[0x05] = 0x00;
960 s
->dev
.config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
961 s
->dev
.config
[0x07] = 0x00; // status = fast devsel
962 s
->dev
.config
[0x08] = 0x00; // revision
963 s
->dev
.config
[0x09] = 0x00; // programming i/f
964 pci_config_set_class(s
->dev
.config
, PCI_CLASS_BRIDGE_PCI
);
965 s
->dev
.config
[0x0D] = 0x10; // latency_timer
966 s
->dev
.config
[PCI_HEADER_TYPE
] =
967 PCI_HEADER_TYPE_MULTI_FUNCTION
| PCI_HEADER_TYPE_BRIDGE
; // header_type
968 s
->dev
.config
[0x1E] = 0xa0; // secondary status
970 s
->bus
= pci_register_secondary_bus(&s
->dev
, map_irq
);
976 pci_qdev_initfn init
;
979 static void pci_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
981 PCIDevice
*pci_dev
= (PCIDevice
*)qdev
;
982 PCIDeviceInfo
*info
= container_of(base
, PCIDeviceInfo
, qdev
);
986 bus
= FROM_QBUS(PCIBus
, qdev_get_parent_bus(qdev
));
987 devfn
= qdev_get_prop_int(qdev
, "devfn", -1);
988 pci_dev
= do_pci_register_device(pci_dev
, bus
, "FIXME", devfn
,
989 NULL
, NULL
);//FIXME:config_read, config_write);
994 void pci_qdev_register(const char *name
, int size
, pci_qdev_initfn init
)
998 info
= qemu_mallocz(sizeof(*info
));
999 info
->qdev
.name
= qemu_strdup(name
);
1000 info
->qdev
.size
= size
;
1002 info
->qdev
.init
= pci_qdev_init
;
1003 info
->qdev
.bus_type
= BUS_TYPE_PCI
;
1005 qdev_register(&info
->qdev
);
1008 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
1012 dev
= qdev_create(&bus
->qbus
, name
);
1013 qdev_set_prop_int(dev
, "devfn", devfn
);
1016 return (PCIDevice
*)dev
;
1019 int pci_enable_capability_support(PCIDevice
*pci_dev
,
1020 uint32_t config_start
,
1021 PCICapConfigReadFunc
*config_read
,
1022 PCICapConfigWriteFunc
*config_write
,
1023 PCICapConfigInitFunc
*config_init
)
1028 pci_dev
->config
[0x06] |= 0x10; // status = capabilities
1030 if (config_start
== 0)
1031 pci_dev
->cap
.start
= PCI_CAPABILITY_CONFIG_DEFAULT_START_ADDR
;
1032 else if (config_start
>= 0x40 && config_start
< 0xff)
1033 pci_dev
->cap
.start
= config_start
;
1038 pci_dev
->cap
.config_read
= config_read
;
1040 pci_dev
->cap
.config_read
= pci_default_cap_read_config
;
1042 pci_dev
->cap
.config_write
= config_write
;
1044 pci_dev
->cap
.config_write
= pci_default_cap_write_config
;
1045 pci_dev
->cap
.supported
= 1;
1046 pci_dev
->config
[PCI_CAPABILITY_LIST
] = pci_dev
->cap
.start
;
1047 return config_init(pci_dev
);