[PATCH 07/11] vfio-pci: move igd initialization to vfio_pci.c

From: Max Gurtovoy
Date: Thu Jun 03 2021 - 12:09:01 EST


This is a preparation before splitting vfio_pci.ko to 2 drivers. Move
the vendor specific igd initialization from the core part to pci_driver
part.

Signed-off-by: Max Gurtovoy <mgurtovoy@xxxxxxxxxx>
---
drivers/vfio/pci/vfio_pci.c | 31 ++++++++++++++++++++-
drivers/vfio/pci/vfio_pci_core.c | 46 +++++++++++---------------------
drivers/vfio/pci/vfio_pci_core.h | 8 ++++++
3 files changed, 54 insertions(+), 31 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 23a21ecbc674..850ea3a94e28 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -83,9 +83,38 @@ static bool vfio_pci_is_denylisted(struct pci_dev *pdev)
return true;
}

+static int vfio_pci_open(struct vfio_device *core_vdev)
+{
+ struct vfio_pci_core_device *vdev =
+ container_of(core_vdev, struct vfio_pci_core_device, vdev);
+ struct pci_dev *pdev = vdev->pdev;
+ int ret;
+
+ lockdep_assert_held(&core_vdev->reflck->lock);
+
+ ret = vfio_pci_core_enable(vdev);
+ if (ret)
+ return ret;
+
+ if (vfio_pci_is_vga(pdev) &&
+ pdev->vendor == PCI_VENDOR_ID_INTEL &&
+ IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
+ ret = vfio_pci_igd_init(vdev);
+ if (ret && ret != -ENODEV) {
+ pci_warn(pdev, "Failed to setup Intel IGD regions\n");
+ vfio_pci_core_disable(vdev);
+ return ret;
+ }
+ }
+
+ vfio_pci_core_finish_enable(vdev);
+
+ return 0;
+}
+
static const struct vfio_device_ops vfio_pci_ops = {
.name = "vfio-pci",
- .open = vfio_pci_core_open,
+ .open = vfio_pci_open,
.release = vfio_pci_core_release,
.ioctl = vfio_pci_core_ioctl,
.read = vfio_pci_core_read,
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 12d5392c78cc..39a3f18bbc08 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -91,11 +91,6 @@ static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga)
return decodes;
}

-static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
-{
- return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
-}
-
static void vfio_pci_probe_mmaps(struct vfio_pci_core_device *vdev)
{
struct resource *res;
@@ -165,7 +160,6 @@ static void vfio_pci_probe_mmaps(struct vfio_pci_core_device *vdev)
}

static void vfio_pci_try_bus_reset(struct vfio_pci_core_device *vdev);
-static void vfio_pci_disable(struct vfio_pci_core_device *vdev);
static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data);

/*
@@ -250,7 +244,7 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat
return ret;
}

-static int vfio_pci_enable(struct vfio_pci_core_device *vdev)
+int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
{
struct pci_dev *pdev = vdev->pdev;
int ret;
@@ -319,26 +313,11 @@ static int vfio_pci_enable(struct vfio_pci_core_device *vdev)
if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
vdev->has_vga = true;

- if (vfio_pci_is_vga(pdev) &&
- pdev->vendor == PCI_VENDOR_ID_INTEL &&
- IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
- ret = vfio_pci_igd_init(vdev);
- if (ret && ret != -ENODEV) {
- pci_warn(pdev, "Failed to setup Intel IGD regions\n");
- goto disable_exit;
- }
- }
-
- vfio_pci_probe_mmaps(vdev);

return 0;
-
-disable_exit:
- vfio_pci_disable(vdev);
- return ret;
}

-static void vfio_pci_disable(struct vfio_pci_core_device *vdev)
+void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
{
struct pci_dev *pdev = vdev->pdev;
struct vfio_pci_dummy_resource *dummy_res, *tmp;
@@ -481,7 +460,7 @@ void vfio_pci_core_release(struct vfio_device *core_vdev)

vfio_pci_vf_token_user_add(vdev, -1);
vfio_spapr_pci_eeh_release(vdev->pdev);
- vfio_pci_disable(vdev);
+ vfio_pci_core_disable(vdev);

mutex_lock(&vdev->igate);
if (vdev->err_trigger) {
@@ -495,6 +474,13 @@ void vfio_pci_core_release(struct vfio_device *core_vdev)
mutex_unlock(&vdev->igate);
}

+void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev)
+{
+ vfio_pci_probe_mmaps(vdev);
+ vfio_spapr_pci_eeh_open(vdev->pdev);
+ vfio_pci_vf_token_user_add(vdev, 1);
+}
+
int vfio_pci_core_open(struct vfio_device *core_vdev)
{
struct vfio_pci_core_device *vdev =
@@ -503,13 +489,13 @@ int vfio_pci_core_open(struct vfio_device *core_vdev)

lockdep_assert_held(&core_vdev->reflck->lock);

- ret = vfio_pci_enable(vdev);
+ ret = vfio_pci_core_enable(vdev);
if (ret)
- goto error;
- vfio_spapr_pci_eeh_open(vdev->pdev);
- vfio_pci_vf_token_user_add(vdev, 1);
-error:
- return ret;
+ return ret;
+
+ vfio_pci_core_finish_enable(vdev);
+
+ return 0;
}

static int vfio_pci_get_irq_count(struct vfio_pci_core_device *vdev, int irq_type)
diff --git a/drivers/vfio/pci/vfio_pci_core.h b/drivers/vfio/pci/vfio_pci_core.h
index 245862d5d6e4..406e934e23b2 100644
--- a/drivers/vfio/pci/vfio_pci_core.h
+++ b/drivers/vfio/pci/vfio_pci_core.h
@@ -227,5 +227,13 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
int vfio_pci_core_reflck_attach(struct vfio_device *core_vdev);
+int vfio_pci_core_enable(struct vfio_pci_core_device *vdev);
+void vfio_pci_core_disable(struct vfio_pci_core_device *vdev);
+void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);
+
+static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
+{
+ return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
+}

#endif /* VFIO_PCI_CORE_H */
--
2.21.0