From fbb2f1511cb7536cbe18ee6b7003d661a92e1bb4 Mon Sep 17 00:00:00 2001 From: Tomasz Bursztyka Date: Mon, 15 Mar 2021 15:00:52 +0100 Subject: [PATCH] drivers/pcie: Add a function to know MSI/MSI-x support of an endpoint And since it does yet another round of pcie_get_cap() on PCIE_MSI_CAP_ID and PCIE_MSIX_CAP_ID, let's factorize that into a utility function and change the relevant places to use that function instead. Signed-off-by: Tomasz Bursztyka --- drivers/pcie/host/msi.c | 78 +++++++++++++++++++------------------- include/drivers/pcie/msi.h | 8 ++++ 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/drivers/pcie/host/msi.c b/drivers/pcie/host/msi.c index 2ec073e3402..303461e237e 100644 --- a/drivers/pcie/host/msi.c +++ b/drivers/pcie/host/msi.c @@ -10,6 +10,32 @@ /* functions documented in include/drivers/pcie/msi.h */ +static uint32_t pcie_msi_base(pcie_bdf_t bdf, bool *msi) +{ + uint32_t base; + + if (msi != NULL) { + *msi = true; + } + + base = pcie_get_cap(bdf, PCI_CAP_ID_MSI); + + if (IS_ENABLED(CONFIG_PCIE_MSI_X)) { + uint32_t base_msix; + + base_msix = pcie_get_cap(bdf, PCI_CAP_ID_MSIX); + if (base_msix != 0U) { + base = base_msix; + + if (msi != NULL) { + *msi = false; + } + } + } + + return base; +} + #ifdef CONFIG_PCIE_MSI_MULTI_VECTOR #include @@ -115,21 +141,11 @@ uint8_t pcie_msi_vectors_allocate(pcie_bdf_t bdf, msi_vector_t *vectors, uint8_t n_vector) { - bool msi = true; - uint32_t base; uint32_t req_vectors; + uint32_t base; + bool msi; - base = pcie_get_cap(bdf, PCI_CAP_ID_MSI); - - if (IS_ENABLED(CONFIG_PCIE_MSI_X)) { - uint32_t base_msix; - - base_msix = pcie_get_cap(bdf, PCI_CAP_ID_MSIX); - if (base_msix != 0U) { - msi = false; - base = base_msix; - } - } + base = pcie_msi_base(bdf, &msi); if (IS_ENABLED(CONFIG_PCIE_MSI_X)) { set_msix(vectors, n_vector, !msi); @@ -162,17 +178,7 @@ bool pcie_msi_vector_connect(pcie_bdf_t bdf, { uint32_t base; - base = pcie_get_cap(bdf, PCI_CAP_ID_MSI); - - if (IS_ENABLED(CONFIG_PCIE_MSI_X)) { - uint32_t base_msix; - - base_msix = pcie_get_cap(bdf, PCI_CAP_ID_MSIX); - if (base_msix != 0U) { - base = base_msix; - } - } - + base = pcie_msi_base(bdf, NULL); if (base == 0U) { return false; } @@ -263,29 +269,16 @@ bool pcie_msi_enable(pcie_bdf_t bdf, uint8_t n_vector, unsigned int irq) { - bool msi = true; uint32_t base; + bool msi; - base = pcie_get_cap(bdf, PCI_CAP_ID_MSI); - - if (IS_ENABLED(CONFIG_PCIE_MSI_X)) { - uint32_t base_msix; - - base_msix = pcie_get_cap(bdf, PCI_CAP_ID_MSIX); - if ((base_msix != 0U) && (base != 0U)) { - disable_msi(bdf, base); - } - if ((base_msix != 0U)) { - msi = false; - base = base_msix; - } - } - + base = pcie_msi_base(bdf, &msi); if (base == 0U) { return false; } if (!msi && IS_ENABLED(CONFIG_PCIE_MSI_X)) { + disable_msi(bdf, base); enable_msix(bdf, vectors, n_vector, base, irq); } else { enable_msi(bdf, vectors, n_vector, base, irq); @@ -295,3 +288,8 @@ bool pcie_msi_enable(pcie_bdf_t bdf, return true; } + +bool pcie_is_msi(pcie_bdf_t bdf) +{ + return (pcie_msi_base(bdf, NULL) != 0); +} diff --git a/include/drivers/pcie/msi.h b/include/drivers/pcie/msi.h index ba08e60c4fd..d1bcb2760ea 100644 --- a/include/drivers/pcie/msi.h +++ b/include/drivers/pcie/msi.h @@ -109,6 +109,14 @@ extern bool pcie_msi_enable(pcie_bdf_t bdf, uint8_t n_vector, unsigned int irq); +/** + * @brief Check if the given PCI endpoint supports MSI/MSI-X + * + * @param bdf the target PCI endpoint + * @return true if the endpoint support MSI/MSI-X + */ +extern bool pcie_is_msi(pcie_bdf_t bdf); + /* * The first word of the MSI capability is shared with the * capability ID and list link. The high 16 bits are the MCR.