Add method to attach PCI device with new name.

- Added an overloaded version of attach_pci_device which takes a suffix string that can be used to make a device name unique so that multiple devices of the same class can be added to a machine. The method returns a PCIDevice which can be easily used to attach more PCI devices if it is a PCIHost.
This commit is contained in:
joevt 2023-02-05 01:12:50 -08:00
parent f61854a0ae
commit bee24b166d
2 changed files with 27 additions and 9 deletions

View File

@ -83,27 +83,44 @@ bool PCIHost::pci_unregister_mmio_region(uint32_t start_addr, uint32_t size, PCI
return mem_ctrl->remove_mmio_region(start_addr, size, obj);
}
void PCIHost::attach_pci_device(std::string& dev_name, int slot_id)
void PCIHost::attach_pci_device(const std::string& dev_name, int slot_id)
{
this->attach_pci_device(dev_name, slot_id, "");
}
PCIDevice *PCIHost::attach_pci_device(const std::string& dev_name, int slot_id, const std::string& dev_suffix)
{
if (!DeviceRegistry::device_registered(dev_name)) {
LOG_F(WARNING, "PCIHost: specified PCI device %s doesn't exist", dev_name.c_str());
return;
HWComponent *hwc = dynamic_cast<HWComponent*>(this);
LOG_F(
WARNING, "%s: specified PCI device %s doesn't exist",
hwc ? hwc->get_name().c_str() : "PCIHost", dev_name.c_str()
);
return NULL;
}
// attempt to create device object
auto dev_obj = DeviceRegistry::get_descriptor(dev_name).m_create_func();
if (!dev_obj->supports_type(HWCompType::PCI_DEV)) {
LOG_F(WARNING, "PCIHost: cannot attach non-PCI device %s", dev_name.c_str());
return;
HWComponent *hwc = dynamic_cast<HWComponent*>(this);
LOG_F(
WARNING, "%s: cannot attach non-PCI device %s",
hwc ? hwc->get_name().c_str() : "PCIHost", dev_name.c_str()
);
return NULL;
}
// add device to the machine object
gMachineObj->add_device(dev_name, std::move(dev_obj));
gMachineObj->add_device(dev_name + dev_suffix, std::move(dev_obj));
PCIDevice *dev = dynamic_cast<PCIDevice*>(gMachineObj->get_comp_by_name(dev_name + dev_suffix));
// register device with the PCI host
this->pci_register_device(
slot_id, dynamic_cast<PCIDevice*>(gMachineObj->get_comp_by_name(dev_name)));
this->pci_register_device(slot_id, dev);
return dev;
}
bool PCIHost::pci_io_read_loop(uint32_t offset, int size, uint32_t &res)

View File

@ -66,7 +66,8 @@ public:
virtual bool pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj);
virtual bool pci_unregister_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj);
virtual void attach_pci_device(std::string& dev_name, int slot_id);
virtual void attach_pci_device(const std::string& dev_name, int slot_id);
PCIDevice *attach_pci_device(const std::string& dev_name, int slot_id, const std::string& dev_suffix);
virtual bool pci_io_read_loop (uint32_t offset, int size, uint32_t &res);
virtual bool pci_io_write_loop(uint32_t offset, int size, uint32_t value);