@@ -36,6 +36,7 @@
#include "hw/pci/msix.h"
#include "exec/address-spaces.h"
#include "hw/hotplug.h"
+#include "qemu/bitmap.h"
//#define DEBUG_PCI
#ifdef DEBUG_PCI
@@ -316,6 +317,12 @@ static void pci_bus_init(PCIBus *bus, DeviceState *parent,
QLIST_INIT(&bus->child);
pci_host_bus_register(bus, parent);
+
+ if (bus->pci_bdf_bitmap) {
+ g_free(bus->pci_bdf_bitmap);
+ }
+ bus->pci_bdf_bitmap = bitmap_new(PCI_SLOT_MAX * PCI_FUNC_MAX);
+ bitmap_clear(bus->pci_bdf_bitmap, 0, PCI_SLOT_MAX * PCI_FUNC_MAX);
}
bool pci_bus_is_express(PCIBus *bus)
@@ -798,6 +805,7 @@ static void pci_config_free(PCIDevice *pci_dev)
static void do_pci_unregister_device(PCIDevice *pci_dev)
{
+ clear_bit(pci_dev->devfn, pci_dev->bus->pci_bdf_bitmap);
pci_dev->bus->devices[pci_dev->devfn] = NULL;
pci_config_free(pci_dev);
@@ -817,8 +825,9 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
if (devfn < 0) {
for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
devfn += PCI_FUNC_MAX) {
- if (!bus->devices[devfn])
+ if (!test_bit(devfn, bus->pci_bdf_bitmap)) {
goto found;
+ }
}
error_report("PCI: no slot/function available for %s, all in use", name);
return NULL;
@@ -840,6 +849,7 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
name);
pci_dev->devfn = devfn;
+ set_bit(pci_dev->devfn, pci_dev->bus->pci_bdf_bitmap);
pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
pci_dev->irq_state = 0;
pci_config_alloc(pci_dev);
@@ -19,6 +19,7 @@ struct PCIBus {
void *irq_opaque;
PCIDevice *devices[PCI_SLOT_MAX * PCI_FUNC_MAX];
PCIDevice *parent_dev;
+ unsigned long *pci_bdf_bitmap;
MemoryRegion *address_space_mem;
MemoryRegion *address_space_io;
It would be convenient to manage devfn directly in some cases. Signed-off-by: Tiejun Chen <tiejun.chen@intel.com> --- v2: * New patch hw/pci/pci.c | 12 +++++++++++- include/hw/pci/pci_bus.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-)