From fb2f686c68a8de5229aacc63e6ad91977be7c274 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Fri, 11 Nov 2022 13:15:55 +0200 Subject: [PATCH] drivers: virtualization: ivshmem: Convert to use dynamic BDF lookup Use the new PCIe core infrastructure for looking up the BDF at runtime based on the VID/DID values. Signed-off-by: Johan Hedberg --- drivers/virtualization/virt_ivshmem.c | 32 +++++++++---------- drivers/virtualization/virt_ivshmem.h | 3 +- dts/bindings/virtualization/qemu,ivshmem.yaml | 2 +- .../virtualization/ivshmem/app.overlay | 6 ++-- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/drivers/virtualization/virt_ivshmem.c b/drivers/virtualization/virt_ivshmem.c index 3bc06d8409e..3377692341e 100644 --- a/drivers/virtualization/virt_ivshmem.c +++ b/drivers/virtualization/virt_ivshmem.c @@ -45,7 +45,7 @@ static bool ivshmem_configure_interrupts(const struct device *dev) key = irq_lock(); - n_vectors = pcie_msi_vectors_allocate(data->bdf, + n_vectors = pcie_msi_vectors_allocate(data->pcie->bdf, CONFIG_IVSHMEM_INT_PRIORITY, data->vectors, CONFIG_IVSHMEM_MSI_X_VECTORS); @@ -61,7 +61,7 @@ static bool ivshmem_configure_interrupts(const struct device *dev) data->params[i].dev = dev; data->params[i].vector = i; - if (!pcie_msi_vector_connect(data->bdf, + if (!pcie_msi_vector_connect(data->pcie->bdf, &data->vectors[i], ivshmem_doorbell, &data->params[i], 0)) { @@ -72,7 +72,7 @@ static bool ivshmem_configure_interrupts(const struct device *dev) LOG_DBG("%u MSI-X Vectors connected", n_vectors); - if (!pcie_msi_enable(data->bdf, data->vectors, n_vectors, 0)) { + if (!pcie_msi_enable(data->pcie->bdf, data->vectors, n_vectors, 0)) { LOG_ERR("Could not enable MSI-X"); goto out; } @@ -110,7 +110,7 @@ static bool ivshmem_configure(const struct device *dev) struct ivshmem *data = dev->data; struct pcie_bar mbar_regs, mbar_mem; - if (!pcie_get_mbar(data->bdf, IVSHMEM_PCIE_REG_BAR_IDX, &mbar_regs)) { + if (!pcie_get_mbar(data->pcie->bdf, IVSHMEM_PCIE_REG_BAR_IDX, &mbar_regs)) { #ifdef CONFIG_IVSHMEM_DOORBELL LOG_ERR("ivshmem regs bar not found"); return false; @@ -120,13 +120,13 @@ static bool ivshmem_configure(const struct device *dev) sizeof(struct ivshmem_reg), K_MEM_CACHE_NONE); #endif /* CONFIG_IVSHMEM_DOORBELL */ } else { - pcie_set_cmd(data->bdf, PCIE_CONF_CMDSTAT_MEM, true); + pcie_set_cmd(data->pcie->bdf, PCIE_CONF_CMDSTAT_MEM, true); device_map(DEVICE_MMIO_RAM_PTR(dev), mbar_regs.phys_addr, mbar_regs.size, K_MEM_CACHE_NONE); } - if (!pcie_get_mbar(data->bdf, IVSHMEM_PCIE_SHMEM_BAR_IDX, &mbar_mem)) { + if (!pcie_get_mbar(data->pcie->bdf, IVSHMEM_PCIE_SHMEM_BAR_IDX, &mbar_mem)) { LOG_ERR("ivshmem mem bar not found"); return false; } @@ -227,24 +227,24 @@ static int ivshmem_init(const struct device *dev) struct ivshmem *data = dev->data; static bool bdf_lookup_done; - if ((data->bdf == PCIE_BDF_NONE) && bdf_lookup_done) { + if ((data->pcie->bdf == PCIE_BDF_NONE) && bdf_lookup_done) { LOG_ERR("One instance of ivshmem with pcie_bdf_lookup() already initialized.\n" "Using more than one with PCIE_BDF_NONE parameter might conflict\n" "with already initialized instances."); return -ENOTSUP; } - if ((data->bdf == PCIE_BDF_NONE) && !bdf_lookup_done) { - if (data->dev_ven_id) { - data->bdf = pcie_bdf_lookup(data->dev_ven_id); - } else { - data->bdf = pcie_bdf_lookup(PCIE_ID(IVSHMEM_VENDOR_ID, IVSHMEM_DEVICE_ID)); + if ((data->pcie->bdf == PCIE_BDF_NONE) && !bdf_lookup_done) { + if (data->pcie->id == PCIE_ID_NONE) { + data->pcie->id = PCIE_ID(IVSHMEM_VENDOR_ID, + IVSHMEM_DEVICE_ID); + data->pcie->bdf = pcie_bdf_lookup(data->pcie->id); } - if (data->bdf == PCIE_BDF_NONE) { + if (data->pcie->bdf == PCIE_BDF_NONE) { LOG_WRN("ivshmem device not found"); return -ENOTSUP; } } - LOG_DBG("ivshmem found at bdf 0x%x", data->bdf); + LOG_DBG("ivshmem found at bdf 0x%x", data->pcie->bdf); bdf_lookup_done = true; if (!ivshmem_configure(dev)) { @@ -254,9 +254,9 @@ static int ivshmem_init(const struct device *dev) } #define IVSHMEM_DEVICE_INIT(n) \ + DEVICE_PCIE_INST_DECLARE(n); \ static struct ivshmem ivshmem_data_##n = { \ - .bdf = DT_INST_REG_ADDR_BY_IDX(n, 0), \ - .dev_ven_id = DT_INST_REG_SIZE_BY_IDX(n, 0) \ + DEVICE_PCIE_INST_INIT(n, pcie), \ }; \ DEVICE_DT_INST_DEFINE(n, &ivshmem_init, NULL, \ &ivshmem_data_##n, NULL, \ diff --git a/drivers/virtualization/virt_ivshmem.h b/drivers/virtualization/virt_ivshmem.h index 9eef358864f..0059ad66624 100644 --- a/drivers/virtualization/virt_ivshmem.h +++ b/drivers/virtualization/virt_ivshmem.h @@ -24,8 +24,7 @@ struct ivshmem_param { struct ivshmem { DEVICE_MMIO_RAM; - pcie_bdf_t bdf; - uint32_t dev_ven_id; + struct pcie_dev *pcie; uintptr_t shmem; size_t size; #ifdef CONFIG_IVSHMEM_DOORBELL diff --git a/dts/bindings/virtualization/qemu,ivshmem.yaml b/dts/bindings/virtualization/qemu,ivshmem.yaml index 22095389a9b..bd841339f1a 100644 --- a/dts/bindings/virtualization/qemu,ivshmem.yaml +++ b/dts/bindings/virtualization/qemu,ivshmem.yaml @@ -5,4 +5,4 @@ description: ivShMem device properties compatible: "qemu,ivshmem" -include: base.yaml +include: [base.yaml, pcie-device.yaml] diff --git a/tests/drivers/virtualization/ivshmem/app.overlay b/tests/drivers/virtualization/ivshmem/app.overlay index 688f29e0531..6afb2993ae3 100644 --- a/tests/drivers/virtualization/ivshmem/app.overlay +++ b/tests/drivers/virtualization/ivshmem/app.overlay @@ -12,10 +12,12 @@ compatible = "intel,pcie"; ranges; - ivshmem0: ivshmem@800 { + ivshmem0: ivshmem0 { compatible = "qemu,ivshmem"; - reg = ; + vendor-id = <0x1af4>; + device-id = <0x1110>; + status = "okay"; }; };