From: Shameer Kolothum Date: Thu, 29 Jan 2026 13:32:04 +0000 (+0000) Subject: hw/pci/pci: Move pci_init_bus_master() after adding device to bus X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4eab7d4a1480016876e74081ffef10ab495210b;p=thirdparty%2Fqemu.git hw/pci/pci: Move pci_init_bus_master() after adding device to bus During PCI hotplug, in do_pci_register_device(), pci_init_bus_master() is called before storing the pci_dev pointer in bus->devices[devfn]. This causes a problem if pci_init_bus_master() (via its get_address_space() callback) attempts to retrieve the device using pci_find_device(), since the PCI device is not yet visible on the bus. Fix this by moving the pci_init_bus_master() call to after the device has been added to bus->devices[devfn]. This prepares for a subsequent patch where the accel SMMUv3 get_address_space() callback retrieves the pci_dev to identify the attached device type. No functional change intended. Cc: Michael S. Tsirkin Reviewed-by: Jonathan Cameron Reviewed-by: Eric Auger Reviewed-by: Nicolin Chen Reviewed-by: Michael S. Tsirkin Tested-by: Eric Auger Tested-by: Zhangfei Gao Signed-off-by: Shameer Kolothum Reviewed-by: Yi Liu Message-id: 20260126104342.253965-8-skolothumtho@nvidia.com Signed-off-by: Peter Maydell --- diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 8cbf5f5d70..229ea7cfb1 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -1381,9 +1381,6 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, pci_dev->bus_master_as.max_bounce_buffer_size = pci_dev->max_bounce_buffer_size; - if (phase_check(PHASE_MACHINE_READY)) { - pci_init_bus_master(pci_dev); - } pci_dev->irq_state = 0; pci_config_alloc(pci_dev); @@ -1427,6 +1424,9 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, pci_dev->config_write = config_write; bus->devices[devfn] = pci_dev; pci_dev->version_id = 2; /* Current pci device vmstate version */ + if (phase_check(PHASE_MACHINE_READY)) { + pci_init_bus_master(pci_dev); + } return pci_dev; }