[PATCH] vfio_pci: Wake device to D0 before resets
From: Alex Williamson
Date: Thu Aug 12 2021 - 13:37:38 EST
pci_pm_reset() actually depends on the device starting in the D0 power
state, therefore any time we're using a flavor of pci_reset_function()
we should make sure the device is fully powered-up in case the PM reset
method is used.
It's not uncommon that shutdown of a VM will put the device into a D3
state such that vfio_pci_disable() is managing a device in this low
power state. The reset state of a device is the D0 power state, so
it's also reasonable to put the device into this state prior to reset.
Signed-off-by: Alex Williamson <alex.williamson@xxxxxxxxxx>
---
drivers/vfio/pci/vfio_pci.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index a4f44ea52fa3..e3faa1eb1a8c 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -454,6 +454,15 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev)
vdev->needs_reset = true;
+ /*
+ * Userspace may have left the device in a low power state which
+ * affects our ability to trigger a PM reset, restore to D0 and
+ * toss any saved state from the previous session.
+ */
+ pci_set_power_state(pdev, PCI_D0);
+ kfree(vdev->pm_save);
+ vdev->pm_save = NULL;
+
/*
* If we have saved state, restore it. If we can reset the device,
* even better. Resetting with current state seems better than
@@ -1013,6 +1022,9 @@ static long vfio_pci_ioctl(struct vfio_device *core_vdev,
if (!vdev->reset_works)
return -EINVAL;
+ /* PM reset depends on the device not already being in D3 */
+ vfio_pci_set_power_state(vdev, PCI_D0);
+
vfio_pci_zap_and_down_write_memory_lock(vdev);
ret = pci_try_reset_function(vdev->pdev);
up_write(&vdev->memory_lock);