Generalize this function out of pci_setup_msi(), then it can be further
used to enable INTx (or, disable MSI).

Signed-off-by: Peter Xu <pet...@redhat.com>
---
 lib/pci.c | 19 +++++++++++++++++--
 lib/pci.h |  1 +
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/lib/pci.c b/lib/pci.c
index a7cfacb..5062036 100644
--- a/lib/pci.c
+++ b/lib/pci.c
@@ -39,6 +39,22 @@ void pci_cap_walk(struct pci_dev *dev)
        }
 }
 
+void pci_msi_set_enable(struct pci_dev *dev, bool enabled)
+{
+       uint16_t msi_control;
+       uint16_t offset;
+
+       offset = dev->msi_offset;
+       msi_control = pci_config_readw(dev->bdf, offset + PCI_MSI_FLAGS);
+
+       if (enabled)
+               msi_control |= PCI_MSI_FLAGS_ENABLE;
+       else
+               msi_control &= ~PCI_MSI_FLAGS_ENABLE;
+
+       pci_config_writew(dev->bdf, offset + PCI_MSI_FLAGS, msi_control);
+}
+
 bool pci_setup_msi(struct pci_dev *dev, uint64_t msi_addr, uint32_t msi_data)
 {
        uint16_t msi_control;
@@ -69,8 +85,7 @@ bool pci_setup_msi(struct pci_dev *dev, uint64_t msi_addr, 
uint32_t msi_data)
        }
        printf("addr=0x%lx, data=0x%x\n", msi_addr, msi_data);
 
-       msi_control |= PCI_MSI_FLAGS_ENABLE;
-       pci_config_writew(addr, offset + PCI_MSI_FLAGS, msi_control);
+       pci_msi_set_enable(dev, true);
 
        return true;
 }
diff --git a/lib/pci.h b/lib/pci.h
index df86951..0815754 100644
--- a/lib/pci.h
+++ b/lib/pci.h
@@ -65,6 +65,7 @@ extern bool pci_bar_is_valid(struct pci_dev *dev, int 
bar_num);
 extern void pci_bar_print(struct pci_dev *dev, int bar_num);
 extern void pci_dev_print_id(pcidevaddr_t dev);
 extern uint8_t pci_intx_line(struct pci_dev *dev);
+void pci_msi_set_enable(struct pci_dev *dev, bool enabled);
 
 extern int pci_testdev(void);
 
-- 
2.7.4


Reply via email to