diff --git a/devicemodel/hw/pci/core.c b/devicemodel/hw/pci/core.c index 2bb62384b..ce35230d0 100644 --- a/devicemodel/hw/pci/core.c +++ b/devicemodel/hw/pci/core.c @@ -703,12 +703,23 @@ pci_emul_alloc_pbar(struct pci_vdev *pdi, int idx, uint64_t hostbase, void pci_emul_free_bars(struct pci_vdev *pdi) { - int i; + int i, enabled; for (i = 0; i < PCI_BARMAX; i++) { if ((pdi->bar[i].type != PCIBAR_NONE) && (pdi->bar[i].type != PCIBAR_MEMHI64)){ - unregister_bar(pdi, i); + /* + * Check whether the bar is enabled or not, + * if it is disabled then it should have been + * unregistered in pci_emul_cmdsts_write. + */ + if (pdi->bar[i].type == PCIBAR_IO) + enabled = porten(pdi); + else + enabled = memen(pdi); + + if (enabled) + unregister_bar(pdi, i); pdi->bar[i].type = PCIBAR_NONE; } }